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