mmu.c 88 KB

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