mmu.c 148 KB

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