vmx.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include <linux/kvm_host.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/highmem.h>
  25. #include <linux/sched.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/ftrace_event.h>
  28. #include <linux/slab.h>
  29. #include <linux/tboot.h>
  30. #include "kvm_cache_regs.h"
  31. #include "x86.h"
  32. #include <asm/io.h>
  33. #include <asm/desc.h>
  34. #include <asm/vmx.h>
  35. #include <asm/virtext.h>
  36. #include <asm/mce.h>
  37. #include <asm/i387.h>
  38. #include <asm/xcr.h>
  39. #include "trace.h"
  40. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  41. MODULE_AUTHOR("Qumranet");
  42. MODULE_LICENSE("GPL");
  43. static int __read_mostly bypass_guest_pf = 1;
  44. module_param(bypass_guest_pf, bool, S_IRUGO);
  45. static int __read_mostly enable_vpid = 1;
  46. module_param_named(vpid, enable_vpid, bool, 0444);
  47. static int __read_mostly flexpriority_enabled = 1;
  48. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  49. static int __read_mostly enable_ept = 1;
  50. module_param_named(ept, enable_ept, bool, S_IRUGO);
  51. static int __read_mostly enable_unrestricted_guest = 1;
  52. module_param_named(unrestricted_guest,
  53. enable_unrestricted_guest, bool, S_IRUGO);
  54. static int __read_mostly emulate_invalid_guest_state = 0;
  55. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  56. static int __read_mostly vmm_exclusive = 1;
  57. module_param(vmm_exclusive, bool, S_IRUGO);
  58. static int __read_mostly yield_on_hlt = 1;
  59. module_param(yield_on_hlt, bool, S_IRUGO);
  60. #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
  61. (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
  62. #define KVM_GUEST_CR0_MASK \
  63. (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  64. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
  65. (X86_CR0_WP | X86_CR0_NE)
  66. #define KVM_VM_CR0_ALWAYS_ON \
  67. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  68. #define KVM_CR4_GUEST_OWNED_BITS \
  69. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  70. | X86_CR4_OSXMMEXCPT)
  71. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  72. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  73. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  74. /*
  75. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  76. * ple_gap: upper bound on the amount of time between two successive
  77. * executions of PAUSE in a loop. Also indicate if ple enabled.
  78. * According to test, this time is usually smaller than 128 cycles.
  79. * ple_window: upper bound on the amount of time a guest is allowed to execute
  80. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  81. * less than 2^12 cycles
  82. * Time is measured based on a counter that runs at the same rate as the TSC,
  83. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  84. */
  85. #define KVM_VMX_DEFAULT_PLE_GAP 128
  86. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  87. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  88. module_param(ple_gap, int, S_IRUGO);
  89. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  90. module_param(ple_window, int, S_IRUGO);
  91. #define NR_AUTOLOAD_MSRS 1
  92. struct vmcs {
  93. u32 revision_id;
  94. u32 abort;
  95. char data[0];
  96. };
  97. struct shared_msr_entry {
  98. unsigned index;
  99. u64 data;
  100. u64 mask;
  101. };
  102. struct vcpu_vmx {
  103. struct kvm_vcpu vcpu;
  104. struct list_head local_vcpus_link;
  105. unsigned long host_rsp;
  106. int launched;
  107. u8 fail;
  108. u8 cpl;
  109. bool nmi_known_unmasked;
  110. u32 exit_intr_info;
  111. u32 idt_vectoring_info;
  112. ulong rflags;
  113. struct shared_msr_entry *guest_msrs;
  114. int nmsrs;
  115. int save_nmsrs;
  116. #ifdef CONFIG_X86_64
  117. u64 msr_host_kernel_gs_base;
  118. u64 msr_guest_kernel_gs_base;
  119. #endif
  120. struct vmcs *vmcs;
  121. struct msr_autoload {
  122. unsigned nr;
  123. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  124. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  125. } msr_autoload;
  126. struct {
  127. int loaded;
  128. u16 fs_sel, gs_sel, ldt_sel;
  129. int gs_ldt_reload_needed;
  130. int fs_reload_needed;
  131. } host_state;
  132. struct {
  133. int vm86_active;
  134. ulong save_rflags;
  135. struct kvm_save_segment {
  136. u16 selector;
  137. unsigned long base;
  138. u32 limit;
  139. u32 ar;
  140. } tr, es, ds, fs, gs;
  141. } rmode;
  142. struct {
  143. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  144. struct kvm_save_segment seg[8];
  145. } segment_cache;
  146. int vpid;
  147. bool emulation_required;
  148. /* Support for vnmi-less CPUs */
  149. int soft_vnmi_blocked;
  150. ktime_t entry_time;
  151. s64 vnmi_blocked_time;
  152. u32 exit_reason;
  153. bool rdtscp_enabled;
  154. };
  155. enum segment_cache_field {
  156. SEG_FIELD_SEL = 0,
  157. SEG_FIELD_BASE = 1,
  158. SEG_FIELD_LIMIT = 2,
  159. SEG_FIELD_AR = 3,
  160. SEG_FIELD_NR = 4
  161. };
  162. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  163. {
  164. return container_of(vcpu, struct vcpu_vmx, vcpu);
  165. }
  166. static u64 construct_eptp(unsigned long root_hpa);
  167. static void kvm_cpu_vmxon(u64 addr);
  168. static void kvm_cpu_vmxoff(void);
  169. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  170. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  171. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  172. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  173. static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
  174. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  175. static unsigned long *vmx_io_bitmap_a;
  176. static unsigned long *vmx_io_bitmap_b;
  177. static unsigned long *vmx_msr_bitmap_legacy;
  178. static unsigned long *vmx_msr_bitmap_longmode;
  179. static bool cpu_has_load_ia32_efer;
  180. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  181. static DEFINE_SPINLOCK(vmx_vpid_lock);
  182. static struct vmcs_config {
  183. int size;
  184. int order;
  185. u32 revision_id;
  186. u32 pin_based_exec_ctrl;
  187. u32 cpu_based_exec_ctrl;
  188. u32 cpu_based_2nd_exec_ctrl;
  189. u32 vmexit_ctrl;
  190. u32 vmentry_ctrl;
  191. } vmcs_config;
  192. static struct vmx_capability {
  193. u32 ept;
  194. u32 vpid;
  195. } vmx_capability;
  196. #define VMX_SEGMENT_FIELD(seg) \
  197. [VCPU_SREG_##seg] = { \
  198. .selector = GUEST_##seg##_SELECTOR, \
  199. .base = GUEST_##seg##_BASE, \
  200. .limit = GUEST_##seg##_LIMIT, \
  201. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  202. }
  203. static struct kvm_vmx_segment_field {
  204. unsigned selector;
  205. unsigned base;
  206. unsigned limit;
  207. unsigned ar_bytes;
  208. } kvm_vmx_segment_fields[] = {
  209. VMX_SEGMENT_FIELD(CS),
  210. VMX_SEGMENT_FIELD(DS),
  211. VMX_SEGMENT_FIELD(ES),
  212. VMX_SEGMENT_FIELD(FS),
  213. VMX_SEGMENT_FIELD(GS),
  214. VMX_SEGMENT_FIELD(SS),
  215. VMX_SEGMENT_FIELD(TR),
  216. VMX_SEGMENT_FIELD(LDTR),
  217. };
  218. static u64 host_efer;
  219. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  220. /*
  221. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  222. * away by decrementing the array size.
  223. */
  224. static const u32 vmx_msr_index[] = {
  225. #ifdef CONFIG_X86_64
  226. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  227. #endif
  228. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  229. };
  230. #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
  231. static inline bool is_page_fault(u32 intr_info)
  232. {
  233. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  234. INTR_INFO_VALID_MASK)) ==
  235. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  236. }
  237. static inline bool is_no_device(u32 intr_info)
  238. {
  239. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  240. INTR_INFO_VALID_MASK)) ==
  241. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  242. }
  243. static inline bool is_invalid_opcode(u32 intr_info)
  244. {
  245. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  246. INTR_INFO_VALID_MASK)) ==
  247. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  248. }
  249. static inline bool is_external_interrupt(u32 intr_info)
  250. {
  251. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  252. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  253. }
  254. static inline bool is_machine_check(u32 intr_info)
  255. {
  256. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  257. INTR_INFO_VALID_MASK)) ==
  258. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  259. }
  260. static inline bool cpu_has_vmx_msr_bitmap(void)
  261. {
  262. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  263. }
  264. static inline bool cpu_has_vmx_tpr_shadow(void)
  265. {
  266. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  267. }
  268. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  269. {
  270. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  271. }
  272. static inline bool cpu_has_secondary_exec_ctrls(void)
  273. {
  274. return vmcs_config.cpu_based_exec_ctrl &
  275. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  276. }
  277. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  278. {
  279. return vmcs_config.cpu_based_2nd_exec_ctrl &
  280. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  281. }
  282. static inline bool cpu_has_vmx_flexpriority(void)
  283. {
  284. return cpu_has_vmx_tpr_shadow() &&
  285. cpu_has_vmx_virtualize_apic_accesses();
  286. }
  287. static inline bool cpu_has_vmx_ept_execute_only(void)
  288. {
  289. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  290. }
  291. static inline bool cpu_has_vmx_eptp_uncacheable(void)
  292. {
  293. return vmx_capability.ept & VMX_EPTP_UC_BIT;
  294. }
  295. static inline bool cpu_has_vmx_eptp_writeback(void)
  296. {
  297. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  298. }
  299. static inline bool cpu_has_vmx_ept_2m_page(void)
  300. {
  301. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  302. }
  303. static inline bool cpu_has_vmx_ept_1g_page(void)
  304. {
  305. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  306. }
  307. static inline bool cpu_has_vmx_ept_4levels(void)
  308. {
  309. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  310. }
  311. static inline bool cpu_has_vmx_invept_individual_addr(void)
  312. {
  313. return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
  314. }
  315. static inline bool cpu_has_vmx_invept_context(void)
  316. {
  317. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  318. }
  319. static inline bool cpu_has_vmx_invept_global(void)
  320. {
  321. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  322. }
  323. static inline bool cpu_has_vmx_invvpid_single(void)
  324. {
  325. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  326. }
  327. static inline bool cpu_has_vmx_invvpid_global(void)
  328. {
  329. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  330. }
  331. static inline bool cpu_has_vmx_ept(void)
  332. {
  333. return vmcs_config.cpu_based_2nd_exec_ctrl &
  334. SECONDARY_EXEC_ENABLE_EPT;
  335. }
  336. static inline bool cpu_has_vmx_unrestricted_guest(void)
  337. {
  338. return vmcs_config.cpu_based_2nd_exec_ctrl &
  339. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  340. }
  341. static inline bool cpu_has_vmx_ple(void)
  342. {
  343. return vmcs_config.cpu_based_2nd_exec_ctrl &
  344. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  345. }
  346. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  347. {
  348. return flexpriority_enabled && irqchip_in_kernel(kvm);
  349. }
  350. static inline bool cpu_has_vmx_vpid(void)
  351. {
  352. return vmcs_config.cpu_based_2nd_exec_ctrl &
  353. SECONDARY_EXEC_ENABLE_VPID;
  354. }
  355. static inline bool cpu_has_vmx_rdtscp(void)
  356. {
  357. return vmcs_config.cpu_based_2nd_exec_ctrl &
  358. SECONDARY_EXEC_RDTSCP;
  359. }
  360. static inline bool cpu_has_virtual_nmis(void)
  361. {
  362. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  363. }
  364. static inline bool cpu_has_vmx_wbinvd_exit(void)
  365. {
  366. return vmcs_config.cpu_based_2nd_exec_ctrl &
  367. SECONDARY_EXEC_WBINVD_EXITING;
  368. }
  369. static inline bool report_flexpriority(void)
  370. {
  371. return flexpriority_enabled;
  372. }
  373. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  374. {
  375. int i;
  376. for (i = 0; i < vmx->nmsrs; ++i)
  377. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  378. return i;
  379. return -1;
  380. }
  381. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  382. {
  383. struct {
  384. u64 vpid : 16;
  385. u64 rsvd : 48;
  386. u64 gva;
  387. } operand = { vpid, 0, gva };
  388. asm volatile (__ex(ASM_VMX_INVVPID)
  389. /* CF==1 or ZF==1 --> rc = -1 */
  390. "; ja 1f ; ud2 ; 1:"
  391. : : "a"(&operand), "c"(ext) : "cc", "memory");
  392. }
  393. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  394. {
  395. struct {
  396. u64 eptp, gpa;
  397. } operand = {eptp, gpa};
  398. asm volatile (__ex(ASM_VMX_INVEPT)
  399. /* CF==1 or ZF==1 --> rc = -1 */
  400. "; ja 1f ; ud2 ; 1:\n"
  401. : : "a" (&operand), "c" (ext) : "cc", "memory");
  402. }
  403. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  404. {
  405. int i;
  406. i = __find_msr_index(vmx, msr);
  407. if (i >= 0)
  408. return &vmx->guest_msrs[i];
  409. return NULL;
  410. }
  411. static void vmcs_clear(struct vmcs *vmcs)
  412. {
  413. u64 phys_addr = __pa(vmcs);
  414. u8 error;
  415. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  416. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  417. : "cc", "memory");
  418. if (error)
  419. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  420. vmcs, phys_addr);
  421. }
  422. static void vmcs_load(struct vmcs *vmcs)
  423. {
  424. u64 phys_addr = __pa(vmcs);
  425. u8 error;
  426. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  427. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  428. : "cc", "memory");
  429. if (error)
  430. printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
  431. vmcs, phys_addr);
  432. }
  433. static void __vcpu_clear(void *arg)
  434. {
  435. struct vcpu_vmx *vmx = arg;
  436. int cpu = raw_smp_processor_id();
  437. if (vmx->vcpu.cpu == cpu)
  438. vmcs_clear(vmx->vmcs);
  439. if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
  440. per_cpu(current_vmcs, cpu) = NULL;
  441. list_del(&vmx->local_vcpus_link);
  442. vmx->vcpu.cpu = -1;
  443. vmx->launched = 0;
  444. }
  445. static void vcpu_clear(struct vcpu_vmx *vmx)
  446. {
  447. if (vmx->vcpu.cpu == -1)
  448. return;
  449. smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
  450. }
  451. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  452. {
  453. if (vmx->vpid == 0)
  454. return;
  455. if (cpu_has_vmx_invvpid_single())
  456. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  457. }
  458. static inline void vpid_sync_vcpu_global(void)
  459. {
  460. if (cpu_has_vmx_invvpid_global())
  461. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  462. }
  463. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  464. {
  465. if (cpu_has_vmx_invvpid_single())
  466. vpid_sync_vcpu_single(vmx);
  467. else
  468. vpid_sync_vcpu_global();
  469. }
  470. static inline void ept_sync_global(void)
  471. {
  472. if (cpu_has_vmx_invept_global())
  473. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  474. }
  475. static inline void ept_sync_context(u64 eptp)
  476. {
  477. if (enable_ept) {
  478. if (cpu_has_vmx_invept_context())
  479. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  480. else
  481. ept_sync_global();
  482. }
  483. }
  484. static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
  485. {
  486. if (enable_ept) {
  487. if (cpu_has_vmx_invept_individual_addr())
  488. __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
  489. eptp, gpa);
  490. else
  491. ept_sync_context(eptp);
  492. }
  493. }
  494. static unsigned long vmcs_readl(unsigned long field)
  495. {
  496. unsigned long value = 0;
  497. asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
  498. : "+a"(value) : "d"(field) : "cc");
  499. return value;
  500. }
  501. static u16 vmcs_read16(unsigned long field)
  502. {
  503. return vmcs_readl(field);
  504. }
  505. static u32 vmcs_read32(unsigned long field)
  506. {
  507. return vmcs_readl(field);
  508. }
  509. static u64 vmcs_read64(unsigned long field)
  510. {
  511. #ifdef CONFIG_X86_64
  512. return vmcs_readl(field);
  513. #else
  514. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  515. #endif
  516. }
  517. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  518. {
  519. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  520. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  521. dump_stack();
  522. }
  523. static void vmcs_writel(unsigned long field, unsigned long value)
  524. {
  525. u8 error;
  526. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  527. : "=q"(error) : "a"(value), "d"(field) : "cc");
  528. if (unlikely(error))
  529. vmwrite_error(field, value);
  530. }
  531. static void vmcs_write16(unsigned long field, u16 value)
  532. {
  533. vmcs_writel(field, value);
  534. }
  535. static void vmcs_write32(unsigned long field, u32 value)
  536. {
  537. vmcs_writel(field, value);
  538. }
  539. static void vmcs_write64(unsigned long field, u64 value)
  540. {
  541. vmcs_writel(field, value);
  542. #ifndef CONFIG_X86_64
  543. asm volatile ("");
  544. vmcs_writel(field+1, value >> 32);
  545. #endif
  546. }
  547. static void vmcs_clear_bits(unsigned long field, u32 mask)
  548. {
  549. vmcs_writel(field, vmcs_readl(field) & ~mask);
  550. }
  551. static void vmcs_set_bits(unsigned long field, u32 mask)
  552. {
  553. vmcs_writel(field, vmcs_readl(field) | mask);
  554. }
  555. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  556. {
  557. vmx->segment_cache.bitmask = 0;
  558. }
  559. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  560. unsigned field)
  561. {
  562. bool ret;
  563. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  564. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  565. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  566. vmx->segment_cache.bitmask = 0;
  567. }
  568. ret = vmx->segment_cache.bitmask & mask;
  569. vmx->segment_cache.bitmask |= mask;
  570. return ret;
  571. }
  572. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  573. {
  574. u16 *p = &vmx->segment_cache.seg[seg].selector;
  575. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  576. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  577. return *p;
  578. }
  579. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  580. {
  581. ulong *p = &vmx->segment_cache.seg[seg].base;
  582. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  583. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  584. return *p;
  585. }
  586. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  587. {
  588. u32 *p = &vmx->segment_cache.seg[seg].limit;
  589. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  590. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  591. return *p;
  592. }
  593. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  594. {
  595. u32 *p = &vmx->segment_cache.seg[seg].ar;
  596. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  597. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  598. return *p;
  599. }
  600. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  601. {
  602. u32 eb;
  603. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  604. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  605. if ((vcpu->guest_debug &
  606. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  607. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  608. eb |= 1u << BP_VECTOR;
  609. if (to_vmx(vcpu)->rmode.vm86_active)
  610. eb = ~0;
  611. if (enable_ept)
  612. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  613. if (vcpu->fpu_active)
  614. eb &= ~(1u << NM_VECTOR);
  615. vmcs_write32(EXCEPTION_BITMAP, eb);
  616. }
  617. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  618. {
  619. unsigned i;
  620. struct msr_autoload *m = &vmx->msr_autoload;
  621. if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
  622. vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
  623. vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
  624. return;
  625. }
  626. for (i = 0; i < m->nr; ++i)
  627. if (m->guest[i].index == msr)
  628. break;
  629. if (i == m->nr)
  630. return;
  631. --m->nr;
  632. m->guest[i] = m->guest[m->nr];
  633. m->host[i] = m->host[m->nr];
  634. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  635. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  636. }
  637. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  638. u64 guest_val, u64 host_val)
  639. {
  640. unsigned i;
  641. struct msr_autoload *m = &vmx->msr_autoload;
  642. if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
  643. vmcs_write64(GUEST_IA32_EFER, guest_val);
  644. vmcs_write64(HOST_IA32_EFER, host_val);
  645. vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
  646. vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
  647. return;
  648. }
  649. for (i = 0; i < m->nr; ++i)
  650. if (m->guest[i].index == msr)
  651. break;
  652. if (i == m->nr) {
  653. ++m->nr;
  654. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  655. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  656. }
  657. m->guest[i].index = msr;
  658. m->guest[i].value = guest_val;
  659. m->host[i].index = msr;
  660. m->host[i].value = host_val;
  661. }
  662. static void reload_tss(void)
  663. {
  664. /*
  665. * VT restores TR but not its size. Useless.
  666. */
  667. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  668. struct desc_struct *descs;
  669. descs = (void *)gdt->address;
  670. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  671. load_TR_desc();
  672. }
  673. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  674. {
  675. u64 guest_efer;
  676. u64 ignore_bits;
  677. guest_efer = vmx->vcpu.arch.efer;
  678. /*
  679. * NX is emulated; LMA and LME handled by hardware; SCE meaninless
  680. * outside long mode
  681. */
  682. ignore_bits = EFER_NX | EFER_SCE;
  683. #ifdef CONFIG_X86_64
  684. ignore_bits |= EFER_LMA | EFER_LME;
  685. /* SCE is meaningful only in long mode on Intel */
  686. if (guest_efer & EFER_LMA)
  687. ignore_bits &= ~(u64)EFER_SCE;
  688. #endif
  689. guest_efer &= ~ignore_bits;
  690. guest_efer |= host_efer & ignore_bits;
  691. vmx->guest_msrs[efer_offset].data = guest_efer;
  692. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  693. clear_atomic_switch_msr(vmx, MSR_EFER);
  694. /* On ept, can't emulate nx, and must switch nx atomically */
  695. if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
  696. guest_efer = vmx->vcpu.arch.efer;
  697. if (!(guest_efer & EFER_LMA))
  698. guest_efer &= ~EFER_LME;
  699. add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
  700. return false;
  701. }
  702. return true;
  703. }
  704. static unsigned long segment_base(u16 selector)
  705. {
  706. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  707. struct desc_struct *d;
  708. unsigned long table_base;
  709. unsigned long v;
  710. if (!(selector & ~3))
  711. return 0;
  712. table_base = gdt->address;
  713. if (selector & 4) { /* from ldt */
  714. u16 ldt_selector = kvm_read_ldt();
  715. if (!(ldt_selector & ~3))
  716. return 0;
  717. table_base = segment_base(ldt_selector);
  718. }
  719. d = (struct desc_struct *)(table_base + (selector & ~7));
  720. v = get_desc_base(d);
  721. #ifdef CONFIG_X86_64
  722. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  723. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  724. #endif
  725. return v;
  726. }
  727. static inline unsigned long kvm_read_tr_base(void)
  728. {
  729. u16 tr;
  730. asm("str %0" : "=g"(tr));
  731. return segment_base(tr);
  732. }
  733. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  734. {
  735. struct vcpu_vmx *vmx = to_vmx(vcpu);
  736. int i;
  737. if (vmx->host_state.loaded)
  738. return;
  739. vmx->host_state.loaded = 1;
  740. /*
  741. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  742. * allow segment selectors with cpl > 0 or ti == 1.
  743. */
  744. vmx->host_state.ldt_sel = kvm_read_ldt();
  745. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  746. savesegment(fs, vmx->host_state.fs_sel);
  747. if (!(vmx->host_state.fs_sel & 7)) {
  748. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  749. vmx->host_state.fs_reload_needed = 0;
  750. } else {
  751. vmcs_write16(HOST_FS_SELECTOR, 0);
  752. vmx->host_state.fs_reload_needed = 1;
  753. }
  754. savesegment(gs, vmx->host_state.gs_sel);
  755. if (!(vmx->host_state.gs_sel & 7))
  756. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  757. else {
  758. vmcs_write16(HOST_GS_SELECTOR, 0);
  759. vmx->host_state.gs_ldt_reload_needed = 1;
  760. }
  761. #ifdef CONFIG_X86_64
  762. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  763. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  764. #else
  765. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  766. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  767. #endif
  768. #ifdef CONFIG_X86_64
  769. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  770. if (is_long_mode(&vmx->vcpu))
  771. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  772. #endif
  773. for (i = 0; i < vmx->save_nmsrs; ++i)
  774. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  775. vmx->guest_msrs[i].data,
  776. vmx->guest_msrs[i].mask);
  777. }
  778. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  779. {
  780. if (!vmx->host_state.loaded)
  781. return;
  782. ++vmx->vcpu.stat.host_state_reload;
  783. vmx->host_state.loaded = 0;
  784. #ifdef CONFIG_X86_64
  785. if (is_long_mode(&vmx->vcpu))
  786. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  787. #endif
  788. if (vmx->host_state.gs_ldt_reload_needed) {
  789. kvm_load_ldt(vmx->host_state.ldt_sel);
  790. #ifdef CONFIG_X86_64
  791. load_gs_index(vmx->host_state.gs_sel);
  792. #else
  793. loadsegment(gs, vmx->host_state.gs_sel);
  794. #endif
  795. }
  796. if (vmx->host_state.fs_reload_needed)
  797. loadsegment(fs, vmx->host_state.fs_sel);
  798. reload_tss();
  799. #ifdef CONFIG_X86_64
  800. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  801. #endif
  802. if (current_thread_info()->status & TS_USEDFPU)
  803. clts();
  804. load_gdt(&__get_cpu_var(host_gdt));
  805. }
  806. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  807. {
  808. preempt_disable();
  809. __vmx_load_host_state(vmx);
  810. preempt_enable();
  811. }
  812. /*
  813. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  814. * vcpu mutex is already taken.
  815. */
  816. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  817. {
  818. struct vcpu_vmx *vmx = to_vmx(vcpu);
  819. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  820. if (!vmm_exclusive)
  821. kvm_cpu_vmxon(phys_addr);
  822. else if (vcpu->cpu != cpu)
  823. vcpu_clear(vmx);
  824. if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
  825. per_cpu(current_vmcs, cpu) = vmx->vmcs;
  826. vmcs_load(vmx->vmcs);
  827. }
  828. if (vcpu->cpu != cpu) {
  829. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  830. unsigned long sysenter_esp;
  831. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  832. local_irq_disable();
  833. list_add(&vmx->local_vcpus_link,
  834. &per_cpu(vcpus_on_cpu, cpu));
  835. local_irq_enable();
  836. /*
  837. * Linux uses per-cpu TSS and GDT, so set these when switching
  838. * processors.
  839. */
  840. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  841. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  842. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  843. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  844. }
  845. }
  846. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  847. {
  848. __vmx_load_host_state(to_vmx(vcpu));
  849. if (!vmm_exclusive) {
  850. __vcpu_clear(to_vmx(vcpu));
  851. kvm_cpu_vmxoff();
  852. }
  853. }
  854. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  855. {
  856. ulong cr0;
  857. if (vcpu->fpu_active)
  858. return;
  859. vcpu->fpu_active = 1;
  860. cr0 = vmcs_readl(GUEST_CR0);
  861. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  862. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  863. vmcs_writel(GUEST_CR0, cr0);
  864. update_exception_bitmap(vcpu);
  865. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  866. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  867. }
  868. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  869. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  870. {
  871. vmx_decache_cr0_guest_bits(vcpu);
  872. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  873. update_exception_bitmap(vcpu);
  874. vcpu->arch.cr0_guest_owned_bits = 0;
  875. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  876. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  877. }
  878. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  879. {
  880. unsigned long rflags, save_rflags;
  881. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  882. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  883. rflags = vmcs_readl(GUEST_RFLAGS);
  884. if (to_vmx(vcpu)->rmode.vm86_active) {
  885. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  886. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  887. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  888. }
  889. to_vmx(vcpu)->rflags = rflags;
  890. }
  891. return to_vmx(vcpu)->rflags;
  892. }
  893. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  894. {
  895. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  896. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  897. to_vmx(vcpu)->rflags = rflags;
  898. if (to_vmx(vcpu)->rmode.vm86_active) {
  899. to_vmx(vcpu)->rmode.save_rflags = rflags;
  900. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  901. }
  902. vmcs_writel(GUEST_RFLAGS, rflags);
  903. }
  904. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  905. {
  906. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  907. int ret = 0;
  908. if (interruptibility & GUEST_INTR_STATE_STI)
  909. ret |= KVM_X86_SHADOW_INT_STI;
  910. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  911. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  912. return ret & mask;
  913. }
  914. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  915. {
  916. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  917. u32 interruptibility = interruptibility_old;
  918. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  919. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  920. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  921. else if (mask & KVM_X86_SHADOW_INT_STI)
  922. interruptibility |= GUEST_INTR_STATE_STI;
  923. if ((interruptibility != interruptibility_old))
  924. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  925. }
  926. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  927. {
  928. unsigned long rip;
  929. rip = kvm_rip_read(vcpu);
  930. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  931. kvm_rip_write(vcpu, rip);
  932. /* skipping an emulated instruction also counts */
  933. vmx_set_interrupt_shadow(vcpu, 0);
  934. }
  935. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  936. {
  937. /* Ensure that we clear the HLT state in the VMCS. We don't need to
  938. * explicitly skip the instruction because if the HLT state is set, then
  939. * the instruction is already executing and RIP has already been
  940. * advanced. */
  941. if (!yield_on_hlt &&
  942. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  943. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  944. }
  945. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  946. bool has_error_code, u32 error_code,
  947. bool reinject)
  948. {
  949. struct vcpu_vmx *vmx = to_vmx(vcpu);
  950. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  951. if (has_error_code) {
  952. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  953. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  954. }
  955. if (vmx->rmode.vm86_active) {
  956. int inc_eip = 0;
  957. if (kvm_exception_is_soft(nr))
  958. inc_eip = vcpu->arch.event_exit_inst_len;
  959. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  960. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  961. return;
  962. }
  963. if (kvm_exception_is_soft(nr)) {
  964. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  965. vmx->vcpu.arch.event_exit_inst_len);
  966. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  967. } else
  968. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  969. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  970. vmx_clear_hlt(vcpu);
  971. }
  972. static bool vmx_rdtscp_supported(void)
  973. {
  974. return cpu_has_vmx_rdtscp();
  975. }
  976. /*
  977. * Swap MSR entry in host/guest MSR entry array.
  978. */
  979. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  980. {
  981. struct shared_msr_entry tmp;
  982. tmp = vmx->guest_msrs[to];
  983. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  984. vmx->guest_msrs[from] = tmp;
  985. }
  986. /*
  987. * Set up the vmcs to automatically save and restore system
  988. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  989. * mode, as fiddling with msrs is very expensive.
  990. */
  991. static void setup_msrs(struct vcpu_vmx *vmx)
  992. {
  993. int save_nmsrs, index;
  994. unsigned long *msr_bitmap;
  995. vmx_load_host_state(vmx);
  996. save_nmsrs = 0;
  997. #ifdef CONFIG_X86_64
  998. if (is_long_mode(&vmx->vcpu)) {
  999. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1000. if (index >= 0)
  1001. move_msr_up(vmx, index, save_nmsrs++);
  1002. index = __find_msr_index(vmx, MSR_LSTAR);
  1003. if (index >= 0)
  1004. move_msr_up(vmx, index, save_nmsrs++);
  1005. index = __find_msr_index(vmx, MSR_CSTAR);
  1006. if (index >= 0)
  1007. move_msr_up(vmx, index, save_nmsrs++);
  1008. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1009. if (index >= 0 && vmx->rdtscp_enabled)
  1010. move_msr_up(vmx, index, save_nmsrs++);
  1011. /*
  1012. * MSR_STAR is only needed on long mode guests, and only
  1013. * if efer.sce is enabled.
  1014. */
  1015. index = __find_msr_index(vmx, MSR_STAR);
  1016. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1017. move_msr_up(vmx, index, save_nmsrs++);
  1018. }
  1019. #endif
  1020. index = __find_msr_index(vmx, MSR_EFER);
  1021. if (index >= 0 && update_transition_efer(vmx, index))
  1022. move_msr_up(vmx, index, save_nmsrs++);
  1023. vmx->save_nmsrs = save_nmsrs;
  1024. if (cpu_has_vmx_msr_bitmap()) {
  1025. if (is_long_mode(&vmx->vcpu))
  1026. msr_bitmap = vmx_msr_bitmap_longmode;
  1027. else
  1028. msr_bitmap = vmx_msr_bitmap_legacy;
  1029. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1030. }
  1031. }
  1032. /*
  1033. * reads and returns guest's timestamp counter "register"
  1034. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1035. */
  1036. static u64 guest_read_tsc(void)
  1037. {
  1038. u64 host_tsc, tsc_offset;
  1039. rdtscll(host_tsc);
  1040. tsc_offset = vmcs_read64(TSC_OFFSET);
  1041. return host_tsc + tsc_offset;
  1042. }
  1043. /*
  1044. * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
  1045. * ioctl. In this case the call-back should update internal vmx state to make
  1046. * the changes effective.
  1047. */
  1048. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
  1049. {
  1050. /* Nothing to do here */
  1051. }
  1052. /*
  1053. * writes 'offset' into guest's timestamp counter offset register
  1054. */
  1055. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1056. {
  1057. vmcs_write64(TSC_OFFSET, offset);
  1058. }
  1059. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
  1060. {
  1061. u64 offset = vmcs_read64(TSC_OFFSET);
  1062. vmcs_write64(TSC_OFFSET, offset + adjustment);
  1063. }
  1064. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  1065. {
  1066. return target_tsc - native_read_tsc();
  1067. }
  1068. /*
  1069. * Reads an msr value (of 'msr_index') into 'pdata'.
  1070. * Returns 0 on success, non-0 otherwise.
  1071. * Assumes vcpu_load() was already called.
  1072. */
  1073. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1074. {
  1075. u64 data;
  1076. struct shared_msr_entry *msr;
  1077. if (!pdata) {
  1078. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  1079. return -EINVAL;
  1080. }
  1081. switch (msr_index) {
  1082. #ifdef CONFIG_X86_64
  1083. case MSR_FS_BASE:
  1084. data = vmcs_readl(GUEST_FS_BASE);
  1085. break;
  1086. case MSR_GS_BASE:
  1087. data = vmcs_readl(GUEST_GS_BASE);
  1088. break;
  1089. case MSR_KERNEL_GS_BASE:
  1090. vmx_load_host_state(to_vmx(vcpu));
  1091. data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  1092. break;
  1093. #endif
  1094. case MSR_EFER:
  1095. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1096. case MSR_IA32_TSC:
  1097. data = guest_read_tsc();
  1098. break;
  1099. case MSR_IA32_SYSENTER_CS:
  1100. data = vmcs_read32(GUEST_SYSENTER_CS);
  1101. break;
  1102. case MSR_IA32_SYSENTER_EIP:
  1103. data = vmcs_readl(GUEST_SYSENTER_EIP);
  1104. break;
  1105. case MSR_IA32_SYSENTER_ESP:
  1106. data = vmcs_readl(GUEST_SYSENTER_ESP);
  1107. break;
  1108. case MSR_TSC_AUX:
  1109. if (!to_vmx(vcpu)->rdtscp_enabled)
  1110. return 1;
  1111. /* Otherwise falls through */
  1112. default:
  1113. vmx_load_host_state(to_vmx(vcpu));
  1114. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  1115. if (msr) {
  1116. vmx_load_host_state(to_vmx(vcpu));
  1117. data = msr->data;
  1118. break;
  1119. }
  1120. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1121. }
  1122. *pdata = data;
  1123. return 0;
  1124. }
  1125. /*
  1126. * Writes msr value into into the appropriate "register".
  1127. * Returns 0 on success, non-0 otherwise.
  1128. * Assumes vcpu_load() was already called.
  1129. */
  1130. static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1131. {
  1132. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1133. struct shared_msr_entry *msr;
  1134. int ret = 0;
  1135. switch (msr_index) {
  1136. case MSR_EFER:
  1137. vmx_load_host_state(vmx);
  1138. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1139. break;
  1140. #ifdef CONFIG_X86_64
  1141. case MSR_FS_BASE:
  1142. vmx_segment_cache_clear(vmx);
  1143. vmcs_writel(GUEST_FS_BASE, data);
  1144. break;
  1145. case MSR_GS_BASE:
  1146. vmx_segment_cache_clear(vmx);
  1147. vmcs_writel(GUEST_GS_BASE, data);
  1148. break;
  1149. case MSR_KERNEL_GS_BASE:
  1150. vmx_load_host_state(vmx);
  1151. vmx->msr_guest_kernel_gs_base = data;
  1152. break;
  1153. #endif
  1154. case MSR_IA32_SYSENTER_CS:
  1155. vmcs_write32(GUEST_SYSENTER_CS, data);
  1156. break;
  1157. case MSR_IA32_SYSENTER_EIP:
  1158. vmcs_writel(GUEST_SYSENTER_EIP, data);
  1159. break;
  1160. case MSR_IA32_SYSENTER_ESP:
  1161. vmcs_writel(GUEST_SYSENTER_ESP, data);
  1162. break;
  1163. case MSR_IA32_TSC:
  1164. kvm_write_tsc(vcpu, data);
  1165. break;
  1166. case MSR_IA32_CR_PAT:
  1167. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  1168. vmcs_write64(GUEST_IA32_PAT, data);
  1169. vcpu->arch.pat = data;
  1170. break;
  1171. }
  1172. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1173. break;
  1174. case MSR_TSC_AUX:
  1175. if (!vmx->rdtscp_enabled)
  1176. return 1;
  1177. /* Check reserved bit, higher 32 bits should be zero */
  1178. if ((data >> 32) != 0)
  1179. return 1;
  1180. /* Otherwise falls through */
  1181. default:
  1182. msr = find_msr_entry(vmx, msr_index);
  1183. if (msr) {
  1184. vmx_load_host_state(vmx);
  1185. msr->data = data;
  1186. break;
  1187. }
  1188. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1189. }
  1190. return ret;
  1191. }
  1192. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1193. {
  1194. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  1195. switch (reg) {
  1196. case VCPU_REGS_RSP:
  1197. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  1198. break;
  1199. case VCPU_REGS_RIP:
  1200. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  1201. break;
  1202. case VCPU_EXREG_PDPTR:
  1203. if (enable_ept)
  1204. ept_save_pdptrs(vcpu);
  1205. break;
  1206. default:
  1207. break;
  1208. }
  1209. }
  1210. static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1211. {
  1212. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1213. vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
  1214. else
  1215. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  1216. update_exception_bitmap(vcpu);
  1217. }
  1218. static __init int cpu_has_kvm_support(void)
  1219. {
  1220. return cpu_has_vmx();
  1221. }
  1222. static __init int vmx_disabled_by_bios(void)
  1223. {
  1224. u64 msr;
  1225. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1226. if (msr & FEATURE_CONTROL_LOCKED) {
  1227. /* launched w/ TXT and VMX disabled */
  1228. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  1229. && tboot_enabled())
  1230. return 1;
  1231. /* launched w/o TXT and VMX only enabled w/ TXT */
  1232. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  1233. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  1234. && !tboot_enabled()) {
  1235. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  1236. "activate TXT before enabling KVM\n");
  1237. return 1;
  1238. }
  1239. /* launched w/o TXT and VMX disabled */
  1240. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  1241. && !tboot_enabled())
  1242. return 1;
  1243. }
  1244. return 0;
  1245. }
  1246. static void kvm_cpu_vmxon(u64 addr)
  1247. {
  1248. asm volatile (ASM_VMX_VMXON_RAX
  1249. : : "a"(&addr), "m"(addr)
  1250. : "memory", "cc");
  1251. }
  1252. static int hardware_enable(void *garbage)
  1253. {
  1254. int cpu = raw_smp_processor_id();
  1255. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1256. u64 old, test_bits;
  1257. if (read_cr4() & X86_CR4_VMXE)
  1258. return -EBUSY;
  1259. INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
  1260. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  1261. test_bits = FEATURE_CONTROL_LOCKED;
  1262. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  1263. if (tboot_enabled())
  1264. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  1265. if ((old & test_bits) != test_bits) {
  1266. /* enable and lock */
  1267. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  1268. }
  1269. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  1270. if (vmm_exclusive) {
  1271. kvm_cpu_vmxon(phys_addr);
  1272. ept_sync_global();
  1273. }
  1274. store_gdt(&__get_cpu_var(host_gdt));
  1275. return 0;
  1276. }
  1277. static void vmclear_local_vcpus(void)
  1278. {
  1279. int cpu = raw_smp_processor_id();
  1280. struct vcpu_vmx *vmx, *n;
  1281. list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
  1282. local_vcpus_link)
  1283. __vcpu_clear(vmx);
  1284. }
  1285. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  1286. * tricks.
  1287. */
  1288. static void kvm_cpu_vmxoff(void)
  1289. {
  1290. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  1291. }
  1292. static void hardware_disable(void *garbage)
  1293. {
  1294. if (vmm_exclusive) {
  1295. vmclear_local_vcpus();
  1296. kvm_cpu_vmxoff();
  1297. }
  1298. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  1299. }
  1300. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  1301. u32 msr, u32 *result)
  1302. {
  1303. u32 vmx_msr_low, vmx_msr_high;
  1304. u32 ctl = ctl_min | ctl_opt;
  1305. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  1306. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  1307. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  1308. /* Ensure minimum (required) set of control bits are supported. */
  1309. if (ctl_min & ~ctl)
  1310. return -EIO;
  1311. *result = ctl;
  1312. return 0;
  1313. }
  1314. static __init bool allow_1_setting(u32 msr, u32 ctl)
  1315. {
  1316. u32 vmx_msr_low, vmx_msr_high;
  1317. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  1318. return vmx_msr_high & ctl;
  1319. }
  1320. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  1321. {
  1322. u32 vmx_msr_low, vmx_msr_high;
  1323. u32 min, opt, min2, opt2;
  1324. u32 _pin_based_exec_control = 0;
  1325. u32 _cpu_based_exec_control = 0;
  1326. u32 _cpu_based_2nd_exec_control = 0;
  1327. u32 _vmexit_control = 0;
  1328. u32 _vmentry_control = 0;
  1329. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  1330. opt = PIN_BASED_VIRTUAL_NMIS;
  1331. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  1332. &_pin_based_exec_control) < 0)
  1333. return -EIO;
  1334. min =
  1335. #ifdef CONFIG_X86_64
  1336. CPU_BASED_CR8_LOAD_EXITING |
  1337. CPU_BASED_CR8_STORE_EXITING |
  1338. #endif
  1339. CPU_BASED_CR3_LOAD_EXITING |
  1340. CPU_BASED_CR3_STORE_EXITING |
  1341. CPU_BASED_USE_IO_BITMAPS |
  1342. CPU_BASED_MOV_DR_EXITING |
  1343. CPU_BASED_USE_TSC_OFFSETING |
  1344. CPU_BASED_MWAIT_EXITING |
  1345. CPU_BASED_MONITOR_EXITING |
  1346. CPU_BASED_INVLPG_EXITING;
  1347. if (yield_on_hlt)
  1348. min |= CPU_BASED_HLT_EXITING;
  1349. opt = CPU_BASED_TPR_SHADOW |
  1350. CPU_BASED_USE_MSR_BITMAPS |
  1351. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1352. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  1353. &_cpu_based_exec_control) < 0)
  1354. return -EIO;
  1355. #ifdef CONFIG_X86_64
  1356. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  1357. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  1358. ~CPU_BASED_CR8_STORE_EXITING;
  1359. #endif
  1360. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  1361. min2 = 0;
  1362. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  1363. SECONDARY_EXEC_WBINVD_EXITING |
  1364. SECONDARY_EXEC_ENABLE_VPID |
  1365. SECONDARY_EXEC_ENABLE_EPT |
  1366. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  1367. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  1368. SECONDARY_EXEC_RDTSCP;
  1369. if (adjust_vmx_controls(min2, opt2,
  1370. MSR_IA32_VMX_PROCBASED_CTLS2,
  1371. &_cpu_based_2nd_exec_control) < 0)
  1372. return -EIO;
  1373. }
  1374. #ifndef CONFIG_X86_64
  1375. if (!(_cpu_based_2nd_exec_control &
  1376. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  1377. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  1378. #endif
  1379. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  1380. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  1381. enabled */
  1382. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  1383. CPU_BASED_CR3_STORE_EXITING |
  1384. CPU_BASED_INVLPG_EXITING);
  1385. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  1386. vmx_capability.ept, vmx_capability.vpid);
  1387. }
  1388. min = 0;
  1389. #ifdef CONFIG_X86_64
  1390. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1391. #endif
  1392. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
  1393. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  1394. &_vmexit_control) < 0)
  1395. return -EIO;
  1396. min = 0;
  1397. opt = VM_ENTRY_LOAD_IA32_PAT;
  1398. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  1399. &_vmentry_control) < 0)
  1400. return -EIO;
  1401. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  1402. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  1403. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  1404. return -EIO;
  1405. #ifdef CONFIG_X86_64
  1406. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  1407. if (vmx_msr_high & (1u<<16))
  1408. return -EIO;
  1409. #endif
  1410. /* Require Write-Back (WB) memory type for VMCS accesses. */
  1411. if (((vmx_msr_high >> 18) & 15) != 6)
  1412. return -EIO;
  1413. vmcs_conf->size = vmx_msr_high & 0x1fff;
  1414. vmcs_conf->order = get_order(vmcs_config.size);
  1415. vmcs_conf->revision_id = vmx_msr_low;
  1416. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  1417. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  1418. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  1419. vmcs_conf->vmexit_ctrl = _vmexit_control;
  1420. vmcs_conf->vmentry_ctrl = _vmentry_control;
  1421. cpu_has_load_ia32_efer =
  1422. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  1423. VM_ENTRY_LOAD_IA32_EFER)
  1424. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  1425. VM_EXIT_LOAD_IA32_EFER);
  1426. return 0;
  1427. }
  1428. static struct vmcs *alloc_vmcs_cpu(int cpu)
  1429. {
  1430. int node = cpu_to_node(cpu);
  1431. struct page *pages;
  1432. struct vmcs *vmcs;
  1433. pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
  1434. if (!pages)
  1435. return NULL;
  1436. vmcs = page_address(pages);
  1437. memset(vmcs, 0, vmcs_config.size);
  1438. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  1439. return vmcs;
  1440. }
  1441. static struct vmcs *alloc_vmcs(void)
  1442. {
  1443. return alloc_vmcs_cpu(raw_smp_processor_id());
  1444. }
  1445. static void free_vmcs(struct vmcs *vmcs)
  1446. {
  1447. free_pages((unsigned long)vmcs, vmcs_config.order);
  1448. }
  1449. static void free_kvm_area(void)
  1450. {
  1451. int cpu;
  1452. for_each_possible_cpu(cpu) {
  1453. free_vmcs(per_cpu(vmxarea, cpu));
  1454. per_cpu(vmxarea, cpu) = NULL;
  1455. }
  1456. }
  1457. static __init int alloc_kvm_area(void)
  1458. {
  1459. int cpu;
  1460. for_each_possible_cpu(cpu) {
  1461. struct vmcs *vmcs;
  1462. vmcs = alloc_vmcs_cpu(cpu);
  1463. if (!vmcs) {
  1464. free_kvm_area();
  1465. return -ENOMEM;
  1466. }
  1467. per_cpu(vmxarea, cpu) = vmcs;
  1468. }
  1469. return 0;
  1470. }
  1471. static __init int hardware_setup(void)
  1472. {
  1473. if (setup_vmcs_config(&vmcs_config) < 0)
  1474. return -EIO;
  1475. if (boot_cpu_has(X86_FEATURE_NX))
  1476. kvm_enable_efer_bits(EFER_NX);
  1477. if (!cpu_has_vmx_vpid())
  1478. enable_vpid = 0;
  1479. if (!cpu_has_vmx_ept() ||
  1480. !cpu_has_vmx_ept_4levels()) {
  1481. enable_ept = 0;
  1482. enable_unrestricted_guest = 0;
  1483. }
  1484. if (!cpu_has_vmx_unrestricted_guest())
  1485. enable_unrestricted_guest = 0;
  1486. if (!cpu_has_vmx_flexpriority())
  1487. flexpriority_enabled = 0;
  1488. if (!cpu_has_vmx_tpr_shadow())
  1489. kvm_x86_ops->update_cr8_intercept = NULL;
  1490. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  1491. kvm_disable_largepages();
  1492. if (!cpu_has_vmx_ple())
  1493. ple_gap = 0;
  1494. return alloc_kvm_area();
  1495. }
  1496. static __exit void hardware_unsetup(void)
  1497. {
  1498. free_kvm_area();
  1499. }
  1500. static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
  1501. {
  1502. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1503. if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
  1504. vmcs_write16(sf->selector, save->selector);
  1505. vmcs_writel(sf->base, save->base);
  1506. vmcs_write32(sf->limit, save->limit);
  1507. vmcs_write32(sf->ar_bytes, save->ar);
  1508. } else {
  1509. u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
  1510. << AR_DPL_SHIFT;
  1511. vmcs_write32(sf->ar_bytes, 0x93 | dpl);
  1512. }
  1513. }
  1514. static void enter_pmode(struct kvm_vcpu *vcpu)
  1515. {
  1516. unsigned long flags;
  1517. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1518. vmx->emulation_required = 1;
  1519. vmx->rmode.vm86_active = 0;
  1520. vmx_segment_cache_clear(vmx);
  1521. vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
  1522. vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
  1523. vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
  1524. vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
  1525. flags = vmcs_readl(GUEST_RFLAGS);
  1526. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1527. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1528. vmcs_writel(GUEST_RFLAGS, flags);
  1529. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  1530. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  1531. update_exception_bitmap(vcpu);
  1532. if (emulate_invalid_guest_state)
  1533. return;
  1534. fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
  1535. fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
  1536. fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
  1537. fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
  1538. vmx_segment_cache_clear(vmx);
  1539. vmcs_write16(GUEST_SS_SELECTOR, 0);
  1540. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  1541. vmcs_write16(GUEST_CS_SELECTOR,
  1542. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  1543. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  1544. }
  1545. static gva_t rmode_tss_base(struct kvm *kvm)
  1546. {
  1547. if (!kvm->arch.tss_addr) {
  1548. struct kvm_memslots *slots;
  1549. gfn_t base_gfn;
  1550. slots = kvm_memslots(kvm);
  1551. base_gfn = slots->memslots[0].base_gfn +
  1552. kvm->memslots->memslots[0].npages - 3;
  1553. return base_gfn << PAGE_SHIFT;
  1554. }
  1555. return kvm->arch.tss_addr;
  1556. }
  1557. static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
  1558. {
  1559. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1560. save->selector = vmcs_read16(sf->selector);
  1561. save->base = vmcs_readl(sf->base);
  1562. save->limit = vmcs_read32(sf->limit);
  1563. save->ar = vmcs_read32(sf->ar_bytes);
  1564. vmcs_write16(sf->selector, save->base >> 4);
  1565. vmcs_write32(sf->base, save->base & 0xffff0);
  1566. vmcs_write32(sf->limit, 0xffff);
  1567. vmcs_write32(sf->ar_bytes, 0xf3);
  1568. if (save->base & 0xf)
  1569. printk_once(KERN_WARNING "kvm: segment base is not paragraph"
  1570. " aligned when entering protected mode (seg=%d)",
  1571. seg);
  1572. }
  1573. static void enter_rmode(struct kvm_vcpu *vcpu)
  1574. {
  1575. unsigned long flags;
  1576. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1577. if (enable_unrestricted_guest)
  1578. return;
  1579. vmx->emulation_required = 1;
  1580. vmx->rmode.vm86_active = 1;
  1581. /*
  1582. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  1583. * vcpu. Call it here with phys address pointing 16M below 4G.
  1584. */
  1585. if (!vcpu->kvm->arch.tss_addr) {
  1586. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  1587. "called before entering vcpu\n");
  1588. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  1589. vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
  1590. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1591. }
  1592. vmx_segment_cache_clear(vmx);
  1593. vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
  1594. vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
  1595. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  1596. vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
  1597. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  1598. vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1599. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1600. flags = vmcs_readl(GUEST_RFLAGS);
  1601. vmx->rmode.save_rflags = flags;
  1602. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1603. vmcs_writel(GUEST_RFLAGS, flags);
  1604. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  1605. update_exception_bitmap(vcpu);
  1606. if (emulate_invalid_guest_state)
  1607. goto continue_rmode;
  1608. vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
  1609. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  1610. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  1611. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  1612. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  1613. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  1614. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  1615. vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
  1616. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
  1617. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
  1618. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
  1619. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
  1620. continue_rmode:
  1621. kvm_mmu_reset_context(vcpu);
  1622. }
  1623. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1624. {
  1625. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1626. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  1627. if (!msr)
  1628. return;
  1629. /*
  1630. * Force kernel_gs_base reloading before EFER changes, as control
  1631. * of this msr depends on is_long_mode().
  1632. */
  1633. vmx_load_host_state(to_vmx(vcpu));
  1634. vcpu->arch.efer = efer;
  1635. if (efer & EFER_LMA) {
  1636. vmcs_write32(VM_ENTRY_CONTROLS,
  1637. vmcs_read32(VM_ENTRY_CONTROLS) |
  1638. VM_ENTRY_IA32E_MODE);
  1639. msr->data = efer;
  1640. } else {
  1641. vmcs_write32(VM_ENTRY_CONTROLS,
  1642. vmcs_read32(VM_ENTRY_CONTROLS) &
  1643. ~VM_ENTRY_IA32E_MODE);
  1644. msr->data = efer & ~EFER_LME;
  1645. }
  1646. setup_msrs(vmx);
  1647. }
  1648. #ifdef CONFIG_X86_64
  1649. static void enter_lmode(struct kvm_vcpu *vcpu)
  1650. {
  1651. u32 guest_tr_ar;
  1652. vmx_segment_cache_clear(to_vmx(vcpu));
  1653. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1654. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  1655. printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
  1656. __func__);
  1657. vmcs_write32(GUEST_TR_AR_BYTES,
  1658. (guest_tr_ar & ~AR_TYPE_MASK)
  1659. | AR_TYPE_BUSY_64_TSS);
  1660. }
  1661. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  1662. }
  1663. static void exit_lmode(struct kvm_vcpu *vcpu)
  1664. {
  1665. vmcs_write32(VM_ENTRY_CONTROLS,
  1666. vmcs_read32(VM_ENTRY_CONTROLS)
  1667. & ~VM_ENTRY_IA32E_MODE);
  1668. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  1669. }
  1670. #endif
  1671. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  1672. {
  1673. vpid_sync_context(to_vmx(vcpu));
  1674. if (enable_ept) {
  1675. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  1676. return;
  1677. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  1678. }
  1679. }
  1680. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1681. {
  1682. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  1683. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  1684. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  1685. }
  1686. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  1687. {
  1688. if (enable_ept && is_paging(vcpu))
  1689. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  1690. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  1691. }
  1692. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1693. {
  1694. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  1695. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  1696. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  1697. }
  1698. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  1699. {
  1700. if (!test_bit(VCPU_EXREG_PDPTR,
  1701. (unsigned long *)&vcpu->arch.regs_dirty))
  1702. return;
  1703. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1704. vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
  1705. vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
  1706. vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
  1707. vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
  1708. }
  1709. }
  1710. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  1711. {
  1712. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1713. vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  1714. vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  1715. vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  1716. vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  1717. }
  1718. __set_bit(VCPU_EXREG_PDPTR,
  1719. (unsigned long *)&vcpu->arch.regs_avail);
  1720. __set_bit(VCPU_EXREG_PDPTR,
  1721. (unsigned long *)&vcpu->arch.regs_dirty);
  1722. }
  1723. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  1724. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  1725. unsigned long cr0,
  1726. struct kvm_vcpu *vcpu)
  1727. {
  1728. vmx_decache_cr3(vcpu);
  1729. if (!(cr0 & X86_CR0_PG)) {
  1730. /* From paging/starting to nonpaging */
  1731. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1732. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  1733. (CPU_BASED_CR3_LOAD_EXITING |
  1734. CPU_BASED_CR3_STORE_EXITING));
  1735. vcpu->arch.cr0 = cr0;
  1736. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  1737. } else if (!is_paging(vcpu)) {
  1738. /* From nonpaging to paging */
  1739. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1740. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  1741. ~(CPU_BASED_CR3_LOAD_EXITING |
  1742. CPU_BASED_CR3_STORE_EXITING));
  1743. vcpu->arch.cr0 = cr0;
  1744. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  1745. }
  1746. if (!(cr0 & X86_CR0_WP))
  1747. *hw_cr0 &= ~X86_CR0_WP;
  1748. }
  1749. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1750. {
  1751. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1752. unsigned long hw_cr0;
  1753. if (enable_unrestricted_guest)
  1754. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
  1755. | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  1756. else
  1757. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
  1758. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  1759. enter_pmode(vcpu);
  1760. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  1761. enter_rmode(vcpu);
  1762. #ifdef CONFIG_X86_64
  1763. if (vcpu->arch.efer & EFER_LME) {
  1764. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  1765. enter_lmode(vcpu);
  1766. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  1767. exit_lmode(vcpu);
  1768. }
  1769. #endif
  1770. if (enable_ept)
  1771. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  1772. if (!vcpu->fpu_active)
  1773. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  1774. vmcs_writel(CR0_READ_SHADOW, cr0);
  1775. vmcs_writel(GUEST_CR0, hw_cr0);
  1776. vcpu->arch.cr0 = cr0;
  1777. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1778. }
  1779. static u64 construct_eptp(unsigned long root_hpa)
  1780. {
  1781. u64 eptp;
  1782. /* TODO write the value reading from MSR */
  1783. eptp = VMX_EPT_DEFAULT_MT |
  1784. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  1785. eptp |= (root_hpa & PAGE_MASK);
  1786. return eptp;
  1787. }
  1788. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  1789. {
  1790. unsigned long guest_cr3;
  1791. u64 eptp;
  1792. guest_cr3 = cr3;
  1793. if (enable_ept) {
  1794. eptp = construct_eptp(cr3);
  1795. vmcs_write64(EPT_POINTER, eptp);
  1796. guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
  1797. vcpu->kvm->arch.ept_identity_map_addr;
  1798. ept_load_pdptrs(vcpu);
  1799. }
  1800. vmx_flush_tlb(vcpu);
  1801. vmcs_writel(GUEST_CR3, guest_cr3);
  1802. }
  1803. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1804. {
  1805. unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
  1806. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  1807. vcpu->arch.cr4 = cr4;
  1808. if (enable_ept) {
  1809. if (!is_paging(vcpu)) {
  1810. hw_cr4 &= ~X86_CR4_PAE;
  1811. hw_cr4 |= X86_CR4_PSE;
  1812. } else if (!(cr4 & X86_CR4_PAE)) {
  1813. hw_cr4 &= ~X86_CR4_PAE;
  1814. }
  1815. }
  1816. vmcs_writel(CR4_READ_SHADOW, cr4);
  1817. vmcs_writel(GUEST_CR4, hw_cr4);
  1818. }
  1819. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1820. struct kvm_segment *var, int seg)
  1821. {
  1822. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1823. struct kvm_save_segment *save;
  1824. u32 ar;
  1825. if (vmx->rmode.vm86_active
  1826. && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
  1827. || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
  1828. || seg == VCPU_SREG_GS)
  1829. && !emulate_invalid_guest_state) {
  1830. switch (seg) {
  1831. case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
  1832. case VCPU_SREG_ES: save = &vmx->rmode.es; break;
  1833. case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
  1834. case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
  1835. case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
  1836. default: BUG();
  1837. }
  1838. var->selector = save->selector;
  1839. var->base = save->base;
  1840. var->limit = save->limit;
  1841. ar = save->ar;
  1842. if (seg == VCPU_SREG_TR
  1843. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  1844. goto use_saved_rmode_seg;
  1845. }
  1846. var->base = vmx_read_guest_seg_base(vmx, seg);
  1847. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  1848. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  1849. ar = vmx_read_guest_seg_ar(vmx, seg);
  1850. use_saved_rmode_seg:
  1851. if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
  1852. ar = 0;
  1853. var->type = ar & 15;
  1854. var->s = (ar >> 4) & 1;
  1855. var->dpl = (ar >> 5) & 3;
  1856. var->present = (ar >> 7) & 1;
  1857. var->avl = (ar >> 12) & 1;
  1858. var->l = (ar >> 13) & 1;
  1859. var->db = (ar >> 14) & 1;
  1860. var->g = (ar >> 15) & 1;
  1861. var->unusable = (ar >> 16) & 1;
  1862. }
  1863. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1864. {
  1865. struct kvm_segment s;
  1866. if (to_vmx(vcpu)->rmode.vm86_active) {
  1867. vmx_get_segment(vcpu, &s, seg);
  1868. return s.base;
  1869. }
  1870. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  1871. }
  1872. static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
  1873. {
  1874. if (!is_protmode(vcpu))
  1875. return 0;
  1876. if (!is_long_mode(vcpu)
  1877. && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
  1878. return 3;
  1879. return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
  1880. }
  1881. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  1882. {
  1883. if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
  1884. __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1885. to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
  1886. }
  1887. return to_vmx(vcpu)->cpl;
  1888. }
  1889. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  1890. {
  1891. u32 ar;
  1892. if (var->unusable)
  1893. ar = 1 << 16;
  1894. else {
  1895. ar = var->type & 15;
  1896. ar |= (var->s & 1) << 4;
  1897. ar |= (var->dpl & 3) << 5;
  1898. ar |= (var->present & 1) << 7;
  1899. ar |= (var->avl & 1) << 12;
  1900. ar |= (var->l & 1) << 13;
  1901. ar |= (var->db & 1) << 14;
  1902. ar |= (var->g & 1) << 15;
  1903. }
  1904. if (ar == 0) /* a 0 value means unusable */
  1905. ar = AR_UNUSABLE_MASK;
  1906. return ar;
  1907. }
  1908. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1909. struct kvm_segment *var, int seg)
  1910. {
  1911. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1912. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1913. u32 ar;
  1914. vmx_segment_cache_clear(vmx);
  1915. if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
  1916. vmcs_write16(sf->selector, var->selector);
  1917. vmx->rmode.tr.selector = var->selector;
  1918. vmx->rmode.tr.base = var->base;
  1919. vmx->rmode.tr.limit = var->limit;
  1920. vmx->rmode.tr.ar = vmx_segment_access_rights(var);
  1921. return;
  1922. }
  1923. vmcs_writel(sf->base, var->base);
  1924. vmcs_write32(sf->limit, var->limit);
  1925. vmcs_write16(sf->selector, var->selector);
  1926. if (vmx->rmode.vm86_active && var->s) {
  1927. /*
  1928. * Hack real-mode segments into vm86 compatibility.
  1929. */
  1930. if (var->base == 0xffff0000 && var->selector == 0xf000)
  1931. vmcs_writel(sf->base, 0xf0000);
  1932. ar = 0xf3;
  1933. } else
  1934. ar = vmx_segment_access_rights(var);
  1935. /*
  1936. * Fix the "Accessed" bit in AR field of segment registers for older
  1937. * qemu binaries.
  1938. * IA32 arch specifies that at the time of processor reset the
  1939. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  1940. * is setting it to 0 in the usedland code. This causes invalid guest
  1941. * state vmexit when "unrestricted guest" mode is turned on.
  1942. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  1943. * tree. Newer qemu binaries with that qemu fix would not need this
  1944. * kvm hack.
  1945. */
  1946. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  1947. ar |= 0x1; /* Accessed */
  1948. vmcs_write32(sf->ar_bytes, ar);
  1949. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1950. }
  1951. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  1952. {
  1953. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  1954. *db = (ar >> 14) & 1;
  1955. *l = (ar >> 13) & 1;
  1956. }
  1957. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1958. {
  1959. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  1960. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  1961. }
  1962. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1963. {
  1964. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  1965. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  1966. }
  1967. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1968. {
  1969. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  1970. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  1971. }
  1972. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1973. {
  1974. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  1975. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  1976. }
  1977. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1978. {
  1979. struct kvm_segment var;
  1980. u32 ar;
  1981. vmx_get_segment(vcpu, &var, seg);
  1982. ar = vmx_segment_access_rights(&var);
  1983. if (var.base != (var.selector << 4))
  1984. return false;
  1985. if (var.limit != 0xffff)
  1986. return false;
  1987. if (ar != 0xf3)
  1988. return false;
  1989. return true;
  1990. }
  1991. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  1992. {
  1993. struct kvm_segment cs;
  1994. unsigned int cs_rpl;
  1995. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1996. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  1997. if (cs.unusable)
  1998. return false;
  1999. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  2000. return false;
  2001. if (!cs.s)
  2002. return false;
  2003. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  2004. if (cs.dpl > cs_rpl)
  2005. return false;
  2006. } else {
  2007. if (cs.dpl != cs_rpl)
  2008. return false;
  2009. }
  2010. if (!cs.present)
  2011. return false;
  2012. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  2013. return true;
  2014. }
  2015. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  2016. {
  2017. struct kvm_segment ss;
  2018. unsigned int ss_rpl;
  2019. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2020. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  2021. if (ss.unusable)
  2022. return true;
  2023. if (ss.type != 3 && ss.type != 7)
  2024. return false;
  2025. if (!ss.s)
  2026. return false;
  2027. if (ss.dpl != ss_rpl) /* DPL != RPL */
  2028. return false;
  2029. if (!ss.present)
  2030. return false;
  2031. return true;
  2032. }
  2033. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2034. {
  2035. struct kvm_segment var;
  2036. unsigned int rpl;
  2037. vmx_get_segment(vcpu, &var, seg);
  2038. rpl = var.selector & SELECTOR_RPL_MASK;
  2039. if (var.unusable)
  2040. return true;
  2041. if (!var.s)
  2042. return false;
  2043. if (!var.present)
  2044. return false;
  2045. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  2046. if (var.dpl < rpl) /* DPL < RPL */
  2047. return false;
  2048. }
  2049. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  2050. * rights flags
  2051. */
  2052. return true;
  2053. }
  2054. static bool tr_valid(struct kvm_vcpu *vcpu)
  2055. {
  2056. struct kvm_segment tr;
  2057. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  2058. if (tr.unusable)
  2059. return false;
  2060. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2061. return false;
  2062. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  2063. return false;
  2064. if (!tr.present)
  2065. return false;
  2066. return true;
  2067. }
  2068. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  2069. {
  2070. struct kvm_segment ldtr;
  2071. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  2072. if (ldtr.unusable)
  2073. return true;
  2074. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2075. return false;
  2076. if (ldtr.type != 2)
  2077. return false;
  2078. if (!ldtr.present)
  2079. return false;
  2080. return true;
  2081. }
  2082. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  2083. {
  2084. struct kvm_segment cs, ss;
  2085. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2086. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2087. return ((cs.selector & SELECTOR_RPL_MASK) ==
  2088. (ss.selector & SELECTOR_RPL_MASK));
  2089. }
  2090. /*
  2091. * Check if guest state is valid. Returns true if valid, false if
  2092. * not.
  2093. * We assume that registers are always usable
  2094. */
  2095. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  2096. {
  2097. /* real mode guest state checks */
  2098. if (!is_protmode(vcpu)) {
  2099. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  2100. return false;
  2101. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  2102. return false;
  2103. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  2104. return false;
  2105. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  2106. return false;
  2107. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  2108. return false;
  2109. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  2110. return false;
  2111. } else {
  2112. /* protected mode guest state checks */
  2113. if (!cs_ss_rpl_check(vcpu))
  2114. return false;
  2115. if (!code_segment_valid(vcpu))
  2116. return false;
  2117. if (!stack_segment_valid(vcpu))
  2118. return false;
  2119. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  2120. return false;
  2121. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  2122. return false;
  2123. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  2124. return false;
  2125. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  2126. return false;
  2127. if (!tr_valid(vcpu))
  2128. return false;
  2129. if (!ldtr_valid(vcpu))
  2130. return false;
  2131. }
  2132. /* TODO:
  2133. * - Add checks on RIP
  2134. * - Add checks on RFLAGS
  2135. */
  2136. return true;
  2137. }
  2138. static int init_rmode_tss(struct kvm *kvm)
  2139. {
  2140. gfn_t fn;
  2141. u16 data = 0;
  2142. int r, idx, ret = 0;
  2143. idx = srcu_read_lock(&kvm->srcu);
  2144. fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  2145. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2146. if (r < 0)
  2147. goto out;
  2148. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  2149. r = kvm_write_guest_page(kvm, fn++, &data,
  2150. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  2151. if (r < 0)
  2152. goto out;
  2153. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  2154. if (r < 0)
  2155. goto out;
  2156. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2157. if (r < 0)
  2158. goto out;
  2159. data = ~0;
  2160. r = kvm_write_guest_page(kvm, fn, &data,
  2161. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  2162. sizeof(u8));
  2163. if (r < 0)
  2164. goto out;
  2165. ret = 1;
  2166. out:
  2167. srcu_read_unlock(&kvm->srcu, idx);
  2168. return ret;
  2169. }
  2170. static int init_rmode_identity_map(struct kvm *kvm)
  2171. {
  2172. int i, idx, r, ret;
  2173. pfn_t identity_map_pfn;
  2174. u32 tmp;
  2175. if (!enable_ept)
  2176. return 1;
  2177. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  2178. printk(KERN_ERR "EPT: identity-mapping pagetable "
  2179. "haven't been allocated!\n");
  2180. return 0;
  2181. }
  2182. if (likely(kvm->arch.ept_identity_pagetable_done))
  2183. return 1;
  2184. ret = 0;
  2185. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  2186. idx = srcu_read_lock(&kvm->srcu);
  2187. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  2188. if (r < 0)
  2189. goto out;
  2190. /* Set up identity-mapping pagetable for EPT in real mode */
  2191. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  2192. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  2193. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  2194. r = kvm_write_guest_page(kvm, identity_map_pfn,
  2195. &tmp, i * sizeof(tmp), sizeof(tmp));
  2196. if (r < 0)
  2197. goto out;
  2198. }
  2199. kvm->arch.ept_identity_pagetable_done = true;
  2200. ret = 1;
  2201. out:
  2202. srcu_read_unlock(&kvm->srcu, idx);
  2203. return ret;
  2204. }
  2205. static void seg_setup(int seg)
  2206. {
  2207. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2208. unsigned int ar;
  2209. vmcs_write16(sf->selector, 0);
  2210. vmcs_writel(sf->base, 0);
  2211. vmcs_write32(sf->limit, 0xffff);
  2212. if (enable_unrestricted_guest) {
  2213. ar = 0x93;
  2214. if (seg == VCPU_SREG_CS)
  2215. ar |= 0x08; /* code segment */
  2216. } else
  2217. ar = 0xf3;
  2218. vmcs_write32(sf->ar_bytes, ar);
  2219. }
  2220. static int alloc_apic_access_page(struct kvm *kvm)
  2221. {
  2222. struct kvm_userspace_memory_region kvm_userspace_mem;
  2223. int r = 0;
  2224. mutex_lock(&kvm->slots_lock);
  2225. if (kvm->arch.apic_access_page)
  2226. goto out;
  2227. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  2228. kvm_userspace_mem.flags = 0;
  2229. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  2230. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2231. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2232. if (r)
  2233. goto out;
  2234. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  2235. out:
  2236. mutex_unlock(&kvm->slots_lock);
  2237. return r;
  2238. }
  2239. static int alloc_identity_pagetable(struct kvm *kvm)
  2240. {
  2241. struct kvm_userspace_memory_region kvm_userspace_mem;
  2242. int r = 0;
  2243. mutex_lock(&kvm->slots_lock);
  2244. if (kvm->arch.ept_identity_pagetable)
  2245. goto out;
  2246. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  2247. kvm_userspace_mem.flags = 0;
  2248. kvm_userspace_mem.guest_phys_addr =
  2249. kvm->arch.ept_identity_map_addr;
  2250. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2251. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2252. if (r)
  2253. goto out;
  2254. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  2255. kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
  2256. out:
  2257. mutex_unlock(&kvm->slots_lock);
  2258. return r;
  2259. }
  2260. static void allocate_vpid(struct vcpu_vmx *vmx)
  2261. {
  2262. int vpid;
  2263. vmx->vpid = 0;
  2264. if (!enable_vpid)
  2265. return;
  2266. spin_lock(&vmx_vpid_lock);
  2267. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  2268. if (vpid < VMX_NR_VPIDS) {
  2269. vmx->vpid = vpid;
  2270. __set_bit(vpid, vmx_vpid_bitmap);
  2271. }
  2272. spin_unlock(&vmx_vpid_lock);
  2273. }
  2274. static void free_vpid(struct vcpu_vmx *vmx)
  2275. {
  2276. if (!enable_vpid)
  2277. return;
  2278. spin_lock(&vmx_vpid_lock);
  2279. if (vmx->vpid != 0)
  2280. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  2281. spin_unlock(&vmx_vpid_lock);
  2282. }
  2283. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
  2284. {
  2285. int f = sizeof(unsigned long);
  2286. if (!cpu_has_vmx_msr_bitmap())
  2287. return;
  2288. /*
  2289. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  2290. * have the write-low and read-high bitmap offsets the wrong way round.
  2291. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  2292. */
  2293. if (msr <= 0x1fff) {
  2294. __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
  2295. __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
  2296. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2297. msr &= 0x1fff;
  2298. __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
  2299. __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
  2300. }
  2301. }
  2302. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  2303. {
  2304. if (!longmode_only)
  2305. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
  2306. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
  2307. }
  2308. /*
  2309. * Sets up the vmcs for emulated real mode.
  2310. */
  2311. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  2312. {
  2313. u32 host_sysenter_cs, msr_low, msr_high;
  2314. u32 junk;
  2315. u64 host_pat;
  2316. unsigned long a;
  2317. struct desc_ptr dt;
  2318. int i;
  2319. unsigned long kvm_vmx_return;
  2320. u32 exec_control;
  2321. /* I/O */
  2322. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  2323. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  2324. if (cpu_has_vmx_msr_bitmap())
  2325. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  2326. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  2327. /* Control */
  2328. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  2329. vmcs_config.pin_based_exec_ctrl);
  2330. exec_control = vmcs_config.cpu_based_exec_ctrl;
  2331. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  2332. exec_control &= ~CPU_BASED_TPR_SHADOW;
  2333. #ifdef CONFIG_X86_64
  2334. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  2335. CPU_BASED_CR8_LOAD_EXITING;
  2336. #endif
  2337. }
  2338. if (!enable_ept)
  2339. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  2340. CPU_BASED_CR3_LOAD_EXITING |
  2341. CPU_BASED_INVLPG_EXITING;
  2342. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  2343. if (cpu_has_secondary_exec_ctrls()) {
  2344. exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  2345. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2346. exec_control &=
  2347. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  2348. if (vmx->vpid == 0)
  2349. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  2350. if (!enable_ept) {
  2351. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  2352. enable_unrestricted_guest = 0;
  2353. }
  2354. if (!enable_unrestricted_guest)
  2355. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2356. if (!ple_gap)
  2357. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  2358. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  2359. }
  2360. if (ple_gap) {
  2361. vmcs_write32(PLE_GAP, ple_gap);
  2362. vmcs_write32(PLE_WINDOW, ple_window);
  2363. }
  2364. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
  2365. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
  2366. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  2367. vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
  2368. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  2369. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  2370. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  2371. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2372. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2373. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  2374. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  2375. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2376. #ifdef CONFIG_X86_64
  2377. rdmsrl(MSR_FS_BASE, a);
  2378. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  2379. rdmsrl(MSR_GS_BASE, a);
  2380. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  2381. #else
  2382. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  2383. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  2384. #endif
  2385. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  2386. native_store_idt(&dt);
  2387. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  2388. asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
  2389. vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
  2390. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  2391. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  2392. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  2393. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  2394. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  2395. rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
  2396. vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
  2397. rdmsrl(MSR_IA32_SYSENTER_ESP, a);
  2398. vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
  2399. rdmsrl(MSR_IA32_SYSENTER_EIP, a);
  2400. vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
  2401. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  2402. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2403. host_pat = msr_low | ((u64) msr_high << 32);
  2404. vmcs_write64(HOST_IA32_PAT, host_pat);
  2405. }
  2406. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  2407. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2408. host_pat = msr_low | ((u64) msr_high << 32);
  2409. /* Write the default value follow host pat */
  2410. vmcs_write64(GUEST_IA32_PAT, host_pat);
  2411. /* Keep arch.pat sync with GUEST_IA32_PAT */
  2412. vmx->vcpu.arch.pat = host_pat;
  2413. }
  2414. for (i = 0; i < NR_VMX_MSR; ++i) {
  2415. u32 index = vmx_msr_index[i];
  2416. u32 data_low, data_high;
  2417. int j = vmx->nmsrs;
  2418. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  2419. continue;
  2420. if (wrmsr_safe(index, data_low, data_high) < 0)
  2421. continue;
  2422. vmx->guest_msrs[j].index = i;
  2423. vmx->guest_msrs[j].data = 0;
  2424. vmx->guest_msrs[j].mask = -1ull;
  2425. ++vmx->nmsrs;
  2426. }
  2427. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  2428. /* 22.2.1, 20.8.1 */
  2429. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  2430. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  2431. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  2432. if (enable_ept)
  2433. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  2434. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  2435. kvm_write_tsc(&vmx->vcpu, 0);
  2436. return 0;
  2437. }
  2438. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  2439. {
  2440. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2441. u64 msr;
  2442. int ret;
  2443. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  2444. vmx->rmode.vm86_active = 0;
  2445. vmx->soft_vnmi_blocked = 0;
  2446. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  2447. kvm_set_cr8(&vmx->vcpu, 0);
  2448. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  2449. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2450. msr |= MSR_IA32_APICBASE_BSP;
  2451. kvm_set_apic_base(&vmx->vcpu, msr);
  2452. ret = fx_init(&vmx->vcpu);
  2453. if (ret != 0)
  2454. goto out;
  2455. vmx_segment_cache_clear(vmx);
  2456. seg_setup(VCPU_SREG_CS);
  2457. /*
  2458. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  2459. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  2460. */
  2461. if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
  2462. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  2463. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  2464. } else {
  2465. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  2466. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  2467. }
  2468. seg_setup(VCPU_SREG_DS);
  2469. seg_setup(VCPU_SREG_ES);
  2470. seg_setup(VCPU_SREG_FS);
  2471. seg_setup(VCPU_SREG_GS);
  2472. seg_setup(VCPU_SREG_SS);
  2473. vmcs_write16(GUEST_TR_SELECTOR, 0);
  2474. vmcs_writel(GUEST_TR_BASE, 0);
  2475. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  2476. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2477. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  2478. vmcs_writel(GUEST_LDTR_BASE, 0);
  2479. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  2480. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  2481. vmcs_write32(GUEST_SYSENTER_CS, 0);
  2482. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  2483. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  2484. vmcs_writel(GUEST_RFLAGS, 0x02);
  2485. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2486. kvm_rip_write(vcpu, 0xfff0);
  2487. else
  2488. kvm_rip_write(vcpu, 0);
  2489. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  2490. vmcs_writel(GUEST_DR7, 0x400);
  2491. vmcs_writel(GUEST_GDTR_BASE, 0);
  2492. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  2493. vmcs_writel(GUEST_IDTR_BASE, 0);
  2494. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  2495. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2496. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  2497. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  2498. /* Special registers */
  2499. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  2500. setup_msrs(vmx);
  2501. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  2502. if (cpu_has_vmx_tpr_shadow()) {
  2503. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  2504. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  2505. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  2506. __pa(vmx->vcpu.arch.apic->regs));
  2507. vmcs_write32(TPR_THRESHOLD, 0);
  2508. }
  2509. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2510. vmcs_write64(APIC_ACCESS_ADDR,
  2511. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  2512. if (vmx->vpid != 0)
  2513. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  2514. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  2515. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  2516. vmx_set_cr4(&vmx->vcpu, 0);
  2517. vmx_set_efer(&vmx->vcpu, 0);
  2518. vmx_fpu_activate(&vmx->vcpu);
  2519. update_exception_bitmap(&vmx->vcpu);
  2520. vpid_sync_context(vmx);
  2521. ret = 0;
  2522. /* HACK: Don't enable emulation on guest boot/reset */
  2523. vmx->emulation_required = 0;
  2524. out:
  2525. return ret;
  2526. }
  2527. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2528. {
  2529. u32 cpu_based_vm_exec_control;
  2530. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2531. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  2532. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2533. }
  2534. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2535. {
  2536. u32 cpu_based_vm_exec_control;
  2537. if (!cpu_has_virtual_nmis()) {
  2538. enable_irq_window(vcpu);
  2539. return;
  2540. }
  2541. if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  2542. enable_irq_window(vcpu);
  2543. return;
  2544. }
  2545. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2546. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  2547. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2548. }
  2549. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  2550. {
  2551. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2552. uint32_t intr;
  2553. int irq = vcpu->arch.interrupt.nr;
  2554. trace_kvm_inj_virq(irq);
  2555. ++vcpu->stat.irq_injections;
  2556. if (vmx->rmode.vm86_active) {
  2557. int inc_eip = 0;
  2558. if (vcpu->arch.interrupt.soft)
  2559. inc_eip = vcpu->arch.event_exit_inst_len;
  2560. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  2561. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2562. return;
  2563. }
  2564. intr = irq | INTR_INFO_VALID_MASK;
  2565. if (vcpu->arch.interrupt.soft) {
  2566. intr |= INTR_TYPE_SOFT_INTR;
  2567. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2568. vmx->vcpu.arch.event_exit_inst_len);
  2569. } else
  2570. intr |= INTR_TYPE_EXT_INTR;
  2571. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  2572. vmx_clear_hlt(vcpu);
  2573. }
  2574. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  2575. {
  2576. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2577. if (!cpu_has_virtual_nmis()) {
  2578. /*
  2579. * Tracking the NMI-blocked state in software is built upon
  2580. * finding the next open IRQ window. This, in turn, depends on
  2581. * well-behaving guests: They have to keep IRQs disabled at
  2582. * least as long as the NMI handler runs. Otherwise we may
  2583. * cause NMI nesting, maybe breaking the guest. But as this is
  2584. * highly unlikely, we can live with the residual risk.
  2585. */
  2586. vmx->soft_vnmi_blocked = 1;
  2587. vmx->vnmi_blocked_time = 0;
  2588. }
  2589. ++vcpu->stat.nmi_injections;
  2590. vmx->nmi_known_unmasked = false;
  2591. if (vmx->rmode.vm86_active) {
  2592. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  2593. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2594. return;
  2595. }
  2596. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2597. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  2598. vmx_clear_hlt(vcpu);
  2599. }
  2600. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  2601. {
  2602. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  2603. return 0;
  2604. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2605. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  2606. | GUEST_INTR_STATE_NMI));
  2607. }
  2608. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  2609. {
  2610. if (!cpu_has_virtual_nmis())
  2611. return to_vmx(vcpu)->soft_vnmi_blocked;
  2612. if (to_vmx(vcpu)->nmi_known_unmasked)
  2613. return false;
  2614. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  2615. }
  2616. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2617. {
  2618. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2619. if (!cpu_has_virtual_nmis()) {
  2620. if (vmx->soft_vnmi_blocked != masked) {
  2621. vmx->soft_vnmi_blocked = masked;
  2622. vmx->vnmi_blocked_time = 0;
  2623. }
  2624. } else {
  2625. vmx->nmi_known_unmasked = !masked;
  2626. if (masked)
  2627. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2628. GUEST_INTR_STATE_NMI);
  2629. else
  2630. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  2631. GUEST_INTR_STATE_NMI);
  2632. }
  2633. }
  2634. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  2635. {
  2636. return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  2637. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2638. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  2639. }
  2640. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2641. {
  2642. int ret;
  2643. struct kvm_userspace_memory_region tss_mem = {
  2644. .slot = TSS_PRIVATE_MEMSLOT,
  2645. .guest_phys_addr = addr,
  2646. .memory_size = PAGE_SIZE * 3,
  2647. .flags = 0,
  2648. };
  2649. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  2650. if (ret)
  2651. return ret;
  2652. kvm->arch.tss_addr = addr;
  2653. if (!init_rmode_tss(kvm))
  2654. return -ENOMEM;
  2655. return 0;
  2656. }
  2657. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  2658. int vec, u32 err_code)
  2659. {
  2660. /*
  2661. * Instruction with address size override prefix opcode 0x67
  2662. * Cause the #SS fault with 0 error code in VM86 mode.
  2663. */
  2664. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  2665. if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
  2666. return 1;
  2667. /*
  2668. * Forward all other exceptions that are valid in real mode.
  2669. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  2670. * the required debugging infrastructure rework.
  2671. */
  2672. switch (vec) {
  2673. case DB_VECTOR:
  2674. if (vcpu->guest_debug &
  2675. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  2676. return 0;
  2677. kvm_queue_exception(vcpu, vec);
  2678. return 1;
  2679. case BP_VECTOR:
  2680. /*
  2681. * Update instruction length as we may reinject the exception
  2682. * from user space while in guest debugging mode.
  2683. */
  2684. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  2685. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2686. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  2687. return 0;
  2688. /* fall through */
  2689. case DE_VECTOR:
  2690. case OF_VECTOR:
  2691. case BR_VECTOR:
  2692. case UD_VECTOR:
  2693. case DF_VECTOR:
  2694. case SS_VECTOR:
  2695. case GP_VECTOR:
  2696. case MF_VECTOR:
  2697. kvm_queue_exception(vcpu, vec);
  2698. return 1;
  2699. }
  2700. return 0;
  2701. }
  2702. /*
  2703. * Trigger machine check on the host. We assume all the MSRs are already set up
  2704. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  2705. * We pass a fake environment to the machine check handler because we want
  2706. * the guest to be always treated like user space, no matter what context
  2707. * it used internally.
  2708. */
  2709. static void kvm_machine_check(void)
  2710. {
  2711. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  2712. struct pt_regs regs = {
  2713. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  2714. .flags = X86_EFLAGS_IF,
  2715. };
  2716. do_machine_check(&regs, 0);
  2717. #endif
  2718. }
  2719. static int handle_machine_check(struct kvm_vcpu *vcpu)
  2720. {
  2721. /* already handled by vcpu_run */
  2722. return 1;
  2723. }
  2724. static int handle_exception(struct kvm_vcpu *vcpu)
  2725. {
  2726. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2727. struct kvm_run *kvm_run = vcpu->run;
  2728. u32 intr_info, ex_no, error_code;
  2729. unsigned long cr2, rip, dr6;
  2730. u32 vect_info;
  2731. enum emulation_result er;
  2732. vect_info = vmx->idt_vectoring_info;
  2733. intr_info = vmx->exit_intr_info;
  2734. if (is_machine_check(intr_info))
  2735. return handle_machine_check(vcpu);
  2736. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  2737. !is_page_fault(intr_info)) {
  2738. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2739. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  2740. vcpu->run->internal.ndata = 2;
  2741. vcpu->run->internal.data[0] = vect_info;
  2742. vcpu->run->internal.data[1] = intr_info;
  2743. return 0;
  2744. }
  2745. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  2746. return 1; /* already handled by vmx_vcpu_run() */
  2747. if (is_no_device(intr_info)) {
  2748. vmx_fpu_activate(vcpu);
  2749. return 1;
  2750. }
  2751. if (is_invalid_opcode(intr_info)) {
  2752. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  2753. if (er != EMULATE_DONE)
  2754. kvm_queue_exception(vcpu, UD_VECTOR);
  2755. return 1;
  2756. }
  2757. error_code = 0;
  2758. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  2759. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  2760. if (is_page_fault(intr_info)) {
  2761. /* EPT won't cause page fault directly */
  2762. if (enable_ept)
  2763. BUG();
  2764. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  2765. trace_kvm_page_fault(cr2, error_code);
  2766. if (kvm_event_needs_reinjection(vcpu))
  2767. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  2768. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  2769. }
  2770. if (vmx->rmode.vm86_active &&
  2771. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  2772. error_code)) {
  2773. if (vcpu->arch.halt_request) {
  2774. vcpu->arch.halt_request = 0;
  2775. return kvm_emulate_halt(vcpu);
  2776. }
  2777. return 1;
  2778. }
  2779. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  2780. switch (ex_no) {
  2781. case DB_VECTOR:
  2782. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  2783. if (!(vcpu->guest_debug &
  2784. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  2785. vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
  2786. kvm_queue_exception(vcpu, DB_VECTOR);
  2787. return 1;
  2788. }
  2789. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  2790. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  2791. /* fall through */
  2792. case BP_VECTOR:
  2793. /*
  2794. * Update instruction length as we may reinject #BP from
  2795. * user space while in guest debugging mode. Reading it for
  2796. * #DB as well causes no harm, it is not used in that case.
  2797. */
  2798. vmx->vcpu.arch.event_exit_inst_len =
  2799. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2800. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  2801. rip = kvm_rip_read(vcpu);
  2802. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  2803. kvm_run->debug.arch.exception = ex_no;
  2804. break;
  2805. default:
  2806. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  2807. kvm_run->ex.exception = ex_no;
  2808. kvm_run->ex.error_code = error_code;
  2809. break;
  2810. }
  2811. return 0;
  2812. }
  2813. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  2814. {
  2815. ++vcpu->stat.irq_exits;
  2816. return 1;
  2817. }
  2818. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  2819. {
  2820. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  2821. return 0;
  2822. }
  2823. static int handle_io(struct kvm_vcpu *vcpu)
  2824. {
  2825. unsigned long exit_qualification;
  2826. int size, in, string;
  2827. unsigned port;
  2828. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2829. string = (exit_qualification & 16) != 0;
  2830. in = (exit_qualification & 8) != 0;
  2831. ++vcpu->stat.io_exits;
  2832. if (string || in)
  2833. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  2834. port = exit_qualification >> 16;
  2835. size = (exit_qualification & 7) + 1;
  2836. skip_emulated_instruction(vcpu);
  2837. return kvm_fast_pio_out(vcpu, size, port);
  2838. }
  2839. static void
  2840. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2841. {
  2842. /*
  2843. * Patch in the VMCALL instruction:
  2844. */
  2845. hypercall[0] = 0x0f;
  2846. hypercall[1] = 0x01;
  2847. hypercall[2] = 0xc1;
  2848. }
  2849. static int handle_cr(struct kvm_vcpu *vcpu)
  2850. {
  2851. unsigned long exit_qualification, val;
  2852. int cr;
  2853. int reg;
  2854. int err;
  2855. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2856. cr = exit_qualification & 15;
  2857. reg = (exit_qualification >> 8) & 15;
  2858. switch ((exit_qualification >> 4) & 3) {
  2859. case 0: /* mov to cr */
  2860. val = kvm_register_read(vcpu, reg);
  2861. trace_kvm_cr_write(cr, val);
  2862. switch (cr) {
  2863. case 0:
  2864. err = kvm_set_cr0(vcpu, val);
  2865. kvm_complete_insn_gp(vcpu, err);
  2866. return 1;
  2867. case 3:
  2868. err = kvm_set_cr3(vcpu, val);
  2869. kvm_complete_insn_gp(vcpu, err);
  2870. return 1;
  2871. case 4:
  2872. err = kvm_set_cr4(vcpu, val);
  2873. kvm_complete_insn_gp(vcpu, err);
  2874. return 1;
  2875. case 8: {
  2876. u8 cr8_prev = kvm_get_cr8(vcpu);
  2877. u8 cr8 = kvm_register_read(vcpu, reg);
  2878. err = kvm_set_cr8(vcpu, cr8);
  2879. kvm_complete_insn_gp(vcpu, err);
  2880. if (irqchip_in_kernel(vcpu->kvm))
  2881. return 1;
  2882. if (cr8_prev <= cr8)
  2883. return 1;
  2884. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  2885. return 0;
  2886. }
  2887. };
  2888. break;
  2889. case 2: /* clts */
  2890. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  2891. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  2892. skip_emulated_instruction(vcpu);
  2893. vmx_fpu_activate(vcpu);
  2894. return 1;
  2895. case 1: /*mov from cr*/
  2896. switch (cr) {
  2897. case 3:
  2898. val = kvm_read_cr3(vcpu);
  2899. kvm_register_write(vcpu, reg, val);
  2900. trace_kvm_cr_read(cr, val);
  2901. skip_emulated_instruction(vcpu);
  2902. return 1;
  2903. case 8:
  2904. val = kvm_get_cr8(vcpu);
  2905. kvm_register_write(vcpu, reg, val);
  2906. trace_kvm_cr_read(cr, val);
  2907. skip_emulated_instruction(vcpu);
  2908. return 1;
  2909. }
  2910. break;
  2911. case 3: /* lmsw */
  2912. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  2913. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  2914. kvm_lmsw(vcpu, val);
  2915. skip_emulated_instruction(vcpu);
  2916. return 1;
  2917. default:
  2918. break;
  2919. }
  2920. vcpu->run->exit_reason = 0;
  2921. pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  2922. (int)(exit_qualification >> 4) & 3, cr);
  2923. return 0;
  2924. }
  2925. static int handle_dr(struct kvm_vcpu *vcpu)
  2926. {
  2927. unsigned long exit_qualification;
  2928. int dr, reg;
  2929. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  2930. if (!kvm_require_cpl(vcpu, 0))
  2931. return 1;
  2932. dr = vmcs_readl(GUEST_DR7);
  2933. if (dr & DR7_GD) {
  2934. /*
  2935. * As the vm-exit takes precedence over the debug trap, we
  2936. * need to emulate the latter, either for the host or the
  2937. * guest debugging itself.
  2938. */
  2939. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  2940. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  2941. vcpu->run->debug.arch.dr7 = dr;
  2942. vcpu->run->debug.arch.pc =
  2943. vmcs_readl(GUEST_CS_BASE) +
  2944. vmcs_readl(GUEST_RIP);
  2945. vcpu->run->debug.arch.exception = DB_VECTOR;
  2946. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  2947. return 0;
  2948. } else {
  2949. vcpu->arch.dr7 &= ~DR7_GD;
  2950. vcpu->arch.dr6 |= DR6_BD;
  2951. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  2952. kvm_queue_exception(vcpu, DB_VECTOR);
  2953. return 1;
  2954. }
  2955. }
  2956. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2957. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  2958. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  2959. if (exit_qualification & TYPE_MOV_FROM_DR) {
  2960. unsigned long val;
  2961. if (!kvm_get_dr(vcpu, dr, &val))
  2962. kvm_register_write(vcpu, reg, val);
  2963. } else
  2964. kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
  2965. skip_emulated_instruction(vcpu);
  2966. return 1;
  2967. }
  2968. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  2969. {
  2970. vmcs_writel(GUEST_DR7, val);
  2971. }
  2972. static int handle_cpuid(struct kvm_vcpu *vcpu)
  2973. {
  2974. kvm_emulate_cpuid(vcpu);
  2975. return 1;
  2976. }
  2977. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  2978. {
  2979. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2980. u64 data;
  2981. if (vmx_get_msr(vcpu, ecx, &data)) {
  2982. trace_kvm_msr_read_ex(ecx);
  2983. kvm_inject_gp(vcpu, 0);
  2984. return 1;
  2985. }
  2986. trace_kvm_msr_read(ecx, data);
  2987. /* FIXME: handling of bits 32:63 of rax, rdx */
  2988. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  2989. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  2990. skip_emulated_instruction(vcpu);
  2991. return 1;
  2992. }
  2993. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  2994. {
  2995. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2996. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  2997. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2998. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  2999. trace_kvm_msr_write_ex(ecx, data);
  3000. kvm_inject_gp(vcpu, 0);
  3001. return 1;
  3002. }
  3003. trace_kvm_msr_write(ecx, data);
  3004. skip_emulated_instruction(vcpu);
  3005. return 1;
  3006. }
  3007. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  3008. {
  3009. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3010. return 1;
  3011. }
  3012. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  3013. {
  3014. u32 cpu_based_vm_exec_control;
  3015. /* clear pending irq */
  3016. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3017. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  3018. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3019. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3020. ++vcpu->stat.irq_window_exits;
  3021. /*
  3022. * If the user space waits to inject interrupts, exit as soon as
  3023. * possible
  3024. */
  3025. if (!irqchip_in_kernel(vcpu->kvm) &&
  3026. vcpu->run->request_interrupt_window &&
  3027. !kvm_cpu_has_interrupt(vcpu)) {
  3028. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  3029. return 0;
  3030. }
  3031. return 1;
  3032. }
  3033. static int handle_halt(struct kvm_vcpu *vcpu)
  3034. {
  3035. skip_emulated_instruction(vcpu);
  3036. return kvm_emulate_halt(vcpu);
  3037. }
  3038. static int handle_vmcall(struct kvm_vcpu *vcpu)
  3039. {
  3040. skip_emulated_instruction(vcpu);
  3041. kvm_emulate_hypercall(vcpu);
  3042. return 1;
  3043. }
  3044. static int handle_vmx_insn(struct kvm_vcpu *vcpu)
  3045. {
  3046. kvm_queue_exception(vcpu, UD_VECTOR);
  3047. return 1;
  3048. }
  3049. static int handle_invd(struct kvm_vcpu *vcpu)
  3050. {
  3051. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3052. }
  3053. static int handle_invlpg(struct kvm_vcpu *vcpu)
  3054. {
  3055. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3056. kvm_mmu_invlpg(vcpu, exit_qualification);
  3057. skip_emulated_instruction(vcpu);
  3058. return 1;
  3059. }
  3060. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  3061. {
  3062. skip_emulated_instruction(vcpu);
  3063. kvm_emulate_wbinvd(vcpu);
  3064. return 1;
  3065. }
  3066. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  3067. {
  3068. u64 new_bv = kvm_read_edx_eax(vcpu);
  3069. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3070. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  3071. skip_emulated_instruction(vcpu);
  3072. return 1;
  3073. }
  3074. static int handle_apic_access(struct kvm_vcpu *vcpu)
  3075. {
  3076. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3077. }
  3078. static int handle_task_switch(struct kvm_vcpu *vcpu)
  3079. {
  3080. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3081. unsigned long exit_qualification;
  3082. bool has_error_code = false;
  3083. u32 error_code = 0;
  3084. u16 tss_selector;
  3085. int reason, type, idt_v;
  3086. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  3087. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  3088. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3089. reason = (u32)exit_qualification >> 30;
  3090. if (reason == TASK_SWITCH_GATE && idt_v) {
  3091. switch (type) {
  3092. case INTR_TYPE_NMI_INTR:
  3093. vcpu->arch.nmi_injected = false;
  3094. vmx_set_nmi_mask(vcpu, true);
  3095. break;
  3096. case INTR_TYPE_EXT_INTR:
  3097. case INTR_TYPE_SOFT_INTR:
  3098. kvm_clear_interrupt_queue(vcpu);
  3099. break;
  3100. case INTR_TYPE_HARD_EXCEPTION:
  3101. if (vmx->idt_vectoring_info &
  3102. VECTORING_INFO_DELIVER_CODE_MASK) {
  3103. has_error_code = true;
  3104. error_code =
  3105. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  3106. }
  3107. /* fall through */
  3108. case INTR_TYPE_SOFT_EXCEPTION:
  3109. kvm_clear_exception_queue(vcpu);
  3110. break;
  3111. default:
  3112. break;
  3113. }
  3114. }
  3115. tss_selector = exit_qualification;
  3116. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  3117. type != INTR_TYPE_EXT_INTR &&
  3118. type != INTR_TYPE_NMI_INTR))
  3119. skip_emulated_instruction(vcpu);
  3120. if (kvm_task_switch(vcpu, tss_selector, reason,
  3121. has_error_code, error_code) == EMULATE_FAIL) {
  3122. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3123. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  3124. vcpu->run->internal.ndata = 0;
  3125. return 0;
  3126. }
  3127. /* clear all local breakpoint enable flags */
  3128. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
  3129. /*
  3130. * TODO: What about debug traps on tss switch?
  3131. * Are we supposed to inject them and update dr6?
  3132. */
  3133. return 1;
  3134. }
  3135. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  3136. {
  3137. unsigned long exit_qualification;
  3138. gpa_t gpa;
  3139. int gla_validity;
  3140. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3141. if (exit_qualification & (1 << 6)) {
  3142. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  3143. return -EINVAL;
  3144. }
  3145. gla_validity = (exit_qualification >> 7) & 0x3;
  3146. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  3147. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  3148. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  3149. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  3150. vmcs_readl(GUEST_LINEAR_ADDRESS));
  3151. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  3152. (long unsigned int)exit_qualification);
  3153. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3154. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  3155. return 0;
  3156. }
  3157. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3158. trace_kvm_page_fault(gpa, exit_qualification);
  3159. return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
  3160. }
  3161. static u64 ept_rsvd_mask(u64 spte, int level)
  3162. {
  3163. int i;
  3164. u64 mask = 0;
  3165. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  3166. mask |= (1ULL << i);
  3167. if (level > 2)
  3168. /* bits 7:3 reserved */
  3169. mask |= 0xf8;
  3170. else if (level == 2) {
  3171. if (spte & (1ULL << 7))
  3172. /* 2MB ref, bits 20:12 reserved */
  3173. mask |= 0x1ff000;
  3174. else
  3175. /* bits 6:3 reserved */
  3176. mask |= 0x78;
  3177. }
  3178. return mask;
  3179. }
  3180. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  3181. int level)
  3182. {
  3183. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  3184. /* 010b (write-only) */
  3185. WARN_ON((spte & 0x7) == 0x2);
  3186. /* 110b (write/execute) */
  3187. WARN_ON((spte & 0x7) == 0x6);
  3188. /* 100b (execute-only) and value not supported by logical processor */
  3189. if (!cpu_has_vmx_ept_execute_only())
  3190. WARN_ON((spte & 0x7) == 0x4);
  3191. /* not 000b */
  3192. if ((spte & 0x7)) {
  3193. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  3194. if (rsvd_bits != 0) {
  3195. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  3196. __func__, rsvd_bits);
  3197. WARN_ON(1);
  3198. }
  3199. if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
  3200. u64 ept_mem_type = (spte & 0x38) >> 3;
  3201. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  3202. ept_mem_type == 7) {
  3203. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  3204. __func__, ept_mem_type);
  3205. WARN_ON(1);
  3206. }
  3207. }
  3208. }
  3209. }
  3210. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  3211. {
  3212. u64 sptes[4];
  3213. int nr_sptes, i;
  3214. gpa_t gpa;
  3215. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3216. printk(KERN_ERR "EPT: Misconfiguration.\n");
  3217. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  3218. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  3219. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  3220. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  3221. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3222. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  3223. return 0;
  3224. }
  3225. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  3226. {
  3227. u32 cpu_based_vm_exec_control;
  3228. /* clear pending NMI */
  3229. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3230. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  3231. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3232. ++vcpu->stat.nmi_window_exits;
  3233. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3234. return 1;
  3235. }
  3236. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  3237. {
  3238. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3239. enum emulation_result err = EMULATE_DONE;
  3240. int ret = 1;
  3241. u32 cpu_exec_ctrl;
  3242. bool intr_window_requested;
  3243. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3244. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  3245. while (!guest_state_valid(vcpu)) {
  3246. if (intr_window_requested
  3247. && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
  3248. return handle_interrupt_window(&vmx->vcpu);
  3249. err = emulate_instruction(vcpu, 0);
  3250. if (err == EMULATE_DO_MMIO) {
  3251. ret = 0;
  3252. goto out;
  3253. }
  3254. if (err != EMULATE_DONE)
  3255. return 0;
  3256. if (signal_pending(current))
  3257. goto out;
  3258. if (need_resched())
  3259. schedule();
  3260. }
  3261. vmx->emulation_required = 0;
  3262. out:
  3263. return ret;
  3264. }
  3265. /*
  3266. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  3267. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  3268. */
  3269. static int handle_pause(struct kvm_vcpu *vcpu)
  3270. {
  3271. skip_emulated_instruction(vcpu);
  3272. kvm_vcpu_on_spin(vcpu);
  3273. return 1;
  3274. }
  3275. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  3276. {
  3277. kvm_queue_exception(vcpu, UD_VECTOR);
  3278. return 1;
  3279. }
  3280. /*
  3281. * The exit handlers return 1 if the exit was handled fully and guest execution
  3282. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  3283. * to be done to userspace and return 0.
  3284. */
  3285. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  3286. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  3287. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  3288. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  3289. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  3290. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  3291. [EXIT_REASON_CR_ACCESS] = handle_cr,
  3292. [EXIT_REASON_DR_ACCESS] = handle_dr,
  3293. [EXIT_REASON_CPUID] = handle_cpuid,
  3294. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  3295. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  3296. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  3297. [EXIT_REASON_HLT] = handle_halt,
  3298. [EXIT_REASON_INVD] = handle_invd,
  3299. [EXIT_REASON_INVLPG] = handle_invlpg,
  3300. [EXIT_REASON_VMCALL] = handle_vmcall,
  3301. [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
  3302. [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
  3303. [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
  3304. [EXIT_REASON_VMPTRST] = handle_vmx_insn,
  3305. [EXIT_REASON_VMREAD] = handle_vmx_insn,
  3306. [EXIT_REASON_VMRESUME] = handle_vmx_insn,
  3307. [EXIT_REASON_VMWRITE] = handle_vmx_insn,
  3308. [EXIT_REASON_VMOFF] = handle_vmx_insn,
  3309. [EXIT_REASON_VMON] = handle_vmx_insn,
  3310. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  3311. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  3312. [EXIT_REASON_WBINVD] = handle_wbinvd,
  3313. [EXIT_REASON_XSETBV] = handle_xsetbv,
  3314. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  3315. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  3316. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  3317. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  3318. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  3319. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
  3320. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
  3321. };
  3322. static const int kvm_vmx_max_exit_handlers =
  3323. ARRAY_SIZE(kvm_vmx_exit_handlers);
  3324. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  3325. {
  3326. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  3327. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  3328. }
  3329. /*
  3330. * The guest has exited. See if we can fix it or if we need userspace
  3331. * assistance.
  3332. */
  3333. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  3334. {
  3335. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3336. u32 exit_reason = vmx->exit_reason;
  3337. u32 vectoring_info = vmx->idt_vectoring_info;
  3338. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  3339. /* If guest state is invalid, start emulating */
  3340. if (vmx->emulation_required && emulate_invalid_guest_state)
  3341. return handle_invalid_guest_state(vcpu);
  3342. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  3343. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3344. vcpu->run->fail_entry.hardware_entry_failure_reason
  3345. = exit_reason;
  3346. return 0;
  3347. }
  3348. if (unlikely(vmx->fail)) {
  3349. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3350. vcpu->run->fail_entry.hardware_entry_failure_reason
  3351. = vmcs_read32(VM_INSTRUCTION_ERROR);
  3352. return 0;
  3353. }
  3354. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  3355. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  3356. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  3357. exit_reason != EXIT_REASON_TASK_SWITCH))
  3358. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  3359. "(0x%x) and exit reason is 0x%x\n",
  3360. __func__, vectoring_info, exit_reason);
  3361. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
  3362. if (vmx_interrupt_allowed(vcpu)) {
  3363. vmx->soft_vnmi_blocked = 0;
  3364. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  3365. vcpu->arch.nmi_pending) {
  3366. /*
  3367. * This CPU don't support us in finding the end of an
  3368. * NMI-blocked window if the guest runs with IRQs
  3369. * disabled. So we pull the trigger after 1 s of
  3370. * futile waiting, but inform the user about this.
  3371. */
  3372. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  3373. "state on VCPU %d after 1 s timeout\n",
  3374. __func__, vcpu->vcpu_id);
  3375. vmx->soft_vnmi_blocked = 0;
  3376. }
  3377. }
  3378. if (exit_reason < kvm_vmx_max_exit_handlers
  3379. && kvm_vmx_exit_handlers[exit_reason])
  3380. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  3381. else {
  3382. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3383. vcpu->run->hw.hardware_exit_reason = exit_reason;
  3384. }
  3385. return 0;
  3386. }
  3387. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  3388. {
  3389. if (irr == -1 || tpr < irr) {
  3390. vmcs_write32(TPR_THRESHOLD, 0);
  3391. return;
  3392. }
  3393. vmcs_write32(TPR_THRESHOLD, irr);
  3394. }
  3395. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  3396. {
  3397. u32 exit_intr_info;
  3398. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  3399. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  3400. return;
  3401. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  3402. exit_intr_info = vmx->exit_intr_info;
  3403. /* Handle machine checks before interrupts are enabled */
  3404. if (is_machine_check(exit_intr_info))
  3405. kvm_machine_check();
  3406. /* We need to handle NMIs before interrupts are enabled */
  3407. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  3408. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  3409. kvm_before_handle_nmi(&vmx->vcpu);
  3410. asm("int $2");
  3411. kvm_after_handle_nmi(&vmx->vcpu);
  3412. }
  3413. }
  3414. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  3415. {
  3416. u32 exit_intr_info;
  3417. bool unblock_nmi;
  3418. u8 vector;
  3419. bool idtv_info_valid;
  3420. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  3421. if (cpu_has_virtual_nmis()) {
  3422. if (vmx->nmi_known_unmasked)
  3423. return;
  3424. /*
  3425. * Can't use vmx->exit_intr_info since we're not sure what
  3426. * the exit reason is.
  3427. */
  3428. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  3429. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  3430. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  3431. /*
  3432. * SDM 3: 27.7.1.2 (September 2008)
  3433. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  3434. * a guest IRET fault.
  3435. * SDM 3: 23.2.2 (September 2008)
  3436. * Bit 12 is undefined in any of the following cases:
  3437. * If the VM exit sets the valid bit in the IDT-vectoring
  3438. * information field.
  3439. * If the VM exit is due to a double fault.
  3440. */
  3441. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  3442. vector != DF_VECTOR && !idtv_info_valid)
  3443. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  3444. GUEST_INTR_STATE_NMI);
  3445. else
  3446. vmx->nmi_known_unmasked =
  3447. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  3448. & GUEST_INTR_STATE_NMI);
  3449. } else if (unlikely(vmx->soft_vnmi_blocked))
  3450. vmx->vnmi_blocked_time +=
  3451. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  3452. }
  3453. static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
  3454. u32 idt_vectoring_info,
  3455. int instr_len_field,
  3456. int error_code_field)
  3457. {
  3458. u8 vector;
  3459. int type;
  3460. bool idtv_info_valid;
  3461. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  3462. vmx->vcpu.arch.nmi_injected = false;
  3463. kvm_clear_exception_queue(&vmx->vcpu);
  3464. kvm_clear_interrupt_queue(&vmx->vcpu);
  3465. if (!idtv_info_valid)
  3466. return;
  3467. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  3468. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  3469. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  3470. switch (type) {
  3471. case INTR_TYPE_NMI_INTR:
  3472. vmx->vcpu.arch.nmi_injected = true;
  3473. /*
  3474. * SDM 3: 27.7.1.2 (September 2008)
  3475. * Clear bit "block by NMI" before VM entry if a NMI
  3476. * delivery faulted.
  3477. */
  3478. vmx_set_nmi_mask(&vmx->vcpu, false);
  3479. break;
  3480. case INTR_TYPE_SOFT_EXCEPTION:
  3481. vmx->vcpu.arch.event_exit_inst_len =
  3482. vmcs_read32(instr_len_field);
  3483. /* fall through */
  3484. case INTR_TYPE_HARD_EXCEPTION:
  3485. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  3486. u32 err = vmcs_read32(error_code_field);
  3487. kvm_queue_exception_e(&vmx->vcpu, vector, err);
  3488. } else
  3489. kvm_queue_exception(&vmx->vcpu, vector);
  3490. break;
  3491. case INTR_TYPE_SOFT_INTR:
  3492. vmx->vcpu.arch.event_exit_inst_len =
  3493. vmcs_read32(instr_len_field);
  3494. /* fall through */
  3495. case INTR_TYPE_EXT_INTR:
  3496. kvm_queue_interrupt(&vmx->vcpu, vector,
  3497. type == INTR_TYPE_SOFT_INTR);
  3498. break;
  3499. default:
  3500. break;
  3501. }
  3502. }
  3503. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  3504. {
  3505. __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
  3506. VM_EXIT_INSTRUCTION_LEN,
  3507. IDT_VECTORING_ERROR_CODE);
  3508. }
  3509. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  3510. {
  3511. __vmx_complete_interrupts(to_vmx(vcpu),
  3512. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  3513. VM_ENTRY_INSTRUCTION_LEN,
  3514. VM_ENTRY_EXCEPTION_ERROR_CODE);
  3515. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  3516. }
  3517. #ifdef CONFIG_X86_64
  3518. #define R "r"
  3519. #define Q "q"
  3520. #else
  3521. #define R "e"
  3522. #define Q "l"
  3523. #endif
  3524. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  3525. {
  3526. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3527. /* Record the guest's net vcpu time for enforced NMI injections. */
  3528. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  3529. vmx->entry_time = ktime_get();
  3530. /* Don't enter VMX if guest state is invalid, let the exit handler
  3531. start emulation until we arrive back to a valid state */
  3532. if (vmx->emulation_required && emulate_invalid_guest_state)
  3533. return;
  3534. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  3535. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  3536. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  3537. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  3538. /* When single-stepping over STI and MOV SS, we must clear the
  3539. * corresponding interruptibility bits in the guest state. Otherwise
  3540. * vmentry fails as it then expects bit 14 (BS) in pending debug
  3541. * exceptions being set, but that's not correct for the guest debugging
  3542. * case. */
  3543. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  3544. vmx_set_interrupt_shadow(vcpu, 0);
  3545. asm(
  3546. /* Store host registers */
  3547. "push %%"R"dx; push %%"R"bp;"
  3548. "push %%"R"cx \n\t" /* placeholder for guest rcx */
  3549. "push %%"R"cx \n\t"
  3550. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  3551. "je 1f \n\t"
  3552. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  3553. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  3554. "1: \n\t"
  3555. /* Reload cr2 if changed */
  3556. "mov %c[cr2](%0), %%"R"ax \n\t"
  3557. "mov %%cr2, %%"R"dx \n\t"
  3558. "cmp %%"R"ax, %%"R"dx \n\t"
  3559. "je 2f \n\t"
  3560. "mov %%"R"ax, %%cr2 \n\t"
  3561. "2: \n\t"
  3562. /* Check if vmlaunch of vmresume is needed */
  3563. "cmpl $0, %c[launched](%0) \n\t"
  3564. /* Load guest registers. Don't clobber flags. */
  3565. "mov %c[rax](%0), %%"R"ax \n\t"
  3566. "mov %c[rbx](%0), %%"R"bx \n\t"
  3567. "mov %c[rdx](%0), %%"R"dx \n\t"
  3568. "mov %c[rsi](%0), %%"R"si \n\t"
  3569. "mov %c[rdi](%0), %%"R"di \n\t"
  3570. "mov %c[rbp](%0), %%"R"bp \n\t"
  3571. #ifdef CONFIG_X86_64
  3572. "mov %c[r8](%0), %%r8 \n\t"
  3573. "mov %c[r9](%0), %%r9 \n\t"
  3574. "mov %c[r10](%0), %%r10 \n\t"
  3575. "mov %c[r11](%0), %%r11 \n\t"
  3576. "mov %c[r12](%0), %%r12 \n\t"
  3577. "mov %c[r13](%0), %%r13 \n\t"
  3578. "mov %c[r14](%0), %%r14 \n\t"
  3579. "mov %c[r15](%0), %%r15 \n\t"
  3580. #endif
  3581. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  3582. /* Enter guest mode */
  3583. "jne .Llaunched \n\t"
  3584. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  3585. "jmp .Lkvm_vmx_return \n\t"
  3586. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  3587. ".Lkvm_vmx_return: "
  3588. /* Save guest registers, load host registers, keep flags */
  3589. "mov %0, %c[wordsize](%%"R"sp) \n\t"
  3590. "pop %0 \n\t"
  3591. "mov %%"R"ax, %c[rax](%0) \n\t"
  3592. "mov %%"R"bx, %c[rbx](%0) \n\t"
  3593. "pop"Q" %c[rcx](%0) \n\t"
  3594. "mov %%"R"dx, %c[rdx](%0) \n\t"
  3595. "mov %%"R"si, %c[rsi](%0) \n\t"
  3596. "mov %%"R"di, %c[rdi](%0) \n\t"
  3597. "mov %%"R"bp, %c[rbp](%0) \n\t"
  3598. #ifdef CONFIG_X86_64
  3599. "mov %%r8, %c[r8](%0) \n\t"
  3600. "mov %%r9, %c[r9](%0) \n\t"
  3601. "mov %%r10, %c[r10](%0) \n\t"
  3602. "mov %%r11, %c[r11](%0) \n\t"
  3603. "mov %%r12, %c[r12](%0) \n\t"
  3604. "mov %%r13, %c[r13](%0) \n\t"
  3605. "mov %%r14, %c[r14](%0) \n\t"
  3606. "mov %%r15, %c[r15](%0) \n\t"
  3607. #endif
  3608. "mov %%cr2, %%"R"ax \n\t"
  3609. "mov %%"R"ax, %c[cr2](%0) \n\t"
  3610. "pop %%"R"bp; pop %%"R"dx \n\t"
  3611. "setbe %c[fail](%0) \n\t"
  3612. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  3613. [launched]"i"(offsetof(struct vcpu_vmx, launched)),
  3614. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  3615. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  3616. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  3617. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  3618. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  3619. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  3620. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  3621. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  3622. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  3623. #ifdef CONFIG_X86_64
  3624. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  3625. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  3626. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  3627. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  3628. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  3629. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  3630. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  3631. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  3632. #endif
  3633. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  3634. [wordsize]"i"(sizeof(ulong))
  3635. : "cc", "memory"
  3636. , R"ax", R"bx", R"di", R"si"
  3637. #ifdef CONFIG_X86_64
  3638. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  3639. #endif
  3640. );
  3641. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  3642. | (1 << VCPU_EXREG_RFLAGS)
  3643. | (1 << VCPU_EXREG_CPL)
  3644. | (1 << VCPU_EXREG_PDPTR)
  3645. | (1 << VCPU_EXREG_SEGMENTS)
  3646. | (1 << VCPU_EXREG_CR3));
  3647. vcpu->arch.regs_dirty = 0;
  3648. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  3649. asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
  3650. vmx->launched = 1;
  3651. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  3652. vmx_complete_atomic_exit(vmx);
  3653. vmx_recover_nmi_blocking(vmx);
  3654. vmx_complete_interrupts(vmx);
  3655. }
  3656. #undef R
  3657. #undef Q
  3658. static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
  3659. {
  3660. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3661. if (vmx->vmcs) {
  3662. vcpu_clear(vmx);
  3663. free_vmcs(vmx->vmcs);
  3664. vmx->vmcs = NULL;
  3665. }
  3666. }
  3667. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  3668. {
  3669. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3670. free_vpid(vmx);
  3671. vmx_free_vmcs(vcpu);
  3672. kfree(vmx->guest_msrs);
  3673. kvm_vcpu_uninit(vcpu);
  3674. kmem_cache_free(kvm_vcpu_cache, vmx);
  3675. }
  3676. static inline void vmcs_init(struct vmcs *vmcs)
  3677. {
  3678. u64 phys_addr = __pa(per_cpu(vmxarea, raw_smp_processor_id()));
  3679. if (!vmm_exclusive)
  3680. kvm_cpu_vmxon(phys_addr);
  3681. vmcs_clear(vmcs);
  3682. if (!vmm_exclusive)
  3683. kvm_cpu_vmxoff();
  3684. }
  3685. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  3686. {
  3687. int err;
  3688. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  3689. int cpu;
  3690. if (!vmx)
  3691. return ERR_PTR(-ENOMEM);
  3692. allocate_vpid(vmx);
  3693. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  3694. if (err)
  3695. goto free_vcpu;
  3696. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3697. err = -ENOMEM;
  3698. if (!vmx->guest_msrs) {
  3699. goto uninit_vcpu;
  3700. }
  3701. vmx->vmcs = alloc_vmcs();
  3702. if (!vmx->vmcs)
  3703. goto free_msrs;
  3704. vmcs_init(vmx->vmcs);
  3705. cpu = get_cpu();
  3706. vmx_vcpu_load(&vmx->vcpu, cpu);
  3707. vmx->vcpu.cpu = cpu;
  3708. err = vmx_vcpu_setup(vmx);
  3709. vmx_vcpu_put(&vmx->vcpu);
  3710. put_cpu();
  3711. if (err)
  3712. goto free_vmcs;
  3713. if (vm_need_virtualize_apic_accesses(kvm))
  3714. err = alloc_apic_access_page(kvm);
  3715. if (err)
  3716. goto free_vmcs;
  3717. if (enable_ept) {
  3718. if (!kvm->arch.ept_identity_map_addr)
  3719. kvm->arch.ept_identity_map_addr =
  3720. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  3721. err = -ENOMEM;
  3722. if (alloc_identity_pagetable(kvm) != 0)
  3723. goto free_vmcs;
  3724. if (!init_rmode_identity_map(kvm))
  3725. goto free_vmcs;
  3726. }
  3727. return &vmx->vcpu;
  3728. free_vmcs:
  3729. free_vmcs(vmx->vmcs);
  3730. free_msrs:
  3731. kfree(vmx->guest_msrs);
  3732. uninit_vcpu:
  3733. kvm_vcpu_uninit(&vmx->vcpu);
  3734. free_vcpu:
  3735. free_vpid(vmx);
  3736. kmem_cache_free(kvm_vcpu_cache, vmx);
  3737. return ERR_PTR(err);
  3738. }
  3739. static void __init vmx_check_processor_compat(void *rtn)
  3740. {
  3741. struct vmcs_config vmcs_conf;
  3742. *(int *)rtn = 0;
  3743. if (setup_vmcs_config(&vmcs_conf) < 0)
  3744. *(int *)rtn = -EIO;
  3745. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  3746. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  3747. smp_processor_id());
  3748. *(int *)rtn = -EIO;
  3749. }
  3750. }
  3751. static int get_ept_level(void)
  3752. {
  3753. return VMX_EPT_DEFAULT_GAW + 1;
  3754. }
  3755. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  3756. {
  3757. u64 ret;
  3758. /* For VT-d and EPT combination
  3759. * 1. MMIO: always map as UC
  3760. * 2. EPT with VT-d:
  3761. * a. VT-d without snooping control feature: can't guarantee the
  3762. * result, try to trust guest.
  3763. * b. VT-d with snooping control feature: snooping control feature of
  3764. * VT-d engine can guarantee the cache correctness. Just set it
  3765. * to WB to keep consistent with host. So the same as item 3.
  3766. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  3767. * consistent with host MTRR
  3768. */
  3769. if (is_mmio)
  3770. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  3771. else if (vcpu->kvm->arch.iommu_domain &&
  3772. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
  3773. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  3774. VMX_EPT_MT_EPTE_SHIFT;
  3775. else
  3776. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  3777. | VMX_EPT_IPAT_BIT;
  3778. return ret;
  3779. }
  3780. #define _ER(x) { EXIT_REASON_##x, #x }
  3781. static const struct trace_print_flags vmx_exit_reasons_str[] = {
  3782. _ER(EXCEPTION_NMI),
  3783. _ER(EXTERNAL_INTERRUPT),
  3784. _ER(TRIPLE_FAULT),
  3785. _ER(PENDING_INTERRUPT),
  3786. _ER(NMI_WINDOW),
  3787. _ER(TASK_SWITCH),
  3788. _ER(CPUID),
  3789. _ER(HLT),
  3790. _ER(INVLPG),
  3791. _ER(RDPMC),
  3792. _ER(RDTSC),
  3793. _ER(VMCALL),
  3794. _ER(VMCLEAR),
  3795. _ER(VMLAUNCH),
  3796. _ER(VMPTRLD),
  3797. _ER(VMPTRST),
  3798. _ER(VMREAD),
  3799. _ER(VMRESUME),
  3800. _ER(VMWRITE),
  3801. _ER(VMOFF),
  3802. _ER(VMON),
  3803. _ER(CR_ACCESS),
  3804. _ER(DR_ACCESS),
  3805. _ER(IO_INSTRUCTION),
  3806. _ER(MSR_READ),
  3807. _ER(MSR_WRITE),
  3808. _ER(MWAIT_INSTRUCTION),
  3809. _ER(MONITOR_INSTRUCTION),
  3810. _ER(PAUSE_INSTRUCTION),
  3811. _ER(MCE_DURING_VMENTRY),
  3812. _ER(TPR_BELOW_THRESHOLD),
  3813. _ER(APIC_ACCESS),
  3814. _ER(EPT_VIOLATION),
  3815. _ER(EPT_MISCONFIG),
  3816. _ER(WBINVD),
  3817. { -1, NULL }
  3818. };
  3819. #undef _ER
  3820. static int vmx_get_lpage_level(void)
  3821. {
  3822. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  3823. return PT_DIRECTORY_LEVEL;
  3824. else
  3825. /* For shadow and EPT supported 1GB page */
  3826. return PT_PDPE_LEVEL;
  3827. }
  3828. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  3829. {
  3830. struct kvm_cpuid_entry2 *best;
  3831. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3832. u32 exec_control;
  3833. vmx->rdtscp_enabled = false;
  3834. if (vmx_rdtscp_supported()) {
  3835. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  3836. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  3837. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  3838. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  3839. vmx->rdtscp_enabled = true;
  3840. else {
  3841. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  3842. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  3843. exec_control);
  3844. }
  3845. }
  3846. }
  3847. }
  3848. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  3849. {
  3850. }
  3851. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  3852. struct x86_instruction_info *info,
  3853. enum x86_intercept_stage stage)
  3854. {
  3855. return X86EMUL_CONTINUE;
  3856. }
  3857. static struct kvm_x86_ops vmx_x86_ops = {
  3858. .cpu_has_kvm_support = cpu_has_kvm_support,
  3859. .disabled_by_bios = vmx_disabled_by_bios,
  3860. .hardware_setup = hardware_setup,
  3861. .hardware_unsetup = hardware_unsetup,
  3862. .check_processor_compatibility = vmx_check_processor_compat,
  3863. .hardware_enable = hardware_enable,
  3864. .hardware_disable = hardware_disable,
  3865. .cpu_has_accelerated_tpr = report_flexpriority,
  3866. .vcpu_create = vmx_create_vcpu,
  3867. .vcpu_free = vmx_free_vcpu,
  3868. .vcpu_reset = vmx_vcpu_reset,
  3869. .prepare_guest_switch = vmx_save_host_state,
  3870. .vcpu_load = vmx_vcpu_load,
  3871. .vcpu_put = vmx_vcpu_put,
  3872. .set_guest_debug = set_guest_debug,
  3873. .get_msr = vmx_get_msr,
  3874. .set_msr = vmx_set_msr,
  3875. .get_segment_base = vmx_get_segment_base,
  3876. .get_segment = vmx_get_segment,
  3877. .set_segment = vmx_set_segment,
  3878. .get_cpl = vmx_get_cpl,
  3879. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  3880. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  3881. .decache_cr3 = vmx_decache_cr3,
  3882. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  3883. .set_cr0 = vmx_set_cr0,
  3884. .set_cr3 = vmx_set_cr3,
  3885. .set_cr4 = vmx_set_cr4,
  3886. .set_efer = vmx_set_efer,
  3887. .get_idt = vmx_get_idt,
  3888. .set_idt = vmx_set_idt,
  3889. .get_gdt = vmx_get_gdt,
  3890. .set_gdt = vmx_set_gdt,
  3891. .set_dr7 = vmx_set_dr7,
  3892. .cache_reg = vmx_cache_reg,
  3893. .get_rflags = vmx_get_rflags,
  3894. .set_rflags = vmx_set_rflags,
  3895. .fpu_activate = vmx_fpu_activate,
  3896. .fpu_deactivate = vmx_fpu_deactivate,
  3897. .tlb_flush = vmx_flush_tlb,
  3898. .run = vmx_vcpu_run,
  3899. .handle_exit = vmx_handle_exit,
  3900. .skip_emulated_instruction = skip_emulated_instruction,
  3901. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  3902. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  3903. .patch_hypercall = vmx_patch_hypercall,
  3904. .set_irq = vmx_inject_irq,
  3905. .set_nmi = vmx_inject_nmi,
  3906. .queue_exception = vmx_queue_exception,
  3907. .cancel_injection = vmx_cancel_injection,
  3908. .interrupt_allowed = vmx_interrupt_allowed,
  3909. .nmi_allowed = vmx_nmi_allowed,
  3910. .get_nmi_mask = vmx_get_nmi_mask,
  3911. .set_nmi_mask = vmx_set_nmi_mask,
  3912. .enable_nmi_window = enable_nmi_window,
  3913. .enable_irq_window = enable_irq_window,
  3914. .update_cr8_intercept = update_cr8_intercept,
  3915. .set_tss_addr = vmx_set_tss_addr,
  3916. .get_tdp_level = get_ept_level,
  3917. .get_mt_mask = vmx_get_mt_mask,
  3918. .get_exit_info = vmx_get_exit_info,
  3919. .exit_reasons_str = vmx_exit_reasons_str,
  3920. .get_lpage_level = vmx_get_lpage_level,
  3921. .cpuid_update = vmx_cpuid_update,
  3922. .rdtscp_supported = vmx_rdtscp_supported,
  3923. .set_supported_cpuid = vmx_set_supported_cpuid,
  3924. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  3925. .set_tsc_khz = vmx_set_tsc_khz,
  3926. .write_tsc_offset = vmx_write_tsc_offset,
  3927. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  3928. .compute_tsc_offset = vmx_compute_tsc_offset,
  3929. .set_tdp_cr3 = vmx_set_cr3,
  3930. .check_intercept = vmx_check_intercept,
  3931. };
  3932. static int __init vmx_init(void)
  3933. {
  3934. int r, i;
  3935. rdmsrl_safe(MSR_EFER, &host_efer);
  3936. for (i = 0; i < NR_VMX_MSR; ++i)
  3937. kvm_define_shared_msr(i, vmx_msr_index[i]);
  3938. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  3939. if (!vmx_io_bitmap_a)
  3940. return -ENOMEM;
  3941. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  3942. if (!vmx_io_bitmap_b) {
  3943. r = -ENOMEM;
  3944. goto out;
  3945. }
  3946. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  3947. if (!vmx_msr_bitmap_legacy) {
  3948. r = -ENOMEM;
  3949. goto out1;
  3950. }
  3951. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  3952. if (!vmx_msr_bitmap_longmode) {
  3953. r = -ENOMEM;
  3954. goto out2;
  3955. }
  3956. /*
  3957. * Allow direct access to the PC debug port (it is often used for I/O
  3958. * delays, but the vmexits simply slow things down).
  3959. */
  3960. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  3961. clear_bit(0x80, vmx_io_bitmap_a);
  3962. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  3963. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  3964. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  3965. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  3966. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  3967. __alignof__(struct vcpu_vmx), THIS_MODULE);
  3968. if (r)
  3969. goto out3;
  3970. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  3971. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  3972. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  3973. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  3974. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  3975. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  3976. if (enable_ept) {
  3977. bypass_guest_pf = 0;
  3978. kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
  3979. VMX_EPT_EXECUTABLE_MASK);
  3980. kvm_enable_tdp();
  3981. } else
  3982. kvm_disable_tdp();
  3983. if (bypass_guest_pf)
  3984. kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
  3985. return 0;
  3986. out3:
  3987. free_page((unsigned long)vmx_msr_bitmap_longmode);
  3988. out2:
  3989. free_page((unsigned long)vmx_msr_bitmap_legacy);
  3990. out1:
  3991. free_page((unsigned long)vmx_io_bitmap_b);
  3992. out:
  3993. free_page((unsigned long)vmx_io_bitmap_a);
  3994. return r;
  3995. }
  3996. static void __exit vmx_exit(void)
  3997. {
  3998. free_page((unsigned long)vmx_msr_bitmap_legacy);
  3999. free_page((unsigned long)vmx_msr_bitmap_longmode);
  4000. free_page((unsigned long)vmx_io_bitmap_b);
  4001. free_page((unsigned long)vmx_io_bitmap_a);
  4002. kvm_exit();
  4003. }
  4004. module_init(vmx_init)
  4005. module_exit(vmx_exit)