mmu.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031
  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. #undef MMU_DEBUG
  57. #ifdef MMU_DEBUG
  58. #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
  59. #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
  60. #else
  61. #define pgprintk(x...) do { } while (0)
  62. #define rmap_printk(x...) do { } while (0)
  63. #endif
  64. #ifdef MMU_DEBUG
  65. static bool dbg = 0;
  66. module_param(dbg, bool, 0644);
  67. #endif
  68. #ifndef MMU_DEBUG
  69. #define ASSERT(x) do { } while (0)
  70. #else
  71. #define ASSERT(x) \
  72. if (!(x)) { \
  73. printk(KERN_WARNING "assertion failed %s:%d: %s\n", \
  74. __FILE__, __LINE__, #x); \
  75. }
  76. #endif
  77. #define PTE_PREFETCH_NUM 8
  78. #define PT_FIRST_AVAIL_BITS_SHIFT 9
  79. #define PT64_SECOND_AVAIL_BITS_SHIFT 52
  80. #define PT64_LEVEL_BITS 9
  81. #define PT64_LEVEL_SHIFT(level) \
  82. (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
  83. #define PT64_INDEX(address, level)\
  84. (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
  85. #define PT32_LEVEL_BITS 10
  86. #define PT32_LEVEL_SHIFT(level) \
  87. (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
  88. #define PT32_LVL_OFFSET_MASK(level) \
  89. (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  90. * PT32_LEVEL_BITS))) - 1))
  91. #define PT32_INDEX(address, level)\
  92. (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
  93. #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
  94. #define PT64_DIR_BASE_ADDR_MASK \
  95. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
  96. #define PT64_LVL_ADDR_MASK(level) \
  97. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  98. * PT64_LEVEL_BITS))) - 1))
  99. #define PT64_LVL_OFFSET_MASK(level) \
  100. (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  101. * PT64_LEVEL_BITS))) - 1))
  102. #define PT32_BASE_ADDR_MASK PAGE_MASK
  103. #define PT32_DIR_BASE_ADDR_MASK \
  104. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
  105. #define PT32_LVL_ADDR_MASK(level) \
  106. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  107. * PT32_LEVEL_BITS))) - 1))
  108. #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK \
  109. | PT64_NX_MASK)
  110. #define PTE_LIST_EXT 4
  111. #define ACC_EXEC_MASK 1
  112. #define ACC_WRITE_MASK PT_WRITABLE_MASK
  113. #define ACC_USER_MASK PT_USER_MASK
  114. #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
  115. #include <trace/events/kvm.h>
  116. #define CREATE_TRACE_POINTS
  117. #include "mmutrace.h"
  118. #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
  119. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  120. struct pte_list_desc {
  121. u64 *sptes[PTE_LIST_EXT];
  122. struct pte_list_desc *more;
  123. };
  124. struct kvm_shadow_walk_iterator {
  125. u64 addr;
  126. hpa_t shadow_addr;
  127. u64 *sptep;
  128. int level;
  129. unsigned index;
  130. };
  131. #define for_each_shadow_entry(_vcpu, _addr, _walker) \
  132. for (shadow_walk_init(&(_walker), _vcpu, _addr); \
  133. shadow_walk_okay(&(_walker)); \
  134. shadow_walk_next(&(_walker)))
  135. #define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
  136. for (shadow_walk_init(&(_walker), _vcpu, _addr); \
  137. shadow_walk_okay(&(_walker)) && \
  138. ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
  139. __shadow_walk_next(&(_walker), spte))
  140. static struct kmem_cache *pte_list_desc_cache;
  141. static struct kmem_cache *mmu_page_header_cache;
  142. static struct percpu_counter kvm_total_used_mmu_pages;
  143. static u64 __read_mostly shadow_nx_mask;
  144. static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
  145. static u64 __read_mostly shadow_user_mask;
  146. static u64 __read_mostly shadow_accessed_mask;
  147. static u64 __read_mostly shadow_dirty_mask;
  148. static u64 __read_mostly shadow_mmio_mask;
  149. static void mmu_spte_set(u64 *sptep, u64 spte);
  150. void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask)
  151. {
  152. shadow_mmio_mask = mmio_mask;
  153. }
  154. EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
  155. static void mark_mmio_spte(u64 *sptep, u64 gfn, unsigned access)
  156. {
  157. access &= ACC_WRITE_MASK | ACC_USER_MASK;
  158. trace_mark_mmio_spte(sptep, gfn, access);
  159. mmu_spte_set(sptep, shadow_mmio_mask | access | gfn << PAGE_SHIFT);
  160. }
  161. static bool is_mmio_spte(u64 spte)
  162. {
  163. return (spte & shadow_mmio_mask) == shadow_mmio_mask;
  164. }
  165. static gfn_t get_mmio_spte_gfn(u64 spte)
  166. {
  167. return (spte & ~shadow_mmio_mask) >> PAGE_SHIFT;
  168. }
  169. static unsigned get_mmio_spte_access(u64 spte)
  170. {
  171. return (spte & ~shadow_mmio_mask) & ~PAGE_MASK;
  172. }
  173. static bool set_mmio_spte(u64 *sptep, gfn_t gfn, pfn_t pfn, unsigned access)
  174. {
  175. if (unlikely(is_noslot_pfn(pfn))) {
  176. mark_mmio_spte(sptep, gfn, access);
  177. return true;
  178. }
  179. return false;
  180. }
  181. static inline u64 rsvd_bits(int s, int e)
  182. {
  183. return ((1ULL << (e - s + 1)) - 1) << s;
  184. }
  185. void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
  186. u64 dirty_mask, u64 nx_mask, u64 x_mask)
  187. {
  188. shadow_user_mask = user_mask;
  189. shadow_accessed_mask = accessed_mask;
  190. shadow_dirty_mask = dirty_mask;
  191. shadow_nx_mask = nx_mask;
  192. shadow_x_mask = x_mask;
  193. }
  194. EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
  195. static int is_cpuid_PSE36(void)
  196. {
  197. return 1;
  198. }
  199. static int is_nx(struct kvm_vcpu *vcpu)
  200. {
  201. return vcpu->arch.efer & EFER_NX;
  202. }
  203. static int is_shadow_present_pte(u64 pte)
  204. {
  205. return pte & PT_PRESENT_MASK && !is_mmio_spte(pte);
  206. }
  207. static int is_large_pte(u64 pte)
  208. {
  209. return pte & PT_PAGE_SIZE_MASK;
  210. }
  211. static int is_dirty_gpte(unsigned long pte)
  212. {
  213. return pte & PT_DIRTY_MASK;
  214. }
  215. static int is_rmap_spte(u64 pte)
  216. {
  217. return is_shadow_present_pte(pte);
  218. }
  219. static int is_last_spte(u64 pte, int level)
  220. {
  221. if (level == PT_PAGE_TABLE_LEVEL)
  222. return 1;
  223. if (is_large_pte(pte))
  224. return 1;
  225. return 0;
  226. }
  227. static pfn_t spte_to_pfn(u64 pte)
  228. {
  229. return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
  230. }
  231. static gfn_t pse36_gfn_delta(u32 gpte)
  232. {
  233. int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
  234. return (gpte & PT32_DIR_PSE36_MASK) << shift;
  235. }
  236. #ifdef CONFIG_X86_64
  237. static void __set_spte(u64 *sptep, u64 spte)
  238. {
  239. *sptep = spte;
  240. }
  241. static void __update_clear_spte_fast(u64 *sptep, u64 spte)
  242. {
  243. *sptep = spte;
  244. }
  245. static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
  246. {
  247. return xchg(sptep, spte);
  248. }
  249. static u64 __get_spte_lockless(u64 *sptep)
  250. {
  251. return ACCESS_ONCE(*sptep);
  252. }
  253. static bool __check_direct_spte_mmio_pf(u64 spte)
  254. {
  255. /* It is valid if the spte is zapped. */
  256. return spte == 0ull;
  257. }
  258. #else
  259. union split_spte {
  260. struct {
  261. u32 spte_low;
  262. u32 spte_high;
  263. };
  264. u64 spte;
  265. };
  266. static void count_spte_clear(u64 *sptep, u64 spte)
  267. {
  268. struct kvm_mmu_page *sp = page_header(__pa(sptep));
  269. if (is_shadow_present_pte(spte))
  270. return;
  271. /* Ensure the spte is completely set before we increase the count */
  272. smp_wmb();
  273. sp->clear_spte_count++;
  274. }
  275. static void __set_spte(u64 *sptep, u64 spte)
  276. {
  277. union split_spte *ssptep, sspte;
  278. ssptep = (union split_spte *)sptep;
  279. sspte = (union split_spte)spte;
  280. ssptep->spte_high = sspte.spte_high;
  281. /*
  282. * If we map the spte from nonpresent to present, We should store
  283. * the high bits firstly, then set present bit, so cpu can not
  284. * fetch this spte while we are setting the spte.
  285. */
  286. smp_wmb();
  287. ssptep->spte_low = sspte.spte_low;
  288. }
  289. static void __update_clear_spte_fast(u64 *sptep, u64 spte)
  290. {
  291. union split_spte *ssptep, sspte;
  292. ssptep = (union split_spte *)sptep;
  293. sspte = (union split_spte)spte;
  294. ssptep->spte_low = sspte.spte_low;
  295. /*
  296. * If we map the spte from present to nonpresent, we should clear
  297. * present bit firstly to avoid vcpu fetch the old high bits.
  298. */
  299. smp_wmb();
  300. ssptep->spte_high = sspte.spte_high;
  301. count_spte_clear(sptep, spte);
  302. }
  303. static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
  304. {
  305. union split_spte *ssptep, sspte, orig;
  306. ssptep = (union split_spte *)sptep;
  307. sspte = (union split_spte)spte;
  308. /* xchg acts as a barrier before the setting of the high bits */
  309. orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
  310. orig.spte_high = ssptep->spte_high;
  311. ssptep->spte_high = sspte.spte_high;
  312. count_spte_clear(sptep, spte);
  313. return orig.spte;
  314. }
  315. /*
  316. * The idea using the light way get the spte on x86_32 guest is from
  317. * gup_get_pte(arch/x86/mm/gup.c).
  318. * The difference is we can not catch the spte tlb flush if we leave
  319. * guest mode, so we emulate it by increase clear_spte_count when spte
  320. * is cleared.
  321. */
  322. static u64 __get_spte_lockless(u64 *sptep)
  323. {
  324. struct kvm_mmu_page *sp = page_header(__pa(sptep));
  325. union split_spte spte, *orig = (union split_spte *)sptep;
  326. int count;
  327. retry:
  328. count = sp->clear_spte_count;
  329. smp_rmb();
  330. spte.spte_low = orig->spte_low;
  331. smp_rmb();
  332. spte.spte_high = orig->spte_high;
  333. smp_rmb();
  334. if (unlikely(spte.spte_low != orig->spte_low ||
  335. count != sp->clear_spte_count))
  336. goto retry;
  337. return spte.spte;
  338. }
  339. static bool __check_direct_spte_mmio_pf(u64 spte)
  340. {
  341. union split_spte sspte = (union split_spte)spte;
  342. u32 high_mmio_mask = shadow_mmio_mask >> 32;
  343. /* It is valid if the spte is zapped. */
  344. if (spte == 0ull)
  345. return true;
  346. /* It is valid if the spte is being zapped. */
  347. if (sspte.spte_low == 0ull &&
  348. (sspte.spte_high & high_mmio_mask) == high_mmio_mask)
  349. return true;
  350. return false;
  351. }
  352. #endif
  353. static bool spte_has_volatile_bits(u64 spte)
  354. {
  355. if (!shadow_accessed_mask)
  356. return false;
  357. if (!is_shadow_present_pte(spte))
  358. return false;
  359. if ((spte & shadow_accessed_mask) &&
  360. (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
  361. return false;
  362. return true;
  363. }
  364. static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
  365. {
  366. return (old_spte & bit_mask) && !(new_spte & bit_mask);
  367. }
  368. /* Rules for using mmu_spte_set:
  369. * Set the sptep from nonpresent to present.
  370. * Note: the sptep being assigned *must* be either not present
  371. * or in a state where the hardware will not attempt to update
  372. * the spte.
  373. */
  374. static void mmu_spte_set(u64 *sptep, u64 new_spte)
  375. {
  376. WARN_ON(is_shadow_present_pte(*sptep));
  377. __set_spte(sptep, new_spte);
  378. }
  379. /* Rules for using mmu_spte_update:
  380. * Update the state bits, it means the mapped pfn is not changged.
  381. */
  382. static void mmu_spte_update(u64 *sptep, u64 new_spte)
  383. {
  384. u64 mask, old_spte = *sptep;
  385. WARN_ON(!is_rmap_spte(new_spte));
  386. if (!is_shadow_present_pte(old_spte))
  387. return mmu_spte_set(sptep, new_spte);
  388. new_spte |= old_spte & shadow_dirty_mask;
  389. mask = shadow_accessed_mask;
  390. if (is_writable_pte(old_spte))
  391. mask |= shadow_dirty_mask;
  392. if (!spte_has_volatile_bits(old_spte) || (new_spte & mask) == mask)
  393. __update_clear_spte_fast(sptep, new_spte);
  394. else
  395. old_spte = __update_clear_spte_slow(sptep, new_spte);
  396. if (!shadow_accessed_mask)
  397. return;
  398. if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
  399. kvm_set_pfn_accessed(spte_to_pfn(old_spte));
  400. if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
  401. kvm_set_pfn_dirty(spte_to_pfn(old_spte));
  402. }
  403. /*
  404. * Rules for using mmu_spte_clear_track_bits:
  405. * It sets the sptep from present to nonpresent, and track the
  406. * state bits, it is used to clear the last level sptep.
  407. */
  408. static int mmu_spte_clear_track_bits(u64 *sptep)
  409. {
  410. pfn_t pfn;
  411. u64 old_spte = *sptep;
  412. if (!spte_has_volatile_bits(old_spte))
  413. __update_clear_spte_fast(sptep, 0ull);
  414. else
  415. old_spte = __update_clear_spte_slow(sptep, 0ull);
  416. if (!is_rmap_spte(old_spte))
  417. return 0;
  418. pfn = spte_to_pfn(old_spte);
  419. if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
  420. kvm_set_pfn_accessed(pfn);
  421. if (!shadow_dirty_mask || (old_spte & shadow_dirty_mask))
  422. kvm_set_pfn_dirty(pfn);
  423. return 1;
  424. }
  425. /*
  426. * Rules for using mmu_spte_clear_no_track:
  427. * Directly clear spte without caring the state bits of sptep,
  428. * it is used to set the upper level spte.
  429. */
  430. static void mmu_spte_clear_no_track(u64 *sptep)
  431. {
  432. __update_clear_spte_fast(sptep, 0ull);
  433. }
  434. static u64 mmu_spte_get_lockless(u64 *sptep)
  435. {
  436. return __get_spte_lockless(sptep);
  437. }
  438. static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
  439. {
  440. rcu_read_lock();
  441. atomic_inc(&vcpu->kvm->arch.reader_counter);
  442. /* Increase the counter before walking shadow page table */
  443. smp_mb__after_atomic_inc();
  444. }
  445. static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
  446. {
  447. /* Decrease the counter after walking shadow page table finished */
  448. smp_mb__before_atomic_dec();
  449. atomic_dec(&vcpu->kvm->arch.reader_counter);
  450. rcu_read_unlock();
  451. }
  452. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  453. struct kmem_cache *base_cache, int min)
  454. {
  455. void *obj;
  456. if (cache->nobjs >= min)
  457. return 0;
  458. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  459. obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
  460. if (!obj)
  461. return -ENOMEM;
  462. cache->objects[cache->nobjs++] = obj;
  463. }
  464. return 0;
  465. }
  466. static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache)
  467. {
  468. return cache->nobjs;
  469. }
  470. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
  471. struct kmem_cache *cache)
  472. {
  473. while (mc->nobjs)
  474. kmem_cache_free(cache, mc->objects[--mc->nobjs]);
  475. }
  476. static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
  477. int min)
  478. {
  479. void *page;
  480. if (cache->nobjs >= min)
  481. return 0;
  482. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  483. page = (void *)__get_free_page(GFP_KERNEL);
  484. if (!page)
  485. return -ENOMEM;
  486. cache->objects[cache->nobjs++] = page;
  487. }
  488. return 0;
  489. }
  490. static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
  491. {
  492. while (mc->nobjs)
  493. free_page((unsigned long)mc->objects[--mc->nobjs]);
  494. }
  495. static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
  496. {
  497. int r;
  498. r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
  499. pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
  500. if (r)
  501. goto out;
  502. r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
  503. if (r)
  504. goto out;
  505. r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
  506. mmu_page_header_cache, 4);
  507. out:
  508. return r;
  509. }
  510. static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  511. {
  512. mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
  513. pte_list_desc_cache);
  514. mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
  515. mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
  516. mmu_page_header_cache);
  517. }
  518. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc,
  519. size_t size)
  520. {
  521. void *p;
  522. BUG_ON(!mc->nobjs);
  523. p = mc->objects[--mc->nobjs];
  524. return p;
  525. }
  526. static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
  527. {
  528. return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache,
  529. sizeof(struct pte_list_desc));
  530. }
  531. static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
  532. {
  533. kmem_cache_free(pte_list_desc_cache, pte_list_desc);
  534. }
  535. static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
  536. {
  537. if (!sp->role.direct)
  538. return sp->gfns[index];
  539. return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
  540. }
  541. static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
  542. {
  543. if (sp->role.direct)
  544. BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
  545. else
  546. sp->gfns[index] = gfn;
  547. }
  548. /*
  549. * Return the pointer to the large page information for a given gfn,
  550. * handling slots that are not large page aligned.
  551. */
  552. static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
  553. struct kvm_memory_slot *slot,
  554. int level)
  555. {
  556. unsigned long idx;
  557. idx = gfn_to_index(gfn, slot->base_gfn, level);
  558. return &slot->arch.lpage_info[level - 2][idx];
  559. }
  560. static void account_shadowed(struct kvm *kvm, gfn_t gfn)
  561. {
  562. struct kvm_memory_slot *slot;
  563. struct kvm_lpage_info *linfo;
  564. int i;
  565. slot = gfn_to_memslot(kvm, gfn);
  566. for (i = PT_DIRECTORY_LEVEL;
  567. i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
  568. linfo = lpage_info_slot(gfn, slot, i);
  569. linfo->write_count += 1;
  570. }
  571. kvm->arch.indirect_shadow_pages++;
  572. }
  573. static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
  574. {
  575. struct kvm_memory_slot *slot;
  576. struct kvm_lpage_info *linfo;
  577. int i;
  578. slot = gfn_to_memslot(kvm, gfn);
  579. for (i = PT_DIRECTORY_LEVEL;
  580. i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
  581. linfo = lpage_info_slot(gfn, slot, i);
  582. linfo->write_count -= 1;
  583. WARN_ON(linfo->write_count < 0);
  584. }
  585. kvm->arch.indirect_shadow_pages--;
  586. }
  587. static int has_wrprotected_page(struct kvm *kvm,
  588. gfn_t gfn,
  589. int level)
  590. {
  591. struct kvm_memory_slot *slot;
  592. struct kvm_lpage_info *linfo;
  593. slot = gfn_to_memslot(kvm, gfn);
  594. if (slot) {
  595. linfo = lpage_info_slot(gfn, slot, level);
  596. return linfo->write_count;
  597. }
  598. return 1;
  599. }
  600. static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
  601. {
  602. unsigned long page_size;
  603. int i, ret = 0;
  604. page_size = kvm_host_page_size(kvm, gfn);
  605. for (i = PT_PAGE_TABLE_LEVEL;
  606. i < (PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES); ++i) {
  607. if (page_size >= KVM_HPAGE_SIZE(i))
  608. ret = i;
  609. else
  610. break;
  611. }
  612. return ret;
  613. }
  614. static struct kvm_memory_slot *
  615. gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
  616. bool no_dirty_log)
  617. {
  618. struct kvm_memory_slot *slot;
  619. slot = gfn_to_memslot(vcpu->kvm, gfn);
  620. if (!slot || slot->flags & KVM_MEMSLOT_INVALID ||
  621. (no_dirty_log && slot->dirty_bitmap))
  622. slot = NULL;
  623. return slot;
  624. }
  625. static bool mapping_level_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t large_gfn)
  626. {
  627. return !gfn_to_memslot_dirty_bitmap(vcpu, large_gfn, true);
  628. }
  629. static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn)
  630. {
  631. int host_level, level, max_level;
  632. host_level = host_mapping_level(vcpu->kvm, large_gfn);
  633. if (host_level == PT_PAGE_TABLE_LEVEL)
  634. return host_level;
  635. max_level = kvm_x86_ops->get_lpage_level() < host_level ?
  636. kvm_x86_ops->get_lpage_level() : host_level;
  637. for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
  638. if (has_wrprotected_page(vcpu->kvm, large_gfn, level))
  639. break;
  640. return level - 1;
  641. }
  642. /*
  643. * Pte mapping structures:
  644. *
  645. * If pte_list bit zero is zero, then pte_list point to the spte.
  646. *
  647. * If pte_list bit zero is one, (then pte_list & ~1) points to a struct
  648. * pte_list_desc containing more mappings.
  649. *
  650. * Returns the number of pte entries before the spte was added or zero if
  651. * the spte was not added.
  652. *
  653. */
  654. static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
  655. unsigned long *pte_list)
  656. {
  657. struct pte_list_desc *desc;
  658. int i, count = 0;
  659. if (!*pte_list) {
  660. rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
  661. *pte_list = (unsigned long)spte;
  662. } else if (!(*pte_list & 1)) {
  663. rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
  664. desc = mmu_alloc_pte_list_desc(vcpu);
  665. desc->sptes[0] = (u64 *)*pte_list;
  666. desc->sptes[1] = spte;
  667. *pte_list = (unsigned long)desc | 1;
  668. ++count;
  669. } else {
  670. rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
  671. desc = (struct pte_list_desc *)(*pte_list & ~1ul);
  672. while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
  673. desc = desc->more;
  674. count += PTE_LIST_EXT;
  675. }
  676. if (desc->sptes[PTE_LIST_EXT-1]) {
  677. desc->more = mmu_alloc_pte_list_desc(vcpu);
  678. desc = desc->more;
  679. }
  680. for (i = 0; desc->sptes[i]; ++i)
  681. ++count;
  682. desc->sptes[i] = spte;
  683. }
  684. return count;
  685. }
  686. static u64 *pte_list_next(unsigned long *pte_list, u64 *spte)
  687. {
  688. struct pte_list_desc *desc;
  689. u64 *prev_spte;
  690. int i;
  691. if (!*pte_list)
  692. return NULL;
  693. else if (!(*pte_list & 1)) {
  694. if (!spte)
  695. return (u64 *)*pte_list;
  696. return NULL;
  697. }
  698. desc = (struct pte_list_desc *)(*pte_list & ~1ul);
  699. prev_spte = NULL;
  700. while (desc) {
  701. for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
  702. if (prev_spte == spte)
  703. return desc->sptes[i];
  704. prev_spte = desc->sptes[i];
  705. }
  706. desc = desc->more;
  707. }
  708. return NULL;
  709. }
  710. static void
  711. pte_list_desc_remove_entry(unsigned long *pte_list, struct pte_list_desc *desc,
  712. int i, struct pte_list_desc *prev_desc)
  713. {
  714. int j;
  715. for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
  716. ;
  717. desc->sptes[i] = desc->sptes[j];
  718. desc->sptes[j] = NULL;
  719. if (j != 0)
  720. return;
  721. if (!prev_desc && !desc->more)
  722. *pte_list = (unsigned long)desc->sptes[0];
  723. else
  724. if (prev_desc)
  725. prev_desc->more = desc->more;
  726. else
  727. *pte_list = (unsigned long)desc->more | 1;
  728. mmu_free_pte_list_desc(desc);
  729. }
  730. static void pte_list_remove(u64 *spte, unsigned long *pte_list)
  731. {
  732. struct pte_list_desc *desc;
  733. struct pte_list_desc *prev_desc;
  734. int i;
  735. if (!*pte_list) {
  736. printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
  737. BUG();
  738. } else if (!(*pte_list & 1)) {
  739. rmap_printk("pte_list_remove: %p 1->0\n", spte);
  740. if ((u64 *)*pte_list != spte) {
  741. printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
  742. BUG();
  743. }
  744. *pte_list = 0;
  745. } else {
  746. rmap_printk("pte_list_remove: %p many->many\n", spte);
  747. desc = (struct pte_list_desc *)(*pte_list & ~1ul);
  748. prev_desc = NULL;
  749. while (desc) {
  750. for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
  751. if (desc->sptes[i] == spte) {
  752. pte_list_desc_remove_entry(pte_list,
  753. desc, i,
  754. prev_desc);
  755. return;
  756. }
  757. prev_desc = desc;
  758. desc = desc->more;
  759. }
  760. pr_err("pte_list_remove: %p many->many\n", spte);
  761. BUG();
  762. }
  763. }
  764. typedef void (*pte_list_walk_fn) (u64 *spte);
  765. static void pte_list_walk(unsigned long *pte_list, pte_list_walk_fn fn)
  766. {
  767. struct pte_list_desc *desc;
  768. int i;
  769. if (!*pte_list)
  770. return;
  771. if (!(*pte_list & 1))
  772. return fn((u64 *)*pte_list);
  773. desc = (struct pte_list_desc *)(*pte_list & ~1ul);
  774. while (desc) {
  775. for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i)
  776. fn(desc->sptes[i]);
  777. desc = desc->more;
  778. }
  779. }
  780. static unsigned long *__gfn_to_rmap(gfn_t gfn, int level,
  781. struct kvm_memory_slot *slot)
  782. {
  783. struct kvm_lpage_info *linfo;
  784. if (likely(level == PT_PAGE_TABLE_LEVEL))
  785. return &slot->rmap[gfn - slot->base_gfn];
  786. linfo = lpage_info_slot(gfn, slot, level);
  787. return &linfo->rmap_pde;
  788. }
  789. /*
  790. * Take gfn and return the reverse mapping to it.
  791. */
  792. static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
  793. {
  794. struct kvm_memory_slot *slot;
  795. slot = gfn_to_memslot(kvm, gfn);
  796. return __gfn_to_rmap(gfn, level, slot);
  797. }
  798. static bool rmap_can_add(struct kvm_vcpu *vcpu)
  799. {
  800. struct kvm_mmu_memory_cache *cache;
  801. cache = &vcpu->arch.mmu_pte_list_desc_cache;
  802. return mmu_memory_cache_free_objects(cache);
  803. }
  804. static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  805. {
  806. struct kvm_mmu_page *sp;
  807. unsigned long *rmapp;
  808. sp = page_header(__pa(spte));
  809. kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
  810. rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
  811. return pte_list_add(vcpu, spte, rmapp);
  812. }
  813. static u64 *rmap_next(unsigned long *rmapp, u64 *spte)
  814. {
  815. return pte_list_next(rmapp, spte);
  816. }
  817. static void rmap_remove(struct kvm *kvm, u64 *spte)
  818. {
  819. struct kvm_mmu_page *sp;
  820. gfn_t gfn;
  821. unsigned long *rmapp;
  822. sp = page_header(__pa(spte));
  823. gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
  824. rmapp = gfn_to_rmap(kvm, gfn, sp->role.level);
  825. pte_list_remove(spte, rmapp);
  826. }
  827. static void drop_spte(struct kvm *kvm, u64 *sptep)
  828. {
  829. if (mmu_spte_clear_track_bits(sptep))
  830. rmap_remove(kvm, sptep);
  831. }
  832. int kvm_mmu_rmap_write_protect(struct kvm *kvm, u64 gfn,
  833. struct kvm_memory_slot *slot)
  834. {
  835. unsigned long *rmapp;
  836. u64 *spte;
  837. int i, write_protected = 0;
  838. rmapp = __gfn_to_rmap(gfn, PT_PAGE_TABLE_LEVEL, slot);
  839. spte = rmap_next(rmapp, NULL);
  840. while (spte) {
  841. BUG_ON(!(*spte & PT_PRESENT_MASK));
  842. rmap_printk("rmap_write_protect: spte %p %llx\n", spte, *spte);
  843. if (is_writable_pte(*spte)) {
  844. mmu_spte_update(spte, *spte & ~PT_WRITABLE_MASK);
  845. write_protected = 1;
  846. }
  847. spte = rmap_next(rmapp, spte);
  848. }
  849. /* check for huge page mappings */
  850. for (i = PT_DIRECTORY_LEVEL;
  851. i < PT_PAGE_TABLE_LEVEL + KVM_NR_PAGE_SIZES; ++i) {
  852. rmapp = __gfn_to_rmap(gfn, i, slot);
  853. spte = rmap_next(rmapp, NULL);
  854. while (spte) {
  855. BUG_ON(!(*spte & PT_PRESENT_MASK));
  856. BUG_ON(!is_large_pte(*spte));
  857. pgprintk("rmap_write_protect(large): spte %p %llx %lld\n", spte, *spte, gfn);
  858. if (is_writable_pte(*spte)) {
  859. drop_spte(kvm, spte);
  860. --kvm->stat.lpages;
  861. spte = NULL;
  862. write_protected = 1;
  863. }
  864. spte = rmap_next(rmapp, spte);
  865. }
  866. }
  867. return write_protected;
  868. }
  869. static int rmap_write_protect(struct kvm *kvm, u64 gfn)
  870. {
  871. struct kvm_memory_slot *slot;
  872. slot = gfn_to_memslot(kvm, gfn);
  873. return kvm_mmu_rmap_write_protect(kvm, gfn, slot);
  874. }
  875. static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
  876. unsigned long data)
  877. {
  878. u64 *spte;
  879. int need_tlb_flush = 0;
  880. while ((spte = rmap_next(rmapp, NULL))) {
  881. BUG_ON(!(*spte & PT_PRESENT_MASK));
  882. rmap_printk("kvm_rmap_unmap_hva: spte %p %llx\n", spte, *spte);
  883. drop_spte(kvm, spte);
  884. need_tlb_flush = 1;
  885. }
  886. return need_tlb_flush;
  887. }
  888. static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp,
  889. unsigned long data)
  890. {
  891. int need_flush = 0;
  892. u64 *spte, new_spte;
  893. pte_t *ptep = (pte_t *)data;
  894. pfn_t new_pfn;
  895. WARN_ON(pte_huge(*ptep));
  896. new_pfn = pte_pfn(*ptep);
  897. spte = rmap_next(rmapp, NULL);
  898. while (spte) {
  899. BUG_ON(!is_shadow_present_pte(*spte));
  900. rmap_printk("kvm_set_pte_rmapp: spte %p %llx\n", spte, *spte);
  901. need_flush = 1;
  902. if (pte_write(*ptep)) {
  903. drop_spte(kvm, spte);
  904. spte = rmap_next(rmapp, NULL);
  905. } else {
  906. new_spte = *spte &~ (PT64_BASE_ADDR_MASK);
  907. new_spte |= (u64)new_pfn << PAGE_SHIFT;
  908. new_spte &= ~PT_WRITABLE_MASK;
  909. new_spte &= ~SPTE_HOST_WRITEABLE;
  910. new_spte &= ~shadow_accessed_mask;
  911. mmu_spte_clear_track_bits(spte);
  912. mmu_spte_set(spte, new_spte);
  913. spte = rmap_next(rmapp, spte);
  914. }
  915. }
  916. if (need_flush)
  917. kvm_flush_remote_tlbs(kvm);
  918. return 0;
  919. }
  920. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  921. unsigned long data,
  922. int (*handler)(struct kvm *kvm, unsigned long *rmapp,
  923. unsigned long data))
  924. {
  925. int j;
  926. int ret;
  927. int retval = 0;
  928. struct kvm_memslots *slots;
  929. struct kvm_memory_slot *memslot;
  930. slots = kvm_memslots(kvm);
  931. kvm_for_each_memslot(memslot, slots) {
  932. unsigned long start = memslot->userspace_addr;
  933. unsigned long end;
  934. end = start + (memslot->npages << PAGE_SHIFT);
  935. if (hva >= start && hva < end) {
  936. gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
  937. gfn_t gfn = memslot->base_gfn + gfn_offset;
  938. ret = handler(kvm, &memslot->rmap[gfn_offset], data);
  939. for (j = 0; j < KVM_NR_PAGE_SIZES - 1; ++j) {
  940. struct kvm_lpage_info *linfo;
  941. linfo = lpage_info_slot(gfn, memslot,
  942. PT_DIRECTORY_LEVEL + j);
  943. ret |= handler(kvm, &linfo->rmap_pde, data);
  944. }
  945. trace_kvm_age_page(hva, memslot, ret);
  946. retval |= ret;
  947. }
  948. }
  949. return retval;
  950. }
  951. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  952. {
  953. return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
  954. }
  955. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  956. {
  957. kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
  958. }
  959. static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  960. unsigned long data)
  961. {
  962. u64 *spte;
  963. int young = 0;
  964. /*
  965. * Emulate the accessed bit for EPT, by checking if this page has
  966. * an EPT mapping, and clearing it if it does. On the next access,
  967. * a new EPT mapping will be established.
  968. * This has some overhead, but not as much as the cost of swapping
  969. * out actively used pages or breaking up actively used hugepages.
  970. */
  971. if (!shadow_accessed_mask)
  972. return kvm_unmap_rmapp(kvm, rmapp, data);
  973. spte = rmap_next(rmapp, NULL);
  974. while (spte) {
  975. int _young;
  976. u64 _spte = *spte;
  977. BUG_ON(!(_spte & PT_PRESENT_MASK));
  978. _young = _spte & PT_ACCESSED_MASK;
  979. if (_young) {
  980. young = 1;
  981. clear_bit(PT_ACCESSED_SHIFT, (unsigned long *)spte);
  982. }
  983. spte = rmap_next(rmapp, spte);
  984. }
  985. return young;
  986. }
  987. static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  988. unsigned long data)
  989. {
  990. u64 *spte;
  991. int young = 0;
  992. /*
  993. * If there's no access bit in the secondary pte set by the
  994. * hardware it's up to gup-fast/gup to set the access bit in
  995. * the primary pte or in the page structure.
  996. */
  997. if (!shadow_accessed_mask)
  998. goto out;
  999. spte = rmap_next(rmapp, NULL);
  1000. while (spte) {
  1001. u64 _spte = *spte;
  1002. BUG_ON(!(_spte & PT_PRESENT_MASK));
  1003. young = _spte & PT_ACCESSED_MASK;
  1004. if (young) {
  1005. young = 1;
  1006. break;
  1007. }
  1008. spte = rmap_next(rmapp, spte);
  1009. }
  1010. out:
  1011. return young;
  1012. }
  1013. #define RMAP_RECYCLE_THRESHOLD 1000
  1014. static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  1015. {
  1016. unsigned long *rmapp;
  1017. struct kvm_mmu_page *sp;
  1018. sp = page_header(__pa(spte));
  1019. rmapp = gfn_to_rmap(vcpu->kvm, gfn, sp->role.level);
  1020. kvm_unmap_rmapp(vcpu->kvm, rmapp, 0);
  1021. kvm_flush_remote_tlbs(vcpu->kvm);
  1022. }
  1023. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  1024. {
  1025. return kvm_handle_hva(kvm, hva, 0, kvm_age_rmapp);
  1026. }
  1027. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  1028. {
  1029. return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
  1030. }
  1031. #ifdef MMU_DEBUG
  1032. static int is_empty_shadow_page(u64 *spt)
  1033. {
  1034. u64 *pos;
  1035. u64 *end;
  1036. for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
  1037. if (is_shadow_present_pte(*pos)) {
  1038. printk(KERN_ERR "%s: %p %llx\n", __func__,
  1039. pos, *pos);
  1040. return 0;
  1041. }
  1042. return 1;
  1043. }
  1044. #endif
  1045. /*
  1046. * This value is the sum of all of the kvm instances's
  1047. * kvm->arch.n_used_mmu_pages values. We need a global,
  1048. * aggregate version in order to make the slab shrinker
  1049. * faster
  1050. */
  1051. static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
  1052. {
  1053. kvm->arch.n_used_mmu_pages += nr;
  1054. percpu_counter_add(&kvm_total_used_mmu_pages, nr);
  1055. }
  1056. /*
  1057. * Remove the sp from shadow page cache, after call it,
  1058. * we can not find this sp from the cache, and the shadow
  1059. * page table is still valid.
  1060. * It should be under the protection of mmu lock.
  1061. */
  1062. static void kvm_mmu_isolate_page(struct kvm_mmu_page *sp)
  1063. {
  1064. ASSERT(is_empty_shadow_page(sp->spt));
  1065. hlist_del(&sp->hash_link);
  1066. if (!sp->role.direct)
  1067. free_page((unsigned long)sp->gfns);
  1068. }
  1069. /*
  1070. * Free the shadow page table and the sp, we can do it
  1071. * out of the protection of mmu lock.
  1072. */
  1073. static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
  1074. {
  1075. list_del(&sp->link);
  1076. free_page((unsigned long)sp->spt);
  1077. kmem_cache_free(mmu_page_header_cache, sp);
  1078. }
  1079. static unsigned kvm_page_table_hashfn(gfn_t gfn)
  1080. {
  1081. return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
  1082. }
  1083. static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
  1084. struct kvm_mmu_page *sp, u64 *parent_pte)
  1085. {
  1086. if (!parent_pte)
  1087. return;
  1088. pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
  1089. }
  1090. static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
  1091. u64 *parent_pte)
  1092. {
  1093. pte_list_remove(parent_pte, &sp->parent_ptes);
  1094. }
  1095. static void drop_parent_pte(struct kvm_mmu_page *sp,
  1096. u64 *parent_pte)
  1097. {
  1098. mmu_page_remove_parent_pte(sp, parent_pte);
  1099. mmu_spte_clear_no_track(parent_pte);
  1100. }
  1101. static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu,
  1102. u64 *parent_pte, int direct)
  1103. {
  1104. struct kvm_mmu_page *sp;
  1105. sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache,
  1106. sizeof *sp);
  1107. sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache, PAGE_SIZE);
  1108. if (!direct)
  1109. sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache,
  1110. PAGE_SIZE);
  1111. set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
  1112. list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
  1113. bitmap_zero(sp->slot_bitmap, KVM_MEM_SLOTS_NUM);
  1114. sp->parent_ptes = 0;
  1115. mmu_page_add_parent_pte(vcpu, sp, parent_pte);
  1116. kvm_mod_used_mmu_pages(vcpu->kvm, +1);
  1117. return sp;
  1118. }
  1119. static void mark_unsync(u64 *spte);
  1120. static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
  1121. {
  1122. pte_list_walk(&sp->parent_ptes, mark_unsync);
  1123. }
  1124. static void mark_unsync(u64 *spte)
  1125. {
  1126. struct kvm_mmu_page *sp;
  1127. unsigned int index;
  1128. sp = page_header(__pa(spte));
  1129. index = spte - sp->spt;
  1130. if (__test_and_set_bit(index, sp->unsync_child_bitmap))
  1131. return;
  1132. if (sp->unsync_children++)
  1133. return;
  1134. kvm_mmu_mark_parents_unsync(sp);
  1135. }
  1136. static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
  1137. struct kvm_mmu_page *sp)
  1138. {
  1139. return 1;
  1140. }
  1141. static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  1142. {
  1143. }
  1144. static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
  1145. struct kvm_mmu_page *sp, u64 *spte,
  1146. const void *pte)
  1147. {
  1148. WARN_ON(1);
  1149. }
  1150. #define KVM_PAGE_ARRAY_NR 16
  1151. struct kvm_mmu_pages {
  1152. struct mmu_page_and_offset {
  1153. struct kvm_mmu_page *sp;
  1154. unsigned int idx;
  1155. } page[KVM_PAGE_ARRAY_NR];
  1156. unsigned int nr;
  1157. };
  1158. static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
  1159. int idx)
  1160. {
  1161. int i;
  1162. if (sp->unsync)
  1163. for (i=0; i < pvec->nr; i++)
  1164. if (pvec->page[i].sp == sp)
  1165. return 0;
  1166. pvec->page[pvec->nr].sp = sp;
  1167. pvec->page[pvec->nr].idx = idx;
  1168. pvec->nr++;
  1169. return (pvec->nr == KVM_PAGE_ARRAY_NR);
  1170. }
  1171. static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
  1172. struct kvm_mmu_pages *pvec)
  1173. {
  1174. int i, ret, nr_unsync_leaf = 0;
  1175. for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
  1176. struct kvm_mmu_page *child;
  1177. u64 ent = sp->spt[i];
  1178. if (!is_shadow_present_pte(ent) || is_large_pte(ent))
  1179. goto clear_child_bitmap;
  1180. child = page_header(ent & PT64_BASE_ADDR_MASK);
  1181. if (child->unsync_children) {
  1182. if (mmu_pages_add(pvec, child, i))
  1183. return -ENOSPC;
  1184. ret = __mmu_unsync_walk(child, pvec);
  1185. if (!ret)
  1186. goto clear_child_bitmap;
  1187. else if (ret > 0)
  1188. nr_unsync_leaf += ret;
  1189. else
  1190. return ret;
  1191. } else if (child->unsync) {
  1192. nr_unsync_leaf++;
  1193. if (mmu_pages_add(pvec, child, i))
  1194. return -ENOSPC;
  1195. } else
  1196. goto clear_child_bitmap;
  1197. continue;
  1198. clear_child_bitmap:
  1199. __clear_bit(i, sp->unsync_child_bitmap);
  1200. sp->unsync_children--;
  1201. WARN_ON((int)sp->unsync_children < 0);
  1202. }
  1203. return nr_unsync_leaf;
  1204. }
  1205. static int mmu_unsync_walk(struct kvm_mmu_page *sp,
  1206. struct kvm_mmu_pages *pvec)
  1207. {
  1208. if (!sp->unsync_children)
  1209. return 0;
  1210. mmu_pages_add(pvec, sp, 0);
  1211. return __mmu_unsync_walk(sp, pvec);
  1212. }
  1213. static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
  1214. {
  1215. WARN_ON(!sp->unsync);
  1216. trace_kvm_mmu_sync_page(sp);
  1217. sp->unsync = 0;
  1218. --kvm->stat.mmu_unsync;
  1219. }
  1220. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1221. struct list_head *invalid_list);
  1222. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1223. struct list_head *invalid_list);
  1224. #define for_each_gfn_sp(kvm, sp, gfn, pos) \
  1225. hlist_for_each_entry(sp, pos, \
  1226. &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
  1227. if ((sp)->gfn != (gfn)) {} else
  1228. #define for_each_gfn_indirect_valid_sp(kvm, sp, gfn, pos) \
  1229. hlist_for_each_entry(sp, pos, \
  1230. &(kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)], hash_link) \
  1231. if ((sp)->gfn != (gfn) || (sp)->role.direct || \
  1232. (sp)->role.invalid) {} else
  1233. /* @sp->gfn should be write-protected at the call site */
  1234. static int __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1235. struct list_head *invalid_list, bool clear_unsync)
  1236. {
  1237. if (sp->role.cr4_pae != !!is_pae(vcpu)) {
  1238. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1239. return 1;
  1240. }
  1241. if (clear_unsync)
  1242. kvm_unlink_unsync_page(vcpu->kvm, sp);
  1243. if (vcpu->arch.mmu.sync_page(vcpu, sp)) {
  1244. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1245. return 1;
  1246. }
  1247. kvm_mmu_flush_tlb(vcpu);
  1248. return 0;
  1249. }
  1250. static int kvm_sync_page_transient(struct kvm_vcpu *vcpu,
  1251. struct kvm_mmu_page *sp)
  1252. {
  1253. LIST_HEAD(invalid_list);
  1254. int ret;
  1255. ret = __kvm_sync_page(vcpu, sp, &invalid_list, false);
  1256. if (ret)
  1257. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1258. return ret;
  1259. }
  1260. #ifdef CONFIG_KVM_MMU_AUDIT
  1261. #include "mmu_audit.c"
  1262. #else
  1263. static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
  1264. static void mmu_audit_disable(void) { }
  1265. #endif
  1266. static int kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1267. struct list_head *invalid_list)
  1268. {
  1269. return __kvm_sync_page(vcpu, sp, invalid_list, true);
  1270. }
  1271. /* @gfn should be write-protected at the call site */
  1272. static void kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
  1273. {
  1274. struct kvm_mmu_page *s;
  1275. struct hlist_node *node;
  1276. LIST_HEAD(invalid_list);
  1277. bool flush = false;
  1278. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
  1279. if (!s->unsync)
  1280. continue;
  1281. WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
  1282. kvm_unlink_unsync_page(vcpu->kvm, s);
  1283. if ((s->role.cr4_pae != !!is_pae(vcpu)) ||
  1284. (vcpu->arch.mmu.sync_page(vcpu, s))) {
  1285. kvm_mmu_prepare_zap_page(vcpu->kvm, s, &invalid_list);
  1286. continue;
  1287. }
  1288. flush = true;
  1289. }
  1290. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1291. if (flush)
  1292. kvm_mmu_flush_tlb(vcpu);
  1293. }
  1294. struct mmu_page_path {
  1295. struct kvm_mmu_page *parent[PT64_ROOT_LEVEL-1];
  1296. unsigned int idx[PT64_ROOT_LEVEL-1];
  1297. };
  1298. #define for_each_sp(pvec, sp, parents, i) \
  1299. for (i = mmu_pages_next(&pvec, &parents, -1), \
  1300. sp = pvec.page[i].sp; \
  1301. i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
  1302. i = mmu_pages_next(&pvec, &parents, i))
  1303. static int mmu_pages_next(struct kvm_mmu_pages *pvec,
  1304. struct mmu_page_path *parents,
  1305. int i)
  1306. {
  1307. int n;
  1308. for (n = i+1; n < pvec->nr; n++) {
  1309. struct kvm_mmu_page *sp = pvec->page[n].sp;
  1310. if (sp->role.level == PT_PAGE_TABLE_LEVEL) {
  1311. parents->idx[0] = pvec->page[n].idx;
  1312. return n;
  1313. }
  1314. parents->parent[sp->role.level-2] = sp;
  1315. parents->idx[sp->role.level-1] = pvec->page[n].idx;
  1316. }
  1317. return n;
  1318. }
  1319. static void mmu_pages_clear_parents(struct mmu_page_path *parents)
  1320. {
  1321. struct kvm_mmu_page *sp;
  1322. unsigned int level = 0;
  1323. do {
  1324. unsigned int idx = parents->idx[level];
  1325. sp = parents->parent[level];
  1326. if (!sp)
  1327. return;
  1328. --sp->unsync_children;
  1329. WARN_ON((int)sp->unsync_children < 0);
  1330. __clear_bit(idx, sp->unsync_child_bitmap);
  1331. level++;
  1332. } while (level < PT64_ROOT_LEVEL-1 && !sp->unsync_children);
  1333. }
  1334. static void kvm_mmu_pages_init(struct kvm_mmu_page *parent,
  1335. struct mmu_page_path *parents,
  1336. struct kvm_mmu_pages *pvec)
  1337. {
  1338. parents->parent[parent->role.level-1] = NULL;
  1339. pvec->nr = 0;
  1340. }
  1341. static void mmu_sync_children(struct kvm_vcpu *vcpu,
  1342. struct kvm_mmu_page *parent)
  1343. {
  1344. int i;
  1345. struct kvm_mmu_page *sp;
  1346. struct mmu_page_path parents;
  1347. struct kvm_mmu_pages pages;
  1348. LIST_HEAD(invalid_list);
  1349. kvm_mmu_pages_init(parent, &parents, &pages);
  1350. while (mmu_unsync_walk(parent, &pages)) {
  1351. int protected = 0;
  1352. for_each_sp(pages, sp, parents, i)
  1353. protected |= rmap_write_protect(vcpu->kvm, sp->gfn);
  1354. if (protected)
  1355. kvm_flush_remote_tlbs(vcpu->kvm);
  1356. for_each_sp(pages, sp, parents, i) {
  1357. kvm_sync_page(vcpu, sp, &invalid_list);
  1358. mmu_pages_clear_parents(&parents);
  1359. }
  1360. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  1361. cond_resched_lock(&vcpu->kvm->mmu_lock);
  1362. kvm_mmu_pages_init(parent, &parents, &pages);
  1363. }
  1364. }
  1365. static void init_shadow_page_table(struct kvm_mmu_page *sp)
  1366. {
  1367. int i;
  1368. for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
  1369. sp->spt[i] = 0ull;
  1370. }
  1371. static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
  1372. {
  1373. sp->write_flooding_count = 0;
  1374. }
  1375. static void clear_sp_write_flooding_count(u64 *spte)
  1376. {
  1377. struct kvm_mmu_page *sp = page_header(__pa(spte));
  1378. __clear_sp_write_flooding_count(sp);
  1379. }
  1380. static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
  1381. gfn_t gfn,
  1382. gva_t gaddr,
  1383. unsigned level,
  1384. int direct,
  1385. unsigned access,
  1386. u64 *parent_pte)
  1387. {
  1388. union kvm_mmu_page_role role;
  1389. unsigned quadrant;
  1390. struct kvm_mmu_page *sp;
  1391. struct hlist_node *node;
  1392. bool need_sync = false;
  1393. role = vcpu->arch.mmu.base_role;
  1394. role.level = level;
  1395. role.direct = direct;
  1396. if (role.direct)
  1397. role.cr4_pae = 0;
  1398. role.access = access;
  1399. if (!vcpu->arch.mmu.direct_map
  1400. && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
  1401. quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
  1402. quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
  1403. role.quadrant = quadrant;
  1404. }
  1405. for_each_gfn_sp(vcpu->kvm, sp, gfn, node) {
  1406. if (!need_sync && sp->unsync)
  1407. need_sync = true;
  1408. if (sp->role.word != role.word)
  1409. continue;
  1410. if (sp->unsync && kvm_sync_page_transient(vcpu, sp))
  1411. break;
  1412. mmu_page_add_parent_pte(vcpu, sp, parent_pte);
  1413. if (sp->unsync_children) {
  1414. kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
  1415. kvm_mmu_mark_parents_unsync(sp);
  1416. } else if (sp->unsync)
  1417. kvm_mmu_mark_parents_unsync(sp);
  1418. __clear_sp_write_flooding_count(sp);
  1419. trace_kvm_mmu_get_page(sp, false);
  1420. return sp;
  1421. }
  1422. ++vcpu->kvm->stat.mmu_cache_miss;
  1423. sp = kvm_mmu_alloc_page(vcpu, parent_pte, direct);
  1424. if (!sp)
  1425. return sp;
  1426. sp->gfn = gfn;
  1427. sp->role = role;
  1428. hlist_add_head(&sp->hash_link,
  1429. &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
  1430. if (!direct) {
  1431. if (rmap_write_protect(vcpu->kvm, gfn))
  1432. kvm_flush_remote_tlbs(vcpu->kvm);
  1433. if (level > PT_PAGE_TABLE_LEVEL && need_sync)
  1434. kvm_sync_pages(vcpu, gfn);
  1435. account_shadowed(vcpu->kvm, gfn);
  1436. }
  1437. init_shadow_page_table(sp);
  1438. trace_kvm_mmu_get_page(sp, true);
  1439. return sp;
  1440. }
  1441. static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
  1442. struct kvm_vcpu *vcpu, u64 addr)
  1443. {
  1444. iterator->addr = addr;
  1445. iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
  1446. iterator->level = vcpu->arch.mmu.shadow_root_level;
  1447. if (iterator->level == PT64_ROOT_LEVEL &&
  1448. vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
  1449. !vcpu->arch.mmu.direct_map)
  1450. --iterator->level;
  1451. if (iterator->level == PT32E_ROOT_LEVEL) {
  1452. iterator->shadow_addr
  1453. = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
  1454. iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
  1455. --iterator->level;
  1456. if (!iterator->shadow_addr)
  1457. iterator->level = 0;
  1458. }
  1459. }
  1460. static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
  1461. {
  1462. if (iterator->level < PT_PAGE_TABLE_LEVEL)
  1463. return false;
  1464. iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
  1465. iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
  1466. return true;
  1467. }
  1468. static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
  1469. u64 spte)
  1470. {
  1471. if (is_last_spte(spte, iterator->level)) {
  1472. iterator->level = 0;
  1473. return;
  1474. }
  1475. iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
  1476. --iterator->level;
  1477. }
  1478. static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
  1479. {
  1480. return __shadow_walk_next(iterator, *iterator->sptep);
  1481. }
  1482. static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
  1483. {
  1484. u64 spte;
  1485. spte = __pa(sp->spt)
  1486. | PT_PRESENT_MASK | PT_ACCESSED_MASK
  1487. | PT_WRITABLE_MASK | PT_USER_MASK;
  1488. mmu_spte_set(sptep, spte);
  1489. }
  1490. static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
  1491. {
  1492. if (is_large_pte(*sptep)) {
  1493. drop_spte(vcpu->kvm, sptep);
  1494. --vcpu->kvm->stat.lpages;
  1495. kvm_flush_remote_tlbs(vcpu->kvm);
  1496. }
  1497. }
  1498. static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  1499. unsigned direct_access)
  1500. {
  1501. if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
  1502. struct kvm_mmu_page *child;
  1503. /*
  1504. * For the direct sp, if the guest pte's dirty bit
  1505. * changed form clean to dirty, it will corrupt the
  1506. * sp's access: allow writable in the read-only sp,
  1507. * so we should update the spte at this point to get
  1508. * a new sp with the correct access.
  1509. */
  1510. child = page_header(*sptep & PT64_BASE_ADDR_MASK);
  1511. if (child->role.access == direct_access)
  1512. return;
  1513. drop_parent_pte(child, sptep);
  1514. kvm_flush_remote_tlbs(vcpu->kvm);
  1515. }
  1516. }
  1517. static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
  1518. u64 *spte)
  1519. {
  1520. u64 pte;
  1521. struct kvm_mmu_page *child;
  1522. pte = *spte;
  1523. if (is_shadow_present_pte(pte)) {
  1524. if (is_last_spte(pte, sp->role.level)) {
  1525. drop_spte(kvm, spte);
  1526. if (is_large_pte(pte))
  1527. --kvm->stat.lpages;
  1528. } else {
  1529. child = page_header(pte & PT64_BASE_ADDR_MASK);
  1530. drop_parent_pte(child, spte);
  1531. }
  1532. return true;
  1533. }
  1534. if (is_mmio_spte(pte))
  1535. mmu_spte_clear_no_track(spte);
  1536. return false;
  1537. }
  1538. static void kvm_mmu_page_unlink_children(struct kvm *kvm,
  1539. struct kvm_mmu_page *sp)
  1540. {
  1541. unsigned i;
  1542. for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
  1543. mmu_page_zap_pte(kvm, sp, sp->spt + i);
  1544. }
  1545. static void kvm_mmu_put_page(struct kvm_mmu_page *sp, u64 *parent_pte)
  1546. {
  1547. mmu_page_remove_parent_pte(sp, parent_pte);
  1548. }
  1549. static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
  1550. {
  1551. u64 *parent_pte;
  1552. while ((parent_pte = pte_list_next(&sp->parent_ptes, NULL)))
  1553. drop_parent_pte(sp, parent_pte);
  1554. }
  1555. static int mmu_zap_unsync_children(struct kvm *kvm,
  1556. struct kvm_mmu_page *parent,
  1557. struct list_head *invalid_list)
  1558. {
  1559. int i, zapped = 0;
  1560. struct mmu_page_path parents;
  1561. struct kvm_mmu_pages pages;
  1562. if (parent->role.level == PT_PAGE_TABLE_LEVEL)
  1563. return 0;
  1564. kvm_mmu_pages_init(parent, &parents, &pages);
  1565. while (mmu_unsync_walk(parent, &pages)) {
  1566. struct kvm_mmu_page *sp;
  1567. for_each_sp(pages, sp, parents, i) {
  1568. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  1569. mmu_pages_clear_parents(&parents);
  1570. zapped++;
  1571. }
  1572. kvm_mmu_pages_init(parent, &parents, &pages);
  1573. }
  1574. return zapped;
  1575. }
  1576. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1577. struct list_head *invalid_list)
  1578. {
  1579. int ret;
  1580. trace_kvm_mmu_prepare_zap_page(sp);
  1581. ++kvm->stat.mmu_shadow_zapped;
  1582. ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
  1583. kvm_mmu_page_unlink_children(kvm, sp);
  1584. kvm_mmu_unlink_parents(kvm, sp);
  1585. if (!sp->role.invalid && !sp->role.direct)
  1586. unaccount_shadowed(kvm, sp->gfn);
  1587. if (sp->unsync)
  1588. kvm_unlink_unsync_page(kvm, sp);
  1589. if (!sp->root_count) {
  1590. /* Count self */
  1591. ret++;
  1592. list_move(&sp->link, invalid_list);
  1593. kvm_mod_used_mmu_pages(kvm, -1);
  1594. } else {
  1595. list_move(&sp->link, &kvm->arch.active_mmu_pages);
  1596. kvm_reload_remote_mmus(kvm);
  1597. }
  1598. sp->role.invalid = 1;
  1599. return ret;
  1600. }
  1601. static void kvm_mmu_isolate_pages(struct list_head *invalid_list)
  1602. {
  1603. struct kvm_mmu_page *sp;
  1604. list_for_each_entry(sp, invalid_list, link)
  1605. kvm_mmu_isolate_page(sp);
  1606. }
  1607. static void free_pages_rcu(struct rcu_head *head)
  1608. {
  1609. struct kvm_mmu_page *next, *sp;
  1610. sp = container_of(head, struct kvm_mmu_page, rcu);
  1611. while (sp) {
  1612. if (!list_empty(&sp->link))
  1613. next = list_first_entry(&sp->link,
  1614. struct kvm_mmu_page, link);
  1615. else
  1616. next = NULL;
  1617. kvm_mmu_free_page(sp);
  1618. sp = next;
  1619. }
  1620. }
  1621. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1622. struct list_head *invalid_list)
  1623. {
  1624. struct kvm_mmu_page *sp;
  1625. if (list_empty(invalid_list))
  1626. return;
  1627. kvm_flush_remote_tlbs(kvm);
  1628. if (atomic_read(&kvm->arch.reader_counter)) {
  1629. kvm_mmu_isolate_pages(invalid_list);
  1630. sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
  1631. list_del_init(invalid_list);
  1632. trace_kvm_mmu_delay_free_pages(sp);
  1633. call_rcu(&sp->rcu, free_pages_rcu);
  1634. return;
  1635. }
  1636. do {
  1637. sp = list_first_entry(invalid_list, struct kvm_mmu_page, link);
  1638. WARN_ON(!sp->role.invalid || sp->root_count);
  1639. kvm_mmu_isolate_page(sp);
  1640. kvm_mmu_free_page(sp);
  1641. } while (!list_empty(invalid_list));
  1642. }
  1643. /*
  1644. * Changing the number of mmu pages allocated to the vm
  1645. * Note: if goal_nr_mmu_pages is too small, you will get dead lock
  1646. */
  1647. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
  1648. {
  1649. LIST_HEAD(invalid_list);
  1650. /*
  1651. * If we set the number of mmu pages to be smaller be than the
  1652. * number of actived pages , we must to free some mmu pages before we
  1653. * change the value
  1654. */
  1655. if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
  1656. while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages &&
  1657. !list_empty(&kvm->arch.active_mmu_pages)) {
  1658. struct kvm_mmu_page *page;
  1659. page = container_of(kvm->arch.active_mmu_pages.prev,
  1660. struct kvm_mmu_page, link);
  1661. kvm_mmu_prepare_zap_page(kvm, page, &invalid_list);
  1662. }
  1663. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  1664. goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
  1665. }
  1666. kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
  1667. }
  1668. int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
  1669. {
  1670. struct kvm_mmu_page *sp;
  1671. struct hlist_node *node;
  1672. LIST_HEAD(invalid_list);
  1673. int r;
  1674. pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
  1675. r = 0;
  1676. spin_lock(&kvm->mmu_lock);
  1677. for_each_gfn_indirect_valid_sp(kvm, sp, gfn, node) {
  1678. pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
  1679. sp->role.word);
  1680. r = 1;
  1681. kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
  1682. }
  1683. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  1684. spin_unlock(&kvm->mmu_lock);
  1685. return r;
  1686. }
  1687. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
  1688. static void page_header_update_slot(struct kvm *kvm, void *pte, gfn_t gfn)
  1689. {
  1690. int slot = memslot_id(kvm, gfn);
  1691. struct kvm_mmu_page *sp = page_header(__pa(pte));
  1692. __set_bit(slot, sp->slot_bitmap);
  1693. }
  1694. /*
  1695. * The function is based on mtrr_type_lookup() in
  1696. * arch/x86/kernel/cpu/mtrr/generic.c
  1697. */
  1698. static int get_mtrr_type(struct mtrr_state_type *mtrr_state,
  1699. u64 start, u64 end)
  1700. {
  1701. int i;
  1702. u64 base, mask;
  1703. u8 prev_match, curr_match;
  1704. int num_var_ranges = KVM_NR_VAR_MTRR;
  1705. if (!mtrr_state->enabled)
  1706. return 0xFF;
  1707. /* Make end inclusive end, instead of exclusive */
  1708. end--;
  1709. /* Look in fixed ranges. Just return the type as per start */
  1710. if (mtrr_state->have_fixed && (start < 0x100000)) {
  1711. int idx;
  1712. if (start < 0x80000) {
  1713. idx = 0;
  1714. idx += (start >> 16);
  1715. return mtrr_state->fixed_ranges[idx];
  1716. } else if (start < 0xC0000) {
  1717. idx = 1 * 8;
  1718. idx += ((start - 0x80000) >> 14);
  1719. return mtrr_state->fixed_ranges[idx];
  1720. } else if (start < 0x1000000) {
  1721. idx = 3 * 8;
  1722. idx += ((start - 0xC0000) >> 12);
  1723. return mtrr_state->fixed_ranges[idx];
  1724. }
  1725. }
  1726. /*
  1727. * Look in variable ranges
  1728. * Look of multiple ranges matching this address and pick type
  1729. * as per MTRR precedence
  1730. */
  1731. if (!(mtrr_state->enabled & 2))
  1732. return mtrr_state->def_type;
  1733. prev_match = 0xFF;
  1734. for (i = 0; i < num_var_ranges; ++i) {
  1735. unsigned short start_state, end_state;
  1736. if (!(mtrr_state->var_ranges[i].mask_lo & (1 << 11)))
  1737. continue;
  1738. base = (((u64)mtrr_state->var_ranges[i].base_hi) << 32) +
  1739. (mtrr_state->var_ranges[i].base_lo & PAGE_MASK);
  1740. mask = (((u64)mtrr_state->var_ranges[i].mask_hi) << 32) +
  1741. (mtrr_state->var_ranges[i].mask_lo & PAGE_MASK);
  1742. start_state = ((start & mask) == (base & mask));
  1743. end_state = ((end & mask) == (base & mask));
  1744. if (start_state != end_state)
  1745. return 0xFE;
  1746. if ((start & mask) != (base & mask))
  1747. continue;
  1748. curr_match = mtrr_state->var_ranges[i].base_lo & 0xff;
  1749. if (prev_match == 0xFF) {
  1750. prev_match = curr_match;
  1751. continue;
  1752. }
  1753. if (prev_match == MTRR_TYPE_UNCACHABLE ||
  1754. curr_match == MTRR_TYPE_UNCACHABLE)
  1755. return MTRR_TYPE_UNCACHABLE;
  1756. if ((prev_match == MTRR_TYPE_WRBACK &&
  1757. curr_match == MTRR_TYPE_WRTHROUGH) ||
  1758. (prev_match == MTRR_TYPE_WRTHROUGH &&
  1759. curr_match == MTRR_TYPE_WRBACK)) {
  1760. prev_match = MTRR_TYPE_WRTHROUGH;
  1761. curr_match = MTRR_TYPE_WRTHROUGH;
  1762. }
  1763. if (prev_match != curr_match)
  1764. return MTRR_TYPE_UNCACHABLE;
  1765. }
  1766. if (prev_match != 0xFF)
  1767. return prev_match;
  1768. return mtrr_state->def_type;
  1769. }
  1770. u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn)
  1771. {
  1772. u8 mtrr;
  1773. mtrr = get_mtrr_type(&vcpu->arch.mtrr_state, gfn << PAGE_SHIFT,
  1774. (gfn << PAGE_SHIFT) + PAGE_SIZE);
  1775. if (mtrr == 0xfe || mtrr == 0xff)
  1776. mtrr = MTRR_TYPE_WRBACK;
  1777. return mtrr;
  1778. }
  1779. EXPORT_SYMBOL_GPL(kvm_get_guest_memory_type);
  1780. static void __kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  1781. {
  1782. trace_kvm_mmu_unsync_page(sp);
  1783. ++vcpu->kvm->stat.mmu_unsync;
  1784. sp->unsync = 1;
  1785. kvm_mmu_mark_parents_unsync(sp);
  1786. }
  1787. static void kvm_unsync_pages(struct kvm_vcpu *vcpu, gfn_t gfn)
  1788. {
  1789. struct kvm_mmu_page *s;
  1790. struct hlist_node *node;
  1791. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
  1792. if (s->unsync)
  1793. continue;
  1794. WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
  1795. __kvm_unsync_page(vcpu, s);
  1796. }
  1797. }
  1798. static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
  1799. bool can_unsync)
  1800. {
  1801. struct kvm_mmu_page *s;
  1802. struct hlist_node *node;
  1803. bool need_unsync = false;
  1804. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn, node) {
  1805. if (!can_unsync)
  1806. return 1;
  1807. if (s->role.level != PT_PAGE_TABLE_LEVEL)
  1808. return 1;
  1809. if (!need_unsync && !s->unsync) {
  1810. need_unsync = true;
  1811. }
  1812. }
  1813. if (need_unsync)
  1814. kvm_unsync_pages(vcpu, gfn);
  1815. return 0;
  1816. }
  1817. static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  1818. unsigned pte_access, int user_fault,
  1819. int write_fault, int level,
  1820. gfn_t gfn, pfn_t pfn, bool speculative,
  1821. bool can_unsync, bool host_writable)
  1822. {
  1823. u64 spte, entry = *sptep;
  1824. int ret = 0;
  1825. if (set_mmio_spte(sptep, gfn, pfn, pte_access))
  1826. return 0;
  1827. spte = PT_PRESENT_MASK;
  1828. if (!speculative)
  1829. spte |= shadow_accessed_mask;
  1830. if (pte_access & ACC_EXEC_MASK)
  1831. spte |= shadow_x_mask;
  1832. else
  1833. spte |= shadow_nx_mask;
  1834. if (pte_access & ACC_USER_MASK)
  1835. spte |= shadow_user_mask;
  1836. if (level > PT_PAGE_TABLE_LEVEL)
  1837. spte |= PT_PAGE_SIZE_MASK;
  1838. if (tdp_enabled)
  1839. spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
  1840. kvm_is_mmio_pfn(pfn));
  1841. if (host_writable)
  1842. spte |= SPTE_HOST_WRITEABLE;
  1843. else
  1844. pte_access &= ~ACC_WRITE_MASK;
  1845. spte |= (u64)pfn << PAGE_SHIFT;
  1846. if ((pte_access & ACC_WRITE_MASK)
  1847. || (!vcpu->arch.mmu.direct_map && write_fault
  1848. && !is_write_protection(vcpu) && !user_fault)) {
  1849. if (level > PT_PAGE_TABLE_LEVEL &&
  1850. has_wrprotected_page(vcpu->kvm, gfn, level)) {
  1851. ret = 1;
  1852. drop_spte(vcpu->kvm, sptep);
  1853. goto done;
  1854. }
  1855. spte |= PT_WRITABLE_MASK;
  1856. if (!vcpu->arch.mmu.direct_map
  1857. && !(pte_access & ACC_WRITE_MASK)) {
  1858. spte &= ~PT_USER_MASK;
  1859. /*
  1860. * If we converted a user page to a kernel page,
  1861. * so that the kernel can write to it when cr0.wp=0,
  1862. * then we should prevent the kernel from executing it
  1863. * if SMEP is enabled.
  1864. */
  1865. if (kvm_read_cr4_bits(vcpu, X86_CR4_SMEP))
  1866. spte |= PT64_NX_MASK;
  1867. }
  1868. /*
  1869. * Optimization: for pte sync, if spte was writable the hash
  1870. * lookup is unnecessary (and expensive). Write protection
  1871. * is responsibility of mmu_get_page / kvm_sync_page.
  1872. * Same reasoning can be applied to dirty page accounting.
  1873. */
  1874. if (!can_unsync && is_writable_pte(*sptep))
  1875. goto set_pte;
  1876. if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
  1877. pgprintk("%s: found shadow page for %llx, marking ro\n",
  1878. __func__, gfn);
  1879. ret = 1;
  1880. pte_access &= ~ACC_WRITE_MASK;
  1881. if (is_writable_pte(spte))
  1882. spte &= ~PT_WRITABLE_MASK;
  1883. }
  1884. }
  1885. if (pte_access & ACC_WRITE_MASK)
  1886. mark_page_dirty(vcpu->kvm, gfn);
  1887. set_pte:
  1888. mmu_spte_update(sptep, spte);
  1889. /*
  1890. * If we overwrite a writable spte with a read-only one we
  1891. * should flush remote TLBs. Otherwise rmap_write_protect
  1892. * will find a read-only spte, even though the writable spte
  1893. * might be cached on a CPU's TLB.
  1894. */
  1895. if (is_writable_pte(entry) && !is_writable_pte(*sptep))
  1896. kvm_flush_remote_tlbs(vcpu->kvm);
  1897. done:
  1898. return ret;
  1899. }
  1900. static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  1901. unsigned pt_access, unsigned pte_access,
  1902. int user_fault, int write_fault,
  1903. int *emulate, int level, gfn_t gfn,
  1904. pfn_t pfn, bool speculative,
  1905. bool host_writable)
  1906. {
  1907. int was_rmapped = 0;
  1908. int rmap_count;
  1909. pgprintk("%s: spte %llx access %x write_fault %d"
  1910. " user_fault %d gfn %llx\n",
  1911. __func__, *sptep, pt_access,
  1912. write_fault, user_fault, gfn);
  1913. if (is_rmap_spte(*sptep)) {
  1914. /*
  1915. * If we overwrite a PTE page pointer with a 2MB PMD, unlink
  1916. * the parent of the now unreachable PTE.
  1917. */
  1918. if (level > PT_PAGE_TABLE_LEVEL &&
  1919. !is_large_pte(*sptep)) {
  1920. struct kvm_mmu_page *child;
  1921. u64 pte = *sptep;
  1922. child = page_header(pte & PT64_BASE_ADDR_MASK);
  1923. drop_parent_pte(child, sptep);
  1924. kvm_flush_remote_tlbs(vcpu->kvm);
  1925. } else if (pfn != spte_to_pfn(*sptep)) {
  1926. pgprintk("hfn old %llx new %llx\n",
  1927. spte_to_pfn(*sptep), pfn);
  1928. drop_spte(vcpu->kvm, sptep);
  1929. kvm_flush_remote_tlbs(vcpu->kvm);
  1930. } else
  1931. was_rmapped = 1;
  1932. }
  1933. if (set_spte(vcpu, sptep, pte_access, user_fault, write_fault,
  1934. level, gfn, pfn, speculative, true,
  1935. host_writable)) {
  1936. if (write_fault)
  1937. *emulate = 1;
  1938. kvm_mmu_flush_tlb(vcpu);
  1939. }
  1940. if (unlikely(is_mmio_spte(*sptep) && emulate))
  1941. *emulate = 1;
  1942. pgprintk("%s: setting spte %llx\n", __func__, *sptep);
  1943. pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
  1944. is_large_pte(*sptep)? "2MB" : "4kB",
  1945. *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
  1946. *sptep, sptep);
  1947. if (!was_rmapped && is_large_pte(*sptep))
  1948. ++vcpu->kvm->stat.lpages;
  1949. if (is_shadow_present_pte(*sptep)) {
  1950. page_header_update_slot(vcpu->kvm, sptep, gfn);
  1951. if (!was_rmapped) {
  1952. rmap_count = rmap_add(vcpu, sptep, gfn);
  1953. if (rmap_count > RMAP_RECYCLE_THRESHOLD)
  1954. rmap_recycle(vcpu, sptep, gfn);
  1955. }
  1956. }
  1957. kvm_release_pfn_clean(pfn);
  1958. }
  1959. static void nonpaging_new_cr3(struct kvm_vcpu *vcpu)
  1960. {
  1961. }
  1962. static pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
  1963. bool no_dirty_log)
  1964. {
  1965. struct kvm_memory_slot *slot;
  1966. unsigned long hva;
  1967. slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
  1968. if (!slot) {
  1969. get_page(fault_page);
  1970. return page_to_pfn(fault_page);
  1971. }
  1972. hva = gfn_to_hva_memslot(slot, gfn);
  1973. return hva_to_pfn_atomic(vcpu->kvm, hva);
  1974. }
  1975. static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
  1976. struct kvm_mmu_page *sp,
  1977. u64 *start, u64 *end)
  1978. {
  1979. struct page *pages[PTE_PREFETCH_NUM];
  1980. unsigned access = sp->role.access;
  1981. int i, ret;
  1982. gfn_t gfn;
  1983. gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
  1984. if (!gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK))
  1985. return -1;
  1986. ret = gfn_to_page_many_atomic(vcpu->kvm, gfn, pages, end - start);
  1987. if (ret <= 0)
  1988. return -1;
  1989. for (i = 0; i < ret; i++, gfn++, start++)
  1990. mmu_set_spte(vcpu, start, ACC_ALL,
  1991. access, 0, 0, NULL,
  1992. sp->role.level, gfn,
  1993. page_to_pfn(pages[i]), true, true);
  1994. return 0;
  1995. }
  1996. static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
  1997. struct kvm_mmu_page *sp, u64 *sptep)
  1998. {
  1999. u64 *spte, *start = NULL;
  2000. int i;
  2001. WARN_ON(!sp->role.direct);
  2002. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  2003. spte = sp->spt + i;
  2004. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  2005. if (is_shadow_present_pte(*spte) || spte == sptep) {
  2006. if (!start)
  2007. continue;
  2008. if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
  2009. break;
  2010. start = NULL;
  2011. } else if (!start)
  2012. start = spte;
  2013. }
  2014. }
  2015. static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
  2016. {
  2017. struct kvm_mmu_page *sp;
  2018. /*
  2019. * Since it's no accessed bit on EPT, it's no way to
  2020. * distinguish between actually accessed translations
  2021. * and prefetched, so disable pte prefetch if EPT is
  2022. * enabled.
  2023. */
  2024. if (!shadow_accessed_mask)
  2025. return;
  2026. sp = page_header(__pa(sptep));
  2027. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  2028. return;
  2029. __direct_pte_prefetch(vcpu, sp, sptep);
  2030. }
  2031. static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
  2032. int map_writable, int level, gfn_t gfn, pfn_t pfn,
  2033. bool prefault)
  2034. {
  2035. struct kvm_shadow_walk_iterator iterator;
  2036. struct kvm_mmu_page *sp;
  2037. int emulate = 0;
  2038. gfn_t pseudo_gfn;
  2039. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2040. return 0;
  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 = vcpu->arch.mmu.get_pdptr(vcpu, 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, MMIO_GVA_ANY);
  2369. 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. 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. 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. {
  2650. int maxphyaddr = cpuid_maxphyaddr(vcpu);
  2651. u64 exb_bit_rsvd = 0;
  2652. if (!context->nx)
  2653. exb_bit_rsvd = rsvd_bits(63, 63);
  2654. switch (context->root_level) {
  2655. case PT32_ROOT_LEVEL:
  2656. /* no rsvd bits for 2 level 4K page table entries */
  2657. context->rsvd_bits_mask[0][1] = 0;
  2658. context->rsvd_bits_mask[0][0] = 0;
  2659. context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
  2660. if (!is_pse(vcpu)) {
  2661. context->rsvd_bits_mask[1][1] = 0;
  2662. break;
  2663. }
  2664. if (is_cpuid_PSE36())
  2665. /* 36bits PSE 4MB page */
  2666. context->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
  2667. else
  2668. /* 32 bits PSE 4MB page */
  2669. context->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
  2670. break;
  2671. case PT32E_ROOT_LEVEL:
  2672. context->rsvd_bits_mask[0][2] =
  2673. rsvd_bits(maxphyaddr, 63) |
  2674. rsvd_bits(7, 8) | rsvd_bits(1, 2); /* PDPTE */
  2675. context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  2676. rsvd_bits(maxphyaddr, 62); /* PDE */
  2677. context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  2678. rsvd_bits(maxphyaddr, 62); /* PTE */
  2679. context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  2680. rsvd_bits(maxphyaddr, 62) |
  2681. rsvd_bits(13, 20); /* large page */
  2682. context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
  2683. break;
  2684. case PT64_ROOT_LEVEL:
  2685. context->rsvd_bits_mask[0][3] = exb_bit_rsvd |
  2686. rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
  2687. context->rsvd_bits_mask[0][2] = exb_bit_rsvd |
  2688. rsvd_bits(maxphyaddr, 51) | rsvd_bits(7, 8);
  2689. context->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  2690. rsvd_bits(maxphyaddr, 51);
  2691. context->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  2692. rsvd_bits(maxphyaddr, 51);
  2693. context->rsvd_bits_mask[1][3] = context->rsvd_bits_mask[0][3];
  2694. context->rsvd_bits_mask[1][2] = exb_bit_rsvd |
  2695. rsvd_bits(maxphyaddr, 51) |
  2696. rsvd_bits(13, 29);
  2697. context->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  2698. rsvd_bits(maxphyaddr, 51) |
  2699. rsvd_bits(13, 20); /* large page */
  2700. context->rsvd_bits_mask[1][0] = context->rsvd_bits_mask[0][0];
  2701. break;
  2702. }
  2703. }
  2704. static int paging64_init_context_common(struct kvm_vcpu *vcpu,
  2705. struct kvm_mmu *context,
  2706. int level)
  2707. {
  2708. context->nx = is_nx(vcpu);
  2709. context->root_level = level;
  2710. reset_rsvds_bits_mask(vcpu, context);
  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->shadow_root_level = level;
  2720. context->root_hpa = INVALID_PAGE;
  2721. context->direct_map = false;
  2722. return 0;
  2723. }
  2724. static int paging64_init_context(struct kvm_vcpu *vcpu,
  2725. struct kvm_mmu *context)
  2726. {
  2727. return paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
  2728. }
  2729. static int paging32_init_context(struct kvm_vcpu *vcpu,
  2730. struct kvm_mmu *context)
  2731. {
  2732. context->nx = false;
  2733. context->root_level = PT32_ROOT_LEVEL;
  2734. reset_rsvds_bits_mask(vcpu, context);
  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->shadow_root_level = PT32E_ROOT_LEVEL;
  2743. context->root_hpa = INVALID_PAGE;
  2744. context->direct_map = false;
  2745. return 0;
  2746. }
  2747. static int paging32E_init_context(struct kvm_vcpu *vcpu,
  2748. struct kvm_mmu *context)
  2749. {
  2750. return paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
  2751. }
  2752. static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
  2753. {
  2754. struct kvm_mmu *context = vcpu->arch.walk_mmu;
  2755. context->base_role.word = 0;
  2756. context->new_cr3 = nonpaging_new_cr3;
  2757. context->page_fault = tdp_page_fault;
  2758. context->free = nonpaging_free;
  2759. context->sync_page = nonpaging_sync_page;
  2760. context->invlpg = nonpaging_invlpg;
  2761. context->update_pte = nonpaging_update_pte;
  2762. context->shadow_root_level = kvm_x86_ops->get_tdp_level();
  2763. context->root_hpa = INVALID_PAGE;
  2764. context->direct_map = true;
  2765. context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
  2766. context->get_cr3 = get_cr3;
  2767. context->get_pdptr = kvm_pdptr_read;
  2768. context->inject_page_fault = kvm_inject_page_fault;
  2769. if (!is_paging(vcpu)) {
  2770. context->nx = false;
  2771. context->gva_to_gpa = nonpaging_gva_to_gpa;
  2772. context->root_level = 0;
  2773. } else if (is_long_mode(vcpu)) {
  2774. context->nx = is_nx(vcpu);
  2775. context->root_level = PT64_ROOT_LEVEL;
  2776. reset_rsvds_bits_mask(vcpu, context);
  2777. context->gva_to_gpa = paging64_gva_to_gpa;
  2778. } else if (is_pae(vcpu)) {
  2779. context->nx = is_nx(vcpu);
  2780. context->root_level = PT32E_ROOT_LEVEL;
  2781. reset_rsvds_bits_mask(vcpu, context);
  2782. context->gva_to_gpa = paging64_gva_to_gpa;
  2783. } else {
  2784. context->nx = false;
  2785. context->root_level = PT32_ROOT_LEVEL;
  2786. reset_rsvds_bits_mask(vcpu, context);
  2787. context->gva_to_gpa = paging32_gva_to_gpa;
  2788. }
  2789. return 0;
  2790. }
  2791. int kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
  2792. {
  2793. int r;
  2794. bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
  2795. ASSERT(vcpu);
  2796. ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2797. if (!is_paging(vcpu))
  2798. r = nonpaging_init_context(vcpu, context);
  2799. else if (is_long_mode(vcpu))
  2800. r = paging64_init_context(vcpu, context);
  2801. else if (is_pae(vcpu))
  2802. r = paging32E_init_context(vcpu, context);
  2803. else
  2804. r = paging32_init_context(vcpu, context);
  2805. vcpu->arch.mmu.base_role.cr4_pae = !!is_pae(vcpu);
  2806. vcpu->arch.mmu.base_role.cr0_wp = is_write_protection(vcpu);
  2807. vcpu->arch.mmu.base_role.smep_andnot_wp
  2808. = smep && !is_write_protection(vcpu);
  2809. return r;
  2810. }
  2811. EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
  2812. static int init_kvm_softmmu(struct kvm_vcpu *vcpu)
  2813. {
  2814. int r = kvm_init_shadow_mmu(vcpu, vcpu->arch.walk_mmu);
  2815. vcpu->arch.walk_mmu->set_cr3 = kvm_x86_ops->set_cr3;
  2816. vcpu->arch.walk_mmu->get_cr3 = get_cr3;
  2817. vcpu->arch.walk_mmu->get_pdptr = kvm_pdptr_read;
  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->get_pdptr = kvm_pdptr_read;
  2826. g_context->inject_page_fault = kvm_inject_page_fault;
  2827. /*
  2828. * Note that arch.mmu.gva_to_gpa translates l2_gva to l1_gpa. The
  2829. * translation of l2_gpa to l1_gpa addresses is done using the
  2830. * arch.nested_mmu.gva_to_gpa function. Basically the gva_to_gpa
  2831. * functions between mmu and nested_mmu are swapped.
  2832. */
  2833. if (!is_paging(vcpu)) {
  2834. g_context->nx = false;
  2835. g_context->root_level = 0;
  2836. g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
  2837. } else if (is_long_mode(vcpu)) {
  2838. g_context->nx = is_nx(vcpu);
  2839. g_context->root_level = PT64_ROOT_LEVEL;
  2840. reset_rsvds_bits_mask(vcpu, g_context);
  2841. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  2842. } else if (is_pae(vcpu)) {
  2843. g_context->nx = is_nx(vcpu);
  2844. g_context->root_level = PT32E_ROOT_LEVEL;
  2845. reset_rsvds_bits_mask(vcpu, g_context);
  2846. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  2847. } else {
  2848. g_context->nx = false;
  2849. g_context->root_level = PT32_ROOT_LEVEL;
  2850. reset_rsvds_bits_mask(vcpu, g_context);
  2851. g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
  2852. }
  2853. return 0;
  2854. }
  2855. static int init_kvm_mmu(struct kvm_vcpu *vcpu)
  2856. {
  2857. if (mmu_is_nested(vcpu))
  2858. return init_kvm_nested_mmu(vcpu);
  2859. else if (tdp_enabled)
  2860. return init_kvm_tdp_mmu(vcpu);
  2861. else
  2862. return init_kvm_softmmu(vcpu);
  2863. }
  2864. static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
  2865. {
  2866. ASSERT(vcpu);
  2867. if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2868. /* mmu.free() should set root_hpa = INVALID_PAGE */
  2869. vcpu->arch.mmu.free(vcpu);
  2870. }
  2871. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
  2872. {
  2873. destroy_kvm_mmu(vcpu);
  2874. return init_kvm_mmu(vcpu);
  2875. }
  2876. EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
  2877. int kvm_mmu_load(struct kvm_vcpu *vcpu)
  2878. {
  2879. int r;
  2880. r = mmu_topup_memory_caches(vcpu);
  2881. if (r)
  2882. goto out;
  2883. r = mmu_alloc_roots(vcpu);
  2884. spin_lock(&vcpu->kvm->mmu_lock);
  2885. mmu_sync_roots(vcpu);
  2886. spin_unlock(&vcpu->kvm->mmu_lock);
  2887. if (r)
  2888. goto out;
  2889. /* set_cr3() should ensure TLB has been flushed */
  2890. vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
  2891. out:
  2892. return r;
  2893. }
  2894. EXPORT_SYMBOL_GPL(kvm_mmu_load);
  2895. void kvm_mmu_unload(struct kvm_vcpu *vcpu)
  2896. {
  2897. mmu_free_roots(vcpu);
  2898. }
  2899. EXPORT_SYMBOL_GPL(kvm_mmu_unload);
  2900. static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
  2901. struct kvm_mmu_page *sp, u64 *spte,
  2902. const void *new)
  2903. {
  2904. if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
  2905. ++vcpu->kvm->stat.mmu_pde_zapped;
  2906. return;
  2907. }
  2908. ++vcpu->kvm->stat.mmu_pte_updated;
  2909. vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
  2910. }
  2911. static bool need_remote_flush(u64 old, u64 new)
  2912. {
  2913. if (!is_shadow_present_pte(old))
  2914. return false;
  2915. if (!is_shadow_present_pte(new))
  2916. return true;
  2917. if ((old ^ new) & PT64_BASE_ADDR_MASK)
  2918. return true;
  2919. old ^= PT64_NX_MASK;
  2920. new ^= PT64_NX_MASK;
  2921. return (old & ~new & PT64_PERM_MASK) != 0;
  2922. }
  2923. static void mmu_pte_write_flush_tlb(struct kvm_vcpu *vcpu, bool zap_page,
  2924. bool remote_flush, bool local_flush)
  2925. {
  2926. if (zap_page)
  2927. return;
  2928. if (remote_flush)
  2929. kvm_flush_remote_tlbs(vcpu->kvm);
  2930. else if (local_flush)
  2931. kvm_mmu_flush_tlb(vcpu);
  2932. }
  2933. static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
  2934. const u8 *new, int *bytes)
  2935. {
  2936. u64 gentry;
  2937. int r;
  2938. /*
  2939. * Assume that the pte write on a page table of the same type
  2940. * as the current vcpu paging mode since we update the sptes only
  2941. * when they have the same mode.
  2942. */
  2943. if (is_pae(vcpu) && *bytes == 4) {
  2944. /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
  2945. *gpa &= ~(gpa_t)7;
  2946. *bytes = 8;
  2947. r = kvm_read_guest(vcpu->kvm, *gpa, &gentry, min(*bytes, 8));
  2948. if (r)
  2949. gentry = 0;
  2950. new = (const u8 *)&gentry;
  2951. }
  2952. switch (*bytes) {
  2953. case 4:
  2954. gentry = *(const u32 *)new;
  2955. break;
  2956. case 8:
  2957. gentry = *(const u64 *)new;
  2958. break;
  2959. default:
  2960. gentry = 0;
  2961. break;
  2962. }
  2963. return gentry;
  2964. }
  2965. /*
  2966. * If we're seeing too many writes to a page, it may no longer be a page table,
  2967. * or we may be forking, in which case it is better to unmap the page.
  2968. */
  2969. static bool detect_write_flooding(struct kvm_mmu_page *sp)
  2970. {
  2971. /*
  2972. * Skip write-flooding detected for the sp whose level is 1, because
  2973. * it can become unsync, then the guest page is not write-protected.
  2974. */
  2975. if (sp->role.level == 1)
  2976. return false;
  2977. return ++sp->write_flooding_count >= 3;
  2978. }
  2979. /*
  2980. * Misaligned accesses are too much trouble to fix up; also, they usually
  2981. * indicate a page is not used as a page table.
  2982. */
  2983. static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
  2984. int bytes)
  2985. {
  2986. unsigned offset, pte_size, misaligned;
  2987. pgprintk("misaligned: gpa %llx bytes %d role %x\n",
  2988. gpa, bytes, sp->role.word);
  2989. offset = offset_in_page(gpa);
  2990. pte_size = sp->role.cr4_pae ? 8 : 4;
  2991. /*
  2992. * Sometimes, the OS only writes the last one bytes to update status
  2993. * bits, for example, in linux, andb instruction is used in clear_bit().
  2994. */
  2995. if (!(offset & (pte_size - 1)) && bytes == 1)
  2996. return false;
  2997. misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
  2998. misaligned |= bytes < 4;
  2999. return misaligned;
  3000. }
  3001. static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
  3002. {
  3003. unsigned page_offset, quadrant;
  3004. u64 *spte;
  3005. int level;
  3006. page_offset = offset_in_page(gpa);
  3007. level = sp->role.level;
  3008. *nspte = 1;
  3009. if (!sp->role.cr4_pae) {
  3010. page_offset <<= 1; /* 32->64 */
  3011. /*
  3012. * A 32-bit pde maps 4MB while the shadow pdes map
  3013. * only 2MB. So we need to double the offset again
  3014. * and zap two pdes instead of one.
  3015. */
  3016. if (level == PT32_ROOT_LEVEL) {
  3017. page_offset &= ~7; /* kill rounding error */
  3018. page_offset <<= 1;
  3019. *nspte = 2;
  3020. }
  3021. quadrant = page_offset >> PAGE_SHIFT;
  3022. page_offset &= ~PAGE_MASK;
  3023. if (quadrant != sp->role.quadrant)
  3024. return NULL;
  3025. }
  3026. spte = &sp->spt[page_offset / sizeof(*spte)];
  3027. return spte;
  3028. }
  3029. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  3030. const u8 *new, int bytes)
  3031. {
  3032. gfn_t gfn = gpa >> PAGE_SHIFT;
  3033. union kvm_mmu_page_role mask = { .word = 0 };
  3034. struct kvm_mmu_page *sp;
  3035. struct hlist_node *node;
  3036. LIST_HEAD(invalid_list);
  3037. u64 entry, gentry, *spte;
  3038. int npte;
  3039. bool remote_flush, local_flush, zap_page;
  3040. /*
  3041. * If we don't have indirect shadow pages, it means no page is
  3042. * write-protected, so we can exit simply.
  3043. */
  3044. if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
  3045. return;
  3046. zap_page = remote_flush = local_flush = false;
  3047. pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
  3048. gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes);
  3049. /*
  3050. * No need to care whether allocation memory is successful
  3051. * or not since pte prefetch is skiped if it does not have
  3052. * enough objects in the cache.
  3053. */
  3054. mmu_topup_memory_caches(vcpu);
  3055. spin_lock(&vcpu->kvm->mmu_lock);
  3056. ++vcpu->kvm->stat.mmu_pte_write;
  3057. kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
  3058. mask.cr0_wp = mask.cr4_pae = mask.nxe = mask.smep_andnot_wp = 1;
  3059. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn, node) {
  3060. if (detect_write_misaligned(sp, gpa, bytes) ||
  3061. detect_write_flooding(sp)) {
  3062. zap_page |= !!kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
  3063. &invalid_list);
  3064. ++vcpu->kvm->stat.mmu_flooded;
  3065. continue;
  3066. }
  3067. spte = get_written_sptes(sp, gpa, &npte);
  3068. if (!spte)
  3069. continue;
  3070. local_flush = true;
  3071. while (npte--) {
  3072. entry = *spte;
  3073. mmu_page_zap_pte(vcpu->kvm, sp, spte);
  3074. if (gentry &&
  3075. !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
  3076. & mask.word) && rmap_can_add(vcpu))
  3077. mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
  3078. if (!remote_flush && need_remote_flush(entry, *spte))
  3079. remote_flush = true;
  3080. ++spte;
  3081. }
  3082. }
  3083. mmu_pte_write_flush_tlb(vcpu, zap_page, remote_flush, local_flush);
  3084. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  3085. kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
  3086. spin_unlock(&vcpu->kvm->mmu_lock);
  3087. }
  3088. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
  3089. {
  3090. gpa_t gpa;
  3091. int r;
  3092. if (vcpu->arch.mmu.direct_map)
  3093. return 0;
  3094. gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
  3095. r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3096. return r;
  3097. }
  3098. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
  3099. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
  3100. {
  3101. LIST_HEAD(invalid_list);
  3102. while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES &&
  3103. !list_empty(&vcpu->kvm->arch.active_mmu_pages)) {
  3104. struct kvm_mmu_page *sp;
  3105. sp = container_of(vcpu->kvm->arch.active_mmu_pages.prev,
  3106. struct kvm_mmu_page, link);
  3107. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
  3108. ++vcpu->kvm->stat.mmu_recycled;
  3109. }
  3110. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  3111. }
  3112. static bool is_mmio_page_fault(struct kvm_vcpu *vcpu, gva_t addr)
  3113. {
  3114. if (vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu))
  3115. return vcpu_match_mmio_gpa(vcpu, addr);
  3116. return vcpu_match_mmio_gva(vcpu, addr);
  3117. }
  3118. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
  3119. void *insn, int insn_len)
  3120. {
  3121. int r, emulation_type = EMULTYPE_RETRY;
  3122. enum emulation_result er;
  3123. r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
  3124. if (r < 0)
  3125. goto out;
  3126. if (!r) {
  3127. r = 1;
  3128. goto out;
  3129. }
  3130. if (is_mmio_page_fault(vcpu, cr2))
  3131. emulation_type = 0;
  3132. er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
  3133. switch (er) {
  3134. case EMULATE_DONE:
  3135. return 1;
  3136. case EMULATE_DO_MMIO:
  3137. ++vcpu->stat.mmio_exits;
  3138. /* fall through */
  3139. case EMULATE_FAIL:
  3140. return 0;
  3141. default:
  3142. BUG();
  3143. }
  3144. out:
  3145. return r;
  3146. }
  3147. EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
  3148. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  3149. {
  3150. vcpu->arch.mmu.invlpg(vcpu, gva);
  3151. kvm_mmu_flush_tlb(vcpu);
  3152. ++vcpu->stat.invlpg;
  3153. }
  3154. EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
  3155. void kvm_enable_tdp(void)
  3156. {
  3157. tdp_enabled = true;
  3158. }
  3159. EXPORT_SYMBOL_GPL(kvm_enable_tdp);
  3160. void kvm_disable_tdp(void)
  3161. {
  3162. tdp_enabled = false;
  3163. }
  3164. EXPORT_SYMBOL_GPL(kvm_disable_tdp);
  3165. static void free_mmu_pages(struct kvm_vcpu *vcpu)
  3166. {
  3167. free_page((unsigned long)vcpu->arch.mmu.pae_root);
  3168. if (vcpu->arch.mmu.lm_root != NULL)
  3169. free_page((unsigned long)vcpu->arch.mmu.lm_root);
  3170. }
  3171. static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
  3172. {
  3173. struct page *page;
  3174. int i;
  3175. ASSERT(vcpu);
  3176. /*
  3177. * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
  3178. * Therefore we need to allocate shadow page tables in the first
  3179. * 4GB of memory, which happens to fit the DMA32 zone.
  3180. */
  3181. page = alloc_page(GFP_KERNEL | __GFP_DMA32);
  3182. if (!page)
  3183. return -ENOMEM;
  3184. vcpu->arch.mmu.pae_root = page_address(page);
  3185. for (i = 0; i < 4; ++i)
  3186. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  3187. return 0;
  3188. }
  3189. int kvm_mmu_create(struct kvm_vcpu *vcpu)
  3190. {
  3191. ASSERT(vcpu);
  3192. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  3193. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  3194. vcpu->arch.mmu.translate_gpa = translate_gpa;
  3195. vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
  3196. return alloc_mmu_pages(vcpu);
  3197. }
  3198. int kvm_mmu_setup(struct kvm_vcpu *vcpu)
  3199. {
  3200. ASSERT(vcpu);
  3201. ASSERT(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3202. return init_kvm_mmu(vcpu);
  3203. }
  3204. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot)
  3205. {
  3206. struct kvm_mmu_page *sp;
  3207. list_for_each_entry(sp, &kvm->arch.active_mmu_pages, link) {
  3208. int i;
  3209. u64 *pt;
  3210. if (!test_bit(slot, sp->slot_bitmap))
  3211. continue;
  3212. pt = sp->spt;
  3213. for (i = 0; i < PT64_ENT_PER_PAGE; ++i) {
  3214. if (!is_shadow_present_pte(pt[i]) ||
  3215. !is_last_spte(pt[i], sp->role.level))
  3216. continue;
  3217. if (is_large_pte(pt[i])) {
  3218. drop_spte(kvm, &pt[i]);
  3219. --kvm->stat.lpages;
  3220. continue;
  3221. }
  3222. /* avoid RMW */
  3223. if (is_writable_pte(pt[i]))
  3224. mmu_spte_update(&pt[i],
  3225. pt[i] & ~PT_WRITABLE_MASK);
  3226. }
  3227. }
  3228. kvm_flush_remote_tlbs(kvm);
  3229. }
  3230. void kvm_mmu_zap_all(struct kvm *kvm)
  3231. {
  3232. struct kvm_mmu_page *sp, *node;
  3233. LIST_HEAD(invalid_list);
  3234. spin_lock(&kvm->mmu_lock);
  3235. restart:
  3236. list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link)
  3237. if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list))
  3238. goto restart;
  3239. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  3240. spin_unlock(&kvm->mmu_lock);
  3241. }
  3242. static void kvm_mmu_remove_some_alloc_mmu_pages(struct kvm *kvm,
  3243. struct list_head *invalid_list)
  3244. {
  3245. struct kvm_mmu_page *page;
  3246. page = container_of(kvm->arch.active_mmu_pages.prev,
  3247. struct kvm_mmu_page, link);
  3248. kvm_mmu_prepare_zap_page(kvm, page, invalid_list);
  3249. }
  3250. static int mmu_shrink(struct shrinker *shrink, struct shrink_control *sc)
  3251. {
  3252. struct kvm *kvm;
  3253. struct kvm *kvm_freed = NULL;
  3254. int nr_to_scan = sc->nr_to_scan;
  3255. if (nr_to_scan == 0)
  3256. goto out;
  3257. raw_spin_lock(&kvm_lock);
  3258. list_for_each_entry(kvm, &vm_list, vm_list) {
  3259. int idx;
  3260. LIST_HEAD(invalid_list);
  3261. idx = srcu_read_lock(&kvm->srcu);
  3262. spin_lock(&kvm->mmu_lock);
  3263. if (!kvm_freed && nr_to_scan > 0 &&
  3264. kvm->arch.n_used_mmu_pages > 0) {
  3265. kvm_mmu_remove_some_alloc_mmu_pages(kvm,
  3266. &invalid_list);
  3267. kvm_freed = kvm;
  3268. }
  3269. nr_to_scan--;
  3270. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  3271. spin_unlock(&kvm->mmu_lock);
  3272. srcu_read_unlock(&kvm->srcu, idx);
  3273. }
  3274. if (kvm_freed)
  3275. list_move_tail(&kvm_freed->vm_list, &vm_list);
  3276. raw_spin_unlock(&kvm_lock);
  3277. out:
  3278. return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
  3279. }
  3280. static struct shrinker mmu_shrinker = {
  3281. .shrink = mmu_shrink,
  3282. .seeks = DEFAULT_SEEKS * 10,
  3283. };
  3284. static void mmu_destroy_caches(void)
  3285. {
  3286. if (pte_list_desc_cache)
  3287. kmem_cache_destroy(pte_list_desc_cache);
  3288. if (mmu_page_header_cache)
  3289. kmem_cache_destroy(mmu_page_header_cache);
  3290. }
  3291. int kvm_mmu_module_init(void)
  3292. {
  3293. pte_list_desc_cache = kmem_cache_create("pte_list_desc",
  3294. sizeof(struct pte_list_desc),
  3295. 0, 0, NULL);
  3296. if (!pte_list_desc_cache)
  3297. goto nomem;
  3298. mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
  3299. sizeof(struct kvm_mmu_page),
  3300. 0, 0, NULL);
  3301. if (!mmu_page_header_cache)
  3302. goto nomem;
  3303. if (percpu_counter_init(&kvm_total_used_mmu_pages, 0))
  3304. goto nomem;
  3305. register_shrinker(&mmu_shrinker);
  3306. return 0;
  3307. nomem:
  3308. mmu_destroy_caches();
  3309. return -ENOMEM;
  3310. }
  3311. /*
  3312. * Caculate mmu pages needed for kvm.
  3313. */
  3314. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
  3315. {
  3316. unsigned int nr_mmu_pages;
  3317. unsigned int nr_pages = 0;
  3318. struct kvm_memslots *slots;
  3319. struct kvm_memory_slot *memslot;
  3320. slots = kvm_memslots(kvm);
  3321. kvm_for_each_memslot(memslot, slots)
  3322. nr_pages += memslot->npages;
  3323. nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
  3324. nr_mmu_pages = max(nr_mmu_pages,
  3325. (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
  3326. return nr_mmu_pages;
  3327. }
  3328. int kvm_mmu_get_spte_hierarchy(struct kvm_vcpu *vcpu, u64 addr, u64 sptes[4])
  3329. {
  3330. struct kvm_shadow_walk_iterator iterator;
  3331. u64 spte;
  3332. int nr_sptes = 0;
  3333. walk_shadow_page_lockless_begin(vcpu);
  3334. for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
  3335. sptes[iterator.level-1] = spte;
  3336. nr_sptes++;
  3337. if (!is_shadow_present_pte(spte))
  3338. break;
  3339. }
  3340. walk_shadow_page_lockless_end(vcpu);
  3341. return nr_sptes;
  3342. }
  3343. EXPORT_SYMBOL_GPL(kvm_mmu_get_spte_hierarchy);
  3344. void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
  3345. {
  3346. ASSERT(vcpu);
  3347. destroy_kvm_mmu(vcpu);
  3348. free_mmu_pages(vcpu);
  3349. mmu_free_memory_caches(vcpu);
  3350. }
  3351. void kvm_mmu_module_exit(void)
  3352. {
  3353. mmu_destroy_caches();
  3354. percpu_counter_destroy(&kvm_total_used_mmu_pages);
  3355. unregister_shrinker(&mmu_shrinker);
  3356. mmu_audit_disable();
  3357. }