mmu.c 98 KB

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