mmu.c 89 KB

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