svm.c 107 KB

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