vmx.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539
  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_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
  1431. vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
  1432. vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
  1433. vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
  1434. flags = vmcs_readl(GUEST_RFLAGS);
  1435. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1436. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1437. vmcs_writel(GUEST_RFLAGS, flags);
  1438. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  1439. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  1440. update_exception_bitmap(vcpu);
  1441. if (emulate_invalid_guest_state)
  1442. return;
  1443. fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
  1444. fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
  1445. fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
  1446. fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
  1447. vmcs_write16(GUEST_SS_SELECTOR, 0);
  1448. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  1449. vmcs_write16(GUEST_CS_SELECTOR,
  1450. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  1451. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  1452. }
  1453. static gva_t rmode_tss_base(struct kvm *kvm)
  1454. {
  1455. if (!kvm->arch.tss_addr) {
  1456. struct kvm_memslots *slots;
  1457. gfn_t base_gfn;
  1458. slots = kvm_memslots(kvm);
  1459. base_gfn = slots->memslots[0].base_gfn +
  1460. kvm->memslots->memslots[0].npages - 3;
  1461. return base_gfn << PAGE_SHIFT;
  1462. }
  1463. return kvm->arch.tss_addr;
  1464. }
  1465. static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
  1466. {
  1467. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1468. save->selector = vmcs_read16(sf->selector);
  1469. save->base = vmcs_readl(sf->base);
  1470. save->limit = vmcs_read32(sf->limit);
  1471. save->ar = vmcs_read32(sf->ar_bytes);
  1472. vmcs_write16(sf->selector, save->base >> 4);
  1473. vmcs_write32(sf->base, save->base & 0xffff0);
  1474. vmcs_write32(sf->limit, 0xffff);
  1475. vmcs_write32(sf->ar_bytes, 0xf3);
  1476. if (save->base & 0xf)
  1477. printk_once(KERN_WARNING "kvm: segment base is not paragraph"
  1478. " aligned when entering protected mode (seg=%d)",
  1479. seg);
  1480. }
  1481. static void enter_rmode(struct kvm_vcpu *vcpu)
  1482. {
  1483. unsigned long flags;
  1484. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1485. if (enable_unrestricted_guest)
  1486. return;
  1487. vmx->emulation_required = 1;
  1488. vmx->rmode.vm86_active = 1;
  1489. vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
  1490. vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
  1491. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  1492. vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
  1493. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  1494. vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1495. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1496. flags = vmcs_readl(GUEST_RFLAGS);
  1497. vmx->rmode.save_rflags = flags;
  1498. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1499. vmcs_writel(GUEST_RFLAGS, flags);
  1500. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  1501. update_exception_bitmap(vcpu);
  1502. if (emulate_invalid_guest_state)
  1503. goto continue_rmode;
  1504. vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
  1505. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  1506. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  1507. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  1508. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  1509. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  1510. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  1511. vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
  1512. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
  1513. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
  1514. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
  1515. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
  1516. continue_rmode:
  1517. kvm_mmu_reset_context(vcpu);
  1518. init_rmode(vcpu->kvm);
  1519. }
  1520. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1521. {
  1522. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1523. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  1524. if (!msr)
  1525. return;
  1526. /*
  1527. * Force kernel_gs_base reloading before EFER changes, as control
  1528. * of this msr depends on is_long_mode().
  1529. */
  1530. vmx_load_host_state(to_vmx(vcpu));
  1531. vcpu->arch.efer = efer;
  1532. if (efer & EFER_LMA) {
  1533. vmcs_write32(VM_ENTRY_CONTROLS,
  1534. vmcs_read32(VM_ENTRY_CONTROLS) |
  1535. VM_ENTRY_IA32E_MODE);
  1536. msr->data = efer;
  1537. } else {
  1538. vmcs_write32(VM_ENTRY_CONTROLS,
  1539. vmcs_read32(VM_ENTRY_CONTROLS) &
  1540. ~VM_ENTRY_IA32E_MODE);
  1541. msr->data = efer & ~EFER_LME;
  1542. }
  1543. setup_msrs(vmx);
  1544. }
  1545. #ifdef CONFIG_X86_64
  1546. static void enter_lmode(struct kvm_vcpu *vcpu)
  1547. {
  1548. u32 guest_tr_ar;
  1549. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1550. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  1551. printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
  1552. __func__);
  1553. vmcs_write32(GUEST_TR_AR_BYTES,
  1554. (guest_tr_ar & ~AR_TYPE_MASK)
  1555. | AR_TYPE_BUSY_64_TSS);
  1556. }
  1557. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  1558. }
  1559. static void exit_lmode(struct kvm_vcpu *vcpu)
  1560. {
  1561. vmcs_write32(VM_ENTRY_CONTROLS,
  1562. vmcs_read32(VM_ENTRY_CONTROLS)
  1563. & ~VM_ENTRY_IA32E_MODE);
  1564. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  1565. }
  1566. #endif
  1567. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  1568. {
  1569. vpid_sync_context(to_vmx(vcpu));
  1570. if (enable_ept) {
  1571. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  1572. return;
  1573. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  1574. }
  1575. }
  1576. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1577. {
  1578. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  1579. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  1580. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  1581. }
  1582. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  1583. {
  1584. if (enable_ept && is_paging(vcpu))
  1585. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  1586. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  1587. }
  1588. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1589. {
  1590. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  1591. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  1592. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  1593. }
  1594. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  1595. {
  1596. if (!test_bit(VCPU_EXREG_PDPTR,
  1597. (unsigned long *)&vcpu->arch.regs_dirty))
  1598. return;
  1599. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1600. vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
  1601. vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
  1602. vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
  1603. vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
  1604. }
  1605. }
  1606. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  1607. {
  1608. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1609. vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  1610. vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  1611. vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  1612. vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  1613. }
  1614. __set_bit(VCPU_EXREG_PDPTR,
  1615. (unsigned long *)&vcpu->arch.regs_avail);
  1616. __set_bit(VCPU_EXREG_PDPTR,
  1617. (unsigned long *)&vcpu->arch.regs_dirty);
  1618. }
  1619. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  1620. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  1621. unsigned long cr0,
  1622. struct kvm_vcpu *vcpu)
  1623. {
  1624. vmx_decache_cr3(vcpu);
  1625. if (!(cr0 & X86_CR0_PG)) {
  1626. /* From paging/starting to nonpaging */
  1627. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1628. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  1629. (CPU_BASED_CR3_LOAD_EXITING |
  1630. CPU_BASED_CR3_STORE_EXITING));
  1631. vcpu->arch.cr0 = cr0;
  1632. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  1633. } else if (!is_paging(vcpu)) {
  1634. /* From nonpaging to paging */
  1635. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1636. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  1637. ~(CPU_BASED_CR3_LOAD_EXITING |
  1638. CPU_BASED_CR3_STORE_EXITING));
  1639. vcpu->arch.cr0 = cr0;
  1640. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  1641. }
  1642. if (!(cr0 & X86_CR0_WP))
  1643. *hw_cr0 &= ~X86_CR0_WP;
  1644. }
  1645. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1646. {
  1647. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1648. unsigned long hw_cr0;
  1649. if (enable_unrestricted_guest)
  1650. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
  1651. | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  1652. else
  1653. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
  1654. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  1655. enter_pmode(vcpu);
  1656. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  1657. enter_rmode(vcpu);
  1658. #ifdef CONFIG_X86_64
  1659. if (vcpu->arch.efer & EFER_LME) {
  1660. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  1661. enter_lmode(vcpu);
  1662. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  1663. exit_lmode(vcpu);
  1664. }
  1665. #endif
  1666. if (enable_ept)
  1667. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  1668. if (!vcpu->fpu_active)
  1669. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  1670. vmcs_writel(CR0_READ_SHADOW, cr0);
  1671. vmcs_writel(GUEST_CR0, hw_cr0);
  1672. vcpu->arch.cr0 = cr0;
  1673. }
  1674. static u64 construct_eptp(unsigned long root_hpa)
  1675. {
  1676. u64 eptp;
  1677. /* TODO write the value reading from MSR */
  1678. eptp = VMX_EPT_DEFAULT_MT |
  1679. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  1680. eptp |= (root_hpa & PAGE_MASK);
  1681. return eptp;
  1682. }
  1683. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  1684. {
  1685. unsigned long guest_cr3;
  1686. u64 eptp;
  1687. guest_cr3 = cr3;
  1688. if (enable_ept) {
  1689. eptp = construct_eptp(cr3);
  1690. vmcs_write64(EPT_POINTER, eptp);
  1691. guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
  1692. vcpu->kvm->arch.ept_identity_map_addr;
  1693. ept_load_pdptrs(vcpu);
  1694. }
  1695. vmx_flush_tlb(vcpu);
  1696. vmcs_writel(GUEST_CR3, guest_cr3);
  1697. }
  1698. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1699. {
  1700. unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
  1701. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  1702. vcpu->arch.cr4 = cr4;
  1703. if (enable_ept) {
  1704. if (!is_paging(vcpu)) {
  1705. hw_cr4 &= ~X86_CR4_PAE;
  1706. hw_cr4 |= X86_CR4_PSE;
  1707. } else if (!(cr4 & X86_CR4_PAE)) {
  1708. hw_cr4 &= ~X86_CR4_PAE;
  1709. }
  1710. }
  1711. vmcs_writel(CR4_READ_SHADOW, cr4);
  1712. vmcs_writel(GUEST_CR4, hw_cr4);
  1713. }
  1714. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1715. struct kvm_segment *var, int seg)
  1716. {
  1717. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1718. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1719. struct kvm_save_segment *save;
  1720. u32 ar;
  1721. if (vmx->rmode.vm86_active
  1722. && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
  1723. || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
  1724. || seg == VCPU_SREG_GS)
  1725. && !emulate_invalid_guest_state) {
  1726. switch (seg) {
  1727. case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
  1728. case VCPU_SREG_ES: save = &vmx->rmode.es; break;
  1729. case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
  1730. case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
  1731. case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
  1732. default: BUG();
  1733. }
  1734. var->selector = save->selector;
  1735. var->base = save->base;
  1736. var->limit = save->limit;
  1737. ar = save->ar;
  1738. if (seg == VCPU_SREG_TR
  1739. || var->selector == vmcs_read16(sf->selector))
  1740. goto use_saved_rmode_seg;
  1741. }
  1742. var->base = vmcs_readl(sf->base);
  1743. var->limit = vmcs_read32(sf->limit);
  1744. var->selector = vmcs_read16(sf->selector);
  1745. ar = vmcs_read32(sf->ar_bytes);
  1746. use_saved_rmode_seg:
  1747. if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
  1748. ar = 0;
  1749. var->type = ar & 15;
  1750. var->s = (ar >> 4) & 1;
  1751. var->dpl = (ar >> 5) & 3;
  1752. var->present = (ar >> 7) & 1;
  1753. var->avl = (ar >> 12) & 1;
  1754. var->l = (ar >> 13) & 1;
  1755. var->db = (ar >> 14) & 1;
  1756. var->g = (ar >> 15) & 1;
  1757. var->unusable = (ar >> 16) & 1;
  1758. }
  1759. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1760. {
  1761. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1762. struct kvm_segment s;
  1763. if (to_vmx(vcpu)->rmode.vm86_active) {
  1764. vmx_get_segment(vcpu, &s, seg);
  1765. return s.base;
  1766. }
  1767. return vmcs_readl(sf->base);
  1768. }
  1769. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  1770. {
  1771. if (!is_protmode(vcpu))
  1772. return 0;
  1773. if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
  1774. return 3;
  1775. return vmcs_read16(GUEST_CS_SELECTOR) & 3;
  1776. }
  1777. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  1778. {
  1779. u32 ar;
  1780. if (var->unusable)
  1781. ar = 1 << 16;
  1782. else {
  1783. ar = var->type & 15;
  1784. ar |= (var->s & 1) << 4;
  1785. ar |= (var->dpl & 3) << 5;
  1786. ar |= (var->present & 1) << 7;
  1787. ar |= (var->avl & 1) << 12;
  1788. ar |= (var->l & 1) << 13;
  1789. ar |= (var->db & 1) << 14;
  1790. ar |= (var->g & 1) << 15;
  1791. }
  1792. if (ar == 0) /* a 0 value means unusable */
  1793. ar = AR_UNUSABLE_MASK;
  1794. return ar;
  1795. }
  1796. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1797. struct kvm_segment *var, int seg)
  1798. {
  1799. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1800. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1801. u32 ar;
  1802. if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
  1803. vmx->rmode.tr.selector = var->selector;
  1804. vmx->rmode.tr.base = var->base;
  1805. vmx->rmode.tr.limit = var->limit;
  1806. vmx->rmode.tr.ar = vmx_segment_access_rights(var);
  1807. return;
  1808. }
  1809. vmcs_writel(sf->base, var->base);
  1810. vmcs_write32(sf->limit, var->limit);
  1811. vmcs_write16(sf->selector, var->selector);
  1812. if (vmx->rmode.vm86_active && var->s) {
  1813. /*
  1814. * Hack real-mode segments into vm86 compatibility.
  1815. */
  1816. if (var->base == 0xffff0000 && var->selector == 0xf000)
  1817. vmcs_writel(sf->base, 0xf0000);
  1818. ar = 0xf3;
  1819. } else
  1820. ar = vmx_segment_access_rights(var);
  1821. /*
  1822. * Fix the "Accessed" bit in AR field of segment registers for older
  1823. * qemu binaries.
  1824. * IA32 arch specifies that at the time of processor reset the
  1825. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  1826. * is setting it to 0 in the usedland code. This causes invalid guest
  1827. * state vmexit when "unrestricted guest" mode is turned on.
  1828. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  1829. * tree. Newer qemu binaries with that qemu fix would not need this
  1830. * kvm hack.
  1831. */
  1832. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  1833. ar |= 0x1; /* Accessed */
  1834. vmcs_write32(sf->ar_bytes, ar);
  1835. }
  1836. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  1837. {
  1838. u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
  1839. *db = (ar >> 14) & 1;
  1840. *l = (ar >> 13) & 1;
  1841. }
  1842. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1843. {
  1844. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  1845. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  1846. }
  1847. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1848. {
  1849. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  1850. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  1851. }
  1852. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1853. {
  1854. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  1855. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  1856. }
  1857. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1858. {
  1859. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  1860. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  1861. }
  1862. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1863. {
  1864. struct kvm_segment var;
  1865. u32 ar;
  1866. vmx_get_segment(vcpu, &var, seg);
  1867. ar = vmx_segment_access_rights(&var);
  1868. if (var.base != (var.selector << 4))
  1869. return false;
  1870. if (var.limit != 0xffff)
  1871. return false;
  1872. if (ar != 0xf3)
  1873. return false;
  1874. return true;
  1875. }
  1876. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  1877. {
  1878. struct kvm_segment cs;
  1879. unsigned int cs_rpl;
  1880. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1881. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  1882. if (cs.unusable)
  1883. return false;
  1884. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  1885. return false;
  1886. if (!cs.s)
  1887. return false;
  1888. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  1889. if (cs.dpl > cs_rpl)
  1890. return false;
  1891. } else {
  1892. if (cs.dpl != cs_rpl)
  1893. return false;
  1894. }
  1895. if (!cs.present)
  1896. return false;
  1897. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  1898. return true;
  1899. }
  1900. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  1901. {
  1902. struct kvm_segment ss;
  1903. unsigned int ss_rpl;
  1904. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  1905. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  1906. if (ss.unusable)
  1907. return true;
  1908. if (ss.type != 3 && ss.type != 7)
  1909. return false;
  1910. if (!ss.s)
  1911. return false;
  1912. if (ss.dpl != ss_rpl) /* DPL != RPL */
  1913. return false;
  1914. if (!ss.present)
  1915. return false;
  1916. return true;
  1917. }
  1918. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1919. {
  1920. struct kvm_segment var;
  1921. unsigned int rpl;
  1922. vmx_get_segment(vcpu, &var, seg);
  1923. rpl = var.selector & SELECTOR_RPL_MASK;
  1924. if (var.unusable)
  1925. return true;
  1926. if (!var.s)
  1927. return false;
  1928. if (!var.present)
  1929. return false;
  1930. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  1931. if (var.dpl < rpl) /* DPL < RPL */
  1932. return false;
  1933. }
  1934. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  1935. * rights flags
  1936. */
  1937. return true;
  1938. }
  1939. static bool tr_valid(struct kvm_vcpu *vcpu)
  1940. {
  1941. struct kvm_segment tr;
  1942. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  1943. if (tr.unusable)
  1944. return false;
  1945. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  1946. return false;
  1947. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  1948. return false;
  1949. if (!tr.present)
  1950. return false;
  1951. return true;
  1952. }
  1953. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  1954. {
  1955. struct kvm_segment ldtr;
  1956. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  1957. if (ldtr.unusable)
  1958. return true;
  1959. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  1960. return false;
  1961. if (ldtr.type != 2)
  1962. return false;
  1963. if (!ldtr.present)
  1964. return false;
  1965. return true;
  1966. }
  1967. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  1968. {
  1969. struct kvm_segment cs, ss;
  1970. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1971. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  1972. return ((cs.selector & SELECTOR_RPL_MASK) ==
  1973. (ss.selector & SELECTOR_RPL_MASK));
  1974. }
  1975. /*
  1976. * Check if guest state is valid. Returns true if valid, false if
  1977. * not.
  1978. * We assume that registers are always usable
  1979. */
  1980. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  1981. {
  1982. /* real mode guest state checks */
  1983. if (!is_protmode(vcpu)) {
  1984. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  1985. return false;
  1986. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  1987. return false;
  1988. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  1989. return false;
  1990. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  1991. return false;
  1992. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  1993. return false;
  1994. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  1995. return false;
  1996. } else {
  1997. /* protected mode guest state checks */
  1998. if (!cs_ss_rpl_check(vcpu))
  1999. return false;
  2000. if (!code_segment_valid(vcpu))
  2001. return false;
  2002. if (!stack_segment_valid(vcpu))
  2003. return false;
  2004. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  2005. return false;
  2006. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  2007. return false;
  2008. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  2009. return false;
  2010. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  2011. return false;
  2012. if (!tr_valid(vcpu))
  2013. return false;
  2014. if (!ldtr_valid(vcpu))
  2015. return false;
  2016. }
  2017. /* TODO:
  2018. * - Add checks on RIP
  2019. * - Add checks on RFLAGS
  2020. */
  2021. return true;
  2022. }
  2023. static int init_rmode_tss(struct kvm *kvm)
  2024. {
  2025. gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  2026. u16 data = 0;
  2027. int ret = 0;
  2028. int r;
  2029. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2030. if (r < 0)
  2031. goto out;
  2032. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  2033. r = kvm_write_guest_page(kvm, fn++, &data,
  2034. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  2035. if (r < 0)
  2036. goto out;
  2037. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  2038. if (r < 0)
  2039. goto out;
  2040. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2041. if (r < 0)
  2042. goto out;
  2043. data = ~0;
  2044. r = kvm_write_guest_page(kvm, fn, &data,
  2045. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  2046. sizeof(u8));
  2047. if (r < 0)
  2048. goto out;
  2049. ret = 1;
  2050. out:
  2051. return ret;
  2052. }
  2053. static int init_rmode_identity_map(struct kvm *kvm)
  2054. {
  2055. int i, r, ret;
  2056. pfn_t identity_map_pfn;
  2057. u32 tmp;
  2058. if (!enable_ept)
  2059. return 1;
  2060. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  2061. printk(KERN_ERR "EPT: identity-mapping pagetable "
  2062. "haven't been allocated!\n");
  2063. return 0;
  2064. }
  2065. if (likely(kvm->arch.ept_identity_pagetable_done))
  2066. return 1;
  2067. ret = 0;
  2068. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  2069. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  2070. if (r < 0)
  2071. goto out;
  2072. /* Set up identity-mapping pagetable for EPT in real mode */
  2073. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  2074. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  2075. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  2076. r = kvm_write_guest_page(kvm, identity_map_pfn,
  2077. &tmp, i * sizeof(tmp), sizeof(tmp));
  2078. if (r < 0)
  2079. goto out;
  2080. }
  2081. kvm->arch.ept_identity_pagetable_done = true;
  2082. ret = 1;
  2083. out:
  2084. return ret;
  2085. }
  2086. static void seg_setup(int seg)
  2087. {
  2088. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2089. unsigned int ar;
  2090. vmcs_write16(sf->selector, 0);
  2091. vmcs_writel(sf->base, 0);
  2092. vmcs_write32(sf->limit, 0xffff);
  2093. if (enable_unrestricted_guest) {
  2094. ar = 0x93;
  2095. if (seg == VCPU_SREG_CS)
  2096. ar |= 0x08; /* code segment */
  2097. } else
  2098. ar = 0xf3;
  2099. vmcs_write32(sf->ar_bytes, ar);
  2100. }
  2101. static int alloc_apic_access_page(struct kvm *kvm)
  2102. {
  2103. struct kvm_userspace_memory_region kvm_userspace_mem;
  2104. int r = 0;
  2105. mutex_lock(&kvm->slots_lock);
  2106. if (kvm->arch.apic_access_page)
  2107. goto out;
  2108. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  2109. kvm_userspace_mem.flags = 0;
  2110. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  2111. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2112. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2113. if (r)
  2114. goto out;
  2115. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  2116. out:
  2117. mutex_unlock(&kvm->slots_lock);
  2118. return r;
  2119. }
  2120. static int alloc_identity_pagetable(struct kvm *kvm)
  2121. {
  2122. struct kvm_userspace_memory_region kvm_userspace_mem;
  2123. int r = 0;
  2124. mutex_lock(&kvm->slots_lock);
  2125. if (kvm->arch.ept_identity_pagetable)
  2126. goto out;
  2127. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  2128. kvm_userspace_mem.flags = 0;
  2129. kvm_userspace_mem.guest_phys_addr =
  2130. kvm->arch.ept_identity_map_addr;
  2131. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2132. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2133. if (r)
  2134. goto out;
  2135. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  2136. kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
  2137. out:
  2138. mutex_unlock(&kvm->slots_lock);
  2139. return r;
  2140. }
  2141. static void allocate_vpid(struct vcpu_vmx *vmx)
  2142. {
  2143. int vpid;
  2144. vmx->vpid = 0;
  2145. if (!enable_vpid)
  2146. return;
  2147. spin_lock(&vmx_vpid_lock);
  2148. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  2149. if (vpid < VMX_NR_VPIDS) {
  2150. vmx->vpid = vpid;
  2151. __set_bit(vpid, vmx_vpid_bitmap);
  2152. }
  2153. spin_unlock(&vmx_vpid_lock);
  2154. }
  2155. static void free_vpid(struct vcpu_vmx *vmx)
  2156. {
  2157. if (!enable_vpid)
  2158. return;
  2159. spin_lock(&vmx_vpid_lock);
  2160. if (vmx->vpid != 0)
  2161. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  2162. spin_unlock(&vmx_vpid_lock);
  2163. }
  2164. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
  2165. {
  2166. int f = sizeof(unsigned long);
  2167. if (!cpu_has_vmx_msr_bitmap())
  2168. return;
  2169. /*
  2170. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  2171. * have the write-low and read-high bitmap offsets the wrong way round.
  2172. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  2173. */
  2174. if (msr <= 0x1fff) {
  2175. __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
  2176. __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
  2177. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2178. msr &= 0x1fff;
  2179. __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
  2180. __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
  2181. }
  2182. }
  2183. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  2184. {
  2185. if (!longmode_only)
  2186. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
  2187. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
  2188. }
  2189. /*
  2190. * Sets up the vmcs for emulated real mode.
  2191. */
  2192. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  2193. {
  2194. u32 host_sysenter_cs, msr_low, msr_high;
  2195. u32 junk;
  2196. u64 host_pat;
  2197. unsigned long a;
  2198. struct desc_ptr dt;
  2199. int i;
  2200. unsigned long kvm_vmx_return;
  2201. u32 exec_control;
  2202. /* I/O */
  2203. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  2204. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  2205. if (cpu_has_vmx_msr_bitmap())
  2206. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  2207. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  2208. /* Control */
  2209. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  2210. vmcs_config.pin_based_exec_ctrl);
  2211. exec_control = vmcs_config.cpu_based_exec_ctrl;
  2212. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  2213. exec_control &= ~CPU_BASED_TPR_SHADOW;
  2214. #ifdef CONFIG_X86_64
  2215. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  2216. CPU_BASED_CR8_LOAD_EXITING;
  2217. #endif
  2218. }
  2219. if (!enable_ept)
  2220. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  2221. CPU_BASED_CR3_LOAD_EXITING |
  2222. CPU_BASED_INVLPG_EXITING;
  2223. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  2224. if (cpu_has_secondary_exec_ctrls()) {
  2225. exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  2226. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2227. exec_control &=
  2228. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  2229. if (vmx->vpid == 0)
  2230. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  2231. if (!enable_ept) {
  2232. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  2233. enable_unrestricted_guest = 0;
  2234. }
  2235. if (!enable_unrestricted_guest)
  2236. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2237. if (!ple_gap)
  2238. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  2239. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  2240. }
  2241. if (ple_gap) {
  2242. vmcs_write32(PLE_GAP, ple_gap);
  2243. vmcs_write32(PLE_WINDOW, ple_window);
  2244. }
  2245. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
  2246. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
  2247. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  2248. vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
  2249. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  2250. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  2251. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  2252. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2253. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2254. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  2255. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  2256. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2257. #ifdef CONFIG_X86_64
  2258. rdmsrl(MSR_FS_BASE, a);
  2259. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  2260. rdmsrl(MSR_GS_BASE, a);
  2261. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  2262. #else
  2263. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  2264. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  2265. #endif
  2266. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  2267. native_store_idt(&dt);
  2268. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  2269. asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
  2270. vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
  2271. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  2272. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  2273. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  2274. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  2275. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  2276. rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
  2277. vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
  2278. rdmsrl(MSR_IA32_SYSENTER_ESP, a);
  2279. vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
  2280. rdmsrl(MSR_IA32_SYSENTER_EIP, a);
  2281. vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
  2282. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  2283. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2284. host_pat = msr_low | ((u64) msr_high << 32);
  2285. vmcs_write64(HOST_IA32_PAT, host_pat);
  2286. }
  2287. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  2288. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2289. host_pat = msr_low | ((u64) msr_high << 32);
  2290. /* Write the default value follow host pat */
  2291. vmcs_write64(GUEST_IA32_PAT, host_pat);
  2292. /* Keep arch.pat sync with GUEST_IA32_PAT */
  2293. vmx->vcpu.arch.pat = host_pat;
  2294. }
  2295. for (i = 0; i < NR_VMX_MSR; ++i) {
  2296. u32 index = vmx_msr_index[i];
  2297. u32 data_low, data_high;
  2298. int j = vmx->nmsrs;
  2299. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  2300. continue;
  2301. if (wrmsr_safe(index, data_low, data_high) < 0)
  2302. continue;
  2303. vmx->guest_msrs[j].index = i;
  2304. vmx->guest_msrs[j].data = 0;
  2305. vmx->guest_msrs[j].mask = -1ull;
  2306. ++vmx->nmsrs;
  2307. }
  2308. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  2309. /* 22.2.1, 20.8.1 */
  2310. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  2311. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  2312. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  2313. if (enable_ept)
  2314. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  2315. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  2316. kvm_write_tsc(&vmx->vcpu, 0);
  2317. return 0;
  2318. }
  2319. static int init_rmode(struct kvm *kvm)
  2320. {
  2321. int idx, ret = 0;
  2322. idx = srcu_read_lock(&kvm->srcu);
  2323. if (!init_rmode_tss(kvm))
  2324. goto exit;
  2325. if (!init_rmode_identity_map(kvm))
  2326. goto exit;
  2327. ret = 1;
  2328. exit:
  2329. srcu_read_unlock(&kvm->srcu, idx);
  2330. return ret;
  2331. }
  2332. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  2333. {
  2334. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2335. u64 msr;
  2336. int ret;
  2337. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  2338. if (!init_rmode(vmx->vcpu.kvm)) {
  2339. ret = -ENOMEM;
  2340. goto out;
  2341. }
  2342. vmx->rmode.vm86_active = 0;
  2343. vmx->soft_vnmi_blocked = 0;
  2344. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  2345. kvm_set_cr8(&vmx->vcpu, 0);
  2346. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  2347. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2348. msr |= MSR_IA32_APICBASE_BSP;
  2349. kvm_set_apic_base(&vmx->vcpu, msr);
  2350. ret = fx_init(&vmx->vcpu);
  2351. if (ret != 0)
  2352. goto out;
  2353. seg_setup(VCPU_SREG_CS);
  2354. /*
  2355. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  2356. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  2357. */
  2358. if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
  2359. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  2360. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  2361. } else {
  2362. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  2363. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  2364. }
  2365. seg_setup(VCPU_SREG_DS);
  2366. seg_setup(VCPU_SREG_ES);
  2367. seg_setup(VCPU_SREG_FS);
  2368. seg_setup(VCPU_SREG_GS);
  2369. seg_setup(VCPU_SREG_SS);
  2370. vmcs_write16(GUEST_TR_SELECTOR, 0);
  2371. vmcs_writel(GUEST_TR_BASE, 0);
  2372. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  2373. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2374. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  2375. vmcs_writel(GUEST_LDTR_BASE, 0);
  2376. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  2377. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  2378. vmcs_write32(GUEST_SYSENTER_CS, 0);
  2379. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  2380. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  2381. vmcs_writel(GUEST_RFLAGS, 0x02);
  2382. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2383. kvm_rip_write(vcpu, 0xfff0);
  2384. else
  2385. kvm_rip_write(vcpu, 0);
  2386. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  2387. vmcs_writel(GUEST_DR7, 0x400);
  2388. vmcs_writel(GUEST_GDTR_BASE, 0);
  2389. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  2390. vmcs_writel(GUEST_IDTR_BASE, 0);
  2391. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  2392. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2393. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  2394. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  2395. /* Special registers */
  2396. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  2397. setup_msrs(vmx);
  2398. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  2399. if (cpu_has_vmx_tpr_shadow()) {
  2400. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  2401. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  2402. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  2403. page_to_phys(vmx->vcpu.arch.apic->regs_page));
  2404. vmcs_write32(TPR_THRESHOLD, 0);
  2405. }
  2406. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2407. vmcs_write64(APIC_ACCESS_ADDR,
  2408. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  2409. if (vmx->vpid != 0)
  2410. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  2411. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  2412. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  2413. vmx_set_cr4(&vmx->vcpu, 0);
  2414. vmx_set_efer(&vmx->vcpu, 0);
  2415. vmx_fpu_activate(&vmx->vcpu);
  2416. update_exception_bitmap(&vmx->vcpu);
  2417. vpid_sync_context(vmx);
  2418. ret = 0;
  2419. /* HACK: Don't enable emulation on guest boot/reset */
  2420. vmx->emulation_required = 0;
  2421. out:
  2422. return ret;
  2423. }
  2424. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2425. {
  2426. u32 cpu_based_vm_exec_control;
  2427. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2428. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  2429. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2430. }
  2431. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2432. {
  2433. u32 cpu_based_vm_exec_control;
  2434. if (!cpu_has_virtual_nmis()) {
  2435. enable_irq_window(vcpu);
  2436. return;
  2437. }
  2438. if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  2439. enable_irq_window(vcpu);
  2440. return;
  2441. }
  2442. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2443. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  2444. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2445. }
  2446. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  2447. {
  2448. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2449. uint32_t intr;
  2450. int irq = vcpu->arch.interrupt.nr;
  2451. trace_kvm_inj_virq(irq);
  2452. ++vcpu->stat.irq_injections;
  2453. if (vmx->rmode.vm86_active) {
  2454. if (kvm_inject_realmode_interrupt(vcpu, irq) != EMULATE_DONE)
  2455. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2456. return;
  2457. }
  2458. intr = irq | INTR_INFO_VALID_MASK;
  2459. if (vcpu->arch.interrupt.soft) {
  2460. intr |= INTR_TYPE_SOFT_INTR;
  2461. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2462. vmx->vcpu.arch.event_exit_inst_len);
  2463. } else
  2464. intr |= INTR_TYPE_EXT_INTR;
  2465. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  2466. vmx_clear_hlt(vcpu);
  2467. }
  2468. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  2469. {
  2470. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2471. if (!cpu_has_virtual_nmis()) {
  2472. /*
  2473. * Tracking the NMI-blocked state in software is built upon
  2474. * finding the next open IRQ window. This, in turn, depends on
  2475. * well-behaving guests: They have to keep IRQs disabled at
  2476. * least as long as the NMI handler runs. Otherwise we may
  2477. * cause NMI nesting, maybe breaking the guest. But as this is
  2478. * highly unlikely, we can live with the residual risk.
  2479. */
  2480. vmx->soft_vnmi_blocked = 1;
  2481. vmx->vnmi_blocked_time = 0;
  2482. }
  2483. ++vcpu->stat.nmi_injections;
  2484. if (vmx->rmode.vm86_active) {
  2485. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR) != EMULATE_DONE)
  2486. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2487. return;
  2488. }
  2489. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2490. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  2491. vmx_clear_hlt(vcpu);
  2492. }
  2493. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  2494. {
  2495. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  2496. return 0;
  2497. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2498. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  2499. | GUEST_INTR_STATE_NMI));
  2500. }
  2501. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  2502. {
  2503. if (!cpu_has_virtual_nmis())
  2504. return to_vmx(vcpu)->soft_vnmi_blocked;
  2505. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  2506. }
  2507. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2508. {
  2509. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2510. if (!cpu_has_virtual_nmis()) {
  2511. if (vmx->soft_vnmi_blocked != masked) {
  2512. vmx->soft_vnmi_blocked = masked;
  2513. vmx->vnmi_blocked_time = 0;
  2514. }
  2515. } else {
  2516. if (masked)
  2517. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2518. GUEST_INTR_STATE_NMI);
  2519. else
  2520. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  2521. GUEST_INTR_STATE_NMI);
  2522. }
  2523. }
  2524. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  2525. {
  2526. return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  2527. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2528. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  2529. }
  2530. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2531. {
  2532. int ret;
  2533. struct kvm_userspace_memory_region tss_mem = {
  2534. .slot = TSS_PRIVATE_MEMSLOT,
  2535. .guest_phys_addr = addr,
  2536. .memory_size = PAGE_SIZE * 3,
  2537. .flags = 0,
  2538. };
  2539. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  2540. if (ret)
  2541. return ret;
  2542. kvm->arch.tss_addr = addr;
  2543. return 0;
  2544. }
  2545. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  2546. int vec, u32 err_code)
  2547. {
  2548. /*
  2549. * Instruction with address size override prefix opcode 0x67
  2550. * Cause the #SS fault with 0 error code in VM86 mode.
  2551. */
  2552. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  2553. if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
  2554. return 1;
  2555. /*
  2556. * Forward all other exceptions that are valid in real mode.
  2557. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  2558. * the required debugging infrastructure rework.
  2559. */
  2560. switch (vec) {
  2561. case DB_VECTOR:
  2562. if (vcpu->guest_debug &
  2563. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  2564. return 0;
  2565. kvm_queue_exception(vcpu, vec);
  2566. return 1;
  2567. case BP_VECTOR:
  2568. /*
  2569. * Update instruction length as we may reinject the exception
  2570. * from user space while in guest debugging mode.
  2571. */
  2572. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  2573. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2574. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  2575. return 0;
  2576. /* fall through */
  2577. case DE_VECTOR:
  2578. case OF_VECTOR:
  2579. case BR_VECTOR:
  2580. case UD_VECTOR:
  2581. case DF_VECTOR:
  2582. case SS_VECTOR:
  2583. case GP_VECTOR:
  2584. case MF_VECTOR:
  2585. kvm_queue_exception(vcpu, vec);
  2586. return 1;
  2587. }
  2588. return 0;
  2589. }
  2590. /*
  2591. * Trigger machine check on the host. We assume all the MSRs are already set up
  2592. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  2593. * We pass a fake environment to the machine check handler because we want
  2594. * the guest to be always treated like user space, no matter what context
  2595. * it used internally.
  2596. */
  2597. static void kvm_machine_check(void)
  2598. {
  2599. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  2600. struct pt_regs regs = {
  2601. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  2602. .flags = X86_EFLAGS_IF,
  2603. };
  2604. do_machine_check(&regs, 0);
  2605. #endif
  2606. }
  2607. static int handle_machine_check(struct kvm_vcpu *vcpu)
  2608. {
  2609. /* already handled by vcpu_run */
  2610. return 1;
  2611. }
  2612. static int handle_exception(struct kvm_vcpu *vcpu)
  2613. {
  2614. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2615. struct kvm_run *kvm_run = vcpu->run;
  2616. u32 intr_info, ex_no, error_code;
  2617. unsigned long cr2, rip, dr6;
  2618. u32 vect_info;
  2619. enum emulation_result er;
  2620. vect_info = vmx->idt_vectoring_info;
  2621. intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  2622. if (is_machine_check(intr_info))
  2623. return handle_machine_check(vcpu);
  2624. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  2625. !is_page_fault(intr_info)) {
  2626. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2627. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  2628. vcpu->run->internal.ndata = 2;
  2629. vcpu->run->internal.data[0] = vect_info;
  2630. vcpu->run->internal.data[1] = intr_info;
  2631. return 0;
  2632. }
  2633. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  2634. return 1; /* already handled by vmx_vcpu_run() */
  2635. if (is_no_device(intr_info)) {
  2636. vmx_fpu_activate(vcpu);
  2637. return 1;
  2638. }
  2639. if (is_invalid_opcode(intr_info)) {
  2640. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  2641. if (er != EMULATE_DONE)
  2642. kvm_queue_exception(vcpu, UD_VECTOR);
  2643. return 1;
  2644. }
  2645. error_code = 0;
  2646. rip = kvm_rip_read(vcpu);
  2647. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  2648. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  2649. if (is_page_fault(intr_info)) {
  2650. /* EPT won't cause page fault directly */
  2651. if (enable_ept)
  2652. BUG();
  2653. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  2654. trace_kvm_page_fault(cr2, error_code);
  2655. if (kvm_event_needs_reinjection(vcpu))
  2656. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  2657. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  2658. }
  2659. if (vmx->rmode.vm86_active &&
  2660. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  2661. error_code)) {
  2662. if (vcpu->arch.halt_request) {
  2663. vcpu->arch.halt_request = 0;
  2664. return kvm_emulate_halt(vcpu);
  2665. }
  2666. return 1;
  2667. }
  2668. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  2669. switch (ex_no) {
  2670. case DB_VECTOR:
  2671. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  2672. if (!(vcpu->guest_debug &
  2673. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  2674. vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
  2675. kvm_queue_exception(vcpu, DB_VECTOR);
  2676. return 1;
  2677. }
  2678. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  2679. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  2680. /* fall through */
  2681. case BP_VECTOR:
  2682. /*
  2683. * Update instruction length as we may reinject #BP from
  2684. * user space while in guest debugging mode. Reading it for
  2685. * #DB as well causes no harm, it is not used in that case.
  2686. */
  2687. vmx->vcpu.arch.event_exit_inst_len =
  2688. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2689. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  2690. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  2691. kvm_run->debug.arch.exception = ex_no;
  2692. break;
  2693. default:
  2694. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  2695. kvm_run->ex.exception = ex_no;
  2696. kvm_run->ex.error_code = error_code;
  2697. break;
  2698. }
  2699. return 0;
  2700. }
  2701. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  2702. {
  2703. ++vcpu->stat.irq_exits;
  2704. return 1;
  2705. }
  2706. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  2707. {
  2708. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  2709. return 0;
  2710. }
  2711. static int handle_io(struct kvm_vcpu *vcpu)
  2712. {
  2713. unsigned long exit_qualification;
  2714. int size, in, string;
  2715. unsigned port;
  2716. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2717. string = (exit_qualification & 16) != 0;
  2718. in = (exit_qualification & 8) != 0;
  2719. ++vcpu->stat.io_exits;
  2720. if (string || in)
  2721. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  2722. port = exit_qualification >> 16;
  2723. size = (exit_qualification & 7) + 1;
  2724. skip_emulated_instruction(vcpu);
  2725. return kvm_fast_pio_out(vcpu, size, port);
  2726. }
  2727. static void
  2728. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2729. {
  2730. /*
  2731. * Patch in the VMCALL instruction:
  2732. */
  2733. hypercall[0] = 0x0f;
  2734. hypercall[1] = 0x01;
  2735. hypercall[2] = 0xc1;
  2736. }
  2737. static int handle_cr(struct kvm_vcpu *vcpu)
  2738. {
  2739. unsigned long exit_qualification, val;
  2740. int cr;
  2741. int reg;
  2742. int err;
  2743. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2744. cr = exit_qualification & 15;
  2745. reg = (exit_qualification >> 8) & 15;
  2746. switch ((exit_qualification >> 4) & 3) {
  2747. case 0: /* mov to cr */
  2748. val = kvm_register_read(vcpu, reg);
  2749. trace_kvm_cr_write(cr, val);
  2750. switch (cr) {
  2751. case 0:
  2752. err = kvm_set_cr0(vcpu, val);
  2753. kvm_complete_insn_gp(vcpu, err);
  2754. return 1;
  2755. case 3:
  2756. err = kvm_set_cr3(vcpu, val);
  2757. kvm_complete_insn_gp(vcpu, err);
  2758. return 1;
  2759. case 4:
  2760. err = kvm_set_cr4(vcpu, val);
  2761. kvm_complete_insn_gp(vcpu, err);
  2762. return 1;
  2763. case 8: {
  2764. u8 cr8_prev = kvm_get_cr8(vcpu);
  2765. u8 cr8 = kvm_register_read(vcpu, reg);
  2766. err = kvm_set_cr8(vcpu, cr8);
  2767. kvm_complete_insn_gp(vcpu, err);
  2768. if (irqchip_in_kernel(vcpu->kvm))
  2769. return 1;
  2770. if (cr8_prev <= cr8)
  2771. return 1;
  2772. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  2773. return 0;
  2774. }
  2775. };
  2776. break;
  2777. case 2: /* clts */
  2778. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  2779. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  2780. skip_emulated_instruction(vcpu);
  2781. vmx_fpu_activate(vcpu);
  2782. return 1;
  2783. case 1: /*mov from cr*/
  2784. switch (cr) {
  2785. case 3:
  2786. val = kvm_read_cr3(vcpu);
  2787. kvm_register_write(vcpu, reg, val);
  2788. trace_kvm_cr_read(cr, val);
  2789. skip_emulated_instruction(vcpu);
  2790. return 1;
  2791. case 8:
  2792. val = kvm_get_cr8(vcpu);
  2793. kvm_register_write(vcpu, reg, val);
  2794. trace_kvm_cr_read(cr, val);
  2795. skip_emulated_instruction(vcpu);
  2796. return 1;
  2797. }
  2798. break;
  2799. case 3: /* lmsw */
  2800. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  2801. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  2802. kvm_lmsw(vcpu, val);
  2803. skip_emulated_instruction(vcpu);
  2804. return 1;
  2805. default:
  2806. break;
  2807. }
  2808. vcpu->run->exit_reason = 0;
  2809. pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  2810. (int)(exit_qualification >> 4) & 3, cr);
  2811. return 0;
  2812. }
  2813. static int handle_dr(struct kvm_vcpu *vcpu)
  2814. {
  2815. unsigned long exit_qualification;
  2816. int dr, reg;
  2817. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  2818. if (!kvm_require_cpl(vcpu, 0))
  2819. return 1;
  2820. dr = vmcs_readl(GUEST_DR7);
  2821. if (dr & DR7_GD) {
  2822. /*
  2823. * As the vm-exit takes precedence over the debug trap, we
  2824. * need to emulate the latter, either for the host or the
  2825. * guest debugging itself.
  2826. */
  2827. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  2828. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  2829. vcpu->run->debug.arch.dr7 = dr;
  2830. vcpu->run->debug.arch.pc =
  2831. vmcs_readl(GUEST_CS_BASE) +
  2832. vmcs_readl(GUEST_RIP);
  2833. vcpu->run->debug.arch.exception = DB_VECTOR;
  2834. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  2835. return 0;
  2836. } else {
  2837. vcpu->arch.dr7 &= ~DR7_GD;
  2838. vcpu->arch.dr6 |= DR6_BD;
  2839. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  2840. kvm_queue_exception(vcpu, DB_VECTOR);
  2841. return 1;
  2842. }
  2843. }
  2844. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2845. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  2846. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  2847. if (exit_qualification & TYPE_MOV_FROM_DR) {
  2848. unsigned long val;
  2849. if (!kvm_get_dr(vcpu, dr, &val))
  2850. kvm_register_write(vcpu, reg, val);
  2851. } else
  2852. kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
  2853. skip_emulated_instruction(vcpu);
  2854. return 1;
  2855. }
  2856. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  2857. {
  2858. vmcs_writel(GUEST_DR7, val);
  2859. }
  2860. static int handle_cpuid(struct kvm_vcpu *vcpu)
  2861. {
  2862. kvm_emulate_cpuid(vcpu);
  2863. return 1;
  2864. }
  2865. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  2866. {
  2867. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2868. u64 data;
  2869. if (vmx_get_msr(vcpu, ecx, &data)) {
  2870. trace_kvm_msr_read_ex(ecx);
  2871. kvm_inject_gp(vcpu, 0);
  2872. return 1;
  2873. }
  2874. trace_kvm_msr_read(ecx, data);
  2875. /* FIXME: handling of bits 32:63 of rax, rdx */
  2876. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  2877. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  2878. skip_emulated_instruction(vcpu);
  2879. return 1;
  2880. }
  2881. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  2882. {
  2883. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2884. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  2885. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2886. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  2887. trace_kvm_msr_write_ex(ecx, data);
  2888. kvm_inject_gp(vcpu, 0);
  2889. return 1;
  2890. }
  2891. trace_kvm_msr_write(ecx, data);
  2892. skip_emulated_instruction(vcpu);
  2893. return 1;
  2894. }
  2895. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  2896. {
  2897. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2898. return 1;
  2899. }
  2900. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  2901. {
  2902. u32 cpu_based_vm_exec_control;
  2903. /* clear pending irq */
  2904. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2905. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  2906. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2907. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2908. ++vcpu->stat.irq_window_exits;
  2909. /*
  2910. * If the user space waits to inject interrupts, exit as soon as
  2911. * possible
  2912. */
  2913. if (!irqchip_in_kernel(vcpu->kvm) &&
  2914. vcpu->run->request_interrupt_window &&
  2915. !kvm_cpu_has_interrupt(vcpu)) {
  2916. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2917. return 0;
  2918. }
  2919. return 1;
  2920. }
  2921. static int handle_halt(struct kvm_vcpu *vcpu)
  2922. {
  2923. skip_emulated_instruction(vcpu);
  2924. return kvm_emulate_halt(vcpu);
  2925. }
  2926. static int handle_vmcall(struct kvm_vcpu *vcpu)
  2927. {
  2928. skip_emulated_instruction(vcpu);
  2929. kvm_emulate_hypercall(vcpu);
  2930. return 1;
  2931. }
  2932. static int handle_vmx_insn(struct kvm_vcpu *vcpu)
  2933. {
  2934. kvm_queue_exception(vcpu, UD_VECTOR);
  2935. return 1;
  2936. }
  2937. static int handle_invd(struct kvm_vcpu *vcpu)
  2938. {
  2939. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  2940. }
  2941. static int handle_invlpg(struct kvm_vcpu *vcpu)
  2942. {
  2943. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2944. kvm_mmu_invlpg(vcpu, exit_qualification);
  2945. skip_emulated_instruction(vcpu);
  2946. return 1;
  2947. }
  2948. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  2949. {
  2950. skip_emulated_instruction(vcpu);
  2951. kvm_emulate_wbinvd(vcpu);
  2952. return 1;
  2953. }
  2954. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  2955. {
  2956. u64 new_bv = kvm_read_edx_eax(vcpu);
  2957. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2958. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  2959. skip_emulated_instruction(vcpu);
  2960. return 1;
  2961. }
  2962. static int handle_apic_access(struct kvm_vcpu *vcpu)
  2963. {
  2964. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  2965. }
  2966. static int handle_task_switch(struct kvm_vcpu *vcpu)
  2967. {
  2968. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2969. unsigned long exit_qualification;
  2970. bool has_error_code = false;
  2971. u32 error_code = 0;
  2972. u16 tss_selector;
  2973. int reason, type, idt_v;
  2974. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  2975. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  2976. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2977. reason = (u32)exit_qualification >> 30;
  2978. if (reason == TASK_SWITCH_GATE && idt_v) {
  2979. switch (type) {
  2980. case INTR_TYPE_NMI_INTR:
  2981. vcpu->arch.nmi_injected = false;
  2982. if (cpu_has_virtual_nmis())
  2983. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2984. GUEST_INTR_STATE_NMI);
  2985. break;
  2986. case INTR_TYPE_EXT_INTR:
  2987. case INTR_TYPE_SOFT_INTR:
  2988. kvm_clear_interrupt_queue(vcpu);
  2989. break;
  2990. case INTR_TYPE_HARD_EXCEPTION:
  2991. if (vmx->idt_vectoring_info &
  2992. VECTORING_INFO_DELIVER_CODE_MASK) {
  2993. has_error_code = true;
  2994. error_code =
  2995. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  2996. }
  2997. /* fall through */
  2998. case INTR_TYPE_SOFT_EXCEPTION:
  2999. kvm_clear_exception_queue(vcpu);
  3000. break;
  3001. default:
  3002. break;
  3003. }
  3004. }
  3005. tss_selector = exit_qualification;
  3006. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  3007. type != INTR_TYPE_EXT_INTR &&
  3008. type != INTR_TYPE_NMI_INTR))
  3009. skip_emulated_instruction(vcpu);
  3010. if (kvm_task_switch(vcpu, tss_selector, reason,
  3011. has_error_code, error_code) == EMULATE_FAIL) {
  3012. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3013. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  3014. vcpu->run->internal.ndata = 0;
  3015. return 0;
  3016. }
  3017. /* clear all local breakpoint enable flags */
  3018. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
  3019. /*
  3020. * TODO: What about debug traps on tss switch?
  3021. * Are we supposed to inject them and update dr6?
  3022. */
  3023. return 1;
  3024. }
  3025. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  3026. {
  3027. unsigned long exit_qualification;
  3028. gpa_t gpa;
  3029. int gla_validity;
  3030. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3031. if (exit_qualification & (1 << 6)) {
  3032. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  3033. return -EINVAL;
  3034. }
  3035. gla_validity = (exit_qualification >> 7) & 0x3;
  3036. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  3037. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  3038. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  3039. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  3040. vmcs_readl(GUEST_LINEAR_ADDRESS));
  3041. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  3042. (long unsigned int)exit_qualification);
  3043. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3044. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  3045. return 0;
  3046. }
  3047. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3048. trace_kvm_page_fault(gpa, exit_qualification);
  3049. return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
  3050. }
  3051. static u64 ept_rsvd_mask(u64 spte, int level)
  3052. {
  3053. int i;
  3054. u64 mask = 0;
  3055. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  3056. mask |= (1ULL << i);
  3057. if (level > 2)
  3058. /* bits 7:3 reserved */
  3059. mask |= 0xf8;
  3060. else if (level == 2) {
  3061. if (spte & (1ULL << 7))
  3062. /* 2MB ref, bits 20:12 reserved */
  3063. mask |= 0x1ff000;
  3064. else
  3065. /* bits 6:3 reserved */
  3066. mask |= 0x78;
  3067. }
  3068. return mask;
  3069. }
  3070. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  3071. int level)
  3072. {
  3073. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  3074. /* 010b (write-only) */
  3075. WARN_ON((spte & 0x7) == 0x2);
  3076. /* 110b (write/execute) */
  3077. WARN_ON((spte & 0x7) == 0x6);
  3078. /* 100b (execute-only) and value not supported by logical processor */
  3079. if (!cpu_has_vmx_ept_execute_only())
  3080. WARN_ON((spte & 0x7) == 0x4);
  3081. /* not 000b */
  3082. if ((spte & 0x7)) {
  3083. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  3084. if (rsvd_bits != 0) {
  3085. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  3086. __func__, rsvd_bits);
  3087. WARN_ON(1);
  3088. }
  3089. if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
  3090. u64 ept_mem_type = (spte & 0x38) >> 3;
  3091. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  3092. ept_mem_type == 7) {
  3093. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  3094. __func__, ept_mem_type);
  3095. WARN_ON(1);
  3096. }
  3097. }
  3098. }
  3099. }
  3100. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  3101. {
  3102. u64 sptes[4];
  3103. int nr_sptes, i;
  3104. gpa_t gpa;
  3105. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3106. printk(KERN_ERR "EPT: Misconfiguration.\n");
  3107. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  3108. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  3109. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  3110. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  3111. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3112. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  3113. return 0;
  3114. }
  3115. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  3116. {
  3117. u32 cpu_based_vm_exec_control;
  3118. /* clear pending NMI */
  3119. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3120. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  3121. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3122. ++vcpu->stat.nmi_window_exits;
  3123. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3124. return 1;
  3125. }
  3126. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  3127. {
  3128. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3129. enum emulation_result err = EMULATE_DONE;
  3130. int ret = 1;
  3131. u32 cpu_exec_ctrl;
  3132. bool intr_window_requested;
  3133. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3134. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  3135. while (!guest_state_valid(vcpu)) {
  3136. if (intr_window_requested
  3137. && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
  3138. return handle_interrupt_window(&vmx->vcpu);
  3139. err = emulate_instruction(vcpu, 0);
  3140. if (err == EMULATE_DO_MMIO) {
  3141. ret = 0;
  3142. goto out;
  3143. }
  3144. if (err != EMULATE_DONE)
  3145. return 0;
  3146. if (signal_pending(current))
  3147. goto out;
  3148. if (need_resched())
  3149. schedule();
  3150. }
  3151. vmx->emulation_required = 0;
  3152. out:
  3153. return ret;
  3154. }
  3155. /*
  3156. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  3157. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  3158. */
  3159. static int handle_pause(struct kvm_vcpu *vcpu)
  3160. {
  3161. skip_emulated_instruction(vcpu);
  3162. kvm_vcpu_on_spin(vcpu);
  3163. return 1;
  3164. }
  3165. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  3166. {
  3167. kvm_queue_exception(vcpu, UD_VECTOR);
  3168. return 1;
  3169. }
  3170. /*
  3171. * The exit handlers return 1 if the exit was handled fully and guest execution
  3172. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  3173. * to be done to userspace and return 0.
  3174. */
  3175. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  3176. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  3177. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  3178. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  3179. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  3180. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  3181. [EXIT_REASON_CR_ACCESS] = handle_cr,
  3182. [EXIT_REASON_DR_ACCESS] = handle_dr,
  3183. [EXIT_REASON_CPUID] = handle_cpuid,
  3184. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  3185. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  3186. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  3187. [EXIT_REASON_HLT] = handle_halt,
  3188. [EXIT_REASON_INVD] = handle_invd,
  3189. [EXIT_REASON_INVLPG] = handle_invlpg,
  3190. [EXIT_REASON_VMCALL] = handle_vmcall,
  3191. [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
  3192. [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
  3193. [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
  3194. [EXIT_REASON_VMPTRST] = handle_vmx_insn,
  3195. [EXIT_REASON_VMREAD] = handle_vmx_insn,
  3196. [EXIT_REASON_VMRESUME] = handle_vmx_insn,
  3197. [EXIT_REASON_VMWRITE] = handle_vmx_insn,
  3198. [EXIT_REASON_VMOFF] = handle_vmx_insn,
  3199. [EXIT_REASON_VMON] = handle_vmx_insn,
  3200. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  3201. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  3202. [EXIT_REASON_WBINVD] = handle_wbinvd,
  3203. [EXIT_REASON_XSETBV] = handle_xsetbv,
  3204. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  3205. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  3206. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  3207. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  3208. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  3209. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
  3210. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
  3211. };
  3212. static const int kvm_vmx_max_exit_handlers =
  3213. ARRAY_SIZE(kvm_vmx_exit_handlers);
  3214. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  3215. {
  3216. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  3217. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  3218. }
  3219. /*
  3220. * The guest has exited. See if we can fix it or if we need userspace
  3221. * assistance.
  3222. */
  3223. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  3224. {
  3225. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3226. u32 exit_reason = vmx->exit_reason;
  3227. u32 vectoring_info = vmx->idt_vectoring_info;
  3228. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  3229. /* If guest state is invalid, start emulating */
  3230. if (vmx->emulation_required && emulate_invalid_guest_state)
  3231. return handle_invalid_guest_state(vcpu);
  3232. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  3233. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3234. vcpu->run->fail_entry.hardware_entry_failure_reason
  3235. = exit_reason;
  3236. return 0;
  3237. }
  3238. if (unlikely(vmx->fail)) {
  3239. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3240. vcpu->run->fail_entry.hardware_entry_failure_reason
  3241. = vmcs_read32(VM_INSTRUCTION_ERROR);
  3242. return 0;
  3243. }
  3244. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  3245. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  3246. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  3247. exit_reason != EXIT_REASON_TASK_SWITCH))
  3248. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  3249. "(0x%x) and exit reason is 0x%x\n",
  3250. __func__, vectoring_info, exit_reason);
  3251. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
  3252. if (vmx_interrupt_allowed(vcpu)) {
  3253. vmx->soft_vnmi_blocked = 0;
  3254. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  3255. vcpu->arch.nmi_pending) {
  3256. /*
  3257. * This CPU don't support us in finding the end of an
  3258. * NMI-blocked window if the guest runs with IRQs
  3259. * disabled. So we pull the trigger after 1 s of
  3260. * futile waiting, but inform the user about this.
  3261. */
  3262. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  3263. "state on VCPU %d after 1 s timeout\n",
  3264. __func__, vcpu->vcpu_id);
  3265. vmx->soft_vnmi_blocked = 0;
  3266. }
  3267. }
  3268. if (exit_reason < kvm_vmx_max_exit_handlers
  3269. && kvm_vmx_exit_handlers[exit_reason])
  3270. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  3271. else {
  3272. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3273. vcpu->run->hw.hardware_exit_reason = exit_reason;
  3274. }
  3275. return 0;
  3276. }
  3277. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  3278. {
  3279. if (irr == -1 || tpr < irr) {
  3280. vmcs_write32(TPR_THRESHOLD, 0);
  3281. return;
  3282. }
  3283. vmcs_write32(TPR_THRESHOLD, irr);
  3284. }
  3285. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  3286. {
  3287. u32 exit_intr_info = vmx->exit_intr_info;
  3288. /* Handle machine checks before interrupts are enabled */
  3289. if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
  3290. || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
  3291. && is_machine_check(exit_intr_info)))
  3292. kvm_machine_check();
  3293. /* We need to handle NMIs before interrupts are enabled */
  3294. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  3295. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  3296. kvm_before_handle_nmi(&vmx->vcpu);
  3297. asm("int $2");
  3298. kvm_after_handle_nmi(&vmx->vcpu);
  3299. }
  3300. }
  3301. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  3302. {
  3303. u32 exit_intr_info = vmx->exit_intr_info;
  3304. bool unblock_nmi;
  3305. u8 vector;
  3306. bool idtv_info_valid;
  3307. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  3308. if (cpu_has_virtual_nmis()) {
  3309. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  3310. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  3311. /*
  3312. * SDM 3: 27.7.1.2 (September 2008)
  3313. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  3314. * a guest IRET fault.
  3315. * SDM 3: 23.2.2 (September 2008)
  3316. * Bit 12 is undefined in any of the following cases:
  3317. * If the VM exit sets the valid bit in the IDT-vectoring
  3318. * information field.
  3319. * If the VM exit is due to a double fault.
  3320. */
  3321. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  3322. vector != DF_VECTOR && !idtv_info_valid)
  3323. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  3324. GUEST_INTR_STATE_NMI);
  3325. } else if (unlikely(vmx->soft_vnmi_blocked))
  3326. vmx->vnmi_blocked_time +=
  3327. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  3328. }
  3329. static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
  3330. u32 idt_vectoring_info,
  3331. int instr_len_field,
  3332. int error_code_field)
  3333. {
  3334. u8 vector;
  3335. int type;
  3336. bool idtv_info_valid;
  3337. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  3338. vmx->vcpu.arch.nmi_injected = false;
  3339. kvm_clear_exception_queue(&vmx->vcpu);
  3340. kvm_clear_interrupt_queue(&vmx->vcpu);
  3341. if (!idtv_info_valid)
  3342. return;
  3343. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  3344. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  3345. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  3346. switch (type) {
  3347. case INTR_TYPE_NMI_INTR:
  3348. vmx->vcpu.arch.nmi_injected = true;
  3349. /*
  3350. * SDM 3: 27.7.1.2 (September 2008)
  3351. * Clear bit "block by NMI" before VM entry if a NMI
  3352. * delivery faulted.
  3353. */
  3354. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  3355. GUEST_INTR_STATE_NMI);
  3356. break;
  3357. case INTR_TYPE_SOFT_EXCEPTION:
  3358. vmx->vcpu.arch.event_exit_inst_len =
  3359. vmcs_read32(instr_len_field);
  3360. /* fall through */
  3361. case INTR_TYPE_HARD_EXCEPTION:
  3362. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  3363. u32 err = vmcs_read32(error_code_field);
  3364. kvm_queue_exception_e(&vmx->vcpu, vector, err);
  3365. } else
  3366. kvm_queue_exception(&vmx->vcpu, vector);
  3367. break;
  3368. case INTR_TYPE_SOFT_INTR:
  3369. vmx->vcpu.arch.event_exit_inst_len =
  3370. vmcs_read32(instr_len_field);
  3371. /* fall through */
  3372. case INTR_TYPE_EXT_INTR:
  3373. kvm_queue_interrupt(&vmx->vcpu, vector,
  3374. type == INTR_TYPE_SOFT_INTR);
  3375. break;
  3376. default:
  3377. break;
  3378. }
  3379. }
  3380. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  3381. {
  3382. __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
  3383. VM_EXIT_INSTRUCTION_LEN,
  3384. IDT_VECTORING_ERROR_CODE);
  3385. }
  3386. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  3387. {
  3388. __vmx_complete_interrupts(to_vmx(vcpu),
  3389. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  3390. VM_ENTRY_INSTRUCTION_LEN,
  3391. VM_ENTRY_EXCEPTION_ERROR_CODE);
  3392. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  3393. }
  3394. #ifdef CONFIG_X86_64
  3395. #define R "r"
  3396. #define Q "q"
  3397. #else
  3398. #define R "e"
  3399. #define Q "l"
  3400. #endif
  3401. static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
  3402. {
  3403. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3404. /* Record the guest's net vcpu time for enforced NMI injections. */
  3405. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  3406. vmx->entry_time = ktime_get();
  3407. /* Don't enter VMX if guest state is invalid, let the exit handler
  3408. start emulation until we arrive back to a valid state */
  3409. if (vmx->emulation_required && emulate_invalid_guest_state)
  3410. return;
  3411. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  3412. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  3413. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  3414. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  3415. /* When single-stepping over STI and MOV SS, we must clear the
  3416. * corresponding interruptibility bits in the guest state. Otherwise
  3417. * vmentry fails as it then expects bit 14 (BS) in pending debug
  3418. * exceptions being set, but that's not correct for the guest debugging
  3419. * case. */
  3420. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  3421. vmx_set_interrupt_shadow(vcpu, 0);
  3422. asm(
  3423. /* Store host registers */
  3424. "push %%"R"dx; push %%"R"bp;"
  3425. "push %%"R"cx \n\t"
  3426. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  3427. "je 1f \n\t"
  3428. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  3429. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  3430. "1: \n\t"
  3431. /* Reload cr2 if changed */
  3432. "mov %c[cr2](%0), %%"R"ax \n\t"
  3433. "mov %%cr2, %%"R"dx \n\t"
  3434. "cmp %%"R"ax, %%"R"dx \n\t"
  3435. "je 2f \n\t"
  3436. "mov %%"R"ax, %%cr2 \n\t"
  3437. "2: \n\t"
  3438. /* Check if vmlaunch of vmresume is needed */
  3439. "cmpl $0, %c[launched](%0) \n\t"
  3440. /* Load guest registers. Don't clobber flags. */
  3441. "mov %c[rax](%0), %%"R"ax \n\t"
  3442. "mov %c[rbx](%0), %%"R"bx \n\t"
  3443. "mov %c[rdx](%0), %%"R"dx \n\t"
  3444. "mov %c[rsi](%0), %%"R"si \n\t"
  3445. "mov %c[rdi](%0), %%"R"di \n\t"
  3446. "mov %c[rbp](%0), %%"R"bp \n\t"
  3447. #ifdef CONFIG_X86_64
  3448. "mov %c[r8](%0), %%r8 \n\t"
  3449. "mov %c[r9](%0), %%r9 \n\t"
  3450. "mov %c[r10](%0), %%r10 \n\t"
  3451. "mov %c[r11](%0), %%r11 \n\t"
  3452. "mov %c[r12](%0), %%r12 \n\t"
  3453. "mov %c[r13](%0), %%r13 \n\t"
  3454. "mov %c[r14](%0), %%r14 \n\t"
  3455. "mov %c[r15](%0), %%r15 \n\t"
  3456. #endif
  3457. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  3458. /* Enter guest mode */
  3459. "jne .Llaunched \n\t"
  3460. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  3461. "jmp .Lkvm_vmx_return \n\t"
  3462. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  3463. ".Lkvm_vmx_return: "
  3464. /* Save guest registers, load host registers, keep flags */
  3465. "xchg %0, (%%"R"sp) \n\t"
  3466. "mov %%"R"ax, %c[rax](%0) \n\t"
  3467. "mov %%"R"bx, %c[rbx](%0) \n\t"
  3468. "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
  3469. "mov %%"R"dx, %c[rdx](%0) \n\t"
  3470. "mov %%"R"si, %c[rsi](%0) \n\t"
  3471. "mov %%"R"di, %c[rdi](%0) \n\t"
  3472. "mov %%"R"bp, %c[rbp](%0) \n\t"
  3473. #ifdef CONFIG_X86_64
  3474. "mov %%r8, %c[r8](%0) \n\t"
  3475. "mov %%r9, %c[r9](%0) \n\t"
  3476. "mov %%r10, %c[r10](%0) \n\t"
  3477. "mov %%r11, %c[r11](%0) \n\t"
  3478. "mov %%r12, %c[r12](%0) \n\t"
  3479. "mov %%r13, %c[r13](%0) \n\t"
  3480. "mov %%r14, %c[r14](%0) \n\t"
  3481. "mov %%r15, %c[r15](%0) \n\t"
  3482. #endif
  3483. "mov %%cr2, %%"R"ax \n\t"
  3484. "mov %%"R"ax, %c[cr2](%0) \n\t"
  3485. "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
  3486. "setbe %c[fail](%0) \n\t"
  3487. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  3488. [launched]"i"(offsetof(struct vcpu_vmx, launched)),
  3489. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  3490. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  3491. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  3492. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  3493. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  3494. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  3495. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  3496. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  3497. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  3498. #ifdef CONFIG_X86_64
  3499. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  3500. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  3501. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  3502. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  3503. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  3504. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  3505. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  3506. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  3507. #endif
  3508. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
  3509. : "cc", "memory"
  3510. , R"ax", R"bx", R"di", R"si"
  3511. #ifdef CONFIG_X86_64
  3512. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  3513. #endif
  3514. );
  3515. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  3516. | (1 << VCPU_EXREG_PDPTR)
  3517. | (1 << VCPU_EXREG_CR3));
  3518. vcpu->arch.regs_dirty = 0;
  3519. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  3520. asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
  3521. vmx->launched = 1;
  3522. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  3523. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  3524. vmx_complete_atomic_exit(vmx);
  3525. vmx_recover_nmi_blocking(vmx);
  3526. vmx_complete_interrupts(vmx);
  3527. }
  3528. #undef R
  3529. #undef Q
  3530. static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
  3531. {
  3532. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3533. if (vmx->vmcs) {
  3534. vcpu_clear(vmx);
  3535. free_vmcs(vmx->vmcs);
  3536. vmx->vmcs = NULL;
  3537. }
  3538. }
  3539. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  3540. {
  3541. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3542. free_vpid(vmx);
  3543. vmx_free_vmcs(vcpu);
  3544. kfree(vmx->guest_msrs);
  3545. kvm_vcpu_uninit(vcpu);
  3546. kmem_cache_free(kvm_vcpu_cache, vmx);
  3547. }
  3548. static inline void vmcs_init(struct vmcs *vmcs)
  3549. {
  3550. u64 phys_addr = __pa(per_cpu(vmxarea, raw_smp_processor_id()));
  3551. if (!vmm_exclusive)
  3552. kvm_cpu_vmxon(phys_addr);
  3553. vmcs_clear(vmcs);
  3554. if (!vmm_exclusive)
  3555. kvm_cpu_vmxoff();
  3556. }
  3557. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  3558. {
  3559. int err;
  3560. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  3561. int cpu;
  3562. if (!vmx)
  3563. return ERR_PTR(-ENOMEM);
  3564. allocate_vpid(vmx);
  3565. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  3566. if (err)
  3567. goto free_vcpu;
  3568. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3569. if (!vmx->guest_msrs) {
  3570. err = -ENOMEM;
  3571. goto uninit_vcpu;
  3572. }
  3573. vmx->vmcs = alloc_vmcs();
  3574. if (!vmx->vmcs)
  3575. goto free_msrs;
  3576. vmcs_init(vmx->vmcs);
  3577. cpu = get_cpu();
  3578. vmx_vcpu_load(&vmx->vcpu, cpu);
  3579. vmx->vcpu.cpu = cpu;
  3580. err = vmx_vcpu_setup(vmx);
  3581. vmx_vcpu_put(&vmx->vcpu);
  3582. put_cpu();
  3583. if (err)
  3584. goto free_vmcs;
  3585. if (vm_need_virtualize_apic_accesses(kvm))
  3586. if (alloc_apic_access_page(kvm) != 0)
  3587. goto free_vmcs;
  3588. if (enable_ept) {
  3589. if (!kvm->arch.ept_identity_map_addr)
  3590. kvm->arch.ept_identity_map_addr =
  3591. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  3592. if (alloc_identity_pagetable(kvm) != 0)
  3593. goto free_vmcs;
  3594. }
  3595. return &vmx->vcpu;
  3596. free_vmcs:
  3597. free_vmcs(vmx->vmcs);
  3598. free_msrs:
  3599. kfree(vmx->guest_msrs);
  3600. uninit_vcpu:
  3601. kvm_vcpu_uninit(&vmx->vcpu);
  3602. free_vcpu:
  3603. free_vpid(vmx);
  3604. kmem_cache_free(kvm_vcpu_cache, vmx);
  3605. return ERR_PTR(err);
  3606. }
  3607. static void __init vmx_check_processor_compat(void *rtn)
  3608. {
  3609. struct vmcs_config vmcs_conf;
  3610. *(int *)rtn = 0;
  3611. if (setup_vmcs_config(&vmcs_conf) < 0)
  3612. *(int *)rtn = -EIO;
  3613. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  3614. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  3615. smp_processor_id());
  3616. *(int *)rtn = -EIO;
  3617. }
  3618. }
  3619. static int get_ept_level(void)
  3620. {
  3621. return VMX_EPT_DEFAULT_GAW + 1;
  3622. }
  3623. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  3624. {
  3625. u64 ret;
  3626. /* For VT-d and EPT combination
  3627. * 1. MMIO: always map as UC
  3628. * 2. EPT with VT-d:
  3629. * a. VT-d without snooping control feature: can't guarantee the
  3630. * result, try to trust guest.
  3631. * b. VT-d with snooping control feature: snooping control feature of
  3632. * VT-d engine can guarantee the cache correctness. Just set it
  3633. * to WB to keep consistent with host. So the same as item 3.
  3634. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  3635. * consistent with host MTRR
  3636. */
  3637. if (is_mmio)
  3638. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  3639. else if (vcpu->kvm->arch.iommu_domain &&
  3640. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
  3641. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  3642. VMX_EPT_MT_EPTE_SHIFT;
  3643. else
  3644. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  3645. | VMX_EPT_IPAT_BIT;
  3646. return ret;
  3647. }
  3648. #define _ER(x) { EXIT_REASON_##x, #x }
  3649. static const struct trace_print_flags vmx_exit_reasons_str[] = {
  3650. _ER(EXCEPTION_NMI),
  3651. _ER(EXTERNAL_INTERRUPT),
  3652. _ER(TRIPLE_FAULT),
  3653. _ER(PENDING_INTERRUPT),
  3654. _ER(NMI_WINDOW),
  3655. _ER(TASK_SWITCH),
  3656. _ER(CPUID),
  3657. _ER(HLT),
  3658. _ER(INVLPG),
  3659. _ER(RDPMC),
  3660. _ER(RDTSC),
  3661. _ER(VMCALL),
  3662. _ER(VMCLEAR),
  3663. _ER(VMLAUNCH),
  3664. _ER(VMPTRLD),
  3665. _ER(VMPTRST),
  3666. _ER(VMREAD),
  3667. _ER(VMRESUME),
  3668. _ER(VMWRITE),
  3669. _ER(VMOFF),
  3670. _ER(VMON),
  3671. _ER(CR_ACCESS),
  3672. _ER(DR_ACCESS),
  3673. _ER(IO_INSTRUCTION),
  3674. _ER(MSR_READ),
  3675. _ER(MSR_WRITE),
  3676. _ER(MWAIT_INSTRUCTION),
  3677. _ER(MONITOR_INSTRUCTION),
  3678. _ER(PAUSE_INSTRUCTION),
  3679. _ER(MCE_DURING_VMENTRY),
  3680. _ER(TPR_BELOW_THRESHOLD),
  3681. _ER(APIC_ACCESS),
  3682. _ER(EPT_VIOLATION),
  3683. _ER(EPT_MISCONFIG),
  3684. _ER(WBINVD),
  3685. { -1, NULL }
  3686. };
  3687. #undef _ER
  3688. static int vmx_get_lpage_level(void)
  3689. {
  3690. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  3691. return PT_DIRECTORY_LEVEL;
  3692. else
  3693. /* For shadow and EPT supported 1GB page */
  3694. return PT_PDPE_LEVEL;
  3695. }
  3696. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  3697. {
  3698. struct kvm_cpuid_entry2 *best;
  3699. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3700. u32 exec_control;
  3701. vmx->rdtscp_enabled = false;
  3702. if (vmx_rdtscp_supported()) {
  3703. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  3704. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  3705. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  3706. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  3707. vmx->rdtscp_enabled = true;
  3708. else {
  3709. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  3710. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  3711. exec_control);
  3712. }
  3713. }
  3714. }
  3715. }
  3716. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  3717. {
  3718. }
  3719. static struct kvm_x86_ops vmx_x86_ops = {
  3720. .cpu_has_kvm_support = cpu_has_kvm_support,
  3721. .disabled_by_bios = vmx_disabled_by_bios,
  3722. .hardware_setup = hardware_setup,
  3723. .hardware_unsetup = hardware_unsetup,
  3724. .check_processor_compatibility = vmx_check_processor_compat,
  3725. .hardware_enable = hardware_enable,
  3726. .hardware_disable = hardware_disable,
  3727. .cpu_has_accelerated_tpr = report_flexpriority,
  3728. .vcpu_create = vmx_create_vcpu,
  3729. .vcpu_free = vmx_free_vcpu,
  3730. .vcpu_reset = vmx_vcpu_reset,
  3731. .prepare_guest_switch = vmx_save_host_state,
  3732. .vcpu_load = vmx_vcpu_load,
  3733. .vcpu_put = vmx_vcpu_put,
  3734. .set_guest_debug = set_guest_debug,
  3735. .get_msr = vmx_get_msr,
  3736. .set_msr = vmx_set_msr,
  3737. .get_segment_base = vmx_get_segment_base,
  3738. .get_segment = vmx_get_segment,
  3739. .set_segment = vmx_set_segment,
  3740. .get_cpl = vmx_get_cpl,
  3741. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  3742. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  3743. .decache_cr3 = vmx_decache_cr3,
  3744. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  3745. .set_cr0 = vmx_set_cr0,
  3746. .set_cr3 = vmx_set_cr3,
  3747. .set_cr4 = vmx_set_cr4,
  3748. .set_efer = vmx_set_efer,
  3749. .get_idt = vmx_get_idt,
  3750. .set_idt = vmx_set_idt,
  3751. .get_gdt = vmx_get_gdt,
  3752. .set_gdt = vmx_set_gdt,
  3753. .set_dr7 = vmx_set_dr7,
  3754. .cache_reg = vmx_cache_reg,
  3755. .get_rflags = vmx_get_rflags,
  3756. .set_rflags = vmx_set_rflags,
  3757. .fpu_activate = vmx_fpu_activate,
  3758. .fpu_deactivate = vmx_fpu_deactivate,
  3759. .tlb_flush = vmx_flush_tlb,
  3760. .run = vmx_vcpu_run,
  3761. .handle_exit = vmx_handle_exit,
  3762. .skip_emulated_instruction = skip_emulated_instruction,
  3763. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  3764. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  3765. .patch_hypercall = vmx_patch_hypercall,
  3766. .set_irq = vmx_inject_irq,
  3767. .set_nmi = vmx_inject_nmi,
  3768. .queue_exception = vmx_queue_exception,
  3769. .cancel_injection = vmx_cancel_injection,
  3770. .interrupt_allowed = vmx_interrupt_allowed,
  3771. .nmi_allowed = vmx_nmi_allowed,
  3772. .get_nmi_mask = vmx_get_nmi_mask,
  3773. .set_nmi_mask = vmx_set_nmi_mask,
  3774. .enable_nmi_window = enable_nmi_window,
  3775. .enable_irq_window = enable_irq_window,
  3776. .update_cr8_intercept = update_cr8_intercept,
  3777. .set_tss_addr = vmx_set_tss_addr,
  3778. .get_tdp_level = get_ept_level,
  3779. .get_mt_mask = vmx_get_mt_mask,
  3780. .get_exit_info = vmx_get_exit_info,
  3781. .exit_reasons_str = vmx_exit_reasons_str,
  3782. .get_lpage_level = vmx_get_lpage_level,
  3783. .cpuid_update = vmx_cpuid_update,
  3784. .rdtscp_supported = vmx_rdtscp_supported,
  3785. .set_supported_cpuid = vmx_set_supported_cpuid,
  3786. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  3787. .write_tsc_offset = vmx_write_tsc_offset,
  3788. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  3789. .set_tdp_cr3 = vmx_set_cr3,
  3790. };
  3791. static int __init vmx_init(void)
  3792. {
  3793. int r, i;
  3794. rdmsrl_safe(MSR_EFER, &host_efer);
  3795. for (i = 0; i < NR_VMX_MSR; ++i)
  3796. kvm_define_shared_msr(i, vmx_msr_index[i]);
  3797. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  3798. if (!vmx_io_bitmap_a)
  3799. return -ENOMEM;
  3800. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  3801. if (!vmx_io_bitmap_b) {
  3802. r = -ENOMEM;
  3803. goto out;
  3804. }
  3805. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  3806. if (!vmx_msr_bitmap_legacy) {
  3807. r = -ENOMEM;
  3808. goto out1;
  3809. }
  3810. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  3811. if (!vmx_msr_bitmap_longmode) {
  3812. r = -ENOMEM;
  3813. goto out2;
  3814. }
  3815. /*
  3816. * Allow direct access to the PC debug port (it is often used for I/O
  3817. * delays, but the vmexits simply slow things down).
  3818. */
  3819. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  3820. clear_bit(0x80, vmx_io_bitmap_a);
  3821. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  3822. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  3823. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  3824. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  3825. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  3826. __alignof__(struct vcpu_vmx), THIS_MODULE);
  3827. if (r)
  3828. goto out3;
  3829. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  3830. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  3831. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  3832. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  3833. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  3834. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  3835. if (enable_ept) {
  3836. bypass_guest_pf = 0;
  3837. kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
  3838. VMX_EPT_EXECUTABLE_MASK);
  3839. kvm_enable_tdp();
  3840. } else
  3841. kvm_disable_tdp();
  3842. if (bypass_guest_pf)
  3843. kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
  3844. return 0;
  3845. out3:
  3846. free_page((unsigned long)vmx_msr_bitmap_longmode);
  3847. out2:
  3848. free_page((unsigned long)vmx_msr_bitmap_legacy);
  3849. out1:
  3850. free_page((unsigned long)vmx_io_bitmap_b);
  3851. out:
  3852. free_page((unsigned long)vmx_io_bitmap_a);
  3853. return r;
  3854. }
  3855. static void __exit vmx_exit(void)
  3856. {
  3857. free_page((unsigned long)vmx_msr_bitmap_legacy);
  3858. free_page((unsigned long)vmx_msr_bitmap_longmode);
  3859. free_page((unsigned long)vmx_io_bitmap_b);
  3860. free_page((unsigned long)vmx_io_bitmap_a);
  3861. kvm_exit();
  3862. }
  3863. module_init(vmx_init)
  3864. module_exit(vmx_exit)