vmx.c 117 KB

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