svm.c 139 KB

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