vmx.c 113 KB

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