mmu.c 90 KB

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