mmu.c 90 KB

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