mmu.c 139 KB

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