svm.c 111 KB

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