mmu.c 89 KB

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