mmu.c 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * MMU support
  8. *
  9. * Copyright (C) 2006 Qumranet, Inc.
  10. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  11. *
  12. * Authors:
  13. * Yaniv Kamay <yaniv@qumranet.com>
  14. * Avi Kivity <avi@qumranet.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. #include "irq.h"
  21. #include "mmu.h"
  22. #include "x86.h"
  23. #include "kvm_cache_regs.h"
  24. #include "cpuid.h"
  25. #include <linux/kvm_host.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/mm.h>
  29. #include <linux/highmem.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/export.h>
  32. #include <linux/swap.h>
  33. #include <linux/hugetlb.h>
  34. #include <linux/compiler.h>
  35. #include <linux/srcu.h>
  36. #include <linux/slab.h>
  37. #include <linux/sched/signal.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/hash.h>
  40. #include <linux/kern_levels.h>
  41. #include <asm/page.h>
  42. #include <asm/pat.h>
  43. #include <asm/cmpxchg.h>
  44. #include <asm/io.h>
  45. #include <asm/vmx.h>
  46. #include <asm/kvm_page_track.h>
  47. #include "trace.h"
  48. /*
  49. * When setting this variable to true it enables Two-Dimensional-Paging
  50. * where the hardware walks 2 page tables:
  51. * 1. the guest-virtual to guest-physical
  52. * 2. while doing 1. it walks guest-physical to host-physical
  53. * If the hardware supports that we don't need to do shadow paging.
  54. */
  55. bool tdp_enabled = false;
  56. enum {
  57. AUDIT_PRE_PAGE_FAULT,
  58. AUDIT_POST_PAGE_FAULT,
  59. AUDIT_PRE_PTE_WRITE,
  60. AUDIT_POST_PTE_WRITE,
  61. AUDIT_PRE_SYNC,
  62. AUDIT_POST_SYNC
  63. };
  64. #undef MMU_DEBUG
  65. #ifdef MMU_DEBUG
  66. static bool dbg = 0;
  67. module_param(dbg, bool, 0644);
  68. #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
  69. #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
  70. #define MMU_WARN_ON(x) WARN_ON(x)
  71. #else
  72. #define pgprintk(x...) do { } while (0)
  73. #define rmap_printk(x...) do { } while (0)
  74. #define MMU_WARN_ON(x) do { } while (0)
  75. #endif
  76. #define PTE_PREFETCH_NUM 8
  77. #define PT_FIRST_AVAIL_BITS_SHIFT 10
  78. #define PT64_SECOND_AVAIL_BITS_SHIFT 52
  79. #define PT64_LEVEL_BITS 9
  80. #define PT64_LEVEL_SHIFT(level) \
  81. (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
  82. #define PT64_INDEX(address, level)\
  83. (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
  84. #define PT32_LEVEL_BITS 10
  85. #define PT32_LEVEL_SHIFT(level) \
  86. (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
  87. #define PT32_LVL_OFFSET_MASK(level) \
  88. (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  89. * PT32_LEVEL_BITS))) - 1))
  90. #define PT32_INDEX(address, level)\
  91. (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
  92. #define PT64_BASE_ADDR_MASK __sme_clr((((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1)))
  93. #define PT64_DIR_BASE_ADDR_MASK \
  94. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
  95. #define PT64_LVL_ADDR_MASK(level) \
  96. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  97. * PT64_LEVEL_BITS))) - 1))
  98. #define PT64_LVL_OFFSET_MASK(level) \
  99. (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  100. * PT64_LEVEL_BITS))) - 1))
  101. #define PT32_BASE_ADDR_MASK PAGE_MASK
  102. #define PT32_DIR_BASE_ADDR_MASK \
  103. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
  104. #define PT32_LVL_ADDR_MASK(level) \
  105. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  106. * PT32_LEVEL_BITS))) - 1))
  107. #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | shadow_user_mask \
  108. | shadow_x_mask | shadow_nx_mask | shadow_me_mask)
  109. #define ACC_EXEC_MASK 1
  110. #define ACC_WRITE_MASK PT_WRITABLE_MASK
  111. #define ACC_USER_MASK PT_USER_MASK
  112. #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
  113. /* The mask for the R/X bits in EPT PTEs */
  114. #define PT64_EPT_READABLE_MASK 0x1ull
  115. #define PT64_EPT_EXECUTABLE_MASK 0x4ull
  116. #include <trace/events/kvm.h>
  117. #define CREATE_TRACE_POINTS
  118. #include "mmutrace.h"
  119. #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
  120. #define SPTE_MMU_WRITEABLE (1ULL << (PT_FIRST_AVAIL_BITS_SHIFT + 1))
  121. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  122. /* make pte_list_desc fit well in cache line */
  123. #define PTE_LIST_EXT 3
  124. /*
  125. * Return values of handle_mmio_page_fault and mmu.page_fault:
  126. * RET_PF_RETRY: let CPU fault again on the address.
  127. * RET_PF_EMULATE: mmio page fault, emulate the instruction directly.
  128. *
  129. * For handle_mmio_page_fault only:
  130. * RET_PF_INVALID: the spte is invalid, let the real page fault path update it.
  131. */
  132. enum {
  133. RET_PF_RETRY = 0,
  134. RET_PF_EMULATE = 1,
  135. RET_PF_INVALID = 2,
  136. };
  137. struct pte_list_desc {
  138. u64 *sptes[PTE_LIST_EXT];
  139. struct pte_list_desc *more;
  140. };
  141. struct kvm_shadow_walk_iterator {
  142. u64 addr;
  143. hpa_t shadow_addr;
  144. u64 *sptep;
  145. int level;
  146. unsigned index;
  147. };
  148. #define for_each_shadow_entry(_vcpu, _addr, _walker) \
  149. for (shadow_walk_init(&(_walker), _vcpu, _addr); \
  150. shadow_walk_okay(&(_walker)); \
  151. shadow_walk_next(&(_walker)))
  152. #define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
  153. for (shadow_walk_init(&(_walker), _vcpu, _addr); \
  154. shadow_walk_okay(&(_walker)) && \
  155. ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
  156. __shadow_walk_next(&(_walker), spte))
  157. static struct kmem_cache *pte_list_desc_cache;
  158. static struct kmem_cache *mmu_page_header_cache;
  159. static struct percpu_counter kvm_total_used_mmu_pages;
  160. static u64 __read_mostly shadow_nx_mask;
  161. static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
  162. static u64 __read_mostly shadow_user_mask;
  163. static u64 __read_mostly shadow_accessed_mask;
  164. static u64 __read_mostly shadow_dirty_mask;
  165. static u64 __read_mostly shadow_mmio_mask;
  166. static u64 __read_mostly shadow_mmio_value;
  167. static u64 __read_mostly shadow_present_mask;
  168. static u64 __read_mostly shadow_me_mask;
  169. /*
  170. * SPTEs used by MMUs without A/D bits are marked with shadow_acc_track_value.
  171. * Non-present SPTEs with shadow_acc_track_value set are in place for access
  172. * tracking.
  173. */
  174. static u64 __read_mostly shadow_acc_track_mask;
  175. static const u64 shadow_acc_track_value = SPTE_SPECIAL_MASK;
  176. /*
  177. * The mask/shift to use for saving the original R/X bits when marking the PTE
  178. * as not-present for access tracking purposes. We do not save the W bit as the
  179. * PTEs being access tracked also need to be dirty tracked, so the W bit will be
  180. * restored only when a write is attempted to the page.
  181. */
  182. static const u64 shadow_acc_track_saved_bits_mask = PT64_EPT_READABLE_MASK |
  183. PT64_EPT_EXECUTABLE_MASK;
  184. static const u64 shadow_acc_track_saved_bits_shift = PT64_SECOND_AVAIL_BITS_SHIFT;
  185. static void mmu_spte_set(u64 *sptep, u64 spte);
  186. void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask, u64 mmio_value)
  187. {
  188. BUG_ON((mmio_mask & mmio_value) != mmio_value);
  189. shadow_mmio_value = mmio_value | SPTE_SPECIAL_MASK;
  190. shadow_mmio_mask = mmio_mask | SPTE_SPECIAL_MASK;
  191. }
  192. EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
  193. static inline bool sp_ad_disabled(struct kvm_mmu_page *sp)
  194. {
  195. return sp->role.ad_disabled;
  196. }
  197. static inline bool spte_ad_enabled(u64 spte)
  198. {
  199. MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
  200. return !(spte & shadow_acc_track_value);
  201. }
  202. static inline u64 spte_shadow_accessed_mask(u64 spte)
  203. {
  204. MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
  205. return spte_ad_enabled(spte) ? shadow_accessed_mask : 0;
  206. }
  207. static inline u64 spte_shadow_dirty_mask(u64 spte)
  208. {
  209. MMU_WARN_ON((spte & shadow_mmio_mask) == shadow_mmio_value);
  210. return spte_ad_enabled(spte) ? shadow_dirty_mask : 0;
  211. }
  212. static inline bool is_access_track_spte(u64 spte)
  213. {
  214. return !spte_ad_enabled(spte) && (spte & shadow_acc_track_mask) == 0;
  215. }
  216. /*
  217. * the low bit of the generation number is always presumed to be zero.
  218. * This disables mmio caching during memslot updates. The concept is
  219. * similar to a seqcount but instead of retrying the access we just punt
  220. * and ignore the cache.
  221. *
  222. * spte bits 3-11 are used as bits 1-9 of the generation number,
  223. * the bits 52-61 are used as bits 10-19 of the generation number.
  224. */
  225. #define MMIO_SPTE_GEN_LOW_SHIFT 2
  226. #define MMIO_SPTE_GEN_HIGH_SHIFT 52
  227. #define MMIO_GEN_SHIFT 20
  228. #define MMIO_GEN_LOW_SHIFT 10
  229. #define MMIO_GEN_LOW_MASK ((1 << MMIO_GEN_LOW_SHIFT) - 2)
  230. #define MMIO_GEN_MASK ((1 << MMIO_GEN_SHIFT) - 1)
  231. static u64 generation_mmio_spte_mask(unsigned int gen)
  232. {
  233. u64 mask;
  234. WARN_ON(gen & ~MMIO_GEN_MASK);
  235. mask = (gen & MMIO_GEN_LOW_MASK) << MMIO_SPTE_GEN_LOW_SHIFT;
  236. mask |= ((u64)gen >> MMIO_GEN_LOW_SHIFT) << MMIO_SPTE_GEN_HIGH_SHIFT;
  237. return mask;
  238. }
  239. static unsigned int get_mmio_spte_generation(u64 spte)
  240. {
  241. unsigned int gen;
  242. spte &= ~shadow_mmio_mask;
  243. gen = (spte >> MMIO_SPTE_GEN_LOW_SHIFT) & MMIO_GEN_LOW_MASK;
  244. gen |= (spte >> MMIO_SPTE_GEN_HIGH_SHIFT) << MMIO_GEN_LOW_SHIFT;
  245. return gen;
  246. }
  247. static unsigned int kvm_current_mmio_generation(struct kvm_vcpu *vcpu)
  248. {
  249. return kvm_vcpu_memslots(vcpu)->generation & MMIO_GEN_MASK;
  250. }
  251. static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn,
  252. unsigned access)
  253. {
  254. unsigned int gen = kvm_current_mmio_generation(vcpu);
  255. u64 mask = generation_mmio_spte_mask(gen);
  256. access &= ACC_WRITE_MASK | ACC_USER_MASK;
  257. mask |= shadow_mmio_value | access | gfn << PAGE_SHIFT;
  258. trace_mark_mmio_spte(sptep, gfn, access, gen);
  259. mmu_spte_set(sptep, mask);
  260. }
  261. static bool is_mmio_spte(u64 spte)
  262. {
  263. return (spte & shadow_mmio_mask) == shadow_mmio_value;
  264. }
  265. static gfn_t get_mmio_spte_gfn(u64 spte)
  266. {
  267. u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask;
  268. return (spte & ~mask) >> PAGE_SHIFT;
  269. }
  270. static unsigned get_mmio_spte_access(u64 spte)
  271. {
  272. u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask;
  273. return (spte & ~mask) & ~PAGE_MASK;
  274. }
  275. static bool set_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
  276. kvm_pfn_t pfn, unsigned access)
  277. {
  278. if (unlikely(is_noslot_pfn(pfn))) {
  279. mark_mmio_spte(vcpu, sptep, gfn, access);
  280. return true;
  281. }
  282. return false;
  283. }
  284. static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)
  285. {
  286. unsigned int kvm_gen, spte_gen;
  287. kvm_gen = kvm_current_mmio_generation(vcpu);
  288. spte_gen = get_mmio_spte_generation(spte);
  289. trace_check_mmio_spte(spte, kvm_gen, spte_gen);
  290. return likely(kvm_gen == spte_gen);
  291. }
  292. /*
  293. * Sets the shadow PTE masks used by the MMU.
  294. *
  295. * Assumptions:
  296. * - Setting either @accessed_mask or @dirty_mask requires setting both
  297. * - At least one of @accessed_mask or @acc_track_mask must be set
  298. */
  299. void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
  300. u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 p_mask,
  301. u64 acc_track_mask, u64 me_mask)
  302. {
  303. BUG_ON(!dirty_mask != !accessed_mask);
  304. BUG_ON(!accessed_mask && !acc_track_mask);
  305. BUG_ON(acc_track_mask & shadow_acc_track_value);
  306. shadow_user_mask = user_mask;
  307. shadow_accessed_mask = accessed_mask;
  308. shadow_dirty_mask = dirty_mask;
  309. shadow_nx_mask = nx_mask;
  310. shadow_x_mask = x_mask;
  311. shadow_present_mask = p_mask;
  312. shadow_acc_track_mask = acc_track_mask;
  313. shadow_me_mask = me_mask;
  314. }
  315. EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
  316. static void kvm_mmu_clear_all_pte_masks(void)
  317. {
  318. shadow_user_mask = 0;
  319. shadow_accessed_mask = 0;
  320. shadow_dirty_mask = 0;
  321. shadow_nx_mask = 0;
  322. shadow_x_mask = 0;
  323. shadow_mmio_mask = 0;
  324. shadow_present_mask = 0;
  325. shadow_acc_track_mask = 0;
  326. }
  327. static int is_cpuid_PSE36(void)
  328. {
  329. return 1;
  330. }
  331. static int is_nx(struct kvm_vcpu *vcpu)
  332. {
  333. return vcpu->arch.efer & EFER_NX;
  334. }
  335. static int is_shadow_present_pte(u64 pte)
  336. {
  337. return (pte != 0) && !is_mmio_spte(pte);
  338. }
  339. static int is_large_pte(u64 pte)
  340. {
  341. return pte & PT_PAGE_SIZE_MASK;
  342. }
  343. static int is_last_spte(u64 pte, int level)
  344. {
  345. if (level == PT_PAGE_TABLE_LEVEL)
  346. return 1;
  347. if (is_large_pte(pte))
  348. return 1;
  349. return 0;
  350. }
  351. static bool is_executable_pte(u64 spte)
  352. {
  353. return (spte & (shadow_x_mask | shadow_nx_mask)) == shadow_x_mask;
  354. }
  355. static kvm_pfn_t spte_to_pfn(u64 pte)
  356. {
  357. return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
  358. }
  359. static gfn_t pse36_gfn_delta(u32 gpte)
  360. {
  361. int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
  362. return (gpte & PT32_DIR_PSE36_MASK) << shift;
  363. }
  364. #ifdef CONFIG_X86_64
  365. static void __set_spte(u64 *sptep, u64 spte)
  366. {
  367. WRITE_ONCE(*sptep, spte);
  368. }
  369. static void __update_clear_spte_fast(u64 *sptep, u64 spte)
  370. {
  371. WRITE_ONCE(*sptep, spte);
  372. }
  373. static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
  374. {
  375. return xchg(sptep, spte);
  376. }
  377. static u64 __get_spte_lockless(u64 *sptep)
  378. {
  379. return READ_ONCE(*sptep);
  380. }
  381. #else
  382. union split_spte {
  383. struct {
  384. u32 spte_low;
  385. u32 spte_high;
  386. };
  387. u64 spte;
  388. };
  389. static void count_spte_clear(u64 *sptep, u64 spte)
  390. {
  391. struct kvm_mmu_page *sp = page_header(__pa(sptep));
  392. if (is_shadow_present_pte(spte))
  393. return;
  394. /* Ensure the spte is completely set before we increase the count */
  395. smp_wmb();
  396. sp->clear_spte_count++;
  397. }
  398. static void __set_spte(u64 *sptep, u64 spte)
  399. {
  400. union split_spte *ssptep, sspte;
  401. ssptep = (union split_spte *)sptep;
  402. sspte = (union split_spte)spte;
  403. ssptep->spte_high = sspte.spte_high;
  404. /*
  405. * If we map the spte from nonpresent to present, We should store
  406. * the high bits firstly, then set present bit, so cpu can not
  407. * fetch this spte while we are setting the spte.
  408. */
  409. smp_wmb();
  410. WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
  411. }
  412. static void __update_clear_spte_fast(u64 *sptep, u64 spte)
  413. {
  414. union split_spte *ssptep, sspte;
  415. ssptep = (union split_spte *)sptep;
  416. sspte = (union split_spte)spte;
  417. WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
  418. /*
  419. * If we map the spte from present to nonpresent, we should clear
  420. * present bit firstly to avoid vcpu fetch the old high bits.
  421. */
  422. smp_wmb();
  423. ssptep->spte_high = sspte.spte_high;
  424. count_spte_clear(sptep, spte);
  425. }
  426. static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
  427. {
  428. union split_spte *ssptep, sspte, orig;
  429. ssptep = (union split_spte *)sptep;
  430. sspte = (union split_spte)spte;
  431. /* xchg acts as a barrier before the setting of the high bits */
  432. orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
  433. orig.spte_high = ssptep->spte_high;
  434. ssptep->spte_high = sspte.spte_high;
  435. count_spte_clear(sptep, spte);
  436. return orig.spte;
  437. }
  438. /*
  439. * The idea using the light way get the spte on x86_32 guest is from
  440. * gup_get_pte(arch/x86/mm/gup.c).
  441. *
  442. * An spte tlb flush may be pending, because kvm_set_pte_rmapp
  443. * coalesces them and we are running out of the MMU lock. Therefore
  444. * we need to protect against in-progress updates of the spte.
  445. *
  446. * Reading the spte while an update is in progress may get the old value
  447. * for the high part of the spte. The race is fine for a present->non-present
  448. * change (because the high part of the spte is ignored for non-present spte),
  449. * but for a present->present change we must reread the spte.
  450. *
  451. * All such changes are done in two steps (present->non-present and
  452. * non-present->present), hence it is enough to count the number of
  453. * present->non-present updates: if it changed while reading the spte,
  454. * we might have hit the race. This is done using clear_spte_count.
  455. */
  456. static u64 __get_spte_lockless(u64 *sptep)
  457. {
  458. struct kvm_mmu_page *sp = page_header(__pa(sptep));
  459. union split_spte spte, *orig = (union split_spte *)sptep;
  460. int count;
  461. retry:
  462. count = sp->clear_spte_count;
  463. smp_rmb();
  464. spte.spte_low = orig->spte_low;
  465. smp_rmb();
  466. spte.spte_high = orig->spte_high;
  467. smp_rmb();
  468. if (unlikely(spte.spte_low != orig->spte_low ||
  469. count != sp->clear_spte_count))
  470. goto retry;
  471. return spte.spte;
  472. }
  473. #endif
  474. static bool spte_can_locklessly_be_made_writable(u64 spte)
  475. {
  476. return (spte & (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE)) ==
  477. (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE);
  478. }
  479. static bool spte_has_volatile_bits(u64 spte)
  480. {
  481. if (!is_shadow_present_pte(spte))
  482. return false;
  483. /*
  484. * Always atomically update spte if it can be updated
  485. * out of mmu-lock, it can ensure dirty bit is not lost,
  486. * also, it can help us to get a stable is_writable_pte()
  487. * to ensure tlb flush is not missed.
  488. */
  489. if (spte_can_locklessly_be_made_writable(spte) ||
  490. is_access_track_spte(spte))
  491. return true;
  492. if (spte_ad_enabled(spte)) {
  493. if ((spte & shadow_accessed_mask) == 0 ||
  494. (is_writable_pte(spte) && (spte & shadow_dirty_mask) == 0))
  495. return true;
  496. }
  497. return false;
  498. }
  499. static bool is_accessed_spte(u64 spte)
  500. {
  501. u64 accessed_mask = spte_shadow_accessed_mask(spte);
  502. return accessed_mask ? spte & accessed_mask
  503. : !is_access_track_spte(spte);
  504. }
  505. static bool is_dirty_spte(u64 spte)
  506. {
  507. u64 dirty_mask = spte_shadow_dirty_mask(spte);
  508. return dirty_mask ? spte & dirty_mask : spte & PT_WRITABLE_MASK;
  509. }
  510. /* Rules for using mmu_spte_set:
  511. * Set the sptep from nonpresent to present.
  512. * Note: the sptep being assigned *must* be either not present
  513. * or in a state where the hardware will not attempt to update
  514. * the spte.
  515. */
  516. static void mmu_spte_set(u64 *sptep, u64 new_spte)
  517. {
  518. WARN_ON(is_shadow_present_pte(*sptep));
  519. __set_spte(sptep, new_spte);
  520. }
  521. /*
  522. * Update the SPTE (excluding the PFN), but do not track changes in its
  523. * accessed/dirty status.
  524. */
  525. static u64 mmu_spte_update_no_track(u64 *sptep, u64 new_spte)
  526. {
  527. u64 old_spte = *sptep;
  528. WARN_ON(!is_shadow_present_pte(new_spte));
  529. if (!is_shadow_present_pte(old_spte)) {
  530. mmu_spte_set(sptep, new_spte);
  531. return old_spte;
  532. }
  533. if (!spte_has_volatile_bits(old_spte))
  534. __update_clear_spte_fast(sptep, new_spte);
  535. else
  536. old_spte = __update_clear_spte_slow(sptep, new_spte);
  537. WARN_ON(spte_to_pfn(old_spte) != spte_to_pfn(new_spte));
  538. return old_spte;
  539. }
  540. /* Rules for using mmu_spte_update:
  541. * Update the state bits, it means the mapped pfn is not changed.
  542. *
  543. * Whenever we overwrite a writable spte with a read-only one we
  544. * should flush remote TLBs. Otherwise rmap_write_protect
  545. * will find a read-only spte, even though the writable spte
  546. * might be cached on a CPU's TLB, the return value indicates this
  547. * case.
  548. *
  549. * Returns true if the TLB needs to be flushed
  550. */
  551. static bool mmu_spte_update(u64 *sptep, u64 new_spte)
  552. {
  553. bool flush = false;
  554. u64 old_spte = mmu_spte_update_no_track(sptep, new_spte);
  555. if (!is_shadow_present_pte(old_spte))
  556. return false;
  557. /*
  558. * For the spte updated out of mmu-lock is safe, since
  559. * we always atomically update it, see the comments in
  560. * spte_has_volatile_bits().
  561. */
  562. if (spte_can_locklessly_be_made_writable(old_spte) &&
  563. !is_writable_pte(new_spte))
  564. flush = true;
  565. /*
  566. * Flush TLB when accessed/dirty states are changed in the page tables,
  567. * to guarantee consistency between TLB and page tables.
  568. */
  569. if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) {
  570. flush = true;
  571. kvm_set_pfn_accessed(spte_to_pfn(old_spte));
  572. }
  573. if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) {
  574. flush = true;
  575. kvm_set_pfn_dirty(spte_to_pfn(old_spte));
  576. }
  577. return flush;
  578. }
  579. /*
  580. * Rules for using mmu_spte_clear_track_bits:
  581. * It sets the sptep from present to nonpresent, and track the
  582. * state bits, it is used to clear the last level sptep.
  583. * Returns non-zero if the PTE was previously valid.
  584. */
  585. static int mmu_spte_clear_track_bits(u64 *sptep)
  586. {
  587. kvm_pfn_t pfn;
  588. u64 old_spte = *sptep;
  589. if (!spte_has_volatile_bits(old_spte))
  590. __update_clear_spte_fast(sptep, 0ull);
  591. else
  592. old_spte = __update_clear_spte_slow(sptep, 0ull);
  593. if (!is_shadow_present_pte(old_spte))
  594. return 0;
  595. pfn = spte_to_pfn(old_spte);
  596. /*
  597. * KVM does not hold the refcount of the page used by
  598. * kvm mmu, before reclaiming the page, we should
  599. * unmap it from mmu first.
  600. */
  601. WARN_ON(!kvm_is_reserved_pfn(pfn) && !page_count(pfn_to_page(pfn)));
  602. if (is_accessed_spte(old_spte))
  603. kvm_set_pfn_accessed(pfn);
  604. if (is_dirty_spte(old_spte))
  605. kvm_set_pfn_dirty(pfn);
  606. return 1;
  607. }
  608. /*
  609. * Rules for using mmu_spte_clear_no_track:
  610. * Directly clear spte without caring the state bits of sptep,
  611. * it is used to set the upper level spte.
  612. */
  613. static void mmu_spte_clear_no_track(u64 *sptep)
  614. {
  615. __update_clear_spte_fast(sptep, 0ull);
  616. }
  617. static u64 mmu_spte_get_lockless(u64 *sptep)
  618. {
  619. return __get_spte_lockless(sptep);
  620. }
  621. static u64 mark_spte_for_access_track(u64 spte)
  622. {
  623. if (spte_ad_enabled(spte))
  624. return spte & ~shadow_accessed_mask;
  625. if (is_access_track_spte(spte))
  626. return spte;
  627. /*
  628. * Making an Access Tracking PTE will result in removal of write access
  629. * from the PTE. So, verify that we will be able to restore the write
  630. * access in the fast page fault path later on.
  631. */
  632. WARN_ONCE((spte & PT_WRITABLE_MASK) &&
  633. !spte_can_locklessly_be_made_writable(spte),
  634. "kvm: Writable SPTE is not locklessly dirty-trackable\n");
  635. WARN_ONCE(spte & (shadow_acc_track_saved_bits_mask <<
  636. shadow_acc_track_saved_bits_shift),
  637. "kvm: Access Tracking saved bit locations are not zero\n");
  638. spte |= (spte & shadow_acc_track_saved_bits_mask) <<
  639. shadow_acc_track_saved_bits_shift;
  640. spte &= ~shadow_acc_track_mask;
  641. return spte;
  642. }
  643. /* Restore an acc-track PTE back to a regular PTE */
  644. static u64 restore_acc_track_spte(u64 spte)
  645. {
  646. u64 new_spte = spte;
  647. u64 saved_bits = (spte >> shadow_acc_track_saved_bits_shift)
  648. & shadow_acc_track_saved_bits_mask;
  649. WARN_ON_ONCE(spte_ad_enabled(spte));
  650. WARN_ON_ONCE(!is_access_track_spte(spte));
  651. new_spte &= ~shadow_acc_track_mask;
  652. new_spte &= ~(shadow_acc_track_saved_bits_mask <<
  653. shadow_acc_track_saved_bits_shift);
  654. new_spte |= saved_bits;
  655. return new_spte;
  656. }
  657. /* Returns the Accessed status of the PTE and resets it at the same time. */
  658. static bool mmu_spte_age(u64 *sptep)
  659. {
  660. u64 spte = mmu_spte_get_lockless(sptep);
  661. if (!is_accessed_spte(spte))
  662. return false;
  663. if (spte_ad_enabled(spte)) {
  664. clear_bit((ffs(shadow_accessed_mask) - 1),
  665. (unsigned long *)sptep);
  666. } else {
  667. /*
  668. * Capture the dirty status of the page, so that it doesn't get
  669. * lost when the SPTE is marked for access tracking.
  670. */
  671. if (is_writable_pte(spte))
  672. kvm_set_pfn_dirty(spte_to_pfn(spte));
  673. spte = mark_spte_for_access_track(spte);
  674. mmu_spte_update_no_track(sptep, spte);
  675. }
  676. return true;
  677. }
  678. static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
  679. {
  680. /*
  681. * Prevent page table teardown by making any free-er wait during
  682. * kvm_flush_remote_tlbs() IPI to all active vcpus.
  683. */
  684. local_irq_disable();
  685. /*
  686. * Make sure a following spte read is not reordered ahead of the write
  687. * to vcpu->mode.
  688. */
  689. smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES);
  690. }
  691. static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
  692. {
  693. /*
  694. * Make sure the write to vcpu->mode is not reordered in front of
  695. * reads to sptes. If it does, kvm_commit_zap_page() can see us
  696. * OUTSIDE_GUEST_MODE and proceed to free the shadow page table.
  697. */
  698. smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE);
  699. local_irq_enable();
  700. }
  701. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  702. struct kmem_cache *base_cache, int min)
  703. {
  704. void *obj;
  705. if (cache->nobjs >= min)
  706. return 0;
  707. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  708. obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
  709. if (!obj)
  710. return -ENOMEM;
  711. cache->objects[cache->nobjs++] = obj;
  712. }
  713. return 0;
  714. }
  715. static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache)
  716. {
  717. return cache->nobjs;
  718. }
  719. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
  720. struct kmem_cache *cache)
  721. {
  722. while (mc->nobjs)
  723. kmem_cache_free(cache, mc->objects[--mc->nobjs]);
  724. }
  725. static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
  726. int min)
  727. {
  728. void *page;
  729. if (cache->nobjs >= min)
  730. return 0;
  731. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  732. page = (void *)__get_free_page(GFP_KERNEL);
  733. if (!page)
  734. return -ENOMEM;
  735. cache->objects[cache->nobjs++] = page;
  736. }
  737. return 0;
  738. }
  739. static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
  740. {
  741. while (mc->nobjs)
  742. free_page((unsigned long)mc->objects[--mc->nobjs]);
  743. }
  744. static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
  745. {
  746. int r;
  747. r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
  748. pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
  749. if (r)
  750. goto out;
  751. r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
  752. if (r)
  753. goto out;
  754. r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
  755. mmu_page_header_cache, 4);
  756. out:
  757. return r;
  758. }
  759. static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  760. {
  761. mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
  762. pte_list_desc_cache);
  763. mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
  764. mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
  765. mmu_page_header_cache);
  766. }
  767. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  768. {
  769. void *p;
  770. BUG_ON(!mc->nobjs);
  771. p = mc->objects[--mc->nobjs];
  772. return p;
  773. }
  774. static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
  775. {
  776. return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache);
  777. }
  778. static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
  779. {
  780. kmem_cache_free(pte_list_desc_cache, pte_list_desc);
  781. }
  782. static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
  783. {
  784. if (!sp->role.direct)
  785. return sp->gfns[index];
  786. return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
  787. }
  788. static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
  789. {
  790. if (sp->role.direct)
  791. BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
  792. else
  793. sp->gfns[index] = gfn;
  794. }
  795. /*
  796. * Return the pointer to the large page information for a given gfn,
  797. * handling slots that are not large page aligned.
  798. */
  799. static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
  800. struct kvm_memory_slot *slot,
  801. int level)
  802. {
  803. unsigned long idx;
  804. idx = gfn_to_index(gfn, slot->base_gfn, level);
  805. return &slot->arch.lpage_info[level - 2][idx];
  806. }
  807. static void update_gfn_disallow_lpage_count(struct kvm_memory_slot *slot,
  808. gfn_t gfn, int count)
  809. {
  810. struct kvm_lpage_info *linfo;
  811. int i;
  812. for (i = PT_DIRECTORY_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
  813. linfo = lpage_info_slot(gfn, slot, i);
  814. linfo->disallow_lpage += count;
  815. WARN_ON(linfo->disallow_lpage < 0);
  816. }
  817. }
  818. void kvm_mmu_gfn_disallow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
  819. {
  820. update_gfn_disallow_lpage_count(slot, gfn, 1);
  821. }
  822. void kvm_mmu_gfn_allow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
  823. {
  824. update_gfn_disallow_lpage_count(slot, gfn, -1);
  825. }
  826. static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
  827. {
  828. struct kvm_memslots *slots;
  829. struct kvm_memory_slot *slot;
  830. gfn_t gfn;
  831. kvm->arch.indirect_shadow_pages++;
  832. gfn = sp->gfn;
  833. slots = kvm_memslots_for_spte_role(kvm, sp->role);
  834. slot = __gfn_to_memslot(slots, gfn);
  835. /* the non-leaf shadow pages are keeping readonly. */
  836. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  837. return kvm_slot_page_track_add_page(kvm, slot, gfn,
  838. KVM_PAGE_TRACK_WRITE);
  839. kvm_mmu_gfn_disallow_lpage(slot, gfn);
  840. }
  841. static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
  842. {
  843. struct kvm_memslots *slots;
  844. struct kvm_memory_slot *slot;
  845. gfn_t gfn;
  846. kvm->arch.indirect_shadow_pages--;
  847. gfn = sp->gfn;
  848. slots = kvm_memslots_for_spte_role(kvm, sp->role);
  849. slot = __gfn_to_memslot(slots, gfn);
  850. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  851. return kvm_slot_page_track_remove_page(kvm, slot, gfn,
  852. KVM_PAGE_TRACK_WRITE);
  853. kvm_mmu_gfn_allow_lpage(slot, gfn);
  854. }
  855. static bool __mmu_gfn_lpage_is_disallowed(gfn_t gfn, int level,
  856. struct kvm_memory_slot *slot)
  857. {
  858. struct kvm_lpage_info *linfo;
  859. if (slot) {
  860. linfo = lpage_info_slot(gfn, slot, level);
  861. return !!linfo->disallow_lpage;
  862. }
  863. return true;
  864. }
  865. static bool mmu_gfn_lpage_is_disallowed(struct kvm_vcpu *vcpu, gfn_t gfn,
  866. int level)
  867. {
  868. struct kvm_memory_slot *slot;
  869. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  870. return __mmu_gfn_lpage_is_disallowed(gfn, level, slot);
  871. }
  872. static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
  873. {
  874. unsigned long page_size;
  875. int i, ret = 0;
  876. page_size = kvm_host_page_size(kvm, gfn);
  877. for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
  878. if (page_size >= KVM_HPAGE_SIZE(i))
  879. ret = i;
  880. else
  881. break;
  882. }
  883. return ret;
  884. }
  885. static inline bool memslot_valid_for_gpte(struct kvm_memory_slot *slot,
  886. bool no_dirty_log)
  887. {
  888. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  889. return false;
  890. if (no_dirty_log && slot->dirty_bitmap)
  891. return false;
  892. return true;
  893. }
  894. static struct kvm_memory_slot *
  895. gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
  896. bool no_dirty_log)
  897. {
  898. struct kvm_memory_slot *slot;
  899. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  900. if (!memslot_valid_for_gpte(slot, no_dirty_log))
  901. slot = NULL;
  902. return slot;
  903. }
  904. static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn,
  905. bool *force_pt_level)
  906. {
  907. int host_level, level, max_level;
  908. struct kvm_memory_slot *slot;
  909. if (unlikely(*force_pt_level))
  910. return PT_PAGE_TABLE_LEVEL;
  911. slot = kvm_vcpu_gfn_to_memslot(vcpu, large_gfn);
  912. *force_pt_level = !memslot_valid_for_gpte(slot, true);
  913. if (unlikely(*force_pt_level))
  914. return PT_PAGE_TABLE_LEVEL;
  915. host_level = host_mapping_level(vcpu->kvm, large_gfn);
  916. if (host_level == PT_PAGE_TABLE_LEVEL)
  917. return host_level;
  918. max_level = min(kvm_x86_ops->get_lpage_level(), host_level);
  919. for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
  920. if (__mmu_gfn_lpage_is_disallowed(large_gfn, level, slot))
  921. break;
  922. return level - 1;
  923. }
  924. /*
  925. * About rmap_head encoding:
  926. *
  927. * If the bit zero of rmap_head->val is clear, then it points to the only spte
  928. * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct
  929. * pte_list_desc containing more mappings.
  930. */
  931. /*
  932. * Returns the number of pointers in the rmap chain, not counting the new one.
  933. */
  934. static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
  935. struct kvm_rmap_head *rmap_head)
  936. {
  937. struct pte_list_desc *desc;
  938. int i, count = 0;
  939. if (!rmap_head->val) {
  940. rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
  941. rmap_head->val = (unsigned long)spte;
  942. } else if (!(rmap_head->val & 1)) {
  943. rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
  944. desc = mmu_alloc_pte_list_desc(vcpu);
  945. desc->sptes[0] = (u64 *)rmap_head->val;
  946. desc->sptes[1] = spte;
  947. rmap_head->val = (unsigned long)desc | 1;
  948. ++count;
  949. } else {
  950. rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
  951. desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
  952. while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
  953. desc = desc->more;
  954. count += PTE_LIST_EXT;
  955. }
  956. if (desc->sptes[PTE_LIST_EXT-1]) {
  957. desc->more = mmu_alloc_pte_list_desc(vcpu);
  958. desc = desc->more;
  959. }
  960. for (i = 0; desc->sptes[i]; ++i)
  961. ++count;
  962. desc->sptes[i] = spte;
  963. }
  964. return count;
  965. }
  966. static void
  967. pte_list_desc_remove_entry(struct kvm_rmap_head *rmap_head,
  968. struct pte_list_desc *desc, int i,
  969. struct pte_list_desc *prev_desc)
  970. {
  971. int j;
  972. for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
  973. ;
  974. desc->sptes[i] = desc->sptes[j];
  975. desc->sptes[j] = NULL;
  976. if (j != 0)
  977. return;
  978. if (!prev_desc && !desc->more)
  979. rmap_head->val = (unsigned long)desc->sptes[0];
  980. else
  981. if (prev_desc)
  982. prev_desc->more = desc->more;
  983. else
  984. rmap_head->val = (unsigned long)desc->more | 1;
  985. mmu_free_pte_list_desc(desc);
  986. }
  987. static void pte_list_remove(u64 *spte, struct kvm_rmap_head *rmap_head)
  988. {
  989. struct pte_list_desc *desc;
  990. struct pte_list_desc *prev_desc;
  991. int i;
  992. if (!rmap_head->val) {
  993. printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
  994. BUG();
  995. } else if (!(rmap_head->val & 1)) {
  996. rmap_printk("pte_list_remove: %p 1->0\n", spte);
  997. if ((u64 *)rmap_head->val != spte) {
  998. printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
  999. BUG();
  1000. }
  1001. rmap_head->val = 0;
  1002. } else {
  1003. rmap_printk("pte_list_remove: %p many->many\n", spte);
  1004. desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
  1005. prev_desc = NULL;
  1006. while (desc) {
  1007. for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
  1008. if (desc->sptes[i] == spte) {
  1009. pte_list_desc_remove_entry(rmap_head,
  1010. desc, i, prev_desc);
  1011. return;
  1012. }
  1013. }
  1014. prev_desc = desc;
  1015. desc = desc->more;
  1016. }
  1017. pr_err("pte_list_remove: %p many->many\n", spte);
  1018. BUG();
  1019. }
  1020. }
  1021. static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level,
  1022. struct kvm_memory_slot *slot)
  1023. {
  1024. unsigned long idx;
  1025. idx = gfn_to_index(gfn, slot->base_gfn, level);
  1026. return &slot->arch.rmap[level - PT_PAGE_TABLE_LEVEL][idx];
  1027. }
  1028. static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn,
  1029. struct kvm_mmu_page *sp)
  1030. {
  1031. struct kvm_memslots *slots;
  1032. struct kvm_memory_slot *slot;
  1033. slots = kvm_memslots_for_spte_role(kvm, sp->role);
  1034. slot = __gfn_to_memslot(slots, gfn);
  1035. return __gfn_to_rmap(gfn, sp->role.level, slot);
  1036. }
  1037. static bool rmap_can_add(struct kvm_vcpu *vcpu)
  1038. {
  1039. struct kvm_mmu_memory_cache *cache;
  1040. cache = &vcpu->arch.mmu_pte_list_desc_cache;
  1041. return mmu_memory_cache_free_objects(cache);
  1042. }
  1043. static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  1044. {
  1045. struct kvm_mmu_page *sp;
  1046. struct kvm_rmap_head *rmap_head;
  1047. sp = page_header(__pa(spte));
  1048. kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
  1049. rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
  1050. return pte_list_add(vcpu, spte, rmap_head);
  1051. }
  1052. static void rmap_remove(struct kvm *kvm, u64 *spte)
  1053. {
  1054. struct kvm_mmu_page *sp;
  1055. gfn_t gfn;
  1056. struct kvm_rmap_head *rmap_head;
  1057. sp = page_header(__pa(spte));
  1058. gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
  1059. rmap_head = gfn_to_rmap(kvm, gfn, sp);
  1060. pte_list_remove(spte, rmap_head);
  1061. }
  1062. /*
  1063. * Used by the following functions to iterate through the sptes linked by a
  1064. * rmap. All fields are private and not assumed to be used outside.
  1065. */
  1066. struct rmap_iterator {
  1067. /* private fields */
  1068. struct pte_list_desc *desc; /* holds the sptep if not NULL */
  1069. int pos; /* index of the sptep */
  1070. };
  1071. /*
  1072. * Iteration must be started by this function. This should also be used after
  1073. * removing/dropping sptes from the rmap link because in such cases the
  1074. * information in the itererator may not be valid.
  1075. *
  1076. * Returns sptep if found, NULL otherwise.
  1077. */
  1078. static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head,
  1079. struct rmap_iterator *iter)
  1080. {
  1081. u64 *sptep;
  1082. if (!rmap_head->val)
  1083. return NULL;
  1084. if (!(rmap_head->val & 1)) {
  1085. iter->desc = NULL;
  1086. sptep = (u64 *)rmap_head->val;
  1087. goto out;
  1088. }
  1089. iter->desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
  1090. iter->pos = 0;
  1091. sptep = iter->desc->sptes[iter->pos];
  1092. out:
  1093. BUG_ON(!is_shadow_present_pte(*sptep));
  1094. return sptep;
  1095. }
  1096. /*
  1097. * Must be used with a valid iterator: e.g. after rmap_get_first().
  1098. *
  1099. * Returns sptep if found, NULL otherwise.
  1100. */
  1101. static u64 *rmap_get_next(struct rmap_iterator *iter)
  1102. {
  1103. u64 *sptep;
  1104. if (iter->desc) {
  1105. if (iter->pos < PTE_LIST_EXT - 1) {
  1106. ++iter->pos;
  1107. sptep = iter->desc->sptes[iter->pos];
  1108. if (sptep)
  1109. goto out;
  1110. }
  1111. iter->desc = iter->desc->more;
  1112. if (iter->desc) {
  1113. iter->pos = 0;
  1114. /* desc->sptes[0] cannot be NULL */
  1115. sptep = iter->desc->sptes[iter->pos];
  1116. goto out;
  1117. }
  1118. }
  1119. return NULL;
  1120. out:
  1121. BUG_ON(!is_shadow_present_pte(*sptep));
  1122. return sptep;
  1123. }
  1124. #define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \
  1125. for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \
  1126. _spte_; _spte_ = rmap_get_next(_iter_))
  1127. static void drop_spte(struct kvm *kvm, u64 *sptep)
  1128. {
  1129. if (mmu_spte_clear_track_bits(sptep))
  1130. rmap_remove(kvm, sptep);
  1131. }
  1132. static bool __drop_large_spte(struct kvm *kvm, u64 *sptep)
  1133. {
  1134. if (is_large_pte(*sptep)) {
  1135. WARN_ON(page_header(__pa(sptep))->role.level ==
  1136. PT_PAGE_TABLE_LEVEL);
  1137. drop_spte(kvm, sptep);
  1138. --kvm->stat.lpages;
  1139. return true;
  1140. }
  1141. return false;
  1142. }
  1143. static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
  1144. {
  1145. if (__drop_large_spte(vcpu->kvm, sptep))
  1146. kvm_flush_remote_tlbs(vcpu->kvm);
  1147. }
  1148. /*
  1149. * Write-protect on the specified @sptep, @pt_protect indicates whether
  1150. * spte write-protection is caused by protecting shadow page table.
  1151. *
  1152. * Note: write protection is difference between dirty logging and spte
  1153. * protection:
  1154. * - for dirty logging, the spte can be set to writable at anytime if
  1155. * its dirty bitmap is properly set.
  1156. * - for spte protection, the spte can be writable only after unsync-ing
  1157. * shadow page.
  1158. *
  1159. * Return true if tlb need be flushed.
  1160. */
  1161. static bool spte_write_protect(u64 *sptep, bool pt_protect)
  1162. {
  1163. u64 spte = *sptep;
  1164. if (!is_writable_pte(spte) &&
  1165. !(pt_protect && spte_can_locklessly_be_made_writable(spte)))
  1166. return false;
  1167. rmap_printk("rmap_write_protect: spte %p %llx\n", sptep, *sptep);
  1168. if (pt_protect)
  1169. spte &= ~SPTE_MMU_WRITEABLE;
  1170. spte = spte & ~PT_WRITABLE_MASK;
  1171. return mmu_spte_update(sptep, spte);
  1172. }
  1173. static bool __rmap_write_protect(struct kvm *kvm,
  1174. struct kvm_rmap_head *rmap_head,
  1175. bool pt_protect)
  1176. {
  1177. u64 *sptep;
  1178. struct rmap_iterator iter;
  1179. bool flush = false;
  1180. for_each_rmap_spte(rmap_head, &iter, sptep)
  1181. flush |= spte_write_protect(sptep, pt_protect);
  1182. return flush;
  1183. }
  1184. static bool spte_clear_dirty(u64 *sptep)
  1185. {
  1186. u64 spte = *sptep;
  1187. rmap_printk("rmap_clear_dirty: spte %p %llx\n", sptep, *sptep);
  1188. spte &= ~shadow_dirty_mask;
  1189. return mmu_spte_update(sptep, spte);
  1190. }
  1191. static bool wrprot_ad_disabled_spte(u64 *sptep)
  1192. {
  1193. bool was_writable = test_and_clear_bit(PT_WRITABLE_SHIFT,
  1194. (unsigned long *)sptep);
  1195. if (was_writable)
  1196. kvm_set_pfn_dirty(spte_to_pfn(*sptep));
  1197. return was_writable;
  1198. }
  1199. /*
  1200. * Gets the GFN ready for another round of dirty logging by clearing the
  1201. * - D bit on ad-enabled SPTEs, and
  1202. * - W bit on ad-disabled SPTEs.
  1203. * Returns true iff any D or W bits were cleared.
  1204. */
  1205. static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
  1206. {
  1207. u64 *sptep;
  1208. struct rmap_iterator iter;
  1209. bool flush = false;
  1210. for_each_rmap_spte(rmap_head, &iter, sptep)
  1211. if (spte_ad_enabled(*sptep))
  1212. flush |= spte_clear_dirty(sptep);
  1213. else
  1214. flush |= wrprot_ad_disabled_spte(sptep);
  1215. return flush;
  1216. }
  1217. static bool spte_set_dirty(u64 *sptep)
  1218. {
  1219. u64 spte = *sptep;
  1220. rmap_printk("rmap_set_dirty: spte %p %llx\n", sptep, *sptep);
  1221. spte |= shadow_dirty_mask;
  1222. return mmu_spte_update(sptep, spte);
  1223. }
  1224. static bool __rmap_set_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
  1225. {
  1226. u64 *sptep;
  1227. struct rmap_iterator iter;
  1228. bool flush = false;
  1229. for_each_rmap_spte(rmap_head, &iter, sptep)
  1230. if (spte_ad_enabled(*sptep))
  1231. flush |= spte_set_dirty(sptep);
  1232. return flush;
  1233. }
  1234. /**
  1235. * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages
  1236. * @kvm: kvm instance
  1237. * @slot: slot to protect
  1238. * @gfn_offset: start of the BITS_PER_LONG pages we care about
  1239. * @mask: indicates which pages we should protect
  1240. *
  1241. * Used when we do not need to care about huge page mappings: e.g. during dirty
  1242. * logging we do not have any such mappings.
  1243. */
  1244. static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  1245. struct kvm_memory_slot *slot,
  1246. gfn_t gfn_offset, unsigned long mask)
  1247. {
  1248. struct kvm_rmap_head *rmap_head;
  1249. while (mask) {
  1250. rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
  1251. PT_PAGE_TABLE_LEVEL, slot);
  1252. __rmap_write_protect(kvm, rmap_head, false);
  1253. /* clear the first set bit */
  1254. mask &= mask - 1;
  1255. }
  1256. }
  1257. /**
  1258. * kvm_mmu_clear_dirty_pt_masked - clear MMU D-bit for PT level pages, or write
  1259. * protect the page if the D-bit isn't supported.
  1260. * @kvm: kvm instance
  1261. * @slot: slot to clear D-bit
  1262. * @gfn_offset: start of the BITS_PER_LONG pages we care about
  1263. * @mask: indicates which pages we should clear D-bit
  1264. *
  1265. * Used for PML to re-log the dirty GPAs after userspace querying dirty_bitmap.
  1266. */
  1267. void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
  1268. struct kvm_memory_slot *slot,
  1269. gfn_t gfn_offset, unsigned long mask)
  1270. {
  1271. struct kvm_rmap_head *rmap_head;
  1272. while (mask) {
  1273. rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
  1274. PT_PAGE_TABLE_LEVEL, slot);
  1275. __rmap_clear_dirty(kvm, rmap_head);
  1276. /* clear the first set bit */
  1277. mask &= mask - 1;
  1278. }
  1279. }
  1280. EXPORT_SYMBOL_GPL(kvm_mmu_clear_dirty_pt_masked);
  1281. /**
  1282. * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
  1283. * PT level pages.
  1284. *
  1285. * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
  1286. * enable dirty logging for them.
  1287. *
  1288. * Used when we do not need to care about huge page mappings: e.g. during dirty
  1289. * logging we do not have any such mappings.
  1290. */
  1291. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  1292. struct kvm_memory_slot *slot,
  1293. gfn_t gfn_offset, unsigned long mask)
  1294. {
  1295. if (kvm_x86_ops->enable_log_dirty_pt_masked)
  1296. kvm_x86_ops->enable_log_dirty_pt_masked(kvm, slot, gfn_offset,
  1297. mask);
  1298. else
  1299. kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
  1300. }
  1301. /**
  1302. * kvm_arch_write_log_dirty - emulate dirty page logging
  1303. * @vcpu: Guest mode vcpu
  1304. *
  1305. * Emulate arch specific page modification logging for the
  1306. * nested hypervisor
  1307. */
  1308. int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu)
  1309. {
  1310. if (kvm_x86_ops->write_log_dirty)
  1311. return kvm_x86_ops->write_log_dirty(vcpu);
  1312. return 0;
  1313. }
  1314. bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
  1315. struct kvm_memory_slot *slot, u64 gfn)
  1316. {
  1317. struct kvm_rmap_head *rmap_head;
  1318. int i;
  1319. bool write_protected = false;
  1320. for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
  1321. rmap_head = __gfn_to_rmap(gfn, i, slot);
  1322. write_protected |= __rmap_write_protect(kvm, rmap_head, true);
  1323. }
  1324. return write_protected;
  1325. }
  1326. static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
  1327. {
  1328. struct kvm_memory_slot *slot;
  1329. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1330. return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn);
  1331. }
  1332. static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
  1333. {
  1334. u64 *sptep;
  1335. struct rmap_iterator iter;
  1336. bool flush = false;
  1337. while ((sptep = rmap_get_first(rmap_head, &iter))) {
  1338. rmap_printk("%s: spte %p %llx.\n", __func__, sptep, *sptep);
  1339. drop_spte(kvm, sptep);
  1340. flush = true;
  1341. }
  1342. return flush;
  1343. }
  1344. static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1345. struct kvm_memory_slot *slot, gfn_t gfn, int level,
  1346. unsigned long data)
  1347. {
  1348. return kvm_zap_rmapp(kvm, rmap_head);
  1349. }
  1350. static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1351. struct kvm_memory_slot *slot, gfn_t gfn, int level,
  1352. unsigned long data)
  1353. {
  1354. u64 *sptep;
  1355. struct rmap_iterator iter;
  1356. int need_flush = 0;
  1357. u64 new_spte;
  1358. pte_t *ptep = (pte_t *)data;
  1359. kvm_pfn_t new_pfn;
  1360. WARN_ON(pte_huge(*ptep));
  1361. new_pfn = pte_pfn(*ptep);
  1362. restart:
  1363. for_each_rmap_spte(rmap_head, &iter, sptep) {
  1364. rmap_printk("kvm_set_pte_rmapp: spte %p %llx gfn %llx (%d)\n",
  1365. sptep, *sptep, gfn, level);
  1366. need_flush = 1;
  1367. if (pte_write(*ptep)) {
  1368. drop_spte(kvm, sptep);
  1369. goto restart;
  1370. } else {
  1371. new_spte = *sptep & ~PT64_BASE_ADDR_MASK;
  1372. new_spte |= (u64)new_pfn << PAGE_SHIFT;
  1373. new_spte &= ~PT_WRITABLE_MASK;
  1374. new_spte &= ~SPTE_HOST_WRITEABLE;
  1375. new_spte = mark_spte_for_access_track(new_spte);
  1376. mmu_spte_clear_track_bits(sptep);
  1377. mmu_spte_set(sptep, new_spte);
  1378. }
  1379. }
  1380. if (need_flush)
  1381. kvm_flush_remote_tlbs(kvm);
  1382. return 0;
  1383. }
  1384. struct slot_rmap_walk_iterator {
  1385. /* input fields. */
  1386. struct kvm_memory_slot *slot;
  1387. gfn_t start_gfn;
  1388. gfn_t end_gfn;
  1389. int start_level;
  1390. int end_level;
  1391. /* output fields. */
  1392. gfn_t gfn;
  1393. struct kvm_rmap_head *rmap;
  1394. int level;
  1395. /* private field. */
  1396. struct kvm_rmap_head *end_rmap;
  1397. };
  1398. static void
  1399. rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator, int level)
  1400. {
  1401. iterator->level = level;
  1402. iterator->gfn = iterator->start_gfn;
  1403. iterator->rmap = __gfn_to_rmap(iterator->gfn, level, iterator->slot);
  1404. iterator->end_rmap = __gfn_to_rmap(iterator->end_gfn, level,
  1405. iterator->slot);
  1406. }
  1407. static void
  1408. slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
  1409. struct kvm_memory_slot *slot, int start_level,
  1410. int end_level, gfn_t start_gfn, gfn_t end_gfn)
  1411. {
  1412. iterator->slot = slot;
  1413. iterator->start_level = start_level;
  1414. iterator->end_level = end_level;
  1415. iterator->start_gfn = start_gfn;
  1416. iterator->end_gfn = end_gfn;
  1417. rmap_walk_init_level(iterator, iterator->start_level);
  1418. }
  1419. static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
  1420. {
  1421. return !!iterator->rmap;
  1422. }
  1423. static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
  1424. {
  1425. if (++iterator->rmap <= iterator->end_rmap) {
  1426. iterator->gfn += (1UL << KVM_HPAGE_GFN_SHIFT(iterator->level));
  1427. return;
  1428. }
  1429. if (++iterator->level > iterator->end_level) {
  1430. iterator->rmap = NULL;
  1431. return;
  1432. }
  1433. rmap_walk_init_level(iterator, iterator->level);
  1434. }
  1435. #define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_, \
  1436. _start_gfn, _end_gfn, _iter_) \
  1437. for (slot_rmap_walk_init(_iter_, _slot_, _start_level_, \
  1438. _end_level_, _start_gfn, _end_gfn); \
  1439. slot_rmap_walk_okay(_iter_); \
  1440. slot_rmap_walk_next(_iter_))
  1441. static int kvm_handle_hva_range(struct kvm *kvm,
  1442. unsigned long start,
  1443. unsigned long end,
  1444. unsigned long data,
  1445. int (*handler)(struct kvm *kvm,
  1446. struct kvm_rmap_head *rmap_head,
  1447. struct kvm_memory_slot *slot,
  1448. gfn_t gfn,
  1449. int level,
  1450. unsigned long data))
  1451. {
  1452. struct kvm_memslots *slots;
  1453. struct kvm_memory_slot *memslot;
  1454. struct slot_rmap_walk_iterator iterator;
  1455. int ret = 0;
  1456. int i;
  1457. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  1458. slots = __kvm_memslots(kvm, i);
  1459. kvm_for_each_memslot(memslot, slots) {
  1460. unsigned long hva_start, hva_end;
  1461. gfn_t gfn_start, gfn_end;
  1462. hva_start = max(start, memslot->userspace_addr);
  1463. hva_end = min(end, memslot->userspace_addr +
  1464. (memslot->npages << PAGE_SHIFT));
  1465. if (hva_start >= hva_end)
  1466. continue;
  1467. /*
  1468. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  1469. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  1470. */
  1471. gfn_start = hva_to_gfn_memslot(hva_start, memslot);
  1472. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  1473. for_each_slot_rmap_range(memslot, PT_PAGE_TABLE_LEVEL,
  1474. PT_MAX_HUGEPAGE_LEVEL,
  1475. gfn_start, gfn_end - 1,
  1476. &iterator)
  1477. ret |= handler(kvm, iterator.rmap, memslot,
  1478. iterator.gfn, iterator.level, data);
  1479. }
  1480. }
  1481. return ret;
  1482. }
  1483. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  1484. unsigned long data,
  1485. int (*handler)(struct kvm *kvm,
  1486. struct kvm_rmap_head *rmap_head,
  1487. struct kvm_memory_slot *slot,
  1488. gfn_t gfn, int level,
  1489. unsigned long data))
  1490. {
  1491. return kvm_handle_hva_range(kvm, hva, hva + 1, data, handler);
  1492. }
  1493. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  1494. {
  1495. return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
  1496. }
  1497. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  1498. {
  1499. return kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp);
  1500. }
  1501. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  1502. {
  1503. kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
  1504. }
  1505. static int kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1506. struct kvm_memory_slot *slot, gfn_t gfn, int level,
  1507. unsigned long data)
  1508. {
  1509. u64 *sptep;
  1510. struct rmap_iterator uninitialized_var(iter);
  1511. int young = 0;
  1512. for_each_rmap_spte(rmap_head, &iter, sptep)
  1513. young |= mmu_spte_age(sptep);
  1514. trace_kvm_age_page(gfn, level, slot, young);
  1515. return young;
  1516. }
  1517. static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1518. struct kvm_memory_slot *slot, gfn_t gfn,
  1519. int level, unsigned long data)
  1520. {
  1521. u64 *sptep;
  1522. struct rmap_iterator iter;
  1523. for_each_rmap_spte(rmap_head, &iter, sptep)
  1524. if (is_accessed_spte(*sptep))
  1525. return 1;
  1526. return 0;
  1527. }
  1528. #define RMAP_RECYCLE_THRESHOLD 1000
  1529. static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  1530. {
  1531. struct kvm_rmap_head *rmap_head;
  1532. struct kvm_mmu_page *sp;
  1533. sp = page_header(__pa(spte));
  1534. rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
  1535. kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, 0);
  1536. kvm_flush_remote_tlbs(vcpu->kvm);
  1537. }
  1538. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
  1539. {
  1540. return kvm_handle_hva_range(kvm, start, end, 0, kvm_age_rmapp);
  1541. }
  1542. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  1543. {
  1544. return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
  1545. }
  1546. #ifdef MMU_DEBUG
  1547. static int is_empty_shadow_page(u64 *spt)
  1548. {
  1549. u64 *pos;
  1550. u64 *end;
  1551. for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
  1552. if (is_shadow_present_pte(*pos)) {
  1553. printk(KERN_ERR "%s: %p %llx\n", __func__,
  1554. pos, *pos);
  1555. return 0;
  1556. }
  1557. return 1;
  1558. }
  1559. #endif
  1560. /*
  1561. * This value is the sum of all of the kvm instances's
  1562. * kvm->arch.n_used_mmu_pages values. We need a global,
  1563. * aggregate version in order to make the slab shrinker
  1564. * faster
  1565. */
  1566. static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
  1567. {
  1568. kvm->arch.n_used_mmu_pages += nr;
  1569. percpu_counter_add(&kvm_total_used_mmu_pages, nr);
  1570. }
  1571. static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
  1572. {
  1573. MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
  1574. hlist_del(&sp->hash_link);
  1575. list_del(&sp->link);
  1576. free_page((unsigned long)sp->spt);
  1577. if (!sp->role.direct)
  1578. free_page((unsigned long)sp->gfns);
  1579. kmem_cache_free(mmu_page_header_cache, sp);
  1580. }
  1581. static unsigned kvm_page_table_hashfn(gfn_t gfn)
  1582. {
  1583. return hash_64(gfn, KVM_MMU_HASH_SHIFT);
  1584. }
  1585. static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
  1586. struct kvm_mmu_page *sp, u64 *parent_pte)
  1587. {
  1588. if (!parent_pte)
  1589. return;
  1590. pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
  1591. }
  1592. static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
  1593. u64 *parent_pte)
  1594. {
  1595. pte_list_remove(parent_pte, &sp->parent_ptes);
  1596. }
  1597. static void drop_parent_pte(struct kvm_mmu_page *sp,
  1598. u64 *parent_pte)
  1599. {
  1600. mmu_page_remove_parent_pte(sp, parent_pte);
  1601. mmu_spte_clear_no_track(parent_pte);
  1602. }
  1603. static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct)
  1604. {
  1605. struct kvm_mmu_page *sp;
  1606. sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
  1607. sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
  1608. if (!direct)
  1609. sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
  1610. set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
  1611. /*
  1612. * The active_mmu_pages list is the FIFO list, do not move the
  1613. * page until it is zapped. kvm_zap_obsolete_pages depends on
  1614. * this feature. See the comments in kvm_zap_obsolete_pages().
  1615. */
  1616. list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
  1617. kvm_mod_used_mmu_pages(vcpu->kvm, +1);
  1618. return sp;
  1619. }
  1620. static void mark_unsync(u64 *spte);
  1621. static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
  1622. {
  1623. u64 *sptep;
  1624. struct rmap_iterator iter;
  1625. for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
  1626. mark_unsync(sptep);
  1627. }
  1628. }
  1629. static void mark_unsync(u64 *spte)
  1630. {
  1631. struct kvm_mmu_page *sp;
  1632. unsigned int index;
  1633. sp = page_header(__pa(spte));
  1634. index = spte - sp->spt;
  1635. if (__test_and_set_bit(index, sp->unsync_child_bitmap))
  1636. return;
  1637. if (sp->unsync_children++)
  1638. return;
  1639. kvm_mmu_mark_parents_unsync(sp);
  1640. }
  1641. static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
  1642. struct kvm_mmu_page *sp)
  1643. {
  1644. return 0;
  1645. }
  1646. static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  1647. {
  1648. }
  1649. static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
  1650. struct kvm_mmu_page *sp, u64 *spte,
  1651. const void *pte)
  1652. {
  1653. WARN_ON(1);
  1654. }
  1655. #define KVM_PAGE_ARRAY_NR 16
  1656. struct kvm_mmu_pages {
  1657. struct mmu_page_and_offset {
  1658. struct kvm_mmu_page *sp;
  1659. unsigned int idx;
  1660. } page[KVM_PAGE_ARRAY_NR];
  1661. unsigned int nr;
  1662. };
  1663. static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
  1664. int idx)
  1665. {
  1666. int i;
  1667. if (sp->unsync)
  1668. for (i=0; i < pvec->nr; i++)
  1669. if (pvec->page[i].sp == sp)
  1670. return 0;
  1671. pvec->page[pvec->nr].sp = sp;
  1672. pvec->page[pvec->nr].idx = idx;
  1673. pvec->nr++;
  1674. return (pvec->nr == KVM_PAGE_ARRAY_NR);
  1675. }
  1676. static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
  1677. {
  1678. --sp->unsync_children;
  1679. WARN_ON((int)sp->unsync_children < 0);
  1680. __clear_bit(idx, sp->unsync_child_bitmap);
  1681. }
  1682. static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
  1683. struct kvm_mmu_pages *pvec)
  1684. {
  1685. int i, ret, nr_unsync_leaf = 0;
  1686. for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
  1687. struct kvm_mmu_page *child;
  1688. u64 ent = sp->spt[i];
  1689. if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
  1690. clear_unsync_child_bit(sp, i);
  1691. continue;
  1692. }
  1693. child = page_header(ent & PT64_BASE_ADDR_MASK);
  1694. if (child->unsync_children) {
  1695. if (mmu_pages_add(pvec, child, i))
  1696. return -ENOSPC;
  1697. ret = __mmu_unsync_walk(child, pvec);
  1698. if (!ret) {
  1699. clear_unsync_child_bit(sp, i);
  1700. continue;
  1701. } else if (ret > 0) {
  1702. nr_unsync_leaf += ret;
  1703. } else
  1704. return ret;
  1705. } else if (child->unsync) {
  1706. nr_unsync_leaf++;
  1707. if (mmu_pages_add(pvec, child, i))
  1708. return -ENOSPC;
  1709. } else
  1710. clear_unsync_child_bit(sp, i);
  1711. }
  1712. return nr_unsync_leaf;
  1713. }
  1714. #define INVALID_INDEX (-1)
  1715. static int mmu_unsync_walk(struct kvm_mmu_page *sp,
  1716. struct kvm_mmu_pages *pvec)
  1717. {
  1718. pvec->nr = 0;
  1719. if (!sp->unsync_children)
  1720. return 0;
  1721. mmu_pages_add(pvec, sp, INVALID_INDEX);
  1722. return __mmu_unsync_walk(sp, pvec);
  1723. }
  1724. static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
  1725. {
  1726. WARN_ON(!sp->unsync);
  1727. trace_kvm_mmu_sync_page(sp);
  1728. sp->unsync = 0;
  1729. --kvm->stat.mmu_unsync;
  1730. }
  1731. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1732. struct list_head *invalid_list);
  1733. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1734. struct list_head *invalid_list);
  1735. /*
  1736. * NOTE: we should pay more attention on the zapped-obsolete page
  1737. * (is_obsolete_sp(sp) && sp->role.invalid) when you do hash list walk
  1738. * since it has been deleted from active_mmu_pages but still can be found
  1739. * at hast list.
  1740. *
  1741. * for_each_valid_sp() has skipped that kind of pages.
  1742. */
  1743. #define for_each_valid_sp(_kvm, _sp, _gfn) \
  1744. hlist_for_each_entry(_sp, \
  1745. &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \
  1746. if (is_obsolete_sp((_kvm), (_sp)) || (_sp)->role.invalid) { \
  1747. } else
  1748. #define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \
  1749. for_each_valid_sp(_kvm, _sp, _gfn) \
  1750. if ((_sp)->gfn != (_gfn) || (_sp)->role.direct) {} else
  1751. /* @sp->gfn should be write-protected at the call site */
  1752. static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1753. struct list_head *invalid_list)
  1754. {
  1755. if (sp->role.cr4_pae != !!is_pae(vcpu)) {
  1756. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1757. return false;
  1758. }
  1759. if (vcpu->arch.mmu.sync_page(vcpu, sp) == 0) {
  1760. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1761. return false;
  1762. }
  1763. return true;
  1764. }
  1765. static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu,
  1766. struct list_head *invalid_list,
  1767. bool remote_flush, bool local_flush)
  1768. {
  1769. if (!list_empty(invalid_list)) {
  1770. kvm_mmu_commit_zap_page(vcpu->kvm, invalid_list);
  1771. return;
  1772. }
  1773. if (remote_flush)
  1774. kvm_flush_remote_tlbs(vcpu->kvm);
  1775. else if (local_flush)
  1776. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1777. }
  1778. #ifdef CONFIG_KVM_MMU_AUDIT
  1779. #include "mmu_audit.c"
  1780. #else
  1781. static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
  1782. static void mmu_audit_disable(void) { }
  1783. #endif
  1784. static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
  1785. {
  1786. return unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
  1787. }
  1788. static bool kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1789. struct list_head *invalid_list)
  1790. {
  1791. kvm_unlink_unsync_page(vcpu->kvm, sp);
  1792. return __kvm_sync_page(vcpu, sp, invalid_list);
  1793. }
  1794. /* @gfn should be write-protected at the call site */
  1795. static bool kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn,
  1796. struct list_head *invalid_list)
  1797. {
  1798. struct kvm_mmu_page *s;
  1799. bool ret = false;
  1800. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
  1801. if (!s->unsync)
  1802. continue;
  1803. WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
  1804. ret |= kvm_sync_page(vcpu, s, invalid_list);
  1805. }
  1806. return ret;
  1807. }
  1808. struct mmu_page_path {
  1809. struct kvm_mmu_page *parent[PT64_ROOT_MAX_LEVEL];
  1810. unsigned int idx[PT64_ROOT_MAX_LEVEL];
  1811. };
  1812. #define for_each_sp(pvec, sp, parents, i) \
  1813. for (i = mmu_pages_first(&pvec, &parents); \
  1814. i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
  1815. i = mmu_pages_next(&pvec, &parents, i))
  1816. static int mmu_pages_next(struct kvm_mmu_pages *pvec,
  1817. struct mmu_page_path *parents,
  1818. int i)
  1819. {
  1820. int n;
  1821. for (n = i+1; n < pvec->nr; n++) {
  1822. struct kvm_mmu_page *sp = pvec->page[n].sp;
  1823. unsigned idx = pvec->page[n].idx;
  1824. int level = sp->role.level;
  1825. parents->idx[level-1] = idx;
  1826. if (level == PT_PAGE_TABLE_LEVEL)
  1827. break;
  1828. parents->parent[level-2] = sp;
  1829. }
  1830. return n;
  1831. }
  1832. static int mmu_pages_first(struct kvm_mmu_pages *pvec,
  1833. struct mmu_page_path *parents)
  1834. {
  1835. struct kvm_mmu_page *sp;
  1836. int level;
  1837. if (pvec->nr == 0)
  1838. return 0;
  1839. WARN_ON(pvec->page[0].idx != INVALID_INDEX);
  1840. sp = pvec->page[0].sp;
  1841. level = sp->role.level;
  1842. WARN_ON(level == PT_PAGE_TABLE_LEVEL);
  1843. parents->parent[level-2] = sp;
  1844. /* Also set up a sentinel. Further entries in pvec are all
  1845. * children of sp, so this element is never overwritten.
  1846. */
  1847. parents->parent[level-1] = NULL;
  1848. return mmu_pages_next(pvec, parents, 0);
  1849. }
  1850. static void mmu_pages_clear_parents(struct mmu_page_path *parents)
  1851. {
  1852. struct kvm_mmu_page *sp;
  1853. unsigned int level = 0;
  1854. do {
  1855. unsigned int idx = parents->idx[level];
  1856. sp = parents->parent[level];
  1857. if (!sp)
  1858. return;
  1859. WARN_ON(idx == INVALID_INDEX);
  1860. clear_unsync_child_bit(sp, idx);
  1861. level++;
  1862. } while (!sp->unsync_children);
  1863. }
  1864. static void mmu_sync_children(struct kvm_vcpu *vcpu,
  1865. struct kvm_mmu_page *parent)
  1866. {
  1867. int i;
  1868. struct kvm_mmu_page *sp;
  1869. struct mmu_page_path parents;
  1870. struct kvm_mmu_pages pages;
  1871. LIST_HEAD(invalid_list);
  1872. bool flush = false;
  1873. while (mmu_unsync_walk(parent, &pages)) {
  1874. bool protected = false;
  1875. for_each_sp(pages, sp, parents, i)
  1876. protected |= rmap_write_protect(vcpu, sp->gfn);
  1877. if (protected) {
  1878. kvm_flush_remote_tlbs(vcpu->kvm);
  1879. flush = false;
  1880. }
  1881. for_each_sp(pages, sp, parents, i) {
  1882. flush |= kvm_sync_page(vcpu, sp, &invalid_list);
  1883. mmu_pages_clear_parents(&parents);
  1884. }
  1885. if (need_resched() || spin_needbreak(&vcpu->kvm->mmu_lock)) {
  1886. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1887. cond_resched_lock(&vcpu->kvm->mmu_lock);
  1888. flush = false;
  1889. }
  1890. }
  1891. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1892. }
  1893. static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
  1894. {
  1895. atomic_set(&sp->write_flooding_count, 0);
  1896. }
  1897. static void clear_sp_write_flooding_count(u64 *spte)
  1898. {
  1899. struct kvm_mmu_page *sp = page_header(__pa(spte));
  1900. __clear_sp_write_flooding_count(sp);
  1901. }
  1902. static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
  1903. gfn_t gfn,
  1904. gva_t gaddr,
  1905. unsigned level,
  1906. int direct,
  1907. unsigned access)
  1908. {
  1909. union kvm_mmu_page_role role;
  1910. unsigned quadrant;
  1911. struct kvm_mmu_page *sp;
  1912. bool need_sync = false;
  1913. bool flush = false;
  1914. int collisions = 0;
  1915. LIST_HEAD(invalid_list);
  1916. role = vcpu->arch.mmu.base_role;
  1917. role.level = level;
  1918. role.direct = direct;
  1919. if (role.direct)
  1920. role.cr4_pae = 0;
  1921. role.access = access;
  1922. if (!vcpu->arch.mmu.direct_map
  1923. && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
  1924. quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
  1925. quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
  1926. role.quadrant = quadrant;
  1927. }
  1928. for_each_valid_sp(vcpu->kvm, sp, gfn) {
  1929. if (sp->gfn != gfn) {
  1930. collisions++;
  1931. continue;
  1932. }
  1933. if (!need_sync && sp->unsync)
  1934. need_sync = true;
  1935. if (sp->role.word != role.word)
  1936. continue;
  1937. if (sp->unsync) {
  1938. /* The page is good, but __kvm_sync_page might still end
  1939. * up zapping it. If so, break in order to rebuild it.
  1940. */
  1941. if (!__kvm_sync_page(vcpu, sp, &invalid_list))
  1942. break;
  1943. WARN_ON(!list_empty(&invalid_list));
  1944. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1945. }
  1946. if (sp->unsync_children)
  1947. kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
  1948. __clear_sp_write_flooding_count(sp);
  1949. trace_kvm_mmu_get_page(sp, false);
  1950. goto out;
  1951. }
  1952. ++vcpu->kvm->stat.mmu_cache_miss;
  1953. sp = kvm_mmu_alloc_page(vcpu, direct);
  1954. sp->gfn = gfn;
  1955. sp->role = role;
  1956. hlist_add_head(&sp->hash_link,
  1957. &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
  1958. if (!direct) {
  1959. /*
  1960. * we should do write protection before syncing pages
  1961. * otherwise the content of the synced shadow page may
  1962. * be inconsistent with guest page table.
  1963. */
  1964. account_shadowed(vcpu->kvm, sp);
  1965. if (level == PT_PAGE_TABLE_LEVEL &&
  1966. rmap_write_protect(vcpu, gfn))
  1967. kvm_flush_remote_tlbs(vcpu->kvm);
  1968. if (level > PT_PAGE_TABLE_LEVEL && need_sync)
  1969. flush |= kvm_sync_pages(vcpu, gfn, &invalid_list);
  1970. }
  1971. sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
  1972. clear_page(sp->spt);
  1973. trace_kvm_mmu_get_page(sp, true);
  1974. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1975. out:
  1976. if (collisions > vcpu->kvm->stat.max_mmu_page_hash_collisions)
  1977. vcpu->kvm->stat.max_mmu_page_hash_collisions = collisions;
  1978. return sp;
  1979. }
  1980. static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
  1981. struct kvm_vcpu *vcpu, u64 addr)
  1982. {
  1983. iterator->addr = addr;
  1984. iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
  1985. iterator->level = vcpu->arch.mmu.shadow_root_level;
  1986. if (iterator->level == PT64_ROOT_4LEVEL &&
  1987. vcpu->arch.mmu.root_level < PT64_ROOT_4LEVEL &&
  1988. !vcpu->arch.mmu.direct_map)
  1989. --iterator->level;
  1990. if (iterator->level == PT32E_ROOT_LEVEL) {
  1991. iterator->shadow_addr
  1992. = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
  1993. iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
  1994. --iterator->level;
  1995. if (!iterator->shadow_addr)
  1996. iterator->level = 0;
  1997. }
  1998. }
  1999. static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
  2000. {
  2001. if (iterator->level < PT_PAGE_TABLE_LEVEL)
  2002. return false;
  2003. iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
  2004. iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
  2005. return true;
  2006. }
  2007. static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
  2008. u64 spte)
  2009. {
  2010. if (is_last_spte(spte, iterator->level)) {
  2011. iterator->level = 0;
  2012. return;
  2013. }
  2014. iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
  2015. --iterator->level;
  2016. }
  2017. static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
  2018. {
  2019. __shadow_walk_next(iterator, *iterator->sptep);
  2020. }
  2021. static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
  2022. struct kvm_mmu_page *sp)
  2023. {
  2024. u64 spte;
  2025. BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
  2026. spte = __pa(sp->spt) | shadow_present_mask | PT_WRITABLE_MASK |
  2027. shadow_user_mask | shadow_x_mask | shadow_me_mask;
  2028. if (sp_ad_disabled(sp))
  2029. spte |= shadow_acc_track_value;
  2030. else
  2031. spte |= shadow_accessed_mask;
  2032. mmu_spte_set(sptep, spte);
  2033. mmu_page_add_parent_pte(vcpu, sp, sptep);
  2034. if (sp->unsync_children || sp->unsync)
  2035. mark_unsync(sptep);
  2036. }
  2037. static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  2038. unsigned direct_access)
  2039. {
  2040. if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
  2041. struct kvm_mmu_page *child;
  2042. /*
  2043. * For the direct sp, if the guest pte's dirty bit
  2044. * changed form clean to dirty, it will corrupt the
  2045. * sp's access: allow writable in the read-only sp,
  2046. * so we should update the spte at this point to get
  2047. * a new sp with the correct access.
  2048. */
  2049. child = page_header(*sptep & PT64_BASE_ADDR_MASK);
  2050. if (child->role.access == direct_access)
  2051. return;
  2052. drop_parent_pte(child, sptep);
  2053. kvm_flush_remote_tlbs(vcpu->kvm);
  2054. }
  2055. }
  2056. static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
  2057. u64 *spte)
  2058. {
  2059. u64 pte;
  2060. struct kvm_mmu_page *child;
  2061. pte = *spte;
  2062. if (is_shadow_present_pte(pte)) {
  2063. if (is_last_spte(pte, sp->role.level)) {
  2064. drop_spte(kvm, spte);
  2065. if (is_large_pte(pte))
  2066. --kvm->stat.lpages;
  2067. } else {
  2068. child = page_header(pte & PT64_BASE_ADDR_MASK);
  2069. drop_parent_pte(child, spte);
  2070. }
  2071. return true;
  2072. }
  2073. if (is_mmio_spte(pte))
  2074. mmu_spte_clear_no_track(spte);
  2075. return false;
  2076. }
  2077. static void kvm_mmu_page_unlink_children(struct kvm *kvm,
  2078. struct kvm_mmu_page *sp)
  2079. {
  2080. unsigned i;
  2081. for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
  2082. mmu_page_zap_pte(kvm, sp, sp->spt + i);
  2083. }
  2084. static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
  2085. {
  2086. u64 *sptep;
  2087. struct rmap_iterator iter;
  2088. while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
  2089. drop_parent_pte(sp, sptep);
  2090. }
  2091. static int mmu_zap_unsync_children(struct kvm *kvm,
  2092. struct kvm_mmu_page *parent,
  2093. struct list_head *invalid_list)
  2094. {
  2095. int i, zapped = 0;
  2096. struct mmu_page_path parents;
  2097. struct kvm_mmu_pages pages;
  2098. if (parent->role.level == PT_PAGE_TABLE_LEVEL)
  2099. return 0;
  2100. while (mmu_unsync_walk(parent, &pages)) {
  2101. struct kvm_mmu_page *sp;
  2102. for_each_sp(pages, sp, parents, i) {
  2103. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  2104. mmu_pages_clear_parents(&parents);
  2105. zapped++;
  2106. }
  2107. }
  2108. return zapped;
  2109. }
  2110. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  2111. struct list_head *invalid_list)
  2112. {
  2113. int ret;
  2114. trace_kvm_mmu_prepare_zap_page(sp);
  2115. ++kvm->stat.mmu_shadow_zapped;
  2116. ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
  2117. kvm_mmu_page_unlink_children(kvm, sp);
  2118. kvm_mmu_unlink_parents(kvm, sp);
  2119. if (!sp->role.invalid && !sp->role.direct)
  2120. unaccount_shadowed(kvm, sp);
  2121. if (sp->unsync)
  2122. kvm_unlink_unsync_page(kvm, sp);
  2123. if (!sp->root_count) {
  2124. /* Count self */
  2125. ret++;
  2126. list_move(&sp->link, invalid_list);
  2127. kvm_mod_used_mmu_pages(kvm, -1);
  2128. } else {
  2129. list_move(&sp->link, &kvm->arch.active_mmu_pages);
  2130. /*
  2131. * The obsolete pages can not be used on any vcpus.
  2132. * See the comments in kvm_mmu_invalidate_zap_all_pages().
  2133. */
  2134. if (!sp->role.invalid && !is_obsolete_sp(kvm, sp))
  2135. kvm_reload_remote_mmus(kvm);
  2136. }
  2137. sp->role.invalid = 1;
  2138. return ret;
  2139. }
  2140. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  2141. struct list_head *invalid_list)
  2142. {
  2143. struct kvm_mmu_page *sp, *nsp;
  2144. if (list_empty(invalid_list))
  2145. return;
  2146. /*
  2147. * We need to make sure everyone sees our modifications to
  2148. * the page tables and see changes to vcpu->mode here. The barrier
  2149. * in the kvm_flush_remote_tlbs() achieves this. This pairs
  2150. * with vcpu_enter_guest and walk_shadow_page_lockless_begin/end.
  2151. *
  2152. * In addition, kvm_flush_remote_tlbs waits for all vcpus to exit
  2153. * guest mode and/or lockless shadow page table walks.
  2154. */
  2155. kvm_flush_remote_tlbs(kvm);
  2156. list_for_each_entry_safe(sp, nsp, invalid_list, link) {
  2157. WARN_ON(!sp->role.invalid || sp->root_count);
  2158. kvm_mmu_free_page(sp);
  2159. }
  2160. }
  2161. static bool prepare_zap_oldest_mmu_page(struct kvm *kvm,
  2162. struct list_head *invalid_list)
  2163. {
  2164. struct kvm_mmu_page *sp;
  2165. if (list_empty(&kvm->arch.active_mmu_pages))
  2166. return false;
  2167. sp = list_last_entry(&kvm->arch.active_mmu_pages,
  2168. struct kvm_mmu_page, link);
  2169. return kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  2170. }
  2171. /*
  2172. * Changing the number of mmu pages allocated to the vm
  2173. * Note: if goal_nr_mmu_pages is too small, you will get dead lock
  2174. */
  2175. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
  2176. {
  2177. LIST_HEAD(invalid_list);
  2178. spin_lock(&kvm->mmu_lock);
  2179. if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
  2180. /* Need to free some mmu pages to achieve the goal. */
  2181. while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages)
  2182. if (!prepare_zap_oldest_mmu_page(kvm, &invalid_list))
  2183. break;
  2184. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2185. goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
  2186. }
  2187. kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
  2188. spin_unlock(&kvm->mmu_lock);
  2189. }
  2190. int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
  2191. {
  2192. struct kvm_mmu_page *sp;
  2193. LIST_HEAD(invalid_list);
  2194. int r;
  2195. pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
  2196. r = 0;
  2197. spin_lock(&kvm->mmu_lock);
  2198. for_each_gfn_indirect_valid_sp(kvm, sp, gfn) {
  2199. pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
  2200. sp->role.word);
  2201. r = 1;
  2202. kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
  2203. }
  2204. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2205. spin_unlock(&kvm->mmu_lock);
  2206. return r;
  2207. }
  2208. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
  2209. static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  2210. {
  2211. trace_kvm_mmu_unsync_page(sp);
  2212. ++vcpu->kvm->stat.mmu_unsync;
  2213. sp->unsync = 1;
  2214. kvm_mmu_mark_parents_unsync(sp);
  2215. }
  2216. static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
  2217. bool can_unsync)
  2218. {
  2219. struct kvm_mmu_page *sp;
  2220. if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
  2221. return true;
  2222. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
  2223. if (!can_unsync)
  2224. return true;
  2225. if (sp->unsync)
  2226. continue;
  2227. WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
  2228. kvm_unsync_page(vcpu, sp);
  2229. }
  2230. return false;
  2231. }
  2232. static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
  2233. {
  2234. if (pfn_valid(pfn))
  2235. return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn)) &&
  2236. /*
  2237. * Some reserved pages, such as those from NVDIMM
  2238. * DAX devices, are not for MMIO, and can be mapped
  2239. * with cached memory type for better performance.
  2240. * However, the above check misconceives those pages
  2241. * as MMIO, and results in KVM mapping them with UC
  2242. * memory type, which would hurt the performance.
  2243. * Therefore, we check the host memory type in addition
  2244. * and only treat UC/UC-/WC pages as MMIO.
  2245. */
  2246. (!pat_enabled() || pat_pfn_immune_to_uc_mtrr(pfn));
  2247. return true;
  2248. }
  2249. static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  2250. unsigned pte_access, int level,
  2251. gfn_t gfn, kvm_pfn_t pfn, bool speculative,
  2252. bool can_unsync, bool host_writable)
  2253. {
  2254. u64 spte = 0;
  2255. int ret = 0;
  2256. struct kvm_mmu_page *sp;
  2257. if (set_mmio_spte(vcpu, sptep, gfn, pfn, pte_access))
  2258. return 0;
  2259. sp = page_header(__pa(sptep));
  2260. if (sp_ad_disabled(sp))
  2261. spte |= shadow_acc_track_value;
  2262. /*
  2263. * For the EPT case, shadow_present_mask is 0 if hardware
  2264. * supports exec-only page table entries. In that case,
  2265. * ACC_USER_MASK and shadow_user_mask are used to represent
  2266. * read access. See FNAME(gpte_access) in paging_tmpl.h.
  2267. */
  2268. spte |= shadow_present_mask;
  2269. if (!speculative)
  2270. spte |= spte_shadow_accessed_mask(spte);
  2271. if (pte_access & ACC_EXEC_MASK)
  2272. spte |= shadow_x_mask;
  2273. else
  2274. spte |= shadow_nx_mask;
  2275. if (pte_access & ACC_USER_MASK)
  2276. spte |= shadow_user_mask;
  2277. if (level > PT_PAGE_TABLE_LEVEL)
  2278. spte |= PT_PAGE_SIZE_MASK;
  2279. if (tdp_enabled)
  2280. spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
  2281. kvm_is_mmio_pfn(pfn));
  2282. if (host_writable)
  2283. spte |= SPTE_HOST_WRITEABLE;
  2284. else
  2285. pte_access &= ~ACC_WRITE_MASK;
  2286. if (!kvm_is_mmio_pfn(pfn))
  2287. spte |= shadow_me_mask;
  2288. spte |= (u64)pfn << PAGE_SHIFT;
  2289. if (pte_access & ACC_WRITE_MASK) {
  2290. /*
  2291. * Other vcpu creates new sp in the window between
  2292. * mapping_level() and acquiring mmu-lock. We can
  2293. * allow guest to retry the access, the mapping can
  2294. * be fixed if guest refault.
  2295. */
  2296. if (level > PT_PAGE_TABLE_LEVEL &&
  2297. mmu_gfn_lpage_is_disallowed(vcpu, gfn, level))
  2298. goto done;
  2299. spte |= PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE;
  2300. /*
  2301. * Optimization: for pte sync, if spte was writable the hash
  2302. * lookup is unnecessary (and expensive). Write protection
  2303. * is responsibility of mmu_get_page / kvm_sync_page.
  2304. * Same reasoning can be applied to dirty page accounting.
  2305. */
  2306. if (!can_unsync && is_writable_pte(*sptep))
  2307. goto set_pte;
  2308. if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
  2309. pgprintk("%s: found shadow page for %llx, marking ro\n",
  2310. __func__, gfn);
  2311. ret = 1;
  2312. pte_access &= ~ACC_WRITE_MASK;
  2313. spte &= ~(PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE);
  2314. }
  2315. }
  2316. if (pte_access & ACC_WRITE_MASK) {
  2317. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  2318. spte |= spte_shadow_dirty_mask(spte);
  2319. }
  2320. if (speculative)
  2321. spte = mark_spte_for_access_track(spte);
  2322. set_pte:
  2323. if (mmu_spte_update(sptep, spte))
  2324. kvm_flush_remote_tlbs(vcpu->kvm);
  2325. done:
  2326. return ret;
  2327. }
  2328. static int mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access,
  2329. int write_fault, int level, gfn_t gfn, kvm_pfn_t pfn,
  2330. bool speculative, bool host_writable)
  2331. {
  2332. int was_rmapped = 0;
  2333. int rmap_count;
  2334. int ret = RET_PF_RETRY;
  2335. pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
  2336. *sptep, write_fault, gfn);
  2337. if (is_shadow_present_pte(*sptep)) {
  2338. /*
  2339. * If we overwrite a PTE page pointer with a 2MB PMD, unlink
  2340. * the parent of the now unreachable PTE.
  2341. */
  2342. if (level > PT_PAGE_TABLE_LEVEL &&
  2343. !is_large_pte(*sptep)) {
  2344. struct kvm_mmu_page *child;
  2345. u64 pte = *sptep;
  2346. child = page_header(pte & PT64_BASE_ADDR_MASK);
  2347. drop_parent_pte(child, sptep);
  2348. kvm_flush_remote_tlbs(vcpu->kvm);
  2349. } else if (pfn != spte_to_pfn(*sptep)) {
  2350. pgprintk("hfn old %llx new %llx\n",
  2351. spte_to_pfn(*sptep), pfn);
  2352. drop_spte(vcpu->kvm, sptep);
  2353. kvm_flush_remote_tlbs(vcpu->kvm);
  2354. } else
  2355. was_rmapped = 1;
  2356. }
  2357. if (set_spte(vcpu, sptep, pte_access, level, gfn, pfn, speculative,
  2358. true, host_writable)) {
  2359. if (write_fault)
  2360. ret = RET_PF_EMULATE;
  2361. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2362. }
  2363. if (unlikely(is_mmio_spte(*sptep)))
  2364. ret = RET_PF_EMULATE;
  2365. pgprintk("%s: setting spte %llx\n", __func__, *sptep);
  2366. pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
  2367. is_large_pte(*sptep)? "2MB" : "4kB",
  2368. *sptep & PT_WRITABLE_MASK ? "RW" : "R", gfn,
  2369. *sptep, sptep);
  2370. if (!was_rmapped && is_large_pte(*sptep))
  2371. ++vcpu->kvm->stat.lpages;
  2372. if (is_shadow_present_pte(*sptep)) {
  2373. if (!was_rmapped) {
  2374. rmap_count = rmap_add(vcpu, sptep, gfn);
  2375. if (rmap_count > RMAP_RECYCLE_THRESHOLD)
  2376. rmap_recycle(vcpu, sptep, gfn);
  2377. }
  2378. }
  2379. kvm_release_pfn_clean(pfn);
  2380. return ret;
  2381. }
  2382. static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
  2383. bool no_dirty_log)
  2384. {
  2385. struct kvm_memory_slot *slot;
  2386. slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
  2387. if (!slot)
  2388. return KVM_PFN_ERR_FAULT;
  2389. return gfn_to_pfn_memslot_atomic(slot, gfn);
  2390. }
  2391. static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
  2392. struct kvm_mmu_page *sp,
  2393. u64 *start, u64 *end)
  2394. {
  2395. struct page *pages[PTE_PREFETCH_NUM];
  2396. struct kvm_memory_slot *slot;
  2397. unsigned access = sp->role.access;
  2398. int i, ret;
  2399. gfn_t gfn;
  2400. gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
  2401. slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
  2402. if (!slot)
  2403. return -1;
  2404. ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start);
  2405. if (ret <= 0)
  2406. return -1;
  2407. for (i = 0; i < ret; i++, gfn++, start++)
  2408. mmu_set_spte(vcpu, start, access, 0, sp->role.level, gfn,
  2409. page_to_pfn(pages[i]), true, true);
  2410. return 0;
  2411. }
  2412. static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
  2413. struct kvm_mmu_page *sp, u64 *sptep)
  2414. {
  2415. u64 *spte, *start = NULL;
  2416. int i;
  2417. WARN_ON(!sp->role.direct);
  2418. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  2419. spte = sp->spt + i;
  2420. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  2421. if (is_shadow_present_pte(*spte) || spte == sptep) {
  2422. if (!start)
  2423. continue;
  2424. if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
  2425. break;
  2426. start = NULL;
  2427. } else if (!start)
  2428. start = spte;
  2429. }
  2430. }
  2431. static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
  2432. {
  2433. struct kvm_mmu_page *sp;
  2434. sp = page_header(__pa(sptep));
  2435. /*
  2436. * Without accessed bits, there's no way to distinguish between
  2437. * actually accessed translations and prefetched, so disable pte
  2438. * prefetch if accessed bits aren't available.
  2439. */
  2440. if (sp_ad_disabled(sp))
  2441. return;
  2442. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  2443. return;
  2444. __direct_pte_prefetch(vcpu, sp, sptep);
  2445. }
  2446. static int __direct_map(struct kvm_vcpu *vcpu, int write, int map_writable,
  2447. int level, gfn_t gfn, kvm_pfn_t pfn, bool prefault)
  2448. {
  2449. struct kvm_shadow_walk_iterator iterator;
  2450. struct kvm_mmu_page *sp;
  2451. int emulate = 0;
  2452. gfn_t pseudo_gfn;
  2453. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2454. return 0;
  2455. for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
  2456. if (iterator.level == level) {
  2457. emulate = mmu_set_spte(vcpu, iterator.sptep, ACC_ALL,
  2458. write, level, gfn, pfn, prefault,
  2459. map_writable);
  2460. direct_pte_prefetch(vcpu, iterator.sptep);
  2461. ++vcpu->stat.pf_fixed;
  2462. break;
  2463. }
  2464. drop_large_spte(vcpu, iterator.sptep);
  2465. if (!is_shadow_present_pte(*iterator.sptep)) {
  2466. u64 base_addr = iterator.addr;
  2467. base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
  2468. pseudo_gfn = base_addr >> PAGE_SHIFT;
  2469. sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
  2470. iterator.level - 1, 1, ACC_ALL);
  2471. link_shadow_page(vcpu, iterator.sptep, sp);
  2472. }
  2473. }
  2474. return emulate;
  2475. }
  2476. static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
  2477. {
  2478. siginfo_t info;
  2479. clear_siginfo(&info);
  2480. info.si_signo = SIGBUS;
  2481. info.si_errno = 0;
  2482. info.si_code = BUS_MCEERR_AR;
  2483. info.si_addr = (void __user *)address;
  2484. info.si_addr_lsb = PAGE_SHIFT;
  2485. send_sig_info(SIGBUS, &info, tsk);
  2486. }
  2487. static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
  2488. {
  2489. /*
  2490. * Do not cache the mmio info caused by writing the readonly gfn
  2491. * into the spte otherwise read access on readonly gfn also can
  2492. * caused mmio page fault and treat it as mmio access.
  2493. */
  2494. if (pfn == KVM_PFN_ERR_RO_FAULT)
  2495. return RET_PF_EMULATE;
  2496. if (pfn == KVM_PFN_ERR_HWPOISON) {
  2497. kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current);
  2498. return RET_PF_RETRY;
  2499. }
  2500. return -EFAULT;
  2501. }
  2502. static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
  2503. gfn_t *gfnp, kvm_pfn_t *pfnp,
  2504. int *levelp)
  2505. {
  2506. kvm_pfn_t pfn = *pfnp;
  2507. gfn_t gfn = *gfnp;
  2508. int level = *levelp;
  2509. /*
  2510. * Check if it's a transparent hugepage. If this would be an
  2511. * hugetlbfs page, level wouldn't be set to
  2512. * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
  2513. * here.
  2514. */
  2515. if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) &&
  2516. level == PT_PAGE_TABLE_LEVEL &&
  2517. PageTransCompoundMap(pfn_to_page(pfn)) &&
  2518. !mmu_gfn_lpage_is_disallowed(vcpu, gfn, PT_DIRECTORY_LEVEL)) {
  2519. unsigned long mask;
  2520. /*
  2521. * mmu_notifier_retry was successful and we hold the
  2522. * mmu_lock here, so the pmd can't become splitting
  2523. * from under us, and in turn
  2524. * __split_huge_page_refcount() can't run from under
  2525. * us and we can safely transfer the refcount from
  2526. * PG_tail to PG_head as we switch the pfn to tail to
  2527. * head.
  2528. */
  2529. *levelp = level = PT_DIRECTORY_LEVEL;
  2530. mask = KVM_PAGES_PER_HPAGE(level) - 1;
  2531. VM_BUG_ON((gfn & mask) != (pfn & mask));
  2532. if (pfn & mask) {
  2533. gfn &= ~mask;
  2534. *gfnp = gfn;
  2535. kvm_release_pfn_clean(pfn);
  2536. pfn &= ~mask;
  2537. kvm_get_pfn(pfn);
  2538. *pfnp = pfn;
  2539. }
  2540. }
  2541. }
  2542. static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
  2543. kvm_pfn_t pfn, unsigned access, int *ret_val)
  2544. {
  2545. /* The pfn is invalid, report the error! */
  2546. if (unlikely(is_error_pfn(pfn))) {
  2547. *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
  2548. return true;
  2549. }
  2550. if (unlikely(is_noslot_pfn(pfn)))
  2551. vcpu_cache_mmio_info(vcpu, gva, gfn, access);
  2552. return false;
  2553. }
  2554. static bool page_fault_can_be_fast(u32 error_code)
  2555. {
  2556. /*
  2557. * Do not fix the mmio spte with invalid generation number which
  2558. * need to be updated by slow page fault path.
  2559. */
  2560. if (unlikely(error_code & PFERR_RSVD_MASK))
  2561. return false;
  2562. /* See if the page fault is due to an NX violation */
  2563. if (unlikely(((error_code & (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))
  2564. == (PFERR_FETCH_MASK | PFERR_PRESENT_MASK))))
  2565. return false;
  2566. /*
  2567. * #PF can be fast if:
  2568. * 1. The shadow page table entry is not present, which could mean that
  2569. * the fault is potentially caused by access tracking (if enabled).
  2570. * 2. The shadow page table entry is present and the fault
  2571. * is caused by write-protect, that means we just need change the W
  2572. * bit of the spte which can be done out of mmu-lock.
  2573. *
  2574. * However, if access tracking is disabled we know that a non-present
  2575. * page must be a genuine page fault where we have to create a new SPTE.
  2576. * So, if access tracking is disabled, we return true only for write
  2577. * accesses to a present page.
  2578. */
  2579. return shadow_acc_track_mask != 0 ||
  2580. ((error_code & (PFERR_WRITE_MASK | PFERR_PRESENT_MASK))
  2581. == (PFERR_WRITE_MASK | PFERR_PRESENT_MASK));
  2582. }
  2583. /*
  2584. * Returns true if the SPTE was fixed successfully. Otherwise,
  2585. * someone else modified the SPTE from its original value.
  2586. */
  2587. static bool
  2588. fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  2589. u64 *sptep, u64 old_spte, u64 new_spte)
  2590. {
  2591. gfn_t gfn;
  2592. WARN_ON(!sp->role.direct);
  2593. /*
  2594. * Theoretically we could also set dirty bit (and flush TLB) here in
  2595. * order to eliminate unnecessary PML logging. See comments in
  2596. * set_spte. But fast_page_fault is very unlikely to happen with PML
  2597. * enabled, so we do not do this. This might result in the same GPA
  2598. * to be logged in PML buffer again when the write really happens, and
  2599. * eventually to be called by mark_page_dirty twice. But it's also no
  2600. * harm. This also avoids the TLB flush needed after setting dirty bit
  2601. * so non-PML cases won't be impacted.
  2602. *
  2603. * Compare with set_spte where instead shadow_dirty_mask is set.
  2604. */
  2605. if (cmpxchg64(sptep, old_spte, new_spte) != old_spte)
  2606. return false;
  2607. if (is_writable_pte(new_spte) && !is_writable_pte(old_spte)) {
  2608. /*
  2609. * The gfn of direct spte is stable since it is
  2610. * calculated by sp->gfn.
  2611. */
  2612. gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
  2613. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  2614. }
  2615. return true;
  2616. }
  2617. static bool is_access_allowed(u32 fault_err_code, u64 spte)
  2618. {
  2619. if (fault_err_code & PFERR_FETCH_MASK)
  2620. return is_executable_pte(spte);
  2621. if (fault_err_code & PFERR_WRITE_MASK)
  2622. return is_writable_pte(spte);
  2623. /* Fault was on Read access */
  2624. return spte & PT_PRESENT_MASK;
  2625. }
  2626. /*
  2627. * Return value:
  2628. * - true: let the vcpu to access on the same address again.
  2629. * - false: let the real page fault path to fix it.
  2630. */
  2631. static bool fast_page_fault(struct kvm_vcpu *vcpu, gva_t gva, int level,
  2632. u32 error_code)
  2633. {
  2634. struct kvm_shadow_walk_iterator iterator;
  2635. struct kvm_mmu_page *sp;
  2636. bool fault_handled = false;
  2637. u64 spte = 0ull;
  2638. uint retry_count = 0;
  2639. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2640. return false;
  2641. if (!page_fault_can_be_fast(error_code))
  2642. return false;
  2643. walk_shadow_page_lockless_begin(vcpu);
  2644. do {
  2645. u64 new_spte;
  2646. for_each_shadow_entry_lockless(vcpu, gva, iterator, spte)
  2647. if (!is_shadow_present_pte(spte) ||
  2648. iterator.level < level)
  2649. break;
  2650. sp = page_header(__pa(iterator.sptep));
  2651. if (!is_last_spte(spte, sp->role.level))
  2652. break;
  2653. /*
  2654. * Check whether the memory access that caused the fault would
  2655. * still cause it if it were to be performed right now. If not,
  2656. * then this is a spurious fault caused by TLB lazily flushed,
  2657. * or some other CPU has already fixed the PTE after the
  2658. * current CPU took the fault.
  2659. *
  2660. * Need not check the access of upper level table entries since
  2661. * they are always ACC_ALL.
  2662. */
  2663. if (is_access_allowed(error_code, spte)) {
  2664. fault_handled = true;
  2665. break;
  2666. }
  2667. new_spte = spte;
  2668. if (is_access_track_spte(spte))
  2669. new_spte = restore_acc_track_spte(new_spte);
  2670. /*
  2671. * Currently, to simplify the code, write-protection can
  2672. * be removed in the fast path only if the SPTE was
  2673. * write-protected for dirty-logging or access tracking.
  2674. */
  2675. if ((error_code & PFERR_WRITE_MASK) &&
  2676. spte_can_locklessly_be_made_writable(spte))
  2677. {
  2678. new_spte |= PT_WRITABLE_MASK;
  2679. /*
  2680. * Do not fix write-permission on the large spte. Since
  2681. * we only dirty the first page into the dirty-bitmap in
  2682. * fast_pf_fix_direct_spte(), other pages are missed
  2683. * if its slot has dirty logging enabled.
  2684. *
  2685. * Instead, we let the slow page fault path create a
  2686. * normal spte to fix the access.
  2687. *
  2688. * See the comments in kvm_arch_commit_memory_region().
  2689. */
  2690. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  2691. break;
  2692. }
  2693. /* Verify that the fault can be handled in the fast path */
  2694. if (new_spte == spte ||
  2695. !is_access_allowed(error_code, new_spte))
  2696. break;
  2697. /*
  2698. * Currently, fast page fault only works for direct mapping
  2699. * since the gfn is not stable for indirect shadow page. See
  2700. * Documentation/virtual/kvm/locking.txt to get more detail.
  2701. */
  2702. fault_handled = fast_pf_fix_direct_spte(vcpu, sp,
  2703. iterator.sptep, spte,
  2704. new_spte);
  2705. if (fault_handled)
  2706. break;
  2707. if (++retry_count > 4) {
  2708. printk_once(KERN_WARNING
  2709. "kvm: Fast #PF retrying more than 4 times.\n");
  2710. break;
  2711. }
  2712. } while (true);
  2713. trace_fast_page_fault(vcpu, gva, error_code, iterator.sptep,
  2714. spte, fault_handled);
  2715. walk_shadow_page_lockless_end(vcpu);
  2716. return fault_handled;
  2717. }
  2718. static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
  2719. gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable);
  2720. static int make_mmu_pages_available(struct kvm_vcpu *vcpu);
  2721. static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, u32 error_code,
  2722. gfn_t gfn, bool prefault)
  2723. {
  2724. int r;
  2725. int level;
  2726. bool force_pt_level = false;
  2727. kvm_pfn_t pfn;
  2728. unsigned long mmu_seq;
  2729. bool map_writable, write = error_code & PFERR_WRITE_MASK;
  2730. level = mapping_level(vcpu, gfn, &force_pt_level);
  2731. if (likely(!force_pt_level)) {
  2732. /*
  2733. * This path builds a PAE pagetable - so we can map
  2734. * 2mb pages at maximum. Therefore check if the level
  2735. * is larger than that.
  2736. */
  2737. if (level > PT_DIRECTORY_LEVEL)
  2738. level = PT_DIRECTORY_LEVEL;
  2739. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  2740. }
  2741. if (fast_page_fault(vcpu, v, level, error_code))
  2742. return RET_PF_RETRY;
  2743. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  2744. smp_rmb();
  2745. if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
  2746. return RET_PF_RETRY;
  2747. if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
  2748. return r;
  2749. spin_lock(&vcpu->kvm->mmu_lock);
  2750. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  2751. goto out_unlock;
  2752. if (make_mmu_pages_available(vcpu) < 0)
  2753. goto out_unlock;
  2754. if (likely(!force_pt_level))
  2755. transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
  2756. r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
  2757. spin_unlock(&vcpu->kvm->mmu_lock);
  2758. return r;
  2759. out_unlock:
  2760. spin_unlock(&vcpu->kvm->mmu_lock);
  2761. kvm_release_pfn_clean(pfn);
  2762. return RET_PF_RETRY;
  2763. }
  2764. static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa,
  2765. struct list_head *invalid_list)
  2766. {
  2767. struct kvm_mmu_page *sp;
  2768. if (!VALID_PAGE(*root_hpa))
  2769. return;
  2770. sp = page_header(*root_hpa & PT64_BASE_ADDR_MASK);
  2771. --sp->root_count;
  2772. if (!sp->root_count && sp->role.invalid)
  2773. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  2774. *root_hpa = INVALID_PAGE;
  2775. }
  2776. void kvm_mmu_free_roots(struct kvm_vcpu *vcpu)
  2777. {
  2778. int i;
  2779. LIST_HEAD(invalid_list);
  2780. struct kvm_mmu *mmu = &vcpu->arch.mmu;
  2781. if (!VALID_PAGE(mmu->root_hpa))
  2782. return;
  2783. spin_lock(&vcpu->kvm->mmu_lock);
  2784. if (mmu->shadow_root_level >= PT64_ROOT_4LEVEL &&
  2785. (mmu->root_level >= PT64_ROOT_4LEVEL || mmu->direct_map)) {
  2786. mmu_free_root_page(vcpu->kvm, &mmu->root_hpa, &invalid_list);
  2787. } else {
  2788. for (i = 0; i < 4; ++i)
  2789. if (mmu->pae_root[i] != 0)
  2790. mmu_free_root_page(vcpu->kvm, &mmu->pae_root[i],
  2791. &invalid_list);
  2792. mmu->root_hpa = INVALID_PAGE;
  2793. }
  2794. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  2795. spin_unlock(&vcpu->kvm->mmu_lock);
  2796. }
  2797. EXPORT_SYMBOL_GPL(kvm_mmu_free_roots);
  2798. static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
  2799. {
  2800. int ret = 0;
  2801. if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
  2802. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2803. ret = 1;
  2804. }
  2805. return ret;
  2806. }
  2807. static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
  2808. {
  2809. struct kvm_mmu_page *sp;
  2810. unsigned i;
  2811. if (vcpu->arch.mmu.shadow_root_level >= PT64_ROOT_4LEVEL) {
  2812. spin_lock(&vcpu->kvm->mmu_lock);
  2813. if(make_mmu_pages_available(vcpu) < 0) {
  2814. spin_unlock(&vcpu->kvm->mmu_lock);
  2815. return -ENOSPC;
  2816. }
  2817. sp = kvm_mmu_get_page(vcpu, 0, 0,
  2818. vcpu->arch.mmu.shadow_root_level, 1, ACC_ALL);
  2819. ++sp->root_count;
  2820. spin_unlock(&vcpu->kvm->mmu_lock);
  2821. vcpu->arch.mmu.root_hpa = __pa(sp->spt);
  2822. } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
  2823. for (i = 0; i < 4; ++i) {
  2824. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2825. MMU_WARN_ON(VALID_PAGE(root));
  2826. spin_lock(&vcpu->kvm->mmu_lock);
  2827. if (make_mmu_pages_available(vcpu) < 0) {
  2828. spin_unlock(&vcpu->kvm->mmu_lock);
  2829. return -ENOSPC;
  2830. }
  2831. sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
  2832. i << 30, PT32_ROOT_LEVEL, 1, ACC_ALL);
  2833. root = __pa(sp->spt);
  2834. ++sp->root_count;
  2835. spin_unlock(&vcpu->kvm->mmu_lock);
  2836. vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
  2837. }
  2838. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
  2839. } else
  2840. BUG();
  2841. return 0;
  2842. }
  2843. static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
  2844. {
  2845. struct kvm_mmu_page *sp;
  2846. u64 pdptr, pm_mask;
  2847. gfn_t root_gfn;
  2848. int i;
  2849. root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
  2850. if (mmu_check_root(vcpu, root_gfn))
  2851. return 1;
  2852. /*
  2853. * Do we shadow a long mode page table? If so we need to
  2854. * write-protect the guests page table root.
  2855. */
  2856. if (vcpu->arch.mmu.root_level >= PT64_ROOT_4LEVEL) {
  2857. hpa_t root = vcpu->arch.mmu.root_hpa;
  2858. MMU_WARN_ON(VALID_PAGE(root));
  2859. spin_lock(&vcpu->kvm->mmu_lock);
  2860. if (make_mmu_pages_available(vcpu) < 0) {
  2861. spin_unlock(&vcpu->kvm->mmu_lock);
  2862. return -ENOSPC;
  2863. }
  2864. sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
  2865. vcpu->arch.mmu.shadow_root_level, 0, ACC_ALL);
  2866. root = __pa(sp->spt);
  2867. ++sp->root_count;
  2868. spin_unlock(&vcpu->kvm->mmu_lock);
  2869. vcpu->arch.mmu.root_hpa = root;
  2870. return 0;
  2871. }
  2872. /*
  2873. * We shadow a 32 bit page table. This may be a legacy 2-level
  2874. * or a PAE 3-level page table. In either case we need to be aware that
  2875. * the shadow page table may be a PAE or a long mode page table.
  2876. */
  2877. pm_mask = PT_PRESENT_MASK;
  2878. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_4LEVEL)
  2879. pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
  2880. for (i = 0; i < 4; ++i) {
  2881. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2882. MMU_WARN_ON(VALID_PAGE(root));
  2883. if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
  2884. pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i);
  2885. if (!(pdptr & PT_PRESENT_MASK)) {
  2886. vcpu->arch.mmu.pae_root[i] = 0;
  2887. continue;
  2888. }
  2889. root_gfn = pdptr >> PAGE_SHIFT;
  2890. if (mmu_check_root(vcpu, root_gfn))
  2891. return 1;
  2892. }
  2893. spin_lock(&vcpu->kvm->mmu_lock);
  2894. if (make_mmu_pages_available(vcpu) < 0) {
  2895. spin_unlock(&vcpu->kvm->mmu_lock);
  2896. return -ENOSPC;
  2897. }
  2898. sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, PT32_ROOT_LEVEL,
  2899. 0, ACC_ALL);
  2900. root = __pa(sp->spt);
  2901. ++sp->root_count;
  2902. spin_unlock(&vcpu->kvm->mmu_lock);
  2903. vcpu->arch.mmu.pae_root[i] = root | pm_mask;
  2904. }
  2905. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
  2906. /*
  2907. * If we shadow a 32 bit page table with a long mode page
  2908. * table we enter this path.
  2909. */
  2910. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_4LEVEL) {
  2911. if (vcpu->arch.mmu.lm_root == NULL) {
  2912. /*
  2913. * The additional page necessary for this is only
  2914. * allocated on demand.
  2915. */
  2916. u64 *lm_root;
  2917. lm_root = (void*)get_zeroed_page(GFP_KERNEL);
  2918. if (lm_root == NULL)
  2919. return 1;
  2920. lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
  2921. vcpu->arch.mmu.lm_root = lm_root;
  2922. }
  2923. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
  2924. }
  2925. return 0;
  2926. }
  2927. static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
  2928. {
  2929. if (vcpu->arch.mmu.direct_map)
  2930. return mmu_alloc_direct_roots(vcpu);
  2931. else
  2932. return mmu_alloc_shadow_roots(vcpu);
  2933. }
  2934. static void mmu_sync_roots(struct kvm_vcpu *vcpu)
  2935. {
  2936. int i;
  2937. struct kvm_mmu_page *sp;
  2938. if (vcpu->arch.mmu.direct_map)
  2939. return;
  2940. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2941. return;
  2942. vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
  2943. kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
  2944. if (vcpu->arch.mmu.root_level >= PT64_ROOT_4LEVEL) {
  2945. hpa_t root = vcpu->arch.mmu.root_hpa;
  2946. sp = page_header(root);
  2947. mmu_sync_children(vcpu, sp);
  2948. kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
  2949. return;
  2950. }
  2951. for (i = 0; i < 4; ++i) {
  2952. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2953. if (root && VALID_PAGE(root)) {
  2954. root &= PT64_BASE_ADDR_MASK;
  2955. sp = page_header(root);
  2956. mmu_sync_children(vcpu, sp);
  2957. }
  2958. }
  2959. kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
  2960. }
  2961. void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
  2962. {
  2963. spin_lock(&vcpu->kvm->mmu_lock);
  2964. mmu_sync_roots(vcpu);
  2965. spin_unlock(&vcpu->kvm->mmu_lock);
  2966. }
  2967. EXPORT_SYMBOL_GPL(kvm_mmu_sync_roots);
  2968. static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
  2969. u32 access, struct x86_exception *exception)
  2970. {
  2971. if (exception)
  2972. exception->error_code = 0;
  2973. return vaddr;
  2974. }
  2975. static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
  2976. u32 access,
  2977. struct x86_exception *exception)
  2978. {
  2979. if (exception)
  2980. exception->error_code = 0;
  2981. return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception);
  2982. }
  2983. static bool
  2984. __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
  2985. {
  2986. int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f;
  2987. return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) |
  2988. ((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0);
  2989. }
  2990. static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
  2991. {
  2992. return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level);
  2993. }
  2994. static bool is_shadow_zero_bits_set(struct kvm_mmu *mmu, u64 spte, int level)
  2995. {
  2996. return __is_rsvd_bits_set(&mmu->shadow_zero_check, spte, level);
  2997. }
  2998. static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
  2999. {
  3000. /*
  3001. * A nested guest cannot use the MMIO cache if it is using nested
  3002. * page tables, because cr2 is a nGPA while the cache stores GPAs.
  3003. */
  3004. if (mmu_is_nested(vcpu))
  3005. return false;
  3006. if (direct)
  3007. return vcpu_match_mmio_gpa(vcpu, addr);
  3008. return vcpu_match_mmio_gva(vcpu, addr);
  3009. }
  3010. /* return true if reserved bit is detected on spte. */
  3011. static bool
  3012. walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
  3013. {
  3014. struct kvm_shadow_walk_iterator iterator;
  3015. u64 sptes[PT64_ROOT_MAX_LEVEL], spte = 0ull;
  3016. int root, leaf;
  3017. bool reserved = false;
  3018. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  3019. goto exit;
  3020. walk_shadow_page_lockless_begin(vcpu);
  3021. for (shadow_walk_init(&iterator, vcpu, addr),
  3022. leaf = root = iterator.level;
  3023. shadow_walk_okay(&iterator);
  3024. __shadow_walk_next(&iterator, spte)) {
  3025. spte = mmu_spte_get_lockless(iterator.sptep);
  3026. sptes[leaf - 1] = spte;
  3027. leaf--;
  3028. if (!is_shadow_present_pte(spte))
  3029. break;
  3030. reserved |= is_shadow_zero_bits_set(&vcpu->arch.mmu, spte,
  3031. iterator.level);
  3032. }
  3033. walk_shadow_page_lockless_end(vcpu);
  3034. if (reserved) {
  3035. pr_err("%s: detect reserved bits on spte, addr 0x%llx, dump hierarchy:\n",
  3036. __func__, addr);
  3037. while (root > leaf) {
  3038. pr_err("------ spte 0x%llx level %d.\n",
  3039. sptes[root - 1], root);
  3040. root--;
  3041. }
  3042. }
  3043. exit:
  3044. *sptep = spte;
  3045. return reserved;
  3046. }
  3047. static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
  3048. {
  3049. u64 spte;
  3050. bool reserved;
  3051. if (mmio_info_in_cache(vcpu, addr, direct))
  3052. return RET_PF_EMULATE;
  3053. reserved = walk_shadow_page_get_mmio_spte(vcpu, addr, &spte);
  3054. if (WARN_ON(reserved))
  3055. return -EINVAL;
  3056. if (is_mmio_spte(spte)) {
  3057. gfn_t gfn = get_mmio_spte_gfn(spte);
  3058. unsigned access = get_mmio_spte_access(spte);
  3059. if (!check_mmio_spte(vcpu, spte))
  3060. return RET_PF_INVALID;
  3061. if (direct)
  3062. addr = 0;
  3063. trace_handle_mmio_page_fault(addr, gfn, access);
  3064. vcpu_cache_mmio_info(vcpu, addr, gfn, access);
  3065. return RET_PF_EMULATE;
  3066. }
  3067. /*
  3068. * If the page table is zapped by other cpus, let CPU fault again on
  3069. * the address.
  3070. */
  3071. return RET_PF_RETRY;
  3072. }
  3073. static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
  3074. u32 error_code, gfn_t gfn)
  3075. {
  3076. if (unlikely(error_code & PFERR_RSVD_MASK))
  3077. return false;
  3078. if (!(error_code & PFERR_PRESENT_MASK) ||
  3079. !(error_code & PFERR_WRITE_MASK))
  3080. return false;
  3081. /*
  3082. * guest is writing the page which is write tracked which can
  3083. * not be fixed by page fault handler.
  3084. */
  3085. if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
  3086. return true;
  3087. return false;
  3088. }
  3089. static void shadow_page_table_clear_flood(struct kvm_vcpu *vcpu, gva_t addr)
  3090. {
  3091. struct kvm_shadow_walk_iterator iterator;
  3092. u64 spte;
  3093. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  3094. return;
  3095. walk_shadow_page_lockless_begin(vcpu);
  3096. for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
  3097. clear_sp_write_flooding_count(iterator.sptep);
  3098. if (!is_shadow_present_pte(spte))
  3099. break;
  3100. }
  3101. walk_shadow_page_lockless_end(vcpu);
  3102. }
  3103. static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
  3104. u32 error_code, bool prefault)
  3105. {
  3106. gfn_t gfn = gva >> PAGE_SHIFT;
  3107. int r;
  3108. pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
  3109. if (page_fault_handle_page_track(vcpu, error_code, gfn))
  3110. return RET_PF_EMULATE;
  3111. r = mmu_topup_memory_caches(vcpu);
  3112. if (r)
  3113. return r;
  3114. MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3115. return nonpaging_map(vcpu, gva & PAGE_MASK,
  3116. error_code, gfn, prefault);
  3117. }
  3118. static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
  3119. {
  3120. struct kvm_arch_async_pf arch;
  3121. arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
  3122. arch.gfn = gfn;
  3123. arch.direct_map = vcpu->arch.mmu.direct_map;
  3124. arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
  3125. return kvm_setup_async_pf(vcpu, gva, kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch);
  3126. }
  3127. bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
  3128. {
  3129. if (unlikely(!lapic_in_kernel(vcpu) ||
  3130. kvm_event_needs_reinjection(vcpu) ||
  3131. vcpu->arch.exception.pending))
  3132. return false;
  3133. if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
  3134. return false;
  3135. return kvm_x86_ops->interrupt_allowed(vcpu);
  3136. }
  3137. static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
  3138. gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable)
  3139. {
  3140. struct kvm_memory_slot *slot;
  3141. bool async;
  3142. /*
  3143. * Don't expose private memslots to L2.
  3144. */
  3145. if (is_guest_mode(vcpu) && !kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  3146. *pfn = KVM_PFN_NOSLOT;
  3147. return false;
  3148. }
  3149. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  3150. async = false;
  3151. *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, write, writable);
  3152. if (!async)
  3153. return false; /* *pfn has correct page already */
  3154. if (!prefault && kvm_can_do_async_pf(vcpu)) {
  3155. trace_kvm_try_async_get_page(gva, gfn);
  3156. if (kvm_find_async_pf_gfn(vcpu, gfn)) {
  3157. trace_kvm_async_pf_doublefault(gva, gfn);
  3158. kvm_make_request(KVM_REQ_APF_HALT, vcpu);
  3159. return true;
  3160. } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
  3161. return true;
  3162. }
  3163. *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL, write, writable);
  3164. return false;
  3165. }
  3166. int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
  3167. u64 fault_address, char *insn, int insn_len)
  3168. {
  3169. int r = 1;
  3170. switch (vcpu->arch.apf.host_apf_reason) {
  3171. default:
  3172. trace_kvm_page_fault(fault_address, error_code);
  3173. if (kvm_event_needs_reinjection(vcpu))
  3174. kvm_mmu_unprotect_page_virt(vcpu, fault_address);
  3175. r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
  3176. insn_len);
  3177. break;
  3178. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  3179. vcpu->arch.apf.host_apf_reason = 0;
  3180. local_irq_disable();
  3181. kvm_async_pf_task_wait(fault_address, 0);
  3182. local_irq_enable();
  3183. break;
  3184. case KVM_PV_REASON_PAGE_READY:
  3185. vcpu->arch.apf.host_apf_reason = 0;
  3186. local_irq_disable();
  3187. kvm_async_pf_task_wake(fault_address);
  3188. local_irq_enable();
  3189. break;
  3190. }
  3191. return r;
  3192. }
  3193. EXPORT_SYMBOL_GPL(kvm_handle_page_fault);
  3194. static bool
  3195. check_hugepage_cache_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int level)
  3196. {
  3197. int page_num = KVM_PAGES_PER_HPAGE(level);
  3198. gfn &= ~(page_num - 1);
  3199. return kvm_mtrr_check_gfn_range_consistency(vcpu, gfn, page_num);
  3200. }
  3201. static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
  3202. bool prefault)
  3203. {
  3204. kvm_pfn_t pfn;
  3205. int r;
  3206. int level;
  3207. bool force_pt_level;
  3208. gfn_t gfn = gpa >> PAGE_SHIFT;
  3209. unsigned long mmu_seq;
  3210. int write = error_code & PFERR_WRITE_MASK;
  3211. bool map_writable;
  3212. MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3213. if (page_fault_handle_page_track(vcpu, error_code, gfn))
  3214. return RET_PF_EMULATE;
  3215. r = mmu_topup_memory_caches(vcpu);
  3216. if (r)
  3217. return r;
  3218. force_pt_level = !check_hugepage_cache_consistency(vcpu, gfn,
  3219. PT_DIRECTORY_LEVEL);
  3220. level = mapping_level(vcpu, gfn, &force_pt_level);
  3221. if (likely(!force_pt_level)) {
  3222. if (level > PT_DIRECTORY_LEVEL &&
  3223. !check_hugepage_cache_consistency(vcpu, gfn, level))
  3224. level = PT_DIRECTORY_LEVEL;
  3225. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  3226. }
  3227. if (fast_page_fault(vcpu, gpa, level, error_code))
  3228. return RET_PF_RETRY;
  3229. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  3230. smp_rmb();
  3231. if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
  3232. return RET_PF_RETRY;
  3233. if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
  3234. return r;
  3235. spin_lock(&vcpu->kvm->mmu_lock);
  3236. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  3237. goto out_unlock;
  3238. if (make_mmu_pages_available(vcpu) < 0)
  3239. goto out_unlock;
  3240. if (likely(!force_pt_level))
  3241. transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
  3242. r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
  3243. spin_unlock(&vcpu->kvm->mmu_lock);
  3244. return r;
  3245. out_unlock:
  3246. spin_unlock(&vcpu->kvm->mmu_lock);
  3247. kvm_release_pfn_clean(pfn);
  3248. return RET_PF_RETRY;
  3249. }
  3250. static void nonpaging_init_context(struct kvm_vcpu *vcpu,
  3251. struct kvm_mmu *context)
  3252. {
  3253. context->page_fault = nonpaging_page_fault;
  3254. context->gva_to_gpa = nonpaging_gva_to_gpa;
  3255. context->sync_page = nonpaging_sync_page;
  3256. context->invlpg = nonpaging_invlpg;
  3257. context->update_pte = nonpaging_update_pte;
  3258. context->root_level = 0;
  3259. context->shadow_root_level = PT32E_ROOT_LEVEL;
  3260. context->root_hpa = INVALID_PAGE;
  3261. context->direct_map = true;
  3262. context->nx = false;
  3263. }
  3264. void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu)
  3265. {
  3266. kvm_mmu_free_roots(vcpu);
  3267. }
  3268. static unsigned long get_cr3(struct kvm_vcpu *vcpu)
  3269. {
  3270. return kvm_read_cr3(vcpu);
  3271. }
  3272. static void inject_page_fault(struct kvm_vcpu *vcpu,
  3273. struct x86_exception *fault)
  3274. {
  3275. vcpu->arch.mmu.inject_page_fault(vcpu, fault);
  3276. }
  3277. static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
  3278. unsigned access, int *nr_present)
  3279. {
  3280. if (unlikely(is_mmio_spte(*sptep))) {
  3281. if (gfn != get_mmio_spte_gfn(*sptep)) {
  3282. mmu_spte_clear_no_track(sptep);
  3283. return true;
  3284. }
  3285. (*nr_present)++;
  3286. mark_mmio_spte(vcpu, sptep, gfn, access);
  3287. return true;
  3288. }
  3289. return false;
  3290. }
  3291. static inline bool is_last_gpte(struct kvm_mmu *mmu,
  3292. unsigned level, unsigned gpte)
  3293. {
  3294. /*
  3295. * The RHS has bit 7 set iff level < mmu->last_nonleaf_level.
  3296. * If it is clear, there are no large pages at this level, so clear
  3297. * PT_PAGE_SIZE_MASK in gpte if that is the case.
  3298. */
  3299. gpte &= level - mmu->last_nonleaf_level;
  3300. /*
  3301. * PT_PAGE_TABLE_LEVEL always terminates. The RHS has bit 7 set
  3302. * iff level <= PT_PAGE_TABLE_LEVEL, which for our purpose means
  3303. * level == PT_PAGE_TABLE_LEVEL; set PT_PAGE_SIZE_MASK in gpte then.
  3304. */
  3305. gpte |= level - PT_PAGE_TABLE_LEVEL - 1;
  3306. return gpte & PT_PAGE_SIZE_MASK;
  3307. }
  3308. #define PTTYPE_EPT 18 /* arbitrary */
  3309. #define PTTYPE PTTYPE_EPT
  3310. #include "paging_tmpl.h"
  3311. #undef PTTYPE
  3312. #define PTTYPE 64
  3313. #include "paging_tmpl.h"
  3314. #undef PTTYPE
  3315. #define PTTYPE 32
  3316. #include "paging_tmpl.h"
  3317. #undef PTTYPE
  3318. static void
  3319. __reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
  3320. struct rsvd_bits_validate *rsvd_check,
  3321. int maxphyaddr, int level, bool nx, bool gbpages,
  3322. bool pse, bool amd)
  3323. {
  3324. u64 exb_bit_rsvd = 0;
  3325. u64 gbpages_bit_rsvd = 0;
  3326. u64 nonleaf_bit8_rsvd = 0;
  3327. rsvd_check->bad_mt_xwr = 0;
  3328. if (!nx)
  3329. exb_bit_rsvd = rsvd_bits(63, 63);
  3330. if (!gbpages)
  3331. gbpages_bit_rsvd = rsvd_bits(7, 7);
  3332. /*
  3333. * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
  3334. * leaf entries) on AMD CPUs only.
  3335. */
  3336. if (amd)
  3337. nonleaf_bit8_rsvd = rsvd_bits(8, 8);
  3338. switch (level) {
  3339. case PT32_ROOT_LEVEL:
  3340. /* no rsvd bits for 2 level 4K page table entries */
  3341. rsvd_check->rsvd_bits_mask[0][1] = 0;
  3342. rsvd_check->rsvd_bits_mask[0][0] = 0;
  3343. rsvd_check->rsvd_bits_mask[1][0] =
  3344. rsvd_check->rsvd_bits_mask[0][0];
  3345. if (!pse) {
  3346. rsvd_check->rsvd_bits_mask[1][1] = 0;
  3347. break;
  3348. }
  3349. if (is_cpuid_PSE36())
  3350. /* 36bits PSE 4MB page */
  3351. rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
  3352. else
  3353. /* 32 bits PSE 4MB page */
  3354. rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
  3355. break;
  3356. case PT32E_ROOT_LEVEL:
  3357. rsvd_check->rsvd_bits_mask[0][2] =
  3358. rsvd_bits(maxphyaddr, 63) |
  3359. rsvd_bits(5, 8) | rsvd_bits(1, 2); /* PDPTE */
  3360. rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  3361. rsvd_bits(maxphyaddr, 62); /* PDE */
  3362. rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  3363. rsvd_bits(maxphyaddr, 62); /* PTE */
  3364. rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  3365. rsvd_bits(maxphyaddr, 62) |
  3366. rsvd_bits(13, 20); /* large page */
  3367. rsvd_check->rsvd_bits_mask[1][0] =
  3368. rsvd_check->rsvd_bits_mask[0][0];
  3369. break;
  3370. case PT64_ROOT_5LEVEL:
  3371. rsvd_check->rsvd_bits_mask[0][4] = exb_bit_rsvd |
  3372. nonleaf_bit8_rsvd | rsvd_bits(7, 7) |
  3373. rsvd_bits(maxphyaddr, 51);
  3374. rsvd_check->rsvd_bits_mask[1][4] =
  3375. rsvd_check->rsvd_bits_mask[0][4];
  3376. case PT64_ROOT_4LEVEL:
  3377. rsvd_check->rsvd_bits_mask[0][3] = exb_bit_rsvd |
  3378. nonleaf_bit8_rsvd | rsvd_bits(7, 7) |
  3379. rsvd_bits(maxphyaddr, 51);
  3380. rsvd_check->rsvd_bits_mask[0][2] = exb_bit_rsvd |
  3381. nonleaf_bit8_rsvd | gbpages_bit_rsvd |
  3382. rsvd_bits(maxphyaddr, 51);
  3383. rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  3384. rsvd_bits(maxphyaddr, 51);
  3385. rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  3386. rsvd_bits(maxphyaddr, 51);
  3387. rsvd_check->rsvd_bits_mask[1][3] =
  3388. rsvd_check->rsvd_bits_mask[0][3];
  3389. rsvd_check->rsvd_bits_mask[1][2] = exb_bit_rsvd |
  3390. gbpages_bit_rsvd | rsvd_bits(maxphyaddr, 51) |
  3391. rsvd_bits(13, 29);
  3392. rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  3393. rsvd_bits(maxphyaddr, 51) |
  3394. rsvd_bits(13, 20); /* large page */
  3395. rsvd_check->rsvd_bits_mask[1][0] =
  3396. rsvd_check->rsvd_bits_mask[0][0];
  3397. break;
  3398. }
  3399. }
  3400. static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
  3401. struct kvm_mmu *context)
  3402. {
  3403. __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check,
  3404. cpuid_maxphyaddr(vcpu), context->root_level,
  3405. context->nx,
  3406. guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
  3407. is_pse(vcpu), guest_cpuid_is_amd(vcpu));
  3408. }
  3409. static void
  3410. __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
  3411. int maxphyaddr, bool execonly)
  3412. {
  3413. u64 bad_mt_xwr;
  3414. rsvd_check->rsvd_bits_mask[0][4] =
  3415. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7);
  3416. rsvd_check->rsvd_bits_mask[0][3] =
  3417. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7);
  3418. rsvd_check->rsvd_bits_mask[0][2] =
  3419. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
  3420. rsvd_check->rsvd_bits_mask[0][1] =
  3421. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
  3422. rsvd_check->rsvd_bits_mask[0][0] = rsvd_bits(maxphyaddr, 51);
  3423. /* large page */
  3424. rsvd_check->rsvd_bits_mask[1][4] = rsvd_check->rsvd_bits_mask[0][4];
  3425. rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
  3426. rsvd_check->rsvd_bits_mask[1][2] =
  3427. rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 29);
  3428. rsvd_check->rsvd_bits_mask[1][1] =
  3429. rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 20);
  3430. rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
  3431. bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */
  3432. bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */
  3433. bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */
  3434. bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */
  3435. bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */
  3436. if (!execonly) {
  3437. /* bits 0..2 must not be 100 unless VMX capabilities allow it */
  3438. bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
  3439. }
  3440. rsvd_check->bad_mt_xwr = bad_mt_xwr;
  3441. }
  3442. static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
  3443. struct kvm_mmu *context, bool execonly)
  3444. {
  3445. __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
  3446. cpuid_maxphyaddr(vcpu), execonly);
  3447. }
  3448. /*
  3449. * the page table on host is the shadow page table for the page
  3450. * table in guest or amd nested guest, its mmu features completely
  3451. * follow the features in guest.
  3452. */
  3453. void
  3454. reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
  3455. {
  3456. bool uses_nx = context->nx || context->base_role.smep_andnot_wp;
  3457. struct rsvd_bits_validate *shadow_zero_check;
  3458. int i;
  3459. /*
  3460. * Passing "true" to the last argument is okay; it adds a check
  3461. * on bit 8 of the SPTEs which KVM doesn't use anyway.
  3462. */
  3463. shadow_zero_check = &context->shadow_zero_check;
  3464. __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
  3465. boot_cpu_data.x86_phys_bits,
  3466. context->shadow_root_level, uses_nx,
  3467. guest_cpuid_has(vcpu, X86_FEATURE_GBPAGES),
  3468. is_pse(vcpu), true);
  3469. if (!shadow_me_mask)
  3470. return;
  3471. for (i = context->shadow_root_level; --i >= 0;) {
  3472. shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
  3473. shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
  3474. }
  3475. }
  3476. EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask);
  3477. static inline bool boot_cpu_is_amd(void)
  3478. {
  3479. WARN_ON_ONCE(!tdp_enabled);
  3480. return shadow_x_mask == 0;
  3481. }
  3482. /*
  3483. * the direct page table on host, use as much mmu features as
  3484. * possible, however, kvm currently does not do execution-protection.
  3485. */
  3486. static void
  3487. reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
  3488. struct kvm_mmu *context)
  3489. {
  3490. struct rsvd_bits_validate *shadow_zero_check;
  3491. int i;
  3492. shadow_zero_check = &context->shadow_zero_check;
  3493. if (boot_cpu_is_amd())
  3494. __reset_rsvds_bits_mask(vcpu, shadow_zero_check,
  3495. boot_cpu_data.x86_phys_bits,
  3496. context->shadow_root_level, false,
  3497. boot_cpu_has(X86_FEATURE_GBPAGES),
  3498. true, true);
  3499. else
  3500. __reset_rsvds_bits_mask_ept(shadow_zero_check,
  3501. boot_cpu_data.x86_phys_bits,
  3502. false);
  3503. if (!shadow_me_mask)
  3504. return;
  3505. for (i = context->shadow_root_level; --i >= 0;) {
  3506. shadow_zero_check->rsvd_bits_mask[0][i] &= ~shadow_me_mask;
  3507. shadow_zero_check->rsvd_bits_mask[1][i] &= ~shadow_me_mask;
  3508. }
  3509. }
  3510. /*
  3511. * as the comments in reset_shadow_zero_bits_mask() except it
  3512. * is the shadow page table for intel nested guest.
  3513. */
  3514. static void
  3515. reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
  3516. struct kvm_mmu *context, bool execonly)
  3517. {
  3518. __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
  3519. boot_cpu_data.x86_phys_bits, execonly);
  3520. }
  3521. #define BYTE_MASK(access) \
  3522. ((1 & (access) ? 2 : 0) | \
  3523. (2 & (access) ? 4 : 0) | \
  3524. (3 & (access) ? 8 : 0) | \
  3525. (4 & (access) ? 16 : 0) | \
  3526. (5 & (access) ? 32 : 0) | \
  3527. (6 & (access) ? 64 : 0) | \
  3528. (7 & (access) ? 128 : 0))
  3529. static void update_permission_bitmask(struct kvm_vcpu *vcpu,
  3530. struct kvm_mmu *mmu, bool ept)
  3531. {
  3532. unsigned byte;
  3533. const u8 x = BYTE_MASK(ACC_EXEC_MASK);
  3534. const u8 w = BYTE_MASK(ACC_WRITE_MASK);
  3535. const u8 u = BYTE_MASK(ACC_USER_MASK);
  3536. bool cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP) != 0;
  3537. bool cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP) != 0;
  3538. bool cr0_wp = is_write_protection(vcpu);
  3539. for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
  3540. unsigned pfec = byte << 1;
  3541. /*
  3542. * Each "*f" variable has a 1 bit for each UWX value
  3543. * that causes a fault with the given PFEC.
  3544. */
  3545. /* Faults from writes to non-writable pages */
  3546. u8 wf = (pfec & PFERR_WRITE_MASK) ? ~w : 0;
  3547. /* Faults from user mode accesses to supervisor pages */
  3548. u8 uf = (pfec & PFERR_USER_MASK) ? ~u : 0;
  3549. /* Faults from fetches of non-executable pages*/
  3550. u8 ff = (pfec & PFERR_FETCH_MASK) ? ~x : 0;
  3551. /* Faults from kernel mode fetches of user pages */
  3552. u8 smepf = 0;
  3553. /* Faults from kernel mode accesses of user pages */
  3554. u8 smapf = 0;
  3555. if (!ept) {
  3556. /* Faults from kernel mode accesses to user pages */
  3557. u8 kf = (pfec & PFERR_USER_MASK) ? 0 : u;
  3558. /* Not really needed: !nx will cause pte.nx to fault */
  3559. if (!mmu->nx)
  3560. ff = 0;
  3561. /* Allow supervisor writes if !cr0.wp */
  3562. if (!cr0_wp)
  3563. wf = (pfec & PFERR_USER_MASK) ? wf : 0;
  3564. /* Disallow supervisor fetches of user code if cr4.smep */
  3565. if (cr4_smep)
  3566. smepf = (pfec & PFERR_FETCH_MASK) ? kf : 0;
  3567. /*
  3568. * SMAP:kernel-mode data accesses from user-mode
  3569. * mappings should fault. A fault is considered
  3570. * as a SMAP violation if all of the following
  3571. * conditions are ture:
  3572. * - X86_CR4_SMAP is set in CR4
  3573. * - A user page is accessed
  3574. * - The access is not a fetch
  3575. * - Page fault in kernel mode
  3576. * - if CPL = 3 or X86_EFLAGS_AC is clear
  3577. *
  3578. * Here, we cover the first three conditions.
  3579. * The fourth is computed dynamically in permission_fault();
  3580. * PFERR_RSVD_MASK bit will be set in PFEC if the access is
  3581. * *not* subject to SMAP restrictions.
  3582. */
  3583. if (cr4_smap)
  3584. smapf = (pfec & (PFERR_RSVD_MASK|PFERR_FETCH_MASK)) ? 0 : kf;
  3585. }
  3586. mmu->permissions[byte] = ff | uf | wf | smepf | smapf;
  3587. }
  3588. }
  3589. /*
  3590. * PKU is an additional mechanism by which the paging controls access to
  3591. * user-mode addresses based on the value in the PKRU register. Protection
  3592. * key violations are reported through a bit in the page fault error code.
  3593. * Unlike other bits of the error code, the PK bit is not known at the
  3594. * call site of e.g. gva_to_gpa; it must be computed directly in
  3595. * permission_fault based on two bits of PKRU, on some machine state (CR4,
  3596. * CR0, EFER, CPL), and on other bits of the error code and the page tables.
  3597. *
  3598. * In particular the following conditions come from the error code, the
  3599. * page tables and the machine state:
  3600. * - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
  3601. * - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
  3602. * - PK is always zero if U=0 in the page tables
  3603. * - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
  3604. *
  3605. * The PKRU bitmask caches the result of these four conditions. The error
  3606. * code (minus the P bit) and the page table's U bit form an index into the
  3607. * PKRU bitmask. Two bits of the PKRU bitmask are then extracted and ANDed
  3608. * with the two bits of the PKRU register corresponding to the protection key.
  3609. * For the first three conditions above the bits will be 00, thus masking
  3610. * away both AD and WD. For all reads or if the last condition holds, WD
  3611. * only will be masked away.
  3612. */
  3613. static void update_pkru_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  3614. bool ept)
  3615. {
  3616. unsigned bit;
  3617. bool wp;
  3618. if (ept) {
  3619. mmu->pkru_mask = 0;
  3620. return;
  3621. }
  3622. /* PKEY is enabled only if CR4.PKE and EFER.LMA are both set. */
  3623. if (!kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || !is_long_mode(vcpu)) {
  3624. mmu->pkru_mask = 0;
  3625. return;
  3626. }
  3627. wp = is_write_protection(vcpu);
  3628. for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) {
  3629. unsigned pfec, pkey_bits;
  3630. bool check_pkey, check_write, ff, uf, wf, pte_user;
  3631. pfec = bit << 1;
  3632. ff = pfec & PFERR_FETCH_MASK;
  3633. uf = pfec & PFERR_USER_MASK;
  3634. wf = pfec & PFERR_WRITE_MASK;
  3635. /* PFEC.RSVD is replaced by ACC_USER_MASK. */
  3636. pte_user = pfec & PFERR_RSVD_MASK;
  3637. /*
  3638. * Only need to check the access which is not an
  3639. * instruction fetch and is to a user page.
  3640. */
  3641. check_pkey = (!ff && pte_user);
  3642. /*
  3643. * write access is controlled by PKRU if it is a
  3644. * user access or CR0.WP = 1.
  3645. */
  3646. check_write = check_pkey && wf && (uf || wp);
  3647. /* PKRU.AD stops both read and write access. */
  3648. pkey_bits = !!check_pkey;
  3649. /* PKRU.WD stops write access. */
  3650. pkey_bits |= (!!check_write) << 1;
  3651. mmu->pkru_mask |= (pkey_bits & 3) << pfec;
  3652. }
  3653. }
  3654. static void update_last_nonleaf_level(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
  3655. {
  3656. unsigned root_level = mmu->root_level;
  3657. mmu->last_nonleaf_level = root_level;
  3658. if (root_level == PT32_ROOT_LEVEL && is_pse(vcpu))
  3659. mmu->last_nonleaf_level++;
  3660. }
  3661. static void paging64_init_context_common(struct kvm_vcpu *vcpu,
  3662. struct kvm_mmu *context,
  3663. int level)
  3664. {
  3665. context->nx = is_nx(vcpu);
  3666. context->root_level = level;
  3667. reset_rsvds_bits_mask(vcpu, context);
  3668. update_permission_bitmask(vcpu, context, false);
  3669. update_pkru_bitmask(vcpu, context, false);
  3670. update_last_nonleaf_level(vcpu, context);
  3671. MMU_WARN_ON(!is_pae(vcpu));
  3672. context->page_fault = paging64_page_fault;
  3673. context->gva_to_gpa = paging64_gva_to_gpa;
  3674. context->sync_page = paging64_sync_page;
  3675. context->invlpg = paging64_invlpg;
  3676. context->update_pte = paging64_update_pte;
  3677. context->shadow_root_level = level;
  3678. context->root_hpa = INVALID_PAGE;
  3679. context->direct_map = false;
  3680. }
  3681. static void paging64_init_context(struct kvm_vcpu *vcpu,
  3682. struct kvm_mmu *context)
  3683. {
  3684. int root_level = is_la57_mode(vcpu) ?
  3685. PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
  3686. paging64_init_context_common(vcpu, context, root_level);
  3687. }
  3688. static void paging32_init_context(struct kvm_vcpu *vcpu,
  3689. struct kvm_mmu *context)
  3690. {
  3691. context->nx = false;
  3692. context->root_level = PT32_ROOT_LEVEL;
  3693. reset_rsvds_bits_mask(vcpu, context);
  3694. update_permission_bitmask(vcpu, context, false);
  3695. update_pkru_bitmask(vcpu, context, false);
  3696. update_last_nonleaf_level(vcpu, context);
  3697. context->page_fault = paging32_page_fault;
  3698. context->gva_to_gpa = paging32_gva_to_gpa;
  3699. context->sync_page = paging32_sync_page;
  3700. context->invlpg = paging32_invlpg;
  3701. context->update_pte = paging32_update_pte;
  3702. context->shadow_root_level = PT32E_ROOT_LEVEL;
  3703. context->root_hpa = INVALID_PAGE;
  3704. context->direct_map = false;
  3705. }
  3706. static void paging32E_init_context(struct kvm_vcpu *vcpu,
  3707. struct kvm_mmu *context)
  3708. {
  3709. paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
  3710. }
  3711. static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
  3712. {
  3713. struct kvm_mmu *context = &vcpu->arch.mmu;
  3714. context->base_role.word = 0;
  3715. context->base_role.guest_mode = is_guest_mode(vcpu);
  3716. context->base_role.smm = is_smm(vcpu);
  3717. context->base_role.ad_disabled = (shadow_accessed_mask == 0);
  3718. context->page_fault = tdp_page_fault;
  3719. context->sync_page = nonpaging_sync_page;
  3720. context->invlpg = nonpaging_invlpg;
  3721. context->update_pte = nonpaging_update_pte;
  3722. context->shadow_root_level = kvm_x86_ops->get_tdp_level(vcpu);
  3723. context->root_hpa = INVALID_PAGE;
  3724. context->direct_map = true;
  3725. context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
  3726. context->get_cr3 = get_cr3;
  3727. context->get_pdptr = kvm_pdptr_read;
  3728. context->inject_page_fault = kvm_inject_page_fault;
  3729. if (!is_paging(vcpu)) {
  3730. context->nx = false;
  3731. context->gva_to_gpa = nonpaging_gva_to_gpa;
  3732. context->root_level = 0;
  3733. } else if (is_long_mode(vcpu)) {
  3734. context->nx = is_nx(vcpu);
  3735. context->root_level = is_la57_mode(vcpu) ?
  3736. PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
  3737. reset_rsvds_bits_mask(vcpu, context);
  3738. context->gva_to_gpa = paging64_gva_to_gpa;
  3739. } else if (is_pae(vcpu)) {
  3740. context->nx = is_nx(vcpu);
  3741. context->root_level = PT32E_ROOT_LEVEL;
  3742. reset_rsvds_bits_mask(vcpu, context);
  3743. context->gva_to_gpa = paging64_gva_to_gpa;
  3744. } else {
  3745. context->nx = false;
  3746. context->root_level = PT32_ROOT_LEVEL;
  3747. reset_rsvds_bits_mask(vcpu, context);
  3748. context->gva_to_gpa = paging32_gva_to_gpa;
  3749. }
  3750. update_permission_bitmask(vcpu, context, false);
  3751. update_pkru_bitmask(vcpu, context, false);
  3752. update_last_nonleaf_level(vcpu, context);
  3753. reset_tdp_shadow_zero_bits_mask(vcpu, context);
  3754. }
  3755. void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)
  3756. {
  3757. bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
  3758. bool smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
  3759. struct kvm_mmu *context = &vcpu->arch.mmu;
  3760. MMU_WARN_ON(VALID_PAGE(context->root_hpa));
  3761. if (!is_paging(vcpu))
  3762. nonpaging_init_context(vcpu, context);
  3763. else if (is_long_mode(vcpu))
  3764. paging64_init_context(vcpu, context);
  3765. else if (is_pae(vcpu))
  3766. paging32E_init_context(vcpu, context);
  3767. else
  3768. paging32_init_context(vcpu, context);
  3769. context->base_role.nxe = is_nx(vcpu);
  3770. context->base_role.cr4_pae = !!is_pae(vcpu);
  3771. context->base_role.cr0_wp = is_write_protection(vcpu);
  3772. context->base_role.smep_andnot_wp
  3773. = smep && !is_write_protection(vcpu);
  3774. context->base_role.smap_andnot_wp
  3775. = smap && !is_write_protection(vcpu);
  3776. context->base_role.guest_mode = is_guest_mode(vcpu);
  3777. context->base_role.smm = is_smm(vcpu);
  3778. reset_shadow_zero_bits_mask(vcpu, context);
  3779. }
  3780. EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
  3781. void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
  3782. bool accessed_dirty)
  3783. {
  3784. struct kvm_mmu *context = &vcpu->arch.mmu;
  3785. MMU_WARN_ON(VALID_PAGE(context->root_hpa));
  3786. context->shadow_root_level = PT64_ROOT_4LEVEL;
  3787. context->nx = true;
  3788. context->ept_ad = accessed_dirty;
  3789. context->page_fault = ept_page_fault;
  3790. context->gva_to_gpa = ept_gva_to_gpa;
  3791. context->sync_page = ept_sync_page;
  3792. context->invlpg = ept_invlpg;
  3793. context->update_pte = ept_update_pte;
  3794. context->root_level = PT64_ROOT_4LEVEL;
  3795. context->root_hpa = INVALID_PAGE;
  3796. context->direct_map = false;
  3797. context->base_role.ad_disabled = !accessed_dirty;
  3798. context->base_role.guest_mode = 1;
  3799. update_permission_bitmask(vcpu, context, true);
  3800. update_pkru_bitmask(vcpu, context, true);
  3801. update_last_nonleaf_level(vcpu, context);
  3802. reset_rsvds_bits_mask_ept(vcpu, context, execonly);
  3803. reset_ept_shadow_zero_bits_mask(vcpu, context, execonly);
  3804. }
  3805. EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
  3806. static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
  3807. {
  3808. struct kvm_mmu *context = &vcpu->arch.mmu;
  3809. kvm_init_shadow_mmu(vcpu);
  3810. context->set_cr3 = kvm_x86_ops->set_cr3;
  3811. context->get_cr3 = get_cr3;
  3812. context->get_pdptr = kvm_pdptr_read;
  3813. context->inject_page_fault = kvm_inject_page_fault;
  3814. }
  3815. static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
  3816. {
  3817. struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
  3818. g_context->get_cr3 = get_cr3;
  3819. g_context->get_pdptr = kvm_pdptr_read;
  3820. g_context->inject_page_fault = kvm_inject_page_fault;
  3821. /*
  3822. * Note that arch.mmu.gva_to_gpa translates l2_gpa to l1_gpa using
  3823. * L1's nested page tables (e.g. EPT12). The nested translation
  3824. * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
  3825. * L2's page tables as the first level of translation and L1's
  3826. * nested page tables as the second level of translation. Basically
  3827. * the gva_to_gpa functions between mmu and nested_mmu are swapped.
  3828. */
  3829. if (!is_paging(vcpu)) {
  3830. g_context->nx = false;
  3831. g_context->root_level = 0;
  3832. g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
  3833. } else if (is_long_mode(vcpu)) {
  3834. g_context->nx = is_nx(vcpu);
  3835. g_context->root_level = is_la57_mode(vcpu) ?
  3836. PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
  3837. reset_rsvds_bits_mask(vcpu, g_context);
  3838. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  3839. } else if (is_pae(vcpu)) {
  3840. g_context->nx = is_nx(vcpu);
  3841. g_context->root_level = PT32E_ROOT_LEVEL;
  3842. reset_rsvds_bits_mask(vcpu, g_context);
  3843. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  3844. } else {
  3845. g_context->nx = false;
  3846. g_context->root_level = PT32_ROOT_LEVEL;
  3847. reset_rsvds_bits_mask(vcpu, g_context);
  3848. g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
  3849. }
  3850. update_permission_bitmask(vcpu, g_context, false);
  3851. update_pkru_bitmask(vcpu, g_context, false);
  3852. update_last_nonleaf_level(vcpu, g_context);
  3853. }
  3854. static void init_kvm_mmu(struct kvm_vcpu *vcpu)
  3855. {
  3856. if (mmu_is_nested(vcpu))
  3857. init_kvm_nested_mmu(vcpu);
  3858. else if (tdp_enabled)
  3859. init_kvm_tdp_mmu(vcpu);
  3860. else
  3861. init_kvm_softmmu(vcpu);
  3862. }
  3863. void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
  3864. {
  3865. kvm_mmu_unload(vcpu);
  3866. init_kvm_mmu(vcpu);
  3867. }
  3868. EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
  3869. int kvm_mmu_load(struct kvm_vcpu *vcpu)
  3870. {
  3871. int r;
  3872. r = mmu_topup_memory_caches(vcpu);
  3873. if (r)
  3874. goto out;
  3875. r = mmu_alloc_roots(vcpu);
  3876. kvm_mmu_sync_roots(vcpu);
  3877. if (r)
  3878. goto out;
  3879. /* set_cr3() should ensure TLB has been flushed */
  3880. vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
  3881. out:
  3882. return r;
  3883. }
  3884. EXPORT_SYMBOL_GPL(kvm_mmu_load);
  3885. void kvm_mmu_unload(struct kvm_vcpu *vcpu)
  3886. {
  3887. kvm_mmu_free_roots(vcpu);
  3888. WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3889. }
  3890. EXPORT_SYMBOL_GPL(kvm_mmu_unload);
  3891. static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
  3892. struct kvm_mmu_page *sp, u64 *spte,
  3893. const void *new)
  3894. {
  3895. if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
  3896. ++vcpu->kvm->stat.mmu_pde_zapped;
  3897. return;
  3898. }
  3899. ++vcpu->kvm->stat.mmu_pte_updated;
  3900. vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
  3901. }
  3902. static bool need_remote_flush(u64 old, u64 new)
  3903. {
  3904. if (!is_shadow_present_pte(old))
  3905. return false;
  3906. if (!is_shadow_present_pte(new))
  3907. return true;
  3908. if ((old ^ new) & PT64_BASE_ADDR_MASK)
  3909. return true;
  3910. old ^= shadow_nx_mask;
  3911. new ^= shadow_nx_mask;
  3912. return (old & ~new & PT64_PERM_MASK) != 0;
  3913. }
  3914. static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
  3915. const u8 *new, int *bytes)
  3916. {
  3917. u64 gentry;
  3918. int r;
  3919. /*
  3920. * Assume that the pte write on a page table of the same type
  3921. * as the current vcpu paging mode since we update the sptes only
  3922. * when they have the same mode.
  3923. */
  3924. if (is_pae(vcpu) && *bytes == 4) {
  3925. /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
  3926. *gpa &= ~(gpa_t)7;
  3927. *bytes = 8;
  3928. r = kvm_vcpu_read_guest(vcpu, *gpa, &gentry, 8);
  3929. if (r)
  3930. gentry = 0;
  3931. new = (const u8 *)&gentry;
  3932. }
  3933. switch (*bytes) {
  3934. case 4:
  3935. gentry = *(const u32 *)new;
  3936. break;
  3937. case 8:
  3938. gentry = *(const u64 *)new;
  3939. break;
  3940. default:
  3941. gentry = 0;
  3942. break;
  3943. }
  3944. return gentry;
  3945. }
  3946. /*
  3947. * If we're seeing too many writes to a page, it may no longer be a page table,
  3948. * or we may be forking, in which case it is better to unmap the page.
  3949. */
  3950. static bool detect_write_flooding(struct kvm_mmu_page *sp)
  3951. {
  3952. /*
  3953. * Skip write-flooding detected for the sp whose level is 1, because
  3954. * it can become unsync, then the guest page is not write-protected.
  3955. */
  3956. if (sp->role.level == PT_PAGE_TABLE_LEVEL)
  3957. return false;
  3958. atomic_inc(&sp->write_flooding_count);
  3959. return atomic_read(&sp->write_flooding_count) >= 3;
  3960. }
  3961. /*
  3962. * Misaligned accesses are too much trouble to fix up; also, they usually
  3963. * indicate a page is not used as a page table.
  3964. */
  3965. static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
  3966. int bytes)
  3967. {
  3968. unsigned offset, pte_size, misaligned;
  3969. pgprintk("misaligned: gpa %llx bytes %d role %x\n",
  3970. gpa, bytes, sp->role.word);
  3971. offset = offset_in_page(gpa);
  3972. pte_size = sp->role.cr4_pae ? 8 : 4;
  3973. /*
  3974. * Sometimes, the OS only writes the last one bytes to update status
  3975. * bits, for example, in linux, andb instruction is used in clear_bit().
  3976. */
  3977. if (!(offset & (pte_size - 1)) && bytes == 1)
  3978. return false;
  3979. misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
  3980. misaligned |= bytes < 4;
  3981. return misaligned;
  3982. }
  3983. static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
  3984. {
  3985. unsigned page_offset, quadrant;
  3986. u64 *spte;
  3987. int level;
  3988. page_offset = offset_in_page(gpa);
  3989. level = sp->role.level;
  3990. *nspte = 1;
  3991. if (!sp->role.cr4_pae) {
  3992. page_offset <<= 1; /* 32->64 */
  3993. /*
  3994. * A 32-bit pde maps 4MB while the shadow pdes map
  3995. * only 2MB. So we need to double the offset again
  3996. * and zap two pdes instead of one.
  3997. */
  3998. if (level == PT32_ROOT_LEVEL) {
  3999. page_offset &= ~7; /* kill rounding error */
  4000. page_offset <<= 1;
  4001. *nspte = 2;
  4002. }
  4003. quadrant = page_offset >> PAGE_SHIFT;
  4004. page_offset &= ~PAGE_MASK;
  4005. if (quadrant != sp->role.quadrant)
  4006. return NULL;
  4007. }
  4008. spte = &sp->spt[page_offset / sizeof(*spte)];
  4009. return spte;
  4010. }
  4011. static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  4012. const u8 *new, int bytes,
  4013. struct kvm_page_track_notifier_node *node)
  4014. {
  4015. gfn_t gfn = gpa >> PAGE_SHIFT;
  4016. struct kvm_mmu_page *sp;
  4017. LIST_HEAD(invalid_list);
  4018. u64 entry, gentry, *spte;
  4019. int npte;
  4020. bool remote_flush, local_flush;
  4021. union kvm_mmu_page_role mask = { };
  4022. mask.cr0_wp = 1;
  4023. mask.cr4_pae = 1;
  4024. mask.nxe = 1;
  4025. mask.smep_andnot_wp = 1;
  4026. mask.smap_andnot_wp = 1;
  4027. mask.smm = 1;
  4028. mask.guest_mode = 1;
  4029. mask.ad_disabled = 1;
  4030. /*
  4031. * If we don't have indirect shadow pages, it means no page is
  4032. * write-protected, so we can exit simply.
  4033. */
  4034. if (!READ_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
  4035. return;
  4036. remote_flush = local_flush = false;
  4037. pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
  4038. gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes);
  4039. /*
  4040. * No need to care whether allocation memory is successful
  4041. * or not since pte prefetch is skiped if it does not have
  4042. * enough objects in the cache.
  4043. */
  4044. mmu_topup_memory_caches(vcpu);
  4045. spin_lock(&vcpu->kvm->mmu_lock);
  4046. ++vcpu->kvm->stat.mmu_pte_write;
  4047. kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
  4048. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
  4049. if (detect_write_misaligned(sp, gpa, bytes) ||
  4050. detect_write_flooding(sp)) {
  4051. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
  4052. ++vcpu->kvm->stat.mmu_flooded;
  4053. continue;
  4054. }
  4055. spte = get_written_sptes(sp, gpa, &npte);
  4056. if (!spte)
  4057. continue;
  4058. local_flush = true;
  4059. while (npte--) {
  4060. entry = *spte;
  4061. mmu_page_zap_pte(vcpu->kvm, sp, spte);
  4062. if (gentry &&
  4063. !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
  4064. & mask.word) && rmap_can_add(vcpu))
  4065. mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
  4066. if (need_remote_flush(entry, *spte))
  4067. remote_flush = true;
  4068. ++spte;
  4069. }
  4070. }
  4071. kvm_mmu_flush_or_zap(vcpu, &invalid_list, remote_flush, local_flush);
  4072. kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
  4073. spin_unlock(&vcpu->kvm->mmu_lock);
  4074. }
  4075. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
  4076. {
  4077. gpa_t gpa;
  4078. int r;
  4079. if (vcpu->arch.mmu.direct_map)
  4080. return 0;
  4081. gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
  4082. r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  4083. return r;
  4084. }
  4085. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
  4086. static int make_mmu_pages_available(struct kvm_vcpu *vcpu)
  4087. {
  4088. LIST_HEAD(invalid_list);
  4089. if (likely(kvm_mmu_available_pages(vcpu->kvm) >= KVM_MIN_FREE_MMU_PAGES))
  4090. return 0;
  4091. while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES) {
  4092. if (!prepare_zap_oldest_mmu_page(vcpu->kvm, &invalid_list))
  4093. break;
  4094. ++vcpu->kvm->stat.mmu_recycled;
  4095. }
  4096. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  4097. if (!kvm_mmu_available_pages(vcpu->kvm))
  4098. return -ENOSPC;
  4099. return 0;
  4100. }
  4101. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,
  4102. void *insn, int insn_len)
  4103. {
  4104. int r, emulation_type = EMULTYPE_RETRY;
  4105. enum emulation_result er;
  4106. bool direct = vcpu->arch.mmu.direct_map;
  4107. /* With shadow page tables, fault_address contains a GVA or nGPA. */
  4108. if (vcpu->arch.mmu.direct_map) {
  4109. vcpu->arch.gpa_available = true;
  4110. vcpu->arch.gpa_val = cr2;
  4111. }
  4112. r = RET_PF_INVALID;
  4113. if (unlikely(error_code & PFERR_RSVD_MASK)) {
  4114. r = handle_mmio_page_fault(vcpu, cr2, direct);
  4115. if (r == RET_PF_EMULATE) {
  4116. emulation_type = 0;
  4117. goto emulate;
  4118. }
  4119. }
  4120. if (r == RET_PF_INVALID) {
  4121. r = vcpu->arch.mmu.page_fault(vcpu, cr2, lower_32_bits(error_code),
  4122. false);
  4123. WARN_ON(r == RET_PF_INVALID);
  4124. }
  4125. if (r == RET_PF_RETRY)
  4126. return 1;
  4127. if (r < 0)
  4128. return r;
  4129. /*
  4130. * Before emulating the instruction, check if the error code
  4131. * was due to a RO violation while translating the guest page.
  4132. * This can occur when using nested virtualization with nested
  4133. * paging in both guests. If true, we simply unprotect the page
  4134. * and resume the guest.
  4135. */
  4136. if (vcpu->arch.mmu.direct_map &&
  4137. (error_code & PFERR_NESTED_GUEST_PAGE) == PFERR_NESTED_GUEST_PAGE) {
  4138. kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(cr2));
  4139. return 1;
  4140. }
  4141. if (mmio_info_in_cache(vcpu, cr2, direct))
  4142. emulation_type = 0;
  4143. emulate:
  4144. /*
  4145. * On AMD platforms, under certain conditions insn_len may be zero on #NPF.
  4146. * This can happen if a guest gets a page-fault on data access but the HW
  4147. * table walker is not able to read the instruction page (e.g instruction
  4148. * page is not present in memory). In those cases we simply restart the
  4149. * guest.
  4150. */
  4151. if (unlikely(insn && !insn_len))
  4152. return 1;
  4153. er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
  4154. switch (er) {
  4155. case EMULATE_DONE:
  4156. return 1;
  4157. case EMULATE_USER_EXIT:
  4158. ++vcpu->stat.mmio_exits;
  4159. /* fall through */
  4160. case EMULATE_FAIL:
  4161. return 0;
  4162. default:
  4163. BUG();
  4164. }
  4165. }
  4166. EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
  4167. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  4168. {
  4169. vcpu->arch.mmu.invlpg(vcpu, gva);
  4170. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  4171. ++vcpu->stat.invlpg;
  4172. }
  4173. EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
  4174. void kvm_enable_tdp(void)
  4175. {
  4176. tdp_enabled = true;
  4177. }
  4178. EXPORT_SYMBOL_GPL(kvm_enable_tdp);
  4179. void kvm_disable_tdp(void)
  4180. {
  4181. tdp_enabled = false;
  4182. }
  4183. EXPORT_SYMBOL_GPL(kvm_disable_tdp);
  4184. static void free_mmu_pages(struct kvm_vcpu *vcpu)
  4185. {
  4186. free_page((unsigned long)vcpu->arch.mmu.pae_root);
  4187. free_page((unsigned long)vcpu->arch.mmu.lm_root);
  4188. }
  4189. static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
  4190. {
  4191. struct page *page;
  4192. int i;
  4193. /*
  4194. * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
  4195. * Therefore we need to allocate shadow page tables in the first
  4196. * 4GB of memory, which happens to fit the DMA32 zone.
  4197. */
  4198. page = alloc_page(GFP_KERNEL | __GFP_DMA32);
  4199. if (!page)
  4200. return -ENOMEM;
  4201. vcpu->arch.mmu.pae_root = page_address(page);
  4202. for (i = 0; i < 4; ++i)
  4203. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  4204. return 0;
  4205. }
  4206. int kvm_mmu_create(struct kvm_vcpu *vcpu)
  4207. {
  4208. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  4209. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  4210. vcpu->arch.mmu.translate_gpa = translate_gpa;
  4211. vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
  4212. return alloc_mmu_pages(vcpu);
  4213. }
  4214. void kvm_mmu_setup(struct kvm_vcpu *vcpu)
  4215. {
  4216. MMU_WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  4217. init_kvm_mmu(vcpu);
  4218. }
  4219. static void kvm_mmu_invalidate_zap_pages_in_memslot(struct kvm *kvm,
  4220. struct kvm_memory_slot *slot,
  4221. struct kvm_page_track_notifier_node *node)
  4222. {
  4223. kvm_mmu_invalidate_zap_all_pages(kvm);
  4224. }
  4225. void kvm_mmu_init_vm(struct kvm *kvm)
  4226. {
  4227. struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
  4228. node->track_write = kvm_mmu_pte_write;
  4229. node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot;
  4230. kvm_page_track_register_notifier(kvm, node);
  4231. }
  4232. void kvm_mmu_uninit_vm(struct kvm *kvm)
  4233. {
  4234. struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
  4235. kvm_page_track_unregister_notifier(kvm, node);
  4236. }
  4237. /* The return value indicates if tlb flush on all vcpus is needed. */
  4238. typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head);
  4239. /* The caller should hold mmu-lock before calling this function. */
  4240. static __always_inline bool
  4241. slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot,
  4242. slot_level_handler fn, int start_level, int end_level,
  4243. gfn_t start_gfn, gfn_t end_gfn, bool lock_flush_tlb)
  4244. {
  4245. struct slot_rmap_walk_iterator iterator;
  4246. bool flush = false;
  4247. for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn,
  4248. end_gfn, &iterator) {
  4249. if (iterator.rmap)
  4250. flush |= fn(kvm, iterator.rmap);
  4251. if (need_resched() || spin_needbreak(&kvm->mmu_lock)) {
  4252. if (flush && lock_flush_tlb) {
  4253. kvm_flush_remote_tlbs(kvm);
  4254. flush = false;
  4255. }
  4256. cond_resched_lock(&kvm->mmu_lock);
  4257. }
  4258. }
  4259. if (flush && lock_flush_tlb) {
  4260. kvm_flush_remote_tlbs(kvm);
  4261. flush = false;
  4262. }
  4263. return flush;
  4264. }
  4265. static __always_inline bool
  4266. slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  4267. slot_level_handler fn, int start_level, int end_level,
  4268. bool lock_flush_tlb)
  4269. {
  4270. return slot_handle_level_range(kvm, memslot, fn, start_level,
  4271. end_level, memslot->base_gfn,
  4272. memslot->base_gfn + memslot->npages - 1,
  4273. lock_flush_tlb);
  4274. }
  4275. static __always_inline bool
  4276. slot_handle_all_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  4277. slot_level_handler fn, bool lock_flush_tlb)
  4278. {
  4279. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
  4280. PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
  4281. }
  4282. static __always_inline bool
  4283. slot_handle_large_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  4284. slot_level_handler fn, bool lock_flush_tlb)
  4285. {
  4286. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL + 1,
  4287. PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
  4288. }
  4289. static __always_inline bool
  4290. slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot,
  4291. slot_level_handler fn, bool lock_flush_tlb)
  4292. {
  4293. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
  4294. PT_PAGE_TABLE_LEVEL, lock_flush_tlb);
  4295. }
  4296. void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
  4297. {
  4298. struct kvm_memslots *slots;
  4299. struct kvm_memory_slot *memslot;
  4300. int i;
  4301. spin_lock(&kvm->mmu_lock);
  4302. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  4303. slots = __kvm_memslots(kvm, i);
  4304. kvm_for_each_memslot(memslot, slots) {
  4305. gfn_t start, end;
  4306. start = max(gfn_start, memslot->base_gfn);
  4307. end = min(gfn_end, memslot->base_gfn + memslot->npages);
  4308. if (start >= end)
  4309. continue;
  4310. slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
  4311. PT_PAGE_TABLE_LEVEL, PT_MAX_HUGEPAGE_LEVEL,
  4312. start, end - 1, true);
  4313. }
  4314. }
  4315. spin_unlock(&kvm->mmu_lock);
  4316. }
  4317. static bool slot_rmap_write_protect(struct kvm *kvm,
  4318. struct kvm_rmap_head *rmap_head)
  4319. {
  4320. return __rmap_write_protect(kvm, rmap_head, false);
  4321. }
  4322. void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
  4323. struct kvm_memory_slot *memslot)
  4324. {
  4325. bool flush;
  4326. spin_lock(&kvm->mmu_lock);
  4327. flush = slot_handle_all_level(kvm, memslot, slot_rmap_write_protect,
  4328. false);
  4329. spin_unlock(&kvm->mmu_lock);
  4330. /*
  4331. * kvm_mmu_slot_remove_write_access() and kvm_vm_ioctl_get_dirty_log()
  4332. * which do tlb flush out of mmu-lock should be serialized by
  4333. * kvm->slots_lock otherwise tlb flush would be missed.
  4334. */
  4335. lockdep_assert_held(&kvm->slots_lock);
  4336. /*
  4337. * We can flush all the TLBs out of the mmu lock without TLB
  4338. * corruption since we just change the spte from writable to
  4339. * readonly so that we only need to care the case of changing
  4340. * spte from present to present (changing the spte from present
  4341. * to nonpresent will flush all the TLBs immediately), in other
  4342. * words, the only case we care is mmu_spte_update() where we
  4343. * haved checked SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE
  4344. * instead of PT_WRITABLE_MASK, that means it does not depend
  4345. * on PT_WRITABLE_MASK anymore.
  4346. */
  4347. if (flush)
  4348. kvm_flush_remote_tlbs(kvm);
  4349. }
  4350. static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
  4351. struct kvm_rmap_head *rmap_head)
  4352. {
  4353. u64 *sptep;
  4354. struct rmap_iterator iter;
  4355. int need_tlb_flush = 0;
  4356. kvm_pfn_t pfn;
  4357. struct kvm_mmu_page *sp;
  4358. restart:
  4359. for_each_rmap_spte(rmap_head, &iter, sptep) {
  4360. sp = page_header(__pa(sptep));
  4361. pfn = spte_to_pfn(*sptep);
  4362. /*
  4363. * We cannot do huge page mapping for indirect shadow pages,
  4364. * which are found on the last rmap (level = 1) when not using
  4365. * tdp; such shadow pages are synced with the page table in
  4366. * the guest, and the guest page table is using 4K page size
  4367. * mapping if the indirect sp has level = 1.
  4368. */
  4369. if (sp->role.direct &&
  4370. !kvm_is_reserved_pfn(pfn) &&
  4371. PageTransCompoundMap(pfn_to_page(pfn))) {
  4372. drop_spte(kvm, sptep);
  4373. need_tlb_flush = 1;
  4374. goto restart;
  4375. }
  4376. }
  4377. return need_tlb_flush;
  4378. }
  4379. void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
  4380. const struct kvm_memory_slot *memslot)
  4381. {
  4382. /* FIXME: const-ify all uses of struct kvm_memory_slot. */
  4383. spin_lock(&kvm->mmu_lock);
  4384. slot_handle_leaf(kvm, (struct kvm_memory_slot *)memslot,
  4385. kvm_mmu_zap_collapsible_spte, true);
  4386. spin_unlock(&kvm->mmu_lock);
  4387. }
  4388. void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
  4389. struct kvm_memory_slot *memslot)
  4390. {
  4391. bool flush;
  4392. spin_lock(&kvm->mmu_lock);
  4393. flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false);
  4394. spin_unlock(&kvm->mmu_lock);
  4395. lockdep_assert_held(&kvm->slots_lock);
  4396. /*
  4397. * It's also safe to flush TLBs out of mmu lock here as currently this
  4398. * function is only used for dirty logging, in which case flushing TLB
  4399. * out of mmu lock also guarantees no dirty pages will be lost in
  4400. * dirty_bitmap.
  4401. */
  4402. if (flush)
  4403. kvm_flush_remote_tlbs(kvm);
  4404. }
  4405. EXPORT_SYMBOL_GPL(kvm_mmu_slot_leaf_clear_dirty);
  4406. void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,
  4407. struct kvm_memory_slot *memslot)
  4408. {
  4409. bool flush;
  4410. spin_lock(&kvm->mmu_lock);
  4411. flush = slot_handle_large_level(kvm, memslot, slot_rmap_write_protect,
  4412. false);
  4413. spin_unlock(&kvm->mmu_lock);
  4414. /* see kvm_mmu_slot_remove_write_access */
  4415. lockdep_assert_held(&kvm->slots_lock);
  4416. if (flush)
  4417. kvm_flush_remote_tlbs(kvm);
  4418. }
  4419. EXPORT_SYMBOL_GPL(kvm_mmu_slot_largepage_remove_write_access);
  4420. void kvm_mmu_slot_set_dirty(struct kvm *kvm,
  4421. struct kvm_memory_slot *memslot)
  4422. {
  4423. bool flush;
  4424. spin_lock(&kvm->mmu_lock);
  4425. flush = slot_handle_all_level(kvm, memslot, __rmap_set_dirty, false);
  4426. spin_unlock(&kvm->mmu_lock);
  4427. lockdep_assert_held(&kvm->slots_lock);
  4428. /* see kvm_mmu_slot_leaf_clear_dirty */
  4429. if (flush)
  4430. kvm_flush_remote_tlbs(kvm);
  4431. }
  4432. EXPORT_SYMBOL_GPL(kvm_mmu_slot_set_dirty);
  4433. #define BATCH_ZAP_PAGES 10
  4434. static void kvm_zap_obsolete_pages(struct kvm *kvm)
  4435. {
  4436. struct kvm_mmu_page *sp, *node;
  4437. int batch = 0;
  4438. restart:
  4439. list_for_each_entry_safe_reverse(sp, node,
  4440. &kvm->arch.active_mmu_pages, link) {
  4441. int ret;
  4442. /*
  4443. * No obsolete page exists before new created page since
  4444. * active_mmu_pages is the FIFO list.
  4445. */
  4446. if (!is_obsolete_sp(kvm, sp))
  4447. break;
  4448. /*
  4449. * Since we are reversely walking the list and the invalid
  4450. * list will be moved to the head, skip the invalid page
  4451. * can help us to avoid the infinity list walking.
  4452. */
  4453. if (sp->role.invalid)
  4454. continue;
  4455. /*
  4456. * Need not flush tlb since we only zap the sp with invalid
  4457. * generation number.
  4458. */
  4459. if (batch >= BATCH_ZAP_PAGES &&
  4460. cond_resched_lock(&kvm->mmu_lock)) {
  4461. batch = 0;
  4462. goto restart;
  4463. }
  4464. ret = kvm_mmu_prepare_zap_page(kvm, sp,
  4465. &kvm->arch.zapped_obsolete_pages);
  4466. batch += ret;
  4467. if (ret)
  4468. goto restart;
  4469. }
  4470. /*
  4471. * Should flush tlb before free page tables since lockless-walking
  4472. * may use the pages.
  4473. */
  4474. kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
  4475. }
  4476. /*
  4477. * Fast invalidate all shadow pages and use lock-break technique
  4478. * to zap obsolete pages.
  4479. *
  4480. * It's required when memslot is being deleted or VM is being
  4481. * destroyed, in these cases, we should ensure that KVM MMU does
  4482. * not use any resource of the being-deleted slot or all slots
  4483. * after calling the function.
  4484. */
  4485. void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm)
  4486. {
  4487. spin_lock(&kvm->mmu_lock);
  4488. trace_kvm_mmu_invalidate_zap_all_pages(kvm);
  4489. kvm->arch.mmu_valid_gen++;
  4490. /*
  4491. * Notify all vcpus to reload its shadow page table
  4492. * and flush TLB. Then all vcpus will switch to new
  4493. * shadow page table with the new mmu_valid_gen.
  4494. *
  4495. * Note: we should do this under the protection of
  4496. * mmu-lock, otherwise, vcpu would purge shadow page
  4497. * but miss tlb flush.
  4498. */
  4499. kvm_reload_remote_mmus(kvm);
  4500. kvm_zap_obsolete_pages(kvm);
  4501. spin_unlock(&kvm->mmu_lock);
  4502. }
  4503. static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
  4504. {
  4505. return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
  4506. }
  4507. void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, struct kvm_memslots *slots)
  4508. {
  4509. /*
  4510. * The very rare case: if the generation-number is round,
  4511. * zap all shadow pages.
  4512. */
  4513. if (unlikely((slots->generation & MMIO_GEN_MASK) == 0)) {
  4514. kvm_debug_ratelimited("kvm: zapping shadow pages for mmio generation wraparound\n");
  4515. kvm_mmu_invalidate_zap_all_pages(kvm);
  4516. }
  4517. }
  4518. static unsigned long
  4519. mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  4520. {
  4521. struct kvm *kvm;
  4522. int nr_to_scan = sc->nr_to_scan;
  4523. unsigned long freed = 0;
  4524. spin_lock(&kvm_lock);
  4525. list_for_each_entry(kvm, &vm_list, vm_list) {
  4526. int idx;
  4527. LIST_HEAD(invalid_list);
  4528. /*
  4529. * Never scan more than sc->nr_to_scan VM instances.
  4530. * Will not hit this condition practically since we do not try
  4531. * to shrink more than one VM and it is very unlikely to see
  4532. * !n_used_mmu_pages so many times.
  4533. */
  4534. if (!nr_to_scan--)
  4535. break;
  4536. /*
  4537. * n_used_mmu_pages is accessed without holding kvm->mmu_lock
  4538. * here. We may skip a VM instance errorneosly, but we do not
  4539. * want to shrink a VM that only started to populate its MMU
  4540. * anyway.
  4541. */
  4542. if (!kvm->arch.n_used_mmu_pages &&
  4543. !kvm_has_zapped_obsolete_pages(kvm))
  4544. continue;
  4545. idx = srcu_read_lock(&kvm->srcu);
  4546. spin_lock(&kvm->mmu_lock);
  4547. if (kvm_has_zapped_obsolete_pages(kvm)) {
  4548. kvm_mmu_commit_zap_page(kvm,
  4549. &kvm->arch.zapped_obsolete_pages);
  4550. goto unlock;
  4551. }
  4552. if (prepare_zap_oldest_mmu_page(kvm, &invalid_list))
  4553. freed++;
  4554. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  4555. unlock:
  4556. spin_unlock(&kvm->mmu_lock);
  4557. srcu_read_unlock(&kvm->srcu, idx);
  4558. /*
  4559. * unfair on small ones
  4560. * per-vm shrinkers cry out
  4561. * sadness comes quickly
  4562. */
  4563. list_move_tail(&kvm->vm_list, &vm_list);
  4564. break;
  4565. }
  4566. spin_unlock(&kvm_lock);
  4567. return freed;
  4568. }
  4569. static unsigned long
  4570. mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  4571. {
  4572. return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
  4573. }
  4574. static struct shrinker mmu_shrinker = {
  4575. .count_objects = mmu_shrink_count,
  4576. .scan_objects = mmu_shrink_scan,
  4577. .seeks = DEFAULT_SEEKS * 10,
  4578. };
  4579. static void mmu_destroy_caches(void)
  4580. {
  4581. kmem_cache_destroy(pte_list_desc_cache);
  4582. kmem_cache_destroy(mmu_page_header_cache);
  4583. }
  4584. int kvm_mmu_module_init(void)
  4585. {
  4586. int ret = -ENOMEM;
  4587. kvm_mmu_clear_all_pte_masks();
  4588. pte_list_desc_cache = kmem_cache_create("pte_list_desc",
  4589. sizeof(struct pte_list_desc),
  4590. 0, SLAB_ACCOUNT, NULL);
  4591. if (!pte_list_desc_cache)
  4592. goto out;
  4593. mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
  4594. sizeof(struct kvm_mmu_page),
  4595. 0, SLAB_ACCOUNT, NULL);
  4596. if (!mmu_page_header_cache)
  4597. goto out;
  4598. if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL))
  4599. goto out;
  4600. ret = register_shrinker(&mmu_shrinker);
  4601. if (ret)
  4602. goto out;
  4603. return 0;
  4604. out:
  4605. mmu_destroy_caches();
  4606. return ret;
  4607. }
  4608. /*
  4609. * Caculate mmu pages needed for kvm.
  4610. */
  4611. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
  4612. {
  4613. unsigned int nr_mmu_pages;
  4614. unsigned int nr_pages = 0;
  4615. struct kvm_memslots *slots;
  4616. struct kvm_memory_slot *memslot;
  4617. int i;
  4618. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  4619. slots = __kvm_memslots(kvm, i);
  4620. kvm_for_each_memslot(memslot, slots)
  4621. nr_pages += memslot->npages;
  4622. }
  4623. nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
  4624. nr_mmu_pages = max(nr_mmu_pages,
  4625. (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
  4626. return nr_mmu_pages;
  4627. }
  4628. void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
  4629. {
  4630. kvm_mmu_unload(vcpu);
  4631. free_mmu_pages(vcpu);
  4632. mmu_free_memory_caches(vcpu);
  4633. }
  4634. void kvm_mmu_module_exit(void)
  4635. {
  4636. mmu_destroy_caches();
  4637. percpu_counter_destroy(&kvm_total_used_mmu_pages);
  4638. unregister_shrinker(&mmu_shrinker);
  4639. mmu_audit_disable();
  4640. }