vmx.c 115 KB

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