mmu.c 92 KB

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