vmx.c 100 KB

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