mmu.c 92 KB

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