svm.c 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * AMD SVM support
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  8. *
  9. * Authors:
  10. * Yaniv Kamay <yaniv@qumranet.com>
  11. * Avi Kivity <avi@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #define pr_fmt(fmt) "SVM: " fmt
  18. #include <linux/kvm_host.h>
  19. #include "irq.h"
  20. #include "mmu.h"
  21. #include "kvm_cache_regs.h"
  22. #include "x86.h"
  23. #include "cpuid.h"
  24. #include "pmu.h"
  25. #include <linux/module.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/kernel.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/highmem.h>
  30. #include <linux/sched.h>
  31. #include <linux/trace_events.h>
  32. #include <linux/slab.h>
  33. #include <linux/amd-iommu.h>
  34. #include <linux/hashtable.h>
  35. #include <linux/frame.h>
  36. #include <asm/apic.h>
  37. #include <asm/perf_event.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/desc.h>
  40. #include <asm/debugreg.h>
  41. #include <asm/kvm_para.h>
  42. #include <asm/irq_remapping.h>
  43. #include <asm/virtext.h>
  44. #include "trace.h"
  45. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  46. MODULE_AUTHOR("Qumranet");
  47. MODULE_LICENSE("GPL");
  48. static const struct x86_cpu_id svm_cpu_id[] = {
  49. X86_FEATURE_MATCH(X86_FEATURE_SVM),
  50. {}
  51. };
  52. MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
  53. #define IOPM_ALLOC_ORDER 2
  54. #define MSRPM_ALLOC_ORDER 1
  55. #define SEG_TYPE_LDT 2
  56. #define SEG_TYPE_BUSY_TSS16 3
  57. #define SVM_FEATURE_NPT (1 << 0)
  58. #define SVM_FEATURE_LBRV (1 << 1)
  59. #define SVM_FEATURE_SVML (1 << 2)
  60. #define SVM_FEATURE_NRIP (1 << 3)
  61. #define SVM_FEATURE_TSC_RATE (1 << 4)
  62. #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
  63. #define SVM_FEATURE_FLUSH_ASID (1 << 6)
  64. #define SVM_FEATURE_DECODE_ASSIST (1 << 7)
  65. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  66. #define SVM_AVIC_DOORBELL 0xc001011b
  67. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  68. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  69. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  70. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  71. #define TSC_RATIO_RSVD 0xffffff0000000000ULL
  72. #define TSC_RATIO_MIN 0x0000000000000001ULL
  73. #define TSC_RATIO_MAX 0x000000ffffffffffULL
  74. #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
  75. /*
  76. * 0xff is broadcast, so the max index allowed for physical APIC ID
  77. * table is 0xfe. APIC IDs above 0xff are reserved.
  78. */
  79. #define AVIC_MAX_PHYSICAL_ID_COUNT 255
  80. #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
  81. #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
  82. #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
  83. /* AVIC GATAG is encoded using VM and VCPU IDs */
  84. #define AVIC_VCPU_ID_BITS 8
  85. #define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
  86. #define AVIC_VM_ID_BITS 24
  87. #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
  88. #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
  89. #define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
  90. (y & AVIC_VCPU_ID_MASK))
  91. #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
  92. #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
  93. static bool erratum_383_found __read_mostly;
  94. static const u32 host_save_user_msrs[] = {
  95. #ifdef CONFIG_X86_64
  96. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  97. MSR_FS_BASE,
  98. #endif
  99. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  100. MSR_TSC_AUX,
  101. };
  102. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  103. struct kvm_vcpu;
  104. struct nested_state {
  105. struct vmcb *hsave;
  106. u64 hsave_msr;
  107. u64 vm_cr_msr;
  108. u64 vmcb;
  109. /* These are the merged vectors */
  110. u32 *msrpm;
  111. /* gpa pointers to the real vectors */
  112. u64 vmcb_msrpm;
  113. u64 vmcb_iopm;
  114. /* A VMEXIT is required but not yet emulated */
  115. bool exit_required;
  116. /* cache for intercepts of the guest */
  117. u32 intercept_cr;
  118. u32 intercept_dr;
  119. u32 intercept_exceptions;
  120. u64 intercept;
  121. /* Nested Paging related state */
  122. u64 nested_cr3;
  123. };
  124. #define MSRPM_OFFSETS 16
  125. static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  126. /*
  127. * Set osvw_len to higher value when updated Revision Guides
  128. * are published and we know what the new status bits are
  129. */
  130. static uint64_t osvw_len = 4, osvw_status;
  131. struct vcpu_svm {
  132. struct kvm_vcpu vcpu;
  133. struct vmcb *vmcb;
  134. unsigned long vmcb_pa;
  135. struct svm_cpu_data *svm_data;
  136. uint64_t asid_generation;
  137. uint64_t sysenter_esp;
  138. uint64_t sysenter_eip;
  139. uint64_t tsc_aux;
  140. u64 next_rip;
  141. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  142. struct {
  143. u16 fs;
  144. u16 gs;
  145. u16 ldt;
  146. u64 gs_base;
  147. } host;
  148. u32 *msrpm;
  149. ulong nmi_iret_rip;
  150. struct nested_state nested;
  151. bool nmi_singlestep;
  152. u64 nmi_singlestep_guest_rflags;
  153. unsigned int3_injected;
  154. unsigned long int3_rip;
  155. /* cached guest cpuid flags for faster access */
  156. bool nrips_enabled : 1;
  157. u32 ldr_reg;
  158. struct page *avic_backing_page;
  159. u64 *avic_physical_id_cache;
  160. bool avic_is_running;
  161. /*
  162. * Per-vcpu list of struct amd_svm_iommu_ir:
  163. * This is used mainly to store interrupt remapping information used
  164. * when update the vcpu affinity. This avoids the need to scan for
  165. * IRTE and try to match ga_tag in the IOMMU driver.
  166. */
  167. struct list_head ir_list;
  168. spinlock_t ir_list_lock;
  169. };
  170. /*
  171. * This is a wrapper of struct amd_iommu_ir_data.
  172. */
  173. struct amd_svm_iommu_ir {
  174. struct list_head node; /* Used by SVM for per-vcpu ir_list */
  175. void *data; /* Storing pointer to struct amd_ir_data */
  176. };
  177. #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
  178. #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
  179. #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
  180. #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
  181. #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
  182. #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
  183. static DEFINE_PER_CPU(u64, current_tsc_ratio);
  184. #define TSC_RATIO_DEFAULT 0x0100000000ULL
  185. #define MSR_INVALID 0xffffffffU
  186. static const struct svm_direct_access_msrs {
  187. u32 index; /* Index of the MSR */
  188. bool always; /* True if intercept is always on */
  189. } direct_access_msrs[] = {
  190. { .index = MSR_STAR, .always = true },
  191. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  192. #ifdef CONFIG_X86_64
  193. { .index = MSR_GS_BASE, .always = true },
  194. { .index = MSR_FS_BASE, .always = true },
  195. { .index = MSR_KERNEL_GS_BASE, .always = true },
  196. { .index = MSR_LSTAR, .always = true },
  197. { .index = MSR_CSTAR, .always = true },
  198. { .index = MSR_SYSCALL_MASK, .always = true },
  199. #endif
  200. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  201. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  202. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  203. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  204. { .index = MSR_INVALID, .always = false },
  205. };
  206. /* enable NPT for AMD64 and X86 with PAE */
  207. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  208. static bool npt_enabled = true;
  209. #else
  210. static bool npt_enabled;
  211. #endif
  212. /* allow nested paging (virtualized MMU) for all guests */
  213. static int npt = true;
  214. module_param(npt, int, S_IRUGO);
  215. /* allow nested virtualization in KVM/SVM */
  216. static int nested = true;
  217. module_param(nested, int, S_IRUGO);
  218. /* enable / disable AVIC */
  219. static int avic;
  220. #ifdef CONFIG_X86_LOCAL_APIC
  221. module_param(avic, int, S_IRUGO);
  222. #endif
  223. /* enable/disable Virtual VMLOAD VMSAVE */
  224. static int vls = true;
  225. module_param(vls, int, 0444);
  226. /* enable/disable Virtual GIF */
  227. static int vgif = true;
  228. module_param(vgif, int, 0444);
  229. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  230. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  231. static void svm_complete_interrupts(struct vcpu_svm *svm);
  232. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  233. static int nested_svm_intercept(struct vcpu_svm *svm);
  234. static int nested_svm_vmexit(struct vcpu_svm *svm);
  235. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  236. bool has_error_code, u32 error_code);
  237. enum {
  238. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  239. pause filter count */
  240. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  241. VMCB_ASID, /* ASID */
  242. VMCB_INTR, /* int_ctl, int_vector */
  243. VMCB_NPT, /* npt_en, nCR3, gPAT */
  244. VMCB_CR, /* CR0, CR3, CR4, EFER */
  245. VMCB_DR, /* DR6, DR7 */
  246. VMCB_DT, /* GDT, IDT */
  247. VMCB_SEG, /* CS, DS, SS, ES, CPL */
  248. VMCB_CR2, /* CR2 only */
  249. VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
  250. VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
  251. * AVIC PHYSICAL_TABLE pointer,
  252. * AVIC LOGICAL_TABLE pointer
  253. */
  254. VMCB_DIRTY_MAX,
  255. };
  256. /* TPR and CR2 are always written before VMRUN */
  257. #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
  258. #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
  259. static inline void mark_all_dirty(struct vmcb *vmcb)
  260. {
  261. vmcb->control.clean = 0;
  262. }
  263. static inline void mark_all_clean(struct vmcb *vmcb)
  264. {
  265. vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
  266. & ~VMCB_ALWAYS_DIRTY_MASK;
  267. }
  268. static inline void mark_dirty(struct vmcb *vmcb, int bit)
  269. {
  270. vmcb->control.clean &= ~(1 << bit);
  271. }
  272. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  273. {
  274. return container_of(vcpu, struct vcpu_svm, vcpu);
  275. }
  276. static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
  277. {
  278. svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
  279. mark_dirty(svm->vmcb, VMCB_AVIC);
  280. }
  281. static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
  282. {
  283. struct vcpu_svm *svm = to_svm(vcpu);
  284. u64 *entry = svm->avic_physical_id_cache;
  285. if (!entry)
  286. return false;
  287. return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
  288. }
  289. static void recalc_intercepts(struct vcpu_svm *svm)
  290. {
  291. struct vmcb_control_area *c, *h;
  292. struct nested_state *g;
  293. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  294. if (!is_guest_mode(&svm->vcpu))
  295. return;
  296. c = &svm->vmcb->control;
  297. h = &svm->nested.hsave->control;
  298. g = &svm->nested;
  299. c->intercept_cr = h->intercept_cr | g->intercept_cr;
  300. c->intercept_dr = h->intercept_dr | g->intercept_dr;
  301. c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
  302. c->intercept = h->intercept | g->intercept;
  303. }
  304. static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
  305. {
  306. if (is_guest_mode(&svm->vcpu))
  307. return svm->nested.hsave;
  308. else
  309. return svm->vmcb;
  310. }
  311. static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
  312. {
  313. struct vmcb *vmcb = get_host_vmcb(svm);
  314. vmcb->control.intercept_cr |= (1U << bit);
  315. recalc_intercepts(svm);
  316. }
  317. static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
  318. {
  319. struct vmcb *vmcb = get_host_vmcb(svm);
  320. vmcb->control.intercept_cr &= ~(1U << bit);
  321. recalc_intercepts(svm);
  322. }
  323. static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
  324. {
  325. struct vmcb *vmcb = get_host_vmcb(svm);
  326. return vmcb->control.intercept_cr & (1U << bit);
  327. }
  328. static inline void set_dr_intercepts(struct vcpu_svm *svm)
  329. {
  330. struct vmcb *vmcb = get_host_vmcb(svm);
  331. vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
  332. | (1 << INTERCEPT_DR1_READ)
  333. | (1 << INTERCEPT_DR2_READ)
  334. | (1 << INTERCEPT_DR3_READ)
  335. | (1 << INTERCEPT_DR4_READ)
  336. | (1 << INTERCEPT_DR5_READ)
  337. | (1 << INTERCEPT_DR6_READ)
  338. | (1 << INTERCEPT_DR7_READ)
  339. | (1 << INTERCEPT_DR0_WRITE)
  340. | (1 << INTERCEPT_DR1_WRITE)
  341. | (1 << INTERCEPT_DR2_WRITE)
  342. | (1 << INTERCEPT_DR3_WRITE)
  343. | (1 << INTERCEPT_DR4_WRITE)
  344. | (1 << INTERCEPT_DR5_WRITE)
  345. | (1 << INTERCEPT_DR6_WRITE)
  346. | (1 << INTERCEPT_DR7_WRITE);
  347. recalc_intercepts(svm);
  348. }
  349. static inline void clr_dr_intercepts(struct vcpu_svm *svm)
  350. {
  351. struct vmcb *vmcb = get_host_vmcb(svm);
  352. vmcb->control.intercept_dr = 0;
  353. recalc_intercepts(svm);
  354. }
  355. static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
  356. {
  357. struct vmcb *vmcb = get_host_vmcb(svm);
  358. vmcb->control.intercept_exceptions |= (1U << bit);
  359. recalc_intercepts(svm);
  360. }
  361. static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
  362. {
  363. struct vmcb *vmcb = get_host_vmcb(svm);
  364. vmcb->control.intercept_exceptions &= ~(1U << bit);
  365. recalc_intercepts(svm);
  366. }
  367. static inline void set_intercept(struct vcpu_svm *svm, int bit)
  368. {
  369. struct vmcb *vmcb = get_host_vmcb(svm);
  370. vmcb->control.intercept |= (1ULL << bit);
  371. recalc_intercepts(svm);
  372. }
  373. static inline void clr_intercept(struct vcpu_svm *svm, int bit)
  374. {
  375. struct vmcb *vmcb = get_host_vmcb(svm);
  376. vmcb->control.intercept &= ~(1ULL << bit);
  377. recalc_intercepts(svm);
  378. }
  379. static inline bool vgif_enabled(struct vcpu_svm *svm)
  380. {
  381. return !!(svm->vmcb->control.int_ctl & V_GIF_ENABLE_MASK);
  382. }
  383. static inline void enable_gif(struct vcpu_svm *svm)
  384. {
  385. if (vgif_enabled(svm))
  386. svm->vmcb->control.int_ctl |= V_GIF_MASK;
  387. else
  388. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  389. }
  390. static inline void disable_gif(struct vcpu_svm *svm)
  391. {
  392. if (vgif_enabled(svm))
  393. svm->vmcb->control.int_ctl &= ~V_GIF_MASK;
  394. else
  395. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  396. }
  397. static inline bool gif_set(struct vcpu_svm *svm)
  398. {
  399. if (vgif_enabled(svm))
  400. return !!(svm->vmcb->control.int_ctl & V_GIF_MASK);
  401. else
  402. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  403. }
  404. static unsigned long iopm_base;
  405. struct kvm_ldttss_desc {
  406. u16 limit0;
  407. u16 base0;
  408. unsigned base1:8, type:5, dpl:2, p:1;
  409. unsigned limit1:4, zero0:3, g:1, base2:8;
  410. u32 base3;
  411. u32 zero1;
  412. } __attribute__((packed));
  413. struct svm_cpu_data {
  414. int cpu;
  415. u64 asid_generation;
  416. u32 max_asid;
  417. u32 next_asid;
  418. struct kvm_ldttss_desc *tss_desc;
  419. struct page *save_area;
  420. };
  421. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  422. struct svm_init_data {
  423. int cpu;
  424. int r;
  425. };
  426. static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  427. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  428. #define MSRS_RANGE_SIZE 2048
  429. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  430. static u32 svm_msrpm_offset(u32 msr)
  431. {
  432. u32 offset;
  433. int i;
  434. for (i = 0; i < NUM_MSR_MAPS; i++) {
  435. if (msr < msrpm_ranges[i] ||
  436. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  437. continue;
  438. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  439. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  440. /* Now we have the u8 offset - but need the u32 offset */
  441. return offset / 4;
  442. }
  443. /* MSR not in any range */
  444. return MSR_INVALID;
  445. }
  446. #define MAX_INST_SIZE 15
  447. static inline void clgi(void)
  448. {
  449. asm volatile (__ex(SVM_CLGI));
  450. }
  451. static inline void stgi(void)
  452. {
  453. asm volatile (__ex(SVM_STGI));
  454. }
  455. static inline void invlpga(unsigned long addr, u32 asid)
  456. {
  457. asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
  458. }
  459. static int get_npt_level(void)
  460. {
  461. #ifdef CONFIG_X86_64
  462. return PT64_ROOT_LEVEL;
  463. #else
  464. return PT32E_ROOT_LEVEL;
  465. #endif
  466. }
  467. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  468. {
  469. vcpu->arch.efer = efer;
  470. if (!npt_enabled && !(efer & EFER_LMA))
  471. efer &= ~EFER_LME;
  472. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  473. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  474. }
  475. static int is_external_interrupt(u32 info)
  476. {
  477. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  478. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  479. }
  480. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  481. {
  482. struct vcpu_svm *svm = to_svm(vcpu);
  483. u32 ret = 0;
  484. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  485. ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  486. return ret;
  487. }
  488. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  489. {
  490. struct vcpu_svm *svm = to_svm(vcpu);
  491. if (mask == 0)
  492. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  493. else
  494. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  495. }
  496. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  497. {
  498. struct vcpu_svm *svm = to_svm(vcpu);
  499. if (svm->vmcb->control.next_rip != 0) {
  500. WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
  501. svm->next_rip = svm->vmcb->control.next_rip;
  502. }
  503. if (!svm->next_rip) {
  504. if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
  505. EMULATE_DONE)
  506. printk(KERN_DEBUG "%s: NOP\n", __func__);
  507. return;
  508. }
  509. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  510. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  511. __func__, kvm_rip_read(vcpu), svm->next_rip);
  512. kvm_rip_write(vcpu, svm->next_rip);
  513. svm_set_interrupt_shadow(vcpu, 0);
  514. }
  515. static void svm_queue_exception(struct kvm_vcpu *vcpu)
  516. {
  517. struct vcpu_svm *svm = to_svm(vcpu);
  518. unsigned nr = vcpu->arch.exception.nr;
  519. bool has_error_code = vcpu->arch.exception.has_error_code;
  520. bool reinject = vcpu->arch.exception.reinject;
  521. u32 error_code = vcpu->arch.exception.error_code;
  522. /*
  523. * If we are within a nested VM we'd better #VMEXIT and let the guest
  524. * handle the exception
  525. */
  526. if (!reinject &&
  527. nested_svm_check_exception(svm, nr, has_error_code, error_code))
  528. return;
  529. if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
  530. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  531. /*
  532. * For guest debugging where we have to reinject #BP if some
  533. * INT3 is guest-owned:
  534. * Emulate nRIP by moving RIP forward. Will fail if injection
  535. * raises a fault that is not intercepted. Still better than
  536. * failing in all cases.
  537. */
  538. skip_emulated_instruction(&svm->vcpu);
  539. rip = kvm_rip_read(&svm->vcpu);
  540. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  541. svm->int3_injected = rip - old_rip;
  542. }
  543. svm->vmcb->control.event_inj = nr
  544. | SVM_EVTINJ_VALID
  545. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  546. | SVM_EVTINJ_TYPE_EXEPT;
  547. svm->vmcb->control.event_inj_err = error_code;
  548. }
  549. static void svm_init_erratum_383(void)
  550. {
  551. u32 low, high;
  552. int err;
  553. u64 val;
  554. if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
  555. return;
  556. /* Use _safe variants to not break nested virtualization */
  557. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  558. if (err)
  559. return;
  560. val |= (1ULL << 47);
  561. low = lower_32_bits(val);
  562. high = upper_32_bits(val);
  563. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  564. erratum_383_found = true;
  565. }
  566. static void svm_init_osvw(struct kvm_vcpu *vcpu)
  567. {
  568. /*
  569. * Guests should see errata 400 and 415 as fixed (assuming that
  570. * HLT and IO instructions are intercepted).
  571. */
  572. vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
  573. vcpu->arch.osvw.status = osvw_status & ~(6ULL);
  574. /*
  575. * By increasing VCPU's osvw.length to 3 we are telling the guest that
  576. * all osvw.status bits inside that length, including bit 0 (which is
  577. * reserved for erratum 298), are valid. However, if host processor's
  578. * osvw_len is 0 then osvw_status[0] carries no information. We need to
  579. * be conservative here and therefore we tell the guest that erratum 298
  580. * is present (because we really don't know).
  581. */
  582. if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
  583. vcpu->arch.osvw.status |= 1;
  584. }
  585. static int has_svm(void)
  586. {
  587. const char *msg;
  588. if (!cpu_has_svm(&msg)) {
  589. printk(KERN_INFO "has_svm: %s\n", msg);
  590. return 0;
  591. }
  592. return 1;
  593. }
  594. static void svm_hardware_disable(void)
  595. {
  596. /* Make sure we clean up behind us */
  597. if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
  598. wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
  599. cpu_svm_disable();
  600. amd_pmu_disable_virt();
  601. }
  602. static int svm_hardware_enable(void)
  603. {
  604. struct svm_cpu_data *sd;
  605. uint64_t efer;
  606. struct desc_struct *gdt;
  607. int me = raw_smp_processor_id();
  608. rdmsrl(MSR_EFER, efer);
  609. if (efer & EFER_SVME)
  610. return -EBUSY;
  611. if (!has_svm()) {
  612. pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
  613. return -EINVAL;
  614. }
  615. sd = per_cpu(svm_data, me);
  616. if (!sd) {
  617. pr_err("%s: svm_data is NULL on %d\n", __func__, me);
  618. return -EINVAL;
  619. }
  620. sd->asid_generation = 1;
  621. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  622. sd->next_asid = sd->max_asid + 1;
  623. gdt = get_current_gdt_rw();
  624. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  625. wrmsrl(MSR_EFER, efer | EFER_SVME);
  626. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  627. if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  628. wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
  629. __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
  630. }
  631. /*
  632. * Get OSVW bits.
  633. *
  634. * Note that it is possible to have a system with mixed processor
  635. * revisions and therefore different OSVW bits. If bits are not the same
  636. * on different processors then choose the worst case (i.e. if erratum
  637. * is present on one processor and not on another then assume that the
  638. * erratum is present everywhere).
  639. */
  640. if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
  641. uint64_t len, status = 0;
  642. int err;
  643. len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
  644. if (!err)
  645. status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
  646. &err);
  647. if (err)
  648. osvw_status = osvw_len = 0;
  649. else {
  650. if (len < osvw_len)
  651. osvw_len = len;
  652. osvw_status |= status;
  653. osvw_status &= (1ULL << osvw_len) - 1;
  654. }
  655. } else
  656. osvw_status = osvw_len = 0;
  657. svm_init_erratum_383();
  658. amd_pmu_enable_virt();
  659. return 0;
  660. }
  661. static void svm_cpu_uninit(int cpu)
  662. {
  663. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  664. if (!sd)
  665. return;
  666. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  667. __free_page(sd->save_area);
  668. kfree(sd);
  669. }
  670. static int svm_cpu_init(int cpu)
  671. {
  672. struct svm_cpu_data *sd;
  673. int r;
  674. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  675. if (!sd)
  676. return -ENOMEM;
  677. sd->cpu = cpu;
  678. sd->save_area = alloc_page(GFP_KERNEL);
  679. r = -ENOMEM;
  680. if (!sd->save_area)
  681. goto err_1;
  682. per_cpu(svm_data, cpu) = sd;
  683. return 0;
  684. err_1:
  685. kfree(sd);
  686. return r;
  687. }
  688. static bool valid_msr_intercept(u32 index)
  689. {
  690. int i;
  691. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  692. if (direct_access_msrs[i].index == index)
  693. return true;
  694. return false;
  695. }
  696. static void set_msr_interception(u32 *msrpm, unsigned msr,
  697. int read, int write)
  698. {
  699. u8 bit_read, bit_write;
  700. unsigned long tmp;
  701. u32 offset;
  702. /*
  703. * If this warning triggers extend the direct_access_msrs list at the
  704. * beginning of the file
  705. */
  706. WARN_ON(!valid_msr_intercept(msr));
  707. offset = svm_msrpm_offset(msr);
  708. bit_read = 2 * (msr & 0x0f);
  709. bit_write = 2 * (msr & 0x0f) + 1;
  710. tmp = msrpm[offset];
  711. BUG_ON(offset == MSR_INVALID);
  712. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  713. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  714. msrpm[offset] = tmp;
  715. }
  716. static void svm_vcpu_init_msrpm(u32 *msrpm)
  717. {
  718. int i;
  719. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  720. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  721. if (!direct_access_msrs[i].always)
  722. continue;
  723. set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
  724. }
  725. }
  726. static void add_msr_offset(u32 offset)
  727. {
  728. int i;
  729. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  730. /* Offset already in list? */
  731. if (msrpm_offsets[i] == offset)
  732. return;
  733. /* Slot used by another offset? */
  734. if (msrpm_offsets[i] != MSR_INVALID)
  735. continue;
  736. /* Add offset to list */
  737. msrpm_offsets[i] = offset;
  738. return;
  739. }
  740. /*
  741. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  742. * increase MSRPM_OFFSETS in this case.
  743. */
  744. BUG();
  745. }
  746. static void init_msrpm_offsets(void)
  747. {
  748. int i;
  749. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  750. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  751. u32 offset;
  752. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  753. BUG_ON(offset == MSR_INVALID);
  754. add_msr_offset(offset);
  755. }
  756. }
  757. static void svm_enable_lbrv(struct vcpu_svm *svm)
  758. {
  759. u32 *msrpm = svm->msrpm;
  760. svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
  761. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  762. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  763. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  764. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  765. }
  766. static void svm_disable_lbrv(struct vcpu_svm *svm)
  767. {
  768. u32 *msrpm = svm->msrpm;
  769. svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
  770. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  771. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  772. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  773. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  774. }
  775. static void disable_nmi_singlestep(struct vcpu_svm *svm)
  776. {
  777. svm->nmi_singlestep = false;
  778. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
  779. /* Clear our flags if they were not set by the guest */
  780. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
  781. svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
  782. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
  783. svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
  784. }
  785. }
  786. /* Note:
  787. * This hash table is used to map VM_ID to a struct kvm_arch,
  788. * when handling AMD IOMMU GALOG notification to schedule in
  789. * a particular vCPU.
  790. */
  791. #define SVM_VM_DATA_HASH_BITS 8
  792. static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
  793. static u32 next_vm_id = 0;
  794. static bool next_vm_id_wrapped = 0;
  795. static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
  796. /* Note:
  797. * This function is called from IOMMU driver to notify
  798. * SVM to schedule in a particular vCPU of a particular VM.
  799. */
  800. static int avic_ga_log_notifier(u32 ga_tag)
  801. {
  802. unsigned long flags;
  803. struct kvm_arch *ka = NULL;
  804. struct kvm_vcpu *vcpu = NULL;
  805. u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
  806. u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
  807. pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
  808. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  809. hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
  810. struct kvm *kvm = container_of(ka, struct kvm, arch);
  811. struct kvm_arch *vm_data = &kvm->arch;
  812. if (vm_data->avic_vm_id != vm_id)
  813. continue;
  814. vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
  815. break;
  816. }
  817. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  818. if (!vcpu)
  819. return 0;
  820. /* Note:
  821. * At this point, the IOMMU should have already set the pending
  822. * bit in the vAPIC backing page. So, we just need to schedule
  823. * in the vcpu.
  824. */
  825. if (vcpu->mode == OUTSIDE_GUEST_MODE)
  826. kvm_vcpu_wake_up(vcpu);
  827. return 0;
  828. }
  829. static __init int svm_hardware_setup(void)
  830. {
  831. int cpu;
  832. struct page *iopm_pages;
  833. void *iopm_va;
  834. int r;
  835. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  836. if (!iopm_pages)
  837. return -ENOMEM;
  838. iopm_va = page_address(iopm_pages);
  839. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  840. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  841. init_msrpm_offsets();
  842. if (boot_cpu_has(X86_FEATURE_NX))
  843. kvm_enable_efer_bits(EFER_NX);
  844. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  845. kvm_enable_efer_bits(EFER_FFXSR);
  846. if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  847. kvm_has_tsc_control = true;
  848. kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
  849. kvm_tsc_scaling_ratio_frac_bits = 32;
  850. }
  851. if (nested) {
  852. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  853. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  854. }
  855. for_each_possible_cpu(cpu) {
  856. r = svm_cpu_init(cpu);
  857. if (r)
  858. goto err;
  859. }
  860. if (!boot_cpu_has(X86_FEATURE_NPT))
  861. npt_enabled = false;
  862. if (npt_enabled && !npt) {
  863. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  864. npt_enabled = false;
  865. }
  866. if (npt_enabled) {
  867. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  868. kvm_enable_tdp();
  869. } else
  870. kvm_disable_tdp();
  871. if (avic) {
  872. if (!npt_enabled ||
  873. !boot_cpu_has(X86_FEATURE_AVIC) ||
  874. !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
  875. avic = false;
  876. } else {
  877. pr_info("AVIC enabled\n");
  878. amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
  879. }
  880. }
  881. if (vls) {
  882. if (!npt_enabled ||
  883. !boot_cpu_has(X86_FEATURE_VIRTUAL_VMLOAD_VMSAVE) ||
  884. !IS_ENABLED(CONFIG_X86_64)) {
  885. vls = false;
  886. } else {
  887. pr_info("Virtual VMLOAD VMSAVE supported\n");
  888. }
  889. }
  890. if (vgif) {
  891. if (!boot_cpu_has(X86_FEATURE_VGIF))
  892. vgif = false;
  893. else
  894. pr_info("Virtual GIF supported\n");
  895. }
  896. return 0;
  897. err:
  898. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  899. iopm_base = 0;
  900. return r;
  901. }
  902. static __exit void svm_hardware_unsetup(void)
  903. {
  904. int cpu;
  905. for_each_possible_cpu(cpu)
  906. svm_cpu_uninit(cpu);
  907. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  908. iopm_base = 0;
  909. }
  910. static void init_seg(struct vmcb_seg *seg)
  911. {
  912. seg->selector = 0;
  913. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  914. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  915. seg->limit = 0xffff;
  916. seg->base = 0;
  917. }
  918. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  919. {
  920. seg->selector = 0;
  921. seg->attrib = SVM_SELECTOR_P_MASK | type;
  922. seg->limit = 0xffff;
  923. seg->base = 0;
  924. }
  925. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  926. {
  927. struct vcpu_svm *svm = to_svm(vcpu);
  928. u64 g_tsc_offset = 0;
  929. if (is_guest_mode(vcpu)) {
  930. g_tsc_offset = svm->vmcb->control.tsc_offset -
  931. svm->nested.hsave->control.tsc_offset;
  932. svm->nested.hsave->control.tsc_offset = offset;
  933. } else
  934. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  935. svm->vmcb->control.tsc_offset,
  936. offset);
  937. svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
  938. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  939. }
  940. static void avic_init_vmcb(struct vcpu_svm *svm)
  941. {
  942. struct vmcb *vmcb = svm->vmcb;
  943. struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
  944. phys_addr_t bpa = page_to_phys(svm->avic_backing_page);
  945. phys_addr_t lpa = page_to_phys(vm_data->avic_logical_id_table_page);
  946. phys_addr_t ppa = page_to_phys(vm_data->avic_physical_id_table_page);
  947. vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
  948. vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
  949. vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
  950. vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
  951. vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
  952. svm->vcpu.arch.apicv_active = true;
  953. }
  954. static void init_vmcb(struct vcpu_svm *svm)
  955. {
  956. struct vmcb_control_area *control = &svm->vmcb->control;
  957. struct vmcb_save_area *save = &svm->vmcb->save;
  958. svm->vcpu.arch.hflags = 0;
  959. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  960. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  961. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  962. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  963. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  964. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  965. if (!kvm_vcpu_apicv_active(&svm->vcpu))
  966. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  967. set_dr_intercepts(svm);
  968. set_exception_intercept(svm, PF_VECTOR);
  969. set_exception_intercept(svm, UD_VECTOR);
  970. set_exception_intercept(svm, MC_VECTOR);
  971. set_exception_intercept(svm, AC_VECTOR);
  972. set_exception_intercept(svm, DB_VECTOR);
  973. set_intercept(svm, INTERCEPT_INTR);
  974. set_intercept(svm, INTERCEPT_NMI);
  975. set_intercept(svm, INTERCEPT_SMI);
  976. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  977. set_intercept(svm, INTERCEPT_RDPMC);
  978. set_intercept(svm, INTERCEPT_CPUID);
  979. set_intercept(svm, INTERCEPT_INVD);
  980. set_intercept(svm, INTERCEPT_HLT);
  981. set_intercept(svm, INTERCEPT_INVLPG);
  982. set_intercept(svm, INTERCEPT_INVLPGA);
  983. set_intercept(svm, INTERCEPT_IOIO_PROT);
  984. set_intercept(svm, INTERCEPT_MSR_PROT);
  985. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  986. set_intercept(svm, INTERCEPT_SHUTDOWN);
  987. set_intercept(svm, INTERCEPT_VMRUN);
  988. set_intercept(svm, INTERCEPT_VMMCALL);
  989. set_intercept(svm, INTERCEPT_VMLOAD);
  990. set_intercept(svm, INTERCEPT_VMSAVE);
  991. set_intercept(svm, INTERCEPT_STGI);
  992. set_intercept(svm, INTERCEPT_CLGI);
  993. set_intercept(svm, INTERCEPT_SKINIT);
  994. set_intercept(svm, INTERCEPT_WBINVD);
  995. set_intercept(svm, INTERCEPT_XSETBV);
  996. if (!kvm_mwait_in_guest()) {
  997. set_intercept(svm, INTERCEPT_MONITOR);
  998. set_intercept(svm, INTERCEPT_MWAIT);
  999. }
  1000. control->iopm_base_pa = iopm_base;
  1001. control->msrpm_base_pa = __pa(svm->msrpm);
  1002. control->int_ctl = V_INTR_MASKING_MASK;
  1003. init_seg(&save->es);
  1004. init_seg(&save->ss);
  1005. init_seg(&save->ds);
  1006. init_seg(&save->fs);
  1007. init_seg(&save->gs);
  1008. save->cs.selector = 0xf000;
  1009. save->cs.base = 0xffff0000;
  1010. /* Executable/Readable Code Segment */
  1011. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  1012. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  1013. save->cs.limit = 0xffff;
  1014. save->gdtr.limit = 0xffff;
  1015. save->idtr.limit = 0xffff;
  1016. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  1017. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  1018. svm_set_efer(&svm->vcpu, 0);
  1019. save->dr6 = 0xffff0ff0;
  1020. kvm_set_rflags(&svm->vcpu, 2);
  1021. save->rip = 0x0000fff0;
  1022. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  1023. /*
  1024. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  1025. * It also updates the guest-visible cr0 value.
  1026. */
  1027. svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  1028. kvm_mmu_reset_context(&svm->vcpu);
  1029. save->cr4 = X86_CR4_PAE;
  1030. /* rdx = ?? */
  1031. if (npt_enabled) {
  1032. /* Setup VMCB for Nested Paging */
  1033. control->nested_ctl = 1;
  1034. clr_intercept(svm, INTERCEPT_INVLPG);
  1035. clr_exception_intercept(svm, PF_VECTOR);
  1036. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  1037. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  1038. save->g_pat = svm->vcpu.arch.pat;
  1039. save->cr3 = 0;
  1040. save->cr4 = 0;
  1041. }
  1042. svm->asid_generation = 0;
  1043. svm->nested.vmcb = 0;
  1044. svm->vcpu.arch.hflags = 0;
  1045. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  1046. control->pause_filter_count = 3000;
  1047. set_intercept(svm, INTERCEPT_PAUSE);
  1048. }
  1049. if (avic)
  1050. avic_init_vmcb(svm);
  1051. /*
  1052. * If hardware supports Virtual VMLOAD VMSAVE then enable it
  1053. * in VMCB and clear intercepts to avoid #VMEXIT.
  1054. */
  1055. if (vls) {
  1056. clr_intercept(svm, INTERCEPT_VMLOAD);
  1057. clr_intercept(svm, INTERCEPT_VMSAVE);
  1058. svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
  1059. }
  1060. if (vgif) {
  1061. clr_intercept(svm, INTERCEPT_STGI);
  1062. clr_intercept(svm, INTERCEPT_CLGI);
  1063. svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
  1064. }
  1065. mark_all_dirty(svm->vmcb);
  1066. enable_gif(svm);
  1067. }
  1068. static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu,
  1069. unsigned int index)
  1070. {
  1071. u64 *avic_physical_id_table;
  1072. struct kvm_arch *vm_data = &vcpu->kvm->arch;
  1073. if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
  1074. return NULL;
  1075. avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
  1076. return &avic_physical_id_table[index];
  1077. }
  1078. /**
  1079. * Note:
  1080. * AVIC hardware walks the nested page table to check permissions,
  1081. * but does not use the SPA address specified in the leaf page
  1082. * table entry since it uses address in the AVIC_BACKING_PAGE pointer
  1083. * field of the VMCB. Therefore, we set up the
  1084. * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
  1085. */
  1086. static int avic_init_access_page(struct kvm_vcpu *vcpu)
  1087. {
  1088. struct kvm *kvm = vcpu->kvm;
  1089. int ret;
  1090. if (kvm->arch.apic_access_page_done)
  1091. return 0;
  1092. ret = x86_set_memory_region(kvm,
  1093. APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  1094. APIC_DEFAULT_PHYS_BASE,
  1095. PAGE_SIZE);
  1096. if (ret)
  1097. return ret;
  1098. kvm->arch.apic_access_page_done = true;
  1099. return 0;
  1100. }
  1101. static int avic_init_backing_page(struct kvm_vcpu *vcpu)
  1102. {
  1103. int ret;
  1104. u64 *entry, new_entry;
  1105. int id = vcpu->vcpu_id;
  1106. struct vcpu_svm *svm = to_svm(vcpu);
  1107. ret = avic_init_access_page(vcpu);
  1108. if (ret)
  1109. return ret;
  1110. if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
  1111. return -EINVAL;
  1112. if (!svm->vcpu.arch.apic->regs)
  1113. return -EINVAL;
  1114. svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
  1115. /* Setting AVIC backing page address in the phy APIC ID table */
  1116. entry = avic_get_physical_id_entry(vcpu, id);
  1117. if (!entry)
  1118. return -EINVAL;
  1119. new_entry = READ_ONCE(*entry);
  1120. new_entry = (page_to_phys(svm->avic_backing_page) &
  1121. AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
  1122. AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
  1123. WRITE_ONCE(*entry, new_entry);
  1124. svm->avic_physical_id_cache = entry;
  1125. return 0;
  1126. }
  1127. static void avic_vm_destroy(struct kvm *kvm)
  1128. {
  1129. unsigned long flags;
  1130. struct kvm_arch *vm_data = &kvm->arch;
  1131. if (!avic)
  1132. return;
  1133. if (vm_data->avic_logical_id_table_page)
  1134. __free_page(vm_data->avic_logical_id_table_page);
  1135. if (vm_data->avic_physical_id_table_page)
  1136. __free_page(vm_data->avic_physical_id_table_page);
  1137. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  1138. hash_del(&vm_data->hnode);
  1139. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  1140. }
  1141. static int avic_vm_init(struct kvm *kvm)
  1142. {
  1143. unsigned long flags;
  1144. int err = -ENOMEM;
  1145. struct kvm_arch *vm_data = &kvm->arch;
  1146. struct page *p_page;
  1147. struct page *l_page;
  1148. struct kvm_arch *ka;
  1149. u32 vm_id;
  1150. if (!avic)
  1151. return 0;
  1152. /* Allocating physical APIC ID table (4KB) */
  1153. p_page = alloc_page(GFP_KERNEL);
  1154. if (!p_page)
  1155. goto free_avic;
  1156. vm_data->avic_physical_id_table_page = p_page;
  1157. clear_page(page_address(p_page));
  1158. /* Allocating logical APIC ID table (4KB) */
  1159. l_page = alloc_page(GFP_KERNEL);
  1160. if (!l_page)
  1161. goto free_avic;
  1162. vm_data->avic_logical_id_table_page = l_page;
  1163. clear_page(page_address(l_page));
  1164. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  1165. again:
  1166. vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK;
  1167. if (vm_id == 0) { /* id is 1-based, zero is not okay */
  1168. next_vm_id_wrapped = 1;
  1169. goto again;
  1170. }
  1171. /* Is it still in use? Only possible if wrapped at least once */
  1172. if (next_vm_id_wrapped) {
  1173. hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
  1174. struct kvm *k2 = container_of(ka, struct kvm, arch);
  1175. struct kvm_arch *vd2 = &k2->arch;
  1176. if (vd2->avic_vm_id == vm_id)
  1177. goto again;
  1178. }
  1179. }
  1180. vm_data->avic_vm_id = vm_id;
  1181. hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
  1182. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  1183. return 0;
  1184. free_avic:
  1185. avic_vm_destroy(kvm);
  1186. return err;
  1187. }
  1188. static inline int
  1189. avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
  1190. {
  1191. int ret = 0;
  1192. unsigned long flags;
  1193. struct amd_svm_iommu_ir *ir;
  1194. struct vcpu_svm *svm = to_svm(vcpu);
  1195. if (!kvm_arch_has_assigned_device(vcpu->kvm))
  1196. return 0;
  1197. /*
  1198. * Here, we go through the per-vcpu ir_list to update all existing
  1199. * interrupt remapping table entry targeting this vcpu.
  1200. */
  1201. spin_lock_irqsave(&svm->ir_list_lock, flags);
  1202. if (list_empty(&svm->ir_list))
  1203. goto out;
  1204. list_for_each_entry(ir, &svm->ir_list, node) {
  1205. ret = amd_iommu_update_ga(cpu, r, ir->data);
  1206. if (ret)
  1207. break;
  1208. }
  1209. out:
  1210. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  1211. return ret;
  1212. }
  1213. static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1214. {
  1215. u64 entry;
  1216. /* ID = 0xff (broadcast), ID > 0xff (reserved) */
  1217. int h_physical_id = kvm_cpu_get_apicid(cpu);
  1218. struct vcpu_svm *svm = to_svm(vcpu);
  1219. if (!kvm_vcpu_apicv_active(vcpu))
  1220. return;
  1221. if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
  1222. return;
  1223. entry = READ_ONCE(*(svm->avic_physical_id_cache));
  1224. WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
  1225. entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
  1226. entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
  1227. entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1228. if (svm->avic_is_running)
  1229. entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1230. WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
  1231. avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
  1232. svm->avic_is_running);
  1233. }
  1234. static void avic_vcpu_put(struct kvm_vcpu *vcpu)
  1235. {
  1236. u64 entry;
  1237. struct vcpu_svm *svm = to_svm(vcpu);
  1238. if (!kvm_vcpu_apicv_active(vcpu))
  1239. return;
  1240. entry = READ_ONCE(*(svm->avic_physical_id_cache));
  1241. if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
  1242. avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
  1243. entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1244. WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
  1245. }
  1246. /**
  1247. * This function is called during VCPU halt/unhalt.
  1248. */
  1249. static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
  1250. {
  1251. struct vcpu_svm *svm = to_svm(vcpu);
  1252. svm->avic_is_running = is_run;
  1253. if (is_run)
  1254. avic_vcpu_load(vcpu, vcpu->cpu);
  1255. else
  1256. avic_vcpu_put(vcpu);
  1257. }
  1258. static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  1259. {
  1260. struct vcpu_svm *svm = to_svm(vcpu);
  1261. u32 dummy;
  1262. u32 eax = 1;
  1263. if (!init_event) {
  1264. svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
  1265. MSR_IA32_APICBASE_ENABLE;
  1266. if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
  1267. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  1268. }
  1269. init_vmcb(svm);
  1270. kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
  1271. kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
  1272. if (kvm_vcpu_apicv_active(vcpu) && !init_event)
  1273. avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
  1274. }
  1275. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  1276. {
  1277. struct vcpu_svm *svm;
  1278. struct page *page;
  1279. struct page *msrpm_pages;
  1280. struct page *hsave_page;
  1281. struct page *nested_msrpm_pages;
  1282. int err;
  1283. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1284. if (!svm) {
  1285. err = -ENOMEM;
  1286. goto out;
  1287. }
  1288. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  1289. if (err)
  1290. goto free_svm;
  1291. err = -ENOMEM;
  1292. page = alloc_page(GFP_KERNEL);
  1293. if (!page)
  1294. goto uninit;
  1295. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  1296. if (!msrpm_pages)
  1297. goto free_page1;
  1298. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  1299. if (!nested_msrpm_pages)
  1300. goto free_page2;
  1301. hsave_page = alloc_page(GFP_KERNEL);
  1302. if (!hsave_page)
  1303. goto free_page3;
  1304. if (avic) {
  1305. err = avic_init_backing_page(&svm->vcpu);
  1306. if (err)
  1307. goto free_page4;
  1308. INIT_LIST_HEAD(&svm->ir_list);
  1309. spin_lock_init(&svm->ir_list_lock);
  1310. }
  1311. /* We initialize this flag to true to make sure that the is_running
  1312. * bit would be set the first time the vcpu is loaded.
  1313. */
  1314. svm->avic_is_running = true;
  1315. svm->nested.hsave = page_address(hsave_page);
  1316. svm->msrpm = page_address(msrpm_pages);
  1317. svm_vcpu_init_msrpm(svm->msrpm);
  1318. svm->nested.msrpm = page_address(nested_msrpm_pages);
  1319. svm_vcpu_init_msrpm(svm->nested.msrpm);
  1320. svm->vmcb = page_address(page);
  1321. clear_page(svm->vmcb);
  1322. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  1323. svm->asid_generation = 0;
  1324. init_vmcb(svm);
  1325. svm_init_osvw(&svm->vcpu);
  1326. return &svm->vcpu;
  1327. free_page4:
  1328. __free_page(hsave_page);
  1329. free_page3:
  1330. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  1331. free_page2:
  1332. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  1333. free_page1:
  1334. __free_page(page);
  1335. uninit:
  1336. kvm_vcpu_uninit(&svm->vcpu);
  1337. free_svm:
  1338. kmem_cache_free(kvm_vcpu_cache, svm);
  1339. out:
  1340. return ERR_PTR(err);
  1341. }
  1342. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  1343. {
  1344. struct vcpu_svm *svm = to_svm(vcpu);
  1345. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  1346. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  1347. __free_page(virt_to_page(svm->nested.hsave));
  1348. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  1349. kvm_vcpu_uninit(vcpu);
  1350. kmem_cache_free(kvm_vcpu_cache, svm);
  1351. }
  1352. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1353. {
  1354. struct vcpu_svm *svm = to_svm(vcpu);
  1355. int i;
  1356. if (unlikely(cpu != vcpu->cpu)) {
  1357. svm->asid_generation = 0;
  1358. mark_all_dirty(svm->vmcb);
  1359. }
  1360. #ifdef CONFIG_X86_64
  1361. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  1362. #endif
  1363. savesegment(fs, svm->host.fs);
  1364. savesegment(gs, svm->host.gs);
  1365. svm->host.ldt = kvm_read_ldt();
  1366. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1367. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1368. if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  1369. u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
  1370. if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
  1371. __this_cpu_write(current_tsc_ratio, tsc_ratio);
  1372. wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
  1373. }
  1374. }
  1375. /* This assumes that the kernel never uses MSR_TSC_AUX */
  1376. if (static_cpu_has(X86_FEATURE_RDTSCP))
  1377. wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
  1378. avic_vcpu_load(vcpu, cpu);
  1379. }
  1380. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  1381. {
  1382. struct vcpu_svm *svm = to_svm(vcpu);
  1383. int i;
  1384. avic_vcpu_put(vcpu);
  1385. ++vcpu->stat.host_state_reload;
  1386. kvm_load_ldt(svm->host.ldt);
  1387. #ifdef CONFIG_X86_64
  1388. loadsegment(fs, svm->host.fs);
  1389. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
  1390. load_gs_index(svm->host.gs);
  1391. #else
  1392. #ifdef CONFIG_X86_32_LAZY_GS
  1393. loadsegment(gs, svm->host.gs);
  1394. #endif
  1395. #endif
  1396. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1397. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1398. }
  1399. static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
  1400. {
  1401. avic_set_running(vcpu, false);
  1402. }
  1403. static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
  1404. {
  1405. avic_set_running(vcpu, true);
  1406. }
  1407. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  1408. {
  1409. struct vcpu_svm *svm = to_svm(vcpu);
  1410. unsigned long rflags = svm->vmcb->save.rflags;
  1411. if (svm->nmi_singlestep) {
  1412. /* Hide our flags if they were not set by the guest */
  1413. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
  1414. rflags &= ~X86_EFLAGS_TF;
  1415. if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
  1416. rflags &= ~X86_EFLAGS_RF;
  1417. }
  1418. return rflags;
  1419. }
  1420. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1421. {
  1422. if (to_svm(vcpu)->nmi_singlestep)
  1423. rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  1424. /*
  1425. * Any change of EFLAGS.VM is accompanied by a reload of SS
  1426. * (caused by either a task switch or an inter-privilege IRET),
  1427. * so we do not need to update the CPL here.
  1428. */
  1429. to_svm(vcpu)->vmcb->save.rflags = rflags;
  1430. }
  1431. static u32 svm_get_pkru(struct kvm_vcpu *vcpu)
  1432. {
  1433. return 0;
  1434. }
  1435. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1436. {
  1437. switch (reg) {
  1438. case VCPU_EXREG_PDPTR:
  1439. BUG_ON(!npt_enabled);
  1440. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  1441. break;
  1442. default:
  1443. BUG();
  1444. }
  1445. }
  1446. static void svm_set_vintr(struct vcpu_svm *svm)
  1447. {
  1448. set_intercept(svm, INTERCEPT_VINTR);
  1449. }
  1450. static void svm_clear_vintr(struct vcpu_svm *svm)
  1451. {
  1452. clr_intercept(svm, INTERCEPT_VINTR);
  1453. }
  1454. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  1455. {
  1456. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1457. switch (seg) {
  1458. case VCPU_SREG_CS: return &save->cs;
  1459. case VCPU_SREG_DS: return &save->ds;
  1460. case VCPU_SREG_ES: return &save->es;
  1461. case VCPU_SREG_FS: return &save->fs;
  1462. case VCPU_SREG_GS: return &save->gs;
  1463. case VCPU_SREG_SS: return &save->ss;
  1464. case VCPU_SREG_TR: return &save->tr;
  1465. case VCPU_SREG_LDTR: return &save->ldtr;
  1466. }
  1467. BUG();
  1468. return NULL;
  1469. }
  1470. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1471. {
  1472. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1473. return s->base;
  1474. }
  1475. static void svm_get_segment(struct kvm_vcpu *vcpu,
  1476. struct kvm_segment *var, int seg)
  1477. {
  1478. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1479. var->base = s->base;
  1480. var->limit = s->limit;
  1481. var->selector = s->selector;
  1482. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  1483. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  1484. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1485. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  1486. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  1487. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  1488. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  1489. /*
  1490. * AMD CPUs circa 2014 track the G bit for all segments except CS.
  1491. * However, the SVM spec states that the G bit is not observed by the
  1492. * CPU, and some VMware virtual CPUs drop the G bit for all segments.
  1493. * So let's synthesize a legal G bit for all segments, this helps
  1494. * running KVM nested. It also helps cross-vendor migration, because
  1495. * Intel's vmentry has a check on the 'G' bit.
  1496. */
  1497. var->g = s->limit > 0xfffff;
  1498. /*
  1499. * AMD's VMCB does not have an explicit unusable field, so emulate it
  1500. * for cross vendor migration purposes by "not present"
  1501. */
  1502. var->unusable = !var->present;
  1503. switch (seg) {
  1504. case VCPU_SREG_TR:
  1505. /*
  1506. * Work around a bug where the busy flag in the tr selector
  1507. * isn't exposed
  1508. */
  1509. var->type |= 0x2;
  1510. break;
  1511. case VCPU_SREG_DS:
  1512. case VCPU_SREG_ES:
  1513. case VCPU_SREG_FS:
  1514. case VCPU_SREG_GS:
  1515. /*
  1516. * The accessed bit must always be set in the segment
  1517. * descriptor cache, although it can be cleared in the
  1518. * descriptor, the cached bit always remains at 1. Since
  1519. * Intel has a check on this, set it here to support
  1520. * cross-vendor migration.
  1521. */
  1522. if (!var->unusable)
  1523. var->type |= 0x1;
  1524. break;
  1525. case VCPU_SREG_SS:
  1526. /*
  1527. * On AMD CPUs sometimes the DB bit in the segment
  1528. * descriptor is left as 1, although the whole segment has
  1529. * been made unusable. Clear it here to pass an Intel VMX
  1530. * entry check when cross vendor migrating.
  1531. */
  1532. if (var->unusable)
  1533. var->db = 0;
  1534. /* This is symmetric with svm_set_segment() */
  1535. var->dpl = to_svm(vcpu)->vmcb->save.cpl;
  1536. break;
  1537. }
  1538. }
  1539. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1540. {
  1541. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1542. return save->cpl;
  1543. }
  1544. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1545. {
  1546. struct vcpu_svm *svm = to_svm(vcpu);
  1547. dt->size = svm->vmcb->save.idtr.limit;
  1548. dt->address = svm->vmcb->save.idtr.base;
  1549. }
  1550. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1551. {
  1552. struct vcpu_svm *svm = to_svm(vcpu);
  1553. svm->vmcb->save.idtr.limit = dt->size;
  1554. svm->vmcb->save.idtr.base = dt->address ;
  1555. mark_dirty(svm->vmcb, VMCB_DT);
  1556. }
  1557. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1558. {
  1559. struct vcpu_svm *svm = to_svm(vcpu);
  1560. dt->size = svm->vmcb->save.gdtr.limit;
  1561. dt->address = svm->vmcb->save.gdtr.base;
  1562. }
  1563. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1564. {
  1565. struct vcpu_svm *svm = to_svm(vcpu);
  1566. svm->vmcb->save.gdtr.limit = dt->size;
  1567. svm->vmcb->save.gdtr.base = dt->address ;
  1568. mark_dirty(svm->vmcb, VMCB_DT);
  1569. }
  1570. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1571. {
  1572. }
  1573. static void svm_decache_cr3(struct kvm_vcpu *vcpu)
  1574. {
  1575. }
  1576. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1577. {
  1578. }
  1579. static void update_cr0_intercept(struct vcpu_svm *svm)
  1580. {
  1581. ulong gcr0 = svm->vcpu.arch.cr0;
  1582. u64 *hcr0 = &svm->vmcb->save.cr0;
  1583. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1584. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1585. mark_dirty(svm->vmcb, VMCB_CR);
  1586. if (gcr0 == *hcr0) {
  1587. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  1588. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1589. } else {
  1590. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1591. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1592. }
  1593. }
  1594. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1595. {
  1596. struct vcpu_svm *svm = to_svm(vcpu);
  1597. #ifdef CONFIG_X86_64
  1598. if (vcpu->arch.efer & EFER_LME) {
  1599. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1600. vcpu->arch.efer |= EFER_LMA;
  1601. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1602. }
  1603. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1604. vcpu->arch.efer &= ~EFER_LMA;
  1605. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1606. }
  1607. }
  1608. #endif
  1609. vcpu->arch.cr0 = cr0;
  1610. if (!npt_enabled)
  1611. cr0 |= X86_CR0_PG | X86_CR0_WP;
  1612. /*
  1613. * re-enable caching here because the QEMU bios
  1614. * does not do it - this results in some delay at
  1615. * reboot
  1616. */
  1617. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  1618. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1619. svm->vmcb->save.cr0 = cr0;
  1620. mark_dirty(svm->vmcb, VMCB_CR);
  1621. update_cr0_intercept(svm);
  1622. }
  1623. static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1624. {
  1625. unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
  1626. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  1627. if (cr4 & X86_CR4_VMXE)
  1628. return 1;
  1629. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1630. svm_flush_tlb(vcpu);
  1631. vcpu->arch.cr4 = cr4;
  1632. if (!npt_enabled)
  1633. cr4 |= X86_CR4_PAE;
  1634. cr4 |= host_cr4_mce;
  1635. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1636. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  1637. return 0;
  1638. }
  1639. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1640. struct kvm_segment *var, int seg)
  1641. {
  1642. struct vcpu_svm *svm = to_svm(vcpu);
  1643. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1644. s->base = var->base;
  1645. s->limit = var->limit;
  1646. s->selector = var->selector;
  1647. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1648. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1649. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1650. s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
  1651. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1652. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1653. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1654. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1655. /*
  1656. * This is always accurate, except if SYSRET returned to a segment
  1657. * with SS.DPL != 3. Intel does not have this quirk, and always
  1658. * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
  1659. * would entail passing the CPL to userspace and back.
  1660. */
  1661. if (seg == VCPU_SREG_SS)
  1662. /* This is symmetric with svm_get_segment() */
  1663. svm->vmcb->save.cpl = (var->dpl & 3);
  1664. mark_dirty(svm->vmcb, VMCB_SEG);
  1665. }
  1666. static void update_bp_intercept(struct kvm_vcpu *vcpu)
  1667. {
  1668. struct vcpu_svm *svm = to_svm(vcpu);
  1669. clr_exception_intercept(svm, BP_VECTOR);
  1670. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1671. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1672. set_exception_intercept(svm, BP_VECTOR);
  1673. } else
  1674. vcpu->guest_debug = 0;
  1675. }
  1676. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1677. {
  1678. if (sd->next_asid > sd->max_asid) {
  1679. ++sd->asid_generation;
  1680. sd->next_asid = 1;
  1681. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1682. }
  1683. svm->asid_generation = sd->asid_generation;
  1684. svm->vmcb->control.asid = sd->next_asid++;
  1685. mark_dirty(svm->vmcb, VMCB_ASID);
  1686. }
  1687. static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
  1688. {
  1689. return to_svm(vcpu)->vmcb->save.dr6;
  1690. }
  1691. static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
  1692. {
  1693. struct vcpu_svm *svm = to_svm(vcpu);
  1694. svm->vmcb->save.dr6 = value;
  1695. mark_dirty(svm->vmcb, VMCB_DR);
  1696. }
  1697. static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  1698. {
  1699. struct vcpu_svm *svm = to_svm(vcpu);
  1700. get_debugreg(vcpu->arch.db[0], 0);
  1701. get_debugreg(vcpu->arch.db[1], 1);
  1702. get_debugreg(vcpu->arch.db[2], 2);
  1703. get_debugreg(vcpu->arch.db[3], 3);
  1704. vcpu->arch.dr6 = svm_get_dr6(vcpu);
  1705. vcpu->arch.dr7 = svm->vmcb->save.dr7;
  1706. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  1707. set_dr_intercepts(svm);
  1708. }
  1709. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1710. {
  1711. struct vcpu_svm *svm = to_svm(vcpu);
  1712. svm->vmcb->save.dr7 = value;
  1713. mark_dirty(svm->vmcb, VMCB_DR);
  1714. }
  1715. static int pf_interception(struct vcpu_svm *svm)
  1716. {
  1717. u64 fault_address = svm->vmcb->control.exit_info_2;
  1718. u64 error_code = svm->vmcb->control.exit_info_1;
  1719. return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
  1720. svm->vmcb->control.insn_bytes,
  1721. svm->vmcb->control.insn_len, !npt_enabled);
  1722. }
  1723. static int db_interception(struct vcpu_svm *svm)
  1724. {
  1725. struct kvm_run *kvm_run = svm->vcpu.run;
  1726. if (!(svm->vcpu.guest_debug &
  1727. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1728. !svm->nmi_singlestep) {
  1729. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1730. return 1;
  1731. }
  1732. if (svm->nmi_singlestep) {
  1733. disable_nmi_singlestep(svm);
  1734. }
  1735. if (svm->vcpu.guest_debug &
  1736. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1737. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1738. kvm_run->debug.arch.pc =
  1739. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1740. kvm_run->debug.arch.exception = DB_VECTOR;
  1741. return 0;
  1742. }
  1743. return 1;
  1744. }
  1745. static int bp_interception(struct vcpu_svm *svm)
  1746. {
  1747. struct kvm_run *kvm_run = svm->vcpu.run;
  1748. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1749. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1750. kvm_run->debug.arch.exception = BP_VECTOR;
  1751. return 0;
  1752. }
  1753. static int ud_interception(struct vcpu_svm *svm)
  1754. {
  1755. int er;
  1756. er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
  1757. if (er != EMULATE_DONE)
  1758. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1759. return 1;
  1760. }
  1761. static int ac_interception(struct vcpu_svm *svm)
  1762. {
  1763. kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
  1764. return 1;
  1765. }
  1766. static bool is_erratum_383(void)
  1767. {
  1768. int err, i;
  1769. u64 value;
  1770. if (!erratum_383_found)
  1771. return false;
  1772. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  1773. if (err)
  1774. return false;
  1775. /* Bit 62 may or may not be set for this mce */
  1776. value &= ~(1ULL << 62);
  1777. if (value != 0xb600000000010015ULL)
  1778. return false;
  1779. /* Clear MCi_STATUS registers */
  1780. for (i = 0; i < 6; ++i)
  1781. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  1782. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  1783. if (!err) {
  1784. u32 low, high;
  1785. value &= ~(1ULL << 2);
  1786. low = lower_32_bits(value);
  1787. high = upper_32_bits(value);
  1788. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  1789. }
  1790. /* Flush tlb to evict multi-match entries */
  1791. __flush_tlb_all();
  1792. return true;
  1793. }
  1794. static void svm_handle_mce(struct vcpu_svm *svm)
  1795. {
  1796. if (is_erratum_383()) {
  1797. /*
  1798. * Erratum 383 triggered. Guest state is corrupt so kill the
  1799. * guest.
  1800. */
  1801. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  1802. kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
  1803. return;
  1804. }
  1805. /*
  1806. * On an #MC intercept the MCE handler is not called automatically in
  1807. * the host. So do it by hand here.
  1808. */
  1809. asm volatile (
  1810. "int $0x12\n");
  1811. /* not sure if we ever come back to this point */
  1812. return;
  1813. }
  1814. static int mc_interception(struct vcpu_svm *svm)
  1815. {
  1816. return 1;
  1817. }
  1818. static int shutdown_interception(struct vcpu_svm *svm)
  1819. {
  1820. struct kvm_run *kvm_run = svm->vcpu.run;
  1821. /*
  1822. * VMCB is undefined after a SHUTDOWN intercept
  1823. * so reinitialize it.
  1824. */
  1825. clear_page(svm->vmcb);
  1826. init_vmcb(svm);
  1827. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1828. return 0;
  1829. }
  1830. static int io_interception(struct vcpu_svm *svm)
  1831. {
  1832. struct kvm_vcpu *vcpu = &svm->vcpu;
  1833. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1834. int size, in, string, ret;
  1835. unsigned port;
  1836. ++svm->vcpu.stat.io_exits;
  1837. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1838. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1839. if (string)
  1840. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  1841. port = io_info >> 16;
  1842. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1843. svm->next_rip = svm->vmcb->control.exit_info_2;
  1844. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  1845. /*
  1846. * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
  1847. * KVM_EXIT_DEBUG here.
  1848. */
  1849. if (in)
  1850. return kvm_fast_pio_in(vcpu, size, port) && ret;
  1851. else
  1852. return kvm_fast_pio_out(vcpu, size, port) && ret;
  1853. }
  1854. static int nmi_interception(struct vcpu_svm *svm)
  1855. {
  1856. return 1;
  1857. }
  1858. static int intr_interception(struct vcpu_svm *svm)
  1859. {
  1860. ++svm->vcpu.stat.irq_exits;
  1861. return 1;
  1862. }
  1863. static int nop_on_interception(struct vcpu_svm *svm)
  1864. {
  1865. return 1;
  1866. }
  1867. static int halt_interception(struct vcpu_svm *svm)
  1868. {
  1869. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1870. return kvm_emulate_halt(&svm->vcpu);
  1871. }
  1872. static int vmmcall_interception(struct vcpu_svm *svm)
  1873. {
  1874. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1875. return kvm_emulate_hypercall(&svm->vcpu);
  1876. }
  1877. static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
  1878. {
  1879. struct vcpu_svm *svm = to_svm(vcpu);
  1880. return svm->nested.nested_cr3;
  1881. }
  1882. static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
  1883. {
  1884. struct vcpu_svm *svm = to_svm(vcpu);
  1885. u64 cr3 = svm->nested.nested_cr3;
  1886. u64 pdpte;
  1887. int ret;
  1888. ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
  1889. offset_in_page(cr3) + index * 8, 8);
  1890. if (ret)
  1891. return 0;
  1892. return pdpte;
  1893. }
  1894. static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
  1895. unsigned long root)
  1896. {
  1897. struct vcpu_svm *svm = to_svm(vcpu);
  1898. svm->vmcb->control.nested_cr3 = root;
  1899. mark_dirty(svm->vmcb, VMCB_NPT);
  1900. svm_flush_tlb(vcpu);
  1901. }
  1902. static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
  1903. struct x86_exception *fault)
  1904. {
  1905. struct vcpu_svm *svm = to_svm(vcpu);
  1906. if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
  1907. /*
  1908. * TODO: track the cause of the nested page fault, and
  1909. * correctly fill in the high bits of exit_info_1.
  1910. */
  1911. svm->vmcb->control.exit_code = SVM_EXIT_NPF;
  1912. svm->vmcb->control.exit_code_hi = 0;
  1913. svm->vmcb->control.exit_info_1 = (1ULL << 32);
  1914. svm->vmcb->control.exit_info_2 = fault->address;
  1915. }
  1916. svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
  1917. svm->vmcb->control.exit_info_1 |= fault->error_code;
  1918. /*
  1919. * The present bit is always zero for page structure faults on real
  1920. * hardware.
  1921. */
  1922. if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
  1923. svm->vmcb->control.exit_info_1 &= ~1;
  1924. nested_svm_vmexit(svm);
  1925. }
  1926. static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
  1927. {
  1928. WARN_ON(mmu_is_nested(vcpu));
  1929. kvm_init_shadow_mmu(vcpu);
  1930. vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
  1931. vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
  1932. vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
  1933. vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
  1934. vcpu->arch.mmu.shadow_root_level = get_npt_level();
  1935. reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
  1936. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  1937. }
  1938. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  1939. {
  1940. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  1941. }
  1942. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1943. {
  1944. if (!(svm->vcpu.arch.efer & EFER_SVME) ||
  1945. !is_paging(&svm->vcpu)) {
  1946. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1947. return 1;
  1948. }
  1949. if (svm->vmcb->save.cpl) {
  1950. kvm_inject_gp(&svm->vcpu, 0);
  1951. return 1;
  1952. }
  1953. return 0;
  1954. }
  1955. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1956. bool has_error_code, u32 error_code)
  1957. {
  1958. int vmexit;
  1959. if (!is_guest_mode(&svm->vcpu))
  1960. return 0;
  1961. vmexit = nested_svm_intercept(svm);
  1962. if (vmexit != NESTED_EXIT_DONE)
  1963. return 0;
  1964. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1965. svm->vmcb->control.exit_code_hi = 0;
  1966. svm->vmcb->control.exit_info_1 = error_code;
  1967. /*
  1968. * FIXME: we should not write CR2 when L1 intercepts an L2 #PF exception.
  1969. * The fix is to add the ancillary datum (CR2 or DR6) to structs
  1970. * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 can be
  1971. * written only when inject_pending_event runs (DR6 would written here
  1972. * too). This should be conditional on a new capability---if the
  1973. * capability is disabled, kvm_multiple_exception would write the
  1974. * ancillary information to CR2 or DR6, for backwards ABI-compatibility.
  1975. */
  1976. if (svm->vcpu.arch.exception.nested_apf)
  1977. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.apf.nested_apf_token;
  1978. else
  1979. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1980. svm->nested.exit_required = true;
  1981. return vmexit;
  1982. }
  1983. /* This function returns true if it is save to enable the irq window */
  1984. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1985. {
  1986. if (!is_guest_mode(&svm->vcpu))
  1987. return true;
  1988. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1989. return true;
  1990. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1991. return false;
  1992. /*
  1993. * if vmexit was already requested (by intercepted exception
  1994. * for instance) do not overwrite it with "external interrupt"
  1995. * vmexit.
  1996. */
  1997. if (svm->nested.exit_required)
  1998. return false;
  1999. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  2000. svm->vmcb->control.exit_info_1 = 0;
  2001. svm->vmcb->control.exit_info_2 = 0;
  2002. if (svm->nested.intercept & 1ULL) {
  2003. /*
  2004. * The #vmexit can't be emulated here directly because this
  2005. * code path runs with irqs and preemption disabled. A
  2006. * #vmexit emulation might sleep. Only signal request for
  2007. * the #vmexit here.
  2008. */
  2009. svm->nested.exit_required = true;
  2010. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  2011. return false;
  2012. }
  2013. return true;
  2014. }
  2015. /* This function returns true if it is save to enable the nmi window */
  2016. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  2017. {
  2018. if (!is_guest_mode(&svm->vcpu))
  2019. return true;
  2020. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  2021. return true;
  2022. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  2023. svm->nested.exit_required = true;
  2024. return false;
  2025. }
  2026. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  2027. {
  2028. struct page *page;
  2029. might_sleep();
  2030. page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
  2031. if (is_error_page(page))
  2032. goto error;
  2033. *_page = page;
  2034. return kmap(page);
  2035. error:
  2036. kvm_inject_gp(&svm->vcpu, 0);
  2037. return NULL;
  2038. }
  2039. static void nested_svm_unmap(struct page *page)
  2040. {
  2041. kunmap(page);
  2042. kvm_release_page_dirty(page);
  2043. }
  2044. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  2045. {
  2046. unsigned port, size, iopm_len;
  2047. u16 val, mask;
  2048. u8 start_bit;
  2049. u64 gpa;
  2050. if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
  2051. return NESTED_EXIT_HOST;
  2052. port = svm->vmcb->control.exit_info_1 >> 16;
  2053. size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
  2054. SVM_IOIO_SIZE_SHIFT;
  2055. gpa = svm->nested.vmcb_iopm + (port / 8);
  2056. start_bit = port % 8;
  2057. iopm_len = (start_bit + size > 8) ? 2 : 1;
  2058. mask = (0xf >> (4 - size)) << start_bit;
  2059. val = 0;
  2060. if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
  2061. return NESTED_EXIT_DONE;
  2062. return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  2063. }
  2064. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  2065. {
  2066. u32 offset, msr, value;
  2067. int write, mask;
  2068. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  2069. return NESTED_EXIT_HOST;
  2070. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2071. offset = svm_msrpm_offset(msr);
  2072. write = svm->vmcb->control.exit_info_1 & 1;
  2073. mask = 1 << ((2 * (msr & 0xf)) + write);
  2074. if (offset == MSR_INVALID)
  2075. return NESTED_EXIT_DONE;
  2076. /* Offset is in 32 bit units but need in 8 bit units */
  2077. offset *= 4;
  2078. if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
  2079. return NESTED_EXIT_DONE;
  2080. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  2081. }
  2082. /* DB exceptions for our internal use must not cause vmexit */
  2083. static int nested_svm_intercept_db(struct vcpu_svm *svm)
  2084. {
  2085. unsigned long dr6;
  2086. /* if we're not singlestepping, it's not ours */
  2087. if (!svm->nmi_singlestep)
  2088. return NESTED_EXIT_DONE;
  2089. /* if it's not a singlestep exception, it's not ours */
  2090. if (kvm_get_dr(&svm->vcpu, 6, &dr6))
  2091. return NESTED_EXIT_DONE;
  2092. if (!(dr6 & DR6_BS))
  2093. return NESTED_EXIT_DONE;
  2094. /* if the guest is singlestepping, it should get the vmexit */
  2095. if (svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF) {
  2096. disable_nmi_singlestep(svm);
  2097. return NESTED_EXIT_DONE;
  2098. }
  2099. /* it's ours, the nested hypervisor must not see this one */
  2100. return NESTED_EXIT_HOST;
  2101. }
  2102. static int nested_svm_exit_special(struct vcpu_svm *svm)
  2103. {
  2104. u32 exit_code = svm->vmcb->control.exit_code;
  2105. switch (exit_code) {
  2106. case SVM_EXIT_INTR:
  2107. case SVM_EXIT_NMI:
  2108. case SVM_EXIT_EXCP_BASE + MC_VECTOR:
  2109. return NESTED_EXIT_HOST;
  2110. case SVM_EXIT_NPF:
  2111. /* For now we are always handling NPFs when using them */
  2112. if (npt_enabled)
  2113. return NESTED_EXIT_HOST;
  2114. break;
  2115. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  2116. /* When we're shadowing, trap PFs, but not async PF */
  2117. if (!npt_enabled && svm->vcpu.arch.apf.host_apf_reason == 0)
  2118. return NESTED_EXIT_HOST;
  2119. break;
  2120. default:
  2121. break;
  2122. }
  2123. return NESTED_EXIT_CONTINUE;
  2124. }
  2125. /*
  2126. * If this function returns true, this #vmexit was already handled
  2127. */
  2128. static int nested_svm_intercept(struct vcpu_svm *svm)
  2129. {
  2130. u32 exit_code = svm->vmcb->control.exit_code;
  2131. int vmexit = NESTED_EXIT_HOST;
  2132. switch (exit_code) {
  2133. case SVM_EXIT_MSR:
  2134. vmexit = nested_svm_exit_handled_msr(svm);
  2135. break;
  2136. case SVM_EXIT_IOIO:
  2137. vmexit = nested_svm_intercept_ioio(svm);
  2138. break;
  2139. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  2140. u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
  2141. if (svm->nested.intercept_cr & bit)
  2142. vmexit = NESTED_EXIT_DONE;
  2143. break;
  2144. }
  2145. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  2146. u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
  2147. if (svm->nested.intercept_dr & bit)
  2148. vmexit = NESTED_EXIT_DONE;
  2149. break;
  2150. }
  2151. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  2152. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  2153. if (svm->nested.intercept_exceptions & excp_bits) {
  2154. if (exit_code == SVM_EXIT_EXCP_BASE + DB_VECTOR)
  2155. vmexit = nested_svm_intercept_db(svm);
  2156. else
  2157. vmexit = NESTED_EXIT_DONE;
  2158. }
  2159. /* async page fault always cause vmexit */
  2160. else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
  2161. svm->vcpu.arch.exception.nested_apf != 0)
  2162. vmexit = NESTED_EXIT_DONE;
  2163. break;
  2164. }
  2165. case SVM_EXIT_ERR: {
  2166. vmexit = NESTED_EXIT_DONE;
  2167. break;
  2168. }
  2169. default: {
  2170. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  2171. if (svm->nested.intercept & exit_bits)
  2172. vmexit = NESTED_EXIT_DONE;
  2173. }
  2174. }
  2175. return vmexit;
  2176. }
  2177. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  2178. {
  2179. int vmexit;
  2180. vmexit = nested_svm_intercept(svm);
  2181. if (vmexit == NESTED_EXIT_DONE)
  2182. nested_svm_vmexit(svm);
  2183. return vmexit;
  2184. }
  2185. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  2186. {
  2187. struct vmcb_control_area *dst = &dst_vmcb->control;
  2188. struct vmcb_control_area *from = &from_vmcb->control;
  2189. dst->intercept_cr = from->intercept_cr;
  2190. dst->intercept_dr = from->intercept_dr;
  2191. dst->intercept_exceptions = from->intercept_exceptions;
  2192. dst->intercept = from->intercept;
  2193. dst->iopm_base_pa = from->iopm_base_pa;
  2194. dst->msrpm_base_pa = from->msrpm_base_pa;
  2195. dst->tsc_offset = from->tsc_offset;
  2196. dst->asid = from->asid;
  2197. dst->tlb_ctl = from->tlb_ctl;
  2198. dst->int_ctl = from->int_ctl;
  2199. dst->int_vector = from->int_vector;
  2200. dst->int_state = from->int_state;
  2201. dst->exit_code = from->exit_code;
  2202. dst->exit_code_hi = from->exit_code_hi;
  2203. dst->exit_info_1 = from->exit_info_1;
  2204. dst->exit_info_2 = from->exit_info_2;
  2205. dst->exit_int_info = from->exit_int_info;
  2206. dst->exit_int_info_err = from->exit_int_info_err;
  2207. dst->nested_ctl = from->nested_ctl;
  2208. dst->event_inj = from->event_inj;
  2209. dst->event_inj_err = from->event_inj_err;
  2210. dst->nested_cr3 = from->nested_cr3;
  2211. dst->virt_ext = from->virt_ext;
  2212. }
  2213. static int nested_svm_vmexit(struct vcpu_svm *svm)
  2214. {
  2215. struct vmcb *nested_vmcb;
  2216. struct vmcb *hsave = svm->nested.hsave;
  2217. struct vmcb *vmcb = svm->vmcb;
  2218. struct page *page;
  2219. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  2220. vmcb->control.exit_info_1,
  2221. vmcb->control.exit_info_2,
  2222. vmcb->control.exit_int_info,
  2223. vmcb->control.exit_int_info_err,
  2224. KVM_ISA_SVM);
  2225. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  2226. if (!nested_vmcb)
  2227. return 1;
  2228. /* Exit Guest-Mode */
  2229. leave_guest_mode(&svm->vcpu);
  2230. svm->nested.vmcb = 0;
  2231. /* Give the current vmcb to the guest */
  2232. disable_gif(svm);
  2233. nested_vmcb->save.es = vmcb->save.es;
  2234. nested_vmcb->save.cs = vmcb->save.cs;
  2235. nested_vmcb->save.ss = vmcb->save.ss;
  2236. nested_vmcb->save.ds = vmcb->save.ds;
  2237. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  2238. nested_vmcb->save.idtr = vmcb->save.idtr;
  2239. nested_vmcb->save.efer = svm->vcpu.arch.efer;
  2240. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  2241. nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
  2242. nested_vmcb->save.cr2 = vmcb->save.cr2;
  2243. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  2244. nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
  2245. nested_vmcb->save.rip = vmcb->save.rip;
  2246. nested_vmcb->save.rsp = vmcb->save.rsp;
  2247. nested_vmcb->save.rax = vmcb->save.rax;
  2248. nested_vmcb->save.dr7 = vmcb->save.dr7;
  2249. nested_vmcb->save.dr6 = vmcb->save.dr6;
  2250. nested_vmcb->save.cpl = vmcb->save.cpl;
  2251. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  2252. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  2253. nested_vmcb->control.int_state = vmcb->control.int_state;
  2254. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  2255. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  2256. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  2257. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  2258. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  2259. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  2260. if (svm->nrips_enabled)
  2261. nested_vmcb->control.next_rip = vmcb->control.next_rip;
  2262. /*
  2263. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  2264. * to make sure that we do not lose injected events. So check event_inj
  2265. * here and copy it to exit_int_info if it is valid.
  2266. * Exit_int_info and event_inj can't be both valid because the case
  2267. * below only happens on a VMRUN instruction intercept which has
  2268. * no valid exit_int_info set.
  2269. */
  2270. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  2271. struct vmcb_control_area *nc = &nested_vmcb->control;
  2272. nc->exit_int_info = vmcb->control.event_inj;
  2273. nc->exit_int_info_err = vmcb->control.event_inj_err;
  2274. }
  2275. nested_vmcb->control.tlb_ctl = 0;
  2276. nested_vmcb->control.event_inj = 0;
  2277. nested_vmcb->control.event_inj_err = 0;
  2278. /* We always set V_INTR_MASKING and remember the old value in hflags */
  2279. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  2280. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  2281. /* Restore the original control entries */
  2282. copy_vmcb_control_area(vmcb, hsave);
  2283. kvm_clear_exception_queue(&svm->vcpu);
  2284. kvm_clear_interrupt_queue(&svm->vcpu);
  2285. svm->nested.nested_cr3 = 0;
  2286. /* Restore selected save entries */
  2287. svm->vmcb->save.es = hsave->save.es;
  2288. svm->vmcb->save.cs = hsave->save.cs;
  2289. svm->vmcb->save.ss = hsave->save.ss;
  2290. svm->vmcb->save.ds = hsave->save.ds;
  2291. svm->vmcb->save.gdtr = hsave->save.gdtr;
  2292. svm->vmcb->save.idtr = hsave->save.idtr;
  2293. kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
  2294. svm_set_efer(&svm->vcpu, hsave->save.efer);
  2295. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  2296. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  2297. if (npt_enabled) {
  2298. svm->vmcb->save.cr3 = hsave->save.cr3;
  2299. svm->vcpu.arch.cr3 = hsave->save.cr3;
  2300. } else {
  2301. (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  2302. }
  2303. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  2304. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  2305. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  2306. svm->vmcb->save.dr7 = 0;
  2307. svm->vmcb->save.cpl = 0;
  2308. svm->vmcb->control.exit_int_info = 0;
  2309. mark_all_dirty(svm->vmcb);
  2310. nested_svm_unmap(page);
  2311. nested_svm_uninit_mmu_context(&svm->vcpu);
  2312. kvm_mmu_reset_context(&svm->vcpu);
  2313. kvm_mmu_load(&svm->vcpu);
  2314. return 0;
  2315. }
  2316. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  2317. {
  2318. /*
  2319. * This function merges the msr permission bitmaps of kvm and the
  2320. * nested vmcb. It is optimized in that it only merges the parts where
  2321. * the kvm msr permission bitmap may contain zero bits
  2322. */
  2323. int i;
  2324. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  2325. return true;
  2326. for (i = 0; i < MSRPM_OFFSETS; i++) {
  2327. u32 value, p;
  2328. u64 offset;
  2329. if (msrpm_offsets[i] == 0xffffffff)
  2330. break;
  2331. p = msrpm_offsets[i];
  2332. offset = svm->nested.vmcb_msrpm + (p * 4);
  2333. if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
  2334. return false;
  2335. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  2336. }
  2337. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  2338. return true;
  2339. }
  2340. static bool nested_vmcb_checks(struct vmcb *vmcb)
  2341. {
  2342. if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  2343. return false;
  2344. if (vmcb->control.asid == 0)
  2345. return false;
  2346. if (vmcb->control.nested_ctl && !npt_enabled)
  2347. return false;
  2348. return true;
  2349. }
  2350. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  2351. {
  2352. struct vmcb *nested_vmcb;
  2353. struct vmcb *hsave = svm->nested.hsave;
  2354. struct vmcb *vmcb = svm->vmcb;
  2355. struct page *page;
  2356. u64 vmcb_gpa;
  2357. vmcb_gpa = svm->vmcb->save.rax;
  2358. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2359. if (!nested_vmcb)
  2360. return false;
  2361. if (!nested_vmcb_checks(nested_vmcb)) {
  2362. nested_vmcb->control.exit_code = SVM_EXIT_ERR;
  2363. nested_vmcb->control.exit_code_hi = 0;
  2364. nested_vmcb->control.exit_info_1 = 0;
  2365. nested_vmcb->control.exit_info_2 = 0;
  2366. nested_svm_unmap(page);
  2367. return false;
  2368. }
  2369. trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
  2370. nested_vmcb->save.rip,
  2371. nested_vmcb->control.int_ctl,
  2372. nested_vmcb->control.event_inj,
  2373. nested_vmcb->control.nested_ctl);
  2374. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
  2375. nested_vmcb->control.intercept_cr >> 16,
  2376. nested_vmcb->control.intercept_exceptions,
  2377. nested_vmcb->control.intercept);
  2378. /* Clear internal status */
  2379. kvm_clear_exception_queue(&svm->vcpu);
  2380. kvm_clear_interrupt_queue(&svm->vcpu);
  2381. /*
  2382. * Save the old vmcb, so we don't need to pick what we save, but can
  2383. * restore everything when a VMEXIT occurs
  2384. */
  2385. hsave->save.es = vmcb->save.es;
  2386. hsave->save.cs = vmcb->save.cs;
  2387. hsave->save.ss = vmcb->save.ss;
  2388. hsave->save.ds = vmcb->save.ds;
  2389. hsave->save.gdtr = vmcb->save.gdtr;
  2390. hsave->save.idtr = vmcb->save.idtr;
  2391. hsave->save.efer = svm->vcpu.arch.efer;
  2392. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  2393. hsave->save.cr4 = svm->vcpu.arch.cr4;
  2394. hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
  2395. hsave->save.rip = kvm_rip_read(&svm->vcpu);
  2396. hsave->save.rsp = vmcb->save.rsp;
  2397. hsave->save.rax = vmcb->save.rax;
  2398. if (npt_enabled)
  2399. hsave->save.cr3 = vmcb->save.cr3;
  2400. else
  2401. hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
  2402. copy_vmcb_control_area(hsave, vmcb);
  2403. if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
  2404. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  2405. else
  2406. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  2407. if (nested_vmcb->control.nested_ctl) {
  2408. kvm_mmu_unload(&svm->vcpu);
  2409. svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
  2410. nested_svm_init_mmu_context(&svm->vcpu);
  2411. }
  2412. /* Load the nested guest state */
  2413. svm->vmcb->save.es = nested_vmcb->save.es;
  2414. svm->vmcb->save.cs = nested_vmcb->save.cs;
  2415. svm->vmcb->save.ss = nested_vmcb->save.ss;
  2416. svm->vmcb->save.ds = nested_vmcb->save.ds;
  2417. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  2418. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  2419. kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
  2420. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  2421. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  2422. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  2423. if (npt_enabled) {
  2424. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  2425. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  2426. } else
  2427. (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  2428. /* Guest paging mode is active - reset mmu */
  2429. kvm_mmu_reset_context(&svm->vcpu);
  2430. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  2431. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  2432. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  2433. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  2434. /* In case we don't even reach vcpu_run, the fields are not updated */
  2435. svm->vmcb->save.rax = nested_vmcb->save.rax;
  2436. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  2437. svm->vmcb->save.rip = nested_vmcb->save.rip;
  2438. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  2439. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  2440. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  2441. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
  2442. svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
  2443. /* cache intercepts */
  2444. svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
  2445. svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
  2446. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  2447. svm->nested.intercept = nested_vmcb->control.intercept;
  2448. svm_flush_tlb(&svm->vcpu);
  2449. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  2450. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  2451. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  2452. else
  2453. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  2454. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  2455. /* We only want the cr8 intercept bits of the guest */
  2456. clr_cr_intercept(svm, INTERCEPT_CR8_READ);
  2457. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2458. }
  2459. /* We don't want to see VMMCALLs from a nested guest */
  2460. clr_intercept(svm, INTERCEPT_VMMCALL);
  2461. svm->vmcb->control.virt_ext = nested_vmcb->control.virt_ext;
  2462. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  2463. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  2464. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  2465. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  2466. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  2467. nested_svm_unmap(page);
  2468. /* Enter Guest-Mode */
  2469. enter_guest_mode(&svm->vcpu);
  2470. /*
  2471. * Merge guest and host intercepts - must be called with vcpu in
  2472. * guest-mode to take affect here
  2473. */
  2474. recalc_intercepts(svm);
  2475. svm->nested.vmcb = vmcb_gpa;
  2476. enable_gif(svm);
  2477. mark_all_dirty(svm->vmcb);
  2478. return true;
  2479. }
  2480. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  2481. {
  2482. to_vmcb->save.fs = from_vmcb->save.fs;
  2483. to_vmcb->save.gs = from_vmcb->save.gs;
  2484. to_vmcb->save.tr = from_vmcb->save.tr;
  2485. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  2486. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  2487. to_vmcb->save.star = from_vmcb->save.star;
  2488. to_vmcb->save.lstar = from_vmcb->save.lstar;
  2489. to_vmcb->save.cstar = from_vmcb->save.cstar;
  2490. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  2491. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  2492. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  2493. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  2494. }
  2495. static int vmload_interception(struct vcpu_svm *svm)
  2496. {
  2497. struct vmcb *nested_vmcb;
  2498. struct page *page;
  2499. int ret;
  2500. if (nested_svm_check_permissions(svm))
  2501. return 1;
  2502. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2503. if (!nested_vmcb)
  2504. return 1;
  2505. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2506. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2507. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  2508. nested_svm_unmap(page);
  2509. return ret;
  2510. }
  2511. static int vmsave_interception(struct vcpu_svm *svm)
  2512. {
  2513. struct vmcb *nested_vmcb;
  2514. struct page *page;
  2515. int ret;
  2516. if (nested_svm_check_permissions(svm))
  2517. return 1;
  2518. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2519. if (!nested_vmcb)
  2520. return 1;
  2521. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2522. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2523. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  2524. nested_svm_unmap(page);
  2525. return ret;
  2526. }
  2527. static int vmrun_interception(struct vcpu_svm *svm)
  2528. {
  2529. if (nested_svm_check_permissions(svm))
  2530. return 1;
  2531. /* Save rip after vmrun instruction */
  2532. kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
  2533. if (!nested_svm_vmrun(svm))
  2534. return 1;
  2535. if (!nested_svm_vmrun_msrpm(svm))
  2536. goto failed;
  2537. return 1;
  2538. failed:
  2539. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  2540. svm->vmcb->control.exit_code_hi = 0;
  2541. svm->vmcb->control.exit_info_1 = 0;
  2542. svm->vmcb->control.exit_info_2 = 0;
  2543. nested_svm_vmexit(svm);
  2544. return 1;
  2545. }
  2546. static int stgi_interception(struct vcpu_svm *svm)
  2547. {
  2548. int ret;
  2549. if (nested_svm_check_permissions(svm))
  2550. return 1;
  2551. /*
  2552. * If VGIF is enabled, the STGI intercept is only added to
  2553. * detect the opening of the NMI window; remove it now.
  2554. */
  2555. if (vgif_enabled(svm))
  2556. clr_intercept(svm, INTERCEPT_STGI);
  2557. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2558. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2559. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2560. enable_gif(svm);
  2561. return ret;
  2562. }
  2563. static int clgi_interception(struct vcpu_svm *svm)
  2564. {
  2565. int ret;
  2566. if (nested_svm_check_permissions(svm))
  2567. return 1;
  2568. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2569. ret = kvm_skip_emulated_instruction(&svm->vcpu);
  2570. disable_gif(svm);
  2571. /* After a CLGI no interrupts should come */
  2572. if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
  2573. svm_clear_vintr(svm);
  2574. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2575. mark_dirty(svm->vmcb, VMCB_INTR);
  2576. }
  2577. return ret;
  2578. }
  2579. static int invlpga_interception(struct vcpu_svm *svm)
  2580. {
  2581. struct kvm_vcpu *vcpu = &svm->vcpu;
  2582. trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
  2583. kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  2584. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  2585. kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  2586. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2587. return kvm_skip_emulated_instruction(&svm->vcpu);
  2588. }
  2589. static int skinit_interception(struct vcpu_svm *svm)
  2590. {
  2591. trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  2592. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2593. return 1;
  2594. }
  2595. static int wbinvd_interception(struct vcpu_svm *svm)
  2596. {
  2597. return kvm_emulate_wbinvd(&svm->vcpu);
  2598. }
  2599. static int xsetbv_interception(struct vcpu_svm *svm)
  2600. {
  2601. u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
  2602. u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  2603. if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
  2604. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2605. return kvm_skip_emulated_instruction(&svm->vcpu);
  2606. }
  2607. return 1;
  2608. }
  2609. static int task_switch_interception(struct vcpu_svm *svm)
  2610. {
  2611. u16 tss_selector;
  2612. int reason;
  2613. int int_type = svm->vmcb->control.exit_int_info &
  2614. SVM_EXITINTINFO_TYPE_MASK;
  2615. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  2616. uint32_t type =
  2617. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  2618. uint32_t idt_v =
  2619. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  2620. bool has_error_code = false;
  2621. u32 error_code = 0;
  2622. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  2623. if (svm->vmcb->control.exit_info_2 &
  2624. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  2625. reason = TASK_SWITCH_IRET;
  2626. else if (svm->vmcb->control.exit_info_2 &
  2627. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  2628. reason = TASK_SWITCH_JMP;
  2629. else if (idt_v)
  2630. reason = TASK_SWITCH_GATE;
  2631. else
  2632. reason = TASK_SWITCH_CALL;
  2633. if (reason == TASK_SWITCH_GATE) {
  2634. switch (type) {
  2635. case SVM_EXITINTINFO_TYPE_NMI:
  2636. svm->vcpu.arch.nmi_injected = false;
  2637. break;
  2638. case SVM_EXITINTINFO_TYPE_EXEPT:
  2639. if (svm->vmcb->control.exit_info_2 &
  2640. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  2641. has_error_code = true;
  2642. error_code =
  2643. (u32)svm->vmcb->control.exit_info_2;
  2644. }
  2645. kvm_clear_exception_queue(&svm->vcpu);
  2646. break;
  2647. case SVM_EXITINTINFO_TYPE_INTR:
  2648. kvm_clear_interrupt_queue(&svm->vcpu);
  2649. break;
  2650. default:
  2651. break;
  2652. }
  2653. }
  2654. if (reason != TASK_SWITCH_GATE ||
  2655. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2656. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2657. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  2658. skip_emulated_instruction(&svm->vcpu);
  2659. if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
  2660. int_vec = -1;
  2661. if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
  2662. has_error_code, error_code) == EMULATE_FAIL) {
  2663. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2664. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  2665. svm->vcpu.run->internal.ndata = 0;
  2666. return 0;
  2667. }
  2668. return 1;
  2669. }
  2670. static int cpuid_interception(struct vcpu_svm *svm)
  2671. {
  2672. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2673. return kvm_emulate_cpuid(&svm->vcpu);
  2674. }
  2675. static int iret_interception(struct vcpu_svm *svm)
  2676. {
  2677. ++svm->vcpu.stat.nmi_window_exits;
  2678. clr_intercept(svm, INTERCEPT_IRET);
  2679. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  2680. svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
  2681. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2682. return 1;
  2683. }
  2684. static int invlpg_interception(struct vcpu_svm *svm)
  2685. {
  2686. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2687. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2688. kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
  2689. return kvm_skip_emulated_instruction(&svm->vcpu);
  2690. }
  2691. static int emulate_on_interception(struct vcpu_svm *svm)
  2692. {
  2693. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2694. }
  2695. static int rdpmc_interception(struct vcpu_svm *svm)
  2696. {
  2697. int err;
  2698. if (!static_cpu_has(X86_FEATURE_NRIPS))
  2699. return emulate_on_interception(svm);
  2700. err = kvm_rdpmc(&svm->vcpu);
  2701. return kvm_complete_insn_gp(&svm->vcpu, err);
  2702. }
  2703. static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
  2704. unsigned long val)
  2705. {
  2706. unsigned long cr0 = svm->vcpu.arch.cr0;
  2707. bool ret = false;
  2708. u64 intercept;
  2709. intercept = svm->nested.intercept;
  2710. if (!is_guest_mode(&svm->vcpu) ||
  2711. (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
  2712. return false;
  2713. cr0 &= ~SVM_CR0_SELECTIVE_MASK;
  2714. val &= ~SVM_CR0_SELECTIVE_MASK;
  2715. if (cr0 ^ val) {
  2716. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  2717. ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
  2718. }
  2719. return ret;
  2720. }
  2721. #define CR_VALID (1ULL << 63)
  2722. static int cr_interception(struct vcpu_svm *svm)
  2723. {
  2724. int reg, cr;
  2725. unsigned long val;
  2726. int err;
  2727. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2728. return emulate_on_interception(svm);
  2729. if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
  2730. return emulate_on_interception(svm);
  2731. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2732. if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
  2733. cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
  2734. else
  2735. cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
  2736. err = 0;
  2737. if (cr >= 16) { /* mov to cr */
  2738. cr -= 16;
  2739. val = kvm_register_read(&svm->vcpu, reg);
  2740. switch (cr) {
  2741. case 0:
  2742. if (!check_selective_cr0_intercepted(svm, val))
  2743. err = kvm_set_cr0(&svm->vcpu, val);
  2744. else
  2745. return 1;
  2746. break;
  2747. case 3:
  2748. err = kvm_set_cr3(&svm->vcpu, val);
  2749. break;
  2750. case 4:
  2751. err = kvm_set_cr4(&svm->vcpu, val);
  2752. break;
  2753. case 8:
  2754. err = kvm_set_cr8(&svm->vcpu, val);
  2755. break;
  2756. default:
  2757. WARN(1, "unhandled write to CR%d", cr);
  2758. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2759. return 1;
  2760. }
  2761. } else { /* mov from cr */
  2762. switch (cr) {
  2763. case 0:
  2764. val = kvm_read_cr0(&svm->vcpu);
  2765. break;
  2766. case 2:
  2767. val = svm->vcpu.arch.cr2;
  2768. break;
  2769. case 3:
  2770. val = kvm_read_cr3(&svm->vcpu);
  2771. break;
  2772. case 4:
  2773. val = kvm_read_cr4(&svm->vcpu);
  2774. break;
  2775. case 8:
  2776. val = kvm_get_cr8(&svm->vcpu);
  2777. break;
  2778. default:
  2779. WARN(1, "unhandled read from CR%d", cr);
  2780. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2781. return 1;
  2782. }
  2783. kvm_register_write(&svm->vcpu, reg, val);
  2784. }
  2785. return kvm_complete_insn_gp(&svm->vcpu, err);
  2786. }
  2787. static int dr_interception(struct vcpu_svm *svm)
  2788. {
  2789. int reg, dr;
  2790. unsigned long val;
  2791. if (svm->vcpu.guest_debug == 0) {
  2792. /*
  2793. * No more DR vmexits; force a reload of the debug registers
  2794. * and reenter on this instruction. The next vmexit will
  2795. * retrieve the full state of the debug registers.
  2796. */
  2797. clr_dr_intercepts(svm);
  2798. svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  2799. return 1;
  2800. }
  2801. if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
  2802. return emulate_on_interception(svm);
  2803. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2804. dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
  2805. if (dr >= 16) { /* mov to DRn */
  2806. if (!kvm_require_dr(&svm->vcpu, dr - 16))
  2807. return 1;
  2808. val = kvm_register_read(&svm->vcpu, reg);
  2809. kvm_set_dr(&svm->vcpu, dr - 16, val);
  2810. } else {
  2811. if (!kvm_require_dr(&svm->vcpu, dr))
  2812. return 1;
  2813. kvm_get_dr(&svm->vcpu, dr, &val);
  2814. kvm_register_write(&svm->vcpu, reg, val);
  2815. }
  2816. return kvm_skip_emulated_instruction(&svm->vcpu);
  2817. }
  2818. static int cr8_write_interception(struct vcpu_svm *svm)
  2819. {
  2820. struct kvm_run *kvm_run = svm->vcpu.run;
  2821. int r;
  2822. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  2823. /* instruction emulation calls kvm_set_cr8() */
  2824. r = cr_interception(svm);
  2825. if (lapic_in_kernel(&svm->vcpu))
  2826. return r;
  2827. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  2828. return r;
  2829. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2830. return 0;
  2831. }
  2832. static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2833. {
  2834. struct vcpu_svm *svm = to_svm(vcpu);
  2835. switch (msr_info->index) {
  2836. case MSR_IA32_TSC: {
  2837. msr_info->data = svm->vmcb->control.tsc_offset +
  2838. kvm_scale_tsc(vcpu, rdtsc());
  2839. break;
  2840. }
  2841. case MSR_STAR:
  2842. msr_info->data = svm->vmcb->save.star;
  2843. break;
  2844. #ifdef CONFIG_X86_64
  2845. case MSR_LSTAR:
  2846. msr_info->data = svm->vmcb->save.lstar;
  2847. break;
  2848. case MSR_CSTAR:
  2849. msr_info->data = svm->vmcb->save.cstar;
  2850. break;
  2851. case MSR_KERNEL_GS_BASE:
  2852. msr_info->data = svm->vmcb->save.kernel_gs_base;
  2853. break;
  2854. case MSR_SYSCALL_MASK:
  2855. msr_info->data = svm->vmcb->save.sfmask;
  2856. break;
  2857. #endif
  2858. case MSR_IA32_SYSENTER_CS:
  2859. msr_info->data = svm->vmcb->save.sysenter_cs;
  2860. break;
  2861. case MSR_IA32_SYSENTER_EIP:
  2862. msr_info->data = svm->sysenter_eip;
  2863. break;
  2864. case MSR_IA32_SYSENTER_ESP:
  2865. msr_info->data = svm->sysenter_esp;
  2866. break;
  2867. case MSR_TSC_AUX:
  2868. if (!boot_cpu_has(X86_FEATURE_RDTSCP))
  2869. return 1;
  2870. msr_info->data = svm->tsc_aux;
  2871. break;
  2872. /*
  2873. * Nobody will change the following 5 values in the VMCB so we can
  2874. * safely return them on rdmsr. They will always be 0 until LBRV is
  2875. * implemented.
  2876. */
  2877. case MSR_IA32_DEBUGCTLMSR:
  2878. msr_info->data = svm->vmcb->save.dbgctl;
  2879. break;
  2880. case MSR_IA32_LASTBRANCHFROMIP:
  2881. msr_info->data = svm->vmcb->save.br_from;
  2882. break;
  2883. case MSR_IA32_LASTBRANCHTOIP:
  2884. msr_info->data = svm->vmcb->save.br_to;
  2885. break;
  2886. case MSR_IA32_LASTINTFROMIP:
  2887. msr_info->data = svm->vmcb->save.last_excp_from;
  2888. break;
  2889. case MSR_IA32_LASTINTTOIP:
  2890. msr_info->data = svm->vmcb->save.last_excp_to;
  2891. break;
  2892. case MSR_VM_HSAVE_PA:
  2893. msr_info->data = svm->nested.hsave_msr;
  2894. break;
  2895. case MSR_VM_CR:
  2896. msr_info->data = svm->nested.vm_cr_msr;
  2897. break;
  2898. case MSR_IA32_UCODE_REV:
  2899. msr_info->data = 0x01000065;
  2900. break;
  2901. case MSR_F15H_IC_CFG: {
  2902. int family, model;
  2903. family = guest_cpuid_family(vcpu);
  2904. model = guest_cpuid_model(vcpu);
  2905. if (family < 0 || model < 0)
  2906. return kvm_get_msr_common(vcpu, msr_info);
  2907. msr_info->data = 0;
  2908. if (family == 0x15 &&
  2909. (model >= 0x2 && model < 0x20))
  2910. msr_info->data = 0x1E;
  2911. }
  2912. break;
  2913. default:
  2914. return kvm_get_msr_common(vcpu, msr_info);
  2915. }
  2916. return 0;
  2917. }
  2918. static int rdmsr_interception(struct vcpu_svm *svm)
  2919. {
  2920. u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  2921. struct msr_data msr_info;
  2922. msr_info.index = ecx;
  2923. msr_info.host_initiated = false;
  2924. if (svm_get_msr(&svm->vcpu, &msr_info)) {
  2925. trace_kvm_msr_read_ex(ecx);
  2926. kvm_inject_gp(&svm->vcpu, 0);
  2927. return 1;
  2928. } else {
  2929. trace_kvm_msr_read(ecx, msr_info.data);
  2930. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
  2931. msr_info.data & 0xffffffff);
  2932. kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
  2933. msr_info.data >> 32);
  2934. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2935. return kvm_skip_emulated_instruction(&svm->vcpu);
  2936. }
  2937. }
  2938. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2939. {
  2940. struct vcpu_svm *svm = to_svm(vcpu);
  2941. int svm_dis, chg_mask;
  2942. if (data & ~SVM_VM_CR_VALID_MASK)
  2943. return 1;
  2944. chg_mask = SVM_VM_CR_VALID_MASK;
  2945. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2946. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2947. svm->nested.vm_cr_msr &= ~chg_mask;
  2948. svm->nested.vm_cr_msr |= (data & chg_mask);
  2949. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2950. /* check for svm_disable while efer.svme is set */
  2951. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2952. return 1;
  2953. return 0;
  2954. }
  2955. static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  2956. {
  2957. struct vcpu_svm *svm = to_svm(vcpu);
  2958. u32 ecx = msr->index;
  2959. u64 data = msr->data;
  2960. switch (ecx) {
  2961. case MSR_IA32_TSC:
  2962. kvm_write_tsc(vcpu, msr);
  2963. break;
  2964. case MSR_STAR:
  2965. svm->vmcb->save.star = data;
  2966. break;
  2967. #ifdef CONFIG_X86_64
  2968. case MSR_LSTAR:
  2969. svm->vmcb->save.lstar = data;
  2970. break;
  2971. case MSR_CSTAR:
  2972. svm->vmcb->save.cstar = data;
  2973. break;
  2974. case MSR_KERNEL_GS_BASE:
  2975. svm->vmcb->save.kernel_gs_base = data;
  2976. break;
  2977. case MSR_SYSCALL_MASK:
  2978. svm->vmcb->save.sfmask = data;
  2979. break;
  2980. #endif
  2981. case MSR_IA32_SYSENTER_CS:
  2982. svm->vmcb->save.sysenter_cs = data;
  2983. break;
  2984. case MSR_IA32_SYSENTER_EIP:
  2985. svm->sysenter_eip = data;
  2986. svm->vmcb->save.sysenter_eip = data;
  2987. break;
  2988. case MSR_IA32_SYSENTER_ESP:
  2989. svm->sysenter_esp = data;
  2990. svm->vmcb->save.sysenter_esp = data;
  2991. break;
  2992. case MSR_TSC_AUX:
  2993. if (!boot_cpu_has(X86_FEATURE_RDTSCP))
  2994. return 1;
  2995. /*
  2996. * This is rare, so we update the MSR here instead of using
  2997. * direct_access_msrs. Doing that would require a rdmsr in
  2998. * svm_vcpu_put.
  2999. */
  3000. svm->tsc_aux = data;
  3001. wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
  3002. break;
  3003. case MSR_IA32_DEBUGCTLMSR:
  3004. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  3005. vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  3006. __func__, data);
  3007. break;
  3008. }
  3009. if (data & DEBUGCTL_RESERVED_BITS)
  3010. return 1;
  3011. svm->vmcb->save.dbgctl = data;
  3012. mark_dirty(svm->vmcb, VMCB_LBR);
  3013. if (data & (1ULL<<0))
  3014. svm_enable_lbrv(svm);
  3015. else
  3016. svm_disable_lbrv(svm);
  3017. break;
  3018. case MSR_VM_HSAVE_PA:
  3019. svm->nested.hsave_msr = data;
  3020. break;
  3021. case MSR_VM_CR:
  3022. return svm_set_vm_cr(vcpu, data);
  3023. case MSR_VM_IGNNE:
  3024. vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  3025. break;
  3026. case MSR_IA32_APICBASE:
  3027. if (kvm_vcpu_apicv_active(vcpu))
  3028. avic_update_vapic_bar(to_svm(vcpu), data);
  3029. /* Follow through */
  3030. default:
  3031. return kvm_set_msr_common(vcpu, msr);
  3032. }
  3033. return 0;
  3034. }
  3035. static int wrmsr_interception(struct vcpu_svm *svm)
  3036. {
  3037. struct msr_data msr;
  3038. u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  3039. u64 data = kvm_read_edx_eax(&svm->vcpu);
  3040. msr.data = data;
  3041. msr.index = ecx;
  3042. msr.host_initiated = false;
  3043. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  3044. if (kvm_set_msr(&svm->vcpu, &msr)) {
  3045. trace_kvm_msr_write_ex(ecx, data);
  3046. kvm_inject_gp(&svm->vcpu, 0);
  3047. return 1;
  3048. } else {
  3049. trace_kvm_msr_write(ecx, data);
  3050. return kvm_skip_emulated_instruction(&svm->vcpu);
  3051. }
  3052. }
  3053. static int msr_interception(struct vcpu_svm *svm)
  3054. {
  3055. if (svm->vmcb->control.exit_info_1)
  3056. return wrmsr_interception(svm);
  3057. else
  3058. return rdmsr_interception(svm);
  3059. }
  3060. static int interrupt_window_interception(struct vcpu_svm *svm)
  3061. {
  3062. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3063. svm_clear_vintr(svm);
  3064. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  3065. mark_dirty(svm->vmcb, VMCB_INTR);
  3066. ++svm->vcpu.stat.irq_window_exits;
  3067. return 1;
  3068. }
  3069. static int pause_interception(struct vcpu_svm *svm)
  3070. {
  3071. struct kvm_vcpu *vcpu = &svm->vcpu;
  3072. bool in_kernel = (svm_get_cpl(vcpu) == 0);
  3073. kvm_vcpu_on_spin(vcpu, in_kernel);
  3074. return 1;
  3075. }
  3076. static int nop_interception(struct vcpu_svm *svm)
  3077. {
  3078. return kvm_skip_emulated_instruction(&(svm->vcpu));
  3079. }
  3080. static int monitor_interception(struct vcpu_svm *svm)
  3081. {
  3082. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  3083. return nop_interception(svm);
  3084. }
  3085. static int mwait_interception(struct vcpu_svm *svm)
  3086. {
  3087. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  3088. return nop_interception(svm);
  3089. }
  3090. enum avic_ipi_failure_cause {
  3091. AVIC_IPI_FAILURE_INVALID_INT_TYPE,
  3092. AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
  3093. AVIC_IPI_FAILURE_INVALID_TARGET,
  3094. AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
  3095. };
  3096. static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
  3097. {
  3098. u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
  3099. u32 icrl = svm->vmcb->control.exit_info_1;
  3100. u32 id = svm->vmcb->control.exit_info_2 >> 32;
  3101. u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
  3102. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3103. trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
  3104. switch (id) {
  3105. case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
  3106. /*
  3107. * AVIC hardware handles the generation of
  3108. * IPIs when the specified Message Type is Fixed
  3109. * (also known as fixed delivery mode) and
  3110. * the Trigger Mode is edge-triggered. The hardware
  3111. * also supports self and broadcast delivery modes
  3112. * specified via the Destination Shorthand(DSH)
  3113. * field of the ICRL. Logical and physical APIC ID
  3114. * formats are supported. All other IPI types cause
  3115. * a #VMEXIT, which needs to emulated.
  3116. */
  3117. kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
  3118. kvm_lapic_reg_write(apic, APIC_ICR, icrl);
  3119. break;
  3120. case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
  3121. int i;
  3122. struct kvm_vcpu *vcpu;
  3123. struct kvm *kvm = svm->vcpu.kvm;
  3124. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3125. /*
  3126. * At this point, we expect that the AVIC HW has already
  3127. * set the appropriate IRR bits on the valid target
  3128. * vcpus. So, we just need to kick the appropriate vcpu.
  3129. */
  3130. kvm_for_each_vcpu(i, vcpu, kvm) {
  3131. bool m = kvm_apic_match_dest(vcpu, apic,
  3132. icrl & KVM_APIC_SHORT_MASK,
  3133. GET_APIC_DEST_FIELD(icrh),
  3134. icrl & KVM_APIC_DEST_MASK);
  3135. if (m && !avic_vcpu_is_running(vcpu))
  3136. kvm_vcpu_wake_up(vcpu);
  3137. }
  3138. break;
  3139. }
  3140. case AVIC_IPI_FAILURE_INVALID_TARGET:
  3141. break;
  3142. case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
  3143. WARN_ONCE(1, "Invalid backing page\n");
  3144. break;
  3145. default:
  3146. pr_err("Unknown IPI interception\n");
  3147. }
  3148. return 1;
  3149. }
  3150. static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
  3151. {
  3152. struct kvm_arch *vm_data = &vcpu->kvm->arch;
  3153. int index;
  3154. u32 *logical_apic_id_table;
  3155. int dlid = GET_APIC_LOGICAL_ID(ldr);
  3156. if (!dlid)
  3157. return NULL;
  3158. if (flat) { /* flat */
  3159. index = ffs(dlid) - 1;
  3160. if (index > 7)
  3161. return NULL;
  3162. } else { /* cluster */
  3163. int cluster = (dlid & 0xf0) >> 4;
  3164. int apic = ffs(dlid & 0x0f) - 1;
  3165. if ((apic < 0) || (apic > 7) ||
  3166. (cluster >= 0xf))
  3167. return NULL;
  3168. index = (cluster << 2) + apic;
  3169. }
  3170. logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
  3171. return &logical_apic_id_table[index];
  3172. }
  3173. static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
  3174. bool valid)
  3175. {
  3176. bool flat;
  3177. u32 *entry, new_entry;
  3178. flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
  3179. entry = avic_get_logical_id_entry(vcpu, ldr, flat);
  3180. if (!entry)
  3181. return -EINVAL;
  3182. new_entry = READ_ONCE(*entry);
  3183. new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
  3184. new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
  3185. if (valid)
  3186. new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
  3187. else
  3188. new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
  3189. WRITE_ONCE(*entry, new_entry);
  3190. return 0;
  3191. }
  3192. static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
  3193. {
  3194. int ret;
  3195. struct vcpu_svm *svm = to_svm(vcpu);
  3196. u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
  3197. if (!ldr)
  3198. return 1;
  3199. ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
  3200. if (ret && svm->ldr_reg) {
  3201. avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
  3202. svm->ldr_reg = 0;
  3203. } else {
  3204. svm->ldr_reg = ldr;
  3205. }
  3206. return ret;
  3207. }
  3208. static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
  3209. {
  3210. u64 *old, *new;
  3211. struct vcpu_svm *svm = to_svm(vcpu);
  3212. u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
  3213. u32 id = (apic_id_reg >> 24) & 0xff;
  3214. if (vcpu->vcpu_id == id)
  3215. return 0;
  3216. old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
  3217. new = avic_get_physical_id_entry(vcpu, id);
  3218. if (!new || !old)
  3219. return 1;
  3220. /* We need to move physical_id_entry to new offset */
  3221. *new = *old;
  3222. *old = 0ULL;
  3223. to_svm(vcpu)->avic_physical_id_cache = new;
  3224. /*
  3225. * Also update the guest physical APIC ID in the logical
  3226. * APIC ID table entry if already setup the LDR.
  3227. */
  3228. if (svm->ldr_reg)
  3229. avic_handle_ldr_update(vcpu);
  3230. return 0;
  3231. }
  3232. static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
  3233. {
  3234. struct vcpu_svm *svm = to_svm(vcpu);
  3235. struct kvm_arch *vm_data = &vcpu->kvm->arch;
  3236. u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
  3237. u32 mod = (dfr >> 28) & 0xf;
  3238. /*
  3239. * We assume that all local APICs are using the same type.
  3240. * If this changes, we need to flush the AVIC logical
  3241. * APID id table.
  3242. */
  3243. if (vm_data->ldr_mode == mod)
  3244. return 0;
  3245. clear_page(page_address(vm_data->avic_logical_id_table_page));
  3246. vm_data->ldr_mode = mod;
  3247. if (svm->ldr_reg)
  3248. avic_handle_ldr_update(vcpu);
  3249. return 0;
  3250. }
  3251. static int avic_unaccel_trap_write(struct vcpu_svm *svm)
  3252. {
  3253. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3254. u32 offset = svm->vmcb->control.exit_info_1 &
  3255. AVIC_UNACCEL_ACCESS_OFFSET_MASK;
  3256. switch (offset) {
  3257. case APIC_ID:
  3258. if (avic_handle_apic_id_update(&svm->vcpu))
  3259. return 0;
  3260. break;
  3261. case APIC_LDR:
  3262. if (avic_handle_ldr_update(&svm->vcpu))
  3263. return 0;
  3264. break;
  3265. case APIC_DFR:
  3266. avic_handle_dfr_update(&svm->vcpu);
  3267. break;
  3268. default:
  3269. break;
  3270. }
  3271. kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
  3272. return 1;
  3273. }
  3274. static bool is_avic_unaccelerated_access_trap(u32 offset)
  3275. {
  3276. bool ret = false;
  3277. switch (offset) {
  3278. case APIC_ID:
  3279. case APIC_EOI:
  3280. case APIC_RRR:
  3281. case APIC_LDR:
  3282. case APIC_DFR:
  3283. case APIC_SPIV:
  3284. case APIC_ESR:
  3285. case APIC_ICR:
  3286. case APIC_LVTT:
  3287. case APIC_LVTTHMR:
  3288. case APIC_LVTPC:
  3289. case APIC_LVT0:
  3290. case APIC_LVT1:
  3291. case APIC_LVTERR:
  3292. case APIC_TMICT:
  3293. case APIC_TDCR:
  3294. ret = true;
  3295. break;
  3296. default:
  3297. break;
  3298. }
  3299. return ret;
  3300. }
  3301. static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
  3302. {
  3303. int ret = 0;
  3304. u32 offset = svm->vmcb->control.exit_info_1 &
  3305. AVIC_UNACCEL_ACCESS_OFFSET_MASK;
  3306. u32 vector = svm->vmcb->control.exit_info_2 &
  3307. AVIC_UNACCEL_ACCESS_VECTOR_MASK;
  3308. bool write = (svm->vmcb->control.exit_info_1 >> 32) &
  3309. AVIC_UNACCEL_ACCESS_WRITE_MASK;
  3310. bool trap = is_avic_unaccelerated_access_trap(offset);
  3311. trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
  3312. trap, write, vector);
  3313. if (trap) {
  3314. /* Handling Trap */
  3315. WARN_ONCE(!write, "svm: Handling trap read.\n");
  3316. ret = avic_unaccel_trap_write(svm);
  3317. } else {
  3318. /* Handling Fault */
  3319. ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
  3320. }
  3321. return ret;
  3322. }
  3323. static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
  3324. [SVM_EXIT_READ_CR0] = cr_interception,
  3325. [SVM_EXIT_READ_CR3] = cr_interception,
  3326. [SVM_EXIT_READ_CR4] = cr_interception,
  3327. [SVM_EXIT_READ_CR8] = cr_interception,
  3328. [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
  3329. [SVM_EXIT_WRITE_CR0] = cr_interception,
  3330. [SVM_EXIT_WRITE_CR3] = cr_interception,
  3331. [SVM_EXIT_WRITE_CR4] = cr_interception,
  3332. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  3333. [SVM_EXIT_READ_DR0] = dr_interception,
  3334. [SVM_EXIT_READ_DR1] = dr_interception,
  3335. [SVM_EXIT_READ_DR2] = dr_interception,
  3336. [SVM_EXIT_READ_DR3] = dr_interception,
  3337. [SVM_EXIT_READ_DR4] = dr_interception,
  3338. [SVM_EXIT_READ_DR5] = dr_interception,
  3339. [SVM_EXIT_READ_DR6] = dr_interception,
  3340. [SVM_EXIT_READ_DR7] = dr_interception,
  3341. [SVM_EXIT_WRITE_DR0] = dr_interception,
  3342. [SVM_EXIT_WRITE_DR1] = dr_interception,
  3343. [SVM_EXIT_WRITE_DR2] = dr_interception,
  3344. [SVM_EXIT_WRITE_DR3] = dr_interception,
  3345. [SVM_EXIT_WRITE_DR4] = dr_interception,
  3346. [SVM_EXIT_WRITE_DR5] = dr_interception,
  3347. [SVM_EXIT_WRITE_DR6] = dr_interception,
  3348. [SVM_EXIT_WRITE_DR7] = dr_interception,
  3349. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  3350. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  3351. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  3352. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  3353. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  3354. [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
  3355. [SVM_EXIT_INTR] = intr_interception,
  3356. [SVM_EXIT_NMI] = nmi_interception,
  3357. [SVM_EXIT_SMI] = nop_on_interception,
  3358. [SVM_EXIT_INIT] = nop_on_interception,
  3359. [SVM_EXIT_VINTR] = interrupt_window_interception,
  3360. [SVM_EXIT_RDPMC] = rdpmc_interception,
  3361. [SVM_EXIT_CPUID] = cpuid_interception,
  3362. [SVM_EXIT_IRET] = iret_interception,
  3363. [SVM_EXIT_INVD] = emulate_on_interception,
  3364. [SVM_EXIT_PAUSE] = pause_interception,
  3365. [SVM_EXIT_HLT] = halt_interception,
  3366. [SVM_EXIT_INVLPG] = invlpg_interception,
  3367. [SVM_EXIT_INVLPGA] = invlpga_interception,
  3368. [SVM_EXIT_IOIO] = io_interception,
  3369. [SVM_EXIT_MSR] = msr_interception,
  3370. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  3371. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  3372. [SVM_EXIT_VMRUN] = vmrun_interception,
  3373. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  3374. [SVM_EXIT_VMLOAD] = vmload_interception,
  3375. [SVM_EXIT_VMSAVE] = vmsave_interception,
  3376. [SVM_EXIT_STGI] = stgi_interception,
  3377. [SVM_EXIT_CLGI] = clgi_interception,
  3378. [SVM_EXIT_SKINIT] = skinit_interception,
  3379. [SVM_EXIT_WBINVD] = wbinvd_interception,
  3380. [SVM_EXIT_MONITOR] = monitor_interception,
  3381. [SVM_EXIT_MWAIT] = mwait_interception,
  3382. [SVM_EXIT_XSETBV] = xsetbv_interception,
  3383. [SVM_EXIT_NPF] = pf_interception,
  3384. [SVM_EXIT_RSM] = emulate_on_interception,
  3385. [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
  3386. [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
  3387. };
  3388. static void dump_vmcb(struct kvm_vcpu *vcpu)
  3389. {
  3390. struct vcpu_svm *svm = to_svm(vcpu);
  3391. struct vmcb_control_area *control = &svm->vmcb->control;
  3392. struct vmcb_save_area *save = &svm->vmcb->save;
  3393. pr_err("VMCB Control Area:\n");
  3394. pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
  3395. pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
  3396. pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
  3397. pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
  3398. pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
  3399. pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
  3400. pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
  3401. pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
  3402. pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
  3403. pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
  3404. pr_err("%-20s%d\n", "asid:", control->asid);
  3405. pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
  3406. pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
  3407. pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
  3408. pr_err("%-20s%08x\n", "int_state:", control->int_state);
  3409. pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
  3410. pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
  3411. pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
  3412. pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
  3413. pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
  3414. pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
  3415. pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
  3416. pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
  3417. pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
  3418. pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
  3419. pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
  3420. pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
  3421. pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
  3422. pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
  3423. pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
  3424. pr_err("VMCB State Save Area:\n");
  3425. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3426. "es:",
  3427. save->es.selector, save->es.attrib,
  3428. save->es.limit, save->es.base);
  3429. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3430. "cs:",
  3431. save->cs.selector, save->cs.attrib,
  3432. save->cs.limit, save->cs.base);
  3433. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3434. "ss:",
  3435. save->ss.selector, save->ss.attrib,
  3436. save->ss.limit, save->ss.base);
  3437. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3438. "ds:",
  3439. save->ds.selector, save->ds.attrib,
  3440. save->ds.limit, save->ds.base);
  3441. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3442. "fs:",
  3443. save->fs.selector, save->fs.attrib,
  3444. save->fs.limit, save->fs.base);
  3445. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3446. "gs:",
  3447. save->gs.selector, save->gs.attrib,
  3448. save->gs.limit, save->gs.base);
  3449. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3450. "gdtr:",
  3451. save->gdtr.selector, save->gdtr.attrib,
  3452. save->gdtr.limit, save->gdtr.base);
  3453. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3454. "ldtr:",
  3455. save->ldtr.selector, save->ldtr.attrib,
  3456. save->ldtr.limit, save->ldtr.base);
  3457. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3458. "idtr:",
  3459. save->idtr.selector, save->idtr.attrib,
  3460. save->idtr.limit, save->idtr.base);
  3461. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3462. "tr:",
  3463. save->tr.selector, save->tr.attrib,
  3464. save->tr.limit, save->tr.base);
  3465. pr_err("cpl: %d efer: %016llx\n",
  3466. save->cpl, save->efer);
  3467. pr_err("%-15s %016llx %-13s %016llx\n",
  3468. "cr0:", save->cr0, "cr2:", save->cr2);
  3469. pr_err("%-15s %016llx %-13s %016llx\n",
  3470. "cr3:", save->cr3, "cr4:", save->cr4);
  3471. pr_err("%-15s %016llx %-13s %016llx\n",
  3472. "dr6:", save->dr6, "dr7:", save->dr7);
  3473. pr_err("%-15s %016llx %-13s %016llx\n",
  3474. "rip:", save->rip, "rflags:", save->rflags);
  3475. pr_err("%-15s %016llx %-13s %016llx\n",
  3476. "rsp:", save->rsp, "rax:", save->rax);
  3477. pr_err("%-15s %016llx %-13s %016llx\n",
  3478. "star:", save->star, "lstar:", save->lstar);
  3479. pr_err("%-15s %016llx %-13s %016llx\n",
  3480. "cstar:", save->cstar, "sfmask:", save->sfmask);
  3481. pr_err("%-15s %016llx %-13s %016llx\n",
  3482. "kernel_gs_base:", save->kernel_gs_base,
  3483. "sysenter_cs:", save->sysenter_cs);
  3484. pr_err("%-15s %016llx %-13s %016llx\n",
  3485. "sysenter_esp:", save->sysenter_esp,
  3486. "sysenter_eip:", save->sysenter_eip);
  3487. pr_err("%-15s %016llx %-13s %016llx\n",
  3488. "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
  3489. pr_err("%-15s %016llx %-13s %016llx\n",
  3490. "br_from:", save->br_from, "br_to:", save->br_to);
  3491. pr_err("%-15s %016llx %-13s %016llx\n",
  3492. "excp_from:", save->last_excp_from,
  3493. "excp_to:", save->last_excp_to);
  3494. }
  3495. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  3496. {
  3497. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  3498. *info1 = control->exit_info_1;
  3499. *info2 = control->exit_info_2;
  3500. }
  3501. static int handle_exit(struct kvm_vcpu *vcpu)
  3502. {
  3503. struct vcpu_svm *svm = to_svm(vcpu);
  3504. struct kvm_run *kvm_run = vcpu->run;
  3505. u32 exit_code = svm->vmcb->control.exit_code;
  3506. trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
  3507. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  3508. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  3509. if (npt_enabled)
  3510. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  3511. if (unlikely(svm->nested.exit_required)) {
  3512. nested_svm_vmexit(svm);
  3513. svm->nested.exit_required = false;
  3514. return 1;
  3515. }
  3516. if (is_guest_mode(vcpu)) {
  3517. int vmexit;
  3518. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  3519. svm->vmcb->control.exit_info_1,
  3520. svm->vmcb->control.exit_info_2,
  3521. svm->vmcb->control.exit_int_info,
  3522. svm->vmcb->control.exit_int_info_err,
  3523. KVM_ISA_SVM);
  3524. vmexit = nested_svm_exit_special(svm);
  3525. if (vmexit == NESTED_EXIT_CONTINUE)
  3526. vmexit = nested_svm_exit_handled(svm);
  3527. if (vmexit == NESTED_EXIT_DONE)
  3528. return 1;
  3529. }
  3530. svm_complete_interrupts(svm);
  3531. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  3532. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3533. kvm_run->fail_entry.hardware_entry_failure_reason
  3534. = svm->vmcb->control.exit_code;
  3535. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  3536. dump_vmcb(vcpu);
  3537. return 0;
  3538. }
  3539. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  3540. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  3541. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  3542. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  3543. printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
  3544. "exit_code 0x%x\n",
  3545. __func__, svm->vmcb->control.exit_int_info,
  3546. exit_code);
  3547. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  3548. || !svm_exit_handlers[exit_code]) {
  3549. WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
  3550. kvm_queue_exception(vcpu, UD_VECTOR);
  3551. return 1;
  3552. }
  3553. return svm_exit_handlers[exit_code](svm);
  3554. }
  3555. static void reload_tss(struct kvm_vcpu *vcpu)
  3556. {
  3557. int cpu = raw_smp_processor_id();
  3558. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  3559. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  3560. load_TR_desc();
  3561. }
  3562. static void pre_svm_run(struct vcpu_svm *svm)
  3563. {
  3564. int cpu = raw_smp_processor_id();
  3565. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  3566. /* FIXME: handle wraparound of asid_generation */
  3567. if (svm->asid_generation != sd->asid_generation)
  3568. new_asid(svm, sd);
  3569. }
  3570. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  3571. {
  3572. struct vcpu_svm *svm = to_svm(vcpu);
  3573. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  3574. vcpu->arch.hflags |= HF_NMI_MASK;
  3575. set_intercept(svm, INTERCEPT_IRET);
  3576. ++vcpu->stat.nmi_injections;
  3577. }
  3578. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  3579. {
  3580. struct vmcb_control_area *control;
  3581. /* The following fields are ignored when AVIC is enabled */
  3582. control = &svm->vmcb->control;
  3583. control->int_vector = irq;
  3584. control->int_ctl &= ~V_INTR_PRIO_MASK;
  3585. control->int_ctl |= V_IRQ_MASK |
  3586. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  3587. mark_dirty(svm->vmcb, VMCB_INTR);
  3588. }
  3589. static void svm_set_irq(struct kvm_vcpu *vcpu)
  3590. {
  3591. struct vcpu_svm *svm = to_svm(vcpu);
  3592. BUG_ON(!(gif_set(svm)));
  3593. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  3594. ++vcpu->stat.irq_injections;
  3595. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  3596. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  3597. }
  3598. static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
  3599. {
  3600. return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
  3601. }
  3602. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  3603. {
  3604. struct vcpu_svm *svm = to_svm(vcpu);
  3605. if (svm_nested_virtualize_tpr(vcpu) ||
  3606. kvm_vcpu_apicv_active(vcpu))
  3607. return;
  3608. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  3609. if (irr == -1)
  3610. return;
  3611. if (tpr >= irr)
  3612. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  3613. }
  3614. static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  3615. {
  3616. return;
  3617. }
  3618. static bool svm_get_enable_apicv(void)
  3619. {
  3620. return avic;
  3621. }
  3622. static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  3623. {
  3624. }
  3625. static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  3626. {
  3627. }
  3628. /* Note: Currently only used by Hyper-V. */
  3629. static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  3630. {
  3631. struct vcpu_svm *svm = to_svm(vcpu);
  3632. struct vmcb *vmcb = svm->vmcb;
  3633. if (!avic)
  3634. return;
  3635. vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
  3636. mark_dirty(vmcb, VMCB_INTR);
  3637. }
  3638. static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  3639. {
  3640. return;
  3641. }
  3642. static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
  3643. {
  3644. kvm_lapic_set_irr(vec, vcpu->arch.apic);
  3645. smp_mb__after_atomic();
  3646. if (avic_vcpu_is_running(vcpu))
  3647. wrmsrl(SVM_AVIC_DOORBELL,
  3648. kvm_cpu_get_apicid(vcpu->cpu));
  3649. else
  3650. kvm_vcpu_wake_up(vcpu);
  3651. }
  3652. static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
  3653. {
  3654. unsigned long flags;
  3655. struct amd_svm_iommu_ir *cur;
  3656. spin_lock_irqsave(&svm->ir_list_lock, flags);
  3657. list_for_each_entry(cur, &svm->ir_list, node) {
  3658. if (cur->data != pi->ir_data)
  3659. continue;
  3660. list_del(&cur->node);
  3661. kfree(cur);
  3662. break;
  3663. }
  3664. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  3665. }
  3666. static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
  3667. {
  3668. int ret = 0;
  3669. unsigned long flags;
  3670. struct amd_svm_iommu_ir *ir;
  3671. /**
  3672. * In some cases, the existing irte is updaed and re-set,
  3673. * so we need to check here if it's already been * added
  3674. * to the ir_list.
  3675. */
  3676. if (pi->ir_data && (pi->prev_ga_tag != 0)) {
  3677. struct kvm *kvm = svm->vcpu.kvm;
  3678. u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
  3679. struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
  3680. struct vcpu_svm *prev_svm;
  3681. if (!prev_vcpu) {
  3682. ret = -EINVAL;
  3683. goto out;
  3684. }
  3685. prev_svm = to_svm(prev_vcpu);
  3686. svm_ir_list_del(prev_svm, pi);
  3687. }
  3688. /**
  3689. * Allocating new amd_iommu_pi_data, which will get
  3690. * add to the per-vcpu ir_list.
  3691. */
  3692. ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
  3693. if (!ir) {
  3694. ret = -ENOMEM;
  3695. goto out;
  3696. }
  3697. ir->data = pi->ir_data;
  3698. spin_lock_irqsave(&svm->ir_list_lock, flags);
  3699. list_add(&ir->node, &svm->ir_list);
  3700. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  3701. out:
  3702. return ret;
  3703. }
  3704. /**
  3705. * Note:
  3706. * The HW cannot support posting multicast/broadcast
  3707. * interrupts to a vCPU. So, we still use legacy interrupt
  3708. * remapping for these kind of interrupts.
  3709. *
  3710. * For lowest-priority interrupts, we only support
  3711. * those with single CPU as the destination, e.g. user
  3712. * configures the interrupts via /proc/irq or uses
  3713. * irqbalance to make the interrupts single-CPU.
  3714. */
  3715. static int
  3716. get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
  3717. struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
  3718. {
  3719. struct kvm_lapic_irq irq;
  3720. struct kvm_vcpu *vcpu = NULL;
  3721. kvm_set_msi_irq(kvm, e, &irq);
  3722. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  3723. pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
  3724. __func__, irq.vector);
  3725. return -1;
  3726. }
  3727. pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
  3728. irq.vector);
  3729. *svm = to_svm(vcpu);
  3730. vcpu_info->pi_desc_addr = page_to_phys((*svm)->avic_backing_page);
  3731. vcpu_info->vector = irq.vector;
  3732. return 0;
  3733. }
  3734. /*
  3735. * svm_update_pi_irte - set IRTE for Posted-Interrupts
  3736. *
  3737. * @kvm: kvm
  3738. * @host_irq: host irq of the interrupt
  3739. * @guest_irq: gsi of the interrupt
  3740. * @set: set or unset PI
  3741. * returns 0 on success, < 0 on failure
  3742. */
  3743. static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  3744. uint32_t guest_irq, bool set)
  3745. {
  3746. struct kvm_kernel_irq_routing_entry *e;
  3747. struct kvm_irq_routing_table *irq_rt;
  3748. int idx, ret = -EINVAL;
  3749. if (!kvm_arch_has_assigned_device(kvm) ||
  3750. !irq_remapping_cap(IRQ_POSTING_CAP))
  3751. return 0;
  3752. pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
  3753. __func__, host_irq, guest_irq, set);
  3754. idx = srcu_read_lock(&kvm->irq_srcu);
  3755. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  3756. WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
  3757. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  3758. struct vcpu_data vcpu_info;
  3759. struct vcpu_svm *svm = NULL;
  3760. if (e->type != KVM_IRQ_ROUTING_MSI)
  3761. continue;
  3762. /**
  3763. * Here, we setup with legacy mode in the following cases:
  3764. * 1. When cannot target interrupt to a specific vcpu.
  3765. * 2. Unsetting posted interrupt.
  3766. * 3. APIC virtialization is disabled for the vcpu.
  3767. */
  3768. if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
  3769. kvm_vcpu_apicv_active(&svm->vcpu)) {
  3770. struct amd_iommu_pi_data pi;
  3771. /* Try to enable guest_mode in IRTE */
  3772. pi.base = page_to_phys(svm->avic_backing_page) & AVIC_HPA_MASK;
  3773. pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
  3774. svm->vcpu.vcpu_id);
  3775. pi.is_guest_mode = true;
  3776. pi.vcpu_data = &vcpu_info;
  3777. ret = irq_set_vcpu_affinity(host_irq, &pi);
  3778. /**
  3779. * Here, we successfully setting up vcpu affinity in
  3780. * IOMMU guest mode. Now, we need to store the posted
  3781. * interrupt information in a per-vcpu ir_list so that
  3782. * we can reference to them directly when we update vcpu
  3783. * scheduling information in IOMMU irte.
  3784. */
  3785. if (!ret && pi.is_guest_mode)
  3786. svm_ir_list_add(svm, &pi);
  3787. } else {
  3788. /* Use legacy mode in IRTE */
  3789. struct amd_iommu_pi_data pi;
  3790. /**
  3791. * Here, pi is used to:
  3792. * - Tell IOMMU to use legacy mode for this interrupt.
  3793. * - Retrieve ga_tag of prior interrupt remapping data.
  3794. */
  3795. pi.is_guest_mode = false;
  3796. ret = irq_set_vcpu_affinity(host_irq, &pi);
  3797. /**
  3798. * Check if the posted interrupt was previously
  3799. * setup with the guest_mode by checking if the ga_tag
  3800. * was cached. If so, we need to clean up the per-vcpu
  3801. * ir_list.
  3802. */
  3803. if (!ret && pi.prev_ga_tag) {
  3804. int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
  3805. struct kvm_vcpu *vcpu;
  3806. vcpu = kvm_get_vcpu_by_id(kvm, id);
  3807. if (vcpu)
  3808. svm_ir_list_del(to_svm(vcpu), &pi);
  3809. }
  3810. }
  3811. if (!ret && svm) {
  3812. trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
  3813. host_irq, e->gsi,
  3814. vcpu_info.vector,
  3815. vcpu_info.pi_desc_addr, set);
  3816. }
  3817. if (ret < 0) {
  3818. pr_err("%s: failed to update PI IRTE\n", __func__);
  3819. goto out;
  3820. }
  3821. }
  3822. ret = 0;
  3823. out:
  3824. srcu_read_unlock(&kvm->irq_srcu, idx);
  3825. return ret;
  3826. }
  3827. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  3828. {
  3829. struct vcpu_svm *svm = to_svm(vcpu);
  3830. struct vmcb *vmcb = svm->vmcb;
  3831. int ret;
  3832. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  3833. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  3834. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  3835. return ret;
  3836. }
  3837. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  3838. {
  3839. struct vcpu_svm *svm = to_svm(vcpu);
  3840. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  3841. }
  3842. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  3843. {
  3844. struct vcpu_svm *svm = to_svm(vcpu);
  3845. if (masked) {
  3846. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  3847. set_intercept(svm, INTERCEPT_IRET);
  3848. } else {
  3849. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  3850. clr_intercept(svm, INTERCEPT_IRET);
  3851. }
  3852. }
  3853. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  3854. {
  3855. struct vcpu_svm *svm = to_svm(vcpu);
  3856. struct vmcb *vmcb = svm->vmcb;
  3857. int ret;
  3858. if (!gif_set(svm) ||
  3859. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  3860. return 0;
  3861. ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
  3862. if (is_guest_mode(vcpu))
  3863. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  3864. return ret;
  3865. }
  3866. static void enable_irq_window(struct kvm_vcpu *vcpu)
  3867. {
  3868. struct vcpu_svm *svm = to_svm(vcpu);
  3869. if (kvm_vcpu_apicv_active(vcpu))
  3870. return;
  3871. /*
  3872. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  3873. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  3874. * get that intercept, this function will be called again though and
  3875. * we'll get the vintr intercept. However, if the vGIF feature is
  3876. * enabled, the STGI interception will not occur. Enable the irq
  3877. * window under the assumption that the hardware will set the GIF.
  3878. */
  3879. if ((vgif_enabled(svm) || gif_set(svm)) && nested_svm_intr(svm)) {
  3880. svm_set_vintr(svm);
  3881. svm_inject_irq(svm, 0x0);
  3882. }
  3883. }
  3884. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  3885. {
  3886. struct vcpu_svm *svm = to_svm(vcpu);
  3887. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  3888. == HF_NMI_MASK)
  3889. return; /* IRET will cause a vm exit */
  3890. if (!gif_set(svm)) {
  3891. if (vgif_enabled(svm))
  3892. set_intercept(svm, INTERCEPT_STGI);
  3893. return; /* STGI will cause a vm exit */
  3894. }
  3895. if (svm->nested.exit_required)
  3896. return; /* we're not going to run the guest yet */
  3897. /*
  3898. * Something prevents NMI from been injected. Single step over possible
  3899. * problem (IRET or exception injection or interrupt shadow)
  3900. */
  3901. svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
  3902. svm->nmi_singlestep = true;
  3903. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  3904. }
  3905. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  3906. {
  3907. return 0;
  3908. }
  3909. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  3910. {
  3911. struct vcpu_svm *svm = to_svm(vcpu);
  3912. if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
  3913. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
  3914. else
  3915. svm->asid_generation--;
  3916. }
  3917. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  3918. {
  3919. }
  3920. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  3921. {
  3922. struct vcpu_svm *svm = to_svm(vcpu);
  3923. if (svm_nested_virtualize_tpr(vcpu))
  3924. return;
  3925. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  3926. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  3927. kvm_set_cr8(vcpu, cr8);
  3928. }
  3929. }
  3930. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  3931. {
  3932. struct vcpu_svm *svm = to_svm(vcpu);
  3933. u64 cr8;
  3934. if (svm_nested_virtualize_tpr(vcpu) ||
  3935. kvm_vcpu_apicv_active(vcpu))
  3936. return;
  3937. cr8 = kvm_get_cr8(vcpu);
  3938. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  3939. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  3940. }
  3941. static void svm_complete_interrupts(struct vcpu_svm *svm)
  3942. {
  3943. u8 vector;
  3944. int type;
  3945. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  3946. unsigned int3_injected = svm->int3_injected;
  3947. svm->int3_injected = 0;
  3948. /*
  3949. * If we've made progress since setting HF_IRET_MASK, we've
  3950. * executed an IRET and can allow NMI injection.
  3951. */
  3952. if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
  3953. && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
  3954. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  3955. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3956. }
  3957. svm->vcpu.arch.nmi_injected = false;
  3958. kvm_clear_exception_queue(&svm->vcpu);
  3959. kvm_clear_interrupt_queue(&svm->vcpu);
  3960. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  3961. return;
  3962. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3963. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  3964. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  3965. switch (type) {
  3966. case SVM_EXITINTINFO_TYPE_NMI:
  3967. svm->vcpu.arch.nmi_injected = true;
  3968. break;
  3969. case SVM_EXITINTINFO_TYPE_EXEPT:
  3970. /*
  3971. * In case of software exceptions, do not reinject the vector,
  3972. * but re-execute the instruction instead. Rewind RIP first
  3973. * if we emulated INT3 before.
  3974. */
  3975. if (kvm_exception_is_soft(vector)) {
  3976. if (vector == BP_VECTOR && int3_injected &&
  3977. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  3978. kvm_rip_write(&svm->vcpu,
  3979. kvm_rip_read(&svm->vcpu) -
  3980. int3_injected);
  3981. break;
  3982. }
  3983. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  3984. u32 err = svm->vmcb->control.exit_int_info_err;
  3985. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  3986. } else
  3987. kvm_requeue_exception(&svm->vcpu, vector);
  3988. break;
  3989. case SVM_EXITINTINFO_TYPE_INTR:
  3990. kvm_queue_interrupt(&svm->vcpu, vector, false);
  3991. break;
  3992. default:
  3993. break;
  3994. }
  3995. }
  3996. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  3997. {
  3998. struct vcpu_svm *svm = to_svm(vcpu);
  3999. struct vmcb_control_area *control = &svm->vmcb->control;
  4000. control->exit_int_info = control->event_inj;
  4001. control->exit_int_info_err = control->event_inj_err;
  4002. control->event_inj = 0;
  4003. svm_complete_interrupts(svm);
  4004. }
  4005. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  4006. {
  4007. struct vcpu_svm *svm = to_svm(vcpu);
  4008. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  4009. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  4010. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  4011. /*
  4012. * A vmexit emulation is required before the vcpu can be executed
  4013. * again.
  4014. */
  4015. if (unlikely(svm->nested.exit_required))
  4016. return;
  4017. /*
  4018. * Disable singlestep if we're injecting an interrupt/exception.
  4019. * We don't want our modified rflags to be pushed on the stack where
  4020. * we might not be able to easily reset them if we disabled NMI
  4021. * singlestep later.
  4022. */
  4023. if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
  4024. /*
  4025. * Event injection happens before external interrupts cause a
  4026. * vmexit and interrupts are disabled here, so smp_send_reschedule
  4027. * is enough to force an immediate vmexit.
  4028. */
  4029. disable_nmi_singlestep(svm);
  4030. smp_send_reschedule(vcpu->cpu);
  4031. }
  4032. pre_svm_run(svm);
  4033. sync_lapic_to_cr8(vcpu);
  4034. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  4035. clgi();
  4036. local_irq_enable();
  4037. asm volatile (
  4038. "push %%" _ASM_BP "; \n\t"
  4039. "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
  4040. "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
  4041. "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
  4042. "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
  4043. "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
  4044. "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
  4045. #ifdef CONFIG_X86_64
  4046. "mov %c[r8](%[svm]), %%r8 \n\t"
  4047. "mov %c[r9](%[svm]), %%r9 \n\t"
  4048. "mov %c[r10](%[svm]), %%r10 \n\t"
  4049. "mov %c[r11](%[svm]), %%r11 \n\t"
  4050. "mov %c[r12](%[svm]), %%r12 \n\t"
  4051. "mov %c[r13](%[svm]), %%r13 \n\t"
  4052. "mov %c[r14](%[svm]), %%r14 \n\t"
  4053. "mov %c[r15](%[svm]), %%r15 \n\t"
  4054. #endif
  4055. /* Enter guest mode */
  4056. "push %%" _ASM_AX " \n\t"
  4057. "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
  4058. __ex(SVM_VMLOAD) "\n\t"
  4059. __ex(SVM_VMRUN) "\n\t"
  4060. __ex(SVM_VMSAVE) "\n\t"
  4061. "pop %%" _ASM_AX " \n\t"
  4062. /* Save guest registers, load host registers */
  4063. "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
  4064. "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
  4065. "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
  4066. "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
  4067. "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
  4068. "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
  4069. #ifdef CONFIG_X86_64
  4070. "mov %%r8, %c[r8](%[svm]) \n\t"
  4071. "mov %%r9, %c[r9](%[svm]) \n\t"
  4072. "mov %%r10, %c[r10](%[svm]) \n\t"
  4073. "mov %%r11, %c[r11](%[svm]) \n\t"
  4074. "mov %%r12, %c[r12](%[svm]) \n\t"
  4075. "mov %%r13, %c[r13](%[svm]) \n\t"
  4076. "mov %%r14, %c[r14](%[svm]) \n\t"
  4077. "mov %%r15, %c[r15](%[svm]) \n\t"
  4078. #endif
  4079. "pop %%" _ASM_BP
  4080. :
  4081. : [svm]"a"(svm),
  4082. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  4083. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  4084. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  4085. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  4086. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  4087. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  4088. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  4089. #ifdef CONFIG_X86_64
  4090. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  4091. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  4092. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  4093. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  4094. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  4095. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  4096. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  4097. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  4098. #endif
  4099. : "cc", "memory"
  4100. #ifdef CONFIG_X86_64
  4101. , "rbx", "rcx", "rdx", "rsi", "rdi"
  4102. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  4103. #else
  4104. , "ebx", "ecx", "edx", "esi", "edi"
  4105. #endif
  4106. );
  4107. #ifdef CONFIG_X86_64
  4108. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  4109. #else
  4110. loadsegment(fs, svm->host.fs);
  4111. #ifndef CONFIG_X86_32_LAZY_GS
  4112. loadsegment(gs, svm->host.gs);
  4113. #endif
  4114. #endif
  4115. reload_tss(vcpu);
  4116. local_irq_disable();
  4117. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  4118. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  4119. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  4120. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  4121. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  4122. kvm_before_handle_nmi(&svm->vcpu);
  4123. stgi();
  4124. /* Any pending NMI will happen here */
  4125. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  4126. kvm_after_handle_nmi(&svm->vcpu);
  4127. sync_cr8_to_lapic(vcpu);
  4128. svm->next_rip = 0;
  4129. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  4130. /* if exit due to PF check for async PF */
  4131. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  4132. svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
  4133. if (npt_enabled) {
  4134. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  4135. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  4136. }
  4137. /*
  4138. * We need to handle MC intercepts here before the vcpu has a chance to
  4139. * change the physical cpu
  4140. */
  4141. if (unlikely(svm->vmcb->control.exit_code ==
  4142. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  4143. svm_handle_mce(svm);
  4144. mark_all_clean(svm->vmcb);
  4145. }
  4146. STACK_FRAME_NON_STANDARD(svm_vcpu_run);
  4147. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  4148. {
  4149. struct vcpu_svm *svm = to_svm(vcpu);
  4150. svm->vmcb->save.cr3 = root;
  4151. mark_dirty(svm->vmcb, VMCB_CR);
  4152. svm_flush_tlb(vcpu);
  4153. }
  4154. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  4155. {
  4156. struct vcpu_svm *svm = to_svm(vcpu);
  4157. svm->vmcb->control.nested_cr3 = root;
  4158. mark_dirty(svm->vmcb, VMCB_NPT);
  4159. /* Also sync guest cr3 here in case we live migrate */
  4160. svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
  4161. mark_dirty(svm->vmcb, VMCB_CR);
  4162. svm_flush_tlb(vcpu);
  4163. }
  4164. static int is_disabled(void)
  4165. {
  4166. u64 vm_cr;
  4167. rdmsrl(MSR_VM_CR, vm_cr);
  4168. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  4169. return 1;
  4170. return 0;
  4171. }
  4172. static void
  4173. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  4174. {
  4175. /*
  4176. * Patch in the VMMCALL instruction:
  4177. */
  4178. hypercall[0] = 0x0f;
  4179. hypercall[1] = 0x01;
  4180. hypercall[2] = 0xd9;
  4181. }
  4182. static void svm_check_processor_compat(void *rtn)
  4183. {
  4184. *(int *)rtn = 0;
  4185. }
  4186. static bool svm_cpu_has_accelerated_tpr(void)
  4187. {
  4188. return false;
  4189. }
  4190. static bool svm_has_high_real_mode_segbase(void)
  4191. {
  4192. return true;
  4193. }
  4194. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  4195. {
  4196. return 0;
  4197. }
  4198. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  4199. {
  4200. struct vcpu_svm *svm = to_svm(vcpu);
  4201. /* Update nrips enabled cache */
  4202. svm->nrips_enabled = !!guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
  4203. if (!kvm_vcpu_apicv_active(vcpu))
  4204. return;
  4205. guest_cpuid_clear(vcpu, X86_FEATURE_X2APIC);
  4206. }
  4207. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  4208. {
  4209. switch (func) {
  4210. case 0x1:
  4211. if (avic)
  4212. entry->ecx &= ~bit(X86_FEATURE_X2APIC);
  4213. break;
  4214. case 0x80000001:
  4215. if (nested)
  4216. entry->ecx |= (1 << 2); /* Set SVM bit */
  4217. break;
  4218. case 0x8000000A:
  4219. entry->eax = 1; /* SVM revision 1 */
  4220. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  4221. ASID emulation to nested SVM */
  4222. entry->ecx = 0; /* Reserved */
  4223. entry->edx = 0; /* Per default do not support any
  4224. additional features */
  4225. /* Support next_rip if host supports it */
  4226. if (boot_cpu_has(X86_FEATURE_NRIPS))
  4227. entry->edx |= SVM_FEATURE_NRIP;
  4228. /* Support NPT for the guest if enabled */
  4229. if (npt_enabled)
  4230. entry->edx |= SVM_FEATURE_NPT;
  4231. break;
  4232. }
  4233. }
  4234. static int svm_get_lpage_level(void)
  4235. {
  4236. return PT_PDPE_LEVEL;
  4237. }
  4238. static bool svm_rdtscp_supported(void)
  4239. {
  4240. return boot_cpu_has(X86_FEATURE_RDTSCP);
  4241. }
  4242. static bool svm_invpcid_supported(void)
  4243. {
  4244. return false;
  4245. }
  4246. static bool svm_mpx_supported(void)
  4247. {
  4248. return false;
  4249. }
  4250. static bool svm_xsaves_supported(void)
  4251. {
  4252. return false;
  4253. }
  4254. static bool svm_has_wbinvd_exit(void)
  4255. {
  4256. return true;
  4257. }
  4258. #define PRE_EX(exit) { .exit_code = (exit), \
  4259. .stage = X86_ICPT_PRE_EXCEPT, }
  4260. #define POST_EX(exit) { .exit_code = (exit), \
  4261. .stage = X86_ICPT_POST_EXCEPT, }
  4262. #define POST_MEM(exit) { .exit_code = (exit), \
  4263. .stage = X86_ICPT_POST_MEMACCESS, }
  4264. static const struct __x86_intercept {
  4265. u32 exit_code;
  4266. enum x86_intercept_stage stage;
  4267. } x86_intercept_map[] = {
  4268. [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
  4269. [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
  4270. [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
  4271. [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
  4272. [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
  4273. [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
  4274. [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
  4275. [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
  4276. [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
  4277. [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
  4278. [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
  4279. [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
  4280. [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
  4281. [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
  4282. [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
  4283. [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
  4284. [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
  4285. [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
  4286. [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
  4287. [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
  4288. [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
  4289. [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
  4290. [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
  4291. [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
  4292. [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
  4293. [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
  4294. [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
  4295. [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
  4296. [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
  4297. [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
  4298. [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
  4299. [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
  4300. [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
  4301. [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
  4302. [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
  4303. [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
  4304. [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
  4305. [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
  4306. [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
  4307. [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
  4308. [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
  4309. [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
  4310. [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
  4311. [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
  4312. [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
  4313. [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
  4314. };
  4315. #undef PRE_EX
  4316. #undef POST_EX
  4317. #undef POST_MEM
  4318. static int svm_check_intercept(struct kvm_vcpu *vcpu,
  4319. struct x86_instruction_info *info,
  4320. enum x86_intercept_stage stage)
  4321. {
  4322. struct vcpu_svm *svm = to_svm(vcpu);
  4323. int vmexit, ret = X86EMUL_CONTINUE;
  4324. struct __x86_intercept icpt_info;
  4325. struct vmcb *vmcb = svm->vmcb;
  4326. if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
  4327. goto out;
  4328. icpt_info = x86_intercept_map[info->intercept];
  4329. if (stage != icpt_info.stage)
  4330. goto out;
  4331. switch (icpt_info.exit_code) {
  4332. case SVM_EXIT_READ_CR0:
  4333. if (info->intercept == x86_intercept_cr_read)
  4334. icpt_info.exit_code += info->modrm_reg;
  4335. break;
  4336. case SVM_EXIT_WRITE_CR0: {
  4337. unsigned long cr0, val;
  4338. u64 intercept;
  4339. if (info->intercept == x86_intercept_cr_write)
  4340. icpt_info.exit_code += info->modrm_reg;
  4341. if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
  4342. info->intercept == x86_intercept_clts)
  4343. break;
  4344. intercept = svm->nested.intercept;
  4345. if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
  4346. break;
  4347. cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
  4348. val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
  4349. if (info->intercept == x86_intercept_lmsw) {
  4350. cr0 &= 0xfUL;
  4351. val &= 0xfUL;
  4352. /* lmsw can't clear PE - catch this here */
  4353. if (cr0 & X86_CR0_PE)
  4354. val |= X86_CR0_PE;
  4355. }
  4356. if (cr0 ^ val)
  4357. icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  4358. break;
  4359. }
  4360. case SVM_EXIT_READ_DR0:
  4361. case SVM_EXIT_WRITE_DR0:
  4362. icpt_info.exit_code += info->modrm_reg;
  4363. break;
  4364. case SVM_EXIT_MSR:
  4365. if (info->intercept == x86_intercept_wrmsr)
  4366. vmcb->control.exit_info_1 = 1;
  4367. else
  4368. vmcb->control.exit_info_1 = 0;
  4369. break;
  4370. case SVM_EXIT_PAUSE:
  4371. /*
  4372. * We get this for NOP only, but pause
  4373. * is rep not, check this here
  4374. */
  4375. if (info->rep_prefix != REPE_PREFIX)
  4376. goto out;
  4377. case SVM_EXIT_IOIO: {
  4378. u64 exit_info;
  4379. u32 bytes;
  4380. if (info->intercept == x86_intercept_in ||
  4381. info->intercept == x86_intercept_ins) {
  4382. exit_info = ((info->src_val & 0xffff) << 16) |
  4383. SVM_IOIO_TYPE_MASK;
  4384. bytes = info->dst_bytes;
  4385. } else {
  4386. exit_info = (info->dst_val & 0xffff) << 16;
  4387. bytes = info->src_bytes;
  4388. }
  4389. if (info->intercept == x86_intercept_outs ||
  4390. info->intercept == x86_intercept_ins)
  4391. exit_info |= SVM_IOIO_STR_MASK;
  4392. if (info->rep_prefix)
  4393. exit_info |= SVM_IOIO_REP_MASK;
  4394. bytes = min(bytes, 4u);
  4395. exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
  4396. exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
  4397. vmcb->control.exit_info_1 = exit_info;
  4398. vmcb->control.exit_info_2 = info->next_rip;
  4399. break;
  4400. }
  4401. default:
  4402. break;
  4403. }
  4404. /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
  4405. if (static_cpu_has(X86_FEATURE_NRIPS))
  4406. vmcb->control.next_rip = info->next_rip;
  4407. vmcb->control.exit_code = icpt_info.exit_code;
  4408. vmexit = nested_svm_exit_handled(svm);
  4409. ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
  4410. : X86EMUL_CONTINUE;
  4411. out:
  4412. return ret;
  4413. }
  4414. static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
  4415. {
  4416. local_irq_enable();
  4417. /*
  4418. * We must have an instruction with interrupts enabled, so
  4419. * the timer interrupt isn't delayed by the interrupt shadow.
  4420. */
  4421. asm("nop");
  4422. local_irq_disable();
  4423. }
  4424. static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
  4425. {
  4426. }
  4427. static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
  4428. {
  4429. if (avic_handle_apic_id_update(vcpu) != 0)
  4430. return;
  4431. if (avic_handle_dfr_update(vcpu) != 0)
  4432. return;
  4433. avic_handle_ldr_update(vcpu);
  4434. }
  4435. static void svm_setup_mce(struct kvm_vcpu *vcpu)
  4436. {
  4437. /* [63:9] are reserved. */
  4438. vcpu->arch.mcg_cap &= 0x1ff;
  4439. }
  4440. static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
  4441. .cpu_has_kvm_support = has_svm,
  4442. .disabled_by_bios = is_disabled,
  4443. .hardware_setup = svm_hardware_setup,
  4444. .hardware_unsetup = svm_hardware_unsetup,
  4445. .check_processor_compatibility = svm_check_processor_compat,
  4446. .hardware_enable = svm_hardware_enable,
  4447. .hardware_disable = svm_hardware_disable,
  4448. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  4449. .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
  4450. .vcpu_create = svm_create_vcpu,
  4451. .vcpu_free = svm_free_vcpu,
  4452. .vcpu_reset = svm_vcpu_reset,
  4453. .vm_init = avic_vm_init,
  4454. .vm_destroy = avic_vm_destroy,
  4455. .prepare_guest_switch = svm_prepare_guest_switch,
  4456. .vcpu_load = svm_vcpu_load,
  4457. .vcpu_put = svm_vcpu_put,
  4458. .vcpu_blocking = svm_vcpu_blocking,
  4459. .vcpu_unblocking = svm_vcpu_unblocking,
  4460. .update_bp_intercept = update_bp_intercept,
  4461. .get_msr = svm_get_msr,
  4462. .set_msr = svm_set_msr,
  4463. .get_segment_base = svm_get_segment_base,
  4464. .get_segment = svm_get_segment,
  4465. .set_segment = svm_set_segment,
  4466. .get_cpl = svm_get_cpl,
  4467. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  4468. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  4469. .decache_cr3 = svm_decache_cr3,
  4470. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  4471. .set_cr0 = svm_set_cr0,
  4472. .set_cr3 = svm_set_cr3,
  4473. .set_cr4 = svm_set_cr4,
  4474. .set_efer = svm_set_efer,
  4475. .get_idt = svm_get_idt,
  4476. .set_idt = svm_set_idt,
  4477. .get_gdt = svm_get_gdt,
  4478. .set_gdt = svm_set_gdt,
  4479. .get_dr6 = svm_get_dr6,
  4480. .set_dr6 = svm_set_dr6,
  4481. .set_dr7 = svm_set_dr7,
  4482. .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
  4483. .cache_reg = svm_cache_reg,
  4484. .get_rflags = svm_get_rflags,
  4485. .set_rflags = svm_set_rflags,
  4486. .get_pkru = svm_get_pkru,
  4487. .tlb_flush = svm_flush_tlb,
  4488. .run = svm_vcpu_run,
  4489. .handle_exit = handle_exit,
  4490. .skip_emulated_instruction = skip_emulated_instruction,
  4491. .set_interrupt_shadow = svm_set_interrupt_shadow,
  4492. .get_interrupt_shadow = svm_get_interrupt_shadow,
  4493. .patch_hypercall = svm_patch_hypercall,
  4494. .set_irq = svm_set_irq,
  4495. .set_nmi = svm_inject_nmi,
  4496. .queue_exception = svm_queue_exception,
  4497. .cancel_injection = svm_cancel_injection,
  4498. .interrupt_allowed = svm_interrupt_allowed,
  4499. .nmi_allowed = svm_nmi_allowed,
  4500. .get_nmi_mask = svm_get_nmi_mask,
  4501. .set_nmi_mask = svm_set_nmi_mask,
  4502. .enable_nmi_window = enable_nmi_window,
  4503. .enable_irq_window = enable_irq_window,
  4504. .update_cr8_intercept = update_cr8_intercept,
  4505. .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
  4506. .get_enable_apicv = svm_get_enable_apicv,
  4507. .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
  4508. .load_eoi_exitmap = svm_load_eoi_exitmap,
  4509. .hwapic_irr_update = svm_hwapic_irr_update,
  4510. .hwapic_isr_update = svm_hwapic_isr_update,
  4511. .apicv_post_state_restore = avic_post_state_restore,
  4512. .set_tss_addr = svm_set_tss_addr,
  4513. .get_tdp_level = get_npt_level,
  4514. .get_mt_mask = svm_get_mt_mask,
  4515. .get_exit_info = svm_get_exit_info,
  4516. .get_lpage_level = svm_get_lpage_level,
  4517. .cpuid_update = svm_cpuid_update,
  4518. .rdtscp_supported = svm_rdtscp_supported,
  4519. .invpcid_supported = svm_invpcid_supported,
  4520. .mpx_supported = svm_mpx_supported,
  4521. .xsaves_supported = svm_xsaves_supported,
  4522. .set_supported_cpuid = svm_set_supported_cpuid,
  4523. .has_wbinvd_exit = svm_has_wbinvd_exit,
  4524. .write_tsc_offset = svm_write_tsc_offset,
  4525. .set_tdp_cr3 = set_tdp_cr3,
  4526. .check_intercept = svm_check_intercept,
  4527. .handle_external_intr = svm_handle_external_intr,
  4528. .sched_in = svm_sched_in,
  4529. .pmu_ops = &amd_pmu_ops,
  4530. .deliver_posted_interrupt = svm_deliver_avic_intr,
  4531. .update_pi_irte = svm_update_pi_irte,
  4532. .setup_mce = svm_setup_mce,
  4533. };
  4534. static int __init svm_init(void)
  4535. {
  4536. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  4537. __alignof__(struct vcpu_svm), THIS_MODULE);
  4538. }
  4539. static void __exit svm_exit(void)
  4540. {
  4541. kvm_exit();
  4542. }
  4543. module_init(svm_init)
  4544. module_exit(svm_exit)