mmu.c 89 KB

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