vmx.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699
  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. *
  9. * Authors:
  10. * Avi Kivity <avi@qumranet.com>
  11. * Yaniv Kamay <yaniv@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #include "irq.h"
  18. #include "vmx.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 "kvm_cache_regs.h"
  28. #include "x86.h"
  29. #include <asm/io.h>
  30. #include <asm/desc.h>
  31. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  32. MODULE_AUTHOR("Qumranet");
  33. MODULE_LICENSE("GPL");
  34. static int bypass_guest_pf = 1;
  35. module_param(bypass_guest_pf, bool, 0);
  36. static int enable_vpid = 1;
  37. module_param(enable_vpid, bool, 0);
  38. static int flexpriority_enabled = 1;
  39. module_param(flexpriority_enabled, bool, 0);
  40. static int enable_ept = 1;
  41. module_param(enable_ept, bool, 0);
  42. static int emulate_invalid_guest_state = 0;
  43. module_param(emulate_invalid_guest_state, bool, 0);
  44. struct vmcs {
  45. u32 revision_id;
  46. u32 abort;
  47. char data[0];
  48. };
  49. struct vcpu_vmx {
  50. struct kvm_vcpu vcpu;
  51. struct list_head local_vcpus_link;
  52. unsigned long host_rsp;
  53. int launched;
  54. u8 fail;
  55. u32 idt_vectoring_info;
  56. struct kvm_msr_entry *guest_msrs;
  57. struct kvm_msr_entry *host_msrs;
  58. int nmsrs;
  59. int save_nmsrs;
  60. int msr_offset_efer;
  61. #ifdef CONFIG_X86_64
  62. int msr_offset_kernel_gs_base;
  63. #endif
  64. struct vmcs *vmcs;
  65. struct {
  66. int loaded;
  67. u16 fs_sel, gs_sel, ldt_sel;
  68. int gs_ldt_reload_needed;
  69. int fs_reload_needed;
  70. int guest_efer_loaded;
  71. } host_state;
  72. struct {
  73. struct {
  74. bool pending;
  75. u8 vector;
  76. unsigned rip;
  77. } irq;
  78. } rmode;
  79. int vpid;
  80. bool emulation_required;
  81. /* Support for vnmi-less CPUs */
  82. int soft_vnmi_blocked;
  83. ktime_t entry_time;
  84. s64 vnmi_blocked_time;
  85. };
  86. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  87. {
  88. return container_of(vcpu, struct vcpu_vmx, vcpu);
  89. }
  90. static int init_rmode(struct kvm *kvm);
  91. static u64 construct_eptp(unsigned long root_hpa);
  92. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  93. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  94. static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
  95. static struct page *vmx_io_bitmap_a;
  96. static struct page *vmx_io_bitmap_b;
  97. static struct page *vmx_msr_bitmap;
  98. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  99. static DEFINE_SPINLOCK(vmx_vpid_lock);
  100. static struct vmcs_config {
  101. int size;
  102. int order;
  103. u32 revision_id;
  104. u32 pin_based_exec_ctrl;
  105. u32 cpu_based_exec_ctrl;
  106. u32 cpu_based_2nd_exec_ctrl;
  107. u32 vmexit_ctrl;
  108. u32 vmentry_ctrl;
  109. } vmcs_config;
  110. struct vmx_capability {
  111. u32 ept;
  112. u32 vpid;
  113. } vmx_capability;
  114. #define VMX_SEGMENT_FIELD(seg) \
  115. [VCPU_SREG_##seg] = { \
  116. .selector = GUEST_##seg##_SELECTOR, \
  117. .base = GUEST_##seg##_BASE, \
  118. .limit = GUEST_##seg##_LIMIT, \
  119. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  120. }
  121. static struct kvm_vmx_segment_field {
  122. unsigned selector;
  123. unsigned base;
  124. unsigned limit;
  125. unsigned ar_bytes;
  126. } kvm_vmx_segment_fields[] = {
  127. VMX_SEGMENT_FIELD(CS),
  128. VMX_SEGMENT_FIELD(DS),
  129. VMX_SEGMENT_FIELD(ES),
  130. VMX_SEGMENT_FIELD(FS),
  131. VMX_SEGMENT_FIELD(GS),
  132. VMX_SEGMENT_FIELD(SS),
  133. VMX_SEGMENT_FIELD(TR),
  134. VMX_SEGMENT_FIELD(LDTR),
  135. };
  136. /*
  137. * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
  138. * away by decrementing the array size.
  139. */
  140. static const u32 vmx_msr_index[] = {
  141. #ifdef CONFIG_X86_64
  142. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
  143. #endif
  144. MSR_EFER, MSR_K6_STAR,
  145. };
  146. #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
  147. static void load_msrs(struct kvm_msr_entry *e, int n)
  148. {
  149. int i;
  150. for (i = 0; i < n; ++i)
  151. wrmsrl(e[i].index, e[i].data);
  152. }
  153. static void save_msrs(struct kvm_msr_entry *e, int n)
  154. {
  155. int i;
  156. for (i = 0; i < n; ++i)
  157. rdmsrl(e[i].index, e[i].data);
  158. }
  159. static inline int is_page_fault(u32 intr_info)
  160. {
  161. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  162. INTR_INFO_VALID_MASK)) ==
  163. (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  164. }
  165. static inline int is_no_device(u32 intr_info)
  166. {
  167. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  168. INTR_INFO_VALID_MASK)) ==
  169. (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  170. }
  171. static inline int is_invalid_opcode(u32 intr_info)
  172. {
  173. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  174. INTR_INFO_VALID_MASK)) ==
  175. (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  176. }
  177. static inline int is_external_interrupt(u32 intr_info)
  178. {
  179. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  180. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  181. }
  182. static inline int cpu_has_vmx_msr_bitmap(void)
  183. {
  184. return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
  185. }
  186. static inline int cpu_has_vmx_tpr_shadow(void)
  187. {
  188. return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
  189. }
  190. static inline int vm_need_tpr_shadow(struct kvm *kvm)
  191. {
  192. return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
  193. }
  194. static inline int cpu_has_secondary_exec_ctrls(void)
  195. {
  196. return (vmcs_config.cpu_based_exec_ctrl &
  197. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
  198. }
  199. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  200. {
  201. return flexpriority_enabled
  202. && (vmcs_config.cpu_based_2nd_exec_ctrl &
  203. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
  204. }
  205. static inline int cpu_has_vmx_invept_individual_addr(void)
  206. {
  207. return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
  208. }
  209. static inline int cpu_has_vmx_invept_context(void)
  210. {
  211. return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
  212. }
  213. static inline int cpu_has_vmx_invept_global(void)
  214. {
  215. return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
  216. }
  217. static inline int cpu_has_vmx_ept(void)
  218. {
  219. return (vmcs_config.cpu_based_2nd_exec_ctrl &
  220. SECONDARY_EXEC_ENABLE_EPT);
  221. }
  222. static inline int vm_need_ept(void)
  223. {
  224. return (cpu_has_vmx_ept() && enable_ept);
  225. }
  226. static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
  227. {
  228. return ((cpu_has_vmx_virtualize_apic_accesses()) &&
  229. (irqchip_in_kernel(kvm)));
  230. }
  231. static inline int cpu_has_vmx_vpid(void)
  232. {
  233. return (vmcs_config.cpu_based_2nd_exec_ctrl &
  234. SECONDARY_EXEC_ENABLE_VPID);
  235. }
  236. static inline int cpu_has_virtual_nmis(void)
  237. {
  238. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  239. }
  240. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  241. {
  242. int i;
  243. for (i = 0; i < vmx->nmsrs; ++i)
  244. if (vmx->guest_msrs[i].index == msr)
  245. return i;
  246. return -1;
  247. }
  248. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  249. {
  250. struct {
  251. u64 vpid : 16;
  252. u64 rsvd : 48;
  253. u64 gva;
  254. } operand = { vpid, 0, gva };
  255. asm volatile (__ex(ASM_VMX_INVVPID)
  256. /* CF==1 or ZF==1 --> rc = -1 */
  257. "; ja 1f ; ud2 ; 1:"
  258. : : "a"(&operand), "c"(ext) : "cc", "memory");
  259. }
  260. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  261. {
  262. struct {
  263. u64 eptp, gpa;
  264. } operand = {eptp, gpa};
  265. asm volatile (__ex(ASM_VMX_INVEPT)
  266. /* CF==1 or ZF==1 --> rc = -1 */
  267. "; ja 1f ; ud2 ; 1:\n"
  268. : : "a" (&operand), "c" (ext) : "cc", "memory");
  269. }
  270. static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  271. {
  272. int i;
  273. i = __find_msr_index(vmx, msr);
  274. if (i >= 0)
  275. return &vmx->guest_msrs[i];
  276. return NULL;
  277. }
  278. static void vmcs_clear(struct vmcs *vmcs)
  279. {
  280. u64 phys_addr = __pa(vmcs);
  281. u8 error;
  282. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  283. : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
  284. : "cc", "memory");
  285. if (error)
  286. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  287. vmcs, phys_addr);
  288. }
  289. static void __vcpu_clear(void *arg)
  290. {
  291. struct vcpu_vmx *vmx = arg;
  292. int cpu = raw_smp_processor_id();
  293. if (vmx->vcpu.cpu == cpu)
  294. vmcs_clear(vmx->vmcs);
  295. if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
  296. per_cpu(current_vmcs, cpu) = NULL;
  297. rdtscll(vmx->vcpu.arch.host_tsc);
  298. list_del(&vmx->local_vcpus_link);
  299. vmx->vcpu.cpu = -1;
  300. vmx->launched = 0;
  301. }
  302. static void vcpu_clear(struct vcpu_vmx *vmx)
  303. {
  304. if (vmx->vcpu.cpu == -1)
  305. return;
  306. smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
  307. }
  308. static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
  309. {
  310. if (vmx->vpid == 0)
  311. return;
  312. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  313. }
  314. static inline void ept_sync_global(void)
  315. {
  316. if (cpu_has_vmx_invept_global())
  317. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  318. }
  319. static inline void ept_sync_context(u64 eptp)
  320. {
  321. if (vm_need_ept()) {
  322. if (cpu_has_vmx_invept_context())
  323. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  324. else
  325. ept_sync_global();
  326. }
  327. }
  328. static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
  329. {
  330. if (vm_need_ept()) {
  331. if (cpu_has_vmx_invept_individual_addr())
  332. __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
  333. eptp, gpa);
  334. else
  335. ept_sync_context(eptp);
  336. }
  337. }
  338. static unsigned long vmcs_readl(unsigned long field)
  339. {
  340. unsigned long value;
  341. asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
  342. : "=a"(value) : "d"(field) : "cc");
  343. return value;
  344. }
  345. static u16 vmcs_read16(unsigned long field)
  346. {
  347. return vmcs_readl(field);
  348. }
  349. static u32 vmcs_read32(unsigned long field)
  350. {
  351. return vmcs_readl(field);
  352. }
  353. static u64 vmcs_read64(unsigned long field)
  354. {
  355. #ifdef CONFIG_X86_64
  356. return vmcs_readl(field);
  357. #else
  358. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  359. #endif
  360. }
  361. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  362. {
  363. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  364. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  365. dump_stack();
  366. }
  367. static void vmcs_writel(unsigned long field, unsigned long value)
  368. {
  369. u8 error;
  370. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  371. : "=q"(error) : "a"(value), "d"(field) : "cc");
  372. if (unlikely(error))
  373. vmwrite_error(field, value);
  374. }
  375. static void vmcs_write16(unsigned long field, u16 value)
  376. {
  377. vmcs_writel(field, value);
  378. }
  379. static void vmcs_write32(unsigned long field, u32 value)
  380. {
  381. vmcs_writel(field, value);
  382. }
  383. static void vmcs_write64(unsigned long field, u64 value)
  384. {
  385. vmcs_writel(field, value);
  386. #ifndef CONFIG_X86_64
  387. asm volatile ("");
  388. vmcs_writel(field+1, value >> 32);
  389. #endif
  390. }
  391. static void vmcs_clear_bits(unsigned long field, u32 mask)
  392. {
  393. vmcs_writel(field, vmcs_readl(field) & ~mask);
  394. }
  395. static void vmcs_set_bits(unsigned long field, u32 mask)
  396. {
  397. vmcs_writel(field, vmcs_readl(field) | mask);
  398. }
  399. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  400. {
  401. u32 eb;
  402. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
  403. if (!vcpu->fpu_active)
  404. eb |= 1u << NM_VECTOR;
  405. if (vcpu->guest_debug.enabled)
  406. eb |= 1u << DB_VECTOR;
  407. if (vcpu->arch.rmode.active)
  408. eb = ~0;
  409. if (vm_need_ept())
  410. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  411. vmcs_write32(EXCEPTION_BITMAP, eb);
  412. }
  413. static void reload_tss(void)
  414. {
  415. /*
  416. * VT restores TR but not its size. Useless.
  417. */
  418. struct descriptor_table gdt;
  419. struct desc_struct *descs;
  420. kvm_get_gdt(&gdt);
  421. descs = (void *)gdt.base;
  422. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  423. load_TR_desc();
  424. }
  425. static void load_transition_efer(struct vcpu_vmx *vmx)
  426. {
  427. int efer_offset = vmx->msr_offset_efer;
  428. u64 host_efer = vmx->host_msrs[efer_offset].data;
  429. u64 guest_efer = vmx->guest_msrs[efer_offset].data;
  430. u64 ignore_bits;
  431. if (efer_offset < 0)
  432. return;
  433. /*
  434. * NX is emulated; LMA and LME handled by hardware; SCE meaninless
  435. * outside long mode
  436. */
  437. ignore_bits = EFER_NX | EFER_SCE;
  438. #ifdef CONFIG_X86_64
  439. ignore_bits |= EFER_LMA | EFER_LME;
  440. /* SCE is meaningful only in long mode on Intel */
  441. if (guest_efer & EFER_LMA)
  442. ignore_bits &= ~(u64)EFER_SCE;
  443. #endif
  444. if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
  445. return;
  446. vmx->host_state.guest_efer_loaded = 1;
  447. guest_efer &= ~ignore_bits;
  448. guest_efer |= host_efer & ignore_bits;
  449. wrmsrl(MSR_EFER, guest_efer);
  450. vmx->vcpu.stat.efer_reload++;
  451. }
  452. static void reload_host_efer(struct vcpu_vmx *vmx)
  453. {
  454. if (vmx->host_state.guest_efer_loaded) {
  455. vmx->host_state.guest_efer_loaded = 0;
  456. load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
  457. }
  458. }
  459. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  460. {
  461. struct vcpu_vmx *vmx = to_vmx(vcpu);
  462. if (vmx->host_state.loaded)
  463. return;
  464. vmx->host_state.loaded = 1;
  465. /*
  466. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  467. * allow segment selectors with cpl > 0 or ti == 1.
  468. */
  469. vmx->host_state.ldt_sel = kvm_read_ldt();
  470. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  471. vmx->host_state.fs_sel = kvm_read_fs();
  472. if (!(vmx->host_state.fs_sel & 7)) {
  473. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  474. vmx->host_state.fs_reload_needed = 0;
  475. } else {
  476. vmcs_write16(HOST_FS_SELECTOR, 0);
  477. vmx->host_state.fs_reload_needed = 1;
  478. }
  479. vmx->host_state.gs_sel = kvm_read_gs();
  480. if (!(vmx->host_state.gs_sel & 7))
  481. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  482. else {
  483. vmcs_write16(HOST_GS_SELECTOR, 0);
  484. vmx->host_state.gs_ldt_reload_needed = 1;
  485. }
  486. #ifdef CONFIG_X86_64
  487. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  488. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  489. #else
  490. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  491. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  492. #endif
  493. #ifdef CONFIG_X86_64
  494. if (is_long_mode(&vmx->vcpu))
  495. save_msrs(vmx->host_msrs +
  496. vmx->msr_offset_kernel_gs_base, 1);
  497. #endif
  498. load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
  499. load_transition_efer(vmx);
  500. }
  501. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  502. {
  503. unsigned long flags;
  504. if (!vmx->host_state.loaded)
  505. return;
  506. ++vmx->vcpu.stat.host_state_reload;
  507. vmx->host_state.loaded = 0;
  508. if (vmx->host_state.fs_reload_needed)
  509. kvm_load_fs(vmx->host_state.fs_sel);
  510. if (vmx->host_state.gs_ldt_reload_needed) {
  511. kvm_load_ldt(vmx->host_state.ldt_sel);
  512. /*
  513. * If we have to reload gs, we must take care to
  514. * preserve our gs base.
  515. */
  516. local_irq_save(flags);
  517. kvm_load_gs(vmx->host_state.gs_sel);
  518. #ifdef CONFIG_X86_64
  519. wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
  520. #endif
  521. local_irq_restore(flags);
  522. }
  523. reload_tss();
  524. save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
  525. load_msrs(vmx->host_msrs, vmx->save_nmsrs);
  526. reload_host_efer(vmx);
  527. }
  528. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  529. {
  530. preempt_disable();
  531. __vmx_load_host_state(vmx);
  532. preempt_enable();
  533. }
  534. /*
  535. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  536. * vcpu mutex is already taken.
  537. */
  538. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  539. {
  540. struct vcpu_vmx *vmx = to_vmx(vcpu);
  541. u64 phys_addr = __pa(vmx->vmcs);
  542. u64 tsc_this, delta, new_offset;
  543. if (vcpu->cpu != cpu) {
  544. vcpu_clear(vmx);
  545. kvm_migrate_timers(vcpu);
  546. vpid_sync_vcpu_all(vmx);
  547. local_irq_disable();
  548. list_add(&vmx->local_vcpus_link,
  549. &per_cpu(vcpus_on_cpu, cpu));
  550. local_irq_enable();
  551. }
  552. if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
  553. u8 error;
  554. per_cpu(current_vmcs, cpu) = vmx->vmcs;
  555. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  556. : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
  557. : "cc");
  558. if (error)
  559. printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
  560. vmx->vmcs, phys_addr);
  561. }
  562. if (vcpu->cpu != cpu) {
  563. struct descriptor_table dt;
  564. unsigned long sysenter_esp;
  565. vcpu->cpu = cpu;
  566. /*
  567. * Linux uses per-cpu TSS and GDT, so set these when switching
  568. * processors.
  569. */
  570. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  571. kvm_get_gdt(&dt);
  572. vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
  573. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  574. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  575. /*
  576. * Make sure the time stamp counter is monotonous.
  577. */
  578. rdtscll(tsc_this);
  579. if (tsc_this < vcpu->arch.host_tsc) {
  580. delta = vcpu->arch.host_tsc - tsc_this;
  581. new_offset = vmcs_read64(TSC_OFFSET) + delta;
  582. vmcs_write64(TSC_OFFSET, new_offset);
  583. }
  584. }
  585. }
  586. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  587. {
  588. __vmx_load_host_state(to_vmx(vcpu));
  589. }
  590. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  591. {
  592. if (vcpu->fpu_active)
  593. return;
  594. vcpu->fpu_active = 1;
  595. vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
  596. if (vcpu->arch.cr0 & X86_CR0_TS)
  597. vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
  598. update_exception_bitmap(vcpu);
  599. }
  600. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  601. {
  602. if (!vcpu->fpu_active)
  603. return;
  604. vcpu->fpu_active = 0;
  605. vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
  606. update_exception_bitmap(vcpu);
  607. }
  608. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  609. {
  610. return vmcs_readl(GUEST_RFLAGS);
  611. }
  612. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  613. {
  614. if (vcpu->arch.rmode.active)
  615. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  616. vmcs_writel(GUEST_RFLAGS, rflags);
  617. }
  618. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  619. {
  620. unsigned long rip;
  621. u32 interruptibility;
  622. rip = kvm_rip_read(vcpu);
  623. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  624. kvm_rip_write(vcpu, rip);
  625. /*
  626. * We emulated an instruction, so temporary interrupt blocking
  627. * should be removed, if set.
  628. */
  629. interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  630. if (interruptibility & 3)
  631. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
  632. interruptibility & ~3);
  633. vcpu->arch.interrupt_window_open = 1;
  634. }
  635. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  636. bool has_error_code, u32 error_code)
  637. {
  638. struct vcpu_vmx *vmx = to_vmx(vcpu);
  639. if (has_error_code)
  640. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  641. if (vcpu->arch.rmode.active) {
  642. vmx->rmode.irq.pending = true;
  643. vmx->rmode.irq.vector = nr;
  644. vmx->rmode.irq.rip = kvm_rip_read(vcpu);
  645. if (nr == BP_VECTOR)
  646. vmx->rmode.irq.rip++;
  647. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  648. nr | INTR_TYPE_SOFT_INTR
  649. | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
  650. | INTR_INFO_VALID_MASK);
  651. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
  652. kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
  653. return;
  654. }
  655. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  656. nr | INTR_TYPE_EXCEPTION
  657. | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
  658. | INTR_INFO_VALID_MASK);
  659. }
  660. static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
  661. {
  662. return false;
  663. }
  664. /*
  665. * Swap MSR entry in host/guest MSR entry array.
  666. */
  667. #ifdef CONFIG_X86_64
  668. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  669. {
  670. struct kvm_msr_entry tmp;
  671. tmp = vmx->guest_msrs[to];
  672. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  673. vmx->guest_msrs[from] = tmp;
  674. tmp = vmx->host_msrs[to];
  675. vmx->host_msrs[to] = vmx->host_msrs[from];
  676. vmx->host_msrs[from] = tmp;
  677. }
  678. #endif
  679. /*
  680. * Set up the vmcs to automatically save and restore system
  681. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  682. * mode, as fiddling with msrs is very expensive.
  683. */
  684. static void setup_msrs(struct vcpu_vmx *vmx)
  685. {
  686. int save_nmsrs;
  687. vmx_load_host_state(vmx);
  688. save_nmsrs = 0;
  689. #ifdef CONFIG_X86_64
  690. if (is_long_mode(&vmx->vcpu)) {
  691. int index;
  692. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  693. if (index >= 0)
  694. move_msr_up(vmx, index, save_nmsrs++);
  695. index = __find_msr_index(vmx, MSR_LSTAR);
  696. if (index >= 0)
  697. move_msr_up(vmx, index, save_nmsrs++);
  698. index = __find_msr_index(vmx, MSR_CSTAR);
  699. if (index >= 0)
  700. move_msr_up(vmx, index, save_nmsrs++);
  701. index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
  702. if (index >= 0)
  703. move_msr_up(vmx, index, save_nmsrs++);
  704. /*
  705. * MSR_K6_STAR is only needed on long mode guests, and only
  706. * if efer.sce is enabled.
  707. */
  708. index = __find_msr_index(vmx, MSR_K6_STAR);
  709. if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
  710. move_msr_up(vmx, index, save_nmsrs++);
  711. }
  712. #endif
  713. vmx->save_nmsrs = save_nmsrs;
  714. #ifdef CONFIG_X86_64
  715. vmx->msr_offset_kernel_gs_base =
  716. __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
  717. #endif
  718. vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
  719. }
  720. /*
  721. * reads and returns guest's timestamp counter "register"
  722. * guest_tsc = host_tsc + tsc_offset -- 21.3
  723. */
  724. static u64 guest_read_tsc(void)
  725. {
  726. u64 host_tsc, tsc_offset;
  727. rdtscll(host_tsc);
  728. tsc_offset = vmcs_read64(TSC_OFFSET);
  729. return host_tsc + tsc_offset;
  730. }
  731. /*
  732. * writes 'guest_tsc' into guest's timestamp counter "register"
  733. * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
  734. */
  735. static void guest_write_tsc(u64 guest_tsc)
  736. {
  737. u64 host_tsc;
  738. rdtscll(host_tsc);
  739. vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
  740. }
  741. /*
  742. * Reads an msr value (of 'msr_index') into 'pdata'.
  743. * Returns 0 on success, non-0 otherwise.
  744. * Assumes vcpu_load() was already called.
  745. */
  746. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  747. {
  748. u64 data;
  749. struct kvm_msr_entry *msr;
  750. if (!pdata) {
  751. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  752. return -EINVAL;
  753. }
  754. switch (msr_index) {
  755. #ifdef CONFIG_X86_64
  756. case MSR_FS_BASE:
  757. data = vmcs_readl(GUEST_FS_BASE);
  758. break;
  759. case MSR_GS_BASE:
  760. data = vmcs_readl(GUEST_GS_BASE);
  761. break;
  762. case MSR_EFER:
  763. return kvm_get_msr_common(vcpu, msr_index, pdata);
  764. #endif
  765. case MSR_IA32_TIME_STAMP_COUNTER:
  766. data = guest_read_tsc();
  767. break;
  768. case MSR_IA32_SYSENTER_CS:
  769. data = vmcs_read32(GUEST_SYSENTER_CS);
  770. break;
  771. case MSR_IA32_SYSENTER_EIP:
  772. data = vmcs_readl(GUEST_SYSENTER_EIP);
  773. break;
  774. case MSR_IA32_SYSENTER_ESP:
  775. data = vmcs_readl(GUEST_SYSENTER_ESP);
  776. break;
  777. default:
  778. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  779. if (msr) {
  780. data = msr->data;
  781. break;
  782. }
  783. return kvm_get_msr_common(vcpu, msr_index, pdata);
  784. }
  785. *pdata = data;
  786. return 0;
  787. }
  788. /*
  789. * Writes msr value into into the appropriate "register".
  790. * Returns 0 on success, non-0 otherwise.
  791. * Assumes vcpu_load() was already called.
  792. */
  793. static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  794. {
  795. struct vcpu_vmx *vmx = to_vmx(vcpu);
  796. struct kvm_msr_entry *msr;
  797. int ret = 0;
  798. switch (msr_index) {
  799. #ifdef CONFIG_X86_64
  800. case MSR_EFER:
  801. vmx_load_host_state(vmx);
  802. ret = kvm_set_msr_common(vcpu, msr_index, data);
  803. break;
  804. case MSR_FS_BASE:
  805. vmcs_writel(GUEST_FS_BASE, data);
  806. break;
  807. case MSR_GS_BASE:
  808. vmcs_writel(GUEST_GS_BASE, data);
  809. break;
  810. #endif
  811. case MSR_IA32_SYSENTER_CS:
  812. vmcs_write32(GUEST_SYSENTER_CS, data);
  813. break;
  814. case MSR_IA32_SYSENTER_EIP:
  815. vmcs_writel(GUEST_SYSENTER_EIP, data);
  816. break;
  817. case MSR_IA32_SYSENTER_ESP:
  818. vmcs_writel(GUEST_SYSENTER_ESP, data);
  819. break;
  820. case MSR_IA32_TIME_STAMP_COUNTER:
  821. guest_write_tsc(data);
  822. break;
  823. case MSR_P6_PERFCTR0:
  824. case MSR_P6_PERFCTR1:
  825. case MSR_P6_EVNTSEL0:
  826. case MSR_P6_EVNTSEL1:
  827. /*
  828. * Just discard all writes to the performance counters; this
  829. * should keep both older linux and windows 64-bit guests
  830. * happy
  831. */
  832. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", msr_index, data);
  833. break;
  834. default:
  835. vmx_load_host_state(vmx);
  836. msr = find_msr_entry(vmx, msr_index);
  837. if (msr) {
  838. msr->data = data;
  839. break;
  840. }
  841. ret = kvm_set_msr_common(vcpu, msr_index, data);
  842. }
  843. return ret;
  844. }
  845. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  846. {
  847. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  848. switch (reg) {
  849. case VCPU_REGS_RSP:
  850. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  851. break;
  852. case VCPU_REGS_RIP:
  853. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  854. break;
  855. default:
  856. break;
  857. }
  858. }
  859. static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  860. {
  861. unsigned long dr7 = 0x400;
  862. int old_singlestep;
  863. old_singlestep = vcpu->guest_debug.singlestep;
  864. vcpu->guest_debug.enabled = dbg->enabled;
  865. if (vcpu->guest_debug.enabled) {
  866. int i;
  867. dr7 |= 0x200; /* exact */
  868. for (i = 0; i < 4; ++i) {
  869. if (!dbg->breakpoints[i].enabled)
  870. continue;
  871. vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
  872. dr7 |= 2 << (i*2); /* global enable */
  873. dr7 |= 0 << (i*4+16); /* execution breakpoint */
  874. }
  875. vcpu->guest_debug.singlestep = dbg->singlestep;
  876. } else
  877. vcpu->guest_debug.singlestep = 0;
  878. if (old_singlestep && !vcpu->guest_debug.singlestep) {
  879. unsigned long flags;
  880. flags = vmcs_readl(GUEST_RFLAGS);
  881. flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  882. vmcs_writel(GUEST_RFLAGS, flags);
  883. }
  884. update_exception_bitmap(vcpu);
  885. vmcs_writel(GUEST_DR7, dr7);
  886. return 0;
  887. }
  888. static int vmx_get_irq(struct kvm_vcpu *vcpu)
  889. {
  890. if (!vcpu->arch.interrupt.pending)
  891. return -1;
  892. return vcpu->arch.interrupt.nr;
  893. }
  894. static __init int cpu_has_kvm_support(void)
  895. {
  896. unsigned long ecx = cpuid_ecx(1);
  897. return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
  898. }
  899. static __init int vmx_disabled_by_bios(void)
  900. {
  901. u64 msr;
  902. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  903. return (msr & (FEATURE_CONTROL_LOCKED |
  904. FEATURE_CONTROL_VMXON_ENABLED))
  905. == FEATURE_CONTROL_LOCKED;
  906. /* locked but not enabled */
  907. }
  908. static void hardware_enable(void *garbage)
  909. {
  910. int cpu = raw_smp_processor_id();
  911. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  912. u64 old;
  913. INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
  914. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  915. if ((old & (FEATURE_CONTROL_LOCKED |
  916. FEATURE_CONTROL_VMXON_ENABLED))
  917. != (FEATURE_CONTROL_LOCKED |
  918. FEATURE_CONTROL_VMXON_ENABLED))
  919. /* enable and lock */
  920. wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
  921. FEATURE_CONTROL_LOCKED |
  922. FEATURE_CONTROL_VMXON_ENABLED);
  923. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  924. asm volatile (ASM_VMX_VMXON_RAX
  925. : : "a"(&phys_addr), "m"(phys_addr)
  926. : "memory", "cc");
  927. }
  928. static void vmclear_local_vcpus(void)
  929. {
  930. int cpu = raw_smp_processor_id();
  931. struct vcpu_vmx *vmx, *n;
  932. list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
  933. local_vcpus_link)
  934. __vcpu_clear(vmx);
  935. }
  936. static void hardware_disable(void *garbage)
  937. {
  938. vmclear_local_vcpus();
  939. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  940. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  941. }
  942. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  943. u32 msr, u32 *result)
  944. {
  945. u32 vmx_msr_low, vmx_msr_high;
  946. u32 ctl = ctl_min | ctl_opt;
  947. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  948. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  949. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  950. /* Ensure minimum (required) set of control bits are supported. */
  951. if (ctl_min & ~ctl)
  952. return -EIO;
  953. *result = ctl;
  954. return 0;
  955. }
  956. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  957. {
  958. u32 vmx_msr_low, vmx_msr_high;
  959. u32 min, opt, min2, opt2;
  960. u32 _pin_based_exec_control = 0;
  961. u32 _cpu_based_exec_control = 0;
  962. u32 _cpu_based_2nd_exec_control = 0;
  963. u32 _vmexit_control = 0;
  964. u32 _vmentry_control = 0;
  965. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  966. opt = PIN_BASED_VIRTUAL_NMIS;
  967. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  968. &_pin_based_exec_control) < 0)
  969. return -EIO;
  970. min = CPU_BASED_HLT_EXITING |
  971. #ifdef CONFIG_X86_64
  972. CPU_BASED_CR8_LOAD_EXITING |
  973. CPU_BASED_CR8_STORE_EXITING |
  974. #endif
  975. CPU_BASED_CR3_LOAD_EXITING |
  976. CPU_BASED_CR3_STORE_EXITING |
  977. CPU_BASED_USE_IO_BITMAPS |
  978. CPU_BASED_MOV_DR_EXITING |
  979. CPU_BASED_USE_TSC_OFFSETING |
  980. CPU_BASED_INVLPG_EXITING;
  981. opt = CPU_BASED_TPR_SHADOW |
  982. CPU_BASED_USE_MSR_BITMAPS |
  983. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  984. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  985. &_cpu_based_exec_control) < 0)
  986. return -EIO;
  987. #ifdef CONFIG_X86_64
  988. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  989. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  990. ~CPU_BASED_CR8_STORE_EXITING;
  991. #endif
  992. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  993. min2 = 0;
  994. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  995. SECONDARY_EXEC_WBINVD_EXITING |
  996. SECONDARY_EXEC_ENABLE_VPID |
  997. SECONDARY_EXEC_ENABLE_EPT;
  998. if (adjust_vmx_controls(min2, opt2,
  999. MSR_IA32_VMX_PROCBASED_CTLS2,
  1000. &_cpu_based_2nd_exec_control) < 0)
  1001. return -EIO;
  1002. }
  1003. #ifndef CONFIG_X86_64
  1004. if (!(_cpu_based_2nd_exec_control &
  1005. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  1006. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  1007. #endif
  1008. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  1009. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  1010. enabled */
  1011. min &= ~(CPU_BASED_CR3_LOAD_EXITING |
  1012. CPU_BASED_CR3_STORE_EXITING |
  1013. CPU_BASED_INVLPG_EXITING);
  1014. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  1015. &_cpu_based_exec_control) < 0)
  1016. return -EIO;
  1017. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  1018. vmx_capability.ept, vmx_capability.vpid);
  1019. }
  1020. min = 0;
  1021. #ifdef CONFIG_X86_64
  1022. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1023. #endif
  1024. opt = 0;
  1025. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  1026. &_vmexit_control) < 0)
  1027. return -EIO;
  1028. min = opt = 0;
  1029. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  1030. &_vmentry_control) < 0)
  1031. return -EIO;
  1032. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  1033. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  1034. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  1035. return -EIO;
  1036. #ifdef CONFIG_X86_64
  1037. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  1038. if (vmx_msr_high & (1u<<16))
  1039. return -EIO;
  1040. #endif
  1041. /* Require Write-Back (WB) memory type for VMCS accesses. */
  1042. if (((vmx_msr_high >> 18) & 15) != 6)
  1043. return -EIO;
  1044. vmcs_conf->size = vmx_msr_high & 0x1fff;
  1045. vmcs_conf->order = get_order(vmcs_config.size);
  1046. vmcs_conf->revision_id = vmx_msr_low;
  1047. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  1048. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  1049. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  1050. vmcs_conf->vmexit_ctrl = _vmexit_control;
  1051. vmcs_conf->vmentry_ctrl = _vmentry_control;
  1052. return 0;
  1053. }
  1054. static struct vmcs *alloc_vmcs_cpu(int cpu)
  1055. {
  1056. int node = cpu_to_node(cpu);
  1057. struct page *pages;
  1058. struct vmcs *vmcs;
  1059. pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  1060. if (!pages)
  1061. return NULL;
  1062. vmcs = page_address(pages);
  1063. memset(vmcs, 0, vmcs_config.size);
  1064. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  1065. return vmcs;
  1066. }
  1067. static struct vmcs *alloc_vmcs(void)
  1068. {
  1069. return alloc_vmcs_cpu(raw_smp_processor_id());
  1070. }
  1071. static void free_vmcs(struct vmcs *vmcs)
  1072. {
  1073. free_pages((unsigned long)vmcs, vmcs_config.order);
  1074. }
  1075. static void free_kvm_area(void)
  1076. {
  1077. int cpu;
  1078. for_each_online_cpu(cpu)
  1079. free_vmcs(per_cpu(vmxarea, cpu));
  1080. }
  1081. static __init int alloc_kvm_area(void)
  1082. {
  1083. int cpu;
  1084. for_each_online_cpu(cpu) {
  1085. struct vmcs *vmcs;
  1086. vmcs = alloc_vmcs_cpu(cpu);
  1087. if (!vmcs) {
  1088. free_kvm_area();
  1089. return -ENOMEM;
  1090. }
  1091. per_cpu(vmxarea, cpu) = vmcs;
  1092. }
  1093. return 0;
  1094. }
  1095. static __init int hardware_setup(void)
  1096. {
  1097. if (setup_vmcs_config(&vmcs_config) < 0)
  1098. return -EIO;
  1099. if (boot_cpu_has(X86_FEATURE_NX))
  1100. kvm_enable_efer_bits(EFER_NX);
  1101. return alloc_kvm_area();
  1102. }
  1103. static __exit void hardware_unsetup(void)
  1104. {
  1105. free_kvm_area();
  1106. }
  1107. static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
  1108. {
  1109. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1110. if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
  1111. vmcs_write16(sf->selector, save->selector);
  1112. vmcs_writel(sf->base, save->base);
  1113. vmcs_write32(sf->limit, save->limit);
  1114. vmcs_write32(sf->ar_bytes, save->ar);
  1115. } else {
  1116. u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
  1117. << AR_DPL_SHIFT;
  1118. vmcs_write32(sf->ar_bytes, 0x93 | dpl);
  1119. }
  1120. }
  1121. static void enter_pmode(struct kvm_vcpu *vcpu)
  1122. {
  1123. unsigned long flags;
  1124. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1125. vmx->emulation_required = 1;
  1126. vcpu->arch.rmode.active = 0;
  1127. vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
  1128. vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
  1129. vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
  1130. flags = vmcs_readl(GUEST_RFLAGS);
  1131. flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
  1132. flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
  1133. vmcs_writel(GUEST_RFLAGS, flags);
  1134. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  1135. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  1136. update_exception_bitmap(vcpu);
  1137. if (emulate_invalid_guest_state)
  1138. return;
  1139. fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
  1140. fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
  1141. fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
  1142. fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
  1143. vmcs_write16(GUEST_SS_SELECTOR, 0);
  1144. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  1145. vmcs_write16(GUEST_CS_SELECTOR,
  1146. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  1147. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  1148. }
  1149. static gva_t rmode_tss_base(struct kvm *kvm)
  1150. {
  1151. if (!kvm->arch.tss_addr) {
  1152. gfn_t base_gfn = kvm->memslots[0].base_gfn +
  1153. kvm->memslots[0].npages - 3;
  1154. return base_gfn << PAGE_SHIFT;
  1155. }
  1156. return kvm->arch.tss_addr;
  1157. }
  1158. static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
  1159. {
  1160. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1161. save->selector = vmcs_read16(sf->selector);
  1162. save->base = vmcs_readl(sf->base);
  1163. save->limit = vmcs_read32(sf->limit);
  1164. save->ar = vmcs_read32(sf->ar_bytes);
  1165. vmcs_write16(sf->selector, save->base >> 4);
  1166. vmcs_write32(sf->base, save->base & 0xfffff);
  1167. vmcs_write32(sf->limit, 0xffff);
  1168. vmcs_write32(sf->ar_bytes, 0xf3);
  1169. }
  1170. static void enter_rmode(struct kvm_vcpu *vcpu)
  1171. {
  1172. unsigned long flags;
  1173. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1174. vmx->emulation_required = 1;
  1175. vcpu->arch.rmode.active = 1;
  1176. vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
  1177. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  1178. vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
  1179. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  1180. vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1181. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1182. flags = vmcs_readl(GUEST_RFLAGS);
  1183. vcpu->arch.rmode.save_iopl
  1184. = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
  1185. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1186. vmcs_writel(GUEST_RFLAGS, flags);
  1187. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  1188. update_exception_bitmap(vcpu);
  1189. if (emulate_invalid_guest_state)
  1190. goto continue_rmode;
  1191. vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
  1192. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  1193. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  1194. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  1195. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  1196. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  1197. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  1198. vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
  1199. fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
  1200. fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
  1201. fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
  1202. fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
  1203. continue_rmode:
  1204. kvm_mmu_reset_context(vcpu);
  1205. init_rmode(vcpu->kvm);
  1206. }
  1207. #ifdef CONFIG_X86_64
  1208. static void enter_lmode(struct kvm_vcpu *vcpu)
  1209. {
  1210. u32 guest_tr_ar;
  1211. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1212. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  1213. printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
  1214. __func__);
  1215. vmcs_write32(GUEST_TR_AR_BYTES,
  1216. (guest_tr_ar & ~AR_TYPE_MASK)
  1217. | AR_TYPE_BUSY_64_TSS);
  1218. }
  1219. vcpu->arch.shadow_efer |= EFER_LMA;
  1220. find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
  1221. vmcs_write32(VM_ENTRY_CONTROLS,
  1222. vmcs_read32(VM_ENTRY_CONTROLS)
  1223. | VM_ENTRY_IA32E_MODE);
  1224. }
  1225. static void exit_lmode(struct kvm_vcpu *vcpu)
  1226. {
  1227. vcpu->arch.shadow_efer &= ~EFER_LMA;
  1228. vmcs_write32(VM_ENTRY_CONTROLS,
  1229. vmcs_read32(VM_ENTRY_CONTROLS)
  1230. & ~VM_ENTRY_IA32E_MODE);
  1231. }
  1232. #endif
  1233. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  1234. {
  1235. vpid_sync_vcpu_all(to_vmx(vcpu));
  1236. if (vm_need_ept())
  1237. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  1238. }
  1239. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1240. {
  1241. vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
  1242. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
  1243. }
  1244. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  1245. {
  1246. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1247. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1248. printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
  1249. return;
  1250. }
  1251. vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
  1252. vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
  1253. vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
  1254. vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
  1255. }
  1256. }
  1257. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  1258. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  1259. unsigned long cr0,
  1260. struct kvm_vcpu *vcpu)
  1261. {
  1262. if (!(cr0 & X86_CR0_PG)) {
  1263. /* From paging/starting to nonpaging */
  1264. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1265. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  1266. (CPU_BASED_CR3_LOAD_EXITING |
  1267. CPU_BASED_CR3_STORE_EXITING));
  1268. vcpu->arch.cr0 = cr0;
  1269. vmx_set_cr4(vcpu, vcpu->arch.cr4);
  1270. *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
  1271. *hw_cr0 &= ~X86_CR0_WP;
  1272. } else if (!is_paging(vcpu)) {
  1273. /* From nonpaging to paging */
  1274. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1275. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  1276. ~(CPU_BASED_CR3_LOAD_EXITING |
  1277. CPU_BASED_CR3_STORE_EXITING));
  1278. vcpu->arch.cr0 = cr0;
  1279. vmx_set_cr4(vcpu, vcpu->arch.cr4);
  1280. if (!(vcpu->arch.cr0 & X86_CR0_WP))
  1281. *hw_cr0 &= ~X86_CR0_WP;
  1282. }
  1283. }
  1284. static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
  1285. struct kvm_vcpu *vcpu)
  1286. {
  1287. if (!is_paging(vcpu)) {
  1288. *hw_cr4 &= ~X86_CR4_PAE;
  1289. *hw_cr4 |= X86_CR4_PSE;
  1290. } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
  1291. *hw_cr4 &= ~X86_CR4_PAE;
  1292. }
  1293. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1294. {
  1295. unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
  1296. KVM_VM_CR0_ALWAYS_ON;
  1297. vmx_fpu_deactivate(vcpu);
  1298. if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
  1299. enter_pmode(vcpu);
  1300. if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
  1301. enter_rmode(vcpu);
  1302. #ifdef CONFIG_X86_64
  1303. if (vcpu->arch.shadow_efer & EFER_LME) {
  1304. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  1305. enter_lmode(vcpu);
  1306. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  1307. exit_lmode(vcpu);
  1308. }
  1309. #endif
  1310. if (vm_need_ept())
  1311. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  1312. vmcs_writel(CR0_READ_SHADOW, cr0);
  1313. vmcs_writel(GUEST_CR0, hw_cr0);
  1314. vcpu->arch.cr0 = cr0;
  1315. if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
  1316. vmx_fpu_activate(vcpu);
  1317. }
  1318. static u64 construct_eptp(unsigned long root_hpa)
  1319. {
  1320. u64 eptp;
  1321. /* TODO write the value reading from MSR */
  1322. eptp = VMX_EPT_DEFAULT_MT |
  1323. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  1324. eptp |= (root_hpa & PAGE_MASK);
  1325. return eptp;
  1326. }
  1327. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  1328. {
  1329. unsigned long guest_cr3;
  1330. u64 eptp;
  1331. guest_cr3 = cr3;
  1332. if (vm_need_ept()) {
  1333. eptp = construct_eptp(cr3);
  1334. vmcs_write64(EPT_POINTER, eptp);
  1335. ept_sync_context(eptp);
  1336. ept_load_pdptrs(vcpu);
  1337. guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
  1338. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  1339. }
  1340. vmx_flush_tlb(vcpu);
  1341. vmcs_writel(GUEST_CR3, guest_cr3);
  1342. if (vcpu->arch.cr0 & X86_CR0_PE)
  1343. vmx_fpu_deactivate(vcpu);
  1344. }
  1345. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1346. {
  1347. unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
  1348. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  1349. vcpu->arch.cr4 = cr4;
  1350. if (vm_need_ept())
  1351. ept_update_paging_mode_cr4(&hw_cr4, vcpu);
  1352. vmcs_writel(CR4_READ_SHADOW, cr4);
  1353. vmcs_writel(GUEST_CR4, hw_cr4);
  1354. }
  1355. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1356. {
  1357. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1358. struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  1359. vcpu->arch.shadow_efer = efer;
  1360. if (!msr)
  1361. return;
  1362. if (efer & EFER_LMA) {
  1363. vmcs_write32(VM_ENTRY_CONTROLS,
  1364. vmcs_read32(VM_ENTRY_CONTROLS) |
  1365. VM_ENTRY_IA32E_MODE);
  1366. msr->data = efer;
  1367. } else {
  1368. vmcs_write32(VM_ENTRY_CONTROLS,
  1369. vmcs_read32(VM_ENTRY_CONTROLS) &
  1370. ~VM_ENTRY_IA32E_MODE);
  1371. msr->data = efer & ~EFER_LME;
  1372. }
  1373. setup_msrs(vmx);
  1374. }
  1375. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1376. {
  1377. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1378. return vmcs_readl(sf->base);
  1379. }
  1380. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1381. struct kvm_segment *var, int seg)
  1382. {
  1383. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1384. u32 ar;
  1385. var->base = vmcs_readl(sf->base);
  1386. var->limit = vmcs_read32(sf->limit);
  1387. var->selector = vmcs_read16(sf->selector);
  1388. ar = vmcs_read32(sf->ar_bytes);
  1389. if (ar & AR_UNUSABLE_MASK)
  1390. ar = 0;
  1391. var->type = ar & 15;
  1392. var->s = (ar >> 4) & 1;
  1393. var->dpl = (ar >> 5) & 3;
  1394. var->present = (ar >> 7) & 1;
  1395. var->avl = (ar >> 12) & 1;
  1396. var->l = (ar >> 13) & 1;
  1397. var->db = (ar >> 14) & 1;
  1398. var->g = (ar >> 15) & 1;
  1399. var->unusable = (ar >> 16) & 1;
  1400. }
  1401. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  1402. {
  1403. struct kvm_segment kvm_seg;
  1404. if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
  1405. return 0;
  1406. if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
  1407. return 3;
  1408. vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
  1409. return kvm_seg.selector & 3;
  1410. }
  1411. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  1412. {
  1413. u32 ar;
  1414. if (var->unusable)
  1415. ar = 1 << 16;
  1416. else {
  1417. ar = var->type & 15;
  1418. ar |= (var->s & 1) << 4;
  1419. ar |= (var->dpl & 3) << 5;
  1420. ar |= (var->present & 1) << 7;
  1421. ar |= (var->avl & 1) << 12;
  1422. ar |= (var->l & 1) << 13;
  1423. ar |= (var->db & 1) << 14;
  1424. ar |= (var->g & 1) << 15;
  1425. }
  1426. if (ar == 0) /* a 0 value means unusable */
  1427. ar = AR_UNUSABLE_MASK;
  1428. return ar;
  1429. }
  1430. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1431. struct kvm_segment *var, int seg)
  1432. {
  1433. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1434. u32 ar;
  1435. if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
  1436. vcpu->arch.rmode.tr.selector = var->selector;
  1437. vcpu->arch.rmode.tr.base = var->base;
  1438. vcpu->arch.rmode.tr.limit = var->limit;
  1439. vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
  1440. return;
  1441. }
  1442. vmcs_writel(sf->base, var->base);
  1443. vmcs_write32(sf->limit, var->limit);
  1444. vmcs_write16(sf->selector, var->selector);
  1445. if (vcpu->arch.rmode.active && var->s) {
  1446. /*
  1447. * Hack real-mode segments into vm86 compatibility.
  1448. */
  1449. if (var->base == 0xffff0000 && var->selector == 0xf000)
  1450. vmcs_writel(sf->base, 0xf0000);
  1451. ar = 0xf3;
  1452. } else
  1453. ar = vmx_segment_access_rights(var);
  1454. vmcs_write32(sf->ar_bytes, ar);
  1455. }
  1456. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  1457. {
  1458. u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
  1459. *db = (ar >> 14) & 1;
  1460. *l = (ar >> 13) & 1;
  1461. }
  1462. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1463. {
  1464. dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
  1465. dt->base = vmcs_readl(GUEST_IDTR_BASE);
  1466. }
  1467. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1468. {
  1469. vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
  1470. vmcs_writel(GUEST_IDTR_BASE, dt->base);
  1471. }
  1472. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1473. {
  1474. dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
  1475. dt->base = vmcs_readl(GUEST_GDTR_BASE);
  1476. }
  1477. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1478. {
  1479. vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
  1480. vmcs_writel(GUEST_GDTR_BASE, dt->base);
  1481. }
  1482. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1483. {
  1484. struct kvm_segment var;
  1485. u32 ar;
  1486. vmx_get_segment(vcpu, &var, seg);
  1487. ar = vmx_segment_access_rights(&var);
  1488. if (var.base != (var.selector << 4))
  1489. return false;
  1490. if (var.limit != 0xffff)
  1491. return false;
  1492. if (ar != 0xf3)
  1493. return false;
  1494. return true;
  1495. }
  1496. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  1497. {
  1498. struct kvm_segment cs;
  1499. unsigned int cs_rpl;
  1500. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1501. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  1502. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  1503. return false;
  1504. if (!cs.s)
  1505. return false;
  1506. if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) {
  1507. if (cs.dpl > cs_rpl)
  1508. return false;
  1509. } else if (cs.type & AR_TYPE_CODE_MASK) {
  1510. if (cs.dpl != cs_rpl)
  1511. return false;
  1512. }
  1513. if (!cs.present)
  1514. return false;
  1515. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  1516. return true;
  1517. }
  1518. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  1519. {
  1520. struct kvm_segment ss;
  1521. unsigned int ss_rpl;
  1522. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  1523. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  1524. if ((ss.type != 3) || (ss.type != 7))
  1525. return false;
  1526. if (!ss.s)
  1527. return false;
  1528. if (ss.dpl != ss_rpl) /* DPL != RPL */
  1529. return false;
  1530. if (!ss.present)
  1531. return false;
  1532. return true;
  1533. }
  1534. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1535. {
  1536. struct kvm_segment var;
  1537. unsigned int rpl;
  1538. vmx_get_segment(vcpu, &var, seg);
  1539. rpl = var.selector & SELECTOR_RPL_MASK;
  1540. if (!var.s)
  1541. return false;
  1542. if (!var.present)
  1543. return false;
  1544. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  1545. if (var.dpl < rpl) /* DPL < RPL */
  1546. return false;
  1547. }
  1548. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  1549. * rights flags
  1550. */
  1551. return true;
  1552. }
  1553. static bool tr_valid(struct kvm_vcpu *vcpu)
  1554. {
  1555. struct kvm_segment tr;
  1556. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  1557. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  1558. return false;
  1559. if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */
  1560. return false;
  1561. if (!tr.present)
  1562. return false;
  1563. return true;
  1564. }
  1565. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  1566. {
  1567. struct kvm_segment ldtr;
  1568. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  1569. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  1570. return false;
  1571. if (ldtr.type != 2)
  1572. return false;
  1573. if (!ldtr.present)
  1574. return false;
  1575. return true;
  1576. }
  1577. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  1578. {
  1579. struct kvm_segment cs, ss;
  1580. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1581. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  1582. return ((cs.selector & SELECTOR_RPL_MASK) ==
  1583. (ss.selector & SELECTOR_RPL_MASK));
  1584. }
  1585. /*
  1586. * Check if guest state is valid. Returns true if valid, false if
  1587. * not.
  1588. * We assume that registers are always usable
  1589. */
  1590. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  1591. {
  1592. /* real mode guest state checks */
  1593. if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
  1594. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  1595. return false;
  1596. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  1597. return false;
  1598. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  1599. return false;
  1600. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  1601. return false;
  1602. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  1603. return false;
  1604. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  1605. return false;
  1606. } else {
  1607. /* protected mode guest state checks */
  1608. if (!cs_ss_rpl_check(vcpu))
  1609. return false;
  1610. if (!code_segment_valid(vcpu))
  1611. return false;
  1612. if (!stack_segment_valid(vcpu))
  1613. return false;
  1614. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  1615. return false;
  1616. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  1617. return false;
  1618. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  1619. return false;
  1620. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  1621. return false;
  1622. if (!tr_valid(vcpu))
  1623. return false;
  1624. if (!ldtr_valid(vcpu))
  1625. return false;
  1626. }
  1627. /* TODO:
  1628. * - Add checks on RIP
  1629. * - Add checks on RFLAGS
  1630. */
  1631. return true;
  1632. }
  1633. static int init_rmode_tss(struct kvm *kvm)
  1634. {
  1635. gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  1636. u16 data = 0;
  1637. int ret = 0;
  1638. int r;
  1639. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  1640. if (r < 0)
  1641. goto out;
  1642. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  1643. r = kvm_write_guest_page(kvm, fn++, &data,
  1644. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  1645. if (r < 0)
  1646. goto out;
  1647. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  1648. if (r < 0)
  1649. goto out;
  1650. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  1651. if (r < 0)
  1652. goto out;
  1653. data = ~0;
  1654. r = kvm_write_guest_page(kvm, fn, &data,
  1655. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  1656. sizeof(u8));
  1657. if (r < 0)
  1658. goto out;
  1659. ret = 1;
  1660. out:
  1661. return ret;
  1662. }
  1663. static int init_rmode_identity_map(struct kvm *kvm)
  1664. {
  1665. int i, r, ret;
  1666. pfn_t identity_map_pfn;
  1667. u32 tmp;
  1668. if (!vm_need_ept())
  1669. return 1;
  1670. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  1671. printk(KERN_ERR "EPT: identity-mapping pagetable "
  1672. "haven't been allocated!\n");
  1673. return 0;
  1674. }
  1675. if (likely(kvm->arch.ept_identity_pagetable_done))
  1676. return 1;
  1677. ret = 0;
  1678. identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
  1679. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  1680. if (r < 0)
  1681. goto out;
  1682. /* Set up identity-mapping pagetable for EPT in real mode */
  1683. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  1684. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  1685. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  1686. r = kvm_write_guest_page(kvm, identity_map_pfn,
  1687. &tmp, i * sizeof(tmp), sizeof(tmp));
  1688. if (r < 0)
  1689. goto out;
  1690. }
  1691. kvm->arch.ept_identity_pagetable_done = true;
  1692. ret = 1;
  1693. out:
  1694. return ret;
  1695. }
  1696. static void seg_setup(int seg)
  1697. {
  1698. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1699. vmcs_write16(sf->selector, 0);
  1700. vmcs_writel(sf->base, 0);
  1701. vmcs_write32(sf->limit, 0xffff);
  1702. vmcs_write32(sf->ar_bytes, 0xf3);
  1703. }
  1704. static int alloc_apic_access_page(struct kvm *kvm)
  1705. {
  1706. struct kvm_userspace_memory_region kvm_userspace_mem;
  1707. int r = 0;
  1708. down_write(&kvm->slots_lock);
  1709. if (kvm->arch.apic_access_page)
  1710. goto out;
  1711. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  1712. kvm_userspace_mem.flags = 0;
  1713. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  1714. kvm_userspace_mem.memory_size = PAGE_SIZE;
  1715. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1716. if (r)
  1717. goto out;
  1718. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  1719. out:
  1720. up_write(&kvm->slots_lock);
  1721. return r;
  1722. }
  1723. static int alloc_identity_pagetable(struct kvm *kvm)
  1724. {
  1725. struct kvm_userspace_memory_region kvm_userspace_mem;
  1726. int r = 0;
  1727. down_write(&kvm->slots_lock);
  1728. if (kvm->arch.ept_identity_pagetable)
  1729. goto out;
  1730. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  1731. kvm_userspace_mem.flags = 0;
  1732. kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  1733. kvm_userspace_mem.memory_size = PAGE_SIZE;
  1734. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1735. if (r)
  1736. goto out;
  1737. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  1738. VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
  1739. out:
  1740. up_write(&kvm->slots_lock);
  1741. return r;
  1742. }
  1743. static void allocate_vpid(struct vcpu_vmx *vmx)
  1744. {
  1745. int vpid;
  1746. vmx->vpid = 0;
  1747. if (!enable_vpid || !cpu_has_vmx_vpid())
  1748. return;
  1749. spin_lock(&vmx_vpid_lock);
  1750. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1751. if (vpid < VMX_NR_VPIDS) {
  1752. vmx->vpid = vpid;
  1753. __set_bit(vpid, vmx_vpid_bitmap);
  1754. }
  1755. spin_unlock(&vmx_vpid_lock);
  1756. }
  1757. static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
  1758. {
  1759. void *va;
  1760. if (!cpu_has_vmx_msr_bitmap())
  1761. return;
  1762. /*
  1763. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  1764. * have the write-low and read-high bitmap offsets the wrong way round.
  1765. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  1766. */
  1767. va = kmap(msr_bitmap);
  1768. if (msr <= 0x1fff) {
  1769. __clear_bit(msr, va + 0x000); /* read-low */
  1770. __clear_bit(msr, va + 0x800); /* write-low */
  1771. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  1772. msr &= 0x1fff;
  1773. __clear_bit(msr, va + 0x400); /* read-high */
  1774. __clear_bit(msr, va + 0xc00); /* write-high */
  1775. }
  1776. kunmap(msr_bitmap);
  1777. }
  1778. /*
  1779. * Sets up the vmcs for emulated real mode.
  1780. */
  1781. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  1782. {
  1783. u32 host_sysenter_cs;
  1784. u32 junk;
  1785. unsigned long a;
  1786. struct descriptor_table dt;
  1787. int i;
  1788. unsigned long kvm_vmx_return;
  1789. u32 exec_control;
  1790. /* I/O */
  1791. vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
  1792. vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
  1793. if (cpu_has_vmx_msr_bitmap())
  1794. vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
  1795. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  1796. /* Control */
  1797. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  1798. vmcs_config.pin_based_exec_ctrl);
  1799. exec_control = vmcs_config.cpu_based_exec_ctrl;
  1800. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  1801. exec_control &= ~CPU_BASED_TPR_SHADOW;
  1802. #ifdef CONFIG_X86_64
  1803. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  1804. CPU_BASED_CR8_LOAD_EXITING;
  1805. #endif
  1806. }
  1807. if (!vm_need_ept())
  1808. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  1809. CPU_BASED_CR3_LOAD_EXITING |
  1810. CPU_BASED_INVLPG_EXITING;
  1811. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  1812. if (cpu_has_secondary_exec_ctrls()) {
  1813. exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  1814. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  1815. exec_control &=
  1816. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1817. if (vmx->vpid == 0)
  1818. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  1819. if (!vm_need_ept())
  1820. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  1821. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  1822. }
  1823. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
  1824. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
  1825. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  1826. vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
  1827. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  1828. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  1829. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  1830. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  1831. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  1832. vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
  1833. vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
  1834. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  1835. #ifdef CONFIG_X86_64
  1836. rdmsrl(MSR_FS_BASE, a);
  1837. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  1838. rdmsrl(MSR_GS_BASE, a);
  1839. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  1840. #else
  1841. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  1842. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  1843. #endif
  1844. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  1845. kvm_get_idt(&dt);
  1846. vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
  1847. asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
  1848. vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
  1849. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  1850. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  1851. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  1852. rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
  1853. vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
  1854. rdmsrl(MSR_IA32_SYSENTER_ESP, a);
  1855. vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
  1856. rdmsrl(MSR_IA32_SYSENTER_EIP, a);
  1857. vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
  1858. for (i = 0; i < NR_VMX_MSR; ++i) {
  1859. u32 index = vmx_msr_index[i];
  1860. u32 data_low, data_high;
  1861. u64 data;
  1862. int j = vmx->nmsrs;
  1863. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  1864. continue;
  1865. if (wrmsr_safe(index, data_low, data_high) < 0)
  1866. continue;
  1867. data = data_low | ((u64)data_high << 32);
  1868. vmx->host_msrs[j].index = index;
  1869. vmx->host_msrs[j].reserved = 0;
  1870. vmx->host_msrs[j].data = data;
  1871. vmx->guest_msrs[j] = vmx->host_msrs[j];
  1872. ++vmx->nmsrs;
  1873. }
  1874. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  1875. /* 22.2.1, 20.8.1 */
  1876. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  1877. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  1878. vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
  1879. return 0;
  1880. }
  1881. static int init_rmode(struct kvm *kvm)
  1882. {
  1883. if (!init_rmode_tss(kvm))
  1884. return 0;
  1885. if (!init_rmode_identity_map(kvm))
  1886. return 0;
  1887. return 1;
  1888. }
  1889. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  1890. {
  1891. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1892. u64 msr;
  1893. int ret;
  1894. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  1895. down_read(&vcpu->kvm->slots_lock);
  1896. if (!init_rmode(vmx->vcpu.kvm)) {
  1897. ret = -ENOMEM;
  1898. goto out;
  1899. }
  1900. vmx->vcpu.arch.rmode.active = 0;
  1901. vmx->soft_vnmi_blocked = 0;
  1902. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  1903. kvm_set_cr8(&vmx->vcpu, 0);
  1904. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  1905. if (vmx->vcpu.vcpu_id == 0)
  1906. msr |= MSR_IA32_APICBASE_BSP;
  1907. kvm_set_apic_base(&vmx->vcpu, msr);
  1908. fx_init(&vmx->vcpu);
  1909. seg_setup(VCPU_SREG_CS);
  1910. /*
  1911. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  1912. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  1913. */
  1914. if (vmx->vcpu.vcpu_id == 0) {
  1915. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  1916. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  1917. } else {
  1918. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  1919. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  1920. }
  1921. seg_setup(VCPU_SREG_DS);
  1922. seg_setup(VCPU_SREG_ES);
  1923. seg_setup(VCPU_SREG_FS);
  1924. seg_setup(VCPU_SREG_GS);
  1925. seg_setup(VCPU_SREG_SS);
  1926. vmcs_write16(GUEST_TR_SELECTOR, 0);
  1927. vmcs_writel(GUEST_TR_BASE, 0);
  1928. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  1929. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1930. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  1931. vmcs_writel(GUEST_LDTR_BASE, 0);
  1932. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  1933. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  1934. vmcs_write32(GUEST_SYSENTER_CS, 0);
  1935. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  1936. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  1937. vmcs_writel(GUEST_RFLAGS, 0x02);
  1938. if (vmx->vcpu.vcpu_id == 0)
  1939. kvm_rip_write(vcpu, 0xfff0);
  1940. else
  1941. kvm_rip_write(vcpu, 0);
  1942. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  1943. /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
  1944. vmcs_writel(GUEST_DR7, 0x400);
  1945. vmcs_writel(GUEST_GDTR_BASE, 0);
  1946. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  1947. vmcs_writel(GUEST_IDTR_BASE, 0);
  1948. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  1949. vmcs_write32(GUEST_ACTIVITY_STATE, 0);
  1950. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  1951. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  1952. guest_write_tsc(0);
  1953. /* Special registers */
  1954. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  1955. setup_msrs(vmx);
  1956. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  1957. if (cpu_has_vmx_tpr_shadow()) {
  1958. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  1959. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  1960. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  1961. page_to_phys(vmx->vcpu.arch.apic->regs_page));
  1962. vmcs_write32(TPR_THRESHOLD, 0);
  1963. }
  1964. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  1965. vmcs_write64(APIC_ACCESS_ADDR,
  1966. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  1967. if (vmx->vpid != 0)
  1968. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  1969. vmx->vcpu.arch.cr0 = 0x60000010;
  1970. vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
  1971. vmx_set_cr4(&vmx->vcpu, 0);
  1972. vmx_set_efer(&vmx->vcpu, 0);
  1973. vmx_fpu_activate(&vmx->vcpu);
  1974. update_exception_bitmap(&vmx->vcpu);
  1975. vpid_sync_vcpu_all(vmx);
  1976. ret = 0;
  1977. /* HACK: Don't enable emulation on guest boot/reset */
  1978. vmx->emulation_required = 0;
  1979. out:
  1980. up_read(&vcpu->kvm->slots_lock);
  1981. return ret;
  1982. }
  1983. static void enable_irq_window(struct kvm_vcpu *vcpu)
  1984. {
  1985. u32 cpu_based_vm_exec_control;
  1986. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  1987. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  1988. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  1989. }
  1990. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  1991. {
  1992. u32 cpu_based_vm_exec_control;
  1993. if (!cpu_has_virtual_nmis()) {
  1994. enable_irq_window(vcpu);
  1995. return;
  1996. }
  1997. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  1998. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  1999. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2000. }
  2001. static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
  2002. {
  2003. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2004. KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
  2005. ++vcpu->stat.irq_injections;
  2006. if (vcpu->arch.rmode.active) {
  2007. vmx->rmode.irq.pending = true;
  2008. vmx->rmode.irq.vector = irq;
  2009. vmx->rmode.irq.rip = kvm_rip_read(vcpu);
  2010. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2011. irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
  2012. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
  2013. kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
  2014. return;
  2015. }
  2016. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2017. irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  2018. }
  2019. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  2020. {
  2021. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2022. if (!cpu_has_virtual_nmis()) {
  2023. /*
  2024. * Tracking the NMI-blocked state in software is built upon
  2025. * finding the next open IRQ window. This, in turn, depends on
  2026. * well-behaving guests: They have to keep IRQs disabled at
  2027. * least as long as the NMI handler runs. Otherwise we may
  2028. * cause NMI nesting, maybe breaking the guest. But as this is
  2029. * highly unlikely, we can live with the residual risk.
  2030. */
  2031. vmx->soft_vnmi_blocked = 1;
  2032. vmx->vnmi_blocked_time = 0;
  2033. }
  2034. ++vcpu->stat.nmi_injections;
  2035. if (vcpu->arch.rmode.active) {
  2036. vmx->rmode.irq.pending = true;
  2037. vmx->rmode.irq.vector = NMI_VECTOR;
  2038. vmx->rmode.irq.rip = kvm_rip_read(vcpu);
  2039. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2040. NMI_VECTOR | INTR_TYPE_SOFT_INTR |
  2041. INTR_INFO_VALID_MASK);
  2042. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
  2043. kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
  2044. return;
  2045. }
  2046. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2047. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  2048. }
  2049. static void vmx_update_window_states(struct kvm_vcpu *vcpu)
  2050. {
  2051. u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2052. vcpu->arch.nmi_window_open =
  2053. !(guest_intr & (GUEST_INTR_STATE_STI |
  2054. GUEST_INTR_STATE_MOV_SS |
  2055. GUEST_INTR_STATE_NMI));
  2056. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  2057. vcpu->arch.nmi_window_open = 0;
  2058. vcpu->arch.interrupt_window_open =
  2059. ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  2060. !(guest_intr & (GUEST_INTR_STATE_STI |
  2061. GUEST_INTR_STATE_MOV_SS)));
  2062. }
  2063. static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
  2064. {
  2065. int word_index = __ffs(vcpu->arch.irq_summary);
  2066. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  2067. int irq = word_index * BITS_PER_LONG + bit_index;
  2068. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  2069. if (!vcpu->arch.irq_pending[word_index])
  2070. clear_bit(word_index, &vcpu->arch.irq_summary);
  2071. kvm_queue_interrupt(vcpu, irq);
  2072. }
  2073. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  2074. struct kvm_run *kvm_run)
  2075. {
  2076. vmx_update_window_states(vcpu);
  2077. if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
  2078. if (vcpu->arch.nmi_window_open) {
  2079. vcpu->arch.nmi_pending = false;
  2080. vcpu->arch.nmi_injected = true;
  2081. } else {
  2082. enable_nmi_window(vcpu);
  2083. return;
  2084. }
  2085. }
  2086. if (vcpu->arch.nmi_injected) {
  2087. vmx_inject_nmi(vcpu);
  2088. if (vcpu->arch.nmi_pending || kvm_run->request_nmi_window)
  2089. enable_nmi_window(vcpu);
  2090. else if (vcpu->arch.irq_summary
  2091. || kvm_run->request_interrupt_window)
  2092. enable_irq_window(vcpu);
  2093. return;
  2094. }
  2095. if (!vcpu->arch.nmi_window_open || kvm_run->request_nmi_window)
  2096. enable_nmi_window(vcpu);
  2097. if (vcpu->arch.interrupt_window_open) {
  2098. if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
  2099. kvm_do_inject_irq(vcpu);
  2100. if (vcpu->arch.interrupt.pending)
  2101. vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
  2102. }
  2103. if (!vcpu->arch.interrupt_window_open &&
  2104. (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
  2105. enable_irq_window(vcpu);
  2106. }
  2107. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2108. {
  2109. int ret;
  2110. struct kvm_userspace_memory_region tss_mem = {
  2111. .slot = 8,
  2112. .guest_phys_addr = addr,
  2113. .memory_size = PAGE_SIZE * 3,
  2114. .flags = 0,
  2115. };
  2116. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  2117. if (ret)
  2118. return ret;
  2119. kvm->arch.tss_addr = addr;
  2120. return 0;
  2121. }
  2122. static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
  2123. {
  2124. struct kvm_guest_debug *dbg = &vcpu->guest_debug;
  2125. set_debugreg(dbg->bp[0], 0);
  2126. set_debugreg(dbg->bp[1], 1);
  2127. set_debugreg(dbg->bp[2], 2);
  2128. set_debugreg(dbg->bp[3], 3);
  2129. if (dbg->singlestep) {
  2130. unsigned long flags;
  2131. flags = vmcs_readl(GUEST_RFLAGS);
  2132. flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
  2133. vmcs_writel(GUEST_RFLAGS, flags);
  2134. }
  2135. }
  2136. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  2137. int vec, u32 err_code)
  2138. {
  2139. /*
  2140. * Instruction with address size override prefix opcode 0x67
  2141. * Cause the #SS fault with 0 error code in VM86 mode.
  2142. */
  2143. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  2144. if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
  2145. return 1;
  2146. /*
  2147. * Forward all other exceptions that are valid in real mode.
  2148. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  2149. * the required debugging infrastructure rework.
  2150. */
  2151. switch (vec) {
  2152. case DE_VECTOR:
  2153. case DB_VECTOR:
  2154. case BP_VECTOR:
  2155. case OF_VECTOR:
  2156. case BR_VECTOR:
  2157. case UD_VECTOR:
  2158. case DF_VECTOR:
  2159. case SS_VECTOR:
  2160. case GP_VECTOR:
  2161. case MF_VECTOR:
  2162. kvm_queue_exception(vcpu, vec);
  2163. return 1;
  2164. }
  2165. return 0;
  2166. }
  2167. static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2168. {
  2169. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2170. u32 intr_info, error_code;
  2171. unsigned long cr2, rip;
  2172. u32 vect_info;
  2173. enum emulation_result er;
  2174. vect_info = vmx->idt_vectoring_info;
  2175. intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  2176. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  2177. !is_page_fault(intr_info))
  2178. printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
  2179. "intr info 0x%x\n", __func__, vect_info, intr_info);
  2180. if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
  2181. int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
  2182. set_bit(irq, vcpu->arch.irq_pending);
  2183. set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  2184. }
  2185. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  2186. return 1; /* already handled by vmx_vcpu_run() */
  2187. if (is_no_device(intr_info)) {
  2188. vmx_fpu_activate(vcpu);
  2189. return 1;
  2190. }
  2191. if (is_invalid_opcode(intr_info)) {
  2192. er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  2193. if (er != EMULATE_DONE)
  2194. kvm_queue_exception(vcpu, UD_VECTOR);
  2195. return 1;
  2196. }
  2197. error_code = 0;
  2198. rip = kvm_rip_read(vcpu);
  2199. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  2200. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  2201. if (is_page_fault(intr_info)) {
  2202. /* EPT won't cause page fault directly */
  2203. if (vm_need_ept())
  2204. BUG();
  2205. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  2206. KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
  2207. (u32)((u64)cr2 >> 32), handler);
  2208. if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
  2209. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  2210. return kvm_mmu_page_fault(vcpu, cr2, error_code);
  2211. }
  2212. if (vcpu->arch.rmode.active &&
  2213. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  2214. error_code)) {
  2215. if (vcpu->arch.halt_request) {
  2216. vcpu->arch.halt_request = 0;
  2217. return kvm_emulate_halt(vcpu);
  2218. }
  2219. return 1;
  2220. }
  2221. if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
  2222. (INTR_TYPE_EXCEPTION | 1)) {
  2223. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  2224. return 0;
  2225. }
  2226. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  2227. kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
  2228. kvm_run->ex.error_code = error_code;
  2229. return 0;
  2230. }
  2231. static int handle_external_interrupt(struct kvm_vcpu *vcpu,
  2232. struct kvm_run *kvm_run)
  2233. {
  2234. ++vcpu->stat.irq_exits;
  2235. KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
  2236. return 1;
  2237. }
  2238. static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2239. {
  2240. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  2241. return 0;
  2242. }
  2243. static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2244. {
  2245. unsigned long exit_qualification;
  2246. int size, down, in, string, rep;
  2247. unsigned port;
  2248. ++vcpu->stat.io_exits;
  2249. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2250. string = (exit_qualification & 16) != 0;
  2251. if (string) {
  2252. if (emulate_instruction(vcpu,
  2253. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  2254. return 0;
  2255. return 1;
  2256. }
  2257. size = (exit_qualification & 7) + 1;
  2258. in = (exit_qualification & 8) != 0;
  2259. down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
  2260. rep = (exit_qualification & 32) != 0;
  2261. port = exit_qualification >> 16;
  2262. return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
  2263. }
  2264. static void
  2265. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2266. {
  2267. /*
  2268. * Patch in the VMCALL instruction:
  2269. */
  2270. hypercall[0] = 0x0f;
  2271. hypercall[1] = 0x01;
  2272. hypercall[2] = 0xc1;
  2273. }
  2274. static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2275. {
  2276. unsigned long exit_qualification;
  2277. int cr;
  2278. int reg;
  2279. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2280. cr = exit_qualification & 15;
  2281. reg = (exit_qualification >> 8) & 15;
  2282. switch ((exit_qualification >> 4) & 3) {
  2283. case 0: /* mov to cr */
  2284. KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
  2285. (u32)kvm_register_read(vcpu, reg),
  2286. (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
  2287. handler);
  2288. switch (cr) {
  2289. case 0:
  2290. kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
  2291. skip_emulated_instruction(vcpu);
  2292. return 1;
  2293. case 3:
  2294. kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
  2295. skip_emulated_instruction(vcpu);
  2296. return 1;
  2297. case 4:
  2298. kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
  2299. skip_emulated_instruction(vcpu);
  2300. return 1;
  2301. case 8:
  2302. kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
  2303. skip_emulated_instruction(vcpu);
  2304. if (irqchip_in_kernel(vcpu->kvm))
  2305. return 1;
  2306. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2307. return 0;
  2308. };
  2309. break;
  2310. case 2: /* clts */
  2311. vmx_fpu_deactivate(vcpu);
  2312. vcpu->arch.cr0 &= ~X86_CR0_TS;
  2313. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  2314. vmx_fpu_activate(vcpu);
  2315. KVMTRACE_0D(CLTS, vcpu, handler);
  2316. skip_emulated_instruction(vcpu);
  2317. return 1;
  2318. case 1: /*mov from cr*/
  2319. switch (cr) {
  2320. case 3:
  2321. kvm_register_write(vcpu, reg, vcpu->arch.cr3);
  2322. KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
  2323. (u32)kvm_register_read(vcpu, reg),
  2324. (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
  2325. handler);
  2326. skip_emulated_instruction(vcpu);
  2327. return 1;
  2328. case 8:
  2329. kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
  2330. KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
  2331. (u32)kvm_register_read(vcpu, reg), handler);
  2332. skip_emulated_instruction(vcpu);
  2333. return 1;
  2334. }
  2335. break;
  2336. case 3: /* lmsw */
  2337. kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
  2338. skip_emulated_instruction(vcpu);
  2339. return 1;
  2340. default:
  2341. break;
  2342. }
  2343. kvm_run->exit_reason = 0;
  2344. pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  2345. (int)(exit_qualification >> 4) & 3, cr);
  2346. return 0;
  2347. }
  2348. static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2349. {
  2350. unsigned long exit_qualification;
  2351. unsigned long val;
  2352. int dr, reg;
  2353. /*
  2354. * FIXME: this code assumes the host is debugging the guest.
  2355. * need to deal with guest debugging itself too.
  2356. */
  2357. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2358. dr = exit_qualification & 7;
  2359. reg = (exit_qualification >> 8) & 15;
  2360. if (exit_qualification & 16) {
  2361. /* mov from dr */
  2362. switch (dr) {
  2363. case 6:
  2364. val = 0xffff0ff0;
  2365. break;
  2366. case 7:
  2367. val = 0x400;
  2368. break;
  2369. default:
  2370. val = 0;
  2371. }
  2372. kvm_register_write(vcpu, reg, val);
  2373. KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
  2374. } else {
  2375. /* mov to dr */
  2376. }
  2377. skip_emulated_instruction(vcpu);
  2378. return 1;
  2379. }
  2380. static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2381. {
  2382. kvm_emulate_cpuid(vcpu);
  2383. return 1;
  2384. }
  2385. static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2386. {
  2387. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2388. u64 data;
  2389. if (vmx_get_msr(vcpu, ecx, &data)) {
  2390. kvm_inject_gp(vcpu, 0);
  2391. return 1;
  2392. }
  2393. KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
  2394. handler);
  2395. /* FIXME: handling of bits 32:63 of rax, rdx */
  2396. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  2397. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  2398. skip_emulated_instruction(vcpu);
  2399. return 1;
  2400. }
  2401. static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2402. {
  2403. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2404. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  2405. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2406. KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
  2407. handler);
  2408. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  2409. kvm_inject_gp(vcpu, 0);
  2410. return 1;
  2411. }
  2412. skip_emulated_instruction(vcpu);
  2413. return 1;
  2414. }
  2415. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
  2416. struct kvm_run *kvm_run)
  2417. {
  2418. return 1;
  2419. }
  2420. static int handle_interrupt_window(struct kvm_vcpu *vcpu,
  2421. struct kvm_run *kvm_run)
  2422. {
  2423. u32 cpu_based_vm_exec_control;
  2424. /* clear pending irq */
  2425. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2426. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  2427. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2428. KVMTRACE_0D(PEND_INTR, vcpu, handler);
  2429. ++vcpu->stat.irq_window_exits;
  2430. /*
  2431. * If the user space waits to inject interrupts, exit as soon as
  2432. * possible
  2433. */
  2434. if (kvm_run->request_interrupt_window &&
  2435. !vcpu->arch.irq_summary) {
  2436. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2437. return 0;
  2438. }
  2439. return 1;
  2440. }
  2441. static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2442. {
  2443. skip_emulated_instruction(vcpu);
  2444. return kvm_emulate_halt(vcpu);
  2445. }
  2446. static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2447. {
  2448. skip_emulated_instruction(vcpu);
  2449. kvm_emulate_hypercall(vcpu);
  2450. return 1;
  2451. }
  2452. static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2453. {
  2454. u64 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
  2455. kvm_mmu_invlpg(vcpu, exit_qualification);
  2456. skip_emulated_instruction(vcpu);
  2457. return 1;
  2458. }
  2459. static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2460. {
  2461. skip_emulated_instruction(vcpu);
  2462. /* TODO: Add support for VT-d/pass-through device */
  2463. return 1;
  2464. }
  2465. static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2466. {
  2467. u64 exit_qualification;
  2468. enum emulation_result er;
  2469. unsigned long offset;
  2470. exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
  2471. offset = exit_qualification & 0xffful;
  2472. er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
  2473. if (er != EMULATE_DONE) {
  2474. printk(KERN_ERR
  2475. "Fail to handle apic access vmexit! Offset is 0x%lx\n",
  2476. offset);
  2477. return -ENOTSUPP;
  2478. }
  2479. return 1;
  2480. }
  2481. static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2482. {
  2483. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2484. unsigned long exit_qualification;
  2485. u16 tss_selector;
  2486. int reason;
  2487. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2488. reason = (u32)exit_qualification >> 30;
  2489. if (reason == TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected &&
  2490. (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  2491. (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK)
  2492. == INTR_TYPE_NMI_INTR) {
  2493. vcpu->arch.nmi_injected = false;
  2494. if (cpu_has_virtual_nmis())
  2495. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2496. GUEST_INTR_STATE_NMI);
  2497. }
  2498. tss_selector = exit_qualification;
  2499. return kvm_task_switch(vcpu, tss_selector, reason);
  2500. }
  2501. static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2502. {
  2503. u64 exit_qualification;
  2504. enum emulation_result er;
  2505. gpa_t gpa;
  2506. unsigned long hva;
  2507. int gla_validity;
  2508. int r;
  2509. exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
  2510. if (exit_qualification & (1 << 6)) {
  2511. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  2512. return -ENOTSUPP;
  2513. }
  2514. gla_validity = (exit_qualification >> 7) & 0x3;
  2515. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  2516. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  2517. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  2518. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  2519. (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
  2520. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  2521. (long unsigned int)exit_qualification);
  2522. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2523. kvm_run->hw.hardware_exit_reason = 0;
  2524. return -ENOTSUPP;
  2525. }
  2526. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  2527. hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
  2528. if (!kvm_is_error_hva(hva)) {
  2529. r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
  2530. if (r < 0) {
  2531. printk(KERN_ERR "EPT: Not enough memory!\n");
  2532. return -ENOMEM;
  2533. }
  2534. return 1;
  2535. } else {
  2536. /* must be MMIO */
  2537. er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
  2538. if (er == EMULATE_FAIL) {
  2539. printk(KERN_ERR
  2540. "EPT: Fail to handle EPT violation vmexit!er is %d\n",
  2541. er);
  2542. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  2543. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  2544. (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
  2545. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  2546. (long unsigned int)exit_qualification);
  2547. return -ENOTSUPP;
  2548. } else if (er == EMULATE_DO_MMIO)
  2549. return 0;
  2550. }
  2551. return 1;
  2552. }
  2553. static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2554. {
  2555. u32 cpu_based_vm_exec_control;
  2556. /* clear pending NMI */
  2557. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2558. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  2559. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2560. ++vcpu->stat.nmi_window_exits;
  2561. /*
  2562. * If the user space waits to inject a NMI, exit as soon as possible
  2563. */
  2564. if (kvm_run->request_nmi_window && !vcpu->arch.nmi_pending) {
  2565. kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN;
  2566. return 0;
  2567. }
  2568. return 1;
  2569. }
  2570. static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
  2571. struct kvm_run *kvm_run)
  2572. {
  2573. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2574. int err;
  2575. preempt_enable();
  2576. local_irq_enable();
  2577. while (!guest_state_valid(vcpu)) {
  2578. err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
  2579. switch (err) {
  2580. case EMULATE_DONE:
  2581. break;
  2582. case EMULATE_DO_MMIO:
  2583. kvm_report_emulation_failure(vcpu, "mmio");
  2584. /* TODO: Handle MMIO */
  2585. return;
  2586. default:
  2587. kvm_report_emulation_failure(vcpu, "emulation failure");
  2588. return;
  2589. }
  2590. if (signal_pending(current))
  2591. break;
  2592. if (need_resched())
  2593. schedule();
  2594. }
  2595. local_irq_disable();
  2596. preempt_disable();
  2597. /* Guest state should be valid now, no more emulation should be needed */
  2598. vmx->emulation_required = 0;
  2599. }
  2600. /*
  2601. * The exit handlers return 1 if the exit was handled fully and guest execution
  2602. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  2603. * to be done to userspace and return 0.
  2604. */
  2605. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
  2606. struct kvm_run *kvm_run) = {
  2607. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  2608. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  2609. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  2610. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  2611. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  2612. [EXIT_REASON_CR_ACCESS] = handle_cr,
  2613. [EXIT_REASON_DR_ACCESS] = handle_dr,
  2614. [EXIT_REASON_CPUID] = handle_cpuid,
  2615. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  2616. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  2617. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  2618. [EXIT_REASON_HLT] = handle_halt,
  2619. [EXIT_REASON_INVLPG] = handle_invlpg,
  2620. [EXIT_REASON_VMCALL] = handle_vmcall,
  2621. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  2622. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  2623. [EXIT_REASON_WBINVD] = handle_wbinvd,
  2624. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  2625. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  2626. };
  2627. static const int kvm_vmx_max_exit_handlers =
  2628. ARRAY_SIZE(kvm_vmx_exit_handlers);
  2629. /*
  2630. * The guest has exited. See if we can fix it or if we need userspace
  2631. * assistance.
  2632. */
  2633. static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  2634. {
  2635. u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
  2636. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2637. u32 vectoring_info = vmx->idt_vectoring_info;
  2638. KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
  2639. (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
  2640. /* Access CR3 don't cause VMExit in paging mode, so we need
  2641. * to sync with guest real CR3. */
  2642. if (vm_need_ept() && is_paging(vcpu)) {
  2643. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  2644. ept_load_pdptrs(vcpu);
  2645. }
  2646. if (unlikely(vmx->fail)) {
  2647. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2648. kvm_run->fail_entry.hardware_entry_failure_reason
  2649. = vmcs_read32(VM_INSTRUCTION_ERROR);
  2650. return 0;
  2651. }
  2652. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  2653. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  2654. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  2655. exit_reason != EXIT_REASON_TASK_SWITCH))
  2656. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  2657. "(0x%x) and exit reason is 0x%x\n",
  2658. __func__, vectoring_info, exit_reason);
  2659. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
  2660. if (vcpu->arch.interrupt_window_open) {
  2661. vmx->soft_vnmi_blocked = 0;
  2662. vcpu->arch.nmi_window_open = 1;
  2663. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  2664. (kvm_run->request_nmi_window || vcpu->arch.nmi_pending)) {
  2665. /*
  2666. * This CPU don't support us in finding the end of an
  2667. * NMI-blocked window if the guest runs with IRQs
  2668. * disabled. So we pull the trigger after 1 s of
  2669. * futile waiting, but inform the user about this.
  2670. */
  2671. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  2672. "state on VCPU %d after 1 s timeout\n",
  2673. __func__, vcpu->vcpu_id);
  2674. vmx->soft_vnmi_blocked = 0;
  2675. vmx->vcpu.arch.nmi_window_open = 1;
  2676. }
  2677. /*
  2678. * If the user space waits to inject an NNI, exit ASAP
  2679. */
  2680. if (vcpu->arch.nmi_window_open && kvm_run->request_nmi_window
  2681. && !vcpu->arch.nmi_pending) {
  2682. kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN;
  2683. ++vcpu->stat.nmi_window_exits;
  2684. return 0;
  2685. }
  2686. }
  2687. if (exit_reason < kvm_vmx_max_exit_handlers
  2688. && kvm_vmx_exit_handlers[exit_reason])
  2689. return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
  2690. else {
  2691. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2692. kvm_run->hw.hardware_exit_reason = exit_reason;
  2693. }
  2694. return 0;
  2695. }
  2696. static void update_tpr_threshold(struct kvm_vcpu *vcpu)
  2697. {
  2698. int max_irr, tpr;
  2699. if (!vm_need_tpr_shadow(vcpu->kvm))
  2700. return;
  2701. if (!kvm_lapic_enabled(vcpu) ||
  2702. ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
  2703. vmcs_write32(TPR_THRESHOLD, 0);
  2704. return;
  2705. }
  2706. tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
  2707. vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
  2708. }
  2709. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  2710. {
  2711. u32 exit_intr_info;
  2712. u32 idt_vectoring_info;
  2713. bool unblock_nmi;
  2714. u8 vector;
  2715. int type;
  2716. bool idtv_info_valid;
  2717. u32 error;
  2718. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  2719. if (cpu_has_virtual_nmis()) {
  2720. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  2721. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  2722. /*
  2723. * SDM 3: 25.7.1.2
  2724. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  2725. * a guest IRET fault.
  2726. */
  2727. if (unblock_nmi && vector != DF_VECTOR)
  2728. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2729. GUEST_INTR_STATE_NMI);
  2730. } else if (unlikely(vmx->soft_vnmi_blocked))
  2731. vmx->vnmi_blocked_time +=
  2732. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  2733. idt_vectoring_info = vmx->idt_vectoring_info;
  2734. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  2735. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  2736. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  2737. if (vmx->vcpu.arch.nmi_injected) {
  2738. /*
  2739. * SDM 3: 25.7.1.2
  2740. * Clear bit "block by NMI" before VM entry if a NMI delivery
  2741. * faulted.
  2742. */
  2743. if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
  2744. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  2745. GUEST_INTR_STATE_NMI);
  2746. else
  2747. vmx->vcpu.arch.nmi_injected = false;
  2748. }
  2749. kvm_clear_exception_queue(&vmx->vcpu);
  2750. if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) {
  2751. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  2752. error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
  2753. kvm_queue_exception_e(&vmx->vcpu, vector, error);
  2754. } else
  2755. kvm_queue_exception(&vmx->vcpu, vector);
  2756. vmx->idt_vectoring_info = 0;
  2757. }
  2758. kvm_clear_interrupt_queue(&vmx->vcpu);
  2759. if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
  2760. kvm_queue_interrupt(&vmx->vcpu, vector);
  2761. vmx->idt_vectoring_info = 0;
  2762. }
  2763. }
  2764. static void vmx_intr_assist(struct kvm_vcpu *vcpu)
  2765. {
  2766. update_tpr_threshold(vcpu);
  2767. vmx_update_window_states(vcpu);
  2768. if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
  2769. if (vcpu->arch.interrupt.pending) {
  2770. enable_nmi_window(vcpu);
  2771. } else if (vcpu->arch.nmi_window_open) {
  2772. vcpu->arch.nmi_pending = false;
  2773. vcpu->arch.nmi_injected = true;
  2774. } else {
  2775. enable_nmi_window(vcpu);
  2776. return;
  2777. }
  2778. }
  2779. if (vcpu->arch.nmi_injected) {
  2780. vmx_inject_nmi(vcpu);
  2781. if (vcpu->arch.nmi_pending)
  2782. enable_nmi_window(vcpu);
  2783. else if (kvm_cpu_has_interrupt(vcpu))
  2784. enable_irq_window(vcpu);
  2785. return;
  2786. }
  2787. if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
  2788. if (vcpu->arch.interrupt_window_open)
  2789. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
  2790. else
  2791. enable_irq_window(vcpu);
  2792. }
  2793. if (vcpu->arch.interrupt.pending) {
  2794. vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
  2795. kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr);
  2796. }
  2797. }
  2798. /*
  2799. * Failure to inject an interrupt should give us the information
  2800. * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
  2801. * when fetching the interrupt redirection bitmap in the real-mode
  2802. * tss, this doesn't happen. So we do it ourselves.
  2803. */
  2804. static void fixup_rmode_irq(struct vcpu_vmx *vmx)
  2805. {
  2806. vmx->rmode.irq.pending = 0;
  2807. if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
  2808. return;
  2809. kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
  2810. if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
  2811. vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
  2812. vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
  2813. return;
  2814. }
  2815. vmx->idt_vectoring_info =
  2816. VECTORING_INFO_VALID_MASK
  2817. | INTR_TYPE_EXT_INTR
  2818. | vmx->rmode.irq.vector;
  2819. }
  2820. #ifdef CONFIG_X86_64
  2821. #define R "r"
  2822. #define Q "q"
  2823. #else
  2824. #define R "e"
  2825. #define Q "l"
  2826. #endif
  2827. static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2828. {
  2829. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2830. u32 intr_info;
  2831. /* Record the guest's net vcpu time for enforced NMI injections. */
  2832. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  2833. vmx->entry_time = ktime_get();
  2834. /* Handle invalid guest state instead of entering VMX */
  2835. if (vmx->emulation_required && emulate_invalid_guest_state) {
  2836. handle_invalid_guest_state(vcpu, kvm_run);
  2837. return;
  2838. }
  2839. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  2840. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  2841. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  2842. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  2843. /*
  2844. * Loading guest fpu may have cleared host cr0.ts
  2845. */
  2846. vmcs_writel(HOST_CR0, read_cr0());
  2847. asm(
  2848. /* Store host registers */
  2849. "push %%"R"dx; push %%"R"bp;"
  2850. "push %%"R"cx \n\t"
  2851. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  2852. "je 1f \n\t"
  2853. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  2854. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  2855. "1: \n\t"
  2856. /* Check if vmlaunch of vmresume is needed */
  2857. "cmpl $0, %c[launched](%0) \n\t"
  2858. /* Load guest registers. Don't clobber flags. */
  2859. "mov %c[cr2](%0), %%"R"ax \n\t"
  2860. "mov %%"R"ax, %%cr2 \n\t"
  2861. "mov %c[rax](%0), %%"R"ax \n\t"
  2862. "mov %c[rbx](%0), %%"R"bx \n\t"
  2863. "mov %c[rdx](%0), %%"R"dx \n\t"
  2864. "mov %c[rsi](%0), %%"R"si \n\t"
  2865. "mov %c[rdi](%0), %%"R"di \n\t"
  2866. "mov %c[rbp](%0), %%"R"bp \n\t"
  2867. #ifdef CONFIG_X86_64
  2868. "mov %c[r8](%0), %%r8 \n\t"
  2869. "mov %c[r9](%0), %%r9 \n\t"
  2870. "mov %c[r10](%0), %%r10 \n\t"
  2871. "mov %c[r11](%0), %%r11 \n\t"
  2872. "mov %c[r12](%0), %%r12 \n\t"
  2873. "mov %c[r13](%0), %%r13 \n\t"
  2874. "mov %c[r14](%0), %%r14 \n\t"
  2875. "mov %c[r15](%0), %%r15 \n\t"
  2876. #endif
  2877. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  2878. /* Enter guest mode */
  2879. "jne .Llaunched \n\t"
  2880. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  2881. "jmp .Lkvm_vmx_return \n\t"
  2882. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  2883. ".Lkvm_vmx_return: "
  2884. /* Save guest registers, load host registers, keep flags */
  2885. "xchg %0, (%%"R"sp) \n\t"
  2886. "mov %%"R"ax, %c[rax](%0) \n\t"
  2887. "mov %%"R"bx, %c[rbx](%0) \n\t"
  2888. "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
  2889. "mov %%"R"dx, %c[rdx](%0) \n\t"
  2890. "mov %%"R"si, %c[rsi](%0) \n\t"
  2891. "mov %%"R"di, %c[rdi](%0) \n\t"
  2892. "mov %%"R"bp, %c[rbp](%0) \n\t"
  2893. #ifdef CONFIG_X86_64
  2894. "mov %%r8, %c[r8](%0) \n\t"
  2895. "mov %%r9, %c[r9](%0) \n\t"
  2896. "mov %%r10, %c[r10](%0) \n\t"
  2897. "mov %%r11, %c[r11](%0) \n\t"
  2898. "mov %%r12, %c[r12](%0) \n\t"
  2899. "mov %%r13, %c[r13](%0) \n\t"
  2900. "mov %%r14, %c[r14](%0) \n\t"
  2901. "mov %%r15, %c[r15](%0) \n\t"
  2902. #endif
  2903. "mov %%cr2, %%"R"ax \n\t"
  2904. "mov %%"R"ax, %c[cr2](%0) \n\t"
  2905. "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
  2906. "setbe %c[fail](%0) \n\t"
  2907. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  2908. [launched]"i"(offsetof(struct vcpu_vmx, launched)),
  2909. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  2910. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  2911. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  2912. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  2913. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  2914. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  2915. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  2916. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  2917. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  2918. #ifdef CONFIG_X86_64
  2919. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  2920. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  2921. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  2922. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  2923. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  2924. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  2925. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  2926. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  2927. #endif
  2928. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
  2929. : "cc", "memory"
  2930. , R"bx", R"di", R"si"
  2931. #ifdef CONFIG_X86_64
  2932. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  2933. #endif
  2934. );
  2935. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  2936. vcpu->arch.regs_dirty = 0;
  2937. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  2938. if (vmx->rmode.irq.pending)
  2939. fixup_rmode_irq(vmx);
  2940. vmx_update_window_states(vcpu);
  2941. asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
  2942. vmx->launched = 1;
  2943. intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  2944. /* We need to handle NMIs before interrupts are enabled */
  2945. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  2946. (intr_info & INTR_INFO_VALID_MASK)) {
  2947. KVMTRACE_0D(NMI, vcpu, handler);
  2948. asm("int $2");
  2949. }
  2950. vmx_complete_interrupts(vmx);
  2951. }
  2952. #undef R
  2953. #undef Q
  2954. static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
  2955. {
  2956. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2957. if (vmx->vmcs) {
  2958. vcpu_clear(vmx);
  2959. free_vmcs(vmx->vmcs);
  2960. vmx->vmcs = NULL;
  2961. }
  2962. }
  2963. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  2964. {
  2965. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2966. spin_lock(&vmx_vpid_lock);
  2967. if (vmx->vpid != 0)
  2968. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  2969. spin_unlock(&vmx_vpid_lock);
  2970. vmx_free_vmcs(vcpu);
  2971. kfree(vmx->host_msrs);
  2972. kfree(vmx->guest_msrs);
  2973. kvm_vcpu_uninit(vcpu);
  2974. kmem_cache_free(kvm_vcpu_cache, vmx);
  2975. }
  2976. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  2977. {
  2978. int err;
  2979. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  2980. int cpu;
  2981. if (!vmx)
  2982. return ERR_PTR(-ENOMEM);
  2983. allocate_vpid(vmx);
  2984. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  2985. if (err)
  2986. goto free_vcpu;
  2987. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2988. if (!vmx->guest_msrs) {
  2989. err = -ENOMEM;
  2990. goto uninit_vcpu;
  2991. }
  2992. vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  2993. if (!vmx->host_msrs)
  2994. goto free_guest_msrs;
  2995. vmx->vmcs = alloc_vmcs();
  2996. if (!vmx->vmcs)
  2997. goto free_msrs;
  2998. vmcs_clear(vmx->vmcs);
  2999. cpu = get_cpu();
  3000. vmx_vcpu_load(&vmx->vcpu, cpu);
  3001. err = vmx_vcpu_setup(vmx);
  3002. vmx_vcpu_put(&vmx->vcpu);
  3003. put_cpu();
  3004. if (err)
  3005. goto free_vmcs;
  3006. if (vm_need_virtualize_apic_accesses(kvm))
  3007. if (alloc_apic_access_page(kvm) != 0)
  3008. goto free_vmcs;
  3009. if (vm_need_ept())
  3010. if (alloc_identity_pagetable(kvm) != 0)
  3011. goto free_vmcs;
  3012. return &vmx->vcpu;
  3013. free_vmcs:
  3014. free_vmcs(vmx->vmcs);
  3015. free_msrs:
  3016. kfree(vmx->host_msrs);
  3017. free_guest_msrs:
  3018. kfree(vmx->guest_msrs);
  3019. uninit_vcpu:
  3020. kvm_vcpu_uninit(&vmx->vcpu);
  3021. free_vcpu:
  3022. kmem_cache_free(kvm_vcpu_cache, vmx);
  3023. return ERR_PTR(err);
  3024. }
  3025. static void __init vmx_check_processor_compat(void *rtn)
  3026. {
  3027. struct vmcs_config vmcs_conf;
  3028. *(int *)rtn = 0;
  3029. if (setup_vmcs_config(&vmcs_conf) < 0)
  3030. *(int *)rtn = -EIO;
  3031. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  3032. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  3033. smp_processor_id());
  3034. *(int *)rtn = -EIO;
  3035. }
  3036. }
  3037. static int get_ept_level(void)
  3038. {
  3039. return VMX_EPT_DEFAULT_GAW + 1;
  3040. }
  3041. static struct kvm_x86_ops vmx_x86_ops = {
  3042. .cpu_has_kvm_support = cpu_has_kvm_support,
  3043. .disabled_by_bios = vmx_disabled_by_bios,
  3044. .hardware_setup = hardware_setup,
  3045. .hardware_unsetup = hardware_unsetup,
  3046. .check_processor_compatibility = vmx_check_processor_compat,
  3047. .hardware_enable = hardware_enable,
  3048. .hardware_disable = hardware_disable,
  3049. .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
  3050. .vcpu_create = vmx_create_vcpu,
  3051. .vcpu_free = vmx_free_vcpu,
  3052. .vcpu_reset = vmx_vcpu_reset,
  3053. .prepare_guest_switch = vmx_save_host_state,
  3054. .vcpu_load = vmx_vcpu_load,
  3055. .vcpu_put = vmx_vcpu_put,
  3056. .set_guest_debug = set_guest_debug,
  3057. .guest_debug_pre = kvm_guest_debug_pre,
  3058. .get_msr = vmx_get_msr,
  3059. .set_msr = vmx_set_msr,
  3060. .get_segment_base = vmx_get_segment_base,
  3061. .get_segment = vmx_get_segment,
  3062. .set_segment = vmx_set_segment,
  3063. .get_cpl = vmx_get_cpl,
  3064. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  3065. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  3066. .set_cr0 = vmx_set_cr0,
  3067. .set_cr3 = vmx_set_cr3,
  3068. .set_cr4 = vmx_set_cr4,
  3069. .set_efer = vmx_set_efer,
  3070. .get_idt = vmx_get_idt,
  3071. .set_idt = vmx_set_idt,
  3072. .get_gdt = vmx_get_gdt,
  3073. .set_gdt = vmx_set_gdt,
  3074. .cache_reg = vmx_cache_reg,
  3075. .get_rflags = vmx_get_rflags,
  3076. .set_rflags = vmx_set_rflags,
  3077. .tlb_flush = vmx_flush_tlb,
  3078. .run = vmx_vcpu_run,
  3079. .handle_exit = kvm_handle_exit,
  3080. .skip_emulated_instruction = skip_emulated_instruction,
  3081. .patch_hypercall = vmx_patch_hypercall,
  3082. .get_irq = vmx_get_irq,
  3083. .set_irq = vmx_inject_irq,
  3084. .queue_exception = vmx_queue_exception,
  3085. .exception_injected = vmx_exception_injected,
  3086. .inject_pending_irq = vmx_intr_assist,
  3087. .inject_pending_vectors = do_interrupt_requests,
  3088. .set_tss_addr = vmx_set_tss_addr,
  3089. .get_tdp_level = get_ept_level,
  3090. };
  3091. static int __init vmx_init(void)
  3092. {
  3093. void *va;
  3094. int r;
  3095. vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  3096. if (!vmx_io_bitmap_a)
  3097. return -ENOMEM;
  3098. vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  3099. if (!vmx_io_bitmap_b) {
  3100. r = -ENOMEM;
  3101. goto out;
  3102. }
  3103. vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  3104. if (!vmx_msr_bitmap) {
  3105. r = -ENOMEM;
  3106. goto out1;
  3107. }
  3108. /*
  3109. * Allow direct access to the PC debug port (it is often used for I/O
  3110. * delays, but the vmexits simply slow things down).
  3111. */
  3112. va = kmap(vmx_io_bitmap_a);
  3113. memset(va, 0xff, PAGE_SIZE);
  3114. clear_bit(0x80, va);
  3115. kunmap(vmx_io_bitmap_a);
  3116. va = kmap(vmx_io_bitmap_b);
  3117. memset(va, 0xff, PAGE_SIZE);
  3118. kunmap(vmx_io_bitmap_b);
  3119. va = kmap(vmx_msr_bitmap);
  3120. memset(va, 0xff, PAGE_SIZE);
  3121. kunmap(vmx_msr_bitmap);
  3122. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  3123. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
  3124. if (r)
  3125. goto out2;
  3126. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
  3127. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
  3128. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
  3129. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
  3130. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
  3131. if (vm_need_ept()) {
  3132. bypass_guest_pf = 0;
  3133. kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
  3134. VMX_EPT_WRITABLE_MASK |
  3135. VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT |
  3136. VMX_EPT_IGMT_BIT);
  3137. kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
  3138. VMX_EPT_EXECUTABLE_MASK);
  3139. kvm_enable_tdp();
  3140. } else
  3141. kvm_disable_tdp();
  3142. if (bypass_guest_pf)
  3143. kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
  3144. ept_sync_global();
  3145. return 0;
  3146. out2:
  3147. __free_page(vmx_msr_bitmap);
  3148. out1:
  3149. __free_page(vmx_io_bitmap_b);
  3150. out:
  3151. __free_page(vmx_io_bitmap_a);
  3152. return r;
  3153. }
  3154. static void __exit vmx_exit(void)
  3155. {
  3156. __free_page(vmx_msr_bitmap);
  3157. __free_page(vmx_io_bitmap_b);
  3158. __free_page(vmx_io_bitmap_a);
  3159. kvm_exit();
  3160. }
  3161. module_init(vmx_init)
  3162. module_exit(vmx_exit)