mmu.c 98 KB

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