mmu.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543
  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 affilates.
  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 "mmu.h"
  21. #include "x86.h"
  22. #include "kvm_cache_regs.h"
  23. #include <linux/kvm_host.h>
  24. #include <linux/types.h>
  25. #include <linux/string.h>
  26. #include <linux/mm.h>
  27. #include <linux/highmem.h>
  28. #include <linux/module.h>
  29. #include <linux/swap.h>
  30. #include <linux/hugetlb.h>
  31. #include <linux/compiler.h>
  32. #include <linux/srcu.h>
  33. #include <linux/slab.h>
  34. #include <linux/uaccess.h>
  35. #include <asm/page.h>
  36. #include <asm/cmpxchg.h>
  37. #include <asm/io.h>
  38. #include <asm/vmx.h>
  39. /*
  40. * When setting this variable to true it enables Two-Dimensional-Paging
  41. * where the hardware walks 2 page tables:
  42. * 1. the guest-virtual to guest-physical
  43. * 2. while doing 1. it walks guest-physical to host-physical
  44. * If the hardware supports that we don't need to do shadow paging.
  45. */
  46. bool tdp_enabled = false;
  47. #undef MMU_DEBUG
  48. #undef AUDIT
  49. #ifdef AUDIT
  50. static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg);
  51. #else
  52. static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg) {}
  53. #endif
  54. #ifdef MMU_DEBUG
  55. #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
  56. #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
  57. #else
  58. #define pgprintk(x...) do { } while (0)
  59. #define rmap_printk(x...) do { } while (0)
  60. #endif
  61. #if defined(MMU_DEBUG) || defined(AUDIT)
  62. static int dbg = 0;
  63. module_param(dbg, bool, 0644);
  64. #endif
  65. static int oos_shadow = 1;
  66. module_param(oos_shadow, bool, 0644);
  67. #ifndef MMU_DEBUG
  68. #define ASSERT(x) do { } while (0)
  69. #else
  70. #define ASSERT(x) \
  71. if (!(x)) { \
  72. printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
  73. __FILE__, __LINE__, #x); \
  74. }
  75. #endif
  76. #define PT_FIRST_AVAIL_BITS_SHIFT 9
  77. #define PT64_SECOND_AVAIL_BITS_SHIFT 52
  78. #define VALID_PAGE(x) ((x) != INVALID_PAGE)
  79. #define PT64_LEVEL_BITS 9
  80. #define PT64_LEVEL_SHIFT(level) \
  81. (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
  82. #define PT64_LEVEL_MASK(level) \
  83. (((1ULL << PT64_LEVEL_BITS) - 1) << PT64_LEVEL_SHIFT(level))
  84. #define PT64_INDEX(address, level)\
  85. (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
  86. #define PT32_LEVEL_BITS 10
  87. #define PT32_LEVEL_SHIFT(level) \
  88. (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
  89. #define PT32_LEVEL_MASK(level) \
  90. (((1ULL << PT32_LEVEL_BITS) - 1) << PT32_LEVEL_SHIFT(level))
  91. #define PT32_LVL_OFFSET_MASK(level) \
  92. (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  93. * PT32_LEVEL_BITS))) - 1))
  94. #define PT32_INDEX(address, level)\
  95. (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
  96. #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
  97. #define PT64_DIR_BASE_ADDR_MASK \
  98. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
  99. #define PT64_LVL_ADDR_MASK(level) \
  100. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  101. * PT64_LEVEL_BITS))) - 1))
  102. #define PT64_LVL_OFFSET_MASK(level) \
  103. (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  104. * PT64_LEVEL_BITS))) - 1))
  105. #define PT32_BASE_ADDR_MASK PAGE_MASK
  106. #define PT32_DIR_BASE_ADDR_MASK \
  107. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
  108. #define PT32_LVL_ADDR_MASK(level) \
  109. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  110. * PT32_LEVEL_BITS))) - 1))
  111. #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
  112. | PT64_NX_MASK)
  113. #define RMAP_EXT 4
  114. #define ACC_EXEC_MASK 1
  115. #define ACC_WRITE_MASK PT_WRITABLE_MASK
  116. #define ACC_USER_MASK PT_USER_MASK
  117. #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
  118. #include <trace/events/kvm.h>
  119. #define CREATE_TRACE_POINTS
  120. #include "mmutrace.h"
  121. #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
  122. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  123. struct kvm_rmap_desc {
  124. u64 *sptes[RMAP_EXT];
  125. struct kvm_rmap_desc *more;
  126. };
  127. struct kvm_shadow_walk_iterator {
  128. u64 addr;
  129. hpa_t shadow_addr;
  130. int level;
  131. u64 *sptep;
  132. unsigned index;
  133. };
  134. #define for_each_shadow_entry(_vcpu, _addr, _walker) \
  135. for (shadow_walk_init(&(_walker), _vcpu, _addr); \
  136. shadow_walk_okay(&(_walker)); \
  137. shadow_walk_next(&(_walker)))
  138. typedef void (*mmu_parent_walk_fn) (struct kvm_mmu_page *sp, u64 *spte);
  139. static struct kmem_cache *pte_chain_cache;
  140. static struct kmem_cache *rmap_desc_cache;
  141. static struct kmem_cache *mmu_page_header_cache;
  142. static u64 __read_mostly shadow_trap_nonpresent_pte;
  143. static u64 __read_mostly shadow_notrap_nonpresent_pte;
  144. static u64 __read_mostly shadow_base_present_pte;
  145. static u64 __read_mostly shadow_nx_mask;
  146. static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
  147. static u64 __read_mostly shadow_user_mask;
  148. static u64 __read_mostly shadow_accessed_mask;
  149. static u64 __read_mostly shadow_dirty_mask;
  150. static inline u64 rsvd_bits(int s, int e)
  151. {
  152. return ((1ULL << (e - s + 1)) - 1) << s;
  153. }
  154. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
  155. {
  156. shadow_trap_nonpresent_pte = trap_pte;
  157. shadow_notrap_nonpresent_pte = notrap_pte;
  158. }
  159. EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
  160. void kvm_mmu_set_base_ptes(u64 base_pte)
  161. {
  162. shadow_base_present_pte = base_pte;
  163. }
  164. EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
  165. void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
  166. u64 dirty_mask, u64 nx_mask, u64 x_mask)
  167. {
  168. shadow_user_mask = user_mask;
  169. shadow_accessed_mask = accessed_mask;
  170. shadow_dirty_mask = dirty_mask;
  171. shadow_nx_mask = nx_mask;
  172. shadow_x_mask = x_mask;
  173. }
  174. EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
  175. static bool is_write_protection(struct kvm_vcpu *vcpu)
  176. {
  177. return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
  178. }
  179. static int is_cpuid_PSE36(void)
  180. {
  181. return 1;
  182. }
  183. static int is_nx(struct kvm_vcpu *vcpu)
  184. {
  185. return vcpu->arch.efer & EFER_NX;
  186. }
  187. static int is_shadow_present_pte(u64 pte)
  188. {
  189. return pte != shadow_trap_nonpresent_pte
  190. && pte != shadow_notrap_nonpresent_pte;
  191. }
  192. static int is_large_pte(u64 pte)
  193. {
  194. return pte & PT_PAGE_SIZE_MASK;
  195. }
  196. static int is_writable_pte(unsigned long pte)
  197. {
  198. return pte & PT_WRITABLE_MASK;
  199. }
  200. static int is_dirty_gpte(unsigned long pte)
  201. {
  202. return pte & PT_DIRTY_MASK;
  203. }
  204. static int is_rmap_spte(u64 pte)
  205. {
  206. return is_shadow_present_pte(pte);
  207. }
  208. static int is_last_spte(u64 pte, int level)
  209. {
  210. if (level == PT_PAGE_TABLE_LEVEL)
  211. return 1;
  212. if (is_large_pte(pte))
  213. return 1;
  214. return 0;
  215. }
  216. static pfn_t spte_to_pfn(u64 pte)
  217. {
  218. return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
  219. }
  220. static gfn_t pse36_gfn_delta(u32 gpte)
  221. {
  222. int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
  223. return (gpte & PT32_DIR_PSE36_MASK) << shift;
  224. }
  225. static void __set_spte(u64 *sptep, u64 spte)
  226. {
  227. #ifdef CONFIG_X86_64
  228. set_64bit((unsigned long *)sptep, spte);
  229. #else
  230. set_64bit((unsigned long long *)sptep, spte);
  231. #endif
  232. }
  233. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  234. struct kmem_cache *base_cache, int min)
  235. {
  236. void *obj;
  237. if (cache->nobjs >= min)
  238. return 0;
  239. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  240. obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
  241. if (!obj)
  242. return -ENOMEM;
  243. cache->objects[cache->nobjs++] = obj;
  244. }
  245. return 0;
  246. }
  247. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
  248. struct kmem_cache *cache)
  249. {
  250. while (mc->nobjs)
  251. kmem_cache_free(cache, mc->objects[--mc->nobjs]);
  252. }
  253. static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
  254. int min)
  255. {
  256. struct page *page;
  257. if (cache->nobjs >= min)
  258. return 0;
  259. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  260. page = alloc_page(GFP_KERNEL);
  261. if (!page)
  262. return -ENOMEM;
  263. cache->objects[cache->nobjs++] = page_address(page);
  264. }
  265. return 0;
  266. }
  267. static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
  268. {
  269. while (mc->nobjs)
  270. free_page((unsigned long)mc->objects[--mc->nobjs]);
  271. }
  272. static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
  273. {
  274. int r;
  275. r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_chain_cache,
  276. pte_chain_cache, 4);
  277. if (r)
  278. goto out;
  279. r = mmu_topup_memory_cache(&vcpu->arch.mmu_rmap_desc_cache,
  280. rmap_desc_cache, 4);
  281. if (r)
  282. goto out;
  283. r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
  284. if (r)
  285. goto out;
  286. r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
  287. mmu_page_header_cache, 4);
  288. out:
  289. return r;
  290. }
  291. static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  292. {
  293. mmu_free_memory_cache(&vcpu->arch.mmu_pte_chain_cache, pte_chain_cache);
  294. mmu_free_memory_cache(&vcpu->arch.mmu_rmap_desc_cache, rmap_desc_cache);
  295. mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
  296. mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
  297. mmu_page_header_cache);
  298. }
  299. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
  300. size_t size)
  301. {
  302. void *p;
  303. BUG_ON(!mc->nobjs);
  304. p = mc->objects[--mc->nobjs];
  305. return p;
  306. }
  307. static struct kvm_pte_chain *mmu_alloc_pte_chain(struct kvm_vcpu *vcpu)
  308. {
  309. return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_chain_cache,
  310. sizeof(struct kvm_pte_chain));
  311. }
  312. static void mmu_free_pte_chain(struct kvm_pte_chain *pc)
  313. {
  314. kmem_cache_free(pte_chain_cache, pc);
  315. }
  316. static struct kvm_rmap_desc *mmu_alloc_rmap_desc(struct kvm_vcpu *vcpu)
  317. {
  318. return mmu_memory_cache_alloc(&vcpu->arch.mmu_rmap_desc_cache,
  319. sizeof(struct kvm_rmap_desc));
  320. }
  321. static void mmu_free_rmap_desc(struct kvm_rmap_desc *rd)
  322. {
  323. kmem_cache_free(rmap_desc_cache, rd);
  324. }
  325. static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
  326. {
  327. if (!sp->role.direct)
  328. return sp->gfns[index];
  329. return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
  330. }
  331. static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
  332. {
  333. if (sp->role.direct)
  334. BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
  335. else
  336. sp->gfns[index] = gfn;
  337. }
  338. /*
  339. * Return the pointer to the largepage write count for a given
  340. * gfn, handling slots that are not large page aligned.
  341. */
  342. static int *slot_largepage_idx(gfn_t gfn,
  343. struct kvm_memory_slot *slot,
  344. int level)
  345. {
  346. unsigned long idx;
  347. idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
  348. (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
  349. return &slot->lpage_info[level - 2][idx].write_count;
  350. }
  351. static void account_shadowed(struct kvm *kvm, gfn_t gfn)
  352. {
  353. struct kvm_memory_slot *slot;
  354. int *write_count;
  355. int i;
  356. gfn = unalias_gfn(kvm, gfn);
  357. slot = gfn_to_memslot_unaliased(kvm, gfn);
  358. for (i = PT_DIRECTORY_LEVEL;
  359. i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
  360. write_count = slot_largepage_idx(gfn, slot, i);
  361. *write_count += 1;
  362. }
  363. }
  364. static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
  365. {
  366. struct kvm_memory_slot *slot;
  367. int *write_count;
  368. int i;
  369. gfn = unalias_gfn(kvm, gfn);
  370. slot = gfn_to_memslot_unaliased(kvm, gfn);
  371. for (i = PT_DIRECTORY_LEVEL;
  372. i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
  373. write_count = slot_largepage_idx(gfn, slot, i);
  374. *write_count -= 1;
  375. WARN_ON(*write_count < 0);
  376. }
  377. }
  378. static int has_wrprotected_page(struct kvm *kvm,
  379. gfn_t gfn,
  380. int level)
  381. {
  382. struct kvm_memory_slot *slot;
  383. int *largepage_idx;
  384. gfn = unalias_gfn(kvm, gfn);
  385. slot = gfn_to_memslot_unaliased(kvm, gfn);
  386. if (slot) {
  387. largepage_idx = slot_largepage_idx(gfn, slot, level);
  388. return *largepage_idx;
  389. }
  390. return 1;
  391. }
  392. static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
  393. {
  394. unsigned long page_size;
  395. int i, ret = 0;
  396. page_size = kvm_host_page_size(kvm, gfn);
  397. for (i = PT_PAGE_TABLE_LEVEL;
  398. i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
  399. if (page_size >= KVM_HPAGE_SIZE(i))
  400. ret = i;
  401. else
  402. break;
  403. }
  404. return ret;
  405. }
  406. static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
  407. {
  408. struct kvm_memory_slot *slot;
  409. int host_level, level, max_level;
  410. slot = gfn_to_memslot(vcpu->kvm, large_gfn);
  411. if (slot && slot->dirty_bitmap)
  412. return PT_PAGE_TABLE_LEVEL;
  413. host_level = host_mapping_level(vcpu->kvm, large_gfn);
  414. if (host_level == PT_PAGE_TABLE_LEVEL)
  415. return host_level;
  416. max_level = kvm_x86_ops->get_lpage_level() < host_level ?
  417. kvm_x86_ops->get_lpage_level() : host_level;
  418. for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
  419. if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
  420. break;
  421. return level - 1;
  422. }
  423. /*
  424. * Take gfn and return the reverse mapping to it.
  425. * Note: gfn must be unaliased before this function get called
  426. */
  427. static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
  428. {
  429. struct kvm_memory_slot *slot;
  430. unsigned long idx;
  431. slot = gfn_to_memslot(kvm, gfn);
  432. if (likely(level == PT_PAGE_TABLE_LEVEL))
  433. return &slot->rmap[gfn - slot->base_gfn];
  434. idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
  435. (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
  436. return &slot->lpage_info[level - 2][idx].rmap_pde;
  437. }
  438. /*
  439. * Reverse mapping data structures:
  440. *
  441. * If rmapp bit zero is zero, then rmapp point to the shadw page table entry
  442. * that points to page_address(page).
  443. *
  444. * If rmapp bit zero is one, (then rmap & ~1) points to a struct kvm_rmap_desc
  445. * containing more mappings.
  446. *
  447. * Returns the number of rmap entries before the spte was added or zero if
  448. * the spte was not added.
  449. *
  450. */
  451. static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  452. {
  453. struct kvm_mmu_page *sp;
  454. struct kvm_rmap_desc *desc;
  455. unsigned long *rmapp;
  456. int i, count = 0;
  457. if (!is_rmap_spte(*spte))
  458. return count;
  459. gfn = unalias_gfn(vcpu->kvm, gfn);
  460. sp = page_header(__pa(spte));
  461. kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
  462. rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
  463. if (!*rmapp) {
  464. rmap_printk("rmap_add: %p %llx 0->1\n", spte, *spte);
  465. *rmapp = (unsigned long)spte;
  466. } else if (!(*rmapp & 1)) {
  467. rmap_printk("rmap_add: %p %llx 1->many\n", spte, *spte);
  468. desc = mmu_alloc_rmap_desc(vcpu);
  469. desc->sptes[0] = (u64 *)*rmapp;
  470. desc->sptes[1] = spte;
  471. *rmapp = (unsigned long)desc | 1;
  472. } else {
  473. rmap_printk("rmap_add: %p %llx many->many\n", spte, *spte);
  474. desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
  475. while (desc->sptes[RMAP_EXT-1] && desc->more) {
  476. desc = desc->more;
  477. count += RMAP_EXT;
  478. }
  479. if (desc->sptes[RMAP_EXT-1]) {
  480. desc->more = mmu_alloc_rmap_desc(vcpu);
  481. desc = desc->more;
  482. }
  483. for (i = 0; desc->sptes[i]; ++i)
  484. ;
  485. desc->sptes[i] = spte;
  486. }
  487. return count;
  488. }
  489. static void rmap_desc_remove_entry(unsigned long *rmapp,
  490. struct kvm_rmap_desc *desc,
  491. int i,
  492. struct kvm_rmap_desc *prev_desc)
  493. {
  494. int j;
  495. for (j = RMAP_EXT - 1; !desc->sptes[j] && j > i; --j)
  496. ;
  497. desc->sptes[i] = desc->sptes[j];
  498. desc->sptes[j] = NULL;
  499. if (j != 0)
  500. return;
  501. if (!prev_desc && !desc->more)
  502. *rmapp = (unsigned long)desc->sptes[0];
  503. else
  504. if (prev_desc)
  505. prev_desc->more = desc->more;
  506. else
  507. *rmapp = (unsigned long)desc->more | 1;
  508. mmu_free_rmap_desc(desc);
  509. }
  510. static void rmap_remove(struct kvm *kvm, u64 *spte)
  511. {
  512. struct kvm_rmap_desc *desc;
  513. struct kvm_rmap_desc *prev_desc;
  514. struct kvm_mmu_page *sp;
  515. pfn_t pfn;
  516. gfn_t gfn;
  517. unsigned long *rmapp;
  518. int i;
  519. if (!is_rmap_spte(*spte))
  520. return;
  521. sp = page_header(__pa(spte));
  522. pfn = spte_to_pfn(*spte);
  523. if (*spte & shadow_accessed_mask)
  524. kvm_set_pfn_accessed(pfn);
  525. if (is_writable_pte(*spte))
  526. kvm_set_pfn_dirty(pfn);
  527. gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
  528. rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
  529. if (!*rmapp) {
  530. printk(KERN_ERR "rmap_remove: %p %llx 0->BUG\n", spte, *spte);
  531. BUG();
  532. } else if (!(*rmapp & 1)) {
  533. rmap_printk("rmap_remove: %p %llx 1->0\n", spte, *spte);
  534. if ((u64 *)*rmapp != spte) {
  535. printk(KERN_ERR "rmap_remove: %p %llx 1->BUG\n",
  536. spte, *spte);
  537. BUG();
  538. }
  539. *rmapp = 0;
  540. } else {
  541. rmap_printk("rmap_remove: %p %llx many->many\n", spte, *spte);
  542. desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
  543. prev_desc = NULL;
  544. while (desc) {
  545. for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i)
  546. if (desc->sptes[i] == spte) {
  547. rmap_desc_remove_entry(rmapp,
  548. desc, i,
  549. prev_desc);
  550. return;
  551. }
  552. prev_desc = desc;
  553. desc = desc->more;
  554. }
  555. pr_err("rmap_remove: %p %llx many->many\n", spte, *spte);
  556. BUG();
  557. }
  558. }
  559. static u64 *rmap_next(struct kvm *kvm, unsigned long *rmapp, u64 *spte)
  560. {
  561. struct kvm_rmap_desc *desc;
  562. u64 *prev_spte;
  563. int i;
  564. if (!*rmapp)
  565. return NULL;
  566. else if (!(*rmapp & 1)) {
  567. if (!spte)
  568. return (u64 *)*rmapp;
  569. return NULL;
  570. }
  571. desc = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
  572. prev_spte = NULL;
  573. while (desc) {
  574. for (i = 0; i < RMAP_EXT && desc->sptes[i]; ++i) {
  575. if (prev_spte == spte)
  576. return desc->sptes[i];
  577. prev_spte = desc->sptes[i];
  578. }
  579. desc = desc->more;
  580. }
  581. return NULL;
  582. }
  583. static int rmap_write_protect(struct kvm *kvm, u64 gfn)
  584. {
  585. unsigned long *rmapp;
  586. u64 *spte;
  587. int i, write_protected = 0;
  588. gfn = unalias_gfn(kvm, gfn);
  589. rmapp = gfn_to_rmap(kvm, gfn, PT_PAGE_TABLE_LEVEL);
  590. spte = rmap_next(kvm, rmapp, NULL);
  591. while (spte) {
  592. BUG_ON(!spte);
  593. BUG_ON(!(*spte & PT_PRESENT_MASK));
  594. rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
  595. if (is_writable_pte(*spte)) {
  596. __set_spte(spte, *spte & ~PT_WRITABLE_MASK);
  597. write_protected = 1;
  598. }
  599. spte = rmap_next(kvm, rmapp, spte);
  600. }
  601. if (write_protected) {
  602. pfn_t pfn;
  603. spte = rmap_next(kvm, rmapp, NULL);
  604. pfn = spte_to_pfn(*spte);
  605. kvm_set_pfn_dirty(pfn);
  606. }
  607. /* check for huge page mappings */
  608. for (i = PT_DIRECTORY_LEVEL;
  609. i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
  610. rmapp = gfn_to_rmap(kvm, gfn, i);
  611. spte = rmap_next(kvm, rmapp, NULL);
  612. while (spte) {
  613. BUG_ON(!spte);
  614. BUG_ON(!(*spte & PT_PRESENT_MASK));
  615. BUG_ON((*spte & (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK)) != (PT_PAGE_SIZE_MASK|PT_PRESENT_MASK));
  616. pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
  617. if (is_writable_pte(*spte)) {
  618. rmap_remove(kvm, spte);
  619. --kvm->stat.lpages;
  620. __set_spte(spte, shadow_trap_nonpresent_pte);
  621. spte = NULL;
  622. write_protected = 1;
  623. }
  624. spte = rmap_next(kvm, rmapp, spte);
  625. }
  626. }
  627. return write_protected;
  628. }
  629. static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
  630. unsigned long data)
  631. {
  632. u64 *spte;
  633. int need_tlb_flush = 0;
  634. while ((spte = rmap_next(kvm, rmapp, NULL))) {
  635. BUG_ON(!(*spte & PT_PRESENT_MASK));
  636. rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
  637. rmap_remove(kvm, spte);
  638. __set_spte(spte, shadow_trap_nonpresent_pte);
  639. need_tlb_flush = 1;
  640. }
  641. return need_tlb_flush;
  642. }
  643. static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
  644. unsigned long data)
  645. {
  646. int need_flush = 0;
  647. u64 *spte, new_spte;
  648. pte_t *ptep = (pte_t *)data;
  649. pfn_t new_pfn;
  650. WARN_ON(pte_huge(*ptep));
  651. new_pfn = pte_pfn(*ptep);
  652. spte = rmap_next(kvm, rmapp, NULL);
  653. while (spte) {
  654. BUG_ON(!is_shadow_present_pte(*spte));
  655. rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
  656. need_flush = 1;
  657. if (pte_write(*ptep)) {
  658. rmap_remove(kvm, spte);
  659. __set_spte(spte, shadow_trap_nonpresent_pte);
  660. spte = rmap_next(kvm, rmapp, NULL);
  661. } else {
  662. new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
  663. new_spte |= (u64)new_pfn << PAGE_SHIFT;
  664. new_spte &= ~PT_WRITABLE_MASK;
  665. new_spte &= ~SPTE_HOST_WRITEABLE;
  666. if (is_writable_pte(*spte))
  667. kvm_set_pfn_dirty(spte_to_pfn(*spte));
  668. __set_spte(spte, new_spte);
  669. spte = rmap_next(kvm, rmapp, spte);
  670. }
  671. }
  672. if (need_flush)
  673. kvm_flush_remote_tlbs(kvm);
  674. return 0;
  675. }
  676. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  677. unsigned long data,
  678. int (*handler)(struct kvm *kvm, unsigned long *rmapp,
  679. unsigned long data))
  680. {
  681. int i, j;
  682. int ret;
  683. int retval = 0;
  684. struct kvm_memslots *slots;
  685. slots = kvm_memslots(kvm);
  686. for (i = 0; i < slots->nmemslots; i++) {
  687. struct kvm_memory_slot *memslot = &slots->memslots[i];
  688. unsigned long start = memslot->userspace_addr;
  689. unsigned long end;
  690. end = start + (memslot->npages << PAGE_SHIFT);
  691. if (hva >= start && hva < end) {
  692. gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
  693. ret = handler(kvm, &memslot->rmap[gfn_offset], data);
  694. for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
  695. int idx = gfn_offset;
  696. idx /= KVM_PAGES_PER_HPAGE(PT_DIRECTORY_LEVEL + j);
  697. ret |= handler(kvm,
  698. &memslot->lpage_info[j][idx].rmap_pde,
  699. data);
  700. }
  701. trace_kvm_age_page(hva, memslot, ret);
  702. retval |= ret;
  703. }
  704. }
  705. return retval;
  706. }
  707. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  708. {
  709. return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
  710. }
  711. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  712. {
  713. kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
  714. }
  715. static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  716. unsigned long data)
  717. {
  718. u64 *spte;
  719. int young = 0;
  720. /*
  721. * Emulate the accessed bit for EPT, by checking if this page has
  722. * an EPT mapping, and clearing it if it does. On the next access,
  723. * a new EPT mapping will be established.
  724. * This has some overhead, but not as much as the cost of swapping
  725. * out actively used pages or breaking up actively used hugepages.
  726. */
  727. if (!shadow_accessed_mask)
  728. return kvm_unmap_rmapp(kvm, rmapp, data);
  729. spte = rmap_next(kvm, rmapp, NULL);
  730. while (spte) {
  731. int _young;
  732. u64 _spte = *spte;
  733. BUG_ON(!(_spte & PT_PRESENT_MASK));
  734. _young = _spte & PT_ACCESSED_MASK;
  735. if (_young) {
  736. young = 1;
  737. clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
  738. }
  739. spte = rmap_next(kvm, rmapp, spte);
  740. }
  741. return young;
  742. }
  743. #define RMAP_RECYCLE_THRESHOLD 1000
  744. static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  745. {
  746. unsigned long *rmapp;
  747. struct kvm_mmu_page *sp;
  748. sp = page_header(__pa(spte));
  749. gfn = unalias_gfn(vcpu->kvm, gfn);
  750. rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
  751. kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
  752. kvm_flush_remote_tlbs(vcpu->kvm);
  753. }
  754. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  755. {
  756. return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
  757. }
  758. #ifdef MMU_DEBUG
  759. static int is_empty_shadow_page(u64 *spt)
  760. {
  761. u64 *pos;
  762. u64 *end;
  763. for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
  764. if (is_shadow_present_pte(*pos)) {
  765. printk(KERN_ERR "%s: %p %llx\n", __func__,
  766. pos, *pos);
  767. return 0;
  768. }
  769. return 1;
  770. }
  771. #endif
  772. static void kvm_mmu_free_page(struct kvm *kvm, struct kvm_mmu_page *sp)
  773. {
  774. ASSERT(is_empty_shadow_page(sp->spt));
  775. hlist_del(&sp->hash_link);
  776. list_del(&sp->link);
  777. __free_page(virt_to_page(sp->spt));
  778. if (!sp->role.direct)
  779. __free_page(virt_to_page(sp->gfns));
  780. kmem_cache_free(mmu_page_header_cache, sp);
  781. ++kvm->arch.n_free_mmu_pages;
  782. }
  783. static unsigned kvm_page_table_hashfn(gfn_t gfn)
  784. {
  785. return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
  786. }
  787. static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
  788. u64 *parent_pte, int direct)
  789. {
  790. struct kvm_mmu_page *sp;
  791. sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache, sizeof *sp);
  792. sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
  793. if (!direct)
  794. sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
  795. PAGE_SIZE);
  796. set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
  797. list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
  798. bitmap_zero(sp->slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
  799. sp->multimapped = 0;
  800. sp->parent_pte = parent_pte;
  801. --vcpu->kvm->arch.n_free_mmu_pages;
  802. return sp;
  803. }
  804. static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
  805. struct kvm_mmu_page *sp, u64 *parent_pte)
  806. {
  807. struct kvm_pte_chain *pte_chain;
  808. struct hlist_node *node;
  809. int i;
  810. if (!parent_pte)
  811. return;
  812. if (!sp->multimapped) {
  813. u64 *old = sp->parent_pte;
  814. if (!old) {
  815. sp->parent_pte = parent_pte;
  816. return;
  817. }
  818. sp->multimapped = 1;
  819. pte_chain = mmu_alloc_pte_chain(vcpu);
  820. INIT_HLIST_HEAD(&sp->parent_ptes);
  821. hlist_add_head(&pte_chain->link, &sp->parent_ptes);
  822. pte_chain->parent_ptes[0] = old;
  823. }
  824. hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link) {
  825. if (pte_chain->parent_ptes[NR_PTE_CHAIN_ENTRIES-1])
  826. continue;
  827. for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i)
  828. if (!pte_chain->parent_ptes[i]) {
  829. pte_chain->parent_ptes[i] = parent_pte;
  830. return;
  831. }
  832. }
  833. pte_chain = mmu_alloc_pte_chain(vcpu);
  834. BUG_ON(!pte_chain);
  835. hlist_add_head(&pte_chain->link, &sp->parent_ptes);
  836. pte_chain->parent_ptes[0] = parent_pte;
  837. }
  838. static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
  839. u64 *parent_pte)
  840. {
  841. struct kvm_pte_chain *pte_chain;
  842. struct hlist_node *node;
  843. int i;
  844. if (!sp->multimapped) {
  845. BUG_ON(sp->parent_pte != parent_pte);
  846. sp->parent_pte = NULL;
  847. return;
  848. }
  849. hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
  850. for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
  851. if (!pte_chain->parent_ptes[i])
  852. break;
  853. if (pte_chain->parent_ptes[i] != parent_pte)
  854. continue;
  855. while (i + 1 < NR_PTE_CHAIN_ENTRIES
  856. && pte_chain->parent_ptes[i + 1]) {
  857. pte_chain->parent_ptes[i]
  858. = pte_chain->parent_ptes[i + 1];
  859. ++i;
  860. }
  861. pte_chain->parent_ptes[i] = NULL;
  862. if (i == 0) {
  863. hlist_del(&pte_chain->link);
  864. mmu_free_pte_chain(pte_chain);
  865. if (hlist_empty(&sp->parent_ptes)) {
  866. sp->multimapped = 0;
  867. sp->parent_pte = NULL;
  868. }
  869. }
  870. return;
  871. }
  872. BUG();
  873. }
  874. static void mmu_parent_walk(struct kvm_mmu_page *sp, mmu_parent_walk_fn fn)
  875. {
  876. struct kvm_pte_chain *pte_chain;
  877. struct hlist_node *node;
  878. struct kvm_mmu_page *parent_sp;
  879. int i;
  880. if (!sp->multimapped && sp->parent_pte) {
  881. parent_sp = page_header(__pa(sp->parent_pte));
  882. fn(parent_sp, sp->parent_pte);
  883. return;
  884. }
  885. hlist_for_each_entry(pte_chain, node, &sp->parent_ptes, link)
  886. for (i = 0; i < NR_PTE_CHAIN_ENTRIES; ++i) {
  887. u64 *spte = pte_chain->parent_ptes[i];
  888. if (!spte)
  889. break;
  890. parent_sp = page_header(__pa(spte));
  891. fn(parent_sp, spte);
  892. }
  893. }
  894. static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte);
  895. static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
  896. {
  897. mmu_parent_walk(sp, mark_unsync);
  898. }
  899. static void mark_unsync(struct kvm_mmu_page *sp, u64 *spte)
  900. {
  901. unsigned int index;
  902. index = spte - sp->spt;
  903. if (__test_and_set_bit(index, sp->unsync_child_bitmap))
  904. return;
  905. if (sp->unsync_children++)
  906. return;
  907. kvm_mmu_mark_parents_unsync(sp);
  908. }
  909. static void nonpaging_prefetch_page(struct kvm_vcpu *vcpu,
  910. struct kvm_mmu_page *sp)
  911. {
  912. int i;
  913. for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
  914. sp->spt[i] = shadow_trap_nonpresent_pte;
  915. }
  916. static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
  917. struct kvm_mmu_page *sp, bool clear_unsync)
  918. {
  919. return 1;
  920. }
  921. static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  922. {
  923. }
  924. #define KVM_PAGE_ARRAY_NR 16
  925. struct kvm_mmu_pages {
  926. struct mmu_page_and_offset {
  927. struct kvm_mmu_page *sp;
  928. unsigned int idx;
  929. } page[KVM_PAGE_ARRAY_NR];
  930. unsigned int nr;
  931. };
  932. #define for_each_unsync_children(bitmap, idx) \
  933. for (idx = find_first_bit(bitmap, 512); \
  934. idx < 512; \
  935. idx = find_next_bit(bitmap, 512, idx+1))
  936. static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
  937. int idx)
  938. {
  939. int i;
  940. if (sp->unsync)
  941. for (i=0; i < pvec->nr; i++)
  942. if (pvec->page[i].sp == sp)
  943. return 0;
  944. pvec->page[pvec->nr].sp = sp;
  945. pvec->page[pvec->nr].idx = idx;
  946. pvec->nr++;
  947. return (pvec->nr == KVM_PAGE_ARRAY_NR);
  948. }
  949. static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
  950. struct kvm_mmu_pages *pvec)
  951. {
  952. int i, ret, nr_unsync_leaf = 0;
  953. for_each_unsync_children(sp->unsync_child_bitmap, i) {
  954. struct kvm_mmu_page *child;
  955. u64 ent = sp->spt[i];
  956. if (!is_shadow_present_pte(ent) || is_large_pte(ent))
  957. goto clear_child_bitmap;
  958. child = page_header(ent & PT64_BASE_ADDR_MASK);
  959. if (child->unsync_children) {
  960. if (mmu_pages_add(pvec, child, i))
  961. return -ENOSPC;
  962. ret = __mmu_unsync_walk(child, pvec);
  963. if (!ret)
  964. goto clear_child_bitmap;
  965. else if (ret > 0)
  966. nr_unsync_leaf += ret;
  967. else
  968. return ret;
  969. } else if (child->unsync) {
  970. nr_unsync_leaf++;
  971. if (mmu_pages_add(pvec, child, i))
  972. return -ENOSPC;
  973. } else
  974. goto clear_child_bitmap;
  975. continue;
  976. clear_child_bitmap:
  977. __clear_bit(i, sp->unsync_child_bitmap);
  978. sp->unsync_children--;
  979. WARN_ON((int)sp->unsync_children < 0);
  980. }
  981. return nr_unsync_leaf;
  982. }
  983. static int mmu_unsync_walk(struct kvm_mmu_page *sp,
  984. struct kvm_mmu_pages *pvec)
  985. {
  986. if (!sp->unsync_children)
  987. return 0;
  988. mmu_pages_add(pvec, sp, 0);
  989. return __mmu_unsync_walk(sp, pvec);
  990. }
  991. static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
  992. {
  993. WARN_ON(!sp->unsync);
  994. trace_kvm_mmu_sync_page(sp);
  995. sp->unsync = 0;
  996. --kvm->stat.mmu_unsync;
  997. }
  998. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  999. struct list_head *invalid_list);
  1000. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1001. struct list_head *invalid_list);
  1002. #define for_each_gfn_sp(kvm, sp, gfn, pos) \
  1003. hlist_for_each_entry(sp, pos, \
  1004. &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
  1005. if ((sp)->gfn != (gfn)) {} else
  1006. #define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
  1007. hlist_for_each_entry(sp, pos, \
  1008. &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
  1009. if ((sp)->gfn != (gfn) || (sp)->role.direct || \
  1010. (sp)->role.invalid) {} else
  1011. /* @sp->gfn should be write-protected at the call site */
  1012. static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1013. struct list_head *invalid_list, bool clear_unsync)
  1014. {
  1015. if (sp->role.cr4_pae != !!is_pae(vcpu)) {
  1016. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1017. return 1;
  1018. }
  1019. if (clear_unsync)
  1020. kvm_unlink_unsync_page(vcpu->kvm, sp);
  1021. if (vcpu->arch.mmu.sync_page(vcpu, sp, clear_unsync)) {
  1022. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1023. return 1;
  1024. }
  1025. kvm_mmu_flush_tlb(vcpu);
  1026. return 0;
  1027. }
  1028. static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
  1029. struct kvm_mmu_page *sp)
  1030. {
  1031. LIST_HEAD(invalid_list);
  1032. int ret;
  1033. ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
  1034. if (ret)
  1035. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1036. return ret;
  1037. }
  1038. static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1039. struct list_head *invalid_list)
  1040. {
  1041. return __kvm_sync_page(vcpu, sp, invalid_list, true);
  1042. }
  1043. /* @gfn should be write-protected at the call site */
  1044. static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
  1045. {
  1046. struct kvm_mmu_page *s;
  1047. struct hlist_node *node;
  1048. LIST_HEAD(invalid_list);
  1049. bool flush = false;
  1050. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
  1051. if (!s->unsync)
  1052. continue;
  1053. WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
  1054. if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
  1055. (vcpu->arch.mmu.sync_page(vcpu, s, true))) {
  1056. kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
  1057. continue;
  1058. }
  1059. kvm_unlink_unsync_page(vcpu->kvm, s);
  1060. flush = true;
  1061. }
  1062. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1063. if (flush)
  1064. kvm_mmu_flush_tlb(vcpu);
  1065. }
  1066. struct mmu_page_path {
  1067. struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
  1068. unsigned int idx[PT64_ROOT_LEVEL-1];
  1069. };
  1070. #define for_each_sp(pvec, sp, parents, i) \
  1071. for (i = mmu_pages_next(&pvec, &parents, -1), \
  1072. sp = pvec.page[i].sp; \
  1073. i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
  1074. i = mmu_pages_next(&pvec, &parents, i))
  1075. static int mmu_pages_next(struct kvm_mmu_pages *pvec,
  1076. struct mmu_page_path *parents,
  1077. int i)
  1078. {
  1079. int n;
  1080. for (n = i+1; n < pvec->nr; n++) {
  1081. struct kvm_mmu_page *sp = pvec->page[n].sp;
  1082. if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
  1083. parents->idx[0] = pvec->page[n].idx;
  1084. return n;
  1085. }
  1086. parents->parent[sp->role.level-2] = sp;
  1087. parents->idx[sp->role.level-1] = pvec->page[n].idx;
  1088. }
  1089. return n;
  1090. }
  1091. static void mmu_pages_clear_parents(struct mmu_page_path *parents)
  1092. {
  1093. struct kvm_mmu_page *sp;
  1094. unsigned int level = 0;
  1095. do {
  1096. unsigned int idx = parents->idx[level];
  1097. sp = parents->parent[level];
  1098. if (!sp)
  1099. return;
  1100. --sp->unsync_children;
  1101. WARN_ON((int)sp->unsync_children < 0);
  1102. __clear_bit(idx, sp->unsync_child_bitmap);
  1103. level++;
  1104. } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
  1105. }
  1106. static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
  1107. struct mmu_page_path *parents,
  1108. struct kvm_mmu_pages *pvec)
  1109. {
  1110. parents->parent[parent->role.level-1] = NULL;
  1111. pvec->nr = 0;
  1112. }
  1113. static void mmu_sync_children(struct kvm_vcpu *vcpu,
  1114. struct kvm_mmu_page *parent)
  1115. {
  1116. int i;
  1117. struct kvm_mmu_page *sp;
  1118. struct mmu_page_path parents;
  1119. struct kvm_mmu_pages pages;
  1120. LIST_HEAD(invalid_list);
  1121. kvm_mmu_pages_init(parent, &parents, &pages);
  1122. while (mmu_unsync_walk(parent, &pages)) {
  1123. int protected = 0;
  1124. for_each_sp(pages, sp, parents, i)
  1125. protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
  1126. if (protected)
  1127. kvm_flush_remote_tlbs(vcpu->kvm);
  1128. for_each_sp(pages, sp, parents, i) {
  1129. kvm_sync_page(vcpu, sp, &invalid_list);
  1130. mmu_pages_clear_parents(&parents);
  1131. }
  1132. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1133. cond_resched_lock(&vcpu->kvm->mmu_lock);
  1134. kvm_mmu_pages_init(parent, &parents, &pages);
  1135. }
  1136. }
  1137. static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
  1138. gfn_t gfn,
  1139. gva_t gaddr,
  1140. unsigned level,
  1141. int direct,
  1142. unsigned access,
  1143. u64 *parent_pte)
  1144. {
  1145. union kvm_mmu_page_role role;
  1146. unsigned quadrant;
  1147. struct kvm_mmu_page *sp;
  1148. struct hlist_node *node;
  1149. bool need_sync = false;
  1150. role = vcpu->arch.mmu.base_role;
  1151. role.level = level;
  1152. role.direct = direct;
  1153. if (role.direct)
  1154. role.cr4_pae = 0;
  1155. role.access = access;
  1156. if (!tdp_enabled && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
  1157. quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
  1158. quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
  1159. role.quadrant = quadrant;
  1160. }
  1161. for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
  1162. if (!need_sync && sp->unsync)
  1163. need_sync = true;
  1164. if (sp->role.word != role.word)
  1165. continue;
  1166. if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
  1167. break;
  1168. mmu_page_add_parent_pte(vcpu, sp, parent_pte);
  1169. if (sp->unsync_children) {
  1170. set_bit(KVM_REQ_MMU_SYNC, &vcpu->requests);
  1171. kvm_mmu_mark_parents_unsync(sp);
  1172. } else if (sp->unsync)
  1173. kvm_mmu_mark_parents_unsync(sp);
  1174. trace_kvm_mmu_get_page(sp, false);
  1175. return sp;
  1176. }
  1177. ++vcpu->kvm->stat.mmu_cache_miss;
  1178. sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
  1179. if (!sp)
  1180. return sp;
  1181. sp->gfn = gfn;
  1182. sp->role = role;
  1183. hlist_add_head(&sp->hash_link,
  1184. &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
  1185. if (!direct) {
  1186. if (rmap_write_protect(vcpu->kvm, gfn))
  1187. kvm_flush_remote_tlbs(vcpu->kvm);
  1188. if (level > PT_PAGE_TABLE_LEVEL && need_sync)
  1189. kvm_sync_pages(vcpu, gfn);
  1190. account_shadowed(vcpu->kvm, gfn);
  1191. }
  1192. if (shadow_trap_nonpresent_pte != shadow_notrap_nonpresent_pte)
  1193. vcpu->arch.mmu.prefetch_page(vcpu, sp);
  1194. else
  1195. nonpaging_prefetch_page(vcpu, sp);
  1196. trace_kvm_mmu_get_page(sp, true);
  1197. return sp;
  1198. }
  1199. static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
  1200. struct kvm_vcpu *vcpu, u64 addr)
  1201. {
  1202. iterator->addr = addr;
  1203. iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
  1204. iterator->level = vcpu->arch.mmu.shadow_root_level;
  1205. if (iterator->level == PT32E_ROOT_LEVEL) {
  1206. iterator->shadow_addr
  1207. = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
  1208. iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
  1209. --iterator->level;
  1210. if (!iterator->shadow_addr)
  1211. iterator->level = 0;
  1212. }
  1213. }
  1214. static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
  1215. {
  1216. if (iterator->level < PT_PAGE_TABLE_LEVEL)
  1217. return false;
  1218. if (iterator->level == PT_PAGE_TABLE_LEVEL)
  1219. if (is_large_pte(*iterator->sptep))
  1220. return false;
  1221. iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
  1222. iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
  1223. return true;
  1224. }
  1225. static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
  1226. {
  1227. iterator->shadow_addr = *iterator->sptep & PT64_BASE_ADDR_MASK;
  1228. --iterator->level;
  1229. }
  1230. static void kvm_mmu_page_unlink_children(struct kvm *kvm,
  1231. struct kvm_mmu_page *sp)
  1232. {
  1233. unsigned i;
  1234. u64 *pt;
  1235. u64 ent;
  1236. pt = sp->spt;
  1237. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  1238. ent = pt[i];
  1239. if (is_shadow_present_pte(ent)) {
  1240. if (!is_last_spte(ent, sp->role.level)) {
  1241. ent &= PT64_BASE_ADDR_MASK;
  1242. mmu_page_remove_parent_pte(page_header(ent),
  1243. &pt[i]);
  1244. } else {
  1245. if (is_large_pte(ent))
  1246. --kvm->stat.lpages;
  1247. rmap_remove(kvm, &pt[i]);
  1248. }
  1249. }
  1250. pt[i] = shadow_trap_nonpresent_pte;
  1251. }
  1252. }
  1253. static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
  1254. {
  1255. mmu_page_remove_parent_pte(sp, parent_pte);
  1256. }
  1257. static void kvm_mmu_reset_last_pte_updated(struct kvm *kvm)
  1258. {
  1259. int i;
  1260. struct kvm_vcpu *vcpu;
  1261. kvm_for_each_vcpu(i, vcpu, kvm)
  1262. vcpu->arch.last_pte_updated = NULL;
  1263. }
  1264. static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
  1265. {
  1266. u64 *parent_pte;
  1267. while (sp->multimapped || sp->parent_pte) {
  1268. if (!sp->multimapped)
  1269. parent_pte = sp->parent_pte;
  1270. else {
  1271. struct kvm_pte_chain *chain;
  1272. chain = container_of(sp->parent_ptes.first,
  1273. struct kvm_pte_chain, link);
  1274. parent_pte = chain->parent_ptes[0];
  1275. }
  1276. BUG_ON(!parent_pte);
  1277. kvm_mmu_put_page(sp, parent_pte);
  1278. __set_spte(parent_pte, shadow_trap_nonpresent_pte);
  1279. }
  1280. }
  1281. static int mmu_zap_unsync_children(struct kvm *kvm,
  1282. struct kvm_mmu_page *parent,
  1283. struct list_head *invalid_list)
  1284. {
  1285. int i, zapped = 0;
  1286. struct mmu_page_path parents;
  1287. struct kvm_mmu_pages pages;
  1288. if (parent->role.level == PT_PAGE_TABLE_LEVEL)
  1289. return 0;
  1290. kvm_mmu_pages_init(parent, &parents, &pages);
  1291. while (mmu_unsync_walk(parent, &pages)) {
  1292. struct kvm_mmu_page *sp;
  1293. for_each_sp(pages, sp, parents, i) {
  1294. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  1295. mmu_pages_clear_parents(&parents);
  1296. zapped++;
  1297. }
  1298. kvm_mmu_pages_init(parent, &parents, &pages);
  1299. }
  1300. return zapped;
  1301. }
  1302. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1303. struct list_head *invalid_list)
  1304. {
  1305. int ret;
  1306. trace_kvm_mmu_prepare_zap_page(sp);
  1307. ++kvm->stat.mmu_shadow_zapped;
  1308. ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
  1309. kvm_mmu_page_unlink_children(kvm, sp);
  1310. kvm_mmu_unlink_parents(kvm, sp);
  1311. if (!sp->role.invalid && !sp->role.direct)
  1312. unaccount_shadowed(kvm, sp->gfn);
  1313. if (sp->unsync)
  1314. kvm_unlink_unsync_page(kvm, sp);
  1315. if (!sp->root_count) {
  1316. /* Count self */
  1317. ret++;
  1318. list_move(&sp->link, invalid_list);
  1319. } else {
  1320. list_move(&sp->link, &kvm->arch.active_mmu_pages);
  1321. kvm_reload_remote_mmus(kvm);
  1322. }
  1323. sp->role.invalid = 1;
  1324. kvm_mmu_reset_last_pte_updated(kvm);
  1325. return ret;
  1326. }
  1327. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1328. struct list_head *invalid_list)
  1329. {
  1330. struct kvm_mmu_page *sp;
  1331. if (list_empty(invalid_list))
  1332. return;
  1333. kvm_flush_remote_tlbs(kvm);
  1334. do {
  1335. sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
  1336. WARN_ON(!sp->role.invalid || sp->root_count);
  1337. kvm_mmu_free_page(kvm, sp);
  1338. } while (!list_empty(invalid_list));
  1339. }
  1340. /*
  1341. * Changing the number of mmu pages allocated to the vm
  1342. * Note: if kvm_nr_mmu_pages is too small, you will get dead lock
  1343. */
  1344. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages)
  1345. {
  1346. int used_pages;
  1347. LIST_HEAD(invalid_list);
  1348. used_pages = kvm->arch.n_alloc_mmu_pages - kvm->arch.n_free_mmu_pages;
  1349. used_pages = max(0, used_pages);
  1350. /*
  1351. * If we set the number of mmu pages to be smaller be than the
  1352. * number of actived pages , we must to free some mmu pages before we
  1353. * change the value
  1354. */
  1355. if (used_pages > kvm_nr_mmu_pages) {
  1356. while (used_pages > kvm_nr_mmu_pages &&
  1357. !list_empty(&kvm->arch.active_mmu_pages)) {
  1358. struct kvm_mmu_page *page;
  1359. page = container_of(kvm->arch.active_mmu_pages.prev,
  1360. struct kvm_mmu_page, link);
  1361. used_pages -= kvm_mmu_prepare_zap_page(kvm, page,
  1362. &invalid_list);
  1363. }
  1364. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  1365. kvm_nr_mmu_pages = used_pages;
  1366. kvm->arch.n_free_mmu_pages = 0;
  1367. }
  1368. else
  1369. kvm->arch.n_free_mmu_pages += kvm_nr_mmu_pages
  1370. - kvm->arch.n_alloc_mmu_pages;
  1371. kvm->arch.n_alloc_mmu_pages = kvm_nr_mmu_pages;
  1372. }
  1373. static int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
  1374. {
  1375. struct kvm_mmu_page *sp;
  1376. struct hlist_node *node;
  1377. LIST_HEAD(invalid_list);
  1378. int r;
  1379. pgprintk("%s: looking for gfn %lx\n", __func__, gfn);
  1380. r = 0;
  1381. for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
  1382. pgprintk("%s: gfn %lx role %x\n", __func__, gfn,
  1383. sp->role.word);
  1384. r = 1;
  1385. kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
  1386. }
  1387. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  1388. return r;
  1389. }
  1390. static void mmu_unshadow(struct kvm *kvm, gfn_t gfn)
  1391. {
  1392. struct kvm_mmu_page *sp;
  1393. struct hlist_node *node;
  1394. LIST_HEAD(invalid_list);
  1395. for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
  1396. pgprintk("%s: zap %lx %x\n",
  1397. __func__, gfn, sp->role.word);
  1398. kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
  1399. }
  1400. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  1401. }
  1402. static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
  1403. {
  1404. int slot = memslot_id(kvm, gfn);
  1405. struct kvm_mmu_page *sp = page_header(__pa(pte));
  1406. __set_bit(slot, sp->slot_bitmap);
  1407. }
  1408. static void mmu_convert_notrap(struct kvm_mmu_page *sp)
  1409. {
  1410. int i;
  1411. u64 *pt = sp->spt;
  1412. if (shadow_trap_nonpresent_pte == shadow_notrap_nonpresent_pte)
  1413. return;
  1414. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  1415. if (pt[i] == shadow_notrap_nonpresent_pte)
  1416. __set_spte(&pt[i], shadow_trap_nonpresent_pte);
  1417. }
  1418. }
  1419. /*
  1420. * The function is based on mtrr_type_lookup() in
  1421. * arch/x86/kernel/cpu/mtrr/generic.c
  1422. */
  1423. static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
  1424. u64 start, u64 end)
  1425. {
  1426. int i;
  1427. u64 base, mask;
  1428. u8 prev_match, curr_match;
  1429. int num_var_ranges = KVM_NR_VAR_MTRR;
  1430. if (!mtrr_state->enabled)
  1431. return 0xFF;
  1432. /* Make end inclusive end, instead of exclusive */
  1433. end--;
  1434. /* Look in fixed ranges. Just return the type as per start */
  1435. if (mtrr_state->have_fixed && (start < 0x100000)) {
  1436. int idx;
  1437. if (start < 0x80000) {
  1438. idx = 0;
  1439. idx += (start >> 16);
  1440. return mtrr_state->fixed_ranges[idx];
  1441. } else if (start < 0xC0000) {
  1442. idx = 1 * 8;
  1443. idx += ((start - 0x80000) >> 14);
  1444. return mtrr_state->fixed_ranges[idx];
  1445. } else if (start < 0x1000000) {
  1446. idx = 3 * 8;
  1447. idx += ((start - 0xC0000) >> 12);
  1448. return mtrr_state->fixed_ranges[idx];
  1449. }
  1450. }
  1451. /*
  1452. * Look in variable ranges
  1453. * Look of multiple ranges matching this address and pick type
  1454. * as per MTRR precedence
  1455. */
  1456. if (!(mtrr_state->enabled & 2))
  1457. return mtrr_state->def_type;
  1458. prev_match = 0xFF;
  1459. for (i = 0; i < num_var_ranges; ++i) {
  1460. unsigned short start_state, end_state;
  1461. if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
  1462. continue;
  1463. base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
  1464. (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
  1465. mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
  1466. (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
  1467. start_state = ((start & mask) == (base & mask));
  1468. end_state = ((end & mask) == (base & mask));
  1469. if (start_state != end_state)
  1470. return 0xFE;
  1471. if ((start & mask) != (base & mask))
  1472. continue;
  1473. curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
  1474. if (prev_match == 0xFF) {
  1475. prev_match = curr_match;
  1476. continue;
  1477. }
  1478. if (prev_match == MTRR_TYPE_UNCACHABLE ||
  1479. curr_match == MTRR_TYPE_UNCACHABLE)
  1480. return MTRR_TYPE_UNCACHABLE;
  1481. if ((prev_match == MTRR_TYPE_WRBACK &&
  1482. curr_match == MTRR_TYPE_WRTHROUGH) ||
  1483. (prev_match == MTRR_TYPE_WRTHROUGH &&
  1484. curr_match == MTRR_TYPE_WRBACK)) {
  1485. prev_match = MTRR_TYPE_WRTHROUGH;
  1486. curr_match = MTRR_TYPE_WRTHROUGH;
  1487. }
  1488. if (prev_match != curr_match)
  1489. return MTRR_TYPE_UNCACHABLE;
  1490. }
  1491. if (prev_match != 0xFF)
  1492. return prev_match;
  1493. return mtrr_state->def_type;
  1494. }
  1495. u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
  1496. {
  1497. u8 mtrr;
  1498. mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
  1499. (gfn << PAGE_SHIFT) + PAGE_SIZE);
  1500. if (mtrr == 0xfe || mtrr == 0xff)
  1501. mtrr = MTRR_TYPE_WRBACK;
  1502. return mtrr;
  1503. }
  1504. EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
  1505. static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  1506. {
  1507. trace_kvm_mmu_unsync_page(sp);
  1508. ++vcpu->kvm->stat.mmu_unsync;
  1509. sp->unsync = 1;
  1510. kvm_mmu_mark_parents_unsync(sp);
  1511. mmu_convert_notrap(sp);
  1512. }
  1513. static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
  1514. {
  1515. struct kvm_mmu_page *s;
  1516. struct hlist_node *node;
  1517. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
  1518. if (s->unsync)
  1519. continue;
  1520. WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
  1521. __kvm_unsync_page(vcpu, s);
  1522. }
  1523. }
  1524. static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
  1525. bool can_unsync)
  1526. {
  1527. struct kvm_mmu_page *s;
  1528. struct hlist_node *node;
  1529. bool need_unsync = false;
  1530. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
  1531. if (s->role.level != PT_PAGE_TABLE_LEVEL)
  1532. return 1;
  1533. if (!need_unsync && !s->unsync) {
  1534. if (!can_unsync || !oos_shadow)
  1535. return 1;
  1536. need_unsync = true;
  1537. }
  1538. }
  1539. if (need_unsync)
  1540. kvm_unsync_pages(vcpu, gfn);
  1541. return 0;
  1542. }
  1543. static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  1544. unsigned pte_access, int user_fault,
  1545. int write_fault, int dirty, int level,
  1546. gfn_t gfn, pfn_t pfn, bool speculative,
  1547. bool can_unsync, bool reset_host_protection)
  1548. {
  1549. u64 spte;
  1550. int ret = 0;
  1551. /*
  1552. * We don't set the accessed bit, since we sometimes want to see
  1553. * whether the guest actually used the pte (in order to detect
  1554. * demand paging).
  1555. */
  1556. spte = shadow_base_present_pte | shadow_dirty_mask;
  1557. if (!speculative)
  1558. spte |= shadow_accessed_mask;
  1559. if (!dirty)
  1560. pte_access &= ~ACC_WRITE_MASK;
  1561. if (pte_access & ACC_EXEC_MASK)
  1562. spte |= shadow_x_mask;
  1563. else
  1564. spte |= shadow_nx_mask;
  1565. if (pte_access & ACC_USER_MASK)
  1566. spte |= shadow_user_mask;
  1567. if (level > PT_PAGE_TABLE_LEVEL)
  1568. spte |= PT_PAGE_SIZE_MASK;
  1569. if (tdp_enabled)
  1570. spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
  1571. kvm_is_mmio_pfn(pfn));
  1572. if (reset_host_protection)
  1573. spte |= SPTE_HOST_WRITEABLE;
  1574. spte |= (u64)pfn << PAGE_SHIFT;
  1575. if ((pte_access & ACC_WRITE_MASK)
  1576. || (!tdp_enabled && write_fault && !is_write_protection(vcpu)
  1577. && !user_fault)) {
  1578. if (level > PT_PAGE_TABLE_LEVEL &&
  1579. has_wrprotected_page(vcpu->kvm, gfn, level)) {
  1580. ret = 1;
  1581. rmap_remove(vcpu->kvm, sptep);
  1582. spte = shadow_trap_nonpresent_pte;
  1583. goto set_pte;
  1584. }
  1585. spte |= PT_WRITABLE_MASK;
  1586. if (!tdp_enabled && !(pte_access & ACC_WRITE_MASK))
  1587. spte &= ~PT_USER_MASK;
  1588. /*
  1589. * Optimization: for pte sync, if spte was writable the hash
  1590. * lookup is unnecessary (and expensive). Write protection
  1591. * is responsibility of mmu_get_page / kvm_sync_page.
  1592. * Same reasoning can be applied to dirty page accounting.
  1593. */
  1594. if (!can_unsync && is_writable_pte(*sptep))
  1595. goto set_pte;
  1596. if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
  1597. pgprintk("%s: found shadow page for %lx, marking ro\n",
  1598. __func__, gfn);
  1599. ret = 1;
  1600. pte_access &= ~ACC_WRITE_MASK;
  1601. if (is_writable_pte(spte))
  1602. spte &= ~PT_WRITABLE_MASK;
  1603. }
  1604. }
  1605. if (pte_access & ACC_WRITE_MASK)
  1606. mark_page_dirty(vcpu->kvm, gfn);
  1607. set_pte:
  1608. __set_spte(sptep, spte);
  1609. return ret;
  1610. }
  1611. static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  1612. unsigned pt_access, unsigned pte_access,
  1613. int user_fault, int write_fault, int dirty,
  1614. int *ptwrite, int level, gfn_t gfn,
  1615. pfn_t pfn, bool speculative,
  1616. bool reset_host_protection)
  1617. {
  1618. int was_rmapped = 0;
  1619. int was_writable = is_writable_pte(*sptep);
  1620. int rmap_count;
  1621. pgprintk("%s: spte %llx access %x write_fault %d"
  1622. " user_fault %d gfn %lx\n",
  1623. __func__, *sptep, pt_access,
  1624. write_fault, user_fault, gfn);
  1625. if (is_rmap_spte(*sptep)) {
  1626. /*
  1627. * If we overwrite a PTE page pointer with a 2MB PMD, unlink
  1628. * the parent of the now unreachable PTE.
  1629. */
  1630. if (level > PT_PAGE_TABLE_LEVEL &&
  1631. !is_large_pte(*sptep)) {
  1632. struct kvm_mmu_page *child;
  1633. u64 pte = *sptep;
  1634. child = page_header(pte & PT64_BASE_ADDR_MASK);
  1635. mmu_page_remove_parent_pte(child, sptep);
  1636. __set_spte(sptep, shadow_trap_nonpresent_pte);
  1637. kvm_flush_remote_tlbs(vcpu->kvm);
  1638. } else if (pfn != spte_to_pfn(*sptep)) {
  1639. pgprintk("hfn old %lx new %lx\n",
  1640. spte_to_pfn(*sptep), pfn);
  1641. rmap_remove(vcpu->kvm, sptep);
  1642. __set_spte(sptep, shadow_trap_nonpresent_pte);
  1643. kvm_flush_remote_tlbs(vcpu->kvm);
  1644. } else
  1645. was_rmapped = 1;
  1646. }
  1647. if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
  1648. dirty, level, gfn, pfn, speculative, true,
  1649. reset_host_protection)) {
  1650. if (write_fault)
  1651. *ptwrite = 1;
  1652. kvm_mmu_flush_tlb(vcpu);
  1653. }
  1654. pgprintk("%s: setting spte %llx\n", __func__, *sptep);
  1655. pgprintk("instantiating %s PTE (%s) at %ld (%llx) addr %p\n",
  1656. is_large_pte(*sptep)? "2MB" : "4kB",
  1657. *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
  1658. *sptep, sptep);
  1659. if (!was_rmapped && is_large_pte(*sptep))
  1660. ++vcpu->kvm->stat.lpages;
  1661. page_header_update_slot(vcpu->kvm, sptep, gfn);
  1662. if (!was_rmapped) {
  1663. rmap_count = rmap_add(vcpu, sptep, gfn);
  1664. kvm_release_pfn_clean(pfn);
  1665. if (rmap_count > RMAP_RECYCLE_THRESHOLD)
  1666. rmap_recycle(vcpu, sptep, gfn);
  1667. } else {
  1668. if (was_writable)
  1669. kvm_release_pfn_dirty(pfn);
  1670. else
  1671. kvm_release_pfn_clean(pfn);
  1672. }
  1673. if (speculative) {
  1674. vcpu->arch.last_pte_updated = sptep;
  1675. vcpu->arch.last_pte_gfn = gfn;
  1676. }
  1677. }
  1678. static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
  1679. {
  1680. }
  1681. static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
  1682. int level, gfn_t gfn, pfn_t pfn)
  1683. {
  1684. struct kvm_shadow_walk_iterator iterator;
  1685. struct kvm_mmu_page *sp;
  1686. int pt_write = 0;
  1687. gfn_t pseudo_gfn;
  1688. for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
  1689. if (iterator.level == level) {
  1690. mmu_set_spte(vcpu, iterator.sptep, ACC_ALL, ACC_ALL,
  1691. 0, write, 1, &pt_write,
  1692. level, gfn, pfn, false, true);
  1693. ++vcpu->stat.pf_fixed;
  1694. break;
  1695. }
  1696. if (*iterator.sptep == shadow_trap_nonpresent_pte) {
  1697. u64 base_addr = iterator.addr;
  1698. base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
  1699. pseudo_gfn = base_addr >> PAGE_SHIFT;
  1700. sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
  1701. iterator.level - 1,
  1702. 1, ACC_ALL, iterator.sptep);
  1703. if (!sp) {
  1704. pgprintk("nonpaging_map: ENOMEM\n");
  1705. kvm_release_pfn_clean(pfn);
  1706. return -ENOMEM;
  1707. }
  1708. __set_spte(iterator.sptep,
  1709. __pa(sp->spt)
  1710. | PT_PRESENT_MASK | PT_WRITABLE_MASK
  1711. | shadow_user_mask | shadow_x_mask);
  1712. }
  1713. }
  1714. return pt_write;
  1715. }
  1716. static void kvm_send_hwpoison_signal(struct kvm *kvm, gfn_t gfn)
  1717. {
  1718. char buf[1];
  1719. void __user *hva;
  1720. int r;
  1721. /* Touch the page, so send SIGBUS */
  1722. hva = (void __user *)gfn_to_hva(kvm, gfn);
  1723. r = copy_from_user(buf, hva, 1);
  1724. }
  1725. static int kvm_handle_bad_page(struct kvm *kvm, gfn_t gfn, pfn_t pfn)
  1726. {
  1727. kvm_release_pfn_clean(pfn);
  1728. if (is_hwpoison_pfn(pfn)) {
  1729. kvm_send_hwpoison_signal(kvm, gfn);
  1730. return 0;
  1731. }
  1732. return 1;
  1733. }
  1734. static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, int write, gfn_t gfn)
  1735. {
  1736. int r;
  1737. int level;
  1738. pfn_t pfn;
  1739. unsigned long mmu_seq;
  1740. level = mapping_level(vcpu, gfn);
  1741. /*
  1742. * This path builds a PAE pagetable - so we can map 2mb pages at
  1743. * maximum. Therefore check if the level is larger than that.
  1744. */
  1745. if (level > PT_DIRECTORY_LEVEL)
  1746. level = PT_DIRECTORY_LEVEL;
  1747. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  1748. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  1749. smp_rmb();
  1750. pfn = gfn_to_pfn(vcpu->kvm, gfn);
  1751. /* mmio */
  1752. if (is_error_pfn(pfn))
  1753. return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
  1754. spin_lock(&vcpu->kvm->mmu_lock);
  1755. if (mmu_notifier_retry(vcpu, mmu_seq))
  1756. goto out_unlock;
  1757. kvm_mmu_free_some_pages(vcpu);
  1758. r = __direct_map(vcpu, v, write, level, gfn, pfn);
  1759. spin_unlock(&vcpu->kvm->mmu_lock);
  1760. return r;
  1761. out_unlock:
  1762. spin_unlock(&vcpu->kvm->mmu_lock);
  1763. kvm_release_pfn_clean(pfn);
  1764. return 0;
  1765. }
  1766. static void mmu_free_roots(struct kvm_vcpu *vcpu)
  1767. {
  1768. int i;
  1769. struct kvm_mmu_page *sp;
  1770. LIST_HEAD(invalid_list);
  1771. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  1772. return;
  1773. spin_lock(&vcpu->kvm->mmu_lock);
  1774. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  1775. hpa_t root = vcpu->arch.mmu.root_hpa;
  1776. sp = page_header(root);
  1777. --sp->root_count;
  1778. if (!sp->root_count && sp->role.invalid) {
  1779. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
  1780. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1781. }
  1782. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  1783. spin_unlock(&vcpu->kvm->mmu_lock);
  1784. return;
  1785. }
  1786. for (i = 0; i < 4; ++i) {
  1787. hpa_t root = vcpu->arch.mmu.pae_root[i];
  1788. if (root) {
  1789. root &= PT64_BASE_ADDR_MASK;
  1790. sp = page_header(root);
  1791. --sp->root_count;
  1792. if (!sp->root_count && sp->role.invalid)
  1793. kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
  1794. &invalid_list);
  1795. }
  1796. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  1797. }
  1798. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1799. spin_unlock(&vcpu->kvm->mmu_lock);
  1800. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  1801. }
  1802. static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
  1803. {
  1804. int ret = 0;
  1805. if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
  1806. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  1807. ret = 1;
  1808. }
  1809. return ret;
  1810. }
  1811. static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
  1812. {
  1813. int i;
  1814. gfn_t root_gfn;
  1815. struct kvm_mmu_page *sp;
  1816. int direct = 0;
  1817. u64 pdptr;
  1818. root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
  1819. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  1820. hpa_t root = vcpu->arch.mmu.root_hpa;
  1821. ASSERT(!VALID_PAGE(root));
  1822. if (mmu_check_root(vcpu, root_gfn))
  1823. return 1;
  1824. if (tdp_enabled) {
  1825. direct = 1;
  1826. root_gfn = 0;
  1827. }
  1828. spin_lock(&vcpu->kvm->mmu_lock);
  1829. kvm_mmu_free_some_pages(vcpu);
  1830. sp = kvm_mmu_get_page(vcpu, root_gfn, 0,
  1831. PT64_ROOT_LEVEL, direct,
  1832. ACC_ALL, NULL);
  1833. root = __pa(sp->spt);
  1834. ++sp->root_count;
  1835. spin_unlock(&vcpu->kvm->mmu_lock);
  1836. vcpu->arch.mmu.root_hpa = root;
  1837. return 0;
  1838. }
  1839. direct = !is_paging(vcpu);
  1840. for (i = 0; i < 4; ++i) {
  1841. hpa_t root = vcpu->arch.mmu.pae_root[i];
  1842. ASSERT(!VALID_PAGE(root));
  1843. if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
  1844. pdptr = kvm_pdptr_read(vcpu, i);
  1845. if (!is_present_gpte(pdptr)) {
  1846. vcpu->arch.mmu.pae_root[i] = 0;
  1847. continue;
  1848. }
  1849. root_gfn = pdptr >> PAGE_SHIFT;
  1850. } else if (vcpu->arch.mmu.root_level == 0)
  1851. root_gfn = 0;
  1852. if (mmu_check_root(vcpu, root_gfn))
  1853. return 1;
  1854. if (tdp_enabled) {
  1855. direct = 1;
  1856. root_gfn = i << 30;
  1857. }
  1858. spin_lock(&vcpu->kvm->mmu_lock);
  1859. kvm_mmu_free_some_pages(vcpu);
  1860. sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30,
  1861. PT32_ROOT_LEVEL, direct,
  1862. ACC_ALL, NULL);
  1863. root = __pa(sp->spt);
  1864. ++sp->root_count;
  1865. spin_unlock(&vcpu->kvm->mmu_lock);
  1866. vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
  1867. }
  1868. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
  1869. return 0;
  1870. }
  1871. static void mmu_sync_roots(struct kvm_vcpu *vcpu)
  1872. {
  1873. int i;
  1874. struct kvm_mmu_page *sp;
  1875. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  1876. return;
  1877. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  1878. hpa_t root = vcpu->arch.mmu.root_hpa;
  1879. sp = page_header(root);
  1880. mmu_sync_children(vcpu, sp);
  1881. return;
  1882. }
  1883. for (i = 0; i < 4; ++i) {
  1884. hpa_t root = vcpu->arch.mmu.pae_root[i];
  1885. if (root && VALID_PAGE(root)) {
  1886. root &= PT64_BASE_ADDR_MASK;
  1887. sp = page_header(root);
  1888. mmu_sync_children(vcpu, sp);
  1889. }
  1890. }
  1891. }
  1892. void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
  1893. {
  1894. spin_lock(&vcpu->kvm->mmu_lock);
  1895. mmu_sync_roots(vcpu);
  1896. spin_unlock(&vcpu->kvm->mmu_lock);
  1897. }
  1898. static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
  1899. u32 access, u32 *error)
  1900. {
  1901. if (error)
  1902. *error = 0;
  1903. return vaddr;
  1904. }
  1905. static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
  1906. u32 error_code)
  1907. {
  1908. gfn_t gfn;
  1909. int r;
  1910. pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
  1911. r = mmu_topup_memory_caches(vcpu);
  1912. if (r)
  1913. return r;
  1914. ASSERT(vcpu);
  1915. ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  1916. gfn = gva >> PAGE_SHIFT;
  1917. return nonpaging_map(vcpu, gva & PAGE_MASK,
  1918. error_code & PFERR_WRITE_MASK, gfn);
  1919. }
  1920. static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
  1921. u32 error_code)
  1922. {
  1923. pfn_t pfn;
  1924. int r;
  1925. int level;
  1926. gfn_t gfn = gpa >> PAGE_SHIFT;
  1927. unsigned long mmu_seq;
  1928. ASSERT(vcpu);
  1929. ASSERT(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  1930. r = mmu_topup_memory_caches(vcpu);
  1931. if (r)
  1932. return r;
  1933. level = mapping_level(vcpu, gfn);
  1934. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  1935. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  1936. smp_rmb();
  1937. pfn = gfn_to_pfn(vcpu->kvm, gfn);
  1938. if (is_error_pfn(pfn))
  1939. return kvm_handle_bad_page(vcpu->kvm, gfn, pfn);
  1940. spin_lock(&vcpu->kvm->mmu_lock);
  1941. if (mmu_notifier_retry(vcpu, mmu_seq))
  1942. goto out_unlock;
  1943. kvm_mmu_free_some_pages(vcpu);
  1944. r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
  1945. level, gfn, pfn);
  1946. spin_unlock(&vcpu->kvm->mmu_lock);
  1947. return r;
  1948. out_unlock:
  1949. spin_unlock(&vcpu->kvm->mmu_lock);
  1950. kvm_release_pfn_clean(pfn);
  1951. return 0;
  1952. }
  1953. static void nonpaging_free(struct kvm_vcpu *vcpu)
  1954. {
  1955. mmu_free_roots(vcpu);
  1956. }
  1957. static int nonpaging_init_context(struct kvm_vcpu *vcpu)
  1958. {
  1959. struct kvm_mmu *context = &vcpu->arch.mmu;
  1960. context->new_cr3 = nonpaging_new_cr3;
  1961. context->page_fault = nonpaging_page_fault;
  1962. context->gva_to_gpa = nonpaging_gva_to_gpa;
  1963. context->free = nonpaging_free;
  1964. context->prefetch_page = nonpaging_prefetch_page;
  1965. context->sync_page = nonpaging_sync_page;
  1966. context->invlpg = nonpaging_invlpg;
  1967. context->root_level = 0;
  1968. context->shadow_root_level = PT32E_ROOT_LEVEL;
  1969. context->root_hpa = INVALID_PAGE;
  1970. return 0;
  1971. }
  1972. void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu)
  1973. {
  1974. ++vcpu->stat.tlb_flush;
  1975. set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
  1976. }
  1977. static void paging_new_cr3(struct kvm_vcpu *vcpu)
  1978. {
  1979. pgprintk("%s: cr3 %lx\n", __func__, vcpu->arch.cr3);
  1980. mmu_free_roots(vcpu);
  1981. }
  1982. static void inject_page_fault(struct kvm_vcpu *vcpu,
  1983. u64 addr,
  1984. u32 err_code)
  1985. {
  1986. kvm_inject_page_fault(vcpu, addr, err_code);
  1987. }
  1988. static void paging_free(struct kvm_vcpu *vcpu)
  1989. {
  1990. nonpaging_free(vcpu);
  1991. }
  1992. static bool is_rsvd_bits_set(struct kvm_vcpu *vcpu, u64 gpte, int level)
  1993. {
  1994. int bit7;
  1995. bit7 = (gpte >> 7) & 1;
  1996. return (gpte & vcpu->arch.mmu.rsvd_bits_mask[bit7][level-1]) != 0;
  1997. }
  1998. #define PTTYPE 64
  1999. #include "paging_tmpl.h"
  2000. #undef PTTYPE
  2001. #define PTTYPE 32
  2002. #include "paging_tmpl.h"
  2003. #undef PTTYPE
  2004. static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu, int level)
  2005. {
  2006. struct kvm_mmu *context = &vcpu->arch.mmu;
  2007. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  2008. u64 exb_bit_rsvd = 0;
  2009. if (!is_nx(vcpu))
  2010. exb_bit_rsvd = rsvd_bits(63, 63);
  2011. switch (level) {
  2012. case PT32_ROOT_LEVEL:
  2013. /* no rsvd bits for 2 level 4K page table entries */
  2014. context->rsvd_bits_mask[0][1] = 0;
  2015. context->rsvd_bits_mask[0][0] = 0;
  2016. context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
  2017. if (!is_pse(vcpu)) {
  2018. context->rsvd_bits_mask[1][1] = 0;
  2019. break;
  2020. }
  2021. if (is_cpuid_PSE36())
  2022. /* 36bits PSE 4MB page */
  2023. context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
  2024. else
  2025. /* 32 bits PSE 4MB page */
  2026. context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
  2027. break;
  2028. case PT32E_ROOT_LEVEL:
  2029. context->rsvd_bits_mask[0][2] =
  2030. rsvd_bits(maxphyaddr, 63) |
  2031. rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
  2032. context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  2033. rsvd_bits(maxphyaddr, 62); /* PDE */
  2034. context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  2035. rsvd_bits(maxphyaddr, 62); /* PTE */
  2036. context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  2037. rsvd_bits(maxphyaddr, 62) |
  2038. rsvd_bits(13, 20); /* large page */
  2039. context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
  2040. break;
  2041. case PT64_ROOT_LEVEL:
  2042. context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
  2043. rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
  2044. context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
  2045. rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
  2046. context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  2047. rsvd_bits(maxphyaddr, 51);
  2048. context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  2049. rsvd_bits(maxphyaddr, 51);
  2050. context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
  2051. context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
  2052. rsvd_bits(maxphyaddr, 51) |
  2053. rsvd_bits(13, 29);
  2054. context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  2055. rsvd_bits(maxphyaddr, 51) |
  2056. rsvd_bits(13, 20); /* large page */
  2057. context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
  2058. break;
  2059. }
  2060. }
  2061. static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)
  2062. {
  2063. struct kvm_mmu *context = &vcpu->arch.mmu;
  2064. ASSERT(is_pae(vcpu));
  2065. context->new_cr3 = paging_new_cr3;
  2066. context->page_fault = paging64_page_fault;
  2067. context->gva_to_gpa = paging64_gva_to_gpa;
  2068. context->prefetch_page = paging64_prefetch_page;
  2069. context->sync_page = paging64_sync_page;
  2070. context->invlpg = paging64_invlpg;
  2071. context->free = paging_free;
  2072. context->root_level = level;
  2073. context->shadow_root_level = level;
  2074. context->root_hpa = INVALID_PAGE;
  2075. return 0;
  2076. }
  2077. static int paging64_init_context(struct kvm_vcpu *vcpu)
  2078. {
  2079. reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
  2080. return paging64_init_context_common(vcpu, PT64_ROOT_LEVEL);
  2081. }
  2082. static int paging32_init_context(struct kvm_vcpu *vcpu)
  2083. {
  2084. struct kvm_mmu *context = &vcpu->arch.mmu;
  2085. reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
  2086. context->new_cr3 = paging_new_cr3;
  2087. context->page_fault = paging32_page_fault;
  2088. context->gva_to_gpa = paging32_gva_to_gpa;
  2089. context->free = paging_free;
  2090. context->prefetch_page = paging32_prefetch_page;
  2091. context->sync_page = paging32_sync_page;
  2092. context->invlpg = paging32_invlpg;
  2093. context->root_level = PT32_ROOT_LEVEL;
  2094. context->shadow_root_level = PT32E_ROOT_LEVEL;
  2095. context->root_hpa = INVALID_PAGE;
  2096. return 0;
  2097. }
  2098. static int paging32E_init_context(struct kvm_vcpu *vcpu)
  2099. {
  2100. reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
  2101. return paging64_init_context_common(vcpu, PT32E_ROOT_LEVEL);
  2102. }
  2103. static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
  2104. {
  2105. struct kvm_mmu *context = &vcpu->arch.mmu;
  2106. context->new_cr3 = nonpaging_new_cr3;
  2107. context->page_fault = tdp_page_fault;
  2108. context->free = nonpaging_free;
  2109. context->prefetch_page = nonpaging_prefetch_page;
  2110. context->sync_page = nonpaging_sync_page;
  2111. context->invlpg = nonpaging_invlpg;
  2112. context->shadow_root_level = kvm_x86_ops->get_tdp_level();
  2113. context->root_hpa = INVALID_PAGE;
  2114. if (!is_paging(vcpu)) {
  2115. context->gva_to_gpa = nonpaging_gva_to_gpa;
  2116. context->root_level = 0;
  2117. } else if (is_long_mode(vcpu)) {
  2118. reset_rsvds_bits_mask(vcpu, PT64_ROOT_LEVEL);
  2119. context->gva_to_gpa = paging64_gva_to_gpa;
  2120. context->root_level = PT64_ROOT_LEVEL;
  2121. } else if (is_pae(vcpu)) {
  2122. reset_rsvds_bits_mask(vcpu, PT32E_ROOT_LEVEL);
  2123. context->gva_to_gpa = paging64_gva_to_gpa;
  2124. context->root_level = PT32E_ROOT_LEVEL;
  2125. } else {
  2126. reset_rsvds_bits_mask(vcpu, PT32_ROOT_LEVEL);
  2127. context->gva_to_gpa = paging32_gva_to_gpa;
  2128. context->root_level = PT32_ROOT_LEVEL;
  2129. }
  2130. return 0;
  2131. }
  2132. static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
  2133. {
  2134. int r;
  2135. ASSERT(vcpu);
  2136. ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2137. if (!is_paging(vcpu))
  2138. r = nonpaging_init_context(vcpu);
  2139. else if (is_long_mode(vcpu))
  2140. r = paging64_init_context(vcpu);
  2141. else if (is_pae(vcpu))
  2142. r = paging32E_init_context(vcpu);
  2143. else
  2144. r = paging32_init_context(vcpu);
  2145. vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
  2146. vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
  2147. return r;
  2148. }
  2149. static int init_kvm_mmu(struct kvm_vcpu *vcpu)
  2150. {
  2151. vcpu->arch.update_pte.pfn = bad_pfn;
  2152. if (tdp_enabled)
  2153. return init_kvm_tdp_mmu(vcpu);
  2154. else
  2155. return init_kvm_softmmu(vcpu);
  2156. }
  2157. static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
  2158. {
  2159. ASSERT(vcpu);
  2160. if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2161. /* mmu.free() should set root_hpa = INVALID_PAGE */
  2162. vcpu->arch.mmu.free(vcpu);
  2163. }
  2164. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
  2165. {
  2166. destroy_kvm_mmu(vcpu);
  2167. return init_kvm_mmu(vcpu);
  2168. }
  2169. EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
  2170. int kvm_mmu_load(struct kvm_vcpu *vcpu)
  2171. {
  2172. int r;
  2173. r = mmu_topup_memory_caches(vcpu);
  2174. if (r)
  2175. goto out;
  2176. r = mmu_alloc_roots(vcpu);
  2177. spin_lock(&vcpu->kvm->mmu_lock);
  2178. mmu_sync_roots(vcpu);
  2179. spin_unlock(&vcpu->kvm->mmu_lock);
  2180. if (r)
  2181. goto out;
  2182. /* set_cr3() should ensure TLB has been flushed */
  2183. kvm_x86_ops->set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
  2184. out:
  2185. return r;
  2186. }
  2187. EXPORT_SYMBOL_GPL(kvm_mmu_load);
  2188. void kvm_mmu_unload(struct kvm_vcpu *vcpu)
  2189. {
  2190. mmu_free_roots(vcpu);
  2191. }
  2192. static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu,
  2193. struct kvm_mmu_page *sp,
  2194. u64 *spte)
  2195. {
  2196. u64 pte;
  2197. struct kvm_mmu_page *child;
  2198. pte = *spte;
  2199. if (is_shadow_present_pte(pte)) {
  2200. if (is_last_spte(pte, sp->role.level))
  2201. rmap_remove(vcpu->kvm, spte);
  2202. else {
  2203. child = page_header(pte & PT64_BASE_ADDR_MASK);
  2204. mmu_page_remove_parent_pte(child, spte);
  2205. }
  2206. }
  2207. __set_spte(spte, shadow_trap_nonpresent_pte);
  2208. if (is_large_pte(pte))
  2209. --vcpu->kvm->stat.lpages;
  2210. }
  2211. static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
  2212. struct kvm_mmu_page *sp,
  2213. u64 *spte,
  2214. const void *new)
  2215. {
  2216. if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
  2217. ++vcpu->kvm->stat.mmu_pde_zapped;
  2218. return;
  2219. }
  2220. ++vcpu->kvm->stat.mmu_pte_updated;
  2221. if (!sp->role.cr4_pae)
  2222. paging32_update_pte(vcpu, sp, spte, new);
  2223. else
  2224. paging64_update_pte(vcpu, sp, spte, new);
  2225. }
  2226. static bool need_remote_flush(u64 old, u64 new)
  2227. {
  2228. if (!is_shadow_present_pte(old))
  2229. return false;
  2230. if (!is_shadow_present_pte(new))
  2231. return true;
  2232. if ((old ^ new) & PT64_BASE_ADDR_MASK)
  2233. return true;
  2234. old ^= PT64_NX_MASK;
  2235. new ^= PT64_NX_MASK;
  2236. return (old & ~new & PT64_PERM_MASK) != 0;
  2237. }
  2238. static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
  2239. bool remote_flush, bool local_flush)
  2240. {
  2241. if (zap_page)
  2242. return;
  2243. if (remote_flush)
  2244. kvm_flush_remote_tlbs(vcpu->kvm);
  2245. else if (local_flush)
  2246. kvm_mmu_flush_tlb(vcpu);
  2247. }
  2248. static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
  2249. {
  2250. u64 *spte = vcpu->arch.last_pte_updated;
  2251. return !!(spte && (*spte & shadow_accessed_mask));
  2252. }
  2253. static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  2254. u64 gpte)
  2255. {
  2256. gfn_t gfn;
  2257. pfn_t pfn;
  2258. if (!is_present_gpte(gpte))
  2259. return;
  2260. gfn = (gpte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
  2261. vcpu->arch.update_pte.mmu_seq = vcpu->kvm->mmu_notifier_seq;
  2262. smp_rmb();
  2263. pfn = gfn_to_pfn(vcpu->kvm, gfn);
  2264. if (is_error_pfn(pfn)) {
  2265. kvm_release_pfn_clean(pfn);
  2266. return;
  2267. }
  2268. vcpu->arch.update_pte.gfn = gfn;
  2269. vcpu->arch.update_pte.pfn = pfn;
  2270. }
  2271. static void kvm_mmu_access_page(struct kvm_vcpu *vcpu, gfn_t gfn)
  2272. {
  2273. u64 *spte = vcpu->arch.last_pte_updated;
  2274. if (spte
  2275. && vcpu->arch.last_pte_gfn == gfn
  2276. && shadow_accessed_mask
  2277. && !(*spte & shadow_accessed_mask)
  2278. && is_shadow_present_pte(*spte))
  2279. set_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
  2280. }
  2281. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  2282. const u8 *new, int bytes,
  2283. bool guest_initiated)
  2284. {
  2285. gfn_t gfn = gpa >> PAGE_SHIFT;
  2286. struct kvm_mmu_page *sp;
  2287. struct hlist_node *node;
  2288. LIST_HEAD(invalid_list);
  2289. u64 entry, gentry;
  2290. u64 *spte;
  2291. unsigned offset = offset_in_page(gpa);
  2292. unsigned pte_size;
  2293. unsigned page_offset;
  2294. unsigned misaligned;
  2295. unsigned quadrant;
  2296. int level;
  2297. int flooded = 0;
  2298. int npte;
  2299. int r;
  2300. int invlpg_counter;
  2301. bool remote_flush, local_flush, zap_page;
  2302. zap_page = remote_flush = local_flush = false;
  2303. pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
  2304. invlpg_counter = atomic_read(&vcpu->kvm->arch.invlpg_counter);
  2305. /*
  2306. * Assume that the pte write on a page table of the same type
  2307. * as the current vcpu paging mode. This is nearly always true
  2308. * (might be false while changing modes). Note it is verified later
  2309. * by update_pte().
  2310. */
  2311. if ((is_pae(vcpu) && bytes == 4) || !new) {
  2312. /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
  2313. if (is_pae(vcpu)) {
  2314. gpa &= ~(gpa_t)7;
  2315. bytes = 8;
  2316. }
  2317. r = kvm_read_guest(vcpu->kvm, gpa, &gentry, min(bytes, 8));
  2318. if (r)
  2319. gentry = 0;
  2320. new = (const u8 *)&gentry;
  2321. }
  2322. switch (bytes) {
  2323. case 4:
  2324. gentry = *(const u32 *)new;
  2325. break;
  2326. case 8:
  2327. gentry = *(const u64 *)new;
  2328. break;
  2329. default:
  2330. gentry = 0;
  2331. break;
  2332. }
  2333. mmu_guess_page_from_pte_write(vcpu, gpa, gentry);
  2334. spin_lock(&vcpu->kvm->mmu_lock);
  2335. if (atomic_read(&vcpu->kvm->arch.invlpg_counter) != invlpg_counter)
  2336. gentry = 0;
  2337. kvm_mmu_access_page(vcpu, gfn);
  2338. kvm_mmu_free_some_pages(vcpu);
  2339. ++vcpu->kvm->stat.mmu_pte_write;
  2340. kvm_mmu_audit(vcpu, "pre pte write");
  2341. if (guest_initiated) {
  2342. if (gfn == vcpu->arch.last_pt_write_gfn
  2343. && !last_updated_pte_accessed(vcpu)) {
  2344. ++vcpu->arch.last_pt_write_count;
  2345. if (vcpu->arch.last_pt_write_count >= 3)
  2346. flooded = 1;
  2347. } else {
  2348. vcpu->arch.last_pt_write_gfn = gfn;
  2349. vcpu->arch.last_pt_write_count = 1;
  2350. vcpu->arch.last_pte_updated = NULL;
  2351. }
  2352. }
  2353. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
  2354. pte_size = sp->role.cr4_pae ? 8 : 4;
  2355. misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
  2356. misaligned |= bytes < 4;
  2357. if (misaligned || flooded) {
  2358. /*
  2359. * Misaligned accesses are too much trouble to fix
  2360. * up; also, they usually indicate a page is not used
  2361. * as a page table.
  2362. *
  2363. * If we're seeing too many writes to a page,
  2364. * it may no longer be a page table, or we may be
  2365. * forking, in which case it is better to unmap the
  2366. * page.
  2367. */
  2368. pgprintk("misaligned: gpa %llx bytes %d role %x\n",
  2369. gpa, bytes, sp->role.word);
  2370. zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
  2371. &invalid_list);
  2372. ++vcpu->kvm->stat.mmu_flooded;
  2373. continue;
  2374. }
  2375. page_offset = offset;
  2376. level = sp->role.level;
  2377. npte = 1;
  2378. if (!sp->role.cr4_pae) {
  2379. page_offset <<= 1; /* 32->64 */
  2380. /*
  2381. * A 32-bit pde maps 4MB while the shadow pdes map
  2382. * only 2MB. So we need to double the offset again
  2383. * and zap two pdes instead of one.
  2384. */
  2385. if (level == PT32_ROOT_LEVEL) {
  2386. page_offset &= ~7; /* kill rounding error */
  2387. page_offset <<= 1;
  2388. npte = 2;
  2389. }
  2390. quadrant = page_offset >> PAGE_SHIFT;
  2391. page_offset &= ~PAGE_MASK;
  2392. if (quadrant != sp->role.quadrant)
  2393. continue;
  2394. }
  2395. local_flush = true;
  2396. spte = &sp->spt[page_offset / sizeof(*spte)];
  2397. while (npte--) {
  2398. entry = *spte;
  2399. mmu_pte_write_zap_pte(vcpu, sp, spte);
  2400. if (gentry)
  2401. mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
  2402. if (!remote_flush && need_remote_flush(entry, *spte))
  2403. remote_flush = true;
  2404. ++spte;
  2405. }
  2406. }
  2407. mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
  2408. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  2409. kvm_mmu_audit(vcpu, "post pte write");
  2410. spin_unlock(&vcpu->kvm->mmu_lock);
  2411. if (!is_error_pfn(vcpu->arch.update_pte.pfn)) {
  2412. kvm_release_pfn_clean(vcpu->arch.update_pte.pfn);
  2413. vcpu->arch.update_pte.pfn = bad_pfn;
  2414. }
  2415. }
  2416. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
  2417. {
  2418. gpa_t gpa;
  2419. int r;
  2420. if (tdp_enabled)
  2421. return 0;
  2422. gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
  2423. spin_lock(&vcpu->kvm->mmu_lock);
  2424. r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  2425. spin_unlock(&vcpu->kvm->mmu_lock);
  2426. return r;
  2427. }
  2428. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
  2429. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
  2430. {
  2431. int free_pages;
  2432. LIST_HEAD(invalid_list);
  2433. free_pages = vcpu->kvm->arch.n_free_mmu_pages;
  2434. while (free_pages < KVM_REFILL_PAGES &&
  2435. !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
  2436. struct kvm_mmu_page *sp;
  2437. sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
  2438. struct kvm_mmu_page, link);
  2439. free_pages += kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
  2440. &invalid_list);
  2441. ++vcpu->kvm->stat.mmu_recycled;
  2442. }
  2443. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  2444. }
  2445. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)
  2446. {
  2447. int r;
  2448. enum emulation_result er;
  2449. r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code);
  2450. if (r < 0)
  2451. goto out;
  2452. if (!r) {
  2453. r = 1;
  2454. goto out;
  2455. }
  2456. r = mmu_topup_memory_caches(vcpu);
  2457. if (r)
  2458. goto out;
  2459. er = emulate_instruction(vcpu, cr2, error_code, 0);
  2460. switch (er) {
  2461. case EMULATE_DONE:
  2462. return 1;
  2463. case EMULATE_DO_MMIO:
  2464. ++vcpu->stat.mmio_exits;
  2465. /* fall through */
  2466. case EMULATE_FAIL:
  2467. return 0;
  2468. default:
  2469. BUG();
  2470. }
  2471. out:
  2472. return r;
  2473. }
  2474. EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
  2475. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  2476. {
  2477. vcpu->arch.mmu.invlpg(vcpu, gva);
  2478. kvm_mmu_flush_tlb(vcpu);
  2479. ++vcpu->stat.invlpg;
  2480. }
  2481. EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
  2482. void kvm_enable_tdp(void)
  2483. {
  2484. tdp_enabled = true;
  2485. }
  2486. EXPORT_SYMBOL_GPL(kvm_enable_tdp);
  2487. void kvm_disable_tdp(void)
  2488. {
  2489. tdp_enabled = false;
  2490. }
  2491. EXPORT_SYMBOL_GPL(kvm_disable_tdp);
  2492. static void free_mmu_pages(struct kvm_vcpu *vcpu)
  2493. {
  2494. free_page((unsigned long)vcpu->arch.mmu.pae_root);
  2495. }
  2496. static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
  2497. {
  2498. struct page *page;
  2499. int i;
  2500. ASSERT(vcpu);
  2501. /*
  2502. * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
  2503. * Therefore we need to allocate shadow page tables in the first
  2504. * 4GB of memory, which happens to fit the DMA32 zone.
  2505. */
  2506. page = alloc_page(GFP_KERNEL | __GFP_DMA32);
  2507. if (!page)
  2508. return -ENOMEM;
  2509. vcpu->arch.mmu.pae_root = page_address(page);
  2510. for (i = 0; i < 4; ++i)
  2511. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  2512. return 0;
  2513. }
  2514. int kvm_mmu_create(struct kvm_vcpu *vcpu)
  2515. {
  2516. ASSERT(vcpu);
  2517. ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2518. return alloc_mmu_pages(vcpu);
  2519. }
  2520. int kvm_mmu_setup(struct kvm_vcpu *vcpu)
  2521. {
  2522. ASSERT(vcpu);
  2523. ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2524. return init_kvm_mmu(vcpu);
  2525. }
  2526. void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
  2527. {
  2528. ASSERT(vcpu);
  2529. destroy_kvm_mmu(vcpu);
  2530. free_mmu_pages(vcpu);
  2531. mmu_free_memory_caches(vcpu);
  2532. }
  2533. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
  2534. {
  2535. struct kvm_mmu_page *sp;
  2536. list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
  2537. int i;
  2538. u64 *pt;
  2539. if (!test_bit(slot, sp->slot_bitmap))
  2540. continue;
  2541. pt = sp->spt;
  2542. for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
  2543. /* avoid RMW */
  2544. if (is_writable_pte(pt[i]))
  2545. pt[i] &= ~PT_WRITABLE_MASK;
  2546. }
  2547. kvm_flush_remote_tlbs(kvm);
  2548. }
  2549. void kvm_mmu_zap_all(struct kvm *kvm)
  2550. {
  2551. struct kvm_mmu_page *sp, *node;
  2552. LIST_HEAD(invalid_list);
  2553. spin_lock(&kvm->mmu_lock);
  2554. restart:
  2555. list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
  2556. if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
  2557. goto restart;
  2558. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2559. spin_unlock(&kvm->mmu_lock);
  2560. }
  2561. static int kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
  2562. struct list_head *invalid_list)
  2563. {
  2564. struct kvm_mmu_page *page;
  2565. page = container_of(kvm->arch.active_mmu_pages.prev,
  2566. struct kvm_mmu_page, link);
  2567. return kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
  2568. }
  2569. static int mmu_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  2570. {
  2571. struct kvm *kvm;
  2572. struct kvm *kvm_freed = NULL;
  2573. int cache_count = 0;
  2574. spin_lock(&kvm_lock);
  2575. list_for_each_entry(kvm, &vm_list, vm_list) {
  2576. int npages, idx, freed_pages;
  2577. LIST_HEAD(invalid_list);
  2578. idx = srcu_read_lock(&kvm->srcu);
  2579. spin_lock(&kvm->mmu_lock);
  2580. npages = kvm->arch.n_alloc_mmu_pages -
  2581. kvm->arch.n_free_mmu_pages;
  2582. cache_count += npages;
  2583. if (!kvm_freed && nr_to_scan > 0 && npages > 0) {
  2584. freed_pages = kvm_mmu_remove_some_alloc_mmu_pages(kvm,
  2585. &invalid_list);
  2586. cache_count -= freed_pages;
  2587. kvm_freed = kvm;
  2588. }
  2589. nr_to_scan--;
  2590. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2591. spin_unlock(&kvm->mmu_lock);
  2592. srcu_read_unlock(&kvm->srcu, idx);
  2593. }
  2594. if (kvm_freed)
  2595. list_move_tail(&kvm_freed->vm_list, &vm_list);
  2596. spin_unlock(&kvm_lock);
  2597. return cache_count;
  2598. }
  2599. static struct shrinker mmu_shrinker = {
  2600. .shrink = mmu_shrink,
  2601. .seeks = DEFAULT_SEEKS * 10,
  2602. };
  2603. static void mmu_destroy_caches(void)
  2604. {
  2605. if (pte_chain_cache)
  2606. kmem_cache_destroy(pte_chain_cache);
  2607. if (rmap_desc_cache)
  2608. kmem_cache_destroy(rmap_desc_cache);
  2609. if (mmu_page_header_cache)
  2610. kmem_cache_destroy(mmu_page_header_cache);
  2611. }
  2612. void kvm_mmu_module_exit(void)
  2613. {
  2614. mmu_destroy_caches();
  2615. unregister_shrinker(&mmu_shrinker);
  2616. }
  2617. int kvm_mmu_module_init(void)
  2618. {
  2619. pte_chain_cache = kmem_cache_create("kvm_pte_chain",
  2620. sizeof(struct kvm_pte_chain),
  2621. 0, 0, NULL);
  2622. if (!pte_chain_cache)
  2623. goto nomem;
  2624. rmap_desc_cache = kmem_cache_create("kvm_rmap_desc",
  2625. sizeof(struct kvm_rmap_desc),
  2626. 0, 0, NULL);
  2627. if (!rmap_desc_cache)
  2628. goto nomem;
  2629. mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
  2630. sizeof(struct kvm_mmu_page),
  2631. 0, 0, NULL);
  2632. if (!mmu_page_header_cache)
  2633. goto nomem;
  2634. register_shrinker(&mmu_shrinker);
  2635. return 0;
  2636. nomem:
  2637. mmu_destroy_caches();
  2638. return -ENOMEM;
  2639. }
  2640. /*
  2641. * Caculate mmu pages needed for kvm.
  2642. */
  2643. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
  2644. {
  2645. int i;
  2646. unsigned int nr_mmu_pages;
  2647. unsigned int nr_pages = 0;
  2648. struct kvm_memslots *slots;
  2649. slots = kvm_memslots(kvm);
  2650. for (i = 0; i < slots->nmemslots; i++)
  2651. nr_pages += slots->memslots[i].npages;
  2652. nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
  2653. nr_mmu_pages = max(nr_mmu_pages,
  2654. (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
  2655. return nr_mmu_pages;
  2656. }
  2657. static void *pv_mmu_peek_buffer(struct kvm_pv_mmu_op_buffer *buffer,
  2658. unsigned len)
  2659. {
  2660. if (len > buffer->len)
  2661. return NULL;
  2662. return buffer->ptr;
  2663. }
  2664. static void *pv_mmu_read_buffer(struct kvm_pv_mmu_op_buffer *buffer,
  2665. unsigned len)
  2666. {
  2667. void *ret;
  2668. ret = pv_mmu_peek_buffer(buffer, len);
  2669. if (!ret)
  2670. return ret;
  2671. buffer->ptr += len;
  2672. buffer->len -= len;
  2673. buffer->processed += len;
  2674. return ret;
  2675. }
  2676. static int kvm_pv_mmu_write(struct kvm_vcpu *vcpu,
  2677. gpa_t addr, gpa_t value)
  2678. {
  2679. int bytes = 8;
  2680. int r;
  2681. if (!is_long_mode(vcpu) && !is_pae(vcpu))
  2682. bytes = 4;
  2683. r = mmu_topup_memory_caches(vcpu);
  2684. if (r)
  2685. return r;
  2686. if (!emulator_write_phys(vcpu, addr, &value, bytes))
  2687. return -EFAULT;
  2688. return 1;
  2689. }
  2690. static int kvm_pv_mmu_flush_tlb(struct kvm_vcpu *vcpu)
  2691. {
  2692. (void)kvm_set_cr3(vcpu, vcpu->arch.cr3);
  2693. return 1;
  2694. }
  2695. static int kvm_pv_mmu_release_pt(struct kvm_vcpu *vcpu, gpa_t addr)
  2696. {
  2697. spin_lock(&vcpu->kvm->mmu_lock);
  2698. mmu_unshadow(vcpu->kvm, addr >> PAGE_SHIFT);
  2699. spin_unlock(&vcpu->kvm->mmu_lock);
  2700. return 1;
  2701. }
  2702. static int kvm_pv_mmu_op_one(struct kvm_vcpu *vcpu,
  2703. struct kvm_pv_mmu_op_buffer *buffer)
  2704. {
  2705. struct kvm_mmu_op_header *header;
  2706. header = pv_mmu_peek_buffer(buffer, sizeof *header);
  2707. if (!header)
  2708. return 0;
  2709. switch (header->op) {
  2710. case KVM_MMU_OP_WRITE_PTE: {
  2711. struct kvm_mmu_op_write_pte *wpte;
  2712. wpte = pv_mmu_read_buffer(buffer, sizeof *wpte);
  2713. if (!wpte)
  2714. return 0;
  2715. return kvm_pv_mmu_write(vcpu, wpte->pte_phys,
  2716. wpte->pte_val);
  2717. }
  2718. case KVM_MMU_OP_FLUSH_TLB: {
  2719. struct kvm_mmu_op_flush_tlb *ftlb;
  2720. ftlb = pv_mmu_read_buffer(buffer, sizeof *ftlb);
  2721. if (!ftlb)
  2722. return 0;
  2723. return kvm_pv_mmu_flush_tlb(vcpu);
  2724. }
  2725. case KVM_MMU_OP_RELEASE_PT: {
  2726. struct kvm_mmu_op_release_pt *rpt;
  2727. rpt = pv_mmu_read_buffer(buffer, sizeof *rpt);
  2728. if (!rpt)
  2729. return 0;
  2730. return kvm_pv_mmu_release_pt(vcpu, rpt->pt_phys);
  2731. }
  2732. default: return 0;
  2733. }
  2734. }
  2735. int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
  2736. gpa_t addr, unsigned long *ret)
  2737. {
  2738. int r;
  2739. struct kvm_pv_mmu_op_buffer *buffer = &vcpu->arch.mmu_op_buffer;
  2740. buffer->ptr = buffer->buf;
  2741. buffer->len = min_t(unsigned long, bytes, sizeof buffer->buf);
  2742. buffer->processed = 0;
  2743. r = kvm_read_guest(vcpu->kvm, addr, buffer->buf, buffer->len);
  2744. if (r)
  2745. goto out;
  2746. while (buffer->len) {
  2747. r = kvm_pv_mmu_op_one(vcpu, buffer);
  2748. if (r < 0)
  2749. goto out;
  2750. if (r == 0)
  2751. break;
  2752. }
  2753. r = 1;
  2754. out:
  2755. *ret = buffer->processed;
  2756. return r;
  2757. }
  2758. int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
  2759. {
  2760. struct kvm_shadow_walk_iterator iterator;
  2761. int nr_sptes = 0;
  2762. spin_lock(&vcpu->kvm->mmu_lock);
  2763. for_each_shadow_entry(vcpu, addr, iterator) {
  2764. sptes[iterator.level-1] = *iterator.sptep;
  2765. nr_sptes++;
  2766. if (!is_shadow_present_pte(*iterator.sptep))
  2767. break;
  2768. }
  2769. spin_unlock(&vcpu->kvm->mmu_lock);
  2770. return nr_sptes;
  2771. }
  2772. EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
  2773. #ifdef AUDIT
  2774. static const char *audit_msg;
  2775. static gva_t canonicalize(gva_t gva)
  2776. {
  2777. #ifdef CONFIG_X86_64
  2778. gva = (long long)(gva << 16) >> 16;
  2779. #endif
  2780. return gva;
  2781. }
  2782. typedef void (*inspect_spte_fn) (struct kvm *kvm, u64 *sptep);
  2783. static void __mmu_spte_walk(struct kvm *kvm, struct kvm_mmu_page *sp,
  2784. inspect_spte_fn fn)
  2785. {
  2786. int i;
  2787. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  2788. u64 ent = sp->spt[i];
  2789. if (is_shadow_present_pte(ent)) {
  2790. if (!is_last_spte(ent, sp->role.level)) {
  2791. struct kvm_mmu_page *child;
  2792. child = page_header(ent & PT64_BASE_ADDR_MASK);
  2793. __mmu_spte_walk(kvm, child, fn);
  2794. } else
  2795. fn(kvm, &sp->spt[i]);
  2796. }
  2797. }
  2798. }
  2799. static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)
  2800. {
  2801. int i;
  2802. struct kvm_mmu_page *sp;
  2803. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2804. return;
  2805. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  2806. hpa_t root = vcpu->arch.mmu.root_hpa;
  2807. sp = page_header(root);
  2808. __mmu_spte_walk(vcpu->kvm, sp, fn);
  2809. return;
  2810. }
  2811. for (i = 0; i < 4; ++i) {
  2812. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2813. if (root && VALID_PAGE(root)) {
  2814. root &= PT64_BASE_ADDR_MASK;
  2815. sp = page_header(root);
  2816. __mmu_spte_walk(vcpu->kvm, sp, fn);
  2817. }
  2818. }
  2819. return;
  2820. }
  2821. static void audit_mappings_page(struct kvm_vcpu *vcpu, u64 page_pte,
  2822. gva_t va, int level)
  2823. {
  2824. u64 *pt = __va(page_pte & PT64_BASE_ADDR_MASK);
  2825. int i;
  2826. gva_t va_delta = 1ul << (PAGE_SHIFT + 9 * (level - 1));
  2827. for (i = 0; i < PT64_ENT_PER_PAGE; ++i, va += va_delta) {
  2828. u64 ent = pt[i];
  2829. if (ent == shadow_trap_nonpresent_pte)
  2830. continue;
  2831. va = canonicalize(va);
  2832. if (is_shadow_present_pte(ent) && !is_last_spte(ent, level))
  2833. audit_mappings_page(vcpu, ent, va, level - 1);
  2834. else {
  2835. gpa_t gpa = kvm_mmu_gva_to_gpa_read(vcpu, va, NULL);
  2836. gfn_t gfn = gpa >> PAGE_SHIFT;
  2837. pfn_t pfn = gfn_to_pfn(vcpu->kvm, gfn);
  2838. hpa_t hpa = (hpa_t)pfn << PAGE_SHIFT;
  2839. if (is_error_pfn(pfn)) {
  2840. kvm_release_pfn_clean(pfn);
  2841. continue;
  2842. }
  2843. if (is_shadow_present_pte(ent)
  2844. && (ent & PT64_BASE_ADDR_MASK) != hpa)
  2845. printk(KERN_ERR "xx audit error: (%s) levels %d"
  2846. " gva %lx gpa %llx hpa %llx ent %llx %d\n",
  2847. audit_msg, vcpu->arch.mmu.root_level,
  2848. va, gpa, hpa, ent,
  2849. is_shadow_present_pte(ent));
  2850. else if (ent == shadow_notrap_nonpresent_pte
  2851. && !is_error_hpa(hpa))
  2852. printk(KERN_ERR "audit: (%s) notrap shadow,"
  2853. " valid guest gva %lx\n", audit_msg, va);
  2854. kvm_release_pfn_clean(pfn);
  2855. }
  2856. }
  2857. }
  2858. static void audit_mappings(struct kvm_vcpu *vcpu)
  2859. {
  2860. unsigned i;
  2861. if (vcpu->arch.mmu.root_level == 4)
  2862. audit_mappings_page(vcpu, vcpu->arch.mmu.root_hpa, 0, 4);
  2863. else
  2864. for (i = 0; i < 4; ++i)
  2865. if (vcpu->arch.mmu.pae_root[i] & PT_PRESENT_MASK)
  2866. audit_mappings_page(vcpu,
  2867. vcpu->arch.mmu.pae_root[i],
  2868. i << 30,
  2869. 2);
  2870. }
  2871. static int count_rmaps(struct kvm_vcpu *vcpu)
  2872. {
  2873. struct kvm *kvm = vcpu->kvm;
  2874. struct kvm_memslots *slots;
  2875. int nmaps = 0;
  2876. int i, j, k, idx;
  2877. idx = srcu_read_lock(&kvm->srcu);
  2878. slots = kvm_memslots(kvm);
  2879. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  2880. struct kvm_memory_slot *m = &slots->memslots[i];
  2881. struct kvm_rmap_desc *d;
  2882. for (j = 0; j < m->npages; ++j) {
  2883. unsigned long *rmapp = &m->rmap[j];
  2884. if (!*rmapp)
  2885. continue;
  2886. if (!(*rmapp & 1)) {
  2887. ++nmaps;
  2888. continue;
  2889. }
  2890. d = (struct kvm_rmap_desc *)(*rmapp & ~1ul);
  2891. while (d) {
  2892. for (k = 0; k < RMAP_EXT; ++k)
  2893. if (d->sptes[k])
  2894. ++nmaps;
  2895. else
  2896. break;
  2897. d = d->more;
  2898. }
  2899. }
  2900. }
  2901. srcu_read_unlock(&kvm->srcu, idx);
  2902. return nmaps;
  2903. }
  2904. void inspect_spte_has_rmap(struct kvm *kvm, u64 *sptep)
  2905. {
  2906. unsigned long *rmapp;
  2907. struct kvm_mmu_page *rev_sp;
  2908. gfn_t gfn;
  2909. if (is_writable_pte(*sptep)) {
  2910. rev_sp = page_header(__pa(sptep));
  2911. gfn = kvm_mmu_page_get_gfn(rev_sp, sptep - rev_sp->spt);
  2912. if (!gfn_to_memslot(kvm, gfn)) {
  2913. if (!printk_ratelimit())
  2914. return;
  2915. printk(KERN_ERR "%s: no memslot for gfn %ld\n",
  2916. audit_msg, gfn);
  2917. printk(KERN_ERR "%s: index %ld of sp (gfn=%lx)\n",
  2918. audit_msg, (long int)(sptep - rev_sp->spt),
  2919. rev_sp->gfn);
  2920. dump_stack();
  2921. return;
  2922. }
  2923. rmapp = gfn_to_rmap(kvm, gfn, rev_sp->role.level);
  2924. if (!*rmapp) {
  2925. if (!printk_ratelimit())
  2926. return;
  2927. printk(KERN_ERR "%s: no rmap for writable spte %llx\n",
  2928. audit_msg, *sptep);
  2929. dump_stack();
  2930. }
  2931. }
  2932. }
  2933. void audit_writable_sptes_have_rmaps(struct kvm_vcpu *vcpu)
  2934. {
  2935. mmu_spte_walk(vcpu, inspect_spte_has_rmap);
  2936. }
  2937. static void check_writable_mappings_rmap(struct kvm_vcpu *vcpu)
  2938. {
  2939. struct kvm_mmu_page *sp;
  2940. int i;
  2941. list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
  2942. u64 *pt = sp->spt;
  2943. if (sp->role.level != PT_PAGE_TABLE_LEVEL)
  2944. continue;
  2945. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  2946. u64 ent = pt[i];
  2947. if (!(ent & PT_PRESENT_MASK))
  2948. continue;
  2949. if (!is_writable_pte(ent))
  2950. continue;
  2951. inspect_spte_has_rmap(vcpu->kvm, &pt[i]);
  2952. }
  2953. }
  2954. return;
  2955. }
  2956. static void audit_rmap(struct kvm_vcpu *vcpu)
  2957. {
  2958. check_writable_mappings_rmap(vcpu);
  2959. count_rmaps(vcpu);
  2960. }
  2961. static void audit_write_protection(struct kvm_vcpu *vcpu)
  2962. {
  2963. struct kvm_mmu_page *sp;
  2964. struct kvm_memory_slot *slot;
  2965. unsigned long *rmapp;
  2966. u64 *spte;
  2967. gfn_t gfn;
  2968. list_for_each_entry(sp, &vcpu->kvm->arch.active_mmu_pages, link) {
  2969. if (sp->role.direct)
  2970. continue;
  2971. if (sp->unsync)
  2972. continue;
  2973. gfn = unalias_gfn(vcpu->kvm, sp->gfn);
  2974. slot = gfn_to_memslot_unaliased(vcpu->kvm, sp->gfn);
  2975. rmapp = &slot->rmap[gfn - slot->base_gfn];
  2976. spte = rmap_next(vcpu->kvm, rmapp, NULL);
  2977. while (spte) {
  2978. if (is_writable_pte(*spte))
  2979. printk(KERN_ERR "%s: (%s) shadow page has "
  2980. "writable mappings: gfn %lx role %x\n",
  2981. __func__, audit_msg, sp->gfn,
  2982. sp->role.word);
  2983. spte = rmap_next(vcpu->kvm, rmapp, spte);
  2984. }
  2985. }
  2986. }
  2987. static void kvm_mmu_audit(struct kvm_vcpu *vcpu, const char *msg)
  2988. {
  2989. int olddbg = dbg;
  2990. dbg = 0;
  2991. audit_msg = msg;
  2992. audit_rmap(vcpu);
  2993. audit_write_protection(vcpu);
  2994. if (strcmp("pre pte write", audit_msg) != 0)
  2995. audit_mappings(vcpu);
  2996. audit_writable_sptes_have_rmaps(vcpu);
  2997. dbg = olddbg;
  2998. }
  2999. #endif