mmu.c 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116
  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_ACCOUNT);
  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. /*
  1378. * We are holding the kvm->mmu_lock, and we are blowing up
  1379. * shadow PTEs. MMU notifier consumers need to be kept at bay.
  1380. * This is correct as long as we don't decouple the mmu_lock
  1381. * protected regions (like invalidate_range_start|end does).
  1382. */
  1383. kvm->mmu_notifier_seq++;
  1384. return kvm_handle_hva_range(kvm, start, end, 0,
  1385. kvm_unmap_rmapp);
  1386. }
  1387. return kvm_handle_hva_range(kvm, start, end, 0, kvm_age_rmapp);
  1388. }
  1389. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  1390. {
  1391. return kvm_handle_hva(kvm, hva, 0, kvm_test_age_rmapp);
  1392. }
  1393. #ifdef MMU_DEBUG
  1394. static int is_empty_shadow_page(u64 *spt)
  1395. {
  1396. u64 *pos;
  1397. u64 *end;
  1398. for (pos = spt, end = pos + PAGE_SIZE / sizeof(u64); pos != end; pos++)
  1399. if (is_shadow_present_pte(*pos)) {
  1400. printk(KERN_ERR "%s: %p %llx\n", __func__,
  1401. pos, *pos);
  1402. return 0;
  1403. }
  1404. return 1;
  1405. }
  1406. #endif
  1407. /*
  1408. * This value is the sum of all of the kvm instances's
  1409. * kvm->arch.n_used_mmu_pages values. We need a global,
  1410. * aggregate version in order to make the slab shrinker
  1411. * faster
  1412. */
  1413. static inline void kvm_mod_used_mmu_pages(struct kvm *kvm, int nr)
  1414. {
  1415. kvm->arch.n_used_mmu_pages += nr;
  1416. percpu_counter_add(&kvm_total_used_mmu_pages, nr);
  1417. }
  1418. static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
  1419. {
  1420. MMU_WARN_ON(!is_empty_shadow_page(sp->spt));
  1421. hlist_del(&sp->hash_link);
  1422. list_del(&sp->link);
  1423. free_page((unsigned long)sp->spt);
  1424. if (!sp->role.direct)
  1425. free_page((unsigned long)sp->gfns);
  1426. kmem_cache_free(mmu_page_header_cache, sp);
  1427. }
  1428. static unsigned kvm_page_table_hashfn(gfn_t gfn)
  1429. {
  1430. return gfn & ((1 << KVM_MMU_HASH_SHIFT) - 1);
  1431. }
  1432. static void mmu_page_add_parent_pte(struct kvm_vcpu *vcpu,
  1433. struct kvm_mmu_page *sp, u64 *parent_pte)
  1434. {
  1435. if (!parent_pte)
  1436. return;
  1437. pte_list_add(vcpu, parent_pte, &sp->parent_ptes);
  1438. }
  1439. static void mmu_page_remove_parent_pte(struct kvm_mmu_page *sp,
  1440. u64 *parent_pte)
  1441. {
  1442. pte_list_remove(parent_pte, &sp->parent_ptes);
  1443. }
  1444. static void drop_parent_pte(struct kvm_mmu_page *sp,
  1445. u64 *parent_pte)
  1446. {
  1447. mmu_page_remove_parent_pte(sp, parent_pte);
  1448. mmu_spte_clear_no_track(parent_pte);
  1449. }
  1450. static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, int direct)
  1451. {
  1452. struct kvm_mmu_page *sp;
  1453. sp = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
  1454. sp->spt = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
  1455. if (!direct)
  1456. sp->gfns = mmu_memory_cache_alloc(&vcpu->arch.mmu_page_cache);
  1457. set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
  1458. /*
  1459. * The active_mmu_pages list is the FIFO list, do not move the
  1460. * page until it is zapped. kvm_zap_obsolete_pages depends on
  1461. * this feature. See the comments in kvm_zap_obsolete_pages().
  1462. */
  1463. list_add(&sp->link, &vcpu->kvm->arch.active_mmu_pages);
  1464. kvm_mod_used_mmu_pages(vcpu->kvm, +1);
  1465. return sp;
  1466. }
  1467. static void mark_unsync(u64 *spte);
  1468. static void kvm_mmu_mark_parents_unsync(struct kvm_mmu_page *sp)
  1469. {
  1470. u64 *sptep;
  1471. struct rmap_iterator iter;
  1472. for_each_rmap_spte(&sp->parent_ptes, &iter, sptep) {
  1473. mark_unsync(sptep);
  1474. }
  1475. }
  1476. static void mark_unsync(u64 *spte)
  1477. {
  1478. struct kvm_mmu_page *sp;
  1479. unsigned int index;
  1480. sp = page_header(__pa(spte));
  1481. index = spte - sp->spt;
  1482. if (__test_and_set_bit(index, sp->unsync_child_bitmap))
  1483. return;
  1484. if (sp->unsync_children++)
  1485. return;
  1486. kvm_mmu_mark_parents_unsync(sp);
  1487. }
  1488. static int nonpaging_sync_page(struct kvm_vcpu *vcpu,
  1489. struct kvm_mmu_page *sp)
  1490. {
  1491. return 0;
  1492. }
  1493. static void nonpaging_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  1494. {
  1495. }
  1496. static void nonpaging_update_pte(struct kvm_vcpu *vcpu,
  1497. struct kvm_mmu_page *sp, u64 *spte,
  1498. const void *pte)
  1499. {
  1500. WARN_ON(1);
  1501. }
  1502. #define KVM_PAGE_ARRAY_NR 16
  1503. struct kvm_mmu_pages {
  1504. struct mmu_page_and_offset {
  1505. struct kvm_mmu_page *sp;
  1506. unsigned int idx;
  1507. } page[KVM_PAGE_ARRAY_NR];
  1508. unsigned int nr;
  1509. };
  1510. static int mmu_pages_add(struct kvm_mmu_pages *pvec, struct kvm_mmu_page *sp,
  1511. int idx)
  1512. {
  1513. int i;
  1514. if (sp->unsync)
  1515. for (i=0; i < pvec->nr; i++)
  1516. if (pvec->page[i].sp == sp)
  1517. return 0;
  1518. pvec->page[pvec->nr].sp = sp;
  1519. pvec->page[pvec->nr].idx = idx;
  1520. pvec->nr++;
  1521. return (pvec->nr == KVM_PAGE_ARRAY_NR);
  1522. }
  1523. static inline void clear_unsync_child_bit(struct kvm_mmu_page *sp, int idx)
  1524. {
  1525. --sp->unsync_children;
  1526. WARN_ON((int)sp->unsync_children < 0);
  1527. __clear_bit(idx, sp->unsync_child_bitmap);
  1528. }
  1529. static int __mmu_unsync_walk(struct kvm_mmu_page *sp,
  1530. struct kvm_mmu_pages *pvec)
  1531. {
  1532. int i, ret, nr_unsync_leaf = 0;
  1533. for_each_set_bit(i, sp->unsync_child_bitmap, 512) {
  1534. struct kvm_mmu_page *child;
  1535. u64 ent = sp->spt[i];
  1536. if (!is_shadow_present_pte(ent) || is_large_pte(ent)) {
  1537. clear_unsync_child_bit(sp, i);
  1538. continue;
  1539. }
  1540. child = page_header(ent & PT64_BASE_ADDR_MASK);
  1541. if (child->unsync_children) {
  1542. if (mmu_pages_add(pvec, child, i))
  1543. return -ENOSPC;
  1544. ret = __mmu_unsync_walk(child, pvec);
  1545. if (!ret) {
  1546. clear_unsync_child_bit(sp, i);
  1547. continue;
  1548. } else if (ret > 0) {
  1549. nr_unsync_leaf += ret;
  1550. } else
  1551. return ret;
  1552. } else if (child->unsync) {
  1553. nr_unsync_leaf++;
  1554. if (mmu_pages_add(pvec, child, i))
  1555. return -ENOSPC;
  1556. } else
  1557. clear_unsync_child_bit(sp, i);
  1558. }
  1559. return nr_unsync_leaf;
  1560. }
  1561. #define INVALID_INDEX (-1)
  1562. static int mmu_unsync_walk(struct kvm_mmu_page *sp,
  1563. struct kvm_mmu_pages *pvec)
  1564. {
  1565. pvec->nr = 0;
  1566. if (!sp->unsync_children)
  1567. return 0;
  1568. mmu_pages_add(pvec, sp, INVALID_INDEX);
  1569. return __mmu_unsync_walk(sp, pvec);
  1570. }
  1571. static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
  1572. {
  1573. WARN_ON(!sp->unsync);
  1574. trace_kvm_mmu_sync_page(sp);
  1575. sp->unsync = 0;
  1576. --kvm->stat.mmu_unsync;
  1577. }
  1578. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1579. struct list_head *invalid_list);
  1580. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1581. struct list_head *invalid_list);
  1582. /*
  1583. * NOTE: we should pay more attention on the zapped-obsolete page
  1584. * (is_obsolete_sp(sp) && sp->role.invalid) when you do hash list walk
  1585. * since it has been deleted from active_mmu_pages but still can be found
  1586. * at hast list.
  1587. *
  1588. * for_each_gfn_valid_sp() has skipped that kind of pages.
  1589. */
  1590. #define for_each_gfn_valid_sp(_kvm, _sp, _gfn) \
  1591. hlist_for_each_entry(_sp, \
  1592. &(_kvm)->arch.mmu_page_hash[kvm_page_table_hashfn(_gfn)], hash_link) \
  1593. if ((_sp)->gfn != (_gfn) || is_obsolete_sp((_kvm), (_sp)) \
  1594. || (_sp)->role.invalid) {} else
  1595. #define for_each_gfn_indirect_valid_sp(_kvm, _sp, _gfn) \
  1596. for_each_gfn_valid_sp(_kvm, _sp, _gfn) \
  1597. if ((_sp)->role.direct) {} else
  1598. /* @sp->gfn should be write-protected at the call site */
  1599. static bool __kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1600. struct list_head *invalid_list)
  1601. {
  1602. if (sp->role.cr4_pae != !!is_pae(vcpu)) {
  1603. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1604. return false;
  1605. }
  1606. if (vcpu->arch.mmu.sync_page(vcpu, sp) == 0) {
  1607. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, invalid_list);
  1608. return false;
  1609. }
  1610. return true;
  1611. }
  1612. static void kvm_mmu_flush_or_zap(struct kvm_vcpu *vcpu,
  1613. struct list_head *invalid_list,
  1614. bool remote_flush, bool local_flush)
  1615. {
  1616. if (!list_empty(invalid_list)) {
  1617. kvm_mmu_commit_zap_page(vcpu->kvm, invalid_list);
  1618. return;
  1619. }
  1620. if (remote_flush)
  1621. kvm_flush_remote_tlbs(vcpu->kvm);
  1622. else if (local_flush)
  1623. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1624. }
  1625. #ifdef CONFIG_KVM_MMU_AUDIT
  1626. #include "mmu_audit.c"
  1627. #else
  1628. static void kvm_mmu_audit(struct kvm_vcpu *vcpu, int point) { }
  1629. static void mmu_audit_disable(void) { }
  1630. #endif
  1631. static bool is_obsolete_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
  1632. {
  1633. return unlikely(sp->mmu_valid_gen != kvm->arch.mmu_valid_gen);
  1634. }
  1635. static bool kvm_sync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  1636. struct list_head *invalid_list)
  1637. {
  1638. kvm_unlink_unsync_page(vcpu->kvm, sp);
  1639. return __kvm_sync_page(vcpu, sp, invalid_list);
  1640. }
  1641. /* @gfn should be write-protected at the call site */
  1642. static bool kvm_sync_pages(struct kvm_vcpu *vcpu, gfn_t gfn,
  1643. struct list_head *invalid_list)
  1644. {
  1645. struct kvm_mmu_page *s;
  1646. bool ret = false;
  1647. for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
  1648. if (!s->unsync)
  1649. continue;
  1650. WARN_ON(s->role.level != PT_PAGE_TABLE_LEVEL);
  1651. ret |= kvm_sync_page(vcpu, s, invalid_list);
  1652. }
  1653. return ret;
  1654. }
  1655. struct mmu_page_path {
  1656. struct kvm_mmu_page *parent[PT64_ROOT_LEVEL];
  1657. unsigned int idx[PT64_ROOT_LEVEL];
  1658. };
  1659. #define for_each_sp(pvec, sp, parents, i) \
  1660. for (i = mmu_pages_first(&pvec, &parents); \
  1661. i < pvec.nr && ({ sp = pvec.page[i].sp; 1;}); \
  1662. i = mmu_pages_next(&pvec, &parents, i))
  1663. static int mmu_pages_next(struct kvm_mmu_pages *pvec,
  1664. struct mmu_page_path *parents,
  1665. int i)
  1666. {
  1667. int n;
  1668. for (n = i+1; n < pvec->nr; n++) {
  1669. struct kvm_mmu_page *sp = pvec->page[n].sp;
  1670. unsigned idx = pvec->page[n].idx;
  1671. int level = sp->role.level;
  1672. parents->idx[level-1] = idx;
  1673. if (level == PT_PAGE_TABLE_LEVEL)
  1674. break;
  1675. parents->parent[level-2] = sp;
  1676. }
  1677. return n;
  1678. }
  1679. static int mmu_pages_first(struct kvm_mmu_pages *pvec,
  1680. struct mmu_page_path *parents)
  1681. {
  1682. struct kvm_mmu_page *sp;
  1683. int level;
  1684. if (pvec->nr == 0)
  1685. return 0;
  1686. WARN_ON(pvec->page[0].idx != INVALID_INDEX);
  1687. sp = pvec->page[0].sp;
  1688. level = sp->role.level;
  1689. WARN_ON(level == PT_PAGE_TABLE_LEVEL);
  1690. parents->parent[level-2] = sp;
  1691. /* Also set up a sentinel. Further entries in pvec are all
  1692. * children of sp, so this element is never overwritten.
  1693. */
  1694. parents->parent[level-1] = NULL;
  1695. return mmu_pages_next(pvec, parents, 0);
  1696. }
  1697. static void mmu_pages_clear_parents(struct mmu_page_path *parents)
  1698. {
  1699. struct kvm_mmu_page *sp;
  1700. unsigned int level = 0;
  1701. do {
  1702. unsigned int idx = parents->idx[level];
  1703. sp = parents->parent[level];
  1704. if (!sp)
  1705. return;
  1706. WARN_ON(idx == INVALID_INDEX);
  1707. clear_unsync_child_bit(sp, idx);
  1708. level++;
  1709. } while (!sp->unsync_children);
  1710. }
  1711. static void mmu_sync_children(struct kvm_vcpu *vcpu,
  1712. struct kvm_mmu_page *parent)
  1713. {
  1714. int i;
  1715. struct kvm_mmu_page *sp;
  1716. struct mmu_page_path parents;
  1717. struct kvm_mmu_pages pages;
  1718. LIST_HEAD(invalid_list);
  1719. bool flush = false;
  1720. while (mmu_unsync_walk(parent, &pages)) {
  1721. bool protected = false;
  1722. for_each_sp(pages, sp, parents, i)
  1723. protected |= rmap_write_protect(vcpu, sp->gfn);
  1724. if (protected) {
  1725. kvm_flush_remote_tlbs(vcpu->kvm);
  1726. flush = false;
  1727. }
  1728. for_each_sp(pages, sp, parents, i) {
  1729. flush |= kvm_sync_page(vcpu, sp, &invalid_list);
  1730. mmu_pages_clear_parents(&parents);
  1731. }
  1732. if (need_resched() || spin_needbreak(&vcpu->kvm->mmu_lock)) {
  1733. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1734. cond_resched_lock(&vcpu->kvm->mmu_lock);
  1735. flush = false;
  1736. }
  1737. }
  1738. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1739. }
  1740. static void __clear_sp_write_flooding_count(struct kvm_mmu_page *sp)
  1741. {
  1742. atomic_set(&sp->write_flooding_count, 0);
  1743. }
  1744. static void clear_sp_write_flooding_count(u64 *spte)
  1745. {
  1746. struct kvm_mmu_page *sp = page_header(__pa(spte));
  1747. __clear_sp_write_flooding_count(sp);
  1748. }
  1749. static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
  1750. gfn_t gfn,
  1751. gva_t gaddr,
  1752. unsigned level,
  1753. int direct,
  1754. unsigned access)
  1755. {
  1756. union kvm_mmu_page_role role;
  1757. unsigned quadrant;
  1758. struct kvm_mmu_page *sp;
  1759. bool need_sync = false;
  1760. bool flush = false;
  1761. LIST_HEAD(invalid_list);
  1762. role = vcpu->arch.mmu.base_role;
  1763. role.level = level;
  1764. role.direct = direct;
  1765. if (role.direct)
  1766. role.cr4_pae = 0;
  1767. role.access = access;
  1768. if (!vcpu->arch.mmu.direct_map
  1769. && vcpu->arch.mmu.root_level <= PT32_ROOT_LEVEL) {
  1770. quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
  1771. quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1;
  1772. role.quadrant = quadrant;
  1773. }
  1774. for_each_gfn_valid_sp(vcpu->kvm, sp, gfn) {
  1775. if (!need_sync && sp->unsync)
  1776. need_sync = true;
  1777. if (sp->role.word != role.word)
  1778. continue;
  1779. if (sp->unsync) {
  1780. /* The page is good, but __kvm_sync_page might still end
  1781. * up zapping it. If so, break in order to rebuild it.
  1782. */
  1783. if (!__kvm_sync_page(vcpu, sp, &invalid_list))
  1784. break;
  1785. WARN_ON(!list_empty(&invalid_list));
  1786. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  1787. }
  1788. if (sp->unsync_children)
  1789. kvm_make_request(KVM_REQ_MMU_SYNC, vcpu);
  1790. __clear_sp_write_flooding_count(sp);
  1791. trace_kvm_mmu_get_page(sp, false);
  1792. return sp;
  1793. }
  1794. ++vcpu->kvm->stat.mmu_cache_miss;
  1795. sp = kvm_mmu_alloc_page(vcpu, direct);
  1796. sp->gfn = gfn;
  1797. sp->role = role;
  1798. hlist_add_head(&sp->hash_link,
  1799. &vcpu->kvm->arch.mmu_page_hash[kvm_page_table_hashfn(gfn)]);
  1800. if (!direct) {
  1801. /*
  1802. * we should do write protection before syncing pages
  1803. * otherwise the content of the synced shadow page may
  1804. * be inconsistent with guest page table.
  1805. */
  1806. account_shadowed(vcpu->kvm, sp);
  1807. if (level == PT_PAGE_TABLE_LEVEL &&
  1808. rmap_write_protect(vcpu, gfn))
  1809. kvm_flush_remote_tlbs(vcpu->kvm);
  1810. if (level > PT_PAGE_TABLE_LEVEL && need_sync)
  1811. flush |= kvm_sync_pages(vcpu, gfn, &invalid_list);
  1812. }
  1813. sp->mmu_valid_gen = vcpu->kvm->arch.mmu_valid_gen;
  1814. clear_page(sp->spt);
  1815. trace_kvm_mmu_get_page(sp, true);
  1816. kvm_mmu_flush_or_zap(vcpu, &invalid_list, false, flush);
  1817. return sp;
  1818. }
  1819. static void shadow_walk_init(struct kvm_shadow_walk_iterator *iterator,
  1820. struct kvm_vcpu *vcpu, u64 addr)
  1821. {
  1822. iterator->addr = addr;
  1823. iterator->shadow_addr = vcpu->arch.mmu.root_hpa;
  1824. iterator->level = vcpu->arch.mmu.shadow_root_level;
  1825. if (iterator->level == PT64_ROOT_LEVEL &&
  1826. vcpu->arch.mmu.root_level < PT64_ROOT_LEVEL &&
  1827. !vcpu->arch.mmu.direct_map)
  1828. --iterator->level;
  1829. if (iterator->level == PT32E_ROOT_LEVEL) {
  1830. iterator->shadow_addr
  1831. = vcpu->arch.mmu.pae_root[(addr >> 30) & 3];
  1832. iterator->shadow_addr &= PT64_BASE_ADDR_MASK;
  1833. --iterator->level;
  1834. if (!iterator->shadow_addr)
  1835. iterator->level = 0;
  1836. }
  1837. }
  1838. static bool shadow_walk_okay(struct kvm_shadow_walk_iterator *iterator)
  1839. {
  1840. if (iterator->level < PT_PAGE_TABLE_LEVEL)
  1841. return false;
  1842. iterator->index = SHADOW_PT_INDEX(iterator->addr, iterator->level);
  1843. iterator->sptep = ((u64 *)__va(iterator->shadow_addr)) + iterator->index;
  1844. return true;
  1845. }
  1846. static void __shadow_walk_next(struct kvm_shadow_walk_iterator *iterator,
  1847. u64 spte)
  1848. {
  1849. if (is_last_spte(spte, iterator->level)) {
  1850. iterator->level = 0;
  1851. return;
  1852. }
  1853. iterator->shadow_addr = spte & PT64_BASE_ADDR_MASK;
  1854. --iterator->level;
  1855. }
  1856. static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
  1857. {
  1858. return __shadow_walk_next(iterator, *iterator->sptep);
  1859. }
  1860. static void link_shadow_page(struct kvm_vcpu *vcpu, u64 *sptep,
  1861. struct kvm_mmu_page *sp)
  1862. {
  1863. u64 spte;
  1864. BUILD_BUG_ON(VMX_EPT_WRITABLE_MASK != PT_WRITABLE_MASK);
  1865. spte = __pa(sp->spt) | shadow_present_mask | PT_WRITABLE_MASK |
  1866. shadow_user_mask | shadow_x_mask | shadow_accessed_mask;
  1867. mmu_spte_set(sptep, spte);
  1868. mmu_page_add_parent_pte(vcpu, sp, sptep);
  1869. if (sp->unsync_children || sp->unsync)
  1870. mark_unsync(sptep);
  1871. }
  1872. static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  1873. unsigned direct_access)
  1874. {
  1875. if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
  1876. struct kvm_mmu_page *child;
  1877. /*
  1878. * For the direct sp, if the guest pte's dirty bit
  1879. * changed form clean to dirty, it will corrupt the
  1880. * sp's access: allow writable in the read-only sp,
  1881. * so we should update the spte at this point to get
  1882. * a new sp with the correct access.
  1883. */
  1884. child = page_header(*sptep & PT64_BASE_ADDR_MASK);
  1885. if (child->role.access == direct_access)
  1886. return;
  1887. drop_parent_pte(child, sptep);
  1888. kvm_flush_remote_tlbs(vcpu->kvm);
  1889. }
  1890. }
  1891. static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp,
  1892. u64 *spte)
  1893. {
  1894. u64 pte;
  1895. struct kvm_mmu_page *child;
  1896. pte = *spte;
  1897. if (is_shadow_present_pte(pte)) {
  1898. if (is_last_spte(pte, sp->role.level)) {
  1899. drop_spte(kvm, spte);
  1900. if (is_large_pte(pte))
  1901. --kvm->stat.lpages;
  1902. } else {
  1903. child = page_header(pte & PT64_BASE_ADDR_MASK);
  1904. drop_parent_pte(child, spte);
  1905. }
  1906. return true;
  1907. }
  1908. if (is_mmio_spte(pte))
  1909. mmu_spte_clear_no_track(spte);
  1910. return false;
  1911. }
  1912. static void kvm_mmu_page_unlink_children(struct kvm *kvm,
  1913. struct kvm_mmu_page *sp)
  1914. {
  1915. unsigned i;
  1916. for (i = 0; i < PT64_ENT_PER_PAGE; ++i)
  1917. mmu_page_zap_pte(kvm, sp, sp->spt + i);
  1918. }
  1919. static void kvm_mmu_unlink_parents(struct kvm *kvm, struct kvm_mmu_page *sp)
  1920. {
  1921. u64 *sptep;
  1922. struct rmap_iterator iter;
  1923. while ((sptep = rmap_get_first(&sp->parent_ptes, &iter)))
  1924. drop_parent_pte(sp, sptep);
  1925. }
  1926. static int mmu_zap_unsync_children(struct kvm *kvm,
  1927. struct kvm_mmu_page *parent,
  1928. struct list_head *invalid_list)
  1929. {
  1930. int i, zapped = 0;
  1931. struct mmu_page_path parents;
  1932. struct kvm_mmu_pages pages;
  1933. if (parent->role.level == PT_PAGE_TABLE_LEVEL)
  1934. return 0;
  1935. while (mmu_unsync_walk(parent, &pages)) {
  1936. struct kvm_mmu_page *sp;
  1937. for_each_sp(pages, sp, parents, i) {
  1938. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  1939. mmu_pages_clear_parents(&parents);
  1940. zapped++;
  1941. }
  1942. }
  1943. return zapped;
  1944. }
  1945. static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
  1946. struct list_head *invalid_list)
  1947. {
  1948. int ret;
  1949. trace_kvm_mmu_prepare_zap_page(sp);
  1950. ++kvm->stat.mmu_shadow_zapped;
  1951. ret = mmu_zap_unsync_children(kvm, sp, invalid_list);
  1952. kvm_mmu_page_unlink_children(kvm, sp);
  1953. kvm_mmu_unlink_parents(kvm, sp);
  1954. if (!sp->role.invalid && !sp->role.direct)
  1955. unaccount_shadowed(kvm, sp);
  1956. if (sp->unsync)
  1957. kvm_unlink_unsync_page(kvm, sp);
  1958. if (!sp->root_count) {
  1959. /* Count self */
  1960. ret++;
  1961. list_move(&sp->link, invalid_list);
  1962. kvm_mod_used_mmu_pages(kvm, -1);
  1963. } else {
  1964. list_move(&sp->link, &kvm->arch.active_mmu_pages);
  1965. /*
  1966. * The obsolete pages can not be used on any vcpus.
  1967. * See the comments in kvm_mmu_invalidate_zap_all_pages().
  1968. */
  1969. if (!sp->role.invalid && !is_obsolete_sp(kvm, sp))
  1970. kvm_reload_remote_mmus(kvm);
  1971. }
  1972. sp->role.invalid = 1;
  1973. return ret;
  1974. }
  1975. static void kvm_mmu_commit_zap_page(struct kvm *kvm,
  1976. struct list_head *invalid_list)
  1977. {
  1978. struct kvm_mmu_page *sp, *nsp;
  1979. if (list_empty(invalid_list))
  1980. return;
  1981. /*
  1982. * We need to make sure everyone sees our modifications to
  1983. * the page tables and see changes to vcpu->mode here. The barrier
  1984. * in the kvm_flush_remote_tlbs() achieves this. This pairs
  1985. * with vcpu_enter_guest and walk_shadow_page_lockless_begin/end.
  1986. *
  1987. * In addition, kvm_flush_remote_tlbs waits for all vcpus to exit
  1988. * guest mode and/or lockless shadow page table walks.
  1989. */
  1990. kvm_flush_remote_tlbs(kvm);
  1991. list_for_each_entry_safe(sp, nsp, invalid_list, link) {
  1992. WARN_ON(!sp->role.invalid || sp->root_count);
  1993. kvm_mmu_free_page(sp);
  1994. }
  1995. }
  1996. static bool prepare_zap_oldest_mmu_page(struct kvm *kvm,
  1997. struct list_head *invalid_list)
  1998. {
  1999. struct kvm_mmu_page *sp;
  2000. if (list_empty(&kvm->arch.active_mmu_pages))
  2001. return false;
  2002. sp = list_last_entry(&kvm->arch.active_mmu_pages,
  2003. struct kvm_mmu_page, link);
  2004. kvm_mmu_prepare_zap_page(kvm, sp, invalid_list);
  2005. return true;
  2006. }
  2007. /*
  2008. * Changing the number of mmu pages allocated to the vm
  2009. * Note: if goal_nr_mmu_pages is too small, you will get dead lock
  2010. */
  2011. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int goal_nr_mmu_pages)
  2012. {
  2013. LIST_HEAD(invalid_list);
  2014. spin_lock(&kvm->mmu_lock);
  2015. if (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages) {
  2016. /* Need to free some mmu pages to achieve the goal. */
  2017. while (kvm->arch.n_used_mmu_pages > goal_nr_mmu_pages)
  2018. if (!prepare_zap_oldest_mmu_page(kvm, &invalid_list))
  2019. break;
  2020. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2021. goal_nr_mmu_pages = kvm->arch.n_used_mmu_pages;
  2022. }
  2023. kvm->arch.n_max_mmu_pages = goal_nr_mmu_pages;
  2024. spin_unlock(&kvm->mmu_lock);
  2025. }
  2026. int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
  2027. {
  2028. struct kvm_mmu_page *sp;
  2029. LIST_HEAD(invalid_list);
  2030. int r;
  2031. pgprintk("%s: looking for gfn %llx\n", __func__, gfn);
  2032. r = 0;
  2033. spin_lock(&kvm->mmu_lock);
  2034. for_each_gfn_indirect_valid_sp(kvm, sp, gfn) {
  2035. pgprintk("%s: gfn %llx role %x\n", __func__, gfn,
  2036. sp->role.word);
  2037. r = 1;
  2038. kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list);
  2039. }
  2040. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  2041. spin_unlock(&kvm->mmu_lock);
  2042. return r;
  2043. }
  2044. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page);
  2045. static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  2046. {
  2047. trace_kvm_mmu_unsync_page(sp);
  2048. ++vcpu->kvm->stat.mmu_unsync;
  2049. sp->unsync = 1;
  2050. kvm_mmu_mark_parents_unsync(sp);
  2051. }
  2052. static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
  2053. bool can_unsync)
  2054. {
  2055. struct kvm_mmu_page *sp;
  2056. if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
  2057. return true;
  2058. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
  2059. if (!can_unsync)
  2060. return true;
  2061. if (sp->unsync)
  2062. continue;
  2063. WARN_ON(sp->role.level != PT_PAGE_TABLE_LEVEL);
  2064. kvm_unsync_page(vcpu, sp);
  2065. }
  2066. return false;
  2067. }
  2068. static bool kvm_is_mmio_pfn(kvm_pfn_t pfn)
  2069. {
  2070. if (pfn_valid(pfn))
  2071. return !is_zero_pfn(pfn) && PageReserved(pfn_to_page(pfn));
  2072. return true;
  2073. }
  2074. static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep,
  2075. unsigned pte_access, int level,
  2076. gfn_t gfn, kvm_pfn_t pfn, bool speculative,
  2077. bool can_unsync, bool host_writable)
  2078. {
  2079. u64 spte = 0;
  2080. int ret = 0;
  2081. if (set_mmio_spte(vcpu, sptep, gfn, pfn, pte_access))
  2082. return 0;
  2083. /*
  2084. * For the EPT case, shadow_present_mask is 0 if hardware
  2085. * supports exec-only page table entries. In that case,
  2086. * ACC_USER_MASK and shadow_user_mask are used to represent
  2087. * read access. See FNAME(gpte_access) in paging_tmpl.h.
  2088. */
  2089. spte |= shadow_present_mask;
  2090. if (!speculative)
  2091. spte |= shadow_accessed_mask;
  2092. if (pte_access & ACC_EXEC_MASK)
  2093. spte |= shadow_x_mask;
  2094. else
  2095. spte |= shadow_nx_mask;
  2096. if (pte_access & ACC_USER_MASK)
  2097. spte |= shadow_user_mask;
  2098. if (level > PT_PAGE_TABLE_LEVEL)
  2099. spte |= PT_PAGE_SIZE_MASK;
  2100. if (tdp_enabled)
  2101. spte |= kvm_x86_ops->get_mt_mask(vcpu, gfn,
  2102. kvm_is_mmio_pfn(pfn));
  2103. if (host_writable)
  2104. spte |= SPTE_HOST_WRITEABLE;
  2105. else
  2106. pte_access &= ~ACC_WRITE_MASK;
  2107. spte |= (u64)pfn << PAGE_SHIFT;
  2108. if (pte_access & ACC_WRITE_MASK) {
  2109. /*
  2110. * Other vcpu creates new sp in the window between
  2111. * mapping_level() and acquiring mmu-lock. We can
  2112. * allow guest to retry the access, the mapping can
  2113. * be fixed if guest refault.
  2114. */
  2115. if (level > PT_PAGE_TABLE_LEVEL &&
  2116. mmu_gfn_lpage_is_disallowed(vcpu, gfn, level))
  2117. goto done;
  2118. spte |= PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE;
  2119. /*
  2120. * Optimization: for pte sync, if spte was writable the hash
  2121. * lookup is unnecessary (and expensive). Write protection
  2122. * is responsibility of mmu_get_page / kvm_sync_page.
  2123. * Same reasoning can be applied to dirty page accounting.
  2124. */
  2125. if (!can_unsync && is_writable_pte(*sptep))
  2126. goto set_pte;
  2127. if (mmu_need_write_protect(vcpu, gfn, can_unsync)) {
  2128. pgprintk("%s: found shadow page for %llx, marking ro\n",
  2129. __func__, gfn);
  2130. ret = 1;
  2131. pte_access &= ~ACC_WRITE_MASK;
  2132. spte &= ~(PT_WRITABLE_MASK | SPTE_MMU_WRITEABLE);
  2133. }
  2134. }
  2135. if (pte_access & ACC_WRITE_MASK) {
  2136. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  2137. spte |= shadow_dirty_mask;
  2138. }
  2139. set_pte:
  2140. if (mmu_spte_update(sptep, spte))
  2141. kvm_flush_remote_tlbs(vcpu->kvm);
  2142. done:
  2143. return ret;
  2144. }
  2145. static bool mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access,
  2146. int write_fault, int level, gfn_t gfn, kvm_pfn_t pfn,
  2147. bool speculative, bool host_writable)
  2148. {
  2149. int was_rmapped = 0;
  2150. int rmap_count;
  2151. bool emulate = false;
  2152. pgprintk("%s: spte %llx write_fault %d gfn %llx\n", __func__,
  2153. *sptep, write_fault, gfn);
  2154. if (is_shadow_present_pte(*sptep)) {
  2155. /*
  2156. * If we overwrite a PTE page pointer with a 2MB PMD, unlink
  2157. * the parent of the now unreachable PTE.
  2158. */
  2159. if (level > PT_PAGE_TABLE_LEVEL &&
  2160. !is_large_pte(*sptep)) {
  2161. struct kvm_mmu_page *child;
  2162. u64 pte = *sptep;
  2163. child = page_header(pte & PT64_BASE_ADDR_MASK);
  2164. drop_parent_pte(child, sptep);
  2165. kvm_flush_remote_tlbs(vcpu->kvm);
  2166. } else if (pfn != spte_to_pfn(*sptep)) {
  2167. pgprintk("hfn old %llx new %llx\n",
  2168. spte_to_pfn(*sptep), pfn);
  2169. drop_spte(vcpu->kvm, sptep);
  2170. kvm_flush_remote_tlbs(vcpu->kvm);
  2171. } else
  2172. was_rmapped = 1;
  2173. }
  2174. if (set_spte(vcpu, sptep, pte_access, level, gfn, pfn, speculative,
  2175. true, host_writable)) {
  2176. if (write_fault)
  2177. emulate = true;
  2178. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  2179. }
  2180. if (unlikely(is_mmio_spte(*sptep)))
  2181. emulate = true;
  2182. pgprintk("%s: setting spte %llx\n", __func__, *sptep);
  2183. pgprintk("instantiating %s PTE (%s) at %llx (%llx) addr %p\n",
  2184. is_large_pte(*sptep)? "2MB" : "4kB",
  2185. *sptep & PT_PRESENT_MASK ?"RW":"R", gfn,
  2186. *sptep, sptep);
  2187. if (!was_rmapped && is_large_pte(*sptep))
  2188. ++vcpu->kvm->stat.lpages;
  2189. if (is_shadow_present_pte(*sptep)) {
  2190. if (!was_rmapped) {
  2191. rmap_count = rmap_add(vcpu, sptep, gfn);
  2192. if (rmap_count > RMAP_RECYCLE_THRESHOLD)
  2193. rmap_recycle(vcpu, sptep, gfn);
  2194. }
  2195. }
  2196. kvm_release_pfn_clean(pfn);
  2197. return emulate;
  2198. }
  2199. static kvm_pfn_t pte_prefetch_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn,
  2200. bool no_dirty_log)
  2201. {
  2202. struct kvm_memory_slot *slot;
  2203. slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, no_dirty_log);
  2204. if (!slot)
  2205. return KVM_PFN_ERR_FAULT;
  2206. return gfn_to_pfn_memslot_atomic(slot, gfn);
  2207. }
  2208. static int direct_pte_prefetch_many(struct kvm_vcpu *vcpu,
  2209. struct kvm_mmu_page *sp,
  2210. u64 *start, u64 *end)
  2211. {
  2212. struct page *pages[PTE_PREFETCH_NUM];
  2213. struct kvm_memory_slot *slot;
  2214. unsigned access = sp->role.access;
  2215. int i, ret;
  2216. gfn_t gfn;
  2217. gfn = kvm_mmu_page_get_gfn(sp, start - sp->spt);
  2218. slot = gfn_to_memslot_dirty_bitmap(vcpu, gfn, access & ACC_WRITE_MASK);
  2219. if (!slot)
  2220. return -1;
  2221. ret = gfn_to_page_many_atomic(slot, gfn, pages, end - start);
  2222. if (ret <= 0)
  2223. return -1;
  2224. for (i = 0; i < ret; i++, gfn++, start++)
  2225. mmu_set_spte(vcpu, start, access, 0, sp->role.level, gfn,
  2226. page_to_pfn(pages[i]), true, true);
  2227. return 0;
  2228. }
  2229. static void __direct_pte_prefetch(struct kvm_vcpu *vcpu,
  2230. struct kvm_mmu_page *sp, u64 *sptep)
  2231. {
  2232. u64 *spte, *start = NULL;
  2233. int i;
  2234. WARN_ON(!sp->role.direct);
  2235. i = (sptep - sp->spt) & ~(PTE_PREFETCH_NUM - 1);
  2236. spte = sp->spt + i;
  2237. for (i = 0; i < PTE_PREFETCH_NUM; i++, spte++) {
  2238. if (is_shadow_present_pte(*spte) || spte == sptep) {
  2239. if (!start)
  2240. continue;
  2241. if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0)
  2242. break;
  2243. start = NULL;
  2244. } else if (!start)
  2245. start = spte;
  2246. }
  2247. }
  2248. static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)
  2249. {
  2250. struct kvm_mmu_page *sp;
  2251. /*
  2252. * Since it's no accessed bit on EPT, it's no way to
  2253. * distinguish between actually accessed translations
  2254. * and prefetched, so disable pte prefetch if EPT is
  2255. * enabled.
  2256. */
  2257. if (!shadow_accessed_mask)
  2258. return;
  2259. sp = page_header(__pa(sptep));
  2260. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  2261. return;
  2262. __direct_pte_prefetch(vcpu, sp, sptep);
  2263. }
  2264. static int __direct_map(struct kvm_vcpu *vcpu, int write, int map_writable,
  2265. int level, gfn_t gfn, kvm_pfn_t pfn, bool prefault)
  2266. {
  2267. struct kvm_shadow_walk_iterator iterator;
  2268. struct kvm_mmu_page *sp;
  2269. int emulate = 0;
  2270. gfn_t pseudo_gfn;
  2271. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2272. return 0;
  2273. for_each_shadow_entry(vcpu, (u64)gfn << PAGE_SHIFT, iterator) {
  2274. if (iterator.level == level) {
  2275. emulate = mmu_set_spte(vcpu, iterator.sptep, ACC_ALL,
  2276. write, level, gfn, pfn, prefault,
  2277. map_writable);
  2278. direct_pte_prefetch(vcpu, iterator.sptep);
  2279. ++vcpu->stat.pf_fixed;
  2280. break;
  2281. }
  2282. drop_large_spte(vcpu, iterator.sptep);
  2283. if (!is_shadow_present_pte(*iterator.sptep)) {
  2284. u64 base_addr = iterator.addr;
  2285. base_addr &= PT64_LVL_ADDR_MASK(iterator.level);
  2286. pseudo_gfn = base_addr >> PAGE_SHIFT;
  2287. sp = kvm_mmu_get_page(vcpu, pseudo_gfn, iterator.addr,
  2288. iterator.level - 1, 1, ACC_ALL);
  2289. link_shadow_page(vcpu, iterator.sptep, sp);
  2290. }
  2291. }
  2292. return emulate;
  2293. }
  2294. static void kvm_send_hwpoison_signal(unsigned long address, struct task_struct *tsk)
  2295. {
  2296. siginfo_t info;
  2297. info.si_signo = SIGBUS;
  2298. info.si_errno = 0;
  2299. info.si_code = BUS_MCEERR_AR;
  2300. info.si_addr = (void __user *)address;
  2301. info.si_addr_lsb = PAGE_SHIFT;
  2302. send_sig_info(SIGBUS, &info, tsk);
  2303. }
  2304. static int kvm_handle_bad_page(struct kvm_vcpu *vcpu, gfn_t gfn, kvm_pfn_t pfn)
  2305. {
  2306. /*
  2307. * Do not cache the mmio info caused by writing the readonly gfn
  2308. * into the spte otherwise read access on readonly gfn also can
  2309. * caused mmio page fault and treat it as mmio access.
  2310. * Return 1 to tell kvm to emulate it.
  2311. */
  2312. if (pfn == KVM_PFN_ERR_RO_FAULT)
  2313. return 1;
  2314. if (pfn == KVM_PFN_ERR_HWPOISON) {
  2315. kvm_send_hwpoison_signal(kvm_vcpu_gfn_to_hva(vcpu, gfn), current);
  2316. return 0;
  2317. }
  2318. return -EFAULT;
  2319. }
  2320. static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
  2321. gfn_t *gfnp, kvm_pfn_t *pfnp,
  2322. int *levelp)
  2323. {
  2324. kvm_pfn_t pfn = *pfnp;
  2325. gfn_t gfn = *gfnp;
  2326. int level = *levelp;
  2327. /*
  2328. * Check if it's a transparent hugepage. If this would be an
  2329. * hugetlbfs page, level wouldn't be set to
  2330. * PT_PAGE_TABLE_LEVEL and there would be no adjustment done
  2331. * here.
  2332. */
  2333. if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) &&
  2334. level == PT_PAGE_TABLE_LEVEL &&
  2335. PageTransCompoundMap(pfn_to_page(pfn)) &&
  2336. !mmu_gfn_lpage_is_disallowed(vcpu, gfn, PT_DIRECTORY_LEVEL)) {
  2337. unsigned long mask;
  2338. /*
  2339. * mmu_notifier_retry was successful and we hold the
  2340. * mmu_lock here, so the pmd can't become splitting
  2341. * from under us, and in turn
  2342. * __split_huge_page_refcount() can't run from under
  2343. * us and we can safely transfer the refcount from
  2344. * PG_tail to PG_head as we switch the pfn to tail to
  2345. * head.
  2346. */
  2347. *levelp = level = PT_DIRECTORY_LEVEL;
  2348. mask = KVM_PAGES_PER_HPAGE(level) - 1;
  2349. VM_BUG_ON((gfn & mask) != (pfn & mask));
  2350. if (pfn & mask) {
  2351. gfn &= ~mask;
  2352. *gfnp = gfn;
  2353. kvm_release_pfn_clean(pfn);
  2354. pfn &= ~mask;
  2355. kvm_get_pfn(pfn);
  2356. *pfnp = pfn;
  2357. }
  2358. }
  2359. }
  2360. static bool handle_abnormal_pfn(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
  2361. kvm_pfn_t pfn, unsigned access, int *ret_val)
  2362. {
  2363. /* The pfn is invalid, report the error! */
  2364. if (unlikely(is_error_pfn(pfn))) {
  2365. *ret_val = kvm_handle_bad_page(vcpu, gfn, pfn);
  2366. return true;
  2367. }
  2368. if (unlikely(is_noslot_pfn(pfn)))
  2369. vcpu_cache_mmio_info(vcpu, gva, gfn, access);
  2370. return false;
  2371. }
  2372. static bool page_fault_can_be_fast(u32 error_code)
  2373. {
  2374. /*
  2375. * Do not fix the mmio spte with invalid generation number which
  2376. * need to be updated by slow page fault path.
  2377. */
  2378. if (unlikely(error_code & PFERR_RSVD_MASK))
  2379. return false;
  2380. /*
  2381. * #PF can be fast only if the shadow page table is present and it
  2382. * is caused by write-protect, that means we just need change the
  2383. * W bit of the spte which can be done out of mmu-lock.
  2384. */
  2385. if (!(error_code & PFERR_PRESENT_MASK) ||
  2386. !(error_code & PFERR_WRITE_MASK))
  2387. return false;
  2388. return true;
  2389. }
  2390. static bool
  2391. fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  2392. u64 *sptep, u64 spte)
  2393. {
  2394. gfn_t gfn;
  2395. WARN_ON(!sp->role.direct);
  2396. /*
  2397. * The gfn of direct spte is stable since it is calculated
  2398. * by sp->gfn.
  2399. */
  2400. gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt);
  2401. /*
  2402. * Theoretically we could also set dirty bit (and flush TLB) here in
  2403. * order to eliminate unnecessary PML logging. See comments in
  2404. * set_spte. But fast_page_fault is very unlikely to happen with PML
  2405. * enabled, so we do not do this. This might result in the same GPA
  2406. * to be logged in PML buffer again when the write really happens, and
  2407. * eventually to be called by mark_page_dirty twice. But it's also no
  2408. * harm. This also avoids the TLB flush needed after setting dirty bit
  2409. * so non-PML cases won't be impacted.
  2410. *
  2411. * Compare with set_spte where instead shadow_dirty_mask is set.
  2412. */
  2413. if (cmpxchg64(sptep, spte, spte | PT_WRITABLE_MASK) == spte)
  2414. kvm_vcpu_mark_page_dirty(vcpu, gfn);
  2415. return true;
  2416. }
  2417. /*
  2418. * Return value:
  2419. * - true: let the vcpu to access on the same address again.
  2420. * - false: let the real page fault path to fix it.
  2421. */
  2422. static bool fast_page_fault(struct kvm_vcpu *vcpu, gva_t gva, int level,
  2423. u32 error_code)
  2424. {
  2425. struct kvm_shadow_walk_iterator iterator;
  2426. struct kvm_mmu_page *sp;
  2427. bool ret = false;
  2428. u64 spte = 0ull;
  2429. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2430. return false;
  2431. if (!page_fault_can_be_fast(error_code))
  2432. return false;
  2433. walk_shadow_page_lockless_begin(vcpu);
  2434. for_each_shadow_entry_lockless(vcpu, gva, iterator, spte)
  2435. if (!is_shadow_present_pte(spte) || iterator.level < level)
  2436. break;
  2437. /*
  2438. * If the mapping has been changed, let the vcpu fault on the
  2439. * same address again.
  2440. */
  2441. if (!is_shadow_present_pte(spte)) {
  2442. ret = true;
  2443. goto exit;
  2444. }
  2445. sp = page_header(__pa(iterator.sptep));
  2446. if (!is_last_spte(spte, sp->role.level))
  2447. goto exit;
  2448. /*
  2449. * Check if it is a spurious fault caused by TLB lazily flushed.
  2450. *
  2451. * Need not check the access of upper level table entries since
  2452. * they are always ACC_ALL.
  2453. */
  2454. if (is_writable_pte(spte)) {
  2455. ret = true;
  2456. goto exit;
  2457. }
  2458. /*
  2459. * Currently, to simplify the code, only the spte write-protected
  2460. * by dirty-log can be fast fixed.
  2461. */
  2462. if (!spte_is_locklessly_modifiable(spte))
  2463. goto exit;
  2464. /*
  2465. * Do not fix write-permission on the large spte since we only dirty
  2466. * the first page into the dirty-bitmap in fast_pf_fix_direct_spte()
  2467. * that means other pages are missed if its slot is dirty-logged.
  2468. *
  2469. * Instead, we let the slow page fault path create a normal spte to
  2470. * fix the access.
  2471. *
  2472. * See the comments in kvm_arch_commit_memory_region().
  2473. */
  2474. if (sp->role.level > PT_PAGE_TABLE_LEVEL)
  2475. goto exit;
  2476. /*
  2477. * Currently, fast page fault only works for direct mapping since
  2478. * the gfn is not stable for indirect shadow page.
  2479. * See Documentation/virtual/kvm/locking.txt to get more detail.
  2480. */
  2481. ret = fast_pf_fix_direct_spte(vcpu, sp, iterator.sptep, spte);
  2482. exit:
  2483. trace_fast_page_fault(vcpu, gva, error_code, iterator.sptep,
  2484. spte, ret);
  2485. walk_shadow_page_lockless_end(vcpu);
  2486. return ret;
  2487. }
  2488. static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
  2489. gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable);
  2490. static void make_mmu_pages_available(struct kvm_vcpu *vcpu);
  2491. static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, u32 error_code,
  2492. gfn_t gfn, bool prefault)
  2493. {
  2494. int r;
  2495. int level;
  2496. bool force_pt_level = false;
  2497. kvm_pfn_t pfn;
  2498. unsigned long mmu_seq;
  2499. bool map_writable, write = error_code & PFERR_WRITE_MASK;
  2500. level = mapping_level(vcpu, gfn, &force_pt_level);
  2501. if (likely(!force_pt_level)) {
  2502. /*
  2503. * This path builds a PAE pagetable - so we can map
  2504. * 2mb pages at maximum. Therefore check if the level
  2505. * is larger than that.
  2506. */
  2507. if (level > PT_DIRECTORY_LEVEL)
  2508. level = PT_DIRECTORY_LEVEL;
  2509. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  2510. }
  2511. if (fast_page_fault(vcpu, v, level, error_code))
  2512. return 0;
  2513. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  2514. smp_rmb();
  2515. if (try_async_pf(vcpu, prefault, gfn, v, &pfn, write, &map_writable))
  2516. return 0;
  2517. if (handle_abnormal_pfn(vcpu, v, gfn, pfn, ACC_ALL, &r))
  2518. return r;
  2519. spin_lock(&vcpu->kvm->mmu_lock);
  2520. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  2521. goto out_unlock;
  2522. make_mmu_pages_available(vcpu);
  2523. if (likely(!force_pt_level))
  2524. transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
  2525. r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
  2526. spin_unlock(&vcpu->kvm->mmu_lock);
  2527. return r;
  2528. out_unlock:
  2529. spin_unlock(&vcpu->kvm->mmu_lock);
  2530. kvm_release_pfn_clean(pfn);
  2531. return 0;
  2532. }
  2533. static void mmu_free_roots(struct kvm_vcpu *vcpu)
  2534. {
  2535. int i;
  2536. struct kvm_mmu_page *sp;
  2537. LIST_HEAD(invalid_list);
  2538. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2539. return;
  2540. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL &&
  2541. (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL ||
  2542. vcpu->arch.mmu.direct_map)) {
  2543. hpa_t root = vcpu->arch.mmu.root_hpa;
  2544. spin_lock(&vcpu->kvm->mmu_lock);
  2545. sp = page_header(root);
  2546. --sp->root_count;
  2547. if (!sp->root_count && sp->role.invalid) {
  2548. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
  2549. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  2550. }
  2551. spin_unlock(&vcpu->kvm->mmu_lock);
  2552. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  2553. return;
  2554. }
  2555. spin_lock(&vcpu->kvm->mmu_lock);
  2556. for (i = 0; i < 4; ++i) {
  2557. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2558. if (root) {
  2559. root &= PT64_BASE_ADDR_MASK;
  2560. sp = page_header(root);
  2561. --sp->root_count;
  2562. if (!sp->root_count && sp->role.invalid)
  2563. kvm_mmu_prepare_zap_page(vcpu->kvm, sp,
  2564. &invalid_list);
  2565. }
  2566. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  2567. }
  2568. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  2569. spin_unlock(&vcpu->kvm->mmu_lock);
  2570. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  2571. }
  2572. static int mmu_check_root(struct kvm_vcpu *vcpu, gfn_t root_gfn)
  2573. {
  2574. int ret = 0;
  2575. if (!kvm_is_visible_gfn(vcpu->kvm, root_gfn)) {
  2576. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2577. ret = 1;
  2578. }
  2579. return ret;
  2580. }
  2581. static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
  2582. {
  2583. struct kvm_mmu_page *sp;
  2584. unsigned i;
  2585. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  2586. spin_lock(&vcpu->kvm->mmu_lock);
  2587. make_mmu_pages_available(vcpu);
  2588. sp = kvm_mmu_get_page(vcpu, 0, 0, PT64_ROOT_LEVEL, 1, ACC_ALL);
  2589. ++sp->root_count;
  2590. spin_unlock(&vcpu->kvm->mmu_lock);
  2591. vcpu->arch.mmu.root_hpa = __pa(sp->spt);
  2592. } else if (vcpu->arch.mmu.shadow_root_level == PT32E_ROOT_LEVEL) {
  2593. for (i = 0; i < 4; ++i) {
  2594. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2595. MMU_WARN_ON(VALID_PAGE(root));
  2596. spin_lock(&vcpu->kvm->mmu_lock);
  2597. make_mmu_pages_available(vcpu);
  2598. sp = kvm_mmu_get_page(vcpu, i << (30 - PAGE_SHIFT),
  2599. i << 30, PT32_ROOT_LEVEL, 1, ACC_ALL);
  2600. root = __pa(sp->spt);
  2601. ++sp->root_count;
  2602. spin_unlock(&vcpu->kvm->mmu_lock);
  2603. vcpu->arch.mmu.pae_root[i] = root | PT_PRESENT_MASK;
  2604. }
  2605. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
  2606. } else
  2607. BUG();
  2608. return 0;
  2609. }
  2610. static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)
  2611. {
  2612. struct kvm_mmu_page *sp;
  2613. u64 pdptr, pm_mask;
  2614. gfn_t root_gfn;
  2615. int i;
  2616. root_gfn = vcpu->arch.mmu.get_cr3(vcpu) >> PAGE_SHIFT;
  2617. if (mmu_check_root(vcpu, root_gfn))
  2618. return 1;
  2619. /*
  2620. * Do we shadow a long mode page table? If so we need to
  2621. * write-protect the guests page table root.
  2622. */
  2623. if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
  2624. hpa_t root = vcpu->arch.mmu.root_hpa;
  2625. MMU_WARN_ON(VALID_PAGE(root));
  2626. spin_lock(&vcpu->kvm->mmu_lock);
  2627. make_mmu_pages_available(vcpu);
  2628. sp = kvm_mmu_get_page(vcpu, root_gfn, 0, PT64_ROOT_LEVEL,
  2629. 0, ACC_ALL);
  2630. root = __pa(sp->spt);
  2631. ++sp->root_count;
  2632. spin_unlock(&vcpu->kvm->mmu_lock);
  2633. vcpu->arch.mmu.root_hpa = root;
  2634. return 0;
  2635. }
  2636. /*
  2637. * We shadow a 32 bit page table. This may be a legacy 2-level
  2638. * or a PAE 3-level page table. In either case we need to be aware that
  2639. * the shadow page table may be a PAE or a long mode page table.
  2640. */
  2641. pm_mask = PT_PRESENT_MASK;
  2642. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL)
  2643. pm_mask |= PT_ACCESSED_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
  2644. for (i = 0; i < 4; ++i) {
  2645. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2646. MMU_WARN_ON(VALID_PAGE(root));
  2647. if (vcpu->arch.mmu.root_level == PT32E_ROOT_LEVEL) {
  2648. pdptr = vcpu->arch.mmu.get_pdptr(vcpu, i);
  2649. if (!(pdptr & PT_PRESENT_MASK)) {
  2650. vcpu->arch.mmu.pae_root[i] = 0;
  2651. continue;
  2652. }
  2653. root_gfn = pdptr >> PAGE_SHIFT;
  2654. if (mmu_check_root(vcpu, root_gfn))
  2655. return 1;
  2656. }
  2657. spin_lock(&vcpu->kvm->mmu_lock);
  2658. make_mmu_pages_available(vcpu);
  2659. sp = kvm_mmu_get_page(vcpu, root_gfn, i << 30, PT32_ROOT_LEVEL,
  2660. 0, ACC_ALL);
  2661. root = __pa(sp->spt);
  2662. ++sp->root_count;
  2663. spin_unlock(&vcpu->kvm->mmu_lock);
  2664. vcpu->arch.mmu.pae_root[i] = root | pm_mask;
  2665. }
  2666. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.pae_root);
  2667. /*
  2668. * If we shadow a 32 bit page table with a long mode page
  2669. * table we enter this path.
  2670. */
  2671. if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
  2672. if (vcpu->arch.mmu.lm_root == NULL) {
  2673. /*
  2674. * The additional page necessary for this is only
  2675. * allocated on demand.
  2676. */
  2677. u64 *lm_root;
  2678. lm_root = (void*)get_zeroed_page(GFP_KERNEL);
  2679. if (lm_root == NULL)
  2680. return 1;
  2681. lm_root[0] = __pa(vcpu->arch.mmu.pae_root) | pm_mask;
  2682. vcpu->arch.mmu.lm_root = lm_root;
  2683. }
  2684. vcpu->arch.mmu.root_hpa = __pa(vcpu->arch.mmu.lm_root);
  2685. }
  2686. return 0;
  2687. }
  2688. static int mmu_alloc_roots(struct kvm_vcpu *vcpu)
  2689. {
  2690. if (vcpu->arch.mmu.direct_map)
  2691. return mmu_alloc_direct_roots(vcpu);
  2692. else
  2693. return mmu_alloc_shadow_roots(vcpu);
  2694. }
  2695. static void mmu_sync_roots(struct kvm_vcpu *vcpu)
  2696. {
  2697. int i;
  2698. struct kvm_mmu_page *sp;
  2699. if (vcpu->arch.mmu.direct_map)
  2700. return;
  2701. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2702. return;
  2703. vcpu_clear_mmio_info(vcpu, MMIO_GVA_ANY);
  2704. kvm_mmu_audit(vcpu, AUDIT_PRE_SYNC);
  2705. if (vcpu->arch.mmu.root_level == PT64_ROOT_LEVEL) {
  2706. hpa_t root = vcpu->arch.mmu.root_hpa;
  2707. sp = page_header(root);
  2708. mmu_sync_children(vcpu, sp);
  2709. kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
  2710. return;
  2711. }
  2712. for (i = 0; i < 4; ++i) {
  2713. hpa_t root = vcpu->arch.mmu.pae_root[i];
  2714. if (root && VALID_PAGE(root)) {
  2715. root &= PT64_BASE_ADDR_MASK;
  2716. sp = page_header(root);
  2717. mmu_sync_children(vcpu, sp);
  2718. }
  2719. }
  2720. kvm_mmu_audit(vcpu, AUDIT_POST_SYNC);
  2721. }
  2722. void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
  2723. {
  2724. spin_lock(&vcpu->kvm->mmu_lock);
  2725. mmu_sync_roots(vcpu);
  2726. spin_unlock(&vcpu->kvm->mmu_lock);
  2727. }
  2728. EXPORT_SYMBOL_GPL(kvm_mmu_sync_roots);
  2729. static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr,
  2730. u32 access, struct x86_exception *exception)
  2731. {
  2732. if (exception)
  2733. exception->error_code = 0;
  2734. return vaddr;
  2735. }
  2736. static gpa_t nonpaging_gva_to_gpa_nested(struct kvm_vcpu *vcpu, gva_t vaddr,
  2737. u32 access,
  2738. struct x86_exception *exception)
  2739. {
  2740. if (exception)
  2741. exception->error_code = 0;
  2742. return vcpu->arch.nested_mmu.translate_gpa(vcpu, vaddr, access, exception);
  2743. }
  2744. static bool
  2745. __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level)
  2746. {
  2747. int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f;
  2748. return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) |
  2749. ((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0);
  2750. }
  2751. static bool is_rsvd_bits_set(struct kvm_mmu *mmu, u64 gpte, int level)
  2752. {
  2753. return __is_rsvd_bits_set(&mmu->guest_rsvd_check, gpte, level);
  2754. }
  2755. static bool is_shadow_zero_bits_set(struct kvm_mmu *mmu, u64 spte, int level)
  2756. {
  2757. return __is_rsvd_bits_set(&mmu->shadow_zero_check, spte, level);
  2758. }
  2759. static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
  2760. {
  2761. if (direct)
  2762. return vcpu_match_mmio_gpa(vcpu, addr);
  2763. return vcpu_match_mmio_gva(vcpu, addr);
  2764. }
  2765. /* return true if reserved bit is detected on spte. */
  2766. static bool
  2767. walk_shadow_page_get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep)
  2768. {
  2769. struct kvm_shadow_walk_iterator iterator;
  2770. u64 sptes[PT64_ROOT_LEVEL], spte = 0ull;
  2771. int root, leaf;
  2772. bool reserved = false;
  2773. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2774. goto exit;
  2775. walk_shadow_page_lockless_begin(vcpu);
  2776. for (shadow_walk_init(&iterator, vcpu, addr),
  2777. leaf = root = iterator.level;
  2778. shadow_walk_okay(&iterator);
  2779. __shadow_walk_next(&iterator, spte)) {
  2780. spte = mmu_spte_get_lockless(iterator.sptep);
  2781. sptes[leaf - 1] = spte;
  2782. leaf--;
  2783. if (!is_shadow_present_pte(spte))
  2784. break;
  2785. reserved |= is_shadow_zero_bits_set(&vcpu->arch.mmu, spte,
  2786. iterator.level);
  2787. }
  2788. walk_shadow_page_lockless_end(vcpu);
  2789. if (reserved) {
  2790. pr_err("%s: detect reserved bits on spte, addr 0x%llx, dump hierarchy:\n",
  2791. __func__, addr);
  2792. while (root > leaf) {
  2793. pr_err("------ spte 0x%llx level %d.\n",
  2794. sptes[root - 1], root);
  2795. root--;
  2796. }
  2797. }
  2798. exit:
  2799. *sptep = spte;
  2800. return reserved;
  2801. }
  2802. int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
  2803. {
  2804. u64 spte;
  2805. bool reserved;
  2806. if (mmio_info_in_cache(vcpu, addr, direct))
  2807. return RET_MMIO_PF_EMULATE;
  2808. reserved = walk_shadow_page_get_mmio_spte(vcpu, addr, &spte);
  2809. if (WARN_ON(reserved))
  2810. return RET_MMIO_PF_BUG;
  2811. if (is_mmio_spte(spte)) {
  2812. gfn_t gfn = get_mmio_spte_gfn(spte);
  2813. unsigned access = get_mmio_spte_access(spte);
  2814. if (!check_mmio_spte(vcpu, spte))
  2815. return RET_MMIO_PF_INVALID;
  2816. if (direct)
  2817. addr = 0;
  2818. trace_handle_mmio_page_fault(addr, gfn, access);
  2819. vcpu_cache_mmio_info(vcpu, addr, gfn, access);
  2820. return RET_MMIO_PF_EMULATE;
  2821. }
  2822. /*
  2823. * If the page table is zapped by other cpus, let CPU fault again on
  2824. * the address.
  2825. */
  2826. return RET_MMIO_PF_RETRY;
  2827. }
  2828. EXPORT_SYMBOL_GPL(handle_mmio_page_fault);
  2829. static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
  2830. u32 error_code, gfn_t gfn)
  2831. {
  2832. if (unlikely(error_code & PFERR_RSVD_MASK))
  2833. return false;
  2834. if (!(error_code & PFERR_PRESENT_MASK) ||
  2835. !(error_code & PFERR_WRITE_MASK))
  2836. return false;
  2837. /*
  2838. * guest is writing the page which is write tracked which can
  2839. * not be fixed by page fault handler.
  2840. */
  2841. if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
  2842. return true;
  2843. return false;
  2844. }
  2845. static void shadow_page_table_clear_flood(struct kvm_vcpu *vcpu, gva_t addr)
  2846. {
  2847. struct kvm_shadow_walk_iterator iterator;
  2848. u64 spte;
  2849. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  2850. return;
  2851. walk_shadow_page_lockless_begin(vcpu);
  2852. for_each_shadow_entry_lockless(vcpu, addr, iterator, spte) {
  2853. clear_sp_write_flooding_count(iterator.sptep);
  2854. if (!is_shadow_present_pte(spte))
  2855. break;
  2856. }
  2857. walk_shadow_page_lockless_end(vcpu);
  2858. }
  2859. static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
  2860. u32 error_code, bool prefault)
  2861. {
  2862. gfn_t gfn = gva >> PAGE_SHIFT;
  2863. int r;
  2864. pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
  2865. if (page_fault_handle_page_track(vcpu, error_code, gfn))
  2866. return 1;
  2867. r = mmu_topup_memory_caches(vcpu);
  2868. if (r)
  2869. return r;
  2870. MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2871. return nonpaging_map(vcpu, gva & PAGE_MASK,
  2872. error_code, gfn, prefault);
  2873. }
  2874. static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
  2875. {
  2876. struct kvm_arch_async_pf arch;
  2877. arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
  2878. arch.gfn = gfn;
  2879. arch.direct_map = vcpu->arch.mmu.direct_map;
  2880. arch.cr3 = vcpu->arch.mmu.get_cr3(vcpu);
  2881. return kvm_setup_async_pf(vcpu, gva, kvm_vcpu_gfn_to_hva(vcpu, gfn), &arch);
  2882. }
  2883. bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
  2884. {
  2885. if (unlikely(!lapic_in_kernel(vcpu) ||
  2886. kvm_event_needs_reinjection(vcpu)))
  2887. return false;
  2888. if (is_guest_mode(vcpu))
  2889. return false;
  2890. return kvm_x86_ops->interrupt_allowed(vcpu);
  2891. }
  2892. static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn,
  2893. gva_t gva, kvm_pfn_t *pfn, bool write, bool *writable)
  2894. {
  2895. struct kvm_memory_slot *slot;
  2896. bool async;
  2897. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  2898. async = false;
  2899. *pfn = __gfn_to_pfn_memslot(slot, gfn, false, &async, write, writable);
  2900. if (!async)
  2901. return false; /* *pfn has correct page already */
  2902. if (!prefault && kvm_can_do_async_pf(vcpu)) {
  2903. trace_kvm_try_async_get_page(gva, gfn);
  2904. if (kvm_find_async_pf_gfn(vcpu, gfn)) {
  2905. trace_kvm_async_pf_doublefault(gva, gfn);
  2906. kvm_make_request(KVM_REQ_APF_HALT, vcpu);
  2907. return true;
  2908. } else if (kvm_arch_setup_async_pf(vcpu, gva, gfn))
  2909. return true;
  2910. }
  2911. *pfn = __gfn_to_pfn_memslot(slot, gfn, false, NULL, write, writable);
  2912. return false;
  2913. }
  2914. static bool
  2915. check_hugepage_cache_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int level)
  2916. {
  2917. int page_num = KVM_PAGES_PER_HPAGE(level);
  2918. gfn &= ~(page_num - 1);
  2919. return kvm_mtrr_check_gfn_range_consistency(vcpu, gfn, page_num);
  2920. }
  2921. static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
  2922. bool prefault)
  2923. {
  2924. kvm_pfn_t pfn;
  2925. int r;
  2926. int level;
  2927. bool force_pt_level;
  2928. gfn_t gfn = gpa >> PAGE_SHIFT;
  2929. unsigned long mmu_seq;
  2930. int write = error_code & PFERR_WRITE_MASK;
  2931. bool map_writable;
  2932. MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
  2933. if (page_fault_handle_page_track(vcpu, error_code, gfn))
  2934. return 1;
  2935. r = mmu_topup_memory_caches(vcpu);
  2936. if (r)
  2937. return r;
  2938. force_pt_level = !check_hugepage_cache_consistency(vcpu, gfn,
  2939. PT_DIRECTORY_LEVEL);
  2940. level = mapping_level(vcpu, gfn, &force_pt_level);
  2941. if (likely(!force_pt_level)) {
  2942. if (level > PT_DIRECTORY_LEVEL &&
  2943. !check_hugepage_cache_consistency(vcpu, gfn, level))
  2944. level = PT_DIRECTORY_LEVEL;
  2945. gfn &= ~(KVM_PAGES_PER_HPAGE(level) - 1);
  2946. }
  2947. if (fast_page_fault(vcpu, gpa, level, error_code))
  2948. return 0;
  2949. mmu_seq = vcpu->kvm->mmu_notifier_seq;
  2950. smp_rmb();
  2951. if (try_async_pf(vcpu, prefault, gfn, gpa, &pfn, write, &map_writable))
  2952. return 0;
  2953. if (handle_abnormal_pfn(vcpu, 0, gfn, pfn, ACC_ALL, &r))
  2954. return r;
  2955. spin_lock(&vcpu->kvm->mmu_lock);
  2956. if (mmu_notifier_retry(vcpu->kvm, mmu_seq))
  2957. goto out_unlock;
  2958. make_mmu_pages_available(vcpu);
  2959. if (likely(!force_pt_level))
  2960. transparent_hugepage_adjust(vcpu, &gfn, &pfn, &level);
  2961. r = __direct_map(vcpu, write, map_writable, level, gfn, pfn, prefault);
  2962. spin_unlock(&vcpu->kvm->mmu_lock);
  2963. return r;
  2964. out_unlock:
  2965. spin_unlock(&vcpu->kvm->mmu_lock);
  2966. kvm_release_pfn_clean(pfn);
  2967. return 0;
  2968. }
  2969. static void nonpaging_init_context(struct kvm_vcpu *vcpu,
  2970. struct kvm_mmu *context)
  2971. {
  2972. context->page_fault = nonpaging_page_fault;
  2973. context->gva_to_gpa = nonpaging_gva_to_gpa;
  2974. context->sync_page = nonpaging_sync_page;
  2975. context->invlpg = nonpaging_invlpg;
  2976. context->update_pte = nonpaging_update_pte;
  2977. context->root_level = 0;
  2978. context->shadow_root_level = PT32E_ROOT_LEVEL;
  2979. context->root_hpa = INVALID_PAGE;
  2980. context->direct_map = true;
  2981. context->nx = false;
  2982. }
  2983. void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu)
  2984. {
  2985. mmu_free_roots(vcpu);
  2986. }
  2987. static unsigned long get_cr3(struct kvm_vcpu *vcpu)
  2988. {
  2989. return kvm_read_cr3(vcpu);
  2990. }
  2991. static void inject_page_fault(struct kvm_vcpu *vcpu,
  2992. struct x86_exception *fault)
  2993. {
  2994. vcpu->arch.mmu.inject_page_fault(vcpu, fault);
  2995. }
  2996. static bool sync_mmio_spte(struct kvm_vcpu *vcpu, u64 *sptep, gfn_t gfn,
  2997. unsigned access, int *nr_present)
  2998. {
  2999. if (unlikely(is_mmio_spte(*sptep))) {
  3000. if (gfn != get_mmio_spte_gfn(*sptep)) {
  3001. mmu_spte_clear_no_track(sptep);
  3002. return true;
  3003. }
  3004. (*nr_present)++;
  3005. mark_mmio_spte(vcpu, sptep, gfn, access);
  3006. return true;
  3007. }
  3008. return false;
  3009. }
  3010. static inline bool is_last_gpte(struct kvm_mmu *mmu,
  3011. unsigned level, unsigned gpte)
  3012. {
  3013. /*
  3014. * The RHS has bit 7 set iff level < mmu->last_nonleaf_level.
  3015. * If it is clear, there are no large pages at this level, so clear
  3016. * PT_PAGE_SIZE_MASK in gpte if that is the case.
  3017. */
  3018. gpte &= level - mmu->last_nonleaf_level;
  3019. /*
  3020. * PT_PAGE_TABLE_LEVEL always terminates. The RHS has bit 7 set
  3021. * iff level <= PT_PAGE_TABLE_LEVEL, which for our purpose means
  3022. * level == PT_PAGE_TABLE_LEVEL; set PT_PAGE_SIZE_MASK in gpte then.
  3023. */
  3024. gpte |= level - PT_PAGE_TABLE_LEVEL - 1;
  3025. return gpte & PT_PAGE_SIZE_MASK;
  3026. }
  3027. #define PTTYPE_EPT 18 /* arbitrary */
  3028. #define PTTYPE PTTYPE_EPT
  3029. #include "paging_tmpl.h"
  3030. #undef PTTYPE
  3031. #define PTTYPE 64
  3032. #include "paging_tmpl.h"
  3033. #undef PTTYPE
  3034. #define PTTYPE 32
  3035. #include "paging_tmpl.h"
  3036. #undef PTTYPE
  3037. static void
  3038. __reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
  3039. struct rsvd_bits_validate *rsvd_check,
  3040. int maxphyaddr, int level, bool nx, bool gbpages,
  3041. bool pse, bool amd)
  3042. {
  3043. u64 exb_bit_rsvd = 0;
  3044. u64 gbpages_bit_rsvd = 0;
  3045. u64 nonleaf_bit8_rsvd = 0;
  3046. rsvd_check->bad_mt_xwr = 0;
  3047. if (!nx)
  3048. exb_bit_rsvd = rsvd_bits(63, 63);
  3049. if (!gbpages)
  3050. gbpages_bit_rsvd = rsvd_bits(7, 7);
  3051. /*
  3052. * Non-leaf PML4Es and PDPEs reserve bit 8 (which would be the G bit for
  3053. * leaf entries) on AMD CPUs only.
  3054. */
  3055. if (amd)
  3056. nonleaf_bit8_rsvd = rsvd_bits(8, 8);
  3057. switch (level) {
  3058. case PT32_ROOT_LEVEL:
  3059. /* no rsvd bits for 2 level 4K page table entries */
  3060. rsvd_check->rsvd_bits_mask[0][1] = 0;
  3061. rsvd_check->rsvd_bits_mask[0][0] = 0;
  3062. rsvd_check->rsvd_bits_mask[1][0] =
  3063. rsvd_check->rsvd_bits_mask[0][0];
  3064. if (!pse) {
  3065. rsvd_check->rsvd_bits_mask[1][1] = 0;
  3066. break;
  3067. }
  3068. if (is_cpuid_PSE36())
  3069. /* 36bits PSE 4MB page */
  3070. rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(17, 21);
  3071. else
  3072. /* 32 bits PSE 4MB page */
  3073. rsvd_check->rsvd_bits_mask[1][1] = rsvd_bits(13, 21);
  3074. break;
  3075. case PT32E_ROOT_LEVEL:
  3076. rsvd_check->rsvd_bits_mask[0][2] =
  3077. rsvd_bits(maxphyaddr, 63) |
  3078. rsvd_bits(5, 8) | rsvd_bits(1, 2); /* PDPTE */
  3079. rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  3080. rsvd_bits(maxphyaddr, 62); /* PDE */
  3081. rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  3082. rsvd_bits(maxphyaddr, 62); /* PTE */
  3083. rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  3084. rsvd_bits(maxphyaddr, 62) |
  3085. rsvd_bits(13, 20); /* large page */
  3086. rsvd_check->rsvd_bits_mask[1][0] =
  3087. rsvd_check->rsvd_bits_mask[0][0];
  3088. break;
  3089. case PT64_ROOT_LEVEL:
  3090. rsvd_check->rsvd_bits_mask[0][3] = exb_bit_rsvd |
  3091. nonleaf_bit8_rsvd | rsvd_bits(7, 7) |
  3092. rsvd_bits(maxphyaddr, 51);
  3093. rsvd_check->rsvd_bits_mask[0][2] = exb_bit_rsvd |
  3094. nonleaf_bit8_rsvd | gbpages_bit_rsvd |
  3095. rsvd_bits(maxphyaddr, 51);
  3096. rsvd_check->rsvd_bits_mask[0][1] = exb_bit_rsvd |
  3097. rsvd_bits(maxphyaddr, 51);
  3098. rsvd_check->rsvd_bits_mask[0][0] = exb_bit_rsvd |
  3099. rsvd_bits(maxphyaddr, 51);
  3100. rsvd_check->rsvd_bits_mask[1][3] =
  3101. rsvd_check->rsvd_bits_mask[0][3];
  3102. rsvd_check->rsvd_bits_mask[1][2] = exb_bit_rsvd |
  3103. gbpages_bit_rsvd | rsvd_bits(maxphyaddr, 51) |
  3104. rsvd_bits(13, 29);
  3105. rsvd_check->rsvd_bits_mask[1][1] = exb_bit_rsvd |
  3106. rsvd_bits(maxphyaddr, 51) |
  3107. rsvd_bits(13, 20); /* large page */
  3108. rsvd_check->rsvd_bits_mask[1][0] =
  3109. rsvd_check->rsvd_bits_mask[0][0];
  3110. break;
  3111. }
  3112. }
  3113. static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,
  3114. struct kvm_mmu *context)
  3115. {
  3116. __reset_rsvds_bits_mask(vcpu, &context->guest_rsvd_check,
  3117. cpuid_maxphyaddr(vcpu), context->root_level,
  3118. context->nx, guest_cpuid_has_gbpages(vcpu),
  3119. is_pse(vcpu), guest_cpuid_is_amd(vcpu));
  3120. }
  3121. static void
  3122. __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,
  3123. int maxphyaddr, bool execonly)
  3124. {
  3125. u64 bad_mt_xwr;
  3126. rsvd_check->rsvd_bits_mask[0][3] =
  3127. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 7);
  3128. rsvd_check->rsvd_bits_mask[0][2] =
  3129. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
  3130. rsvd_check->rsvd_bits_mask[0][1] =
  3131. rsvd_bits(maxphyaddr, 51) | rsvd_bits(3, 6);
  3132. rsvd_check->rsvd_bits_mask[0][0] = rsvd_bits(maxphyaddr, 51);
  3133. /* large page */
  3134. rsvd_check->rsvd_bits_mask[1][3] = rsvd_check->rsvd_bits_mask[0][3];
  3135. rsvd_check->rsvd_bits_mask[1][2] =
  3136. rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 29);
  3137. rsvd_check->rsvd_bits_mask[1][1] =
  3138. rsvd_bits(maxphyaddr, 51) | rsvd_bits(12, 20);
  3139. rsvd_check->rsvd_bits_mask[1][0] = rsvd_check->rsvd_bits_mask[0][0];
  3140. bad_mt_xwr = 0xFFull << (2 * 8); /* bits 3..5 must not be 2 */
  3141. bad_mt_xwr |= 0xFFull << (3 * 8); /* bits 3..5 must not be 3 */
  3142. bad_mt_xwr |= 0xFFull << (7 * 8); /* bits 3..5 must not be 7 */
  3143. bad_mt_xwr |= REPEAT_BYTE(1ull << 2); /* bits 0..2 must not be 010 */
  3144. bad_mt_xwr |= REPEAT_BYTE(1ull << 6); /* bits 0..2 must not be 110 */
  3145. if (!execonly) {
  3146. /* bits 0..2 must not be 100 unless VMX capabilities allow it */
  3147. bad_mt_xwr |= REPEAT_BYTE(1ull << 4);
  3148. }
  3149. rsvd_check->bad_mt_xwr = bad_mt_xwr;
  3150. }
  3151. static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,
  3152. struct kvm_mmu *context, bool execonly)
  3153. {
  3154. __reset_rsvds_bits_mask_ept(&context->guest_rsvd_check,
  3155. cpuid_maxphyaddr(vcpu), execonly);
  3156. }
  3157. /*
  3158. * the page table on host is the shadow page table for the page
  3159. * table in guest or amd nested guest, its mmu features completely
  3160. * follow the features in guest.
  3161. */
  3162. void
  3163. reset_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context)
  3164. {
  3165. bool uses_nx = context->nx || context->base_role.smep_andnot_wp;
  3166. /*
  3167. * Passing "true" to the last argument is okay; it adds a check
  3168. * on bit 8 of the SPTEs which KVM doesn't use anyway.
  3169. */
  3170. __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check,
  3171. boot_cpu_data.x86_phys_bits,
  3172. context->shadow_root_level, uses_nx,
  3173. guest_cpuid_has_gbpages(vcpu), is_pse(vcpu),
  3174. true);
  3175. }
  3176. EXPORT_SYMBOL_GPL(reset_shadow_zero_bits_mask);
  3177. static inline bool boot_cpu_is_amd(void)
  3178. {
  3179. WARN_ON_ONCE(!tdp_enabled);
  3180. return shadow_x_mask == 0;
  3181. }
  3182. /*
  3183. * the direct page table on host, use as much mmu features as
  3184. * possible, however, kvm currently does not do execution-protection.
  3185. */
  3186. static void
  3187. reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
  3188. struct kvm_mmu *context)
  3189. {
  3190. if (boot_cpu_is_amd())
  3191. __reset_rsvds_bits_mask(vcpu, &context->shadow_zero_check,
  3192. boot_cpu_data.x86_phys_bits,
  3193. context->shadow_root_level, false,
  3194. boot_cpu_has(X86_FEATURE_GBPAGES),
  3195. true, true);
  3196. else
  3197. __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
  3198. boot_cpu_data.x86_phys_bits,
  3199. false);
  3200. }
  3201. /*
  3202. * as the comments in reset_shadow_zero_bits_mask() except it
  3203. * is the shadow page table for intel nested guest.
  3204. */
  3205. static void
  3206. reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu,
  3207. struct kvm_mmu *context, bool execonly)
  3208. {
  3209. __reset_rsvds_bits_mask_ept(&context->shadow_zero_check,
  3210. boot_cpu_data.x86_phys_bits, execonly);
  3211. }
  3212. static void update_permission_bitmask(struct kvm_vcpu *vcpu,
  3213. struct kvm_mmu *mmu, bool ept)
  3214. {
  3215. unsigned bit, byte, pfec;
  3216. u8 map;
  3217. bool fault, x, w, u, wf, uf, ff, smapf, cr4_smap, cr4_smep, smap = 0;
  3218. cr4_smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
  3219. cr4_smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
  3220. for (byte = 0; byte < ARRAY_SIZE(mmu->permissions); ++byte) {
  3221. pfec = byte << 1;
  3222. map = 0;
  3223. wf = pfec & PFERR_WRITE_MASK;
  3224. uf = pfec & PFERR_USER_MASK;
  3225. ff = pfec & PFERR_FETCH_MASK;
  3226. /*
  3227. * PFERR_RSVD_MASK bit is set in PFEC if the access is not
  3228. * subject to SMAP restrictions, and cleared otherwise. The
  3229. * bit is only meaningful if the SMAP bit is set in CR4.
  3230. */
  3231. smapf = !(pfec & PFERR_RSVD_MASK);
  3232. for (bit = 0; bit < 8; ++bit) {
  3233. x = bit & ACC_EXEC_MASK;
  3234. w = bit & ACC_WRITE_MASK;
  3235. u = bit & ACC_USER_MASK;
  3236. if (!ept) {
  3237. /* Not really needed: !nx will cause pte.nx to fault */
  3238. x |= !mmu->nx;
  3239. /* Allow supervisor writes if !cr0.wp */
  3240. w |= !is_write_protection(vcpu) && !uf;
  3241. /* Disallow supervisor fetches of user code if cr4.smep */
  3242. x &= !(cr4_smep && u && !uf);
  3243. /*
  3244. * SMAP:kernel-mode data accesses from user-mode
  3245. * mappings should fault. A fault is considered
  3246. * as a SMAP violation if all of the following
  3247. * conditions are ture:
  3248. * - X86_CR4_SMAP is set in CR4
  3249. * - An user page is accessed
  3250. * - Page fault in kernel mode
  3251. * - if CPL = 3 or X86_EFLAGS_AC is clear
  3252. *
  3253. * Here, we cover the first three conditions.
  3254. * The fourth is computed dynamically in
  3255. * permission_fault() and is in smapf.
  3256. *
  3257. * Also, SMAP does not affect instruction
  3258. * fetches, add the !ff check here to make it
  3259. * clearer.
  3260. */
  3261. smap = cr4_smap && u && !uf && !ff;
  3262. }
  3263. fault = (ff && !x) || (uf && !u) || (wf && !w) ||
  3264. (smapf && smap);
  3265. map |= fault << bit;
  3266. }
  3267. mmu->permissions[byte] = map;
  3268. }
  3269. }
  3270. /*
  3271. * PKU is an additional mechanism by which the paging controls access to
  3272. * user-mode addresses based on the value in the PKRU register. Protection
  3273. * key violations are reported through a bit in the page fault error code.
  3274. * Unlike other bits of the error code, the PK bit is not known at the
  3275. * call site of e.g. gva_to_gpa; it must be computed directly in
  3276. * permission_fault based on two bits of PKRU, on some machine state (CR4,
  3277. * CR0, EFER, CPL), and on other bits of the error code and the page tables.
  3278. *
  3279. * In particular the following conditions come from the error code, the
  3280. * page tables and the machine state:
  3281. * - PK is always zero unless CR4.PKE=1 and EFER.LMA=1
  3282. * - PK is always zero if RSVD=1 (reserved bit set) or F=1 (instruction fetch)
  3283. * - PK is always zero if U=0 in the page tables
  3284. * - PKRU.WD is ignored if CR0.WP=0 and the access is a supervisor access.
  3285. *
  3286. * The PKRU bitmask caches the result of these four conditions. The error
  3287. * code (minus the P bit) and the page table's U bit form an index into the
  3288. * PKRU bitmask. Two bits of the PKRU bitmask are then extracted and ANDed
  3289. * with the two bits of the PKRU register corresponding to the protection key.
  3290. * For the first three conditions above the bits will be 00, thus masking
  3291. * away both AD and WD. For all reads or if the last condition holds, WD
  3292. * only will be masked away.
  3293. */
  3294. static void update_pkru_bitmask(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  3295. bool ept)
  3296. {
  3297. unsigned bit;
  3298. bool wp;
  3299. if (ept) {
  3300. mmu->pkru_mask = 0;
  3301. return;
  3302. }
  3303. /* PKEY is enabled only if CR4.PKE and EFER.LMA are both set. */
  3304. if (!kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || !is_long_mode(vcpu)) {
  3305. mmu->pkru_mask = 0;
  3306. return;
  3307. }
  3308. wp = is_write_protection(vcpu);
  3309. for (bit = 0; bit < ARRAY_SIZE(mmu->permissions); ++bit) {
  3310. unsigned pfec, pkey_bits;
  3311. bool check_pkey, check_write, ff, uf, wf, pte_user;
  3312. pfec = bit << 1;
  3313. ff = pfec & PFERR_FETCH_MASK;
  3314. uf = pfec & PFERR_USER_MASK;
  3315. wf = pfec & PFERR_WRITE_MASK;
  3316. /* PFEC.RSVD is replaced by ACC_USER_MASK. */
  3317. pte_user = pfec & PFERR_RSVD_MASK;
  3318. /*
  3319. * Only need to check the access which is not an
  3320. * instruction fetch and is to a user page.
  3321. */
  3322. check_pkey = (!ff && pte_user);
  3323. /*
  3324. * write access is controlled by PKRU if it is a
  3325. * user access or CR0.WP = 1.
  3326. */
  3327. check_write = check_pkey && wf && (uf || wp);
  3328. /* PKRU.AD stops both read and write access. */
  3329. pkey_bits = !!check_pkey;
  3330. /* PKRU.WD stops write access. */
  3331. pkey_bits |= (!!check_write) << 1;
  3332. mmu->pkru_mask |= (pkey_bits & 3) << pfec;
  3333. }
  3334. }
  3335. static void update_last_nonleaf_level(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu)
  3336. {
  3337. unsigned root_level = mmu->root_level;
  3338. mmu->last_nonleaf_level = root_level;
  3339. if (root_level == PT32_ROOT_LEVEL && is_pse(vcpu))
  3340. mmu->last_nonleaf_level++;
  3341. }
  3342. static void paging64_init_context_common(struct kvm_vcpu *vcpu,
  3343. struct kvm_mmu *context,
  3344. int level)
  3345. {
  3346. context->nx = is_nx(vcpu);
  3347. context->root_level = level;
  3348. reset_rsvds_bits_mask(vcpu, context);
  3349. update_permission_bitmask(vcpu, context, false);
  3350. update_pkru_bitmask(vcpu, context, false);
  3351. update_last_nonleaf_level(vcpu, context);
  3352. MMU_WARN_ON(!is_pae(vcpu));
  3353. context->page_fault = paging64_page_fault;
  3354. context->gva_to_gpa = paging64_gva_to_gpa;
  3355. context->sync_page = paging64_sync_page;
  3356. context->invlpg = paging64_invlpg;
  3357. context->update_pte = paging64_update_pte;
  3358. context->shadow_root_level = level;
  3359. context->root_hpa = INVALID_PAGE;
  3360. context->direct_map = false;
  3361. }
  3362. static void paging64_init_context(struct kvm_vcpu *vcpu,
  3363. struct kvm_mmu *context)
  3364. {
  3365. paging64_init_context_common(vcpu, context, PT64_ROOT_LEVEL);
  3366. }
  3367. static void paging32_init_context(struct kvm_vcpu *vcpu,
  3368. struct kvm_mmu *context)
  3369. {
  3370. context->nx = false;
  3371. context->root_level = PT32_ROOT_LEVEL;
  3372. reset_rsvds_bits_mask(vcpu, context);
  3373. update_permission_bitmask(vcpu, context, false);
  3374. update_pkru_bitmask(vcpu, context, false);
  3375. update_last_nonleaf_level(vcpu, context);
  3376. context->page_fault = paging32_page_fault;
  3377. context->gva_to_gpa = paging32_gva_to_gpa;
  3378. context->sync_page = paging32_sync_page;
  3379. context->invlpg = paging32_invlpg;
  3380. context->update_pte = paging32_update_pte;
  3381. context->shadow_root_level = PT32E_ROOT_LEVEL;
  3382. context->root_hpa = INVALID_PAGE;
  3383. context->direct_map = false;
  3384. }
  3385. static void paging32E_init_context(struct kvm_vcpu *vcpu,
  3386. struct kvm_mmu *context)
  3387. {
  3388. paging64_init_context_common(vcpu, context, PT32E_ROOT_LEVEL);
  3389. }
  3390. static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
  3391. {
  3392. struct kvm_mmu *context = &vcpu->arch.mmu;
  3393. context->base_role.word = 0;
  3394. context->base_role.smm = is_smm(vcpu);
  3395. context->page_fault = tdp_page_fault;
  3396. context->sync_page = nonpaging_sync_page;
  3397. context->invlpg = nonpaging_invlpg;
  3398. context->update_pte = nonpaging_update_pte;
  3399. context->shadow_root_level = kvm_x86_ops->get_tdp_level();
  3400. context->root_hpa = INVALID_PAGE;
  3401. context->direct_map = true;
  3402. context->set_cr3 = kvm_x86_ops->set_tdp_cr3;
  3403. context->get_cr3 = get_cr3;
  3404. context->get_pdptr = kvm_pdptr_read;
  3405. context->inject_page_fault = kvm_inject_page_fault;
  3406. if (!is_paging(vcpu)) {
  3407. context->nx = false;
  3408. context->gva_to_gpa = nonpaging_gva_to_gpa;
  3409. context->root_level = 0;
  3410. } else if (is_long_mode(vcpu)) {
  3411. context->nx = is_nx(vcpu);
  3412. context->root_level = PT64_ROOT_LEVEL;
  3413. reset_rsvds_bits_mask(vcpu, context);
  3414. context->gva_to_gpa = paging64_gva_to_gpa;
  3415. } else if (is_pae(vcpu)) {
  3416. context->nx = is_nx(vcpu);
  3417. context->root_level = PT32E_ROOT_LEVEL;
  3418. reset_rsvds_bits_mask(vcpu, context);
  3419. context->gva_to_gpa = paging64_gva_to_gpa;
  3420. } else {
  3421. context->nx = false;
  3422. context->root_level = PT32_ROOT_LEVEL;
  3423. reset_rsvds_bits_mask(vcpu, context);
  3424. context->gva_to_gpa = paging32_gva_to_gpa;
  3425. }
  3426. update_permission_bitmask(vcpu, context, false);
  3427. update_pkru_bitmask(vcpu, context, false);
  3428. update_last_nonleaf_level(vcpu, context);
  3429. reset_tdp_shadow_zero_bits_mask(vcpu, context);
  3430. }
  3431. void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)
  3432. {
  3433. bool smep = kvm_read_cr4_bits(vcpu, X86_CR4_SMEP);
  3434. bool smap = kvm_read_cr4_bits(vcpu, X86_CR4_SMAP);
  3435. struct kvm_mmu *context = &vcpu->arch.mmu;
  3436. MMU_WARN_ON(VALID_PAGE(context->root_hpa));
  3437. if (!is_paging(vcpu))
  3438. nonpaging_init_context(vcpu, context);
  3439. else if (is_long_mode(vcpu))
  3440. paging64_init_context(vcpu, context);
  3441. else if (is_pae(vcpu))
  3442. paging32E_init_context(vcpu, context);
  3443. else
  3444. paging32_init_context(vcpu, context);
  3445. context->base_role.nxe = is_nx(vcpu);
  3446. context->base_role.cr4_pae = !!is_pae(vcpu);
  3447. context->base_role.cr0_wp = is_write_protection(vcpu);
  3448. context->base_role.smep_andnot_wp
  3449. = smep && !is_write_protection(vcpu);
  3450. context->base_role.smap_andnot_wp
  3451. = smap && !is_write_protection(vcpu);
  3452. context->base_role.smm = is_smm(vcpu);
  3453. reset_shadow_zero_bits_mask(vcpu, context);
  3454. }
  3455. EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);
  3456. void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly)
  3457. {
  3458. struct kvm_mmu *context = &vcpu->arch.mmu;
  3459. MMU_WARN_ON(VALID_PAGE(context->root_hpa));
  3460. context->shadow_root_level = kvm_x86_ops->get_tdp_level();
  3461. context->nx = true;
  3462. context->page_fault = ept_page_fault;
  3463. context->gva_to_gpa = ept_gva_to_gpa;
  3464. context->sync_page = ept_sync_page;
  3465. context->invlpg = ept_invlpg;
  3466. context->update_pte = ept_update_pte;
  3467. context->root_level = context->shadow_root_level;
  3468. context->root_hpa = INVALID_PAGE;
  3469. context->direct_map = false;
  3470. update_permission_bitmask(vcpu, context, true);
  3471. update_pkru_bitmask(vcpu, context, true);
  3472. update_last_nonleaf_level(vcpu, context);
  3473. reset_rsvds_bits_mask_ept(vcpu, context, execonly);
  3474. reset_ept_shadow_zero_bits_mask(vcpu, context, execonly);
  3475. }
  3476. EXPORT_SYMBOL_GPL(kvm_init_shadow_ept_mmu);
  3477. static void init_kvm_softmmu(struct kvm_vcpu *vcpu)
  3478. {
  3479. struct kvm_mmu *context = &vcpu->arch.mmu;
  3480. kvm_init_shadow_mmu(vcpu);
  3481. context->set_cr3 = kvm_x86_ops->set_cr3;
  3482. context->get_cr3 = get_cr3;
  3483. context->get_pdptr = kvm_pdptr_read;
  3484. context->inject_page_fault = kvm_inject_page_fault;
  3485. }
  3486. static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)
  3487. {
  3488. struct kvm_mmu *g_context = &vcpu->arch.nested_mmu;
  3489. g_context->get_cr3 = get_cr3;
  3490. g_context->get_pdptr = kvm_pdptr_read;
  3491. g_context->inject_page_fault = kvm_inject_page_fault;
  3492. /*
  3493. * Note that arch.mmu.gva_to_gpa translates l2_gpa to l1_gpa using
  3494. * L1's nested page tables (e.g. EPT12). The nested translation
  3495. * of l2_gva to l1_gpa is done by arch.nested_mmu.gva_to_gpa using
  3496. * L2's page tables as the first level of translation and L1's
  3497. * nested page tables as the second level of translation. Basically
  3498. * the gva_to_gpa functions between mmu and nested_mmu are swapped.
  3499. */
  3500. if (!is_paging(vcpu)) {
  3501. g_context->nx = false;
  3502. g_context->root_level = 0;
  3503. g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested;
  3504. } else if (is_long_mode(vcpu)) {
  3505. g_context->nx = is_nx(vcpu);
  3506. g_context->root_level = PT64_ROOT_LEVEL;
  3507. reset_rsvds_bits_mask(vcpu, g_context);
  3508. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  3509. } else if (is_pae(vcpu)) {
  3510. g_context->nx = is_nx(vcpu);
  3511. g_context->root_level = PT32E_ROOT_LEVEL;
  3512. reset_rsvds_bits_mask(vcpu, g_context);
  3513. g_context->gva_to_gpa = paging64_gva_to_gpa_nested;
  3514. } else {
  3515. g_context->nx = false;
  3516. g_context->root_level = PT32_ROOT_LEVEL;
  3517. reset_rsvds_bits_mask(vcpu, g_context);
  3518. g_context->gva_to_gpa = paging32_gva_to_gpa_nested;
  3519. }
  3520. update_permission_bitmask(vcpu, g_context, false);
  3521. update_pkru_bitmask(vcpu, g_context, false);
  3522. update_last_nonleaf_level(vcpu, g_context);
  3523. }
  3524. static void init_kvm_mmu(struct kvm_vcpu *vcpu)
  3525. {
  3526. if (mmu_is_nested(vcpu))
  3527. init_kvm_nested_mmu(vcpu);
  3528. else if (tdp_enabled)
  3529. init_kvm_tdp_mmu(vcpu);
  3530. else
  3531. init_kvm_softmmu(vcpu);
  3532. }
  3533. void kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
  3534. {
  3535. kvm_mmu_unload(vcpu);
  3536. init_kvm_mmu(vcpu);
  3537. }
  3538. EXPORT_SYMBOL_GPL(kvm_mmu_reset_context);
  3539. int kvm_mmu_load(struct kvm_vcpu *vcpu)
  3540. {
  3541. int r;
  3542. r = mmu_topup_memory_caches(vcpu);
  3543. if (r)
  3544. goto out;
  3545. r = mmu_alloc_roots(vcpu);
  3546. kvm_mmu_sync_roots(vcpu);
  3547. if (r)
  3548. goto out;
  3549. /* set_cr3() should ensure TLB has been flushed */
  3550. vcpu->arch.mmu.set_cr3(vcpu, vcpu->arch.mmu.root_hpa);
  3551. out:
  3552. return r;
  3553. }
  3554. EXPORT_SYMBOL_GPL(kvm_mmu_load);
  3555. void kvm_mmu_unload(struct kvm_vcpu *vcpu)
  3556. {
  3557. mmu_free_roots(vcpu);
  3558. WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3559. }
  3560. EXPORT_SYMBOL_GPL(kvm_mmu_unload);
  3561. static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
  3562. struct kvm_mmu_page *sp, u64 *spte,
  3563. const void *new)
  3564. {
  3565. if (sp->role.level != PT_PAGE_TABLE_LEVEL) {
  3566. ++vcpu->kvm->stat.mmu_pde_zapped;
  3567. return;
  3568. }
  3569. ++vcpu->kvm->stat.mmu_pte_updated;
  3570. vcpu->arch.mmu.update_pte(vcpu, sp, spte, new);
  3571. }
  3572. static bool need_remote_flush(u64 old, u64 new)
  3573. {
  3574. if (!is_shadow_present_pte(old))
  3575. return false;
  3576. if (!is_shadow_present_pte(new))
  3577. return true;
  3578. if ((old ^ new) & PT64_BASE_ADDR_MASK)
  3579. return true;
  3580. old ^= shadow_nx_mask;
  3581. new ^= shadow_nx_mask;
  3582. return (old & ~new & PT64_PERM_MASK) != 0;
  3583. }
  3584. static u64 mmu_pte_write_fetch_gpte(struct kvm_vcpu *vcpu, gpa_t *gpa,
  3585. const u8 *new, int *bytes)
  3586. {
  3587. u64 gentry;
  3588. int r;
  3589. /*
  3590. * Assume that the pte write on a page table of the same type
  3591. * as the current vcpu paging mode since we update the sptes only
  3592. * when they have the same mode.
  3593. */
  3594. if (is_pae(vcpu) && *bytes == 4) {
  3595. /* Handle a 32-bit guest writing two halves of a 64-bit gpte */
  3596. *gpa &= ~(gpa_t)7;
  3597. *bytes = 8;
  3598. r = kvm_vcpu_read_guest(vcpu, *gpa, &gentry, 8);
  3599. if (r)
  3600. gentry = 0;
  3601. new = (const u8 *)&gentry;
  3602. }
  3603. switch (*bytes) {
  3604. case 4:
  3605. gentry = *(const u32 *)new;
  3606. break;
  3607. case 8:
  3608. gentry = *(const u64 *)new;
  3609. break;
  3610. default:
  3611. gentry = 0;
  3612. break;
  3613. }
  3614. return gentry;
  3615. }
  3616. /*
  3617. * If we're seeing too many writes to a page, it may no longer be a page table,
  3618. * or we may be forking, in which case it is better to unmap the page.
  3619. */
  3620. static bool detect_write_flooding(struct kvm_mmu_page *sp)
  3621. {
  3622. /*
  3623. * Skip write-flooding detected for the sp whose level is 1, because
  3624. * it can become unsync, then the guest page is not write-protected.
  3625. */
  3626. if (sp->role.level == PT_PAGE_TABLE_LEVEL)
  3627. return false;
  3628. atomic_inc(&sp->write_flooding_count);
  3629. return atomic_read(&sp->write_flooding_count) >= 3;
  3630. }
  3631. /*
  3632. * Misaligned accesses are too much trouble to fix up; also, they usually
  3633. * indicate a page is not used as a page table.
  3634. */
  3635. static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa,
  3636. int bytes)
  3637. {
  3638. unsigned offset, pte_size, misaligned;
  3639. pgprintk("misaligned: gpa %llx bytes %d role %x\n",
  3640. gpa, bytes, sp->role.word);
  3641. offset = offset_in_page(gpa);
  3642. pte_size = sp->role.cr4_pae ? 8 : 4;
  3643. /*
  3644. * Sometimes, the OS only writes the last one bytes to update status
  3645. * bits, for example, in linux, andb instruction is used in clear_bit().
  3646. */
  3647. if (!(offset & (pte_size - 1)) && bytes == 1)
  3648. return false;
  3649. misaligned = (offset ^ (offset + bytes - 1)) & ~(pte_size - 1);
  3650. misaligned |= bytes < 4;
  3651. return misaligned;
  3652. }
  3653. static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)
  3654. {
  3655. unsigned page_offset, quadrant;
  3656. u64 *spte;
  3657. int level;
  3658. page_offset = offset_in_page(gpa);
  3659. level = sp->role.level;
  3660. *nspte = 1;
  3661. if (!sp->role.cr4_pae) {
  3662. page_offset <<= 1; /* 32->64 */
  3663. /*
  3664. * A 32-bit pde maps 4MB while the shadow pdes map
  3665. * only 2MB. So we need to double the offset again
  3666. * and zap two pdes instead of one.
  3667. */
  3668. if (level == PT32_ROOT_LEVEL) {
  3669. page_offset &= ~7; /* kill rounding error */
  3670. page_offset <<= 1;
  3671. *nspte = 2;
  3672. }
  3673. quadrant = page_offset >> PAGE_SHIFT;
  3674. page_offset &= ~PAGE_MASK;
  3675. if (quadrant != sp->role.quadrant)
  3676. return NULL;
  3677. }
  3678. spte = &sp->spt[page_offset / sizeof(*spte)];
  3679. return spte;
  3680. }
  3681. static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  3682. const u8 *new, int bytes)
  3683. {
  3684. gfn_t gfn = gpa >> PAGE_SHIFT;
  3685. struct kvm_mmu_page *sp;
  3686. LIST_HEAD(invalid_list);
  3687. u64 entry, gentry, *spte;
  3688. int npte;
  3689. bool remote_flush, local_flush;
  3690. union kvm_mmu_page_role mask = { };
  3691. mask.cr0_wp = 1;
  3692. mask.cr4_pae = 1;
  3693. mask.nxe = 1;
  3694. mask.smep_andnot_wp = 1;
  3695. mask.smap_andnot_wp = 1;
  3696. mask.smm = 1;
  3697. /*
  3698. * If we don't have indirect shadow pages, it means no page is
  3699. * write-protected, so we can exit simply.
  3700. */
  3701. if (!ACCESS_ONCE(vcpu->kvm->arch.indirect_shadow_pages))
  3702. return;
  3703. remote_flush = local_flush = false;
  3704. pgprintk("%s: gpa %llx bytes %d\n", __func__, gpa, bytes);
  3705. gentry = mmu_pte_write_fetch_gpte(vcpu, &gpa, new, &bytes);
  3706. /*
  3707. * No need to care whether allocation memory is successful
  3708. * or not since pte prefetch is skiped if it does not have
  3709. * enough objects in the cache.
  3710. */
  3711. mmu_topup_memory_caches(vcpu);
  3712. spin_lock(&vcpu->kvm->mmu_lock);
  3713. ++vcpu->kvm->stat.mmu_pte_write;
  3714. kvm_mmu_audit(vcpu, AUDIT_PRE_PTE_WRITE);
  3715. for_each_gfn_indirect_valid_sp(vcpu->kvm, sp, gfn) {
  3716. if (detect_write_misaligned(sp, gpa, bytes) ||
  3717. detect_write_flooding(sp)) {
  3718. kvm_mmu_prepare_zap_page(vcpu->kvm, sp, &invalid_list);
  3719. ++vcpu->kvm->stat.mmu_flooded;
  3720. continue;
  3721. }
  3722. spte = get_written_sptes(sp, gpa, &npte);
  3723. if (!spte)
  3724. continue;
  3725. local_flush = true;
  3726. while (npte--) {
  3727. entry = *spte;
  3728. mmu_page_zap_pte(vcpu->kvm, sp, spte);
  3729. if (gentry &&
  3730. !((sp->role.word ^ vcpu->arch.mmu.base_role.word)
  3731. & mask.word) && rmap_can_add(vcpu))
  3732. mmu_pte_write_new_pte(vcpu, sp, spte, &gentry);
  3733. if (need_remote_flush(entry, *spte))
  3734. remote_flush = true;
  3735. ++spte;
  3736. }
  3737. }
  3738. kvm_mmu_flush_or_zap(vcpu, &invalid_list, remote_flush, local_flush);
  3739. kvm_mmu_audit(vcpu, AUDIT_POST_PTE_WRITE);
  3740. spin_unlock(&vcpu->kvm->mmu_lock);
  3741. }
  3742. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva)
  3743. {
  3744. gpa_t gpa;
  3745. int r;
  3746. if (vcpu->arch.mmu.direct_map)
  3747. return 0;
  3748. gpa = kvm_mmu_gva_to_gpa_read(vcpu, gva, NULL);
  3749. r = kvm_mmu_unprotect_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3750. return r;
  3751. }
  3752. EXPORT_SYMBOL_GPL(kvm_mmu_unprotect_page_virt);
  3753. static void make_mmu_pages_available(struct kvm_vcpu *vcpu)
  3754. {
  3755. LIST_HEAD(invalid_list);
  3756. if (likely(kvm_mmu_available_pages(vcpu->kvm) >= KVM_MIN_FREE_MMU_PAGES))
  3757. return;
  3758. while (kvm_mmu_available_pages(vcpu->kvm) < KVM_REFILL_PAGES) {
  3759. if (!prepare_zap_oldest_mmu_page(vcpu->kvm, &invalid_list))
  3760. break;
  3761. ++vcpu->kvm->stat.mmu_recycled;
  3762. }
  3763. kvm_mmu_commit_zap_page(vcpu->kvm, &invalid_list);
  3764. }
  3765. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code,
  3766. void *insn, int insn_len)
  3767. {
  3768. int r, emulation_type = EMULTYPE_RETRY;
  3769. enum emulation_result er;
  3770. bool direct = vcpu->arch.mmu.direct_map || mmu_is_nested(vcpu);
  3771. if (unlikely(error_code & PFERR_RSVD_MASK)) {
  3772. r = handle_mmio_page_fault(vcpu, cr2, direct);
  3773. if (r == RET_MMIO_PF_EMULATE) {
  3774. emulation_type = 0;
  3775. goto emulate;
  3776. }
  3777. if (r == RET_MMIO_PF_RETRY)
  3778. return 1;
  3779. if (r < 0)
  3780. return r;
  3781. }
  3782. r = vcpu->arch.mmu.page_fault(vcpu, cr2, error_code, false);
  3783. if (r < 0)
  3784. return r;
  3785. if (!r)
  3786. return 1;
  3787. if (mmio_info_in_cache(vcpu, cr2, direct))
  3788. emulation_type = 0;
  3789. emulate:
  3790. er = x86_emulate_instruction(vcpu, cr2, emulation_type, insn, insn_len);
  3791. switch (er) {
  3792. case EMULATE_DONE:
  3793. return 1;
  3794. case EMULATE_USER_EXIT:
  3795. ++vcpu->stat.mmio_exits;
  3796. /* fall through */
  3797. case EMULATE_FAIL:
  3798. return 0;
  3799. default:
  3800. BUG();
  3801. }
  3802. }
  3803. EXPORT_SYMBOL_GPL(kvm_mmu_page_fault);
  3804. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)
  3805. {
  3806. vcpu->arch.mmu.invlpg(vcpu, gva);
  3807. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  3808. ++vcpu->stat.invlpg;
  3809. }
  3810. EXPORT_SYMBOL_GPL(kvm_mmu_invlpg);
  3811. void kvm_enable_tdp(void)
  3812. {
  3813. tdp_enabled = true;
  3814. }
  3815. EXPORT_SYMBOL_GPL(kvm_enable_tdp);
  3816. void kvm_disable_tdp(void)
  3817. {
  3818. tdp_enabled = false;
  3819. }
  3820. EXPORT_SYMBOL_GPL(kvm_disable_tdp);
  3821. static void free_mmu_pages(struct kvm_vcpu *vcpu)
  3822. {
  3823. free_page((unsigned long)vcpu->arch.mmu.pae_root);
  3824. if (vcpu->arch.mmu.lm_root != NULL)
  3825. free_page((unsigned long)vcpu->arch.mmu.lm_root);
  3826. }
  3827. static int alloc_mmu_pages(struct kvm_vcpu *vcpu)
  3828. {
  3829. struct page *page;
  3830. int i;
  3831. /*
  3832. * When emulating 32-bit mode, cr3 is only 32 bits even on x86_64.
  3833. * Therefore we need to allocate shadow page tables in the first
  3834. * 4GB of memory, which happens to fit the DMA32 zone.
  3835. */
  3836. page = alloc_page(GFP_KERNEL | __GFP_DMA32);
  3837. if (!page)
  3838. return -ENOMEM;
  3839. vcpu->arch.mmu.pae_root = page_address(page);
  3840. for (i = 0; i < 4; ++i)
  3841. vcpu->arch.mmu.pae_root[i] = INVALID_PAGE;
  3842. return 0;
  3843. }
  3844. int kvm_mmu_create(struct kvm_vcpu *vcpu)
  3845. {
  3846. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  3847. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  3848. vcpu->arch.mmu.translate_gpa = translate_gpa;
  3849. vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;
  3850. return alloc_mmu_pages(vcpu);
  3851. }
  3852. void kvm_mmu_setup(struct kvm_vcpu *vcpu)
  3853. {
  3854. MMU_WARN_ON(VALID_PAGE(vcpu->arch.mmu.root_hpa));
  3855. init_kvm_mmu(vcpu);
  3856. }
  3857. void kvm_mmu_init_vm(struct kvm *kvm)
  3858. {
  3859. struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
  3860. node->track_write = kvm_mmu_pte_write;
  3861. kvm_page_track_register_notifier(kvm, node);
  3862. }
  3863. void kvm_mmu_uninit_vm(struct kvm *kvm)
  3864. {
  3865. struct kvm_page_track_notifier_node *node = &kvm->arch.mmu_sp_tracker;
  3866. kvm_page_track_unregister_notifier(kvm, node);
  3867. }
  3868. /* The return value indicates if tlb flush on all vcpus is needed. */
  3869. typedef bool (*slot_level_handler) (struct kvm *kvm, struct kvm_rmap_head *rmap_head);
  3870. /* The caller should hold mmu-lock before calling this function. */
  3871. static __always_inline bool
  3872. slot_handle_level_range(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3873. slot_level_handler fn, int start_level, int end_level,
  3874. gfn_t start_gfn, gfn_t end_gfn, bool lock_flush_tlb)
  3875. {
  3876. struct slot_rmap_walk_iterator iterator;
  3877. bool flush = false;
  3878. for_each_slot_rmap_range(memslot, start_level, end_level, start_gfn,
  3879. end_gfn, &iterator) {
  3880. if (iterator.rmap)
  3881. flush |= fn(kvm, iterator.rmap);
  3882. if (need_resched() || spin_needbreak(&kvm->mmu_lock)) {
  3883. if (flush && lock_flush_tlb) {
  3884. kvm_flush_remote_tlbs(kvm);
  3885. flush = false;
  3886. }
  3887. cond_resched_lock(&kvm->mmu_lock);
  3888. }
  3889. }
  3890. if (flush && lock_flush_tlb) {
  3891. kvm_flush_remote_tlbs(kvm);
  3892. flush = false;
  3893. }
  3894. return flush;
  3895. }
  3896. static __always_inline bool
  3897. slot_handle_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3898. slot_level_handler fn, int start_level, int end_level,
  3899. bool lock_flush_tlb)
  3900. {
  3901. return slot_handle_level_range(kvm, memslot, fn, start_level,
  3902. end_level, memslot->base_gfn,
  3903. memslot->base_gfn + memslot->npages - 1,
  3904. lock_flush_tlb);
  3905. }
  3906. static __always_inline bool
  3907. slot_handle_all_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3908. slot_level_handler fn, bool lock_flush_tlb)
  3909. {
  3910. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
  3911. PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
  3912. }
  3913. static __always_inline bool
  3914. slot_handle_large_level(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3915. slot_level_handler fn, bool lock_flush_tlb)
  3916. {
  3917. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL + 1,
  3918. PT_MAX_HUGEPAGE_LEVEL, lock_flush_tlb);
  3919. }
  3920. static __always_inline bool
  3921. slot_handle_leaf(struct kvm *kvm, struct kvm_memory_slot *memslot,
  3922. slot_level_handler fn, bool lock_flush_tlb)
  3923. {
  3924. return slot_handle_level(kvm, memslot, fn, PT_PAGE_TABLE_LEVEL,
  3925. PT_PAGE_TABLE_LEVEL, lock_flush_tlb);
  3926. }
  3927. void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
  3928. {
  3929. struct kvm_memslots *slots;
  3930. struct kvm_memory_slot *memslot;
  3931. int i;
  3932. spin_lock(&kvm->mmu_lock);
  3933. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  3934. slots = __kvm_memslots(kvm, i);
  3935. kvm_for_each_memslot(memslot, slots) {
  3936. gfn_t start, end;
  3937. start = max(gfn_start, memslot->base_gfn);
  3938. end = min(gfn_end, memslot->base_gfn + memslot->npages);
  3939. if (start >= end)
  3940. continue;
  3941. slot_handle_level_range(kvm, memslot, kvm_zap_rmapp,
  3942. PT_PAGE_TABLE_LEVEL, PT_MAX_HUGEPAGE_LEVEL,
  3943. start, end - 1, true);
  3944. }
  3945. }
  3946. spin_unlock(&kvm->mmu_lock);
  3947. }
  3948. static bool slot_rmap_write_protect(struct kvm *kvm,
  3949. struct kvm_rmap_head *rmap_head)
  3950. {
  3951. return __rmap_write_protect(kvm, rmap_head, false);
  3952. }
  3953. void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
  3954. struct kvm_memory_slot *memslot)
  3955. {
  3956. bool flush;
  3957. spin_lock(&kvm->mmu_lock);
  3958. flush = slot_handle_all_level(kvm, memslot, slot_rmap_write_protect,
  3959. false);
  3960. spin_unlock(&kvm->mmu_lock);
  3961. /*
  3962. * kvm_mmu_slot_remove_write_access() and kvm_vm_ioctl_get_dirty_log()
  3963. * which do tlb flush out of mmu-lock should be serialized by
  3964. * kvm->slots_lock otherwise tlb flush would be missed.
  3965. */
  3966. lockdep_assert_held(&kvm->slots_lock);
  3967. /*
  3968. * We can flush all the TLBs out of the mmu lock without TLB
  3969. * corruption since we just change the spte from writable to
  3970. * readonly so that we only need to care the case of changing
  3971. * spte from present to present (changing the spte from present
  3972. * to nonpresent will flush all the TLBs immediately), in other
  3973. * words, the only case we care is mmu_spte_update() where we
  3974. * haved checked SPTE_HOST_WRITEABLE | SPTE_MMU_WRITEABLE
  3975. * instead of PT_WRITABLE_MASK, that means it does not depend
  3976. * on PT_WRITABLE_MASK anymore.
  3977. */
  3978. if (flush)
  3979. kvm_flush_remote_tlbs(kvm);
  3980. }
  3981. static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
  3982. struct kvm_rmap_head *rmap_head)
  3983. {
  3984. u64 *sptep;
  3985. struct rmap_iterator iter;
  3986. int need_tlb_flush = 0;
  3987. kvm_pfn_t pfn;
  3988. struct kvm_mmu_page *sp;
  3989. restart:
  3990. for_each_rmap_spte(rmap_head, &iter, sptep) {
  3991. sp = page_header(__pa(sptep));
  3992. pfn = spte_to_pfn(*sptep);
  3993. /*
  3994. * We cannot do huge page mapping for indirect shadow pages,
  3995. * which are found on the last rmap (level = 1) when not using
  3996. * tdp; such shadow pages are synced with the page table in
  3997. * the guest, and the guest page table is using 4K page size
  3998. * mapping if the indirect sp has level = 1.
  3999. */
  4000. if (sp->role.direct &&
  4001. !kvm_is_reserved_pfn(pfn) &&
  4002. PageTransCompoundMap(pfn_to_page(pfn))) {
  4003. drop_spte(kvm, sptep);
  4004. need_tlb_flush = 1;
  4005. goto restart;
  4006. }
  4007. }
  4008. return need_tlb_flush;
  4009. }
  4010. void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
  4011. const struct kvm_memory_slot *memslot)
  4012. {
  4013. /* FIXME: const-ify all uses of struct kvm_memory_slot. */
  4014. spin_lock(&kvm->mmu_lock);
  4015. slot_handle_leaf(kvm, (struct kvm_memory_slot *)memslot,
  4016. kvm_mmu_zap_collapsible_spte, true);
  4017. spin_unlock(&kvm->mmu_lock);
  4018. }
  4019. void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
  4020. struct kvm_memory_slot *memslot)
  4021. {
  4022. bool flush;
  4023. spin_lock(&kvm->mmu_lock);
  4024. flush = slot_handle_leaf(kvm, memslot, __rmap_clear_dirty, false);
  4025. spin_unlock(&kvm->mmu_lock);
  4026. lockdep_assert_held(&kvm->slots_lock);
  4027. /*
  4028. * It's also safe to flush TLBs out of mmu lock here as currently this
  4029. * function is only used for dirty logging, in which case flushing TLB
  4030. * out of mmu lock also guarantees no dirty pages will be lost in
  4031. * dirty_bitmap.
  4032. */
  4033. if (flush)
  4034. kvm_flush_remote_tlbs(kvm);
  4035. }
  4036. EXPORT_SYMBOL_GPL(kvm_mmu_slot_leaf_clear_dirty);
  4037. void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,
  4038. struct kvm_memory_slot *memslot)
  4039. {
  4040. bool flush;
  4041. spin_lock(&kvm->mmu_lock);
  4042. flush = slot_handle_large_level(kvm, memslot, slot_rmap_write_protect,
  4043. false);
  4044. spin_unlock(&kvm->mmu_lock);
  4045. /* see kvm_mmu_slot_remove_write_access */
  4046. lockdep_assert_held(&kvm->slots_lock);
  4047. if (flush)
  4048. kvm_flush_remote_tlbs(kvm);
  4049. }
  4050. EXPORT_SYMBOL_GPL(kvm_mmu_slot_largepage_remove_write_access);
  4051. void kvm_mmu_slot_set_dirty(struct kvm *kvm,
  4052. struct kvm_memory_slot *memslot)
  4053. {
  4054. bool flush;
  4055. spin_lock(&kvm->mmu_lock);
  4056. flush = slot_handle_all_level(kvm, memslot, __rmap_set_dirty, false);
  4057. spin_unlock(&kvm->mmu_lock);
  4058. lockdep_assert_held(&kvm->slots_lock);
  4059. /* see kvm_mmu_slot_leaf_clear_dirty */
  4060. if (flush)
  4061. kvm_flush_remote_tlbs(kvm);
  4062. }
  4063. EXPORT_SYMBOL_GPL(kvm_mmu_slot_set_dirty);
  4064. #define BATCH_ZAP_PAGES 10
  4065. static void kvm_zap_obsolete_pages(struct kvm *kvm)
  4066. {
  4067. struct kvm_mmu_page *sp, *node;
  4068. int batch = 0;
  4069. restart:
  4070. list_for_each_entry_safe_reverse(sp, node,
  4071. &kvm->arch.active_mmu_pages, link) {
  4072. int ret;
  4073. /*
  4074. * No obsolete page exists before new created page since
  4075. * active_mmu_pages is the FIFO list.
  4076. */
  4077. if (!is_obsolete_sp(kvm, sp))
  4078. break;
  4079. /*
  4080. * Since we are reversely walking the list and the invalid
  4081. * list will be moved to the head, skip the invalid page
  4082. * can help us to avoid the infinity list walking.
  4083. */
  4084. if (sp->role.invalid)
  4085. continue;
  4086. /*
  4087. * Need not flush tlb since we only zap the sp with invalid
  4088. * generation number.
  4089. */
  4090. if (batch >= BATCH_ZAP_PAGES &&
  4091. cond_resched_lock(&kvm->mmu_lock)) {
  4092. batch = 0;
  4093. goto restart;
  4094. }
  4095. ret = kvm_mmu_prepare_zap_page(kvm, sp,
  4096. &kvm->arch.zapped_obsolete_pages);
  4097. batch += ret;
  4098. if (ret)
  4099. goto restart;
  4100. }
  4101. /*
  4102. * Should flush tlb before free page tables since lockless-walking
  4103. * may use the pages.
  4104. */
  4105. kvm_mmu_commit_zap_page(kvm, &kvm->arch.zapped_obsolete_pages);
  4106. }
  4107. /*
  4108. * Fast invalidate all shadow pages and use lock-break technique
  4109. * to zap obsolete pages.
  4110. *
  4111. * It's required when memslot is being deleted or VM is being
  4112. * destroyed, in these cases, we should ensure that KVM MMU does
  4113. * not use any resource of the being-deleted slot or all slots
  4114. * after calling the function.
  4115. */
  4116. void kvm_mmu_invalidate_zap_all_pages(struct kvm *kvm)
  4117. {
  4118. spin_lock(&kvm->mmu_lock);
  4119. trace_kvm_mmu_invalidate_zap_all_pages(kvm);
  4120. kvm->arch.mmu_valid_gen++;
  4121. /*
  4122. * Notify all vcpus to reload its shadow page table
  4123. * and flush TLB. Then all vcpus will switch to new
  4124. * shadow page table with the new mmu_valid_gen.
  4125. *
  4126. * Note: we should do this under the protection of
  4127. * mmu-lock, otherwise, vcpu would purge shadow page
  4128. * but miss tlb flush.
  4129. */
  4130. kvm_reload_remote_mmus(kvm);
  4131. kvm_zap_obsolete_pages(kvm);
  4132. spin_unlock(&kvm->mmu_lock);
  4133. }
  4134. static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm)
  4135. {
  4136. return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages));
  4137. }
  4138. void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, struct kvm_memslots *slots)
  4139. {
  4140. /*
  4141. * The very rare case: if the generation-number is round,
  4142. * zap all shadow pages.
  4143. */
  4144. if (unlikely((slots->generation & MMIO_GEN_MASK) == 0)) {
  4145. printk_ratelimited(KERN_DEBUG "kvm: zapping shadow pages for mmio generation wraparound\n");
  4146. kvm_mmu_invalidate_zap_all_pages(kvm);
  4147. }
  4148. }
  4149. static unsigned long
  4150. mmu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  4151. {
  4152. struct kvm *kvm;
  4153. int nr_to_scan = sc->nr_to_scan;
  4154. unsigned long freed = 0;
  4155. spin_lock(&kvm_lock);
  4156. list_for_each_entry(kvm, &vm_list, vm_list) {
  4157. int idx;
  4158. LIST_HEAD(invalid_list);
  4159. /*
  4160. * Never scan more than sc->nr_to_scan VM instances.
  4161. * Will not hit this condition practically since we do not try
  4162. * to shrink more than one VM and it is very unlikely to see
  4163. * !n_used_mmu_pages so many times.
  4164. */
  4165. if (!nr_to_scan--)
  4166. break;
  4167. /*
  4168. * n_used_mmu_pages is accessed without holding kvm->mmu_lock
  4169. * here. We may skip a VM instance errorneosly, but we do not
  4170. * want to shrink a VM that only started to populate its MMU
  4171. * anyway.
  4172. */
  4173. if (!kvm->arch.n_used_mmu_pages &&
  4174. !kvm_has_zapped_obsolete_pages(kvm))
  4175. continue;
  4176. idx = srcu_read_lock(&kvm->srcu);
  4177. spin_lock(&kvm->mmu_lock);
  4178. if (kvm_has_zapped_obsolete_pages(kvm)) {
  4179. kvm_mmu_commit_zap_page(kvm,
  4180. &kvm->arch.zapped_obsolete_pages);
  4181. goto unlock;
  4182. }
  4183. if (prepare_zap_oldest_mmu_page(kvm, &invalid_list))
  4184. freed++;
  4185. kvm_mmu_commit_zap_page(kvm, &invalid_list);
  4186. unlock:
  4187. spin_unlock(&kvm->mmu_lock);
  4188. srcu_read_unlock(&kvm->srcu, idx);
  4189. /*
  4190. * unfair on small ones
  4191. * per-vm shrinkers cry out
  4192. * sadness comes quickly
  4193. */
  4194. list_move_tail(&kvm->vm_list, &vm_list);
  4195. break;
  4196. }
  4197. spin_unlock(&kvm_lock);
  4198. return freed;
  4199. }
  4200. static unsigned long
  4201. mmu_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  4202. {
  4203. return percpu_counter_read_positive(&kvm_total_used_mmu_pages);
  4204. }
  4205. static struct shrinker mmu_shrinker = {
  4206. .count_objects = mmu_shrink_count,
  4207. .scan_objects = mmu_shrink_scan,
  4208. .seeks = DEFAULT_SEEKS * 10,
  4209. };
  4210. static void mmu_destroy_caches(void)
  4211. {
  4212. if (pte_list_desc_cache)
  4213. kmem_cache_destroy(pte_list_desc_cache);
  4214. if (mmu_page_header_cache)
  4215. kmem_cache_destroy(mmu_page_header_cache);
  4216. }
  4217. int kvm_mmu_module_init(void)
  4218. {
  4219. pte_list_desc_cache = kmem_cache_create("pte_list_desc",
  4220. sizeof(struct pte_list_desc),
  4221. 0, SLAB_ACCOUNT, NULL);
  4222. if (!pte_list_desc_cache)
  4223. goto nomem;
  4224. mmu_page_header_cache = kmem_cache_create("kvm_mmu_page_header",
  4225. sizeof(struct kvm_mmu_page),
  4226. 0, SLAB_ACCOUNT, NULL);
  4227. if (!mmu_page_header_cache)
  4228. goto nomem;
  4229. if (percpu_counter_init(&kvm_total_used_mmu_pages, 0, GFP_KERNEL))
  4230. goto nomem;
  4231. register_shrinker(&mmu_shrinker);
  4232. return 0;
  4233. nomem:
  4234. mmu_destroy_caches();
  4235. return -ENOMEM;
  4236. }
  4237. /*
  4238. * Caculate mmu pages needed for kvm.
  4239. */
  4240. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm)
  4241. {
  4242. unsigned int nr_mmu_pages;
  4243. unsigned int nr_pages = 0;
  4244. struct kvm_memslots *slots;
  4245. struct kvm_memory_slot *memslot;
  4246. int i;
  4247. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  4248. slots = __kvm_memslots(kvm, i);
  4249. kvm_for_each_memslot(memslot, slots)
  4250. nr_pages += memslot->npages;
  4251. }
  4252. nr_mmu_pages = nr_pages * KVM_PERMILLE_MMU_PAGES / 1000;
  4253. nr_mmu_pages = max(nr_mmu_pages,
  4254. (unsigned int) KVM_MIN_ALLOC_MMU_PAGES);
  4255. return nr_mmu_pages;
  4256. }
  4257. void kvm_mmu_destroy(struct kvm_vcpu *vcpu)
  4258. {
  4259. kvm_mmu_unload(vcpu);
  4260. free_mmu_pages(vcpu);
  4261. mmu_free_memory_caches(vcpu);
  4262. }
  4263. void kvm_mmu_module_exit(void)
  4264. {
  4265. mmu_destroy_caches();
  4266. percpu_counter_destroy(&kvm_total_used_mmu_pages);
  4267. unregister_shrinker(&mmu_shrinker);
  4268. mmu_audit_disable();
  4269. }