mmu.c 97 KB

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