mmu.c 97 KB

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