mmu.c 142 KB

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