mmu.c 88 KB

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