mmu.c 130 KB

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