svm.c 138 KB

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