svm.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * AMD SVM support
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  8. *
  9. * Authors:
  10. * Yaniv Kamay <yaniv@qumranet.com>
  11. * Avi Kivity <avi@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 <linux/kvm_host.h>
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include "kvm_cache_regs.h"
  21. #include "x86.h"
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/ftrace_event.h>
  28. #include <linux/slab.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/desc.h>
  31. #include <asm/kvm_para.h>
  32. #include <asm/virtext.h>
  33. #include "trace.h"
  34. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  35. MODULE_AUTHOR("Qumranet");
  36. MODULE_LICENSE("GPL");
  37. #define IOPM_ALLOC_ORDER 2
  38. #define MSRPM_ALLOC_ORDER 1
  39. #define SEG_TYPE_LDT 2
  40. #define SEG_TYPE_BUSY_TSS16 3
  41. #define SVM_FEATURE_NPT (1 << 0)
  42. #define SVM_FEATURE_LBRV (1 << 1)
  43. #define SVM_FEATURE_SVML (1 << 2)
  44. #define SVM_FEATURE_NRIP (1 << 3)
  45. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  46. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  47. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  48. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  49. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  50. static bool erratum_383_found __read_mostly;
  51. static const u32 host_save_user_msrs[] = {
  52. #ifdef CONFIG_X86_64
  53. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  54. MSR_FS_BASE,
  55. #endif
  56. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  57. };
  58. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  59. struct kvm_vcpu;
  60. struct nested_state {
  61. struct vmcb *hsave;
  62. u64 hsave_msr;
  63. u64 vm_cr_msr;
  64. u64 vmcb;
  65. /* These are the merged vectors */
  66. u32 *msrpm;
  67. /* gpa pointers to the real vectors */
  68. u64 vmcb_msrpm;
  69. u64 vmcb_iopm;
  70. /* A VMEXIT is required but not yet emulated */
  71. bool exit_required;
  72. /*
  73. * If we vmexit during an instruction emulation we need this to restore
  74. * the l1 guest rip after the emulation
  75. */
  76. unsigned long vmexit_rip;
  77. unsigned long vmexit_rsp;
  78. unsigned long vmexit_rax;
  79. /* cache for intercepts of the guest */
  80. u32 intercept_cr;
  81. u32 intercept_dr;
  82. u32 intercept_exceptions;
  83. u64 intercept;
  84. /* Nested Paging related state */
  85. u64 nested_cr3;
  86. };
  87. #define MSRPM_OFFSETS 16
  88. static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  89. struct vcpu_svm {
  90. struct kvm_vcpu vcpu;
  91. struct vmcb *vmcb;
  92. unsigned long vmcb_pa;
  93. struct svm_cpu_data *svm_data;
  94. uint64_t asid_generation;
  95. uint64_t sysenter_esp;
  96. uint64_t sysenter_eip;
  97. u64 next_rip;
  98. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  99. struct {
  100. u16 fs;
  101. u16 gs;
  102. u16 ldt;
  103. u64 gs_base;
  104. } host;
  105. u32 *msrpm;
  106. struct nested_state nested;
  107. bool nmi_singlestep;
  108. unsigned int3_injected;
  109. unsigned long int3_rip;
  110. u32 apf_reason;
  111. };
  112. #define MSR_INVALID 0xffffffffU
  113. static struct svm_direct_access_msrs {
  114. u32 index; /* Index of the MSR */
  115. bool always; /* True if intercept is always on */
  116. } direct_access_msrs[] = {
  117. { .index = MSR_STAR, .always = true },
  118. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  119. #ifdef CONFIG_X86_64
  120. { .index = MSR_GS_BASE, .always = true },
  121. { .index = MSR_FS_BASE, .always = true },
  122. { .index = MSR_KERNEL_GS_BASE, .always = true },
  123. { .index = MSR_LSTAR, .always = true },
  124. { .index = MSR_CSTAR, .always = true },
  125. { .index = MSR_SYSCALL_MASK, .always = true },
  126. #endif
  127. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  128. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  129. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  130. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  131. { .index = MSR_INVALID, .always = false },
  132. };
  133. /* enable NPT for AMD64 and X86 with PAE */
  134. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  135. static bool npt_enabled = true;
  136. #else
  137. static bool npt_enabled;
  138. #endif
  139. static int npt = 1;
  140. module_param(npt, int, S_IRUGO);
  141. static int nested = 1;
  142. module_param(nested, int, S_IRUGO);
  143. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  144. static void svm_complete_interrupts(struct vcpu_svm *svm);
  145. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  146. static int nested_svm_intercept(struct vcpu_svm *svm);
  147. static int nested_svm_vmexit(struct vcpu_svm *svm);
  148. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  149. bool has_error_code, u32 error_code);
  150. enum {
  151. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  152. pause filter count */
  153. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  154. VMCB_ASID, /* ASID */
  155. VMCB_INTR, /* int_ctl, int_vector */
  156. VMCB_NPT, /* npt_en, nCR3, gPAT */
  157. VMCB_CR, /* CR0, CR3, CR4, EFER */
  158. VMCB_DR, /* DR6, DR7 */
  159. VMCB_DT, /* GDT, IDT */
  160. VMCB_SEG, /* CS, DS, SS, ES, CPL */
  161. VMCB_CR2, /* CR2 only */
  162. VMCB_DIRTY_MAX,
  163. };
  164. /* TPR and CR2 are always written before VMRUN */
  165. #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
  166. static inline void mark_all_dirty(struct vmcb *vmcb)
  167. {
  168. vmcb->control.clean = 0;
  169. }
  170. static inline void mark_all_clean(struct vmcb *vmcb)
  171. {
  172. vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
  173. & ~VMCB_ALWAYS_DIRTY_MASK;
  174. }
  175. static inline void mark_dirty(struct vmcb *vmcb, int bit)
  176. {
  177. vmcb->control.clean &= ~(1 << bit);
  178. }
  179. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  180. {
  181. return container_of(vcpu, struct vcpu_svm, vcpu);
  182. }
  183. static void recalc_intercepts(struct vcpu_svm *svm)
  184. {
  185. struct vmcb_control_area *c, *h;
  186. struct nested_state *g;
  187. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  188. if (!is_guest_mode(&svm->vcpu))
  189. return;
  190. c = &svm->vmcb->control;
  191. h = &svm->nested.hsave->control;
  192. g = &svm->nested;
  193. c->intercept_cr = h->intercept_cr | g->intercept_cr;
  194. c->intercept_dr = h->intercept_dr | g->intercept_dr;
  195. c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
  196. c->intercept = h->intercept | g->intercept;
  197. }
  198. static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
  199. {
  200. if (is_guest_mode(&svm->vcpu))
  201. return svm->nested.hsave;
  202. else
  203. return svm->vmcb;
  204. }
  205. static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
  206. {
  207. struct vmcb *vmcb = get_host_vmcb(svm);
  208. vmcb->control.intercept_cr |= (1U << bit);
  209. recalc_intercepts(svm);
  210. }
  211. static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
  212. {
  213. struct vmcb *vmcb = get_host_vmcb(svm);
  214. vmcb->control.intercept_cr &= ~(1U << bit);
  215. recalc_intercepts(svm);
  216. }
  217. static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
  218. {
  219. struct vmcb *vmcb = get_host_vmcb(svm);
  220. return vmcb->control.intercept_cr & (1U << bit);
  221. }
  222. static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
  223. {
  224. struct vmcb *vmcb = get_host_vmcb(svm);
  225. vmcb->control.intercept_dr |= (1U << bit);
  226. recalc_intercepts(svm);
  227. }
  228. static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
  229. {
  230. struct vmcb *vmcb = get_host_vmcb(svm);
  231. vmcb->control.intercept_dr &= ~(1U << bit);
  232. recalc_intercepts(svm);
  233. }
  234. static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
  235. {
  236. struct vmcb *vmcb = get_host_vmcb(svm);
  237. vmcb->control.intercept_exceptions |= (1U << bit);
  238. recalc_intercepts(svm);
  239. }
  240. static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
  241. {
  242. struct vmcb *vmcb = get_host_vmcb(svm);
  243. vmcb->control.intercept_exceptions &= ~(1U << bit);
  244. recalc_intercepts(svm);
  245. }
  246. static inline void set_intercept(struct vcpu_svm *svm, int bit)
  247. {
  248. struct vmcb *vmcb = get_host_vmcb(svm);
  249. vmcb->control.intercept |= (1ULL << bit);
  250. recalc_intercepts(svm);
  251. }
  252. static inline void clr_intercept(struct vcpu_svm *svm, int bit)
  253. {
  254. struct vmcb *vmcb = get_host_vmcb(svm);
  255. vmcb->control.intercept &= ~(1ULL << bit);
  256. recalc_intercepts(svm);
  257. }
  258. static inline void enable_gif(struct vcpu_svm *svm)
  259. {
  260. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  261. }
  262. static inline void disable_gif(struct vcpu_svm *svm)
  263. {
  264. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  265. }
  266. static inline bool gif_set(struct vcpu_svm *svm)
  267. {
  268. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  269. }
  270. static unsigned long iopm_base;
  271. struct kvm_ldttss_desc {
  272. u16 limit0;
  273. u16 base0;
  274. unsigned base1:8, type:5, dpl:2, p:1;
  275. unsigned limit1:4, zero0:3, g:1, base2:8;
  276. u32 base3;
  277. u32 zero1;
  278. } __attribute__((packed));
  279. struct svm_cpu_data {
  280. int cpu;
  281. u64 asid_generation;
  282. u32 max_asid;
  283. u32 next_asid;
  284. struct kvm_ldttss_desc *tss_desc;
  285. struct page *save_area;
  286. };
  287. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  288. static uint32_t svm_features;
  289. struct svm_init_data {
  290. int cpu;
  291. int r;
  292. };
  293. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  294. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  295. #define MSRS_RANGE_SIZE 2048
  296. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  297. static u32 svm_msrpm_offset(u32 msr)
  298. {
  299. u32 offset;
  300. int i;
  301. for (i = 0; i < NUM_MSR_MAPS; i++) {
  302. if (msr < msrpm_ranges[i] ||
  303. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  304. continue;
  305. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  306. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  307. /* Now we have the u8 offset - but need the u32 offset */
  308. return offset / 4;
  309. }
  310. /* MSR not in any range */
  311. return MSR_INVALID;
  312. }
  313. #define MAX_INST_SIZE 15
  314. static inline void clgi(void)
  315. {
  316. asm volatile (__ex(SVM_CLGI));
  317. }
  318. static inline void stgi(void)
  319. {
  320. asm volatile (__ex(SVM_STGI));
  321. }
  322. static inline void invlpga(unsigned long addr, u32 asid)
  323. {
  324. asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
  325. }
  326. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  327. {
  328. to_svm(vcpu)->asid_generation--;
  329. }
  330. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  331. {
  332. force_new_asid(vcpu);
  333. }
  334. static int get_npt_level(void)
  335. {
  336. #ifdef CONFIG_X86_64
  337. return PT64_ROOT_LEVEL;
  338. #else
  339. return PT32E_ROOT_LEVEL;
  340. #endif
  341. }
  342. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  343. {
  344. vcpu->arch.efer = efer;
  345. if (!npt_enabled && !(efer & EFER_LMA))
  346. efer &= ~EFER_LME;
  347. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  348. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  349. }
  350. static int is_external_interrupt(u32 info)
  351. {
  352. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  353. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  354. }
  355. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  356. {
  357. struct vcpu_svm *svm = to_svm(vcpu);
  358. u32 ret = 0;
  359. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  360. ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  361. return ret & mask;
  362. }
  363. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  364. {
  365. struct vcpu_svm *svm = to_svm(vcpu);
  366. if (mask == 0)
  367. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  368. else
  369. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  370. }
  371. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  372. {
  373. struct vcpu_svm *svm = to_svm(vcpu);
  374. if (svm->vmcb->control.next_rip != 0)
  375. svm->next_rip = svm->vmcb->control.next_rip;
  376. if (!svm->next_rip) {
  377. if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
  378. EMULATE_DONE)
  379. printk(KERN_DEBUG "%s: NOP\n", __func__);
  380. return;
  381. }
  382. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  383. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  384. __func__, kvm_rip_read(vcpu), svm->next_rip);
  385. kvm_rip_write(vcpu, svm->next_rip);
  386. svm_set_interrupt_shadow(vcpu, 0);
  387. }
  388. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  389. bool has_error_code, u32 error_code,
  390. bool reinject)
  391. {
  392. struct vcpu_svm *svm = to_svm(vcpu);
  393. /*
  394. * If we are within a nested VM we'd better #VMEXIT and let the guest
  395. * handle the exception
  396. */
  397. if (!reinject &&
  398. nested_svm_check_exception(svm, nr, has_error_code, error_code))
  399. return;
  400. if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
  401. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  402. /*
  403. * For guest debugging where we have to reinject #BP if some
  404. * INT3 is guest-owned:
  405. * Emulate nRIP by moving RIP forward. Will fail if injection
  406. * raises a fault that is not intercepted. Still better than
  407. * failing in all cases.
  408. */
  409. skip_emulated_instruction(&svm->vcpu);
  410. rip = kvm_rip_read(&svm->vcpu);
  411. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  412. svm->int3_injected = rip - old_rip;
  413. }
  414. svm->vmcb->control.event_inj = nr
  415. | SVM_EVTINJ_VALID
  416. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  417. | SVM_EVTINJ_TYPE_EXEPT;
  418. svm->vmcb->control.event_inj_err = error_code;
  419. }
  420. static void svm_init_erratum_383(void)
  421. {
  422. u32 low, high;
  423. int err;
  424. u64 val;
  425. if (!cpu_has_amd_erratum(amd_erratum_383))
  426. return;
  427. /* Use _safe variants to not break nested virtualization */
  428. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  429. if (err)
  430. return;
  431. val |= (1ULL << 47);
  432. low = lower_32_bits(val);
  433. high = upper_32_bits(val);
  434. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  435. erratum_383_found = true;
  436. }
  437. static int has_svm(void)
  438. {
  439. const char *msg;
  440. if (!cpu_has_svm(&msg)) {
  441. printk(KERN_INFO "has_svm: %s\n", msg);
  442. return 0;
  443. }
  444. return 1;
  445. }
  446. static void svm_hardware_disable(void *garbage)
  447. {
  448. cpu_svm_disable();
  449. }
  450. static int svm_hardware_enable(void *garbage)
  451. {
  452. struct svm_cpu_data *sd;
  453. uint64_t efer;
  454. struct desc_ptr gdt_descr;
  455. struct desc_struct *gdt;
  456. int me = raw_smp_processor_id();
  457. rdmsrl(MSR_EFER, efer);
  458. if (efer & EFER_SVME)
  459. return -EBUSY;
  460. if (!has_svm()) {
  461. printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
  462. me);
  463. return -EINVAL;
  464. }
  465. sd = per_cpu(svm_data, me);
  466. if (!sd) {
  467. printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
  468. me);
  469. return -EINVAL;
  470. }
  471. sd->asid_generation = 1;
  472. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  473. sd->next_asid = sd->max_asid + 1;
  474. native_store_gdt(&gdt_descr);
  475. gdt = (struct desc_struct *)gdt_descr.address;
  476. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  477. wrmsrl(MSR_EFER, efer | EFER_SVME);
  478. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  479. svm_init_erratum_383();
  480. return 0;
  481. }
  482. static void svm_cpu_uninit(int cpu)
  483. {
  484. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  485. if (!sd)
  486. return;
  487. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  488. __free_page(sd->save_area);
  489. kfree(sd);
  490. }
  491. static int svm_cpu_init(int cpu)
  492. {
  493. struct svm_cpu_data *sd;
  494. int r;
  495. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  496. if (!sd)
  497. return -ENOMEM;
  498. sd->cpu = cpu;
  499. sd->save_area = alloc_page(GFP_KERNEL);
  500. r = -ENOMEM;
  501. if (!sd->save_area)
  502. goto err_1;
  503. per_cpu(svm_data, cpu) = sd;
  504. return 0;
  505. err_1:
  506. kfree(sd);
  507. return r;
  508. }
  509. static bool valid_msr_intercept(u32 index)
  510. {
  511. int i;
  512. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  513. if (direct_access_msrs[i].index == index)
  514. return true;
  515. return false;
  516. }
  517. static void set_msr_interception(u32 *msrpm, unsigned msr,
  518. int read, int write)
  519. {
  520. u8 bit_read, bit_write;
  521. unsigned long tmp;
  522. u32 offset;
  523. /*
  524. * If this warning triggers extend the direct_access_msrs list at the
  525. * beginning of the file
  526. */
  527. WARN_ON(!valid_msr_intercept(msr));
  528. offset = svm_msrpm_offset(msr);
  529. bit_read = 2 * (msr & 0x0f);
  530. bit_write = 2 * (msr & 0x0f) + 1;
  531. tmp = msrpm[offset];
  532. BUG_ON(offset == MSR_INVALID);
  533. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  534. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  535. msrpm[offset] = tmp;
  536. }
  537. static void svm_vcpu_init_msrpm(u32 *msrpm)
  538. {
  539. int i;
  540. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  541. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  542. if (!direct_access_msrs[i].always)
  543. continue;
  544. set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
  545. }
  546. }
  547. static void add_msr_offset(u32 offset)
  548. {
  549. int i;
  550. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  551. /* Offset already in list? */
  552. if (msrpm_offsets[i] == offset)
  553. return;
  554. /* Slot used by another offset? */
  555. if (msrpm_offsets[i] != MSR_INVALID)
  556. continue;
  557. /* Add offset to list */
  558. msrpm_offsets[i] = offset;
  559. return;
  560. }
  561. /*
  562. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  563. * increase MSRPM_OFFSETS in this case.
  564. */
  565. BUG();
  566. }
  567. static void init_msrpm_offsets(void)
  568. {
  569. int i;
  570. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  571. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  572. u32 offset;
  573. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  574. BUG_ON(offset == MSR_INVALID);
  575. add_msr_offset(offset);
  576. }
  577. }
  578. static void svm_enable_lbrv(struct vcpu_svm *svm)
  579. {
  580. u32 *msrpm = svm->msrpm;
  581. svm->vmcb->control.lbr_ctl = 1;
  582. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  583. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  584. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  585. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  586. }
  587. static void svm_disable_lbrv(struct vcpu_svm *svm)
  588. {
  589. u32 *msrpm = svm->msrpm;
  590. svm->vmcb->control.lbr_ctl = 0;
  591. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  592. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  593. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  594. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  595. }
  596. static __init int svm_hardware_setup(void)
  597. {
  598. int cpu;
  599. struct page *iopm_pages;
  600. void *iopm_va;
  601. int r;
  602. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  603. if (!iopm_pages)
  604. return -ENOMEM;
  605. iopm_va = page_address(iopm_pages);
  606. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  607. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  608. init_msrpm_offsets();
  609. if (boot_cpu_has(X86_FEATURE_NX))
  610. kvm_enable_efer_bits(EFER_NX);
  611. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  612. kvm_enable_efer_bits(EFER_FFXSR);
  613. if (nested) {
  614. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  615. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  616. }
  617. for_each_possible_cpu(cpu) {
  618. r = svm_cpu_init(cpu);
  619. if (r)
  620. goto err;
  621. }
  622. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  623. if (!boot_cpu_has(X86_FEATURE_NPT))
  624. npt_enabled = false;
  625. if (npt_enabled && !npt) {
  626. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  627. npt_enabled = false;
  628. }
  629. if (npt_enabled) {
  630. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  631. kvm_enable_tdp();
  632. } else
  633. kvm_disable_tdp();
  634. return 0;
  635. err:
  636. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  637. iopm_base = 0;
  638. return r;
  639. }
  640. static __exit void svm_hardware_unsetup(void)
  641. {
  642. int cpu;
  643. for_each_possible_cpu(cpu)
  644. svm_cpu_uninit(cpu);
  645. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  646. iopm_base = 0;
  647. }
  648. static void init_seg(struct vmcb_seg *seg)
  649. {
  650. seg->selector = 0;
  651. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  652. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  653. seg->limit = 0xffff;
  654. seg->base = 0;
  655. }
  656. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  657. {
  658. seg->selector = 0;
  659. seg->attrib = SVM_SELECTOR_P_MASK | type;
  660. seg->limit = 0xffff;
  661. seg->base = 0;
  662. }
  663. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  664. {
  665. struct vcpu_svm *svm = to_svm(vcpu);
  666. u64 g_tsc_offset = 0;
  667. if (is_guest_mode(vcpu)) {
  668. g_tsc_offset = svm->vmcb->control.tsc_offset -
  669. svm->nested.hsave->control.tsc_offset;
  670. svm->nested.hsave->control.tsc_offset = offset;
  671. }
  672. svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
  673. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  674. }
  675. static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
  676. {
  677. struct vcpu_svm *svm = to_svm(vcpu);
  678. svm->vmcb->control.tsc_offset += adjustment;
  679. if (is_guest_mode(vcpu))
  680. svm->nested.hsave->control.tsc_offset += adjustment;
  681. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  682. }
  683. static void init_vmcb(struct vcpu_svm *svm)
  684. {
  685. struct vmcb_control_area *control = &svm->vmcb->control;
  686. struct vmcb_save_area *save = &svm->vmcb->save;
  687. svm->vcpu.fpu_active = 1;
  688. svm->vcpu.arch.hflags = 0;
  689. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  690. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  691. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  692. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  693. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  694. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  695. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  696. set_dr_intercept(svm, INTERCEPT_DR0_READ);
  697. set_dr_intercept(svm, INTERCEPT_DR1_READ);
  698. set_dr_intercept(svm, INTERCEPT_DR2_READ);
  699. set_dr_intercept(svm, INTERCEPT_DR3_READ);
  700. set_dr_intercept(svm, INTERCEPT_DR4_READ);
  701. set_dr_intercept(svm, INTERCEPT_DR5_READ);
  702. set_dr_intercept(svm, INTERCEPT_DR6_READ);
  703. set_dr_intercept(svm, INTERCEPT_DR7_READ);
  704. set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
  705. set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
  706. set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
  707. set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
  708. set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
  709. set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
  710. set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
  711. set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
  712. set_exception_intercept(svm, PF_VECTOR);
  713. set_exception_intercept(svm, UD_VECTOR);
  714. set_exception_intercept(svm, MC_VECTOR);
  715. set_intercept(svm, INTERCEPT_INTR);
  716. set_intercept(svm, INTERCEPT_NMI);
  717. set_intercept(svm, INTERCEPT_SMI);
  718. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  719. set_intercept(svm, INTERCEPT_CPUID);
  720. set_intercept(svm, INTERCEPT_INVD);
  721. set_intercept(svm, INTERCEPT_HLT);
  722. set_intercept(svm, INTERCEPT_INVLPG);
  723. set_intercept(svm, INTERCEPT_INVLPGA);
  724. set_intercept(svm, INTERCEPT_IOIO_PROT);
  725. set_intercept(svm, INTERCEPT_MSR_PROT);
  726. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  727. set_intercept(svm, INTERCEPT_SHUTDOWN);
  728. set_intercept(svm, INTERCEPT_VMRUN);
  729. set_intercept(svm, INTERCEPT_VMMCALL);
  730. set_intercept(svm, INTERCEPT_VMLOAD);
  731. set_intercept(svm, INTERCEPT_VMSAVE);
  732. set_intercept(svm, INTERCEPT_STGI);
  733. set_intercept(svm, INTERCEPT_CLGI);
  734. set_intercept(svm, INTERCEPT_SKINIT);
  735. set_intercept(svm, INTERCEPT_WBINVD);
  736. set_intercept(svm, INTERCEPT_MONITOR);
  737. set_intercept(svm, INTERCEPT_MWAIT);
  738. control->iopm_base_pa = iopm_base;
  739. control->msrpm_base_pa = __pa(svm->msrpm);
  740. control->int_ctl = V_INTR_MASKING_MASK;
  741. init_seg(&save->es);
  742. init_seg(&save->ss);
  743. init_seg(&save->ds);
  744. init_seg(&save->fs);
  745. init_seg(&save->gs);
  746. save->cs.selector = 0xf000;
  747. /* Executable/Readable Code Segment */
  748. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  749. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  750. save->cs.limit = 0xffff;
  751. /*
  752. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  753. * be consistent with it.
  754. *
  755. * Replace when we have real mode working for vmx.
  756. */
  757. save->cs.base = 0xf0000;
  758. save->gdtr.limit = 0xffff;
  759. save->idtr.limit = 0xffff;
  760. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  761. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  762. svm_set_efer(&svm->vcpu, 0);
  763. save->dr6 = 0xffff0ff0;
  764. save->dr7 = 0x400;
  765. save->rflags = 2;
  766. save->rip = 0x0000fff0;
  767. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  768. /*
  769. * This is the guest-visible cr0 value.
  770. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  771. */
  772. svm->vcpu.arch.cr0 = 0;
  773. (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  774. save->cr4 = X86_CR4_PAE;
  775. /* rdx = ?? */
  776. if (npt_enabled) {
  777. /* Setup VMCB for Nested Paging */
  778. control->nested_ctl = 1;
  779. clr_intercept(svm, INTERCEPT_TASK_SWITCH);
  780. clr_intercept(svm, INTERCEPT_INVLPG);
  781. clr_exception_intercept(svm, PF_VECTOR);
  782. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  783. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  784. save->g_pat = 0x0007040600070406ULL;
  785. save->cr3 = 0;
  786. save->cr4 = 0;
  787. }
  788. force_new_asid(&svm->vcpu);
  789. svm->nested.vmcb = 0;
  790. svm->vcpu.arch.hflags = 0;
  791. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  792. control->pause_filter_count = 3000;
  793. set_intercept(svm, INTERCEPT_PAUSE);
  794. }
  795. mark_all_dirty(svm->vmcb);
  796. enable_gif(svm);
  797. }
  798. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  799. {
  800. struct vcpu_svm *svm = to_svm(vcpu);
  801. init_vmcb(svm);
  802. if (!kvm_vcpu_is_bsp(vcpu)) {
  803. kvm_rip_write(vcpu, 0);
  804. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  805. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  806. }
  807. vcpu->arch.regs_avail = ~0;
  808. vcpu->arch.regs_dirty = ~0;
  809. return 0;
  810. }
  811. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  812. {
  813. struct vcpu_svm *svm;
  814. struct page *page;
  815. struct page *msrpm_pages;
  816. struct page *hsave_page;
  817. struct page *nested_msrpm_pages;
  818. int err;
  819. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  820. if (!svm) {
  821. err = -ENOMEM;
  822. goto out;
  823. }
  824. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  825. if (err)
  826. goto free_svm;
  827. err = -ENOMEM;
  828. page = alloc_page(GFP_KERNEL);
  829. if (!page)
  830. goto uninit;
  831. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  832. if (!msrpm_pages)
  833. goto free_page1;
  834. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  835. if (!nested_msrpm_pages)
  836. goto free_page2;
  837. hsave_page = alloc_page(GFP_KERNEL);
  838. if (!hsave_page)
  839. goto free_page3;
  840. svm->nested.hsave = page_address(hsave_page);
  841. svm->msrpm = page_address(msrpm_pages);
  842. svm_vcpu_init_msrpm(svm->msrpm);
  843. svm->nested.msrpm = page_address(nested_msrpm_pages);
  844. svm_vcpu_init_msrpm(svm->nested.msrpm);
  845. svm->vmcb = page_address(page);
  846. clear_page(svm->vmcb);
  847. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  848. svm->asid_generation = 0;
  849. init_vmcb(svm);
  850. kvm_write_tsc(&svm->vcpu, 0);
  851. err = fx_init(&svm->vcpu);
  852. if (err)
  853. goto free_page4;
  854. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  855. if (kvm_vcpu_is_bsp(&svm->vcpu))
  856. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  857. return &svm->vcpu;
  858. free_page4:
  859. __free_page(hsave_page);
  860. free_page3:
  861. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  862. free_page2:
  863. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  864. free_page1:
  865. __free_page(page);
  866. uninit:
  867. kvm_vcpu_uninit(&svm->vcpu);
  868. free_svm:
  869. kmem_cache_free(kvm_vcpu_cache, svm);
  870. out:
  871. return ERR_PTR(err);
  872. }
  873. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  874. {
  875. struct vcpu_svm *svm = to_svm(vcpu);
  876. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  877. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  878. __free_page(virt_to_page(svm->nested.hsave));
  879. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  880. kvm_vcpu_uninit(vcpu);
  881. kmem_cache_free(kvm_vcpu_cache, svm);
  882. }
  883. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  884. {
  885. struct vcpu_svm *svm = to_svm(vcpu);
  886. int i;
  887. if (unlikely(cpu != vcpu->cpu)) {
  888. svm->asid_generation = 0;
  889. mark_all_dirty(svm->vmcb);
  890. }
  891. #ifdef CONFIG_X86_64
  892. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  893. #endif
  894. savesegment(fs, svm->host.fs);
  895. savesegment(gs, svm->host.gs);
  896. svm->host.ldt = kvm_read_ldt();
  897. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  898. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  899. }
  900. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  901. {
  902. struct vcpu_svm *svm = to_svm(vcpu);
  903. int i;
  904. ++vcpu->stat.host_state_reload;
  905. kvm_load_ldt(svm->host.ldt);
  906. #ifdef CONFIG_X86_64
  907. loadsegment(fs, svm->host.fs);
  908. load_gs_index(svm->host.gs);
  909. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
  910. #else
  911. loadsegment(gs, svm->host.gs);
  912. #endif
  913. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  914. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  915. }
  916. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  917. {
  918. return to_svm(vcpu)->vmcb->save.rflags;
  919. }
  920. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  921. {
  922. to_svm(vcpu)->vmcb->save.rflags = rflags;
  923. }
  924. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  925. {
  926. switch (reg) {
  927. case VCPU_EXREG_PDPTR:
  928. BUG_ON(!npt_enabled);
  929. load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
  930. break;
  931. default:
  932. BUG();
  933. }
  934. }
  935. static void svm_set_vintr(struct vcpu_svm *svm)
  936. {
  937. set_intercept(svm, INTERCEPT_VINTR);
  938. }
  939. static void svm_clear_vintr(struct vcpu_svm *svm)
  940. {
  941. clr_intercept(svm, INTERCEPT_VINTR);
  942. }
  943. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  944. {
  945. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  946. switch (seg) {
  947. case VCPU_SREG_CS: return &save->cs;
  948. case VCPU_SREG_DS: return &save->ds;
  949. case VCPU_SREG_ES: return &save->es;
  950. case VCPU_SREG_FS: return &save->fs;
  951. case VCPU_SREG_GS: return &save->gs;
  952. case VCPU_SREG_SS: return &save->ss;
  953. case VCPU_SREG_TR: return &save->tr;
  954. case VCPU_SREG_LDTR: return &save->ldtr;
  955. }
  956. BUG();
  957. return NULL;
  958. }
  959. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  960. {
  961. struct vmcb_seg *s = svm_seg(vcpu, seg);
  962. return s->base;
  963. }
  964. static void svm_get_segment(struct kvm_vcpu *vcpu,
  965. struct kvm_segment *var, int seg)
  966. {
  967. struct vmcb_seg *s = svm_seg(vcpu, seg);
  968. var->base = s->base;
  969. var->limit = s->limit;
  970. var->selector = s->selector;
  971. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  972. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  973. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  974. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  975. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  976. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  977. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  978. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  979. /*
  980. * AMD's VMCB does not have an explicit unusable field, so emulate it
  981. * for cross vendor migration purposes by "not present"
  982. */
  983. var->unusable = !var->present || (var->type == 0);
  984. switch (seg) {
  985. case VCPU_SREG_CS:
  986. /*
  987. * SVM always stores 0 for the 'G' bit in the CS selector in
  988. * the VMCB on a VMEXIT. This hurts cross-vendor migration:
  989. * Intel's VMENTRY has a check on the 'G' bit.
  990. */
  991. var->g = s->limit > 0xfffff;
  992. break;
  993. case VCPU_SREG_TR:
  994. /*
  995. * Work around a bug where the busy flag in the tr selector
  996. * isn't exposed
  997. */
  998. var->type |= 0x2;
  999. break;
  1000. case VCPU_SREG_DS:
  1001. case VCPU_SREG_ES:
  1002. case VCPU_SREG_FS:
  1003. case VCPU_SREG_GS:
  1004. /*
  1005. * The accessed bit must always be set in the segment
  1006. * descriptor cache, although it can be cleared in the
  1007. * descriptor, the cached bit always remains at 1. Since
  1008. * Intel has a check on this, set it here to support
  1009. * cross-vendor migration.
  1010. */
  1011. if (!var->unusable)
  1012. var->type |= 0x1;
  1013. break;
  1014. case VCPU_SREG_SS:
  1015. /*
  1016. * On AMD CPUs sometimes the DB bit in the segment
  1017. * descriptor is left as 1, although the whole segment has
  1018. * been made unusable. Clear it here to pass an Intel VMX
  1019. * entry check when cross vendor migrating.
  1020. */
  1021. if (var->unusable)
  1022. var->db = 0;
  1023. break;
  1024. }
  1025. }
  1026. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1027. {
  1028. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1029. return save->cpl;
  1030. }
  1031. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1032. {
  1033. struct vcpu_svm *svm = to_svm(vcpu);
  1034. dt->size = svm->vmcb->save.idtr.limit;
  1035. dt->address = svm->vmcb->save.idtr.base;
  1036. }
  1037. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1038. {
  1039. struct vcpu_svm *svm = to_svm(vcpu);
  1040. svm->vmcb->save.idtr.limit = dt->size;
  1041. svm->vmcb->save.idtr.base = dt->address ;
  1042. mark_dirty(svm->vmcb, VMCB_DT);
  1043. }
  1044. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1045. {
  1046. struct vcpu_svm *svm = to_svm(vcpu);
  1047. dt->size = svm->vmcb->save.gdtr.limit;
  1048. dt->address = svm->vmcb->save.gdtr.base;
  1049. }
  1050. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1051. {
  1052. struct vcpu_svm *svm = to_svm(vcpu);
  1053. svm->vmcb->save.gdtr.limit = dt->size;
  1054. svm->vmcb->save.gdtr.base = dt->address ;
  1055. mark_dirty(svm->vmcb, VMCB_DT);
  1056. }
  1057. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1058. {
  1059. }
  1060. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1061. {
  1062. }
  1063. static void update_cr0_intercept(struct vcpu_svm *svm)
  1064. {
  1065. ulong gcr0 = svm->vcpu.arch.cr0;
  1066. u64 *hcr0 = &svm->vmcb->save.cr0;
  1067. if (!svm->vcpu.fpu_active)
  1068. *hcr0 |= SVM_CR0_SELECTIVE_MASK;
  1069. else
  1070. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1071. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1072. mark_dirty(svm->vmcb, VMCB_CR);
  1073. if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
  1074. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  1075. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1076. } else {
  1077. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1078. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1079. }
  1080. }
  1081. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1082. {
  1083. struct vcpu_svm *svm = to_svm(vcpu);
  1084. if (is_guest_mode(vcpu)) {
  1085. /*
  1086. * We are here because we run in nested mode, the host kvm
  1087. * intercepts cr0 writes but the l1 hypervisor does not.
  1088. * But the L1 hypervisor may intercept selective cr0 writes.
  1089. * This needs to be checked here.
  1090. */
  1091. unsigned long old, new;
  1092. /* Remove bits that would trigger a real cr0 write intercept */
  1093. old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
  1094. new = cr0 & SVM_CR0_SELECTIVE_MASK;
  1095. if (old == new) {
  1096. /* cr0 write with ts and mp unchanged */
  1097. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  1098. if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
  1099. svm->nested.vmexit_rip = kvm_rip_read(vcpu);
  1100. svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  1101. svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  1102. return;
  1103. }
  1104. }
  1105. }
  1106. #ifdef CONFIG_X86_64
  1107. if (vcpu->arch.efer & EFER_LME) {
  1108. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1109. vcpu->arch.efer |= EFER_LMA;
  1110. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1111. }
  1112. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1113. vcpu->arch.efer &= ~EFER_LMA;
  1114. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1115. }
  1116. }
  1117. #endif
  1118. vcpu->arch.cr0 = cr0;
  1119. if (!npt_enabled)
  1120. cr0 |= X86_CR0_PG | X86_CR0_WP;
  1121. if (!vcpu->fpu_active)
  1122. cr0 |= X86_CR0_TS;
  1123. /*
  1124. * re-enable caching here because the QEMU bios
  1125. * does not do it - this results in some delay at
  1126. * reboot
  1127. */
  1128. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1129. svm->vmcb->save.cr0 = cr0;
  1130. mark_dirty(svm->vmcb, VMCB_CR);
  1131. update_cr0_intercept(svm);
  1132. }
  1133. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1134. {
  1135. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  1136. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  1137. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1138. force_new_asid(vcpu);
  1139. vcpu->arch.cr4 = cr4;
  1140. if (!npt_enabled)
  1141. cr4 |= X86_CR4_PAE;
  1142. cr4 |= host_cr4_mce;
  1143. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1144. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  1145. }
  1146. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1147. struct kvm_segment *var, int seg)
  1148. {
  1149. struct vcpu_svm *svm = to_svm(vcpu);
  1150. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1151. s->base = var->base;
  1152. s->limit = var->limit;
  1153. s->selector = var->selector;
  1154. if (var->unusable)
  1155. s->attrib = 0;
  1156. else {
  1157. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1158. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1159. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1160. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  1161. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1162. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1163. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1164. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1165. }
  1166. if (seg == VCPU_SREG_CS)
  1167. svm->vmcb->save.cpl
  1168. = (svm->vmcb->save.cs.attrib
  1169. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1170. mark_dirty(svm->vmcb, VMCB_SEG);
  1171. }
  1172. static void update_db_intercept(struct kvm_vcpu *vcpu)
  1173. {
  1174. struct vcpu_svm *svm = to_svm(vcpu);
  1175. clr_exception_intercept(svm, DB_VECTOR);
  1176. clr_exception_intercept(svm, BP_VECTOR);
  1177. if (svm->nmi_singlestep)
  1178. set_exception_intercept(svm, DB_VECTOR);
  1179. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1180. if (vcpu->guest_debug &
  1181. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  1182. set_exception_intercept(svm, DB_VECTOR);
  1183. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1184. set_exception_intercept(svm, BP_VECTOR);
  1185. } else
  1186. vcpu->guest_debug = 0;
  1187. }
  1188. static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1189. {
  1190. struct vcpu_svm *svm = to_svm(vcpu);
  1191. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1192. svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
  1193. else
  1194. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  1195. mark_dirty(svm->vmcb, VMCB_DR);
  1196. update_db_intercept(vcpu);
  1197. }
  1198. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1199. {
  1200. if (sd->next_asid > sd->max_asid) {
  1201. ++sd->asid_generation;
  1202. sd->next_asid = 1;
  1203. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1204. }
  1205. svm->asid_generation = sd->asid_generation;
  1206. svm->vmcb->control.asid = sd->next_asid++;
  1207. mark_dirty(svm->vmcb, VMCB_ASID);
  1208. }
  1209. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1210. {
  1211. struct vcpu_svm *svm = to_svm(vcpu);
  1212. svm->vmcb->save.dr7 = value;
  1213. mark_dirty(svm->vmcb, VMCB_DR);
  1214. }
  1215. static int pf_interception(struct vcpu_svm *svm)
  1216. {
  1217. u64 fault_address = svm->vmcb->control.exit_info_2;
  1218. u32 error_code;
  1219. int r = 1;
  1220. switch (svm->apf_reason) {
  1221. default:
  1222. error_code = svm->vmcb->control.exit_info_1;
  1223. trace_kvm_page_fault(fault_address, error_code);
  1224. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1225. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1226. r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  1227. break;
  1228. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  1229. svm->apf_reason = 0;
  1230. local_irq_disable();
  1231. kvm_async_pf_task_wait(fault_address);
  1232. local_irq_enable();
  1233. break;
  1234. case KVM_PV_REASON_PAGE_READY:
  1235. svm->apf_reason = 0;
  1236. local_irq_disable();
  1237. kvm_async_pf_task_wake(fault_address);
  1238. local_irq_enable();
  1239. break;
  1240. }
  1241. return r;
  1242. }
  1243. static int db_interception(struct vcpu_svm *svm)
  1244. {
  1245. struct kvm_run *kvm_run = svm->vcpu.run;
  1246. if (!(svm->vcpu.guest_debug &
  1247. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1248. !svm->nmi_singlestep) {
  1249. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1250. return 1;
  1251. }
  1252. if (svm->nmi_singlestep) {
  1253. svm->nmi_singlestep = false;
  1254. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  1255. svm->vmcb->save.rflags &=
  1256. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1257. update_db_intercept(&svm->vcpu);
  1258. }
  1259. if (svm->vcpu.guest_debug &
  1260. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1261. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1262. kvm_run->debug.arch.pc =
  1263. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1264. kvm_run->debug.arch.exception = DB_VECTOR;
  1265. return 0;
  1266. }
  1267. return 1;
  1268. }
  1269. static int bp_interception(struct vcpu_svm *svm)
  1270. {
  1271. struct kvm_run *kvm_run = svm->vcpu.run;
  1272. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1273. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1274. kvm_run->debug.arch.exception = BP_VECTOR;
  1275. return 0;
  1276. }
  1277. static int ud_interception(struct vcpu_svm *svm)
  1278. {
  1279. int er;
  1280. er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
  1281. if (er != EMULATE_DONE)
  1282. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1283. return 1;
  1284. }
  1285. static void svm_fpu_activate(struct kvm_vcpu *vcpu)
  1286. {
  1287. struct vcpu_svm *svm = to_svm(vcpu);
  1288. clr_exception_intercept(svm, NM_VECTOR);
  1289. svm->vcpu.fpu_active = 1;
  1290. update_cr0_intercept(svm);
  1291. }
  1292. static int nm_interception(struct vcpu_svm *svm)
  1293. {
  1294. svm_fpu_activate(&svm->vcpu);
  1295. return 1;
  1296. }
  1297. static bool is_erratum_383(void)
  1298. {
  1299. int err, i;
  1300. u64 value;
  1301. if (!erratum_383_found)
  1302. return false;
  1303. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  1304. if (err)
  1305. return false;
  1306. /* Bit 62 may or may not be set for this mce */
  1307. value &= ~(1ULL << 62);
  1308. if (value != 0xb600000000010015ULL)
  1309. return false;
  1310. /* Clear MCi_STATUS registers */
  1311. for (i = 0; i < 6; ++i)
  1312. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  1313. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  1314. if (!err) {
  1315. u32 low, high;
  1316. value &= ~(1ULL << 2);
  1317. low = lower_32_bits(value);
  1318. high = upper_32_bits(value);
  1319. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  1320. }
  1321. /* Flush tlb to evict multi-match entries */
  1322. __flush_tlb_all();
  1323. return true;
  1324. }
  1325. static void svm_handle_mce(struct vcpu_svm *svm)
  1326. {
  1327. if (is_erratum_383()) {
  1328. /*
  1329. * Erratum 383 triggered. Guest state is corrupt so kill the
  1330. * guest.
  1331. */
  1332. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  1333. kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
  1334. return;
  1335. }
  1336. /*
  1337. * On an #MC intercept the MCE handler is not called automatically in
  1338. * the host. So do it by hand here.
  1339. */
  1340. asm volatile (
  1341. "int $0x12\n");
  1342. /* not sure if we ever come back to this point */
  1343. return;
  1344. }
  1345. static int mc_interception(struct vcpu_svm *svm)
  1346. {
  1347. return 1;
  1348. }
  1349. static int shutdown_interception(struct vcpu_svm *svm)
  1350. {
  1351. struct kvm_run *kvm_run = svm->vcpu.run;
  1352. /*
  1353. * VMCB is undefined after a SHUTDOWN intercept
  1354. * so reinitialize it.
  1355. */
  1356. clear_page(svm->vmcb);
  1357. init_vmcb(svm);
  1358. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1359. return 0;
  1360. }
  1361. static int io_interception(struct vcpu_svm *svm)
  1362. {
  1363. struct kvm_vcpu *vcpu = &svm->vcpu;
  1364. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1365. int size, in, string;
  1366. unsigned port;
  1367. ++svm->vcpu.stat.io_exits;
  1368. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1369. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1370. if (string || in)
  1371. return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
  1372. port = io_info >> 16;
  1373. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1374. svm->next_rip = svm->vmcb->control.exit_info_2;
  1375. skip_emulated_instruction(&svm->vcpu);
  1376. return kvm_fast_pio_out(vcpu, size, port);
  1377. }
  1378. static int nmi_interception(struct vcpu_svm *svm)
  1379. {
  1380. return 1;
  1381. }
  1382. static int intr_interception(struct vcpu_svm *svm)
  1383. {
  1384. ++svm->vcpu.stat.irq_exits;
  1385. return 1;
  1386. }
  1387. static int nop_on_interception(struct vcpu_svm *svm)
  1388. {
  1389. return 1;
  1390. }
  1391. static int halt_interception(struct vcpu_svm *svm)
  1392. {
  1393. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1394. skip_emulated_instruction(&svm->vcpu);
  1395. return kvm_emulate_halt(&svm->vcpu);
  1396. }
  1397. static int vmmcall_interception(struct vcpu_svm *svm)
  1398. {
  1399. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1400. skip_emulated_instruction(&svm->vcpu);
  1401. kvm_emulate_hypercall(&svm->vcpu);
  1402. return 1;
  1403. }
  1404. static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
  1405. {
  1406. struct vcpu_svm *svm = to_svm(vcpu);
  1407. return svm->nested.nested_cr3;
  1408. }
  1409. static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
  1410. unsigned long root)
  1411. {
  1412. struct vcpu_svm *svm = to_svm(vcpu);
  1413. svm->vmcb->control.nested_cr3 = root;
  1414. mark_dirty(svm->vmcb, VMCB_NPT);
  1415. force_new_asid(vcpu);
  1416. }
  1417. static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
  1418. struct x86_exception *fault)
  1419. {
  1420. struct vcpu_svm *svm = to_svm(vcpu);
  1421. svm->vmcb->control.exit_code = SVM_EXIT_NPF;
  1422. svm->vmcb->control.exit_code_hi = 0;
  1423. svm->vmcb->control.exit_info_1 = fault->error_code;
  1424. svm->vmcb->control.exit_info_2 = fault->address;
  1425. nested_svm_vmexit(svm);
  1426. }
  1427. static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
  1428. {
  1429. int r;
  1430. r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
  1431. vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
  1432. vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
  1433. vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
  1434. vcpu->arch.mmu.shadow_root_level = get_npt_level();
  1435. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  1436. return r;
  1437. }
  1438. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  1439. {
  1440. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  1441. }
  1442. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1443. {
  1444. if (!(svm->vcpu.arch.efer & EFER_SVME)
  1445. || !is_paging(&svm->vcpu)) {
  1446. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1447. return 1;
  1448. }
  1449. if (svm->vmcb->save.cpl) {
  1450. kvm_inject_gp(&svm->vcpu, 0);
  1451. return 1;
  1452. }
  1453. return 0;
  1454. }
  1455. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1456. bool has_error_code, u32 error_code)
  1457. {
  1458. int vmexit;
  1459. if (!is_guest_mode(&svm->vcpu))
  1460. return 0;
  1461. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1462. svm->vmcb->control.exit_code_hi = 0;
  1463. svm->vmcb->control.exit_info_1 = error_code;
  1464. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1465. vmexit = nested_svm_intercept(svm);
  1466. if (vmexit == NESTED_EXIT_DONE)
  1467. svm->nested.exit_required = true;
  1468. return vmexit;
  1469. }
  1470. /* This function returns true if it is save to enable the irq window */
  1471. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1472. {
  1473. if (!is_guest_mode(&svm->vcpu))
  1474. return true;
  1475. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1476. return true;
  1477. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1478. return false;
  1479. /*
  1480. * if vmexit was already requested (by intercepted exception
  1481. * for instance) do not overwrite it with "external interrupt"
  1482. * vmexit.
  1483. */
  1484. if (svm->nested.exit_required)
  1485. return false;
  1486. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1487. svm->vmcb->control.exit_info_1 = 0;
  1488. svm->vmcb->control.exit_info_2 = 0;
  1489. if (svm->nested.intercept & 1ULL) {
  1490. /*
  1491. * The #vmexit can't be emulated here directly because this
  1492. * code path runs with irqs and preemtion disabled. A
  1493. * #vmexit emulation might sleep. Only signal request for
  1494. * the #vmexit here.
  1495. */
  1496. svm->nested.exit_required = true;
  1497. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1498. return false;
  1499. }
  1500. return true;
  1501. }
  1502. /* This function returns true if it is save to enable the nmi window */
  1503. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  1504. {
  1505. if (!is_guest_mode(&svm->vcpu))
  1506. return true;
  1507. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  1508. return true;
  1509. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  1510. svm->nested.exit_required = true;
  1511. return false;
  1512. }
  1513. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  1514. {
  1515. struct page *page;
  1516. might_sleep();
  1517. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1518. if (is_error_page(page))
  1519. goto error;
  1520. *_page = page;
  1521. return kmap(page);
  1522. error:
  1523. kvm_release_page_clean(page);
  1524. kvm_inject_gp(&svm->vcpu, 0);
  1525. return NULL;
  1526. }
  1527. static void nested_svm_unmap(struct page *page)
  1528. {
  1529. kunmap(page);
  1530. kvm_release_page_dirty(page);
  1531. }
  1532. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  1533. {
  1534. unsigned port;
  1535. u8 val, bit;
  1536. u64 gpa;
  1537. if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
  1538. return NESTED_EXIT_HOST;
  1539. port = svm->vmcb->control.exit_info_1 >> 16;
  1540. gpa = svm->nested.vmcb_iopm + (port / 8);
  1541. bit = port % 8;
  1542. val = 0;
  1543. if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
  1544. val &= (1 << bit);
  1545. return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1546. }
  1547. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  1548. {
  1549. u32 offset, msr, value;
  1550. int write, mask;
  1551. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1552. return NESTED_EXIT_HOST;
  1553. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1554. offset = svm_msrpm_offset(msr);
  1555. write = svm->vmcb->control.exit_info_1 & 1;
  1556. mask = 1 << ((2 * (msr & 0xf)) + write);
  1557. if (offset == MSR_INVALID)
  1558. return NESTED_EXIT_DONE;
  1559. /* Offset is in 32 bit units but need in 8 bit units */
  1560. offset *= 4;
  1561. if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
  1562. return NESTED_EXIT_DONE;
  1563. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1564. }
  1565. static int nested_svm_exit_special(struct vcpu_svm *svm)
  1566. {
  1567. u32 exit_code = svm->vmcb->control.exit_code;
  1568. switch (exit_code) {
  1569. case SVM_EXIT_INTR:
  1570. case SVM_EXIT_NMI:
  1571. case SVM_EXIT_EXCP_BASE + MC_VECTOR:
  1572. return NESTED_EXIT_HOST;
  1573. case SVM_EXIT_NPF:
  1574. /* For now we are always handling NPFs when using them */
  1575. if (npt_enabled)
  1576. return NESTED_EXIT_HOST;
  1577. break;
  1578. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1579. /* When we're shadowing, trap PFs, but not async PF */
  1580. if (!npt_enabled && svm->apf_reason == 0)
  1581. return NESTED_EXIT_HOST;
  1582. break;
  1583. case SVM_EXIT_EXCP_BASE + NM_VECTOR:
  1584. nm_interception(svm);
  1585. break;
  1586. default:
  1587. break;
  1588. }
  1589. return NESTED_EXIT_CONTINUE;
  1590. }
  1591. /*
  1592. * If this function returns true, this #vmexit was already handled
  1593. */
  1594. static int nested_svm_intercept(struct vcpu_svm *svm)
  1595. {
  1596. u32 exit_code = svm->vmcb->control.exit_code;
  1597. int vmexit = NESTED_EXIT_HOST;
  1598. switch (exit_code) {
  1599. case SVM_EXIT_MSR:
  1600. vmexit = nested_svm_exit_handled_msr(svm);
  1601. break;
  1602. case SVM_EXIT_IOIO:
  1603. vmexit = nested_svm_intercept_ioio(svm);
  1604. break;
  1605. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  1606. u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
  1607. if (svm->nested.intercept_cr & bit)
  1608. vmexit = NESTED_EXIT_DONE;
  1609. break;
  1610. }
  1611. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  1612. u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
  1613. if (svm->nested.intercept_dr & bit)
  1614. vmexit = NESTED_EXIT_DONE;
  1615. break;
  1616. }
  1617. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1618. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1619. if (svm->nested.intercept_exceptions & excp_bits)
  1620. vmexit = NESTED_EXIT_DONE;
  1621. /* async page fault always cause vmexit */
  1622. else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
  1623. svm->apf_reason != 0)
  1624. vmexit = NESTED_EXIT_DONE;
  1625. break;
  1626. }
  1627. case SVM_EXIT_ERR: {
  1628. vmexit = NESTED_EXIT_DONE;
  1629. break;
  1630. }
  1631. default: {
  1632. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1633. if (svm->nested.intercept & exit_bits)
  1634. vmexit = NESTED_EXIT_DONE;
  1635. }
  1636. }
  1637. return vmexit;
  1638. }
  1639. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  1640. {
  1641. int vmexit;
  1642. vmexit = nested_svm_intercept(svm);
  1643. if (vmexit == NESTED_EXIT_DONE)
  1644. nested_svm_vmexit(svm);
  1645. return vmexit;
  1646. }
  1647. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  1648. {
  1649. struct vmcb_control_area *dst = &dst_vmcb->control;
  1650. struct vmcb_control_area *from = &from_vmcb->control;
  1651. dst->intercept_cr = from->intercept_cr;
  1652. dst->intercept_dr = from->intercept_dr;
  1653. dst->intercept_exceptions = from->intercept_exceptions;
  1654. dst->intercept = from->intercept;
  1655. dst->iopm_base_pa = from->iopm_base_pa;
  1656. dst->msrpm_base_pa = from->msrpm_base_pa;
  1657. dst->tsc_offset = from->tsc_offset;
  1658. dst->asid = from->asid;
  1659. dst->tlb_ctl = from->tlb_ctl;
  1660. dst->int_ctl = from->int_ctl;
  1661. dst->int_vector = from->int_vector;
  1662. dst->int_state = from->int_state;
  1663. dst->exit_code = from->exit_code;
  1664. dst->exit_code_hi = from->exit_code_hi;
  1665. dst->exit_info_1 = from->exit_info_1;
  1666. dst->exit_info_2 = from->exit_info_2;
  1667. dst->exit_int_info = from->exit_int_info;
  1668. dst->exit_int_info_err = from->exit_int_info_err;
  1669. dst->nested_ctl = from->nested_ctl;
  1670. dst->event_inj = from->event_inj;
  1671. dst->event_inj_err = from->event_inj_err;
  1672. dst->nested_cr3 = from->nested_cr3;
  1673. dst->lbr_ctl = from->lbr_ctl;
  1674. }
  1675. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1676. {
  1677. struct vmcb *nested_vmcb;
  1678. struct vmcb *hsave = svm->nested.hsave;
  1679. struct vmcb *vmcb = svm->vmcb;
  1680. struct page *page;
  1681. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  1682. vmcb->control.exit_info_1,
  1683. vmcb->control.exit_info_2,
  1684. vmcb->control.exit_int_info,
  1685. vmcb->control.exit_int_info_err);
  1686. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  1687. if (!nested_vmcb)
  1688. return 1;
  1689. /* Exit Guest-Mode */
  1690. leave_guest_mode(&svm->vcpu);
  1691. svm->nested.vmcb = 0;
  1692. /* Give the current vmcb to the guest */
  1693. disable_gif(svm);
  1694. nested_vmcb->save.es = vmcb->save.es;
  1695. nested_vmcb->save.cs = vmcb->save.cs;
  1696. nested_vmcb->save.ss = vmcb->save.ss;
  1697. nested_vmcb->save.ds = vmcb->save.ds;
  1698. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  1699. nested_vmcb->save.idtr = vmcb->save.idtr;
  1700. nested_vmcb->save.efer = svm->vcpu.arch.efer;
  1701. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1702. nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
  1703. nested_vmcb->save.cr2 = vmcb->save.cr2;
  1704. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  1705. nested_vmcb->save.rflags = vmcb->save.rflags;
  1706. nested_vmcb->save.rip = vmcb->save.rip;
  1707. nested_vmcb->save.rsp = vmcb->save.rsp;
  1708. nested_vmcb->save.rax = vmcb->save.rax;
  1709. nested_vmcb->save.dr7 = vmcb->save.dr7;
  1710. nested_vmcb->save.dr6 = vmcb->save.dr6;
  1711. nested_vmcb->save.cpl = vmcb->save.cpl;
  1712. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  1713. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  1714. nested_vmcb->control.int_state = vmcb->control.int_state;
  1715. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  1716. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  1717. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  1718. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  1719. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  1720. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  1721. nested_vmcb->control.next_rip = vmcb->control.next_rip;
  1722. /*
  1723. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  1724. * to make sure that we do not lose injected events. So check event_inj
  1725. * here and copy it to exit_int_info if it is valid.
  1726. * Exit_int_info and event_inj can't be both valid because the case
  1727. * below only happens on a VMRUN instruction intercept which has
  1728. * no valid exit_int_info set.
  1729. */
  1730. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  1731. struct vmcb_control_area *nc = &nested_vmcb->control;
  1732. nc->exit_int_info = vmcb->control.event_inj;
  1733. nc->exit_int_info_err = vmcb->control.event_inj_err;
  1734. }
  1735. nested_vmcb->control.tlb_ctl = 0;
  1736. nested_vmcb->control.event_inj = 0;
  1737. nested_vmcb->control.event_inj_err = 0;
  1738. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1739. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1740. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1741. /* Restore the original control entries */
  1742. copy_vmcb_control_area(vmcb, hsave);
  1743. kvm_clear_exception_queue(&svm->vcpu);
  1744. kvm_clear_interrupt_queue(&svm->vcpu);
  1745. svm->nested.nested_cr3 = 0;
  1746. /* Restore selected save entries */
  1747. svm->vmcb->save.es = hsave->save.es;
  1748. svm->vmcb->save.cs = hsave->save.cs;
  1749. svm->vmcb->save.ss = hsave->save.ss;
  1750. svm->vmcb->save.ds = hsave->save.ds;
  1751. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1752. svm->vmcb->save.idtr = hsave->save.idtr;
  1753. svm->vmcb->save.rflags = hsave->save.rflags;
  1754. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1755. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1756. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1757. if (npt_enabled) {
  1758. svm->vmcb->save.cr3 = hsave->save.cr3;
  1759. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1760. } else {
  1761. (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1762. }
  1763. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1764. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1765. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1766. svm->vmcb->save.dr7 = 0;
  1767. svm->vmcb->save.cpl = 0;
  1768. svm->vmcb->control.exit_int_info = 0;
  1769. mark_all_dirty(svm->vmcb);
  1770. nested_svm_unmap(page);
  1771. nested_svm_uninit_mmu_context(&svm->vcpu);
  1772. kvm_mmu_reset_context(&svm->vcpu);
  1773. kvm_mmu_load(&svm->vcpu);
  1774. return 0;
  1775. }
  1776. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  1777. {
  1778. /*
  1779. * This function merges the msr permission bitmaps of kvm and the
  1780. * nested vmcb. It is omptimized in that it only merges the parts where
  1781. * the kvm msr permission bitmap may contain zero bits
  1782. */
  1783. int i;
  1784. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1785. return true;
  1786. for (i = 0; i < MSRPM_OFFSETS; i++) {
  1787. u32 value, p;
  1788. u64 offset;
  1789. if (msrpm_offsets[i] == 0xffffffff)
  1790. break;
  1791. p = msrpm_offsets[i];
  1792. offset = svm->nested.vmcb_msrpm + (p * 4);
  1793. if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
  1794. return false;
  1795. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  1796. }
  1797. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  1798. return true;
  1799. }
  1800. static bool nested_vmcb_checks(struct vmcb *vmcb)
  1801. {
  1802. if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  1803. return false;
  1804. if (vmcb->control.asid == 0)
  1805. return false;
  1806. if (vmcb->control.nested_ctl && !npt_enabled)
  1807. return false;
  1808. return true;
  1809. }
  1810. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  1811. {
  1812. struct vmcb *nested_vmcb;
  1813. struct vmcb *hsave = svm->nested.hsave;
  1814. struct vmcb *vmcb = svm->vmcb;
  1815. struct page *page;
  1816. u64 vmcb_gpa;
  1817. vmcb_gpa = svm->vmcb->save.rax;
  1818. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1819. if (!nested_vmcb)
  1820. return false;
  1821. if (!nested_vmcb_checks(nested_vmcb)) {
  1822. nested_vmcb->control.exit_code = SVM_EXIT_ERR;
  1823. nested_vmcb->control.exit_code_hi = 0;
  1824. nested_vmcb->control.exit_info_1 = 0;
  1825. nested_vmcb->control.exit_info_2 = 0;
  1826. nested_svm_unmap(page);
  1827. return false;
  1828. }
  1829. trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
  1830. nested_vmcb->save.rip,
  1831. nested_vmcb->control.int_ctl,
  1832. nested_vmcb->control.event_inj,
  1833. nested_vmcb->control.nested_ctl);
  1834. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
  1835. nested_vmcb->control.intercept_cr >> 16,
  1836. nested_vmcb->control.intercept_exceptions,
  1837. nested_vmcb->control.intercept);
  1838. /* Clear internal status */
  1839. kvm_clear_exception_queue(&svm->vcpu);
  1840. kvm_clear_interrupt_queue(&svm->vcpu);
  1841. /*
  1842. * Save the old vmcb, so we don't need to pick what we save, but can
  1843. * restore everything when a VMEXIT occurs
  1844. */
  1845. hsave->save.es = vmcb->save.es;
  1846. hsave->save.cs = vmcb->save.cs;
  1847. hsave->save.ss = vmcb->save.ss;
  1848. hsave->save.ds = vmcb->save.ds;
  1849. hsave->save.gdtr = vmcb->save.gdtr;
  1850. hsave->save.idtr = vmcb->save.idtr;
  1851. hsave->save.efer = svm->vcpu.arch.efer;
  1852. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1853. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1854. hsave->save.rflags = vmcb->save.rflags;
  1855. hsave->save.rip = kvm_rip_read(&svm->vcpu);
  1856. hsave->save.rsp = vmcb->save.rsp;
  1857. hsave->save.rax = vmcb->save.rax;
  1858. if (npt_enabled)
  1859. hsave->save.cr3 = vmcb->save.cr3;
  1860. else
  1861. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1862. copy_vmcb_control_area(hsave, vmcb);
  1863. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1864. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1865. else
  1866. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1867. if (nested_vmcb->control.nested_ctl) {
  1868. kvm_mmu_unload(&svm->vcpu);
  1869. svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
  1870. nested_svm_init_mmu_context(&svm->vcpu);
  1871. }
  1872. /* Load the nested guest state */
  1873. svm->vmcb->save.es = nested_vmcb->save.es;
  1874. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1875. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1876. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1877. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1878. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1879. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1880. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1881. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1882. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1883. if (npt_enabled) {
  1884. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1885. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1886. } else
  1887. (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1888. /* Guest paging mode is active - reset mmu */
  1889. kvm_mmu_reset_context(&svm->vcpu);
  1890. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  1891. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1892. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1893. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1894. /* In case we don't even reach vcpu_run, the fields are not updated */
  1895. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1896. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1897. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1898. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1899. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1900. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1901. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
  1902. svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
  1903. /* cache intercepts */
  1904. svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
  1905. svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
  1906. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  1907. svm->nested.intercept = nested_vmcb->control.intercept;
  1908. force_new_asid(&svm->vcpu);
  1909. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1910. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1911. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1912. else
  1913. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1914. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  1915. /* We only want the cr8 intercept bits of the guest */
  1916. clr_cr_intercept(svm, INTERCEPT_CR8_READ);
  1917. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  1918. }
  1919. /* We don't want to see VMMCALLs from a nested guest */
  1920. clr_intercept(svm, INTERCEPT_VMMCALL);
  1921. svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
  1922. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1923. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1924. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1925. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1926. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1927. nested_svm_unmap(page);
  1928. /* Enter Guest-Mode */
  1929. enter_guest_mode(&svm->vcpu);
  1930. /*
  1931. * Merge guest and host intercepts - must be called with vcpu in
  1932. * guest-mode to take affect here
  1933. */
  1934. recalc_intercepts(svm);
  1935. svm->nested.vmcb = vmcb_gpa;
  1936. enable_gif(svm);
  1937. mark_all_dirty(svm->vmcb);
  1938. return true;
  1939. }
  1940. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1941. {
  1942. to_vmcb->save.fs = from_vmcb->save.fs;
  1943. to_vmcb->save.gs = from_vmcb->save.gs;
  1944. to_vmcb->save.tr = from_vmcb->save.tr;
  1945. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1946. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1947. to_vmcb->save.star = from_vmcb->save.star;
  1948. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1949. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1950. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1951. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1952. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1953. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1954. }
  1955. static int vmload_interception(struct vcpu_svm *svm)
  1956. {
  1957. struct vmcb *nested_vmcb;
  1958. struct page *page;
  1959. if (nested_svm_check_permissions(svm))
  1960. return 1;
  1961. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1962. skip_emulated_instruction(&svm->vcpu);
  1963. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1964. if (!nested_vmcb)
  1965. return 1;
  1966. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  1967. nested_svm_unmap(page);
  1968. return 1;
  1969. }
  1970. static int vmsave_interception(struct vcpu_svm *svm)
  1971. {
  1972. struct vmcb *nested_vmcb;
  1973. struct page *page;
  1974. if (nested_svm_check_permissions(svm))
  1975. return 1;
  1976. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1977. skip_emulated_instruction(&svm->vcpu);
  1978. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1979. if (!nested_vmcb)
  1980. return 1;
  1981. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  1982. nested_svm_unmap(page);
  1983. return 1;
  1984. }
  1985. static int vmrun_interception(struct vcpu_svm *svm)
  1986. {
  1987. if (nested_svm_check_permissions(svm))
  1988. return 1;
  1989. /* Save rip after vmrun instruction */
  1990. kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
  1991. if (!nested_svm_vmrun(svm))
  1992. return 1;
  1993. if (!nested_svm_vmrun_msrpm(svm))
  1994. goto failed;
  1995. return 1;
  1996. failed:
  1997. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  1998. svm->vmcb->control.exit_code_hi = 0;
  1999. svm->vmcb->control.exit_info_1 = 0;
  2000. svm->vmcb->control.exit_info_2 = 0;
  2001. nested_svm_vmexit(svm);
  2002. return 1;
  2003. }
  2004. static int stgi_interception(struct vcpu_svm *svm)
  2005. {
  2006. if (nested_svm_check_permissions(svm))
  2007. return 1;
  2008. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2009. skip_emulated_instruction(&svm->vcpu);
  2010. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2011. enable_gif(svm);
  2012. return 1;
  2013. }
  2014. static int clgi_interception(struct vcpu_svm *svm)
  2015. {
  2016. if (nested_svm_check_permissions(svm))
  2017. return 1;
  2018. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2019. skip_emulated_instruction(&svm->vcpu);
  2020. disable_gif(svm);
  2021. /* After a CLGI no interrupts should come */
  2022. svm_clear_vintr(svm);
  2023. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2024. mark_dirty(svm->vmcb, VMCB_INTR);
  2025. return 1;
  2026. }
  2027. static int invlpga_interception(struct vcpu_svm *svm)
  2028. {
  2029. struct kvm_vcpu *vcpu = &svm->vcpu;
  2030. trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
  2031. vcpu->arch.regs[VCPU_REGS_RAX]);
  2032. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  2033. kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
  2034. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2035. skip_emulated_instruction(&svm->vcpu);
  2036. return 1;
  2037. }
  2038. static int skinit_interception(struct vcpu_svm *svm)
  2039. {
  2040. trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
  2041. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2042. return 1;
  2043. }
  2044. static int invalid_op_interception(struct vcpu_svm *svm)
  2045. {
  2046. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2047. return 1;
  2048. }
  2049. static int task_switch_interception(struct vcpu_svm *svm)
  2050. {
  2051. u16 tss_selector;
  2052. int reason;
  2053. int int_type = svm->vmcb->control.exit_int_info &
  2054. SVM_EXITINTINFO_TYPE_MASK;
  2055. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  2056. uint32_t type =
  2057. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  2058. uint32_t idt_v =
  2059. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  2060. bool has_error_code = false;
  2061. u32 error_code = 0;
  2062. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  2063. if (svm->vmcb->control.exit_info_2 &
  2064. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  2065. reason = TASK_SWITCH_IRET;
  2066. else if (svm->vmcb->control.exit_info_2 &
  2067. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  2068. reason = TASK_SWITCH_JMP;
  2069. else if (idt_v)
  2070. reason = TASK_SWITCH_GATE;
  2071. else
  2072. reason = TASK_SWITCH_CALL;
  2073. if (reason == TASK_SWITCH_GATE) {
  2074. switch (type) {
  2075. case SVM_EXITINTINFO_TYPE_NMI:
  2076. svm->vcpu.arch.nmi_injected = false;
  2077. break;
  2078. case SVM_EXITINTINFO_TYPE_EXEPT:
  2079. if (svm->vmcb->control.exit_info_2 &
  2080. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  2081. has_error_code = true;
  2082. error_code =
  2083. (u32)svm->vmcb->control.exit_info_2;
  2084. }
  2085. kvm_clear_exception_queue(&svm->vcpu);
  2086. break;
  2087. case SVM_EXITINTINFO_TYPE_INTR:
  2088. kvm_clear_interrupt_queue(&svm->vcpu);
  2089. break;
  2090. default:
  2091. break;
  2092. }
  2093. }
  2094. if (reason != TASK_SWITCH_GATE ||
  2095. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2096. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2097. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  2098. skip_emulated_instruction(&svm->vcpu);
  2099. if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
  2100. has_error_code, error_code) == EMULATE_FAIL) {
  2101. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2102. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  2103. svm->vcpu.run->internal.ndata = 0;
  2104. return 0;
  2105. }
  2106. return 1;
  2107. }
  2108. static int cpuid_interception(struct vcpu_svm *svm)
  2109. {
  2110. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2111. kvm_emulate_cpuid(&svm->vcpu);
  2112. return 1;
  2113. }
  2114. static int iret_interception(struct vcpu_svm *svm)
  2115. {
  2116. ++svm->vcpu.stat.nmi_window_exits;
  2117. clr_intercept(svm, INTERCEPT_IRET);
  2118. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  2119. return 1;
  2120. }
  2121. static int invlpg_interception(struct vcpu_svm *svm)
  2122. {
  2123. return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
  2124. }
  2125. static int emulate_on_interception(struct vcpu_svm *svm)
  2126. {
  2127. return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
  2128. }
  2129. static int cr0_write_interception(struct vcpu_svm *svm)
  2130. {
  2131. struct kvm_vcpu *vcpu = &svm->vcpu;
  2132. int r;
  2133. r = emulate_instruction(&svm->vcpu, 0, 0, 0);
  2134. if (svm->nested.vmexit_rip) {
  2135. kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
  2136. kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
  2137. kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
  2138. svm->nested.vmexit_rip = 0;
  2139. }
  2140. return r == EMULATE_DONE;
  2141. }
  2142. static int cr8_write_interception(struct vcpu_svm *svm)
  2143. {
  2144. struct kvm_run *kvm_run = svm->vcpu.run;
  2145. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  2146. /* instruction emulation calls kvm_set_cr8() */
  2147. emulate_instruction(&svm->vcpu, 0, 0, 0);
  2148. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  2149. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2150. return 1;
  2151. }
  2152. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  2153. return 1;
  2154. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2155. return 0;
  2156. }
  2157. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  2158. {
  2159. struct vcpu_svm *svm = to_svm(vcpu);
  2160. switch (ecx) {
  2161. case MSR_IA32_TSC: {
  2162. struct vmcb *vmcb = get_host_vmcb(svm);
  2163. *data = vmcb->control.tsc_offset + native_read_tsc();
  2164. break;
  2165. }
  2166. case MSR_STAR:
  2167. *data = svm->vmcb->save.star;
  2168. break;
  2169. #ifdef CONFIG_X86_64
  2170. case MSR_LSTAR:
  2171. *data = svm->vmcb->save.lstar;
  2172. break;
  2173. case MSR_CSTAR:
  2174. *data = svm->vmcb->save.cstar;
  2175. break;
  2176. case MSR_KERNEL_GS_BASE:
  2177. *data = svm->vmcb->save.kernel_gs_base;
  2178. break;
  2179. case MSR_SYSCALL_MASK:
  2180. *data = svm->vmcb->save.sfmask;
  2181. break;
  2182. #endif
  2183. case MSR_IA32_SYSENTER_CS:
  2184. *data = svm->vmcb->save.sysenter_cs;
  2185. break;
  2186. case MSR_IA32_SYSENTER_EIP:
  2187. *data = svm->sysenter_eip;
  2188. break;
  2189. case MSR_IA32_SYSENTER_ESP:
  2190. *data = svm->sysenter_esp;
  2191. break;
  2192. /*
  2193. * Nobody will change the following 5 values in the VMCB so we can
  2194. * safely return them on rdmsr. They will always be 0 until LBRV is
  2195. * implemented.
  2196. */
  2197. case MSR_IA32_DEBUGCTLMSR:
  2198. *data = svm->vmcb->save.dbgctl;
  2199. break;
  2200. case MSR_IA32_LASTBRANCHFROMIP:
  2201. *data = svm->vmcb->save.br_from;
  2202. break;
  2203. case MSR_IA32_LASTBRANCHTOIP:
  2204. *data = svm->vmcb->save.br_to;
  2205. break;
  2206. case MSR_IA32_LASTINTFROMIP:
  2207. *data = svm->vmcb->save.last_excp_from;
  2208. break;
  2209. case MSR_IA32_LASTINTTOIP:
  2210. *data = svm->vmcb->save.last_excp_to;
  2211. break;
  2212. case MSR_VM_HSAVE_PA:
  2213. *data = svm->nested.hsave_msr;
  2214. break;
  2215. case MSR_VM_CR:
  2216. *data = svm->nested.vm_cr_msr;
  2217. break;
  2218. case MSR_IA32_UCODE_REV:
  2219. *data = 0x01000065;
  2220. break;
  2221. default:
  2222. return kvm_get_msr_common(vcpu, ecx, data);
  2223. }
  2224. return 0;
  2225. }
  2226. static int rdmsr_interception(struct vcpu_svm *svm)
  2227. {
  2228. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2229. u64 data;
  2230. if (svm_get_msr(&svm->vcpu, ecx, &data)) {
  2231. trace_kvm_msr_read_ex(ecx);
  2232. kvm_inject_gp(&svm->vcpu, 0);
  2233. } else {
  2234. trace_kvm_msr_read(ecx, data);
  2235. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  2236. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  2237. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2238. skip_emulated_instruction(&svm->vcpu);
  2239. }
  2240. return 1;
  2241. }
  2242. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2243. {
  2244. struct vcpu_svm *svm = to_svm(vcpu);
  2245. int svm_dis, chg_mask;
  2246. if (data & ~SVM_VM_CR_VALID_MASK)
  2247. return 1;
  2248. chg_mask = SVM_VM_CR_VALID_MASK;
  2249. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2250. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2251. svm->nested.vm_cr_msr &= ~chg_mask;
  2252. svm->nested.vm_cr_msr |= (data & chg_mask);
  2253. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2254. /* check for svm_disable while efer.svme is set */
  2255. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2256. return 1;
  2257. return 0;
  2258. }
  2259. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  2260. {
  2261. struct vcpu_svm *svm = to_svm(vcpu);
  2262. switch (ecx) {
  2263. case MSR_IA32_TSC:
  2264. kvm_write_tsc(vcpu, data);
  2265. break;
  2266. case MSR_STAR:
  2267. svm->vmcb->save.star = data;
  2268. break;
  2269. #ifdef CONFIG_X86_64
  2270. case MSR_LSTAR:
  2271. svm->vmcb->save.lstar = data;
  2272. break;
  2273. case MSR_CSTAR:
  2274. svm->vmcb->save.cstar = data;
  2275. break;
  2276. case MSR_KERNEL_GS_BASE:
  2277. svm->vmcb->save.kernel_gs_base = data;
  2278. break;
  2279. case MSR_SYSCALL_MASK:
  2280. svm->vmcb->save.sfmask = data;
  2281. break;
  2282. #endif
  2283. case MSR_IA32_SYSENTER_CS:
  2284. svm->vmcb->save.sysenter_cs = data;
  2285. break;
  2286. case MSR_IA32_SYSENTER_EIP:
  2287. svm->sysenter_eip = data;
  2288. svm->vmcb->save.sysenter_eip = data;
  2289. break;
  2290. case MSR_IA32_SYSENTER_ESP:
  2291. svm->sysenter_esp = data;
  2292. svm->vmcb->save.sysenter_esp = data;
  2293. break;
  2294. case MSR_IA32_DEBUGCTLMSR:
  2295. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  2296. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  2297. __func__, data);
  2298. break;
  2299. }
  2300. if (data & DEBUGCTL_RESERVED_BITS)
  2301. return 1;
  2302. svm->vmcb->save.dbgctl = data;
  2303. if (data & (1ULL<<0))
  2304. svm_enable_lbrv(svm);
  2305. else
  2306. svm_disable_lbrv(svm);
  2307. break;
  2308. case MSR_VM_HSAVE_PA:
  2309. svm->nested.hsave_msr = data;
  2310. break;
  2311. case MSR_VM_CR:
  2312. return svm_set_vm_cr(vcpu, data);
  2313. case MSR_VM_IGNNE:
  2314. pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  2315. break;
  2316. default:
  2317. return kvm_set_msr_common(vcpu, ecx, data);
  2318. }
  2319. return 0;
  2320. }
  2321. static int wrmsr_interception(struct vcpu_svm *svm)
  2322. {
  2323. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2324. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  2325. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2326. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2327. if (svm_set_msr(&svm->vcpu, ecx, data)) {
  2328. trace_kvm_msr_write_ex(ecx, data);
  2329. kvm_inject_gp(&svm->vcpu, 0);
  2330. } else {
  2331. trace_kvm_msr_write(ecx, data);
  2332. skip_emulated_instruction(&svm->vcpu);
  2333. }
  2334. return 1;
  2335. }
  2336. static int msr_interception(struct vcpu_svm *svm)
  2337. {
  2338. if (svm->vmcb->control.exit_info_1)
  2339. return wrmsr_interception(svm);
  2340. else
  2341. return rdmsr_interception(svm);
  2342. }
  2343. static int interrupt_window_interception(struct vcpu_svm *svm)
  2344. {
  2345. struct kvm_run *kvm_run = svm->vcpu.run;
  2346. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2347. svm_clear_vintr(svm);
  2348. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2349. mark_dirty(svm->vmcb, VMCB_INTR);
  2350. /*
  2351. * If the user space waits to inject interrupts, exit as soon as
  2352. * possible
  2353. */
  2354. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  2355. kvm_run->request_interrupt_window &&
  2356. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  2357. ++svm->vcpu.stat.irq_window_exits;
  2358. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2359. return 0;
  2360. }
  2361. return 1;
  2362. }
  2363. static int pause_interception(struct vcpu_svm *svm)
  2364. {
  2365. kvm_vcpu_on_spin(&(svm->vcpu));
  2366. return 1;
  2367. }
  2368. static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
  2369. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  2370. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  2371. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  2372. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  2373. [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
  2374. [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
  2375. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  2376. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  2377. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2378. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  2379. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  2380. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  2381. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  2382. [SVM_EXIT_READ_DR4] = emulate_on_interception,
  2383. [SVM_EXIT_READ_DR5] = emulate_on_interception,
  2384. [SVM_EXIT_READ_DR6] = emulate_on_interception,
  2385. [SVM_EXIT_READ_DR7] = emulate_on_interception,
  2386. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  2387. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  2388. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  2389. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  2390. [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
  2391. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  2392. [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
  2393. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  2394. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2395. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2396. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2397. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2398. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  2399. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2400. [SVM_EXIT_INTR] = intr_interception,
  2401. [SVM_EXIT_NMI] = nmi_interception,
  2402. [SVM_EXIT_SMI] = nop_on_interception,
  2403. [SVM_EXIT_INIT] = nop_on_interception,
  2404. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2405. [SVM_EXIT_CPUID] = cpuid_interception,
  2406. [SVM_EXIT_IRET] = iret_interception,
  2407. [SVM_EXIT_INVD] = emulate_on_interception,
  2408. [SVM_EXIT_PAUSE] = pause_interception,
  2409. [SVM_EXIT_HLT] = halt_interception,
  2410. [SVM_EXIT_INVLPG] = invlpg_interception,
  2411. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2412. [SVM_EXIT_IOIO] = io_interception,
  2413. [SVM_EXIT_MSR] = msr_interception,
  2414. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2415. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2416. [SVM_EXIT_VMRUN] = vmrun_interception,
  2417. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  2418. [SVM_EXIT_VMLOAD] = vmload_interception,
  2419. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2420. [SVM_EXIT_STGI] = stgi_interception,
  2421. [SVM_EXIT_CLGI] = clgi_interception,
  2422. [SVM_EXIT_SKINIT] = skinit_interception,
  2423. [SVM_EXIT_WBINVD] = emulate_on_interception,
  2424. [SVM_EXIT_MONITOR] = invalid_op_interception,
  2425. [SVM_EXIT_MWAIT] = invalid_op_interception,
  2426. [SVM_EXIT_NPF] = pf_interception,
  2427. };
  2428. void dump_vmcb(struct kvm_vcpu *vcpu)
  2429. {
  2430. struct vcpu_svm *svm = to_svm(vcpu);
  2431. struct vmcb_control_area *control = &svm->vmcb->control;
  2432. struct vmcb_save_area *save = &svm->vmcb->save;
  2433. pr_err("VMCB Control Area:\n");
  2434. pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
  2435. pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
  2436. pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
  2437. pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
  2438. pr_err("exceptions: %08x\n", control->intercept_exceptions);
  2439. pr_err("intercepts: %016llx\n", control->intercept);
  2440. pr_err("pause filter count: %d\n", control->pause_filter_count);
  2441. pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
  2442. pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
  2443. pr_err("tsc_offset: %016llx\n", control->tsc_offset);
  2444. pr_err("asid: %d\n", control->asid);
  2445. pr_err("tlb_ctl: %d\n", control->tlb_ctl);
  2446. pr_err("int_ctl: %08x\n", control->int_ctl);
  2447. pr_err("int_vector: %08x\n", control->int_vector);
  2448. pr_err("int_state: %08x\n", control->int_state);
  2449. pr_err("exit_code: %08x\n", control->exit_code);
  2450. pr_err("exit_info1: %016llx\n", control->exit_info_1);
  2451. pr_err("exit_info2: %016llx\n", control->exit_info_2);
  2452. pr_err("exit_int_info: %08x\n", control->exit_int_info);
  2453. pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
  2454. pr_err("nested_ctl: %lld\n", control->nested_ctl);
  2455. pr_err("nested_cr3: %016llx\n", control->nested_cr3);
  2456. pr_err("event_inj: %08x\n", control->event_inj);
  2457. pr_err("event_inj_err: %08x\n", control->event_inj_err);
  2458. pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
  2459. pr_err("next_rip: %016llx\n", control->next_rip);
  2460. pr_err("VMCB State Save Area:\n");
  2461. pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
  2462. save->es.selector, save->es.attrib,
  2463. save->es.limit, save->es.base);
  2464. pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
  2465. save->cs.selector, save->cs.attrib,
  2466. save->cs.limit, save->cs.base);
  2467. pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
  2468. save->ss.selector, save->ss.attrib,
  2469. save->ss.limit, save->ss.base);
  2470. pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
  2471. save->ds.selector, save->ds.attrib,
  2472. save->ds.limit, save->ds.base);
  2473. pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
  2474. save->fs.selector, save->fs.attrib,
  2475. save->fs.limit, save->fs.base);
  2476. pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
  2477. save->gs.selector, save->gs.attrib,
  2478. save->gs.limit, save->gs.base);
  2479. pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2480. save->gdtr.selector, save->gdtr.attrib,
  2481. save->gdtr.limit, save->gdtr.base);
  2482. pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2483. save->ldtr.selector, save->ldtr.attrib,
  2484. save->ldtr.limit, save->ldtr.base);
  2485. pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2486. save->idtr.selector, save->idtr.attrib,
  2487. save->idtr.limit, save->idtr.base);
  2488. pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
  2489. save->tr.selector, save->tr.attrib,
  2490. save->tr.limit, save->tr.base);
  2491. pr_err("cpl: %d efer: %016llx\n",
  2492. save->cpl, save->efer);
  2493. pr_err("cr0: %016llx cr2: %016llx\n",
  2494. save->cr0, save->cr2);
  2495. pr_err("cr3: %016llx cr4: %016llx\n",
  2496. save->cr3, save->cr4);
  2497. pr_err("dr6: %016llx dr7: %016llx\n",
  2498. save->dr6, save->dr7);
  2499. pr_err("rip: %016llx rflags: %016llx\n",
  2500. save->rip, save->rflags);
  2501. pr_err("rsp: %016llx rax: %016llx\n",
  2502. save->rsp, save->rax);
  2503. pr_err("star: %016llx lstar: %016llx\n",
  2504. save->star, save->lstar);
  2505. pr_err("cstar: %016llx sfmask: %016llx\n",
  2506. save->cstar, save->sfmask);
  2507. pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
  2508. save->kernel_gs_base, save->sysenter_cs);
  2509. pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
  2510. save->sysenter_esp, save->sysenter_eip);
  2511. pr_err("gpat: %016llx dbgctl: %016llx\n",
  2512. save->g_pat, save->dbgctl);
  2513. pr_err("br_from: %016llx br_to: %016llx\n",
  2514. save->br_from, save->br_to);
  2515. pr_err("excp_from: %016llx excp_to: %016llx\n",
  2516. save->last_excp_from, save->last_excp_to);
  2517. }
  2518. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  2519. {
  2520. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  2521. *info1 = control->exit_info_1;
  2522. *info2 = control->exit_info_2;
  2523. }
  2524. static int handle_exit(struct kvm_vcpu *vcpu)
  2525. {
  2526. struct vcpu_svm *svm = to_svm(vcpu);
  2527. struct kvm_run *kvm_run = vcpu->run;
  2528. u32 exit_code = svm->vmcb->control.exit_code;
  2529. trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
  2530. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  2531. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2532. if (npt_enabled)
  2533. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2534. if (unlikely(svm->nested.exit_required)) {
  2535. nested_svm_vmexit(svm);
  2536. svm->nested.exit_required = false;
  2537. return 1;
  2538. }
  2539. if (is_guest_mode(vcpu)) {
  2540. int vmexit;
  2541. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  2542. svm->vmcb->control.exit_info_1,
  2543. svm->vmcb->control.exit_info_2,
  2544. svm->vmcb->control.exit_int_info,
  2545. svm->vmcb->control.exit_int_info_err);
  2546. vmexit = nested_svm_exit_special(svm);
  2547. if (vmexit == NESTED_EXIT_CONTINUE)
  2548. vmexit = nested_svm_exit_handled(svm);
  2549. if (vmexit == NESTED_EXIT_DONE)
  2550. return 1;
  2551. }
  2552. svm_complete_interrupts(svm);
  2553. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2554. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2555. kvm_run->fail_entry.hardware_entry_failure_reason
  2556. = svm->vmcb->control.exit_code;
  2557. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  2558. dump_vmcb(vcpu);
  2559. return 0;
  2560. }
  2561. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2562. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2563. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  2564. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  2565. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  2566. "exit_code 0x%x\n",
  2567. __func__, svm->vmcb->control.exit_int_info,
  2568. exit_code);
  2569. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2570. || !svm_exit_handlers[exit_code]) {
  2571. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2572. kvm_run->hw.hardware_exit_reason = exit_code;
  2573. return 0;
  2574. }
  2575. return svm_exit_handlers[exit_code](svm);
  2576. }
  2577. static void reload_tss(struct kvm_vcpu *vcpu)
  2578. {
  2579. int cpu = raw_smp_processor_id();
  2580. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2581. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2582. load_TR_desc();
  2583. }
  2584. static void pre_svm_run(struct vcpu_svm *svm)
  2585. {
  2586. int cpu = raw_smp_processor_id();
  2587. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2588. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  2589. /* FIXME: handle wraparound of asid_generation */
  2590. if (svm->asid_generation != sd->asid_generation)
  2591. new_asid(svm, sd);
  2592. }
  2593. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2594. {
  2595. struct vcpu_svm *svm = to_svm(vcpu);
  2596. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2597. vcpu->arch.hflags |= HF_NMI_MASK;
  2598. set_intercept(svm, INTERCEPT_IRET);
  2599. ++vcpu->stat.nmi_injections;
  2600. }
  2601. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2602. {
  2603. struct vmcb_control_area *control;
  2604. control = &svm->vmcb->control;
  2605. control->int_vector = irq;
  2606. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2607. control->int_ctl |= V_IRQ_MASK |
  2608. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2609. mark_dirty(svm->vmcb, VMCB_INTR);
  2610. }
  2611. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2612. {
  2613. struct vcpu_svm *svm = to_svm(vcpu);
  2614. BUG_ON(!(gif_set(svm)));
  2615. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  2616. ++vcpu->stat.irq_injections;
  2617. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2618. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2619. }
  2620. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2621. {
  2622. struct vcpu_svm *svm = to_svm(vcpu);
  2623. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2624. return;
  2625. if (irr == -1)
  2626. return;
  2627. if (tpr >= irr)
  2628. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2629. }
  2630. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2631. {
  2632. struct vcpu_svm *svm = to_svm(vcpu);
  2633. struct vmcb *vmcb = svm->vmcb;
  2634. int ret;
  2635. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2636. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2637. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  2638. return ret;
  2639. }
  2640. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  2641. {
  2642. struct vcpu_svm *svm = to_svm(vcpu);
  2643. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2644. }
  2645. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2646. {
  2647. struct vcpu_svm *svm = to_svm(vcpu);
  2648. if (masked) {
  2649. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  2650. set_intercept(svm, INTERCEPT_IRET);
  2651. } else {
  2652. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  2653. clr_intercept(svm, INTERCEPT_IRET);
  2654. }
  2655. }
  2656. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  2657. {
  2658. struct vcpu_svm *svm = to_svm(vcpu);
  2659. struct vmcb *vmcb = svm->vmcb;
  2660. int ret;
  2661. if (!gif_set(svm) ||
  2662. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  2663. return 0;
  2664. ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
  2665. if (is_guest_mode(vcpu))
  2666. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  2667. return ret;
  2668. }
  2669. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2670. {
  2671. struct vcpu_svm *svm = to_svm(vcpu);
  2672. /*
  2673. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  2674. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  2675. * get that intercept, this function will be called again though and
  2676. * we'll get the vintr intercept.
  2677. */
  2678. if (gif_set(svm) && nested_svm_intr(svm)) {
  2679. svm_set_vintr(svm);
  2680. svm_inject_irq(svm, 0x0);
  2681. }
  2682. }
  2683. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2684. {
  2685. struct vcpu_svm *svm = to_svm(vcpu);
  2686. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  2687. == HF_NMI_MASK)
  2688. return; /* IRET will cause a vm exit */
  2689. /*
  2690. * Something prevents NMI from been injected. Single step over possible
  2691. * problem (IRET or exception injection or interrupt shadow)
  2692. */
  2693. svm->nmi_singlestep = true;
  2694. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  2695. update_db_intercept(vcpu);
  2696. }
  2697. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2698. {
  2699. return 0;
  2700. }
  2701. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  2702. {
  2703. force_new_asid(vcpu);
  2704. }
  2705. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  2706. {
  2707. }
  2708. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2709. {
  2710. struct vcpu_svm *svm = to_svm(vcpu);
  2711. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2712. return;
  2713. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  2714. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2715. kvm_set_cr8(vcpu, cr8);
  2716. }
  2717. }
  2718. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2719. {
  2720. struct vcpu_svm *svm = to_svm(vcpu);
  2721. u64 cr8;
  2722. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2723. return;
  2724. cr8 = kvm_get_cr8(vcpu);
  2725. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2726. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2727. }
  2728. static void svm_complete_interrupts(struct vcpu_svm *svm)
  2729. {
  2730. u8 vector;
  2731. int type;
  2732. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  2733. unsigned int3_injected = svm->int3_injected;
  2734. svm->int3_injected = 0;
  2735. if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
  2736. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  2737. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2738. }
  2739. svm->vcpu.arch.nmi_injected = false;
  2740. kvm_clear_exception_queue(&svm->vcpu);
  2741. kvm_clear_interrupt_queue(&svm->vcpu);
  2742. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  2743. return;
  2744. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2745. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  2746. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  2747. switch (type) {
  2748. case SVM_EXITINTINFO_TYPE_NMI:
  2749. svm->vcpu.arch.nmi_injected = true;
  2750. break;
  2751. case SVM_EXITINTINFO_TYPE_EXEPT:
  2752. /*
  2753. * In case of software exceptions, do not reinject the vector,
  2754. * but re-execute the instruction instead. Rewind RIP first
  2755. * if we emulated INT3 before.
  2756. */
  2757. if (kvm_exception_is_soft(vector)) {
  2758. if (vector == BP_VECTOR && int3_injected &&
  2759. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  2760. kvm_rip_write(&svm->vcpu,
  2761. kvm_rip_read(&svm->vcpu) -
  2762. int3_injected);
  2763. break;
  2764. }
  2765. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2766. u32 err = svm->vmcb->control.exit_int_info_err;
  2767. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  2768. } else
  2769. kvm_requeue_exception(&svm->vcpu, vector);
  2770. break;
  2771. case SVM_EXITINTINFO_TYPE_INTR:
  2772. kvm_queue_interrupt(&svm->vcpu, vector, false);
  2773. break;
  2774. default:
  2775. break;
  2776. }
  2777. }
  2778. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  2779. {
  2780. struct vcpu_svm *svm = to_svm(vcpu);
  2781. struct vmcb_control_area *control = &svm->vmcb->control;
  2782. control->exit_int_info = control->event_inj;
  2783. control->exit_int_info_err = control->event_inj_err;
  2784. control->event_inj = 0;
  2785. svm_complete_interrupts(svm);
  2786. }
  2787. #ifdef CONFIG_X86_64
  2788. #define R "r"
  2789. #else
  2790. #define R "e"
  2791. #endif
  2792. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  2793. {
  2794. struct vcpu_svm *svm = to_svm(vcpu);
  2795. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2796. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2797. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2798. /*
  2799. * A vmexit emulation is required before the vcpu can be executed
  2800. * again.
  2801. */
  2802. if (unlikely(svm->nested.exit_required))
  2803. return;
  2804. pre_svm_run(svm);
  2805. sync_lapic_to_cr8(vcpu);
  2806. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2807. clgi();
  2808. local_irq_enable();
  2809. asm volatile (
  2810. "push %%"R"bp; \n\t"
  2811. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2812. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2813. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2814. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2815. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2816. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2817. #ifdef CONFIG_X86_64
  2818. "mov %c[r8](%[svm]), %%r8 \n\t"
  2819. "mov %c[r9](%[svm]), %%r9 \n\t"
  2820. "mov %c[r10](%[svm]), %%r10 \n\t"
  2821. "mov %c[r11](%[svm]), %%r11 \n\t"
  2822. "mov %c[r12](%[svm]), %%r12 \n\t"
  2823. "mov %c[r13](%[svm]), %%r13 \n\t"
  2824. "mov %c[r14](%[svm]), %%r14 \n\t"
  2825. "mov %c[r15](%[svm]), %%r15 \n\t"
  2826. #endif
  2827. /* Enter guest mode */
  2828. "push %%"R"ax \n\t"
  2829. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2830. __ex(SVM_VMLOAD) "\n\t"
  2831. __ex(SVM_VMRUN) "\n\t"
  2832. __ex(SVM_VMSAVE) "\n\t"
  2833. "pop %%"R"ax \n\t"
  2834. /* Save guest registers, load host registers */
  2835. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2836. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2837. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2838. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2839. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2840. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2841. #ifdef CONFIG_X86_64
  2842. "mov %%r8, %c[r8](%[svm]) \n\t"
  2843. "mov %%r9, %c[r9](%[svm]) \n\t"
  2844. "mov %%r10, %c[r10](%[svm]) \n\t"
  2845. "mov %%r11, %c[r11](%[svm]) \n\t"
  2846. "mov %%r12, %c[r12](%[svm]) \n\t"
  2847. "mov %%r13, %c[r13](%[svm]) \n\t"
  2848. "mov %%r14, %c[r14](%[svm]) \n\t"
  2849. "mov %%r15, %c[r15](%[svm]) \n\t"
  2850. #endif
  2851. "pop %%"R"bp"
  2852. :
  2853. : [svm]"a"(svm),
  2854. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2855. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2856. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2857. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2858. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2859. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2860. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2861. #ifdef CONFIG_X86_64
  2862. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2863. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2864. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2865. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2866. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2867. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2868. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2869. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2870. #endif
  2871. : "cc", "memory"
  2872. , R"bx", R"cx", R"dx", R"si", R"di"
  2873. #ifdef CONFIG_X86_64
  2874. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2875. #endif
  2876. );
  2877. #ifdef CONFIG_X86_64
  2878. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  2879. #else
  2880. loadsegment(fs, svm->host.fs);
  2881. #endif
  2882. reload_tss(vcpu);
  2883. local_irq_disable();
  2884. stgi();
  2885. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2886. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2887. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2888. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2889. sync_cr8_to_lapic(vcpu);
  2890. svm->next_rip = 0;
  2891. /* if exit due to PF check for async PF */
  2892. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  2893. svm->apf_reason = kvm_read_and_reset_pf_reason();
  2894. if (npt_enabled) {
  2895. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  2896. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  2897. }
  2898. /*
  2899. * We need to handle MC intercepts here before the vcpu has a chance to
  2900. * change the physical cpu
  2901. */
  2902. if (unlikely(svm->vmcb->control.exit_code ==
  2903. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  2904. svm_handle_mce(svm);
  2905. mark_all_clean(svm->vmcb);
  2906. }
  2907. #undef R
  2908. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2909. {
  2910. struct vcpu_svm *svm = to_svm(vcpu);
  2911. svm->vmcb->save.cr3 = root;
  2912. mark_dirty(svm->vmcb, VMCB_CR);
  2913. force_new_asid(vcpu);
  2914. }
  2915. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2916. {
  2917. struct vcpu_svm *svm = to_svm(vcpu);
  2918. svm->vmcb->control.nested_cr3 = root;
  2919. mark_dirty(svm->vmcb, VMCB_NPT);
  2920. /* Also sync guest cr3 here in case we live migrate */
  2921. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2922. mark_dirty(svm->vmcb, VMCB_CR);
  2923. force_new_asid(vcpu);
  2924. }
  2925. static int is_disabled(void)
  2926. {
  2927. u64 vm_cr;
  2928. rdmsrl(MSR_VM_CR, vm_cr);
  2929. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2930. return 1;
  2931. return 0;
  2932. }
  2933. static void
  2934. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2935. {
  2936. /*
  2937. * Patch in the VMMCALL instruction:
  2938. */
  2939. hypercall[0] = 0x0f;
  2940. hypercall[1] = 0x01;
  2941. hypercall[2] = 0xd9;
  2942. }
  2943. static void svm_check_processor_compat(void *rtn)
  2944. {
  2945. *(int *)rtn = 0;
  2946. }
  2947. static bool svm_cpu_has_accelerated_tpr(void)
  2948. {
  2949. return false;
  2950. }
  2951. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  2952. {
  2953. return 0;
  2954. }
  2955. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  2956. {
  2957. }
  2958. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  2959. {
  2960. switch (func) {
  2961. case 0x00000001:
  2962. /* Mask out xsave bit as long as it is not supported by SVM */
  2963. entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
  2964. break;
  2965. case 0x80000001:
  2966. if (nested)
  2967. entry->ecx |= (1 << 2); /* Set SVM bit */
  2968. break;
  2969. case 0x8000000A:
  2970. entry->eax = 1; /* SVM revision 1 */
  2971. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  2972. ASID emulation to nested SVM */
  2973. entry->ecx = 0; /* Reserved */
  2974. entry->edx = 0; /* Per default do not support any
  2975. additional features */
  2976. /* Support next_rip if host supports it */
  2977. if (boot_cpu_has(X86_FEATURE_NRIPS))
  2978. entry->edx |= SVM_FEATURE_NRIP;
  2979. /* Support NPT for the guest if enabled */
  2980. if (npt_enabled)
  2981. entry->edx |= SVM_FEATURE_NPT;
  2982. break;
  2983. }
  2984. }
  2985. static const struct trace_print_flags svm_exit_reasons_str[] = {
  2986. { SVM_EXIT_READ_CR0, "read_cr0" },
  2987. { SVM_EXIT_READ_CR3, "read_cr3" },
  2988. { SVM_EXIT_READ_CR4, "read_cr4" },
  2989. { SVM_EXIT_READ_CR8, "read_cr8" },
  2990. { SVM_EXIT_WRITE_CR0, "write_cr0" },
  2991. { SVM_EXIT_WRITE_CR3, "write_cr3" },
  2992. { SVM_EXIT_WRITE_CR4, "write_cr4" },
  2993. { SVM_EXIT_WRITE_CR8, "write_cr8" },
  2994. { SVM_EXIT_READ_DR0, "read_dr0" },
  2995. { SVM_EXIT_READ_DR1, "read_dr1" },
  2996. { SVM_EXIT_READ_DR2, "read_dr2" },
  2997. { SVM_EXIT_READ_DR3, "read_dr3" },
  2998. { SVM_EXIT_WRITE_DR0, "write_dr0" },
  2999. { SVM_EXIT_WRITE_DR1, "write_dr1" },
  3000. { SVM_EXIT_WRITE_DR2, "write_dr2" },
  3001. { SVM_EXIT_WRITE_DR3, "write_dr3" },
  3002. { SVM_EXIT_WRITE_DR5, "write_dr5" },
  3003. { SVM_EXIT_WRITE_DR7, "write_dr7" },
  3004. { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
  3005. { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
  3006. { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
  3007. { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
  3008. { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
  3009. { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
  3010. { SVM_EXIT_INTR, "interrupt" },
  3011. { SVM_EXIT_NMI, "nmi" },
  3012. { SVM_EXIT_SMI, "smi" },
  3013. { SVM_EXIT_INIT, "init" },
  3014. { SVM_EXIT_VINTR, "vintr" },
  3015. { SVM_EXIT_CPUID, "cpuid" },
  3016. { SVM_EXIT_INVD, "invd" },
  3017. { SVM_EXIT_HLT, "hlt" },
  3018. { SVM_EXIT_INVLPG, "invlpg" },
  3019. { SVM_EXIT_INVLPGA, "invlpga" },
  3020. { SVM_EXIT_IOIO, "io" },
  3021. { SVM_EXIT_MSR, "msr" },
  3022. { SVM_EXIT_TASK_SWITCH, "task_switch" },
  3023. { SVM_EXIT_SHUTDOWN, "shutdown" },
  3024. { SVM_EXIT_VMRUN, "vmrun" },
  3025. { SVM_EXIT_VMMCALL, "hypercall" },
  3026. { SVM_EXIT_VMLOAD, "vmload" },
  3027. { SVM_EXIT_VMSAVE, "vmsave" },
  3028. { SVM_EXIT_STGI, "stgi" },
  3029. { SVM_EXIT_CLGI, "clgi" },
  3030. { SVM_EXIT_SKINIT, "skinit" },
  3031. { SVM_EXIT_WBINVD, "wbinvd" },
  3032. { SVM_EXIT_MONITOR, "monitor" },
  3033. { SVM_EXIT_MWAIT, "mwait" },
  3034. { SVM_EXIT_NPF, "npf" },
  3035. { -1, NULL }
  3036. };
  3037. static int svm_get_lpage_level(void)
  3038. {
  3039. return PT_PDPE_LEVEL;
  3040. }
  3041. static bool svm_rdtscp_supported(void)
  3042. {
  3043. return false;
  3044. }
  3045. static bool svm_has_wbinvd_exit(void)
  3046. {
  3047. return true;
  3048. }
  3049. static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
  3050. {
  3051. struct vcpu_svm *svm = to_svm(vcpu);
  3052. set_exception_intercept(svm, NM_VECTOR);
  3053. update_cr0_intercept(svm);
  3054. }
  3055. static struct kvm_x86_ops svm_x86_ops = {
  3056. .cpu_has_kvm_support = has_svm,
  3057. .disabled_by_bios = is_disabled,
  3058. .hardware_setup = svm_hardware_setup,
  3059. .hardware_unsetup = svm_hardware_unsetup,
  3060. .check_processor_compatibility = svm_check_processor_compat,
  3061. .hardware_enable = svm_hardware_enable,
  3062. .hardware_disable = svm_hardware_disable,
  3063. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  3064. .vcpu_create = svm_create_vcpu,
  3065. .vcpu_free = svm_free_vcpu,
  3066. .vcpu_reset = svm_vcpu_reset,
  3067. .prepare_guest_switch = svm_prepare_guest_switch,
  3068. .vcpu_load = svm_vcpu_load,
  3069. .vcpu_put = svm_vcpu_put,
  3070. .set_guest_debug = svm_guest_debug,
  3071. .get_msr = svm_get_msr,
  3072. .set_msr = svm_set_msr,
  3073. .get_segment_base = svm_get_segment_base,
  3074. .get_segment = svm_get_segment,
  3075. .set_segment = svm_set_segment,
  3076. .get_cpl = svm_get_cpl,
  3077. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  3078. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  3079. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  3080. .set_cr0 = svm_set_cr0,
  3081. .set_cr3 = svm_set_cr3,
  3082. .set_cr4 = svm_set_cr4,
  3083. .set_efer = svm_set_efer,
  3084. .get_idt = svm_get_idt,
  3085. .set_idt = svm_set_idt,
  3086. .get_gdt = svm_get_gdt,
  3087. .set_gdt = svm_set_gdt,
  3088. .set_dr7 = svm_set_dr7,
  3089. .cache_reg = svm_cache_reg,
  3090. .get_rflags = svm_get_rflags,
  3091. .set_rflags = svm_set_rflags,
  3092. .fpu_activate = svm_fpu_activate,
  3093. .fpu_deactivate = svm_fpu_deactivate,
  3094. .tlb_flush = svm_flush_tlb,
  3095. .run = svm_vcpu_run,
  3096. .handle_exit = handle_exit,
  3097. .skip_emulated_instruction = skip_emulated_instruction,
  3098. .set_interrupt_shadow = svm_set_interrupt_shadow,
  3099. .get_interrupt_shadow = svm_get_interrupt_shadow,
  3100. .patch_hypercall = svm_patch_hypercall,
  3101. .set_irq = svm_set_irq,
  3102. .set_nmi = svm_inject_nmi,
  3103. .queue_exception = svm_queue_exception,
  3104. .cancel_injection = svm_cancel_injection,
  3105. .interrupt_allowed = svm_interrupt_allowed,
  3106. .nmi_allowed = svm_nmi_allowed,
  3107. .get_nmi_mask = svm_get_nmi_mask,
  3108. .set_nmi_mask = svm_set_nmi_mask,
  3109. .enable_nmi_window = enable_nmi_window,
  3110. .enable_irq_window = enable_irq_window,
  3111. .update_cr8_intercept = update_cr8_intercept,
  3112. .set_tss_addr = svm_set_tss_addr,
  3113. .get_tdp_level = get_npt_level,
  3114. .get_mt_mask = svm_get_mt_mask,
  3115. .get_exit_info = svm_get_exit_info,
  3116. .exit_reasons_str = svm_exit_reasons_str,
  3117. .get_lpage_level = svm_get_lpage_level,
  3118. .cpuid_update = svm_cpuid_update,
  3119. .rdtscp_supported = svm_rdtscp_supported,
  3120. .set_supported_cpuid = svm_set_supported_cpuid,
  3121. .has_wbinvd_exit = svm_has_wbinvd_exit,
  3122. .write_tsc_offset = svm_write_tsc_offset,
  3123. .adjust_tsc_offset = svm_adjust_tsc_offset,
  3124. .set_tdp_cr3 = set_tdp_cr3,
  3125. };
  3126. static int __init svm_init(void)
  3127. {
  3128. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  3129. __alignof__(struct vcpu_svm), THIS_MODULE);
  3130. }
  3131. static void __exit svm_exit(void)
  3132. {
  3133. kvm_exit();
  3134. }
  3135. module_init(svm_init)
  3136. module_exit(svm_exit)