mmu.c 89 KB

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