x86.c 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. * Amit Shah <amit.shah@qumranet.com>
  14. * Ben-Ami Yassour <benami@il.ibm.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. #include <linux/kvm_host.h>
  21. #include "irq.h"
  22. #include "mmu.h"
  23. #include "i8254.h"
  24. #include "tss.h"
  25. #include "kvm_cache_regs.h"
  26. #include "x86.h"
  27. #include <linux/clocksource.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kvm.h>
  30. #include <linux/fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/module.h>
  33. #include <linux/mman.h>
  34. #include <linux/highmem.h>
  35. #include <linux/iommu.h>
  36. #include <linux/intel-iommu.h>
  37. #include <linux/cpufreq.h>
  38. #include <trace/events/kvm.h>
  39. #undef TRACE_INCLUDE_FILE
  40. #define CREATE_TRACE_POINTS
  41. #include "trace.h"
  42. #include <asm/uaccess.h>
  43. #include <asm/msr.h>
  44. #include <asm/desc.h>
  45. #include <asm/mtrr.h>
  46. #include <asm/mce.h>
  47. #define MAX_IO_MSRS 256
  48. #define CR0_RESERVED_BITS \
  49. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  50. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  51. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  52. #define CR4_RESERVED_BITS \
  53. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  54. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  55. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  56. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  57. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  58. #define KVM_MAX_MCE_BANKS 32
  59. #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
  60. /* EFER defaults:
  61. * - enable syscall per default because its emulated by KVM
  62. * - enable LME and LMA per default on 64 bit KVM
  63. */
  64. #ifdef CONFIG_X86_64
  65. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
  66. #else
  67. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
  68. #endif
  69. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  70. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  71. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  72. struct kvm_cpuid_entry2 __user *entries);
  73. struct kvm_x86_ops *kvm_x86_ops;
  74. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  75. int ignore_msrs = 0;
  76. module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
  77. struct kvm_stats_debugfs_item debugfs_entries[] = {
  78. { "pf_fixed", VCPU_STAT(pf_fixed) },
  79. { "pf_guest", VCPU_STAT(pf_guest) },
  80. { "tlb_flush", VCPU_STAT(tlb_flush) },
  81. { "invlpg", VCPU_STAT(invlpg) },
  82. { "exits", VCPU_STAT(exits) },
  83. { "io_exits", VCPU_STAT(io_exits) },
  84. { "mmio_exits", VCPU_STAT(mmio_exits) },
  85. { "signal_exits", VCPU_STAT(signal_exits) },
  86. { "irq_window", VCPU_STAT(irq_window_exits) },
  87. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  88. { "halt_exits", VCPU_STAT(halt_exits) },
  89. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  90. { "hypercalls", VCPU_STAT(hypercalls) },
  91. { "request_irq", VCPU_STAT(request_irq_exits) },
  92. { "irq_exits", VCPU_STAT(irq_exits) },
  93. { "host_state_reload", VCPU_STAT(host_state_reload) },
  94. { "efer_reload", VCPU_STAT(efer_reload) },
  95. { "fpu_reload", VCPU_STAT(fpu_reload) },
  96. { "insn_emulation", VCPU_STAT(insn_emulation) },
  97. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  98. { "irq_injections", VCPU_STAT(irq_injections) },
  99. { "nmi_injections", VCPU_STAT(nmi_injections) },
  100. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  101. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  102. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  103. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  104. { "mmu_flooded", VM_STAT(mmu_flooded) },
  105. { "mmu_recycled", VM_STAT(mmu_recycled) },
  106. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  107. { "mmu_unsync", VM_STAT(mmu_unsync) },
  108. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  109. { "largepages", VM_STAT(lpages) },
  110. { NULL }
  111. };
  112. unsigned long segment_base(u16 selector)
  113. {
  114. struct descriptor_table gdt;
  115. struct desc_struct *d;
  116. unsigned long table_base;
  117. unsigned long v;
  118. if (selector == 0)
  119. return 0;
  120. asm("sgdt %0" : "=m"(gdt));
  121. table_base = gdt.base;
  122. if (selector & 4) { /* from ldt */
  123. u16 ldt_selector;
  124. asm("sldt %0" : "=g"(ldt_selector));
  125. table_base = segment_base(ldt_selector);
  126. }
  127. d = (struct desc_struct *)(table_base + (selector & ~7));
  128. v = d->base0 | ((unsigned long)d->base1 << 16) |
  129. ((unsigned long)d->base2 << 24);
  130. #ifdef CONFIG_X86_64
  131. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  132. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  133. #endif
  134. return v;
  135. }
  136. EXPORT_SYMBOL_GPL(segment_base);
  137. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  138. {
  139. if (irqchip_in_kernel(vcpu->kvm))
  140. return vcpu->arch.apic_base;
  141. else
  142. return vcpu->arch.apic_base;
  143. }
  144. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  145. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  146. {
  147. /* TODO: reserve bits check */
  148. if (irqchip_in_kernel(vcpu->kvm))
  149. kvm_lapic_set_base(vcpu, data);
  150. else
  151. vcpu->arch.apic_base = data;
  152. }
  153. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  154. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  155. {
  156. WARN_ON(vcpu->arch.exception.pending);
  157. vcpu->arch.exception.pending = true;
  158. vcpu->arch.exception.has_error_code = false;
  159. vcpu->arch.exception.nr = nr;
  160. }
  161. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  162. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  163. u32 error_code)
  164. {
  165. ++vcpu->stat.pf_guest;
  166. if (vcpu->arch.exception.pending) {
  167. switch(vcpu->arch.exception.nr) {
  168. case DF_VECTOR:
  169. /* triple fault -> shutdown */
  170. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  171. return;
  172. case PF_VECTOR:
  173. vcpu->arch.exception.nr = DF_VECTOR;
  174. vcpu->arch.exception.error_code = 0;
  175. return;
  176. default:
  177. /* replace previous exception with a new one in a hope
  178. that instruction re-execution will regenerate lost
  179. exception */
  180. vcpu->arch.exception.pending = false;
  181. break;
  182. }
  183. }
  184. vcpu->arch.cr2 = addr;
  185. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  186. }
  187. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  188. {
  189. vcpu->arch.nmi_pending = 1;
  190. }
  191. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  192. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  193. {
  194. WARN_ON(vcpu->arch.exception.pending);
  195. vcpu->arch.exception.pending = true;
  196. vcpu->arch.exception.has_error_code = true;
  197. vcpu->arch.exception.nr = nr;
  198. vcpu->arch.exception.error_code = error_code;
  199. }
  200. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  201. static void __queue_exception(struct kvm_vcpu *vcpu)
  202. {
  203. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  204. vcpu->arch.exception.has_error_code,
  205. vcpu->arch.exception.error_code);
  206. }
  207. /*
  208. * Load the pae pdptrs. Return true is they are all valid.
  209. */
  210. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  211. {
  212. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  213. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  214. int i;
  215. int ret;
  216. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  217. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  218. offset * sizeof(u64), sizeof(pdpte));
  219. if (ret < 0) {
  220. ret = 0;
  221. goto out;
  222. }
  223. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  224. if (is_present_gpte(pdpte[i]) &&
  225. (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
  226. ret = 0;
  227. goto out;
  228. }
  229. }
  230. ret = 1;
  231. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  232. __set_bit(VCPU_EXREG_PDPTR,
  233. (unsigned long *)&vcpu->arch.regs_avail);
  234. __set_bit(VCPU_EXREG_PDPTR,
  235. (unsigned long *)&vcpu->arch.regs_dirty);
  236. out:
  237. return ret;
  238. }
  239. EXPORT_SYMBOL_GPL(load_pdptrs);
  240. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  241. {
  242. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  243. bool changed = true;
  244. int r;
  245. if (is_long_mode(vcpu) || !is_pae(vcpu))
  246. return false;
  247. if (!test_bit(VCPU_EXREG_PDPTR,
  248. (unsigned long *)&vcpu->arch.regs_avail))
  249. return true;
  250. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  251. if (r < 0)
  252. goto out;
  253. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  254. out:
  255. return changed;
  256. }
  257. void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  258. {
  259. if (cr0 & CR0_RESERVED_BITS) {
  260. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  261. cr0, vcpu->arch.cr0);
  262. kvm_inject_gp(vcpu, 0);
  263. return;
  264. }
  265. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  266. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  267. kvm_inject_gp(vcpu, 0);
  268. return;
  269. }
  270. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  271. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  272. "and a clear PE flag\n");
  273. kvm_inject_gp(vcpu, 0);
  274. return;
  275. }
  276. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  277. #ifdef CONFIG_X86_64
  278. if ((vcpu->arch.shadow_efer & EFER_LME)) {
  279. int cs_db, cs_l;
  280. if (!is_pae(vcpu)) {
  281. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  282. "in long mode while PAE is disabled\n");
  283. kvm_inject_gp(vcpu, 0);
  284. return;
  285. }
  286. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  287. if (cs_l) {
  288. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  289. "in long mode while CS.L == 1\n");
  290. kvm_inject_gp(vcpu, 0);
  291. return;
  292. }
  293. } else
  294. #endif
  295. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  296. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  297. "reserved bits\n");
  298. kvm_inject_gp(vcpu, 0);
  299. return;
  300. }
  301. }
  302. kvm_x86_ops->set_cr0(vcpu, cr0);
  303. vcpu->arch.cr0 = cr0;
  304. kvm_mmu_reset_context(vcpu);
  305. return;
  306. }
  307. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  308. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  309. {
  310. kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
  311. }
  312. EXPORT_SYMBOL_GPL(kvm_lmsw);
  313. void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  314. {
  315. unsigned long old_cr4 = vcpu->arch.cr4;
  316. unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
  317. if (cr4 & CR4_RESERVED_BITS) {
  318. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  319. kvm_inject_gp(vcpu, 0);
  320. return;
  321. }
  322. if (is_long_mode(vcpu)) {
  323. if (!(cr4 & X86_CR4_PAE)) {
  324. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  325. "in long mode\n");
  326. kvm_inject_gp(vcpu, 0);
  327. return;
  328. }
  329. } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
  330. && ((cr4 ^ old_cr4) & pdptr_bits)
  331. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  332. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  333. kvm_inject_gp(vcpu, 0);
  334. return;
  335. }
  336. if (cr4 & X86_CR4_VMXE) {
  337. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  338. kvm_inject_gp(vcpu, 0);
  339. return;
  340. }
  341. kvm_x86_ops->set_cr4(vcpu, cr4);
  342. vcpu->arch.cr4 = cr4;
  343. vcpu->arch.mmu.base_role.cr4_pge = (cr4 & X86_CR4_PGE) && !tdp_enabled;
  344. kvm_mmu_reset_context(vcpu);
  345. }
  346. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  347. void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  348. {
  349. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  350. kvm_mmu_sync_roots(vcpu);
  351. kvm_mmu_flush_tlb(vcpu);
  352. return;
  353. }
  354. if (is_long_mode(vcpu)) {
  355. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  356. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  357. kvm_inject_gp(vcpu, 0);
  358. return;
  359. }
  360. } else {
  361. if (is_pae(vcpu)) {
  362. if (cr3 & CR3_PAE_RESERVED_BITS) {
  363. printk(KERN_DEBUG
  364. "set_cr3: #GP, reserved bits\n");
  365. kvm_inject_gp(vcpu, 0);
  366. return;
  367. }
  368. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  369. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  370. "reserved bits\n");
  371. kvm_inject_gp(vcpu, 0);
  372. return;
  373. }
  374. }
  375. /*
  376. * We don't check reserved bits in nonpae mode, because
  377. * this isn't enforced, and VMware depends on this.
  378. */
  379. }
  380. /*
  381. * Does the new cr3 value map to physical memory? (Note, we
  382. * catch an invalid cr3 even in real-mode, because it would
  383. * cause trouble later on when we turn on paging anyway.)
  384. *
  385. * A real CPU would silently accept an invalid cr3 and would
  386. * attempt to use it - with largely undefined (and often hard
  387. * to debug) behavior on the guest side.
  388. */
  389. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  390. kvm_inject_gp(vcpu, 0);
  391. else {
  392. vcpu->arch.cr3 = cr3;
  393. vcpu->arch.mmu.new_cr3(vcpu);
  394. }
  395. }
  396. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  397. void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  398. {
  399. if (cr8 & CR8_RESERVED_BITS) {
  400. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  401. kvm_inject_gp(vcpu, 0);
  402. return;
  403. }
  404. if (irqchip_in_kernel(vcpu->kvm))
  405. kvm_lapic_set_tpr(vcpu, cr8);
  406. else
  407. vcpu->arch.cr8 = cr8;
  408. }
  409. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  410. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  411. {
  412. if (irqchip_in_kernel(vcpu->kvm))
  413. return kvm_lapic_get_cr8(vcpu);
  414. else
  415. return vcpu->arch.cr8;
  416. }
  417. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  418. static inline u32 bit(int bitno)
  419. {
  420. return 1 << (bitno & 31);
  421. }
  422. /*
  423. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  424. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  425. *
  426. * This list is modified at module load time to reflect the
  427. * capabilities of the host cpu.
  428. */
  429. static u32 msrs_to_save[] = {
  430. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  431. MSR_K6_STAR,
  432. #ifdef CONFIG_X86_64
  433. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  434. #endif
  435. MSR_IA32_TSC, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  436. MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
  437. };
  438. static unsigned num_msrs_to_save;
  439. static u32 emulated_msrs[] = {
  440. MSR_IA32_MISC_ENABLE,
  441. };
  442. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  443. {
  444. if (efer & efer_reserved_bits) {
  445. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  446. efer);
  447. kvm_inject_gp(vcpu, 0);
  448. return;
  449. }
  450. if (is_paging(vcpu)
  451. && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  452. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  453. kvm_inject_gp(vcpu, 0);
  454. return;
  455. }
  456. if (efer & EFER_FFXSR) {
  457. struct kvm_cpuid_entry2 *feat;
  458. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  459. if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT))) {
  460. printk(KERN_DEBUG "set_efer: #GP, enable FFXSR w/o CPUID capability\n");
  461. kvm_inject_gp(vcpu, 0);
  462. return;
  463. }
  464. }
  465. if (efer & EFER_SVME) {
  466. struct kvm_cpuid_entry2 *feat;
  467. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  468. if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM))) {
  469. printk(KERN_DEBUG "set_efer: #GP, enable SVM w/o SVM\n");
  470. kvm_inject_gp(vcpu, 0);
  471. return;
  472. }
  473. }
  474. kvm_x86_ops->set_efer(vcpu, efer);
  475. efer &= ~EFER_LMA;
  476. efer |= vcpu->arch.shadow_efer & EFER_LMA;
  477. vcpu->arch.shadow_efer = efer;
  478. vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
  479. kvm_mmu_reset_context(vcpu);
  480. }
  481. void kvm_enable_efer_bits(u64 mask)
  482. {
  483. efer_reserved_bits &= ~mask;
  484. }
  485. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  486. /*
  487. * Writes msr value into into the appropriate "register".
  488. * Returns 0 on success, non-0 otherwise.
  489. * Assumes vcpu_load() was already called.
  490. */
  491. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  492. {
  493. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  494. }
  495. /*
  496. * Adapt set_msr() to msr_io()'s calling convention
  497. */
  498. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  499. {
  500. return kvm_set_msr(vcpu, index, *data);
  501. }
  502. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  503. {
  504. static int version;
  505. struct pvclock_wall_clock wc;
  506. struct timespec now, sys, boot;
  507. if (!wall_clock)
  508. return;
  509. version++;
  510. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  511. /*
  512. * The guest calculates current wall clock time by adding
  513. * system time (updated by kvm_write_guest_time below) to the
  514. * wall clock specified here. guest system time equals host
  515. * system time for us, thus we must fill in host boot time here.
  516. */
  517. now = current_kernel_time();
  518. ktime_get_ts(&sys);
  519. boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
  520. wc.sec = boot.tv_sec;
  521. wc.nsec = boot.tv_nsec;
  522. wc.version = version;
  523. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  524. version++;
  525. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  526. }
  527. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  528. {
  529. uint32_t quotient, remainder;
  530. /* Don't try to replace with do_div(), this one calculates
  531. * "(dividend << 32) / divisor" */
  532. __asm__ ( "divl %4"
  533. : "=a" (quotient), "=d" (remainder)
  534. : "0" (0), "1" (dividend), "r" (divisor) );
  535. return quotient;
  536. }
  537. static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
  538. {
  539. uint64_t nsecs = 1000000000LL;
  540. int32_t shift = 0;
  541. uint64_t tps64;
  542. uint32_t tps32;
  543. tps64 = tsc_khz * 1000LL;
  544. while (tps64 > nsecs*2) {
  545. tps64 >>= 1;
  546. shift--;
  547. }
  548. tps32 = (uint32_t)tps64;
  549. while (tps32 <= (uint32_t)nsecs) {
  550. tps32 <<= 1;
  551. shift++;
  552. }
  553. hv_clock->tsc_shift = shift;
  554. hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
  555. pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
  556. __func__, tsc_khz, hv_clock->tsc_shift,
  557. hv_clock->tsc_to_system_mul);
  558. }
  559. static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
  560. static void kvm_write_guest_time(struct kvm_vcpu *v)
  561. {
  562. struct timespec ts;
  563. unsigned long flags;
  564. struct kvm_vcpu_arch *vcpu = &v->arch;
  565. void *shared_kaddr;
  566. unsigned long this_tsc_khz;
  567. if ((!vcpu->time_page))
  568. return;
  569. this_tsc_khz = get_cpu_var(cpu_tsc_khz);
  570. if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
  571. kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
  572. vcpu->hv_clock_tsc_khz = this_tsc_khz;
  573. }
  574. put_cpu_var(cpu_tsc_khz);
  575. /* Keep irq disabled to prevent changes to the clock */
  576. local_irq_save(flags);
  577. kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
  578. ktime_get_ts(&ts);
  579. local_irq_restore(flags);
  580. /* With all the info we got, fill in the values */
  581. vcpu->hv_clock.system_time = ts.tv_nsec +
  582. (NSEC_PER_SEC * (u64)ts.tv_sec);
  583. /*
  584. * The interface expects us to write an even number signaling that the
  585. * update is finished. Since the guest won't see the intermediate
  586. * state, we just increase by 2 at the end.
  587. */
  588. vcpu->hv_clock.version += 2;
  589. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  590. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  591. sizeof(vcpu->hv_clock));
  592. kunmap_atomic(shared_kaddr, KM_USER0);
  593. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  594. }
  595. static int kvm_request_guest_time_update(struct kvm_vcpu *v)
  596. {
  597. struct kvm_vcpu_arch *vcpu = &v->arch;
  598. if (!vcpu->time_page)
  599. return 0;
  600. set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
  601. return 1;
  602. }
  603. static bool msr_mtrr_valid(unsigned msr)
  604. {
  605. switch (msr) {
  606. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  607. case MSR_MTRRfix64K_00000:
  608. case MSR_MTRRfix16K_80000:
  609. case MSR_MTRRfix16K_A0000:
  610. case MSR_MTRRfix4K_C0000:
  611. case MSR_MTRRfix4K_C8000:
  612. case MSR_MTRRfix4K_D0000:
  613. case MSR_MTRRfix4K_D8000:
  614. case MSR_MTRRfix4K_E0000:
  615. case MSR_MTRRfix4K_E8000:
  616. case MSR_MTRRfix4K_F0000:
  617. case MSR_MTRRfix4K_F8000:
  618. case MSR_MTRRdefType:
  619. case MSR_IA32_CR_PAT:
  620. return true;
  621. case 0x2f8:
  622. return true;
  623. }
  624. return false;
  625. }
  626. static bool valid_pat_type(unsigned t)
  627. {
  628. return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
  629. }
  630. static bool valid_mtrr_type(unsigned t)
  631. {
  632. return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
  633. }
  634. static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  635. {
  636. int i;
  637. if (!msr_mtrr_valid(msr))
  638. return false;
  639. if (msr == MSR_IA32_CR_PAT) {
  640. for (i = 0; i < 8; i++)
  641. if (!valid_pat_type((data >> (i * 8)) & 0xff))
  642. return false;
  643. return true;
  644. } else if (msr == MSR_MTRRdefType) {
  645. if (data & ~0xcff)
  646. return false;
  647. return valid_mtrr_type(data & 0xff);
  648. } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
  649. for (i = 0; i < 8 ; i++)
  650. if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
  651. return false;
  652. return true;
  653. }
  654. /* variable MTRRs */
  655. return valid_mtrr_type(data & 0xff);
  656. }
  657. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  658. {
  659. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  660. if (!mtrr_valid(vcpu, msr, data))
  661. return 1;
  662. if (msr == MSR_MTRRdefType) {
  663. vcpu->arch.mtrr_state.def_type = data;
  664. vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
  665. } else if (msr == MSR_MTRRfix64K_00000)
  666. p[0] = data;
  667. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  668. p[1 + msr - MSR_MTRRfix16K_80000] = data;
  669. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  670. p[3 + msr - MSR_MTRRfix4K_C0000] = data;
  671. else if (msr == MSR_IA32_CR_PAT)
  672. vcpu->arch.pat = data;
  673. else { /* Variable MTRRs */
  674. int idx, is_mtrr_mask;
  675. u64 *pt;
  676. idx = (msr - 0x200) / 2;
  677. is_mtrr_mask = msr - 0x200 - 2 * idx;
  678. if (!is_mtrr_mask)
  679. pt =
  680. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  681. else
  682. pt =
  683. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  684. *pt = data;
  685. }
  686. kvm_mmu_reset_context(vcpu);
  687. return 0;
  688. }
  689. static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  690. {
  691. u64 mcg_cap = vcpu->arch.mcg_cap;
  692. unsigned bank_num = mcg_cap & 0xff;
  693. switch (msr) {
  694. case MSR_IA32_MCG_STATUS:
  695. vcpu->arch.mcg_status = data;
  696. break;
  697. case MSR_IA32_MCG_CTL:
  698. if (!(mcg_cap & MCG_CTL_P))
  699. return 1;
  700. if (data != 0 && data != ~(u64)0)
  701. return -1;
  702. vcpu->arch.mcg_ctl = data;
  703. break;
  704. default:
  705. if (msr >= MSR_IA32_MC0_CTL &&
  706. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  707. u32 offset = msr - MSR_IA32_MC0_CTL;
  708. /* only 0 or all 1s can be written to IA32_MCi_CTL */
  709. if ((offset & 0x3) == 0 &&
  710. data != 0 && data != ~(u64)0)
  711. return -1;
  712. vcpu->arch.mce_banks[offset] = data;
  713. break;
  714. }
  715. return 1;
  716. }
  717. return 0;
  718. }
  719. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  720. {
  721. switch (msr) {
  722. case MSR_EFER:
  723. set_efer(vcpu, data);
  724. break;
  725. case MSR_K7_HWCR:
  726. data &= ~(u64)0x40; /* ignore flush filter disable */
  727. if (data != 0) {
  728. pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
  729. data);
  730. return 1;
  731. }
  732. break;
  733. case MSR_FAM10H_MMIO_CONF_BASE:
  734. if (data != 0) {
  735. pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
  736. "0x%llx\n", data);
  737. return 1;
  738. }
  739. break;
  740. case MSR_AMD64_NB_CFG:
  741. break;
  742. case MSR_IA32_DEBUGCTLMSR:
  743. if (!data) {
  744. /* We support the non-activated case already */
  745. break;
  746. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  747. /* Values other than LBR and BTF are vendor-specific,
  748. thus reserved and should throw a #GP */
  749. return 1;
  750. }
  751. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  752. __func__, data);
  753. break;
  754. case MSR_IA32_UCODE_REV:
  755. case MSR_IA32_UCODE_WRITE:
  756. case MSR_VM_HSAVE_PA:
  757. case MSR_AMD64_PATCH_LOADER:
  758. break;
  759. case 0x200 ... 0x2ff:
  760. return set_msr_mtrr(vcpu, msr, data);
  761. case MSR_IA32_APICBASE:
  762. kvm_set_apic_base(vcpu, data);
  763. break;
  764. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  765. return kvm_x2apic_msr_write(vcpu, msr, data);
  766. case MSR_IA32_MISC_ENABLE:
  767. vcpu->arch.ia32_misc_enable_msr = data;
  768. break;
  769. case MSR_KVM_WALL_CLOCK:
  770. vcpu->kvm->arch.wall_clock = data;
  771. kvm_write_wall_clock(vcpu->kvm, data);
  772. break;
  773. case MSR_KVM_SYSTEM_TIME: {
  774. if (vcpu->arch.time_page) {
  775. kvm_release_page_dirty(vcpu->arch.time_page);
  776. vcpu->arch.time_page = NULL;
  777. }
  778. vcpu->arch.time = data;
  779. /* we verify if the enable bit is set... */
  780. if (!(data & 1))
  781. break;
  782. /* ...but clean it before doing the actual write */
  783. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  784. vcpu->arch.time_page =
  785. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  786. if (is_error_page(vcpu->arch.time_page)) {
  787. kvm_release_page_clean(vcpu->arch.time_page);
  788. vcpu->arch.time_page = NULL;
  789. }
  790. kvm_request_guest_time_update(vcpu);
  791. break;
  792. }
  793. case MSR_IA32_MCG_CTL:
  794. case MSR_IA32_MCG_STATUS:
  795. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  796. return set_msr_mce(vcpu, msr, data);
  797. /* Performance counters are not protected by a CPUID bit,
  798. * so we should check all of them in the generic path for the sake of
  799. * cross vendor migration.
  800. * Writing a zero into the event select MSRs disables them,
  801. * which we perfectly emulate ;-). Any other value should be at least
  802. * reported, some guests depend on them.
  803. */
  804. case MSR_P6_EVNTSEL0:
  805. case MSR_P6_EVNTSEL1:
  806. case MSR_K7_EVNTSEL0:
  807. case MSR_K7_EVNTSEL1:
  808. case MSR_K7_EVNTSEL2:
  809. case MSR_K7_EVNTSEL3:
  810. if (data != 0)
  811. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  812. "0x%x data 0x%llx\n", msr, data);
  813. break;
  814. /* at least RHEL 4 unconditionally writes to the perfctr registers,
  815. * so we ignore writes to make it happy.
  816. */
  817. case MSR_P6_PERFCTR0:
  818. case MSR_P6_PERFCTR1:
  819. case MSR_K7_PERFCTR0:
  820. case MSR_K7_PERFCTR1:
  821. case MSR_K7_PERFCTR2:
  822. case MSR_K7_PERFCTR3:
  823. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  824. "0x%x data 0x%llx\n", msr, data);
  825. break;
  826. default:
  827. if (!ignore_msrs) {
  828. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
  829. msr, data);
  830. return 1;
  831. } else {
  832. pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
  833. msr, data);
  834. break;
  835. }
  836. }
  837. return 0;
  838. }
  839. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  840. /*
  841. * Reads an msr value (of 'msr_index') into 'pdata'.
  842. * Returns 0 on success, non-0 otherwise.
  843. * Assumes vcpu_load() was already called.
  844. */
  845. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  846. {
  847. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  848. }
  849. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  850. {
  851. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  852. if (!msr_mtrr_valid(msr))
  853. return 1;
  854. if (msr == MSR_MTRRdefType)
  855. *pdata = vcpu->arch.mtrr_state.def_type +
  856. (vcpu->arch.mtrr_state.enabled << 10);
  857. else if (msr == MSR_MTRRfix64K_00000)
  858. *pdata = p[0];
  859. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  860. *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
  861. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  862. *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
  863. else if (msr == MSR_IA32_CR_PAT)
  864. *pdata = vcpu->arch.pat;
  865. else { /* Variable MTRRs */
  866. int idx, is_mtrr_mask;
  867. u64 *pt;
  868. idx = (msr - 0x200) / 2;
  869. is_mtrr_mask = msr - 0x200 - 2 * idx;
  870. if (!is_mtrr_mask)
  871. pt =
  872. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  873. else
  874. pt =
  875. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  876. *pdata = *pt;
  877. }
  878. return 0;
  879. }
  880. static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  881. {
  882. u64 data;
  883. u64 mcg_cap = vcpu->arch.mcg_cap;
  884. unsigned bank_num = mcg_cap & 0xff;
  885. switch (msr) {
  886. case MSR_IA32_P5_MC_ADDR:
  887. case MSR_IA32_P5_MC_TYPE:
  888. data = 0;
  889. break;
  890. case MSR_IA32_MCG_CAP:
  891. data = vcpu->arch.mcg_cap;
  892. break;
  893. case MSR_IA32_MCG_CTL:
  894. if (!(mcg_cap & MCG_CTL_P))
  895. return 1;
  896. data = vcpu->arch.mcg_ctl;
  897. break;
  898. case MSR_IA32_MCG_STATUS:
  899. data = vcpu->arch.mcg_status;
  900. break;
  901. default:
  902. if (msr >= MSR_IA32_MC0_CTL &&
  903. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  904. u32 offset = msr - MSR_IA32_MC0_CTL;
  905. data = vcpu->arch.mce_banks[offset];
  906. break;
  907. }
  908. return 1;
  909. }
  910. *pdata = data;
  911. return 0;
  912. }
  913. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  914. {
  915. u64 data;
  916. switch (msr) {
  917. case MSR_IA32_PLATFORM_ID:
  918. case MSR_IA32_UCODE_REV:
  919. case MSR_IA32_EBL_CR_POWERON:
  920. case MSR_IA32_DEBUGCTLMSR:
  921. case MSR_IA32_LASTBRANCHFROMIP:
  922. case MSR_IA32_LASTBRANCHTOIP:
  923. case MSR_IA32_LASTINTFROMIP:
  924. case MSR_IA32_LASTINTTOIP:
  925. case MSR_K8_SYSCFG:
  926. case MSR_K7_HWCR:
  927. case MSR_VM_HSAVE_PA:
  928. case MSR_P6_EVNTSEL0:
  929. case MSR_P6_EVNTSEL1:
  930. case MSR_K7_EVNTSEL0:
  931. case MSR_K8_INT_PENDING_MSG:
  932. case MSR_AMD64_NB_CFG:
  933. case MSR_FAM10H_MMIO_CONF_BASE:
  934. data = 0;
  935. break;
  936. case MSR_MTRRcap:
  937. data = 0x500 | KVM_NR_VAR_MTRR;
  938. break;
  939. case 0x200 ... 0x2ff:
  940. return get_msr_mtrr(vcpu, msr, pdata);
  941. case 0xcd: /* fsb frequency */
  942. data = 3;
  943. break;
  944. case MSR_IA32_APICBASE:
  945. data = kvm_get_apic_base(vcpu);
  946. break;
  947. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  948. return kvm_x2apic_msr_read(vcpu, msr, pdata);
  949. break;
  950. case MSR_IA32_MISC_ENABLE:
  951. data = vcpu->arch.ia32_misc_enable_msr;
  952. break;
  953. case MSR_IA32_PERF_STATUS:
  954. /* TSC increment by tick */
  955. data = 1000ULL;
  956. /* CPU multiplier */
  957. data |= (((uint64_t)4ULL) << 40);
  958. break;
  959. case MSR_EFER:
  960. data = vcpu->arch.shadow_efer;
  961. break;
  962. case MSR_KVM_WALL_CLOCK:
  963. data = vcpu->kvm->arch.wall_clock;
  964. break;
  965. case MSR_KVM_SYSTEM_TIME:
  966. data = vcpu->arch.time;
  967. break;
  968. case MSR_IA32_P5_MC_ADDR:
  969. case MSR_IA32_P5_MC_TYPE:
  970. case MSR_IA32_MCG_CAP:
  971. case MSR_IA32_MCG_CTL:
  972. case MSR_IA32_MCG_STATUS:
  973. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  974. return get_msr_mce(vcpu, msr, pdata);
  975. default:
  976. if (!ignore_msrs) {
  977. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  978. return 1;
  979. } else {
  980. pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
  981. data = 0;
  982. }
  983. break;
  984. }
  985. *pdata = data;
  986. return 0;
  987. }
  988. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  989. /*
  990. * Read or write a bunch of msrs. All parameters are kernel addresses.
  991. *
  992. * @return number of msrs set successfully.
  993. */
  994. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  995. struct kvm_msr_entry *entries,
  996. int (*do_msr)(struct kvm_vcpu *vcpu,
  997. unsigned index, u64 *data))
  998. {
  999. int i;
  1000. vcpu_load(vcpu);
  1001. down_read(&vcpu->kvm->slots_lock);
  1002. for (i = 0; i < msrs->nmsrs; ++i)
  1003. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1004. break;
  1005. up_read(&vcpu->kvm->slots_lock);
  1006. vcpu_put(vcpu);
  1007. return i;
  1008. }
  1009. /*
  1010. * Read or write a bunch of msrs. Parameters are user addresses.
  1011. *
  1012. * @return number of msrs set successfully.
  1013. */
  1014. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1015. int (*do_msr)(struct kvm_vcpu *vcpu,
  1016. unsigned index, u64 *data),
  1017. int writeback)
  1018. {
  1019. struct kvm_msrs msrs;
  1020. struct kvm_msr_entry *entries;
  1021. int r, n;
  1022. unsigned size;
  1023. r = -EFAULT;
  1024. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1025. goto out;
  1026. r = -E2BIG;
  1027. if (msrs.nmsrs >= MAX_IO_MSRS)
  1028. goto out;
  1029. r = -ENOMEM;
  1030. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1031. entries = vmalloc(size);
  1032. if (!entries)
  1033. goto out;
  1034. r = -EFAULT;
  1035. if (copy_from_user(entries, user_msrs->entries, size))
  1036. goto out_free;
  1037. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1038. if (r < 0)
  1039. goto out_free;
  1040. r = -EFAULT;
  1041. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1042. goto out_free;
  1043. r = n;
  1044. out_free:
  1045. vfree(entries);
  1046. out:
  1047. return r;
  1048. }
  1049. int kvm_dev_ioctl_check_extension(long ext)
  1050. {
  1051. int r;
  1052. switch (ext) {
  1053. case KVM_CAP_IRQCHIP:
  1054. case KVM_CAP_HLT:
  1055. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  1056. case KVM_CAP_SET_TSS_ADDR:
  1057. case KVM_CAP_EXT_CPUID:
  1058. case KVM_CAP_CLOCKSOURCE:
  1059. case KVM_CAP_PIT:
  1060. case KVM_CAP_NOP_IO_DELAY:
  1061. case KVM_CAP_MP_STATE:
  1062. case KVM_CAP_SYNC_MMU:
  1063. case KVM_CAP_REINJECT_CONTROL:
  1064. case KVM_CAP_IRQ_INJECT_STATUS:
  1065. case KVM_CAP_ASSIGN_DEV_IRQ:
  1066. case KVM_CAP_IRQFD:
  1067. case KVM_CAP_PIT2:
  1068. case KVM_CAP_PIT_STATE2:
  1069. r = 1;
  1070. break;
  1071. case KVM_CAP_COALESCED_MMIO:
  1072. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  1073. break;
  1074. case KVM_CAP_VAPIC:
  1075. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  1076. break;
  1077. case KVM_CAP_NR_VCPUS:
  1078. r = KVM_MAX_VCPUS;
  1079. break;
  1080. case KVM_CAP_NR_MEMSLOTS:
  1081. r = KVM_MEMORY_SLOTS;
  1082. break;
  1083. case KVM_CAP_PV_MMU:
  1084. r = !tdp_enabled;
  1085. break;
  1086. case KVM_CAP_IOMMU:
  1087. r = iommu_found();
  1088. break;
  1089. case KVM_CAP_MCE:
  1090. r = KVM_MAX_MCE_BANKS;
  1091. break;
  1092. default:
  1093. r = 0;
  1094. break;
  1095. }
  1096. return r;
  1097. }
  1098. long kvm_arch_dev_ioctl(struct file *filp,
  1099. unsigned int ioctl, unsigned long arg)
  1100. {
  1101. void __user *argp = (void __user *)arg;
  1102. long r;
  1103. switch (ioctl) {
  1104. case KVM_GET_MSR_INDEX_LIST: {
  1105. struct kvm_msr_list __user *user_msr_list = argp;
  1106. struct kvm_msr_list msr_list;
  1107. unsigned n;
  1108. r = -EFAULT;
  1109. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1110. goto out;
  1111. n = msr_list.nmsrs;
  1112. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1113. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1114. goto out;
  1115. r = -E2BIG;
  1116. if (n < msr_list.nmsrs)
  1117. goto out;
  1118. r = -EFAULT;
  1119. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1120. num_msrs_to_save * sizeof(u32)))
  1121. goto out;
  1122. if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
  1123. &emulated_msrs,
  1124. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1125. goto out;
  1126. r = 0;
  1127. break;
  1128. }
  1129. case KVM_GET_SUPPORTED_CPUID: {
  1130. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1131. struct kvm_cpuid2 cpuid;
  1132. r = -EFAULT;
  1133. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1134. goto out;
  1135. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1136. cpuid_arg->entries);
  1137. if (r)
  1138. goto out;
  1139. r = -EFAULT;
  1140. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1141. goto out;
  1142. r = 0;
  1143. break;
  1144. }
  1145. case KVM_X86_GET_MCE_CAP_SUPPORTED: {
  1146. u64 mce_cap;
  1147. mce_cap = KVM_MCE_CAP_SUPPORTED;
  1148. r = -EFAULT;
  1149. if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
  1150. goto out;
  1151. r = 0;
  1152. break;
  1153. }
  1154. default:
  1155. r = -EINVAL;
  1156. }
  1157. out:
  1158. return r;
  1159. }
  1160. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1161. {
  1162. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1163. kvm_request_guest_time_update(vcpu);
  1164. }
  1165. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1166. {
  1167. kvm_x86_ops->vcpu_put(vcpu);
  1168. kvm_put_guest_fpu(vcpu);
  1169. }
  1170. static int is_efer_nx(void)
  1171. {
  1172. unsigned long long efer = 0;
  1173. rdmsrl_safe(MSR_EFER, &efer);
  1174. return efer & EFER_NX;
  1175. }
  1176. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  1177. {
  1178. int i;
  1179. struct kvm_cpuid_entry2 *e, *entry;
  1180. entry = NULL;
  1181. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  1182. e = &vcpu->arch.cpuid_entries[i];
  1183. if (e->function == 0x80000001) {
  1184. entry = e;
  1185. break;
  1186. }
  1187. }
  1188. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  1189. entry->edx &= ~(1 << 20);
  1190. printk(KERN_INFO "kvm: guest NX capability removed\n");
  1191. }
  1192. }
  1193. /* when an old userspace process fills a new kernel module */
  1194. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1195. struct kvm_cpuid *cpuid,
  1196. struct kvm_cpuid_entry __user *entries)
  1197. {
  1198. int r, i;
  1199. struct kvm_cpuid_entry *cpuid_entries;
  1200. r = -E2BIG;
  1201. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1202. goto out;
  1203. r = -ENOMEM;
  1204. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  1205. if (!cpuid_entries)
  1206. goto out;
  1207. r = -EFAULT;
  1208. if (copy_from_user(cpuid_entries, entries,
  1209. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1210. goto out_free;
  1211. for (i = 0; i < cpuid->nent; i++) {
  1212. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  1213. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  1214. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  1215. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  1216. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  1217. vcpu->arch.cpuid_entries[i].index = 0;
  1218. vcpu->arch.cpuid_entries[i].flags = 0;
  1219. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  1220. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  1221. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  1222. }
  1223. vcpu->arch.cpuid_nent = cpuid->nent;
  1224. cpuid_fix_nx_cap(vcpu);
  1225. r = 0;
  1226. kvm_apic_set_version(vcpu);
  1227. out_free:
  1228. vfree(cpuid_entries);
  1229. out:
  1230. return r;
  1231. }
  1232. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  1233. struct kvm_cpuid2 *cpuid,
  1234. struct kvm_cpuid_entry2 __user *entries)
  1235. {
  1236. int r;
  1237. r = -E2BIG;
  1238. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1239. goto out;
  1240. r = -EFAULT;
  1241. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  1242. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  1243. goto out;
  1244. vcpu->arch.cpuid_nent = cpuid->nent;
  1245. kvm_apic_set_version(vcpu);
  1246. return 0;
  1247. out:
  1248. return r;
  1249. }
  1250. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  1251. struct kvm_cpuid2 *cpuid,
  1252. struct kvm_cpuid_entry2 __user *entries)
  1253. {
  1254. int r;
  1255. r = -E2BIG;
  1256. if (cpuid->nent < vcpu->arch.cpuid_nent)
  1257. goto out;
  1258. r = -EFAULT;
  1259. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  1260. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  1261. goto out;
  1262. return 0;
  1263. out:
  1264. cpuid->nent = vcpu->arch.cpuid_nent;
  1265. return r;
  1266. }
  1267. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1268. u32 index)
  1269. {
  1270. entry->function = function;
  1271. entry->index = index;
  1272. cpuid_count(entry->function, entry->index,
  1273. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  1274. entry->flags = 0;
  1275. }
  1276. #define F(x) bit(X86_FEATURE_##x)
  1277. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1278. u32 index, int *nent, int maxnent)
  1279. {
  1280. unsigned f_nx = is_efer_nx() ? F(NX) : 0;
  1281. #ifdef CONFIG_X86_64
  1282. unsigned f_lm = F(LM);
  1283. #else
  1284. unsigned f_lm = 0;
  1285. #endif
  1286. /* cpuid 1.edx */
  1287. const u32 kvm_supported_word0_x86_features =
  1288. F(FPU) | F(VME) | F(DE) | F(PSE) |
  1289. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  1290. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
  1291. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  1292. F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
  1293. 0 /* Reserved, DS, ACPI */ | F(MMX) |
  1294. F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
  1295. 0 /* HTT, TM, Reserved, PBE */;
  1296. /* cpuid 0x80000001.edx */
  1297. const u32 kvm_supported_word1_x86_features =
  1298. F(FPU) | F(VME) | F(DE) | F(PSE) |
  1299. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  1300. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
  1301. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  1302. F(PAT) | F(PSE36) | 0 /* Reserved */ |
  1303. f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
  1304. F(FXSR) | F(FXSR_OPT) | 0 /* GBPAGES */ | 0 /* RDTSCP */ |
  1305. 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
  1306. /* cpuid 1.ecx */
  1307. const u32 kvm_supported_word4_x86_features =
  1308. F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
  1309. 0 /* DS-CPL, VMX, SMX, EST */ |
  1310. 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
  1311. 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
  1312. 0 /* Reserved, DCA */ | F(XMM4_1) |
  1313. F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
  1314. 0 /* Reserved, XSAVE, OSXSAVE */;
  1315. /* cpuid 0x80000001.ecx */
  1316. const u32 kvm_supported_word6_x86_features =
  1317. F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
  1318. F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
  1319. F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
  1320. 0 /* SKINIT */ | 0 /* WDT */;
  1321. /* all calls to cpuid_count() should be made on the same cpu */
  1322. get_cpu();
  1323. do_cpuid_1_ent(entry, function, index);
  1324. ++*nent;
  1325. switch (function) {
  1326. case 0:
  1327. entry->eax = min(entry->eax, (u32)0xb);
  1328. break;
  1329. case 1:
  1330. entry->edx &= kvm_supported_word0_x86_features;
  1331. entry->ecx &= kvm_supported_word4_x86_features;
  1332. /* we support x2apic emulation even if host does not support
  1333. * it since we emulate x2apic in software */
  1334. entry->ecx |= F(X2APIC);
  1335. break;
  1336. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  1337. * may return different values. This forces us to get_cpu() before
  1338. * issuing the first command, and also to emulate this annoying behavior
  1339. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  1340. case 2: {
  1341. int t, times = entry->eax & 0xff;
  1342. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1343. entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  1344. for (t = 1; t < times && *nent < maxnent; ++t) {
  1345. do_cpuid_1_ent(&entry[t], function, 0);
  1346. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1347. ++*nent;
  1348. }
  1349. break;
  1350. }
  1351. /* function 4 and 0xb have additional index. */
  1352. case 4: {
  1353. int i, cache_type;
  1354. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1355. /* read more entries until cache_type is zero */
  1356. for (i = 1; *nent < maxnent; ++i) {
  1357. cache_type = entry[i - 1].eax & 0x1f;
  1358. if (!cache_type)
  1359. break;
  1360. do_cpuid_1_ent(&entry[i], function, i);
  1361. entry[i].flags |=
  1362. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1363. ++*nent;
  1364. }
  1365. break;
  1366. }
  1367. case 0xb: {
  1368. int i, level_type;
  1369. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1370. /* read more entries until level_type is zero */
  1371. for (i = 1; *nent < maxnent; ++i) {
  1372. level_type = entry[i - 1].ecx & 0xff00;
  1373. if (!level_type)
  1374. break;
  1375. do_cpuid_1_ent(&entry[i], function, i);
  1376. entry[i].flags |=
  1377. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1378. ++*nent;
  1379. }
  1380. break;
  1381. }
  1382. case 0x80000000:
  1383. entry->eax = min(entry->eax, 0x8000001a);
  1384. break;
  1385. case 0x80000001:
  1386. entry->edx &= kvm_supported_word1_x86_features;
  1387. entry->ecx &= kvm_supported_word6_x86_features;
  1388. break;
  1389. }
  1390. put_cpu();
  1391. }
  1392. #undef F
  1393. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  1394. struct kvm_cpuid_entry2 __user *entries)
  1395. {
  1396. struct kvm_cpuid_entry2 *cpuid_entries;
  1397. int limit, nent = 0, r = -E2BIG;
  1398. u32 func;
  1399. if (cpuid->nent < 1)
  1400. goto out;
  1401. r = -ENOMEM;
  1402. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  1403. if (!cpuid_entries)
  1404. goto out;
  1405. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  1406. limit = cpuid_entries[0].eax;
  1407. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  1408. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1409. &nent, cpuid->nent);
  1410. r = -E2BIG;
  1411. if (nent >= cpuid->nent)
  1412. goto out_free;
  1413. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  1414. limit = cpuid_entries[nent - 1].eax;
  1415. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  1416. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1417. &nent, cpuid->nent);
  1418. r = -E2BIG;
  1419. if (nent >= cpuid->nent)
  1420. goto out_free;
  1421. r = -EFAULT;
  1422. if (copy_to_user(entries, cpuid_entries,
  1423. nent * sizeof(struct kvm_cpuid_entry2)))
  1424. goto out_free;
  1425. cpuid->nent = nent;
  1426. r = 0;
  1427. out_free:
  1428. vfree(cpuid_entries);
  1429. out:
  1430. return r;
  1431. }
  1432. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1433. struct kvm_lapic_state *s)
  1434. {
  1435. vcpu_load(vcpu);
  1436. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1437. vcpu_put(vcpu);
  1438. return 0;
  1439. }
  1440. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1441. struct kvm_lapic_state *s)
  1442. {
  1443. vcpu_load(vcpu);
  1444. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1445. kvm_apic_post_state_restore(vcpu);
  1446. vcpu_put(vcpu);
  1447. return 0;
  1448. }
  1449. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1450. struct kvm_interrupt *irq)
  1451. {
  1452. if (irq->irq < 0 || irq->irq >= 256)
  1453. return -EINVAL;
  1454. if (irqchip_in_kernel(vcpu->kvm))
  1455. return -ENXIO;
  1456. vcpu_load(vcpu);
  1457. kvm_queue_interrupt(vcpu, irq->irq, false);
  1458. vcpu_put(vcpu);
  1459. return 0;
  1460. }
  1461. static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
  1462. {
  1463. vcpu_load(vcpu);
  1464. kvm_inject_nmi(vcpu);
  1465. vcpu_put(vcpu);
  1466. return 0;
  1467. }
  1468. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1469. struct kvm_tpr_access_ctl *tac)
  1470. {
  1471. if (tac->flags)
  1472. return -EINVAL;
  1473. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1474. return 0;
  1475. }
  1476. static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
  1477. u64 mcg_cap)
  1478. {
  1479. int r;
  1480. unsigned bank_num = mcg_cap & 0xff, bank;
  1481. r = -EINVAL;
  1482. if (!bank_num)
  1483. goto out;
  1484. if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
  1485. goto out;
  1486. r = 0;
  1487. vcpu->arch.mcg_cap = mcg_cap;
  1488. /* Init IA32_MCG_CTL to all 1s */
  1489. if (mcg_cap & MCG_CTL_P)
  1490. vcpu->arch.mcg_ctl = ~(u64)0;
  1491. /* Init IA32_MCi_CTL to all 1s */
  1492. for (bank = 0; bank < bank_num; bank++)
  1493. vcpu->arch.mce_banks[bank*4] = ~(u64)0;
  1494. out:
  1495. return r;
  1496. }
  1497. static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
  1498. struct kvm_x86_mce *mce)
  1499. {
  1500. u64 mcg_cap = vcpu->arch.mcg_cap;
  1501. unsigned bank_num = mcg_cap & 0xff;
  1502. u64 *banks = vcpu->arch.mce_banks;
  1503. if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
  1504. return -EINVAL;
  1505. /*
  1506. * if IA32_MCG_CTL is not all 1s, the uncorrected error
  1507. * reporting is disabled
  1508. */
  1509. if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
  1510. vcpu->arch.mcg_ctl != ~(u64)0)
  1511. return 0;
  1512. banks += 4 * mce->bank;
  1513. /*
  1514. * if IA32_MCi_CTL is not all 1s, the uncorrected error
  1515. * reporting is disabled for the bank
  1516. */
  1517. if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
  1518. return 0;
  1519. if (mce->status & MCI_STATUS_UC) {
  1520. if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
  1521. !(vcpu->arch.cr4 & X86_CR4_MCE)) {
  1522. printk(KERN_DEBUG "kvm: set_mce: "
  1523. "injects mce exception while "
  1524. "previous one is in progress!\n");
  1525. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  1526. return 0;
  1527. }
  1528. if (banks[1] & MCI_STATUS_VAL)
  1529. mce->status |= MCI_STATUS_OVER;
  1530. banks[2] = mce->addr;
  1531. banks[3] = mce->misc;
  1532. vcpu->arch.mcg_status = mce->mcg_status;
  1533. banks[1] = mce->status;
  1534. kvm_queue_exception(vcpu, MC_VECTOR);
  1535. } else if (!(banks[1] & MCI_STATUS_VAL)
  1536. || !(banks[1] & MCI_STATUS_UC)) {
  1537. if (banks[1] & MCI_STATUS_VAL)
  1538. mce->status |= MCI_STATUS_OVER;
  1539. banks[2] = mce->addr;
  1540. banks[3] = mce->misc;
  1541. banks[1] = mce->status;
  1542. } else
  1543. banks[1] |= MCI_STATUS_OVER;
  1544. return 0;
  1545. }
  1546. long kvm_arch_vcpu_ioctl(struct file *filp,
  1547. unsigned int ioctl, unsigned long arg)
  1548. {
  1549. struct kvm_vcpu *vcpu = filp->private_data;
  1550. void __user *argp = (void __user *)arg;
  1551. int r;
  1552. struct kvm_lapic_state *lapic = NULL;
  1553. switch (ioctl) {
  1554. case KVM_GET_LAPIC: {
  1555. lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1556. r = -ENOMEM;
  1557. if (!lapic)
  1558. goto out;
  1559. r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
  1560. if (r)
  1561. goto out;
  1562. r = -EFAULT;
  1563. if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
  1564. goto out;
  1565. r = 0;
  1566. break;
  1567. }
  1568. case KVM_SET_LAPIC: {
  1569. lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1570. r = -ENOMEM;
  1571. if (!lapic)
  1572. goto out;
  1573. r = -EFAULT;
  1574. if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
  1575. goto out;
  1576. r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
  1577. if (r)
  1578. goto out;
  1579. r = 0;
  1580. break;
  1581. }
  1582. case KVM_INTERRUPT: {
  1583. struct kvm_interrupt irq;
  1584. r = -EFAULT;
  1585. if (copy_from_user(&irq, argp, sizeof irq))
  1586. goto out;
  1587. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1588. if (r)
  1589. goto out;
  1590. r = 0;
  1591. break;
  1592. }
  1593. case KVM_NMI: {
  1594. r = kvm_vcpu_ioctl_nmi(vcpu);
  1595. if (r)
  1596. goto out;
  1597. r = 0;
  1598. break;
  1599. }
  1600. case KVM_SET_CPUID: {
  1601. struct kvm_cpuid __user *cpuid_arg = argp;
  1602. struct kvm_cpuid cpuid;
  1603. r = -EFAULT;
  1604. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1605. goto out;
  1606. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1607. if (r)
  1608. goto out;
  1609. break;
  1610. }
  1611. case KVM_SET_CPUID2: {
  1612. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1613. struct kvm_cpuid2 cpuid;
  1614. r = -EFAULT;
  1615. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1616. goto out;
  1617. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  1618. cpuid_arg->entries);
  1619. if (r)
  1620. goto out;
  1621. break;
  1622. }
  1623. case KVM_GET_CPUID2: {
  1624. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1625. struct kvm_cpuid2 cpuid;
  1626. r = -EFAULT;
  1627. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1628. goto out;
  1629. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  1630. cpuid_arg->entries);
  1631. if (r)
  1632. goto out;
  1633. r = -EFAULT;
  1634. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1635. goto out;
  1636. r = 0;
  1637. break;
  1638. }
  1639. case KVM_GET_MSRS:
  1640. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  1641. break;
  1642. case KVM_SET_MSRS:
  1643. r = msr_io(vcpu, argp, do_set_msr, 0);
  1644. break;
  1645. case KVM_TPR_ACCESS_REPORTING: {
  1646. struct kvm_tpr_access_ctl tac;
  1647. r = -EFAULT;
  1648. if (copy_from_user(&tac, argp, sizeof tac))
  1649. goto out;
  1650. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  1651. if (r)
  1652. goto out;
  1653. r = -EFAULT;
  1654. if (copy_to_user(argp, &tac, sizeof tac))
  1655. goto out;
  1656. r = 0;
  1657. break;
  1658. };
  1659. case KVM_SET_VAPIC_ADDR: {
  1660. struct kvm_vapic_addr va;
  1661. r = -EINVAL;
  1662. if (!irqchip_in_kernel(vcpu->kvm))
  1663. goto out;
  1664. r = -EFAULT;
  1665. if (copy_from_user(&va, argp, sizeof va))
  1666. goto out;
  1667. r = 0;
  1668. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  1669. break;
  1670. }
  1671. case KVM_X86_SETUP_MCE: {
  1672. u64 mcg_cap;
  1673. r = -EFAULT;
  1674. if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
  1675. goto out;
  1676. r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
  1677. break;
  1678. }
  1679. case KVM_X86_SET_MCE: {
  1680. struct kvm_x86_mce mce;
  1681. r = -EFAULT;
  1682. if (copy_from_user(&mce, argp, sizeof mce))
  1683. goto out;
  1684. r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
  1685. break;
  1686. }
  1687. default:
  1688. r = -EINVAL;
  1689. }
  1690. out:
  1691. kfree(lapic);
  1692. return r;
  1693. }
  1694. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  1695. {
  1696. int ret;
  1697. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  1698. return -1;
  1699. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  1700. return ret;
  1701. }
  1702. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  1703. u32 kvm_nr_mmu_pages)
  1704. {
  1705. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  1706. return -EINVAL;
  1707. down_write(&kvm->slots_lock);
  1708. spin_lock(&kvm->mmu_lock);
  1709. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  1710. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  1711. spin_unlock(&kvm->mmu_lock);
  1712. up_write(&kvm->slots_lock);
  1713. return 0;
  1714. }
  1715. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  1716. {
  1717. return kvm->arch.n_alloc_mmu_pages;
  1718. }
  1719. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  1720. {
  1721. int i;
  1722. struct kvm_mem_alias *alias;
  1723. for (i = 0; i < kvm->arch.naliases; ++i) {
  1724. alias = &kvm->arch.aliases[i];
  1725. if (gfn >= alias->base_gfn
  1726. && gfn < alias->base_gfn + alias->npages)
  1727. return alias->target_gfn + gfn - alias->base_gfn;
  1728. }
  1729. return gfn;
  1730. }
  1731. /*
  1732. * Set a new alias region. Aliases map a portion of physical memory into
  1733. * another portion. This is useful for memory windows, for example the PC
  1734. * VGA region.
  1735. */
  1736. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  1737. struct kvm_memory_alias *alias)
  1738. {
  1739. int r, n;
  1740. struct kvm_mem_alias *p;
  1741. r = -EINVAL;
  1742. /* General sanity checks */
  1743. if (alias->memory_size & (PAGE_SIZE - 1))
  1744. goto out;
  1745. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  1746. goto out;
  1747. if (alias->slot >= KVM_ALIAS_SLOTS)
  1748. goto out;
  1749. if (alias->guest_phys_addr + alias->memory_size
  1750. < alias->guest_phys_addr)
  1751. goto out;
  1752. if (alias->target_phys_addr + alias->memory_size
  1753. < alias->target_phys_addr)
  1754. goto out;
  1755. down_write(&kvm->slots_lock);
  1756. spin_lock(&kvm->mmu_lock);
  1757. p = &kvm->arch.aliases[alias->slot];
  1758. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  1759. p->npages = alias->memory_size >> PAGE_SHIFT;
  1760. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  1761. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  1762. if (kvm->arch.aliases[n - 1].npages)
  1763. break;
  1764. kvm->arch.naliases = n;
  1765. spin_unlock(&kvm->mmu_lock);
  1766. kvm_mmu_zap_all(kvm);
  1767. up_write(&kvm->slots_lock);
  1768. return 0;
  1769. out:
  1770. return r;
  1771. }
  1772. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1773. {
  1774. int r;
  1775. r = 0;
  1776. switch (chip->chip_id) {
  1777. case KVM_IRQCHIP_PIC_MASTER:
  1778. memcpy(&chip->chip.pic,
  1779. &pic_irqchip(kvm)->pics[0],
  1780. sizeof(struct kvm_pic_state));
  1781. break;
  1782. case KVM_IRQCHIP_PIC_SLAVE:
  1783. memcpy(&chip->chip.pic,
  1784. &pic_irqchip(kvm)->pics[1],
  1785. sizeof(struct kvm_pic_state));
  1786. break;
  1787. case KVM_IRQCHIP_IOAPIC:
  1788. memcpy(&chip->chip.ioapic,
  1789. ioapic_irqchip(kvm),
  1790. sizeof(struct kvm_ioapic_state));
  1791. break;
  1792. default:
  1793. r = -EINVAL;
  1794. break;
  1795. }
  1796. return r;
  1797. }
  1798. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1799. {
  1800. int r;
  1801. r = 0;
  1802. switch (chip->chip_id) {
  1803. case KVM_IRQCHIP_PIC_MASTER:
  1804. spin_lock(&pic_irqchip(kvm)->lock);
  1805. memcpy(&pic_irqchip(kvm)->pics[0],
  1806. &chip->chip.pic,
  1807. sizeof(struct kvm_pic_state));
  1808. spin_unlock(&pic_irqchip(kvm)->lock);
  1809. break;
  1810. case KVM_IRQCHIP_PIC_SLAVE:
  1811. spin_lock(&pic_irqchip(kvm)->lock);
  1812. memcpy(&pic_irqchip(kvm)->pics[1],
  1813. &chip->chip.pic,
  1814. sizeof(struct kvm_pic_state));
  1815. spin_unlock(&pic_irqchip(kvm)->lock);
  1816. break;
  1817. case KVM_IRQCHIP_IOAPIC:
  1818. mutex_lock(&kvm->irq_lock);
  1819. memcpy(ioapic_irqchip(kvm),
  1820. &chip->chip.ioapic,
  1821. sizeof(struct kvm_ioapic_state));
  1822. mutex_unlock(&kvm->irq_lock);
  1823. break;
  1824. default:
  1825. r = -EINVAL;
  1826. break;
  1827. }
  1828. kvm_pic_update_irq(pic_irqchip(kvm));
  1829. return r;
  1830. }
  1831. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1832. {
  1833. int r = 0;
  1834. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  1835. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  1836. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  1837. return r;
  1838. }
  1839. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1840. {
  1841. int r = 0;
  1842. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  1843. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  1844. kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
  1845. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  1846. return r;
  1847. }
  1848. static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  1849. {
  1850. int r = 0;
  1851. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  1852. memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
  1853. sizeof(ps->channels));
  1854. ps->flags = kvm->arch.vpit->pit_state.flags;
  1855. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  1856. return r;
  1857. }
  1858. static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  1859. {
  1860. int r = 0, start = 0;
  1861. u32 prev_legacy, cur_legacy;
  1862. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  1863. prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
  1864. cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
  1865. if (!prev_legacy && cur_legacy)
  1866. start = 1;
  1867. memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
  1868. sizeof(kvm->arch.vpit->pit_state.channels));
  1869. kvm->arch.vpit->pit_state.flags = ps->flags;
  1870. kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
  1871. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  1872. return r;
  1873. }
  1874. static int kvm_vm_ioctl_reinject(struct kvm *kvm,
  1875. struct kvm_reinject_control *control)
  1876. {
  1877. if (!kvm->arch.vpit)
  1878. return -ENXIO;
  1879. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  1880. kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
  1881. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  1882. return 0;
  1883. }
  1884. /*
  1885. * Get (and clear) the dirty memory log for a memory slot.
  1886. */
  1887. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1888. struct kvm_dirty_log *log)
  1889. {
  1890. int r;
  1891. int n;
  1892. struct kvm_memory_slot *memslot;
  1893. int is_dirty = 0;
  1894. down_write(&kvm->slots_lock);
  1895. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1896. if (r)
  1897. goto out;
  1898. /* If nothing is dirty, don't bother messing with page tables. */
  1899. if (is_dirty) {
  1900. spin_lock(&kvm->mmu_lock);
  1901. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  1902. spin_unlock(&kvm->mmu_lock);
  1903. kvm_flush_remote_tlbs(kvm);
  1904. memslot = &kvm->memslots[log->slot];
  1905. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  1906. memset(memslot->dirty_bitmap, 0, n);
  1907. }
  1908. r = 0;
  1909. out:
  1910. up_write(&kvm->slots_lock);
  1911. return r;
  1912. }
  1913. long kvm_arch_vm_ioctl(struct file *filp,
  1914. unsigned int ioctl, unsigned long arg)
  1915. {
  1916. struct kvm *kvm = filp->private_data;
  1917. void __user *argp = (void __user *)arg;
  1918. int r = -EINVAL;
  1919. /*
  1920. * This union makes it completely explicit to gcc-3.x
  1921. * that these two variables' stack usage should be
  1922. * combined, not added together.
  1923. */
  1924. union {
  1925. struct kvm_pit_state ps;
  1926. struct kvm_pit_state2 ps2;
  1927. struct kvm_memory_alias alias;
  1928. struct kvm_pit_config pit_config;
  1929. } u;
  1930. switch (ioctl) {
  1931. case KVM_SET_TSS_ADDR:
  1932. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  1933. if (r < 0)
  1934. goto out;
  1935. break;
  1936. case KVM_SET_MEMORY_REGION: {
  1937. struct kvm_memory_region kvm_mem;
  1938. struct kvm_userspace_memory_region kvm_userspace_mem;
  1939. r = -EFAULT;
  1940. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1941. goto out;
  1942. kvm_userspace_mem.slot = kvm_mem.slot;
  1943. kvm_userspace_mem.flags = kvm_mem.flags;
  1944. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  1945. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  1946. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1947. if (r)
  1948. goto out;
  1949. break;
  1950. }
  1951. case KVM_SET_NR_MMU_PAGES:
  1952. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  1953. if (r)
  1954. goto out;
  1955. break;
  1956. case KVM_GET_NR_MMU_PAGES:
  1957. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  1958. break;
  1959. case KVM_SET_MEMORY_ALIAS:
  1960. r = -EFAULT;
  1961. if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
  1962. goto out;
  1963. r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
  1964. if (r)
  1965. goto out;
  1966. break;
  1967. case KVM_CREATE_IRQCHIP:
  1968. r = -ENOMEM;
  1969. kvm->arch.vpic = kvm_create_pic(kvm);
  1970. if (kvm->arch.vpic) {
  1971. r = kvm_ioapic_init(kvm);
  1972. if (r) {
  1973. kfree(kvm->arch.vpic);
  1974. kvm->arch.vpic = NULL;
  1975. goto out;
  1976. }
  1977. } else
  1978. goto out;
  1979. r = kvm_setup_default_irq_routing(kvm);
  1980. if (r) {
  1981. kfree(kvm->arch.vpic);
  1982. kfree(kvm->arch.vioapic);
  1983. goto out;
  1984. }
  1985. break;
  1986. case KVM_CREATE_PIT:
  1987. u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
  1988. goto create_pit;
  1989. case KVM_CREATE_PIT2:
  1990. r = -EFAULT;
  1991. if (copy_from_user(&u.pit_config, argp,
  1992. sizeof(struct kvm_pit_config)))
  1993. goto out;
  1994. create_pit:
  1995. down_write(&kvm->slots_lock);
  1996. r = -EEXIST;
  1997. if (kvm->arch.vpit)
  1998. goto create_pit_unlock;
  1999. r = -ENOMEM;
  2000. kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
  2001. if (kvm->arch.vpit)
  2002. r = 0;
  2003. create_pit_unlock:
  2004. up_write(&kvm->slots_lock);
  2005. break;
  2006. case KVM_IRQ_LINE_STATUS:
  2007. case KVM_IRQ_LINE: {
  2008. struct kvm_irq_level irq_event;
  2009. r = -EFAULT;
  2010. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2011. goto out;
  2012. if (irqchip_in_kernel(kvm)) {
  2013. __s32 status;
  2014. mutex_lock(&kvm->irq_lock);
  2015. status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  2016. irq_event.irq, irq_event.level);
  2017. mutex_unlock(&kvm->irq_lock);
  2018. if (ioctl == KVM_IRQ_LINE_STATUS) {
  2019. irq_event.status = status;
  2020. if (copy_to_user(argp, &irq_event,
  2021. sizeof irq_event))
  2022. goto out;
  2023. }
  2024. r = 0;
  2025. }
  2026. break;
  2027. }
  2028. case KVM_GET_IRQCHIP: {
  2029. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2030. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  2031. r = -ENOMEM;
  2032. if (!chip)
  2033. goto out;
  2034. r = -EFAULT;
  2035. if (copy_from_user(chip, argp, sizeof *chip))
  2036. goto get_irqchip_out;
  2037. r = -ENXIO;
  2038. if (!irqchip_in_kernel(kvm))
  2039. goto get_irqchip_out;
  2040. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  2041. if (r)
  2042. goto get_irqchip_out;
  2043. r = -EFAULT;
  2044. if (copy_to_user(argp, chip, sizeof *chip))
  2045. goto get_irqchip_out;
  2046. r = 0;
  2047. get_irqchip_out:
  2048. kfree(chip);
  2049. if (r)
  2050. goto out;
  2051. break;
  2052. }
  2053. case KVM_SET_IRQCHIP: {
  2054. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2055. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  2056. r = -ENOMEM;
  2057. if (!chip)
  2058. goto out;
  2059. r = -EFAULT;
  2060. if (copy_from_user(chip, argp, sizeof *chip))
  2061. goto set_irqchip_out;
  2062. r = -ENXIO;
  2063. if (!irqchip_in_kernel(kvm))
  2064. goto set_irqchip_out;
  2065. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  2066. if (r)
  2067. goto set_irqchip_out;
  2068. r = 0;
  2069. set_irqchip_out:
  2070. kfree(chip);
  2071. if (r)
  2072. goto out;
  2073. break;
  2074. }
  2075. case KVM_GET_PIT: {
  2076. r = -EFAULT;
  2077. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  2078. goto out;
  2079. r = -ENXIO;
  2080. if (!kvm->arch.vpit)
  2081. goto out;
  2082. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  2083. if (r)
  2084. goto out;
  2085. r = -EFAULT;
  2086. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  2087. goto out;
  2088. r = 0;
  2089. break;
  2090. }
  2091. case KVM_SET_PIT: {
  2092. r = -EFAULT;
  2093. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  2094. goto out;
  2095. r = -ENXIO;
  2096. if (!kvm->arch.vpit)
  2097. goto out;
  2098. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  2099. if (r)
  2100. goto out;
  2101. r = 0;
  2102. break;
  2103. }
  2104. case KVM_GET_PIT2: {
  2105. r = -ENXIO;
  2106. if (!kvm->arch.vpit)
  2107. goto out;
  2108. r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
  2109. if (r)
  2110. goto out;
  2111. r = -EFAULT;
  2112. if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
  2113. goto out;
  2114. r = 0;
  2115. break;
  2116. }
  2117. case KVM_SET_PIT2: {
  2118. r = -EFAULT;
  2119. if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
  2120. goto out;
  2121. r = -ENXIO;
  2122. if (!kvm->arch.vpit)
  2123. goto out;
  2124. r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
  2125. if (r)
  2126. goto out;
  2127. r = 0;
  2128. break;
  2129. }
  2130. case KVM_REINJECT_CONTROL: {
  2131. struct kvm_reinject_control control;
  2132. r = -EFAULT;
  2133. if (copy_from_user(&control, argp, sizeof(control)))
  2134. goto out;
  2135. r = kvm_vm_ioctl_reinject(kvm, &control);
  2136. if (r)
  2137. goto out;
  2138. r = 0;
  2139. break;
  2140. }
  2141. default:
  2142. ;
  2143. }
  2144. out:
  2145. return r;
  2146. }
  2147. static void kvm_init_msr_list(void)
  2148. {
  2149. u32 dummy[2];
  2150. unsigned i, j;
  2151. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  2152. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  2153. continue;
  2154. if (j < i)
  2155. msrs_to_save[j] = msrs_to_save[i];
  2156. j++;
  2157. }
  2158. num_msrs_to_save = j;
  2159. }
  2160. static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
  2161. const void *v)
  2162. {
  2163. if (vcpu->arch.apic &&
  2164. !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
  2165. return 0;
  2166. return kvm_io_bus_write(&vcpu->kvm->mmio_bus, addr, len, v);
  2167. }
  2168. static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
  2169. {
  2170. if (vcpu->arch.apic &&
  2171. !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
  2172. return 0;
  2173. return kvm_io_bus_read(&vcpu->kvm->mmio_bus, addr, len, v);
  2174. }
  2175. static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
  2176. struct kvm_vcpu *vcpu)
  2177. {
  2178. void *data = val;
  2179. int r = X86EMUL_CONTINUE;
  2180. while (bytes) {
  2181. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2182. unsigned offset = addr & (PAGE_SIZE-1);
  2183. unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
  2184. int ret;
  2185. if (gpa == UNMAPPED_GVA) {
  2186. r = X86EMUL_PROPAGATE_FAULT;
  2187. goto out;
  2188. }
  2189. ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
  2190. if (ret < 0) {
  2191. r = X86EMUL_UNHANDLEABLE;
  2192. goto out;
  2193. }
  2194. bytes -= toread;
  2195. data += toread;
  2196. addr += toread;
  2197. }
  2198. out:
  2199. return r;
  2200. }
  2201. static int kvm_write_guest_virt(gva_t addr, void *val, unsigned int bytes,
  2202. struct kvm_vcpu *vcpu)
  2203. {
  2204. void *data = val;
  2205. int r = X86EMUL_CONTINUE;
  2206. while (bytes) {
  2207. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2208. unsigned offset = addr & (PAGE_SIZE-1);
  2209. unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
  2210. int ret;
  2211. if (gpa == UNMAPPED_GVA) {
  2212. r = X86EMUL_PROPAGATE_FAULT;
  2213. goto out;
  2214. }
  2215. ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
  2216. if (ret < 0) {
  2217. r = X86EMUL_UNHANDLEABLE;
  2218. goto out;
  2219. }
  2220. bytes -= towrite;
  2221. data += towrite;
  2222. addr += towrite;
  2223. }
  2224. out:
  2225. return r;
  2226. }
  2227. static int emulator_read_emulated(unsigned long addr,
  2228. void *val,
  2229. unsigned int bytes,
  2230. struct kvm_vcpu *vcpu)
  2231. {
  2232. gpa_t gpa;
  2233. if (vcpu->mmio_read_completed) {
  2234. memcpy(val, vcpu->mmio_data, bytes);
  2235. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
  2236. vcpu->mmio_phys_addr, *(u64 *)val);
  2237. vcpu->mmio_read_completed = 0;
  2238. return X86EMUL_CONTINUE;
  2239. }
  2240. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2241. /* For APIC access vmexit */
  2242. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2243. goto mmio;
  2244. if (kvm_read_guest_virt(addr, val, bytes, vcpu)
  2245. == X86EMUL_CONTINUE)
  2246. return X86EMUL_CONTINUE;
  2247. if (gpa == UNMAPPED_GVA)
  2248. return X86EMUL_PROPAGATE_FAULT;
  2249. mmio:
  2250. /*
  2251. * Is this MMIO handled locally?
  2252. */
  2253. if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
  2254. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
  2255. return X86EMUL_CONTINUE;
  2256. }
  2257. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
  2258. vcpu->mmio_needed = 1;
  2259. vcpu->mmio_phys_addr = gpa;
  2260. vcpu->mmio_size = bytes;
  2261. vcpu->mmio_is_write = 0;
  2262. return X86EMUL_UNHANDLEABLE;
  2263. }
  2264. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  2265. const void *val, int bytes)
  2266. {
  2267. int ret;
  2268. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  2269. if (ret < 0)
  2270. return 0;
  2271. kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
  2272. return 1;
  2273. }
  2274. static int emulator_write_emulated_onepage(unsigned long addr,
  2275. const void *val,
  2276. unsigned int bytes,
  2277. struct kvm_vcpu *vcpu)
  2278. {
  2279. gpa_t gpa;
  2280. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2281. if (gpa == UNMAPPED_GVA) {
  2282. kvm_inject_page_fault(vcpu, addr, 2);
  2283. return X86EMUL_PROPAGATE_FAULT;
  2284. }
  2285. /* For APIC access vmexit */
  2286. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2287. goto mmio;
  2288. if (emulator_write_phys(vcpu, gpa, val, bytes))
  2289. return X86EMUL_CONTINUE;
  2290. mmio:
  2291. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
  2292. /*
  2293. * Is this MMIO handled locally?
  2294. */
  2295. if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
  2296. return X86EMUL_CONTINUE;
  2297. vcpu->mmio_needed = 1;
  2298. vcpu->mmio_phys_addr = gpa;
  2299. vcpu->mmio_size = bytes;
  2300. vcpu->mmio_is_write = 1;
  2301. memcpy(vcpu->mmio_data, val, bytes);
  2302. return X86EMUL_CONTINUE;
  2303. }
  2304. int emulator_write_emulated(unsigned long addr,
  2305. const void *val,
  2306. unsigned int bytes,
  2307. struct kvm_vcpu *vcpu)
  2308. {
  2309. /* Crossing a page boundary? */
  2310. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  2311. int rc, now;
  2312. now = -addr & ~PAGE_MASK;
  2313. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  2314. if (rc != X86EMUL_CONTINUE)
  2315. return rc;
  2316. addr += now;
  2317. val += now;
  2318. bytes -= now;
  2319. }
  2320. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  2321. }
  2322. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  2323. static int emulator_cmpxchg_emulated(unsigned long addr,
  2324. const void *old,
  2325. const void *new,
  2326. unsigned int bytes,
  2327. struct kvm_vcpu *vcpu)
  2328. {
  2329. static int reported;
  2330. if (!reported) {
  2331. reported = 1;
  2332. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  2333. }
  2334. #ifndef CONFIG_X86_64
  2335. /* guests cmpxchg8b have to be emulated atomically */
  2336. if (bytes == 8) {
  2337. gpa_t gpa;
  2338. struct page *page;
  2339. char *kaddr;
  2340. u64 val;
  2341. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2342. if (gpa == UNMAPPED_GVA ||
  2343. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2344. goto emul_write;
  2345. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  2346. goto emul_write;
  2347. val = *(u64 *)new;
  2348. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  2349. kaddr = kmap_atomic(page, KM_USER0);
  2350. set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
  2351. kunmap_atomic(kaddr, KM_USER0);
  2352. kvm_release_page_dirty(page);
  2353. }
  2354. emul_write:
  2355. #endif
  2356. return emulator_write_emulated(addr, new, bytes, vcpu);
  2357. }
  2358. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2359. {
  2360. return kvm_x86_ops->get_segment_base(vcpu, seg);
  2361. }
  2362. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  2363. {
  2364. kvm_mmu_invlpg(vcpu, address);
  2365. return X86EMUL_CONTINUE;
  2366. }
  2367. int emulate_clts(struct kvm_vcpu *vcpu)
  2368. {
  2369. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
  2370. return X86EMUL_CONTINUE;
  2371. }
  2372. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  2373. {
  2374. struct kvm_vcpu *vcpu = ctxt->vcpu;
  2375. switch (dr) {
  2376. case 0 ... 3:
  2377. *dest = kvm_x86_ops->get_dr(vcpu, dr);
  2378. return X86EMUL_CONTINUE;
  2379. default:
  2380. pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
  2381. return X86EMUL_UNHANDLEABLE;
  2382. }
  2383. }
  2384. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  2385. {
  2386. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  2387. int exception;
  2388. kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  2389. if (exception) {
  2390. /* FIXME: better handling */
  2391. return X86EMUL_UNHANDLEABLE;
  2392. }
  2393. return X86EMUL_CONTINUE;
  2394. }
  2395. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  2396. {
  2397. u8 opcodes[4];
  2398. unsigned long rip = kvm_rip_read(vcpu);
  2399. unsigned long rip_linear;
  2400. if (!printk_ratelimit())
  2401. return;
  2402. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  2403. kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu);
  2404. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  2405. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  2406. }
  2407. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  2408. static struct x86_emulate_ops emulate_ops = {
  2409. .read_std = kvm_read_guest_virt,
  2410. .read_emulated = emulator_read_emulated,
  2411. .write_emulated = emulator_write_emulated,
  2412. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  2413. };
  2414. static void cache_all_regs(struct kvm_vcpu *vcpu)
  2415. {
  2416. kvm_register_read(vcpu, VCPU_REGS_RAX);
  2417. kvm_register_read(vcpu, VCPU_REGS_RSP);
  2418. kvm_register_read(vcpu, VCPU_REGS_RIP);
  2419. vcpu->arch.regs_dirty = ~0;
  2420. }
  2421. int emulate_instruction(struct kvm_vcpu *vcpu,
  2422. struct kvm_run *run,
  2423. unsigned long cr2,
  2424. u16 error_code,
  2425. int emulation_type)
  2426. {
  2427. int r, shadow_mask;
  2428. struct decode_cache *c;
  2429. kvm_clear_exception_queue(vcpu);
  2430. vcpu->arch.mmio_fault_cr2 = cr2;
  2431. /*
  2432. * TODO: fix x86_emulate.c to use guest_read/write_register
  2433. * instead of direct ->regs accesses, can save hundred cycles
  2434. * on Intel for instructions that don't read/change RSP, for
  2435. * for example.
  2436. */
  2437. cache_all_regs(vcpu);
  2438. vcpu->mmio_is_write = 0;
  2439. vcpu->arch.pio.string = 0;
  2440. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  2441. int cs_db, cs_l;
  2442. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  2443. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  2444. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  2445. vcpu->arch.emulate_ctxt.mode =
  2446. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  2447. ? X86EMUL_MODE_REAL : cs_l
  2448. ? X86EMUL_MODE_PROT64 : cs_db
  2449. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  2450. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2451. /* Only allow emulation of specific instructions on #UD
  2452. * (namely VMMCALL, sysenter, sysexit, syscall)*/
  2453. c = &vcpu->arch.emulate_ctxt.decode;
  2454. if (emulation_type & EMULTYPE_TRAP_UD) {
  2455. if (!c->twobyte)
  2456. return EMULATE_FAIL;
  2457. switch (c->b) {
  2458. case 0x01: /* VMMCALL */
  2459. if (c->modrm_mod != 3 || c->modrm_rm != 1)
  2460. return EMULATE_FAIL;
  2461. break;
  2462. case 0x34: /* sysenter */
  2463. case 0x35: /* sysexit */
  2464. if (c->modrm_mod != 0 || c->modrm_rm != 0)
  2465. return EMULATE_FAIL;
  2466. break;
  2467. case 0x05: /* syscall */
  2468. if (c->modrm_mod != 0 || c->modrm_rm != 0)
  2469. return EMULATE_FAIL;
  2470. break;
  2471. default:
  2472. return EMULATE_FAIL;
  2473. }
  2474. if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
  2475. return EMULATE_FAIL;
  2476. }
  2477. ++vcpu->stat.insn_emulation;
  2478. if (r) {
  2479. ++vcpu->stat.insn_emulation_fail;
  2480. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2481. return EMULATE_DONE;
  2482. return EMULATE_FAIL;
  2483. }
  2484. }
  2485. if (emulation_type & EMULTYPE_SKIP) {
  2486. kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
  2487. return EMULATE_DONE;
  2488. }
  2489. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2490. shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
  2491. if (r == 0)
  2492. kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
  2493. if (vcpu->arch.pio.string)
  2494. return EMULATE_DO_MMIO;
  2495. if ((r || vcpu->mmio_is_write) && run) {
  2496. run->exit_reason = KVM_EXIT_MMIO;
  2497. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  2498. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  2499. run->mmio.len = vcpu->mmio_size;
  2500. run->mmio.is_write = vcpu->mmio_is_write;
  2501. }
  2502. if (r) {
  2503. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2504. return EMULATE_DONE;
  2505. if (!vcpu->mmio_needed) {
  2506. kvm_report_emulation_failure(vcpu, "mmio");
  2507. return EMULATE_FAIL;
  2508. }
  2509. return EMULATE_DO_MMIO;
  2510. }
  2511. kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  2512. if (vcpu->mmio_is_write) {
  2513. vcpu->mmio_needed = 0;
  2514. return EMULATE_DO_MMIO;
  2515. }
  2516. return EMULATE_DONE;
  2517. }
  2518. EXPORT_SYMBOL_GPL(emulate_instruction);
  2519. static int pio_copy_data(struct kvm_vcpu *vcpu)
  2520. {
  2521. void *p = vcpu->arch.pio_data;
  2522. gva_t q = vcpu->arch.pio.guest_gva;
  2523. unsigned bytes;
  2524. int ret;
  2525. bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
  2526. if (vcpu->arch.pio.in)
  2527. ret = kvm_write_guest_virt(q, p, bytes, vcpu);
  2528. else
  2529. ret = kvm_read_guest_virt(q, p, bytes, vcpu);
  2530. return ret;
  2531. }
  2532. int complete_pio(struct kvm_vcpu *vcpu)
  2533. {
  2534. struct kvm_pio_request *io = &vcpu->arch.pio;
  2535. long delta;
  2536. int r;
  2537. unsigned long val;
  2538. if (!io->string) {
  2539. if (io->in) {
  2540. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2541. memcpy(&val, vcpu->arch.pio_data, io->size);
  2542. kvm_register_write(vcpu, VCPU_REGS_RAX, val);
  2543. }
  2544. } else {
  2545. if (io->in) {
  2546. r = pio_copy_data(vcpu);
  2547. if (r)
  2548. return r;
  2549. }
  2550. delta = 1;
  2551. if (io->rep) {
  2552. delta *= io->cur_count;
  2553. /*
  2554. * The size of the register should really depend on
  2555. * current address size.
  2556. */
  2557. val = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2558. val -= delta;
  2559. kvm_register_write(vcpu, VCPU_REGS_RCX, val);
  2560. }
  2561. if (io->down)
  2562. delta = -delta;
  2563. delta *= io->size;
  2564. if (io->in) {
  2565. val = kvm_register_read(vcpu, VCPU_REGS_RDI);
  2566. val += delta;
  2567. kvm_register_write(vcpu, VCPU_REGS_RDI, val);
  2568. } else {
  2569. val = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2570. val += delta;
  2571. kvm_register_write(vcpu, VCPU_REGS_RSI, val);
  2572. }
  2573. }
  2574. io->count -= io->cur_count;
  2575. io->cur_count = 0;
  2576. return 0;
  2577. }
  2578. static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
  2579. {
  2580. /* TODO: String I/O for in kernel device */
  2581. int r;
  2582. if (vcpu->arch.pio.in)
  2583. r = kvm_io_bus_read(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
  2584. vcpu->arch.pio.size, pd);
  2585. else
  2586. r = kvm_io_bus_write(&vcpu->kvm->pio_bus, vcpu->arch.pio.port,
  2587. vcpu->arch.pio.size, pd);
  2588. return r;
  2589. }
  2590. static int pio_string_write(struct kvm_vcpu *vcpu)
  2591. {
  2592. struct kvm_pio_request *io = &vcpu->arch.pio;
  2593. void *pd = vcpu->arch.pio_data;
  2594. int i, r = 0;
  2595. for (i = 0; i < io->cur_count; i++) {
  2596. if (kvm_io_bus_write(&vcpu->kvm->pio_bus,
  2597. io->port, io->size, pd)) {
  2598. r = -EOPNOTSUPP;
  2599. break;
  2600. }
  2601. pd += io->size;
  2602. }
  2603. return r;
  2604. }
  2605. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2606. int size, unsigned port)
  2607. {
  2608. unsigned long val;
  2609. vcpu->run->exit_reason = KVM_EXIT_IO;
  2610. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2611. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2612. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2613. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
  2614. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2615. vcpu->arch.pio.in = in;
  2616. vcpu->arch.pio.string = 0;
  2617. vcpu->arch.pio.down = 0;
  2618. vcpu->arch.pio.rep = 0;
  2619. trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
  2620. size, 1);
  2621. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2622. memcpy(vcpu->arch.pio_data, &val, 4);
  2623. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  2624. complete_pio(vcpu);
  2625. return 1;
  2626. }
  2627. return 0;
  2628. }
  2629. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  2630. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2631. int size, unsigned long count, int down,
  2632. gva_t address, int rep, unsigned port)
  2633. {
  2634. unsigned now, in_page;
  2635. int ret = 0;
  2636. vcpu->run->exit_reason = KVM_EXIT_IO;
  2637. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2638. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2639. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2640. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
  2641. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2642. vcpu->arch.pio.in = in;
  2643. vcpu->arch.pio.string = 1;
  2644. vcpu->arch.pio.down = down;
  2645. vcpu->arch.pio.rep = rep;
  2646. trace_kvm_pio(vcpu->run->io.direction == KVM_EXIT_IO_OUT, port,
  2647. size, count);
  2648. if (!count) {
  2649. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2650. return 1;
  2651. }
  2652. if (!down)
  2653. in_page = PAGE_SIZE - offset_in_page(address);
  2654. else
  2655. in_page = offset_in_page(address) + size;
  2656. now = min(count, (unsigned long)in_page / size);
  2657. if (!now)
  2658. now = 1;
  2659. if (down) {
  2660. /*
  2661. * String I/O in reverse. Yuck. Kill the guest, fix later.
  2662. */
  2663. pr_unimpl(vcpu, "guest string pio down\n");
  2664. kvm_inject_gp(vcpu, 0);
  2665. return 1;
  2666. }
  2667. vcpu->run->io.count = now;
  2668. vcpu->arch.pio.cur_count = now;
  2669. if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
  2670. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2671. vcpu->arch.pio.guest_gva = address;
  2672. if (!vcpu->arch.pio.in) {
  2673. /* string PIO write */
  2674. ret = pio_copy_data(vcpu);
  2675. if (ret == X86EMUL_PROPAGATE_FAULT) {
  2676. kvm_inject_gp(vcpu, 0);
  2677. return 1;
  2678. }
  2679. if (ret == 0 && !pio_string_write(vcpu)) {
  2680. complete_pio(vcpu);
  2681. if (vcpu->arch.pio.count == 0)
  2682. ret = 1;
  2683. }
  2684. }
  2685. /* no string PIO read support yet */
  2686. return ret;
  2687. }
  2688. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  2689. static void bounce_off(void *info)
  2690. {
  2691. /* nothing */
  2692. }
  2693. static unsigned int ref_freq;
  2694. static unsigned long tsc_khz_ref;
  2695. static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  2696. void *data)
  2697. {
  2698. struct cpufreq_freqs *freq = data;
  2699. struct kvm *kvm;
  2700. struct kvm_vcpu *vcpu;
  2701. int i, send_ipi = 0;
  2702. if (!ref_freq)
  2703. ref_freq = freq->old;
  2704. if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
  2705. return 0;
  2706. if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
  2707. return 0;
  2708. per_cpu(cpu_tsc_khz, freq->cpu) = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  2709. spin_lock(&kvm_lock);
  2710. list_for_each_entry(kvm, &vm_list, vm_list) {
  2711. kvm_for_each_vcpu(i, vcpu, kvm) {
  2712. if (vcpu->cpu != freq->cpu)
  2713. continue;
  2714. if (!kvm_request_guest_time_update(vcpu))
  2715. continue;
  2716. if (vcpu->cpu != smp_processor_id())
  2717. send_ipi++;
  2718. }
  2719. }
  2720. spin_unlock(&kvm_lock);
  2721. if (freq->old < freq->new && send_ipi) {
  2722. /*
  2723. * We upscale the frequency. Must make the guest
  2724. * doesn't see old kvmclock values while running with
  2725. * the new frequency, otherwise we risk the guest sees
  2726. * time go backwards.
  2727. *
  2728. * In case we update the frequency for another cpu
  2729. * (which might be in guest context) send an interrupt
  2730. * to kick the cpu out of guest context. Next time
  2731. * guest context is entered kvmclock will be updated,
  2732. * so the guest will not see stale values.
  2733. */
  2734. smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
  2735. }
  2736. return 0;
  2737. }
  2738. static struct notifier_block kvmclock_cpufreq_notifier_block = {
  2739. .notifier_call = kvmclock_cpufreq_notifier
  2740. };
  2741. int kvm_arch_init(void *opaque)
  2742. {
  2743. int r, cpu;
  2744. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  2745. if (kvm_x86_ops) {
  2746. printk(KERN_ERR "kvm: already loaded the other module\n");
  2747. r = -EEXIST;
  2748. goto out;
  2749. }
  2750. if (!ops->cpu_has_kvm_support()) {
  2751. printk(KERN_ERR "kvm: no hardware support\n");
  2752. r = -EOPNOTSUPP;
  2753. goto out;
  2754. }
  2755. if (ops->disabled_by_bios()) {
  2756. printk(KERN_ERR "kvm: disabled by bios\n");
  2757. r = -EOPNOTSUPP;
  2758. goto out;
  2759. }
  2760. r = kvm_mmu_module_init();
  2761. if (r)
  2762. goto out;
  2763. kvm_init_msr_list();
  2764. kvm_x86_ops = ops;
  2765. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  2766. kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
  2767. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  2768. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  2769. for_each_possible_cpu(cpu)
  2770. per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
  2771. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  2772. tsc_khz_ref = tsc_khz;
  2773. cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  2774. CPUFREQ_TRANSITION_NOTIFIER);
  2775. }
  2776. return 0;
  2777. out:
  2778. return r;
  2779. }
  2780. void kvm_arch_exit(void)
  2781. {
  2782. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  2783. cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
  2784. CPUFREQ_TRANSITION_NOTIFIER);
  2785. kvm_x86_ops = NULL;
  2786. kvm_mmu_module_exit();
  2787. }
  2788. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  2789. {
  2790. ++vcpu->stat.halt_exits;
  2791. if (irqchip_in_kernel(vcpu->kvm)) {
  2792. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  2793. return 1;
  2794. } else {
  2795. vcpu->run->exit_reason = KVM_EXIT_HLT;
  2796. return 0;
  2797. }
  2798. }
  2799. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  2800. static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
  2801. unsigned long a1)
  2802. {
  2803. if (is_long_mode(vcpu))
  2804. return a0;
  2805. else
  2806. return a0 | ((gpa_t)a1 << 32);
  2807. }
  2808. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  2809. {
  2810. unsigned long nr, a0, a1, a2, a3, ret;
  2811. int r = 1;
  2812. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2813. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  2814. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2815. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  2816. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2817. trace_kvm_hypercall(nr, a0, a1, a2, a3);
  2818. if (!is_long_mode(vcpu)) {
  2819. nr &= 0xFFFFFFFF;
  2820. a0 &= 0xFFFFFFFF;
  2821. a1 &= 0xFFFFFFFF;
  2822. a2 &= 0xFFFFFFFF;
  2823. a3 &= 0xFFFFFFFF;
  2824. }
  2825. switch (nr) {
  2826. case KVM_HC_VAPIC_POLL_IRQ:
  2827. ret = 0;
  2828. break;
  2829. case KVM_HC_MMU_OP:
  2830. r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
  2831. break;
  2832. default:
  2833. ret = -KVM_ENOSYS;
  2834. break;
  2835. }
  2836. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  2837. ++vcpu->stat.hypercalls;
  2838. return r;
  2839. }
  2840. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  2841. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  2842. {
  2843. char instruction[3];
  2844. int ret = 0;
  2845. unsigned long rip = kvm_rip_read(vcpu);
  2846. /*
  2847. * Blow out the MMU to ensure that no other VCPU has an active mapping
  2848. * to ensure that the updated hypercall appears atomically across all
  2849. * VCPUs.
  2850. */
  2851. kvm_mmu_zap_all(vcpu->kvm);
  2852. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  2853. if (emulator_write_emulated(rip, instruction, 3, vcpu)
  2854. != X86EMUL_CONTINUE)
  2855. ret = -EFAULT;
  2856. return ret;
  2857. }
  2858. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  2859. {
  2860. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  2861. }
  2862. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2863. {
  2864. struct descriptor_table dt = { limit, base };
  2865. kvm_x86_ops->set_gdt(vcpu, &dt);
  2866. }
  2867. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2868. {
  2869. struct descriptor_table dt = { limit, base };
  2870. kvm_x86_ops->set_idt(vcpu, &dt);
  2871. }
  2872. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  2873. unsigned long *rflags)
  2874. {
  2875. kvm_lmsw(vcpu, msw);
  2876. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2877. }
  2878. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  2879. {
  2880. unsigned long value;
  2881. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2882. switch (cr) {
  2883. case 0:
  2884. value = vcpu->arch.cr0;
  2885. break;
  2886. case 2:
  2887. value = vcpu->arch.cr2;
  2888. break;
  2889. case 3:
  2890. value = vcpu->arch.cr3;
  2891. break;
  2892. case 4:
  2893. value = vcpu->arch.cr4;
  2894. break;
  2895. case 8:
  2896. value = kvm_get_cr8(vcpu);
  2897. break;
  2898. default:
  2899. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2900. return 0;
  2901. }
  2902. return value;
  2903. }
  2904. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  2905. unsigned long *rflags)
  2906. {
  2907. switch (cr) {
  2908. case 0:
  2909. kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
  2910. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2911. break;
  2912. case 2:
  2913. vcpu->arch.cr2 = val;
  2914. break;
  2915. case 3:
  2916. kvm_set_cr3(vcpu, val);
  2917. break;
  2918. case 4:
  2919. kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
  2920. break;
  2921. case 8:
  2922. kvm_set_cr8(vcpu, val & 0xfUL);
  2923. break;
  2924. default:
  2925. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2926. }
  2927. }
  2928. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  2929. {
  2930. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  2931. int j, nent = vcpu->arch.cpuid_nent;
  2932. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  2933. /* when no next entry is found, the current entry[i] is reselected */
  2934. for (j = i + 1; ; j = (j + 1) % nent) {
  2935. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  2936. if (ej->function == e->function) {
  2937. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  2938. return j;
  2939. }
  2940. }
  2941. return 0; /* silence gcc, even though control never reaches here */
  2942. }
  2943. /* find an entry with matching function, matching index (if needed), and that
  2944. * should be read next (if it's stateful) */
  2945. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  2946. u32 function, u32 index)
  2947. {
  2948. if (e->function != function)
  2949. return 0;
  2950. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  2951. return 0;
  2952. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  2953. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  2954. return 0;
  2955. return 1;
  2956. }
  2957. struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
  2958. u32 function, u32 index)
  2959. {
  2960. int i;
  2961. struct kvm_cpuid_entry2 *best = NULL;
  2962. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  2963. struct kvm_cpuid_entry2 *e;
  2964. e = &vcpu->arch.cpuid_entries[i];
  2965. if (is_matching_cpuid_entry(e, function, index)) {
  2966. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  2967. move_to_next_stateful_cpuid_entry(vcpu, i);
  2968. best = e;
  2969. break;
  2970. }
  2971. /*
  2972. * Both basic or both extended?
  2973. */
  2974. if (((e->function ^ function) & 0x80000000) == 0)
  2975. if (!best || e->function > best->function)
  2976. best = e;
  2977. }
  2978. return best;
  2979. }
  2980. int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
  2981. {
  2982. struct kvm_cpuid_entry2 *best;
  2983. best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
  2984. if (best)
  2985. return best->eax & 0xff;
  2986. return 36;
  2987. }
  2988. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  2989. {
  2990. u32 function, index;
  2991. struct kvm_cpuid_entry2 *best;
  2992. function = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2993. index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2994. kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
  2995. kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
  2996. kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
  2997. kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  2998. best = kvm_find_cpuid_entry(vcpu, function, index);
  2999. if (best) {
  3000. kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  3001. kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  3002. kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
  3003. kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
  3004. }
  3005. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3006. trace_kvm_cpuid(function,
  3007. kvm_register_read(vcpu, VCPU_REGS_RAX),
  3008. kvm_register_read(vcpu, VCPU_REGS_RBX),
  3009. kvm_register_read(vcpu, VCPU_REGS_RCX),
  3010. kvm_register_read(vcpu, VCPU_REGS_RDX));
  3011. }
  3012. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  3013. /*
  3014. * Check if userspace requested an interrupt window, and that the
  3015. * interrupt window is open.
  3016. *
  3017. * No need to exit to userspace if we already have an interrupt queued.
  3018. */
  3019. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  3020. struct kvm_run *kvm_run)
  3021. {
  3022. return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
  3023. kvm_run->request_interrupt_window &&
  3024. kvm_arch_interrupt_allowed(vcpu));
  3025. }
  3026. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  3027. struct kvm_run *kvm_run)
  3028. {
  3029. kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  3030. kvm_run->cr8 = kvm_get_cr8(vcpu);
  3031. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  3032. if (irqchip_in_kernel(vcpu->kvm))
  3033. kvm_run->ready_for_interrupt_injection = 1;
  3034. else
  3035. kvm_run->ready_for_interrupt_injection =
  3036. kvm_arch_interrupt_allowed(vcpu) &&
  3037. !kvm_cpu_has_interrupt(vcpu) &&
  3038. !kvm_event_needs_reinjection(vcpu);
  3039. }
  3040. static void vapic_enter(struct kvm_vcpu *vcpu)
  3041. {
  3042. struct kvm_lapic *apic = vcpu->arch.apic;
  3043. struct page *page;
  3044. if (!apic || !apic->vapic_addr)
  3045. return;
  3046. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  3047. vcpu->arch.apic->vapic_page = page;
  3048. }
  3049. static void vapic_exit(struct kvm_vcpu *vcpu)
  3050. {
  3051. struct kvm_lapic *apic = vcpu->arch.apic;
  3052. if (!apic || !apic->vapic_addr)
  3053. return;
  3054. down_read(&vcpu->kvm->slots_lock);
  3055. kvm_release_page_dirty(apic->vapic_page);
  3056. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  3057. up_read(&vcpu->kvm->slots_lock);
  3058. }
  3059. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  3060. {
  3061. int max_irr, tpr;
  3062. if (!kvm_x86_ops->update_cr8_intercept)
  3063. return;
  3064. if (!vcpu->arch.apic->vapic_addr)
  3065. max_irr = kvm_lapic_find_highest_irr(vcpu);
  3066. else
  3067. max_irr = -1;
  3068. if (max_irr != -1)
  3069. max_irr >>= 4;
  3070. tpr = kvm_lapic_get_cr8(vcpu);
  3071. kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
  3072. }
  3073. static void inject_pending_irq(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  3074. {
  3075. /* try to reinject previous events if any */
  3076. if (vcpu->arch.nmi_injected) {
  3077. kvm_x86_ops->set_nmi(vcpu);
  3078. return;
  3079. }
  3080. if (vcpu->arch.interrupt.pending) {
  3081. kvm_x86_ops->set_irq(vcpu);
  3082. return;
  3083. }
  3084. /* try to inject new event if pending */
  3085. if (vcpu->arch.nmi_pending) {
  3086. if (kvm_x86_ops->nmi_allowed(vcpu)) {
  3087. vcpu->arch.nmi_pending = false;
  3088. vcpu->arch.nmi_injected = true;
  3089. kvm_x86_ops->set_nmi(vcpu);
  3090. }
  3091. } else if (kvm_cpu_has_interrupt(vcpu)) {
  3092. if (kvm_x86_ops->interrupt_allowed(vcpu)) {
  3093. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
  3094. false);
  3095. kvm_x86_ops->set_irq(vcpu);
  3096. }
  3097. }
  3098. }
  3099. static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  3100. {
  3101. int r;
  3102. bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
  3103. kvm_run->request_interrupt_window;
  3104. if (vcpu->requests)
  3105. if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
  3106. kvm_mmu_unload(vcpu);
  3107. r = kvm_mmu_reload(vcpu);
  3108. if (unlikely(r))
  3109. goto out;
  3110. if (vcpu->requests) {
  3111. if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
  3112. __kvm_migrate_timers(vcpu);
  3113. if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
  3114. kvm_write_guest_time(vcpu);
  3115. if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
  3116. kvm_mmu_sync_roots(vcpu);
  3117. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  3118. kvm_x86_ops->tlb_flush(vcpu);
  3119. if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
  3120. &vcpu->requests)) {
  3121. kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
  3122. r = 0;
  3123. goto out;
  3124. }
  3125. if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
  3126. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  3127. r = 0;
  3128. goto out;
  3129. }
  3130. }
  3131. preempt_disable();
  3132. kvm_x86_ops->prepare_guest_switch(vcpu);
  3133. kvm_load_guest_fpu(vcpu);
  3134. local_irq_disable();
  3135. clear_bit(KVM_REQ_KICK, &vcpu->requests);
  3136. smp_mb__after_clear_bit();
  3137. if (vcpu->requests || need_resched() || signal_pending(current)) {
  3138. set_bit(KVM_REQ_KICK, &vcpu->requests);
  3139. local_irq_enable();
  3140. preempt_enable();
  3141. r = 1;
  3142. goto out;
  3143. }
  3144. if (vcpu->arch.exception.pending)
  3145. __queue_exception(vcpu);
  3146. else
  3147. inject_pending_irq(vcpu, kvm_run);
  3148. /* enable NMI/IRQ window open exits if needed */
  3149. if (vcpu->arch.nmi_pending)
  3150. kvm_x86_ops->enable_nmi_window(vcpu);
  3151. else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
  3152. kvm_x86_ops->enable_irq_window(vcpu);
  3153. if (kvm_lapic_enabled(vcpu)) {
  3154. update_cr8_intercept(vcpu);
  3155. kvm_lapic_sync_to_vapic(vcpu);
  3156. }
  3157. up_read(&vcpu->kvm->slots_lock);
  3158. kvm_guest_enter();
  3159. get_debugreg(vcpu->arch.host_dr6, 6);
  3160. get_debugreg(vcpu->arch.host_dr7, 7);
  3161. if (unlikely(vcpu->arch.switch_db_regs)) {
  3162. get_debugreg(vcpu->arch.host_db[0], 0);
  3163. get_debugreg(vcpu->arch.host_db[1], 1);
  3164. get_debugreg(vcpu->arch.host_db[2], 2);
  3165. get_debugreg(vcpu->arch.host_db[3], 3);
  3166. set_debugreg(0, 7);
  3167. set_debugreg(vcpu->arch.eff_db[0], 0);
  3168. set_debugreg(vcpu->arch.eff_db[1], 1);
  3169. set_debugreg(vcpu->arch.eff_db[2], 2);
  3170. set_debugreg(vcpu->arch.eff_db[3], 3);
  3171. }
  3172. trace_kvm_entry(vcpu->vcpu_id);
  3173. kvm_x86_ops->run(vcpu, kvm_run);
  3174. if (unlikely(vcpu->arch.switch_db_regs)) {
  3175. set_debugreg(0, 7);
  3176. set_debugreg(vcpu->arch.host_db[0], 0);
  3177. set_debugreg(vcpu->arch.host_db[1], 1);
  3178. set_debugreg(vcpu->arch.host_db[2], 2);
  3179. set_debugreg(vcpu->arch.host_db[3], 3);
  3180. }
  3181. set_debugreg(vcpu->arch.host_dr6, 6);
  3182. set_debugreg(vcpu->arch.host_dr7, 7);
  3183. set_bit(KVM_REQ_KICK, &vcpu->requests);
  3184. local_irq_enable();
  3185. ++vcpu->stat.exits;
  3186. /*
  3187. * We must have an instruction between local_irq_enable() and
  3188. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  3189. * the interrupt shadow. The stat.exits increment will do nicely.
  3190. * But we need to prevent reordering, hence this barrier():
  3191. */
  3192. barrier();
  3193. kvm_guest_exit();
  3194. preempt_enable();
  3195. down_read(&vcpu->kvm->slots_lock);
  3196. /*
  3197. * Profile KVM exit RIPs:
  3198. */
  3199. if (unlikely(prof_on == KVM_PROFILING)) {
  3200. unsigned long rip = kvm_rip_read(vcpu);
  3201. profile_hit(KVM_PROFILING, (void *)rip);
  3202. }
  3203. kvm_lapic_sync_from_vapic(vcpu);
  3204. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  3205. out:
  3206. return r;
  3207. }
  3208. static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  3209. {
  3210. int r;
  3211. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  3212. pr_debug("vcpu %d received sipi with vector # %x\n",
  3213. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  3214. kvm_lapic_reset(vcpu);
  3215. r = kvm_arch_vcpu_reset(vcpu);
  3216. if (r)
  3217. return r;
  3218. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3219. }
  3220. down_read(&vcpu->kvm->slots_lock);
  3221. vapic_enter(vcpu);
  3222. r = 1;
  3223. while (r > 0) {
  3224. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
  3225. r = vcpu_enter_guest(vcpu, kvm_run);
  3226. else {
  3227. up_read(&vcpu->kvm->slots_lock);
  3228. kvm_vcpu_block(vcpu);
  3229. down_read(&vcpu->kvm->slots_lock);
  3230. if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
  3231. {
  3232. switch(vcpu->arch.mp_state) {
  3233. case KVM_MP_STATE_HALTED:
  3234. vcpu->arch.mp_state =
  3235. KVM_MP_STATE_RUNNABLE;
  3236. case KVM_MP_STATE_RUNNABLE:
  3237. break;
  3238. case KVM_MP_STATE_SIPI_RECEIVED:
  3239. default:
  3240. r = -EINTR;
  3241. break;
  3242. }
  3243. }
  3244. }
  3245. if (r <= 0)
  3246. break;
  3247. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  3248. if (kvm_cpu_has_pending_timer(vcpu))
  3249. kvm_inject_pending_timer_irqs(vcpu);
  3250. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  3251. r = -EINTR;
  3252. kvm_run->exit_reason = KVM_EXIT_INTR;
  3253. ++vcpu->stat.request_irq_exits;
  3254. }
  3255. if (signal_pending(current)) {
  3256. r = -EINTR;
  3257. kvm_run->exit_reason = KVM_EXIT_INTR;
  3258. ++vcpu->stat.signal_exits;
  3259. }
  3260. if (need_resched()) {
  3261. up_read(&vcpu->kvm->slots_lock);
  3262. kvm_resched(vcpu);
  3263. down_read(&vcpu->kvm->slots_lock);
  3264. }
  3265. }
  3266. up_read(&vcpu->kvm->slots_lock);
  3267. post_kvm_run_save(vcpu, kvm_run);
  3268. vapic_exit(vcpu);
  3269. return r;
  3270. }
  3271. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  3272. {
  3273. int r;
  3274. sigset_t sigsaved;
  3275. vcpu_load(vcpu);
  3276. if (vcpu->sigset_active)
  3277. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  3278. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  3279. kvm_vcpu_block(vcpu);
  3280. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  3281. r = -EAGAIN;
  3282. goto out;
  3283. }
  3284. /* re-sync apic's tpr */
  3285. if (!irqchip_in_kernel(vcpu->kvm))
  3286. kvm_set_cr8(vcpu, kvm_run->cr8);
  3287. if (vcpu->arch.pio.cur_count) {
  3288. r = complete_pio(vcpu);
  3289. if (r)
  3290. goto out;
  3291. }
  3292. #if CONFIG_HAS_IOMEM
  3293. if (vcpu->mmio_needed) {
  3294. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  3295. vcpu->mmio_read_completed = 1;
  3296. vcpu->mmio_needed = 0;
  3297. down_read(&vcpu->kvm->slots_lock);
  3298. r = emulate_instruction(vcpu, kvm_run,
  3299. vcpu->arch.mmio_fault_cr2, 0,
  3300. EMULTYPE_NO_DECODE);
  3301. up_read(&vcpu->kvm->slots_lock);
  3302. if (r == EMULATE_DO_MMIO) {
  3303. /*
  3304. * Read-modify-write. Back to userspace.
  3305. */
  3306. r = 0;
  3307. goto out;
  3308. }
  3309. }
  3310. #endif
  3311. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
  3312. kvm_register_write(vcpu, VCPU_REGS_RAX,
  3313. kvm_run->hypercall.ret);
  3314. r = __vcpu_run(vcpu, kvm_run);
  3315. out:
  3316. if (vcpu->sigset_active)
  3317. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  3318. vcpu_put(vcpu);
  3319. return r;
  3320. }
  3321. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  3322. {
  3323. vcpu_load(vcpu);
  3324. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3325. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3326. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3327. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3328. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3329. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3330. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3331. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3332. #ifdef CONFIG_X86_64
  3333. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  3334. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  3335. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  3336. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  3337. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  3338. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  3339. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  3340. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  3341. #endif
  3342. regs->rip = kvm_rip_read(vcpu);
  3343. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  3344. /*
  3345. * Don't leak debug flags in case they were set for guest debugging
  3346. */
  3347. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  3348. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  3349. vcpu_put(vcpu);
  3350. return 0;
  3351. }
  3352. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  3353. {
  3354. vcpu_load(vcpu);
  3355. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  3356. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  3357. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  3358. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  3359. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  3360. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  3361. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  3362. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  3363. #ifdef CONFIG_X86_64
  3364. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  3365. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  3366. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  3367. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  3368. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  3369. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  3370. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  3371. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  3372. #endif
  3373. kvm_rip_write(vcpu, regs->rip);
  3374. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  3375. vcpu->arch.exception.pending = false;
  3376. vcpu_put(vcpu);
  3377. return 0;
  3378. }
  3379. void kvm_get_segment(struct kvm_vcpu *vcpu,
  3380. struct kvm_segment *var, int seg)
  3381. {
  3382. kvm_x86_ops->get_segment(vcpu, var, seg);
  3383. }
  3384. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  3385. {
  3386. struct kvm_segment cs;
  3387. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  3388. *db = cs.db;
  3389. *l = cs.l;
  3390. }
  3391. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  3392. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  3393. struct kvm_sregs *sregs)
  3394. {
  3395. struct descriptor_table dt;
  3396. vcpu_load(vcpu);
  3397. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  3398. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  3399. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  3400. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  3401. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  3402. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  3403. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  3404. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  3405. kvm_x86_ops->get_idt(vcpu, &dt);
  3406. sregs->idt.limit = dt.limit;
  3407. sregs->idt.base = dt.base;
  3408. kvm_x86_ops->get_gdt(vcpu, &dt);
  3409. sregs->gdt.limit = dt.limit;
  3410. sregs->gdt.base = dt.base;
  3411. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  3412. sregs->cr0 = vcpu->arch.cr0;
  3413. sregs->cr2 = vcpu->arch.cr2;
  3414. sregs->cr3 = vcpu->arch.cr3;
  3415. sregs->cr4 = vcpu->arch.cr4;
  3416. sregs->cr8 = kvm_get_cr8(vcpu);
  3417. sregs->efer = vcpu->arch.shadow_efer;
  3418. sregs->apic_base = kvm_get_apic_base(vcpu);
  3419. memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
  3420. if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
  3421. set_bit(vcpu->arch.interrupt.nr,
  3422. (unsigned long *)sregs->interrupt_bitmap);
  3423. vcpu_put(vcpu);
  3424. return 0;
  3425. }
  3426. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  3427. struct kvm_mp_state *mp_state)
  3428. {
  3429. vcpu_load(vcpu);
  3430. mp_state->mp_state = vcpu->arch.mp_state;
  3431. vcpu_put(vcpu);
  3432. return 0;
  3433. }
  3434. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  3435. struct kvm_mp_state *mp_state)
  3436. {
  3437. vcpu_load(vcpu);
  3438. vcpu->arch.mp_state = mp_state->mp_state;
  3439. vcpu_put(vcpu);
  3440. return 0;
  3441. }
  3442. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3443. struct kvm_segment *var, int seg)
  3444. {
  3445. kvm_x86_ops->set_segment(vcpu, var, seg);
  3446. }
  3447. static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
  3448. struct kvm_segment *kvm_desct)
  3449. {
  3450. kvm_desct->base = seg_desc->base0;
  3451. kvm_desct->base |= seg_desc->base1 << 16;
  3452. kvm_desct->base |= seg_desc->base2 << 24;
  3453. kvm_desct->limit = seg_desc->limit0;
  3454. kvm_desct->limit |= seg_desc->limit << 16;
  3455. if (seg_desc->g) {
  3456. kvm_desct->limit <<= 12;
  3457. kvm_desct->limit |= 0xfff;
  3458. }
  3459. kvm_desct->selector = selector;
  3460. kvm_desct->type = seg_desc->type;
  3461. kvm_desct->present = seg_desc->p;
  3462. kvm_desct->dpl = seg_desc->dpl;
  3463. kvm_desct->db = seg_desc->d;
  3464. kvm_desct->s = seg_desc->s;
  3465. kvm_desct->l = seg_desc->l;
  3466. kvm_desct->g = seg_desc->g;
  3467. kvm_desct->avl = seg_desc->avl;
  3468. if (!selector)
  3469. kvm_desct->unusable = 1;
  3470. else
  3471. kvm_desct->unusable = 0;
  3472. kvm_desct->padding = 0;
  3473. }
  3474. static void get_segment_descriptor_dtable(struct kvm_vcpu *vcpu,
  3475. u16 selector,
  3476. struct descriptor_table *dtable)
  3477. {
  3478. if (selector & 1 << 2) {
  3479. struct kvm_segment kvm_seg;
  3480. kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
  3481. if (kvm_seg.unusable)
  3482. dtable->limit = 0;
  3483. else
  3484. dtable->limit = kvm_seg.limit;
  3485. dtable->base = kvm_seg.base;
  3486. }
  3487. else
  3488. kvm_x86_ops->get_gdt(vcpu, dtable);
  3489. }
  3490. /* allowed just for 8 bytes segments */
  3491. static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3492. struct desc_struct *seg_desc)
  3493. {
  3494. gpa_t gpa;
  3495. struct descriptor_table dtable;
  3496. u16 index = selector >> 3;
  3497. get_segment_descriptor_dtable(vcpu, selector, &dtable);
  3498. if (dtable.limit < index * 8 + 7) {
  3499. kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
  3500. return 1;
  3501. }
  3502. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3503. gpa += index * 8;
  3504. return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
  3505. }
  3506. /* allowed just for 8 bytes segments */
  3507. static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3508. struct desc_struct *seg_desc)
  3509. {
  3510. gpa_t gpa;
  3511. struct descriptor_table dtable;
  3512. u16 index = selector >> 3;
  3513. get_segment_descriptor_dtable(vcpu, selector, &dtable);
  3514. if (dtable.limit < index * 8 + 7)
  3515. return 1;
  3516. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3517. gpa += index * 8;
  3518. return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
  3519. }
  3520. static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
  3521. struct desc_struct *seg_desc)
  3522. {
  3523. u32 base_addr;
  3524. base_addr = seg_desc->base0;
  3525. base_addr |= (seg_desc->base1 << 16);
  3526. base_addr |= (seg_desc->base2 << 24);
  3527. return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
  3528. }
  3529. static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
  3530. {
  3531. struct kvm_segment kvm_seg;
  3532. kvm_get_segment(vcpu, &kvm_seg, seg);
  3533. return kvm_seg.selector;
  3534. }
  3535. static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
  3536. u16 selector,
  3537. struct kvm_segment *kvm_seg)
  3538. {
  3539. struct desc_struct seg_desc;
  3540. if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
  3541. return 1;
  3542. seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
  3543. return 0;
  3544. }
  3545. static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
  3546. {
  3547. struct kvm_segment segvar = {
  3548. .base = selector << 4,
  3549. .limit = 0xffff,
  3550. .selector = selector,
  3551. .type = 3,
  3552. .present = 1,
  3553. .dpl = 3,
  3554. .db = 0,
  3555. .s = 1,
  3556. .l = 0,
  3557. .g = 0,
  3558. .avl = 0,
  3559. .unusable = 0,
  3560. };
  3561. kvm_x86_ops->set_segment(vcpu, &segvar, seg);
  3562. return 0;
  3563. }
  3564. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3565. int type_bits, int seg)
  3566. {
  3567. struct kvm_segment kvm_seg;
  3568. if (!(vcpu->arch.cr0 & X86_CR0_PE))
  3569. return kvm_load_realmode_segment(vcpu, selector, seg);
  3570. if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
  3571. return 1;
  3572. kvm_seg.type |= type_bits;
  3573. if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
  3574. seg != VCPU_SREG_LDTR)
  3575. if (!kvm_seg.s)
  3576. kvm_seg.unusable = 1;
  3577. kvm_set_segment(vcpu, &kvm_seg, seg);
  3578. return 0;
  3579. }
  3580. static void save_state_to_tss32(struct kvm_vcpu *vcpu,
  3581. struct tss_segment_32 *tss)
  3582. {
  3583. tss->cr3 = vcpu->arch.cr3;
  3584. tss->eip = kvm_rip_read(vcpu);
  3585. tss->eflags = kvm_x86_ops->get_rflags(vcpu);
  3586. tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3587. tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3588. tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3589. tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3590. tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3591. tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3592. tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3593. tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3594. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3595. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3596. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3597. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3598. tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
  3599. tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
  3600. tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3601. }
  3602. static int load_state_from_tss32(struct kvm_vcpu *vcpu,
  3603. struct tss_segment_32 *tss)
  3604. {
  3605. kvm_set_cr3(vcpu, tss->cr3);
  3606. kvm_rip_write(vcpu, tss->eip);
  3607. kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
  3608. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
  3609. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
  3610. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
  3611. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
  3612. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
  3613. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
  3614. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
  3615. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
  3616. if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
  3617. return 1;
  3618. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3619. return 1;
  3620. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3621. return 1;
  3622. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3623. return 1;
  3624. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3625. return 1;
  3626. if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
  3627. return 1;
  3628. if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
  3629. return 1;
  3630. return 0;
  3631. }
  3632. static void save_state_to_tss16(struct kvm_vcpu *vcpu,
  3633. struct tss_segment_16 *tss)
  3634. {
  3635. tss->ip = kvm_rip_read(vcpu);
  3636. tss->flag = kvm_x86_ops->get_rflags(vcpu);
  3637. tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3638. tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3639. tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3640. tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3641. tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3642. tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3643. tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3644. tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3645. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3646. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3647. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3648. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3649. tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3650. tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
  3651. }
  3652. static int load_state_from_tss16(struct kvm_vcpu *vcpu,
  3653. struct tss_segment_16 *tss)
  3654. {
  3655. kvm_rip_write(vcpu, tss->ip);
  3656. kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
  3657. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
  3658. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
  3659. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
  3660. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
  3661. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
  3662. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
  3663. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
  3664. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
  3665. if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
  3666. return 1;
  3667. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3668. return 1;
  3669. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3670. return 1;
  3671. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3672. return 1;
  3673. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3674. return 1;
  3675. return 0;
  3676. }
  3677. static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
  3678. u16 old_tss_sel, u32 old_tss_base,
  3679. struct desc_struct *nseg_desc)
  3680. {
  3681. struct tss_segment_16 tss_segment_16;
  3682. int ret = 0;
  3683. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3684. sizeof tss_segment_16))
  3685. goto out;
  3686. save_state_to_tss16(vcpu, &tss_segment_16);
  3687. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3688. sizeof tss_segment_16))
  3689. goto out;
  3690. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3691. &tss_segment_16, sizeof tss_segment_16))
  3692. goto out;
  3693. if (old_tss_sel != 0xffff) {
  3694. tss_segment_16.prev_task_link = old_tss_sel;
  3695. if (kvm_write_guest(vcpu->kvm,
  3696. get_tss_base_addr(vcpu, nseg_desc),
  3697. &tss_segment_16.prev_task_link,
  3698. sizeof tss_segment_16.prev_task_link))
  3699. goto out;
  3700. }
  3701. if (load_state_from_tss16(vcpu, &tss_segment_16))
  3702. goto out;
  3703. ret = 1;
  3704. out:
  3705. return ret;
  3706. }
  3707. static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
  3708. u16 old_tss_sel, u32 old_tss_base,
  3709. struct desc_struct *nseg_desc)
  3710. {
  3711. struct tss_segment_32 tss_segment_32;
  3712. int ret = 0;
  3713. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3714. sizeof tss_segment_32))
  3715. goto out;
  3716. save_state_to_tss32(vcpu, &tss_segment_32);
  3717. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3718. sizeof tss_segment_32))
  3719. goto out;
  3720. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3721. &tss_segment_32, sizeof tss_segment_32))
  3722. goto out;
  3723. if (old_tss_sel != 0xffff) {
  3724. tss_segment_32.prev_task_link = old_tss_sel;
  3725. if (kvm_write_guest(vcpu->kvm,
  3726. get_tss_base_addr(vcpu, nseg_desc),
  3727. &tss_segment_32.prev_task_link,
  3728. sizeof tss_segment_32.prev_task_link))
  3729. goto out;
  3730. }
  3731. if (load_state_from_tss32(vcpu, &tss_segment_32))
  3732. goto out;
  3733. ret = 1;
  3734. out:
  3735. return ret;
  3736. }
  3737. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
  3738. {
  3739. struct kvm_segment tr_seg;
  3740. struct desc_struct cseg_desc;
  3741. struct desc_struct nseg_desc;
  3742. int ret = 0;
  3743. u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
  3744. u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
  3745. old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
  3746. /* FIXME: Handle errors. Failure to read either TSS or their
  3747. * descriptors should generate a pagefault.
  3748. */
  3749. if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
  3750. goto out;
  3751. if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
  3752. goto out;
  3753. if (reason != TASK_SWITCH_IRET) {
  3754. int cpl;
  3755. cpl = kvm_x86_ops->get_cpl(vcpu);
  3756. if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
  3757. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  3758. return 1;
  3759. }
  3760. }
  3761. if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
  3762. kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
  3763. return 1;
  3764. }
  3765. if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
  3766. cseg_desc.type &= ~(1 << 1); //clear the B flag
  3767. save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
  3768. }
  3769. if (reason == TASK_SWITCH_IRET) {
  3770. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3771. kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
  3772. }
  3773. /* set back link to prev task only if NT bit is set in eflags
  3774. note that old_tss_sel is not used afetr this point */
  3775. if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
  3776. old_tss_sel = 0xffff;
  3777. /* set back link to prev task only if NT bit is set in eflags
  3778. note that old_tss_sel is not used afetr this point */
  3779. if (reason != TASK_SWITCH_CALL && reason != TASK_SWITCH_GATE)
  3780. old_tss_sel = 0xffff;
  3781. if (nseg_desc.type & 8)
  3782. ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_sel,
  3783. old_tss_base, &nseg_desc);
  3784. else
  3785. ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_sel,
  3786. old_tss_base, &nseg_desc);
  3787. if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
  3788. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3789. kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
  3790. }
  3791. if (reason != TASK_SWITCH_IRET) {
  3792. nseg_desc.type |= (1 << 1);
  3793. save_guest_segment_descriptor(vcpu, tss_selector,
  3794. &nseg_desc);
  3795. }
  3796. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
  3797. seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
  3798. tr_seg.type = 11;
  3799. kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
  3800. out:
  3801. return ret;
  3802. }
  3803. EXPORT_SYMBOL_GPL(kvm_task_switch);
  3804. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  3805. struct kvm_sregs *sregs)
  3806. {
  3807. int mmu_reset_needed = 0;
  3808. int pending_vec, max_bits;
  3809. struct descriptor_table dt;
  3810. vcpu_load(vcpu);
  3811. dt.limit = sregs->idt.limit;
  3812. dt.base = sregs->idt.base;
  3813. kvm_x86_ops->set_idt(vcpu, &dt);
  3814. dt.limit = sregs->gdt.limit;
  3815. dt.base = sregs->gdt.base;
  3816. kvm_x86_ops->set_gdt(vcpu, &dt);
  3817. vcpu->arch.cr2 = sregs->cr2;
  3818. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  3819. vcpu->arch.cr3 = sregs->cr3;
  3820. kvm_set_cr8(vcpu, sregs->cr8);
  3821. mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
  3822. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  3823. kvm_set_apic_base(vcpu, sregs->apic_base);
  3824. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  3825. mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
  3826. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  3827. vcpu->arch.cr0 = sregs->cr0;
  3828. mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
  3829. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  3830. if (!is_long_mode(vcpu) && is_pae(vcpu))
  3831. load_pdptrs(vcpu, vcpu->arch.cr3);
  3832. if (mmu_reset_needed)
  3833. kvm_mmu_reset_context(vcpu);
  3834. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  3835. pending_vec = find_first_bit(
  3836. (const unsigned long *)sregs->interrupt_bitmap, max_bits);
  3837. if (pending_vec < max_bits) {
  3838. kvm_queue_interrupt(vcpu, pending_vec, false);
  3839. pr_debug("Set back pending irq %d\n", pending_vec);
  3840. if (irqchip_in_kernel(vcpu->kvm))
  3841. kvm_pic_clear_isr_ack(vcpu->kvm);
  3842. }
  3843. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  3844. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  3845. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  3846. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  3847. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  3848. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  3849. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  3850. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  3851. /* Older userspace won't unhalt the vcpu on reset. */
  3852. if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
  3853. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  3854. !(vcpu->arch.cr0 & X86_CR0_PE))
  3855. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3856. vcpu_put(vcpu);
  3857. return 0;
  3858. }
  3859. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  3860. struct kvm_guest_debug *dbg)
  3861. {
  3862. int i, r;
  3863. vcpu_load(vcpu);
  3864. if ((dbg->control & (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) ==
  3865. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP)) {
  3866. for (i = 0; i < KVM_NR_DB_REGS; ++i)
  3867. vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
  3868. vcpu->arch.switch_db_regs =
  3869. (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
  3870. } else {
  3871. for (i = 0; i < KVM_NR_DB_REGS; i++)
  3872. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  3873. vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
  3874. }
  3875. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  3876. if (dbg->control & KVM_GUESTDBG_INJECT_DB)
  3877. kvm_queue_exception(vcpu, DB_VECTOR);
  3878. else if (dbg->control & KVM_GUESTDBG_INJECT_BP)
  3879. kvm_queue_exception(vcpu, BP_VECTOR);
  3880. vcpu_put(vcpu);
  3881. return r;
  3882. }
  3883. /*
  3884. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  3885. * we have asm/x86/processor.h
  3886. */
  3887. struct fxsave {
  3888. u16 cwd;
  3889. u16 swd;
  3890. u16 twd;
  3891. u16 fop;
  3892. u64 rip;
  3893. u64 rdp;
  3894. u32 mxcsr;
  3895. u32 mxcsr_mask;
  3896. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  3897. #ifdef CONFIG_X86_64
  3898. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  3899. #else
  3900. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  3901. #endif
  3902. };
  3903. /*
  3904. * Translate a guest virtual address to a guest physical address.
  3905. */
  3906. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  3907. struct kvm_translation *tr)
  3908. {
  3909. unsigned long vaddr = tr->linear_address;
  3910. gpa_t gpa;
  3911. vcpu_load(vcpu);
  3912. down_read(&vcpu->kvm->slots_lock);
  3913. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
  3914. up_read(&vcpu->kvm->slots_lock);
  3915. tr->physical_address = gpa;
  3916. tr->valid = gpa != UNMAPPED_GVA;
  3917. tr->writeable = 1;
  3918. tr->usermode = 0;
  3919. vcpu_put(vcpu);
  3920. return 0;
  3921. }
  3922. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3923. {
  3924. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3925. vcpu_load(vcpu);
  3926. memcpy(fpu->fpr, fxsave->st_space, 128);
  3927. fpu->fcw = fxsave->cwd;
  3928. fpu->fsw = fxsave->swd;
  3929. fpu->ftwx = fxsave->twd;
  3930. fpu->last_opcode = fxsave->fop;
  3931. fpu->last_ip = fxsave->rip;
  3932. fpu->last_dp = fxsave->rdp;
  3933. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  3934. vcpu_put(vcpu);
  3935. return 0;
  3936. }
  3937. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3938. {
  3939. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3940. vcpu_load(vcpu);
  3941. memcpy(fxsave->st_space, fpu->fpr, 128);
  3942. fxsave->cwd = fpu->fcw;
  3943. fxsave->swd = fpu->fsw;
  3944. fxsave->twd = fpu->ftwx;
  3945. fxsave->fop = fpu->last_opcode;
  3946. fxsave->rip = fpu->last_ip;
  3947. fxsave->rdp = fpu->last_dp;
  3948. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  3949. vcpu_put(vcpu);
  3950. return 0;
  3951. }
  3952. void fx_init(struct kvm_vcpu *vcpu)
  3953. {
  3954. unsigned after_mxcsr_mask;
  3955. /*
  3956. * Touch the fpu the first time in non atomic context as if
  3957. * this is the first fpu instruction the exception handler
  3958. * will fire before the instruction returns and it'll have to
  3959. * allocate ram with GFP_KERNEL.
  3960. */
  3961. if (!used_math())
  3962. kvm_fx_save(&vcpu->arch.host_fx_image);
  3963. /* Initialize guest FPU by resetting ours and saving into guest's */
  3964. preempt_disable();
  3965. kvm_fx_save(&vcpu->arch.host_fx_image);
  3966. kvm_fx_finit();
  3967. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3968. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3969. preempt_enable();
  3970. vcpu->arch.cr0 |= X86_CR0_ET;
  3971. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  3972. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  3973. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  3974. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  3975. }
  3976. EXPORT_SYMBOL_GPL(fx_init);
  3977. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  3978. {
  3979. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  3980. return;
  3981. vcpu->guest_fpu_loaded = 1;
  3982. kvm_fx_save(&vcpu->arch.host_fx_image);
  3983. kvm_fx_restore(&vcpu->arch.guest_fx_image);
  3984. }
  3985. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  3986. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  3987. {
  3988. if (!vcpu->guest_fpu_loaded)
  3989. return;
  3990. vcpu->guest_fpu_loaded = 0;
  3991. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3992. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3993. ++vcpu->stat.fpu_reload;
  3994. }
  3995. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  3996. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  3997. {
  3998. if (vcpu->arch.time_page) {
  3999. kvm_release_page_dirty(vcpu->arch.time_page);
  4000. vcpu->arch.time_page = NULL;
  4001. }
  4002. kvm_x86_ops->vcpu_free(vcpu);
  4003. }
  4004. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  4005. unsigned int id)
  4006. {
  4007. return kvm_x86_ops->vcpu_create(kvm, id);
  4008. }
  4009. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  4010. {
  4011. int r;
  4012. /* We do fxsave: this must be aligned. */
  4013. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  4014. vcpu->arch.mtrr_state.have_fixed = 1;
  4015. vcpu_load(vcpu);
  4016. r = kvm_arch_vcpu_reset(vcpu);
  4017. if (r == 0)
  4018. r = kvm_mmu_setup(vcpu);
  4019. vcpu_put(vcpu);
  4020. if (r < 0)
  4021. goto free_vcpu;
  4022. return 0;
  4023. free_vcpu:
  4024. kvm_x86_ops->vcpu_free(vcpu);
  4025. return r;
  4026. }
  4027. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  4028. {
  4029. vcpu_load(vcpu);
  4030. kvm_mmu_unload(vcpu);
  4031. vcpu_put(vcpu);
  4032. kvm_x86_ops->vcpu_free(vcpu);
  4033. }
  4034. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  4035. {
  4036. vcpu->arch.nmi_pending = false;
  4037. vcpu->arch.nmi_injected = false;
  4038. vcpu->arch.switch_db_regs = 0;
  4039. memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
  4040. vcpu->arch.dr6 = DR6_FIXED_1;
  4041. vcpu->arch.dr7 = DR7_FIXED_1;
  4042. return kvm_x86_ops->vcpu_reset(vcpu);
  4043. }
  4044. void kvm_arch_hardware_enable(void *garbage)
  4045. {
  4046. kvm_x86_ops->hardware_enable(garbage);
  4047. }
  4048. void kvm_arch_hardware_disable(void *garbage)
  4049. {
  4050. kvm_x86_ops->hardware_disable(garbage);
  4051. }
  4052. int kvm_arch_hardware_setup(void)
  4053. {
  4054. return kvm_x86_ops->hardware_setup();
  4055. }
  4056. void kvm_arch_hardware_unsetup(void)
  4057. {
  4058. kvm_x86_ops->hardware_unsetup();
  4059. }
  4060. void kvm_arch_check_processor_compat(void *rtn)
  4061. {
  4062. kvm_x86_ops->check_processor_compatibility(rtn);
  4063. }
  4064. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  4065. {
  4066. struct page *page;
  4067. struct kvm *kvm;
  4068. int r;
  4069. BUG_ON(vcpu->kvm == NULL);
  4070. kvm = vcpu->kvm;
  4071. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  4072. if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
  4073. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4074. else
  4075. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  4076. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  4077. if (!page) {
  4078. r = -ENOMEM;
  4079. goto fail;
  4080. }
  4081. vcpu->arch.pio_data = page_address(page);
  4082. r = kvm_mmu_create(vcpu);
  4083. if (r < 0)
  4084. goto fail_free_pio_data;
  4085. if (irqchip_in_kernel(kvm)) {
  4086. r = kvm_create_lapic(vcpu);
  4087. if (r < 0)
  4088. goto fail_mmu_destroy;
  4089. }
  4090. vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
  4091. GFP_KERNEL);
  4092. if (!vcpu->arch.mce_banks) {
  4093. r = -ENOMEM;
  4094. goto fail_mmu_destroy;
  4095. }
  4096. vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
  4097. return 0;
  4098. fail_mmu_destroy:
  4099. kvm_mmu_destroy(vcpu);
  4100. fail_free_pio_data:
  4101. free_page((unsigned long)vcpu->arch.pio_data);
  4102. fail:
  4103. return r;
  4104. }
  4105. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  4106. {
  4107. kvm_free_lapic(vcpu);
  4108. down_read(&vcpu->kvm->slots_lock);
  4109. kvm_mmu_destroy(vcpu);
  4110. up_read(&vcpu->kvm->slots_lock);
  4111. free_page((unsigned long)vcpu->arch.pio_data);
  4112. }
  4113. struct kvm *kvm_arch_create_vm(void)
  4114. {
  4115. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  4116. if (!kvm)
  4117. return ERR_PTR(-ENOMEM);
  4118. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  4119. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  4120. /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
  4121. set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
  4122. rdtscll(kvm->arch.vm_init_tsc);
  4123. return kvm;
  4124. }
  4125. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  4126. {
  4127. vcpu_load(vcpu);
  4128. kvm_mmu_unload(vcpu);
  4129. vcpu_put(vcpu);
  4130. }
  4131. static void kvm_free_vcpus(struct kvm *kvm)
  4132. {
  4133. unsigned int i;
  4134. struct kvm_vcpu *vcpu;
  4135. /*
  4136. * Unpin any mmu pages first.
  4137. */
  4138. kvm_for_each_vcpu(i, vcpu, kvm)
  4139. kvm_unload_vcpu_mmu(vcpu);
  4140. kvm_for_each_vcpu(i, vcpu, kvm)
  4141. kvm_arch_vcpu_free(vcpu);
  4142. mutex_lock(&kvm->lock);
  4143. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  4144. kvm->vcpus[i] = NULL;
  4145. atomic_set(&kvm->online_vcpus, 0);
  4146. mutex_unlock(&kvm->lock);
  4147. }
  4148. void kvm_arch_sync_events(struct kvm *kvm)
  4149. {
  4150. kvm_free_all_assigned_devices(kvm);
  4151. }
  4152. void kvm_arch_destroy_vm(struct kvm *kvm)
  4153. {
  4154. kvm_iommu_unmap_guest(kvm);
  4155. kvm_free_pit(kvm);
  4156. kfree(kvm->arch.vpic);
  4157. kfree(kvm->arch.vioapic);
  4158. kvm_free_vcpus(kvm);
  4159. kvm_free_physmem(kvm);
  4160. if (kvm->arch.apic_access_page)
  4161. put_page(kvm->arch.apic_access_page);
  4162. if (kvm->arch.ept_identity_pagetable)
  4163. put_page(kvm->arch.ept_identity_pagetable);
  4164. kfree(kvm);
  4165. }
  4166. int kvm_arch_set_memory_region(struct kvm *kvm,
  4167. struct kvm_userspace_memory_region *mem,
  4168. struct kvm_memory_slot old,
  4169. int user_alloc)
  4170. {
  4171. int npages = mem->memory_size >> PAGE_SHIFT;
  4172. struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
  4173. /*To keep backward compatibility with older userspace,
  4174. *x86 needs to hanlde !user_alloc case.
  4175. */
  4176. if (!user_alloc) {
  4177. if (npages && !old.rmap) {
  4178. unsigned long userspace_addr;
  4179. down_write(&current->mm->mmap_sem);
  4180. userspace_addr = do_mmap(NULL, 0,
  4181. npages * PAGE_SIZE,
  4182. PROT_READ | PROT_WRITE,
  4183. MAP_PRIVATE | MAP_ANONYMOUS,
  4184. 0);
  4185. up_write(&current->mm->mmap_sem);
  4186. if (IS_ERR((void *)userspace_addr))
  4187. return PTR_ERR((void *)userspace_addr);
  4188. /* set userspace_addr atomically for kvm_hva_to_rmapp */
  4189. spin_lock(&kvm->mmu_lock);
  4190. memslot->userspace_addr = userspace_addr;
  4191. spin_unlock(&kvm->mmu_lock);
  4192. } else {
  4193. if (!old.user_alloc && old.rmap) {
  4194. int ret;
  4195. down_write(&current->mm->mmap_sem);
  4196. ret = do_munmap(current->mm, old.userspace_addr,
  4197. old.npages * PAGE_SIZE);
  4198. up_write(&current->mm->mmap_sem);
  4199. if (ret < 0)
  4200. printk(KERN_WARNING
  4201. "kvm_vm_ioctl_set_memory_region: "
  4202. "failed to munmap memory\n");
  4203. }
  4204. }
  4205. }
  4206. spin_lock(&kvm->mmu_lock);
  4207. if (!kvm->arch.n_requested_mmu_pages) {
  4208. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  4209. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  4210. }
  4211. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  4212. spin_unlock(&kvm->mmu_lock);
  4213. kvm_flush_remote_tlbs(kvm);
  4214. return 0;
  4215. }
  4216. void kvm_arch_flush_shadow(struct kvm *kvm)
  4217. {
  4218. kvm_mmu_zap_all(kvm);
  4219. kvm_reload_remote_mmus(kvm);
  4220. }
  4221. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  4222. {
  4223. return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
  4224. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
  4225. || vcpu->arch.nmi_pending;
  4226. }
  4227. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  4228. {
  4229. int me;
  4230. int cpu = vcpu->cpu;
  4231. if (waitqueue_active(&vcpu->wq)) {
  4232. wake_up_interruptible(&vcpu->wq);
  4233. ++vcpu->stat.halt_wakeup;
  4234. }
  4235. me = get_cpu();
  4236. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  4237. if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
  4238. smp_send_reschedule(cpu);
  4239. put_cpu();
  4240. }
  4241. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
  4242. {
  4243. return kvm_x86_ops->interrupt_allowed(vcpu);
  4244. }
  4245. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  4246. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
  4247. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
  4248. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
  4249. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);