mmu.c 129 KB

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