mmu.c 88 KB

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