mmu.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111
  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 "cpuid.h"
  25. #include <linux/kvm_host.h>
  26. #include <linux/types.h>
  27. #include <linux/string.h>
  28. #include <linux/mm.h>
  29. #include <linux/highmem.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/export.h>
  32. #include <linux/swap.h>
  33. #include <linux/hugetlb.h>
  34. #include <linux/compiler.h>
  35. #include <linux/srcu.h>
  36. #include <linux/slab.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/page.h>
  39. #include <asm/cmpxchg.h>
  40. #include <asm/io.h>
  41. #include <asm/vmx.h>
  42. #include <asm/kvm_page_track.h>
  43. /*
  44. * When setting this variable to true it enables Two-Dimensional-Paging
  45. * where the hardware walks 2 page tables:
  46. * 1. the guest-virtual to guest-physical
  47. * 2. while doing 1. it walks guest-physical to host-physical
  48. * If the hardware supports that we don't need to do shadow paging.
  49. */
  50. bool tdp_enabled = false;
  51. enum {
  52. AUDIT_PRE_PAGE_FAULT,
  53. AUDIT_POST_PAGE_FAULT,
  54. AUDIT_PRE_PTE_WRITE,
  55. AUDIT_POST_PTE_WRITE,
  56. AUDIT_PRE_SYNC,
  57. AUDIT_POST_SYNC
  58. };
  59. #undef MMU_DEBUG
  60. #ifdef MMU_DEBUG
  61. static bool dbg = 0;
  62. module_param(dbg, bool, 0644);
  63. #define pgprintk(x...) do { if (dbg) printk(x); } while (0)
  64. #define rmap_printk(x...) do { if (dbg) printk(x); } while (0)
  65. #define MMU_WARN_ON(x) WARN_ON(x)
  66. #else
  67. #define pgprintk(x...) do { } while (0)
  68. #define rmap_printk(x...) do { } while (0)
  69. #define MMU_WARN_ON(x) do { } while (0)
  70. #endif
  71. #define PTE_PREFETCH_NUM 8
  72. #define PT_FIRST_AVAIL_BITS_SHIFT 10
  73. #define PT64_SECOND_AVAIL_BITS_SHIFT 52
  74. #define PT64_LEVEL_BITS 9
  75. #define PT64_LEVEL_SHIFT(level) \
  76. (PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS)
  77. #define PT64_INDEX(address, level)\
  78. (((address) >> PT64_LEVEL_SHIFT(level)) & ((1 << PT64_LEVEL_BITS) - 1))
  79. #define PT32_LEVEL_BITS 10
  80. #define PT32_LEVEL_SHIFT(level) \
  81. (PAGE_SHIFT + (level - 1) * PT32_LEVEL_BITS)
  82. #define PT32_LVL_OFFSET_MASK(level) \
  83. (PT32_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  84. * PT32_LEVEL_BITS))) - 1))
  85. #define PT32_INDEX(address, level)\
  86. (((address) >> PT32_LEVEL_SHIFT(level)) & ((1 << PT32_LEVEL_BITS) - 1))
  87. #define PT64_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))
  88. #define PT64_DIR_BASE_ADDR_MASK \
  89. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + PT64_LEVEL_BITS)) - 1))
  90. #define PT64_LVL_ADDR_MASK(level) \
  91. (PT64_BASE_ADDR_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  92. * PT64_LEVEL_BITS))) - 1))
  93. #define PT64_LVL_OFFSET_MASK(level) \
  94. (PT64_BASE_ADDR_MASK & ((1ULL << (PAGE_SHIFT + (((level) - 1) \
  95. * PT64_LEVEL_BITS))) - 1))
  96. #define PT32_BASE_ADDR_MASK PAGE_MASK
  97. #define PT32_DIR_BASE_ADDR_MASK \
  98. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + PT32_LEVEL_BITS)) - 1))
  99. #define PT32_LVL_ADDR_MASK(level) \
  100. (PAGE_MASK & ~((1ULL << (PAGE_SHIFT + (((level) - 1) \
  101. * PT32_LEVEL_BITS))) - 1))
  102. #define PT64_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | shadow_user_mask \
  103. | shadow_x_mask | shadow_nx_mask)
  104. #define ACC_EXEC_MASK 1
  105. #define ACC_WRITE_MASK PT_WRITABLE_MASK
  106. #define ACC_USER_MASK PT_USER_MASK
  107. #define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK)
  108. #include <trace/events/kvm.h>
  109. #define CREATE_TRACE_POINTS
  110. #include "mmutrace.h"
  111. #define SPTE_HOST_WRITEABLE (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)
  112. #define SPTE_MMU_WRITEABLE (1ULL << (PT_FIRST_AVAIL_BITS_SHIFT + 1))
  113. #define SHADOW_PT_INDEX(addr, level) PT64_INDEX(addr, level)
  114. /* make pte_list_desc fit well in cache line */
  115. #define PTE_LIST_EXT 3
  116. struct pte_list_desc {
  117. u64 *sptes[PTE_LIST_EXT];
  118. struct pte_list_desc *more;
  119. };
  120. struct kvm_shadow_walk_iterator {
  121. u64 addr;
  122. hpa_t shadow_addr;
  123. u64 *sptep;
  124. int level;
  125. unsigned index;
  126. };
  127. #define for_each_shadow_entry(_vcpu, _addr, _walker) \
  128. for (shadow_walk_init(&(_walker), _vcpu, _addr); \
  129. shadow_walk_okay(&(_walker)); \
  130. shadow_walk_next(&(_walker)))
  131. #define for_each_shadow_entry_lockless(_vcpu, _addr, _walker, spte) \
  132. for (shadow_walk_init(&(_walker), _vcpu, _addr); \
  133. shadow_walk_okay(&(_walker)) && \
  134. ({ spte = mmu_spte_get_lockless(_walker.sptep); 1; }); \
  135. __shadow_walk_next(&(_walker), spte))
  136. static struct kmem_cache *pte_list_desc_cache;
  137. static struct kmem_cache *mmu_page_header_cache;
  138. static struct percpu_counter kvm_total_used_mmu_pages;
  139. static u64 __read_mostly shadow_nx_mask;
  140. static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
  141. static u64 __read_mostly shadow_user_mask;
  142. static u64 __read_mostly shadow_accessed_mask;
  143. static u64 __read_mostly shadow_dirty_mask;
  144. static u64 __read_mostly shadow_mmio_mask;
  145. static u64 __read_mostly shadow_present_mask;
  146. static void mmu_spte_set(u64 *sptep, u64 spte);
  147. static void mmu_free_roots(struct kvm_vcpu *vcpu);
  148. void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask)
  149. {
  150. shadow_mmio_mask = mmio_mask;
  151. }
  152. EXPORT_SYMBOL_GPL(kvm_mmu_set_mmio_spte_mask);
  153. /*
  154. * the low bit of the generation number is always presumed to be zero.
  155. * This disables mmio caching during memslot updates. The concept is
  156. * similar to a seqcount but instead of retrying the access we just punt
  157. * and ignore the cache.
  158. *
  159. * spte bits 3-11 are used as bits 1-9 of the generation number,
  160. * the bits 52-61 are used as bits 10-19 of the generation number.
  161. */
  162. #define MMIO_SPTE_GEN_LOW_SHIFT 2
  163. #define MMIO_SPTE_GEN_HIGH_SHIFT 52
  164. #define MMIO_GEN_SHIFT 20
  165. #define MMIO_GEN_LOW_SHIFT 10
  166. #define MMIO_GEN_LOW_MASK ((1 << MMIO_GEN_LOW_SHIFT) - 2)
  167. #define MMIO_GEN_MASK ((1 << MMIO_GEN_SHIFT) - 1)
  168. static u64 generation_mmio_spte_mask(unsigned int gen)
  169. {
  170. u64 mask;
  171. WARN_ON(gen & ~MMIO_GEN_MASK);
  172. mask = (gen & MMIO_GEN_LOW_MASK) << MMIO_SPTE_GEN_LOW_SHIFT;
  173. mask |= ((u64)gen >> MMIO_GEN_LOW_SHIFT) << MMIO_SPTE_GEN_HIGH_SHIFT;
  174. return mask;
  175. }
  176. static unsigned int get_mmio_spte_generation(u64 spte)
  177. {
  178. unsigned int gen;
  179. spte &= ~shadow_mmio_mask;
  180. gen = (spte >> MMIO_SPTE_GEN_LOW_SHIFT) & MMIO_GEN_LOW_MASK;
  181. gen |= (spte >> MMIO_SPTE_GEN_HIGH_SHIFT) << MMIO_GEN_LOW_SHIFT;
  182. return gen;
  183. }
  184. static unsigned int kvm_current_mmio_generation(struct kvm_vcpu *vcpu)
  185. {
  186. return kvm_vcpu_memslots(vcpu)->generation & MMIO_GEN_MASK;
  187. }
  188. static void mark_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, u64 gfn,
  189. unsigned access)
  190. {
  191. unsigned int gen = kvm_current_mmio_generation(vcpu);
  192. u64 mask = generation_mmio_spte_mask(gen);
  193. access &= ACC_WRITE_MASK | ACC_USER_MASK;
  194. mask |= shadow_mmio_mask | access | gfn << PAGE_SHIFT;
  195. trace_mark_mmio_spte(sptep, gfn, access, gen);
  196. mmu_spte_set(sptep, mask);
  197. }
  198. static bool is_mmio_spte(u64 spte)
  199. {
  200. return (spte & shadow_mmio_mask) == shadow_mmio_mask;
  201. }
  202. static gfn_t get_mmio_spte_gfn(u64 spte)
  203. {
  204. u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask;
  205. return (spte & ~mask) >> PAGE_SHIFT;
  206. }
  207. static unsigned get_mmio_spte_access(u64 spte)
  208. {
  209. u64 mask = generation_mmio_spte_mask(MMIO_GEN_MASK) | shadow_mmio_mask;
  210. return (spte & ~mask) & ~PAGE_MASK;
  211. }
  212. static bool set_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
  213. kvm_pfn_t pfn, unsigned access)
  214. {
  215. if (unlikely(is_noslot_pfn(pfn))) {
  216. mark_mmio_spte(vcpu, sptep, gfn, access);
  217. return true;
  218. }
  219. return false;
  220. }
  221. static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)
  222. {
  223. unsigned int kvm_gen, spte_gen;
  224. kvm_gen = kvm_current_mmio_generation(vcpu);
  225. spte_gen = get_mmio_spte_generation(spte);
  226. trace_check_mmio_spte(spte, kvm_gen, spte_gen);
  227. return likely(kvm_gen == spte_gen);
  228. }
  229. void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
  230. u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 p_mask)
  231. {
  232. shadow_user_mask = user_mask;
  233. shadow_accessed_mask = accessed_mask;
  234. shadow_dirty_mask = dirty_mask;
  235. shadow_nx_mask = nx_mask;
  236. shadow_x_mask = x_mask;
  237. shadow_present_mask = p_mask;
  238. }
  239. EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
  240. static int is_cpuid_PSE36(void)
  241. {
  242. return 1;
  243. }
  244. static int is_nx(struct kvm_vcpu *vcpu)
  245. {
  246. return vcpu->arch.efer & EFER_NX;
  247. }
  248. static int is_shadow_present_pte(u64 pte)
  249. {
  250. return (pte & 0xFFFFFFFFull) && !is_mmio_spte(pte);
  251. }
  252. static int is_large_pte(u64 pte)
  253. {
  254. return pte & PT_PAGE_SIZE_MASK;
  255. }
  256. static int is_last_spte(u64 pte, int level)
  257. {
  258. if (level == PT_PAGE_TABLE_LEVEL)
  259. return 1;
  260. if (is_large_pte(pte))
  261. return 1;
  262. return 0;
  263. }
  264. static kvm_pfn_t spte_to_pfn(u64 pte)
  265. {
  266. return (pte & PT64_BASE_ADDR_MASK) >> PAGE_SHIFT;
  267. }
  268. static gfn_t pse36_gfn_delta(u32 gpte)
  269. {
  270. int shift = 32 - PT32_DIR_PSE36_SHIFT - PAGE_SHIFT;
  271. return (gpte & PT32_DIR_PSE36_MASK) << shift;
  272. }
  273. #ifdef CONFIG_X86_64
  274. static void __set_spte(u64 *sptep, u64 spte)
  275. {
  276. WRITE_ONCE(*sptep, spte);
  277. }
  278. static void __update_clear_spte_fast(u64 *sptep, u64 spte)
  279. {
  280. WRITE_ONCE(*sptep, spte);
  281. }
  282. static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
  283. {
  284. return xchg(sptep, spte);
  285. }
  286. static u64 __get_spte_lockless(u64 *sptep)
  287. {
  288. return ACCESS_ONCE(*sptep);
  289. }
  290. #else
  291. union split_spte {
  292. struct {
  293. u32 spte_low;
  294. u32 spte_high;
  295. };
  296. u64 spte;
  297. };
  298. static void count_spte_clear(u64 *sptep, u64 spte)
  299. {
  300. struct kvm_mmu_page *sp = page_header(__pa(sptep));
  301. if (is_shadow_present_pte(spte))
  302. return;
  303. /* Ensure the spte is completely set before we increase the count */
  304. smp_wmb();
  305. sp->clear_spte_count++;
  306. }
  307. static void __set_spte(u64 *sptep, u64 spte)
  308. {
  309. union split_spte *ssptep, sspte;
  310. ssptep = (union split_spte *)sptep;
  311. sspte = (union split_spte)spte;
  312. ssptep->spte_high = sspte.spte_high;
  313. /*
  314. * If we map the spte from nonpresent to present, We should store
  315. * the high bits firstly, then set present bit, so cpu can not
  316. * fetch this spte while we are setting the spte.
  317. */
  318. smp_wmb();
  319. WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
  320. }
  321. static void __update_clear_spte_fast(u64 *sptep, u64 spte)
  322. {
  323. union split_spte *ssptep, sspte;
  324. ssptep = (union split_spte *)sptep;
  325. sspte = (union split_spte)spte;
  326. WRITE_ONCE(ssptep->spte_low, sspte.spte_low);
  327. /*
  328. * If we map the spte from present to nonpresent, we should clear
  329. * present bit firstly to avoid vcpu fetch the old high bits.
  330. */
  331. smp_wmb();
  332. ssptep->spte_high = sspte.spte_high;
  333. count_spte_clear(sptep, spte);
  334. }
  335. static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
  336. {
  337. union split_spte *ssptep, sspte, orig;
  338. ssptep = (union split_spte *)sptep;
  339. sspte = (union split_spte)spte;
  340. /* xchg acts as a barrier before the setting of the high bits */
  341. orig.spte_low = xchg(&ssptep->spte_low, sspte.spte_low);
  342. orig.spte_high = ssptep->spte_high;
  343. ssptep->spte_high = sspte.spte_high;
  344. count_spte_clear(sptep, spte);
  345. return orig.spte;
  346. }
  347. /*
  348. * The idea using the light way get the spte on x86_32 guest is from
  349. * gup_get_pte(arch/x86/mm/gup.c).
  350. *
  351. * An spte tlb flush may be pending, because kvm_set_pte_rmapp
  352. * coalesces them and we are running out of the MMU lock. Therefore
  353. * we need to protect against in-progress updates of the spte.
  354. *
  355. * Reading the spte while an update is in progress may get the old value
  356. * for the high part of the spte. The race is fine for a present->non-present
  357. * change (because the high part of the spte is ignored for non-present spte),
  358. * but for a present->present change we must reread the spte.
  359. *
  360. * All such changes are done in two steps (present->non-present and
  361. * non-present->present), hence it is enough to count the number of
  362. * present->non-present updates: if it changed while reading the spte,
  363. * we might have hit the race. This is done using clear_spte_count.
  364. */
  365. static u64 __get_spte_lockless(u64 *sptep)
  366. {
  367. struct kvm_mmu_page *sp = page_header(__pa(sptep));
  368. union split_spte spte, *orig = (union split_spte *)sptep;
  369. int count;
  370. retry:
  371. count = sp->clear_spte_count;
  372. smp_rmb();
  373. spte.spte_low = orig->spte_low;
  374. smp_rmb();
  375. spte.spte_high = orig->spte_high;
  376. smp_rmb();
  377. if (unlikely(spte.spte_low != orig->spte_low ||
  378. count != sp->clear_spte_count))
  379. goto retry;
  380. return spte.spte;
  381. }
  382. #endif
  383. static bool spte_is_locklessly_modifiable(u64 spte)
  384. {
  385. return (spte & (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE)) ==
  386. (SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE);
  387. }
  388. static bool spte_has_volatile_bits(u64 spte)
  389. {
  390. /*
  391. * Always atomically update spte if it can be updated
  392. * out of mmu-lock, it can ensure dirty bit is not lost,
  393. * also, it can help us to get a stable is_writable_pte()
  394. * to ensure tlb flush is not missed.
  395. */
  396. if (spte_is_locklessly_modifiable(spte))
  397. return true;
  398. if (!shadow_accessed_mask)
  399. return false;
  400. if (!is_shadow_present_pte(spte))
  401. return false;
  402. if ((spte & shadow_accessed_mask) &&
  403. (!is_writable_pte(spte) || (spte & shadow_dirty_mask)))
  404. return false;
  405. return true;
  406. }
  407. static bool spte_is_bit_cleared(u64 old_spte, u64 new_spte, u64 bit_mask)
  408. {
  409. return (old_spte & bit_mask) && !(new_spte & bit_mask);
  410. }
  411. static bool spte_is_bit_changed(u64 old_spte, u64 new_spte, u64 bit_mask)
  412. {
  413. return (old_spte & bit_mask) != (new_spte & bit_mask);
  414. }
  415. /* Rules for using mmu_spte_set:
  416. * Set the sptep from nonpresent to present.
  417. * Note: the sptep being assigned *must* be either not present
  418. * or in a state where the hardware will not attempt to update
  419. * the spte.
  420. */
  421. static void mmu_spte_set(u64 *sptep, u64 new_spte)
  422. {
  423. WARN_ON(is_shadow_present_pte(*sptep));
  424. __set_spte(sptep, new_spte);
  425. }
  426. /* Rules for using mmu_spte_update:
  427. * Update the state bits, it means the mapped pfn is not changed.
  428. *
  429. * Whenever we overwrite a writable spte with a read-only one we
  430. * should flush remote TLBs. Otherwise rmap_write_protect
  431. * will find a read-only spte, even though the writable spte
  432. * might be cached on a CPU's TLB, the return value indicates this
  433. * case.
  434. */
  435. static bool mmu_spte_update(u64 *sptep, u64 new_spte)
  436. {
  437. u64 old_spte = *sptep;
  438. bool ret = false;
  439. WARN_ON(!is_shadow_present_pte(new_spte));
  440. if (!is_shadow_present_pte(old_spte)) {
  441. mmu_spte_set(sptep, new_spte);
  442. return ret;
  443. }
  444. if (!spte_has_volatile_bits(old_spte))
  445. __update_clear_spte_fast(sptep, new_spte);
  446. else
  447. old_spte = __update_clear_spte_slow(sptep, new_spte);
  448. /*
  449. * For the spte updated out of mmu-lock is safe, since
  450. * we always atomically update it, see the comments in
  451. * spte_has_volatile_bits().
  452. */
  453. if (spte_is_locklessly_modifiable(old_spte) &&
  454. !is_writable_pte(new_spte))
  455. ret = true;
  456. if (!shadow_accessed_mask) {
  457. /*
  458. * We don't set page dirty when dropping non-writable spte.
  459. * So do it now if the new spte is becoming non-writable.
  460. */
  461. if (ret)
  462. kvm_set_pfn_dirty(spte_to_pfn(old_spte));
  463. return ret;
  464. }
  465. /*
  466. * Flush TLB when accessed/dirty bits are changed in the page tables,
  467. * to guarantee consistency between TLB and page tables.
  468. */
  469. if (spte_is_bit_changed(old_spte, new_spte,
  470. shadow_accessed_mask | shadow_dirty_mask))
  471. ret = true;
  472. if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask))
  473. kvm_set_pfn_accessed(spte_to_pfn(old_spte));
  474. if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))
  475. kvm_set_pfn_dirty(spte_to_pfn(old_spte));
  476. return ret;
  477. }
  478. /*
  479. * Rules for using mmu_spte_clear_track_bits:
  480. * It sets the sptep from present to nonpresent, and track the
  481. * state bits, it is used to clear the last level sptep.
  482. */
  483. static int mmu_spte_clear_track_bits(u64 *sptep)
  484. {
  485. kvm_pfn_t pfn;
  486. u64 old_spte = *sptep;
  487. if (!spte_has_volatile_bits(old_spte))
  488. __update_clear_spte_fast(sptep, 0ull);
  489. else
  490. old_spte = __update_clear_spte_slow(sptep, 0ull);
  491. if (!is_shadow_present_pte(old_spte))
  492. return 0;
  493. pfn = spte_to_pfn(old_spte);
  494. /*
  495. * KVM does not hold the refcount of the page used by
  496. * kvm mmu, before reclaiming the page, we should
  497. * unmap it from mmu first.
  498. */
  499. WARN_ON(!kvm_is_reserved_pfn(pfn) && !page_count(pfn_to_page(pfn)));
  500. if (!shadow_accessed_mask || old_spte & shadow_accessed_mask)
  501. kvm_set_pfn_accessed(pfn);
  502. if (old_spte & (shadow_dirty_mask ? shadow_dirty_mask :
  503. PT_WRITABLE_MASK))
  504. kvm_set_pfn_dirty(pfn);
  505. return 1;
  506. }
  507. /*
  508. * Rules for using mmu_spte_clear_no_track:
  509. * Directly clear spte without caring the state bits of sptep,
  510. * it is used to set the upper level spte.
  511. */
  512. static void mmu_spte_clear_no_track(u64 *sptep)
  513. {
  514. __update_clear_spte_fast(sptep, 0ull);
  515. }
  516. static u64 mmu_spte_get_lockless(u64 *sptep)
  517. {
  518. return __get_spte_lockless(sptep);
  519. }
  520. static void walk_shadow_page_lockless_begin(struct kvm_vcpu *vcpu)
  521. {
  522. /*
  523. * Prevent page table teardown by making any free-er wait during
  524. * kvm_flush_remote_tlbs() IPI to all active vcpus.
  525. */
  526. local_irq_disable();
  527. /*
  528. * Make sure a following spte read is not reordered ahead of the write
  529. * to vcpu->mode.
  530. */
  531. smp_store_mb(vcpu->mode, READING_SHADOW_PAGE_TABLES);
  532. }
  533. static void walk_shadow_page_lockless_end(struct kvm_vcpu *vcpu)
  534. {
  535. /*
  536. * Make sure the write to vcpu->mode is not reordered in front of
  537. * reads to sptes. If it does, kvm_commit_zap_page() can see us
  538. * OUTSIDE_GUEST_MODE and proceed to free the shadow page table.
  539. */
  540. smp_store_release(&vcpu->mode, OUTSIDE_GUEST_MODE);
  541. local_irq_enable();
  542. }
  543. static int mmu_topup_memory_cache(struct kvm_mmu_memory_cache *cache,
  544. struct kmem_cache *base_cache, int min)
  545. {
  546. void *obj;
  547. if (cache->nobjs >= min)
  548. return 0;
  549. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  550. obj = kmem_cache_zalloc(base_cache, GFP_KERNEL);
  551. if (!obj)
  552. return -ENOMEM;
  553. cache->objects[cache->nobjs++] = obj;
  554. }
  555. return 0;
  556. }
  557. static int mmu_memory_cache_free_objects(struct kvm_mmu_memory_cache *cache)
  558. {
  559. return cache->nobjs;
  560. }
  561. static void mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc,
  562. struct kmem_cache *cache)
  563. {
  564. while (mc->nobjs)
  565. kmem_cache_free(cache, mc->objects[--mc->nobjs]);
  566. }
  567. static int mmu_topup_memory_cache_page(struct kvm_mmu_memory_cache *cache,
  568. int min)
  569. {
  570. void *page;
  571. if (cache->nobjs >= min)
  572. return 0;
  573. while (cache->nobjs < ARRAY_SIZE(cache->objects)) {
  574. page = (void *)__get_free_page(GFP_KERNEL);
  575. if (!page)
  576. return -ENOMEM;
  577. cache->objects[cache->nobjs++] = page;
  578. }
  579. return 0;
  580. }
  581. static void mmu_free_memory_cache_page(struct kvm_mmu_memory_cache *mc)
  582. {
  583. while (mc->nobjs)
  584. free_page((unsigned long)mc->objects[--mc->nobjs]);
  585. }
  586. static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu)
  587. {
  588. int r;
  589. r = mmu_topup_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
  590. pte_list_desc_cache, 8 + PTE_PREFETCH_NUM);
  591. if (r)
  592. goto out;
  593. r = mmu_topup_memory_cache_page(&vcpu->arch.mmu_page_cache, 8);
  594. if (r)
  595. goto out;
  596. r = mmu_topup_memory_cache(&vcpu->arch.mmu_page_header_cache,
  597. mmu_page_header_cache, 4);
  598. out:
  599. return r;
  600. }
  601. static void mmu_free_memory_caches(struct kvm_vcpu *vcpu)
  602. {
  603. mmu_free_memory_cache(&vcpu->arch.mmu_pte_list_desc_cache,
  604. pte_list_desc_cache);
  605. mmu_free_memory_cache_page(&vcpu->arch.mmu_page_cache);
  606. mmu_free_memory_cache(&vcpu->arch.mmu_page_header_cache,
  607. mmu_page_header_cache);
  608. }
  609. static void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc)
  610. {
  611. void *p;
  612. BUG_ON(!mc->nobjs);
  613. p = mc->objects[--mc->nobjs];
  614. return p;
  615. }
  616. static struct pte_list_desc *mmu_alloc_pte_list_desc(struct kvm_vcpu *vcpu)
  617. {
  618. return mmu_memory_cache_alloc(&vcpu->arch.mmu_pte_list_desc_cache);
  619. }
  620. static void mmu_free_pte_list_desc(struct pte_list_desc *pte_list_desc)
  621. {
  622. kmem_cache_free(pte_list_desc_cache, pte_list_desc);
  623. }
  624. static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
  625. {
  626. if (!sp->role.direct)
  627. return sp->gfns[index];
  628. return sp->gfn + (index << ((sp->role.level - 1) * PT64_LEVEL_BITS));
  629. }
  630. static void kvm_mmu_page_set_gfn(struct kvm_mmu_page *sp, int index, gfn_t gfn)
  631. {
  632. if (sp->role.direct)
  633. BUG_ON(gfn != kvm_mmu_page_get_gfn(sp, index));
  634. else
  635. sp->gfns[index] = gfn;
  636. }
  637. /*
  638. * Return the pointer to the large page information for a given gfn,
  639. * handling slots that are not large page aligned.
  640. */
  641. static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
  642. struct kvm_memory_slot *slot,
  643. int level)
  644. {
  645. unsigned long idx;
  646. idx = gfn_to_index(gfn, slot->base_gfn, level);
  647. return &slot->arch.lpage_info[level - 2][idx];
  648. }
  649. static void update_gfn_disallow_lpage_count(struct kvm_memory_slot *slot,
  650. gfn_t gfn, int count)
  651. {
  652. struct kvm_lpage_info *linfo;
  653. int i;
  654. for (i = PT_DIRECTORY_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
  655. linfo = lpage_info_slot(gfn, slot, i);
  656. linfo->disallow_lpage += count;
  657. WARN_ON(linfo->disallow_lpage < 0);
  658. }
  659. }
  660. void kvm_mmu_gfn_disallow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
  661. {
  662. update_gfn_disallow_lpage_count(slot, gfn, 1);
  663. }
  664. void kvm_mmu_gfn_allow_lpage(struct kvm_memory_slot *slot, gfn_t gfn)
  665. {
  666. update_gfn_disallow_lpage_count(slot, gfn, -1);
  667. }
  668. static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
  669. {
  670. struct kvm_memslots *slots;
  671. struct kvm_memory_slot *slot;
  672. gfn_t gfn;
  673. kvm->arch.indirect_shadow_pages++;
  674. gfn = sp->gfn;
  675. slots = kvm_memslots_for_spte_role(kvm, sp->role);
  676. slot = __gfn_to_memslot(slots, gfn);
  677. /* the non-leaf shadow pages are keeping readonly. */
  678. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  679. return kvm_slot_page_track_add_page(kvm, slot, gfn,
  680. KVM_PAGE_TRACK_WRITE);
  681. kvm_mmu_gfn_disallow_lpage(slot, gfn);
  682. }
  683. static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp)
  684. {
  685. struct kvm_memslots *slots;
  686. struct kvm_memory_slot *slot;
  687. gfn_t gfn;
  688. kvm->arch.indirect_shadow_pages--;
  689. gfn = sp->gfn;
  690. slots = kvm_memslots_for_spte_role(kvm, sp->role);
  691. slot = __gfn_to_memslot(slots, gfn);
  692. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  693. return kvm_slot_page_track_remove_page(kvm, slot, gfn,
  694. KVM_PAGE_TRACK_WRITE);
  695. kvm_mmu_gfn_allow_lpage(slot, gfn);
  696. }
  697. static bool __mmu_gfn_lpage_is_disallowed(gfn_t gfn, int level,
  698. struct kvm_memory_slot *slot)
  699. {
  700. struct kvm_lpage_info *linfo;
  701. if (slot) {
  702. linfo = lpage_info_slot(gfn, slot, level);
  703. return !!linfo->disallow_lpage;
  704. }
  705. return true;
  706. }
  707. static bool mmu_gfn_lpage_is_disallowed(struct kvm_vcpu *vcpu, gfn_t gfn,
  708. int level)
  709. {
  710. struct kvm_memory_slot *slot;
  711. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  712. return __mmu_gfn_lpage_is_disallowed(gfn, level, slot);
  713. }
  714. static int host_mapping_level(struct kvm *kvm, gfn_t gfn)
  715. {
  716. unsigned long page_size;
  717. int i, ret = 0;
  718. page_size = kvm_host_page_size(kvm, gfn);
  719. for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
  720. if (page_size >= KVM_HPAGE_SIZE(i))
  721. ret = i;
  722. else
  723. break;
  724. }
  725. return ret;
  726. }
  727. static inline bool memslot_valid_for_gpte(struct kvm_memory_slot *slot,
  728. bool no_dirty_log)
  729. {
  730. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  731. return false;
  732. if (no_dirty_log && slot->dirty_bitmap)
  733. return false;
  734. return true;
  735. }
  736. static struct kvm_memory_slot *
  737. gfn_to_memslot_dirty_bitmap(struct kvm_vcpu *vcpu, gfn_t gfn,
  738. bool no_dirty_log)
  739. {
  740. struct kvm_memory_slot *slot;
  741. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  742. if (!memslot_valid_for_gpte(slot, no_dirty_log))
  743. slot = NULL;
  744. return slot;
  745. }
  746. static int mapping_level(struct kvm_vcpu *vcpu, gfn_t large_gfn,
  747. bool *force_pt_level)
  748. {
  749. int host_level, level, max_level;
  750. struct kvm_memory_slot *slot;
  751. if (unlikely(*force_pt_level))
  752. return PT_PAGE_TABLE_LEVEL;
  753. slot = kvm_vcpu_gfn_to_memslot(vcpu, large_gfn);
  754. *force_pt_level = !memslot_valid_for_gpte(slot, true);
  755. if (unlikely(*force_pt_level))
  756. return PT_PAGE_TABLE_LEVEL;
  757. host_level = host_mapping_level(vcpu->kvm, large_gfn);
  758. if (host_level == PT_PAGE_TABLE_LEVEL)
  759. return host_level;
  760. max_level = min(kvm_x86_ops->get_lpage_level(), host_level);
  761. for (level = PT_DIRECTORY_LEVEL; level <= max_level; ++level)
  762. if (__mmu_gfn_lpage_is_disallowed(large_gfn, level, slot))
  763. break;
  764. return level - 1;
  765. }
  766. /*
  767. * About rmap_head encoding:
  768. *
  769. * If the bit zero of rmap_head->val is clear, then it points to the only spte
  770. * in this rmap chain. Otherwise, (rmap_head->val & ~1) points to a struct
  771. * pte_list_desc containing more mappings.
  772. */
  773. /*
  774. * Returns the number of pointers in the rmap chain, not counting the new one.
  775. */
  776. static int pte_list_add(struct kvm_vcpu *vcpu, u64 *spte,
  777. struct kvm_rmap_head *rmap_head)
  778. {
  779. struct pte_list_desc *desc;
  780. int i, count = 0;
  781. if (!rmap_head->val) {
  782. rmap_printk("pte_list_add: %p %llx 0->1\n", spte, *spte);
  783. rmap_head->val = (unsigned long)spte;
  784. } else if (!(rmap_head->val & 1)) {
  785. rmap_printk("pte_list_add: %p %llx 1->many\n", spte, *spte);
  786. desc = mmu_alloc_pte_list_desc(vcpu);
  787. desc->sptes[0] = (u64 *)rmap_head->val;
  788. desc->sptes[1] = spte;
  789. rmap_head->val = (unsigned long)desc | 1;
  790. ++count;
  791. } else {
  792. rmap_printk("pte_list_add: %p %llx many->many\n", spte, *spte);
  793. desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
  794. while (desc->sptes[PTE_LIST_EXT-1] && desc->more) {
  795. desc = desc->more;
  796. count += PTE_LIST_EXT;
  797. }
  798. if (desc->sptes[PTE_LIST_EXT-1]) {
  799. desc->more = mmu_alloc_pte_list_desc(vcpu);
  800. desc = desc->more;
  801. }
  802. for (i = 0; desc->sptes[i]; ++i)
  803. ++count;
  804. desc->sptes[i] = spte;
  805. }
  806. return count;
  807. }
  808. static void
  809. pte_list_desc_remove_entry(struct kvm_rmap_head *rmap_head,
  810. struct pte_list_desc *desc, int i,
  811. struct pte_list_desc *prev_desc)
  812. {
  813. int j;
  814. for (j = PTE_LIST_EXT - 1; !desc->sptes[j] && j > i; --j)
  815. ;
  816. desc->sptes[i] = desc->sptes[j];
  817. desc->sptes[j] = NULL;
  818. if (j != 0)
  819. return;
  820. if (!prev_desc && !desc->more)
  821. rmap_head->val = (unsigned long)desc->sptes[0];
  822. else
  823. if (prev_desc)
  824. prev_desc->more = desc->more;
  825. else
  826. rmap_head->val = (unsigned long)desc->more | 1;
  827. mmu_free_pte_list_desc(desc);
  828. }
  829. static void pte_list_remove(u64 *spte, struct kvm_rmap_head *rmap_head)
  830. {
  831. struct pte_list_desc *desc;
  832. struct pte_list_desc *prev_desc;
  833. int i;
  834. if (!rmap_head->val) {
  835. printk(KERN_ERR "pte_list_remove: %p 0->BUG\n", spte);
  836. BUG();
  837. } else if (!(rmap_head->val & 1)) {
  838. rmap_printk("pte_list_remove: %p 1->0\n", spte);
  839. if ((u64 *)rmap_head->val != spte) {
  840. printk(KERN_ERR "pte_list_remove: %p 1->BUG\n", spte);
  841. BUG();
  842. }
  843. rmap_head->val = 0;
  844. } else {
  845. rmap_printk("pte_list_remove: %p many->many\n", spte);
  846. desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
  847. prev_desc = NULL;
  848. while (desc) {
  849. for (i = 0; i < PTE_LIST_EXT && desc->sptes[i]; ++i) {
  850. if (desc->sptes[i] == spte) {
  851. pte_list_desc_remove_entry(rmap_head,
  852. desc, i, prev_desc);
  853. return;
  854. }
  855. }
  856. prev_desc = desc;
  857. desc = desc->more;
  858. }
  859. pr_err("pte_list_remove: %p many->many\n", spte);
  860. BUG();
  861. }
  862. }
  863. static struct kvm_rmap_head *__gfn_to_rmap(gfn_t gfn, int level,
  864. struct kvm_memory_slot *slot)
  865. {
  866. unsigned long idx;
  867. idx = gfn_to_index(gfn, slot->base_gfn, level);
  868. return &slot->arch.rmap[level - PT_PAGE_TABLE_LEVEL][idx];
  869. }
  870. static struct kvm_rmap_head *gfn_to_rmap(struct kvm *kvm, gfn_t gfn,
  871. struct kvm_mmu_page *sp)
  872. {
  873. struct kvm_memslots *slots;
  874. struct kvm_memory_slot *slot;
  875. slots = kvm_memslots_for_spte_role(kvm, sp->role);
  876. slot = __gfn_to_memslot(slots, gfn);
  877. return __gfn_to_rmap(gfn, sp->role.level, slot);
  878. }
  879. static bool rmap_can_add(struct kvm_vcpu *vcpu)
  880. {
  881. struct kvm_mmu_memory_cache *cache;
  882. cache = &vcpu->arch.mmu_pte_list_desc_cache;
  883. return mmu_memory_cache_free_objects(cache);
  884. }
  885. static int rmap_add(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  886. {
  887. struct kvm_mmu_page *sp;
  888. struct kvm_rmap_head *rmap_head;
  889. sp = page_header(__pa(spte));
  890. kvm_mmu_page_set_gfn(sp, spte - sp->spt, gfn);
  891. rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
  892. return pte_list_add(vcpu, spte, rmap_head);
  893. }
  894. static void rmap_remove(struct kvm *kvm, u64 *spte)
  895. {
  896. struct kvm_mmu_page *sp;
  897. gfn_t gfn;
  898. struct kvm_rmap_head *rmap_head;
  899. sp = page_header(__pa(spte));
  900. gfn = kvm_mmu_page_get_gfn(sp, spte - sp->spt);
  901. rmap_head = gfn_to_rmap(kvm, gfn, sp);
  902. pte_list_remove(spte, rmap_head);
  903. }
  904. /*
  905. * Used by the following functions to iterate through the sptes linked by a
  906. * rmap. All fields are private and not assumed to be used outside.
  907. */
  908. struct rmap_iterator {
  909. /* private fields */
  910. struct pte_list_desc *desc; /* holds the sptep if not NULL */
  911. int pos; /* index of the sptep */
  912. };
  913. /*
  914. * Iteration must be started by this function. This should also be used after
  915. * removing/dropping sptes from the rmap link because in such cases the
  916. * information in the itererator may not be valid.
  917. *
  918. * Returns sptep if found, NULL otherwise.
  919. */
  920. static u64 *rmap_get_first(struct kvm_rmap_head *rmap_head,
  921. struct rmap_iterator *iter)
  922. {
  923. u64 *sptep;
  924. if (!rmap_head->val)
  925. return NULL;
  926. if (!(rmap_head->val & 1)) {
  927. iter->desc = NULL;
  928. sptep = (u64 *)rmap_head->val;
  929. goto out;
  930. }
  931. iter->desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
  932. iter->pos = 0;
  933. sptep = iter->desc->sptes[iter->pos];
  934. out:
  935. BUG_ON(!is_shadow_present_pte(*sptep));
  936. return sptep;
  937. }
  938. /*
  939. * Must be used with a valid iterator: e.g. after rmap_get_first().
  940. *
  941. * Returns sptep if found, NULL otherwise.
  942. */
  943. static u64 *rmap_get_next(struct rmap_iterator *iter)
  944. {
  945. u64 *sptep;
  946. if (iter->desc) {
  947. if (iter->pos < PTE_LIST_EXT - 1) {
  948. ++iter->pos;
  949. sptep = iter->desc->sptes[iter->pos];
  950. if (sptep)
  951. goto out;
  952. }
  953. iter->desc = iter->desc->more;
  954. if (iter->desc) {
  955. iter->pos = 0;
  956. /* desc->sptes[0] cannot be NULL */
  957. sptep = iter->desc->sptes[iter->pos];
  958. goto out;
  959. }
  960. }
  961. return NULL;
  962. out:
  963. BUG_ON(!is_shadow_present_pte(*sptep));
  964. return sptep;
  965. }
  966. #define for_each_rmap_spte(_rmap_head_, _iter_, _spte_) \
  967. for (_spte_ = rmap_get_first(_rmap_head_, _iter_); \
  968. _spte_; _spte_ = rmap_get_next(_iter_))
  969. static void drop_spte(struct kvm *kvm, u64 *sptep)
  970. {
  971. if (mmu_spte_clear_track_bits(sptep))
  972. rmap_remove(kvm, sptep);
  973. }
  974. static bool __drop_large_spte(struct kvm *kvm, u64 *sptep)
  975. {
  976. if (is_large_pte(*sptep)) {
  977. WARN_ON(page_header(__pa(sptep))->role.level ==
  978. PT_PAGE_TABLE_LEVEL);
  979. drop_spte(kvm, sptep);
  980. --kvm->stat.lpages;
  981. return true;
  982. }
  983. return false;
  984. }
  985. static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
  986. {
  987. if (__drop_large_spte(vcpu->kvm, sptep))
  988. kvm_flush_remote_tlbs(vcpu->kvm);
  989. }
  990. /*
  991. * Write-protect on the specified @sptep, @pt_protect indicates whether
  992. * spte write-protection is caused by protecting shadow page table.
  993. *
  994. * Note: write protection is difference between dirty logging and spte
  995. * protection:
  996. * - for dirty logging, the spte can be set to writable at anytime if
  997. * its dirty bitmap is properly set.
  998. * - for spte protection, the spte can be writable only after unsync-ing
  999. * shadow page.
  1000. *
  1001. * Return true if tlb need be flushed.
  1002. */
  1003. static bool spte_write_protect(struct kvm *kvm, u64 *sptep, bool pt_protect)
  1004. {
  1005. u64 spte = *sptep;
  1006. if (!is_writable_pte(spte) &&
  1007. !(pt_protect && spte_is_locklessly_modifiable(spte)))
  1008. return false;
  1009. rmap_printk("rmap_write_protect: spte %p %llx\n", sptep, *sptep);
  1010. if (pt_protect)
  1011. spte &= ~SPTE_MMU_WRITEABLE;
  1012. spte = spte & ~PT_WRITABLE_MASK;
  1013. return mmu_spte_update(sptep, spte);
  1014. }
  1015. static bool __rmap_write_protect(struct kvm *kvm,
  1016. struct kvm_rmap_head *rmap_head,
  1017. bool pt_protect)
  1018. {
  1019. u64 *sptep;
  1020. struct rmap_iterator iter;
  1021. bool flush = false;
  1022. for_each_rmap_spte(rmap_head, &iter, sptep)
  1023. flush |= spte_write_protect(kvm, sptep, pt_protect);
  1024. return flush;
  1025. }
  1026. static bool spte_clear_dirty(struct kvm *kvm, u64 *sptep)
  1027. {
  1028. u64 spte = *sptep;
  1029. rmap_printk("rmap_clear_dirty: spte %p %llx\n", sptep, *sptep);
  1030. spte &= ~shadow_dirty_mask;
  1031. return mmu_spte_update(sptep, spte);
  1032. }
  1033. static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
  1034. {
  1035. u64 *sptep;
  1036. struct rmap_iterator iter;
  1037. bool flush = false;
  1038. for_each_rmap_spte(rmap_head, &iter, sptep)
  1039. flush |= spte_clear_dirty(kvm, sptep);
  1040. return flush;
  1041. }
  1042. static bool spte_set_dirty(struct kvm *kvm, u64 *sptep)
  1043. {
  1044. u64 spte = *sptep;
  1045. rmap_printk("rmap_set_dirty: spte %p %llx\n", sptep, *sptep);
  1046. spte |= shadow_dirty_mask;
  1047. return mmu_spte_update(sptep, spte);
  1048. }
  1049. static bool __rmap_set_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
  1050. {
  1051. u64 *sptep;
  1052. struct rmap_iterator iter;
  1053. bool flush = false;
  1054. for_each_rmap_spte(rmap_head, &iter, sptep)
  1055. flush |= spte_set_dirty(kvm, sptep);
  1056. return flush;
  1057. }
  1058. /**
  1059. * kvm_mmu_write_protect_pt_masked - write protect selected PT level pages
  1060. * @kvm: kvm instance
  1061. * @slot: slot to protect
  1062. * @gfn_offset: start of the BITS_PER_LONG pages we care about
  1063. * @mask: indicates which pages we should protect
  1064. *
  1065. * Used when we do not need to care about huge page mappings: e.g. during dirty
  1066. * logging we do not have any such mappings.
  1067. */
  1068. static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  1069. struct kvm_memory_slot *slot,
  1070. gfn_t gfn_offset, unsigned long mask)
  1071. {
  1072. struct kvm_rmap_head *rmap_head;
  1073. while (mask) {
  1074. rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
  1075. PT_PAGE_TABLE_LEVEL, slot);
  1076. __rmap_write_protect(kvm, rmap_head, false);
  1077. /* clear the first set bit */
  1078. mask &= mask - 1;
  1079. }
  1080. }
  1081. /**
  1082. * kvm_mmu_clear_dirty_pt_masked - clear MMU D-bit for PT level pages
  1083. * @kvm: kvm instance
  1084. * @slot: slot to clear D-bit
  1085. * @gfn_offset: start of the BITS_PER_LONG pages we care about
  1086. * @mask: indicates which pages we should clear D-bit
  1087. *
  1088. * Used for PML to re-log the dirty GPAs after userspace querying dirty_bitmap.
  1089. */
  1090. void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
  1091. struct kvm_memory_slot *slot,
  1092. gfn_t gfn_offset, unsigned long mask)
  1093. {
  1094. struct kvm_rmap_head *rmap_head;
  1095. while (mask) {
  1096. rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
  1097. PT_PAGE_TABLE_LEVEL, slot);
  1098. __rmap_clear_dirty(kvm, rmap_head);
  1099. /* clear the first set bit */
  1100. mask &= mask - 1;
  1101. }
  1102. }
  1103. EXPORT_SYMBOL_GPL(kvm_mmu_clear_dirty_pt_masked);
  1104. /**
  1105. * kvm_arch_mmu_enable_log_dirty_pt_masked - enable dirty logging for selected
  1106. * PT level pages.
  1107. *
  1108. * It calls kvm_mmu_write_protect_pt_masked to write protect selected pages to
  1109. * enable dirty logging for them.
  1110. *
  1111. * Used when we do not need to care about huge page mappings: e.g. during dirty
  1112. * logging we do not have any such mappings.
  1113. */
  1114. void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
  1115. struct kvm_memory_slot *slot,
  1116. gfn_t gfn_offset, unsigned long mask)
  1117. {
  1118. if (kvm_x86_ops->enable_log_dirty_pt_masked)
  1119. kvm_x86_ops->enable_log_dirty_pt_masked(kvm, slot, gfn_offset,
  1120. mask);
  1121. else
  1122. kvm_mmu_write_protect_pt_masked(kvm, slot, gfn_offset, mask);
  1123. }
  1124. bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
  1125. struct kvm_memory_slot *slot, u64 gfn)
  1126. {
  1127. struct kvm_rmap_head *rmap_head;
  1128. int i;
  1129. bool write_protected = false;
  1130. for (i = PT_PAGE_TABLE_LEVEL; i <= PT_MAX_HUGEPAGE_LEVEL; ++i) {
  1131. rmap_head = __gfn_to_rmap(gfn, i, slot);
  1132. write_protected |= __rmap_write_protect(kvm, rmap_head, true);
  1133. }
  1134. return write_protected;
  1135. }
  1136. static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
  1137. {
  1138. struct kvm_memory_slot *slot;
  1139. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1140. return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn);
  1141. }
  1142. static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head)
  1143. {
  1144. u64 *sptep;
  1145. struct rmap_iterator iter;
  1146. bool flush = false;
  1147. while ((sptep = rmap_get_first(rmap_head, &iter))) {
  1148. rmap_printk("%s: spte %p %llx.\n", __func__, sptep, *sptep);
  1149. drop_spte(kvm, sptep);
  1150. flush = true;
  1151. }
  1152. return flush;
  1153. }
  1154. static int kvm_unmap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1155. struct kvm_memory_slot *slot, gfn_t gfn, int level,
  1156. unsigned long data)
  1157. {
  1158. return kvm_zap_rmapp(kvm, rmap_head);
  1159. }
  1160. static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1161. struct kvm_memory_slot *slot, gfn_t gfn, int level,
  1162. unsigned long data)
  1163. {
  1164. u64 *sptep;
  1165. struct rmap_iterator iter;
  1166. int need_flush = 0;
  1167. u64 new_spte;
  1168. pte_t *ptep = (pte_t *)data;
  1169. kvm_pfn_t new_pfn;
  1170. WARN_ON(pte_huge(*ptep));
  1171. new_pfn = pte_pfn(*ptep);
  1172. restart:
  1173. for_each_rmap_spte(rmap_head, &iter, sptep) {
  1174. rmap_printk("kvm_set_pte_rmapp: spte %p %llx gfn %llx (%d)\n",
  1175. sptep, *sptep, gfn, level);
  1176. need_flush = 1;
  1177. if (pte_write(*ptep)) {
  1178. drop_spte(kvm, sptep);
  1179. goto restart;
  1180. } else {
  1181. new_spte = *sptep & ~PT64_BASE_ADDR_MASK;
  1182. new_spte |= (u64)new_pfn << PAGE_SHIFT;
  1183. new_spte &= ~PT_WRITABLE_MASK;
  1184. new_spte &= ~SPTE_HOST_WRITEABLE;
  1185. new_spte &= ~shadow_accessed_mask;
  1186. mmu_spte_clear_track_bits(sptep);
  1187. mmu_spte_set(sptep, new_spte);
  1188. }
  1189. }
  1190. if (need_flush)
  1191. kvm_flush_remote_tlbs(kvm);
  1192. return 0;
  1193. }
  1194. struct slot_rmap_walk_iterator {
  1195. /* input fields. */
  1196. struct kvm_memory_slot *slot;
  1197. gfn_t start_gfn;
  1198. gfn_t end_gfn;
  1199. int start_level;
  1200. int end_level;
  1201. /* output fields. */
  1202. gfn_t gfn;
  1203. struct kvm_rmap_head *rmap;
  1204. int level;
  1205. /* private field. */
  1206. struct kvm_rmap_head *end_rmap;
  1207. };
  1208. static void
  1209. rmap_walk_init_level(struct slot_rmap_walk_iterator *iterator, int level)
  1210. {
  1211. iterator->level = level;
  1212. iterator->gfn = iterator->start_gfn;
  1213. iterator->rmap = __gfn_to_rmap(iterator->gfn, level, iterator->slot);
  1214. iterator->end_rmap = __gfn_to_rmap(iterator->end_gfn, level,
  1215. iterator->slot);
  1216. }
  1217. static void
  1218. slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
  1219. struct kvm_memory_slot *slot, int start_level,
  1220. int end_level, gfn_t start_gfn, gfn_t end_gfn)
  1221. {
  1222. iterator->slot = slot;
  1223. iterator->start_level = start_level;
  1224. iterator->end_level = end_level;
  1225. iterator->start_gfn = start_gfn;
  1226. iterator->end_gfn = end_gfn;
  1227. rmap_walk_init_level(iterator, iterator->start_level);
  1228. }
  1229. static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)
  1230. {
  1231. return !!iterator->rmap;
  1232. }
  1233. static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)
  1234. {
  1235. if (++iterator->rmap <= iterator->end_rmap) {
  1236. iterator->gfn += (1UL << KVM_HPAGE_GFN_SHIFT(iterator->level));
  1237. return;
  1238. }
  1239. if (++iterator->level > iterator->end_level) {
  1240. iterator->rmap = NULL;
  1241. return;
  1242. }
  1243. rmap_walk_init_level(iterator, iterator->level);
  1244. }
  1245. #define for_each_slot_rmap_range(_slot_, _start_level_, _end_level_, \
  1246. _start_gfn, _end_gfn, _iter_) \
  1247. for (slot_rmap_walk_init(_iter_, _slot_, _start_level_, \
  1248. _end_level_, _start_gfn, _end_gfn); \
  1249. slot_rmap_walk_okay(_iter_); \
  1250. slot_rmap_walk_next(_iter_))
  1251. static int kvm_handle_hva_range(struct kvm *kvm,
  1252. unsigned long start,
  1253. unsigned long end,
  1254. unsigned long data,
  1255. int (*handler)(struct kvm *kvm,
  1256. struct kvm_rmap_head *rmap_head,
  1257. struct kvm_memory_slot *slot,
  1258. gfn_t gfn,
  1259. int level,
  1260. unsigned long data))
  1261. {
  1262. struct kvm_memslots *slots;
  1263. struct kvm_memory_slot *memslot;
  1264. struct slot_rmap_walk_iterator iterator;
  1265. int ret = 0;
  1266. int i;
  1267. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  1268. slots = __kvm_memslots(kvm, i);
  1269. kvm_for_each_memslot(memslot, slots) {
  1270. unsigned long hva_start, hva_end;
  1271. gfn_t gfn_start, gfn_end;
  1272. hva_start = max(start, memslot->userspace_addr);
  1273. hva_end = min(end, memslot->userspace_addr +
  1274. (memslot->npages << PAGE_SHIFT));
  1275. if (hva_start >= hva_end)
  1276. continue;
  1277. /*
  1278. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  1279. * {gfn_start, gfn_start+1, ..., gfn_end-1}.
  1280. */
  1281. gfn_start = hva_to_gfn_memslot(hva_start, memslot);
  1282. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  1283. for_each_slot_rmap_range(memslot, PT_PAGE_TABLE_LEVEL,
  1284. PT_MAX_HUGEPAGE_LEVEL,
  1285. gfn_start, gfn_end - 1,
  1286. &iterator)
  1287. ret |= handler(kvm, iterator.rmap, memslot,
  1288. iterator.gfn, iterator.level, data);
  1289. }
  1290. }
  1291. return ret;
  1292. }
  1293. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  1294. unsigned long data,
  1295. int (*handler)(struct kvm *kvm,
  1296. struct kvm_rmap_head *rmap_head,
  1297. struct kvm_memory_slot *slot,
  1298. gfn_t gfn, int level,
  1299. unsigned long data))
  1300. {
  1301. return kvm_handle_hva_range(kvm, hva, hva + 1, data, handler);
  1302. }
  1303. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  1304. {
  1305. return kvm_handle_hva(kvm, hva, 0, kvm_unmap_rmapp);
  1306. }
  1307. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  1308. {
  1309. return kvm_handle_hva_range(kvm, start, end, 0, kvm_unmap_rmapp);
  1310. }
  1311. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  1312. {
  1313. kvm_handle_hva(kvm, hva, (unsigned long)&pte, kvm_set_pte_rmapp);
  1314. }
  1315. static int kvm_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1316. struct kvm_memory_slot *slot, gfn_t gfn, int level,
  1317. unsigned long data)
  1318. {
  1319. u64 *sptep;
  1320. struct rmap_iterator uninitialized_var(iter);
  1321. int young = 0;
  1322. BUG_ON(!shadow_accessed_mask);
  1323. for_each_rmap_spte(rmap_head, &iter, sptep) {
  1324. if (*sptep & shadow_accessed_mask) {
  1325. young = 1;
  1326. clear_bit((ffs(shadow_accessed_mask) - 1),
  1327. (unsigned long *)sptep);
  1328. }
  1329. }
  1330. trace_kvm_age_page(gfn, level, slot, young);
  1331. return young;
  1332. }
  1333. static int kvm_test_age_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
  1334. struct kvm_memory_slot *slot, gfn_t gfn,
  1335. int level, unsigned long data)
  1336. {
  1337. u64 *sptep;
  1338. struct rmap_iterator iter;
  1339. int young = 0;
  1340. /*
  1341. * If there's no access bit in the secondary pte set by the
  1342. * hardware it's up to gup-fast/gup to set the access bit in
  1343. * the primary pte or in the page structure.
  1344. */
  1345. if (!shadow_accessed_mask)
  1346. goto out;
  1347. for_each_rmap_spte(rmap_head, &iter, sptep) {
  1348. if (*sptep & shadow_accessed_mask) {
  1349. young = 1;
  1350. break;
  1351. }
  1352. }
  1353. out:
  1354. return young;
  1355. }
  1356. #define RMAP_RECYCLE_THRESHOLD 1000
  1357. static void rmap_recycle(struct kvm_vcpu *vcpu, u64 *spte, gfn_t gfn)
  1358. {
  1359. struct kvm_rmap_head *rmap_head;
  1360. struct kvm_mmu_page *sp;
  1361. sp = page_header(__pa(spte));
  1362. rmap_head = gfn_to_rmap(vcpu->kvm, gfn, sp);
  1363. kvm_unmap_rmapp(vcpu->kvm, rmap_head, NULL, gfn, sp->role.level, 0);
  1364. kvm_flush_remote_tlbs(vcpu->kvm);
  1365. }
  1366. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
  1367. {
  1368. /*
  1369. * In case of absence of EPT Access and Dirty Bits supports,
  1370. * emulate the accessed bit for EPT, by checking if this page has
  1371. * an EPT mapping, and clearing it if it does. On the next access,
  1372. * a new EPT mapping will be established.
  1373. * This has some overhead, but not as much as the cost of swapping
  1374. * out actively used pages or breaking up actively used hugepages.
  1375. */
  1376. if (!shadow_accessed_mask) {
  1377. /*
  1378. * We are holding the kvm->mmu_lock, and we are blowing up
  1379. * shadow PTEs. MMU notifier consumers need to be kept at bay.
  1380. * This is correct as long as we don't decouple the mmu_lock
  1381. * protected regions (like invalidate_range_start|end does).
  1382. */
  1383. kvm->mmu_notifier_seq++;
  1384. return kvm_handle_hva_range(kvm, start, end, 0,
  1385. kvm_unmap_rmapp);
  1386. }
  1387. return kvm_handle_hva_range(kvm, start, end, 0, kvm_age_rmapp);
  1388. }
  1389. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  1390. {
  1391. return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
  1392. }
  1393. #ifdef MMU_DEBUG
  1394. static int is_empty_shadow_page(u64 *spt)
  1395. {
  1396. u64 *pos;
  1397. u64 *end;
  1398. for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
  1399. if (is_shadow_present_pte(*pos)) {
  1400. printk(KERN_ERR "%s: %p %llx\n", __func__,
  1401. pos, *pos);
  1402. return 0;
  1403. }
  1404. return 1;
  1405. }
  1406. #endif
  1407. /*
  1408. * This value is the sum of all of the kvm instances's
  1409. * kvm->arch.n_used_mmu_pages values. We need a global,
  1410. * aggregate version in order to make the slab shrinker
  1411. * faster
  1412. */
  1413. static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
  1414. {
  1415. kvm->arch.n_used_mmu_pages += nr;
  1416. percpu_counter_add(&kvm_total_used_mmu_pages, nr);
  1417. }
  1418. static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
  1419. {
  1420. MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
  1421. hlist_del(&sp->hash_link);
  1422. list_del(&sp->link);
  1423. free_page((unsigned long)sp->spt);
  1424. if (!sp->role.direct)
  1425. free_page((unsigned long)sp->gfns);
  1426. kmem_cache_free(mmu_page_header_cache, sp);
  1427. }
  1428. static unsigned kvm_page_table_hashfn(gfn_t gfn)
  1429. {
  1430. return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
  1431. }
  1432. static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
  1433. struct kvm_mmu_page *sp, u64 *parent_pte)
  1434. {
  1435. if (!parent_pte)
  1436. return;
  1437. pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
  1438. }
  1439. static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
  1440. u64 *parent_pte)
  1441. {
  1442. pte_list_remove(parent_pte, &sp->parent_ptes);
  1443. }
  1444. static void drop_parent_pte(struct kvm_mmu_page *sp,
  1445. u64 *parent_pte)
  1446. {
  1447. mmu_page_remove_parent_pte(sp, parent_pte);
  1448. mmu_spte_clear_no_track(parent_pte);
  1449. }
  1450. static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct)
  1451. {
  1452. struct kvm_mmu_page *sp;
  1453. sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
  1454. sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
  1455. if (!direct)
  1456. sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
  1457. set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
  1458. /*
  1459. * The active_mmu_pages list is the FIFO list, do not move the
  1460. * page until it is zapped. kvm_zap_obsolete_pages depends on
  1461. * this feature. See the comments in kvm_zap_obsolete_pages().
  1462. */
  1463. list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
  1464. kvm_mod_used_mmu_pages(vcpu->kvm, +1);
  1465. return sp;
  1466. }
  1467. static void mark_unsync(u64 *spte);
  1468. static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
  1469. {
  1470. u64 *sptep;
  1471. struct rmap_iterator iter;
  1472. for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
  1473. mark_unsync(sptep);
  1474. }
  1475. }
  1476. static void mark_unsync(u64 *spte)
  1477. {
  1478. struct kvm_mmu_page *sp;
  1479. unsigned int index;
  1480. sp = page_header(__pa(spte));
  1481. index = spte - sp->spt;
  1482. if (__test_and_set_bit(index, sp->unsync_child_bitmap))
  1483. return;
  1484. if (sp->unsync_children++)
  1485. return;
  1486. kvm_mmu_mark_parents_unsync(sp);
  1487. }
  1488. static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
  1489. struct kvm_mmu_page *sp)
  1490. {
  1491. return 0;
  1492. }
  1493. static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  1494. {
  1495. }
  1496. static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
  1497. struct kvm_mmu_page *sp, u64 *spte,
  1498. const void *pte)
  1499. {
  1500. WARN_ON(1);
  1501. }
  1502. #define KVM_PAGE_ARRAY_NR 16
  1503. struct kvm_mmu_pages {
  1504. struct mmu_page_and_offset {
  1505. struct kvm_mmu_page *sp;
  1506. unsigned int idx;
  1507. } page[KVM_PAGE_ARRAY_NR];
  1508. unsigned int nr;
  1509. };
  1510. static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
  1511. int idx)
  1512. {
  1513. int i;
  1514. if (sp->unsync)
  1515. for (i=0; i < pvec->nr; i++)
  1516. if (pvec->page[i].sp == sp)
  1517. return 0;
  1518. pvec->page[pvec->nr].sp = sp;
  1519. pvec->page[pvec->nr].idx = idx;
  1520. pvec->nr++;
  1521. return (pvec->nr == KVM_PAGE_ARRAY_NR);
  1522. }
  1523. static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
  1524. {
  1525. --sp->unsync_children;
  1526. WARN_ON((int)sp->unsync_children < 0);
  1527. __clear_bit(idx, sp->unsync_child_bitmap);
  1528. }
  1529. static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
  1530. struct kvm_mmu_pages *pvec)
  1531. {
  1532. int i, ret, nr_unsync_leaf = 0;
  1533. for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
  1534. struct kvm_mmu_page *child;
  1535. u64 ent = sp->spt[i];
  1536. if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
  1537. clear_unsync_child_bit(sp, i);
  1538. continue;
  1539. }
  1540. child = page_header(ent & PT64_BASE_ADDR_MASK);
  1541. if (child->unsync_children) {
  1542. if (mmu_pages_add(pvec, child, i))
  1543. return -ENOSPC;
  1544. ret = __mmu_unsync_walk(child, pvec);
  1545. if (!ret) {
  1546. clear_unsync_child_bit(sp, i);
  1547. continue;
  1548. } else if (ret > 0) {
  1549. nr_unsync_leaf += ret;
  1550. } else
  1551. return ret;
  1552. } else if (child->unsync) {
  1553. nr_unsync_leaf++;
  1554. if (mmu_pages_add(pvec, child, i))
  1555. return -ENOSPC;
  1556. } else
  1557. clear_unsync_child_bit(sp, i);
  1558. }
  1559. return nr_unsync_leaf;
  1560. }
  1561. #define INVALID_INDEX (-1)
  1562. static int mmu_unsync_walk(struct kvm_mmu_page *sp,
  1563. struct kvm_mmu_pages *pvec)
  1564. {
  1565. pvec->nr = 0;
  1566. if (!sp->unsync_children)
  1567. return 0;
  1568. mmu_pages_add(pvec, sp, INVALID_INDEX);
  1569. return __mmu_unsync_walk(sp, pvec);
  1570. }
  1571. static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
  1572. {
  1573. WARN_ON(!sp->unsync);
  1574. trace_kvm_mmu_sync_page(sp);
  1575. sp->unsync = 0;
  1576. --kvm->stat.mmu_unsync;
  1577. }
  1578. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1579. struct list_head *invalid_list);
  1580. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1581. struct list_head *invalid_list);
  1582. /*
  1583. * NOTE: we should pay more attention on the zapped-obsolete page
  1584. * (is_obsolete_sp(sp) && sp->role.invalid) when you do hash list walk
  1585. * since it has been deleted from active_mmu_pages but still can be found
  1586. * at hast list.
  1587. *
  1588. * for_each_gfn_valid_sp() has skipped that kind of pages.
  1589. */
  1590. #define for_each_gfn_valid_sp(_kvm, _sp, _gfn) \
  1591. hlist_for_each_entry(_sp, \
  1592. &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \
  1593. if ((_sp)->gfn != (_gfn) || is_obsolete_sp((_kvm), (_sp)) \
  1594. || (_sp)->role.invalid) {} else
  1595. #define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \
  1596. for_each_gfn_valid_sp(_kvm, _sp, _gfn) \
  1597. if ((_sp)->role.direct) {} else
  1598. /* @sp->gfn should be write-protected at the call site */
  1599. static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1600. struct list_head *invalid_list)
  1601. {
  1602. if (sp->role.cr4_pae != !!is_pae(vcpu)) {
  1603. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1604. return false;
  1605. }
  1606. if (vcpu->arch.mmu.sync_page(vcpu, sp) == 0) {
  1607. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1608. return false;
  1609. }
  1610. return true;
  1611. }
  1612. static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu,
  1613. struct list_head *invalid_list,
  1614. bool remote_flush, bool local_flush)
  1615. {
  1616. if (!list_empty(invalid_list)) {
  1617. kvm_mmu_commit_zap_page(vcpu->kvm, invalid_list);
  1618. return;
  1619. }
  1620. if (remote_flush)
  1621. kvm_flush_remote_tlbs(vcpu->kvm);
  1622. else if (local_flush)
  1623. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1624. }
  1625. #ifdef CONFIG_KVM_MMU_AUDIT
  1626. #include "mmu_audit.c"
  1627. #else
  1628. static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
  1629. static void mmu_audit_disable(void) { }
  1630. #endif
  1631. static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
  1632. {
  1633. return unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
  1634. }
  1635. static bool kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1636. struct list_head *invalid_list)
  1637. {
  1638. kvm_unlink_unsync_page(vcpu->kvm, sp);
  1639. return __kvm_sync_page(vcpu, sp, invalid_list);
  1640. }
  1641. /* @gfn should be write-protected at the call site */
  1642. static bool kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn,
  1643. struct list_head *invalid_list)
  1644. {
  1645. struct kvm_mmu_page *s;
  1646. bool ret = false;
  1647. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
  1648. if (!s->unsync)
  1649. continue;
  1650. WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
  1651. ret |= kvm_sync_page(vcpu, s, invalid_list);
  1652. }
  1653. return ret;
  1654. }
  1655. struct mmu_page_path {
  1656. struct kvm_mmu_page *parent[PT64_ROOT_LEVEL];
  1657. unsigned int idx[PT64_ROOT_LEVEL];
  1658. };
  1659. #define for_each_sp(pvec, sp, parents, i) \
  1660. for (i = mmu_pages_first(&pvec, &parents); \
  1661. i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
  1662. i = mmu_pages_next(&pvec, &parents, i))
  1663. static int mmu_pages_next(struct kvm_mmu_pages *pvec,
  1664. struct mmu_page_path *parents,
  1665. int i)
  1666. {
  1667. int n;
  1668. for (n = i+1; n < pvec->nr; n++) {
  1669. struct kvm_mmu_page *sp = pvec->page[n].sp;
  1670. unsigned idx = pvec->page[n].idx;
  1671. int level = sp->role.level;
  1672. parents->idx[level-1] = idx;
  1673. if (level == PT_PAGE_TABLE_LEVEL)
  1674. break;
  1675. parents->parent[level-2] = sp;
  1676. }
  1677. return n;
  1678. }
  1679. static int mmu_pages_first(struct kvm_mmu_pages *pvec,
  1680. struct mmu_page_path *parents)
  1681. {
  1682. struct kvm_mmu_page *sp;
  1683. int level;
  1684. if (pvec->nr == 0)
  1685. return 0;
  1686. WARN_ON(pvec->page[0].idx != INVALID_INDEX);
  1687. sp = pvec->page[0].sp;
  1688. level = sp->role.level;
  1689. WARN_ON(level == PT_PAGE_TABLE_LEVEL);
  1690. parents->parent[level-2] = sp;
  1691. /* Also set up a sentinel. Further entries in pvec are all
  1692. * children of sp, so this element is never overwritten.
  1693. */
  1694. parents->parent[level-1] = NULL;
  1695. return mmu_pages_next(pvec, parents, 0);
  1696. }
  1697. static void mmu_pages_clear_parents(struct mmu_page_path *parents)
  1698. {
  1699. struct kvm_mmu_page *sp;
  1700. unsigned int level = 0;
  1701. do {
  1702. unsigned int idx = parents->idx[level];
  1703. sp = parents->parent[level];
  1704. if (!sp)
  1705. return;
  1706. WARN_ON(idx == INVALID_INDEX);
  1707. clear_unsync_child_bit(sp, idx);
  1708. level++;
  1709. } while (!sp->unsync_children);
  1710. }
  1711. static void mmu_sync_children(struct kvm_vcpu *vcpu,
  1712. struct kvm_mmu_page *parent)
  1713. {
  1714. int i;
  1715. struct kvm_mmu_page *sp;
  1716. struct mmu_page_path parents;
  1717. struct kvm_mmu_pages pages;
  1718. LIST_HEAD(invalid_list);
  1719. bool flush = false;
  1720. while (mmu_unsync_walk(parent, &pages)) {
  1721. bool protected = false;
  1722. for_each_sp(pages, sp, parents, i)
  1723. protected |= rmap_write_protect(vcpu, sp->gfn);
  1724. if (protected) {
  1725. kvm_flush_remote_tlbs(vcpu->kvm);
  1726. flush = false;
  1727. }
  1728. for_each_sp(pages, sp, parents, i) {
  1729. flush |= kvm_sync_page(vcpu, sp, &invalid_list);
  1730. mmu_pages_clear_parents(&parents);
  1731. }
  1732. if (need_resched() || spin_needbreak(&vcpu->kvm->mmu_lock)) {
  1733. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1734. cond_resched_lock(&vcpu->kvm->mmu_lock);
  1735. flush = false;
  1736. }
  1737. }
  1738. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1739. }
  1740. static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
  1741. {
  1742. atomic_set(&sp->write_flooding_count, 0);
  1743. }
  1744. static void clear_sp_write_flooding_count(u64 *spte)
  1745. {
  1746. struct kvm_mmu_page *sp = page_header(__pa(spte));
  1747. __clear_sp_write_flooding_count(sp);
  1748. }
  1749. static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
  1750. gfn_t gfn,
  1751. gva_t gaddr,
  1752. unsigned level,
  1753. int direct,
  1754. unsigned access)
  1755. {
  1756. union kvm_mmu_page_role role;
  1757. unsigned quadrant;
  1758. struct kvm_mmu_page *sp;
  1759. bool need_sync = false;
  1760. bool flush = false;
  1761. LIST_HEAD(invalid_list);
  1762. role = vcpu->arch.mmu.base_role;
  1763. role.level = level;
  1764. role.direct = direct;
  1765. if (role.direct)
  1766. role.cr4_pae = 0;
  1767. role.access = access;
  1768. if (!vcpu->arch.mmu.direct_map
  1769. && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
  1770. quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
  1771. quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
  1772. role.quadrant = quadrant;
  1773. }
  1774. for_each_gfn_valid_sp(vcpu->kvm, sp, gfn) {
  1775. if (!need_sync && sp->unsync)
  1776. need_sync = true;
  1777. if (sp->role.word != role.word)
  1778. continue;
  1779. if (sp->unsync) {
  1780. /* The page is good, but __kvm_sync_page might still end
  1781. * up zapping it. If so, break in order to rebuild it.
  1782. */
  1783. if (!__kvm_sync_page(vcpu, sp, &invalid_list))
  1784. break;
  1785. WARN_ON(!list_empty(&invalid_list));
  1786. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1787. }
  1788. if (sp->unsync_children)
  1789. kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
  1790. __clear_sp_write_flooding_count(sp);
  1791. trace_kvm_mmu_get_page(sp, false);
  1792. return sp;
  1793. }
  1794. ++vcpu->kvm->stat.mmu_cache_miss;
  1795. sp = kvm_mmu_alloc_page(vcpu, direct);
  1796. sp->gfn = gfn;
  1797. sp->role = role;
  1798. hlist_add_head(&sp->hash_link,
  1799. &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
  1800. if (!direct) {
  1801. /*
  1802. * we should do write protection before syncing pages
  1803. * otherwise the content of the synced shadow page may
  1804. * be inconsistent with guest page table.
  1805. */
  1806. account_shadowed(vcpu->kvm, sp);
  1807. if (level == PT_PAGE_TABLE_LEVEL &&
  1808. rmap_write_protect(vcpu, gfn))
  1809. kvm_flush_remote_tlbs(vcpu->kvm);
  1810. if (level > PT_PAGE_TABLE_LEVEL && need_sync)
  1811. flush |= kvm_sync_pages(vcpu, gfn, &invalid_list);
  1812. }
  1813. sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
  1814. clear_page(sp->spt);
  1815. trace_kvm_mmu_get_page(sp, true);
  1816. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1817. return sp;
  1818. }
  1819. static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
  1820. struct kvm_vcpu *vcpu, u64 addr)
  1821. {
  1822. iterator->addr = addr;
  1823. iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
  1824. iterator->level = vcpu->arch.mmu.shadow_root_level;
  1825. if (iterator->level == PT64_ROOT_LEVEL &&
  1826. vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
  1827. !vcpu->arch.mmu.direct_map)
  1828. --iterator->level;
  1829. if (iterator->level == PT32E_ROOT_LEVEL) {
  1830. iterator->shadow_addr
  1831. = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
  1832. iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
  1833. --iterator->level;
  1834. if (!iterator->shadow_addr)
  1835. iterator->level = 0;
  1836. }
  1837. }
  1838. static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
  1839. {
  1840. if (iterator->level < PT_PAGE_TABLE_LEVEL)
  1841. return false;
  1842. iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
  1843. iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
  1844. return true;
  1845. }
  1846. static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
  1847. u64 spte)
  1848. {
  1849. if (is_last_spte(spte, iterator->level)) {
  1850. iterator->level = 0;
  1851. return;
  1852. }
  1853. iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
  1854. --iterator->level;
  1855. }
  1856. static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
  1857. {
  1858. return __shadow_walk_next(iterator, *iterator->sptep);
  1859. }
  1860. static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
  1861. struct kvm_mmu_page *sp)
  1862. {
  1863. u64 spte;
  1864. BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
  1865. spte = __pa(sp->spt) | shadow_present_mask | PT_WRITABLE_MASK |
  1866. shadow_user_mask | shadow_x_mask | shadow_accessed_mask;
  1867. mmu_spte_set(sptep, spte);
  1868. mmu_page_add_parent_pte(vcpu, sp, sptep);
  1869. if (sp->unsync_children || sp->unsync)
  1870. mark_unsync(sptep);
  1871. }
  1872. static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  1873. unsigned direct_access)
  1874. {
  1875. if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
  1876. struct kvm_mmu_page *child;
  1877. /*
  1878. * For the direct sp, if the guest pte's dirty bit
  1879. * changed form clean to dirty, it will corrupt the
  1880. * sp's access: allow writable in the read-only sp,
  1881. * so we should update the spte at this point to get
  1882. * a new sp with the correct access.
  1883. */
  1884. child = page_header(*sptep & PT64_BASE_ADDR_MASK);
  1885. if (child->role.access == direct_access)
  1886. return;
  1887. drop_parent_pte(child, sptep);
  1888. kvm_flush_remote_tlbs(vcpu->kvm);
  1889. }
  1890. }
  1891. static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
  1892. u64 *spte)
  1893. {
  1894. u64 pte;
  1895. struct kvm_mmu_page *child;
  1896. pte = *spte;
  1897. if (is_shadow_present_pte(pte)) {
  1898. if (is_last_spte(pte, sp->role.level)) {
  1899. drop_spte(kvm, spte);
  1900. if (is_large_pte(pte))
  1901. --kvm->stat.lpages;
  1902. } else {
  1903. child = page_header(pte & PT64_BASE_ADDR_MASK);
  1904. drop_parent_pte(child, spte);
  1905. }
  1906. return true;
  1907. }
  1908. if (is_mmio_spte(pte))
  1909. mmu_spte_clear_no_track(spte);
  1910. return false;
  1911. }
  1912. static void kvm_mmu_page_unlink_children(struct kvm *kvm,
  1913. struct kvm_mmu_page *sp)
  1914. {
  1915. unsigned i;
  1916. for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
  1917. mmu_page_zap_pte(kvm, sp, sp->spt + i);
  1918. }
  1919. static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
  1920. {
  1921. u64 *sptep;
  1922. struct rmap_iterator iter;
  1923. while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
  1924. drop_parent_pte(sp, sptep);
  1925. }
  1926. static int mmu_zap_unsync_children(struct kvm *kvm,
  1927. struct kvm_mmu_page *parent,
  1928. struct list_head *invalid_list)
  1929. {
  1930. int i, zapped = 0;
  1931. struct mmu_page_path parents;
  1932. struct kvm_mmu_pages pages;
  1933. if (parent->role.level == PT_PAGE_TABLE_LEVEL)
  1934. return 0;
  1935. while (mmu_unsync_walk(parent, &pages)) {
  1936. struct kvm_mmu_page *sp;
  1937. for_each_sp(pages, sp, parents, i) {
  1938. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  1939. mmu_pages_clear_parents(&parents);
  1940. zapped++;
  1941. }
  1942. }
  1943. return zapped;
  1944. }
  1945. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1946. struct list_head *invalid_list)
  1947. {
  1948. int ret;
  1949. trace_kvm_mmu_prepare_zap_page(sp);
  1950. ++kvm->stat.mmu_shadow_zapped;
  1951. ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
  1952. kvm_mmu_page_unlink_children(kvm, sp);
  1953. kvm_mmu_unlink_parents(kvm, sp);
  1954. if (!sp->role.invalid && !sp->role.direct)
  1955. unaccount_shadowed(kvm, sp);
  1956. if (sp->unsync)
  1957. kvm_unlink_unsync_page(kvm, sp);
  1958. if (!sp->root_count) {
  1959. /* Count self */
  1960. ret++;
  1961. list_move(&sp->link, invalid_list);
  1962. kvm_mod_used_mmu_pages(kvm, -1);
  1963. } else {
  1964. list_move(&sp->link, &kvm->arch.active_mmu_pages);
  1965. /*
  1966. * The obsolete pages can not be used on any vcpus.
  1967. * See the comments in kvm_mmu_invalidate_zap_all_pages().
  1968. */
  1969. if (!sp->role.invalid && !is_obsolete_sp(kvm, sp))
  1970. kvm_reload_remote_mmus(kvm);
  1971. }
  1972. sp->role.invalid = 1;
  1973. return ret;
  1974. }
  1975. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1976. struct list_head *invalid_list)
  1977. {
  1978. struct kvm_mmu_page *sp, *nsp;
  1979. if (list_empty(invalid_list))
  1980. return;
  1981. /*
  1982. * We need to make sure everyone sees our modifications to
  1983. * the page tables and see changes to vcpu->mode here. The barrier
  1984. * in the kvm_flush_remote_tlbs() achieves this. This pairs
  1985. * with vcpu_enter_guest and walk_shadow_page_lockless_begin/end.
  1986. *
  1987. * In addition, kvm_flush_remote_tlbs waits for all vcpus to exit
  1988. * guest mode and/or lockless shadow page table walks.
  1989. */
  1990. kvm_flush_remote_tlbs(kvm);
  1991. list_for_each_entry_safe(sp, nsp, invalid_list, link) {
  1992. WARN_ON(!sp->role.invalid || sp->root_count);
  1993. kvm_mmu_free_page(sp);
  1994. }
  1995. }
  1996. static bool prepare_zap_oldest_mmu_page(struct kvm *kvm,
  1997. struct list_head *invalid_list)
  1998. {
  1999. struct kvm_mmu_page *sp;
  2000. if (list_empty(&kvm->arch.active_mmu_pages))
  2001. return false;
  2002. sp = list_last_entry(&kvm->arch.active_mmu_pages,
  2003. struct kvm_mmu_page, link);
  2004. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  2005. return true;
  2006. }
  2007. /*
  2008. * Changing the number of mmu pages allocated to the vm
  2009. * Note: if goal_nr_mmu_pages is too small, you will get dead lock
  2010. */
  2011. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
  2012. {
  2013. LIST_HEAD(invalid_list);
  2014. spin_lock(&kvm->mmu_lock);
  2015. if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
  2016. /* Need to free some mmu pages to achieve the goal. */
  2017. while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages)
  2018. if (!prepare_zap_oldest_mmu_page(kvm, &invalid_list))
  2019. break;
  2020. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2021. goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
  2022. }
  2023. kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
  2024. spin_unlock(&kvm->mmu_lock);
  2025. }
  2026. int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
  2027. {
  2028. struct kvm_mmu_page *sp;
  2029. LIST_HEAD(invalid_list);
  2030. int r;
  2031. pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
  2032. r = 0;
  2033. spin_lock(&kvm->mmu_lock);
  2034. for_each_gfn_indirect_valid_sp(kvm, sp, gfn) {
  2035. pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
  2036. sp->role.word);
  2037. r = 1;
  2038. kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
  2039. }
  2040. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2041. spin_unlock(&kvm->mmu_lock);
  2042. return r;
  2043. }
  2044. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
  2045. static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  2046. {
  2047. trace_kvm_mmu_unsync_page(sp);
  2048. ++vcpu->kvm->stat.mmu_unsync;
  2049. sp->unsync = 1;
  2050. kvm_mmu_mark_parents_unsync(sp);
  2051. }
  2052. static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
  2053. bool can_unsync)
  2054. {
  2055. struct kvm_mmu_page *sp;
  2056. if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
  2057. return true;
  2058. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
  2059. if (!can_unsync)
  2060. return true;
  2061. if (sp->unsync)
  2062. continue;
  2063. WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
  2064. kvm_unsync_page(vcpu, sp);
  2065. }
  2066. return false;
  2067. }
  2068. static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
  2069. {
  2070. if (pfn_valid(pfn))
  2071. return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn));
  2072. return true;
  2073. }
  2074. static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  2075. unsigned pte_access, int level,
  2076. gfn_t gfn, kvm_pfn_t pfn, bool speculative,
  2077. bool can_unsync, bool host_writable)
  2078. {
  2079. u64 spte = 0;
  2080. int ret = 0;
  2081. if (set_mmio_spte(vcpu, sptep, gfn, pfn, pte_access))
  2082. return 0;
  2083. /*
  2084. * For the EPT case, shadow_present_mask is 0 if hardware
  2085. * supports exec-only page table entries. In that case,
  2086. * ACC_USER_MASK and shadow_user_mask are used to represent
  2087. * read access. See FNAME(gpte_access) in paging_tmpl.h.
  2088. */
  2089. spte |= shadow_present_mask;
  2090. if (!speculative)
  2091. spte |= shadow_accessed_mask;
  2092. if (pte_access & ACC_EXEC_MASK)
  2093. spte |= shadow_x_mask;
  2094. else
  2095. spte |= shadow_nx_mask;
  2096. if (pte_access & ACC_USER_MASK)
  2097. spte |= shadow_user_mask;
  2098. if (level > PT_PAGE_TABLE_LEVEL)
  2099. spte |= PT_PAGE_SIZE_MASK;
  2100. if (tdp_enabled)
  2101. spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
  2102. kvm_is_mmio_pfn(pfn));
  2103. if (host_writable)
  2104. spte |= SPTE_HOST_WRITEABLE;
  2105. else
  2106. pte_access &= ~ACC_WRITE_MASK;
  2107. spte |= (u64)pfn << PAGE_SHIFT;
  2108. if (pte_access & ACC_WRITE_MASK) {
  2109. /*
  2110. * Other vcpu creates new sp in the window between
  2111. * mapping_level() and acquiring mmu-lock. We can
  2112. * allow guest to retry the access, the mapping can
  2113. * be fixed if guest refault.
  2114. */
  2115. if (level > PT_PAGE_TABLE_LEVEL &&
  2116. mmu_gfn_lpage_is_disallowed(vcpu, gfn, level))
  2117. goto done;
  2118. spte |= PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE;
  2119. /*
  2120. * Optimization: for pte sync, if spte was writable the hash
  2121. * lookup is unnecessary (and expensive). Write protection
  2122. * is responsibility of mmu_get_page / kvm_sync_page.
  2123. * Same reasoning can be applied to dirty page accounting.
  2124. */
  2125. if (!can_unsync && is_writable_pte(*sptep))
  2126. goto set_pte;
  2127. if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
  2128. pgprintk("%s: found shadow page for %llx, marking ro\n",
  2129. __func__, gfn);
  2130. ret = 1;
  2131. pte_access &= ~ACC_WRITE_MASK;
  2132. spte &= ~(PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE);
  2133. }
  2134. }
  2135. if (pte_access & ACC_WRITE_MASK) {
  2136. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  2137. spte |= shadow_dirty_mask;
  2138. }
  2139. set_pte:
  2140. if (mmu_spte_update(sptep, spte))
  2141. kvm_flush_remote_tlbs(vcpu->kvm);
  2142. done:
  2143. return ret;
  2144. }
  2145. static bool mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access,
  2146. int write_fault, int level, gfn_t gfn, kvm_pfn_t pfn,
  2147. bool speculative, bool host_writable)
  2148. {
  2149. int was_rmapped = 0;
  2150. int rmap_count;
  2151. bool emulate = false;
  2152. pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
  2153. *sptep, write_fault, gfn);
  2154. if (is_shadow_present_pte(*sptep)) {
  2155. /*
  2156. * If we overwrite a PTE page pointer with a 2MB PMD, unlink
  2157. * the parent of the now unreachable PTE.
  2158. */
  2159. if (level > PT_PAGE_TABLE_LEVEL &&
  2160. !is_large_pte(*sptep)) {
  2161. struct kvm_mmu_page *child;
  2162. u64 pte = *sptep;
  2163. child = page_header(pte & PT64_BASE_ADDR_MASK);
  2164. drop_parent_pte(child, sptep);
  2165. kvm_flush_remote_tlbs(vcpu->kvm);
  2166. } else if (pfn != spte_to_pfn(*sptep)) {
  2167. pgprintk("hfn old %llx new %llx\n",
  2168. spte_to_pfn(*sptep), pfn);
  2169. drop_spte(vcpu->kvm, sptep);
  2170. kvm_flush_remote_tlbs(vcpu->kvm);
  2171. } else
  2172. was_rmapped = 1;
  2173. }
  2174. if (set_spte(vcpu, sptep, pte_access, level, gfn, pfn, speculative,
  2175. true, host_writable)) {
  2176. if (write_fault)
  2177. emulate = true;
  2178. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2179. }
  2180. if (unlikely(is_mmio_spte(*sptep)))
  2181. emulate = true;
  2182. pgprintk("%s: setting spte %llx\n", __func__, *sptep);
  2183. pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
  2184. is_large_pte(*sptep)? "2MB" : "4kB",
  2185. *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
  2186. *sptep, sptep);
  2187. if (!was_rmapped && is_large_pte(*sptep))
  2188. ++vcpu->kvm->stat.lpages;
  2189. if (is_shadow_present_pte(*sptep)) {
  2190. if (!was_rmapped) {
  2191. rmap_count = rmap_add(vcpu, sptep, gfn);
  2192. if (rmap_count > RMAP_RECYCLE_THRESHOLD)
  2193. rmap_recycle(vcpu, sptep, gfn);
  2194. }
  2195. }
  2196. kvm_release_pfn_clean(pfn);
  2197. return emulate;
  2198. }
  2199. static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
  2200. bool no_dirty_log)
  2201. {
  2202. struct kvm_memory_slot *slot;
  2203. slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
  2204. if (!slot)
  2205. return KVM_PFN_ERR_FAULT;
  2206. return gfn_to_pfn_memslot_atomic(slot, gfn);
  2207. }
  2208. static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
  2209. struct kvm_mmu_page *sp,
  2210. u64 *start, u64 *end)
  2211. {
  2212. struct page *pages[PTE_PREFETCH_NUM];
  2213. struct kvm_memory_slot *slot;
  2214. unsigned access = sp->role.access;
  2215. int i, ret;
  2216. gfn_t gfn;
  2217. gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
  2218. slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
  2219. if (!slot)
  2220. return -1;
  2221. ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start);
  2222. if (ret <= 0)
  2223. return -1;
  2224. for (i = 0; i < ret; i++, gfn++, start++)
  2225. mmu_set_spte(vcpu, start, access, 0, sp->role.level, gfn,
  2226. page_to_pfn(pages[i]), true, true);
  2227. return 0;
  2228. }
  2229. static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
  2230. struct kvm_mmu_page *sp, u64 *sptep)
  2231. {
  2232. u64 *spte, *start = NULL;
  2233. int i;
  2234. WARN_ON(!sp->role.direct);
  2235. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  2236. spte = sp->spt + i;
  2237. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  2238. if (is_shadow_present_pte(*spte) || spte == sptep) {
  2239. if (!start)
  2240. continue;
  2241. if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
  2242. break;
  2243. start = NULL;
  2244. } else if (!start)
  2245. start = spte;
  2246. }
  2247. }
  2248. static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
  2249. {
  2250. struct kvm_mmu_page *sp;
  2251. /*
  2252. * Since it's no accessed bit on EPT, it's no way to
  2253. * distinguish between actually accessed translations
  2254. * and prefetched, so disable pte prefetch if EPT is
  2255. * enabled.
  2256. */
  2257. if (!shadow_accessed_mask)
  2258. return;
  2259. sp = page_header(__pa(sptep));
  2260. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  2261. return;
  2262. __direct_pte_prefetch(vcpu, sp, sptep);
  2263. }
  2264. static int __direct_map(struct kvm_vcpu *vcpu, int write, int map_writable,
  2265. int level, gfn_t gfn, kvm_pfn_t pfn, bool prefault)
  2266. {
  2267. struct kvm_shadow_walk_iterator iterator;
  2268. struct kvm_mmu_page *sp;
  2269. int emulate = 0;
  2270. gfn_t pseudo_gfn;
  2271. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2272. return 0;
  2273. for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
  2274. if (iterator.level == level) {
  2275. emulate = mmu_set_spte(vcpu, iterator.sptep, ACC_ALL,
  2276. write, level, gfn, pfn, prefault,
  2277. map_writable);
  2278. direct_pte_prefetch(vcpu, iterator.sptep);
  2279. ++vcpu->stat.pf_fixed;
  2280. break;
  2281. }
  2282. drop_large_spte(vcpu, iterator.sptep);
  2283. if (!is_shadow_present_pte(*iterator.sptep)) {
  2284. u64 base_addr = iterator.addr;
  2285. base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
  2286. pseudo_gfn = base_addr >> PAGE_SHIFT;
  2287. sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
  2288. iterator.level - 1, 1, ACC_ALL);
  2289. link_shadow_page(vcpu, iterator.sptep, sp);
  2290. }
  2291. }
  2292. return emulate;
  2293. }
  2294. static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
  2295. {
  2296. siginfo_t info;
  2297. info.si_signo = SIGBUS;
  2298. info.si_errno = 0;
  2299. info.si_code = BUS_MCEERR_AR;
  2300. info.si_addr = (void __user *)address;
  2301. info.si_addr_lsb = PAGE_SHIFT;
  2302. send_sig_info(SIGBUS, &info, tsk);
  2303. }
  2304. static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
  2305. {
  2306. /*
  2307. * Do not cache the mmio info caused by writing the readonly gfn
  2308. * into the spte otherwise read access on readonly gfn also can
  2309. * caused mmio page fault and treat it as mmio access.
  2310. * Return 1 to tell kvm to emulate it.
  2311. */
  2312. if (pfn == KVM_PFN_ERR_RO_FAULT)
  2313. return 1;
  2314. if (pfn == KVM_PFN_ERR_HWPOISON) {
  2315. kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current);
  2316. return 0;
  2317. }
  2318. return -EFAULT;
  2319. }
  2320. static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
  2321. gfn_t *gfnp, kvm_pfn_t *pfnp,
  2322. int *levelp)
  2323. {
  2324. kvm_pfn_t pfn = *pfnp;
  2325. gfn_t gfn = *gfnp;
  2326. int level = *levelp;
  2327. /*
  2328. * Check if it's a transparent hugepage. If this would be an
  2329. * hugetlbfs page, level wouldn't be set to
  2330. * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
  2331. * here.
  2332. */
  2333. if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) &&
  2334. level == PT_PAGE_TABLE_LEVEL &&
  2335. PageTransCompoundMap(pfn_to_page(pfn)) &&
  2336. !mmu_gfn_lpage_is_disallowed(vcpu, gfn, PT_DIRECTORY_LEVEL)) {
  2337. unsigned long mask;
  2338. /*
  2339. * mmu_notifier_retry was successful and we hold the
  2340. * mmu_lock here, so the pmd can't become splitting
  2341. * from under us, and in turn
  2342. * __split_huge_page_refcount() can't run from under
  2343. * us and we can safely transfer the refcount from
  2344. * PG_tail to PG_head as we switch the pfn to tail to
  2345. * head.
  2346. */
  2347. *levelp = level = PT_DIRECTORY_LEVEL;
  2348. mask = KVM_PAGES_PER_HPAGE(level) - 1;
  2349. VM_BUG_ON((gfn & mask) != (pfn & mask));
  2350. if (pfn & mask) {
  2351. gfn &= ~mask;
  2352. *gfnp = gfn;
  2353. kvm_release_pfn_clean(pfn);
  2354. pfn &= ~mask;
  2355. kvm_get_pfn(pfn);
  2356. *pfnp = pfn;
  2357. }
  2358. }
  2359. }
  2360. static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
  2361. kvm_pfn_t pfn, unsigned access, int *ret_val)
  2362. {
  2363. /* The pfn is invalid, report the error! */
  2364. if (unlikely(is_error_pfn(pfn))) {
  2365. *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
  2366. return true;
  2367. }
  2368. if (unlikely(is_noslot_pfn(pfn)))
  2369. vcpu_cache_mmio_info(vcpu, gva, gfn, access);
  2370. return false;
  2371. }
  2372. static bool page_fault_can_be_fast(u32 error_code)
  2373. {
  2374. /*
  2375. * Do not fix the mmio spte with invalid generation number which
  2376. * need to be updated by slow page fault path.
  2377. */
  2378. if (unlikely(error_code & PFERR_RSVD_MASK))
  2379. return false;
  2380. /*
  2381. * #PF can be fast only if the shadow page table is present and it
  2382. * is caused by write-protect, that means we just need change the
  2383. * W bit of the spte which can be done out of mmu-lock.
  2384. */
  2385. if (!(error_code & PFERR_PRESENT_MASK) ||
  2386. !(error_code & PFERR_WRITE_MASK))
  2387. return false;
  2388. return true;
  2389. }
  2390. static bool
  2391. fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  2392. u64 *sptep, u64 spte)
  2393. {
  2394. gfn_t gfn;
  2395. WARN_ON(!sp->role.direct);
  2396. /*
  2397. * The gfn of direct spte is stable since it is calculated
  2398. * by sp->gfn.
  2399. */
  2400. gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
  2401. /*
  2402. * Theoretically we could also set dirty bit (and flush TLB) here in
  2403. * order to eliminate unnecessary PML logging. See comments in
  2404. * set_spte. But fast_page_fault is very unlikely to happen with PML
  2405. * enabled, so we do not do this. This might result in the same GPA
  2406. * to be logged in PML buffer again when the write really happens, and
  2407. * eventually to be called by mark_page_dirty twice. But it's also no
  2408. * harm. This also avoids the TLB flush needed after setting dirty bit
  2409. * so non-PML cases won't be impacted.
  2410. *
  2411. * Compare with set_spte where instead shadow_dirty_mask is set.
  2412. */
  2413. if (cmpxchg64(sptep, spte, spte | PT_WRITABLE_MASK) == spte)
  2414. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  2415. return true;
  2416. }
  2417. /*
  2418. * Return value:
  2419. * - true: let the vcpu to access on the same address again.
  2420. * - false: let the real page fault path to fix it.
  2421. */
  2422. static bool fast_page_fault(struct kvm_vcpu *vcpu, gva_t gva, int level,
  2423. u32 error_code)
  2424. {
  2425. struct kvm_shadow_walk_iterator iterator;
  2426. struct kvm_mmu_page *sp;
  2427. bool ret = false;
  2428. u64 spte = 0ull;
  2429. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2430. return false;
  2431. if (!page_fault_can_be_fast(error_code))
  2432. return false;
  2433. walk_shadow_page_lockless_begin(vcpu);
  2434. for_each_shadow_entry_lockless(vcpu, gva, iterator, spte)
  2435. if (!is_shadow_present_pte(spte) || iterator.level < level)
  2436. break;
  2437. /*
  2438. * If the mapping has been changed, let the vcpu fault on the
  2439. * same address again.
  2440. */
  2441. if (!is_shadow_present_pte(spte)) {
  2442. ret = true;
  2443. goto exit;
  2444. }
  2445. sp = page_header(__pa(iterator.sptep));
  2446. if (!is_last_spte(spte, sp->role.level))
  2447. goto exit;
  2448. /*
  2449. * Check if it is a spurious fault caused by TLB lazily flushed.
  2450. *
  2451. * Need not check the access of upper level table entries since
  2452. * they are always ACC_ALL.
  2453. */
  2454. if (is_writable_pte(spte)) {
  2455. ret = true;
  2456. goto exit;
  2457. }
  2458. /*
  2459. * Currently, to simplify the code, only the spte write-protected
  2460. * by dirty-log can be fast fixed.
  2461. */
  2462. if (!spte_is_locklessly_modifiable(spte))
  2463. goto exit;
  2464. /*
  2465. * Do not fix write-permission on the large spte since we only dirty
  2466. * the first page into the dirty-bitmap in fast_pf_fix_direct_spte()
  2467. * that means other pages are missed if its slot is dirty-logged.
  2468. *
  2469. * Instead, we let the slow page fault path create a normal spte to
  2470. * fix the access.
  2471. *
  2472. * See the comments in kvm_arch_commit_memory_region().
  2473. */
  2474. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  2475. goto exit;
  2476. /*
  2477. * Currently, fast page fault only works for direct mapping since
  2478. * the gfn is not stable for indirect shadow page.
  2479. * See Documentation/virtual/kvm/locking.txt to get more detail.
  2480. */
  2481. ret = fast_pf_fix_direct_spte(vcpu, sp, iterator.sptep, spte);
  2482. exit:
  2483. trace_fast_page_fault(vcpu, gva, error_code, iterator.sptep,
  2484. spte, ret);
  2485. walk_shadow_page_lockless_end(vcpu);
  2486. return ret;
  2487. }
  2488. static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
  2489. gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable);
  2490. static void make_mmu_pages_available(struct kvm_vcpu *vcpu);
  2491. static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, u32 error_code,
  2492. gfn_t gfn, bool prefault)
  2493. {
  2494. int r;
  2495. int level;
  2496. bool force_pt_level = false;
  2497. kvm_pfn_t pfn;
  2498. unsigned long mmu_seq;
  2499. bool map_writable, write = error_code & PFERR_WRITE_MASK;
  2500. level = mapping_level(vcpu, gfn, &force_pt_level);
  2501. if (likely(!force_pt_level)) {
  2502. /*
  2503. * This path builds a PAE pagetable - so we can map
  2504. * 2mb pages at maximum. Therefore check if the level
  2505. * is larger than that.
  2506. */
  2507. if (level > PT_DIRECTORY_LEVEL)
  2508. level = PT_DIRECTORY_LEVEL;
  2509. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  2510. }
  2511. if (fast_page_fault(vcpu, v, level, error_code))
  2512. return 0;
  2513. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  2514. smp_rmb();
  2515. if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
  2516. return 0;
  2517. if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
  2518. return r;
  2519. spin_lock(&vcpu->kvm->mmu_lock);
  2520. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  2521. goto out_unlock;
  2522. make_mmu_pages_available(vcpu);
  2523. if (likely(!force_pt_level))
  2524. transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
  2525. r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
  2526. spin_unlock(&vcpu->kvm->mmu_lock);
  2527. return r;
  2528. out_unlock:
  2529. spin_unlock(&vcpu->kvm->mmu_lock);
  2530. kvm_release_pfn_clean(pfn);
  2531. return 0;
  2532. }
  2533. static void mmu_free_roots(struct kvm_vcpu *vcpu)
  2534. {
  2535. int i;
  2536. struct kvm_mmu_page *sp;
  2537. LIST_HEAD(invalid_list);
  2538. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2539. return;
  2540. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
  2541. (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
  2542. vcpu->arch.mmu.direct_map)) {
  2543. hpa_t root = vcpu->arch.mmu.root_hpa;
  2544. spin_lock(&vcpu->kvm->mmu_lock);
  2545. sp = page_header(root);
  2546. --sp->root_count;
  2547. if (!sp->root_count && sp->role.invalid) {
  2548. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
  2549. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  2550. }
  2551. spin_unlock(&vcpu->kvm->mmu_lock);
  2552. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  2553. return;
  2554. }
  2555. spin_lock(&vcpu->kvm->mmu_lock);
  2556. for (i = 0; i < 4; ++i) {
  2557. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2558. if (root) {
  2559. root &= PT64_BASE_ADDR_MASK;
  2560. sp = page_header(root);
  2561. --sp->root_count;
  2562. if (!sp->root_count && sp->role.invalid)
  2563. kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
  2564. &invalid_list);
  2565. }
  2566. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  2567. }
  2568. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  2569. spin_unlock(&vcpu->kvm->mmu_lock);
  2570. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  2571. }
  2572. static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
  2573. {
  2574. int ret = 0;
  2575. if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
  2576. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2577. ret = 1;
  2578. }
  2579. return ret;
  2580. }
  2581. static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
  2582. {
  2583. struct kvm_mmu_page *sp;
  2584. unsigned i;
  2585. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  2586. spin_lock(&vcpu->kvm->mmu_lock);
  2587. make_mmu_pages_available(vcpu);
  2588. sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL, 1, ACC_ALL);
  2589. ++sp->root_count;
  2590. spin_unlock(&vcpu->kvm->mmu_lock);
  2591. vcpu->arch.mmu.root_hpa = __pa(sp->spt);
  2592. } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
  2593. for (i = 0; i < 4; ++i) {
  2594. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2595. MMU_WARN_ON(VALID_PAGE(root));
  2596. spin_lock(&vcpu->kvm->mmu_lock);
  2597. make_mmu_pages_available(vcpu);
  2598. sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
  2599. i << 30, PT32_ROOT_LEVEL, 1, ACC_ALL);
  2600. root = __pa(sp->spt);
  2601. ++sp->root_count;
  2602. spin_unlock(&vcpu->kvm->mmu_lock);
  2603. vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
  2604. }
  2605. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
  2606. } else
  2607. BUG();
  2608. return 0;
  2609. }
  2610. static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
  2611. {
  2612. struct kvm_mmu_page *sp;
  2613. u64 pdptr, pm_mask;
  2614. gfn_t root_gfn;
  2615. int i;
  2616. root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
  2617. if (mmu_check_root(vcpu, root_gfn))
  2618. return 1;
  2619. /*
  2620. * Do we shadow a long mode page table? If so we need to
  2621. * write-protect the guests page table root.
  2622. */
  2623. if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
  2624. hpa_t root = vcpu->arch.mmu.root_hpa;
  2625. MMU_WARN_ON(VALID_PAGE(root));
  2626. spin_lock(&vcpu->kvm->mmu_lock);
  2627. make_mmu_pages_available(vcpu);
  2628. sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
  2629. 0, ACC_ALL);
  2630. root = __pa(sp->spt);
  2631. ++sp->root_count;
  2632. spin_unlock(&vcpu->kvm->mmu_lock);
  2633. vcpu->arch.mmu.root_hpa = root;
  2634. return 0;
  2635. }
  2636. /*
  2637. * We shadow a 32 bit page table. This may be a legacy 2-level
  2638. * or a PAE 3-level page table. In either case we need to be aware that
  2639. * the shadow page table may be a PAE or a long mode page table.
  2640. */
  2641. pm_mask = PT_PRESENT_MASK;
  2642. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
  2643. pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
  2644. for (i = 0; i < 4; ++i) {
  2645. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2646. MMU_WARN_ON(VALID_PAGE(root));
  2647. if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
  2648. pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i);
  2649. if (!(pdptr & PT_PRESENT_MASK)) {
  2650. vcpu->arch.mmu.pae_root[i] = 0;
  2651. continue;
  2652. }
  2653. root_gfn = pdptr >> PAGE_SHIFT;
  2654. if (mmu_check_root(vcpu, root_gfn))
  2655. return 1;
  2656. }
  2657. spin_lock(&vcpu->kvm->mmu_lock);
  2658. make_mmu_pages_available(vcpu);
  2659. sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, PT32_ROOT_LEVEL,
  2660. 0, ACC_ALL);
  2661. root = __pa(sp->spt);
  2662. ++sp->root_count;
  2663. spin_unlock(&vcpu->kvm->mmu_lock);
  2664. vcpu->arch.mmu.pae_root[i] = root | pm_mask;
  2665. }
  2666. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
  2667. /*
  2668. * If we shadow a 32 bit page table with a long mode page
  2669. * table we enter this path.
  2670. */
  2671. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  2672. if (vcpu->arch.mmu.lm_root == NULL) {
  2673. /*
  2674. * The additional page necessary for this is only
  2675. * allocated on demand.
  2676. */
  2677. u64 *lm_root;
  2678. lm_root = (void*)get_zeroed_page(GFP_KERNEL);
  2679. if (lm_root == NULL)
  2680. return 1;
  2681. lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
  2682. vcpu->arch.mmu.lm_root = lm_root;
  2683. }
  2684. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
  2685. }
  2686. return 0;
  2687. }
  2688. static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
  2689. {
  2690. if (vcpu->arch.mmu.direct_map)
  2691. return mmu_alloc_direct_roots(vcpu);
  2692. else
  2693. return mmu_alloc_shadow_roots(vcpu);
  2694. }
  2695. static void mmu_sync_roots(struct kvm_vcpu *vcpu)
  2696. {
  2697. int i;
  2698. struct kvm_mmu_page *sp;
  2699. if (vcpu->arch.mmu.direct_map)
  2700. return;
  2701. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2702. return;
  2703. vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
  2704. kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
  2705. if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
  2706. hpa_t root = vcpu->arch.mmu.root_hpa;
  2707. sp = page_header(root);
  2708. mmu_sync_children(vcpu, sp);
  2709. kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
  2710. return;
  2711. }
  2712. for (i = 0; i < 4; ++i) {
  2713. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2714. if (root && VALID_PAGE(root)) {
  2715. root &= PT64_BASE_ADDR_MASK;
  2716. sp = page_header(root);
  2717. mmu_sync_children(vcpu, sp);
  2718. }
  2719. }
  2720. kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
  2721. }
  2722. void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
  2723. {
  2724. spin_lock(&vcpu->kvm->mmu_lock);
  2725. mmu_sync_roots(vcpu);
  2726. spin_unlock(&vcpu->kvm->mmu_lock);
  2727. }
  2728. EXPORT_SYMBOL_GPL(kvm_mmu_sync_roots);
  2729. static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
  2730. u32 access, struct x86_exception *exception)
  2731. {
  2732. if (exception)
  2733. exception->error_code = 0;
  2734. return vaddr;
  2735. }
  2736. static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
  2737. u32 access,
  2738. struct x86_exception *exception)
  2739. {
  2740. if (exception)
  2741. exception->error_code = 0;
  2742. return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception);
  2743. }
  2744. static bool
  2745. __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
  2746. {
  2747. int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f;
  2748. return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) |
  2749. ((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0);
  2750. }
  2751. static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
  2752. {
  2753. return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level);
  2754. }
  2755. static bool is_shadow_zero_bits_set(struct kvm_mmu *mmu, u64 spte, int level)
  2756. {
  2757. return __is_rsvd_bits_set(&mmu->shadow_zero_check, spte, level);
  2758. }
  2759. static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
  2760. {
  2761. if (direct)
  2762. return vcpu_match_mmio_gpa(vcpu, addr);
  2763. return vcpu_match_mmio_gva(vcpu, addr);
  2764. }
  2765. /* return true if reserved bit is detected on spte. */
  2766. static bool
  2767. walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
  2768. {
  2769. struct kvm_shadow_walk_iterator iterator;
  2770. u64 sptes[PT64_ROOT_LEVEL], spte = 0ull;
  2771. int root, leaf;
  2772. bool reserved = false;
  2773. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2774. goto exit;
  2775. walk_shadow_page_lockless_begin(vcpu);
  2776. for (shadow_walk_init(&iterator, vcpu, addr),
  2777. leaf = root = iterator.level;
  2778. shadow_walk_okay(&iterator);
  2779. __shadow_walk_next(&iterator, spte)) {
  2780. spte = mmu_spte_get_lockless(iterator.sptep);
  2781. sptes[leaf - 1] = spte;
  2782. leaf--;
  2783. if (!is_shadow_present_pte(spte))
  2784. break;
  2785. reserved |= is_shadow_zero_bits_set(&vcpu->arch.mmu, spte,
  2786. iterator.level);
  2787. }
  2788. walk_shadow_page_lockless_end(vcpu);
  2789. if (reserved) {
  2790. pr_err("%s: detect reserved bits on spte, addr 0x%llx, dump hierarchy:\n",
  2791. __func__, addr);
  2792. while (root > leaf) {
  2793. pr_err("------ spte 0x%llx level %d.\n",
  2794. sptes[root - 1], root);
  2795. root--;
  2796. }
  2797. }
  2798. exit:
  2799. *sptep = spte;
  2800. return reserved;
  2801. }
  2802. int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
  2803. {
  2804. u64 spte;
  2805. bool reserved;
  2806. if (mmio_info_in_cache(vcpu, addr, direct))
  2807. return RET_MMIO_PF_EMULATE;
  2808. reserved = walk_shadow_page_get_mmio_spte(vcpu, addr, &spte);
  2809. if (WARN_ON(reserved))
  2810. return RET_MMIO_PF_BUG;
  2811. if (is_mmio_spte(spte)) {
  2812. gfn_t gfn = get_mmio_spte_gfn(spte);
  2813. unsigned access = get_mmio_spte_access(spte);
  2814. if (!check_mmio_spte(vcpu, spte))
  2815. return RET_MMIO_PF_INVALID;
  2816. if (direct)
  2817. addr = 0;
  2818. trace_handle_mmio_page_fault(addr, gfn, access);
  2819. vcpu_cache_mmio_info(vcpu, addr, gfn, access);
  2820. return RET_MMIO_PF_EMULATE;
  2821. }
  2822. /*
  2823. * If the page table is zapped by other cpus, let CPU fault again on
  2824. * the address.
  2825. */
  2826. return RET_MMIO_PF_RETRY;
  2827. }
  2828. EXPORT_SYMBOL_GPL(handle_mmio_page_fault);
  2829. static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
  2830. u32 error_code, gfn_t gfn)
  2831. {
  2832. if (unlikely(error_code & PFERR_RSVD_MASK))
  2833. return false;
  2834. if (!(error_code & PFERR_PRESENT_MASK) ||
  2835. !(error_code & PFERR_WRITE_MASK))
  2836. return false;
  2837. /*
  2838. * guest is writing the page which is write tracked which can
  2839. * not be fixed by page fault handler.
  2840. */
  2841. if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
  2842. return true;
  2843. return false;
  2844. }
  2845. static void shadow_page_table_clear_flood(struct kvm_vcpu *vcpu, gva_t addr)
  2846. {
  2847. struct kvm_shadow_walk_iterator iterator;
  2848. u64 spte;
  2849. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2850. return;
  2851. walk_shadow_page_lockless_begin(vcpu);
  2852. for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
  2853. clear_sp_write_flooding_count(iterator.sptep);
  2854. if (!is_shadow_present_pte(spte))
  2855. break;
  2856. }
  2857. walk_shadow_page_lockless_end(vcpu);
  2858. }
  2859. static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
  2860. u32 error_code, bool prefault)
  2861. {
  2862. gfn_t gfn = gva >> PAGE_SHIFT;
  2863. int r;
  2864. pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
  2865. if (page_fault_handle_page_track(vcpu, error_code, gfn))
  2866. return 1;
  2867. r = mmu_topup_memory_caches(vcpu);
  2868. if (r)
  2869. return r;
  2870. MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2871. return nonpaging_map(vcpu, gva & PAGE_MASK,
  2872. error_code, gfn, prefault);
  2873. }
  2874. static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
  2875. {
  2876. struct kvm_arch_async_pf arch;
  2877. arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
  2878. arch.gfn = gfn;
  2879. arch.direct_map = vcpu->arch.mmu.direct_map;
  2880. arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
  2881. return kvm_setup_async_pf(vcpu, gva, kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch);
  2882. }
  2883. static bool can_do_async_pf(struct kvm_vcpu *vcpu)
  2884. {
  2885. if (unlikely(!lapic_in_kernel(vcpu) ||
  2886. kvm_event_needs_reinjection(vcpu)))
  2887. return false;
  2888. return kvm_x86_ops->interrupt_allowed(vcpu);
  2889. }
  2890. static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
  2891. gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable)
  2892. {
  2893. struct kvm_memory_slot *slot;
  2894. bool async;
  2895. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  2896. async = false;
  2897. *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, write, writable);
  2898. if (!async)
  2899. return false; /* *pfn has correct page already */
  2900. if (!prefault && can_do_async_pf(vcpu)) {
  2901. trace_kvm_try_async_get_page(gva, gfn);
  2902. if (kvm_find_async_pf_gfn(vcpu, gfn)) {
  2903. trace_kvm_async_pf_doublefault(gva, gfn);
  2904. kvm_make_request(KVM_REQ_APF_HALT, vcpu);
  2905. return true;
  2906. } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
  2907. return true;
  2908. }
  2909. *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL, write, writable);
  2910. return false;
  2911. }
  2912. static bool
  2913. check_hugepage_cache_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int level)
  2914. {
  2915. int page_num = KVM_PAGES_PER_HPAGE(level);
  2916. gfn &= ~(page_num - 1);
  2917. return kvm_mtrr_check_gfn_range_consistency(vcpu, gfn, page_num);
  2918. }
  2919. static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
  2920. bool prefault)
  2921. {
  2922. kvm_pfn_t pfn;
  2923. int r;
  2924. int level;
  2925. bool force_pt_level;
  2926. gfn_t gfn = gpa >> PAGE_SHIFT;
  2927. unsigned long mmu_seq;
  2928. int write = error_code & PFERR_WRITE_MASK;
  2929. bool map_writable;
  2930. MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2931. if (page_fault_handle_page_track(vcpu, error_code, gfn))
  2932. return 1;
  2933. r = mmu_topup_memory_caches(vcpu);
  2934. if (r)
  2935. return r;
  2936. force_pt_level = !check_hugepage_cache_consistency(vcpu, gfn,
  2937. PT_DIRECTORY_LEVEL);
  2938. level = mapping_level(vcpu, gfn, &force_pt_level);
  2939. if (likely(!force_pt_level)) {
  2940. if (level > PT_DIRECTORY_LEVEL &&
  2941. !check_hugepage_cache_consistency(vcpu, gfn, level))
  2942. level = PT_DIRECTORY_LEVEL;
  2943. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  2944. }
  2945. if (fast_page_fault(vcpu, gpa, level, error_code))
  2946. return 0;
  2947. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  2948. smp_rmb();
  2949. if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
  2950. return 0;
  2951. if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
  2952. return r;
  2953. spin_lock(&vcpu->kvm->mmu_lock);
  2954. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  2955. goto out_unlock;
  2956. make_mmu_pages_available(vcpu);
  2957. if (likely(!force_pt_level))
  2958. transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
  2959. r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
  2960. spin_unlock(&vcpu->kvm->mmu_lock);
  2961. return r;
  2962. out_unlock:
  2963. spin_unlock(&vcpu->kvm->mmu_lock);
  2964. kvm_release_pfn_clean(pfn);
  2965. return 0;
  2966. }
  2967. static void nonpaging_init_context(struct kvm_vcpu *vcpu,
  2968. struct kvm_mmu *context)
  2969. {
  2970. context->page_fault = nonpaging_page_fault;
  2971. context->gva_to_gpa = nonpaging_gva_to_gpa;
  2972. context->sync_page = nonpaging_sync_page;
  2973. context->invlpg = nonpaging_invlpg;
  2974. context->update_pte = nonpaging_update_pte;
  2975. context->root_level = 0;
  2976. context->shadow_root_level = PT32E_ROOT_LEVEL;
  2977. context->root_hpa = INVALID_PAGE;
  2978. context->direct_map = true;
  2979. context->nx = false;
  2980. }
  2981. void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu)
  2982. {
  2983. mmu_free_roots(vcpu);
  2984. }
  2985. static unsigned long get_cr3(struct kvm_vcpu *vcpu)
  2986. {
  2987. return kvm_read_cr3(vcpu);
  2988. }
  2989. static void inject_page_fault(struct kvm_vcpu *vcpu,
  2990. struct x86_exception *fault)
  2991. {
  2992. vcpu->arch.mmu.inject_page_fault(vcpu, fault);
  2993. }
  2994. static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
  2995. unsigned access, int *nr_present)
  2996. {
  2997. if (unlikely(is_mmio_spte(*sptep))) {
  2998. if (gfn != get_mmio_spte_gfn(*sptep)) {
  2999. mmu_spte_clear_no_track(sptep);
  3000. return true;
  3001. }
  3002. (*nr_present)++;
  3003. mark_mmio_spte(vcpu, sptep, gfn, access);
  3004. return true;
  3005. }
  3006. return false;
  3007. }
  3008. static inline bool is_last_gpte(struct kvm_mmu *mmu,
  3009. unsigned level, unsigned gpte)
  3010. {
  3011. /*
  3012. * PT_PAGE_TABLE_LEVEL always terminates. The RHS has bit 7 set
  3013. * iff level <= PT_PAGE_TABLE_LEVEL, which for our purpose means
  3014. * level == PT_PAGE_TABLE_LEVEL; set PT_PAGE_SIZE_MASK in gpte then.
  3015. */
  3016. gpte |= level - PT_PAGE_TABLE_LEVEL - 1;
  3017. /*
  3018. * The RHS has bit 7 set iff level < mmu->last_nonleaf_level.
  3019. * If it is clear, there are no large pages at this level, so clear
  3020. * PT_PAGE_SIZE_MASK in gpte if that is the case.
  3021. */
  3022. gpte &= level - mmu->last_nonleaf_level;
  3023. return gpte & PT_PAGE_SIZE_MASK;
  3024. }
  3025. #define PTTYPE_EPT 18 /* arbitrary */
  3026. #define PTTYPE PTTYPE_EPT
  3027. #include "paging_tmpl.h"
  3028. #undef PTTYPE
  3029. #define PTTYPE 64
  3030. #include "paging_tmpl.h"
  3031. #undef PTTYPE
  3032. #define PTTYPE 32
  3033. #include "paging_tmpl.h"
  3034. #undef PTTYPE
  3035. static void
  3036. __reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
  3037. struct rsvd_bits_validate *rsvd_check,
  3038. int maxphyaddr, int level, bool nx, bool gbpages,
  3039. bool pse, bool amd)
  3040. {
  3041. u64 exb_bit_rsvd = 0;
  3042. u64 gbpages_bit_rsvd = 0;
  3043. u64 nonleaf_bit8_rsvd = 0;
  3044. rsvd_check->bad_mt_xwr = 0;
  3045. if (!nx)
  3046. exb_bit_rsvd = rsvd_bits(63, 63);
  3047. if (!gbpages)
  3048. gbpages_bit_rsvd = rsvd_bits(7, 7);
  3049. /*
  3050. * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
  3051. * leaf entries) on AMD CPUs only.
  3052. */
  3053. if (amd)
  3054. nonleaf_bit8_rsvd = rsvd_bits(8, 8);
  3055. switch (level) {
  3056. case PT32_ROOT_LEVEL:
  3057. /* no rsvd bits for 2 level 4K page table entries */
  3058. rsvd_check->rsvd_bits_mask[0][1] = 0;
  3059. rsvd_check->rsvd_bits_mask[0][0] = 0;
  3060. rsvd_check->rsvd_bits_mask[1][0] =
  3061. rsvd_check->rsvd_bits_mask[0][0];
  3062. if (!pse) {
  3063. rsvd_check->rsvd_bits_mask[1][1] = 0;
  3064. break;
  3065. }
  3066. if (is_cpuid_PSE36())
  3067. /* 36bits PSE 4MB page */
  3068. rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
  3069. else
  3070. /* 32 bits PSE 4MB page */
  3071. rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
  3072. break;
  3073. case PT32E_ROOT_LEVEL:
  3074. rsvd_check->rsvd_bits_mask[0][2] =
  3075. rsvd_bits(maxphyaddr, 63) |
  3076. rsvd_bits(5, 8) | rsvd_bits(1, 2); /* PDPTE */
  3077. rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  3078. rsvd_bits(maxphyaddr, 62); /* PDE */
  3079. rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  3080. rsvd_bits(maxphyaddr, 62); /* PTE */
  3081. rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  3082. rsvd_bits(maxphyaddr, 62) |
  3083. rsvd_bits(13, 20); /* large page */
  3084. rsvd_check->rsvd_bits_mask[1][0] =
  3085. rsvd_check->rsvd_bits_mask[0][0];
  3086. break;
  3087. case PT64_ROOT_LEVEL:
  3088. rsvd_check->rsvd_bits_mask[0][3] = exb_bit_rsvd |
  3089. nonleaf_bit8_rsvd | rsvd_bits(7, 7) |
  3090. rsvd_bits(maxphyaddr, 51);
  3091. rsvd_check->rsvd_bits_mask[0][2] = exb_bit_rsvd |
  3092. nonleaf_bit8_rsvd | gbpages_bit_rsvd |
  3093. rsvd_bits(maxphyaddr, 51);
  3094. rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  3095. rsvd_bits(maxphyaddr, 51);
  3096. rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  3097. rsvd_bits(maxphyaddr, 51);
  3098. rsvd_check->rsvd_bits_mask[1][3] =
  3099. rsvd_check->rsvd_bits_mask[0][3];
  3100. rsvd_check->rsvd_bits_mask[1][2] = exb_bit_rsvd |
  3101. gbpages_bit_rsvd | rsvd_bits(maxphyaddr, 51) |
  3102. rsvd_bits(13, 29);
  3103. rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  3104. rsvd_bits(maxphyaddr, 51) |
  3105. rsvd_bits(13, 20); /* large page */
  3106. rsvd_check->rsvd_bits_mask[1][0] =
  3107. rsvd_check->rsvd_bits_mask[0][0];
  3108. break;
  3109. }
  3110. }
  3111. static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
  3112. struct kvm_mmu *context)
  3113. {
  3114. __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check,
  3115. cpuid_maxphyaddr(vcpu), context->root_level,
  3116. context->nx, guest_cpuid_has_gbpages(vcpu),
  3117. is_pse(vcpu), guest_cpuid_is_amd(vcpu));
  3118. }
  3119. static void
  3120. __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
  3121. int maxphyaddr, bool execonly)
  3122. {
  3123. u64 bad_mt_xwr;
  3124. rsvd_check->rsvd_bits_mask[0][3] =
  3125. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7);
  3126. rsvd_check->rsvd_bits_mask[0][2] =
  3127. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
  3128. rsvd_check->rsvd_bits_mask[0][1] =
  3129. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
  3130. rsvd_check->rsvd_bits_mask[0][0] = rsvd_bits(maxphyaddr, 51);
  3131. /* large page */
  3132. rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
  3133. rsvd_check->rsvd_bits_mask[1][2] =
  3134. rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 29);
  3135. rsvd_check->rsvd_bits_mask[1][1] =
  3136. rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 20);
  3137. rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
  3138. bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */
  3139. bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */
  3140. bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */
  3141. bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */
  3142. bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */
  3143. if (!execonly) {
  3144. /* bits 0..2 must not be 100 unless VMX capabilities allow it */
  3145. bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
  3146. }
  3147. rsvd_check->bad_mt_xwr = bad_mt_xwr;
  3148. }
  3149. static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
  3150. struct kvm_mmu *context, bool execonly)
  3151. {
  3152. __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
  3153. cpuid_maxphyaddr(vcpu), execonly);
  3154. }
  3155. /*
  3156. * the page table on host is the shadow page table for the page
  3157. * table in guest or amd nested guest, its mmu features completely
  3158. * follow the features in guest.
  3159. */
  3160. void
  3161. reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
  3162. {
  3163. bool uses_nx = context->nx || context->base_role.smep_andnot_wp;
  3164. /*
  3165. * Passing "true" to the last argument is okay; it adds a check
  3166. * on bit 8 of the SPTEs which KVM doesn't use anyway.
  3167. */
  3168. __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check,
  3169. boot_cpu_data.x86_phys_bits,
  3170. context->shadow_root_level, uses_nx,
  3171. guest_cpuid_has_gbpages(vcpu), is_pse(vcpu),
  3172. true);
  3173. }
  3174. EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask);
  3175. static inline bool boot_cpu_is_amd(void)
  3176. {
  3177. WARN_ON_ONCE(!tdp_enabled);
  3178. return shadow_x_mask == 0;
  3179. }
  3180. /*
  3181. * the direct page table on host, use as much mmu features as
  3182. * possible, however, kvm currently does not do execution-protection.
  3183. */
  3184. static void
  3185. reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
  3186. struct kvm_mmu *context)
  3187. {
  3188. if (boot_cpu_is_amd())
  3189. __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check,
  3190. boot_cpu_data.x86_phys_bits,
  3191. context->shadow_root_level, false,
  3192. boot_cpu_has(X86_FEATURE_GBPAGES),
  3193. true, true);
  3194. else
  3195. __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
  3196. boot_cpu_data.x86_phys_bits,
  3197. false);
  3198. }
  3199. /*
  3200. * as the comments in reset_shadow_zero_bits_mask() except it
  3201. * is the shadow page table for intel nested guest.
  3202. */
  3203. static void
  3204. reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
  3205. struct kvm_mmu *context, bool execonly)
  3206. {
  3207. __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
  3208. boot_cpu_data.x86_phys_bits, execonly);
  3209. }
  3210. static void update_permission_bitmask(struct kvm_vcpu *vcpu,
  3211. struct kvm_mmu *mmu, bool ept)
  3212. {
  3213. unsigned bit, byte, pfec;
  3214. u8 map;
  3215. bool fault, x, w, u, wf, uf, ff, smapf, cr4_smap, cr4_smep, smap = 0;
  3216. cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
  3217. cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
  3218. for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
  3219. pfec = byte << 1;
  3220. map = 0;
  3221. wf = pfec & PFERR_WRITE_MASK;
  3222. uf = pfec & PFERR_USER_MASK;
  3223. ff = pfec & PFERR_FETCH_MASK;
  3224. /*
  3225. * PFERR_RSVD_MASK bit is set in PFEC if the access is not
  3226. * subject to SMAP restrictions, and cleared otherwise. The
  3227. * bit is only meaningful if the SMAP bit is set in CR4.
  3228. */
  3229. smapf = !(pfec & PFERR_RSVD_MASK);
  3230. for (bit = 0; bit < 8; ++bit) {
  3231. x = bit & ACC_EXEC_MASK;
  3232. w = bit & ACC_WRITE_MASK;
  3233. u = bit & ACC_USER_MASK;
  3234. if (!ept) {
  3235. /* Not really needed: !nx will cause pte.nx to fault */
  3236. x |= !mmu->nx;
  3237. /* Allow supervisor writes if !cr0.wp */
  3238. w |= !is_write_protection(vcpu) && !uf;
  3239. /* Disallow supervisor fetches of user code if cr4.smep */
  3240. x &= !(cr4_smep && u && !uf);
  3241. /*
  3242. * SMAP:kernel-mode data accesses from user-mode
  3243. * mappings should fault. A fault is considered
  3244. * as a SMAP violation if all of the following
  3245. * conditions are ture:
  3246. * - X86_CR4_SMAP is set in CR4
  3247. * - An user page is accessed
  3248. * - Page fault in kernel mode
  3249. * - if CPL = 3 or X86_EFLAGS_AC is clear
  3250. *
  3251. * Here, we cover the first three conditions.
  3252. * The fourth is computed dynamically in
  3253. * permission_fault() and is in smapf.
  3254. *
  3255. * Also, SMAP does not affect instruction
  3256. * fetches, add the !ff check here to make it
  3257. * clearer.
  3258. */
  3259. smap = cr4_smap && u && !uf && !ff;
  3260. }
  3261. fault = (ff && !x) || (uf && !u) || (wf && !w) ||
  3262. (smapf && smap);
  3263. map |= fault << bit;
  3264. }
  3265. mmu->permissions[byte] = map;
  3266. }
  3267. }
  3268. /*
  3269. * PKU is an additional mechanism by which the paging controls access to
  3270. * user-mode addresses based on the value in the PKRU register. Protection
  3271. * key violations are reported through a bit in the page fault error code.
  3272. * Unlike other bits of the error code, the PK bit is not known at the
  3273. * call site of e.g. gva_to_gpa; it must be computed directly in
  3274. * permission_fault based on two bits of PKRU, on some machine state (CR4,
  3275. * CR0, EFER, CPL), and on other bits of the error code and the page tables.
  3276. *
  3277. * In particular the following conditions come from the error code, the
  3278. * page tables and the machine state:
  3279. * - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
  3280. * - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
  3281. * - PK is always zero if U=0 in the page tables
  3282. * - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
  3283. *
  3284. * The PKRU bitmask caches the result of these four conditions. The error
  3285. * code (minus the P bit) and the page table's U bit form an index into the
  3286. * PKRU bitmask. Two bits of the PKRU bitmask are then extracted and ANDed
  3287. * with the two bits of the PKRU register corresponding to the protection key.
  3288. * For the first three conditions above the bits will be 00, thus masking
  3289. * away both AD and WD. For all reads or if the last condition holds, WD
  3290. * only will be masked away.
  3291. */
  3292. static void update_pkru_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  3293. bool ept)
  3294. {
  3295. unsigned bit;
  3296. bool wp;
  3297. if (ept) {
  3298. mmu->pkru_mask = 0;
  3299. return;
  3300. }
  3301. /* PKEY is enabled only if CR4.PKE and EFER.LMA are both set. */
  3302. if (!kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || !is_long_mode(vcpu)) {
  3303. mmu->pkru_mask = 0;
  3304. return;
  3305. }
  3306. wp = is_write_protection(vcpu);
  3307. for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) {
  3308. unsigned pfec, pkey_bits;
  3309. bool check_pkey, check_write, ff, uf, wf, pte_user;
  3310. pfec = bit << 1;
  3311. ff = pfec & PFERR_FETCH_MASK;
  3312. uf = pfec & PFERR_USER_MASK;
  3313. wf = pfec & PFERR_WRITE_MASK;
  3314. /* PFEC.RSVD is replaced by ACC_USER_MASK. */
  3315. pte_user = pfec & PFERR_RSVD_MASK;
  3316. /*
  3317. * Only need to check the access which is not an
  3318. * instruction fetch and is to a user page.
  3319. */
  3320. check_pkey = (!ff && pte_user);
  3321. /*
  3322. * write access is controlled by PKRU if it is a
  3323. * user access or CR0.WP = 1.
  3324. */
  3325. check_write = check_pkey && wf && (uf || wp);
  3326. /* PKRU.AD stops both read and write access. */
  3327. pkey_bits = !!check_pkey;
  3328. /* PKRU.WD stops write access. */
  3329. pkey_bits |= (!!check_write) << 1;
  3330. mmu->pkru_mask |= (pkey_bits & 3) << pfec;
  3331. }
  3332. }
  3333. static void update_last_nonleaf_level(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
  3334. {
  3335. unsigned root_level = mmu->root_level;
  3336. mmu->last_nonleaf_level = root_level;
  3337. if (root_level == PT32_ROOT_LEVEL && is_pse(vcpu))
  3338. mmu->last_nonleaf_level++;
  3339. }
  3340. static void paging64_init_context_common(struct kvm_vcpu *vcpu,
  3341. struct kvm_mmu *context,
  3342. int level)
  3343. {
  3344. context->nx = is_nx(vcpu);
  3345. context->root_level = level;
  3346. reset_rsvds_bits_mask(vcpu, context);
  3347. update_permission_bitmask(vcpu, context, false);
  3348. update_pkru_bitmask(vcpu, context, false);
  3349. update_last_nonleaf_level(vcpu, context);
  3350. MMU_WARN_ON(!is_pae(vcpu));
  3351. context->page_fault = paging64_page_fault;
  3352. context->gva_to_gpa = paging64_gva_to_gpa;
  3353. context->sync_page = paging64_sync_page;
  3354. context->invlpg = paging64_invlpg;
  3355. context->update_pte = paging64_update_pte;
  3356. context->shadow_root_level = level;
  3357. context->root_hpa = INVALID_PAGE;
  3358. context->direct_map = false;
  3359. }
  3360. static void paging64_init_context(struct kvm_vcpu *vcpu,
  3361. struct kvm_mmu *context)
  3362. {
  3363. paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
  3364. }
  3365. static void paging32_init_context(struct kvm_vcpu *vcpu,
  3366. struct kvm_mmu *context)
  3367. {
  3368. context->nx = false;
  3369. context->root_level = PT32_ROOT_LEVEL;
  3370. reset_rsvds_bits_mask(vcpu, context);
  3371. update_permission_bitmask(vcpu, context, false);
  3372. update_pkru_bitmask(vcpu, context, false);
  3373. update_last_nonleaf_level(vcpu, context);
  3374. context->page_fault = paging32_page_fault;
  3375. context->gva_to_gpa = paging32_gva_to_gpa;
  3376. context->sync_page = paging32_sync_page;
  3377. context->invlpg = paging32_invlpg;
  3378. context->update_pte = paging32_update_pte;
  3379. context->shadow_root_level = PT32E_ROOT_LEVEL;
  3380. context->root_hpa = INVALID_PAGE;
  3381. context->direct_map = false;
  3382. }
  3383. static void paging32E_init_context(struct kvm_vcpu *vcpu,
  3384. struct kvm_mmu *context)
  3385. {
  3386. paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
  3387. }
  3388. static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
  3389. {
  3390. struct kvm_mmu *context = &vcpu->arch.mmu;
  3391. context->base_role.word = 0;
  3392. context->base_role.smm = is_smm(vcpu);
  3393. context->page_fault = tdp_page_fault;
  3394. context->sync_page = nonpaging_sync_page;
  3395. context->invlpg = nonpaging_invlpg;
  3396. context->update_pte = nonpaging_update_pte;
  3397. context->shadow_root_level = kvm_x86_ops->get_tdp_level();
  3398. context->root_hpa = INVALID_PAGE;
  3399. context->direct_map = true;
  3400. context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
  3401. context->get_cr3 = get_cr3;
  3402. context->get_pdptr = kvm_pdptr_read;
  3403. context->inject_page_fault = kvm_inject_page_fault;
  3404. if (!is_paging(vcpu)) {
  3405. context->nx = false;
  3406. context->gva_to_gpa = nonpaging_gva_to_gpa;
  3407. context->root_level = 0;
  3408. } else if (is_long_mode(vcpu)) {
  3409. context->nx = is_nx(vcpu);
  3410. context->root_level = PT64_ROOT_LEVEL;
  3411. reset_rsvds_bits_mask(vcpu, context);
  3412. context->gva_to_gpa = paging64_gva_to_gpa;
  3413. } else if (is_pae(vcpu)) {
  3414. context->nx = is_nx(vcpu);
  3415. context->root_level = PT32E_ROOT_LEVEL;
  3416. reset_rsvds_bits_mask(vcpu, context);
  3417. context->gva_to_gpa = paging64_gva_to_gpa;
  3418. } else {
  3419. context->nx = false;
  3420. context->root_level = PT32_ROOT_LEVEL;
  3421. reset_rsvds_bits_mask(vcpu, context);
  3422. context->gva_to_gpa = paging32_gva_to_gpa;
  3423. }
  3424. update_permission_bitmask(vcpu, context, false);
  3425. update_pkru_bitmask(vcpu, context, false);
  3426. update_last_nonleaf_level(vcpu, context);
  3427. reset_tdp_shadow_zero_bits_mask(vcpu, context);
  3428. }
  3429. void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)
  3430. {
  3431. bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
  3432. bool smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
  3433. struct kvm_mmu *context = &vcpu->arch.mmu;
  3434. MMU_WARN_ON(VALID_PAGE(context->root_hpa));
  3435. if (!is_paging(vcpu))
  3436. nonpaging_init_context(vcpu, context);
  3437. else if (is_long_mode(vcpu))
  3438. paging64_init_context(vcpu, context);
  3439. else if (is_pae(vcpu))
  3440. paging32E_init_context(vcpu, context);
  3441. else
  3442. paging32_init_context(vcpu, context);
  3443. context->base_role.nxe = is_nx(vcpu);
  3444. context->base_role.cr4_pae = !!is_pae(vcpu);
  3445. context->base_role.cr0_wp = is_write_protection(vcpu);
  3446. context->base_role.smep_andnot_wp
  3447. = smep && !is_write_protection(vcpu);
  3448. context->base_role.smap_andnot_wp
  3449. = smap && !is_write_protection(vcpu);
  3450. context->base_role.smm = is_smm(vcpu);
  3451. reset_shadow_zero_bits_mask(vcpu, context);
  3452. }
  3453. EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
  3454. void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly)
  3455. {
  3456. struct kvm_mmu *context = &vcpu->arch.mmu;
  3457. MMU_WARN_ON(VALID_PAGE(context->root_hpa));
  3458. context->shadow_root_level = kvm_x86_ops->get_tdp_level();
  3459. context->nx = true;
  3460. context->page_fault = ept_page_fault;
  3461. context->gva_to_gpa = ept_gva_to_gpa;
  3462. context->sync_page = ept_sync_page;
  3463. context->invlpg = ept_invlpg;
  3464. context->update_pte = ept_update_pte;
  3465. context->root_level = context->shadow_root_level;
  3466. context->root_hpa = INVALID_PAGE;
  3467. context->direct_map = false;
  3468. update_permission_bitmask(vcpu, context, true);
  3469. update_pkru_bitmask(vcpu, context, true);
  3470. reset_rsvds_bits_mask_ept(vcpu, context, execonly);
  3471. reset_ept_shadow_zero_bits_mask(vcpu, context, execonly);
  3472. }
  3473. EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
  3474. static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
  3475. {
  3476. struct kvm_mmu *context = &vcpu->arch.mmu;
  3477. kvm_init_shadow_mmu(vcpu);
  3478. context->set_cr3 = kvm_x86_ops->set_cr3;
  3479. context->get_cr3 = get_cr3;
  3480. context->get_pdptr = kvm_pdptr_read;
  3481. context->inject_page_fault = kvm_inject_page_fault;
  3482. }
  3483. static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
  3484. {
  3485. struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
  3486. g_context->get_cr3 = get_cr3;
  3487. g_context->get_pdptr = kvm_pdptr_read;
  3488. g_context->inject_page_fault = kvm_inject_page_fault;
  3489. /*
  3490. * Note that arch.mmu.gva_to_gpa translates l2_gpa to l1_gpa using
  3491. * L1's nested page tables (e.g. EPT12). The nested translation
  3492. * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
  3493. * L2's page tables as the first level of translation and L1's
  3494. * nested page tables as the second level of translation. Basically
  3495. * the gva_to_gpa functions between mmu and nested_mmu are swapped.
  3496. */
  3497. if (!is_paging(vcpu)) {
  3498. g_context->nx = false;
  3499. g_context->root_level = 0;
  3500. g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
  3501. } else if (is_long_mode(vcpu)) {
  3502. g_context->nx = is_nx(vcpu);
  3503. g_context->root_level = PT64_ROOT_LEVEL;
  3504. reset_rsvds_bits_mask(vcpu, g_context);
  3505. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  3506. } else if (is_pae(vcpu)) {
  3507. g_context->nx = is_nx(vcpu);
  3508. g_context->root_level = PT32E_ROOT_LEVEL;
  3509. reset_rsvds_bits_mask(vcpu, g_context);
  3510. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  3511. } else {
  3512. g_context->nx = false;
  3513. g_context->root_level = PT32_ROOT_LEVEL;
  3514. reset_rsvds_bits_mask(vcpu, g_context);
  3515. g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
  3516. }
  3517. update_permission_bitmask(vcpu, g_context, false);
  3518. update_pkru_bitmask(vcpu, g_context, false);
  3519. update_last_nonleaf_level(vcpu, g_context);
  3520. }
  3521. static void init_kvm_mmu(struct kvm_vcpu *vcpu)
  3522. {
  3523. if (mmu_is_nested(vcpu))
  3524. init_kvm_nested_mmu(vcpu);
  3525. else if (tdp_enabled)
  3526. init_kvm_tdp_mmu(vcpu);
  3527. else
  3528. init_kvm_softmmu(vcpu);
  3529. }
  3530. void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
  3531. {
  3532. kvm_mmu_unload(vcpu);
  3533. init_kvm_mmu(vcpu);
  3534. }
  3535. EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
  3536. int kvm_mmu_load(struct kvm_vcpu *vcpu)
  3537. {
  3538. int r;
  3539. r = mmu_topup_memory_caches(vcpu);
  3540. if (r)
  3541. goto out;
  3542. r = mmu_alloc_roots(vcpu);
  3543. kvm_mmu_sync_roots(vcpu);
  3544. if (r)
  3545. goto out;
  3546. /* set_cr3() should ensure TLB has been flushed */
  3547. vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
  3548. out:
  3549. return r;
  3550. }
  3551. EXPORT_SYMBOL_GPL(kvm_mmu_load);
  3552. void kvm_mmu_unload(struct kvm_vcpu *vcpu)
  3553. {
  3554. mmu_free_roots(vcpu);
  3555. WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3556. }
  3557. EXPORT_SYMBOL_GPL(kvm_mmu_unload);
  3558. static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
  3559. struct kvm_mmu_page *sp, u64 *spte,
  3560. const void *new)
  3561. {
  3562. if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
  3563. ++vcpu->kvm->stat.mmu_pde_zapped;
  3564. return;
  3565. }
  3566. ++vcpu->kvm->stat.mmu_pte_updated;
  3567. vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
  3568. }
  3569. static bool need_remote_flush(u64 old, u64 new)
  3570. {
  3571. if (!is_shadow_present_pte(old))
  3572. return false;
  3573. if (!is_shadow_present_pte(new))
  3574. return true;
  3575. if ((old ^ new) & PT64_BASE_ADDR_MASK)
  3576. return true;
  3577. old ^= shadow_nx_mask;
  3578. new ^= shadow_nx_mask;
  3579. return (old & ~new & PT64_PERM_MASK) != 0;
  3580. }
  3581. static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
  3582. const u8 *new, int *bytes)
  3583. {
  3584. u64 gentry;
  3585. int r;
  3586. /*
  3587. * Assume that the pte write on a page table of the same type
  3588. * as the current vcpu paging mode since we update the sptes only
  3589. * when they have the same mode.
  3590. */
  3591. if (is_pae(vcpu) && *bytes == 4) {
  3592. /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
  3593. *gpa &= ~(gpa_t)7;
  3594. *bytes = 8;
  3595. r = kvm_vcpu_read_guest(vcpu, *gpa, &gentry, 8);
  3596. if (r)
  3597. gentry = 0;
  3598. new = (const u8 *)&gentry;
  3599. }
  3600. switch (*bytes) {
  3601. case 4:
  3602. gentry = *(const u32 *)new;
  3603. break;
  3604. case 8:
  3605. gentry = *(const u64 *)new;
  3606. break;
  3607. default:
  3608. gentry = 0;
  3609. break;
  3610. }
  3611. return gentry;
  3612. }
  3613. /*
  3614. * If we're seeing too many writes to a page, it may no longer be a page table,
  3615. * or we may be forking, in which case it is better to unmap the page.
  3616. */
  3617. static bool detect_write_flooding(struct kvm_mmu_page *sp)
  3618. {
  3619. /*
  3620. * Skip write-flooding detected for the sp whose level is 1, because
  3621. * it can become unsync, then the guest page is not write-protected.
  3622. */
  3623. if (sp->role.level == PT_PAGE_TABLE_LEVEL)
  3624. return false;
  3625. atomic_inc(&sp->write_flooding_count);
  3626. return atomic_read(&sp->write_flooding_count) >= 3;
  3627. }
  3628. /*
  3629. * Misaligned accesses are too much trouble to fix up; also, they usually
  3630. * indicate a page is not used as a page table.
  3631. */
  3632. static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
  3633. int bytes)
  3634. {
  3635. unsigned offset, pte_size, misaligned;
  3636. pgprintk("misaligned: gpa %llx bytes %d role %x\n",
  3637. gpa, bytes, sp->role.word);
  3638. offset = offset_in_page(gpa);
  3639. pte_size = sp->role.cr4_pae ? 8 : 4;
  3640. /*
  3641. * Sometimes, the OS only writes the last one bytes to update status
  3642. * bits, for example, in linux, andb instruction is used in clear_bit().
  3643. */
  3644. if (!(offset & (pte_size - 1)) && bytes == 1)
  3645. return false;
  3646. misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
  3647. misaligned |= bytes < 4;
  3648. return misaligned;
  3649. }
  3650. static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
  3651. {
  3652. unsigned page_offset, quadrant;
  3653. u64 *spte;
  3654. int level;
  3655. page_offset = offset_in_page(gpa);
  3656. level = sp->role.level;
  3657. *nspte = 1;
  3658. if (!sp->role.cr4_pae) {
  3659. page_offset <<= 1; /* 32->64 */
  3660. /*
  3661. * A 32-bit pde maps 4MB while the shadow pdes map
  3662. * only 2MB. So we need to double the offset again
  3663. * and zap two pdes instead of one.
  3664. */
  3665. if (level == PT32_ROOT_LEVEL) {
  3666. page_offset &= ~7; /* kill rounding error */
  3667. page_offset <<= 1;
  3668. *nspte = 2;
  3669. }
  3670. quadrant = page_offset >> PAGE_SHIFT;
  3671. page_offset &= ~PAGE_MASK;
  3672. if (quadrant != sp->role.quadrant)
  3673. return NULL;
  3674. }
  3675. spte = &sp->spt[page_offset / sizeof(*spte)];
  3676. return spte;
  3677. }
  3678. static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  3679. const u8 *new, int bytes)
  3680. {
  3681. gfn_t gfn = gpa >> PAGE_SHIFT;
  3682. struct kvm_mmu_page *sp;
  3683. LIST_HEAD(invalid_list);
  3684. u64 entry, gentry, *spte;
  3685. int npte;
  3686. bool remote_flush, local_flush;
  3687. union kvm_mmu_page_role mask = { };
  3688. mask.cr0_wp = 1;
  3689. mask.cr4_pae = 1;
  3690. mask.nxe = 1;
  3691. mask.smep_andnot_wp = 1;
  3692. mask.smap_andnot_wp = 1;
  3693. mask.smm = 1;
  3694. /*
  3695. * If we don't have indirect shadow pages, it means no page is
  3696. * write-protected, so we can exit simply.
  3697. */
  3698. if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
  3699. return;
  3700. remote_flush = local_flush = false;
  3701. pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
  3702. gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes);
  3703. /*
  3704. * No need to care whether allocation memory is successful
  3705. * or not since pte prefetch is skiped if it does not have
  3706. * enough objects in the cache.
  3707. */
  3708. mmu_topup_memory_caches(vcpu);
  3709. spin_lock(&vcpu->kvm->mmu_lock);
  3710. ++vcpu->kvm->stat.mmu_pte_write;
  3711. kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
  3712. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
  3713. if (detect_write_misaligned(sp, gpa, bytes) ||
  3714. detect_write_flooding(sp)) {
  3715. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
  3716. ++vcpu->kvm->stat.mmu_flooded;
  3717. continue;
  3718. }
  3719. spte = get_written_sptes(sp, gpa, &npte);
  3720. if (!spte)
  3721. continue;
  3722. local_flush = true;
  3723. while (npte--) {
  3724. entry = *spte;
  3725. mmu_page_zap_pte(vcpu->kvm, sp, spte);
  3726. if (gentry &&
  3727. !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
  3728. & mask.word) && rmap_can_add(vcpu))
  3729. mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
  3730. if (need_remote_flush(entry, *spte))
  3731. remote_flush = true;
  3732. ++spte;
  3733. }
  3734. }
  3735. kvm_mmu_flush_or_zap(vcpu, &invalid_list, remote_flush, local_flush);
  3736. kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
  3737. spin_unlock(&vcpu->kvm->mmu_lock);
  3738. }
  3739. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
  3740. {
  3741. gpa_t gpa;
  3742. int r;
  3743. if (vcpu->arch.mmu.direct_map)
  3744. return 0;
  3745. gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
  3746. r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3747. return r;
  3748. }
  3749. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
  3750. static void make_mmu_pages_available(struct kvm_vcpu *vcpu)
  3751. {
  3752. LIST_HEAD(invalid_list);
  3753. if (likely(kvm_mmu_available_pages(vcpu->kvm) >= KVM_MIN_FREE_MMU_PAGES))
  3754. return;
  3755. while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES) {
  3756. if (!prepare_zap_oldest_mmu_page(vcpu->kvm, &invalid_list))
  3757. break;
  3758. ++vcpu->kvm->stat.mmu_recycled;
  3759. }
  3760. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  3761. }
  3762. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
  3763. void *insn, int insn_len)
  3764. {
  3765. int r, emulation_type = EMULTYPE_RETRY;
  3766. enum emulation_result er;
  3767. bool direct = vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu);
  3768. if (unlikely(error_code & PFERR_RSVD_MASK)) {
  3769. r = handle_mmio_page_fault(vcpu, cr2, direct);
  3770. if (r == RET_MMIO_PF_EMULATE) {
  3771. emulation_type = 0;
  3772. goto emulate;
  3773. }
  3774. if (r == RET_MMIO_PF_RETRY)
  3775. return 1;
  3776. if (r < 0)
  3777. return r;
  3778. }
  3779. r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
  3780. if (r < 0)
  3781. return r;
  3782. if (!r)
  3783. return 1;
  3784. if (mmio_info_in_cache(vcpu, cr2, direct))
  3785. emulation_type = 0;
  3786. emulate:
  3787. er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
  3788. switch (er) {
  3789. case EMULATE_DONE:
  3790. return 1;
  3791. case EMULATE_USER_EXIT:
  3792. ++vcpu->stat.mmio_exits;
  3793. /* fall through */
  3794. case EMULATE_FAIL:
  3795. return 0;
  3796. default:
  3797. BUG();
  3798. }
  3799. }
  3800. EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
  3801. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  3802. {
  3803. vcpu->arch.mmu.invlpg(vcpu, gva);
  3804. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  3805. ++vcpu->stat.invlpg;
  3806. }
  3807. EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
  3808. void kvm_enable_tdp(void)
  3809. {
  3810. tdp_enabled = true;
  3811. }
  3812. EXPORT_SYMBOL_GPL(kvm_enable_tdp);
  3813. void kvm_disable_tdp(void)
  3814. {
  3815. tdp_enabled = false;
  3816. }
  3817. EXPORT_SYMBOL_GPL(kvm_disable_tdp);
  3818. static void free_mmu_pages(struct kvm_vcpu *vcpu)
  3819. {
  3820. free_page((unsigned long)vcpu->arch.mmu.pae_root);
  3821. if (vcpu->arch.mmu.lm_root != NULL)
  3822. free_page((unsigned long)vcpu->arch.mmu.lm_root);
  3823. }
  3824. static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
  3825. {
  3826. struct page *page;
  3827. int i;
  3828. /*
  3829. * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
  3830. * Therefore we need to allocate shadow page tables in the first
  3831. * 4GB of memory, which happens to fit the DMA32 zone.
  3832. */
  3833. page = alloc_page(GFP_KERNEL | __GFP_DMA32);
  3834. if (!page)
  3835. return -ENOMEM;
  3836. vcpu->arch.mmu.pae_root = page_address(page);
  3837. for (i = 0; i < 4; ++i)
  3838. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  3839. return 0;
  3840. }
  3841. int kvm_mmu_create(struct kvm_vcpu *vcpu)
  3842. {
  3843. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  3844. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  3845. vcpu->arch.mmu.translate_gpa = translate_gpa;
  3846. vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
  3847. return alloc_mmu_pages(vcpu);
  3848. }
  3849. void kvm_mmu_setup(struct kvm_vcpu *vcpu)
  3850. {
  3851. MMU_WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3852. init_kvm_mmu(vcpu);
  3853. }
  3854. void kvm_mmu_init_vm(struct kvm *kvm)
  3855. {
  3856. struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
  3857. node->track_write = kvm_mmu_pte_write;
  3858. kvm_page_track_register_notifier(kvm, node);
  3859. }
  3860. void kvm_mmu_uninit_vm(struct kvm *kvm)
  3861. {
  3862. struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
  3863. kvm_page_track_unregister_notifier(kvm, node);
  3864. }
  3865. /* The return value indicates if tlb flush on all vcpus is needed. */
  3866. typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head);
  3867. /* The caller should hold mmu-lock before calling this function. */
  3868. static bool
  3869. slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3870. slot_level_handler fn, int start_level, int end_level,
  3871. gfn_t start_gfn, gfn_t end_gfn, bool lock_flush_tlb)
  3872. {
  3873. struct slot_rmap_walk_iterator iterator;
  3874. bool flush = false;
  3875. for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn,
  3876. end_gfn, &iterator) {
  3877. if (iterator.rmap)
  3878. flush |= fn(kvm, iterator.rmap);
  3879. if (need_resched() || spin_needbreak(&kvm->mmu_lock)) {
  3880. if (flush && lock_flush_tlb) {
  3881. kvm_flush_remote_tlbs(kvm);
  3882. flush = false;
  3883. }
  3884. cond_resched_lock(&kvm->mmu_lock);
  3885. }
  3886. }
  3887. if (flush && lock_flush_tlb) {
  3888. kvm_flush_remote_tlbs(kvm);
  3889. flush = false;
  3890. }
  3891. return flush;
  3892. }
  3893. static bool
  3894. slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3895. slot_level_handler fn, int start_level, int end_level,
  3896. bool lock_flush_tlb)
  3897. {
  3898. return slot_handle_level_range(kvm, memslot, fn, start_level,
  3899. end_level, memslot->base_gfn,
  3900. memslot->base_gfn + memslot->npages - 1,
  3901. lock_flush_tlb);
  3902. }
  3903. static bool
  3904. slot_handle_all_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3905. slot_level_handler fn, bool lock_flush_tlb)
  3906. {
  3907. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
  3908. PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
  3909. }
  3910. static bool
  3911. slot_handle_large_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3912. slot_level_handler fn, bool lock_flush_tlb)
  3913. {
  3914. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL + 1,
  3915. PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
  3916. }
  3917. static bool
  3918. slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3919. slot_level_handler fn, bool lock_flush_tlb)
  3920. {
  3921. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
  3922. PT_PAGE_TABLE_LEVEL, lock_flush_tlb);
  3923. }
  3924. void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
  3925. {
  3926. struct kvm_memslots *slots;
  3927. struct kvm_memory_slot *memslot;
  3928. int i;
  3929. spin_lock(&kvm->mmu_lock);
  3930. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  3931. slots = __kvm_memslots(kvm, i);
  3932. kvm_for_each_memslot(memslot, slots) {
  3933. gfn_t start, end;
  3934. start = max(gfn_start, memslot->base_gfn);
  3935. end = min(gfn_end, memslot->base_gfn + memslot->npages);
  3936. if (start >= end)
  3937. continue;
  3938. slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
  3939. PT_PAGE_TABLE_LEVEL, PT_MAX_HUGEPAGE_LEVEL,
  3940. start, end - 1, true);
  3941. }
  3942. }
  3943. spin_unlock(&kvm->mmu_lock);
  3944. }
  3945. static bool slot_rmap_write_protect(struct kvm *kvm,
  3946. struct kvm_rmap_head *rmap_head)
  3947. {
  3948. return __rmap_write_protect(kvm, rmap_head, false);
  3949. }
  3950. void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
  3951. struct kvm_memory_slot *memslot)
  3952. {
  3953. bool flush;
  3954. spin_lock(&kvm->mmu_lock);
  3955. flush = slot_handle_all_level(kvm, memslot, slot_rmap_write_protect,
  3956. false);
  3957. spin_unlock(&kvm->mmu_lock);
  3958. /*
  3959. * kvm_mmu_slot_remove_write_access() and kvm_vm_ioctl_get_dirty_log()
  3960. * which do tlb flush out of mmu-lock should be serialized by
  3961. * kvm->slots_lock otherwise tlb flush would be missed.
  3962. */
  3963. lockdep_assert_held(&kvm->slots_lock);
  3964. /*
  3965. * We can flush all the TLBs out of the mmu lock without TLB
  3966. * corruption since we just change the spte from writable to
  3967. * readonly so that we only need to care the case of changing
  3968. * spte from present to present (changing the spte from present
  3969. * to nonpresent will flush all the TLBs immediately), in other
  3970. * words, the only case we care is mmu_spte_update() where we
  3971. * haved checked SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE
  3972. * instead of PT_WRITABLE_MASK, that means it does not depend
  3973. * on PT_WRITABLE_MASK anymore.
  3974. */
  3975. if (flush)
  3976. kvm_flush_remote_tlbs(kvm);
  3977. }
  3978. static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
  3979. struct kvm_rmap_head *rmap_head)
  3980. {
  3981. u64 *sptep;
  3982. struct rmap_iterator iter;
  3983. int need_tlb_flush = 0;
  3984. kvm_pfn_t pfn;
  3985. struct kvm_mmu_page *sp;
  3986. restart:
  3987. for_each_rmap_spte(rmap_head, &iter, sptep) {
  3988. sp = page_header(__pa(sptep));
  3989. pfn = spte_to_pfn(*sptep);
  3990. /*
  3991. * We cannot do huge page mapping for indirect shadow pages,
  3992. * which are found on the last rmap (level = 1) when not using
  3993. * tdp; such shadow pages are synced with the page table in
  3994. * the guest, and the guest page table is using 4K page size
  3995. * mapping if the indirect sp has level = 1.
  3996. */
  3997. if (sp->role.direct &&
  3998. !kvm_is_reserved_pfn(pfn) &&
  3999. PageTransCompoundMap(pfn_to_page(pfn))) {
  4000. drop_spte(kvm, sptep);
  4001. need_tlb_flush = 1;
  4002. goto restart;
  4003. }
  4004. }
  4005. return need_tlb_flush;
  4006. }
  4007. void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
  4008. const struct kvm_memory_slot *memslot)
  4009. {
  4010. /* FIXME: const-ify all uses of struct kvm_memory_slot. */
  4011. spin_lock(&kvm->mmu_lock);
  4012. slot_handle_leaf(kvm, (struct kvm_memory_slot *)memslot,
  4013. kvm_mmu_zap_collapsible_spte, true);
  4014. spin_unlock(&kvm->mmu_lock);
  4015. }
  4016. void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
  4017. struct kvm_memory_slot *memslot)
  4018. {
  4019. bool flush;
  4020. spin_lock(&kvm->mmu_lock);
  4021. flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false);
  4022. spin_unlock(&kvm->mmu_lock);
  4023. lockdep_assert_held(&kvm->slots_lock);
  4024. /*
  4025. * It's also safe to flush TLBs out of mmu lock here as currently this
  4026. * function is only used for dirty logging, in which case flushing TLB
  4027. * out of mmu lock also guarantees no dirty pages will be lost in
  4028. * dirty_bitmap.
  4029. */
  4030. if (flush)
  4031. kvm_flush_remote_tlbs(kvm);
  4032. }
  4033. EXPORT_SYMBOL_GPL(kvm_mmu_slot_leaf_clear_dirty);
  4034. void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,
  4035. struct kvm_memory_slot *memslot)
  4036. {
  4037. bool flush;
  4038. spin_lock(&kvm->mmu_lock);
  4039. flush = slot_handle_large_level(kvm, memslot, slot_rmap_write_protect,
  4040. false);
  4041. spin_unlock(&kvm->mmu_lock);
  4042. /* see kvm_mmu_slot_remove_write_access */
  4043. lockdep_assert_held(&kvm->slots_lock);
  4044. if (flush)
  4045. kvm_flush_remote_tlbs(kvm);
  4046. }
  4047. EXPORT_SYMBOL_GPL(kvm_mmu_slot_largepage_remove_write_access);
  4048. void kvm_mmu_slot_set_dirty(struct kvm *kvm,
  4049. struct kvm_memory_slot *memslot)
  4050. {
  4051. bool flush;
  4052. spin_lock(&kvm->mmu_lock);
  4053. flush = slot_handle_all_level(kvm, memslot, __rmap_set_dirty, false);
  4054. spin_unlock(&kvm->mmu_lock);
  4055. lockdep_assert_held(&kvm->slots_lock);
  4056. /* see kvm_mmu_slot_leaf_clear_dirty */
  4057. if (flush)
  4058. kvm_flush_remote_tlbs(kvm);
  4059. }
  4060. EXPORT_SYMBOL_GPL(kvm_mmu_slot_set_dirty);
  4061. #define BATCH_ZAP_PAGES 10
  4062. static void kvm_zap_obsolete_pages(struct kvm *kvm)
  4063. {
  4064. struct kvm_mmu_page *sp, *node;
  4065. int batch = 0;
  4066. restart:
  4067. list_for_each_entry_safe_reverse(sp, node,
  4068. &kvm->arch.active_mmu_pages, link) {
  4069. int ret;
  4070. /*
  4071. * No obsolete page exists before new created page since
  4072. * active_mmu_pages is the FIFO list.
  4073. */
  4074. if (!is_obsolete_sp(kvm, sp))
  4075. break;
  4076. /*
  4077. * Since we are reversely walking the list and the invalid
  4078. * list will be moved to the head, skip the invalid page
  4079. * can help us to avoid the infinity list walking.
  4080. */
  4081. if (sp->role.invalid)
  4082. continue;
  4083. /*
  4084. * Need not flush tlb since we only zap the sp with invalid
  4085. * generation number.
  4086. */
  4087. if (batch >= BATCH_ZAP_PAGES &&
  4088. cond_resched_lock(&kvm->mmu_lock)) {
  4089. batch = 0;
  4090. goto restart;
  4091. }
  4092. ret = kvm_mmu_prepare_zap_page(kvm, sp,
  4093. &kvm->arch.zapped_obsolete_pages);
  4094. batch += ret;
  4095. if (ret)
  4096. goto restart;
  4097. }
  4098. /*
  4099. * Should flush tlb before free page tables since lockless-walking
  4100. * may use the pages.
  4101. */
  4102. kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
  4103. }
  4104. /*
  4105. * Fast invalidate all shadow pages and use lock-break technique
  4106. * to zap obsolete pages.
  4107. *
  4108. * It's required when memslot is being deleted or VM is being
  4109. * destroyed, in these cases, we should ensure that KVM MMU does
  4110. * not use any resource of the being-deleted slot or all slots
  4111. * after calling the function.
  4112. */
  4113. void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm)
  4114. {
  4115. spin_lock(&kvm->mmu_lock);
  4116. trace_kvm_mmu_invalidate_zap_all_pages(kvm);
  4117. kvm->arch.mmu_valid_gen++;
  4118. /*
  4119. * Notify all vcpus to reload its shadow page table
  4120. * and flush TLB. Then all vcpus will switch to new
  4121. * shadow page table with the new mmu_valid_gen.
  4122. *
  4123. * Note: we should do this under the protection of
  4124. * mmu-lock, otherwise, vcpu would purge shadow page
  4125. * but miss tlb flush.
  4126. */
  4127. kvm_reload_remote_mmus(kvm);
  4128. kvm_zap_obsolete_pages(kvm);
  4129. spin_unlock(&kvm->mmu_lock);
  4130. }
  4131. static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
  4132. {
  4133. return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
  4134. }
  4135. void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, struct kvm_memslots *slots)
  4136. {
  4137. /*
  4138. * The very rare case: if the generation-number is round,
  4139. * zap all shadow pages.
  4140. */
  4141. if (unlikely((slots->generation & MMIO_GEN_MASK) == 0)) {
  4142. printk_ratelimited(KERN_DEBUG "kvm: zapping shadow pages for mmio generation wraparound\n");
  4143. kvm_mmu_invalidate_zap_all_pages(kvm);
  4144. }
  4145. }
  4146. static unsigned long
  4147. mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  4148. {
  4149. struct kvm *kvm;
  4150. int nr_to_scan = sc->nr_to_scan;
  4151. unsigned long freed = 0;
  4152. spin_lock(&kvm_lock);
  4153. list_for_each_entry(kvm, &vm_list, vm_list) {
  4154. int idx;
  4155. LIST_HEAD(invalid_list);
  4156. /*
  4157. * Never scan more than sc->nr_to_scan VM instances.
  4158. * Will not hit this condition practically since we do not try
  4159. * to shrink more than one VM and it is very unlikely to see
  4160. * !n_used_mmu_pages so many times.
  4161. */
  4162. if (!nr_to_scan--)
  4163. break;
  4164. /*
  4165. * n_used_mmu_pages is accessed without holding kvm->mmu_lock
  4166. * here. We may skip a VM instance errorneosly, but we do not
  4167. * want to shrink a VM that only started to populate its MMU
  4168. * anyway.
  4169. */
  4170. if (!kvm->arch.n_used_mmu_pages &&
  4171. !kvm_has_zapped_obsolete_pages(kvm))
  4172. continue;
  4173. idx = srcu_read_lock(&kvm->srcu);
  4174. spin_lock(&kvm->mmu_lock);
  4175. if (kvm_has_zapped_obsolete_pages(kvm)) {
  4176. kvm_mmu_commit_zap_page(kvm,
  4177. &kvm->arch.zapped_obsolete_pages);
  4178. goto unlock;
  4179. }
  4180. if (prepare_zap_oldest_mmu_page(kvm, &invalid_list))
  4181. freed++;
  4182. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  4183. unlock:
  4184. spin_unlock(&kvm->mmu_lock);
  4185. srcu_read_unlock(&kvm->srcu, idx);
  4186. /*
  4187. * unfair on small ones
  4188. * per-vm shrinkers cry out
  4189. * sadness comes quickly
  4190. */
  4191. list_move_tail(&kvm->vm_list, &vm_list);
  4192. break;
  4193. }
  4194. spin_unlock(&kvm_lock);
  4195. return freed;
  4196. }
  4197. static unsigned long
  4198. mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  4199. {
  4200. return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
  4201. }
  4202. static struct shrinker mmu_shrinker = {
  4203. .count_objects = mmu_shrink_count,
  4204. .scan_objects = mmu_shrink_scan,
  4205. .seeks = DEFAULT_SEEKS * 10,
  4206. };
  4207. static void mmu_destroy_caches(void)
  4208. {
  4209. if (pte_list_desc_cache)
  4210. kmem_cache_destroy(pte_list_desc_cache);
  4211. if (mmu_page_header_cache)
  4212. kmem_cache_destroy(mmu_page_header_cache);
  4213. }
  4214. int kvm_mmu_module_init(void)
  4215. {
  4216. pte_list_desc_cache = kmem_cache_create("pte_list_desc",
  4217. sizeof(struct pte_list_desc),
  4218. 0, 0, NULL);
  4219. if (!pte_list_desc_cache)
  4220. goto nomem;
  4221. mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
  4222. sizeof(struct kvm_mmu_page),
  4223. 0, 0, NULL);
  4224. if (!mmu_page_header_cache)
  4225. goto nomem;
  4226. if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL))
  4227. goto nomem;
  4228. register_shrinker(&mmu_shrinker);
  4229. return 0;
  4230. nomem:
  4231. mmu_destroy_caches();
  4232. return -ENOMEM;
  4233. }
  4234. /*
  4235. * Caculate mmu pages needed for kvm.
  4236. */
  4237. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
  4238. {
  4239. unsigned int nr_mmu_pages;
  4240. unsigned int nr_pages = 0;
  4241. struct kvm_memslots *slots;
  4242. struct kvm_memory_slot *memslot;
  4243. int i;
  4244. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  4245. slots = __kvm_memslots(kvm, i);
  4246. kvm_for_each_memslot(memslot, slots)
  4247. nr_pages += memslot->npages;
  4248. }
  4249. nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
  4250. nr_mmu_pages = max(nr_mmu_pages,
  4251. (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
  4252. return nr_mmu_pages;
  4253. }
  4254. void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
  4255. {
  4256. kvm_mmu_unload(vcpu);
  4257. free_mmu_pages(vcpu);
  4258. mmu_free_memory_caches(vcpu);
  4259. }
  4260. void kvm_mmu_module_exit(void)
  4261. {
  4262. mmu_destroy_caches();
  4263. percpu_counter_destroy(&kvm_total_used_mmu_pages);
  4264. unregister_shrinker(&mmu_shrinker);
  4265. mmu_audit_disable();
  4266. }