mmu.c 110 KB

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