mmu.c 136 KB

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