mmu.c 113 KB

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