vmx.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732
  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. case MSR_IA32_CR_PAT:
  835. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  836. vmcs_write64(GUEST_IA32_PAT, data);
  837. vcpu->arch.pat = data;
  838. break;
  839. }
  840. /* Otherwise falls through to kvm_set_msr_common */
  841. default:
  842. vmx_load_host_state(vmx);
  843. msr = find_msr_entry(vmx, msr_index);
  844. if (msr) {
  845. msr->data = data;
  846. break;
  847. }
  848. ret = kvm_set_msr_common(vcpu, msr_index, data);
  849. }
  850. return ret;
  851. }
  852. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  853. {
  854. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  855. switch (reg) {
  856. case VCPU_REGS_RSP:
  857. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  858. break;
  859. case VCPU_REGS_RIP:
  860. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  861. break;
  862. default:
  863. break;
  864. }
  865. }
  866. static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  867. {
  868. unsigned long dr7 = 0x400;
  869. int old_singlestep;
  870. old_singlestep = vcpu->guest_debug.singlestep;
  871. vcpu->guest_debug.enabled = dbg->enabled;
  872. if (vcpu->guest_debug.enabled) {
  873. int i;
  874. dr7 |= 0x200; /* exact */
  875. for (i = 0; i < 4; ++i) {
  876. if (!dbg->breakpoints[i].enabled)
  877. continue;
  878. vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
  879. dr7 |= 2 << (i*2); /* global enable */
  880. dr7 |= 0 << (i*4+16); /* execution breakpoint */
  881. }
  882. vcpu->guest_debug.singlestep = dbg->singlestep;
  883. } else
  884. vcpu->guest_debug.singlestep = 0;
  885. if (old_singlestep && !vcpu->guest_debug.singlestep) {
  886. unsigned long flags;
  887. flags = vmcs_readl(GUEST_RFLAGS);
  888. flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  889. vmcs_writel(GUEST_RFLAGS, flags);
  890. }
  891. update_exception_bitmap(vcpu);
  892. vmcs_writel(GUEST_DR7, dr7);
  893. return 0;
  894. }
  895. static int vmx_get_irq(struct kvm_vcpu *vcpu)
  896. {
  897. if (!vcpu->arch.interrupt.pending)
  898. return -1;
  899. return vcpu->arch.interrupt.nr;
  900. }
  901. static __init int cpu_has_kvm_support(void)
  902. {
  903. unsigned long ecx = cpuid_ecx(1);
  904. return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
  905. }
  906. static __init int vmx_disabled_by_bios(void)
  907. {
  908. u64 msr;
  909. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  910. return (msr & (FEATURE_CONTROL_LOCKED |
  911. FEATURE_CONTROL_VMXON_ENABLED))
  912. == FEATURE_CONTROL_LOCKED;
  913. /* locked but not enabled */
  914. }
  915. static void hardware_enable(void *garbage)
  916. {
  917. int cpu = raw_smp_processor_id();
  918. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  919. u64 old;
  920. INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
  921. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  922. if ((old & (FEATURE_CONTROL_LOCKED |
  923. FEATURE_CONTROL_VMXON_ENABLED))
  924. != (FEATURE_CONTROL_LOCKED |
  925. FEATURE_CONTROL_VMXON_ENABLED))
  926. /* enable and lock */
  927. wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
  928. FEATURE_CONTROL_LOCKED |
  929. FEATURE_CONTROL_VMXON_ENABLED);
  930. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  931. asm volatile (ASM_VMX_VMXON_RAX
  932. : : "a"(&phys_addr), "m"(phys_addr)
  933. : "memory", "cc");
  934. }
  935. static void vmclear_local_vcpus(void)
  936. {
  937. int cpu = raw_smp_processor_id();
  938. struct vcpu_vmx *vmx, *n;
  939. list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
  940. local_vcpus_link)
  941. __vcpu_clear(vmx);
  942. }
  943. static void hardware_disable(void *garbage)
  944. {
  945. vmclear_local_vcpus();
  946. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  947. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  948. }
  949. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  950. u32 msr, u32 *result)
  951. {
  952. u32 vmx_msr_low, vmx_msr_high;
  953. u32 ctl = ctl_min | ctl_opt;
  954. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  955. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  956. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  957. /* Ensure minimum (required) set of control bits are supported. */
  958. if (ctl_min & ~ctl)
  959. return -EIO;
  960. *result = ctl;
  961. return 0;
  962. }
  963. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  964. {
  965. u32 vmx_msr_low, vmx_msr_high;
  966. u32 min, opt, min2, opt2;
  967. u32 _pin_based_exec_control = 0;
  968. u32 _cpu_based_exec_control = 0;
  969. u32 _cpu_based_2nd_exec_control = 0;
  970. u32 _vmexit_control = 0;
  971. u32 _vmentry_control = 0;
  972. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  973. opt = PIN_BASED_VIRTUAL_NMIS;
  974. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  975. &_pin_based_exec_control) < 0)
  976. return -EIO;
  977. min = CPU_BASED_HLT_EXITING |
  978. #ifdef CONFIG_X86_64
  979. CPU_BASED_CR8_LOAD_EXITING |
  980. CPU_BASED_CR8_STORE_EXITING |
  981. #endif
  982. CPU_BASED_CR3_LOAD_EXITING |
  983. CPU_BASED_CR3_STORE_EXITING |
  984. CPU_BASED_USE_IO_BITMAPS |
  985. CPU_BASED_MOV_DR_EXITING |
  986. CPU_BASED_USE_TSC_OFFSETING |
  987. CPU_BASED_INVLPG_EXITING;
  988. opt = CPU_BASED_TPR_SHADOW |
  989. CPU_BASED_USE_MSR_BITMAPS |
  990. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  991. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  992. &_cpu_based_exec_control) < 0)
  993. return -EIO;
  994. #ifdef CONFIG_X86_64
  995. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  996. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  997. ~CPU_BASED_CR8_STORE_EXITING;
  998. #endif
  999. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  1000. min2 = 0;
  1001. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  1002. SECONDARY_EXEC_WBINVD_EXITING |
  1003. SECONDARY_EXEC_ENABLE_VPID |
  1004. SECONDARY_EXEC_ENABLE_EPT;
  1005. if (adjust_vmx_controls(min2, opt2,
  1006. MSR_IA32_VMX_PROCBASED_CTLS2,
  1007. &_cpu_based_2nd_exec_control) < 0)
  1008. return -EIO;
  1009. }
  1010. #ifndef CONFIG_X86_64
  1011. if (!(_cpu_based_2nd_exec_control &
  1012. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  1013. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  1014. #endif
  1015. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  1016. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  1017. enabled */
  1018. min &= ~(CPU_BASED_CR3_LOAD_EXITING |
  1019. CPU_BASED_CR3_STORE_EXITING |
  1020. CPU_BASED_INVLPG_EXITING);
  1021. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  1022. &_cpu_based_exec_control) < 0)
  1023. return -EIO;
  1024. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  1025. vmx_capability.ept, vmx_capability.vpid);
  1026. }
  1027. min = 0;
  1028. #ifdef CONFIG_X86_64
  1029. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1030. #endif
  1031. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
  1032. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  1033. &_vmexit_control) < 0)
  1034. return -EIO;
  1035. min = 0;
  1036. opt = VM_ENTRY_LOAD_IA32_PAT;
  1037. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  1038. &_vmentry_control) < 0)
  1039. return -EIO;
  1040. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  1041. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  1042. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  1043. return -EIO;
  1044. #ifdef CONFIG_X86_64
  1045. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  1046. if (vmx_msr_high & (1u<<16))
  1047. return -EIO;
  1048. #endif
  1049. /* Require Write-Back (WB) memory type for VMCS accesses. */
  1050. if (((vmx_msr_high >> 18) & 15) != 6)
  1051. return -EIO;
  1052. vmcs_conf->size = vmx_msr_high & 0x1fff;
  1053. vmcs_conf->order = get_order(vmcs_config.size);
  1054. vmcs_conf->revision_id = vmx_msr_low;
  1055. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  1056. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  1057. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  1058. vmcs_conf->vmexit_ctrl = _vmexit_control;
  1059. vmcs_conf->vmentry_ctrl = _vmentry_control;
  1060. return 0;
  1061. }
  1062. static struct vmcs *alloc_vmcs_cpu(int cpu)
  1063. {
  1064. int node = cpu_to_node(cpu);
  1065. struct page *pages;
  1066. struct vmcs *vmcs;
  1067. pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
  1068. if (!pages)
  1069. return NULL;
  1070. vmcs = page_address(pages);
  1071. memset(vmcs, 0, vmcs_config.size);
  1072. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  1073. return vmcs;
  1074. }
  1075. static struct vmcs *alloc_vmcs(void)
  1076. {
  1077. return alloc_vmcs_cpu(raw_smp_processor_id());
  1078. }
  1079. static void free_vmcs(struct vmcs *vmcs)
  1080. {
  1081. free_pages((unsigned long)vmcs, vmcs_config.order);
  1082. }
  1083. static void free_kvm_area(void)
  1084. {
  1085. int cpu;
  1086. for_each_online_cpu(cpu)
  1087. free_vmcs(per_cpu(vmxarea, cpu));
  1088. }
  1089. static __init int alloc_kvm_area(void)
  1090. {
  1091. int cpu;
  1092. for_each_online_cpu(cpu) {
  1093. struct vmcs *vmcs;
  1094. vmcs = alloc_vmcs_cpu(cpu);
  1095. if (!vmcs) {
  1096. free_kvm_area();
  1097. return -ENOMEM;
  1098. }
  1099. per_cpu(vmxarea, cpu) = vmcs;
  1100. }
  1101. return 0;
  1102. }
  1103. static __init int hardware_setup(void)
  1104. {
  1105. if (setup_vmcs_config(&vmcs_config) < 0)
  1106. return -EIO;
  1107. if (boot_cpu_has(X86_FEATURE_NX))
  1108. kvm_enable_efer_bits(EFER_NX);
  1109. return alloc_kvm_area();
  1110. }
  1111. static __exit void hardware_unsetup(void)
  1112. {
  1113. free_kvm_area();
  1114. }
  1115. static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
  1116. {
  1117. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1118. if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
  1119. vmcs_write16(sf->selector, save->selector);
  1120. vmcs_writel(sf->base, save->base);
  1121. vmcs_write32(sf->limit, save->limit);
  1122. vmcs_write32(sf->ar_bytes, save->ar);
  1123. } else {
  1124. u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
  1125. << AR_DPL_SHIFT;
  1126. vmcs_write32(sf->ar_bytes, 0x93 | dpl);
  1127. }
  1128. }
  1129. static void enter_pmode(struct kvm_vcpu *vcpu)
  1130. {
  1131. unsigned long flags;
  1132. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1133. vmx->emulation_required = 1;
  1134. vcpu->arch.rmode.active = 0;
  1135. vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
  1136. vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
  1137. vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
  1138. flags = vmcs_readl(GUEST_RFLAGS);
  1139. flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
  1140. flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
  1141. vmcs_writel(GUEST_RFLAGS, flags);
  1142. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  1143. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  1144. update_exception_bitmap(vcpu);
  1145. if (emulate_invalid_guest_state)
  1146. return;
  1147. fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
  1148. fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
  1149. fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
  1150. fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
  1151. vmcs_write16(GUEST_SS_SELECTOR, 0);
  1152. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  1153. vmcs_write16(GUEST_CS_SELECTOR,
  1154. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  1155. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  1156. }
  1157. static gva_t rmode_tss_base(struct kvm *kvm)
  1158. {
  1159. if (!kvm->arch.tss_addr) {
  1160. gfn_t base_gfn = kvm->memslots[0].base_gfn +
  1161. kvm->memslots[0].npages - 3;
  1162. return base_gfn << PAGE_SHIFT;
  1163. }
  1164. return kvm->arch.tss_addr;
  1165. }
  1166. static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
  1167. {
  1168. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1169. save->selector = vmcs_read16(sf->selector);
  1170. save->base = vmcs_readl(sf->base);
  1171. save->limit = vmcs_read32(sf->limit);
  1172. save->ar = vmcs_read32(sf->ar_bytes);
  1173. vmcs_write16(sf->selector, save->base >> 4);
  1174. vmcs_write32(sf->base, save->base & 0xfffff);
  1175. vmcs_write32(sf->limit, 0xffff);
  1176. vmcs_write32(sf->ar_bytes, 0xf3);
  1177. }
  1178. static void enter_rmode(struct kvm_vcpu *vcpu)
  1179. {
  1180. unsigned long flags;
  1181. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1182. vmx->emulation_required = 1;
  1183. vcpu->arch.rmode.active = 1;
  1184. vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
  1185. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  1186. vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
  1187. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  1188. vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1189. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1190. flags = vmcs_readl(GUEST_RFLAGS);
  1191. vcpu->arch.rmode.save_iopl
  1192. = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
  1193. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1194. vmcs_writel(GUEST_RFLAGS, flags);
  1195. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  1196. update_exception_bitmap(vcpu);
  1197. if (emulate_invalid_guest_state)
  1198. goto continue_rmode;
  1199. vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
  1200. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  1201. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  1202. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  1203. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  1204. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  1205. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  1206. vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
  1207. fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
  1208. fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
  1209. fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
  1210. fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
  1211. continue_rmode:
  1212. kvm_mmu_reset_context(vcpu);
  1213. init_rmode(vcpu->kvm);
  1214. }
  1215. #ifdef CONFIG_X86_64
  1216. static void enter_lmode(struct kvm_vcpu *vcpu)
  1217. {
  1218. u32 guest_tr_ar;
  1219. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1220. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  1221. printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
  1222. __func__);
  1223. vmcs_write32(GUEST_TR_AR_BYTES,
  1224. (guest_tr_ar & ~AR_TYPE_MASK)
  1225. | AR_TYPE_BUSY_64_TSS);
  1226. }
  1227. vcpu->arch.shadow_efer |= EFER_LMA;
  1228. find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
  1229. vmcs_write32(VM_ENTRY_CONTROLS,
  1230. vmcs_read32(VM_ENTRY_CONTROLS)
  1231. | VM_ENTRY_IA32E_MODE);
  1232. }
  1233. static void exit_lmode(struct kvm_vcpu *vcpu)
  1234. {
  1235. vcpu->arch.shadow_efer &= ~EFER_LMA;
  1236. vmcs_write32(VM_ENTRY_CONTROLS,
  1237. vmcs_read32(VM_ENTRY_CONTROLS)
  1238. & ~VM_ENTRY_IA32E_MODE);
  1239. }
  1240. #endif
  1241. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  1242. {
  1243. vpid_sync_vcpu_all(to_vmx(vcpu));
  1244. if (vm_need_ept())
  1245. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  1246. }
  1247. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1248. {
  1249. vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
  1250. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
  1251. }
  1252. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  1253. {
  1254. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1255. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1256. printk(KERN_ERR "EPT: Fail to load pdptrs!\n");
  1257. return;
  1258. }
  1259. vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
  1260. vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
  1261. vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
  1262. vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
  1263. }
  1264. }
  1265. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  1266. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  1267. unsigned long cr0,
  1268. struct kvm_vcpu *vcpu)
  1269. {
  1270. if (!(cr0 & X86_CR0_PG)) {
  1271. /* From paging/starting to nonpaging */
  1272. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1273. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  1274. (CPU_BASED_CR3_LOAD_EXITING |
  1275. CPU_BASED_CR3_STORE_EXITING));
  1276. vcpu->arch.cr0 = cr0;
  1277. vmx_set_cr4(vcpu, vcpu->arch.cr4);
  1278. *hw_cr0 |= X86_CR0_PE | X86_CR0_PG;
  1279. *hw_cr0 &= ~X86_CR0_WP;
  1280. } else if (!is_paging(vcpu)) {
  1281. /* From nonpaging to paging */
  1282. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1283. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  1284. ~(CPU_BASED_CR3_LOAD_EXITING |
  1285. CPU_BASED_CR3_STORE_EXITING));
  1286. vcpu->arch.cr0 = cr0;
  1287. vmx_set_cr4(vcpu, vcpu->arch.cr4);
  1288. if (!(vcpu->arch.cr0 & X86_CR0_WP))
  1289. *hw_cr0 &= ~X86_CR0_WP;
  1290. }
  1291. }
  1292. static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
  1293. struct kvm_vcpu *vcpu)
  1294. {
  1295. if (!is_paging(vcpu)) {
  1296. *hw_cr4 &= ~X86_CR4_PAE;
  1297. *hw_cr4 |= X86_CR4_PSE;
  1298. } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
  1299. *hw_cr4 &= ~X86_CR4_PAE;
  1300. }
  1301. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1302. {
  1303. unsigned long hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) |
  1304. KVM_VM_CR0_ALWAYS_ON;
  1305. vmx_fpu_deactivate(vcpu);
  1306. if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
  1307. enter_pmode(vcpu);
  1308. if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
  1309. enter_rmode(vcpu);
  1310. #ifdef CONFIG_X86_64
  1311. if (vcpu->arch.shadow_efer & EFER_LME) {
  1312. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  1313. enter_lmode(vcpu);
  1314. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  1315. exit_lmode(vcpu);
  1316. }
  1317. #endif
  1318. if (vm_need_ept())
  1319. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  1320. vmcs_writel(CR0_READ_SHADOW, cr0);
  1321. vmcs_writel(GUEST_CR0, hw_cr0);
  1322. vcpu->arch.cr0 = cr0;
  1323. if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
  1324. vmx_fpu_activate(vcpu);
  1325. }
  1326. static u64 construct_eptp(unsigned long root_hpa)
  1327. {
  1328. u64 eptp;
  1329. /* TODO write the value reading from MSR */
  1330. eptp = VMX_EPT_DEFAULT_MT |
  1331. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  1332. eptp |= (root_hpa & PAGE_MASK);
  1333. return eptp;
  1334. }
  1335. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  1336. {
  1337. unsigned long guest_cr3;
  1338. u64 eptp;
  1339. guest_cr3 = cr3;
  1340. if (vm_need_ept()) {
  1341. eptp = construct_eptp(cr3);
  1342. vmcs_write64(EPT_POINTER, eptp);
  1343. ept_sync_context(eptp);
  1344. ept_load_pdptrs(vcpu);
  1345. guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
  1346. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  1347. }
  1348. vmx_flush_tlb(vcpu);
  1349. vmcs_writel(GUEST_CR3, guest_cr3);
  1350. if (vcpu->arch.cr0 & X86_CR0_PE)
  1351. vmx_fpu_deactivate(vcpu);
  1352. }
  1353. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1354. {
  1355. unsigned long hw_cr4 = cr4 | (vcpu->arch.rmode.active ?
  1356. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  1357. vcpu->arch.cr4 = cr4;
  1358. if (vm_need_ept())
  1359. ept_update_paging_mode_cr4(&hw_cr4, vcpu);
  1360. vmcs_writel(CR4_READ_SHADOW, cr4);
  1361. vmcs_writel(GUEST_CR4, hw_cr4);
  1362. }
  1363. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1364. {
  1365. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1366. struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  1367. vcpu->arch.shadow_efer = efer;
  1368. if (!msr)
  1369. return;
  1370. if (efer & EFER_LMA) {
  1371. vmcs_write32(VM_ENTRY_CONTROLS,
  1372. vmcs_read32(VM_ENTRY_CONTROLS) |
  1373. VM_ENTRY_IA32E_MODE);
  1374. msr->data = efer;
  1375. } else {
  1376. vmcs_write32(VM_ENTRY_CONTROLS,
  1377. vmcs_read32(VM_ENTRY_CONTROLS) &
  1378. ~VM_ENTRY_IA32E_MODE);
  1379. msr->data = efer & ~EFER_LME;
  1380. }
  1381. setup_msrs(vmx);
  1382. }
  1383. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1384. {
  1385. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1386. return vmcs_readl(sf->base);
  1387. }
  1388. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1389. struct kvm_segment *var, int seg)
  1390. {
  1391. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1392. u32 ar;
  1393. var->base = vmcs_readl(sf->base);
  1394. var->limit = vmcs_read32(sf->limit);
  1395. var->selector = vmcs_read16(sf->selector);
  1396. ar = vmcs_read32(sf->ar_bytes);
  1397. if (ar & AR_UNUSABLE_MASK)
  1398. ar = 0;
  1399. var->type = ar & 15;
  1400. var->s = (ar >> 4) & 1;
  1401. var->dpl = (ar >> 5) & 3;
  1402. var->present = (ar >> 7) & 1;
  1403. var->avl = (ar >> 12) & 1;
  1404. var->l = (ar >> 13) & 1;
  1405. var->db = (ar >> 14) & 1;
  1406. var->g = (ar >> 15) & 1;
  1407. var->unusable = (ar >> 16) & 1;
  1408. }
  1409. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  1410. {
  1411. struct kvm_segment kvm_seg;
  1412. if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
  1413. return 0;
  1414. if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
  1415. return 3;
  1416. vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
  1417. return kvm_seg.selector & 3;
  1418. }
  1419. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  1420. {
  1421. u32 ar;
  1422. if (var->unusable)
  1423. ar = 1 << 16;
  1424. else {
  1425. ar = var->type & 15;
  1426. ar |= (var->s & 1) << 4;
  1427. ar |= (var->dpl & 3) << 5;
  1428. ar |= (var->present & 1) << 7;
  1429. ar |= (var->avl & 1) << 12;
  1430. ar |= (var->l & 1) << 13;
  1431. ar |= (var->db & 1) << 14;
  1432. ar |= (var->g & 1) << 15;
  1433. }
  1434. if (ar == 0) /* a 0 value means unusable */
  1435. ar = AR_UNUSABLE_MASK;
  1436. return ar;
  1437. }
  1438. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1439. struct kvm_segment *var, int seg)
  1440. {
  1441. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1442. u32 ar;
  1443. if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
  1444. vcpu->arch.rmode.tr.selector = var->selector;
  1445. vcpu->arch.rmode.tr.base = var->base;
  1446. vcpu->arch.rmode.tr.limit = var->limit;
  1447. vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
  1448. return;
  1449. }
  1450. vmcs_writel(sf->base, var->base);
  1451. vmcs_write32(sf->limit, var->limit);
  1452. vmcs_write16(sf->selector, var->selector);
  1453. if (vcpu->arch.rmode.active && var->s) {
  1454. /*
  1455. * Hack real-mode segments into vm86 compatibility.
  1456. */
  1457. if (var->base == 0xffff0000 && var->selector == 0xf000)
  1458. vmcs_writel(sf->base, 0xf0000);
  1459. ar = 0xf3;
  1460. } else
  1461. ar = vmx_segment_access_rights(var);
  1462. vmcs_write32(sf->ar_bytes, ar);
  1463. }
  1464. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  1465. {
  1466. u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
  1467. *db = (ar >> 14) & 1;
  1468. *l = (ar >> 13) & 1;
  1469. }
  1470. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1471. {
  1472. dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
  1473. dt->base = vmcs_readl(GUEST_IDTR_BASE);
  1474. }
  1475. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1476. {
  1477. vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
  1478. vmcs_writel(GUEST_IDTR_BASE, dt->base);
  1479. }
  1480. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1481. {
  1482. dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
  1483. dt->base = vmcs_readl(GUEST_GDTR_BASE);
  1484. }
  1485. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  1486. {
  1487. vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
  1488. vmcs_writel(GUEST_GDTR_BASE, dt->base);
  1489. }
  1490. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1491. {
  1492. struct kvm_segment var;
  1493. u32 ar;
  1494. vmx_get_segment(vcpu, &var, seg);
  1495. ar = vmx_segment_access_rights(&var);
  1496. if (var.base != (var.selector << 4))
  1497. return false;
  1498. if (var.limit != 0xffff)
  1499. return false;
  1500. if (ar != 0xf3)
  1501. return false;
  1502. return true;
  1503. }
  1504. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  1505. {
  1506. struct kvm_segment cs;
  1507. unsigned int cs_rpl;
  1508. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1509. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  1510. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  1511. return false;
  1512. if (!cs.s)
  1513. return false;
  1514. if (!(~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK))) {
  1515. if (cs.dpl > cs_rpl)
  1516. return false;
  1517. } else if (cs.type & AR_TYPE_CODE_MASK) {
  1518. if (cs.dpl != cs_rpl)
  1519. return false;
  1520. }
  1521. if (!cs.present)
  1522. return false;
  1523. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  1524. return true;
  1525. }
  1526. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  1527. {
  1528. struct kvm_segment ss;
  1529. unsigned int ss_rpl;
  1530. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  1531. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  1532. if ((ss.type != 3) || (ss.type != 7))
  1533. return false;
  1534. if (!ss.s)
  1535. return false;
  1536. if (ss.dpl != ss_rpl) /* DPL != RPL */
  1537. return false;
  1538. if (!ss.present)
  1539. return false;
  1540. return true;
  1541. }
  1542. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1543. {
  1544. struct kvm_segment var;
  1545. unsigned int rpl;
  1546. vmx_get_segment(vcpu, &var, seg);
  1547. rpl = var.selector & SELECTOR_RPL_MASK;
  1548. if (!var.s)
  1549. return false;
  1550. if (!var.present)
  1551. return false;
  1552. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  1553. if (var.dpl < rpl) /* DPL < RPL */
  1554. return false;
  1555. }
  1556. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  1557. * rights flags
  1558. */
  1559. return true;
  1560. }
  1561. static bool tr_valid(struct kvm_vcpu *vcpu)
  1562. {
  1563. struct kvm_segment tr;
  1564. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  1565. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  1566. return false;
  1567. if ((tr.type != 3) || (tr.type != 11)) /* TODO: Check if guest is in IA32e mode */
  1568. return false;
  1569. if (!tr.present)
  1570. return false;
  1571. return true;
  1572. }
  1573. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  1574. {
  1575. struct kvm_segment ldtr;
  1576. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  1577. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  1578. return false;
  1579. if (ldtr.type != 2)
  1580. return false;
  1581. if (!ldtr.present)
  1582. return false;
  1583. return true;
  1584. }
  1585. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  1586. {
  1587. struct kvm_segment cs, ss;
  1588. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1589. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  1590. return ((cs.selector & SELECTOR_RPL_MASK) ==
  1591. (ss.selector & SELECTOR_RPL_MASK));
  1592. }
  1593. /*
  1594. * Check if guest state is valid. Returns true if valid, false if
  1595. * not.
  1596. * We assume that registers are always usable
  1597. */
  1598. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  1599. {
  1600. /* real mode guest state checks */
  1601. if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
  1602. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  1603. return false;
  1604. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  1605. return false;
  1606. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  1607. return false;
  1608. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  1609. return false;
  1610. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  1611. return false;
  1612. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  1613. return false;
  1614. } else {
  1615. /* protected mode guest state checks */
  1616. if (!cs_ss_rpl_check(vcpu))
  1617. return false;
  1618. if (!code_segment_valid(vcpu))
  1619. return false;
  1620. if (!stack_segment_valid(vcpu))
  1621. return false;
  1622. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  1623. return false;
  1624. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  1625. return false;
  1626. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  1627. return false;
  1628. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  1629. return false;
  1630. if (!tr_valid(vcpu))
  1631. return false;
  1632. if (!ldtr_valid(vcpu))
  1633. return false;
  1634. }
  1635. /* TODO:
  1636. * - Add checks on RIP
  1637. * - Add checks on RFLAGS
  1638. */
  1639. return true;
  1640. }
  1641. static int init_rmode_tss(struct kvm *kvm)
  1642. {
  1643. gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  1644. u16 data = 0;
  1645. int ret = 0;
  1646. int r;
  1647. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  1648. if (r < 0)
  1649. goto out;
  1650. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  1651. r = kvm_write_guest_page(kvm, fn++, &data,
  1652. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  1653. if (r < 0)
  1654. goto out;
  1655. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  1656. if (r < 0)
  1657. goto out;
  1658. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  1659. if (r < 0)
  1660. goto out;
  1661. data = ~0;
  1662. r = kvm_write_guest_page(kvm, fn, &data,
  1663. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  1664. sizeof(u8));
  1665. if (r < 0)
  1666. goto out;
  1667. ret = 1;
  1668. out:
  1669. return ret;
  1670. }
  1671. static int init_rmode_identity_map(struct kvm *kvm)
  1672. {
  1673. int i, r, ret;
  1674. pfn_t identity_map_pfn;
  1675. u32 tmp;
  1676. if (!vm_need_ept())
  1677. return 1;
  1678. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  1679. printk(KERN_ERR "EPT: identity-mapping pagetable "
  1680. "haven't been allocated!\n");
  1681. return 0;
  1682. }
  1683. if (likely(kvm->arch.ept_identity_pagetable_done))
  1684. return 1;
  1685. ret = 0;
  1686. identity_map_pfn = VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT;
  1687. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  1688. if (r < 0)
  1689. goto out;
  1690. /* Set up identity-mapping pagetable for EPT in real mode */
  1691. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  1692. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  1693. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  1694. r = kvm_write_guest_page(kvm, identity_map_pfn,
  1695. &tmp, i * sizeof(tmp), sizeof(tmp));
  1696. if (r < 0)
  1697. goto out;
  1698. }
  1699. kvm->arch.ept_identity_pagetable_done = true;
  1700. ret = 1;
  1701. out:
  1702. return ret;
  1703. }
  1704. static void seg_setup(int seg)
  1705. {
  1706. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1707. vmcs_write16(sf->selector, 0);
  1708. vmcs_writel(sf->base, 0);
  1709. vmcs_write32(sf->limit, 0xffff);
  1710. vmcs_write32(sf->ar_bytes, 0xf3);
  1711. }
  1712. static int alloc_apic_access_page(struct kvm *kvm)
  1713. {
  1714. struct kvm_userspace_memory_region kvm_userspace_mem;
  1715. int r = 0;
  1716. down_write(&kvm->slots_lock);
  1717. if (kvm->arch.apic_access_page)
  1718. goto out;
  1719. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  1720. kvm_userspace_mem.flags = 0;
  1721. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  1722. kvm_userspace_mem.memory_size = PAGE_SIZE;
  1723. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1724. if (r)
  1725. goto out;
  1726. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  1727. out:
  1728. up_write(&kvm->slots_lock);
  1729. return r;
  1730. }
  1731. static int alloc_identity_pagetable(struct kvm *kvm)
  1732. {
  1733. struct kvm_userspace_memory_region kvm_userspace_mem;
  1734. int r = 0;
  1735. down_write(&kvm->slots_lock);
  1736. if (kvm->arch.ept_identity_pagetable)
  1737. goto out;
  1738. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  1739. kvm_userspace_mem.flags = 0;
  1740. kvm_userspace_mem.guest_phys_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  1741. kvm_userspace_mem.memory_size = PAGE_SIZE;
  1742. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1743. if (r)
  1744. goto out;
  1745. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  1746. VMX_EPT_IDENTITY_PAGETABLE_ADDR >> PAGE_SHIFT);
  1747. out:
  1748. up_write(&kvm->slots_lock);
  1749. return r;
  1750. }
  1751. static void allocate_vpid(struct vcpu_vmx *vmx)
  1752. {
  1753. int vpid;
  1754. vmx->vpid = 0;
  1755. if (!enable_vpid || !cpu_has_vmx_vpid())
  1756. return;
  1757. spin_lock(&vmx_vpid_lock);
  1758. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  1759. if (vpid < VMX_NR_VPIDS) {
  1760. vmx->vpid = vpid;
  1761. __set_bit(vpid, vmx_vpid_bitmap);
  1762. }
  1763. spin_unlock(&vmx_vpid_lock);
  1764. }
  1765. static void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
  1766. {
  1767. void *va;
  1768. if (!cpu_has_vmx_msr_bitmap())
  1769. return;
  1770. /*
  1771. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  1772. * have the write-low and read-high bitmap offsets the wrong way round.
  1773. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  1774. */
  1775. va = kmap(msr_bitmap);
  1776. if (msr <= 0x1fff) {
  1777. __clear_bit(msr, va + 0x000); /* read-low */
  1778. __clear_bit(msr, va + 0x800); /* write-low */
  1779. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  1780. msr &= 0x1fff;
  1781. __clear_bit(msr, va + 0x400); /* read-high */
  1782. __clear_bit(msr, va + 0xc00); /* write-high */
  1783. }
  1784. kunmap(msr_bitmap);
  1785. }
  1786. /*
  1787. * Sets up the vmcs for emulated real mode.
  1788. */
  1789. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  1790. {
  1791. u32 host_sysenter_cs, msr_low, msr_high;
  1792. u32 junk;
  1793. u64 host_pat;
  1794. unsigned long a;
  1795. struct descriptor_table dt;
  1796. int i;
  1797. unsigned long kvm_vmx_return;
  1798. u32 exec_control;
  1799. /* I/O */
  1800. vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
  1801. vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
  1802. if (cpu_has_vmx_msr_bitmap())
  1803. vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
  1804. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  1805. /* Control */
  1806. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  1807. vmcs_config.pin_based_exec_ctrl);
  1808. exec_control = vmcs_config.cpu_based_exec_ctrl;
  1809. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  1810. exec_control &= ~CPU_BASED_TPR_SHADOW;
  1811. #ifdef CONFIG_X86_64
  1812. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  1813. CPU_BASED_CR8_LOAD_EXITING;
  1814. #endif
  1815. }
  1816. if (!vm_need_ept())
  1817. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  1818. CPU_BASED_CR3_LOAD_EXITING |
  1819. CPU_BASED_INVLPG_EXITING;
  1820. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  1821. if (cpu_has_secondary_exec_ctrls()) {
  1822. exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  1823. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  1824. exec_control &=
  1825. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  1826. if (vmx->vpid == 0)
  1827. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  1828. if (!vm_need_ept())
  1829. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  1830. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  1831. }
  1832. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
  1833. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
  1834. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  1835. vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
  1836. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  1837. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  1838. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  1839. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  1840. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  1841. vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
  1842. vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
  1843. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  1844. #ifdef CONFIG_X86_64
  1845. rdmsrl(MSR_FS_BASE, a);
  1846. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  1847. rdmsrl(MSR_GS_BASE, a);
  1848. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  1849. #else
  1850. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  1851. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  1852. #endif
  1853. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  1854. kvm_get_idt(&dt);
  1855. vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
  1856. asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
  1857. vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
  1858. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  1859. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  1860. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  1861. rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
  1862. vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
  1863. rdmsrl(MSR_IA32_SYSENTER_ESP, a);
  1864. vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
  1865. rdmsrl(MSR_IA32_SYSENTER_EIP, a);
  1866. vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
  1867. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  1868. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  1869. host_pat = msr_low | ((u64) msr_high << 32);
  1870. vmcs_write64(HOST_IA32_PAT, host_pat);
  1871. }
  1872. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  1873. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  1874. host_pat = msr_low | ((u64) msr_high << 32);
  1875. /* Write the default value follow host pat */
  1876. vmcs_write64(GUEST_IA32_PAT, host_pat);
  1877. /* Keep arch.pat sync with GUEST_IA32_PAT */
  1878. vmx->vcpu.arch.pat = host_pat;
  1879. }
  1880. for (i = 0; i < NR_VMX_MSR; ++i) {
  1881. u32 index = vmx_msr_index[i];
  1882. u32 data_low, data_high;
  1883. u64 data;
  1884. int j = vmx->nmsrs;
  1885. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  1886. continue;
  1887. if (wrmsr_safe(index, data_low, data_high) < 0)
  1888. continue;
  1889. data = data_low | ((u64)data_high << 32);
  1890. vmx->host_msrs[j].index = index;
  1891. vmx->host_msrs[j].reserved = 0;
  1892. vmx->host_msrs[j].data = data;
  1893. vmx->guest_msrs[j] = vmx->host_msrs[j];
  1894. ++vmx->nmsrs;
  1895. }
  1896. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  1897. /* 22.2.1, 20.8.1 */
  1898. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  1899. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  1900. vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
  1901. return 0;
  1902. }
  1903. static int init_rmode(struct kvm *kvm)
  1904. {
  1905. if (!init_rmode_tss(kvm))
  1906. return 0;
  1907. if (!init_rmode_identity_map(kvm))
  1908. return 0;
  1909. return 1;
  1910. }
  1911. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  1912. {
  1913. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1914. u64 msr;
  1915. int ret;
  1916. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  1917. down_read(&vcpu->kvm->slots_lock);
  1918. if (!init_rmode(vmx->vcpu.kvm)) {
  1919. ret = -ENOMEM;
  1920. goto out;
  1921. }
  1922. vmx->vcpu.arch.rmode.active = 0;
  1923. vmx->soft_vnmi_blocked = 0;
  1924. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  1925. kvm_set_cr8(&vmx->vcpu, 0);
  1926. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  1927. if (vmx->vcpu.vcpu_id == 0)
  1928. msr |= MSR_IA32_APICBASE_BSP;
  1929. kvm_set_apic_base(&vmx->vcpu, msr);
  1930. fx_init(&vmx->vcpu);
  1931. seg_setup(VCPU_SREG_CS);
  1932. /*
  1933. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  1934. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  1935. */
  1936. if (vmx->vcpu.vcpu_id == 0) {
  1937. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  1938. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  1939. } else {
  1940. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  1941. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  1942. }
  1943. seg_setup(VCPU_SREG_DS);
  1944. seg_setup(VCPU_SREG_ES);
  1945. seg_setup(VCPU_SREG_FS);
  1946. seg_setup(VCPU_SREG_GS);
  1947. seg_setup(VCPU_SREG_SS);
  1948. vmcs_write16(GUEST_TR_SELECTOR, 0);
  1949. vmcs_writel(GUEST_TR_BASE, 0);
  1950. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  1951. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1952. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  1953. vmcs_writel(GUEST_LDTR_BASE, 0);
  1954. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  1955. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  1956. vmcs_write32(GUEST_SYSENTER_CS, 0);
  1957. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  1958. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  1959. vmcs_writel(GUEST_RFLAGS, 0x02);
  1960. if (vmx->vcpu.vcpu_id == 0)
  1961. kvm_rip_write(vcpu, 0xfff0);
  1962. else
  1963. kvm_rip_write(vcpu, 0);
  1964. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  1965. /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
  1966. vmcs_writel(GUEST_DR7, 0x400);
  1967. vmcs_writel(GUEST_GDTR_BASE, 0);
  1968. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  1969. vmcs_writel(GUEST_IDTR_BASE, 0);
  1970. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  1971. vmcs_write32(GUEST_ACTIVITY_STATE, 0);
  1972. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  1973. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  1974. guest_write_tsc(0);
  1975. /* Special registers */
  1976. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  1977. setup_msrs(vmx);
  1978. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  1979. if (cpu_has_vmx_tpr_shadow()) {
  1980. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  1981. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  1982. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  1983. page_to_phys(vmx->vcpu.arch.apic->regs_page));
  1984. vmcs_write32(TPR_THRESHOLD, 0);
  1985. }
  1986. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  1987. vmcs_write64(APIC_ACCESS_ADDR,
  1988. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  1989. if (vmx->vpid != 0)
  1990. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  1991. vmx->vcpu.arch.cr0 = 0x60000010;
  1992. vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
  1993. vmx_set_cr4(&vmx->vcpu, 0);
  1994. vmx_set_efer(&vmx->vcpu, 0);
  1995. vmx_fpu_activate(&vmx->vcpu);
  1996. update_exception_bitmap(&vmx->vcpu);
  1997. vpid_sync_vcpu_all(vmx);
  1998. ret = 0;
  1999. /* HACK: Don't enable emulation on guest boot/reset */
  2000. vmx->emulation_required = 0;
  2001. out:
  2002. up_read(&vcpu->kvm->slots_lock);
  2003. return ret;
  2004. }
  2005. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2006. {
  2007. u32 cpu_based_vm_exec_control;
  2008. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2009. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  2010. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2011. }
  2012. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2013. {
  2014. u32 cpu_based_vm_exec_control;
  2015. if (!cpu_has_virtual_nmis()) {
  2016. enable_irq_window(vcpu);
  2017. return;
  2018. }
  2019. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2020. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  2021. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2022. }
  2023. static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
  2024. {
  2025. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2026. KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
  2027. ++vcpu->stat.irq_injections;
  2028. if (vcpu->arch.rmode.active) {
  2029. vmx->rmode.irq.pending = true;
  2030. vmx->rmode.irq.vector = irq;
  2031. vmx->rmode.irq.rip = kvm_rip_read(vcpu);
  2032. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2033. irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
  2034. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
  2035. kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
  2036. return;
  2037. }
  2038. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2039. irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  2040. }
  2041. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  2042. {
  2043. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2044. if (!cpu_has_virtual_nmis()) {
  2045. /*
  2046. * Tracking the NMI-blocked state in software is built upon
  2047. * finding the next open IRQ window. This, in turn, depends on
  2048. * well-behaving guests: They have to keep IRQs disabled at
  2049. * least as long as the NMI handler runs. Otherwise we may
  2050. * cause NMI nesting, maybe breaking the guest. But as this is
  2051. * highly unlikely, we can live with the residual risk.
  2052. */
  2053. vmx->soft_vnmi_blocked = 1;
  2054. vmx->vnmi_blocked_time = 0;
  2055. }
  2056. ++vcpu->stat.nmi_injections;
  2057. if (vcpu->arch.rmode.active) {
  2058. vmx->rmode.irq.pending = true;
  2059. vmx->rmode.irq.vector = NMI_VECTOR;
  2060. vmx->rmode.irq.rip = kvm_rip_read(vcpu);
  2061. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2062. NMI_VECTOR | INTR_TYPE_SOFT_INTR |
  2063. INTR_INFO_VALID_MASK);
  2064. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
  2065. kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
  2066. return;
  2067. }
  2068. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2069. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  2070. }
  2071. static void vmx_update_window_states(struct kvm_vcpu *vcpu)
  2072. {
  2073. u32 guest_intr = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  2074. vcpu->arch.nmi_window_open =
  2075. !(guest_intr & (GUEST_INTR_STATE_STI |
  2076. GUEST_INTR_STATE_MOV_SS |
  2077. GUEST_INTR_STATE_NMI));
  2078. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  2079. vcpu->arch.nmi_window_open = 0;
  2080. vcpu->arch.interrupt_window_open =
  2081. ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  2082. !(guest_intr & (GUEST_INTR_STATE_STI |
  2083. GUEST_INTR_STATE_MOV_SS)));
  2084. }
  2085. static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
  2086. {
  2087. int word_index = __ffs(vcpu->arch.irq_summary);
  2088. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  2089. int irq = word_index * BITS_PER_LONG + bit_index;
  2090. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  2091. if (!vcpu->arch.irq_pending[word_index])
  2092. clear_bit(word_index, &vcpu->arch.irq_summary);
  2093. kvm_queue_interrupt(vcpu, irq);
  2094. }
  2095. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  2096. struct kvm_run *kvm_run)
  2097. {
  2098. vmx_update_window_states(vcpu);
  2099. if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
  2100. if (vcpu->arch.nmi_window_open) {
  2101. vcpu->arch.nmi_pending = false;
  2102. vcpu->arch.nmi_injected = true;
  2103. } else {
  2104. enable_nmi_window(vcpu);
  2105. return;
  2106. }
  2107. }
  2108. if (vcpu->arch.nmi_injected) {
  2109. vmx_inject_nmi(vcpu);
  2110. if (vcpu->arch.nmi_pending || kvm_run->request_nmi_window)
  2111. enable_nmi_window(vcpu);
  2112. else if (vcpu->arch.irq_summary
  2113. || kvm_run->request_interrupt_window)
  2114. enable_irq_window(vcpu);
  2115. return;
  2116. }
  2117. if (!vcpu->arch.nmi_window_open || kvm_run->request_nmi_window)
  2118. enable_nmi_window(vcpu);
  2119. if (vcpu->arch.interrupt_window_open) {
  2120. if (vcpu->arch.irq_summary && !vcpu->arch.interrupt.pending)
  2121. kvm_do_inject_irq(vcpu);
  2122. if (vcpu->arch.interrupt.pending)
  2123. vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
  2124. }
  2125. if (!vcpu->arch.interrupt_window_open &&
  2126. (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
  2127. enable_irq_window(vcpu);
  2128. }
  2129. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2130. {
  2131. int ret;
  2132. struct kvm_userspace_memory_region tss_mem = {
  2133. .slot = TSS_PRIVATE_MEMSLOT,
  2134. .guest_phys_addr = addr,
  2135. .memory_size = PAGE_SIZE * 3,
  2136. .flags = 0,
  2137. };
  2138. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  2139. if (ret)
  2140. return ret;
  2141. kvm->arch.tss_addr = addr;
  2142. return 0;
  2143. }
  2144. static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
  2145. {
  2146. struct kvm_guest_debug *dbg = &vcpu->guest_debug;
  2147. set_debugreg(dbg->bp[0], 0);
  2148. set_debugreg(dbg->bp[1], 1);
  2149. set_debugreg(dbg->bp[2], 2);
  2150. set_debugreg(dbg->bp[3], 3);
  2151. if (dbg->singlestep) {
  2152. unsigned long flags;
  2153. flags = vmcs_readl(GUEST_RFLAGS);
  2154. flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
  2155. vmcs_writel(GUEST_RFLAGS, flags);
  2156. }
  2157. }
  2158. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  2159. int vec, u32 err_code)
  2160. {
  2161. /*
  2162. * Instruction with address size override prefix opcode 0x67
  2163. * Cause the #SS fault with 0 error code in VM86 mode.
  2164. */
  2165. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  2166. if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
  2167. return 1;
  2168. /*
  2169. * Forward all other exceptions that are valid in real mode.
  2170. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  2171. * the required debugging infrastructure rework.
  2172. */
  2173. switch (vec) {
  2174. case DE_VECTOR:
  2175. case DB_VECTOR:
  2176. case BP_VECTOR:
  2177. case OF_VECTOR:
  2178. case BR_VECTOR:
  2179. case UD_VECTOR:
  2180. case DF_VECTOR:
  2181. case SS_VECTOR:
  2182. case GP_VECTOR:
  2183. case MF_VECTOR:
  2184. kvm_queue_exception(vcpu, vec);
  2185. return 1;
  2186. }
  2187. return 0;
  2188. }
  2189. static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2190. {
  2191. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2192. u32 intr_info, error_code;
  2193. unsigned long cr2, rip;
  2194. u32 vect_info;
  2195. enum emulation_result er;
  2196. vect_info = vmx->idt_vectoring_info;
  2197. intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  2198. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  2199. !is_page_fault(intr_info))
  2200. printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
  2201. "intr info 0x%x\n", __func__, vect_info, intr_info);
  2202. if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
  2203. int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
  2204. set_bit(irq, vcpu->arch.irq_pending);
  2205. set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  2206. }
  2207. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  2208. return 1; /* already handled by vmx_vcpu_run() */
  2209. if (is_no_device(intr_info)) {
  2210. vmx_fpu_activate(vcpu);
  2211. return 1;
  2212. }
  2213. if (is_invalid_opcode(intr_info)) {
  2214. er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  2215. if (er != EMULATE_DONE)
  2216. kvm_queue_exception(vcpu, UD_VECTOR);
  2217. return 1;
  2218. }
  2219. error_code = 0;
  2220. rip = kvm_rip_read(vcpu);
  2221. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  2222. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  2223. if (is_page_fault(intr_info)) {
  2224. /* EPT won't cause page fault directly */
  2225. if (vm_need_ept())
  2226. BUG();
  2227. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  2228. KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
  2229. (u32)((u64)cr2 >> 32), handler);
  2230. if (vcpu->arch.interrupt.pending || vcpu->arch.exception.pending)
  2231. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  2232. return kvm_mmu_page_fault(vcpu, cr2, error_code);
  2233. }
  2234. if (vcpu->arch.rmode.active &&
  2235. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  2236. error_code)) {
  2237. if (vcpu->arch.halt_request) {
  2238. vcpu->arch.halt_request = 0;
  2239. return kvm_emulate_halt(vcpu);
  2240. }
  2241. return 1;
  2242. }
  2243. if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
  2244. (INTR_TYPE_EXCEPTION | 1)) {
  2245. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  2246. return 0;
  2247. }
  2248. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  2249. kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
  2250. kvm_run->ex.error_code = error_code;
  2251. return 0;
  2252. }
  2253. static int handle_external_interrupt(struct kvm_vcpu *vcpu,
  2254. struct kvm_run *kvm_run)
  2255. {
  2256. ++vcpu->stat.irq_exits;
  2257. KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
  2258. return 1;
  2259. }
  2260. static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2261. {
  2262. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  2263. return 0;
  2264. }
  2265. static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2266. {
  2267. unsigned long exit_qualification;
  2268. int size, down, in, string, rep;
  2269. unsigned port;
  2270. ++vcpu->stat.io_exits;
  2271. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2272. string = (exit_qualification & 16) != 0;
  2273. if (string) {
  2274. if (emulate_instruction(vcpu,
  2275. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  2276. return 0;
  2277. return 1;
  2278. }
  2279. size = (exit_qualification & 7) + 1;
  2280. in = (exit_qualification & 8) != 0;
  2281. down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
  2282. rep = (exit_qualification & 32) != 0;
  2283. port = exit_qualification >> 16;
  2284. skip_emulated_instruction(vcpu);
  2285. return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
  2286. }
  2287. static void
  2288. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2289. {
  2290. /*
  2291. * Patch in the VMCALL instruction:
  2292. */
  2293. hypercall[0] = 0x0f;
  2294. hypercall[1] = 0x01;
  2295. hypercall[2] = 0xc1;
  2296. }
  2297. static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2298. {
  2299. unsigned long exit_qualification;
  2300. int cr;
  2301. int reg;
  2302. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2303. cr = exit_qualification & 15;
  2304. reg = (exit_qualification >> 8) & 15;
  2305. switch ((exit_qualification >> 4) & 3) {
  2306. case 0: /* mov to cr */
  2307. KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr,
  2308. (u32)kvm_register_read(vcpu, reg),
  2309. (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
  2310. handler);
  2311. switch (cr) {
  2312. case 0:
  2313. kvm_set_cr0(vcpu, kvm_register_read(vcpu, reg));
  2314. skip_emulated_instruction(vcpu);
  2315. return 1;
  2316. case 3:
  2317. kvm_set_cr3(vcpu, kvm_register_read(vcpu, reg));
  2318. skip_emulated_instruction(vcpu);
  2319. return 1;
  2320. case 4:
  2321. kvm_set_cr4(vcpu, kvm_register_read(vcpu, reg));
  2322. skip_emulated_instruction(vcpu);
  2323. return 1;
  2324. case 8:
  2325. kvm_set_cr8(vcpu, kvm_register_read(vcpu, reg));
  2326. skip_emulated_instruction(vcpu);
  2327. if (irqchip_in_kernel(vcpu->kvm))
  2328. return 1;
  2329. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2330. return 0;
  2331. };
  2332. break;
  2333. case 2: /* clts */
  2334. vmx_fpu_deactivate(vcpu);
  2335. vcpu->arch.cr0 &= ~X86_CR0_TS;
  2336. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  2337. vmx_fpu_activate(vcpu);
  2338. KVMTRACE_0D(CLTS, vcpu, handler);
  2339. skip_emulated_instruction(vcpu);
  2340. return 1;
  2341. case 1: /*mov from cr*/
  2342. switch (cr) {
  2343. case 3:
  2344. kvm_register_write(vcpu, reg, vcpu->arch.cr3);
  2345. KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
  2346. (u32)kvm_register_read(vcpu, reg),
  2347. (u32)((u64)kvm_register_read(vcpu, reg) >> 32),
  2348. handler);
  2349. skip_emulated_instruction(vcpu);
  2350. return 1;
  2351. case 8:
  2352. kvm_register_write(vcpu, reg, kvm_get_cr8(vcpu));
  2353. KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
  2354. (u32)kvm_register_read(vcpu, reg), handler);
  2355. skip_emulated_instruction(vcpu);
  2356. return 1;
  2357. }
  2358. break;
  2359. case 3: /* lmsw */
  2360. kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
  2361. skip_emulated_instruction(vcpu);
  2362. return 1;
  2363. default:
  2364. break;
  2365. }
  2366. kvm_run->exit_reason = 0;
  2367. pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  2368. (int)(exit_qualification >> 4) & 3, cr);
  2369. return 0;
  2370. }
  2371. static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2372. {
  2373. unsigned long exit_qualification;
  2374. unsigned long val;
  2375. int dr, reg;
  2376. /*
  2377. * FIXME: this code assumes the host is debugging the guest.
  2378. * need to deal with guest debugging itself too.
  2379. */
  2380. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2381. dr = exit_qualification & 7;
  2382. reg = (exit_qualification >> 8) & 15;
  2383. if (exit_qualification & 16) {
  2384. /* mov from dr */
  2385. switch (dr) {
  2386. case 6:
  2387. val = 0xffff0ff0;
  2388. break;
  2389. case 7:
  2390. val = 0x400;
  2391. break;
  2392. default:
  2393. val = 0;
  2394. }
  2395. kvm_register_write(vcpu, reg, val);
  2396. KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
  2397. } else {
  2398. /* mov to dr */
  2399. }
  2400. skip_emulated_instruction(vcpu);
  2401. return 1;
  2402. }
  2403. static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2404. {
  2405. kvm_emulate_cpuid(vcpu);
  2406. return 1;
  2407. }
  2408. static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2409. {
  2410. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2411. u64 data;
  2412. if (vmx_get_msr(vcpu, ecx, &data)) {
  2413. kvm_inject_gp(vcpu, 0);
  2414. return 1;
  2415. }
  2416. KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
  2417. handler);
  2418. /* FIXME: handling of bits 32:63 of rax, rdx */
  2419. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  2420. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  2421. skip_emulated_instruction(vcpu);
  2422. return 1;
  2423. }
  2424. static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2425. {
  2426. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2427. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  2428. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2429. KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
  2430. handler);
  2431. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  2432. kvm_inject_gp(vcpu, 0);
  2433. return 1;
  2434. }
  2435. skip_emulated_instruction(vcpu);
  2436. return 1;
  2437. }
  2438. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
  2439. struct kvm_run *kvm_run)
  2440. {
  2441. return 1;
  2442. }
  2443. static int handle_interrupt_window(struct kvm_vcpu *vcpu,
  2444. struct kvm_run *kvm_run)
  2445. {
  2446. u32 cpu_based_vm_exec_control;
  2447. /* clear pending irq */
  2448. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2449. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  2450. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2451. KVMTRACE_0D(PEND_INTR, vcpu, handler);
  2452. ++vcpu->stat.irq_window_exits;
  2453. /*
  2454. * If the user space waits to inject interrupts, exit as soon as
  2455. * possible
  2456. */
  2457. if (kvm_run->request_interrupt_window &&
  2458. !vcpu->arch.irq_summary) {
  2459. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2460. return 0;
  2461. }
  2462. return 1;
  2463. }
  2464. static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2465. {
  2466. skip_emulated_instruction(vcpu);
  2467. return kvm_emulate_halt(vcpu);
  2468. }
  2469. static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2470. {
  2471. skip_emulated_instruction(vcpu);
  2472. kvm_emulate_hypercall(vcpu);
  2473. return 1;
  2474. }
  2475. static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2476. {
  2477. u64 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
  2478. kvm_mmu_invlpg(vcpu, exit_qualification);
  2479. skip_emulated_instruction(vcpu);
  2480. return 1;
  2481. }
  2482. static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2483. {
  2484. skip_emulated_instruction(vcpu);
  2485. /* TODO: Add support for VT-d/pass-through device */
  2486. return 1;
  2487. }
  2488. static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2489. {
  2490. u64 exit_qualification;
  2491. enum emulation_result er;
  2492. unsigned long offset;
  2493. exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
  2494. offset = exit_qualification & 0xffful;
  2495. er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
  2496. if (er != EMULATE_DONE) {
  2497. printk(KERN_ERR
  2498. "Fail to handle apic access vmexit! Offset is 0x%lx\n",
  2499. offset);
  2500. return -ENOTSUPP;
  2501. }
  2502. return 1;
  2503. }
  2504. static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2505. {
  2506. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2507. unsigned long exit_qualification;
  2508. u16 tss_selector;
  2509. int reason;
  2510. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2511. reason = (u32)exit_qualification >> 30;
  2512. if (reason == TASK_SWITCH_GATE && vmx->vcpu.arch.nmi_injected &&
  2513. (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
  2514. (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK)
  2515. == INTR_TYPE_NMI_INTR) {
  2516. vcpu->arch.nmi_injected = false;
  2517. if (cpu_has_virtual_nmis())
  2518. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2519. GUEST_INTR_STATE_NMI);
  2520. }
  2521. tss_selector = exit_qualification;
  2522. return kvm_task_switch(vcpu, tss_selector, reason);
  2523. }
  2524. static int handle_ept_violation(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2525. {
  2526. u64 exit_qualification;
  2527. enum emulation_result er;
  2528. gpa_t gpa;
  2529. unsigned long hva;
  2530. int gla_validity;
  2531. int r;
  2532. exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
  2533. if (exit_qualification & (1 << 6)) {
  2534. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  2535. return -ENOTSUPP;
  2536. }
  2537. gla_validity = (exit_qualification >> 7) & 0x3;
  2538. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  2539. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  2540. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  2541. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  2542. (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
  2543. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  2544. (long unsigned int)exit_qualification);
  2545. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2546. kvm_run->hw.hardware_exit_reason = 0;
  2547. return -ENOTSUPP;
  2548. }
  2549. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  2550. hva = gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT);
  2551. if (!kvm_is_error_hva(hva)) {
  2552. r = kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
  2553. if (r < 0) {
  2554. printk(KERN_ERR "EPT: Not enough memory!\n");
  2555. return -ENOMEM;
  2556. }
  2557. return 1;
  2558. } else {
  2559. /* must be MMIO */
  2560. er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
  2561. if (er == EMULATE_FAIL) {
  2562. printk(KERN_ERR
  2563. "EPT: Fail to handle EPT violation vmexit!er is %d\n",
  2564. er);
  2565. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  2566. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  2567. (long unsigned int)vmcs_read64(GUEST_LINEAR_ADDRESS));
  2568. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  2569. (long unsigned int)exit_qualification);
  2570. return -ENOTSUPP;
  2571. } else if (er == EMULATE_DO_MMIO)
  2572. return 0;
  2573. }
  2574. return 1;
  2575. }
  2576. static int handle_nmi_window(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2577. {
  2578. u32 cpu_based_vm_exec_control;
  2579. /* clear pending NMI */
  2580. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2581. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  2582. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2583. ++vcpu->stat.nmi_window_exits;
  2584. /*
  2585. * If the user space waits to inject a NMI, exit as soon as possible
  2586. */
  2587. if (kvm_run->request_nmi_window && !vcpu->arch.nmi_pending) {
  2588. kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN;
  2589. return 0;
  2590. }
  2591. return 1;
  2592. }
  2593. static void handle_invalid_guest_state(struct kvm_vcpu *vcpu,
  2594. struct kvm_run *kvm_run)
  2595. {
  2596. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2597. int err;
  2598. preempt_enable();
  2599. local_irq_enable();
  2600. while (!guest_state_valid(vcpu)) {
  2601. err = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
  2602. if (err == EMULATE_DO_MMIO)
  2603. break;
  2604. if (err != EMULATE_DONE) {
  2605. kvm_report_emulation_failure(vcpu, "emulation failure");
  2606. return;
  2607. }
  2608. if (signal_pending(current))
  2609. break;
  2610. if (need_resched())
  2611. schedule();
  2612. }
  2613. local_irq_disable();
  2614. preempt_disable();
  2615. /* Guest state should be valid now except if we need to
  2616. * emulate an MMIO */
  2617. if (guest_state_valid(vcpu))
  2618. vmx->emulation_required = 0;
  2619. }
  2620. /*
  2621. * The exit handlers return 1 if the exit was handled fully and guest execution
  2622. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  2623. * to be done to userspace and return 0.
  2624. */
  2625. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
  2626. struct kvm_run *kvm_run) = {
  2627. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  2628. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  2629. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  2630. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  2631. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  2632. [EXIT_REASON_CR_ACCESS] = handle_cr,
  2633. [EXIT_REASON_DR_ACCESS] = handle_dr,
  2634. [EXIT_REASON_CPUID] = handle_cpuid,
  2635. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  2636. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  2637. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  2638. [EXIT_REASON_HLT] = handle_halt,
  2639. [EXIT_REASON_INVLPG] = handle_invlpg,
  2640. [EXIT_REASON_VMCALL] = handle_vmcall,
  2641. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  2642. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  2643. [EXIT_REASON_WBINVD] = handle_wbinvd,
  2644. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  2645. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  2646. };
  2647. static const int kvm_vmx_max_exit_handlers =
  2648. ARRAY_SIZE(kvm_vmx_exit_handlers);
  2649. /*
  2650. * The guest has exited. See if we can fix it or if we need userspace
  2651. * assistance.
  2652. */
  2653. static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  2654. {
  2655. u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
  2656. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2657. u32 vectoring_info = vmx->idt_vectoring_info;
  2658. KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)kvm_rip_read(vcpu),
  2659. (u32)((u64)kvm_rip_read(vcpu) >> 32), entryexit);
  2660. /* If we need to emulate an MMIO from handle_invalid_guest_state
  2661. * we just return 0 */
  2662. if (vmx->emulation_required && emulate_invalid_guest_state)
  2663. return 0;
  2664. /* Access CR3 don't cause VMExit in paging mode, so we need
  2665. * to sync with guest real CR3. */
  2666. if (vm_need_ept() && is_paging(vcpu)) {
  2667. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  2668. ept_load_pdptrs(vcpu);
  2669. }
  2670. if (unlikely(vmx->fail)) {
  2671. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2672. kvm_run->fail_entry.hardware_entry_failure_reason
  2673. = vmcs_read32(VM_INSTRUCTION_ERROR);
  2674. return 0;
  2675. }
  2676. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  2677. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  2678. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  2679. exit_reason != EXIT_REASON_TASK_SWITCH))
  2680. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  2681. "(0x%x) and exit reason is 0x%x\n",
  2682. __func__, vectoring_info, exit_reason);
  2683. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
  2684. if (vcpu->arch.interrupt_window_open) {
  2685. vmx->soft_vnmi_blocked = 0;
  2686. vcpu->arch.nmi_window_open = 1;
  2687. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  2688. (kvm_run->request_nmi_window || vcpu->arch.nmi_pending)) {
  2689. /*
  2690. * This CPU don't support us in finding the end of an
  2691. * NMI-blocked window if the guest runs with IRQs
  2692. * disabled. So we pull the trigger after 1 s of
  2693. * futile waiting, but inform the user about this.
  2694. */
  2695. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  2696. "state on VCPU %d after 1 s timeout\n",
  2697. __func__, vcpu->vcpu_id);
  2698. vmx->soft_vnmi_blocked = 0;
  2699. vmx->vcpu.arch.nmi_window_open = 1;
  2700. }
  2701. /*
  2702. * If the user space waits to inject an NNI, exit ASAP
  2703. */
  2704. if (vcpu->arch.nmi_window_open && kvm_run->request_nmi_window
  2705. && !vcpu->arch.nmi_pending) {
  2706. kvm_run->exit_reason = KVM_EXIT_NMI_WINDOW_OPEN;
  2707. ++vcpu->stat.nmi_window_exits;
  2708. return 0;
  2709. }
  2710. }
  2711. if (exit_reason < kvm_vmx_max_exit_handlers
  2712. && kvm_vmx_exit_handlers[exit_reason])
  2713. return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
  2714. else {
  2715. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2716. kvm_run->hw.hardware_exit_reason = exit_reason;
  2717. }
  2718. return 0;
  2719. }
  2720. static void update_tpr_threshold(struct kvm_vcpu *vcpu)
  2721. {
  2722. int max_irr, tpr;
  2723. if (!vm_need_tpr_shadow(vcpu->kvm))
  2724. return;
  2725. if (!kvm_lapic_enabled(vcpu) ||
  2726. ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
  2727. vmcs_write32(TPR_THRESHOLD, 0);
  2728. return;
  2729. }
  2730. tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
  2731. vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
  2732. }
  2733. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  2734. {
  2735. u32 exit_intr_info;
  2736. u32 idt_vectoring_info;
  2737. bool unblock_nmi;
  2738. u8 vector;
  2739. int type;
  2740. bool idtv_info_valid;
  2741. u32 error;
  2742. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  2743. if (cpu_has_virtual_nmis()) {
  2744. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  2745. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  2746. /*
  2747. * SDM 3: 25.7.1.2
  2748. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  2749. * a guest IRET fault.
  2750. */
  2751. if (unblock_nmi && vector != DF_VECTOR)
  2752. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2753. GUEST_INTR_STATE_NMI);
  2754. } else if (unlikely(vmx->soft_vnmi_blocked))
  2755. vmx->vnmi_blocked_time +=
  2756. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  2757. idt_vectoring_info = vmx->idt_vectoring_info;
  2758. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  2759. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  2760. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  2761. if (vmx->vcpu.arch.nmi_injected) {
  2762. /*
  2763. * SDM 3: 25.7.1.2
  2764. * Clear bit "block by NMI" before VM entry if a NMI delivery
  2765. * faulted.
  2766. */
  2767. if (idtv_info_valid && type == INTR_TYPE_NMI_INTR)
  2768. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  2769. GUEST_INTR_STATE_NMI);
  2770. else
  2771. vmx->vcpu.arch.nmi_injected = false;
  2772. }
  2773. kvm_clear_exception_queue(&vmx->vcpu);
  2774. if (idtv_info_valid && type == INTR_TYPE_EXCEPTION) {
  2775. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  2776. error = vmcs_read32(IDT_VECTORING_ERROR_CODE);
  2777. kvm_queue_exception_e(&vmx->vcpu, vector, error);
  2778. } else
  2779. kvm_queue_exception(&vmx->vcpu, vector);
  2780. vmx->idt_vectoring_info = 0;
  2781. }
  2782. kvm_clear_interrupt_queue(&vmx->vcpu);
  2783. if (idtv_info_valid && type == INTR_TYPE_EXT_INTR) {
  2784. kvm_queue_interrupt(&vmx->vcpu, vector);
  2785. vmx->idt_vectoring_info = 0;
  2786. }
  2787. }
  2788. static void vmx_intr_assist(struct kvm_vcpu *vcpu)
  2789. {
  2790. update_tpr_threshold(vcpu);
  2791. vmx_update_window_states(vcpu);
  2792. if (vcpu->arch.nmi_pending && !vcpu->arch.nmi_injected) {
  2793. if (vcpu->arch.interrupt.pending) {
  2794. enable_nmi_window(vcpu);
  2795. } else if (vcpu->arch.nmi_window_open) {
  2796. vcpu->arch.nmi_pending = false;
  2797. vcpu->arch.nmi_injected = true;
  2798. } else {
  2799. enable_nmi_window(vcpu);
  2800. return;
  2801. }
  2802. }
  2803. if (vcpu->arch.nmi_injected) {
  2804. vmx_inject_nmi(vcpu);
  2805. if (vcpu->arch.nmi_pending)
  2806. enable_nmi_window(vcpu);
  2807. else if (kvm_cpu_has_interrupt(vcpu))
  2808. enable_irq_window(vcpu);
  2809. return;
  2810. }
  2811. if (!vcpu->arch.interrupt.pending && kvm_cpu_has_interrupt(vcpu)) {
  2812. if (vcpu->arch.interrupt_window_open)
  2813. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu));
  2814. else
  2815. enable_irq_window(vcpu);
  2816. }
  2817. if (vcpu->arch.interrupt.pending) {
  2818. vmx_inject_irq(vcpu, vcpu->arch.interrupt.nr);
  2819. kvm_timer_intr_post(vcpu, vcpu->arch.interrupt.nr);
  2820. }
  2821. }
  2822. /*
  2823. * Failure to inject an interrupt should give us the information
  2824. * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
  2825. * when fetching the interrupt redirection bitmap in the real-mode
  2826. * tss, this doesn't happen. So we do it ourselves.
  2827. */
  2828. static void fixup_rmode_irq(struct vcpu_vmx *vmx)
  2829. {
  2830. vmx->rmode.irq.pending = 0;
  2831. if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
  2832. return;
  2833. kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
  2834. if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
  2835. vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
  2836. vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
  2837. return;
  2838. }
  2839. vmx->idt_vectoring_info =
  2840. VECTORING_INFO_VALID_MASK
  2841. | INTR_TYPE_EXT_INTR
  2842. | vmx->rmode.irq.vector;
  2843. }
  2844. #ifdef CONFIG_X86_64
  2845. #define R "r"
  2846. #define Q "q"
  2847. #else
  2848. #define R "e"
  2849. #define Q "l"
  2850. #endif
  2851. static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2852. {
  2853. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2854. u32 intr_info;
  2855. /* Record the guest's net vcpu time for enforced NMI injections. */
  2856. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  2857. vmx->entry_time = ktime_get();
  2858. /* Handle invalid guest state instead of entering VMX */
  2859. if (vmx->emulation_required && emulate_invalid_guest_state) {
  2860. handle_invalid_guest_state(vcpu, kvm_run);
  2861. return;
  2862. }
  2863. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  2864. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  2865. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  2866. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  2867. /*
  2868. * Loading guest fpu may have cleared host cr0.ts
  2869. */
  2870. vmcs_writel(HOST_CR0, read_cr0());
  2871. asm(
  2872. /* Store host registers */
  2873. "push %%"R"dx; push %%"R"bp;"
  2874. "push %%"R"cx \n\t"
  2875. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  2876. "je 1f \n\t"
  2877. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  2878. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  2879. "1: \n\t"
  2880. /* Check if vmlaunch of vmresume is needed */
  2881. "cmpl $0, %c[launched](%0) \n\t"
  2882. /* Load guest registers. Don't clobber flags. */
  2883. "mov %c[cr2](%0), %%"R"ax \n\t"
  2884. "mov %%"R"ax, %%cr2 \n\t"
  2885. "mov %c[rax](%0), %%"R"ax \n\t"
  2886. "mov %c[rbx](%0), %%"R"bx \n\t"
  2887. "mov %c[rdx](%0), %%"R"dx \n\t"
  2888. "mov %c[rsi](%0), %%"R"si \n\t"
  2889. "mov %c[rdi](%0), %%"R"di \n\t"
  2890. "mov %c[rbp](%0), %%"R"bp \n\t"
  2891. #ifdef CONFIG_X86_64
  2892. "mov %c[r8](%0), %%r8 \n\t"
  2893. "mov %c[r9](%0), %%r9 \n\t"
  2894. "mov %c[r10](%0), %%r10 \n\t"
  2895. "mov %c[r11](%0), %%r11 \n\t"
  2896. "mov %c[r12](%0), %%r12 \n\t"
  2897. "mov %c[r13](%0), %%r13 \n\t"
  2898. "mov %c[r14](%0), %%r14 \n\t"
  2899. "mov %c[r15](%0), %%r15 \n\t"
  2900. #endif
  2901. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  2902. /* Enter guest mode */
  2903. "jne .Llaunched \n\t"
  2904. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  2905. "jmp .Lkvm_vmx_return \n\t"
  2906. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  2907. ".Lkvm_vmx_return: "
  2908. /* Save guest registers, load host registers, keep flags */
  2909. "xchg %0, (%%"R"sp) \n\t"
  2910. "mov %%"R"ax, %c[rax](%0) \n\t"
  2911. "mov %%"R"bx, %c[rbx](%0) \n\t"
  2912. "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
  2913. "mov %%"R"dx, %c[rdx](%0) \n\t"
  2914. "mov %%"R"si, %c[rsi](%0) \n\t"
  2915. "mov %%"R"di, %c[rdi](%0) \n\t"
  2916. "mov %%"R"bp, %c[rbp](%0) \n\t"
  2917. #ifdef CONFIG_X86_64
  2918. "mov %%r8, %c[r8](%0) \n\t"
  2919. "mov %%r9, %c[r9](%0) \n\t"
  2920. "mov %%r10, %c[r10](%0) \n\t"
  2921. "mov %%r11, %c[r11](%0) \n\t"
  2922. "mov %%r12, %c[r12](%0) \n\t"
  2923. "mov %%r13, %c[r13](%0) \n\t"
  2924. "mov %%r14, %c[r14](%0) \n\t"
  2925. "mov %%r15, %c[r15](%0) \n\t"
  2926. #endif
  2927. "mov %%cr2, %%"R"ax \n\t"
  2928. "mov %%"R"ax, %c[cr2](%0) \n\t"
  2929. "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
  2930. "setbe %c[fail](%0) \n\t"
  2931. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  2932. [launched]"i"(offsetof(struct vcpu_vmx, launched)),
  2933. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  2934. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  2935. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  2936. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  2937. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  2938. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  2939. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  2940. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  2941. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  2942. #ifdef CONFIG_X86_64
  2943. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  2944. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  2945. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  2946. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  2947. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  2948. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  2949. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  2950. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  2951. #endif
  2952. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
  2953. : "cc", "memory"
  2954. , R"bx", R"di", R"si"
  2955. #ifdef CONFIG_X86_64
  2956. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  2957. #endif
  2958. );
  2959. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  2960. vcpu->arch.regs_dirty = 0;
  2961. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  2962. if (vmx->rmode.irq.pending)
  2963. fixup_rmode_irq(vmx);
  2964. vmx_update_window_states(vcpu);
  2965. asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
  2966. vmx->launched = 1;
  2967. intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  2968. /* We need to handle NMIs before interrupts are enabled */
  2969. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  2970. (intr_info & INTR_INFO_VALID_MASK)) {
  2971. KVMTRACE_0D(NMI, vcpu, handler);
  2972. asm("int $2");
  2973. }
  2974. vmx_complete_interrupts(vmx);
  2975. }
  2976. #undef R
  2977. #undef Q
  2978. static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
  2979. {
  2980. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2981. if (vmx->vmcs) {
  2982. vcpu_clear(vmx);
  2983. free_vmcs(vmx->vmcs);
  2984. vmx->vmcs = NULL;
  2985. }
  2986. }
  2987. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  2988. {
  2989. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2990. spin_lock(&vmx_vpid_lock);
  2991. if (vmx->vpid != 0)
  2992. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  2993. spin_unlock(&vmx_vpid_lock);
  2994. vmx_free_vmcs(vcpu);
  2995. kfree(vmx->host_msrs);
  2996. kfree(vmx->guest_msrs);
  2997. kvm_vcpu_uninit(vcpu);
  2998. kmem_cache_free(kvm_vcpu_cache, vmx);
  2999. }
  3000. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  3001. {
  3002. int err;
  3003. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  3004. int cpu;
  3005. if (!vmx)
  3006. return ERR_PTR(-ENOMEM);
  3007. allocate_vpid(vmx);
  3008. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  3009. if (err)
  3010. goto free_vcpu;
  3011. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3012. if (!vmx->guest_msrs) {
  3013. err = -ENOMEM;
  3014. goto uninit_vcpu;
  3015. }
  3016. vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3017. if (!vmx->host_msrs)
  3018. goto free_guest_msrs;
  3019. vmx->vmcs = alloc_vmcs();
  3020. if (!vmx->vmcs)
  3021. goto free_msrs;
  3022. vmcs_clear(vmx->vmcs);
  3023. cpu = get_cpu();
  3024. vmx_vcpu_load(&vmx->vcpu, cpu);
  3025. err = vmx_vcpu_setup(vmx);
  3026. vmx_vcpu_put(&vmx->vcpu);
  3027. put_cpu();
  3028. if (err)
  3029. goto free_vmcs;
  3030. if (vm_need_virtualize_apic_accesses(kvm))
  3031. if (alloc_apic_access_page(kvm) != 0)
  3032. goto free_vmcs;
  3033. if (vm_need_ept())
  3034. if (alloc_identity_pagetable(kvm) != 0)
  3035. goto free_vmcs;
  3036. return &vmx->vcpu;
  3037. free_vmcs:
  3038. free_vmcs(vmx->vmcs);
  3039. free_msrs:
  3040. kfree(vmx->host_msrs);
  3041. free_guest_msrs:
  3042. kfree(vmx->guest_msrs);
  3043. uninit_vcpu:
  3044. kvm_vcpu_uninit(&vmx->vcpu);
  3045. free_vcpu:
  3046. kmem_cache_free(kvm_vcpu_cache, vmx);
  3047. return ERR_PTR(err);
  3048. }
  3049. static void __init vmx_check_processor_compat(void *rtn)
  3050. {
  3051. struct vmcs_config vmcs_conf;
  3052. *(int *)rtn = 0;
  3053. if (setup_vmcs_config(&vmcs_conf) < 0)
  3054. *(int *)rtn = -EIO;
  3055. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  3056. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  3057. smp_processor_id());
  3058. *(int *)rtn = -EIO;
  3059. }
  3060. }
  3061. static int get_ept_level(void)
  3062. {
  3063. return VMX_EPT_DEFAULT_GAW + 1;
  3064. }
  3065. static int vmx_get_mt_mask_shift(void)
  3066. {
  3067. return VMX_EPT_MT_EPTE_SHIFT;
  3068. }
  3069. static struct kvm_x86_ops vmx_x86_ops = {
  3070. .cpu_has_kvm_support = cpu_has_kvm_support,
  3071. .disabled_by_bios = vmx_disabled_by_bios,
  3072. .hardware_setup = hardware_setup,
  3073. .hardware_unsetup = hardware_unsetup,
  3074. .check_processor_compatibility = vmx_check_processor_compat,
  3075. .hardware_enable = hardware_enable,
  3076. .hardware_disable = hardware_disable,
  3077. .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
  3078. .vcpu_create = vmx_create_vcpu,
  3079. .vcpu_free = vmx_free_vcpu,
  3080. .vcpu_reset = vmx_vcpu_reset,
  3081. .prepare_guest_switch = vmx_save_host_state,
  3082. .vcpu_load = vmx_vcpu_load,
  3083. .vcpu_put = vmx_vcpu_put,
  3084. .set_guest_debug = set_guest_debug,
  3085. .guest_debug_pre = kvm_guest_debug_pre,
  3086. .get_msr = vmx_get_msr,
  3087. .set_msr = vmx_set_msr,
  3088. .get_segment_base = vmx_get_segment_base,
  3089. .get_segment = vmx_get_segment,
  3090. .set_segment = vmx_set_segment,
  3091. .get_cpl = vmx_get_cpl,
  3092. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  3093. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  3094. .set_cr0 = vmx_set_cr0,
  3095. .set_cr3 = vmx_set_cr3,
  3096. .set_cr4 = vmx_set_cr4,
  3097. .set_efer = vmx_set_efer,
  3098. .get_idt = vmx_get_idt,
  3099. .set_idt = vmx_set_idt,
  3100. .get_gdt = vmx_get_gdt,
  3101. .set_gdt = vmx_set_gdt,
  3102. .cache_reg = vmx_cache_reg,
  3103. .get_rflags = vmx_get_rflags,
  3104. .set_rflags = vmx_set_rflags,
  3105. .tlb_flush = vmx_flush_tlb,
  3106. .run = vmx_vcpu_run,
  3107. .handle_exit = kvm_handle_exit,
  3108. .skip_emulated_instruction = skip_emulated_instruction,
  3109. .patch_hypercall = vmx_patch_hypercall,
  3110. .get_irq = vmx_get_irq,
  3111. .set_irq = vmx_inject_irq,
  3112. .queue_exception = vmx_queue_exception,
  3113. .exception_injected = vmx_exception_injected,
  3114. .inject_pending_irq = vmx_intr_assist,
  3115. .inject_pending_vectors = do_interrupt_requests,
  3116. .set_tss_addr = vmx_set_tss_addr,
  3117. .get_tdp_level = get_ept_level,
  3118. .get_mt_mask_shift = vmx_get_mt_mask_shift,
  3119. };
  3120. static int __init vmx_init(void)
  3121. {
  3122. void *va;
  3123. int r;
  3124. vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  3125. if (!vmx_io_bitmap_a)
  3126. return -ENOMEM;
  3127. vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  3128. if (!vmx_io_bitmap_b) {
  3129. r = -ENOMEM;
  3130. goto out;
  3131. }
  3132. vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  3133. if (!vmx_msr_bitmap) {
  3134. r = -ENOMEM;
  3135. goto out1;
  3136. }
  3137. /*
  3138. * Allow direct access to the PC debug port (it is often used for I/O
  3139. * delays, but the vmexits simply slow things down).
  3140. */
  3141. va = kmap(vmx_io_bitmap_a);
  3142. memset(va, 0xff, PAGE_SIZE);
  3143. clear_bit(0x80, va);
  3144. kunmap(vmx_io_bitmap_a);
  3145. va = kmap(vmx_io_bitmap_b);
  3146. memset(va, 0xff, PAGE_SIZE);
  3147. kunmap(vmx_io_bitmap_b);
  3148. va = kmap(vmx_msr_bitmap);
  3149. memset(va, 0xff, PAGE_SIZE);
  3150. kunmap(vmx_msr_bitmap);
  3151. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  3152. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
  3153. if (r)
  3154. goto out2;
  3155. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
  3156. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
  3157. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
  3158. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
  3159. vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
  3160. if (vm_need_ept()) {
  3161. bypass_guest_pf = 0;
  3162. kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
  3163. VMX_EPT_WRITABLE_MASK |
  3164. VMX_EPT_IGMT_BIT);
  3165. kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
  3166. VMX_EPT_EXECUTABLE_MASK,
  3167. VMX_EPT_DEFAULT_MT << VMX_EPT_MT_EPTE_SHIFT);
  3168. kvm_enable_tdp();
  3169. } else
  3170. kvm_disable_tdp();
  3171. if (bypass_guest_pf)
  3172. kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
  3173. ept_sync_global();
  3174. return 0;
  3175. out2:
  3176. __free_page(vmx_msr_bitmap);
  3177. out1:
  3178. __free_page(vmx_io_bitmap_b);
  3179. out:
  3180. __free_page(vmx_io_bitmap_a);
  3181. return r;
  3182. }
  3183. static void __exit vmx_exit(void)
  3184. {
  3185. __free_page(vmx_msr_bitmap);
  3186. __free_page(vmx_io_bitmap_b);
  3187. __free_page(vmx_io_bitmap_a);
  3188. kvm_exit();
  3189. }
  3190. module_init(vmx_init)
  3191. module_exit(vmx_exit)