svm.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945
  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_TSC_RATE (1 << 4)
  46. #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
  47. #define SVM_FEATURE_FLUSH_ASID (1 << 6)
  48. #define SVM_FEATURE_DECODE_ASSIST (1 << 7)
  49. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  50. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  51. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  52. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  53. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  54. static bool erratum_383_found __read_mostly;
  55. static const u32 host_save_user_msrs[] = {
  56. #ifdef CONFIG_X86_64
  57. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  58. MSR_FS_BASE,
  59. #endif
  60. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  61. };
  62. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  63. struct kvm_vcpu;
  64. struct nested_state {
  65. struct vmcb *hsave;
  66. u64 hsave_msr;
  67. u64 vm_cr_msr;
  68. u64 vmcb;
  69. /* These are the merged vectors */
  70. u32 *msrpm;
  71. /* gpa pointers to the real vectors */
  72. u64 vmcb_msrpm;
  73. u64 vmcb_iopm;
  74. /* A VMEXIT is required but not yet emulated */
  75. bool exit_required;
  76. /*
  77. * If we vmexit during an instruction emulation we need this to restore
  78. * the l1 guest rip after the emulation
  79. */
  80. unsigned long vmexit_rip;
  81. unsigned long vmexit_rsp;
  82. unsigned long vmexit_rax;
  83. /* cache for intercepts of the guest */
  84. u32 intercept_cr;
  85. u32 intercept_dr;
  86. u32 intercept_exceptions;
  87. u64 intercept;
  88. /* Nested Paging related state */
  89. u64 nested_cr3;
  90. };
  91. #define MSRPM_OFFSETS 16
  92. static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  93. struct vcpu_svm {
  94. struct kvm_vcpu vcpu;
  95. struct vmcb *vmcb;
  96. unsigned long vmcb_pa;
  97. struct svm_cpu_data *svm_data;
  98. uint64_t asid_generation;
  99. uint64_t sysenter_esp;
  100. uint64_t sysenter_eip;
  101. u64 next_rip;
  102. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  103. struct {
  104. u16 fs;
  105. u16 gs;
  106. u16 ldt;
  107. u64 gs_base;
  108. } host;
  109. u32 *msrpm;
  110. struct nested_state nested;
  111. bool nmi_singlestep;
  112. unsigned int3_injected;
  113. unsigned long int3_rip;
  114. u32 apf_reason;
  115. };
  116. #define MSR_INVALID 0xffffffffU
  117. static struct svm_direct_access_msrs {
  118. u32 index; /* Index of the MSR */
  119. bool always; /* True if intercept is always on */
  120. } direct_access_msrs[] = {
  121. { .index = MSR_STAR, .always = true },
  122. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  123. #ifdef CONFIG_X86_64
  124. { .index = MSR_GS_BASE, .always = true },
  125. { .index = MSR_FS_BASE, .always = true },
  126. { .index = MSR_KERNEL_GS_BASE, .always = true },
  127. { .index = MSR_LSTAR, .always = true },
  128. { .index = MSR_CSTAR, .always = true },
  129. { .index = MSR_SYSCALL_MASK, .always = true },
  130. #endif
  131. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  132. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  133. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  134. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  135. { .index = MSR_INVALID, .always = false },
  136. };
  137. /* enable NPT for AMD64 and X86 with PAE */
  138. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  139. static bool npt_enabled = true;
  140. #else
  141. static bool npt_enabled;
  142. #endif
  143. static int npt = 1;
  144. module_param(npt, int, S_IRUGO);
  145. static int nested = 1;
  146. module_param(nested, int, S_IRUGO);
  147. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  148. static void svm_complete_interrupts(struct vcpu_svm *svm);
  149. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  150. static int nested_svm_intercept(struct vcpu_svm *svm);
  151. static int nested_svm_vmexit(struct vcpu_svm *svm);
  152. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  153. bool has_error_code, u32 error_code);
  154. enum {
  155. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  156. pause filter count */
  157. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  158. VMCB_ASID, /* ASID */
  159. VMCB_INTR, /* int_ctl, int_vector */
  160. VMCB_NPT, /* npt_en, nCR3, gPAT */
  161. VMCB_CR, /* CR0, CR3, CR4, EFER */
  162. VMCB_DR, /* DR6, DR7 */
  163. VMCB_DT, /* GDT, IDT */
  164. VMCB_SEG, /* CS, DS, SS, ES, CPL */
  165. VMCB_CR2, /* CR2 only */
  166. VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
  167. VMCB_DIRTY_MAX,
  168. };
  169. /* TPR and CR2 are always written before VMRUN */
  170. #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
  171. static inline void mark_all_dirty(struct vmcb *vmcb)
  172. {
  173. vmcb->control.clean = 0;
  174. }
  175. static inline void mark_all_clean(struct vmcb *vmcb)
  176. {
  177. vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
  178. & ~VMCB_ALWAYS_DIRTY_MASK;
  179. }
  180. static inline void mark_dirty(struct vmcb *vmcb, int bit)
  181. {
  182. vmcb->control.clean &= ~(1 << bit);
  183. }
  184. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  185. {
  186. return container_of(vcpu, struct vcpu_svm, vcpu);
  187. }
  188. static void recalc_intercepts(struct vcpu_svm *svm)
  189. {
  190. struct vmcb_control_area *c, *h;
  191. struct nested_state *g;
  192. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  193. if (!is_guest_mode(&svm->vcpu))
  194. return;
  195. c = &svm->vmcb->control;
  196. h = &svm->nested.hsave->control;
  197. g = &svm->nested;
  198. c->intercept_cr = h->intercept_cr | g->intercept_cr;
  199. c->intercept_dr = h->intercept_dr | g->intercept_dr;
  200. c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
  201. c->intercept = h->intercept | g->intercept;
  202. }
  203. static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
  204. {
  205. if (is_guest_mode(&svm->vcpu))
  206. return svm->nested.hsave;
  207. else
  208. return svm->vmcb;
  209. }
  210. static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
  211. {
  212. struct vmcb *vmcb = get_host_vmcb(svm);
  213. vmcb->control.intercept_cr |= (1U << bit);
  214. recalc_intercepts(svm);
  215. }
  216. static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
  217. {
  218. struct vmcb *vmcb = get_host_vmcb(svm);
  219. vmcb->control.intercept_cr &= ~(1U << bit);
  220. recalc_intercepts(svm);
  221. }
  222. static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
  223. {
  224. struct vmcb *vmcb = get_host_vmcb(svm);
  225. return vmcb->control.intercept_cr & (1U << bit);
  226. }
  227. static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
  228. {
  229. struct vmcb *vmcb = get_host_vmcb(svm);
  230. vmcb->control.intercept_dr |= (1U << bit);
  231. recalc_intercepts(svm);
  232. }
  233. static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
  234. {
  235. struct vmcb *vmcb = get_host_vmcb(svm);
  236. vmcb->control.intercept_dr &= ~(1U << bit);
  237. recalc_intercepts(svm);
  238. }
  239. static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
  240. {
  241. struct vmcb *vmcb = get_host_vmcb(svm);
  242. vmcb->control.intercept_exceptions |= (1U << bit);
  243. recalc_intercepts(svm);
  244. }
  245. static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
  246. {
  247. struct vmcb *vmcb = get_host_vmcb(svm);
  248. vmcb->control.intercept_exceptions &= ~(1U << bit);
  249. recalc_intercepts(svm);
  250. }
  251. static inline void set_intercept(struct vcpu_svm *svm, int bit)
  252. {
  253. struct vmcb *vmcb = get_host_vmcb(svm);
  254. vmcb->control.intercept |= (1ULL << bit);
  255. recalc_intercepts(svm);
  256. }
  257. static inline void clr_intercept(struct vcpu_svm *svm, int bit)
  258. {
  259. struct vmcb *vmcb = get_host_vmcb(svm);
  260. vmcb->control.intercept &= ~(1ULL << bit);
  261. recalc_intercepts(svm);
  262. }
  263. static inline void enable_gif(struct vcpu_svm *svm)
  264. {
  265. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  266. }
  267. static inline void disable_gif(struct vcpu_svm *svm)
  268. {
  269. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  270. }
  271. static inline bool gif_set(struct vcpu_svm *svm)
  272. {
  273. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  274. }
  275. static unsigned long iopm_base;
  276. struct kvm_ldttss_desc {
  277. u16 limit0;
  278. u16 base0;
  279. unsigned base1:8, type:5, dpl:2, p:1;
  280. unsigned limit1:4, zero0:3, g:1, base2:8;
  281. u32 base3;
  282. u32 zero1;
  283. } __attribute__((packed));
  284. struct svm_cpu_data {
  285. int cpu;
  286. u64 asid_generation;
  287. u32 max_asid;
  288. u32 next_asid;
  289. struct kvm_ldttss_desc *tss_desc;
  290. struct page *save_area;
  291. };
  292. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  293. static uint32_t svm_features;
  294. struct svm_init_data {
  295. int cpu;
  296. int r;
  297. };
  298. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  299. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  300. #define MSRS_RANGE_SIZE 2048
  301. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  302. static u32 svm_msrpm_offset(u32 msr)
  303. {
  304. u32 offset;
  305. int i;
  306. for (i = 0; i < NUM_MSR_MAPS; i++) {
  307. if (msr < msrpm_ranges[i] ||
  308. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  309. continue;
  310. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  311. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  312. /* Now we have the u8 offset - but need the u32 offset */
  313. return offset / 4;
  314. }
  315. /* MSR not in any range */
  316. return MSR_INVALID;
  317. }
  318. #define MAX_INST_SIZE 15
  319. static inline void clgi(void)
  320. {
  321. asm volatile (__ex(SVM_CLGI));
  322. }
  323. static inline void stgi(void)
  324. {
  325. asm volatile (__ex(SVM_STGI));
  326. }
  327. static inline void invlpga(unsigned long addr, u32 asid)
  328. {
  329. asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
  330. }
  331. static int get_npt_level(void)
  332. {
  333. #ifdef CONFIG_X86_64
  334. return PT64_ROOT_LEVEL;
  335. #else
  336. return PT32E_ROOT_LEVEL;
  337. #endif
  338. }
  339. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  340. {
  341. vcpu->arch.efer = efer;
  342. if (!npt_enabled && !(efer & EFER_LMA))
  343. efer &= ~EFER_LME;
  344. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  345. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  346. }
  347. static int is_external_interrupt(u32 info)
  348. {
  349. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  350. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  351. }
  352. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  353. {
  354. struct vcpu_svm *svm = to_svm(vcpu);
  355. u32 ret = 0;
  356. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  357. ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  358. return ret & mask;
  359. }
  360. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  361. {
  362. struct vcpu_svm *svm = to_svm(vcpu);
  363. if (mask == 0)
  364. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  365. else
  366. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  367. }
  368. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  369. {
  370. struct vcpu_svm *svm = to_svm(vcpu);
  371. if (svm->vmcb->control.next_rip != 0)
  372. svm->next_rip = svm->vmcb->control.next_rip;
  373. if (!svm->next_rip) {
  374. if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
  375. EMULATE_DONE)
  376. printk(KERN_DEBUG "%s: NOP\n", __func__);
  377. return;
  378. }
  379. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  380. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  381. __func__, kvm_rip_read(vcpu), svm->next_rip);
  382. kvm_rip_write(vcpu, svm->next_rip);
  383. svm_set_interrupt_shadow(vcpu, 0);
  384. }
  385. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  386. bool has_error_code, u32 error_code,
  387. bool reinject)
  388. {
  389. struct vcpu_svm *svm = to_svm(vcpu);
  390. /*
  391. * If we are within a nested VM we'd better #VMEXIT and let the guest
  392. * handle the exception
  393. */
  394. if (!reinject &&
  395. nested_svm_check_exception(svm, nr, has_error_code, error_code))
  396. return;
  397. if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
  398. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  399. /*
  400. * For guest debugging where we have to reinject #BP if some
  401. * INT3 is guest-owned:
  402. * Emulate nRIP by moving RIP forward. Will fail if injection
  403. * raises a fault that is not intercepted. Still better than
  404. * failing in all cases.
  405. */
  406. skip_emulated_instruction(&svm->vcpu);
  407. rip = kvm_rip_read(&svm->vcpu);
  408. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  409. svm->int3_injected = rip - old_rip;
  410. }
  411. svm->vmcb->control.event_inj = nr
  412. | SVM_EVTINJ_VALID
  413. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  414. | SVM_EVTINJ_TYPE_EXEPT;
  415. svm->vmcb->control.event_inj_err = error_code;
  416. }
  417. static void svm_init_erratum_383(void)
  418. {
  419. u32 low, high;
  420. int err;
  421. u64 val;
  422. if (!cpu_has_amd_erratum(amd_erratum_383))
  423. return;
  424. /* Use _safe variants to not break nested virtualization */
  425. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  426. if (err)
  427. return;
  428. val |= (1ULL << 47);
  429. low = lower_32_bits(val);
  430. high = upper_32_bits(val);
  431. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  432. erratum_383_found = true;
  433. }
  434. static int has_svm(void)
  435. {
  436. const char *msg;
  437. if (!cpu_has_svm(&msg)) {
  438. printk(KERN_INFO "has_svm: %s\n", msg);
  439. return 0;
  440. }
  441. return 1;
  442. }
  443. static void svm_hardware_disable(void *garbage)
  444. {
  445. cpu_svm_disable();
  446. }
  447. static int svm_hardware_enable(void *garbage)
  448. {
  449. struct svm_cpu_data *sd;
  450. uint64_t efer;
  451. struct desc_ptr gdt_descr;
  452. struct desc_struct *gdt;
  453. int me = raw_smp_processor_id();
  454. rdmsrl(MSR_EFER, efer);
  455. if (efer & EFER_SVME)
  456. return -EBUSY;
  457. if (!has_svm()) {
  458. printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
  459. me);
  460. return -EINVAL;
  461. }
  462. sd = per_cpu(svm_data, me);
  463. if (!sd) {
  464. printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
  465. me);
  466. return -EINVAL;
  467. }
  468. sd->asid_generation = 1;
  469. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  470. sd->next_asid = sd->max_asid + 1;
  471. native_store_gdt(&gdt_descr);
  472. gdt = (struct desc_struct *)gdt_descr.address;
  473. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  474. wrmsrl(MSR_EFER, efer | EFER_SVME);
  475. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  476. svm_init_erratum_383();
  477. return 0;
  478. }
  479. static void svm_cpu_uninit(int cpu)
  480. {
  481. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  482. if (!sd)
  483. return;
  484. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  485. __free_page(sd->save_area);
  486. kfree(sd);
  487. }
  488. static int svm_cpu_init(int cpu)
  489. {
  490. struct svm_cpu_data *sd;
  491. int r;
  492. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  493. if (!sd)
  494. return -ENOMEM;
  495. sd->cpu = cpu;
  496. sd->save_area = alloc_page(GFP_KERNEL);
  497. r = -ENOMEM;
  498. if (!sd->save_area)
  499. goto err_1;
  500. per_cpu(svm_data, cpu) = sd;
  501. return 0;
  502. err_1:
  503. kfree(sd);
  504. return r;
  505. }
  506. static bool valid_msr_intercept(u32 index)
  507. {
  508. int i;
  509. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  510. if (direct_access_msrs[i].index == index)
  511. return true;
  512. return false;
  513. }
  514. static void set_msr_interception(u32 *msrpm, unsigned msr,
  515. int read, int write)
  516. {
  517. u8 bit_read, bit_write;
  518. unsigned long tmp;
  519. u32 offset;
  520. /*
  521. * If this warning triggers extend the direct_access_msrs list at the
  522. * beginning of the file
  523. */
  524. WARN_ON(!valid_msr_intercept(msr));
  525. offset = svm_msrpm_offset(msr);
  526. bit_read = 2 * (msr & 0x0f);
  527. bit_write = 2 * (msr & 0x0f) + 1;
  528. tmp = msrpm[offset];
  529. BUG_ON(offset == MSR_INVALID);
  530. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  531. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  532. msrpm[offset] = tmp;
  533. }
  534. static void svm_vcpu_init_msrpm(u32 *msrpm)
  535. {
  536. int i;
  537. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  538. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  539. if (!direct_access_msrs[i].always)
  540. continue;
  541. set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
  542. }
  543. }
  544. static void add_msr_offset(u32 offset)
  545. {
  546. int i;
  547. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  548. /* Offset already in list? */
  549. if (msrpm_offsets[i] == offset)
  550. return;
  551. /* Slot used by another offset? */
  552. if (msrpm_offsets[i] != MSR_INVALID)
  553. continue;
  554. /* Add offset to list */
  555. msrpm_offsets[i] = offset;
  556. return;
  557. }
  558. /*
  559. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  560. * increase MSRPM_OFFSETS in this case.
  561. */
  562. BUG();
  563. }
  564. static void init_msrpm_offsets(void)
  565. {
  566. int i;
  567. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  568. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  569. u32 offset;
  570. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  571. BUG_ON(offset == MSR_INVALID);
  572. add_msr_offset(offset);
  573. }
  574. }
  575. static void svm_enable_lbrv(struct vcpu_svm *svm)
  576. {
  577. u32 *msrpm = svm->msrpm;
  578. svm->vmcb->control.lbr_ctl = 1;
  579. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  580. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  581. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  582. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  583. }
  584. static void svm_disable_lbrv(struct vcpu_svm *svm)
  585. {
  586. u32 *msrpm = svm->msrpm;
  587. svm->vmcb->control.lbr_ctl = 0;
  588. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  589. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  590. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  591. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  592. }
  593. static __init int svm_hardware_setup(void)
  594. {
  595. int cpu;
  596. struct page *iopm_pages;
  597. void *iopm_va;
  598. int r;
  599. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  600. if (!iopm_pages)
  601. return -ENOMEM;
  602. iopm_va = page_address(iopm_pages);
  603. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  604. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  605. init_msrpm_offsets();
  606. if (boot_cpu_has(X86_FEATURE_NX))
  607. kvm_enable_efer_bits(EFER_NX);
  608. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  609. kvm_enable_efer_bits(EFER_FFXSR);
  610. if (nested) {
  611. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  612. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  613. }
  614. for_each_possible_cpu(cpu) {
  615. r = svm_cpu_init(cpu);
  616. if (r)
  617. goto err;
  618. }
  619. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  620. if (!boot_cpu_has(X86_FEATURE_NPT))
  621. npt_enabled = false;
  622. if (npt_enabled && !npt) {
  623. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  624. npt_enabled = false;
  625. }
  626. if (npt_enabled) {
  627. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  628. kvm_enable_tdp();
  629. } else
  630. kvm_disable_tdp();
  631. return 0;
  632. err:
  633. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  634. iopm_base = 0;
  635. return r;
  636. }
  637. static __exit void svm_hardware_unsetup(void)
  638. {
  639. int cpu;
  640. for_each_possible_cpu(cpu)
  641. svm_cpu_uninit(cpu);
  642. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  643. iopm_base = 0;
  644. }
  645. static void init_seg(struct vmcb_seg *seg)
  646. {
  647. seg->selector = 0;
  648. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  649. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  650. seg->limit = 0xffff;
  651. seg->base = 0;
  652. }
  653. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  654. {
  655. seg->selector = 0;
  656. seg->attrib = SVM_SELECTOR_P_MASK | type;
  657. seg->limit = 0xffff;
  658. seg->base = 0;
  659. }
  660. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  661. {
  662. struct vcpu_svm *svm = to_svm(vcpu);
  663. u64 g_tsc_offset = 0;
  664. if (is_guest_mode(vcpu)) {
  665. g_tsc_offset = svm->vmcb->control.tsc_offset -
  666. svm->nested.hsave->control.tsc_offset;
  667. svm->nested.hsave->control.tsc_offset = offset;
  668. }
  669. svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
  670. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  671. }
  672. static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
  673. {
  674. struct vcpu_svm *svm = to_svm(vcpu);
  675. svm->vmcb->control.tsc_offset += adjustment;
  676. if (is_guest_mode(vcpu))
  677. svm->nested.hsave->control.tsc_offset += adjustment;
  678. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  679. }
  680. static void init_vmcb(struct vcpu_svm *svm)
  681. {
  682. struct vmcb_control_area *control = &svm->vmcb->control;
  683. struct vmcb_save_area *save = &svm->vmcb->save;
  684. svm->vcpu.fpu_active = 1;
  685. svm->vcpu.arch.hflags = 0;
  686. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  687. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  688. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  689. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  690. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  691. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  692. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  693. set_dr_intercept(svm, INTERCEPT_DR0_READ);
  694. set_dr_intercept(svm, INTERCEPT_DR1_READ);
  695. set_dr_intercept(svm, INTERCEPT_DR2_READ);
  696. set_dr_intercept(svm, INTERCEPT_DR3_READ);
  697. set_dr_intercept(svm, INTERCEPT_DR4_READ);
  698. set_dr_intercept(svm, INTERCEPT_DR5_READ);
  699. set_dr_intercept(svm, INTERCEPT_DR6_READ);
  700. set_dr_intercept(svm, INTERCEPT_DR7_READ);
  701. set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
  702. set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
  703. set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
  704. set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
  705. set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
  706. set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
  707. set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
  708. set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
  709. set_exception_intercept(svm, PF_VECTOR);
  710. set_exception_intercept(svm, UD_VECTOR);
  711. set_exception_intercept(svm, MC_VECTOR);
  712. set_intercept(svm, INTERCEPT_INTR);
  713. set_intercept(svm, INTERCEPT_NMI);
  714. set_intercept(svm, INTERCEPT_SMI);
  715. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  716. set_intercept(svm, INTERCEPT_CPUID);
  717. set_intercept(svm, INTERCEPT_INVD);
  718. set_intercept(svm, INTERCEPT_HLT);
  719. set_intercept(svm, INTERCEPT_INVLPG);
  720. set_intercept(svm, INTERCEPT_INVLPGA);
  721. set_intercept(svm, INTERCEPT_IOIO_PROT);
  722. set_intercept(svm, INTERCEPT_MSR_PROT);
  723. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  724. set_intercept(svm, INTERCEPT_SHUTDOWN);
  725. set_intercept(svm, INTERCEPT_VMRUN);
  726. set_intercept(svm, INTERCEPT_VMMCALL);
  727. set_intercept(svm, INTERCEPT_VMLOAD);
  728. set_intercept(svm, INTERCEPT_VMSAVE);
  729. set_intercept(svm, INTERCEPT_STGI);
  730. set_intercept(svm, INTERCEPT_CLGI);
  731. set_intercept(svm, INTERCEPT_SKINIT);
  732. set_intercept(svm, INTERCEPT_WBINVD);
  733. set_intercept(svm, INTERCEPT_MONITOR);
  734. set_intercept(svm, INTERCEPT_MWAIT);
  735. set_intercept(svm, INTERCEPT_XSETBV);
  736. control->iopm_base_pa = iopm_base;
  737. control->msrpm_base_pa = __pa(svm->msrpm);
  738. control->int_ctl = V_INTR_MASKING_MASK;
  739. init_seg(&save->es);
  740. init_seg(&save->ss);
  741. init_seg(&save->ds);
  742. init_seg(&save->fs);
  743. init_seg(&save->gs);
  744. save->cs.selector = 0xf000;
  745. /* Executable/Readable Code Segment */
  746. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  747. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  748. save->cs.limit = 0xffff;
  749. /*
  750. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  751. * be consistent with it.
  752. *
  753. * Replace when we have real mode working for vmx.
  754. */
  755. save->cs.base = 0xf0000;
  756. save->gdtr.limit = 0xffff;
  757. save->idtr.limit = 0xffff;
  758. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  759. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  760. svm_set_efer(&svm->vcpu, 0);
  761. save->dr6 = 0xffff0ff0;
  762. save->dr7 = 0x400;
  763. save->rflags = 2;
  764. save->rip = 0x0000fff0;
  765. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  766. /*
  767. * This is the guest-visible cr0 value.
  768. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  769. */
  770. svm->vcpu.arch.cr0 = 0;
  771. (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  772. save->cr4 = X86_CR4_PAE;
  773. /* rdx = ?? */
  774. if (npt_enabled) {
  775. /* Setup VMCB for Nested Paging */
  776. control->nested_ctl = 1;
  777. clr_intercept(svm, INTERCEPT_TASK_SWITCH);
  778. clr_intercept(svm, INTERCEPT_INVLPG);
  779. clr_exception_intercept(svm, PF_VECTOR);
  780. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  781. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  782. save->g_pat = 0x0007040600070406ULL;
  783. save->cr3 = 0;
  784. save->cr4 = 0;
  785. }
  786. svm->asid_generation = 0;
  787. svm->nested.vmcb = 0;
  788. svm->vcpu.arch.hflags = 0;
  789. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  790. control->pause_filter_count = 3000;
  791. set_intercept(svm, INTERCEPT_PAUSE);
  792. }
  793. mark_all_dirty(svm->vmcb);
  794. enable_gif(svm);
  795. }
  796. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  797. {
  798. struct vcpu_svm *svm = to_svm(vcpu);
  799. init_vmcb(svm);
  800. if (!kvm_vcpu_is_bsp(vcpu)) {
  801. kvm_rip_write(vcpu, 0);
  802. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  803. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  804. }
  805. vcpu->arch.regs_avail = ~0;
  806. vcpu->arch.regs_dirty = ~0;
  807. return 0;
  808. }
  809. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  810. {
  811. struct vcpu_svm *svm;
  812. struct page *page;
  813. struct page *msrpm_pages;
  814. struct page *hsave_page;
  815. struct page *nested_msrpm_pages;
  816. int err;
  817. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  818. if (!svm) {
  819. err = -ENOMEM;
  820. goto out;
  821. }
  822. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  823. if (err)
  824. goto free_svm;
  825. err = -ENOMEM;
  826. page = alloc_page(GFP_KERNEL);
  827. if (!page)
  828. goto uninit;
  829. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  830. if (!msrpm_pages)
  831. goto free_page1;
  832. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  833. if (!nested_msrpm_pages)
  834. goto free_page2;
  835. hsave_page = alloc_page(GFP_KERNEL);
  836. if (!hsave_page)
  837. goto free_page3;
  838. svm->nested.hsave = page_address(hsave_page);
  839. svm->msrpm = page_address(msrpm_pages);
  840. svm_vcpu_init_msrpm(svm->msrpm);
  841. svm->nested.msrpm = page_address(nested_msrpm_pages);
  842. svm_vcpu_init_msrpm(svm->nested.msrpm);
  843. svm->vmcb = page_address(page);
  844. clear_page(svm->vmcb);
  845. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  846. svm->asid_generation = 0;
  847. init_vmcb(svm);
  848. kvm_write_tsc(&svm->vcpu, 0);
  849. err = fx_init(&svm->vcpu);
  850. if (err)
  851. goto free_page4;
  852. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  853. if (kvm_vcpu_is_bsp(&svm->vcpu))
  854. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  855. return &svm->vcpu;
  856. free_page4:
  857. __free_page(hsave_page);
  858. free_page3:
  859. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  860. free_page2:
  861. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  862. free_page1:
  863. __free_page(page);
  864. uninit:
  865. kvm_vcpu_uninit(&svm->vcpu);
  866. free_svm:
  867. kmem_cache_free(kvm_vcpu_cache, svm);
  868. out:
  869. return ERR_PTR(err);
  870. }
  871. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  872. {
  873. struct vcpu_svm *svm = to_svm(vcpu);
  874. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  875. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  876. __free_page(virt_to_page(svm->nested.hsave));
  877. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  878. kvm_vcpu_uninit(vcpu);
  879. kmem_cache_free(kvm_vcpu_cache, svm);
  880. }
  881. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  882. {
  883. struct vcpu_svm *svm = to_svm(vcpu);
  884. int i;
  885. if (unlikely(cpu != vcpu->cpu)) {
  886. svm->asid_generation = 0;
  887. mark_all_dirty(svm->vmcb);
  888. }
  889. #ifdef CONFIG_X86_64
  890. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  891. #endif
  892. savesegment(fs, svm->host.fs);
  893. savesegment(gs, svm->host.gs);
  894. svm->host.ldt = kvm_read_ldt();
  895. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  896. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  897. }
  898. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  899. {
  900. struct vcpu_svm *svm = to_svm(vcpu);
  901. int i;
  902. ++vcpu->stat.host_state_reload;
  903. kvm_load_ldt(svm->host.ldt);
  904. #ifdef CONFIG_X86_64
  905. loadsegment(fs, svm->host.fs);
  906. load_gs_index(svm->host.gs);
  907. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
  908. #else
  909. loadsegment(gs, svm->host.gs);
  910. #endif
  911. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  912. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  913. }
  914. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  915. {
  916. return to_svm(vcpu)->vmcb->save.rflags;
  917. }
  918. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  919. {
  920. to_svm(vcpu)->vmcb->save.rflags = rflags;
  921. }
  922. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  923. {
  924. switch (reg) {
  925. case VCPU_EXREG_PDPTR:
  926. BUG_ON(!npt_enabled);
  927. load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
  928. break;
  929. default:
  930. BUG();
  931. }
  932. }
  933. static void svm_set_vintr(struct vcpu_svm *svm)
  934. {
  935. set_intercept(svm, INTERCEPT_VINTR);
  936. }
  937. static void svm_clear_vintr(struct vcpu_svm *svm)
  938. {
  939. clr_intercept(svm, INTERCEPT_VINTR);
  940. }
  941. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  942. {
  943. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  944. switch (seg) {
  945. case VCPU_SREG_CS: return &save->cs;
  946. case VCPU_SREG_DS: return &save->ds;
  947. case VCPU_SREG_ES: return &save->es;
  948. case VCPU_SREG_FS: return &save->fs;
  949. case VCPU_SREG_GS: return &save->gs;
  950. case VCPU_SREG_SS: return &save->ss;
  951. case VCPU_SREG_TR: return &save->tr;
  952. case VCPU_SREG_LDTR: return &save->ldtr;
  953. }
  954. BUG();
  955. return NULL;
  956. }
  957. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  958. {
  959. struct vmcb_seg *s = svm_seg(vcpu, seg);
  960. return s->base;
  961. }
  962. static void svm_get_segment(struct kvm_vcpu *vcpu,
  963. struct kvm_segment *var, int seg)
  964. {
  965. struct vmcb_seg *s = svm_seg(vcpu, seg);
  966. var->base = s->base;
  967. var->limit = s->limit;
  968. var->selector = s->selector;
  969. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  970. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  971. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  972. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  973. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  974. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  975. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  976. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  977. /*
  978. * AMD's VMCB does not have an explicit unusable field, so emulate it
  979. * for cross vendor migration purposes by "not present"
  980. */
  981. var->unusable = !var->present || (var->type == 0);
  982. switch (seg) {
  983. case VCPU_SREG_CS:
  984. /*
  985. * SVM always stores 0 for the 'G' bit in the CS selector in
  986. * the VMCB on a VMEXIT. This hurts cross-vendor migration:
  987. * Intel's VMENTRY has a check on the 'G' bit.
  988. */
  989. var->g = s->limit > 0xfffff;
  990. break;
  991. case VCPU_SREG_TR:
  992. /*
  993. * Work around a bug where the busy flag in the tr selector
  994. * isn't exposed
  995. */
  996. var->type |= 0x2;
  997. break;
  998. case VCPU_SREG_DS:
  999. case VCPU_SREG_ES:
  1000. case VCPU_SREG_FS:
  1001. case VCPU_SREG_GS:
  1002. /*
  1003. * The accessed bit must always be set in the segment
  1004. * descriptor cache, although it can be cleared in the
  1005. * descriptor, the cached bit always remains at 1. Since
  1006. * Intel has a check on this, set it here to support
  1007. * cross-vendor migration.
  1008. */
  1009. if (!var->unusable)
  1010. var->type |= 0x1;
  1011. break;
  1012. case VCPU_SREG_SS:
  1013. /*
  1014. * On AMD CPUs sometimes the DB bit in the segment
  1015. * descriptor is left as 1, although the whole segment has
  1016. * been made unusable. Clear it here to pass an Intel VMX
  1017. * entry check when cross vendor migrating.
  1018. */
  1019. if (var->unusable)
  1020. var->db = 0;
  1021. break;
  1022. }
  1023. }
  1024. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1025. {
  1026. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1027. return save->cpl;
  1028. }
  1029. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1030. {
  1031. struct vcpu_svm *svm = to_svm(vcpu);
  1032. dt->size = svm->vmcb->save.idtr.limit;
  1033. dt->address = svm->vmcb->save.idtr.base;
  1034. }
  1035. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1036. {
  1037. struct vcpu_svm *svm = to_svm(vcpu);
  1038. svm->vmcb->save.idtr.limit = dt->size;
  1039. svm->vmcb->save.idtr.base = dt->address ;
  1040. mark_dirty(svm->vmcb, VMCB_DT);
  1041. }
  1042. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1043. {
  1044. struct vcpu_svm *svm = to_svm(vcpu);
  1045. dt->size = svm->vmcb->save.gdtr.limit;
  1046. dt->address = svm->vmcb->save.gdtr.base;
  1047. }
  1048. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1049. {
  1050. struct vcpu_svm *svm = to_svm(vcpu);
  1051. svm->vmcb->save.gdtr.limit = dt->size;
  1052. svm->vmcb->save.gdtr.base = dt->address ;
  1053. mark_dirty(svm->vmcb, VMCB_DT);
  1054. }
  1055. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1056. {
  1057. }
  1058. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1059. {
  1060. }
  1061. static void update_cr0_intercept(struct vcpu_svm *svm)
  1062. {
  1063. ulong gcr0 = svm->vcpu.arch.cr0;
  1064. u64 *hcr0 = &svm->vmcb->save.cr0;
  1065. if (!svm->vcpu.fpu_active)
  1066. *hcr0 |= SVM_CR0_SELECTIVE_MASK;
  1067. else
  1068. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1069. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1070. mark_dirty(svm->vmcb, VMCB_CR);
  1071. if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
  1072. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  1073. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1074. } else {
  1075. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1076. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1077. }
  1078. }
  1079. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1080. {
  1081. struct vcpu_svm *svm = to_svm(vcpu);
  1082. if (is_guest_mode(vcpu)) {
  1083. /*
  1084. * We are here because we run in nested mode, the host kvm
  1085. * intercepts cr0 writes but the l1 hypervisor does not.
  1086. * But the L1 hypervisor may intercept selective cr0 writes.
  1087. * This needs to be checked here.
  1088. */
  1089. unsigned long old, new;
  1090. /* Remove bits that would trigger a real cr0 write intercept */
  1091. old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
  1092. new = cr0 & SVM_CR0_SELECTIVE_MASK;
  1093. if (old == new) {
  1094. /* cr0 write with ts and mp unchanged */
  1095. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  1096. if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
  1097. svm->nested.vmexit_rip = kvm_rip_read(vcpu);
  1098. svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  1099. svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  1100. return;
  1101. }
  1102. }
  1103. }
  1104. #ifdef CONFIG_X86_64
  1105. if (vcpu->arch.efer & EFER_LME) {
  1106. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1107. vcpu->arch.efer |= EFER_LMA;
  1108. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1109. }
  1110. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1111. vcpu->arch.efer &= ~EFER_LMA;
  1112. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1113. }
  1114. }
  1115. #endif
  1116. vcpu->arch.cr0 = cr0;
  1117. if (!npt_enabled)
  1118. cr0 |= X86_CR0_PG | X86_CR0_WP;
  1119. if (!vcpu->fpu_active)
  1120. cr0 |= X86_CR0_TS;
  1121. /*
  1122. * re-enable caching here because the QEMU bios
  1123. * does not do it - this results in some delay at
  1124. * reboot
  1125. */
  1126. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1127. svm->vmcb->save.cr0 = cr0;
  1128. mark_dirty(svm->vmcb, VMCB_CR);
  1129. update_cr0_intercept(svm);
  1130. }
  1131. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1132. {
  1133. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  1134. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  1135. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1136. svm_flush_tlb(vcpu);
  1137. vcpu->arch.cr4 = cr4;
  1138. if (!npt_enabled)
  1139. cr4 |= X86_CR4_PAE;
  1140. cr4 |= host_cr4_mce;
  1141. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1142. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  1143. }
  1144. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1145. struct kvm_segment *var, int seg)
  1146. {
  1147. struct vcpu_svm *svm = to_svm(vcpu);
  1148. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1149. s->base = var->base;
  1150. s->limit = var->limit;
  1151. s->selector = var->selector;
  1152. if (var->unusable)
  1153. s->attrib = 0;
  1154. else {
  1155. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1156. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1157. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1158. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  1159. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1160. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1161. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1162. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1163. }
  1164. if (seg == VCPU_SREG_CS)
  1165. svm->vmcb->save.cpl
  1166. = (svm->vmcb->save.cs.attrib
  1167. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1168. mark_dirty(svm->vmcb, VMCB_SEG);
  1169. }
  1170. static void update_db_intercept(struct kvm_vcpu *vcpu)
  1171. {
  1172. struct vcpu_svm *svm = to_svm(vcpu);
  1173. clr_exception_intercept(svm, DB_VECTOR);
  1174. clr_exception_intercept(svm, BP_VECTOR);
  1175. if (svm->nmi_singlestep)
  1176. set_exception_intercept(svm, DB_VECTOR);
  1177. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1178. if (vcpu->guest_debug &
  1179. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  1180. set_exception_intercept(svm, DB_VECTOR);
  1181. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1182. set_exception_intercept(svm, BP_VECTOR);
  1183. } else
  1184. vcpu->guest_debug = 0;
  1185. }
  1186. static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1187. {
  1188. struct vcpu_svm *svm = to_svm(vcpu);
  1189. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1190. svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
  1191. else
  1192. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  1193. mark_dirty(svm->vmcb, VMCB_DR);
  1194. update_db_intercept(vcpu);
  1195. }
  1196. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1197. {
  1198. if (sd->next_asid > sd->max_asid) {
  1199. ++sd->asid_generation;
  1200. sd->next_asid = 1;
  1201. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1202. }
  1203. svm->asid_generation = sd->asid_generation;
  1204. svm->vmcb->control.asid = sd->next_asid++;
  1205. mark_dirty(svm->vmcb, VMCB_ASID);
  1206. }
  1207. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1208. {
  1209. struct vcpu_svm *svm = to_svm(vcpu);
  1210. svm->vmcb->save.dr7 = value;
  1211. mark_dirty(svm->vmcb, VMCB_DR);
  1212. }
  1213. static int pf_interception(struct vcpu_svm *svm)
  1214. {
  1215. u64 fault_address = svm->vmcb->control.exit_info_2;
  1216. u32 error_code;
  1217. int r = 1;
  1218. switch (svm->apf_reason) {
  1219. default:
  1220. error_code = svm->vmcb->control.exit_info_1;
  1221. trace_kvm_page_fault(fault_address, error_code);
  1222. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1223. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1224. r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
  1225. svm->vmcb->control.insn_bytes,
  1226. svm->vmcb->control.insn_len);
  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, 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) == 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. svm_flush_tlb(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. svm_flush_tlb(&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 xsetbv_interception(struct vcpu_svm *svm)
  2045. {
  2046. u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
  2047. u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  2048. if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
  2049. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2050. skip_emulated_instruction(&svm->vcpu);
  2051. }
  2052. return 1;
  2053. }
  2054. static int invalid_op_interception(struct vcpu_svm *svm)
  2055. {
  2056. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2057. return 1;
  2058. }
  2059. static int task_switch_interception(struct vcpu_svm *svm)
  2060. {
  2061. u16 tss_selector;
  2062. int reason;
  2063. int int_type = svm->vmcb->control.exit_int_info &
  2064. SVM_EXITINTINFO_TYPE_MASK;
  2065. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  2066. uint32_t type =
  2067. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  2068. uint32_t idt_v =
  2069. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  2070. bool has_error_code = false;
  2071. u32 error_code = 0;
  2072. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  2073. if (svm->vmcb->control.exit_info_2 &
  2074. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  2075. reason = TASK_SWITCH_IRET;
  2076. else if (svm->vmcb->control.exit_info_2 &
  2077. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  2078. reason = TASK_SWITCH_JMP;
  2079. else if (idt_v)
  2080. reason = TASK_SWITCH_GATE;
  2081. else
  2082. reason = TASK_SWITCH_CALL;
  2083. if (reason == TASK_SWITCH_GATE) {
  2084. switch (type) {
  2085. case SVM_EXITINTINFO_TYPE_NMI:
  2086. svm->vcpu.arch.nmi_injected = false;
  2087. break;
  2088. case SVM_EXITINTINFO_TYPE_EXEPT:
  2089. if (svm->vmcb->control.exit_info_2 &
  2090. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  2091. has_error_code = true;
  2092. error_code =
  2093. (u32)svm->vmcb->control.exit_info_2;
  2094. }
  2095. kvm_clear_exception_queue(&svm->vcpu);
  2096. break;
  2097. case SVM_EXITINTINFO_TYPE_INTR:
  2098. kvm_clear_interrupt_queue(&svm->vcpu);
  2099. break;
  2100. default:
  2101. break;
  2102. }
  2103. }
  2104. if (reason != TASK_SWITCH_GATE ||
  2105. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2106. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2107. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  2108. skip_emulated_instruction(&svm->vcpu);
  2109. if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
  2110. has_error_code, error_code) == EMULATE_FAIL) {
  2111. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2112. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  2113. svm->vcpu.run->internal.ndata = 0;
  2114. return 0;
  2115. }
  2116. return 1;
  2117. }
  2118. static int cpuid_interception(struct vcpu_svm *svm)
  2119. {
  2120. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2121. kvm_emulate_cpuid(&svm->vcpu);
  2122. return 1;
  2123. }
  2124. static int iret_interception(struct vcpu_svm *svm)
  2125. {
  2126. ++svm->vcpu.stat.nmi_window_exits;
  2127. clr_intercept(svm, INTERCEPT_IRET);
  2128. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  2129. return 1;
  2130. }
  2131. static int invlpg_interception(struct vcpu_svm *svm)
  2132. {
  2133. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2134. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2135. kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
  2136. skip_emulated_instruction(&svm->vcpu);
  2137. return 1;
  2138. }
  2139. static int emulate_on_interception(struct vcpu_svm *svm)
  2140. {
  2141. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2142. }
  2143. #define CR_VALID (1ULL << 63)
  2144. static int cr_interception(struct vcpu_svm *svm)
  2145. {
  2146. int reg, cr;
  2147. unsigned long val;
  2148. int err;
  2149. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2150. return emulate_on_interception(svm);
  2151. if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
  2152. return emulate_on_interception(svm);
  2153. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2154. cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
  2155. err = 0;
  2156. if (cr >= 16) { /* mov to cr */
  2157. cr -= 16;
  2158. val = kvm_register_read(&svm->vcpu, reg);
  2159. switch (cr) {
  2160. case 0:
  2161. err = kvm_set_cr0(&svm->vcpu, val);
  2162. break;
  2163. case 3:
  2164. err = kvm_set_cr3(&svm->vcpu, val);
  2165. break;
  2166. case 4:
  2167. err = kvm_set_cr4(&svm->vcpu, val);
  2168. break;
  2169. case 8:
  2170. err = kvm_set_cr8(&svm->vcpu, val);
  2171. break;
  2172. default:
  2173. WARN(1, "unhandled write to CR%d", cr);
  2174. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2175. return 1;
  2176. }
  2177. } else { /* mov from cr */
  2178. switch (cr) {
  2179. case 0:
  2180. val = kvm_read_cr0(&svm->vcpu);
  2181. break;
  2182. case 2:
  2183. val = svm->vcpu.arch.cr2;
  2184. break;
  2185. case 3:
  2186. val = svm->vcpu.arch.cr3;
  2187. break;
  2188. case 4:
  2189. val = kvm_read_cr4(&svm->vcpu);
  2190. break;
  2191. case 8:
  2192. val = kvm_get_cr8(&svm->vcpu);
  2193. break;
  2194. default:
  2195. WARN(1, "unhandled read from CR%d", cr);
  2196. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2197. return 1;
  2198. }
  2199. kvm_register_write(&svm->vcpu, reg, val);
  2200. }
  2201. kvm_complete_insn_gp(&svm->vcpu, err);
  2202. return 1;
  2203. }
  2204. static int cr0_write_interception(struct vcpu_svm *svm)
  2205. {
  2206. struct kvm_vcpu *vcpu = &svm->vcpu;
  2207. int r;
  2208. r = cr_interception(svm);
  2209. if (svm->nested.vmexit_rip) {
  2210. kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
  2211. kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
  2212. kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
  2213. svm->nested.vmexit_rip = 0;
  2214. }
  2215. return r;
  2216. }
  2217. static int dr_interception(struct vcpu_svm *svm)
  2218. {
  2219. int reg, dr;
  2220. unsigned long val;
  2221. int err;
  2222. if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
  2223. return emulate_on_interception(svm);
  2224. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2225. dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
  2226. if (dr >= 16) { /* mov to DRn */
  2227. val = kvm_register_read(&svm->vcpu, reg);
  2228. kvm_set_dr(&svm->vcpu, dr - 16, val);
  2229. } else {
  2230. err = kvm_get_dr(&svm->vcpu, dr, &val);
  2231. if (!err)
  2232. kvm_register_write(&svm->vcpu, reg, val);
  2233. }
  2234. return 1;
  2235. }
  2236. static int cr8_write_interception(struct vcpu_svm *svm)
  2237. {
  2238. struct kvm_run *kvm_run = svm->vcpu.run;
  2239. int r;
  2240. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  2241. /* instruction emulation calls kvm_set_cr8() */
  2242. r = cr_interception(svm);
  2243. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  2244. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2245. return r;
  2246. }
  2247. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  2248. return r;
  2249. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2250. return 0;
  2251. }
  2252. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  2253. {
  2254. struct vcpu_svm *svm = to_svm(vcpu);
  2255. switch (ecx) {
  2256. case MSR_IA32_TSC: {
  2257. struct vmcb *vmcb = get_host_vmcb(svm);
  2258. *data = vmcb->control.tsc_offset + native_read_tsc();
  2259. break;
  2260. }
  2261. case MSR_STAR:
  2262. *data = svm->vmcb->save.star;
  2263. break;
  2264. #ifdef CONFIG_X86_64
  2265. case MSR_LSTAR:
  2266. *data = svm->vmcb->save.lstar;
  2267. break;
  2268. case MSR_CSTAR:
  2269. *data = svm->vmcb->save.cstar;
  2270. break;
  2271. case MSR_KERNEL_GS_BASE:
  2272. *data = svm->vmcb->save.kernel_gs_base;
  2273. break;
  2274. case MSR_SYSCALL_MASK:
  2275. *data = svm->vmcb->save.sfmask;
  2276. break;
  2277. #endif
  2278. case MSR_IA32_SYSENTER_CS:
  2279. *data = svm->vmcb->save.sysenter_cs;
  2280. break;
  2281. case MSR_IA32_SYSENTER_EIP:
  2282. *data = svm->sysenter_eip;
  2283. break;
  2284. case MSR_IA32_SYSENTER_ESP:
  2285. *data = svm->sysenter_esp;
  2286. break;
  2287. /*
  2288. * Nobody will change the following 5 values in the VMCB so we can
  2289. * safely return them on rdmsr. They will always be 0 until LBRV is
  2290. * implemented.
  2291. */
  2292. case MSR_IA32_DEBUGCTLMSR:
  2293. *data = svm->vmcb->save.dbgctl;
  2294. break;
  2295. case MSR_IA32_LASTBRANCHFROMIP:
  2296. *data = svm->vmcb->save.br_from;
  2297. break;
  2298. case MSR_IA32_LASTBRANCHTOIP:
  2299. *data = svm->vmcb->save.br_to;
  2300. break;
  2301. case MSR_IA32_LASTINTFROMIP:
  2302. *data = svm->vmcb->save.last_excp_from;
  2303. break;
  2304. case MSR_IA32_LASTINTTOIP:
  2305. *data = svm->vmcb->save.last_excp_to;
  2306. break;
  2307. case MSR_VM_HSAVE_PA:
  2308. *data = svm->nested.hsave_msr;
  2309. break;
  2310. case MSR_VM_CR:
  2311. *data = svm->nested.vm_cr_msr;
  2312. break;
  2313. case MSR_IA32_UCODE_REV:
  2314. *data = 0x01000065;
  2315. break;
  2316. default:
  2317. return kvm_get_msr_common(vcpu, ecx, data);
  2318. }
  2319. return 0;
  2320. }
  2321. static int rdmsr_interception(struct vcpu_svm *svm)
  2322. {
  2323. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2324. u64 data;
  2325. if (svm_get_msr(&svm->vcpu, ecx, &data)) {
  2326. trace_kvm_msr_read_ex(ecx);
  2327. kvm_inject_gp(&svm->vcpu, 0);
  2328. } else {
  2329. trace_kvm_msr_read(ecx, data);
  2330. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  2331. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  2332. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2333. skip_emulated_instruction(&svm->vcpu);
  2334. }
  2335. return 1;
  2336. }
  2337. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2338. {
  2339. struct vcpu_svm *svm = to_svm(vcpu);
  2340. int svm_dis, chg_mask;
  2341. if (data & ~SVM_VM_CR_VALID_MASK)
  2342. return 1;
  2343. chg_mask = SVM_VM_CR_VALID_MASK;
  2344. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2345. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2346. svm->nested.vm_cr_msr &= ~chg_mask;
  2347. svm->nested.vm_cr_msr |= (data & chg_mask);
  2348. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2349. /* check for svm_disable while efer.svme is set */
  2350. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2351. return 1;
  2352. return 0;
  2353. }
  2354. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  2355. {
  2356. struct vcpu_svm *svm = to_svm(vcpu);
  2357. switch (ecx) {
  2358. case MSR_IA32_TSC:
  2359. kvm_write_tsc(vcpu, data);
  2360. break;
  2361. case MSR_STAR:
  2362. svm->vmcb->save.star = data;
  2363. break;
  2364. #ifdef CONFIG_X86_64
  2365. case MSR_LSTAR:
  2366. svm->vmcb->save.lstar = data;
  2367. break;
  2368. case MSR_CSTAR:
  2369. svm->vmcb->save.cstar = data;
  2370. break;
  2371. case MSR_KERNEL_GS_BASE:
  2372. svm->vmcb->save.kernel_gs_base = data;
  2373. break;
  2374. case MSR_SYSCALL_MASK:
  2375. svm->vmcb->save.sfmask = data;
  2376. break;
  2377. #endif
  2378. case MSR_IA32_SYSENTER_CS:
  2379. svm->vmcb->save.sysenter_cs = data;
  2380. break;
  2381. case MSR_IA32_SYSENTER_EIP:
  2382. svm->sysenter_eip = data;
  2383. svm->vmcb->save.sysenter_eip = data;
  2384. break;
  2385. case MSR_IA32_SYSENTER_ESP:
  2386. svm->sysenter_esp = data;
  2387. svm->vmcb->save.sysenter_esp = data;
  2388. break;
  2389. case MSR_IA32_DEBUGCTLMSR:
  2390. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  2391. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  2392. __func__, data);
  2393. break;
  2394. }
  2395. if (data & DEBUGCTL_RESERVED_BITS)
  2396. return 1;
  2397. svm->vmcb->save.dbgctl = data;
  2398. mark_dirty(svm->vmcb, VMCB_LBR);
  2399. if (data & (1ULL<<0))
  2400. svm_enable_lbrv(svm);
  2401. else
  2402. svm_disable_lbrv(svm);
  2403. break;
  2404. case MSR_VM_HSAVE_PA:
  2405. svm->nested.hsave_msr = data;
  2406. break;
  2407. case MSR_VM_CR:
  2408. return svm_set_vm_cr(vcpu, data);
  2409. case MSR_VM_IGNNE:
  2410. pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  2411. break;
  2412. default:
  2413. return kvm_set_msr_common(vcpu, ecx, data);
  2414. }
  2415. return 0;
  2416. }
  2417. static int wrmsr_interception(struct vcpu_svm *svm)
  2418. {
  2419. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2420. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  2421. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2422. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2423. if (svm_set_msr(&svm->vcpu, ecx, data)) {
  2424. trace_kvm_msr_write_ex(ecx, data);
  2425. kvm_inject_gp(&svm->vcpu, 0);
  2426. } else {
  2427. trace_kvm_msr_write(ecx, data);
  2428. skip_emulated_instruction(&svm->vcpu);
  2429. }
  2430. return 1;
  2431. }
  2432. static int msr_interception(struct vcpu_svm *svm)
  2433. {
  2434. if (svm->vmcb->control.exit_info_1)
  2435. return wrmsr_interception(svm);
  2436. else
  2437. return rdmsr_interception(svm);
  2438. }
  2439. static int interrupt_window_interception(struct vcpu_svm *svm)
  2440. {
  2441. struct kvm_run *kvm_run = svm->vcpu.run;
  2442. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2443. svm_clear_vintr(svm);
  2444. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2445. mark_dirty(svm->vmcb, VMCB_INTR);
  2446. /*
  2447. * If the user space waits to inject interrupts, exit as soon as
  2448. * possible
  2449. */
  2450. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  2451. kvm_run->request_interrupt_window &&
  2452. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  2453. ++svm->vcpu.stat.irq_window_exits;
  2454. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2455. return 0;
  2456. }
  2457. return 1;
  2458. }
  2459. static int pause_interception(struct vcpu_svm *svm)
  2460. {
  2461. kvm_vcpu_on_spin(&(svm->vcpu));
  2462. return 1;
  2463. }
  2464. static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
  2465. [SVM_EXIT_READ_CR0] = cr_interception,
  2466. [SVM_EXIT_READ_CR3] = cr_interception,
  2467. [SVM_EXIT_READ_CR4] = cr_interception,
  2468. [SVM_EXIT_READ_CR8] = cr_interception,
  2469. [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
  2470. [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
  2471. [SVM_EXIT_WRITE_CR3] = cr_interception,
  2472. [SVM_EXIT_WRITE_CR4] = cr_interception,
  2473. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2474. [SVM_EXIT_READ_DR0] = dr_interception,
  2475. [SVM_EXIT_READ_DR1] = dr_interception,
  2476. [SVM_EXIT_READ_DR2] = dr_interception,
  2477. [SVM_EXIT_READ_DR3] = dr_interception,
  2478. [SVM_EXIT_READ_DR4] = dr_interception,
  2479. [SVM_EXIT_READ_DR5] = dr_interception,
  2480. [SVM_EXIT_READ_DR6] = dr_interception,
  2481. [SVM_EXIT_READ_DR7] = dr_interception,
  2482. [SVM_EXIT_WRITE_DR0] = dr_interception,
  2483. [SVM_EXIT_WRITE_DR1] = dr_interception,
  2484. [SVM_EXIT_WRITE_DR2] = dr_interception,
  2485. [SVM_EXIT_WRITE_DR3] = dr_interception,
  2486. [SVM_EXIT_WRITE_DR4] = dr_interception,
  2487. [SVM_EXIT_WRITE_DR5] = dr_interception,
  2488. [SVM_EXIT_WRITE_DR6] = dr_interception,
  2489. [SVM_EXIT_WRITE_DR7] = dr_interception,
  2490. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2491. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2492. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2493. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2494. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  2495. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2496. [SVM_EXIT_INTR] = intr_interception,
  2497. [SVM_EXIT_NMI] = nmi_interception,
  2498. [SVM_EXIT_SMI] = nop_on_interception,
  2499. [SVM_EXIT_INIT] = nop_on_interception,
  2500. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2501. [SVM_EXIT_CPUID] = cpuid_interception,
  2502. [SVM_EXIT_IRET] = iret_interception,
  2503. [SVM_EXIT_INVD] = emulate_on_interception,
  2504. [SVM_EXIT_PAUSE] = pause_interception,
  2505. [SVM_EXIT_HLT] = halt_interception,
  2506. [SVM_EXIT_INVLPG] = invlpg_interception,
  2507. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2508. [SVM_EXIT_IOIO] = io_interception,
  2509. [SVM_EXIT_MSR] = msr_interception,
  2510. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2511. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2512. [SVM_EXIT_VMRUN] = vmrun_interception,
  2513. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  2514. [SVM_EXIT_VMLOAD] = vmload_interception,
  2515. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2516. [SVM_EXIT_STGI] = stgi_interception,
  2517. [SVM_EXIT_CLGI] = clgi_interception,
  2518. [SVM_EXIT_SKINIT] = skinit_interception,
  2519. [SVM_EXIT_WBINVD] = emulate_on_interception,
  2520. [SVM_EXIT_MONITOR] = invalid_op_interception,
  2521. [SVM_EXIT_MWAIT] = invalid_op_interception,
  2522. [SVM_EXIT_XSETBV] = xsetbv_interception,
  2523. [SVM_EXIT_NPF] = pf_interception,
  2524. };
  2525. void dump_vmcb(struct kvm_vcpu *vcpu)
  2526. {
  2527. struct vcpu_svm *svm = to_svm(vcpu);
  2528. struct vmcb_control_area *control = &svm->vmcb->control;
  2529. struct vmcb_save_area *save = &svm->vmcb->save;
  2530. pr_err("VMCB Control Area:\n");
  2531. pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
  2532. pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
  2533. pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
  2534. pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
  2535. pr_err("exceptions: %08x\n", control->intercept_exceptions);
  2536. pr_err("intercepts: %016llx\n", control->intercept);
  2537. pr_err("pause filter count: %d\n", control->pause_filter_count);
  2538. pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
  2539. pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
  2540. pr_err("tsc_offset: %016llx\n", control->tsc_offset);
  2541. pr_err("asid: %d\n", control->asid);
  2542. pr_err("tlb_ctl: %d\n", control->tlb_ctl);
  2543. pr_err("int_ctl: %08x\n", control->int_ctl);
  2544. pr_err("int_vector: %08x\n", control->int_vector);
  2545. pr_err("int_state: %08x\n", control->int_state);
  2546. pr_err("exit_code: %08x\n", control->exit_code);
  2547. pr_err("exit_info1: %016llx\n", control->exit_info_1);
  2548. pr_err("exit_info2: %016llx\n", control->exit_info_2);
  2549. pr_err("exit_int_info: %08x\n", control->exit_int_info);
  2550. pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
  2551. pr_err("nested_ctl: %lld\n", control->nested_ctl);
  2552. pr_err("nested_cr3: %016llx\n", control->nested_cr3);
  2553. pr_err("event_inj: %08x\n", control->event_inj);
  2554. pr_err("event_inj_err: %08x\n", control->event_inj_err);
  2555. pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
  2556. pr_err("next_rip: %016llx\n", control->next_rip);
  2557. pr_err("VMCB State Save Area:\n");
  2558. pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
  2559. save->es.selector, save->es.attrib,
  2560. save->es.limit, save->es.base);
  2561. pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
  2562. save->cs.selector, save->cs.attrib,
  2563. save->cs.limit, save->cs.base);
  2564. pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
  2565. save->ss.selector, save->ss.attrib,
  2566. save->ss.limit, save->ss.base);
  2567. pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
  2568. save->ds.selector, save->ds.attrib,
  2569. save->ds.limit, save->ds.base);
  2570. pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
  2571. save->fs.selector, save->fs.attrib,
  2572. save->fs.limit, save->fs.base);
  2573. pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
  2574. save->gs.selector, save->gs.attrib,
  2575. save->gs.limit, save->gs.base);
  2576. pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2577. save->gdtr.selector, save->gdtr.attrib,
  2578. save->gdtr.limit, save->gdtr.base);
  2579. pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2580. save->ldtr.selector, save->ldtr.attrib,
  2581. save->ldtr.limit, save->ldtr.base);
  2582. pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2583. save->idtr.selector, save->idtr.attrib,
  2584. save->idtr.limit, save->idtr.base);
  2585. pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
  2586. save->tr.selector, save->tr.attrib,
  2587. save->tr.limit, save->tr.base);
  2588. pr_err("cpl: %d efer: %016llx\n",
  2589. save->cpl, save->efer);
  2590. pr_err("cr0: %016llx cr2: %016llx\n",
  2591. save->cr0, save->cr2);
  2592. pr_err("cr3: %016llx cr4: %016llx\n",
  2593. save->cr3, save->cr4);
  2594. pr_err("dr6: %016llx dr7: %016llx\n",
  2595. save->dr6, save->dr7);
  2596. pr_err("rip: %016llx rflags: %016llx\n",
  2597. save->rip, save->rflags);
  2598. pr_err("rsp: %016llx rax: %016llx\n",
  2599. save->rsp, save->rax);
  2600. pr_err("star: %016llx lstar: %016llx\n",
  2601. save->star, save->lstar);
  2602. pr_err("cstar: %016llx sfmask: %016llx\n",
  2603. save->cstar, save->sfmask);
  2604. pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
  2605. save->kernel_gs_base, save->sysenter_cs);
  2606. pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
  2607. save->sysenter_esp, save->sysenter_eip);
  2608. pr_err("gpat: %016llx dbgctl: %016llx\n",
  2609. save->g_pat, save->dbgctl);
  2610. pr_err("br_from: %016llx br_to: %016llx\n",
  2611. save->br_from, save->br_to);
  2612. pr_err("excp_from: %016llx excp_to: %016llx\n",
  2613. save->last_excp_from, save->last_excp_to);
  2614. }
  2615. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  2616. {
  2617. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  2618. *info1 = control->exit_info_1;
  2619. *info2 = control->exit_info_2;
  2620. }
  2621. static int handle_exit(struct kvm_vcpu *vcpu)
  2622. {
  2623. struct vcpu_svm *svm = to_svm(vcpu);
  2624. struct kvm_run *kvm_run = vcpu->run;
  2625. u32 exit_code = svm->vmcb->control.exit_code;
  2626. trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
  2627. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  2628. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2629. if (npt_enabled)
  2630. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2631. if (unlikely(svm->nested.exit_required)) {
  2632. nested_svm_vmexit(svm);
  2633. svm->nested.exit_required = false;
  2634. return 1;
  2635. }
  2636. if (is_guest_mode(vcpu)) {
  2637. int vmexit;
  2638. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  2639. svm->vmcb->control.exit_info_1,
  2640. svm->vmcb->control.exit_info_2,
  2641. svm->vmcb->control.exit_int_info,
  2642. svm->vmcb->control.exit_int_info_err);
  2643. vmexit = nested_svm_exit_special(svm);
  2644. if (vmexit == NESTED_EXIT_CONTINUE)
  2645. vmexit = nested_svm_exit_handled(svm);
  2646. if (vmexit == NESTED_EXIT_DONE)
  2647. return 1;
  2648. }
  2649. svm_complete_interrupts(svm);
  2650. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2651. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2652. kvm_run->fail_entry.hardware_entry_failure_reason
  2653. = svm->vmcb->control.exit_code;
  2654. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  2655. dump_vmcb(vcpu);
  2656. return 0;
  2657. }
  2658. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2659. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2660. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  2661. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  2662. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  2663. "exit_code 0x%x\n",
  2664. __func__, svm->vmcb->control.exit_int_info,
  2665. exit_code);
  2666. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2667. || !svm_exit_handlers[exit_code]) {
  2668. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2669. kvm_run->hw.hardware_exit_reason = exit_code;
  2670. return 0;
  2671. }
  2672. return svm_exit_handlers[exit_code](svm);
  2673. }
  2674. static void reload_tss(struct kvm_vcpu *vcpu)
  2675. {
  2676. int cpu = raw_smp_processor_id();
  2677. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2678. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2679. load_TR_desc();
  2680. }
  2681. static void pre_svm_run(struct vcpu_svm *svm)
  2682. {
  2683. int cpu = raw_smp_processor_id();
  2684. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2685. /* FIXME: handle wraparound of asid_generation */
  2686. if (svm->asid_generation != sd->asid_generation)
  2687. new_asid(svm, sd);
  2688. }
  2689. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2690. {
  2691. struct vcpu_svm *svm = to_svm(vcpu);
  2692. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2693. vcpu->arch.hflags |= HF_NMI_MASK;
  2694. set_intercept(svm, INTERCEPT_IRET);
  2695. ++vcpu->stat.nmi_injections;
  2696. }
  2697. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2698. {
  2699. struct vmcb_control_area *control;
  2700. control = &svm->vmcb->control;
  2701. control->int_vector = irq;
  2702. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2703. control->int_ctl |= V_IRQ_MASK |
  2704. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2705. mark_dirty(svm->vmcb, VMCB_INTR);
  2706. }
  2707. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2708. {
  2709. struct vcpu_svm *svm = to_svm(vcpu);
  2710. BUG_ON(!(gif_set(svm)));
  2711. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  2712. ++vcpu->stat.irq_injections;
  2713. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2714. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2715. }
  2716. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2717. {
  2718. struct vcpu_svm *svm = to_svm(vcpu);
  2719. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2720. return;
  2721. if (irr == -1)
  2722. return;
  2723. if (tpr >= irr)
  2724. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2725. }
  2726. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2727. {
  2728. struct vcpu_svm *svm = to_svm(vcpu);
  2729. struct vmcb *vmcb = svm->vmcb;
  2730. int ret;
  2731. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2732. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2733. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  2734. return ret;
  2735. }
  2736. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  2737. {
  2738. struct vcpu_svm *svm = to_svm(vcpu);
  2739. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2740. }
  2741. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2742. {
  2743. struct vcpu_svm *svm = to_svm(vcpu);
  2744. if (masked) {
  2745. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  2746. set_intercept(svm, INTERCEPT_IRET);
  2747. } else {
  2748. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  2749. clr_intercept(svm, INTERCEPT_IRET);
  2750. }
  2751. }
  2752. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  2753. {
  2754. struct vcpu_svm *svm = to_svm(vcpu);
  2755. struct vmcb *vmcb = svm->vmcb;
  2756. int ret;
  2757. if (!gif_set(svm) ||
  2758. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  2759. return 0;
  2760. ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
  2761. if (is_guest_mode(vcpu))
  2762. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  2763. return ret;
  2764. }
  2765. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2766. {
  2767. struct vcpu_svm *svm = to_svm(vcpu);
  2768. /*
  2769. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  2770. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  2771. * get that intercept, this function will be called again though and
  2772. * we'll get the vintr intercept.
  2773. */
  2774. if (gif_set(svm) && nested_svm_intr(svm)) {
  2775. svm_set_vintr(svm);
  2776. svm_inject_irq(svm, 0x0);
  2777. }
  2778. }
  2779. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2780. {
  2781. struct vcpu_svm *svm = to_svm(vcpu);
  2782. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  2783. == HF_NMI_MASK)
  2784. return; /* IRET will cause a vm exit */
  2785. /*
  2786. * Something prevents NMI from been injected. Single step over possible
  2787. * problem (IRET or exception injection or interrupt shadow)
  2788. */
  2789. svm->nmi_singlestep = true;
  2790. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  2791. update_db_intercept(vcpu);
  2792. }
  2793. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2794. {
  2795. return 0;
  2796. }
  2797. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  2798. {
  2799. struct vcpu_svm *svm = to_svm(vcpu);
  2800. if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
  2801. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
  2802. else
  2803. svm->asid_generation--;
  2804. }
  2805. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  2806. {
  2807. }
  2808. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2809. {
  2810. struct vcpu_svm *svm = to_svm(vcpu);
  2811. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2812. return;
  2813. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  2814. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2815. kvm_set_cr8(vcpu, cr8);
  2816. }
  2817. }
  2818. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2819. {
  2820. struct vcpu_svm *svm = to_svm(vcpu);
  2821. u64 cr8;
  2822. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2823. return;
  2824. cr8 = kvm_get_cr8(vcpu);
  2825. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2826. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2827. }
  2828. static void svm_complete_interrupts(struct vcpu_svm *svm)
  2829. {
  2830. u8 vector;
  2831. int type;
  2832. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  2833. unsigned int3_injected = svm->int3_injected;
  2834. svm->int3_injected = 0;
  2835. if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
  2836. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  2837. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2838. }
  2839. svm->vcpu.arch.nmi_injected = false;
  2840. kvm_clear_exception_queue(&svm->vcpu);
  2841. kvm_clear_interrupt_queue(&svm->vcpu);
  2842. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  2843. return;
  2844. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2845. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  2846. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  2847. switch (type) {
  2848. case SVM_EXITINTINFO_TYPE_NMI:
  2849. svm->vcpu.arch.nmi_injected = true;
  2850. break;
  2851. case SVM_EXITINTINFO_TYPE_EXEPT:
  2852. /*
  2853. * In case of software exceptions, do not reinject the vector,
  2854. * but re-execute the instruction instead. Rewind RIP first
  2855. * if we emulated INT3 before.
  2856. */
  2857. if (kvm_exception_is_soft(vector)) {
  2858. if (vector == BP_VECTOR && int3_injected &&
  2859. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  2860. kvm_rip_write(&svm->vcpu,
  2861. kvm_rip_read(&svm->vcpu) -
  2862. int3_injected);
  2863. break;
  2864. }
  2865. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2866. u32 err = svm->vmcb->control.exit_int_info_err;
  2867. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  2868. } else
  2869. kvm_requeue_exception(&svm->vcpu, vector);
  2870. break;
  2871. case SVM_EXITINTINFO_TYPE_INTR:
  2872. kvm_queue_interrupt(&svm->vcpu, vector, false);
  2873. break;
  2874. default:
  2875. break;
  2876. }
  2877. }
  2878. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  2879. {
  2880. struct vcpu_svm *svm = to_svm(vcpu);
  2881. struct vmcb_control_area *control = &svm->vmcb->control;
  2882. control->exit_int_info = control->event_inj;
  2883. control->exit_int_info_err = control->event_inj_err;
  2884. control->event_inj = 0;
  2885. svm_complete_interrupts(svm);
  2886. }
  2887. #ifdef CONFIG_X86_64
  2888. #define R "r"
  2889. #else
  2890. #define R "e"
  2891. #endif
  2892. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  2893. {
  2894. struct vcpu_svm *svm = to_svm(vcpu);
  2895. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2896. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2897. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2898. /*
  2899. * A vmexit emulation is required before the vcpu can be executed
  2900. * again.
  2901. */
  2902. if (unlikely(svm->nested.exit_required))
  2903. return;
  2904. pre_svm_run(svm);
  2905. sync_lapic_to_cr8(vcpu);
  2906. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2907. clgi();
  2908. local_irq_enable();
  2909. asm volatile (
  2910. "push %%"R"bp; \n\t"
  2911. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2912. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2913. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2914. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2915. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2916. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2917. #ifdef CONFIG_X86_64
  2918. "mov %c[r8](%[svm]), %%r8 \n\t"
  2919. "mov %c[r9](%[svm]), %%r9 \n\t"
  2920. "mov %c[r10](%[svm]), %%r10 \n\t"
  2921. "mov %c[r11](%[svm]), %%r11 \n\t"
  2922. "mov %c[r12](%[svm]), %%r12 \n\t"
  2923. "mov %c[r13](%[svm]), %%r13 \n\t"
  2924. "mov %c[r14](%[svm]), %%r14 \n\t"
  2925. "mov %c[r15](%[svm]), %%r15 \n\t"
  2926. #endif
  2927. /* Enter guest mode */
  2928. "push %%"R"ax \n\t"
  2929. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2930. __ex(SVM_VMLOAD) "\n\t"
  2931. __ex(SVM_VMRUN) "\n\t"
  2932. __ex(SVM_VMSAVE) "\n\t"
  2933. "pop %%"R"ax \n\t"
  2934. /* Save guest registers, load host registers */
  2935. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2936. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2937. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2938. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2939. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2940. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2941. #ifdef CONFIG_X86_64
  2942. "mov %%r8, %c[r8](%[svm]) \n\t"
  2943. "mov %%r9, %c[r9](%[svm]) \n\t"
  2944. "mov %%r10, %c[r10](%[svm]) \n\t"
  2945. "mov %%r11, %c[r11](%[svm]) \n\t"
  2946. "mov %%r12, %c[r12](%[svm]) \n\t"
  2947. "mov %%r13, %c[r13](%[svm]) \n\t"
  2948. "mov %%r14, %c[r14](%[svm]) \n\t"
  2949. "mov %%r15, %c[r15](%[svm]) \n\t"
  2950. #endif
  2951. "pop %%"R"bp"
  2952. :
  2953. : [svm]"a"(svm),
  2954. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2955. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2956. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2957. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2958. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2959. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2960. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2961. #ifdef CONFIG_X86_64
  2962. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2963. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2964. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2965. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2966. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2967. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2968. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2969. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2970. #endif
  2971. : "cc", "memory"
  2972. , R"bx", R"cx", R"dx", R"si", R"di"
  2973. #ifdef CONFIG_X86_64
  2974. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2975. #endif
  2976. );
  2977. #ifdef CONFIG_X86_64
  2978. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  2979. #else
  2980. loadsegment(fs, svm->host.fs);
  2981. #endif
  2982. reload_tss(vcpu);
  2983. local_irq_disable();
  2984. stgi();
  2985. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2986. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2987. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2988. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2989. sync_cr8_to_lapic(vcpu);
  2990. svm->next_rip = 0;
  2991. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  2992. /* if exit due to PF check for async PF */
  2993. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  2994. svm->apf_reason = kvm_read_and_reset_pf_reason();
  2995. if (npt_enabled) {
  2996. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  2997. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  2998. }
  2999. /*
  3000. * We need to handle MC intercepts here before the vcpu has a chance to
  3001. * change the physical cpu
  3002. */
  3003. if (unlikely(svm->vmcb->control.exit_code ==
  3004. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  3005. svm_handle_mce(svm);
  3006. mark_all_clean(svm->vmcb);
  3007. }
  3008. #undef R
  3009. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  3010. {
  3011. struct vcpu_svm *svm = to_svm(vcpu);
  3012. svm->vmcb->save.cr3 = root;
  3013. mark_dirty(svm->vmcb, VMCB_CR);
  3014. svm_flush_tlb(vcpu);
  3015. }
  3016. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  3017. {
  3018. struct vcpu_svm *svm = to_svm(vcpu);
  3019. svm->vmcb->control.nested_cr3 = root;
  3020. mark_dirty(svm->vmcb, VMCB_NPT);
  3021. /* Also sync guest cr3 here in case we live migrate */
  3022. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  3023. mark_dirty(svm->vmcb, VMCB_CR);
  3024. svm_flush_tlb(vcpu);
  3025. }
  3026. static int is_disabled(void)
  3027. {
  3028. u64 vm_cr;
  3029. rdmsrl(MSR_VM_CR, vm_cr);
  3030. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  3031. return 1;
  3032. return 0;
  3033. }
  3034. static void
  3035. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  3036. {
  3037. /*
  3038. * Patch in the VMMCALL instruction:
  3039. */
  3040. hypercall[0] = 0x0f;
  3041. hypercall[1] = 0x01;
  3042. hypercall[2] = 0xd9;
  3043. }
  3044. static void svm_check_processor_compat(void *rtn)
  3045. {
  3046. *(int *)rtn = 0;
  3047. }
  3048. static bool svm_cpu_has_accelerated_tpr(void)
  3049. {
  3050. return false;
  3051. }
  3052. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  3053. {
  3054. return 0;
  3055. }
  3056. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  3057. {
  3058. }
  3059. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  3060. {
  3061. switch (func) {
  3062. case 0x80000001:
  3063. if (nested)
  3064. entry->ecx |= (1 << 2); /* Set SVM bit */
  3065. break;
  3066. case 0x8000000A:
  3067. entry->eax = 1; /* SVM revision 1 */
  3068. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  3069. ASID emulation to nested SVM */
  3070. entry->ecx = 0; /* Reserved */
  3071. entry->edx = 0; /* Per default do not support any
  3072. additional features */
  3073. /* Support next_rip if host supports it */
  3074. if (boot_cpu_has(X86_FEATURE_NRIPS))
  3075. entry->edx |= SVM_FEATURE_NRIP;
  3076. /* Support NPT for the guest if enabled */
  3077. if (npt_enabled)
  3078. entry->edx |= SVM_FEATURE_NPT;
  3079. break;
  3080. }
  3081. }
  3082. static const struct trace_print_flags svm_exit_reasons_str[] = {
  3083. { SVM_EXIT_READ_CR0, "read_cr0" },
  3084. { SVM_EXIT_READ_CR3, "read_cr3" },
  3085. { SVM_EXIT_READ_CR4, "read_cr4" },
  3086. { SVM_EXIT_READ_CR8, "read_cr8" },
  3087. { SVM_EXIT_WRITE_CR0, "write_cr0" },
  3088. { SVM_EXIT_WRITE_CR3, "write_cr3" },
  3089. { SVM_EXIT_WRITE_CR4, "write_cr4" },
  3090. { SVM_EXIT_WRITE_CR8, "write_cr8" },
  3091. { SVM_EXIT_READ_DR0, "read_dr0" },
  3092. { SVM_EXIT_READ_DR1, "read_dr1" },
  3093. { SVM_EXIT_READ_DR2, "read_dr2" },
  3094. { SVM_EXIT_READ_DR3, "read_dr3" },
  3095. { SVM_EXIT_WRITE_DR0, "write_dr0" },
  3096. { SVM_EXIT_WRITE_DR1, "write_dr1" },
  3097. { SVM_EXIT_WRITE_DR2, "write_dr2" },
  3098. { SVM_EXIT_WRITE_DR3, "write_dr3" },
  3099. { SVM_EXIT_WRITE_DR5, "write_dr5" },
  3100. { SVM_EXIT_WRITE_DR7, "write_dr7" },
  3101. { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
  3102. { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
  3103. { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
  3104. { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
  3105. { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
  3106. { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
  3107. { SVM_EXIT_INTR, "interrupt" },
  3108. { SVM_EXIT_NMI, "nmi" },
  3109. { SVM_EXIT_SMI, "smi" },
  3110. { SVM_EXIT_INIT, "init" },
  3111. { SVM_EXIT_VINTR, "vintr" },
  3112. { SVM_EXIT_CPUID, "cpuid" },
  3113. { SVM_EXIT_INVD, "invd" },
  3114. { SVM_EXIT_HLT, "hlt" },
  3115. { SVM_EXIT_INVLPG, "invlpg" },
  3116. { SVM_EXIT_INVLPGA, "invlpga" },
  3117. { SVM_EXIT_IOIO, "io" },
  3118. { SVM_EXIT_MSR, "msr" },
  3119. { SVM_EXIT_TASK_SWITCH, "task_switch" },
  3120. { SVM_EXIT_SHUTDOWN, "shutdown" },
  3121. { SVM_EXIT_VMRUN, "vmrun" },
  3122. { SVM_EXIT_VMMCALL, "hypercall" },
  3123. { SVM_EXIT_VMLOAD, "vmload" },
  3124. { SVM_EXIT_VMSAVE, "vmsave" },
  3125. { SVM_EXIT_STGI, "stgi" },
  3126. { SVM_EXIT_CLGI, "clgi" },
  3127. { SVM_EXIT_SKINIT, "skinit" },
  3128. { SVM_EXIT_WBINVD, "wbinvd" },
  3129. { SVM_EXIT_MONITOR, "monitor" },
  3130. { SVM_EXIT_MWAIT, "mwait" },
  3131. { SVM_EXIT_XSETBV, "xsetbv" },
  3132. { SVM_EXIT_NPF, "npf" },
  3133. { -1, NULL }
  3134. };
  3135. static int svm_get_lpage_level(void)
  3136. {
  3137. return PT_PDPE_LEVEL;
  3138. }
  3139. static bool svm_rdtscp_supported(void)
  3140. {
  3141. return false;
  3142. }
  3143. static bool svm_has_wbinvd_exit(void)
  3144. {
  3145. return true;
  3146. }
  3147. static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
  3148. {
  3149. struct vcpu_svm *svm = to_svm(vcpu);
  3150. set_exception_intercept(svm, NM_VECTOR);
  3151. update_cr0_intercept(svm);
  3152. }
  3153. static struct kvm_x86_ops svm_x86_ops = {
  3154. .cpu_has_kvm_support = has_svm,
  3155. .disabled_by_bios = is_disabled,
  3156. .hardware_setup = svm_hardware_setup,
  3157. .hardware_unsetup = svm_hardware_unsetup,
  3158. .check_processor_compatibility = svm_check_processor_compat,
  3159. .hardware_enable = svm_hardware_enable,
  3160. .hardware_disable = svm_hardware_disable,
  3161. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  3162. .vcpu_create = svm_create_vcpu,
  3163. .vcpu_free = svm_free_vcpu,
  3164. .vcpu_reset = svm_vcpu_reset,
  3165. .prepare_guest_switch = svm_prepare_guest_switch,
  3166. .vcpu_load = svm_vcpu_load,
  3167. .vcpu_put = svm_vcpu_put,
  3168. .set_guest_debug = svm_guest_debug,
  3169. .get_msr = svm_get_msr,
  3170. .set_msr = svm_set_msr,
  3171. .get_segment_base = svm_get_segment_base,
  3172. .get_segment = svm_get_segment,
  3173. .set_segment = svm_set_segment,
  3174. .get_cpl = svm_get_cpl,
  3175. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  3176. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  3177. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  3178. .set_cr0 = svm_set_cr0,
  3179. .set_cr3 = svm_set_cr3,
  3180. .set_cr4 = svm_set_cr4,
  3181. .set_efer = svm_set_efer,
  3182. .get_idt = svm_get_idt,
  3183. .set_idt = svm_set_idt,
  3184. .get_gdt = svm_get_gdt,
  3185. .set_gdt = svm_set_gdt,
  3186. .set_dr7 = svm_set_dr7,
  3187. .cache_reg = svm_cache_reg,
  3188. .get_rflags = svm_get_rflags,
  3189. .set_rflags = svm_set_rflags,
  3190. .fpu_activate = svm_fpu_activate,
  3191. .fpu_deactivate = svm_fpu_deactivate,
  3192. .tlb_flush = svm_flush_tlb,
  3193. .run = svm_vcpu_run,
  3194. .handle_exit = handle_exit,
  3195. .skip_emulated_instruction = skip_emulated_instruction,
  3196. .set_interrupt_shadow = svm_set_interrupt_shadow,
  3197. .get_interrupt_shadow = svm_get_interrupt_shadow,
  3198. .patch_hypercall = svm_patch_hypercall,
  3199. .set_irq = svm_set_irq,
  3200. .set_nmi = svm_inject_nmi,
  3201. .queue_exception = svm_queue_exception,
  3202. .cancel_injection = svm_cancel_injection,
  3203. .interrupt_allowed = svm_interrupt_allowed,
  3204. .nmi_allowed = svm_nmi_allowed,
  3205. .get_nmi_mask = svm_get_nmi_mask,
  3206. .set_nmi_mask = svm_set_nmi_mask,
  3207. .enable_nmi_window = enable_nmi_window,
  3208. .enable_irq_window = enable_irq_window,
  3209. .update_cr8_intercept = update_cr8_intercept,
  3210. .set_tss_addr = svm_set_tss_addr,
  3211. .get_tdp_level = get_npt_level,
  3212. .get_mt_mask = svm_get_mt_mask,
  3213. .get_exit_info = svm_get_exit_info,
  3214. .exit_reasons_str = svm_exit_reasons_str,
  3215. .get_lpage_level = svm_get_lpage_level,
  3216. .cpuid_update = svm_cpuid_update,
  3217. .rdtscp_supported = svm_rdtscp_supported,
  3218. .set_supported_cpuid = svm_set_supported_cpuid,
  3219. .has_wbinvd_exit = svm_has_wbinvd_exit,
  3220. .write_tsc_offset = svm_write_tsc_offset,
  3221. .adjust_tsc_offset = svm_adjust_tsc_offset,
  3222. .set_tdp_cr3 = set_tdp_cr3,
  3223. };
  3224. static int __init svm_init(void)
  3225. {
  3226. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  3227. __alignof__(struct vcpu_svm), THIS_MODULE);
  3228. }
  3229. static void __exit svm_exit(void)
  3230. {
  3231. kvm_exit();
  3232. }
  3233. module_init(svm_init)
  3234. module_exit(svm_exit)