svm.c 113 KB

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