svm.c 112 KB

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