svm.c 117 KB

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