mmu.c 88 KB

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