mmu.c 96 KB

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