mmu.c 90 KB

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