mmu.c 88 KB

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