svm.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172
  1. #define pr_fmt(fmt) "SVM: " fmt
  2. #include <linux/kvm_host.h>
  3. #include "irq.h"
  4. #include "mmu.h"
  5. #include "kvm_cache_regs.h"
  6. #include "x86.h"
  7. #include "cpuid.h"
  8. #include "pmu.h"
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/kernel.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/highmem.h>
  14. #include <linux/amd-iommu.h>
  15. #include <linux/sched.h>
  16. #include <linux/trace_events.h>
  17. #include <linux/slab.h>
  18. #include <linux/hashtable.h>
  19. #include <linux/objtool.h>
  20. #include <linux/psp-sev.h>
  21. #include <linux/file.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/swap.h>
  24. #include <linux/rwsem.h>
  25. #include <linux/cc_platform.h>
  26. #include <asm/apic.h>
  27. #include <asm/perf_event.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/desc.h>
  30. #include <asm/debugreg.h>
  31. #include <asm/kvm_para.h>
  32. #include <asm/irq_remapping.h>
  33. #include <asm/spec-ctrl.h>
  34. #include <asm/cpu_device_id.h>
  35. #include <asm/traps.h>
  36. #include <asm/fpu/api.h>
  37. #include <asm/virtext.h>
  38. #include "trace.h"
  39. #include "svm.h"
  40. #include "svm_ops.h"
  41. #include "kvm_onhyperv.h"
  42. #include "svm_onhyperv.h"
  43. MODULE_AUTHOR("Qumranet");
  44. MODULE_LICENSE("GPL");
  45. #ifdef MODULE
  46. static const struct x86_cpu_id svm_cpu_id[] = {
  47. X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
  48. {}
  49. };
  50. MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
  51. #endif
  52. #define SEG_TYPE_LDT 2
  53. #define SEG_TYPE_BUSY_TSS16 3
  54. static bool erratum_383_found __read_mostly;
  55. u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  56. /*
  57. * Set osvw_len to higher value when updated Revision Guides
  58. * are published and we know what the new status bits are
  59. */
  60. static uint64_t osvw_len = 4, osvw_status;
  61. static DEFINE_PER_CPU(u64, current_tsc_ratio);
  62. #define X2APIC_MSR(x) (APIC_BASE_MSR + (x >> 4))
  63. static const struct svm_direct_access_msrs {
  64. u32 index; /* Index of the MSR */
  65. bool always; /* True if intercept is initially cleared */
  66. } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
  67. { .index = MSR_STAR, .always = true },
  68. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  69. { .index = MSR_IA32_SYSENTER_EIP, .always = false },
  70. { .index = MSR_IA32_SYSENTER_ESP, .always = false },
  71. #ifdef CONFIG_X86_64
  72. { .index = MSR_GS_BASE, .always = true },
  73. { .index = MSR_FS_BASE, .always = true },
  74. { .index = MSR_KERNEL_GS_BASE, .always = true },
  75. { .index = MSR_LSTAR, .always = true },
  76. { .index = MSR_CSTAR, .always = true },
  77. { .index = MSR_SYSCALL_MASK, .always = true },
  78. #endif
  79. { .index = MSR_IA32_SPEC_CTRL, .always = false },
  80. { .index = MSR_IA32_PRED_CMD, .always = false },
  81. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  82. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  83. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  84. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  85. { .index = MSR_EFER, .always = false },
  86. { .index = MSR_IA32_CR_PAT, .always = false },
  87. { .index = MSR_AMD64_SEV_ES_GHCB, .always = true },
  88. { .index = MSR_TSC_AUX, .always = false },
  89. { .index = X2APIC_MSR(APIC_ID), .always = false },
  90. { .index = X2APIC_MSR(APIC_LVR), .always = false },
  91. { .index = X2APIC_MSR(APIC_TASKPRI), .always = false },
  92. { .index = X2APIC_MSR(APIC_ARBPRI), .always = false },
  93. { .index = X2APIC_MSR(APIC_PROCPRI), .always = false },
  94. { .index = X2APIC_MSR(APIC_EOI), .always = false },
  95. { .index = X2APIC_MSR(APIC_RRR), .always = false },
  96. { .index = X2APIC_MSR(APIC_LDR), .always = false },
  97. { .index = X2APIC_MSR(APIC_DFR), .always = false },
  98. { .index = X2APIC_MSR(APIC_SPIV), .always = false },
  99. { .index = X2APIC_MSR(APIC_ISR), .always = false },
  100. { .index = X2APIC_MSR(APIC_TMR), .always = false },
  101. { .index = X2APIC_MSR(APIC_IRR), .always = false },
  102. { .index = X2APIC_MSR(APIC_ESR), .always = false },
  103. { .index = X2APIC_MSR(APIC_ICR), .always = false },
  104. { .index = X2APIC_MSR(APIC_ICR2), .always = false },
  105. /*
  106. * Note:
  107. * AMD does not virtualize APIC TSC-deadline timer mode, but it is
  108. * emulated by KVM. When setting APIC LVTT (0x832) register bit 18,
  109. * the AVIC hardware would generate GP fault. Therefore, always
  110. * intercept the MSR 0x832, and do not setup direct_access_msr.
  111. */
  112. { .index = X2APIC_MSR(APIC_LVTTHMR), .always = false },
  113. { .index = X2APIC_MSR(APIC_LVTPC), .always = false },
  114. { .index = X2APIC_MSR(APIC_LVT0), .always = false },
  115. { .index = X2APIC_MSR(APIC_LVT1), .always = false },
  116. { .index = X2APIC_MSR(APIC_LVTERR), .always = false },
  117. { .index = X2APIC_MSR(APIC_TMICT), .always = false },
  118. { .index = X2APIC_MSR(APIC_TMCCT), .always = false },
  119. { .index = X2APIC_MSR(APIC_TDCR), .always = false },
  120. { .index = MSR_INVALID, .always = false },
  121. };
  122. /*
  123. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  124. * pause_filter_count: On processors that support Pause filtering(indicated
  125. * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
  126. * count value. On VMRUN this value is loaded into an internal counter.
  127. * Each time a pause instruction is executed, this counter is decremented
  128. * until it reaches zero at which time a #VMEXIT is generated if pause
  129. * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
  130. * Intercept Filtering for more details.
  131. * This also indicate if ple logic enabled.
  132. *
  133. * pause_filter_thresh: In addition, some processor families support advanced
  134. * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
  135. * the amount of time a guest is allowed to execute in a pause loop.
  136. * In this mode, a 16-bit pause filter threshold field is added in the
  137. * VMCB. The threshold value is a cycle count that is used to reset the
  138. * pause counter. As with simple pause filtering, VMRUN loads the pause
  139. * count value from VMCB into an internal counter. Then, on each pause
  140. * instruction the hardware checks the elapsed number of cycles since
  141. * the most recent pause instruction against the pause filter threshold.
  142. * If the elapsed cycle count is greater than the pause filter threshold,
  143. * then the internal pause count is reloaded from the VMCB and execution
  144. * continues. If the elapsed cycle count is less than the pause filter
  145. * threshold, then the internal pause count is decremented. If the count
  146. * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
  147. * triggered. If advanced pause filtering is supported and pause filter
  148. * threshold field is set to zero, the filter will operate in the simpler,
  149. * count only mode.
  150. */
  151. static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
  152. module_param(pause_filter_thresh, ushort, 0444);
  153. static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
  154. module_param(pause_filter_count, ushort, 0444);
  155. /* Default doubles per-vcpu window every exit. */
  156. static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
  157. module_param(pause_filter_count_grow, ushort, 0444);
  158. /* Default resets per-vcpu window every exit to pause_filter_count. */
  159. static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
  160. module_param(pause_filter_count_shrink, ushort, 0444);
  161. /* Default is to compute the maximum so we can never overflow. */
  162. static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
  163. module_param(pause_filter_count_max, ushort, 0444);
  164. /*
  165. * Use nested page tables by default. Note, NPT may get forced off by
  166. * svm_hardware_setup() if it's unsupported by hardware or the host kernel.
  167. */
  168. bool npt_enabled = true;
  169. module_param_named(npt, npt_enabled, bool, 0444);
  170. /* allow nested virtualization in KVM/SVM */
  171. static int nested = true;
  172. module_param(nested, int, S_IRUGO);
  173. /* enable/disable Next RIP Save */
  174. static int nrips = true;
  175. module_param(nrips, int, 0444);
  176. /* enable/disable Virtual VMLOAD VMSAVE */
  177. static int vls = true;
  178. module_param(vls, int, 0444);
  179. /* enable/disable Virtual GIF */
  180. int vgif = true;
  181. module_param(vgif, int, 0444);
  182. /* enable/disable LBR virtualization */
  183. static int lbrv = true;
  184. module_param(lbrv, int, 0444);
  185. static int tsc_scaling = true;
  186. module_param(tsc_scaling, int, 0444);
  187. /*
  188. * enable / disable AVIC. Because the defaults differ for APICv
  189. * support between VMX and SVM we cannot use module_param_named.
  190. */
  191. static bool avic;
  192. module_param(avic, bool, 0444);
  193. bool __read_mostly dump_invalid_vmcb;
  194. module_param(dump_invalid_vmcb, bool, 0644);
  195. bool intercept_smi = true;
  196. module_param(intercept_smi, bool, 0444);
  197. static bool svm_gp_erratum_intercept = true;
  198. static u8 rsm_ins_bytes[] = "\x0f\xaa";
  199. static unsigned long iopm_base;
  200. struct kvm_ldttss_desc {
  201. u16 limit0;
  202. u16 base0;
  203. unsigned base1:8, type:5, dpl:2, p:1;
  204. unsigned limit1:4, zero0:3, g:1, base2:8;
  205. u32 base3;
  206. u32 zero1;
  207. } __attribute__((packed));
  208. DEFINE_PER_CPU(struct svm_cpu_data, svm_data);
  209. /*
  210. * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
  211. * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
  212. *
  213. * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to
  214. * defer the restoration of TSC_AUX until the CPU returns to userspace.
  215. */
  216. static int tsc_aux_uret_slot __read_mostly = -1;
  217. static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  218. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  219. #define MSRS_RANGE_SIZE 2048
  220. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  221. u32 svm_msrpm_offset(u32 msr)
  222. {
  223. u32 offset;
  224. int i;
  225. for (i = 0; i < NUM_MSR_MAPS; i++) {
  226. if (msr < msrpm_ranges[i] ||
  227. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  228. continue;
  229. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  230. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  231. /* Now we have the u8 offset - but need the u32 offset */
  232. return offset / 4;
  233. }
  234. /* MSR not in any range */
  235. return MSR_INVALID;
  236. }
  237. static void svm_flush_tlb_current(struct kvm_vcpu *vcpu);
  238. static int get_npt_level(void)
  239. {
  240. #ifdef CONFIG_X86_64
  241. return pgtable_l5_enabled() ? PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
  242. #else
  243. return PT32E_ROOT_LEVEL;
  244. #endif
  245. }
  246. int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  247. {
  248. struct vcpu_svm *svm = to_svm(vcpu);
  249. u64 old_efer = vcpu->arch.efer;
  250. vcpu->arch.efer = efer;
  251. if (!npt_enabled) {
  252. /* Shadow paging assumes NX to be available. */
  253. efer |= EFER_NX;
  254. if (!(efer & EFER_LMA))
  255. efer &= ~EFER_LME;
  256. }
  257. if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
  258. if (!(efer & EFER_SVME)) {
  259. svm_leave_nested(vcpu);
  260. svm_set_gif(svm, true);
  261. /* #GP intercept is still needed for vmware backdoor */
  262. if (!enable_vmware_backdoor)
  263. clr_exception_intercept(svm, GP_VECTOR);
  264. /*
  265. * Free the nested guest state, unless we are in SMM.
  266. * In this case we will return to the nested guest
  267. * as soon as we leave SMM.
  268. */
  269. if (!is_smm(vcpu))
  270. svm_free_nested(svm);
  271. } else {
  272. int ret = svm_allocate_nested(svm);
  273. if (ret) {
  274. vcpu->arch.efer = old_efer;
  275. return ret;
  276. }
  277. /*
  278. * Never intercept #GP for SEV guests, KVM can't
  279. * decrypt guest memory to workaround the erratum.
  280. */
  281. if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm))
  282. set_exception_intercept(svm, GP_VECTOR);
  283. }
  284. }
  285. svm->vmcb->save.efer = efer | EFER_SVME;
  286. vmcb_mark_dirty(svm->vmcb, VMCB_CR);
  287. return 0;
  288. }
  289. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  290. {
  291. struct vcpu_svm *svm = to_svm(vcpu);
  292. u32 ret = 0;
  293. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  294. ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  295. return ret;
  296. }
  297. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  298. {
  299. struct vcpu_svm *svm = to_svm(vcpu);
  300. if (mask == 0)
  301. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  302. else
  303. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  304. }
  305. static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
  306. void *insn, int insn_len);
  307. static int __svm_skip_emulated_instruction(struct kvm_vcpu *vcpu,
  308. bool commit_side_effects)
  309. {
  310. struct vcpu_svm *svm = to_svm(vcpu);
  311. unsigned long old_rflags;
  312. /*
  313. * SEV-ES does not expose the next RIP. The RIP update is controlled by
  314. * the type of exit and the #VC handler in the guest.
  315. */
  316. if (sev_es_guest(vcpu->kvm))
  317. goto done;
  318. if (nrips && svm->vmcb->control.next_rip != 0) {
  319. WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
  320. svm->next_rip = svm->vmcb->control.next_rip;
  321. }
  322. if (!svm->next_rip) {
  323. /*
  324. * FIXME: Drop this when kvm_emulate_instruction() does the
  325. * right thing and treats "can't emulate" as outright failure
  326. * for EMULTYPE_SKIP.
  327. */
  328. if (!svm_can_emulate_instruction(vcpu, EMULTYPE_SKIP, NULL, 0))
  329. return 0;
  330. if (unlikely(!commit_side_effects))
  331. old_rflags = svm->vmcb->save.rflags;
  332. if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
  333. return 0;
  334. if (unlikely(!commit_side_effects))
  335. svm->vmcb->save.rflags = old_rflags;
  336. } else {
  337. kvm_rip_write(vcpu, svm->next_rip);
  338. }
  339. done:
  340. if (likely(commit_side_effects))
  341. svm_set_interrupt_shadow(vcpu, 0);
  342. return 1;
  343. }
  344. static int svm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
  345. {
  346. return __svm_skip_emulated_instruction(vcpu, true);
  347. }
  348. static int svm_update_soft_interrupt_rip(struct kvm_vcpu *vcpu)
  349. {
  350. unsigned long rip, old_rip = kvm_rip_read(vcpu);
  351. struct vcpu_svm *svm = to_svm(vcpu);
  352. /*
  353. * Due to architectural shortcomings, the CPU doesn't always provide
  354. * NextRIP, e.g. if KVM intercepted an exception that occurred while
  355. * the CPU was vectoring an INTO/INT3 in the guest. Temporarily skip
  356. * the instruction even if NextRIP is supported to acquire the next
  357. * RIP so that it can be shoved into the NextRIP field, otherwise
  358. * hardware will fail to advance guest RIP during event injection.
  359. * Drop the exception/interrupt if emulation fails and effectively
  360. * retry the instruction, it's the least awful option. If NRIPS is
  361. * in use, the skip must not commit any side effects such as clearing
  362. * the interrupt shadow or RFLAGS.RF.
  363. */
  364. if (!__svm_skip_emulated_instruction(vcpu, !nrips))
  365. return -EIO;
  366. rip = kvm_rip_read(vcpu);
  367. /*
  368. * Save the injection information, even when using next_rip, as the
  369. * VMCB's next_rip will be lost (cleared on VM-Exit) if the injection
  370. * doesn't complete due to a VM-Exit occurring while the CPU is
  371. * vectoring the event. Decoding the instruction isn't guaranteed to
  372. * work as there may be no backing instruction, e.g. if the event is
  373. * being injected by L1 for L2, or if the guest is patching INT3 into
  374. * a different instruction.
  375. */
  376. svm->soft_int_injected = true;
  377. svm->soft_int_csbase = svm->vmcb->save.cs.base;
  378. svm->soft_int_old_rip = old_rip;
  379. svm->soft_int_next_rip = rip;
  380. if (nrips)
  381. kvm_rip_write(vcpu, old_rip);
  382. if (static_cpu_has(X86_FEATURE_NRIPS))
  383. svm->vmcb->control.next_rip = rip;
  384. return 0;
  385. }
  386. static void svm_inject_exception(struct kvm_vcpu *vcpu)
  387. {
  388. struct kvm_queued_exception *ex = &vcpu->arch.exception;
  389. struct vcpu_svm *svm = to_svm(vcpu);
  390. kvm_deliver_exception_payload(vcpu, ex);
  391. if (kvm_exception_is_soft(ex->vector) &&
  392. svm_update_soft_interrupt_rip(vcpu))
  393. return;
  394. svm->vmcb->control.event_inj = ex->vector
  395. | SVM_EVTINJ_VALID
  396. | (ex->has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  397. | SVM_EVTINJ_TYPE_EXEPT;
  398. svm->vmcb->control.event_inj_err = ex->error_code;
  399. }
  400. static void svm_init_erratum_383(void)
  401. {
  402. u32 low, high;
  403. int err;
  404. u64 val;
  405. if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
  406. return;
  407. /* Use _safe variants to not break nested virtualization */
  408. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  409. if (err)
  410. return;
  411. val |= (1ULL << 47);
  412. low = lower_32_bits(val);
  413. high = upper_32_bits(val);
  414. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  415. erratum_383_found = true;
  416. }
  417. static void svm_init_osvw(struct kvm_vcpu *vcpu)
  418. {
  419. /*
  420. * Guests should see errata 400 and 415 as fixed (assuming that
  421. * HLT and IO instructions are intercepted).
  422. */
  423. vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
  424. vcpu->arch.osvw.status = osvw_status & ~(6ULL);
  425. /*
  426. * By increasing VCPU's osvw.length to 3 we are telling the guest that
  427. * all osvw.status bits inside that length, including bit 0 (which is
  428. * reserved for erratum 298), are valid. However, if host processor's
  429. * osvw_len is 0 then osvw_status[0] carries no information. We need to
  430. * be conservative here and therefore we tell the guest that erratum 298
  431. * is present (because we really don't know).
  432. */
  433. if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
  434. vcpu->arch.osvw.status |= 1;
  435. }
  436. static int has_svm(void)
  437. {
  438. const char *msg;
  439. if (!cpu_has_svm(&msg)) {
  440. printk(KERN_INFO "has_svm: %s\n", msg);
  441. return 0;
  442. }
  443. if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
  444. pr_info("KVM is unsupported when running as an SEV guest\n");
  445. return 0;
  446. }
  447. return 1;
  448. }
  449. void __svm_write_tsc_multiplier(u64 multiplier)
  450. {
  451. preempt_disable();
  452. if (multiplier == __this_cpu_read(current_tsc_ratio))
  453. goto out;
  454. wrmsrl(MSR_AMD64_TSC_RATIO, multiplier);
  455. __this_cpu_write(current_tsc_ratio, multiplier);
  456. out:
  457. preempt_enable();
  458. }
  459. static void svm_hardware_disable(void)
  460. {
  461. /* Make sure we clean up behind us */
  462. if (tsc_scaling)
  463. __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT);
  464. cpu_svm_disable();
  465. amd_pmu_disable_virt();
  466. }
  467. static int svm_hardware_enable(void)
  468. {
  469. struct svm_cpu_data *sd;
  470. uint64_t efer;
  471. struct desc_struct *gdt;
  472. int me = raw_smp_processor_id();
  473. rdmsrl(MSR_EFER, efer);
  474. if (efer & EFER_SVME)
  475. return -EBUSY;
  476. if (!has_svm()) {
  477. pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
  478. return -EINVAL;
  479. }
  480. sd = per_cpu_ptr(&svm_data, me);
  481. sd->asid_generation = 1;
  482. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  483. sd->next_asid = sd->max_asid + 1;
  484. sd->min_asid = max_sev_asid + 1;
  485. gdt = get_current_gdt_rw();
  486. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  487. wrmsrl(MSR_EFER, efer | EFER_SVME);
  488. wrmsrl(MSR_VM_HSAVE_PA, sd->save_area_pa);
  489. if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  490. /*
  491. * Set the default value, even if we don't use TSC scaling
  492. * to avoid having stale value in the msr
  493. */
  494. __svm_write_tsc_multiplier(SVM_TSC_RATIO_DEFAULT);
  495. }
  496. /*
  497. * Get OSVW bits.
  498. *
  499. * Note that it is possible to have a system with mixed processor
  500. * revisions and therefore different OSVW bits. If bits are not the same
  501. * on different processors then choose the worst case (i.e. if erratum
  502. * is present on one processor and not on another then assume that the
  503. * erratum is present everywhere).
  504. */
  505. if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
  506. uint64_t len, status = 0;
  507. int err;
  508. len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
  509. if (!err)
  510. status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
  511. &err);
  512. if (err)
  513. osvw_status = osvw_len = 0;
  514. else {
  515. if (len < osvw_len)
  516. osvw_len = len;
  517. osvw_status |= status;
  518. osvw_status &= (1ULL << osvw_len) - 1;
  519. }
  520. } else
  521. osvw_status = osvw_len = 0;
  522. svm_init_erratum_383();
  523. amd_pmu_enable_virt();
  524. return 0;
  525. }
  526. static void svm_cpu_uninit(int cpu)
  527. {
  528. struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu);
  529. if (!sd->save_area)
  530. return;
  531. kfree(sd->sev_vmcbs);
  532. __free_page(sd->save_area);
  533. sd->save_area_pa = 0;
  534. sd->save_area = NULL;
  535. }
  536. static int svm_cpu_init(int cpu)
  537. {
  538. struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu);
  539. int ret = -ENOMEM;
  540. memset(sd, 0, sizeof(struct svm_cpu_data));
  541. sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO);
  542. if (!sd->save_area)
  543. return ret;
  544. ret = sev_cpu_init(sd);
  545. if (ret)
  546. goto free_save_area;
  547. sd->save_area_pa = __sme_page_pa(sd->save_area);
  548. return 0;
  549. free_save_area:
  550. __free_page(sd->save_area);
  551. sd->save_area = NULL;
  552. return ret;
  553. }
  554. static int direct_access_msr_slot(u32 msr)
  555. {
  556. u32 i;
  557. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  558. if (direct_access_msrs[i].index == msr)
  559. return i;
  560. return -ENOENT;
  561. }
  562. static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
  563. int write)
  564. {
  565. struct vcpu_svm *svm = to_svm(vcpu);
  566. int slot = direct_access_msr_slot(msr);
  567. if (slot == -ENOENT)
  568. return;
  569. /* Set the shadow bitmaps to the desired intercept states */
  570. if (read)
  571. set_bit(slot, svm->shadow_msr_intercept.read);
  572. else
  573. clear_bit(slot, svm->shadow_msr_intercept.read);
  574. if (write)
  575. set_bit(slot, svm->shadow_msr_intercept.write);
  576. else
  577. clear_bit(slot, svm->shadow_msr_intercept.write);
  578. }
  579. static bool valid_msr_intercept(u32 index)
  580. {
  581. return direct_access_msr_slot(index) != -ENOENT;
  582. }
  583. static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
  584. {
  585. u8 bit_write;
  586. unsigned long tmp;
  587. u32 offset;
  588. u32 *msrpm;
  589. /*
  590. * For non-nested case:
  591. * If the L01 MSR bitmap does not intercept the MSR, then we need to
  592. * save it.
  593. *
  594. * For nested case:
  595. * If the L02 MSR bitmap does not intercept the MSR, then we need to
  596. * save it.
  597. */
  598. msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
  599. to_svm(vcpu)->msrpm;
  600. offset = svm_msrpm_offset(msr);
  601. bit_write = 2 * (msr & 0x0f) + 1;
  602. tmp = msrpm[offset];
  603. BUG_ON(offset == MSR_INVALID);
  604. return !!test_bit(bit_write, &tmp);
  605. }
  606. static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
  607. u32 msr, int read, int write)
  608. {
  609. struct vcpu_svm *svm = to_svm(vcpu);
  610. u8 bit_read, bit_write;
  611. unsigned long tmp;
  612. u32 offset;
  613. /*
  614. * If this warning triggers extend the direct_access_msrs list at the
  615. * beginning of the file
  616. */
  617. WARN_ON(!valid_msr_intercept(msr));
  618. /* Enforce non allowed MSRs to trap */
  619. if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
  620. read = 0;
  621. if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
  622. write = 0;
  623. offset = svm_msrpm_offset(msr);
  624. bit_read = 2 * (msr & 0x0f);
  625. bit_write = 2 * (msr & 0x0f) + 1;
  626. tmp = msrpm[offset];
  627. BUG_ON(offset == MSR_INVALID);
  628. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  629. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  630. msrpm[offset] = tmp;
  631. svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
  632. svm->nested.force_msr_bitmap_recalc = true;
  633. }
  634. void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
  635. int read, int write)
  636. {
  637. set_shadow_msr_intercept(vcpu, msr, read, write);
  638. set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
  639. }
  640. u32 *svm_vcpu_alloc_msrpm(void)
  641. {
  642. unsigned int order = get_order(MSRPM_SIZE);
  643. struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
  644. u32 *msrpm;
  645. if (!pages)
  646. return NULL;
  647. msrpm = page_address(pages);
  648. memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
  649. return msrpm;
  650. }
  651. void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
  652. {
  653. int i;
  654. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  655. if (!direct_access_msrs[i].always)
  656. continue;
  657. set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
  658. }
  659. }
  660. void svm_set_x2apic_msr_interception(struct vcpu_svm *svm, bool intercept)
  661. {
  662. int i;
  663. if (intercept == svm->x2avic_msrs_intercepted)
  664. return;
  665. if (avic_mode != AVIC_MODE_X2)
  666. return;
  667. for (i = 0; i < MAX_DIRECT_ACCESS_MSRS; i++) {
  668. int index = direct_access_msrs[i].index;
  669. if ((index < APIC_BASE_MSR) ||
  670. (index > APIC_BASE_MSR + 0xff))
  671. continue;
  672. set_msr_interception(&svm->vcpu, svm->msrpm, index,
  673. !intercept, !intercept);
  674. }
  675. svm->x2avic_msrs_intercepted = intercept;
  676. }
  677. void svm_vcpu_free_msrpm(u32 *msrpm)
  678. {
  679. __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
  680. }
  681. static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
  682. {
  683. struct vcpu_svm *svm = to_svm(vcpu);
  684. u32 i;
  685. /*
  686. * Set intercept permissions for all direct access MSRs again. They
  687. * will automatically get filtered through the MSR filter, so we are
  688. * back in sync after this.
  689. */
  690. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  691. u32 msr = direct_access_msrs[i].index;
  692. u32 read = test_bit(i, svm->shadow_msr_intercept.read);
  693. u32 write = test_bit(i, svm->shadow_msr_intercept.write);
  694. set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
  695. }
  696. }
  697. static void add_msr_offset(u32 offset)
  698. {
  699. int i;
  700. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  701. /* Offset already in list? */
  702. if (msrpm_offsets[i] == offset)
  703. return;
  704. /* Slot used by another offset? */
  705. if (msrpm_offsets[i] != MSR_INVALID)
  706. continue;
  707. /* Add offset to list */
  708. msrpm_offsets[i] = offset;
  709. return;
  710. }
  711. /*
  712. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  713. * increase MSRPM_OFFSETS in this case.
  714. */
  715. BUG();
  716. }
  717. static void init_msrpm_offsets(void)
  718. {
  719. int i;
  720. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  721. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  722. u32 offset;
  723. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  724. BUG_ON(offset == MSR_INVALID);
  725. add_msr_offset(offset);
  726. }
  727. }
  728. void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
  729. {
  730. to_vmcb->save.dbgctl = from_vmcb->save.dbgctl;
  731. to_vmcb->save.br_from = from_vmcb->save.br_from;
  732. to_vmcb->save.br_to = from_vmcb->save.br_to;
  733. to_vmcb->save.last_excp_from = from_vmcb->save.last_excp_from;
  734. to_vmcb->save.last_excp_to = from_vmcb->save.last_excp_to;
  735. vmcb_mark_dirty(to_vmcb, VMCB_LBR);
  736. }
  737. static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
  738. {
  739. struct vcpu_svm *svm = to_svm(vcpu);
  740. svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
  741. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  742. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  743. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  744. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  745. /* Move the LBR msrs to the vmcb02 so that the guest can see them. */
  746. if (is_guest_mode(vcpu))
  747. svm_copy_lbrs(svm->vmcb, svm->vmcb01.ptr);
  748. }
  749. static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
  750. {
  751. struct vcpu_svm *svm = to_svm(vcpu);
  752. svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
  753. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  754. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  755. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  756. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  757. /*
  758. * Move the LBR msrs back to the vmcb01 to avoid copying them
  759. * on nested guest entries.
  760. */
  761. if (is_guest_mode(vcpu))
  762. svm_copy_lbrs(svm->vmcb01.ptr, svm->vmcb);
  763. }
  764. static int svm_get_lbr_msr(struct vcpu_svm *svm, u32 index)
  765. {
  766. /*
  767. * If the LBR virtualization is disabled, the LBR msrs are always
  768. * kept in the vmcb01 to avoid copying them on nested guest entries.
  769. *
  770. * If nested, and the LBR virtualization is enabled/disabled, the msrs
  771. * are moved between the vmcb01 and vmcb02 as needed.
  772. */
  773. struct vmcb *vmcb =
  774. (svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) ?
  775. svm->vmcb : svm->vmcb01.ptr;
  776. switch (index) {
  777. case MSR_IA32_DEBUGCTLMSR:
  778. return vmcb->save.dbgctl;
  779. case MSR_IA32_LASTBRANCHFROMIP:
  780. return vmcb->save.br_from;
  781. case MSR_IA32_LASTBRANCHTOIP:
  782. return vmcb->save.br_to;
  783. case MSR_IA32_LASTINTFROMIP:
  784. return vmcb->save.last_excp_from;
  785. case MSR_IA32_LASTINTTOIP:
  786. return vmcb->save.last_excp_to;
  787. default:
  788. KVM_BUG(false, svm->vcpu.kvm,
  789. "%s: Unknown MSR 0x%x", __func__, index);
  790. return 0;
  791. }
  792. }
  793. void svm_update_lbrv(struct kvm_vcpu *vcpu)
  794. {
  795. struct vcpu_svm *svm = to_svm(vcpu);
  796. bool enable_lbrv = svm_get_lbr_msr(svm, MSR_IA32_DEBUGCTLMSR) &
  797. DEBUGCTLMSR_LBR;
  798. bool current_enable_lbrv = !!(svm->vmcb->control.virt_ext &
  799. LBR_CTL_ENABLE_MASK);
  800. if (unlikely(is_guest_mode(vcpu) && svm->lbrv_enabled))
  801. if (unlikely(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))
  802. enable_lbrv = true;
  803. if (enable_lbrv == current_enable_lbrv)
  804. return;
  805. if (enable_lbrv)
  806. svm_enable_lbrv(vcpu);
  807. else
  808. svm_disable_lbrv(vcpu);
  809. }
  810. void disable_nmi_singlestep(struct vcpu_svm *svm)
  811. {
  812. svm->nmi_singlestep = false;
  813. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
  814. /* Clear our flags if they were not set by the guest */
  815. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
  816. svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
  817. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
  818. svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
  819. }
  820. }
  821. static void grow_ple_window(struct kvm_vcpu *vcpu)
  822. {
  823. struct vcpu_svm *svm = to_svm(vcpu);
  824. struct vmcb_control_area *control = &svm->vmcb->control;
  825. int old = control->pause_filter_count;
  826. if (kvm_pause_in_guest(vcpu->kvm))
  827. return;
  828. control->pause_filter_count = __grow_ple_window(old,
  829. pause_filter_count,
  830. pause_filter_count_grow,
  831. pause_filter_count_max);
  832. if (control->pause_filter_count != old) {
  833. vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  834. trace_kvm_ple_window_update(vcpu->vcpu_id,
  835. control->pause_filter_count, old);
  836. }
  837. }
  838. static void shrink_ple_window(struct kvm_vcpu *vcpu)
  839. {
  840. struct vcpu_svm *svm = to_svm(vcpu);
  841. struct vmcb_control_area *control = &svm->vmcb->control;
  842. int old = control->pause_filter_count;
  843. if (kvm_pause_in_guest(vcpu->kvm))
  844. return;
  845. control->pause_filter_count =
  846. __shrink_ple_window(old,
  847. pause_filter_count,
  848. pause_filter_count_shrink,
  849. pause_filter_count);
  850. if (control->pause_filter_count != old) {
  851. vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  852. trace_kvm_ple_window_update(vcpu->vcpu_id,
  853. control->pause_filter_count, old);
  854. }
  855. }
  856. static void svm_hardware_unsetup(void)
  857. {
  858. int cpu;
  859. sev_hardware_unsetup();
  860. for_each_possible_cpu(cpu)
  861. svm_cpu_uninit(cpu);
  862. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
  863. get_order(IOPM_SIZE));
  864. iopm_base = 0;
  865. }
  866. static void init_seg(struct vmcb_seg *seg)
  867. {
  868. seg->selector = 0;
  869. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  870. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  871. seg->limit = 0xffff;
  872. seg->base = 0;
  873. }
  874. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  875. {
  876. seg->selector = 0;
  877. seg->attrib = SVM_SELECTOR_P_MASK | type;
  878. seg->limit = 0xffff;
  879. seg->base = 0;
  880. }
  881. static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
  882. {
  883. struct vcpu_svm *svm = to_svm(vcpu);
  884. return svm->nested.ctl.tsc_offset;
  885. }
  886. static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
  887. {
  888. struct vcpu_svm *svm = to_svm(vcpu);
  889. return svm->tsc_ratio_msr;
  890. }
  891. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  892. {
  893. struct vcpu_svm *svm = to_svm(vcpu);
  894. svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset;
  895. svm->vmcb->control.tsc_offset = offset;
  896. vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  897. }
  898. static void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier)
  899. {
  900. __svm_write_tsc_multiplier(multiplier);
  901. }
  902. /* Evaluate instruction intercepts that depend on guest CPUID features. */
  903. static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
  904. struct vcpu_svm *svm)
  905. {
  906. /*
  907. * Intercept INVPCID if shadow paging is enabled to sync/free shadow
  908. * roots, or if INVPCID is disabled in the guest to inject #UD.
  909. */
  910. if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
  911. if (!npt_enabled ||
  912. !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
  913. svm_set_intercept(svm, INTERCEPT_INVPCID);
  914. else
  915. svm_clr_intercept(svm, INTERCEPT_INVPCID);
  916. }
  917. if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
  918. if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
  919. svm_clr_intercept(svm, INTERCEPT_RDTSCP);
  920. else
  921. svm_set_intercept(svm, INTERCEPT_RDTSCP);
  922. }
  923. }
  924. static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu)
  925. {
  926. struct vcpu_svm *svm = to_svm(vcpu);
  927. if (guest_cpuid_is_intel(vcpu)) {
  928. /*
  929. * We must intercept SYSENTER_EIP and SYSENTER_ESP
  930. * accesses because the processor only stores 32 bits.
  931. * For the same reason we cannot use virtual VMLOAD/VMSAVE.
  932. */
  933. svm_set_intercept(svm, INTERCEPT_VMLOAD);
  934. svm_set_intercept(svm, INTERCEPT_VMSAVE);
  935. svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
  936. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
  937. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
  938. svm->v_vmload_vmsave_enabled = false;
  939. } else {
  940. /*
  941. * If hardware supports Virtual VMLOAD VMSAVE then enable it
  942. * in VMCB and clear intercepts to avoid #VMEXIT.
  943. */
  944. if (vls) {
  945. svm_clr_intercept(svm, INTERCEPT_VMLOAD);
  946. svm_clr_intercept(svm, INTERCEPT_VMSAVE);
  947. svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
  948. }
  949. /* No need to intercept these MSRs */
  950. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
  951. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
  952. }
  953. }
  954. static void init_vmcb(struct kvm_vcpu *vcpu)
  955. {
  956. struct vcpu_svm *svm = to_svm(vcpu);
  957. struct vmcb *vmcb = svm->vmcb01.ptr;
  958. struct vmcb_control_area *control = &vmcb->control;
  959. struct vmcb_save_area *save = &vmcb->save;
  960. svm_set_intercept(svm, INTERCEPT_CR0_READ);
  961. svm_set_intercept(svm, INTERCEPT_CR3_READ);
  962. svm_set_intercept(svm, INTERCEPT_CR4_READ);
  963. svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
  964. svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
  965. svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
  966. if (!kvm_vcpu_apicv_active(vcpu))
  967. svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
  968. set_dr_intercepts(svm);
  969. set_exception_intercept(svm, PF_VECTOR);
  970. set_exception_intercept(svm, UD_VECTOR);
  971. set_exception_intercept(svm, MC_VECTOR);
  972. set_exception_intercept(svm, AC_VECTOR);
  973. set_exception_intercept(svm, DB_VECTOR);
  974. /*
  975. * Guest access to VMware backdoor ports could legitimately
  976. * trigger #GP because of TSS I/O permission bitmap.
  977. * We intercept those #GP and allow access to them anyway
  978. * as VMware does. Don't intercept #GP for SEV guests as KVM can't
  979. * decrypt guest memory to decode the faulting instruction.
  980. */
  981. if (enable_vmware_backdoor && !sev_guest(vcpu->kvm))
  982. set_exception_intercept(svm, GP_VECTOR);
  983. svm_set_intercept(svm, INTERCEPT_INTR);
  984. svm_set_intercept(svm, INTERCEPT_NMI);
  985. if (intercept_smi)
  986. svm_set_intercept(svm, INTERCEPT_SMI);
  987. svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  988. svm_set_intercept(svm, INTERCEPT_RDPMC);
  989. svm_set_intercept(svm, INTERCEPT_CPUID);
  990. svm_set_intercept(svm, INTERCEPT_INVD);
  991. svm_set_intercept(svm, INTERCEPT_INVLPG);
  992. svm_set_intercept(svm, INTERCEPT_INVLPGA);
  993. svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
  994. svm_set_intercept(svm, INTERCEPT_MSR_PROT);
  995. svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
  996. svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
  997. svm_set_intercept(svm, INTERCEPT_VMRUN);
  998. svm_set_intercept(svm, INTERCEPT_VMMCALL);
  999. svm_set_intercept(svm, INTERCEPT_VMLOAD);
  1000. svm_set_intercept(svm, INTERCEPT_VMSAVE);
  1001. svm_set_intercept(svm, INTERCEPT_STGI);
  1002. svm_set_intercept(svm, INTERCEPT_CLGI);
  1003. svm_set_intercept(svm, INTERCEPT_SKINIT);
  1004. svm_set_intercept(svm, INTERCEPT_WBINVD);
  1005. svm_set_intercept(svm, INTERCEPT_XSETBV);
  1006. svm_set_intercept(svm, INTERCEPT_RDPRU);
  1007. svm_set_intercept(svm, INTERCEPT_RSM);
  1008. if (!kvm_mwait_in_guest(vcpu->kvm)) {
  1009. svm_set_intercept(svm, INTERCEPT_MONITOR);
  1010. svm_set_intercept(svm, INTERCEPT_MWAIT);
  1011. }
  1012. if (!kvm_hlt_in_guest(vcpu->kvm))
  1013. svm_set_intercept(svm, INTERCEPT_HLT);
  1014. control->iopm_base_pa = __sme_set(iopm_base);
  1015. control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
  1016. control->int_ctl = V_INTR_MASKING_MASK;
  1017. init_seg(&save->es);
  1018. init_seg(&save->ss);
  1019. init_seg(&save->ds);
  1020. init_seg(&save->fs);
  1021. init_seg(&save->gs);
  1022. save->cs.selector = 0xf000;
  1023. save->cs.base = 0xffff0000;
  1024. /* Executable/Readable Code Segment */
  1025. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  1026. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  1027. save->cs.limit = 0xffff;
  1028. save->gdtr.base = 0;
  1029. save->gdtr.limit = 0xffff;
  1030. save->idtr.base = 0;
  1031. save->idtr.limit = 0xffff;
  1032. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  1033. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  1034. if (npt_enabled) {
  1035. /* Setup VMCB for Nested Paging */
  1036. control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
  1037. svm_clr_intercept(svm, INTERCEPT_INVLPG);
  1038. clr_exception_intercept(svm, PF_VECTOR);
  1039. svm_clr_intercept(svm, INTERCEPT_CR3_READ);
  1040. svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
  1041. save->g_pat = vcpu->arch.pat;
  1042. save->cr3 = 0;
  1043. }
  1044. svm->current_vmcb->asid_generation = 0;
  1045. svm->asid = 0;
  1046. svm->nested.vmcb12_gpa = INVALID_GPA;
  1047. svm->nested.last_vmcb12_gpa = INVALID_GPA;
  1048. if (!kvm_pause_in_guest(vcpu->kvm)) {
  1049. control->pause_filter_count = pause_filter_count;
  1050. if (pause_filter_thresh)
  1051. control->pause_filter_thresh = pause_filter_thresh;
  1052. svm_set_intercept(svm, INTERCEPT_PAUSE);
  1053. } else {
  1054. svm_clr_intercept(svm, INTERCEPT_PAUSE);
  1055. }
  1056. svm_recalc_instruction_intercepts(vcpu, svm);
  1057. /*
  1058. * If the host supports V_SPEC_CTRL then disable the interception
  1059. * of MSR_IA32_SPEC_CTRL.
  1060. */
  1061. if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
  1062. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
  1063. if (kvm_vcpu_apicv_active(vcpu))
  1064. avic_init_vmcb(svm, vmcb);
  1065. if (vgif) {
  1066. svm_clr_intercept(svm, INTERCEPT_STGI);
  1067. svm_clr_intercept(svm, INTERCEPT_CLGI);
  1068. svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
  1069. }
  1070. if (sev_guest(vcpu->kvm))
  1071. sev_init_vmcb(svm);
  1072. svm_hv_init_vmcb(vmcb);
  1073. init_vmcb_after_set_cpuid(vcpu);
  1074. vmcb_mark_all_dirty(vmcb);
  1075. enable_gif(svm);
  1076. }
  1077. static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)
  1078. {
  1079. struct vcpu_svm *svm = to_svm(vcpu);
  1080. svm_vcpu_init_msrpm(vcpu, svm->msrpm);
  1081. svm_init_osvw(vcpu);
  1082. vcpu->arch.microcode_version = 0x01000065;
  1083. svm->tsc_ratio_msr = kvm_caps.default_tsc_scaling_ratio;
  1084. if (sev_es_guest(vcpu->kvm))
  1085. sev_es_vcpu_reset(svm);
  1086. }
  1087. static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  1088. {
  1089. struct vcpu_svm *svm = to_svm(vcpu);
  1090. svm->spec_ctrl = 0;
  1091. svm->virt_spec_ctrl = 0;
  1092. init_vmcb(vcpu);
  1093. if (!init_event)
  1094. __svm_vcpu_reset(vcpu);
  1095. }
  1096. void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
  1097. {
  1098. svm->current_vmcb = target_vmcb;
  1099. svm->vmcb = target_vmcb->ptr;
  1100. }
  1101. static int svm_vcpu_create(struct kvm_vcpu *vcpu)
  1102. {
  1103. struct vcpu_svm *svm;
  1104. struct page *vmcb01_page;
  1105. struct page *vmsa_page = NULL;
  1106. int err;
  1107. BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
  1108. svm = to_svm(vcpu);
  1109. err = -ENOMEM;
  1110. vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  1111. if (!vmcb01_page)
  1112. goto out;
  1113. if (sev_es_guest(vcpu->kvm)) {
  1114. /*
  1115. * SEV-ES guests require a separate VMSA page used to contain
  1116. * the encrypted register state of the guest.
  1117. */
  1118. vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  1119. if (!vmsa_page)
  1120. goto error_free_vmcb_page;
  1121. /*
  1122. * SEV-ES guests maintain an encrypted version of their FPU
  1123. * state which is restored and saved on VMRUN and VMEXIT.
  1124. * Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't
  1125. * do xsave/xrstor on it.
  1126. */
  1127. fpstate_set_confidential(&vcpu->arch.guest_fpu);
  1128. }
  1129. err = avic_init_vcpu(svm);
  1130. if (err)
  1131. goto error_free_vmsa_page;
  1132. svm->msrpm = svm_vcpu_alloc_msrpm();
  1133. if (!svm->msrpm) {
  1134. err = -ENOMEM;
  1135. goto error_free_vmsa_page;
  1136. }
  1137. svm->x2avic_msrs_intercepted = true;
  1138. svm->vmcb01.ptr = page_address(vmcb01_page);
  1139. svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
  1140. svm_switch_vmcb(svm, &svm->vmcb01);
  1141. if (vmsa_page)
  1142. svm->sev_es.vmsa = page_address(vmsa_page);
  1143. svm->guest_state_loaded = false;
  1144. return 0;
  1145. error_free_vmsa_page:
  1146. if (vmsa_page)
  1147. __free_page(vmsa_page);
  1148. error_free_vmcb_page:
  1149. __free_page(vmcb01_page);
  1150. out:
  1151. return err;
  1152. }
  1153. static void svm_clear_current_vmcb(struct vmcb *vmcb)
  1154. {
  1155. int i;
  1156. for_each_online_cpu(i)
  1157. cmpxchg(per_cpu_ptr(&svm_data.current_vmcb, i), vmcb, NULL);
  1158. }
  1159. static void svm_vcpu_free(struct kvm_vcpu *vcpu)
  1160. {
  1161. struct vcpu_svm *svm = to_svm(vcpu);
  1162. /*
  1163. * The vmcb page can be recycled, causing a false negative in
  1164. * svm_vcpu_load(). So, ensure that no logical CPU has this
  1165. * vmcb page recorded as its current vmcb.
  1166. */
  1167. svm_clear_current_vmcb(svm->vmcb);
  1168. svm_leave_nested(vcpu);
  1169. svm_free_nested(svm);
  1170. sev_free_vcpu(vcpu);
  1171. __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
  1172. __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
  1173. }
  1174. static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
  1175. {
  1176. struct vcpu_svm *svm = to_svm(vcpu);
  1177. struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);
  1178. if (sev_es_guest(vcpu->kvm))
  1179. sev_es_unmap_ghcb(svm);
  1180. if (svm->guest_state_loaded)
  1181. return;
  1182. /*
  1183. * Save additional host state that will be restored on VMEXIT (sev-es)
  1184. * or subsequent vmload of host save area.
  1185. */
  1186. vmsave(sd->save_area_pa);
  1187. if (sev_es_guest(vcpu->kvm)) {
  1188. struct sev_es_save_area *hostsa;
  1189. hostsa = (struct sev_es_save_area *)(page_address(sd->save_area) + 0x400);
  1190. sev_es_prepare_switch_to_guest(hostsa);
  1191. }
  1192. if (tsc_scaling)
  1193. __svm_write_tsc_multiplier(vcpu->arch.tsc_scaling_ratio);
  1194. if (likely(tsc_aux_uret_slot >= 0))
  1195. kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
  1196. svm->guest_state_loaded = true;
  1197. }
  1198. static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
  1199. {
  1200. to_svm(vcpu)->guest_state_loaded = false;
  1201. }
  1202. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1203. {
  1204. struct vcpu_svm *svm = to_svm(vcpu);
  1205. struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, cpu);
  1206. if (sd->current_vmcb != svm->vmcb) {
  1207. sd->current_vmcb = svm->vmcb;
  1208. if (!cpu_feature_enabled(X86_FEATURE_IBPB_ON_VMEXIT))
  1209. indirect_branch_prediction_barrier();
  1210. }
  1211. if (kvm_vcpu_apicv_active(vcpu))
  1212. avic_vcpu_load(vcpu, cpu);
  1213. }
  1214. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  1215. {
  1216. if (kvm_vcpu_apicv_active(vcpu))
  1217. avic_vcpu_put(vcpu);
  1218. svm_prepare_host_switch(vcpu);
  1219. ++vcpu->stat.host_state_reload;
  1220. }
  1221. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  1222. {
  1223. struct vcpu_svm *svm = to_svm(vcpu);
  1224. unsigned long rflags = svm->vmcb->save.rflags;
  1225. if (svm->nmi_singlestep) {
  1226. /* Hide our flags if they were not set by the guest */
  1227. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
  1228. rflags &= ~X86_EFLAGS_TF;
  1229. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
  1230. rflags &= ~X86_EFLAGS_RF;
  1231. }
  1232. return rflags;
  1233. }
  1234. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1235. {
  1236. if (to_svm(vcpu)->nmi_singlestep)
  1237. rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  1238. /*
  1239. * Any change of EFLAGS.VM is accompanied by a reload of SS
  1240. * (caused by either a task switch or an inter-privilege IRET),
  1241. * so we do not need to update the CPL here.
  1242. */
  1243. to_svm(vcpu)->vmcb->save.rflags = rflags;
  1244. }
  1245. static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
  1246. {
  1247. struct vmcb *vmcb = to_svm(vcpu)->vmcb;
  1248. return sev_es_guest(vcpu->kvm)
  1249. ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
  1250. : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;
  1251. }
  1252. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1253. {
  1254. kvm_register_mark_available(vcpu, reg);
  1255. switch (reg) {
  1256. case VCPU_EXREG_PDPTR:
  1257. /*
  1258. * When !npt_enabled, mmu->pdptrs[] is already available since
  1259. * it is always updated per SDM when moving to CRs.
  1260. */
  1261. if (npt_enabled)
  1262. load_pdptrs(vcpu, kvm_read_cr3(vcpu));
  1263. break;
  1264. default:
  1265. KVM_BUG_ON(1, vcpu->kvm);
  1266. }
  1267. }
  1268. static void svm_set_vintr(struct vcpu_svm *svm)
  1269. {
  1270. struct vmcb_control_area *control;
  1271. /*
  1272. * The following fields are ignored when AVIC is enabled
  1273. */
  1274. WARN_ON(kvm_vcpu_apicv_activated(&svm->vcpu));
  1275. svm_set_intercept(svm, INTERCEPT_VINTR);
  1276. /*
  1277. * This is just a dummy VINTR to actually cause a vmexit to happen.
  1278. * Actual injection of virtual interrupts happens through EVENTINJ.
  1279. */
  1280. control = &svm->vmcb->control;
  1281. control->int_vector = 0x0;
  1282. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1283. control->int_ctl |= V_IRQ_MASK |
  1284. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1285. vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
  1286. }
  1287. static void svm_clear_vintr(struct vcpu_svm *svm)
  1288. {
  1289. svm_clr_intercept(svm, INTERCEPT_VINTR);
  1290. /* Drop int_ctl fields related to VINTR injection. */
  1291. svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
  1292. if (is_guest_mode(&svm->vcpu)) {
  1293. svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
  1294. WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
  1295. (svm->nested.ctl.int_ctl & V_TPR_MASK));
  1296. svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl &
  1297. V_IRQ_INJECTION_BITS_MASK;
  1298. svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
  1299. }
  1300. vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
  1301. }
  1302. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  1303. {
  1304. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1305. struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
  1306. switch (seg) {
  1307. case VCPU_SREG_CS: return &save->cs;
  1308. case VCPU_SREG_DS: return &save->ds;
  1309. case VCPU_SREG_ES: return &save->es;
  1310. case VCPU_SREG_FS: return &save01->fs;
  1311. case VCPU_SREG_GS: return &save01->gs;
  1312. case VCPU_SREG_SS: return &save->ss;
  1313. case VCPU_SREG_TR: return &save01->tr;
  1314. case VCPU_SREG_LDTR: return &save01->ldtr;
  1315. }
  1316. BUG();
  1317. return NULL;
  1318. }
  1319. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1320. {
  1321. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1322. return s->base;
  1323. }
  1324. static void svm_get_segment(struct kvm_vcpu *vcpu,
  1325. struct kvm_segment *var, int seg)
  1326. {
  1327. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1328. var->base = s->base;
  1329. var->limit = s->limit;
  1330. var->selector = s->selector;
  1331. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  1332. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  1333. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1334. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  1335. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  1336. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  1337. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  1338. /*
  1339. * AMD CPUs circa 2014 track the G bit for all segments except CS.
  1340. * However, the SVM spec states that the G bit is not observed by the
  1341. * CPU, and some VMware virtual CPUs drop the G bit for all segments.
  1342. * So let's synthesize a legal G bit for all segments, this helps
  1343. * running KVM nested. It also helps cross-vendor migration, because
  1344. * Intel's vmentry has a check on the 'G' bit.
  1345. */
  1346. var->g = s->limit > 0xfffff;
  1347. /*
  1348. * AMD's VMCB does not have an explicit unusable field, so emulate it
  1349. * for cross vendor migration purposes by "not present"
  1350. */
  1351. var->unusable = !var->present;
  1352. switch (seg) {
  1353. case VCPU_SREG_TR:
  1354. /*
  1355. * Work around a bug where the busy flag in the tr selector
  1356. * isn't exposed
  1357. */
  1358. var->type |= 0x2;
  1359. break;
  1360. case VCPU_SREG_DS:
  1361. case VCPU_SREG_ES:
  1362. case VCPU_SREG_FS:
  1363. case VCPU_SREG_GS:
  1364. /*
  1365. * The accessed bit must always be set in the segment
  1366. * descriptor cache, although it can be cleared in the
  1367. * descriptor, the cached bit always remains at 1. Since
  1368. * Intel has a check on this, set it here to support
  1369. * cross-vendor migration.
  1370. */
  1371. if (!var->unusable)
  1372. var->type |= 0x1;
  1373. break;
  1374. case VCPU_SREG_SS:
  1375. /*
  1376. * On AMD CPUs sometimes the DB bit in the segment
  1377. * descriptor is left as 1, although the whole segment has
  1378. * been made unusable. Clear it here to pass an Intel VMX
  1379. * entry check when cross vendor migrating.
  1380. */
  1381. if (var->unusable)
  1382. var->db = 0;
  1383. /* This is symmetric with svm_set_segment() */
  1384. var->dpl = to_svm(vcpu)->vmcb->save.cpl;
  1385. break;
  1386. }
  1387. }
  1388. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1389. {
  1390. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1391. return save->cpl;
  1392. }
  1393. static void svm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  1394. {
  1395. struct kvm_segment cs;
  1396. svm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1397. *db = cs.db;
  1398. *l = cs.l;
  1399. }
  1400. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1401. {
  1402. struct vcpu_svm *svm = to_svm(vcpu);
  1403. dt->size = svm->vmcb->save.idtr.limit;
  1404. dt->address = svm->vmcb->save.idtr.base;
  1405. }
  1406. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1407. {
  1408. struct vcpu_svm *svm = to_svm(vcpu);
  1409. svm->vmcb->save.idtr.limit = dt->size;
  1410. svm->vmcb->save.idtr.base = dt->address ;
  1411. vmcb_mark_dirty(svm->vmcb, VMCB_DT);
  1412. }
  1413. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1414. {
  1415. struct vcpu_svm *svm = to_svm(vcpu);
  1416. dt->size = svm->vmcb->save.gdtr.limit;
  1417. dt->address = svm->vmcb->save.gdtr.base;
  1418. }
  1419. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1420. {
  1421. struct vcpu_svm *svm = to_svm(vcpu);
  1422. svm->vmcb->save.gdtr.limit = dt->size;
  1423. svm->vmcb->save.gdtr.base = dt->address ;
  1424. vmcb_mark_dirty(svm->vmcb, VMCB_DT);
  1425. }
  1426. static void sev_post_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  1427. {
  1428. struct vcpu_svm *svm = to_svm(vcpu);
  1429. /*
  1430. * For guests that don't set guest_state_protected, the cr3 update is
  1431. * handled via kvm_mmu_load() while entering the guest. For guests
  1432. * that do (SEV-ES/SEV-SNP), the cr3 update needs to be written to
  1433. * VMCB save area now, since the save area will become the initial
  1434. * contents of the VMSA, and future VMCB save area updates won't be
  1435. * seen.
  1436. */
  1437. if (sev_es_guest(vcpu->kvm)) {
  1438. svm->vmcb->save.cr3 = cr3;
  1439. vmcb_mark_dirty(svm->vmcb, VMCB_CR);
  1440. }
  1441. }
  1442. static bool svm_is_valid_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1443. {
  1444. return true;
  1445. }
  1446. void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1447. {
  1448. struct vcpu_svm *svm = to_svm(vcpu);
  1449. u64 hcr0 = cr0;
  1450. bool old_paging = is_paging(vcpu);
  1451. #ifdef CONFIG_X86_64
  1452. if (vcpu->arch.efer & EFER_LME) {
  1453. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1454. vcpu->arch.efer |= EFER_LMA;
  1455. if (!vcpu->arch.guest_state_protected)
  1456. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1457. }
  1458. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1459. vcpu->arch.efer &= ~EFER_LMA;
  1460. if (!vcpu->arch.guest_state_protected)
  1461. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1462. }
  1463. }
  1464. #endif
  1465. vcpu->arch.cr0 = cr0;
  1466. if (!npt_enabled) {
  1467. hcr0 |= X86_CR0_PG | X86_CR0_WP;
  1468. if (old_paging != is_paging(vcpu))
  1469. svm_set_cr4(vcpu, kvm_read_cr4(vcpu));
  1470. }
  1471. /*
  1472. * re-enable caching here because the QEMU bios
  1473. * does not do it - this results in some delay at
  1474. * reboot
  1475. */
  1476. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  1477. hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1478. svm->vmcb->save.cr0 = hcr0;
  1479. vmcb_mark_dirty(svm->vmcb, VMCB_CR);
  1480. /*
  1481. * SEV-ES guests must always keep the CR intercepts cleared. CR
  1482. * tracking is done using the CR write traps.
  1483. */
  1484. if (sev_es_guest(vcpu->kvm))
  1485. return;
  1486. if (hcr0 == cr0) {
  1487. /* Selective CR0 write remains on. */
  1488. svm_clr_intercept(svm, INTERCEPT_CR0_READ);
  1489. svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
  1490. } else {
  1491. svm_set_intercept(svm, INTERCEPT_CR0_READ);
  1492. svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
  1493. }
  1494. }
  1495. static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1496. {
  1497. return true;
  1498. }
  1499. void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1500. {
  1501. unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
  1502. unsigned long old_cr4 = vcpu->arch.cr4;
  1503. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1504. svm_flush_tlb_current(vcpu);
  1505. vcpu->arch.cr4 = cr4;
  1506. if (!npt_enabled) {
  1507. cr4 |= X86_CR4_PAE;
  1508. if (!is_paging(vcpu))
  1509. cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
  1510. }
  1511. cr4 |= host_cr4_mce;
  1512. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1513. vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  1514. if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
  1515. kvm_update_cpuid_runtime(vcpu);
  1516. }
  1517. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1518. struct kvm_segment *var, int seg)
  1519. {
  1520. struct vcpu_svm *svm = to_svm(vcpu);
  1521. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1522. s->base = var->base;
  1523. s->limit = var->limit;
  1524. s->selector = var->selector;
  1525. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1526. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1527. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1528. s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
  1529. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1530. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1531. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1532. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1533. /*
  1534. * This is always accurate, except if SYSRET returned to a segment
  1535. * with SS.DPL != 3. Intel does not have this quirk, and always
  1536. * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
  1537. * would entail passing the CPL to userspace and back.
  1538. */
  1539. if (seg == VCPU_SREG_SS)
  1540. /* This is symmetric with svm_get_segment() */
  1541. svm->vmcb->save.cpl = (var->dpl & 3);
  1542. vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
  1543. }
  1544. static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
  1545. {
  1546. struct vcpu_svm *svm = to_svm(vcpu);
  1547. clr_exception_intercept(svm, BP_VECTOR);
  1548. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1549. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1550. set_exception_intercept(svm, BP_VECTOR);
  1551. }
  1552. }
  1553. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1554. {
  1555. if (sd->next_asid > sd->max_asid) {
  1556. ++sd->asid_generation;
  1557. sd->next_asid = sd->min_asid;
  1558. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1559. vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
  1560. }
  1561. svm->current_vmcb->asid_generation = sd->asid_generation;
  1562. svm->asid = sd->next_asid++;
  1563. }
  1564. static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
  1565. {
  1566. struct vmcb *vmcb = svm->vmcb;
  1567. if (svm->vcpu.arch.guest_state_protected)
  1568. return;
  1569. if (unlikely(value != vmcb->save.dr6)) {
  1570. vmcb->save.dr6 = value;
  1571. vmcb_mark_dirty(vmcb, VMCB_DR);
  1572. }
  1573. }
  1574. static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  1575. {
  1576. struct vcpu_svm *svm = to_svm(vcpu);
  1577. if (vcpu->arch.guest_state_protected)
  1578. return;
  1579. get_debugreg(vcpu->arch.db[0], 0);
  1580. get_debugreg(vcpu->arch.db[1], 1);
  1581. get_debugreg(vcpu->arch.db[2], 2);
  1582. get_debugreg(vcpu->arch.db[3], 3);
  1583. /*
  1584. * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
  1585. * because db_interception might need it. We can do it before vmentry.
  1586. */
  1587. vcpu->arch.dr6 = svm->vmcb->save.dr6;
  1588. vcpu->arch.dr7 = svm->vmcb->save.dr7;
  1589. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  1590. set_dr_intercepts(svm);
  1591. }
  1592. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1593. {
  1594. struct vcpu_svm *svm = to_svm(vcpu);
  1595. if (vcpu->arch.guest_state_protected)
  1596. return;
  1597. svm->vmcb->save.dr7 = value;
  1598. vmcb_mark_dirty(svm->vmcb, VMCB_DR);
  1599. }
  1600. static int pf_interception(struct kvm_vcpu *vcpu)
  1601. {
  1602. struct vcpu_svm *svm = to_svm(vcpu);
  1603. u64 fault_address = svm->vmcb->control.exit_info_2;
  1604. u64 error_code = svm->vmcb->control.exit_info_1;
  1605. return kvm_handle_page_fault(vcpu, error_code, fault_address,
  1606. static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
  1607. svm->vmcb->control.insn_bytes : NULL,
  1608. svm->vmcb->control.insn_len);
  1609. }
  1610. static int npf_interception(struct kvm_vcpu *vcpu)
  1611. {
  1612. struct vcpu_svm *svm = to_svm(vcpu);
  1613. u64 fault_address = svm->vmcb->control.exit_info_2;
  1614. u64 error_code = svm->vmcb->control.exit_info_1;
  1615. trace_kvm_page_fault(vcpu, fault_address, error_code);
  1616. return kvm_mmu_page_fault(vcpu, fault_address, error_code,
  1617. static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
  1618. svm->vmcb->control.insn_bytes : NULL,
  1619. svm->vmcb->control.insn_len);
  1620. }
  1621. static int db_interception(struct kvm_vcpu *vcpu)
  1622. {
  1623. struct kvm_run *kvm_run = vcpu->run;
  1624. struct vcpu_svm *svm = to_svm(vcpu);
  1625. if (!(vcpu->guest_debug &
  1626. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1627. !svm->nmi_singlestep) {
  1628. u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
  1629. kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
  1630. return 1;
  1631. }
  1632. if (svm->nmi_singlestep) {
  1633. disable_nmi_singlestep(svm);
  1634. /* Make sure we check for pending NMIs upon entry */
  1635. kvm_make_request(KVM_REQ_EVENT, vcpu);
  1636. }
  1637. if (vcpu->guest_debug &
  1638. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1639. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1640. kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
  1641. kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
  1642. kvm_run->debug.arch.pc =
  1643. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1644. kvm_run->debug.arch.exception = DB_VECTOR;
  1645. return 0;
  1646. }
  1647. return 1;
  1648. }
  1649. static int bp_interception(struct kvm_vcpu *vcpu)
  1650. {
  1651. struct vcpu_svm *svm = to_svm(vcpu);
  1652. struct kvm_run *kvm_run = vcpu->run;
  1653. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1654. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1655. kvm_run->debug.arch.exception = BP_VECTOR;
  1656. return 0;
  1657. }
  1658. static int ud_interception(struct kvm_vcpu *vcpu)
  1659. {
  1660. return handle_ud(vcpu);
  1661. }
  1662. static int ac_interception(struct kvm_vcpu *vcpu)
  1663. {
  1664. kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
  1665. return 1;
  1666. }
  1667. static bool is_erratum_383(void)
  1668. {
  1669. int err, i;
  1670. u64 value;
  1671. if (!erratum_383_found)
  1672. return false;
  1673. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  1674. if (err)
  1675. return false;
  1676. /* Bit 62 may or may not be set for this mce */
  1677. value &= ~(1ULL << 62);
  1678. if (value != 0xb600000000010015ULL)
  1679. return false;
  1680. /* Clear MCi_STATUS registers */
  1681. for (i = 0; i < 6; ++i)
  1682. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  1683. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  1684. if (!err) {
  1685. u32 low, high;
  1686. value &= ~(1ULL << 2);
  1687. low = lower_32_bits(value);
  1688. high = upper_32_bits(value);
  1689. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  1690. }
  1691. /* Flush tlb to evict multi-match entries */
  1692. __flush_tlb_all();
  1693. return true;
  1694. }
  1695. static void svm_handle_mce(struct kvm_vcpu *vcpu)
  1696. {
  1697. if (is_erratum_383()) {
  1698. /*
  1699. * Erratum 383 triggered. Guest state is corrupt so kill the
  1700. * guest.
  1701. */
  1702. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  1703. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  1704. return;
  1705. }
  1706. /*
  1707. * On an #MC intercept the MCE handler is not called automatically in
  1708. * the host. So do it by hand here.
  1709. */
  1710. kvm_machine_check();
  1711. }
  1712. static int mc_interception(struct kvm_vcpu *vcpu)
  1713. {
  1714. return 1;
  1715. }
  1716. static int shutdown_interception(struct kvm_vcpu *vcpu)
  1717. {
  1718. struct kvm_run *kvm_run = vcpu->run;
  1719. struct vcpu_svm *svm = to_svm(vcpu);
  1720. /*
  1721. * The VM save area has already been encrypted so it
  1722. * cannot be reinitialized - just terminate.
  1723. */
  1724. if (sev_es_guest(vcpu->kvm))
  1725. return -EINVAL;
  1726. /*
  1727. * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put
  1728. * the VMCB in a known good state. Unfortuately, KVM doesn't have
  1729. * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking
  1730. * userspace. At a platform view, INIT is acceptable behavior as
  1731. * there exist bare metal platforms that automatically INIT the CPU
  1732. * in response to shutdown.
  1733. */
  1734. clear_page(svm->vmcb);
  1735. kvm_vcpu_reset(vcpu, true);
  1736. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1737. return 0;
  1738. }
  1739. static int io_interception(struct kvm_vcpu *vcpu)
  1740. {
  1741. struct vcpu_svm *svm = to_svm(vcpu);
  1742. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1743. int size, in, string;
  1744. unsigned port;
  1745. ++vcpu->stat.io_exits;
  1746. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1747. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1748. port = io_info >> 16;
  1749. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1750. if (string) {
  1751. if (sev_es_guest(vcpu->kvm))
  1752. return sev_es_string_io(svm, size, port, in);
  1753. else
  1754. return kvm_emulate_instruction(vcpu, 0);
  1755. }
  1756. svm->next_rip = svm->vmcb->control.exit_info_2;
  1757. return kvm_fast_pio(vcpu, size, port, in);
  1758. }
  1759. static int nmi_interception(struct kvm_vcpu *vcpu)
  1760. {
  1761. return 1;
  1762. }
  1763. static int smi_interception(struct kvm_vcpu *vcpu)
  1764. {
  1765. return 1;
  1766. }
  1767. static int intr_interception(struct kvm_vcpu *vcpu)
  1768. {
  1769. ++vcpu->stat.irq_exits;
  1770. return 1;
  1771. }
  1772. static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
  1773. {
  1774. struct vcpu_svm *svm = to_svm(vcpu);
  1775. struct vmcb *vmcb12;
  1776. struct kvm_host_map map;
  1777. int ret;
  1778. if (nested_svm_check_permissions(vcpu))
  1779. return 1;
  1780. ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
  1781. if (ret) {
  1782. if (ret == -EINVAL)
  1783. kvm_inject_gp(vcpu, 0);
  1784. return 1;
  1785. }
  1786. vmcb12 = map.hva;
  1787. ret = kvm_skip_emulated_instruction(vcpu);
  1788. if (vmload) {
  1789. svm_copy_vmloadsave_state(svm->vmcb, vmcb12);
  1790. svm->sysenter_eip_hi = 0;
  1791. svm->sysenter_esp_hi = 0;
  1792. } else {
  1793. svm_copy_vmloadsave_state(vmcb12, svm->vmcb);
  1794. }
  1795. kvm_vcpu_unmap(vcpu, &map, true);
  1796. return ret;
  1797. }
  1798. static int vmload_interception(struct kvm_vcpu *vcpu)
  1799. {
  1800. return vmload_vmsave_interception(vcpu, true);
  1801. }
  1802. static int vmsave_interception(struct kvm_vcpu *vcpu)
  1803. {
  1804. return vmload_vmsave_interception(vcpu, false);
  1805. }
  1806. static int vmrun_interception(struct kvm_vcpu *vcpu)
  1807. {
  1808. if (nested_svm_check_permissions(vcpu))
  1809. return 1;
  1810. return nested_svm_vmrun(vcpu);
  1811. }
  1812. enum {
  1813. NONE_SVM_INSTR,
  1814. SVM_INSTR_VMRUN,
  1815. SVM_INSTR_VMLOAD,
  1816. SVM_INSTR_VMSAVE,
  1817. };
  1818. /* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
  1819. static int svm_instr_opcode(struct kvm_vcpu *vcpu)
  1820. {
  1821. struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
  1822. if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
  1823. return NONE_SVM_INSTR;
  1824. switch (ctxt->modrm) {
  1825. case 0xd8: /* VMRUN */
  1826. return SVM_INSTR_VMRUN;
  1827. case 0xda: /* VMLOAD */
  1828. return SVM_INSTR_VMLOAD;
  1829. case 0xdb: /* VMSAVE */
  1830. return SVM_INSTR_VMSAVE;
  1831. default:
  1832. break;
  1833. }
  1834. return NONE_SVM_INSTR;
  1835. }
  1836. static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
  1837. {
  1838. const int guest_mode_exit_codes[] = {
  1839. [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
  1840. [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
  1841. [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
  1842. };
  1843. int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
  1844. [SVM_INSTR_VMRUN] = vmrun_interception,
  1845. [SVM_INSTR_VMLOAD] = vmload_interception,
  1846. [SVM_INSTR_VMSAVE] = vmsave_interception,
  1847. };
  1848. struct vcpu_svm *svm = to_svm(vcpu);
  1849. int ret;
  1850. if (is_guest_mode(vcpu)) {
  1851. /* Returns '1' or -errno on failure, '0' on success. */
  1852. ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
  1853. if (ret)
  1854. return ret;
  1855. return 1;
  1856. }
  1857. return svm_instr_handlers[opcode](vcpu);
  1858. }
  1859. /*
  1860. * #GP handling code. Note that #GP can be triggered under the following two
  1861. * cases:
  1862. * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
  1863. * some AMD CPUs when EAX of these instructions are in the reserved memory
  1864. * regions (e.g. SMM memory on host).
  1865. * 2) VMware backdoor
  1866. */
  1867. static int gp_interception(struct kvm_vcpu *vcpu)
  1868. {
  1869. struct vcpu_svm *svm = to_svm(vcpu);
  1870. u32 error_code = svm->vmcb->control.exit_info_1;
  1871. int opcode;
  1872. /* Both #GP cases have zero error_code */
  1873. if (error_code)
  1874. goto reinject;
  1875. /* Decode the instruction for usage later */
  1876. if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
  1877. goto reinject;
  1878. opcode = svm_instr_opcode(vcpu);
  1879. if (opcode == NONE_SVM_INSTR) {
  1880. if (!enable_vmware_backdoor)
  1881. goto reinject;
  1882. /*
  1883. * VMware backdoor emulation on #GP interception only handles
  1884. * IN{S}, OUT{S}, and RDPMC.
  1885. */
  1886. if (!is_guest_mode(vcpu))
  1887. return kvm_emulate_instruction(vcpu,
  1888. EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
  1889. } else {
  1890. /* All SVM instructions expect page aligned RAX */
  1891. if (svm->vmcb->save.rax & ~PAGE_MASK)
  1892. goto reinject;
  1893. return emulate_svm_instr(vcpu, opcode);
  1894. }
  1895. reinject:
  1896. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  1897. return 1;
  1898. }
  1899. void svm_set_gif(struct vcpu_svm *svm, bool value)
  1900. {
  1901. if (value) {
  1902. /*
  1903. * If VGIF is enabled, the STGI intercept is only added to
  1904. * detect the opening of the SMI/NMI window; remove it now.
  1905. * Likewise, clear the VINTR intercept, we will set it
  1906. * again while processing KVM_REQ_EVENT if needed.
  1907. */
  1908. if (vgif)
  1909. svm_clr_intercept(svm, INTERCEPT_STGI);
  1910. if (svm_is_intercept(svm, INTERCEPT_VINTR))
  1911. svm_clear_vintr(svm);
  1912. enable_gif(svm);
  1913. if (svm->vcpu.arch.smi_pending ||
  1914. svm->vcpu.arch.nmi_pending ||
  1915. kvm_cpu_has_injectable_intr(&svm->vcpu) ||
  1916. kvm_apic_has_pending_init_or_sipi(&svm->vcpu))
  1917. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  1918. } else {
  1919. disable_gif(svm);
  1920. /*
  1921. * After a CLGI no interrupts should come. But if vGIF is
  1922. * in use, we still rely on the VINTR intercept (rather than
  1923. * STGI) to detect an open interrupt window.
  1924. */
  1925. if (!vgif)
  1926. svm_clear_vintr(svm);
  1927. }
  1928. }
  1929. static int stgi_interception(struct kvm_vcpu *vcpu)
  1930. {
  1931. int ret;
  1932. if (nested_svm_check_permissions(vcpu))
  1933. return 1;
  1934. ret = kvm_skip_emulated_instruction(vcpu);
  1935. svm_set_gif(to_svm(vcpu), true);
  1936. return ret;
  1937. }
  1938. static int clgi_interception(struct kvm_vcpu *vcpu)
  1939. {
  1940. int ret;
  1941. if (nested_svm_check_permissions(vcpu))
  1942. return 1;
  1943. ret = kvm_skip_emulated_instruction(vcpu);
  1944. svm_set_gif(to_svm(vcpu), false);
  1945. return ret;
  1946. }
  1947. static int invlpga_interception(struct kvm_vcpu *vcpu)
  1948. {
  1949. gva_t gva = kvm_rax_read(vcpu);
  1950. u32 asid = kvm_rcx_read(vcpu);
  1951. /* FIXME: Handle an address size prefix. */
  1952. if (!is_long_mode(vcpu))
  1953. gva = (u32)gva;
  1954. trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
  1955. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  1956. kvm_mmu_invlpg(vcpu, gva);
  1957. return kvm_skip_emulated_instruction(vcpu);
  1958. }
  1959. static int skinit_interception(struct kvm_vcpu *vcpu)
  1960. {
  1961. trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
  1962. kvm_queue_exception(vcpu, UD_VECTOR);
  1963. return 1;
  1964. }
  1965. static int task_switch_interception(struct kvm_vcpu *vcpu)
  1966. {
  1967. struct vcpu_svm *svm = to_svm(vcpu);
  1968. u16 tss_selector;
  1969. int reason;
  1970. int int_type = svm->vmcb->control.exit_int_info &
  1971. SVM_EXITINTINFO_TYPE_MASK;
  1972. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  1973. uint32_t type =
  1974. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  1975. uint32_t idt_v =
  1976. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  1977. bool has_error_code = false;
  1978. u32 error_code = 0;
  1979. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  1980. if (svm->vmcb->control.exit_info_2 &
  1981. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  1982. reason = TASK_SWITCH_IRET;
  1983. else if (svm->vmcb->control.exit_info_2 &
  1984. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  1985. reason = TASK_SWITCH_JMP;
  1986. else if (idt_v)
  1987. reason = TASK_SWITCH_GATE;
  1988. else
  1989. reason = TASK_SWITCH_CALL;
  1990. if (reason == TASK_SWITCH_GATE) {
  1991. switch (type) {
  1992. case SVM_EXITINTINFO_TYPE_NMI:
  1993. vcpu->arch.nmi_injected = false;
  1994. break;
  1995. case SVM_EXITINTINFO_TYPE_EXEPT:
  1996. if (svm->vmcb->control.exit_info_2 &
  1997. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  1998. has_error_code = true;
  1999. error_code =
  2000. (u32)svm->vmcb->control.exit_info_2;
  2001. }
  2002. kvm_clear_exception_queue(vcpu);
  2003. break;
  2004. case SVM_EXITINTINFO_TYPE_INTR:
  2005. case SVM_EXITINTINFO_TYPE_SOFT:
  2006. kvm_clear_interrupt_queue(vcpu);
  2007. break;
  2008. default:
  2009. break;
  2010. }
  2011. }
  2012. if (reason != TASK_SWITCH_GATE ||
  2013. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2014. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2015. (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
  2016. if (!svm_skip_emulated_instruction(vcpu))
  2017. return 0;
  2018. }
  2019. if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
  2020. int_vec = -1;
  2021. return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
  2022. has_error_code, error_code);
  2023. }
  2024. static int iret_interception(struct kvm_vcpu *vcpu)
  2025. {
  2026. struct vcpu_svm *svm = to_svm(vcpu);
  2027. ++vcpu->stat.nmi_window_exits;
  2028. vcpu->arch.hflags |= HF_IRET_MASK;
  2029. if (!sev_es_guest(vcpu->kvm)) {
  2030. svm_clr_intercept(svm, INTERCEPT_IRET);
  2031. svm->nmi_iret_rip = kvm_rip_read(vcpu);
  2032. }
  2033. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2034. return 1;
  2035. }
  2036. static int invlpg_interception(struct kvm_vcpu *vcpu)
  2037. {
  2038. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2039. return kvm_emulate_instruction(vcpu, 0);
  2040. kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
  2041. return kvm_skip_emulated_instruction(vcpu);
  2042. }
  2043. static int emulate_on_interception(struct kvm_vcpu *vcpu)
  2044. {
  2045. return kvm_emulate_instruction(vcpu, 0);
  2046. }
  2047. static int rsm_interception(struct kvm_vcpu *vcpu)
  2048. {
  2049. return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
  2050. }
  2051. static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
  2052. unsigned long val)
  2053. {
  2054. struct vcpu_svm *svm = to_svm(vcpu);
  2055. unsigned long cr0 = vcpu->arch.cr0;
  2056. bool ret = false;
  2057. if (!is_guest_mode(vcpu) ||
  2058. (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
  2059. return false;
  2060. cr0 &= ~SVM_CR0_SELECTIVE_MASK;
  2061. val &= ~SVM_CR0_SELECTIVE_MASK;
  2062. if (cr0 ^ val) {
  2063. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  2064. ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
  2065. }
  2066. return ret;
  2067. }
  2068. #define CR_VALID (1ULL << 63)
  2069. static int cr_interception(struct kvm_vcpu *vcpu)
  2070. {
  2071. struct vcpu_svm *svm = to_svm(vcpu);
  2072. int reg, cr;
  2073. unsigned long val;
  2074. int err;
  2075. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2076. return emulate_on_interception(vcpu);
  2077. if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
  2078. return emulate_on_interception(vcpu);
  2079. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2080. if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
  2081. cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
  2082. else
  2083. cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
  2084. err = 0;
  2085. if (cr >= 16) { /* mov to cr */
  2086. cr -= 16;
  2087. val = kvm_register_read(vcpu, reg);
  2088. trace_kvm_cr_write(cr, val);
  2089. switch (cr) {
  2090. case 0:
  2091. if (!check_selective_cr0_intercepted(vcpu, val))
  2092. err = kvm_set_cr0(vcpu, val);
  2093. else
  2094. return 1;
  2095. break;
  2096. case 3:
  2097. err = kvm_set_cr3(vcpu, val);
  2098. break;
  2099. case 4:
  2100. err = kvm_set_cr4(vcpu, val);
  2101. break;
  2102. case 8:
  2103. err = kvm_set_cr8(vcpu, val);
  2104. break;
  2105. default:
  2106. WARN(1, "unhandled write to CR%d", cr);
  2107. kvm_queue_exception(vcpu, UD_VECTOR);
  2108. return 1;
  2109. }
  2110. } else { /* mov from cr */
  2111. switch (cr) {
  2112. case 0:
  2113. val = kvm_read_cr0(vcpu);
  2114. break;
  2115. case 2:
  2116. val = vcpu->arch.cr2;
  2117. break;
  2118. case 3:
  2119. val = kvm_read_cr3(vcpu);
  2120. break;
  2121. case 4:
  2122. val = kvm_read_cr4(vcpu);
  2123. break;
  2124. case 8:
  2125. val = kvm_get_cr8(vcpu);
  2126. break;
  2127. default:
  2128. WARN(1, "unhandled read from CR%d", cr);
  2129. kvm_queue_exception(vcpu, UD_VECTOR);
  2130. return 1;
  2131. }
  2132. kvm_register_write(vcpu, reg, val);
  2133. trace_kvm_cr_read(cr, val);
  2134. }
  2135. return kvm_complete_insn_gp(vcpu, err);
  2136. }
  2137. static int cr_trap(struct kvm_vcpu *vcpu)
  2138. {
  2139. struct vcpu_svm *svm = to_svm(vcpu);
  2140. unsigned long old_value, new_value;
  2141. unsigned int cr;
  2142. int ret = 0;
  2143. new_value = (unsigned long)svm->vmcb->control.exit_info_1;
  2144. cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
  2145. switch (cr) {
  2146. case 0:
  2147. old_value = kvm_read_cr0(vcpu);
  2148. svm_set_cr0(vcpu, new_value);
  2149. kvm_post_set_cr0(vcpu, old_value, new_value);
  2150. break;
  2151. case 4:
  2152. old_value = kvm_read_cr4(vcpu);
  2153. svm_set_cr4(vcpu, new_value);
  2154. kvm_post_set_cr4(vcpu, old_value, new_value);
  2155. break;
  2156. case 8:
  2157. ret = kvm_set_cr8(vcpu, new_value);
  2158. break;
  2159. default:
  2160. WARN(1, "unhandled CR%d write trap", cr);
  2161. kvm_queue_exception(vcpu, UD_VECTOR);
  2162. return 1;
  2163. }
  2164. return kvm_complete_insn_gp(vcpu, ret);
  2165. }
  2166. static int dr_interception(struct kvm_vcpu *vcpu)
  2167. {
  2168. struct vcpu_svm *svm = to_svm(vcpu);
  2169. int reg, dr;
  2170. unsigned long val;
  2171. int err = 0;
  2172. if (vcpu->guest_debug == 0) {
  2173. /*
  2174. * No more DR vmexits; force a reload of the debug registers
  2175. * and reenter on this instruction. The next vmexit will
  2176. * retrieve the full state of the debug registers.
  2177. */
  2178. clr_dr_intercepts(svm);
  2179. vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  2180. return 1;
  2181. }
  2182. if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
  2183. return emulate_on_interception(vcpu);
  2184. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2185. dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
  2186. if (dr >= 16) { /* mov to DRn */
  2187. dr -= 16;
  2188. val = kvm_register_read(vcpu, reg);
  2189. err = kvm_set_dr(vcpu, dr, val);
  2190. } else {
  2191. kvm_get_dr(vcpu, dr, &val);
  2192. kvm_register_write(vcpu, reg, val);
  2193. }
  2194. return kvm_complete_insn_gp(vcpu, err);
  2195. }
  2196. static int cr8_write_interception(struct kvm_vcpu *vcpu)
  2197. {
  2198. int r;
  2199. u8 cr8_prev = kvm_get_cr8(vcpu);
  2200. /* instruction emulation calls kvm_set_cr8() */
  2201. r = cr_interception(vcpu);
  2202. if (lapic_in_kernel(vcpu))
  2203. return r;
  2204. if (cr8_prev <= kvm_get_cr8(vcpu))
  2205. return r;
  2206. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  2207. return 0;
  2208. }
  2209. static int efer_trap(struct kvm_vcpu *vcpu)
  2210. {
  2211. struct msr_data msr_info;
  2212. int ret;
  2213. /*
  2214. * Clear the EFER_SVME bit from EFER. The SVM code always sets this
  2215. * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
  2216. * whether the guest has X86_FEATURE_SVM - this avoids a failure if
  2217. * the guest doesn't have X86_FEATURE_SVM.
  2218. */
  2219. msr_info.host_initiated = false;
  2220. msr_info.index = MSR_EFER;
  2221. msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
  2222. ret = kvm_set_msr_common(vcpu, &msr_info);
  2223. return kvm_complete_insn_gp(vcpu, ret);
  2224. }
  2225. static int svm_get_msr_feature(struct kvm_msr_entry *msr)
  2226. {
  2227. msr->data = 0;
  2228. switch (msr->index) {
  2229. case MSR_AMD64_DE_CFG:
  2230. if (cpu_feature_enabled(X86_FEATURE_LFENCE_RDTSC))
  2231. msr->data |= MSR_AMD64_DE_CFG_LFENCE_SERIALIZE;
  2232. break;
  2233. case MSR_IA32_PERF_CAPABILITIES:
  2234. msr->data = kvm_caps.supported_perf_cap;
  2235. return 0;
  2236. default:
  2237. return KVM_MSR_RET_INVALID;
  2238. }
  2239. return 0;
  2240. }
  2241. static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2242. {
  2243. struct vcpu_svm *svm = to_svm(vcpu);
  2244. switch (msr_info->index) {
  2245. case MSR_AMD64_TSC_RATIO:
  2246. if (!msr_info->host_initiated && !svm->tsc_scaling_enabled)
  2247. return 1;
  2248. msr_info->data = svm->tsc_ratio_msr;
  2249. break;
  2250. case MSR_STAR:
  2251. msr_info->data = svm->vmcb01.ptr->save.star;
  2252. break;
  2253. #ifdef CONFIG_X86_64
  2254. case MSR_LSTAR:
  2255. msr_info->data = svm->vmcb01.ptr->save.lstar;
  2256. break;
  2257. case MSR_CSTAR:
  2258. msr_info->data = svm->vmcb01.ptr->save.cstar;
  2259. break;
  2260. case MSR_KERNEL_GS_BASE:
  2261. msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
  2262. break;
  2263. case MSR_SYSCALL_MASK:
  2264. msr_info->data = svm->vmcb01.ptr->save.sfmask;
  2265. break;
  2266. #endif
  2267. case MSR_IA32_SYSENTER_CS:
  2268. msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
  2269. break;
  2270. case MSR_IA32_SYSENTER_EIP:
  2271. msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
  2272. if (guest_cpuid_is_intel(vcpu))
  2273. msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
  2274. break;
  2275. case MSR_IA32_SYSENTER_ESP:
  2276. msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
  2277. if (guest_cpuid_is_intel(vcpu))
  2278. msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
  2279. break;
  2280. case MSR_TSC_AUX:
  2281. msr_info->data = svm->tsc_aux;
  2282. break;
  2283. case MSR_IA32_DEBUGCTLMSR:
  2284. case MSR_IA32_LASTBRANCHFROMIP:
  2285. case MSR_IA32_LASTBRANCHTOIP:
  2286. case MSR_IA32_LASTINTFROMIP:
  2287. case MSR_IA32_LASTINTTOIP:
  2288. msr_info->data = svm_get_lbr_msr(svm, msr_info->index);
  2289. break;
  2290. case MSR_VM_HSAVE_PA:
  2291. msr_info->data = svm->nested.hsave_msr;
  2292. break;
  2293. case MSR_VM_CR:
  2294. msr_info->data = svm->nested.vm_cr_msr;
  2295. break;
  2296. case MSR_IA32_SPEC_CTRL:
  2297. if (!msr_info->host_initiated &&
  2298. !guest_has_spec_ctrl_msr(vcpu))
  2299. return 1;
  2300. if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
  2301. msr_info->data = svm->vmcb->save.spec_ctrl;
  2302. else
  2303. msr_info->data = svm->spec_ctrl;
  2304. break;
  2305. case MSR_AMD64_VIRT_SPEC_CTRL:
  2306. if (!msr_info->host_initiated &&
  2307. !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
  2308. return 1;
  2309. msr_info->data = svm->virt_spec_ctrl;
  2310. break;
  2311. case MSR_F15H_IC_CFG: {
  2312. int family, model;
  2313. family = guest_cpuid_family(vcpu);
  2314. model = guest_cpuid_model(vcpu);
  2315. if (family < 0 || model < 0)
  2316. return kvm_get_msr_common(vcpu, msr_info);
  2317. msr_info->data = 0;
  2318. if (family == 0x15 &&
  2319. (model >= 0x2 && model < 0x20))
  2320. msr_info->data = 0x1E;
  2321. }
  2322. break;
  2323. case MSR_AMD64_DE_CFG:
  2324. msr_info->data = svm->msr_decfg;
  2325. break;
  2326. default:
  2327. return kvm_get_msr_common(vcpu, msr_info);
  2328. }
  2329. return 0;
  2330. }
  2331. static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
  2332. {
  2333. struct vcpu_svm *svm = to_svm(vcpu);
  2334. if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb))
  2335. return kvm_complete_insn_gp(vcpu, err);
  2336. ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1);
  2337. ghcb_set_sw_exit_info_2(svm->sev_es.ghcb,
  2338. X86_TRAP_GP |
  2339. SVM_EVTINJ_TYPE_EXEPT |
  2340. SVM_EVTINJ_VALID);
  2341. return 1;
  2342. }
  2343. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2344. {
  2345. struct vcpu_svm *svm = to_svm(vcpu);
  2346. int svm_dis, chg_mask;
  2347. if (data & ~SVM_VM_CR_VALID_MASK)
  2348. return 1;
  2349. chg_mask = SVM_VM_CR_VALID_MASK;
  2350. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2351. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2352. svm->nested.vm_cr_msr &= ~chg_mask;
  2353. svm->nested.vm_cr_msr |= (data & chg_mask);
  2354. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2355. /* check for svm_disable while efer.svme is set */
  2356. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2357. return 1;
  2358. return 0;
  2359. }
  2360. static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  2361. {
  2362. struct vcpu_svm *svm = to_svm(vcpu);
  2363. int r;
  2364. u32 ecx = msr->index;
  2365. u64 data = msr->data;
  2366. switch (ecx) {
  2367. case MSR_AMD64_TSC_RATIO:
  2368. if (!svm->tsc_scaling_enabled) {
  2369. if (!msr->host_initiated)
  2370. return 1;
  2371. /*
  2372. * In case TSC scaling is not enabled, always
  2373. * leave this MSR at the default value.
  2374. *
  2375. * Due to bug in qemu 6.2.0, it would try to set
  2376. * this msr to 0 if tsc scaling is not enabled.
  2377. * Ignore this value as well.
  2378. */
  2379. if (data != 0 && data != svm->tsc_ratio_msr)
  2380. return 1;
  2381. break;
  2382. }
  2383. if (data & SVM_TSC_RATIO_RSVD)
  2384. return 1;
  2385. svm->tsc_ratio_msr = data;
  2386. if (svm->tsc_scaling_enabled && is_guest_mode(vcpu))
  2387. nested_svm_update_tsc_ratio_msr(vcpu);
  2388. break;
  2389. case MSR_IA32_CR_PAT:
  2390. if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
  2391. return 1;
  2392. vcpu->arch.pat = data;
  2393. svm->vmcb01.ptr->save.g_pat = data;
  2394. if (is_guest_mode(vcpu))
  2395. nested_vmcb02_compute_g_pat(svm);
  2396. vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
  2397. break;
  2398. case MSR_IA32_SPEC_CTRL:
  2399. if (!msr->host_initiated &&
  2400. !guest_has_spec_ctrl_msr(vcpu))
  2401. return 1;
  2402. if (kvm_spec_ctrl_test_value(data))
  2403. return 1;
  2404. if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
  2405. svm->vmcb->save.spec_ctrl = data;
  2406. else
  2407. svm->spec_ctrl = data;
  2408. if (!data)
  2409. break;
  2410. /*
  2411. * For non-nested:
  2412. * When it's written (to non-zero) for the first time, pass
  2413. * it through.
  2414. *
  2415. * For nested:
  2416. * The handling of the MSR bitmap for L2 guests is done in
  2417. * nested_svm_vmrun_msrpm.
  2418. * We update the L1 MSR bit as well since it will end up
  2419. * touching the MSR anyway now.
  2420. */
  2421. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
  2422. break;
  2423. case MSR_IA32_PRED_CMD:
  2424. if (!msr->host_initiated &&
  2425. !guest_has_pred_cmd_msr(vcpu))
  2426. return 1;
  2427. if (data & ~PRED_CMD_IBPB)
  2428. return 1;
  2429. if (!boot_cpu_has(X86_FEATURE_IBPB))
  2430. return 1;
  2431. if (!data)
  2432. break;
  2433. wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
  2434. set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
  2435. break;
  2436. case MSR_AMD64_VIRT_SPEC_CTRL:
  2437. if (!msr->host_initiated &&
  2438. !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
  2439. return 1;
  2440. if (data & ~SPEC_CTRL_SSBD)
  2441. return 1;
  2442. svm->virt_spec_ctrl = data;
  2443. break;
  2444. case MSR_STAR:
  2445. svm->vmcb01.ptr->save.star = data;
  2446. break;
  2447. #ifdef CONFIG_X86_64
  2448. case MSR_LSTAR:
  2449. svm->vmcb01.ptr->save.lstar = data;
  2450. break;
  2451. case MSR_CSTAR:
  2452. svm->vmcb01.ptr->save.cstar = data;
  2453. break;
  2454. case MSR_KERNEL_GS_BASE:
  2455. svm->vmcb01.ptr->save.kernel_gs_base = data;
  2456. break;
  2457. case MSR_SYSCALL_MASK:
  2458. svm->vmcb01.ptr->save.sfmask = data;
  2459. break;
  2460. #endif
  2461. case MSR_IA32_SYSENTER_CS:
  2462. svm->vmcb01.ptr->save.sysenter_cs = data;
  2463. break;
  2464. case MSR_IA32_SYSENTER_EIP:
  2465. svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
  2466. /*
  2467. * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
  2468. * when we spoof an Intel vendor ID (for cross vendor migration).
  2469. * In this case we use this intercept to track the high
  2470. * 32 bit part of these msrs to support Intel's
  2471. * implementation of SYSENTER/SYSEXIT.
  2472. */
  2473. svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
  2474. break;
  2475. case MSR_IA32_SYSENTER_ESP:
  2476. svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
  2477. svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
  2478. break;
  2479. case MSR_TSC_AUX:
  2480. /*
  2481. * TSC_AUX is usually changed only during boot and never read
  2482. * directly. Intercept TSC_AUX instead of exposing it to the
  2483. * guest via direct_access_msrs, and switch it via user return.
  2484. */
  2485. preempt_disable();
  2486. r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
  2487. preempt_enable();
  2488. if (r)
  2489. return 1;
  2490. svm->tsc_aux = data;
  2491. break;
  2492. case MSR_IA32_DEBUGCTLMSR:
  2493. if (!lbrv) {
  2494. vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  2495. __func__, data);
  2496. break;
  2497. }
  2498. if (data & DEBUGCTL_RESERVED_BITS)
  2499. return 1;
  2500. if (svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK)
  2501. svm->vmcb->save.dbgctl = data;
  2502. else
  2503. svm->vmcb01.ptr->save.dbgctl = data;
  2504. svm_update_lbrv(vcpu);
  2505. break;
  2506. case MSR_VM_HSAVE_PA:
  2507. /*
  2508. * Old kernels did not validate the value written to
  2509. * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid
  2510. * value to allow live migrating buggy or malicious guests
  2511. * originating from those kernels.
  2512. */
  2513. if (!msr->host_initiated && !page_address_valid(vcpu, data))
  2514. return 1;
  2515. svm->nested.hsave_msr = data & PAGE_MASK;
  2516. break;
  2517. case MSR_VM_CR:
  2518. return svm_set_vm_cr(vcpu, data);
  2519. case MSR_VM_IGNNE:
  2520. vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  2521. break;
  2522. case MSR_AMD64_DE_CFG: {
  2523. struct kvm_msr_entry msr_entry;
  2524. msr_entry.index = msr->index;
  2525. if (svm_get_msr_feature(&msr_entry))
  2526. return 1;
  2527. /* Check the supported bits */
  2528. if (data & ~msr_entry.data)
  2529. return 1;
  2530. /* Don't allow the guest to change a bit, #GP */
  2531. if (!msr->host_initiated && (data ^ msr_entry.data))
  2532. return 1;
  2533. svm->msr_decfg = data;
  2534. break;
  2535. }
  2536. default:
  2537. return kvm_set_msr_common(vcpu, msr);
  2538. }
  2539. return 0;
  2540. }
  2541. static int msr_interception(struct kvm_vcpu *vcpu)
  2542. {
  2543. if (to_svm(vcpu)->vmcb->control.exit_info_1)
  2544. return kvm_emulate_wrmsr(vcpu);
  2545. else
  2546. return kvm_emulate_rdmsr(vcpu);
  2547. }
  2548. static int interrupt_window_interception(struct kvm_vcpu *vcpu)
  2549. {
  2550. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2551. svm_clear_vintr(to_svm(vcpu));
  2552. /*
  2553. * If not running nested, for AVIC, the only reason to end up here is ExtINTs.
  2554. * In this case AVIC was temporarily disabled for
  2555. * requesting the IRQ window and we have to re-enable it.
  2556. *
  2557. * If running nested, still remove the VM wide AVIC inhibit to
  2558. * support case in which the interrupt window was requested when the
  2559. * vCPU was not running nested.
  2560. * All vCPUs which run still run nested, will remain to have their
  2561. * AVIC still inhibited due to per-cpu AVIC inhibition.
  2562. */
  2563. kvm_clear_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN);
  2564. ++vcpu->stat.irq_window_exits;
  2565. return 1;
  2566. }
  2567. static int pause_interception(struct kvm_vcpu *vcpu)
  2568. {
  2569. bool in_kernel;
  2570. /*
  2571. * CPL is not made available for an SEV-ES guest, therefore
  2572. * vcpu->arch.preempted_in_kernel can never be true. Just
  2573. * set in_kernel to false as well.
  2574. */
  2575. in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
  2576. grow_ple_window(vcpu);
  2577. kvm_vcpu_on_spin(vcpu, in_kernel);
  2578. return kvm_skip_emulated_instruction(vcpu);
  2579. }
  2580. static int invpcid_interception(struct kvm_vcpu *vcpu)
  2581. {
  2582. struct vcpu_svm *svm = to_svm(vcpu);
  2583. unsigned long type;
  2584. gva_t gva;
  2585. if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
  2586. kvm_queue_exception(vcpu, UD_VECTOR);
  2587. return 1;
  2588. }
  2589. /*
  2590. * For an INVPCID intercept:
  2591. * EXITINFO1 provides the linear address of the memory operand.
  2592. * EXITINFO2 provides the contents of the register operand.
  2593. */
  2594. type = svm->vmcb->control.exit_info_2;
  2595. gva = svm->vmcb->control.exit_info_1;
  2596. return kvm_handle_invpcid(vcpu, type, gva);
  2597. }
  2598. static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  2599. [SVM_EXIT_READ_CR0] = cr_interception,
  2600. [SVM_EXIT_READ_CR3] = cr_interception,
  2601. [SVM_EXIT_READ_CR4] = cr_interception,
  2602. [SVM_EXIT_READ_CR8] = cr_interception,
  2603. [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
  2604. [SVM_EXIT_WRITE_CR0] = cr_interception,
  2605. [SVM_EXIT_WRITE_CR3] = cr_interception,
  2606. [SVM_EXIT_WRITE_CR4] = cr_interception,
  2607. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2608. [SVM_EXIT_READ_DR0] = dr_interception,
  2609. [SVM_EXIT_READ_DR1] = dr_interception,
  2610. [SVM_EXIT_READ_DR2] = dr_interception,
  2611. [SVM_EXIT_READ_DR3] = dr_interception,
  2612. [SVM_EXIT_READ_DR4] = dr_interception,
  2613. [SVM_EXIT_READ_DR5] = dr_interception,
  2614. [SVM_EXIT_READ_DR6] = dr_interception,
  2615. [SVM_EXIT_READ_DR7] = dr_interception,
  2616. [SVM_EXIT_WRITE_DR0] = dr_interception,
  2617. [SVM_EXIT_WRITE_DR1] = dr_interception,
  2618. [SVM_EXIT_WRITE_DR2] = dr_interception,
  2619. [SVM_EXIT_WRITE_DR3] = dr_interception,
  2620. [SVM_EXIT_WRITE_DR4] = dr_interception,
  2621. [SVM_EXIT_WRITE_DR5] = dr_interception,
  2622. [SVM_EXIT_WRITE_DR6] = dr_interception,
  2623. [SVM_EXIT_WRITE_DR7] = dr_interception,
  2624. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2625. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2626. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2627. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2628. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2629. [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
  2630. [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
  2631. [SVM_EXIT_INTR] = intr_interception,
  2632. [SVM_EXIT_NMI] = nmi_interception,
  2633. [SVM_EXIT_SMI] = smi_interception,
  2634. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2635. [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
  2636. [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
  2637. [SVM_EXIT_IRET] = iret_interception,
  2638. [SVM_EXIT_INVD] = kvm_emulate_invd,
  2639. [SVM_EXIT_PAUSE] = pause_interception,
  2640. [SVM_EXIT_HLT] = kvm_emulate_halt,
  2641. [SVM_EXIT_INVLPG] = invlpg_interception,
  2642. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2643. [SVM_EXIT_IOIO] = io_interception,
  2644. [SVM_EXIT_MSR] = msr_interception,
  2645. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2646. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2647. [SVM_EXIT_VMRUN] = vmrun_interception,
  2648. [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
  2649. [SVM_EXIT_VMLOAD] = vmload_interception,
  2650. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2651. [SVM_EXIT_STGI] = stgi_interception,
  2652. [SVM_EXIT_CLGI] = clgi_interception,
  2653. [SVM_EXIT_SKINIT] = skinit_interception,
  2654. [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
  2655. [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
  2656. [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
  2657. [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
  2658. [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
  2659. [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
  2660. [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
  2661. [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
  2662. [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
  2663. [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
  2664. [SVM_EXIT_INVPCID] = invpcid_interception,
  2665. [SVM_EXIT_NPF] = npf_interception,
  2666. [SVM_EXIT_RSM] = rsm_interception,
  2667. [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
  2668. [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
  2669. [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
  2670. };
  2671. static void dump_vmcb(struct kvm_vcpu *vcpu)
  2672. {
  2673. struct vcpu_svm *svm = to_svm(vcpu);
  2674. struct vmcb_control_area *control = &svm->vmcb->control;
  2675. struct vmcb_save_area *save = &svm->vmcb->save;
  2676. struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
  2677. if (!dump_invalid_vmcb) {
  2678. pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
  2679. return;
  2680. }
  2681. pr_err("VMCB %p, last attempted VMRUN on CPU %d\n",
  2682. svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu);
  2683. pr_err("VMCB Control Area:\n");
  2684. pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
  2685. pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
  2686. pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
  2687. pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
  2688. pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
  2689. pr_err("%-20s%08x %08x\n", "intercepts:",
  2690. control->intercepts[INTERCEPT_WORD3],
  2691. control->intercepts[INTERCEPT_WORD4]);
  2692. pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
  2693. pr_err("%-20s%d\n", "pause filter threshold:",
  2694. control->pause_filter_thresh);
  2695. pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
  2696. pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
  2697. pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
  2698. pr_err("%-20s%d\n", "asid:", control->asid);
  2699. pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
  2700. pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
  2701. pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
  2702. pr_err("%-20s%08x\n", "int_state:", control->int_state);
  2703. pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
  2704. pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
  2705. pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
  2706. pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
  2707. pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
  2708. pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
  2709. pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
  2710. pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
  2711. pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
  2712. pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
  2713. pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
  2714. pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
  2715. pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
  2716. pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
  2717. pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
  2718. pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
  2719. pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
  2720. pr_err("VMCB State Save Area:\n");
  2721. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2722. "es:",
  2723. save->es.selector, save->es.attrib,
  2724. save->es.limit, save->es.base);
  2725. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2726. "cs:",
  2727. save->cs.selector, save->cs.attrib,
  2728. save->cs.limit, save->cs.base);
  2729. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2730. "ss:",
  2731. save->ss.selector, save->ss.attrib,
  2732. save->ss.limit, save->ss.base);
  2733. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2734. "ds:",
  2735. save->ds.selector, save->ds.attrib,
  2736. save->ds.limit, save->ds.base);
  2737. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2738. "fs:",
  2739. save01->fs.selector, save01->fs.attrib,
  2740. save01->fs.limit, save01->fs.base);
  2741. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2742. "gs:",
  2743. save01->gs.selector, save01->gs.attrib,
  2744. save01->gs.limit, save01->gs.base);
  2745. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2746. "gdtr:",
  2747. save->gdtr.selector, save->gdtr.attrib,
  2748. save->gdtr.limit, save->gdtr.base);
  2749. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2750. "ldtr:",
  2751. save01->ldtr.selector, save01->ldtr.attrib,
  2752. save01->ldtr.limit, save01->ldtr.base);
  2753. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2754. "idtr:",
  2755. save->idtr.selector, save->idtr.attrib,
  2756. save->idtr.limit, save->idtr.base);
  2757. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2758. "tr:",
  2759. save01->tr.selector, save01->tr.attrib,
  2760. save01->tr.limit, save01->tr.base);
  2761. pr_err("vmpl: %d cpl: %d efer: %016llx\n",
  2762. save->vmpl, save->cpl, save->efer);
  2763. pr_err("%-15s %016llx %-13s %016llx\n",
  2764. "cr0:", save->cr0, "cr2:", save->cr2);
  2765. pr_err("%-15s %016llx %-13s %016llx\n",
  2766. "cr3:", save->cr3, "cr4:", save->cr4);
  2767. pr_err("%-15s %016llx %-13s %016llx\n",
  2768. "dr6:", save->dr6, "dr7:", save->dr7);
  2769. pr_err("%-15s %016llx %-13s %016llx\n",
  2770. "rip:", save->rip, "rflags:", save->rflags);
  2771. pr_err("%-15s %016llx %-13s %016llx\n",
  2772. "rsp:", save->rsp, "rax:", save->rax);
  2773. pr_err("%-15s %016llx %-13s %016llx\n",
  2774. "star:", save01->star, "lstar:", save01->lstar);
  2775. pr_err("%-15s %016llx %-13s %016llx\n",
  2776. "cstar:", save01->cstar, "sfmask:", save01->sfmask);
  2777. pr_err("%-15s %016llx %-13s %016llx\n",
  2778. "kernel_gs_base:", save01->kernel_gs_base,
  2779. "sysenter_cs:", save01->sysenter_cs);
  2780. pr_err("%-15s %016llx %-13s %016llx\n",
  2781. "sysenter_esp:", save01->sysenter_esp,
  2782. "sysenter_eip:", save01->sysenter_eip);
  2783. pr_err("%-15s %016llx %-13s %016llx\n",
  2784. "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
  2785. pr_err("%-15s %016llx %-13s %016llx\n",
  2786. "br_from:", save->br_from, "br_to:", save->br_to);
  2787. pr_err("%-15s %016llx %-13s %016llx\n",
  2788. "excp_from:", save->last_excp_from,
  2789. "excp_to:", save->last_excp_to);
  2790. }
  2791. static bool svm_check_exit_valid(u64 exit_code)
  2792. {
  2793. return (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
  2794. svm_exit_handlers[exit_code]);
  2795. }
  2796. static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
  2797. {
  2798. vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
  2799. dump_vmcb(vcpu);
  2800. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2801. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
  2802. vcpu->run->internal.ndata = 2;
  2803. vcpu->run->internal.data[0] = exit_code;
  2804. vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
  2805. return 0;
  2806. }
  2807. int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
  2808. {
  2809. if (!svm_check_exit_valid(exit_code))
  2810. return svm_handle_invalid_exit(vcpu, exit_code);
  2811. #ifdef CONFIG_RETPOLINE
  2812. if (exit_code == SVM_EXIT_MSR)
  2813. return msr_interception(vcpu);
  2814. else if (exit_code == SVM_EXIT_VINTR)
  2815. return interrupt_window_interception(vcpu);
  2816. else if (exit_code == SVM_EXIT_INTR)
  2817. return intr_interception(vcpu);
  2818. else if (exit_code == SVM_EXIT_HLT)
  2819. return kvm_emulate_halt(vcpu);
  2820. else if (exit_code == SVM_EXIT_NPF)
  2821. return npf_interception(vcpu);
  2822. #endif
  2823. return svm_exit_handlers[exit_code](vcpu);
  2824. }
  2825. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
  2826. u64 *info1, u64 *info2,
  2827. u32 *intr_info, u32 *error_code)
  2828. {
  2829. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  2830. *reason = control->exit_code;
  2831. *info1 = control->exit_info_1;
  2832. *info2 = control->exit_info_2;
  2833. *intr_info = control->exit_int_info;
  2834. if ((*intr_info & SVM_EXITINTINFO_VALID) &&
  2835. (*intr_info & SVM_EXITINTINFO_VALID_ERR))
  2836. *error_code = control->exit_int_info_err;
  2837. else
  2838. *error_code = 0;
  2839. }
  2840. static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
  2841. {
  2842. struct vcpu_svm *svm = to_svm(vcpu);
  2843. struct kvm_run *kvm_run = vcpu->run;
  2844. u32 exit_code = svm->vmcb->control.exit_code;
  2845. trace_kvm_exit(vcpu, KVM_ISA_SVM);
  2846. /* SEV-ES guests must use the CR write traps to track CR registers. */
  2847. if (!sev_es_guest(vcpu->kvm)) {
  2848. if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
  2849. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2850. if (npt_enabled)
  2851. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2852. }
  2853. if (is_guest_mode(vcpu)) {
  2854. int vmexit;
  2855. trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM);
  2856. vmexit = nested_svm_exit_special(svm);
  2857. if (vmexit == NESTED_EXIT_CONTINUE)
  2858. vmexit = nested_svm_exit_handled(svm);
  2859. if (vmexit == NESTED_EXIT_DONE)
  2860. return 1;
  2861. }
  2862. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2863. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2864. kvm_run->fail_entry.hardware_entry_failure_reason
  2865. = svm->vmcb->control.exit_code;
  2866. kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
  2867. dump_vmcb(vcpu);
  2868. return 0;
  2869. }
  2870. if (exit_fastpath != EXIT_FASTPATH_NONE)
  2871. return 1;
  2872. return svm_invoke_exit_handler(vcpu, exit_code);
  2873. }
  2874. static void reload_tss(struct kvm_vcpu *vcpu)
  2875. {
  2876. struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);
  2877. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2878. load_TR_desc();
  2879. }
  2880. static void pre_svm_run(struct kvm_vcpu *vcpu)
  2881. {
  2882. struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);
  2883. struct vcpu_svm *svm = to_svm(vcpu);
  2884. /*
  2885. * If the previous vmrun of the vmcb occurred on a different physical
  2886. * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
  2887. * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
  2888. */
  2889. if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
  2890. svm->current_vmcb->asid_generation = 0;
  2891. vmcb_mark_all_dirty(svm->vmcb);
  2892. svm->current_vmcb->cpu = vcpu->cpu;
  2893. }
  2894. if (sev_guest(vcpu->kvm))
  2895. return pre_sev_run(svm, vcpu->cpu);
  2896. /* FIXME: handle wraparound of asid_generation */
  2897. if (svm->current_vmcb->asid_generation != sd->asid_generation)
  2898. new_asid(svm, sd);
  2899. }
  2900. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2901. {
  2902. struct vcpu_svm *svm = to_svm(vcpu);
  2903. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2904. if (svm->nmi_l1_to_l2)
  2905. return;
  2906. vcpu->arch.hflags |= HF_NMI_MASK;
  2907. if (!sev_es_guest(vcpu->kvm))
  2908. svm_set_intercept(svm, INTERCEPT_IRET);
  2909. ++vcpu->stat.nmi_injections;
  2910. }
  2911. static void svm_inject_irq(struct kvm_vcpu *vcpu, bool reinjected)
  2912. {
  2913. struct vcpu_svm *svm = to_svm(vcpu);
  2914. u32 type;
  2915. if (vcpu->arch.interrupt.soft) {
  2916. if (svm_update_soft_interrupt_rip(vcpu))
  2917. return;
  2918. type = SVM_EVTINJ_TYPE_SOFT;
  2919. } else {
  2920. type = SVM_EVTINJ_TYPE_INTR;
  2921. }
  2922. trace_kvm_inj_virq(vcpu->arch.interrupt.nr,
  2923. vcpu->arch.interrupt.soft, reinjected);
  2924. ++vcpu->stat.irq_injections;
  2925. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2926. SVM_EVTINJ_VALID | type;
  2927. }
  2928. void svm_complete_interrupt_delivery(struct kvm_vcpu *vcpu, int delivery_mode,
  2929. int trig_mode, int vector)
  2930. {
  2931. /*
  2932. * apic->apicv_active must be read after vcpu->mode.
  2933. * Pairs with smp_store_release in vcpu_enter_guest.
  2934. */
  2935. bool in_guest_mode = (smp_load_acquire(&vcpu->mode) == IN_GUEST_MODE);
  2936. /* Note, this is called iff the local APIC is in-kernel. */
  2937. if (!READ_ONCE(vcpu->arch.apic->apicv_active)) {
  2938. /* Process the interrupt via kvm_check_and_inject_events(). */
  2939. kvm_make_request(KVM_REQ_EVENT, vcpu);
  2940. kvm_vcpu_kick(vcpu);
  2941. return;
  2942. }
  2943. trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode, trig_mode, vector);
  2944. if (in_guest_mode) {
  2945. /*
  2946. * Signal the doorbell to tell hardware to inject the IRQ. If
  2947. * the vCPU exits the guest before the doorbell chimes, hardware
  2948. * will automatically process AVIC interrupts at the next VMRUN.
  2949. */
  2950. avic_ring_doorbell(vcpu);
  2951. } else {
  2952. /*
  2953. * Wake the vCPU if it was blocking. KVM will then detect the
  2954. * pending IRQ when checking if the vCPU has a wake event.
  2955. */
  2956. kvm_vcpu_wake_up(vcpu);
  2957. }
  2958. }
  2959. static void svm_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
  2960. int trig_mode, int vector)
  2961. {
  2962. kvm_lapic_set_irr(vector, apic);
  2963. /*
  2964. * Pairs with the smp_mb_*() after setting vcpu->guest_mode in
  2965. * vcpu_enter_guest() to ensure the write to the vIRR is ordered before
  2966. * the read of guest_mode. This guarantees that either VMRUN will see
  2967. * and process the new vIRR entry, or that svm_complete_interrupt_delivery
  2968. * will signal the doorbell if the CPU has already entered the guest.
  2969. */
  2970. smp_mb__after_atomic();
  2971. svm_complete_interrupt_delivery(apic->vcpu, delivery_mode, trig_mode, vector);
  2972. }
  2973. static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2974. {
  2975. struct vcpu_svm *svm = to_svm(vcpu);
  2976. /*
  2977. * SEV-ES guests must always keep the CR intercepts cleared. CR
  2978. * tracking is done using the CR write traps.
  2979. */
  2980. if (sev_es_guest(vcpu->kvm))
  2981. return;
  2982. if (nested_svm_virtualize_tpr(vcpu))
  2983. return;
  2984. svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
  2985. if (irr == -1)
  2986. return;
  2987. if (tpr >= irr)
  2988. svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
  2989. }
  2990. bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
  2991. {
  2992. struct vcpu_svm *svm = to_svm(vcpu);
  2993. struct vmcb *vmcb = svm->vmcb;
  2994. bool ret;
  2995. if (!gif_set(svm))
  2996. return true;
  2997. if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
  2998. return false;
  2999. ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
  3000. (vcpu->arch.hflags & HF_NMI_MASK);
  3001. return ret;
  3002. }
  3003. static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
  3004. {
  3005. struct vcpu_svm *svm = to_svm(vcpu);
  3006. if (svm->nested.nested_run_pending)
  3007. return -EBUSY;
  3008. if (svm_nmi_blocked(vcpu))
  3009. return 0;
  3010. /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
  3011. if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
  3012. return -EBUSY;
  3013. return 1;
  3014. }
  3015. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  3016. {
  3017. return !!(vcpu->arch.hflags & HF_NMI_MASK);
  3018. }
  3019. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  3020. {
  3021. struct vcpu_svm *svm = to_svm(vcpu);
  3022. if (masked) {
  3023. vcpu->arch.hflags |= HF_NMI_MASK;
  3024. if (!sev_es_guest(vcpu->kvm))
  3025. svm_set_intercept(svm, INTERCEPT_IRET);
  3026. } else {
  3027. vcpu->arch.hflags &= ~HF_NMI_MASK;
  3028. if (!sev_es_guest(vcpu->kvm))
  3029. svm_clr_intercept(svm, INTERCEPT_IRET);
  3030. }
  3031. }
  3032. bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
  3033. {
  3034. struct vcpu_svm *svm = to_svm(vcpu);
  3035. struct vmcb *vmcb = svm->vmcb;
  3036. if (!gif_set(svm))
  3037. return true;
  3038. if (is_guest_mode(vcpu)) {
  3039. /* As long as interrupts are being delivered... */
  3040. if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
  3041. ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
  3042. : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
  3043. return true;
  3044. /* ... vmexits aren't blocked by the interrupt shadow */
  3045. if (nested_exit_on_intr(svm))
  3046. return false;
  3047. } else {
  3048. if (!svm_get_if_flag(vcpu))
  3049. return true;
  3050. }
  3051. return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
  3052. }
  3053. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
  3054. {
  3055. struct vcpu_svm *svm = to_svm(vcpu);
  3056. if (svm->nested.nested_run_pending)
  3057. return -EBUSY;
  3058. if (svm_interrupt_blocked(vcpu))
  3059. return 0;
  3060. /*
  3061. * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
  3062. * e.g. if the IRQ arrived asynchronously after checking nested events.
  3063. */
  3064. if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
  3065. return -EBUSY;
  3066. return 1;
  3067. }
  3068. static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
  3069. {
  3070. struct vcpu_svm *svm = to_svm(vcpu);
  3071. /*
  3072. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  3073. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  3074. * get that intercept, this function will be called again though and
  3075. * we'll get the vintr intercept. However, if the vGIF feature is
  3076. * enabled, the STGI interception will not occur. Enable the irq
  3077. * window under the assumption that the hardware will set the GIF.
  3078. */
  3079. if (vgif || gif_set(svm)) {
  3080. /*
  3081. * IRQ window is not needed when AVIC is enabled,
  3082. * unless we have pending ExtINT since it cannot be injected
  3083. * via AVIC. In such case, KVM needs to temporarily disable AVIC,
  3084. * and fallback to injecting IRQ via V_IRQ.
  3085. *
  3086. * If running nested, AVIC is already locally inhibited
  3087. * on this vCPU, therefore there is no need to request
  3088. * the VM wide AVIC inhibition.
  3089. */
  3090. if (!is_guest_mode(vcpu))
  3091. kvm_set_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN);
  3092. svm_set_vintr(svm);
  3093. }
  3094. }
  3095. static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
  3096. {
  3097. struct vcpu_svm *svm = to_svm(vcpu);
  3098. if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
  3099. return; /* IRET will cause a vm exit */
  3100. if (!gif_set(svm)) {
  3101. if (vgif)
  3102. svm_set_intercept(svm, INTERCEPT_STGI);
  3103. return; /* STGI will cause a vm exit */
  3104. }
  3105. /*
  3106. * Something prevents NMI from been injected. Single step over possible
  3107. * problem (IRET or exception injection or interrupt shadow)
  3108. */
  3109. svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
  3110. svm->nmi_singlestep = true;
  3111. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  3112. }
  3113. static void svm_flush_tlb_asid(struct kvm_vcpu *vcpu)
  3114. {
  3115. struct vcpu_svm *svm = to_svm(vcpu);
  3116. /*
  3117. * Flush only the current ASID even if the TLB flush was invoked via
  3118. * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
  3119. * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
  3120. * unconditionally does a TLB flush on both nested VM-Enter and nested
  3121. * VM-Exit (via kvm_mmu_reset_context()).
  3122. */
  3123. if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
  3124. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
  3125. else
  3126. svm->current_vmcb->asid_generation--;
  3127. }
  3128. static void svm_flush_tlb_current(struct kvm_vcpu *vcpu)
  3129. {
  3130. hpa_t root_tdp = vcpu->arch.mmu->root.hpa;
  3131. /*
  3132. * When running on Hyper-V with EnlightenedNptTlb enabled, explicitly
  3133. * flush the NPT mappings via hypercall as flushing the ASID only
  3134. * affects virtual to physical mappings, it does not invalidate guest
  3135. * physical to host physical mappings.
  3136. */
  3137. if (svm_hv_is_enlightened_tlb_enabled(vcpu) && VALID_PAGE(root_tdp))
  3138. hyperv_flush_guest_mapping(root_tdp);
  3139. svm_flush_tlb_asid(vcpu);
  3140. }
  3141. static void svm_flush_tlb_all(struct kvm_vcpu *vcpu)
  3142. {
  3143. /*
  3144. * When running on Hyper-V with EnlightenedNptTlb enabled, remote TLB
  3145. * flushes should be routed to hv_remote_flush_tlb() without requesting
  3146. * a "regular" remote flush. Reaching this point means either there's
  3147. * a KVM bug or a prior hv_remote_flush_tlb() call failed, both of
  3148. * which might be fatal to the guest. Yell, but try to recover.
  3149. */
  3150. if (WARN_ON_ONCE(svm_hv_is_enlightened_tlb_enabled(vcpu)))
  3151. hv_remote_flush_tlb(vcpu->kvm);
  3152. svm_flush_tlb_asid(vcpu);
  3153. }
  3154. static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
  3155. {
  3156. struct vcpu_svm *svm = to_svm(vcpu);
  3157. invlpga(gva, svm->vmcb->control.asid);
  3158. }
  3159. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  3160. {
  3161. struct vcpu_svm *svm = to_svm(vcpu);
  3162. if (nested_svm_virtualize_tpr(vcpu))
  3163. return;
  3164. if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
  3165. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  3166. kvm_set_cr8(vcpu, cr8);
  3167. }
  3168. }
  3169. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  3170. {
  3171. struct vcpu_svm *svm = to_svm(vcpu);
  3172. u64 cr8;
  3173. if (nested_svm_virtualize_tpr(vcpu) ||
  3174. kvm_vcpu_apicv_active(vcpu))
  3175. return;
  3176. cr8 = kvm_get_cr8(vcpu);
  3177. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  3178. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  3179. }
  3180. static void svm_complete_soft_interrupt(struct kvm_vcpu *vcpu, u8 vector,
  3181. int type)
  3182. {
  3183. bool is_exception = (type == SVM_EXITINTINFO_TYPE_EXEPT);
  3184. bool is_soft = (type == SVM_EXITINTINFO_TYPE_SOFT);
  3185. struct vcpu_svm *svm = to_svm(vcpu);
  3186. /*
  3187. * If NRIPS is enabled, KVM must snapshot the pre-VMRUN next_rip that's
  3188. * associated with the original soft exception/interrupt. next_rip is
  3189. * cleared on all exits that can occur while vectoring an event, so KVM
  3190. * needs to manually set next_rip for re-injection. Unlike the !nrips
  3191. * case below, this needs to be done if and only if KVM is re-injecting
  3192. * the same event, i.e. if the event is a soft exception/interrupt,
  3193. * otherwise next_rip is unused on VMRUN.
  3194. */
  3195. if (nrips && (is_soft || (is_exception && kvm_exception_is_soft(vector))) &&
  3196. kvm_is_linear_rip(vcpu, svm->soft_int_old_rip + svm->soft_int_csbase))
  3197. svm->vmcb->control.next_rip = svm->soft_int_next_rip;
  3198. /*
  3199. * If NRIPS isn't enabled, KVM must manually advance RIP prior to
  3200. * injecting the soft exception/interrupt. That advancement needs to
  3201. * be unwound if vectoring didn't complete. Note, the new event may
  3202. * not be the injected event, e.g. if KVM injected an INTn, the INTn
  3203. * hit a #NP in the guest, and the #NP encountered a #PF, the #NP will
  3204. * be the reported vectored event, but RIP still needs to be unwound.
  3205. */
  3206. else if (!nrips && (is_soft || is_exception) &&
  3207. kvm_is_linear_rip(vcpu, svm->soft_int_next_rip + svm->soft_int_csbase))
  3208. kvm_rip_write(vcpu, svm->soft_int_old_rip);
  3209. }
  3210. static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
  3211. {
  3212. struct vcpu_svm *svm = to_svm(vcpu);
  3213. u8 vector;
  3214. int type;
  3215. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  3216. bool nmi_l1_to_l2 = svm->nmi_l1_to_l2;
  3217. bool soft_int_injected = svm->soft_int_injected;
  3218. svm->nmi_l1_to_l2 = false;
  3219. svm->soft_int_injected = false;
  3220. /*
  3221. * If we've made progress since setting HF_IRET_MASK, we've
  3222. * executed an IRET and can allow NMI injection.
  3223. */
  3224. if ((vcpu->arch.hflags & HF_IRET_MASK) &&
  3225. (sev_es_guest(vcpu->kvm) ||
  3226. kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
  3227. vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  3228. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3229. }
  3230. vcpu->arch.nmi_injected = false;
  3231. kvm_clear_exception_queue(vcpu);
  3232. kvm_clear_interrupt_queue(vcpu);
  3233. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  3234. return;
  3235. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3236. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  3237. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  3238. if (soft_int_injected)
  3239. svm_complete_soft_interrupt(vcpu, vector, type);
  3240. switch (type) {
  3241. case SVM_EXITINTINFO_TYPE_NMI:
  3242. vcpu->arch.nmi_injected = true;
  3243. svm->nmi_l1_to_l2 = nmi_l1_to_l2;
  3244. break;
  3245. case SVM_EXITINTINFO_TYPE_EXEPT:
  3246. /*
  3247. * Never re-inject a #VC exception.
  3248. */
  3249. if (vector == X86_TRAP_VC)
  3250. break;
  3251. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  3252. u32 err = svm->vmcb->control.exit_int_info_err;
  3253. kvm_requeue_exception_e(vcpu, vector, err);
  3254. } else
  3255. kvm_requeue_exception(vcpu, vector);
  3256. break;
  3257. case SVM_EXITINTINFO_TYPE_INTR:
  3258. kvm_queue_interrupt(vcpu, vector, false);
  3259. break;
  3260. case SVM_EXITINTINFO_TYPE_SOFT:
  3261. kvm_queue_interrupt(vcpu, vector, true);
  3262. break;
  3263. default:
  3264. break;
  3265. }
  3266. }
  3267. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  3268. {
  3269. struct vcpu_svm *svm = to_svm(vcpu);
  3270. struct vmcb_control_area *control = &svm->vmcb->control;
  3271. control->exit_int_info = control->event_inj;
  3272. control->exit_int_info_err = control->event_inj_err;
  3273. control->event_inj = 0;
  3274. svm_complete_interrupts(vcpu);
  3275. }
  3276. static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu)
  3277. {
  3278. return 1;
  3279. }
  3280. static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
  3281. {
  3282. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  3283. /*
  3284. * Note, the next RIP must be provided as SRCU isn't held, i.e. KVM
  3285. * can't read guest memory (dereference memslots) to decode the WRMSR.
  3286. */
  3287. if (control->exit_code == SVM_EXIT_MSR && control->exit_info_1 &&
  3288. nrips && control->next_rip)
  3289. return handle_fastpath_set_msr_irqoff(vcpu);
  3290. return EXIT_FASTPATH_NONE;
  3291. }
  3292. static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, bool spec_ctrl_intercepted)
  3293. {
  3294. struct vcpu_svm *svm = to_svm(vcpu);
  3295. guest_state_enter_irqoff();
  3296. amd_clear_divider();
  3297. if (sev_es_guest(vcpu->kvm))
  3298. __svm_sev_es_vcpu_run(svm, spec_ctrl_intercepted);
  3299. else
  3300. __svm_vcpu_run(svm, spec_ctrl_intercepted);
  3301. guest_state_exit_irqoff();
  3302. }
  3303. static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
  3304. {
  3305. struct vcpu_svm *svm = to_svm(vcpu);
  3306. bool spec_ctrl_intercepted = msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL);
  3307. trace_kvm_entry(vcpu);
  3308. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  3309. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  3310. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  3311. /*
  3312. * Disable singlestep if we're injecting an interrupt/exception.
  3313. * We don't want our modified rflags to be pushed on the stack where
  3314. * we might not be able to easily reset them if we disabled NMI
  3315. * singlestep later.
  3316. */
  3317. if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
  3318. /*
  3319. * Event injection happens before external interrupts cause a
  3320. * vmexit and interrupts are disabled here, so smp_send_reschedule
  3321. * is enough to force an immediate vmexit.
  3322. */
  3323. disable_nmi_singlestep(svm);
  3324. smp_send_reschedule(vcpu->cpu);
  3325. }
  3326. pre_svm_run(vcpu);
  3327. sync_lapic_to_cr8(vcpu);
  3328. if (unlikely(svm->asid != svm->vmcb->control.asid)) {
  3329. svm->vmcb->control.asid = svm->asid;
  3330. vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
  3331. }
  3332. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  3333. svm_hv_update_vp_id(svm->vmcb, vcpu);
  3334. /*
  3335. * Run with all-zero DR6 unless needed, so that we can get the exact cause
  3336. * of a #DB.
  3337. */
  3338. if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
  3339. svm_set_dr6(svm, vcpu->arch.dr6);
  3340. else
  3341. svm_set_dr6(svm, DR6_ACTIVE_LOW);
  3342. clgi();
  3343. kvm_load_guest_xsave_state(vcpu);
  3344. kvm_wait_lapic_expire(vcpu);
  3345. /*
  3346. * If this vCPU has touched SPEC_CTRL, restore the guest's value if
  3347. * it's non-zero. Since vmentry is serialising on affected CPUs, there
  3348. * is no need to worry about the conditional branch over the wrmsr
  3349. * being speculatively taken.
  3350. */
  3351. if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
  3352. x86_spec_ctrl_set_guest(svm->virt_spec_ctrl);
  3353. svm_vcpu_enter_exit(vcpu, spec_ctrl_intercepted);
  3354. if (!sev_es_guest(vcpu->kvm))
  3355. reload_tss(vcpu);
  3356. if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
  3357. x86_spec_ctrl_restore_host(svm->virt_spec_ctrl);
  3358. if (!sev_es_guest(vcpu->kvm)) {
  3359. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  3360. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  3361. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  3362. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  3363. }
  3364. vcpu->arch.regs_dirty = 0;
  3365. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  3366. kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
  3367. kvm_load_host_xsave_state(vcpu);
  3368. stgi();
  3369. /* Any pending NMI will happen here */
  3370. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  3371. kvm_after_interrupt(vcpu);
  3372. sync_cr8_to_lapic(vcpu);
  3373. svm->next_rip = 0;
  3374. if (is_guest_mode(vcpu)) {
  3375. nested_sync_control_from_vmcb02(svm);
  3376. /* Track VMRUNs that have made past consistency checking */
  3377. if (svm->nested.nested_run_pending &&
  3378. svm->vmcb->control.exit_code != SVM_EXIT_ERR)
  3379. ++vcpu->stat.nested_run;
  3380. svm->nested.nested_run_pending = 0;
  3381. }
  3382. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  3383. vmcb_mark_all_clean(svm->vmcb);
  3384. /* if exit due to PF check for async PF */
  3385. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  3386. vcpu->arch.apf.host_apf_flags =
  3387. kvm_read_and_reset_apf_flags();
  3388. vcpu->arch.regs_avail &= ~SVM_REGS_LAZY_LOAD_SET;
  3389. /*
  3390. * We need to handle MC intercepts here before the vcpu has a chance to
  3391. * change the physical cpu
  3392. */
  3393. if (unlikely(svm->vmcb->control.exit_code ==
  3394. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  3395. svm_handle_mce(vcpu);
  3396. svm_complete_interrupts(vcpu);
  3397. if (is_guest_mode(vcpu))
  3398. return EXIT_FASTPATH_NONE;
  3399. return svm_exit_handlers_fastpath(vcpu);
  3400. }
  3401. static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
  3402. int root_level)
  3403. {
  3404. struct vcpu_svm *svm = to_svm(vcpu);
  3405. unsigned long cr3;
  3406. if (npt_enabled) {
  3407. svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
  3408. vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
  3409. hv_track_root_tdp(vcpu, root_hpa);
  3410. cr3 = vcpu->arch.cr3;
  3411. } else if (root_level >= PT64_ROOT_4LEVEL) {
  3412. cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
  3413. } else {
  3414. /* PCID in the guest should be impossible with a 32-bit MMU. */
  3415. WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
  3416. cr3 = root_hpa;
  3417. }
  3418. svm->vmcb->save.cr3 = cr3;
  3419. vmcb_mark_dirty(svm->vmcb, VMCB_CR);
  3420. }
  3421. static int is_disabled(void)
  3422. {
  3423. u64 vm_cr;
  3424. rdmsrl(MSR_VM_CR, vm_cr);
  3425. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  3426. return 1;
  3427. return 0;
  3428. }
  3429. static void
  3430. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  3431. {
  3432. /*
  3433. * Patch in the VMMCALL instruction:
  3434. */
  3435. hypercall[0] = 0x0f;
  3436. hypercall[1] = 0x01;
  3437. hypercall[2] = 0xd9;
  3438. }
  3439. static int __init svm_check_processor_compat(void)
  3440. {
  3441. return 0;
  3442. }
  3443. /*
  3444. * The kvm parameter can be NULL (module initialization, or invocation before
  3445. * VM creation). Be sure to check the kvm parameter before using it.
  3446. */
  3447. static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
  3448. {
  3449. switch (index) {
  3450. case MSR_IA32_MCG_EXT_CTL:
  3451. case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
  3452. return false;
  3453. case MSR_IA32_SMBASE:
  3454. /* SEV-ES guests do not support SMM, so report false */
  3455. if (kvm && sev_es_guest(kvm))
  3456. return false;
  3457. break;
  3458. default:
  3459. break;
  3460. }
  3461. return true;
  3462. }
  3463. static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
  3464. {
  3465. struct vcpu_svm *svm = to_svm(vcpu);
  3466. vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
  3467. boot_cpu_has(X86_FEATURE_XSAVE) &&
  3468. boot_cpu_has(X86_FEATURE_XSAVES);
  3469. /* Update nrips enabled cache */
  3470. svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
  3471. guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
  3472. svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR);
  3473. svm->lbrv_enabled = lbrv && guest_cpuid_has(vcpu, X86_FEATURE_LBRV);
  3474. svm->v_vmload_vmsave_enabled = vls && guest_cpuid_has(vcpu, X86_FEATURE_V_VMSAVE_VMLOAD);
  3475. svm->pause_filter_enabled = kvm_cpu_cap_has(X86_FEATURE_PAUSEFILTER) &&
  3476. guest_cpuid_has(vcpu, X86_FEATURE_PAUSEFILTER);
  3477. svm->pause_threshold_enabled = kvm_cpu_cap_has(X86_FEATURE_PFTHRESHOLD) &&
  3478. guest_cpuid_has(vcpu, X86_FEATURE_PFTHRESHOLD);
  3479. svm->vgif_enabled = vgif && guest_cpuid_has(vcpu, X86_FEATURE_VGIF);
  3480. svm_recalc_instruction_intercepts(vcpu, svm);
  3481. if (sev_guest(vcpu->kvm))
  3482. sev_vcpu_after_set_cpuid(svm);
  3483. init_vmcb_after_set_cpuid(vcpu);
  3484. }
  3485. static bool svm_has_wbinvd_exit(void)
  3486. {
  3487. return true;
  3488. }
  3489. #define PRE_EX(exit) { .exit_code = (exit), \
  3490. .stage = X86_ICPT_PRE_EXCEPT, }
  3491. #define POST_EX(exit) { .exit_code = (exit), \
  3492. .stage = X86_ICPT_POST_EXCEPT, }
  3493. #define POST_MEM(exit) { .exit_code = (exit), \
  3494. .stage = X86_ICPT_POST_MEMACCESS, }
  3495. static const struct __x86_intercept {
  3496. u32 exit_code;
  3497. enum x86_intercept_stage stage;
  3498. } x86_intercept_map[] = {
  3499. [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
  3500. [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
  3501. [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
  3502. [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
  3503. [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
  3504. [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
  3505. [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
  3506. [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
  3507. [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
  3508. [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
  3509. [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
  3510. [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
  3511. [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
  3512. [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
  3513. [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
  3514. [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
  3515. [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
  3516. [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
  3517. [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
  3518. [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
  3519. [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
  3520. [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
  3521. [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
  3522. [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
  3523. [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
  3524. [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
  3525. [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
  3526. [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
  3527. [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
  3528. [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
  3529. [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
  3530. [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
  3531. [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
  3532. [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
  3533. [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
  3534. [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
  3535. [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
  3536. [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
  3537. [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
  3538. [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
  3539. [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
  3540. [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
  3541. [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
  3542. [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
  3543. [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
  3544. [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
  3545. [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
  3546. };
  3547. #undef PRE_EX
  3548. #undef POST_EX
  3549. #undef POST_MEM
  3550. static int svm_check_intercept(struct kvm_vcpu *vcpu,
  3551. struct x86_instruction_info *info,
  3552. enum x86_intercept_stage stage,
  3553. struct x86_exception *exception)
  3554. {
  3555. struct vcpu_svm *svm = to_svm(vcpu);
  3556. int vmexit, ret = X86EMUL_CONTINUE;
  3557. struct __x86_intercept icpt_info;
  3558. struct vmcb *vmcb = svm->vmcb;
  3559. if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
  3560. goto out;
  3561. icpt_info = x86_intercept_map[info->intercept];
  3562. if (stage != icpt_info.stage)
  3563. goto out;
  3564. switch (icpt_info.exit_code) {
  3565. case SVM_EXIT_READ_CR0:
  3566. if (info->intercept == x86_intercept_cr_read)
  3567. icpt_info.exit_code += info->modrm_reg;
  3568. break;
  3569. case SVM_EXIT_WRITE_CR0: {
  3570. unsigned long cr0, val;
  3571. if (info->intercept == x86_intercept_cr_write)
  3572. icpt_info.exit_code += info->modrm_reg;
  3573. if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
  3574. info->intercept == x86_intercept_clts)
  3575. break;
  3576. if (!(vmcb12_is_intercept(&svm->nested.ctl,
  3577. INTERCEPT_SELECTIVE_CR0)))
  3578. break;
  3579. cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
  3580. val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
  3581. if (info->intercept == x86_intercept_lmsw) {
  3582. cr0 &= 0xfUL;
  3583. val &= 0xfUL;
  3584. /* lmsw can't clear PE - catch this here */
  3585. if (cr0 & X86_CR0_PE)
  3586. val |= X86_CR0_PE;
  3587. }
  3588. if (cr0 ^ val)
  3589. icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  3590. break;
  3591. }
  3592. case SVM_EXIT_READ_DR0:
  3593. case SVM_EXIT_WRITE_DR0:
  3594. icpt_info.exit_code += info->modrm_reg;
  3595. break;
  3596. case SVM_EXIT_MSR:
  3597. if (info->intercept == x86_intercept_wrmsr)
  3598. vmcb->control.exit_info_1 = 1;
  3599. else
  3600. vmcb->control.exit_info_1 = 0;
  3601. break;
  3602. case SVM_EXIT_PAUSE:
  3603. /*
  3604. * We get this for NOP only, but pause
  3605. * is rep not, check this here
  3606. */
  3607. if (info->rep_prefix != REPE_PREFIX)
  3608. goto out;
  3609. break;
  3610. case SVM_EXIT_IOIO: {
  3611. u64 exit_info;
  3612. u32 bytes;
  3613. if (info->intercept == x86_intercept_in ||
  3614. info->intercept == x86_intercept_ins) {
  3615. exit_info = ((info->src_val & 0xffff) << 16) |
  3616. SVM_IOIO_TYPE_MASK;
  3617. bytes = info->dst_bytes;
  3618. } else {
  3619. exit_info = (info->dst_val & 0xffff) << 16;
  3620. bytes = info->src_bytes;
  3621. }
  3622. if (info->intercept == x86_intercept_outs ||
  3623. info->intercept == x86_intercept_ins)
  3624. exit_info |= SVM_IOIO_STR_MASK;
  3625. if (info->rep_prefix)
  3626. exit_info |= SVM_IOIO_REP_MASK;
  3627. bytes = min(bytes, 4u);
  3628. exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
  3629. exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
  3630. vmcb->control.exit_info_1 = exit_info;
  3631. vmcb->control.exit_info_2 = info->next_rip;
  3632. break;
  3633. }
  3634. default:
  3635. break;
  3636. }
  3637. /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
  3638. if (static_cpu_has(X86_FEATURE_NRIPS))
  3639. vmcb->control.next_rip = info->next_rip;
  3640. vmcb->control.exit_code = icpt_info.exit_code;
  3641. vmexit = nested_svm_exit_handled(svm);
  3642. ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
  3643. : X86EMUL_CONTINUE;
  3644. out:
  3645. return ret;
  3646. }
  3647. static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
  3648. {
  3649. if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR)
  3650. vcpu->arch.at_instruction_boundary = true;
  3651. }
  3652. static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
  3653. {
  3654. if (!kvm_pause_in_guest(vcpu->kvm))
  3655. shrink_ple_window(vcpu);
  3656. }
  3657. static void svm_setup_mce(struct kvm_vcpu *vcpu)
  3658. {
  3659. /* [63:9] are reserved. */
  3660. vcpu->arch.mcg_cap &= 0x1ff;
  3661. }
  3662. bool svm_smi_blocked(struct kvm_vcpu *vcpu)
  3663. {
  3664. struct vcpu_svm *svm = to_svm(vcpu);
  3665. /* Per APM Vol.2 15.22.2 "Response to SMI" */
  3666. if (!gif_set(svm))
  3667. return true;
  3668. return is_smm(vcpu);
  3669. }
  3670. static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
  3671. {
  3672. struct vcpu_svm *svm = to_svm(vcpu);
  3673. if (svm->nested.nested_run_pending)
  3674. return -EBUSY;
  3675. if (svm_smi_blocked(vcpu))
  3676. return 0;
  3677. /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
  3678. if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
  3679. return -EBUSY;
  3680. return 1;
  3681. }
  3682. static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
  3683. {
  3684. struct vcpu_svm *svm = to_svm(vcpu);
  3685. struct kvm_host_map map_save;
  3686. int ret;
  3687. if (!is_guest_mode(vcpu))
  3688. return 0;
  3689. /* FED8h - SVM Guest */
  3690. put_smstate(u64, smstate, 0x7ed8, 1);
  3691. /* FEE0h - SVM Guest VMCB Physical Address */
  3692. put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
  3693. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  3694. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  3695. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  3696. ret = nested_svm_simple_vmexit(svm, SVM_EXIT_SW);
  3697. if (ret)
  3698. return ret;
  3699. /*
  3700. * KVM uses VMCB01 to store L1 host state while L2 runs but
  3701. * VMCB01 is going to be used during SMM and thus the state will
  3702. * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save
  3703. * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the
  3704. * format of the area is identical to guest save area offsetted
  3705. * by 0x400 (matches the offset of 'struct vmcb_save_area'
  3706. * within 'struct vmcb'). Note: HSAVE area may also be used by
  3707. * L1 hypervisor to save additional host context (e.g. KVM does
  3708. * that, see svm_prepare_switch_to_guest()) which must be
  3709. * preserved.
  3710. */
  3711. if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr),
  3712. &map_save) == -EINVAL)
  3713. return 1;
  3714. BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400);
  3715. svm_copy_vmrun_state(map_save.hva + 0x400,
  3716. &svm->vmcb01.ptr->save);
  3717. kvm_vcpu_unmap(vcpu, &map_save, true);
  3718. return 0;
  3719. }
  3720. static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
  3721. {
  3722. struct vcpu_svm *svm = to_svm(vcpu);
  3723. struct kvm_host_map map, map_save;
  3724. u64 saved_efer, vmcb12_gpa;
  3725. struct vmcb *vmcb12;
  3726. int ret;
  3727. if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
  3728. return 0;
  3729. /* Non-zero if SMI arrived while vCPU was in guest mode. */
  3730. if (!GET_SMSTATE(u64, smstate, 0x7ed8))
  3731. return 0;
  3732. if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
  3733. return 1;
  3734. saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
  3735. if (!(saved_efer & EFER_SVME))
  3736. return 1;
  3737. vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
  3738. if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
  3739. return 1;
  3740. ret = 1;
  3741. if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save) == -EINVAL)
  3742. goto unmap_map;
  3743. if (svm_allocate_nested(svm))
  3744. goto unmap_save;
  3745. /*
  3746. * Restore L1 host state from L1 HSAVE area as VMCB01 was
  3747. * used during SMM (see svm_enter_smm())
  3748. */
  3749. svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400);
  3750. /*
  3751. * Enter the nested guest now
  3752. */
  3753. vmcb_mark_all_dirty(svm->vmcb01.ptr);
  3754. vmcb12 = map.hva;
  3755. nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
  3756. nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
  3757. ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false);
  3758. if (ret)
  3759. goto unmap_save;
  3760. svm->nested.nested_run_pending = 1;
  3761. unmap_save:
  3762. kvm_vcpu_unmap(vcpu, &map_save, true);
  3763. unmap_map:
  3764. kvm_vcpu_unmap(vcpu, &map, true);
  3765. return ret;
  3766. }
  3767. static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
  3768. {
  3769. struct vcpu_svm *svm = to_svm(vcpu);
  3770. if (!gif_set(svm)) {
  3771. if (vgif)
  3772. svm_set_intercept(svm, INTERCEPT_STGI);
  3773. /* STGI will cause a vm exit */
  3774. } else {
  3775. /* We must be in SMM; RSM will cause a vmexit anyway. */
  3776. }
  3777. }
  3778. static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
  3779. void *insn, int insn_len)
  3780. {
  3781. bool smep, smap, is_user;
  3782. unsigned long cr4;
  3783. u64 error_code;
  3784. /* Emulation is always possible when KVM has access to all guest state. */
  3785. if (!sev_guest(vcpu->kvm))
  3786. return true;
  3787. /* #UD and #GP should never be intercepted for SEV guests. */
  3788. WARN_ON_ONCE(emul_type & (EMULTYPE_TRAP_UD |
  3789. EMULTYPE_TRAP_UD_FORCED |
  3790. EMULTYPE_VMWARE_GP));
  3791. /*
  3792. * Emulation is impossible for SEV-ES guests as KVM doesn't have access
  3793. * to guest register state.
  3794. */
  3795. if (sev_es_guest(vcpu->kvm))
  3796. return false;
  3797. /*
  3798. * Emulation is possible if the instruction is already decoded, e.g.
  3799. * when completing I/O after returning from userspace.
  3800. */
  3801. if (emul_type & EMULTYPE_NO_DECODE)
  3802. return true;
  3803. /*
  3804. * Emulation is possible for SEV guests if and only if a prefilled
  3805. * buffer containing the bytes of the intercepted instruction is
  3806. * available. SEV guest memory is encrypted with a guest specific key
  3807. * and cannot be decrypted by KVM, i.e. KVM would read cyphertext and
  3808. * decode garbage.
  3809. *
  3810. * If KVM is NOT trying to simply skip an instruction, inject #UD if
  3811. * KVM reached this point without an instruction buffer. In practice,
  3812. * this path should never be hit by a well-behaved guest, e.g. KVM
  3813. * doesn't intercept #UD or #GP for SEV guests, but this path is still
  3814. * theoretically reachable, e.g. via unaccelerated fault-like AVIC
  3815. * access, and needs to be handled by KVM to avoid putting the guest
  3816. * into an infinite loop. Injecting #UD is somewhat arbitrary, but
  3817. * its the least awful option given lack of insight into the guest.
  3818. *
  3819. * If KVM is trying to skip an instruction, simply resume the guest.
  3820. * If a #NPF occurs while the guest is vectoring an INT3/INTO, then KVM
  3821. * will attempt to re-inject the INT3/INTO and skip the instruction.
  3822. * In that scenario, retrying the INT3/INTO and hoping the guest will
  3823. * make forward progress is the only option that has a chance of
  3824. * success (and in practice it will work the vast majority of the time).
  3825. */
  3826. if (unlikely(!insn)) {
  3827. if (!(emul_type & EMULTYPE_SKIP))
  3828. kvm_queue_exception(vcpu, UD_VECTOR);
  3829. return false;
  3830. }
  3831. /*
  3832. * Emulate for SEV guests if the insn buffer is not empty. The buffer
  3833. * will be empty if the DecodeAssist microcode cannot fetch bytes for
  3834. * the faulting instruction because the code fetch itself faulted, e.g.
  3835. * the guest attempted to fetch from emulated MMIO or a guest page
  3836. * table used to translate CS:RIP resides in emulated MMIO.
  3837. */
  3838. if (likely(insn_len))
  3839. return true;
  3840. /*
  3841. * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
  3842. *
  3843. * Errata:
  3844. * When CPU raises #NPF on guest data access and vCPU CR4.SMAP=1, it is
  3845. * possible that CPU microcode implementing DecodeAssist will fail to
  3846. * read guest memory at CS:RIP and vmcb.GuestIntrBytes will incorrectly
  3847. * be '0'. This happens because microcode reads CS:RIP using a _data_
  3848. * loap uop with CPL=0 privileges. If the load hits a SMAP #PF, ucode
  3849. * gives up and does not fill the instruction bytes buffer.
  3850. *
  3851. * As above, KVM reaches this point iff the VM is an SEV guest, the CPU
  3852. * supports DecodeAssist, a #NPF was raised, KVM's page fault handler
  3853. * triggered emulation (e.g. for MMIO), and the CPU returned 0 in the
  3854. * GuestIntrBytes field of the VMCB.
  3855. *
  3856. * This does _not_ mean that the erratum has been encountered, as the
  3857. * DecodeAssist will also fail if the load for CS:RIP hits a legitimate
  3858. * #PF, e.g. if the guest attempt to execute from emulated MMIO and
  3859. * encountered a reserved/not-present #PF.
  3860. *
  3861. * To hit the erratum, the following conditions must be true:
  3862. * 1. CR4.SMAP=1 (obviously).
  3863. * 2. CR4.SMEP=0 || CPL=3. If SMEP=1 and CPL<3, the erratum cannot
  3864. * have been hit as the guest would have encountered a SMEP
  3865. * violation #PF, not a #NPF.
  3866. * 3. The #NPF is not due to a code fetch, in which case failure to
  3867. * retrieve the instruction bytes is legitimate (see abvoe).
  3868. *
  3869. * In addition, don't apply the erratum workaround if the #NPF occurred
  3870. * while translating guest page tables (see below).
  3871. */
  3872. error_code = to_svm(vcpu)->vmcb->control.exit_info_1;
  3873. if (error_code & (PFERR_GUEST_PAGE_MASK | PFERR_FETCH_MASK))
  3874. goto resume_guest;
  3875. cr4 = kvm_read_cr4(vcpu);
  3876. smep = cr4 & X86_CR4_SMEP;
  3877. smap = cr4 & X86_CR4_SMAP;
  3878. is_user = svm_get_cpl(vcpu) == 3;
  3879. if (smap && (!smep || is_user)) {
  3880. pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
  3881. /*
  3882. * If the fault occurred in userspace, arbitrarily inject #GP
  3883. * to avoid killing the guest and to hopefully avoid confusing
  3884. * the guest kernel too much, e.g. injecting #PF would not be
  3885. * coherent with respect to the guest's page tables. Request
  3886. * triple fault if the fault occurred in the kernel as there's
  3887. * no fault that KVM can inject without confusing the guest.
  3888. * In practice, the triple fault is moot as no sane SEV kernel
  3889. * will execute from user memory while also running with SMAP=1.
  3890. */
  3891. if (is_user)
  3892. kvm_inject_gp(vcpu, 0);
  3893. else
  3894. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  3895. }
  3896. resume_guest:
  3897. /*
  3898. * If the erratum was not hit, simply resume the guest and let it fault
  3899. * again. While awful, e.g. the vCPU may get stuck in an infinite loop
  3900. * if the fault is at CPL=0, it's the lesser of all evils. Exiting to
  3901. * userspace will kill the guest, and letting the emulator read garbage
  3902. * will yield random behavior and potentially corrupt the guest.
  3903. *
  3904. * Simply resuming the guest is technically not a violation of the SEV
  3905. * architecture. AMD's APM states that all code fetches and page table
  3906. * accesses for SEV guest are encrypted, regardless of the C-Bit. The
  3907. * APM also states that encrypted accesses to MMIO are "ignored", but
  3908. * doesn't explicitly define "ignored", i.e. doing nothing and letting
  3909. * the guest spin is technically "ignoring" the access.
  3910. */
  3911. return false;
  3912. }
  3913. static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
  3914. {
  3915. struct vcpu_svm *svm = to_svm(vcpu);
  3916. return !gif_set(svm);
  3917. }
  3918. static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
  3919. {
  3920. if (!sev_es_guest(vcpu->kvm))
  3921. return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
  3922. sev_vcpu_deliver_sipi_vector(vcpu, vector);
  3923. }
  3924. static void svm_vm_destroy(struct kvm *kvm)
  3925. {
  3926. avic_vm_destroy(kvm);
  3927. sev_vm_destroy(kvm);
  3928. }
  3929. static int svm_vm_init(struct kvm *kvm)
  3930. {
  3931. if (!pause_filter_count || !pause_filter_thresh)
  3932. kvm->arch.pause_in_guest = true;
  3933. if (enable_apicv) {
  3934. int ret = avic_vm_init(kvm);
  3935. if (ret)
  3936. return ret;
  3937. }
  3938. return 0;
  3939. }
  3940. static struct kvm_x86_ops svm_x86_ops __initdata = {
  3941. .name = "kvm_amd",
  3942. .hardware_unsetup = svm_hardware_unsetup,
  3943. .hardware_enable = svm_hardware_enable,
  3944. .hardware_disable = svm_hardware_disable,
  3945. .has_emulated_msr = svm_has_emulated_msr,
  3946. .vcpu_create = svm_vcpu_create,
  3947. .vcpu_free = svm_vcpu_free,
  3948. .vcpu_reset = svm_vcpu_reset,
  3949. .vm_size = sizeof(struct kvm_svm),
  3950. .vm_init = svm_vm_init,
  3951. .vm_destroy = svm_vm_destroy,
  3952. .prepare_switch_to_guest = svm_prepare_switch_to_guest,
  3953. .vcpu_load = svm_vcpu_load,
  3954. .vcpu_put = svm_vcpu_put,
  3955. .vcpu_blocking = avic_vcpu_blocking,
  3956. .vcpu_unblocking = avic_vcpu_unblocking,
  3957. .update_exception_bitmap = svm_update_exception_bitmap,
  3958. .get_msr_feature = svm_get_msr_feature,
  3959. .get_msr = svm_get_msr,
  3960. .set_msr = svm_set_msr,
  3961. .get_segment_base = svm_get_segment_base,
  3962. .get_segment = svm_get_segment,
  3963. .set_segment = svm_set_segment,
  3964. .get_cpl = svm_get_cpl,
  3965. .get_cs_db_l_bits = svm_get_cs_db_l_bits,
  3966. .is_valid_cr0 = svm_is_valid_cr0,
  3967. .set_cr0 = svm_set_cr0,
  3968. .post_set_cr3 = sev_post_set_cr3,
  3969. .is_valid_cr4 = svm_is_valid_cr4,
  3970. .set_cr4 = svm_set_cr4,
  3971. .set_efer = svm_set_efer,
  3972. .get_idt = svm_get_idt,
  3973. .set_idt = svm_set_idt,
  3974. .get_gdt = svm_get_gdt,
  3975. .set_gdt = svm_set_gdt,
  3976. .set_dr7 = svm_set_dr7,
  3977. .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
  3978. .cache_reg = svm_cache_reg,
  3979. .get_rflags = svm_get_rflags,
  3980. .set_rflags = svm_set_rflags,
  3981. .get_if_flag = svm_get_if_flag,
  3982. .flush_tlb_all = svm_flush_tlb_all,
  3983. .flush_tlb_current = svm_flush_tlb_current,
  3984. .flush_tlb_gva = svm_flush_tlb_gva,
  3985. .flush_tlb_guest = svm_flush_tlb_asid,
  3986. .vcpu_pre_run = svm_vcpu_pre_run,
  3987. .vcpu_run = svm_vcpu_run,
  3988. .handle_exit = svm_handle_exit,
  3989. .skip_emulated_instruction = svm_skip_emulated_instruction,
  3990. .update_emulated_instruction = NULL,
  3991. .set_interrupt_shadow = svm_set_interrupt_shadow,
  3992. .get_interrupt_shadow = svm_get_interrupt_shadow,
  3993. .patch_hypercall = svm_patch_hypercall,
  3994. .inject_irq = svm_inject_irq,
  3995. .inject_nmi = svm_inject_nmi,
  3996. .inject_exception = svm_inject_exception,
  3997. .cancel_injection = svm_cancel_injection,
  3998. .interrupt_allowed = svm_interrupt_allowed,
  3999. .nmi_allowed = svm_nmi_allowed,
  4000. .get_nmi_mask = svm_get_nmi_mask,
  4001. .set_nmi_mask = svm_set_nmi_mask,
  4002. .enable_nmi_window = svm_enable_nmi_window,
  4003. .enable_irq_window = svm_enable_irq_window,
  4004. .update_cr8_intercept = svm_update_cr8_intercept,
  4005. .set_virtual_apic_mode = avic_refresh_virtual_apic_mode,
  4006. .refresh_apicv_exec_ctrl = avic_refresh_apicv_exec_ctrl,
  4007. .check_apicv_inhibit_reasons = avic_check_apicv_inhibit_reasons,
  4008. .apicv_post_state_restore = avic_apicv_post_state_restore,
  4009. .get_exit_info = svm_get_exit_info,
  4010. .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
  4011. .has_wbinvd_exit = svm_has_wbinvd_exit,
  4012. .get_l2_tsc_offset = svm_get_l2_tsc_offset,
  4013. .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier,
  4014. .write_tsc_offset = svm_write_tsc_offset,
  4015. .write_tsc_multiplier = svm_write_tsc_multiplier,
  4016. .load_mmu_pgd = svm_load_mmu_pgd,
  4017. .check_intercept = svm_check_intercept,
  4018. .handle_exit_irqoff = svm_handle_exit_irqoff,
  4019. .request_immediate_exit = __kvm_request_immediate_exit,
  4020. .sched_in = svm_sched_in,
  4021. .nested_ops = &svm_nested_ops,
  4022. .deliver_interrupt = svm_deliver_interrupt,
  4023. .pi_update_irte = avic_pi_update_irte,
  4024. .setup_mce = svm_setup_mce,
  4025. .smi_allowed = svm_smi_allowed,
  4026. .enter_smm = svm_enter_smm,
  4027. .leave_smm = svm_leave_smm,
  4028. .enable_smi_window = svm_enable_smi_window,
  4029. .mem_enc_ioctl = sev_mem_enc_ioctl,
  4030. .mem_enc_register_region = sev_mem_enc_register_region,
  4031. .mem_enc_unregister_region = sev_mem_enc_unregister_region,
  4032. .guest_memory_reclaimed = sev_guest_memory_reclaimed,
  4033. .vm_copy_enc_context_from = sev_vm_copy_enc_context_from,
  4034. .vm_move_enc_context_from = sev_vm_move_enc_context_from,
  4035. .can_emulate_instruction = svm_can_emulate_instruction,
  4036. .apic_init_signal_blocked = svm_apic_init_signal_blocked,
  4037. .msr_filter_changed = svm_msr_filter_changed,
  4038. .complete_emulated_msr = svm_complete_emulated_msr,
  4039. .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
  4040. .vcpu_get_apicv_inhibit_reasons = avic_vcpu_get_apicv_inhibit_reasons,
  4041. };
  4042. /*
  4043. * The default MMIO mask is a single bit (excluding the present bit),
  4044. * which could conflict with the memory encryption bit. Check for
  4045. * memory encryption support and override the default MMIO mask if
  4046. * memory encryption is enabled.
  4047. */
  4048. static __init void svm_adjust_mmio_mask(void)
  4049. {
  4050. unsigned int enc_bit, mask_bit;
  4051. u64 msr, mask;
  4052. /* If there is no memory encryption support, use existing mask */
  4053. if (cpuid_eax(0x80000000) < 0x8000001f)
  4054. return;
  4055. /* If memory encryption is not enabled, use existing mask */
  4056. rdmsrl(MSR_AMD64_SYSCFG, msr);
  4057. if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
  4058. return;
  4059. enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
  4060. mask_bit = boot_cpu_data.x86_phys_bits;
  4061. /* Increment the mask bit if it is the same as the encryption bit */
  4062. if (enc_bit == mask_bit)
  4063. mask_bit++;
  4064. /*
  4065. * If the mask bit location is below 52, then some bits above the
  4066. * physical addressing limit will always be reserved, so use the
  4067. * rsvd_bits() function to generate the mask. This mask, along with
  4068. * the present bit, will be used to generate a page fault with
  4069. * PFER.RSV = 1.
  4070. *
  4071. * If the mask bit location is 52 (or above), then clear the mask.
  4072. */
  4073. mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
  4074. kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
  4075. }
  4076. static __init void svm_set_cpu_caps(void)
  4077. {
  4078. kvm_set_cpu_caps();
  4079. kvm_caps.supported_perf_cap = 0;
  4080. kvm_caps.supported_xss = 0;
  4081. /* CPUID 0x80000001 and 0x8000000A (SVM features) */
  4082. if (nested) {
  4083. kvm_cpu_cap_set(X86_FEATURE_SVM);
  4084. kvm_cpu_cap_set(X86_FEATURE_VMCBCLEAN);
  4085. if (nrips)
  4086. kvm_cpu_cap_set(X86_FEATURE_NRIPS);
  4087. if (npt_enabled)
  4088. kvm_cpu_cap_set(X86_FEATURE_NPT);
  4089. if (tsc_scaling)
  4090. kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR);
  4091. if (vls)
  4092. kvm_cpu_cap_set(X86_FEATURE_V_VMSAVE_VMLOAD);
  4093. if (lbrv)
  4094. kvm_cpu_cap_set(X86_FEATURE_LBRV);
  4095. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER))
  4096. kvm_cpu_cap_set(X86_FEATURE_PAUSEFILTER);
  4097. if (boot_cpu_has(X86_FEATURE_PFTHRESHOLD))
  4098. kvm_cpu_cap_set(X86_FEATURE_PFTHRESHOLD);
  4099. if (vgif)
  4100. kvm_cpu_cap_set(X86_FEATURE_VGIF);
  4101. /* Nested VM can receive #VMEXIT instead of triggering #GP */
  4102. kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
  4103. }
  4104. /* CPUID 0x80000008 */
  4105. if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
  4106. boot_cpu_has(X86_FEATURE_AMD_SSBD))
  4107. kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
  4108. /* AMD PMU PERFCTR_CORE CPUID */
  4109. if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
  4110. kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
  4111. /* CPUID 0x8000001F (SME/SEV features) */
  4112. sev_set_cpu_caps();
  4113. }
  4114. static __init int svm_hardware_setup(void)
  4115. {
  4116. int cpu;
  4117. struct page *iopm_pages;
  4118. void *iopm_va;
  4119. int r;
  4120. unsigned int order = get_order(IOPM_SIZE);
  4121. /*
  4122. * NX is required for shadow paging and for NPT if the NX huge pages
  4123. * mitigation is enabled.
  4124. */
  4125. if (!boot_cpu_has(X86_FEATURE_NX)) {
  4126. pr_err_ratelimited("NX (Execute Disable) not supported\n");
  4127. return -EOPNOTSUPP;
  4128. }
  4129. kvm_enable_efer_bits(EFER_NX);
  4130. iopm_pages = alloc_pages(GFP_KERNEL, order);
  4131. if (!iopm_pages)
  4132. return -ENOMEM;
  4133. iopm_va = page_address(iopm_pages);
  4134. memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
  4135. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  4136. init_msrpm_offsets();
  4137. kvm_caps.supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
  4138. XFEATURE_MASK_BNDCSR);
  4139. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  4140. kvm_enable_efer_bits(EFER_FFXSR);
  4141. if (tsc_scaling) {
  4142. if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  4143. tsc_scaling = false;
  4144. } else {
  4145. pr_info("TSC scaling supported\n");
  4146. kvm_caps.has_tsc_control = true;
  4147. }
  4148. }
  4149. kvm_caps.max_tsc_scaling_ratio = SVM_TSC_RATIO_MAX;
  4150. kvm_caps.tsc_scaling_ratio_frac_bits = 32;
  4151. tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
  4152. /* Check for pause filtering support */
  4153. if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  4154. pause_filter_count = 0;
  4155. pause_filter_thresh = 0;
  4156. } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
  4157. pause_filter_thresh = 0;
  4158. }
  4159. if (nested) {
  4160. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  4161. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  4162. }
  4163. /*
  4164. * KVM's MMU doesn't support using 2-level paging for itself, and thus
  4165. * NPT isn't supported if the host is using 2-level paging since host
  4166. * CR4 is unchanged on VMRUN.
  4167. */
  4168. if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
  4169. npt_enabled = false;
  4170. if (!boot_cpu_has(X86_FEATURE_NPT))
  4171. npt_enabled = false;
  4172. /* Force VM NPT level equal to the host's paging level */
  4173. kvm_configure_mmu(npt_enabled, get_npt_level(),
  4174. get_npt_level(), PG_LEVEL_1G);
  4175. pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
  4176. /* Setup shadow_me_value and shadow_me_mask */
  4177. kvm_mmu_set_me_spte_mask(sme_me_mask, sme_me_mask);
  4178. svm_adjust_mmio_mask();
  4179. /*
  4180. * Note, SEV setup consumes npt_enabled and enable_mmio_caching (which
  4181. * may be modified by svm_adjust_mmio_mask()).
  4182. */
  4183. sev_hardware_setup();
  4184. svm_hv_hardware_setup();
  4185. for_each_possible_cpu(cpu) {
  4186. r = svm_cpu_init(cpu);
  4187. if (r)
  4188. goto err;
  4189. }
  4190. if (nrips) {
  4191. if (!boot_cpu_has(X86_FEATURE_NRIPS))
  4192. nrips = false;
  4193. }
  4194. enable_apicv = avic = avic && avic_hardware_setup(&svm_x86_ops);
  4195. if (!enable_apicv) {
  4196. svm_x86_ops.vcpu_blocking = NULL;
  4197. svm_x86_ops.vcpu_unblocking = NULL;
  4198. svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;
  4199. }
  4200. if (vls) {
  4201. if (!npt_enabled ||
  4202. !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
  4203. !IS_ENABLED(CONFIG_X86_64)) {
  4204. vls = false;
  4205. } else {
  4206. pr_info("Virtual VMLOAD VMSAVE supported\n");
  4207. }
  4208. }
  4209. if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
  4210. svm_gp_erratum_intercept = false;
  4211. if (vgif) {
  4212. if (!boot_cpu_has(X86_FEATURE_VGIF))
  4213. vgif = false;
  4214. else
  4215. pr_info("Virtual GIF supported\n");
  4216. }
  4217. if (lbrv) {
  4218. if (!boot_cpu_has(X86_FEATURE_LBRV))
  4219. lbrv = false;
  4220. else
  4221. pr_info("LBR virtualization supported\n");
  4222. }
  4223. if (!enable_pmu)
  4224. pr_info("PMU virtualization is disabled\n");
  4225. svm_set_cpu_caps();
  4226. /*
  4227. * It seems that on AMD processors PTE's accessed bit is
  4228. * being set by the CPU hardware before the NPF vmexit.
  4229. * This is not expected behaviour and our tests fail because
  4230. * of it.
  4231. * A workaround here is to disable support for
  4232. * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
  4233. * In this case userspace can know if there is support using
  4234. * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
  4235. * it
  4236. * If future AMD CPU models change the behaviour described above,
  4237. * this variable can be changed accordingly
  4238. */
  4239. allow_smaller_maxphyaddr = !npt_enabled;
  4240. return 0;
  4241. err:
  4242. svm_hardware_unsetup();
  4243. return r;
  4244. }
  4245. static struct kvm_x86_init_ops svm_init_ops __initdata = {
  4246. .cpu_has_kvm_support = has_svm,
  4247. .disabled_by_bios = is_disabled,
  4248. .hardware_setup = svm_hardware_setup,
  4249. .check_processor_compatibility = svm_check_processor_compat,
  4250. .runtime_ops = &svm_x86_ops,
  4251. .pmu_ops = &amd_pmu_ops,
  4252. };
  4253. static int __init svm_init(void)
  4254. {
  4255. int r;
  4256. __unused_size_checks();
  4257. r = kvm_x86_vendor_init(&svm_init_ops);
  4258. if (r)
  4259. return r;
  4260. /*
  4261. * Common KVM initialization _must_ come last, after this, /dev/kvm is
  4262. * exposed to userspace!
  4263. */
  4264. r = kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
  4265. __alignof__(struct vcpu_svm), THIS_MODULE);
  4266. if (r)
  4267. goto err_kvm_init;
  4268. return 0;
  4269. err_kvm_init:
  4270. kvm_x86_vendor_exit();
  4271. return r;
  4272. }
  4273. static void __exit svm_exit(void)
  4274. {
  4275. kvm_exit();
  4276. kvm_x86_vendor_exit();
  4277. }
  4278. module_init(svm_init)
  4279. module_exit(svm_exit)