mmu.c 139 KB

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