mmu.c 99 KB

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