svm.c 107 KB

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