mmu.c 90 KB

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