book3s_hv.c 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377
  1. /*
  2. * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  3. * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
  4. *
  5. * Authors:
  6. * Paul Mackerras <paulus@au1.ibm.com>
  7. * Alexander Graf <agraf@suse.de>
  8. * Kevin Wolf <mail@kevin-wolf.de>
  9. *
  10. * Description: KVM functions specific to running on Book 3S
  11. * processors in hypervisor mode (specifically POWER7 and later).
  12. *
  13. * This file is derived from arch/powerpc/kvm/book3s.c,
  14. * by Alexander Graf <agraf@suse.de>.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License, version 2, as
  18. * published by the Free Software Foundation.
  19. */
  20. #include <linux/kvm_host.h>
  21. #include <linux/kernel.h>
  22. #include <linux/err.h>
  23. #include <linux/slab.h>
  24. #include <linux/preempt.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/sched/stat.h>
  27. #include <linux/delay.h>
  28. #include <linux/export.h>
  29. #include <linux/fs.h>
  30. #include <linux/anon_inodes.h>
  31. #include <linux/cpu.h>
  32. #include <linux/cpumask.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/page-flags.h>
  35. #include <linux/srcu.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/gfp.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/highmem.h>
  41. #include <linux/hugetlb.h>
  42. #include <linux/kvm_irqfd.h>
  43. #include <linux/irqbypass.h>
  44. #include <linux/module.h>
  45. #include <linux/compiler.h>
  46. #include <linux/of.h>
  47. #include <asm/ftrace.h>
  48. #include <asm/reg.h>
  49. #include <asm/ppc-opcode.h>
  50. #include <asm/asm-prototypes.h>
  51. #include <asm/archrandom.h>
  52. #include <asm/debug.h>
  53. #include <asm/disassemble.h>
  54. #include <asm/cputable.h>
  55. #include <asm/cacheflush.h>
  56. #include <linux/uaccess.h>
  57. #include <asm/io.h>
  58. #include <asm/kvm_ppc.h>
  59. #include <asm/kvm_book3s.h>
  60. #include <asm/mmu_context.h>
  61. #include <asm/lppaca.h>
  62. #include <asm/processor.h>
  63. #include <asm/cputhreads.h>
  64. #include <asm/page.h>
  65. #include <asm/hvcall.h>
  66. #include <asm/switch_to.h>
  67. #include <asm/smp.h>
  68. #include <asm/dbell.h>
  69. #include <asm/hmi.h>
  70. #include <asm/pnv-pci.h>
  71. #include <asm/mmu.h>
  72. #include <asm/opal.h>
  73. #include <asm/xics.h>
  74. #include <asm/xive.h>
  75. #include "book3s.h"
  76. #define CREATE_TRACE_POINTS
  77. #include "trace_hv.h"
  78. /* #define EXIT_DEBUG */
  79. /* #define EXIT_DEBUG_SIMPLE */
  80. /* #define EXIT_DEBUG_INT */
  81. /* Used to indicate that a guest page fault needs to be handled */
  82. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  83. /* Used to indicate that a guest passthrough interrupt needs to be handled */
  84. #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
  85. /* Used as a "null" value for timebase values */
  86. #define TB_NIL (~(u64)0)
  87. static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
  88. static int dynamic_mt_modes = 6;
  89. module_param(dynamic_mt_modes, int, 0644);
  90. MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
  91. static int target_smt_mode;
  92. module_param(target_smt_mode, int, 0644);
  93. MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
  94. static bool indep_threads_mode = true;
  95. module_param(indep_threads_mode, bool, S_IRUGO | S_IWUSR);
  96. MODULE_PARM_DESC(indep_threads_mode, "Independent-threads mode (only on POWER9)");
  97. static bool one_vm_per_core;
  98. module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires indep_threads_mode=N)");
  100. #ifdef CONFIG_KVM_XICS
  101. static struct kernel_param_ops module_param_ops = {
  102. .set = param_set_int,
  103. .get = param_get_int,
  104. };
  105. module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644);
  106. MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
  107. module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
  108. MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
  109. #endif
  110. /* If set, guests are allowed to create and control nested guests */
  111. static bool nested = true;
  112. module_param(nested, bool, S_IRUGO | S_IWUSR);
  113. MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
  114. static inline bool nesting_enabled(struct kvm *kvm)
  115. {
  116. return kvm->arch.nested_enable && kvm_is_radix(kvm);
  117. }
  118. /* If set, the threads on each CPU core have to be in the same MMU mode */
  119. static bool no_mixing_hpt_and_radix;
  120. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  121. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  122. /*
  123. * RWMR values for POWER8. These control the rate at which PURR
  124. * and SPURR count and should be set according to the number of
  125. * online threads in the vcore being run.
  126. */
  127. #define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL
  128. #define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL
  129. #define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL
  130. #define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL
  131. #define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL
  132. #define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL
  133. #define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL
  134. #define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL
  135. static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = {
  136. RWMR_RPA_P8_1THREAD,
  137. RWMR_RPA_P8_1THREAD,
  138. RWMR_RPA_P8_2THREAD,
  139. RWMR_RPA_P8_3THREAD,
  140. RWMR_RPA_P8_4THREAD,
  141. RWMR_RPA_P8_5THREAD,
  142. RWMR_RPA_P8_6THREAD,
  143. RWMR_RPA_P8_7THREAD,
  144. RWMR_RPA_P8_8THREAD,
  145. };
  146. static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
  147. int *ip)
  148. {
  149. int i = *ip;
  150. struct kvm_vcpu *vcpu;
  151. while (++i < MAX_SMT_THREADS) {
  152. vcpu = READ_ONCE(vc->runnable_threads[i]);
  153. if (vcpu) {
  154. *ip = i;
  155. return vcpu;
  156. }
  157. }
  158. return NULL;
  159. }
  160. /* Used to traverse the list of runnable threads for a given vcore */
  161. #define for_each_runnable_thread(i, vcpu, vc) \
  162. for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
  163. static bool kvmppc_ipi_thread(int cpu)
  164. {
  165. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  166. /* If we're a nested hypervisor, fall back to ordinary IPIs for now */
  167. if (kvmhv_on_pseries())
  168. return false;
  169. /* On POWER9 we can use msgsnd to IPI any cpu */
  170. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  171. msg |= get_hard_smp_processor_id(cpu);
  172. smp_mb();
  173. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  174. return true;
  175. }
  176. /* On POWER8 for IPIs to threads in the same core, use msgsnd */
  177. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  178. preempt_disable();
  179. if (cpu_first_thread_sibling(cpu) ==
  180. cpu_first_thread_sibling(smp_processor_id())) {
  181. msg |= cpu_thread_in_core(cpu);
  182. smp_mb();
  183. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  184. preempt_enable();
  185. return true;
  186. }
  187. preempt_enable();
  188. }
  189. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  190. if (cpu >= 0 && cpu < nr_cpu_ids) {
  191. if (paca_ptrs[cpu]->kvm_hstate.xics_phys) {
  192. xics_wake_cpu(cpu);
  193. return true;
  194. }
  195. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
  196. return true;
  197. }
  198. #endif
  199. return false;
  200. }
  201. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  202. {
  203. int cpu;
  204. struct swait_queue_head *wqp;
  205. wqp = kvm_arch_vcpu_wq(vcpu);
  206. if (swq_has_sleeper(wqp)) {
  207. swake_up_one(wqp);
  208. ++vcpu->stat.halt_wakeup;
  209. }
  210. cpu = READ_ONCE(vcpu->arch.thread_cpu);
  211. if (cpu >= 0 && kvmppc_ipi_thread(cpu))
  212. return;
  213. /* CPU points to the first thread of the core */
  214. cpu = vcpu->cpu;
  215. if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
  216. smp_send_reschedule(cpu);
  217. }
  218. /*
  219. * We use the vcpu_load/put functions to measure stolen time.
  220. * Stolen time is counted as time when either the vcpu is able to
  221. * run as part of a virtual core, but the task running the vcore
  222. * is preempted or sleeping, or when the vcpu needs something done
  223. * in the kernel by the task running the vcpu, but that task is
  224. * preempted or sleeping. Those two things have to be counted
  225. * separately, since one of the vcpu tasks will take on the job
  226. * of running the core, and the other vcpu tasks in the vcore will
  227. * sleep waiting for it to do that, but that sleep shouldn't count
  228. * as stolen time.
  229. *
  230. * Hence we accumulate stolen time when the vcpu can run as part of
  231. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  232. * needs its task to do other things in the kernel (for example,
  233. * service a page fault) in busy_stolen. We don't accumulate
  234. * stolen time for a vcore when it is inactive, or for a vcpu
  235. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  236. * a misnomer; it means that the vcpu task is not executing in
  237. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  238. * the kernel. We don't have any way of dividing up that time
  239. * between time that the vcpu is genuinely stopped, time that
  240. * the task is actively working on behalf of the vcpu, and time
  241. * that the task is preempted, so we don't count any of it as
  242. * stolen.
  243. *
  244. * Updates to busy_stolen are protected by arch.tbacct_lock;
  245. * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
  246. * lock. The stolen times are measured in units of timebase ticks.
  247. * (Note that the != TB_NIL checks below are purely defensive;
  248. * they should never fail.)
  249. */
  250. static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
  251. {
  252. unsigned long flags;
  253. spin_lock_irqsave(&vc->stoltb_lock, flags);
  254. vc->preempt_tb = mftb();
  255. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  256. }
  257. static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
  258. {
  259. unsigned long flags;
  260. spin_lock_irqsave(&vc->stoltb_lock, flags);
  261. if (vc->preempt_tb != TB_NIL) {
  262. vc->stolen_tb += mftb() - vc->preempt_tb;
  263. vc->preempt_tb = TB_NIL;
  264. }
  265. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  266. }
  267. static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
  268. {
  269. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  270. unsigned long flags;
  271. /*
  272. * We can test vc->runner without taking the vcore lock,
  273. * because only this task ever sets vc->runner to this
  274. * vcpu, and once it is set to this vcpu, only this task
  275. * ever sets it to NULL.
  276. */
  277. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  278. kvmppc_core_end_stolen(vc);
  279. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  280. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  281. vcpu->arch.busy_preempt != TB_NIL) {
  282. vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
  283. vcpu->arch.busy_preempt = TB_NIL;
  284. }
  285. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  286. }
  287. static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
  288. {
  289. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  290. unsigned long flags;
  291. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  292. kvmppc_core_start_stolen(vc);
  293. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  294. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  295. vcpu->arch.busy_preempt = mftb();
  296. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  297. }
  298. static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
  299. {
  300. /*
  301. * Check for illegal transactional state bit combination
  302. * and if we find it, force the TS field to a safe state.
  303. */
  304. if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
  305. msr &= ~MSR_TS_MASK;
  306. vcpu->arch.shregs.msr = msr;
  307. kvmppc_end_cede(vcpu);
  308. }
  309. static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
  310. {
  311. vcpu->arch.pvr = pvr;
  312. }
  313. /* Dummy value used in computing PCR value below */
  314. #define PCR_ARCH_300 (PCR_ARCH_207 << 1)
  315. static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
  316. {
  317. unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
  318. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  319. /* We can (emulate) our own architecture version and anything older */
  320. if (cpu_has_feature(CPU_FTR_ARCH_300))
  321. host_pcr_bit = PCR_ARCH_300;
  322. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  323. host_pcr_bit = PCR_ARCH_207;
  324. else if (cpu_has_feature(CPU_FTR_ARCH_206))
  325. host_pcr_bit = PCR_ARCH_206;
  326. else
  327. host_pcr_bit = PCR_ARCH_205;
  328. /* Determine lowest PCR bit needed to run guest in given PVR level */
  329. guest_pcr_bit = host_pcr_bit;
  330. if (arch_compat) {
  331. switch (arch_compat) {
  332. case PVR_ARCH_205:
  333. guest_pcr_bit = PCR_ARCH_205;
  334. break;
  335. case PVR_ARCH_206:
  336. case PVR_ARCH_206p:
  337. guest_pcr_bit = PCR_ARCH_206;
  338. break;
  339. case PVR_ARCH_207:
  340. guest_pcr_bit = PCR_ARCH_207;
  341. break;
  342. case PVR_ARCH_300:
  343. guest_pcr_bit = PCR_ARCH_300;
  344. break;
  345. default:
  346. return -EINVAL;
  347. }
  348. }
  349. /* Check requested PCR bits don't exceed our capabilities */
  350. if (guest_pcr_bit > host_pcr_bit)
  351. return -EINVAL;
  352. spin_lock(&vc->lock);
  353. vc->arch_compat = arch_compat;
  354. /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */
  355. vc->pcr = host_pcr_bit - guest_pcr_bit;
  356. spin_unlock(&vc->lock);
  357. return 0;
  358. }
  359. static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  360. {
  361. int r;
  362. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  363. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  364. vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap);
  365. for (r = 0; r < 16; ++r)
  366. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  367. r, kvmppc_get_gpr(vcpu, r),
  368. r+16, kvmppc_get_gpr(vcpu, r+16));
  369. pr_err("ctr = %.16lx lr = %.16lx\n",
  370. vcpu->arch.regs.ctr, vcpu->arch.regs.link);
  371. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  372. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  373. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  374. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  375. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  376. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  377. pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n",
  378. vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);
  379. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  380. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  381. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  382. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  383. for (r = 0; r < vcpu->arch.slb_max; ++r)
  384. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  385. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  386. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  387. vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
  388. vcpu->arch.last_inst);
  389. }
  390. static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  391. {
  392. struct kvm_vcpu *ret;
  393. mutex_lock(&kvm->lock);
  394. ret = kvm_get_vcpu_by_id(kvm, id);
  395. mutex_unlock(&kvm->lock);
  396. return ret;
  397. }
  398. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  399. {
  400. vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
  401. vpa->yield_count = cpu_to_be32(1);
  402. }
  403. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  404. unsigned long addr, unsigned long len)
  405. {
  406. /* check address is cacheline aligned */
  407. if (addr & (L1_CACHE_BYTES - 1))
  408. return -EINVAL;
  409. spin_lock(&vcpu->arch.vpa_update_lock);
  410. if (v->next_gpa != addr || v->len != len) {
  411. v->next_gpa = addr;
  412. v->len = addr ? len : 0;
  413. v->update_pending = 1;
  414. }
  415. spin_unlock(&vcpu->arch.vpa_update_lock);
  416. return 0;
  417. }
  418. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  419. struct reg_vpa {
  420. u32 dummy;
  421. union {
  422. __be16 hword;
  423. __be32 word;
  424. } length;
  425. };
  426. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  427. {
  428. if (vpap->update_pending)
  429. return vpap->next_gpa != 0;
  430. return vpap->pinned_addr != NULL;
  431. }
  432. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  433. unsigned long flags,
  434. unsigned long vcpuid, unsigned long vpa)
  435. {
  436. struct kvm *kvm = vcpu->kvm;
  437. unsigned long len, nb;
  438. void *va;
  439. struct kvm_vcpu *tvcpu;
  440. int err;
  441. int subfunc;
  442. struct kvmppc_vpa *vpap;
  443. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  444. if (!tvcpu)
  445. return H_PARAMETER;
  446. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  447. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  448. subfunc == H_VPA_REG_SLB) {
  449. /* Registering new area - address must be cache-line aligned */
  450. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  451. return H_PARAMETER;
  452. /* convert logical addr to kernel addr and read length */
  453. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  454. if (va == NULL)
  455. return H_PARAMETER;
  456. if (subfunc == H_VPA_REG_VPA)
  457. len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
  458. else
  459. len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
  460. kvmppc_unpin_guest_page(kvm, va, vpa, false);
  461. /* Check length */
  462. if (len > nb || len < sizeof(struct reg_vpa))
  463. return H_PARAMETER;
  464. } else {
  465. vpa = 0;
  466. len = 0;
  467. }
  468. err = H_PARAMETER;
  469. vpap = NULL;
  470. spin_lock(&tvcpu->arch.vpa_update_lock);
  471. switch (subfunc) {
  472. case H_VPA_REG_VPA: /* register VPA */
  473. /*
  474. * The size of our lppaca is 1kB because of the way we align
  475. * it for the guest to avoid crossing a 4kB boundary. We only
  476. * use 640 bytes of the structure though, so we should accept
  477. * clients that set a size of 640.
  478. */
  479. BUILD_BUG_ON(sizeof(struct lppaca) != 640);
  480. if (len < sizeof(struct lppaca))
  481. break;
  482. vpap = &tvcpu->arch.vpa;
  483. err = 0;
  484. break;
  485. case H_VPA_REG_DTL: /* register DTL */
  486. if (len < sizeof(struct dtl_entry))
  487. break;
  488. len -= len % sizeof(struct dtl_entry);
  489. /* Check that they have previously registered a VPA */
  490. err = H_RESOURCE;
  491. if (!vpa_is_registered(&tvcpu->arch.vpa))
  492. break;
  493. vpap = &tvcpu->arch.dtl;
  494. err = 0;
  495. break;
  496. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  497. /* Check that they have previously registered a VPA */
  498. err = H_RESOURCE;
  499. if (!vpa_is_registered(&tvcpu->arch.vpa))
  500. break;
  501. vpap = &tvcpu->arch.slb_shadow;
  502. err = 0;
  503. break;
  504. case H_VPA_DEREG_VPA: /* deregister VPA */
  505. /* Check they don't still have a DTL or SLB buf registered */
  506. err = H_RESOURCE;
  507. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  508. vpa_is_registered(&tvcpu->arch.slb_shadow))
  509. break;
  510. vpap = &tvcpu->arch.vpa;
  511. err = 0;
  512. break;
  513. case H_VPA_DEREG_DTL: /* deregister DTL */
  514. vpap = &tvcpu->arch.dtl;
  515. err = 0;
  516. break;
  517. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  518. vpap = &tvcpu->arch.slb_shadow;
  519. err = 0;
  520. break;
  521. }
  522. if (vpap) {
  523. vpap->next_gpa = vpa;
  524. vpap->len = len;
  525. vpap->update_pending = 1;
  526. }
  527. spin_unlock(&tvcpu->arch.vpa_update_lock);
  528. return err;
  529. }
  530. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  531. {
  532. struct kvm *kvm = vcpu->kvm;
  533. void *va;
  534. unsigned long nb;
  535. unsigned long gpa;
  536. /*
  537. * We need to pin the page pointed to by vpap->next_gpa,
  538. * but we can't call kvmppc_pin_guest_page under the lock
  539. * as it does get_user_pages() and down_read(). So we
  540. * have to drop the lock, pin the page, then get the lock
  541. * again and check that a new area didn't get registered
  542. * in the meantime.
  543. */
  544. for (;;) {
  545. gpa = vpap->next_gpa;
  546. spin_unlock(&vcpu->arch.vpa_update_lock);
  547. va = NULL;
  548. nb = 0;
  549. if (gpa)
  550. va = kvmppc_pin_guest_page(kvm, gpa, &nb);
  551. spin_lock(&vcpu->arch.vpa_update_lock);
  552. if (gpa == vpap->next_gpa)
  553. break;
  554. /* sigh... unpin that one and try again */
  555. if (va)
  556. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  557. }
  558. vpap->update_pending = 0;
  559. if (va && nb < vpap->len) {
  560. /*
  561. * If it's now too short, it must be that userspace
  562. * has changed the mappings underlying guest memory,
  563. * so unregister the region.
  564. */
  565. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  566. va = NULL;
  567. }
  568. if (vpap->pinned_addr)
  569. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
  570. vpap->dirty);
  571. vpap->gpa = gpa;
  572. vpap->pinned_addr = va;
  573. vpap->dirty = false;
  574. if (va)
  575. vpap->pinned_end = va + vpap->len;
  576. }
  577. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  578. {
  579. if (!(vcpu->arch.vpa.update_pending ||
  580. vcpu->arch.slb_shadow.update_pending ||
  581. vcpu->arch.dtl.update_pending))
  582. return;
  583. spin_lock(&vcpu->arch.vpa_update_lock);
  584. if (vcpu->arch.vpa.update_pending) {
  585. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  586. if (vcpu->arch.vpa.pinned_addr)
  587. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  588. }
  589. if (vcpu->arch.dtl.update_pending) {
  590. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  591. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  592. vcpu->arch.dtl_index = 0;
  593. }
  594. if (vcpu->arch.slb_shadow.update_pending)
  595. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  596. spin_unlock(&vcpu->arch.vpa_update_lock);
  597. }
  598. /*
  599. * Return the accumulated stolen time for the vcore up until `now'.
  600. * The caller should hold the vcore lock.
  601. */
  602. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  603. {
  604. u64 p;
  605. unsigned long flags;
  606. spin_lock_irqsave(&vc->stoltb_lock, flags);
  607. p = vc->stolen_tb;
  608. if (vc->vcore_state != VCORE_INACTIVE &&
  609. vc->preempt_tb != TB_NIL)
  610. p += now - vc->preempt_tb;
  611. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  612. return p;
  613. }
  614. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  615. struct kvmppc_vcore *vc)
  616. {
  617. struct dtl_entry *dt;
  618. struct lppaca *vpa;
  619. unsigned long stolen;
  620. unsigned long core_stolen;
  621. u64 now;
  622. unsigned long flags;
  623. dt = vcpu->arch.dtl_ptr;
  624. vpa = vcpu->arch.vpa.pinned_addr;
  625. now = mftb();
  626. core_stolen = vcore_stolen_time(vc, now);
  627. stolen = core_stolen - vcpu->arch.stolen_logged;
  628. vcpu->arch.stolen_logged = core_stolen;
  629. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  630. stolen += vcpu->arch.busy_stolen;
  631. vcpu->arch.busy_stolen = 0;
  632. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  633. if (!dt || !vpa)
  634. return;
  635. memset(dt, 0, sizeof(struct dtl_entry));
  636. dt->dispatch_reason = 7;
  637. dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
  638. dt->timebase = cpu_to_be64(now + vc->tb_offset);
  639. dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
  640. dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
  641. dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
  642. ++dt;
  643. if (dt == vcpu->arch.dtl.pinned_end)
  644. dt = vcpu->arch.dtl.pinned_addr;
  645. vcpu->arch.dtl_ptr = dt;
  646. /* order writing *dt vs. writing vpa->dtl_idx */
  647. smp_wmb();
  648. vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
  649. vcpu->arch.dtl.dirty = true;
  650. }
  651. /* See if there is a doorbell interrupt pending for a vcpu */
  652. static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
  653. {
  654. int thr;
  655. struct kvmppc_vcore *vc;
  656. if (vcpu->arch.doorbell_request)
  657. return true;
  658. /*
  659. * Ensure that the read of vcore->dpdes comes after the read
  660. * of vcpu->doorbell_request. This barrier matches the
  661. * smb_wmb() in kvmppc_guest_entry_inject().
  662. */
  663. smp_rmb();
  664. vc = vcpu->arch.vcore;
  665. thr = vcpu->vcpu_id - vc->first_vcpuid;
  666. return !!(vc->dpdes & (1 << thr));
  667. }
  668. static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
  669. {
  670. if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
  671. return true;
  672. if ((!vcpu->arch.vcore->arch_compat) &&
  673. cpu_has_feature(CPU_FTR_ARCH_207S))
  674. return true;
  675. return false;
  676. }
  677. static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
  678. unsigned long resource, unsigned long value1,
  679. unsigned long value2)
  680. {
  681. switch (resource) {
  682. case H_SET_MODE_RESOURCE_SET_CIABR:
  683. if (!kvmppc_power8_compatible(vcpu))
  684. return H_P2;
  685. if (value2)
  686. return H_P4;
  687. if (mflags)
  688. return H_UNSUPPORTED_FLAG_START;
  689. /* Guests can't breakpoint the hypervisor */
  690. if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
  691. return H_P3;
  692. vcpu->arch.ciabr = value1;
  693. return H_SUCCESS;
  694. case H_SET_MODE_RESOURCE_SET_DAWR:
  695. if (!kvmppc_power8_compatible(vcpu))
  696. return H_P2;
  697. if (!ppc_breakpoint_available())
  698. return H_P2;
  699. if (mflags)
  700. return H_UNSUPPORTED_FLAG_START;
  701. if (value2 & DABRX_HYP)
  702. return H_P4;
  703. vcpu->arch.dawr = value1;
  704. vcpu->arch.dawrx = value2;
  705. return H_SUCCESS;
  706. default:
  707. return H_TOO_HARD;
  708. }
  709. }
  710. static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
  711. {
  712. struct kvmppc_vcore *vcore = target->arch.vcore;
  713. /*
  714. * We expect to have been called by the real mode handler
  715. * (kvmppc_rm_h_confer()) which would have directly returned
  716. * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
  717. * have useful work to do and should not confer) so we don't
  718. * recheck that here.
  719. */
  720. spin_lock(&vcore->lock);
  721. if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
  722. vcore->vcore_state != VCORE_INACTIVE &&
  723. vcore->runner)
  724. target = vcore->runner;
  725. spin_unlock(&vcore->lock);
  726. return kvm_vcpu_yield_to(target);
  727. }
  728. static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
  729. {
  730. int yield_count = 0;
  731. struct lppaca *lppaca;
  732. spin_lock(&vcpu->arch.vpa_update_lock);
  733. lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
  734. if (lppaca)
  735. yield_count = be32_to_cpu(lppaca->yield_count);
  736. spin_unlock(&vcpu->arch.vpa_update_lock);
  737. return yield_count;
  738. }
  739. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  740. {
  741. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  742. unsigned long target, ret = H_SUCCESS;
  743. int yield_count;
  744. struct kvm_vcpu *tvcpu;
  745. int idx, rc;
  746. if (req <= MAX_HCALL_OPCODE &&
  747. !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
  748. return RESUME_HOST;
  749. switch (req) {
  750. case H_CEDE:
  751. break;
  752. case H_PROD:
  753. target = kvmppc_get_gpr(vcpu, 4);
  754. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  755. if (!tvcpu) {
  756. ret = H_PARAMETER;
  757. break;
  758. }
  759. tvcpu->arch.prodded = 1;
  760. smp_mb();
  761. if (tvcpu->arch.ceded)
  762. kvmppc_fast_vcpu_kick_hv(tvcpu);
  763. break;
  764. case H_CONFER:
  765. target = kvmppc_get_gpr(vcpu, 4);
  766. if (target == -1)
  767. break;
  768. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  769. if (!tvcpu) {
  770. ret = H_PARAMETER;
  771. break;
  772. }
  773. yield_count = kvmppc_get_gpr(vcpu, 5);
  774. if (kvmppc_get_yield_count(tvcpu) != yield_count)
  775. break;
  776. kvm_arch_vcpu_yield_to(tvcpu);
  777. break;
  778. case H_REGISTER_VPA:
  779. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  780. kvmppc_get_gpr(vcpu, 5),
  781. kvmppc_get_gpr(vcpu, 6));
  782. break;
  783. case H_RTAS:
  784. if (list_empty(&vcpu->kvm->arch.rtas_tokens))
  785. return RESUME_HOST;
  786. idx = srcu_read_lock(&vcpu->kvm->srcu);
  787. rc = kvmppc_rtas_hcall(vcpu);
  788. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  789. if (rc == -ENOENT)
  790. return RESUME_HOST;
  791. else if (rc == 0)
  792. break;
  793. /* Send the error out to userspace via KVM_RUN */
  794. return rc;
  795. case H_LOGICAL_CI_LOAD:
  796. ret = kvmppc_h_logical_ci_load(vcpu);
  797. if (ret == H_TOO_HARD)
  798. return RESUME_HOST;
  799. break;
  800. case H_LOGICAL_CI_STORE:
  801. ret = kvmppc_h_logical_ci_store(vcpu);
  802. if (ret == H_TOO_HARD)
  803. return RESUME_HOST;
  804. break;
  805. case H_SET_MODE:
  806. ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
  807. kvmppc_get_gpr(vcpu, 5),
  808. kvmppc_get_gpr(vcpu, 6),
  809. kvmppc_get_gpr(vcpu, 7));
  810. if (ret == H_TOO_HARD)
  811. return RESUME_HOST;
  812. break;
  813. case H_XIRR:
  814. case H_CPPR:
  815. case H_EOI:
  816. case H_IPI:
  817. case H_IPOLL:
  818. case H_XIRR_X:
  819. if (kvmppc_xics_enabled(vcpu)) {
  820. if (xive_enabled()) {
  821. ret = H_NOT_AVAILABLE;
  822. return RESUME_GUEST;
  823. }
  824. ret = kvmppc_xics_hcall(vcpu, req);
  825. break;
  826. }
  827. return RESUME_HOST;
  828. case H_SET_DABR:
  829. ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4));
  830. break;
  831. case H_SET_XDABR:
  832. ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4),
  833. kvmppc_get_gpr(vcpu, 5));
  834. break;
  835. case H_GET_TCE:
  836. ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  837. kvmppc_get_gpr(vcpu, 5));
  838. if (ret == H_TOO_HARD)
  839. return RESUME_HOST;
  840. break;
  841. case H_PUT_TCE:
  842. ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  843. kvmppc_get_gpr(vcpu, 5),
  844. kvmppc_get_gpr(vcpu, 6));
  845. if (ret == H_TOO_HARD)
  846. return RESUME_HOST;
  847. break;
  848. case H_PUT_TCE_INDIRECT:
  849. ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
  850. kvmppc_get_gpr(vcpu, 5),
  851. kvmppc_get_gpr(vcpu, 6),
  852. kvmppc_get_gpr(vcpu, 7));
  853. if (ret == H_TOO_HARD)
  854. return RESUME_HOST;
  855. break;
  856. case H_STUFF_TCE:
  857. ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  858. kvmppc_get_gpr(vcpu, 5),
  859. kvmppc_get_gpr(vcpu, 6),
  860. kvmppc_get_gpr(vcpu, 7));
  861. if (ret == H_TOO_HARD)
  862. return RESUME_HOST;
  863. break;
  864. case H_RANDOM:
  865. if (!powernv_get_random_long(&vcpu->arch.regs.gpr[4]))
  866. ret = H_HARDWARE;
  867. break;
  868. case H_SET_PARTITION_TABLE:
  869. ret = H_FUNCTION;
  870. if (nesting_enabled(vcpu->kvm))
  871. ret = kvmhv_set_partition_table(vcpu);
  872. break;
  873. case H_ENTER_NESTED:
  874. ret = H_FUNCTION;
  875. if (!nesting_enabled(vcpu->kvm))
  876. break;
  877. ret = kvmhv_enter_nested_guest(vcpu);
  878. if (ret == H_INTERRUPT) {
  879. kvmppc_set_gpr(vcpu, 3, 0);
  880. return -EINTR;
  881. }
  882. break;
  883. case H_TLB_INVALIDATE:
  884. ret = H_FUNCTION;
  885. if (nesting_enabled(vcpu->kvm))
  886. ret = kvmhv_do_nested_tlbie(vcpu);
  887. break;
  888. default:
  889. return RESUME_HOST;
  890. }
  891. kvmppc_set_gpr(vcpu, 3, ret);
  892. vcpu->arch.hcall_needed = 0;
  893. return RESUME_GUEST;
  894. }
  895. /*
  896. * Handle H_CEDE in the nested virtualization case where we haven't
  897. * called the real-mode hcall handlers in book3s_hv_rmhandlers.S.
  898. * This has to be done early, not in kvmppc_pseries_do_hcall(), so
  899. * that the cede logic in kvmppc_run_single_vcpu() works properly.
  900. */
  901. static void kvmppc_nested_cede(struct kvm_vcpu *vcpu)
  902. {
  903. vcpu->arch.shregs.msr |= MSR_EE;
  904. vcpu->arch.ceded = 1;
  905. smp_mb();
  906. if (vcpu->arch.prodded) {
  907. vcpu->arch.prodded = 0;
  908. smp_mb();
  909. vcpu->arch.ceded = 0;
  910. }
  911. }
  912. static int kvmppc_hcall_impl_hv(unsigned long cmd)
  913. {
  914. switch (cmd) {
  915. case H_CEDE:
  916. case H_PROD:
  917. case H_CONFER:
  918. case H_REGISTER_VPA:
  919. case H_SET_MODE:
  920. case H_LOGICAL_CI_LOAD:
  921. case H_LOGICAL_CI_STORE:
  922. #ifdef CONFIG_KVM_XICS
  923. case H_XIRR:
  924. case H_CPPR:
  925. case H_EOI:
  926. case H_IPI:
  927. case H_IPOLL:
  928. case H_XIRR_X:
  929. #endif
  930. return 1;
  931. }
  932. /* See if it's in the real-mode table */
  933. return kvmppc_hcall_impl_hv_realmode(cmd);
  934. }
  935. static int kvmppc_emulate_debug_inst(struct kvm_run *run,
  936. struct kvm_vcpu *vcpu)
  937. {
  938. u32 last_inst;
  939. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  940. EMULATE_DONE) {
  941. /*
  942. * Fetch failed, so return to guest and
  943. * try executing it again.
  944. */
  945. return RESUME_GUEST;
  946. }
  947. if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
  948. run->exit_reason = KVM_EXIT_DEBUG;
  949. run->debug.arch.address = kvmppc_get_pc(vcpu);
  950. return RESUME_HOST;
  951. } else {
  952. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  953. return RESUME_GUEST;
  954. }
  955. }
  956. static void do_nothing(void *x)
  957. {
  958. }
  959. static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
  960. {
  961. int thr, cpu, pcpu, nthreads;
  962. struct kvm_vcpu *v;
  963. unsigned long dpdes;
  964. nthreads = vcpu->kvm->arch.emul_smt_mode;
  965. dpdes = 0;
  966. cpu = vcpu->vcpu_id & ~(nthreads - 1);
  967. for (thr = 0; thr < nthreads; ++thr, ++cpu) {
  968. v = kvmppc_find_vcpu(vcpu->kvm, cpu);
  969. if (!v)
  970. continue;
  971. /*
  972. * If the vcpu is currently running on a physical cpu thread,
  973. * interrupt it in order to pull it out of the guest briefly,
  974. * which will update its vcore->dpdes value.
  975. */
  976. pcpu = READ_ONCE(v->cpu);
  977. if (pcpu >= 0)
  978. smp_call_function_single(pcpu, do_nothing, NULL, 1);
  979. if (kvmppc_doorbell_pending(v))
  980. dpdes |= 1 << thr;
  981. }
  982. return dpdes;
  983. }
  984. /*
  985. * On POWER9, emulate doorbell-related instructions in order to
  986. * give the guest the illusion of running on a multi-threaded core.
  987. * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
  988. * and mfspr DPDES.
  989. */
  990. static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
  991. {
  992. u32 inst, rb, thr;
  993. unsigned long arg;
  994. struct kvm *kvm = vcpu->kvm;
  995. struct kvm_vcpu *tvcpu;
  996. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE)
  997. return RESUME_GUEST;
  998. if (get_op(inst) != 31)
  999. return EMULATE_FAIL;
  1000. rb = get_rb(inst);
  1001. thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
  1002. switch (get_xop(inst)) {
  1003. case OP_31_XOP_MSGSNDP:
  1004. arg = kvmppc_get_gpr(vcpu, rb);
  1005. if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER)
  1006. break;
  1007. arg &= 0x3f;
  1008. if (arg >= kvm->arch.emul_smt_mode)
  1009. break;
  1010. tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
  1011. if (!tvcpu)
  1012. break;
  1013. if (!tvcpu->arch.doorbell_request) {
  1014. tvcpu->arch.doorbell_request = 1;
  1015. kvmppc_fast_vcpu_kick_hv(tvcpu);
  1016. }
  1017. break;
  1018. case OP_31_XOP_MSGCLRP:
  1019. arg = kvmppc_get_gpr(vcpu, rb);
  1020. if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER)
  1021. break;
  1022. vcpu->arch.vcore->dpdes = 0;
  1023. vcpu->arch.doorbell_request = 0;
  1024. break;
  1025. case OP_31_XOP_MFSPR:
  1026. switch (get_sprn(inst)) {
  1027. case SPRN_TIR:
  1028. arg = thr;
  1029. break;
  1030. case SPRN_DPDES:
  1031. arg = kvmppc_read_dpdes(vcpu);
  1032. break;
  1033. default:
  1034. return EMULATE_FAIL;
  1035. }
  1036. kvmppc_set_gpr(vcpu, get_rt(inst), arg);
  1037. break;
  1038. default:
  1039. return EMULATE_FAIL;
  1040. }
  1041. kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
  1042. return RESUME_GUEST;
  1043. }
  1044. static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1045. struct task_struct *tsk)
  1046. {
  1047. int r = RESUME_HOST;
  1048. vcpu->stat.sum_exits++;
  1049. /*
  1050. * This can happen if an interrupt occurs in the last stages
  1051. * of guest entry or the first stages of guest exit (i.e. after
  1052. * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
  1053. * and before setting it to KVM_GUEST_MODE_HOST_HV).
  1054. * That can happen due to a bug, or due to a machine check
  1055. * occurring at just the wrong time.
  1056. */
  1057. if (vcpu->arch.shregs.msr & MSR_HV) {
  1058. printk(KERN_EMERG "KVM trap in HV mode!\n");
  1059. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  1060. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  1061. vcpu->arch.shregs.msr);
  1062. kvmppc_dump_regs(vcpu);
  1063. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1064. run->hw.hardware_exit_reason = vcpu->arch.trap;
  1065. return RESUME_HOST;
  1066. }
  1067. run->exit_reason = KVM_EXIT_UNKNOWN;
  1068. run->ready_for_interrupt_injection = 1;
  1069. switch (vcpu->arch.trap) {
  1070. /* We're good on these - the host merely wanted to get our attention */
  1071. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  1072. vcpu->stat.dec_exits++;
  1073. r = RESUME_GUEST;
  1074. break;
  1075. case BOOK3S_INTERRUPT_EXTERNAL:
  1076. case BOOK3S_INTERRUPT_H_DOORBELL:
  1077. case BOOK3S_INTERRUPT_H_VIRT:
  1078. vcpu->stat.ext_intr_exits++;
  1079. r = RESUME_GUEST;
  1080. break;
  1081. /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
  1082. case BOOK3S_INTERRUPT_HMI:
  1083. case BOOK3S_INTERRUPT_PERFMON:
  1084. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  1085. r = RESUME_GUEST;
  1086. break;
  1087. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  1088. /* Exit to guest with KVM_EXIT_NMI as exit reason */
  1089. run->exit_reason = KVM_EXIT_NMI;
  1090. run->hw.hardware_exit_reason = vcpu->arch.trap;
  1091. /* Clear out the old NMI status from run->flags */
  1092. run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
  1093. /* Now set the NMI status */
  1094. if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
  1095. run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
  1096. else
  1097. run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
  1098. r = RESUME_HOST;
  1099. /* Print the MCE event to host console. */
  1100. machine_check_print_event_info(&vcpu->arch.mce_evt, false);
  1101. break;
  1102. case BOOK3S_INTERRUPT_PROGRAM:
  1103. {
  1104. ulong flags;
  1105. /*
  1106. * Normally program interrupts are delivered directly
  1107. * to the guest by the hardware, but we can get here
  1108. * as a result of a hypervisor emulation interrupt
  1109. * (e40) getting turned into a 700 by BML RTAS.
  1110. */
  1111. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  1112. kvmppc_core_queue_program(vcpu, flags);
  1113. r = RESUME_GUEST;
  1114. break;
  1115. }
  1116. case BOOK3S_INTERRUPT_SYSCALL:
  1117. {
  1118. /* hcall - punt to userspace */
  1119. int i;
  1120. /* hypercall with MSR_PR has already been handled in rmode,
  1121. * and never reaches here.
  1122. */
  1123. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  1124. for (i = 0; i < 9; ++i)
  1125. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  1126. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  1127. vcpu->arch.hcall_needed = 1;
  1128. r = RESUME_HOST;
  1129. break;
  1130. }
  1131. /*
  1132. * We get these next two if the guest accesses a page which it thinks
  1133. * it has mapped but which is not actually present, either because
  1134. * it is for an emulated I/O device or because the corresonding
  1135. * host page has been paged out. Any other HDSI/HISI interrupts
  1136. * have been handled already.
  1137. */
  1138. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  1139. r = RESUME_PAGE_FAULT;
  1140. break;
  1141. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  1142. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  1143. vcpu->arch.fault_dsisr = vcpu->arch.shregs.msr &
  1144. DSISR_SRR1_MATCH_64S;
  1145. if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
  1146. vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
  1147. r = RESUME_PAGE_FAULT;
  1148. break;
  1149. /*
  1150. * This occurs if the guest executes an illegal instruction.
  1151. * If the guest debug is disabled, generate a program interrupt
  1152. * to the guest. If guest debug is enabled, we need to check
  1153. * whether the instruction is a software breakpoint instruction.
  1154. * Accordingly return to Guest or Host.
  1155. */
  1156. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  1157. if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
  1158. vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
  1159. swab32(vcpu->arch.emul_inst) :
  1160. vcpu->arch.emul_inst;
  1161. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
  1162. r = kvmppc_emulate_debug_inst(run, vcpu);
  1163. } else {
  1164. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  1165. r = RESUME_GUEST;
  1166. }
  1167. break;
  1168. /*
  1169. * This occurs if the guest (kernel or userspace), does something that
  1170. * is prohibited by HFSCR.
  1171. * On POWER9, this could be a doorbell instruction that we need
  1172. * to emulate.
  1173. * Otherwise, we just generate a program interrupt to the guest.
  1174. */
  1175. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
  1176. r = EMULATE_FAIL;
  1177. if (((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG) &&
  1178. cpu_has_feature(CPU_FTR_ARCH_300))
  1179. r = kvmppc_emulate_doorbell_instr(vcpu);
  1180. if (r == EMULATE_FAIL) {
  1181. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  1182. r = RESUME_GUEST;
  1183. }
  1184. break;
  1185. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1186. case BOOK3S_INTERRUPT_HV_SOFTPATCH:
  1187. /*
  1188. * This occurs for various TM-related instructions that
  1189. * we need to emulate on POWER9 DD2.2. We have already
  1190. * handled the cases where the guest was in real-suspend
  1191. * mode and was transitioning to transactional state.
  1192. */
  1193. r = kvmhv_p9_tm_emulation(vcpu);
  1194. break;
  1195. #endif
  1196. case BOOK3S_INTERRUPT_HV_RM_HARD:
  1197. r = RESUME_PASSTHROUGH;
  1198. break;
  1199. default:
  1200. kvmppc_dump_regs(vcpu);
  1201. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  1202. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  1203. vcpu->arch.shregs.msr);
  1204. run->hw.hardware_exit_reason = vcpu->arch.trap;
  1205. r = RESUME_HOST;
  1206. break;
  1207. }
  1208. return r;
  1209. }
  1210. static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
  1211. {
  1212. int r;
  1213. int srcu_idx;
  1214. vcpu->stat.sum_exits++;
  1215. /*
  1216. * This can happen if an interrupt occurs in the last stages
  1217. * of guest entry or the first stages of guest exit (i.e. after
  1218. * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
  1219. * and before setting it to KVM_GUEST_MODE_HOST_HV).
  1220. * That can happen due to a bug, or due to a machine check
  1221. * occurring at just the wrong time.
  1222. */
  1223. if (vcpu->arch.shregs.msr & MSR_HV) {
  1224. pr_emerg("KVM trap in HV mode while nested!\n");
  1225. pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  1226. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  1227. vcpu->arch.shregs.msr);
  1228. kvmppc_dump_regs(vcpu);
  1229. return RESUME_HOST;
  1230. }
  1231. switch (vcpu->arch.trap) {
  1232. /* We're good on these - the host merely wanted to get our attention */
  1233. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  1234. vcpu->stat.dec_exits++;
  1235. r = RESUME_GUEST;
  1236. break;
  1237. case BOOK3S_INTERRUPT_EXTERNAL:
  1238. vcpu->stat.ext_intr_exits++;
  1239. r = RESUME_HOST;
  1240. break;
  1241. case BOOK3S_INTERRUPT_H_DOORBELL:
  1242. case BOOK3S_INTERRUPT_H_VIRT:
  1243. vcpu->stat.ext_intr_exits++;
  1244. r = RESUME_GUEST;
  1245. break;
  1246. /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
  1247. case BOOK3S_INTERRUPT_HMI:
  1248. case BOOK3S_INTERRUPT_PERFMON:
  1249. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  1250. r = RESUME_GUEST;
  1251. break;
  1252. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  1253. /* Pass the machine check to the L1 guest */
  1254. r = RESUME_HOST;
  1255. /* Print the MCE event to host console. */
  1256. machine_check_print_event_info(&vcpu->arch.mce_evt, false);
  1257. break;
  1258. /*
  1259. * We get these next two if the guest accesses a page which it thinks
  1260. * it has mapped but which is not actually present, either because
  1261. * it is for an emulated I/O device or because the corresonding
  1262. * host page has been paged out.
  1263. */
  1264. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  1265. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1266. r = kvmhv_nested_page_fault(vcpu);
  1267. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1268. break;
  1269. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  1270. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  1271. vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) &
  1272. DSISR_SRR1_MATCH_64S;
  1273. if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
  1274. vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
  1275. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1276. r = kvmhv_nested_page_fault(vcpu);
  1277. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1278. break;
  1279. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1280. case BOOK3S_INTERRUPT_HV_SOFTPATCH:
  1281. /*
  1282. * This occurs for various TM-related instructions that
  1283. * we need to emulate on POWER9 DD2.2. We have already
  1284. * handled the cases where the guest was in real-suspend
  1285. * mode and was transitioning to transactional state.
  1286. */
  1287. r = kvmhv_p9_tm_emulation(vcpu);
  1288. break;
  1289. #endif
  1290. case BOOK3S_INTERRUPT_HV_RM_HARD:
  1291. vcpu->arch.trap = 0;
  1292. r = RESUME_GUEST;
  1293. if (!xive_enabled())
  1294. kvmppc_xics_rm_complete(vcpu, 0);
  1295. break;
  1296. default:
  1297. r = RESUME_HOST;
  1298. break;
  1299. }
  1300. return r;
  1301. }
  1302. static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
  1303. struct kvm_sregs *sregs)
  1304. {
  1305. int i;
  1306. memset(sregs, 0, sizeof(struct kvm_sregs));
  1307. sregs->pvr = vcpu->arch.pvr;
  1308. for (i = 0; i < vcpu->arch.slb_max; i++) {
  1309. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  1310. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  1311. }
  1312. return 0;
  1313. }
  1314. static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
  1315. struct kvm_sregs *sregs)
  1316. {
  1317. int i, j;
  1318. /* Only accept the same PVR as the host's, since we can't spoof it */
  1319. if (sregs->pvr != vcpu->arch.pvr)
  1320. return -EINVAL;
  1321. j = 0;
  1322. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  1323. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  1324. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  1325. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  1326. ++j;
  1327. }
  1328. }
  1329. vcpu->arch.slb_max = j;
  1330. return 0;
  1331. }
  1332. static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
  1333. bool preserve_top32)
  1334. {
  1335. struct kvm *kvm = vcpu->kvm;
  1336. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  1337. u64 mask;
  1338. mutex_lock(&kvm->lock);
  1339. spin_lock(&vc->lock);
  1340. /*
  1341. * If ILE (interrupt little-endian) has changed, update the
  1342. * MSR_LE bit in the intr_msr for each vcpu in this vcore.
  1343. */
  1344. if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
  1345. struct kvm_vcpu *vcpu;
  1346. int i;
  1347. kvm_for_each_vcpu(i, vcpu, kvm) {
  1348. if (vcpu->arch.vcore != vc)
  1349. continue;
  1350. if (new_lpcr & LPCR_ILE)
  1351. vcpu->arch.intr_msr |= MSR_LE;
  1352. else
  1353. vcpu->arch.intr_msr &= ~MSR_LE;
  1354. }
  1355. }
  1356. /*
  1357. * Userspace can only modify DPFD (default prefetch depth),
  1358. * ILE (interrupt little-endian) and TC (translation control).
  1359. * On POWER8 and POWER9 userspace can also modify AIL (alt. interrupt loc.).
  1360. */
  1361. mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
  1362. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  1363. mask |= LPCR_AIL;
  1364. /*
  1365. * On POWER9, allow userspace to enable large decrementer for the
  1366. * guest, whether or not the host has it enabled.
  1367. */
  1368. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1369. mask |= LPCR_LD;
  1370. /* Broken 32-bit version of LPCR must not clear top bits */
  1371. if (preserve_top32)
  1372. mask &= 0xFFFFFFFF;
  1373. vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
  1374. spin_unlock(&vc->lock);
  1375. mutex_unlock(&kvm->lock);
  1376. }
  1377. static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1378. union kvmppc_one_reg *val)
  1379. {
  1380. int r = 0;
  1381. long int i;
  1382. switch (id) {
  1383. case KVM_REG_PPC_DEBUG_INST:
  1384. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  1385. break;
  1386. case KVM_REG_PPC_HIOR:
  1387. *val = get_reg_val(id, 0);
  1388. break;
  1389. case KVM_REG_PPC_DABR:
  1390. *val = get_reg_val(id, vcpu->arch.dabr);
  1391. break;
  1392. case KVM_REG_PPC_DABRX:
  1393. *val = get_reg_val(id, vcpu->arch.dabrx);
  1394. break;
  1395. case KVM_REG_PPC_DSCR:
  1396. *val = get_reg_val(id, vcpu->arch.dscr);
  1397. break;
  1398. case KVM_REG_PPC_PURR:
  1399. *val = get_reg_val(id, vcpu->arch.purr);
  1400. break;
  1401. case KVM_REG_PPC_SPURR:
  1402. *val = get_reg_val(id, vcpu->arch.spurr);
  1403. break;
  1404. case KVM_REG_PPC_AMR:
  1405. *val = get_reg_val(id, vcpu->arch.amr);
  1406. break;
  1407. case KVM_REG_PPC_UAMOR:
  1408. *val = get_reg_val(id, vcpu->arch.uamor);
  1409. break;
  1410. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1411. i = id - KVM_REG_PPC_MMCR0;
  1412. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  1413. break;
  1414. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1415. i = id - KVM_REG_PPC_PMC1;
  1416. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  1417. break;
  1418. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1419. i = id - KVM_REG_PPC_SPMC1;
  1420. *val = get_reg_val(id, vcpu->arch.spmc[i]);
  1421. break;
  1422. case KVM_REG_PPC_SIAR:
  1423. *val = get_reg_val(id, vcpu->arch.siar);
  1424. break;
  1425. case KVM_REG_PPC_SDAR:
  1426. *val = get_reg_val(id, vcpu->arch.sdar);
  1427. break;
  1428. case KVM_REG_PPC_SIER:
  1429. *val = get_reg_val(id, vcpu->arch.sier);
  1430. break;
  1431. case KVM_REG_PPC_IAMR:
  1432. *val = get_reg_val(id, vcpu->arch.iamr);
  1433. break;
  1434. case KVM_REG_PPC_PSPB:
  1435. *val = get_reg_val(id, vcpu->arch.pspb);
  1436. break;
  1437. case KVM_REG_PPC_DPDES:
  1438. *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
  1439. break;
  1440. case KVM_REG_PPC_VTB:
  1441. *val = get_reg_val(id, vcpu->arch.vcore->vtb);
  1442. break;
  1443. case KVM_REG_PPC_DAWR:
  1444. *val = get_reg_val(id, vcpu->arch.dawr);
  1445. break;
  1446. case KVM_REG_PPC_DAWRX:
  1447. *val = get_reg_val(id, vcpu->arch.dawrx);
  1448. break;
  1449. case KVM_REG_PPC_CIABR:
  1450. *val = get_reg_val(id, vcpu->arch.ciabr);
  1451. break;
  1452. case KVM_REG_PPC_CSIGR:
  1453. *val = get_reg_val(id, vcpu->arch.csigr);
  1454. break;
  1455. case KVM_REG_PPC_TACR:
  1456. *val = get_reg_val(id, vcpu->arch.tacr);
  1457. break;
  1458. case KVM_REG_PPC_TCSCR:
  1459. *val = get_reg_val(id, vcpu->arch.tcscr);
  1460. break;
  1461. case KVM_REG_PPC_PID:
  1462. *val = get_reg_val(id, vcpu->arch.pid);
  1463. break;
  1464. case KVM_REG_PPC_ACOP:
  1465. *val = get_reg_val(id, vcpu->arch.acop);
  1466. break;
  1467. case KVM_REG_PPC_WORT:
  1468. *val = get_reg_val(id, vcpu->arch.wort);
  1469. break;
  1470. case KVM_REG_PPC_TIDR:
  1471. *val = get_reg_val(id, vcpu->arch.tid);
  1472. break;
  1473. case KVM_REG_PPC_PSSCR:
  1474. *val = get_reg_val(id, vcpu->arch.psscr);
  1475. break;
  1476. case KVM_REG_PPC_VPA_ADDR:
  1477. spin_lock(&vcpu->arch.vpa_update_lock);
  1478. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  1479. spin_unlock(&vcpu->arch.vpa_update_lock);
  1480. break;
  1481. case KVM_REG_PPC_VPA_SLB:
  1482. spin_lock(&vcpu->arch.vpa_update_lock);
  1483. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  1484. val->vpaval.length = vcpu->arch.slb_shadow.len;
  1485. spin_unlock(&vcpu->arch.vpa_update_lock);
  1486. break;
  1487. case KVM_REG_PPC_VPA_DTL:
  1488. spin_lock(&vcpu->arch.vpa_update_lock);
  1489. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  1490. val->vpaval.length = vcpu->arch.dtl.len;
  1491. spin_unlock(&vcpu->arch.vpa_update_lock);
  1492. break;
  1493. case KVM_REG_PPC_TB_OFFSET:
  1494. *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
  1495. break;
  1496. case KVM_REG_PPC_LPCR:
  1497. case KVM_REG_PPC_LPCR_64:
  1498. *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
  1499. break;
  1500. case KVM_REG_PPC_PPR:
  1501. *val = get_reg_val(id, vcpu->arch.ppr);
  1502. break;
  1503. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1504. case KVM_REG_PPC_TFHAR:
  1505. *val = get_reg_val(id, vcpu->arch.tfhar);
  1506. break;
  1507. case KVM_REG_PPC_TFIAR:
  1508. *val = get_reg_val(id, vcpu->arch.tfiar);
  1509. break;
  1510. case KVM_REG_PPC_TEXASR:
  1511. *val = get_reg_val(id, vcpu->arch.texasr);
  1512. break;
  1513. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1514. i = id - KVM_REG_PPC_TM_GPR0;
  1515. *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
  1516. break;
  1517. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1518. {
  1519. int j;
  1520. i = id - KVM_REG_PPC_TM_VSR0;
  1521. if (i < 32)
  1522. for (j = 0; j < TS_FPRWIDTH; j++)
  1523. val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
  1524. else {
  1525. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1526. val->vval = vcpu->arch.vr_tm.vr[i-32];
  1527. else
  1528. r = -ENXIO;
  1529. }
  1530. break;
  1531. }
  1532. case KVM_REG_PPC_TM_CR:
  1533. *val = get_reg_val(id, vcpu->arch.cr_tm);
  1534. break;
  1535. case KVM_REG_PPC_TM_XER:
  1536. *val = get_reg_val(id, vcpu->arch.xer_tm);
  1537. break;
  1538. case KVM_REG_PPC_TM_LR:
  1539. *val = get_reg_val(id, vcpu->arch.lr_tm);
  1540. break;
  1541. case KVM_REG_PPC_TM_CTR:
  1542. *val = get_reg_val(id, vcpu->arch.ctr_tm);
  1543. break;
  1544. case KVM_REG_PPC_TM_FPSCR:
  1545. *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
  1546. break;
  1547. case KVM_REG_PPC_TM_AMR:
  1548. *val = get_reg_val(id, vcpu->arch.amr_tm);
  1549. break;
  1550. case KVM_REG_PPC_TM_PPR:
  1551. *val = get_reg_val(id, vcpu->arch.ppr_tm);
  1552. break;
  1553. case KVM_REG_PPC_TM_VRSAVE:
  1554. *val = get_reg_val(id, vcpu->arch.vrsave_tm);
  1555. break;
  1556. case KVM_REG_PPC_TM_VSCR:
  1557. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1558. *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
  1559. else
  1560. r = -ENXIO;
  1561. break;
  1562. case KVM_REG_PPC_TM_DSCR:
  1563. *val = get_reg_val(id, vcpu->arch.dscr_tm);
  1564. break;
  1565. case KVM_REG_PPC_TM_TAR:
  1566. *val = get_reg_val(id, vcpu->arch.tar_tm);
  1567. break;
  1568. #endif
  1569. case KVM_REG_PPC_ARCH_COMPAT:
  1570. *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
  1571. break;
  1572. case KVM_REG_PPC_DEC_EXPIRY:
  1573. *val = get_reg_val(id, vcpu->arch.dec_expires +
  1574. vcpu->arch.vcore->tb_offset);
  1575. break;
  1576. case KVM_REG_PPC_ONLINE:
  1577. *val = get_reg_val(id, vcpu->arch.online);
  1578. break;
  1579. case KVM_REG_PPC_PTCR:
  1580. *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr);
  1581. break;
  1582. default:
  1583. r = -EINVAL;
  1584. break;
  1585. }
  1586. return r;
  1587. }
  1588. static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1589. union kvmppc_one_reg *val)
  1590. {
  1591. int r = 0;
  1592. long int i;
  1593. unsigned long addr, len;
  1594. switch (id) {
  1595. case KVM_REG_PPC_HIOR:
  1596. /* Only allow this to be set to zero */
  1597. if (set_reg_val(id, *val))
  1598. r = -EINVAL;
  1599. break;
  1600. case KVM_REG_PPC_DABR:
  1601. vcpu->arch.dabr = set_reg_val(id, *val);
  1602. break;
  1603. case KVM_REG_PPC_DABRX:
  1604. vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
  1605. break;
  1606. case KVM_REG_PPC_DSCR:
  1607. vcpu->arch.dscr = set_reg_val(id, *val);
  1608. break;
  1609. case KVM_REG_PPC_PURR:
  1610. vcpu->arch.purr = set_reg_val(id, *val);
  1611. break;
  1612. case KVM_REG_PPC_SPURR:
  1613. vcpu->arch.spurr = set_reg_val(id, *val);
  1614. break;
  1615. case KVM_REG_PPC_AMR:
  1616. vcpu->arch.amr = set_reg_val(id, *val);
  1617. break;
  1618. case KVM_REG_PPC_UAMOR:
  1619. vcpu->arch.uamor = set_reg_val(id, *val);
  1620. break;
  1621. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1622. i = id - KVM_REG_PPC_MMCR0;
  1623. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  1624. break;
  1625. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1626. i = id - KVM_REG_PPC_PMC1;
  1627. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  1628. break;
  1629. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1630. i = id - KVM_REG_PPC_SPMC1;
  1631. vcpu->arch.spmc[i] = set_reg_val(id, *val);
  1632. break;
  1633. case KVM_REG_PPC_SIAR:
  1634. vcpu->arch.siar = set_reg_val(id, *val);
  1635. break;
  1636. case KVM_REG_PPC_SDAR:
  1637. vcpu->arch.sdar = set_reg_val(id, *val);
  1638. break;
  1639. case KVM_REG_PPC_SIER:
  1640. vcpu->arch.sier = set_reg_val(id, *val);
  1641. break;
  1642. case KVM_REG_PPC_IAMR:
  1643. vcpu->arch.iamr = set_reg_val(id, *val);
  1644. break;
  1645. case KVM_REG_PPC_PSPB:
  1646. vcpu->arch.pspb = set_reg_val(id, *val);
  1647. break;
  1648. case KVM_REG_PPC_DPDES:
  1649. vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
  1650. break;
  1651. case KVM_REG_PPC_VTB:
  1652. vcpu->arch.vcore->vtb = set_reg_val(id, *val);
  1653. break;
  1654. case KVM_REG_PPC_DAWR:
  1655. vcpu->arch.dawr = set_reg_val(id, *val);
  1656. break;
  1657. case KVM_REG_PPC_DAWRX:
  1658. vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
  1659. break;
  1660. case KVM_REG_PPC_CIABR:
  1661. vcpu->arch.ciabr = set_reg_val(id, *val);
  1662. /* Don't allow setting breakpoints in hypervisor code */
  1663. if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
  1664. vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
  1665. break;
  1666. case KVM_REG_PPC_CSIGR:
  1667. vcpu->arch.csigr = set_reg_val(id, *val);
  1668. break;
  1669. case KVM_REG_PPC_TACR:
  1670. vcpu->arch.tacr = set_reg_val(id, *val);
  1671. break;
  1672. case KVM_REG_PPC_TCSCR:
  1673. vcpu->arch.tcscr = set_reg_val(id, *val);
  1674. break;
  1675. case KVM_REG_PPC_PID:
  1676. vcpu->arch.pid = set_reg_val(id, *val);
  1677. break;
  1678. case KVM_REG_PPC_ACOP:
  1679. vcpu->arch.acop = set_reg_val(id, *val);
  1680. break;
  1681. case KVM_REG_PPC_WORT:
  1682. vcpu->arch.wort = set_reg_val(id, *val);
  1683. break;
  1684. case KVM_REG_PPC_TIDR:
  1685. vcpu->arch.tid = set_reg_val(id, *val);
  1686. break;
  1687. case KVM_REG_PPC_PSSCR:
  1688. vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
  1689. break;
  1690. case KVM_REG_PPC_VPA_ADDR:
  1691. addr = set_reg_val(id, *val);
  1692. r = -EINVAL;
  1693. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  1694. vcpu->arch.dtl.next_gpa))
  1695. break;
  1696. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  1697. break;
  1698. case KVM_REG_PPC_VPA_SLB:
  1699. addr = val->vpaval.addr;
  1700. len = val->vpaval.length;
  1701. r = -EINVAL;
  1702. if (addr && !vcpu->arch.vpa.next_gpa)
  1703. break;
  1704. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  1705. break;
  1706. case KVM_REG_PPC_VPA_DTL:
  1707. addr = val->vpaval.addr;
  1708. len = val->vpaval.length;
  1709. r = -EINVAL;
  1710. if (addr && (len < sizeof(struct dtl_entry) ||
  1711. !vcpu->arch.vpa.next_gpa))
  1712. break;
  1713. len -= len % sizeof(struct dtl_entry);
  1714. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  1715. break;
  1716. case KVM_REG_PPC_TB_OFFSET:
  1717. /* round up to multiple of 2^24 */
  1718. vcpu->arch.vcore->tb_offset =
  1719. ALIGN(set_reg_val(id, *val), 1UL << 24);
  1720. break;
  1721. case KVM_REG_PPC_LPCR:
  1722. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
  1723. break;
  1724. case KVM_REG_PPC_LPCR_64:
  1725. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
  1726. break;
  1727. case KVM_REG_PPC_PPR:
  1728. vcpu->arch.ppr = set_reg_val(id, *val);
  1729. break;
  1730. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1731. case KVM_REG_PPC_TFHAR:
  1732. vcpu->arch.tfhar = set_reg_val(id, *val);
  1733. break;
  1734. case KVM_REG_PPC_TFIAR:
  1735. vcpu->arch.tfiar = set_reg_val(id, *val);
  1736. break;
  1737. case KVM_REG_PPC_TEXASR:
  1738. vcpu->arch.texasr = set_reg_val(id, *val);
  1739. break;
  1740. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1741. i = id - KVM_REG_PPC_TM_GPR0;
  1742. vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
  1743. break;
  1744. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1745. {
  1746. int j;
  1747. i = id - KVM_REG_PPC_TM_VSR0;
  1748. if (i < 32)
  1749. for (j = 0; j < TS_FPRWIDTH; j++)
  1750. vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
  1751. else
  1752. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1753. vcpu->arch.vr_tm.vr[i-32] = val->vval;
  1754. else
  1755. r = -ENXIO;
  1756. break;
  1757. }
  1758. case KVM_REG_PPC_TM_CR:
  1759. vcpu->arch.cr_tm = set_reg_val(id, *val);
  1760. break;
  1761. case KVM_REG_PPC_TM_XER:
  1762. vcpu->arch.xer_tm = set_reg_val(id, *val);
  1763. break;
  1764. case KVM_REG_PPC_TM_LR:
  1765. vcpu->arch.lr_tm = set_reg_val(id, *val);
  1766. break;
  1767. case KVM_REG_PPC_TM_CTR:
  1768. vcpu->arch.ctr_tm = set_reg_val(id, *val);
  1769. break;
  1770. case KVM_REG_PPC_TM_FPSCR:
  1771. vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
  1772. break;
  1773. case KVM_REG_PPC_TM_AMR:
  1774. vcpu->arch.amr_tm = set_reg_val(id, *val);
  1775. break;
  1776. case KVM_REG_PPC_TM_PPR:
  1777. vcpu->arch.ppr_tm = set_reg_val(id, *val);
  1778. break;
  1779. case KVM_REG_PPC_TM_VRSAVE:
  1780. vcpu->arch.vrsave_tm = set_reg_val(id, *val);
  1781. break;
  1782. case KVM_REG_PPC_TM_VSCR:
  1783. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1784. vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
  1785. else
  1786. r = - ENXIO;
  1787. break;
  1788. case KVM_REG_PPC_TM_DSCR:
  1789. vcpu->arch.dscr_tm = set_reg_val(id, *val);
  1790. break;
  1791. case KVM_REG_PPC_TM_TAR:
  1792. vcpu->arch.tar_tm = set_reg_val(id, *val);
  1793. break;
  1794. #endif
  1795. case KVM_REG_PPC_ARCH_COMPAT:
  1796. r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
  1797. break;
  1798. case KVM_REG_PPC_DEC_EXPIRY:
  1799. vcpu->arch.dec_expires = set_reg_val(id, *val) -
  1800. vcpu->arch.vcore->tb_offset;
  1801. break;
  1802. case KVM_REG_PPC_ONLINE:
  1803. i = set_reg_val(id, *val);
  1804. if (i && !vcpu->arch.online)
  1805. atomic_inc(&vcpu->arch.vcore->online_count);
  1806. else if (!i && vcpu->arch.online)
  1807. atomic_dec(&vcpu->arch.vcore->online_count);
  1808. vcpu->arch.online = i;
  1809. break;
  1810. case KVM_REG_PPC_PTCR:
  1811. vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val);
  1812. break;
  1813. default:
  1814. r = -EINVAL;
  1815. break;
  1816. }
  1817. return r;
  1818. }
  1819. /*
  1820. * On POWER9, threads are independent and can be in different partitions.
  1821. * Therefore we consider each thread to be a subcore.
  1822. * There is a restriction that all threads have to be in the same
  1823. * MMU mode (radix or HPT), unfortunately, but since we only support
  1824. * HPT guests on a HPT host so far, that isn't an impediment yet.
  1825. */
  1826. static int threads_per_vcore(struct kvm *kvm)
  1827. {
  1828. if (kvm->arch.threads_indep)
  1829. return 1;
  1830. return threads_per_subcore;
  1831. }
  1832. static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
  1833. {
  1834. struct kvmppc_vcore *vcore;
  1835. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  1836. if (vcore == NULL)
  1837. return NULL;
  1838. spin_lock_init(&vcore->lock);
  1839. spin_lock_init(&vcore->stoltb_lock);
  1840. init_swait_queue_head(&vcore->wq);
  1841. vcore->preempt_tb = TB_NIL;
  1842. vcore->lpcr = kvm->arch.lpcr;
  1843. vcore->first_vcpuid = id;
  1844. vcore->kvm = kvm;
  1845. INIT_LIST_HEAD(&vcore->preempt_list);
  1846. return vcore;
  1847. }
  1848. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1849. static struct debugfs_timings_element {
  1850. const char *name;
  1851. size_t offset;
  1852. } timings[] = {
  1853. {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
  1854. {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
  1855. {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
  1856. {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
  1857. {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
  1858. };
  1859. #define N_TIMINGS (ARRAY_SIZE(timings))
  1860. struct debugfs_timings_state {
  1861. struct kvm_vcpu *vcpu;
  1862. unsigned int buflen;
  1863. char buf[N_TIMINGS * 100];
  1864. };
  1865. static int debugfs_timings_open(struct inode *inode, struct file *file)
  1866. {
  1867. struct kvm_vcpu *vcpu = inode->i_private;
  1868. struct debugfs_timings_state *p;
  1869. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1870. if (!p)
  1871. return -ENOMEM;
  1872. kvm_get_kvm(vcpu->kvm);
  1873. p->vcpu = vcpu;
  1874. file->private_data = p;
  1875. return nonseekable_open(inode, file);
  1876. }
  1877. static int debugfs_timings_release(struct inode *inode, struct file *file)
  1878. {
  1879. struct debugfs_timings_state *p = file->private_data;
  1880. kvm_put_kvm(p->vcpu->kvm);
  1881. kfree(p);
  1882. return 0;
  1883. }
  1884. static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
  1885. size_t len, loff_t *ppos)
  1886. {
  1887. struct debugfs_timings_state *p = file->private_data;
  1888. struct kvm_vcpu *vcpu = p->vcpu;
  1889. char *s, *buf_end;
  1890. struct kvmhv_tb_accumulator tb;
  1891. u64 count;
  1892. loff_t pos;
  1893. ssize_t n;
  1894. int i, loops;
  1895. bool ok;
  1896. if (!p->buflen) {
  1897. s = p->buf;
  1898. buf_end = s + sizeof(p->buf);
  1899. for (i = 0; i < N_TIMINGS; ++i) {
  1900. struct kvmhv_tb_accumulator *acc;
  1901. acc = (struct kvmhv_tb_accumulator *)
  1902. ((unsigned long)vcpu + timings[i].offset);
  1903. ok = false;
  1904. for (loops = 0; loops < 1000; ++loops) {
  1905. count = acc->seqcount;
  1906. if (!(count & 1)) {
  1907. smp_rmb();
  1908. tb = *acc;
  1909. smp_rmb();
  1910. if (count == acc->seqcount) {
  1911. ok = true;
  1912. break;
  1913. }
  1914. }
  1915. udelay(1);
  1916. }
  1917. if (!ok)
  1918. snprintf(s, buf_end - s, "%s: stuck\n",
  1919. timings[i].name);
  1920. else
  1921. snprintf(s, buf_end - s,
  1922. "%s: %llu %llu %llu %llu\n",
  1923. timings[i].name, count / 2,
  1924. tb_to_ns(tb.tb_total),
  1925. tb_to_ns(tb.tb_min),
  1926. tb_to_ns(tb.tb_max));
  1927. s += strlen(s);
  1928. }
  1929. p->buflen = s - p->buf;
  1930. }
  1931. pos = *ppos;
  1932. if (pos >= p->buflen)
  1933. return 0;
  1934. if (len > p->buflen - pos)
  1935. len = p->buflen - pos;
  1936. n = copy_to_user(buf, p->buf + pos, len);
  1937. if (n) {
  1938. if (n == len)
  1939. return -EFAULT;
  1940. len -= n;
  1941. }
  1942. *ppos = pos + len;
  1943. return len;
  1944. }
  1945. static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
  1946. size_t len, loff_t *ppos)
  1947. {
  1948. return -EACCES;
  1949. }
  1950. static const struct file_operations debugfs_timings_ops = {
  1951. .owner = THIS_MODULE,
  1952. .open = debugfs_timings_open,
  1953. .release = debugfs_timings_release,
  1954. .read = debugfs_timings_read,
  1955. .write = debugfs_timings_write,
  1956. .llseek = generic_file_llseek,
  1957. };
  1958. /* Create a debugfs directory for the vcpu */
  1959. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1960. {
  1961. char buf[16];
  1962. struct kvm *kvm = vcpu->kvm;
  1963. snprintf(buf, sizeof(buf), "vcpu%u", id);
  1964. if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  1965. return;
  1966. vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
  1967. if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
  1968. return;
  1969. vcpu->arch.debugfs_timings =
  1970. debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
  1971. vcpu, &debugfs_timings_ops);
  1972. }
  1973. #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1974. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1975. {
  1976. }
  1977. #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1978. static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
  1979. unsigned int id)
  1980. {
  1981. struct kvm_vcpu *vcpu;
  1982. int err;
  1983. int core;
  1984. struct kvmppc_vcore *vcore;
  1985. err = -ENOMEM;
  1986. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1987. if (!vcpu)
  1988. goto out;
  1989. err = kvm_vcpu_init(vcpu, kvm, id);
  1990. if (err)
  1991. goto free_vcpu;
  1992. vcpu->arch.shared = &vcpu->arch.shregs;
  1993. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  1994. /*
  1995. * The shared struct is never shared on HV,
  1996. * so we can always use host endianness
  1997. */
  1998. #ifdef __BIG_ENDIAN__
  1999. vcpu->arch.shared_big_endian = true;
  2000. #else
  2001. vcpu->arch.shared_big_endian = false;
  2002. #endif
  2003. #endif
  2004. vcpu->arch.mmcr[0] = MMCR0_FC;
  2005. vcpu->arch.ctrl = CTRL_RUNLATCH;
  2006. /* default to host PVR, since we can't spoof it */
  2007. kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
  2008. spin_lock_init(&vcpu->arch.vpa_update_lock);
  2009. spin_lock_init(&vcpu->arch.tbacct_lock);
  2010. vcpu->arch.busy_preempt = TB_NIL;
  2011. vcpu->arch.intr_msr = MSR_SF | MSR_ME;
  2012. /*
  2013. * Set the default HFSCR for the guest from the host value.
  2014. * This value is only used on POWER9.
  2015. * On POWER9, we want to virtualize the doorbell facility, so we
  2016. * don't set the HFSCR_MSGP bit, and that causes those instructions
  2017. * to trap and then we emulate them.
  2018. */
  2019. vcpu->arch.hfscr = HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB |
  2020. HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP;
  2021. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  2022. vcpu->arch.hfscr &= mfspr(SPRN_HFSCR);
  2023. if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
  2024. vcpu->arch.hfscr |= HFSCR_TM;
  2025. }
  2026. if (cpu_has_feature(CPU_FTR_TM_COMP))
  2027. vcpu->arch.hfscr |= HFSCR_TM;
  2028. kvmppc_mmu_book3s_hv_init(vcpu);
  2029. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  2030. init_waitqueue_head(&vcpu->arch.cpu_run);
  2031. mutex_lock(&kvm->lock);
  2032. vcore = NULL;
  2033. err = -EINVAL;
  2034. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  2035. if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
  2036. pr_devel("KVM: VCPU ID too high\n");
  2037. core = KVM_MAX_VCORES;
  2038. } else {
  2039. BUG_ON(kvm->arch.smt_mode != 1);
  2040. core = kvmppc_pack_vcpu_id(kvm, id);
  2041. }
  2042. } else {
  2043. core = id / kvm->arch.smt_mode;
  2044. }
  2045. if (core < KVM_MAX_VCORES) {
  2046. vcore = kvm->arch.vcores[core];
  2047. if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) {
  2048. pr_devel("KVM: collision on id %u", id);
  2049. vcore = NULL;
  2050. } else if (!vcore) {
  2051. err = -ENOMEM;
  2052. vcore = kvmppc_vcore_create(kvm,
  2053. id & ~(kvm->arch.smt_mode - 1));
  2054. kvm->arch.vcores[core] = vcore;
  2055. kvm->arch.online_vcores++;
  2056. }
  2057. }
  2058. mutex_unlock(&kvm->lock);
  2059. if (!vcore)
  2060. goto free_vcpu;
  2061. spin_lock(&vcore->lock);
  2062. ++vcore->num_threads;
  2063. spin_unlock(&vcore->lock);
  2064. vcpu->arch.vcore = vcore;
  2065. vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
  2066. vcpu->arch.thread_cpu = -1;
  2067. vcpu->arch.prev_cpu = -1;
  2068. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  2069. kvmppc_sanity_check(vcpu);
  2070. debugfs_vcpu_init(vcpu, id);
  2071. return vcpu;
  2072. free_vcpu:
  2073. kmem_cache_free(kvm_vcpu_cache, vcpu);
  2074. out:
  2075. return ERR_PTR(err);
  2076. }
  2077. static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
  2078. unsigned long flags)
  2079. {
  2080. int err;
  2081. int esmt = 0;
  2082. if (flags)
  2083. return -EINVAL;
  2084. if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
  2085. return -EINVAL;
  2086. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  2087. /*
  2088. * On POWER8 (or POWER7), the threading mode is "strict",
  2089. * so we pack smt_mode vcpus per vcore.
  2090. */
  2091. if (smt_mode > threads_per_subcore)
  2092. return -EINVAL;
  2093. } else {
  2094. /*
  2095. * On POWER9, the threading mode is "loose",
  2096. * so each vcpu gets its own vcore.
  2097. */
  2098. esmt = smt_mode;
  2099. smt_mode = 1;
  2100. }
  2101. mutex_lock(&kvm->lock);
  2102. err = -EBUSY;
  2103. if (!kvm->arch.online_vcores) {
  2104. kvm->arch.smt_mode = smt_mode;
  2105. kvm->arch.emul_smt_mode = esmt;
  2106. err = 0;
  2107. }
  2108. mutex_unlock(&kvm->lock);
  2109. return err;
  2110. }
  2111. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  2112. {
  2113. if (vpa->pinned_addr)
  2114. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  2115. vpa->dirty);
  2116. }
  2117. static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
  2118. {
  2119. spin_lock(&vcpu->arch.vpa_update_lock);
  2120. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  2121. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  2122. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  2123. spin_unlock(&vcpu->arch.vpa_update_lock);
  2124. kvm_vcpu_uninit(vcpu);
  2125. kmem_cache_free(kvm_vcpu_cache, vcpu);
  2126. }
  2127. static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
  2128. {
  2129. /* Indicate we want to get back into the guest */
  2130. return 1;
  2131. }
  2132. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  2133. {
  2134. unsigned long dec_nsec, now;
  2135. now = get_tb();
  2136. if (now > vcpu->arch.dec_expires) {
  2137. /* decrementer has already gone negative */
  2138. kvmppc_core_queue_dec(vcpu);
  2139. kvmppc_core_prepare_to_enter(vcpu);
  2140. return;
  2141. }
  2142. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  2143. / tb_ticks_per_sec;
  2144. hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
  2145. vcpu->arch.timer_running = 1;
  2146. }
  2147. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  2148. {
  2149. vcpu->arch.ceded = 0;
  2150. if (vcpu->arch.timer_running) {
  2151. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  2152. vcpu->arch.timer_running = 0;
  2153. }
  2154. }
  2155. extern int __kvmppc_vcore_entry(void);
  2156. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  2157. struct kvm_vcpu *vcpu)
  2158. {
  2159. u64 now;
  2160. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  2161. return;
  2162. spin_lock_irq(&vcpu->arch.tbacct_lock);
  2163. now = mftb();
  2164. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  2165. vcpu->arch.stolen_logged;
  2166. vcpu->arch.busy_preempt = now;
  2167. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  2168. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  2169. --vc->n_runnable;
  2170. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
  2171. }
  2172. static int kvmppc_grab_hwthread(int cpu)
  2173. {
  2174. struct paca_struct *tpaca;
  2175. long timeout = 10000;
  2176. tpaca = paca_ptrs[cpu];
  2177. /* Ensure the thread won't go into the kernel if it wakes */
  2178. tpaca->kvm_hstate.kvm_vcpu = NULL;
  2179. tpaca->kvm_hstate.kvm_vcore = NULL;
  2180. tpaca->kvm_hstate.napping = 0;
  2181. smp_wmb();
  2182. tpaca->kvm_hstate.hwthread_req = 1;
  2183. /*
  2184. * If the thread is already executing in the kernel (e.g. handling
  2185. * a stray interrupt), wait for it to get back to nap mode.
  2186. * The smp_mb() is to ensure that our setting of hwthread_req
  2187. * is visible before we look at hwthread_state, so if this
  2188. * races with the code at system_reset_pSeries and the thread
  2189. * misses our setting of hwthread_req, we are sure to see its
  2190. * setting of hwthread_state, and vice versa.
  2191. */
  2192. smp_mb();
  2193. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  2194. if (--timeout <= 0) {
  2195. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  2196. return -EBUSY;
  2197. }
  2198. udelay(1);
  2199. }
  2200. return 0;
  2201. }
  2202. static void kvmppc_release_hwthread(int cpu)
  2203. {
  2204. struct paca_struct *tpaca;
  2205. tpaca = paca_ptrs[cpu];
  2206. tpaca->kvm_hstate.hwthread_req = 0;
  2207. tpaca->kvm_hstate.kvm_vcpu = NULL;
  2208. tpaca->kvm_hstate.kvm_vcore = NULL;
  2209. tpaca->kvm_hstate.kvm_split_mode = NULL;
  2210. }
  2211. static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
  2212. {
  2213. struct kvm_nested_guest *nested = vcpu->arch.nested;
  2214. cpumask_t *cpu_in_guest;
  2215. int i;
  2216. cpu = cpu_first_thread_sibling(cpu);
  2217. if (nested) {
  2218. cpumask_set_cpu(cpu, &nested->need_tlb_flush);
  2219. cpu_in_guest = &nested->cpu_in_guest;
  2220. } else {
  2221. cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
  2222. cpu_in_guest = &kvm->arch.cpu_in_guest;
  2223. }
  2224. /*
  2225. * Make sure setting of bit in need_tlb_flush precedes
  2226. * testing of cpu_in_guest bits. The matching barrier on
  2227. * the other side is the first smp_mb() in kvmppc_run_core().
  2228. */
  2229. smp_mb();
  2230. for (i = 0; i < threads_per_core; ++i)
  2231. if (cpumask_test_cpu(cpu + i, cpu_in_guest))
  2232. smp_call_function_single(cpu + i, do_nothing, NULL, 1);
  2233. }
  2234. static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
  2235. {
  2236. struct kvm_nested_guest *nested = vcpu->arch.nested;
  2237. struct kvm *kvm = vcpu->kvm;
  2238. int prev_cpu;
  2239. if (!cpu_has_feature(CPU_FTR_HVMODE))
  2240. return;
  2241. if (nested)
  2242. prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id];
  2243. else
  2244. prev_cpu = vcpu->arch.prev_cpu;
  2245. /*
  2246. * With radix, the guest can do TLB invalidations itself,
  2247. * and it could choose to use the local form (tlbiel) if
  2248. * it is invalidating a translation that has only ever been
  2249. * used on one vcpu. However, that doesn't mean it has
  2250. * only ever been used on one physical cpu, since vcpus
  2251. * can move around between pcpus. To cope with this, when
  2252. * a vcpu moves from one pcpu to another, we need to tell
  2253. * any vcpus running on the same core as this vcpu previously
  2254. * ran to flush the TLB. The TLB is shared between threads,
  2255. * so we use a single bit in .need_tlb_flush for all 4 threads.
  2256. */
  2257. if (prev_cpu != pcpu) {
  2258. if (prev_cpu >= 0 &&
  2259. cpu_first_thread_sibling(prev_cpu) !=
  2260. cpu_first_thread_sibling(pcpu))
  2261. radix_flush_cpu(kvm, prev_cpu, vcpu);
  2262. if (nested)
  2263. nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu;
  2264. else
  2265. vcpu->arch.prev_cpu = pcpu;
  2266. }
  2267. }
  2268. static void kvmppc_radix_check_need_tlb_flush(struct kvm *kvm, int pcpu,
  2269. struct kvm_nested_guest *nested)
  2270. {
  2271. cpumask_t *need_tlb_flush;
  2272. int lpid;
  2273. if (!cpu_has_feature(CPU_FTR_HVMODE))
  2274. return;
  2275. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2276. pcpu &= ~0x3UL;
  2277. if (nested) {
  2278. lpid = nested->shadow_lpid;
  2279. need_tlb_flush = &nested->need_tlb_flush;
  2280. } else {
  2281. lpid = kvm->arch.lpid;
  2282. need_tlb_flush = &kvm->arch.need_tlb_flush;
  2283. }
  2284. mtspr(SPRN_LPID, lpid);
  2285. isync();
  2286. smp_mb();
  2287. if (cpumask_test_cpu(pcpu, need_tlb_flush)) {
  2288. radix__local_flush_tlb_lpid_guest(lpid);
  2289. /* Clear the bit after the TLB flush */
  2290. cpumask_clear_cpu(pcpu, need_tlb_flush);
  2291. }
  2292. }
  2293. static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
  2294. {
  2295. int cpu;
  2296. struct paca_struct *tpaca;
  2297. struct kvm *kvm = vc->kvm;
  2298. cpu = vc->pcpu;
  2299. if (vcpu) {
  2300. if (vcpu->arch.timer_running) {
  2301. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  2302. vcpu->arch.timer_running = 0;
  2303. }
  2304. cpu += vcpu->arch.ptid;
  2305. vcpu->cpu = vc->pcpu;
  2306. vcpu->arch.thread_cpu = cpu;
  2307. cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
  2308. }
  2309. tpaca = paca_ptrs[cpu];
  2310. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  2311. tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
  2312. tpaca->kvm_hstate.fake_suspend = 0;
  2313. /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
  2314. smp_wmb();
  2315. tpaca->kvm_hstate.kvm_vcore = vc;
  2316. if (cpu != smp_processor_id())
  2317. kvmppc_ipi_thread(cpu);
  2318. }
  2319. static void kvmppc_wait_for_nap(int n_threads)
  2320. {
  2321. int cpu = smp_processor_id();
  2322. int i, loops;
  2323. if (n_threads <= 1)
  2324. return;
  2325. for (loops = 0; loops < 1000000; ++loops) {
  2326. /*
  2327. * Check if all threads are finished.
  2328. * We set the vcore pointer when starting a thread
  2329. * and the thread clears it when finished, so we look
  2330. * for any threads that still have a non-NULL vcore ptr.
  2331. */
  2332. for (i = 1; i < n_threads; ++i)
  2333. if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
  2334. break;
  2335. if (i == n_threads) {
  2336. HMT_medium();
  2337. return;
  2338. }
  2339. HMT_low();
  2340. }
  2341. HMT_medium();
  2342. for (i = 1; i < n_threads; ++i)
  2343. if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
  2344. pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
  2345. }
  2346. /*
  2347. * Check that we are on thread 0 and that any other threads in
  2348. * this core are off-line. Then grab the threads so they can't
  2349. * enter the kernel.
  2350. */
  2351. static int on_primary_thread(void)
  2352. {
  2353. int cpu = smp_processor_id();
  2354. int thr;
  2355. /* Are we on a primary subcore? */
  2356. if (cpu_thread_in_subcore(cpu))
  2357. return 0;
  2358. thr = 0;
  2359. while (++thr < threads_per_subcore)
  2360. if (cpu_online(cpu + thr))
  2361. return 0;
  2362. /* Grab all hw threads so they can't go into the kernel */
  2363. for (thr = 1; thr < threads_per_subcore; ++thr) {
  2364. if (kvmppc_grab_hwthread(cpu + thr)) {
  2365. /* Couldn't grab one; let the others go */
  2366. do {
  2367. kvmppc_release_hwthread(cpu + thr);
  2368. } while (--thr > 0);
  2369. return 0;
  2370. }
  2371. }
  2372. return 1;
  2373. }
  2374. /*
  2375. * A list of virtual cores for each physical CPU.
  2376. * These are vcores that could run but their runner VCPU tasks are
  2377. * (or may be) preempted.
  2378. */
  2379. struct preempted_vcore_list {
  2380. struct list_head list;
  2381. spinlock_t lock;
  2382. };
  2383. static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
  2384. static void init_vcore_lists(void)
  2385. {
  2386. int cpu;
  2387. for_each_possible_cpu(cpu) {
  2388. struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
  2389. spin_lock_init(&lp->lock);
  2390. INIT_LIST_HEAD(&lp->list);
  2391. }
  2392. }
  2393. static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
  2394. {
  2395. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2396. vc->vcore_state = VCORE_PREEMPT;
  2397. vc->pcpu = smp_processor_id();
  2398. if (vc->num_threads < threads_per_vcore(vc->kvm)) {
  2399. spin_lock(&lp->lock);
  2400. list_add_tail(&vc->preempt_list, &lp->list);
  2401. spin_unlock(&lp->lock);
  2402. }
  2403. /* Start accumulating stolen time */
  2404. kvmppc_core_start_stolen(vc);
  2405. }
  2406. static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
  2407. {
  2408. struct preempted_vcore_list *lp;
  2409. kvmppc_core_end_stolen(vc);
  2410. if (!list_empty(&vc->preempt_list)) {
  2411. lp = &per_cpu(preempted_vcores, vc->pcpu);
  2412. spin_lock(&lp->lock);
  2413. list_del_init(&vc->preempt_list);
  2414. spin_unlock(&lp->lock);
  2415. }
  2416. vc->vcore_state = VCORE_INACTIVE;
  2417. }
  2418. /*
  2419. * This stores information about the virtual cores currently
  2420. * assigned to a physical core.
  2421. */
  2422. struct core_info {
  2423. int n_subcores;
  2424. int max_subcore_threads;
  2425. int total_threads;
  2426. int subcore_threads[MAX_SUBCORES];
  2427. struct kvmppc_vcore *vc[MAX_SUBCORES];
  2428. };
  2429. /*
  2430. * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
  2431. * respectively in 2-way micro-threading (split-core) mode on POWER8.
  2432. */
  2433. static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
  2434. static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
  2435. {
  2436. memset(cip, 0, sizeof(*cip));
  2437. cip->n_subcores = 1;
  2438. cip->max_subcore_threads = vc->num_threads;
  2439. cip->total_threads = vc->num_threads;
  2440. cip->subcore_threads[0] = vc->num_threads;
  2441. cip->vc[0] = vc;
  2442. }
  2443. static bool subcore_config_ok(int n_subcores, int n_threads)
  2444. {
  2445. /*
  2446. * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
  2447. * split-core mode, with one thread per subcore.
  2448. */
  2449. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2450. return n_subcores <= 4 && n_threads == 1;
  2451. /* On POWER8, can only dynamically split if unsplit to begin with */
  2452. if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
  2453. return false;
  2454. if (n_subcores > MAX_SUBCORES)
  2455. return false;
  2456. if (n_subcores > 1) {
  2457. if (!(dynamic_mt_modes & 2))
  2458. n_subcores = 4;
  2459. if (n_subcores > 2 && !(dynamic_mt_modes & 4))
  2460. return false;
  2461. }
  2462. return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
  2463. }
  2464. static void init_vcore_to_run(struct kvmppc_vcore *vc)
  2465. {
  2466. vc->entry_exit_map = 0;
  2467. vc->in_guest = 0;
  2468. vc->napping_threads = 0;
  2469. vc->conferring_threads = 0;
  2470. vc->tb_offset_applied = 0;
  2471. }
  2472. static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
  2473. {
  2474. int n_threads = vc->num_threads;
  2475. int sub;
  2476. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  2477. return false;
  2478. /* In one_vm_per_core mode, require all vcores to be from the same vm */
  2479. if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm)
  2480. return false;
  2481. /* Some POWER9 chips require all threads to be in the same MMU mode */
  2482. if (no_mixing_hpt_and_radix &&
  2483. kvm_is_radix(vc->kvm) != kvm_is_radix(cip->vc[0]->kvm))
  2484. return false;
  2485. if (n_threads < cip->max_subcore_threads)
  2486. n_threads = cip->max_subcore_threads;
  2487. if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
  2488. return false;
  2489. cip->max_subcore_threads = n_threads;
  2490. sub = cip->n_subcores;
  2491. ++cip->n_subcores;
  2492. cip->total_threads += vc->num_threads;
  2493. cip->subcore_threads[sub] = vc->num_threads;
  2494. cip->vc[sub] = vc;
  2495. init_vcore_to_run(vc);
  2496. list_del_init(&vc->preempt_list);
  2497. return true;
  2498. }
  2499. /*
  2500. * Work out whether it is possible to piggyback the execution of
  2501. * vcore *pvc onto the execution of the other vcores described in *cip.
  2502. */
  2503. static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
  2504. int target_threads)
  2505. {
  2506. if (cip->total_threads + pvc->num_threads > target_threads)
  2507. return false;
  2508. return can_dynamic_split(pvc, cip);
  2509. }
  2510. static void prepare_threads(struct kvmppc_vcore *vc)
  2511. {
  2512. int i;
  2513. struct kvm_vcpu *vcpu;
  2514. for_each_runnable_thread(i, vcpu, vc) {
  2515. if (signal_pending(vcpu->arch.run_task))
  2516. vcpu->arch.ret = -EINTR;
  2517. else if (vcpu->arch.vpa.update_pending ||
  2518. vcpu->arch.slb_shadow.update_pending ||
  2519. vcpu->arch.dtl.update_pending)
  2520. vcpu->arch.ret = RESUME_GUEST;
  2521. else
  2522. continue;
  2523. kvmppc_remove_runnable(vc, vcpu);
  2524. wake_up(&vcpu->arch.cpu_run);
  2525. }
  2526. }
  2527. static void collect_piggybacks(struct core_info *cip, int target_threads)
  2528. {
  2529. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2530. struct kvmppc_vcore *pvc, *vcnext;
  2531. spin_lock(&lp->lock);
  2532. list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
  2533. if (!spin_trylock(&pvc->lock))
  2534. continue;
  2535. prepare_threads(pvc);
  2536. if (!pvc->n_runnable) {
  2537. list_del_init(&pvc->preempt_list);
  2538. if (pvc->runner == NULL) {
  2539. pvc->vcore_state = VCORE_INACTIVE;
  2540. kvmppc_core_end_stolen(pvc);
  2541. }
  2542. spin_unlock(&pvc->lock);
  2543. continue;
  2544. }
  2545. if (!can_piggyback(pvc, cip, target_threads)) {
  2546. spin_unlock(&pvc->lock);
  2547. continue;
  2548. }
  2549. kvmppc_core_end_stolen(pvc);
  2550. pvc->vcore_state = VCORE_PIGGYBACK;
  2551. if (cip->total_threads >= target_threads)
  2552. break;
  2553. }
  2554. spin_unlock(&lp->lock);
  2555. }
  2556. static bool recheck_signals(struct core_info *cip)
  2557. {
  2558. int sub, i;
  2559. struct kvm_vcpu *vcpu;
  2560. for (sub = 0; sub < cip->n_subcores; ++sub)
  2561. for_each_runnable_thread(i, vcpu, cip->vc[sub])
  2562. if (signal_pending(vcpu->arch.run_task))
  2563. return true;
  2564. return false;
  2565. }
  2566. static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
  2567. {
  2568. int still_running = 0, i;
  2569. u64 now;
  2570. long ret;
  2571. struct kvm_vcpu *vcpu;
  2572. spin_lock(&vc->lock);
  2573. now = get_tb();
  2574. for_each_runnable_thread(i, vcpu, vc) {
  2575. /*
  2576. * It's safe to unlock the vcore in the loop here, because
  2577. * for_each_runnable_thread() is safe against removal of
  2578. * the vcpu, and the vcore state is VCORE_EXITING here,
  2579. * so any vcpus becoming runnable will have their arch.trap
  2580. * set to zero and can't actually run in the guest.
  2581. */
  2582. spin_unlock(&vc->lock);
  2583. /* cancel pending dec exception if dec is positive */
  2584. if (now < vcpu->arch.dec_expires &&
  2585. kvmppc_core_pending_dec(vcpu))
  2586. kvmppc_core_dequeue_dec(vcpu);
  2587. trace_kvm_guest_exit(vcpu);
  2588. ret = RESUME_GUEST;
  2589. if (vcpu->arch.trap)
  2590. ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
  2591. vcpu->arch.run_task);
  2592. vcpu->arch.ret = ret;
  2593. vcpu->arch.trap = 0;
  2594. spin_lock(&vc->lock);
  2595. if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
  2596. if (vcpu->arch.pending_exceptions)
  2597. kvmppc_core_prepare_to_enter(vcpu);
  2598. if (vcpu->arch.ceded)
  2599. kvmppc_set_timer(vcpu);
  2600. else
  2601. ++still_running;
  2602. } else {
  2603. kvmppc_remove_runnable(vc, vcpu);
  2604. wake_up(&vcpu->arch.cpu_run);
  2605. }
  2606. }
  2607. if (!is_master) {
  2608. if (still_running > 0) {
  2609. kvmppc_vcore_preempt(vc);
  2610. } else if (vc->runner) {
  2611. vc->vcore_state = VCORE_PREEMPT;
  2612. kvmppc_core_start_stolen(vc);
  2613. } else {
  2614. vc->vcore_state = VCORE_INACTIVE;
  2615. }
  2616. if (vc->n_runnable > 0 && vc->runner == NULL) {
  2617. /* make sure there's a candidate runner awake */
  2618. i = -1;
  2619. vcpu = next_runnable_thread(vc, &i);
  2620. wake_up(&vcpu->arch.cpu_run);
  2621. }
  2622. }
  2623. spin_unlock(&vc->lock);
  2624. }
  2625. /*
  2626. * Clear core from the list of active host cores as we are about to
  2627. * enter the guest. Only do this if it is the primary thread of the
  2628. * core (not if a subcore) that is entering the guest.
  2629. */
  2630. static inline int kvmppc_clear_host_core(unsigned int cpu)
  2631. {
  2632. int core;
  2633. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2634. return 0;
  2635. /*
  2636. * Memory barrier can be omitted here as we will do a smp_wmb()
  2637. * later in kvmppc_start_thread and we need ensure that state is
  2638. * visible to other CPUs only after we enter guest.
  2639. */
  2640. core = cpu >> threads_shift;
  2641. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
  2642. return 0;
  2643. }
  2644. /*
  2645. * Advertise this core as an active host core since we exited the guest
  2646. * Only need to do this if it is the primary thread of the core that is
  2647. * exiting.
  2648. */
  2649. static inline int kvmppc_set_host_core(unsigned int cpu)
  2650. {
  2651. int core;
  2652. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2653. return 0;
  2654. /*
  2655. * Memory barrier can be omitted here because we do a spin_unlock
  2656. * immediately after this which provides the memory barrier.
  2657. */
  2658. core = cpu >> threads_shift;
  2659. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
  2660. return 0;
  2661. }
  2662. static void set_irq_happened(int trap)
  2663. {
  2664. switch (trap) {
  2665. case BOOK3S_INTERRUPT_EXTERNAL:
  2666. local_paca->irq_happened |= PACA_IRQ_EE;
  2667. break;
  2668. case BOOK3S_INTERRUPT_H_DOORBELL:
  2669. local_paca->irq_happened |= PACA_IRQ_DBELL;
  2670. break;
  2671. case BOOK3S_INTERRUPT_HMI:
  2672. local_paca->irq_happened |= PACA_IRQ_HMI;
  2673. break;
  2674. case BOOK3S_INTERRUPT_SYSTEM_RESET:
  2675. replay_system_reset();
  2676. break;
  2677. }
  2678. }
  2679. /*
  2680. * Run a set of guest threads on a physical core.
  2681. * Called with vc->lock held.
  2682. */
  2683. static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
  2684. {
  2685. struct kvm_vcpu *vcpu;
  2686. int i;
  2687. int srcu_idx;
  2688. struct core_info core_info;
  2689. struct kvmppc_vcore *pvc;
  2690. struct kvm_split_mode split_info, *sip;
  2691. int split, subcore_size, active;
  2692. int sub;
  2693. bool thr0_done;
  2694. unsigned long cmd_bit, stat_bit;
  2695. int pcpu, thr;
  2696. int target_threads;
  2697. int controlled_threads;
  2698. int trap;
  2699. bool is_power8;
  2700. bool hpt_on_radix;
  2701. /*
  2702. * Remove from the list any threads that have a signal pending
  2703. * or need a VPA update done
  2704. */
  2705. prepare_threads(vc);
  2706. /* if the runner is no longer runnable, let the caller pick a new one */
  2707. if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
  2708. return;
  2709. /*
  2710. * Initialize *vc.
  2711. */
  2712. init_vcore_to_run(vc);
  2713. vc->preempt_tb = TB_NIL;
  2714. /*
  2715. * Number of threads that we will be controlling: the same as
  2716. * the number of threads per subcore, except on POWER9,
  2717. * where it's 1 because the threads are (mostly) independent.
  2718. */
  2719. controlled_threads = threads_per_vcore(vc->kvm);
  2720. /*
  2721. * Make sure we are running on primary threads, and that secondary
  2722. * threads are offline. Also check if the number of threads in this
  2723. * guest are greater than the current system threads per guest.
  2724. * On POWER9, we need to be not in independent-threads mode if
  2725. * this is a HPT guest on a radix host machine where the
  2726. * CPU threads may not be in different MMU modes.
  2727. */
  2728. hpt_on_radix = no_mixing_hpt_and_radix && radix_enabled() &&
  2729. !kvm_is_radix(vc->kvm);
  2730. if (((controlled_threads > 1) &&
  2731. ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) ||
  2732. (hpt_on_radix && vc->kvm->arch.threads_indep)) {
  2733. for_each_runnable_thread(i, vcpu, vc) {
  2734. vcpu->arch.ret = -EBUSY;
  2735. kvmppc_remove_runnable(vc, vcpu);
  2736. wake_up(&vcpu->arch.cpu_run);
  2737. }
  2738. goto out;
  2739. }
  2740. /*
  2741. * See if we could run any other vcores on the physical core
  2742. * along with this one.
  2743. */
  2744. init_core_info(&core_info, vc);
  2745. pcpu = smp_processor_id();
  2746. target_threads = controlled_threads;
  2747. if (target_smt_mode && target_smt_mode < target_threads)
  2748. target_threads = target_smt_mode;
  2749. if (vc->num_threads < target_threads)
  2750. collect_piggybacks(&core_info, target_threads);
  2751. /*
  2752. * On radix, arrange for TLB flushing if necessary.
  2753. * This has to be done before disabling interrupts since
  2754. * it uses smp_call_function().
  2755. */
  2756. pcpu = smp_processor_id();
  2757. if (kvm_is_radix(vc->kvm)) {
  2758. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2759. for_each_runnable_thread(i, vcpu, core_info.vc[sub])
  2760. kvmppc_prepare_radix_vcpu(vcpu, pcpu);
  2761. }
  2762. /*
  2763. * Hard-disable interrupts, and check resched flag and signals.
  2764. * If we need to reschedule or deliver a signal, clean up
  2765. * and return without going into the guest(s).
  2766. * If the mmu_ready flag has been cleared, don't go into the
  2767. * guest because that means a HPT resize operation is in progress.
  2768. */
  2769. local_irq_disable();
  2770. hard_irq_disable();
  2771. if (lazy_irq_pending() || need_resched() ||
  2772. recheck_signals(&core_info) || !vc->kvm->arch.mmu_ready) {
  2773. local_irq_enable();
  2774. vc->vcore_state = VCORE_INACTIVE;
  2775. /* Unlock all except the primary vcore */
  2776. for (sub = 1; sub < core_info.n_subcores; ++sub) {
  2777. pvc = core_info.vc[sub];
  2778. /* Put back on to the preempted vcores list */
  2779. kvmppc_vcore_preempt(pvc);
  2780. spin_unlock(&pvc->lock);
  2781. }
  2782. for (i = 0; i < controlled_threads; ++i)
  2783. kvmppc_release_hwthread(pcpu + i);
  2784. return;
  2785. }
  2786. kvmppc_clear_host_core(pcpu);
  2787. /* Decide on micro-threading (split-core) mode */
  2788. subcore_size = threads_per_subcore;
  2789. cmd_bit = stat_bit = 0;
  2790. split = core_info.n_subcores;
  2791. sip = NULL;
  2792. is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S)
  2793. && !cpu_has_feature(CPU_FTR_ARCH_300);
  2794. if (split > 1 || hpt_on_radix) {
  2795. sip = &split_info;
  2796. memset(&split_info, 0, sizeof(split_info));
  2797. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2798. split_info.vc[sub] = core_info.vc[sub];
  2799. if (is_power8) {
  2800. if (split == 2 && (dynamic_mt_modes & 2)) {
  2801. cmd_bit = HID0_POWER8_1TO2LPAR;
  2802. stat_bit = HID0_POWER8_2LPARMODE;
  2803. } else {
  2804. split = 4;
  2805. cmd_bit = HID0_POWER8_1TO4LPAR;
  2806. stat_bit = HID0_POWER8_4LPARMODE;
  2807. }
  2808. subcore_size = MAX_SMT_THREADS / split;
  2809. split_info.rpr = mfspr(SPRN_RPR);
  2810. split_info.pmmar = mfspr(SPRN_PMMAR);
  2811. split_info.ldbar = mfspr(SPRN_LDBAR);
  2812. split_info.subcore_size = subcore_size;
  2813. } else {
  2814. split_info.subcore_size = 1;
  2815. if (hpt_on_radix) {
  2816. /* Use the split_info for LPCR/LPIDR changes */
  2817. split_info.lpcr_req = vc->lpcr;
  2818. split_info.lpidr_req = vc->kvm->arch.lpid;
  2819. split_info.host_lpcr = vc->kvm->arch.host_lpcr;
  2820. split_info.do_set = 1;
  2821. }
  2822. }
  2823. /* order writes to split_info before kvm_split_mode pointer */
  2824. smp_wmb();
  2825. }
  2826. for (thr = 0; thr < controlled_threads; ++thr) {
  2827. struct paca_struct *paca = paca_ptrs[pcpu + thr];
  2828. paca->kvm_hstate.tid = thr;
  2829. paca->kvm_hstate.napping = 0;
  2830. paca->kvm_hstate.kvm_split_mode = sip;
  2831. }
  2832. /* Initiate micro-threading (split-core) on POWER8 if required */
  2833. if (cmd_bit) {
  2834. unsigned long hid0 = mfspr(SPRN_HID0);
  2835. hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
  2836. mb();
  2837. mtspr(SPRN_HID0, hid0);
  2838. isync();
  2839. for (;;) {
  2840. hid0 = mfspr(SPRN_HID0);
  2841. if (hid0 & stat_bit)
  2842. break;
  2843. cpu_relax();
  2844. }
  2845. }
  2846. /*
  2847. * On POWER8, set RWMR register.
  2848. * Since it only affects PURR and SPURR, it doesn't affect
  2849. * the host, so we don't save/restore the host value.
  2850. */
  2851. if (is_power8) {
  2852. unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
  2853. int n_online = atomic_read(&vc->online_count);
  2854. /*
  2855. * Use the 8-thread value if we're doing split-core
  2856. * or if the vcore's online count looks bogus.
  2857. */
  2858. if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
  2859. n_online >= 1 && n_online <= MAX_SMT_THREADS)
  2860. rwmr_val = p8_rwmr_values[n_online];
  2861. mtspr(SPRN_RWMR, rwmr_val);
  2862. }
  2863. /* Start all the threads */
  2864. active = 0;
  2865. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2866. thr = is_power8 ? subcore_thread_map[sub] : sub;
  2867. thr0_done = false;
  2868. active |= 1 << thr;
  2869. pvc = core_info.vc[sub];
  2870. pvc->pcpu = pcpu + thr;
  2871. for_each_runnable_thread(i, vcpu, pvc) {
  2872. kvmppc_start_thread(vcpu, pvc);
  2873. kvmppc_create_dtl_entry(vcpu, pvc);
  2874. trace_kvm_guest_enter(vcpu);
  2875. if (!vcpu->arch.ptid)
  2876. thr0_done = true;
  2877. active |= 1 << (thr + vcpu->arch.ptid);
  2878. }
  2879. /*
  2880. * We need to start the first thread of each subcore
  2881. * even if it doesn't have a vcpu.
  2882. */
  2883. if (!thr0_done)
  2884. kvmppc_start_thread(NULL, pvc);
  2885. }
  2886. /*
  2887. * Ensure that split_info.do_nap is set after setting
  2888. * the vcore pointer in the PACA of the secondaries.
  2889. */
  2890. smp_mb();
  2891. /*
  2892. * When doing micro-threading, poke the inactive threads as well.
  2893. * This gets them to the nap instruction after kvm_do_nap,
  2894. * which reduces the time taken to unsplit later.
  2895. * For POWER9 HPT guest on radix host, we need all the secondary
  2896. * threads woken up so they can do the LPCR/LPIDR change.
  2897. */
  2898. if (cmd_bit || hpt_on_radix) {
  2899. split_info.do_nap = 1; /* ask secondaries to nap when done */
  2900. for (thr = 1; thr < threads_per_subcore; ++thr)
  2901. if (!(active & (1 << thr)))
  2902. kvmppc_ipi_thread(pcpu + thr);
  2903. }
  2904. vc->vcore_state = VCORE_RUNNING;
  2905. preempt_disable();
  2906. trace_kvmppc_run_core(vc, 0);
  2907. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2908. spin_unlock(&core_info.vc[sub]->lock);
  2909. if (kvm_is_radix(vc->kvm)) {
  2910. /*
  2911. * Do we need to flush the process scoped TLB for the LPAR?
  2912. *
  2913. * On POWER9, individual threads can come in here, but the
  2914. * TLB is shared between the 4 threads in a core, hence
  2915. * invalidating on one thread invalidates for all.
  2916. * Thus we make all 4 threads use the same bit here.
  2917. *
  2918. * Hash must be flushed in realmode in order to use tlbiel.
  2919. */
  2920. kvmppc_radix_check_need_tlb_flush(vc->kvm, pcpu, NULL);
  2921. }
  2922. /*
  2923. * Interrupts will be enabled once we get into the guest,
  2924. * so tell lockdep that we're about to enable interrupts.
  2925. */
  2926. trace_hardirqs_on();
  2927. guest_enter_irqoff();
  2928. srcu_idx = srcu_read_lock(&vc->kvm->srcu);
  2929. this_cpu_disable_ftrace();
  2930. trap = __kvmppc_vcore_entry();
  2931. this_cpu_enable_ftrace();
  2932. srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
  2933. trace_hardirqs_off();
  2934. set_irq_happened(trap);
  2935. spin_lock(&vc->lock);
  2936. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  2937. vc->vcore_state = VCORE_EXITING;
  2938. /* wait for secondary threads to finish writing their state to memory */
  2939. kvmppc_wait_for_nap(controlled_threads);
  2940. /* Return to whole-core mode if we split the core earlier */
  2941. if (cmd_bit) {
  2942. unsigned long hid0 = mfspr(SPRN_HID0);
  2943. unsigned long loops = 0;
  2944. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  2945. stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  2946. mb();
  2947. mtspr(SPRN_HID0, hid0);
  2948. isync();
  2949. for (;;) {
  2950. hid0 = mfspr(SPRN_HID0);
  2951. if (!(hid0 & stat_bit))
  2952. break;
  2953. cpu_relax();
  2954. ++loops;
  2955. }
  2956. } else if (hpt_on_radix) {
  2957. /* Wait for all threads to have seen final sync */
  2958. for (thr = 1; thr < controlled_threads; ++thr) {
  2959. struct paca_struct *paca = paca_ptrs[pcpu + thr];
  2960. while (paca->kvm_hstate.kvm_split_mode) {
  2961. HMT_low();
  2962. barrier();
  2963. }
  2964. HMT_medium();
  2965. }
  2966. }
  2967. split_info.do_nap = 0;
  2968. kvmppc_set_host_core(pcpu);
  2969. local_irq_enable();
  2970. guest_exit();
  2971. /* Let secondaries go back to the offline loop */
  2972. for (i = 0; i < controlled_threads; ++i) {
  2973. kvmppc_release_hwthread(pcpu + i);
  2974. if (sip && sip->napped[i])
  2975. kvmppc_ipi_thread(pcpu + i);
  2976. cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
  2977. }
  2978. spin_unlock(&vc->lock);
  2979. /* make sure updates to secondary vcpu structs are visible now */
  2980. smp_mb();
  2981. preempt_enable();
  2982. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2983. pvc = core_info.vc[sub];
  2984. post_guest_process(pvc, pvc == vc);
  2985. }
  2986. spin_lock(&vc->lock);
  2987. out:
  2988. vc->vcore_state = VCORE_INACTIVE;
  2989. trace_kvmppc_run_core(vc, 1);
  2990. }
  2991. /*
  2992. * Load up hypervisor-mode registers on P9.
  2993. */
  2994. static int kvmhv_load_hv_regs_and_go(struct kvm_vcpu *vcpu, u64 time_limit,
  2995. unsigned long lpcr)
  2996. {
  2997. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  2998. s64 hdec;
  2999. u64 tb, purr, spurr;
  3000. int trap;
  3001. unsigned long host_hfscr = mfspr(SPRN_HFSCR);
  3002. unsigned long host_ciabr = mfspr(SPRN_CIABR);
  3003. unsigned long host_dawr = mfspr(SPRN_DAWR);
  3004. unsigned long host_dawrx = mfspr(SPRN_DAWRX);
  3005. unsigned long host_psscr = mfspr(SPRN_PSSCR);
  3006. unsigned long host_pidr = mfspr(SPRN_PID);
  3007. hdec = time_limit - mftb();
  3008. if (hdec < 0)
  3009. return BOOK3S_INTERRUPT_HV_DECREMENTER;
  3010. mtspr(SPRN_HDEC, hdec);
  3011. if (vc->tb_offset) {
  3012. u64 new_tb = mftb() + vc->tb_offset;
  3013. mtspr(SPRN_TBU40, new_tb);
  3014. tb = mftb();
  3015. if ((tb & 0xffffff) < (new_tb & 0xffffff))
  3016. mtspr(SPRN_TBU40, new_tb + 0x1000000);
  3017. vc->tb_offset_applied = vc->tb_offset;
  3018. }
  3019. if (vc->pcr)
  3020. mtspr(SPRN_PCR, vc->pcr);
  3021. mtspr(SPRN_DPDES, vc->dpdes);
  3022. mtspr(SPRN_VTB, vc->vtb);
  3023. local_paca->kvm_hstate.host_purr = mfspr(SPRN_PURR);
  3024. local_paca->kvm_hstate.host_spurr = mfspr(SPRN_SPURR);
  3025. mtspr(SPRN_PURR, vcpu->arch.purr);
  3026. mtspr(SPRN_SPURR, vcpu->arch.spurr);
  3027. if (cpu_has_feature(CPU_FTR_DAWR)) {
  3028. mtspr(SPRN_DAWR, vcpu->arch.dawr);
  3029. mtspr(SPRN_DAWRX, vcpu->arch.dawrx);
  3030. }
  3031. mtspr(SPRN_CIABR, vcpu->arch.ciabr);
  3032. mtspr(SPRN_IC, vcpu->arch.ic);
  3033. mtspr(SPRN_PID, vcpu->arch.pid);
  3034. mtspr(SPRN_PSSCR, vcpu->arch.psscr | PSSCR_EC |
  3035. (local_paca->kvm_hstate.fake_suspend << PSSCR_FAKE_SUSPEND_LG));
  3036. mtspr(SPRN_HFSCR, vcpu->arch.hfscr);
  3037. mtspr(SPRN_SPRG0, vcpu->arch.shregs.sprg0);
  3038. mtspr(SPRN_SPRG1, vcpu->arch.shregs.sprg1);
  3039. mtspr(SPRN_SPRG2, vcpu->arch.shregs.sprg2);
  3040. mtspr(SPRN_SPRG3, vcpu->arch.shregs.sprg3);
  3041. mtspr(SPRN_AMOR, ~0UL);
  3042. mtspr(SPRN_LPCR, lpcr);
  3043. isync();
  3044. kvmppc_xive_push_vcpu(vcpu);
  3045. mtspr(SPRN_SRR0, vcpu->arch.shregs.srr0);
  3046. mtspr(SPRN_SRR1, vcpu->arch.shregs.srr1);
  3047. trap = __kvmhv_vcpu_entry_p9(vcpu);
  3048. /* Advance host PURR/SPURR by the amount used by guest */
  3049. purr = mfspr(SPRN_PURR);
  3050. spurr = mfspr(SPRN_SPURR);
  3051. mtspr(SPRN_PURR, local_paca->kvm_hstate.host_purr +
  3052. purr - vcpu->arch.purr);
  3053. mtspr(SPRN_SPURR, local_paca->kvm_hstate.host_spurr +
  3054. spurr - vcpu->arch.spurr);
  3055. vcpu->arch.purr = purr;
  3056. vcpu->arch.spurr = spurr;
  3057. vcpu->arch.ic = mfspr(SPRN_IC);
  3058. vcpu->arch.pid = mfspr(SPRN_PID);
  3059. vcpu->arch.psscr = mfspr(SPRN_PSSCR) & PSSCR_GUEST_VIS;
  3060. vcpu->arch.shregs.sprg0 = mfspr(SPRN_SPRG0);
  3061. vcpu->arch.shregs.sprg1 = mfspr(SPRN_SPRG1);
  3062. vcpu->arch.shregs.sprg2 = mfspr(SPRN_SPRG2);
  3063. vcpu->arch.shregs.sprg3 = mfspr(SPRN_SPRG3);
  3064. mtspr(SPRN_PSSCR, host_psscr);
  3065. mtspr(SPRN_HFSCR, host_hfscr);
  3066. mtspr(SPRN_CIABR, host_ciabr);
  3067. mtspr(SPRN_DAWR, host_dawr);
  3068. mtspr(SPRN_DAWRX, host_dawrx);
  3069. mtspr(SPRN_PID, host_pidr);
  3070. /*
  3071. * Since this is radix, do a eieio; tlbsync; ptesync sequence in
  3072. * case we interrupted the guest between a tlbie and a ptesync.
  3073. */
  3074. asm volatile("eieio; tlbsync; ptesync");
  3075. mtspr(SPRN_LPID, vcpu->kvm->arch.host_lpid); /* restore host LPID */
  3076. isync();
  3077. vc->dpdes = mfspr(SPRN_DPDES);
  3078. vc->vtb = mfspr(SPRN_VTB);
  3079. mtspr(SPRN_DPDES, 0);
  3080. if (vc->pcr)
  3081. mtspr(SPRN_PCR, 0);
  3082. if (vc->tb_offset_applied) {
  3083. u64 new_tb = mftb() - vc->tb_offset_applied;
  3084. mtspr(SPRN_TBU40, new_tb);
  3085. tb = mftb();
  3086. if ((tb & 0xffffff) < (new_tb & 0xffffff))
  3087. mtspr(SPRN_TBU40, new_tb + 0x1000000);
  3088. vc->tb_offset_applied = 0;
  3089. }
  3090. mtspr(SPRN_HDEC, 0x7fffffff);
  3091. mtspr(SPRN_LPCR, vcpu->kvm->arch.host_lpcr);
  3092. return trap;
  3093. }
  3094. /*
  3095. * Virtual-mode guest entry for POWER9 and later when the host and
  3096. * guest are both using the radix MMU. The LPIDR has already been set.
  3097. */
  3098. int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,
  3099. unsigned long lpcr)
  3100. {
  3101. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  3102. unsigned long host_dscr = mfspr(SPRN_DSCR);
  3103. unsigned long host_tidr = mfspr(SPRN_TIDR);
  3104. unsigned long host_iamr = mfspr(SPRN_IAMR);
  3105. s64 dec;
  3106. u64 tb;
  3107. int trap, save_pmu;
  3108. dec = mfspr(SPRN_DEC);
  3109. tb = mftb();
  3110. if (dec < 512)
  3111. return BOOK3S_INTERRUPT_HV_DECREMENTER;
  3112. local_paca->kvm_hstate.dec_expires = dec + tb;
  3113. if (local_paca->kvm_hstate.dec_expires < time_limit)
  3114. time_limit = local_paca->kvm_hstate.dec_expires;
  3115. vcpu->arch.ceded = 0;
  3116. kvmhv_save_host_pmu(); /* saves it to PACA kvm_hstate */
  3117. kvmppc_subcore_enter_guest();
  3118. vc->entry_exit_map = 1;
  3119. vc->in_guest = 1;
  3120. if (vcpu->arch.vpa.pinned_addr) {
  3121. struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
  3122. u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
  3123. lp->yield_count = cpu_to_be32(yield_count);
  3124. vcpu->arch.vpa.dirty = 1;
  3125. }
  3126. if (cpu_has_feature(CPU_FTR_TM) ||
  3127. cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
  3128. kvmppc_restore_tm_hv(vcpu, vcpu->arch.shregs.msr, true);
  3129. kvmhv_load_guest_pmu(vcpu);
  3130. msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX);
  3131. load_fp_state(&vcpu->arch.fp);
  3132. #ifdef CONFIG_ALTIVEC
  3133. load_vr_state(&vcpu->arch.vr);
  3134. #endif
  3135. mtspr(SPRN_DSCR, vcpu->arch.dscr);
  3136. mtspr(SPRN_IAMR, vcpu->arch.iamr);
  3137. mtspr(SPRN_PSPB, vcpu->arch.pspb);
  3138. mtspr(SPRN_FSCR, vcpu->arch.fscr);
  3139. mtspr(SPRN_TAR, vcpu->arch.tar);
  3140. mtspr(SPRN_EBBHR, vcpu->arch.ebbhr);
  3141. mtspr(SPRN_EBBRR, vcpu->arch.ebbrr);
  3142. mtspr(SPRN_BESCR, vcpu->arch.bescr);
  3143. mtspr(SPRN_WORT, vcpu->arch.wort);
  3144. mtspr(SPRN_TIDR, vcpu->arch.tid);
  3145. mtspr(SPRN_DAR, vcpu->arch.shregs.dar);
  3146. mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);
  3147. mtspr(SPRN_AMR, vcpu->arch.amr);
  3148. mtspr(SPRN_UAMOR, vcpu->arch.uamor);
  3149. if (!(vcpu->arch.ctrl & 1))
  3150. mtspr(SPRN_CTRLT, mfspr(SPRN_CTRLF) & ~1);
  3151. mtspr(SPRN_DEC, vcpu->arch.dec_expires - mftb());
  3152. if (kvmhv_on_pseries()) {
  3153. /* call our hypervisor to load up HV regs and go */
  3154. struct hv_guest_state hvregs;
  3155. kvmhv_save_hv_regs(vcpu, &hvregs);
  3156. hvregs.lpcr = lpcr;
  3157. vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
  3158. hvregs.version = HV_GUEST_STATE_VERSION;
  3159. if (vcpu->arch.nested) {
  3160. hvregs.lpid = vcpu->arch.nested->shadow_lpid;
  3161. hvregs.vcpu_token = vcpu->arch.nested_vcpu_id;
  3162. } else {
  3163. hvregs.lpid = vcpu->kvm->arch.lpid;
  3164. hvregs.vcpu_token = vcpu->vcpu_id;
  3165. }
  3166. hvregs.hdec_expiry = time_limit;
  3167. trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs),
  3168. __pa(&vcpu->arch.regs));
  3169. kvmhv_restore_hv_return_state(vcpu, &hvregs);
  3170. vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
  3171. vcpu->arch.shregs.dar = mfspr(SPRN_DAR);
  3172. vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);
  3173. /* H_CEDE has to be handled now, not later */
  3174. if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested &&
  3175. kvmppc_get_gpr(vcpu, 3) == H_CEDE) {
  3176. kvmppc_nested_cede(vcpu);
  3177. trap = 0;
  3178. }
  3179. } else {
  3180. trap = kvmhv_load_hv_regs_and_go(vcpu, time_limit, lpcr);
  3181. }
  3182. vcpu->arch.slb_max = 0;
  3183. dec = mfspr(SPRN_DEC);
  3184. tb = mftb();
  3185. vcpu->arch.dec_expires = dec + tb;
  3186. vcpu->cpu = -1;
  3187. vcpu->arch.thread_cpu = -1;
  3188. vcpu->arch.ctrl = mfspr(SPRN_CTRLF);
  3189. vcpu->arch.iamr = mfspr(SPRN_IAMR);
  3190. vcpu->arch.pspb = mfspr(SPRN_PSPB);
  3191. vcpu->arch.fscr = mfspr(SPRN_FSCR);
  3192. vcpu->arch.tar = mfspr(SPRN_TAR);
  3193. vcpu->arch.ebbhr = mfspr(SPRN_EBBHR);
  3194. vcpu->arch.ebbrr = mfspr(SPRN_EBBRR);
  3195. vcpu->arch.bescr = mfspr(SPRN_BESCR);
  3196. vcpu->arch.wort = mfspr(SPRN_WORT);
  3197. vcpu->arch.tid = mfspr(SPRN_TIDR);
  3198. vcpu->arch.amr = mfspr(SPRN_AMR);
  3199. vcpu->arch.uamor = mfspr(SPRN_UAMOR);
  3200. vcpu->arch.dscr = mfspr(SPRN_DSCR);
  3201. mtspr(SPRN_PSPB, 0);
  3202. mtspr(SPRN_WORT, 0);
  3203. mtspr(SPRN_AMR, 0);
  3204. mtspr(SPRN_UAMOR, 0);
  3205. mtspr(SPRN_DSCR, host_dscr);
  3206. mtspr(SPRN_TIDR, host_tidr);
  3207. mtspr(SPRN_IAMR, host_iamr);
  3208. mtspr(SPRN_PSPB, 0);
  3209. msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX);
  3210. store_fp_state(&vcpu->arch.fp);
  3211. #ifdef CONFIG_ALTIVEC
  3212. store_vr_state(&vcpu->arch.vr);
  3213. #endif
  3214. if (cpu_has_feature(CPU_FTR_TM) ||
  3215. cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
  3216. kvmppc_save_tm_hv(vcpu, vcpu->arch.shregs.msr, true);
  3217. save_pmu = 1;
  3218. if (vcpu->arch.vpa.pinned_addr) {
  3219. struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
  3220. u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
  3221. lp->yield_count = cpu_to_be32(yield_count);
  3222. vcpu->arch.vpa.dirty = 1;
  3223. save_pmu = lp->pmcregs_in_use;
  3224. }
  3225. kvmhv_save_guest_pmu(vcpu, save_pmu);
  3226. vc->entry_exit_map = 0x101;
  3227. vc->in_guest = 0;
  3228. mtspr(SPRN_DEC, local_paca->kvm_hstate.dec_expires - mftb());
  3229. kvmhv_load_host_pmu();
  3230. kvmppc_subcore_exit_guest();
  3231. return trap;
  3232. }
  3233. /*
  3234. * Wait for some other vcpu thread to execute us, and
  3235. * wake us up when we need to handle something in the host.
  3236. */
  3237. static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
  3238. struct kvm_vcpu *vcpu, int wait_state)
  3239. {
  3240. DEFINE_WAIT(wait);
  3241. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  3242. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  3243. spin_unlock(&vc->lock);
  3244. schedule();
  3245. spin_lock(&vc->lock);
  3246. }
  3247. finish_wait(&vcpu->arch.cpu_run, &wait);
  3248. }
  3249. static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
  3250. {
  3251. /* 10us base */
  3252. if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
  3253. vc->halt_poll_ns = 10000;
  3254. else
  3255. vc->halt_poll_ns *= halt_poll_ns_grow;
  3256. }
  3257. static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
  3258. {
  3259. if (halt_poll_ns_shrink == 0)
  3260. vc->halt_poll_ns = 0;
  3261. else
  3262. vc->halt_poll_ns /= halt_poll_ns_shrink;
  3263. }
  3264. #ifdef CONFIG_KVM_XICS
  3265. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  3266. {
  3267. if (!xive_enabled())
  3268. return false;
  3269. return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
  3270. vcpu->arch.xive_saved_state.cppr;
  3271. }
  3272. #else
  3273. static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
  3274. {
  3275. return false;
  3276. }
  3277. #endif /* CONFIG_KVM_XICS */
  3278. static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
  3279. {
  3280. if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
  3281. kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
  3282. return true;
  3283. return false;
  3284. }
  3285. /*
  3286. * Check to see if any of the runnable vcpus on the vcore have pending
  3287. * exceptions or are no longer ceded
  3288. */
  3289. static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  3290. {
  3291. struct kvm_vcpu *vcpu;
  3292. int i;
  3293. for_each_runnable_thread(i, vcpu, vc) {
  3294. if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
  3295. return 1;
  3296. }
  3297. return 0;
  3298. }
  3299. /*
  3300. * All the vcpus in this vcore are idle, so wait for a decrementer
  3301. * or external interrupt to one of the vcpus. vc->lock is held.
  3302. */
  3303. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  3304. {
  3305. ktime_t cur, start_poll, start_wait;
  3306. int do_sleep = 1;
  3307. u64 block_ns;
  3308. DECLARE_SWAITQUEUE(wait);
  3309. /* Poll for pending exceptions and ceded state */
  3310. cur = start_poll = ktime_get();
  3311. if (vc->halt_poll_ns) {
  3312. ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
  3313. ++vc->runner->stat.halt_attempted_poll;
  3314. vc->vcore_state = VCORE_POLLING;
  3315. spin_unlock(&vc->lock);
  3316. do {
  3317. if (kvmppc_vcore_check_block(vc)) {
  3318. do_sleep = 0;
  3319. break;
  3320. }
  3321. cur = ktime_get();
  3322. } while (single_task_running() && ktime_before(cur, stop));
  3323. spin_lock(&vc->lock);
  3324. vc->vcore_state = VCORE_INACTIVE;
  3325. if (!do_sleep) {
  3326. ++vc->runner->stat.halt_successful_poll;
  3327. goto out;
  3328. }
  3329. }
  3330. prepare_to_swait_exclusive(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  3331. if (kvmppc_vcore_check_block(vc)) {
  3332. finish_swait(&vc->wq, &wait);
  3333. do_sleep = 0;
  3334. /* If we polled, count this as a successful poll */
  3335. if (vc->halt_poll_ns)
  3336. ++vc->runner->stat.halt_successful_poll;
  3337. goto out;
  3338. }
  3339. start_wait = ktime_get();
  3340. vc->vcore_state = VCORE_SLEEPING;
  3341. trace_kvmppc_vcore_blocked(vc, 0);
  3342. spin_unlock(&vc->lock);
  3343. schedule();
  3344. finish_swait(&vc->wq, &wait);
  3345. spin_lock(&vc->lock);
  3346. vc->vcore_state = VCORE_INACTIVE;
  3347. trace_kvmppc_vcore_blocked(vc, 1);
  3348. ++vc->runner->stat.halt_successful_wait;
  3349. cur = ktime_get();
  3350. out:
  3351. block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
  3352. /* Attribute wait time */
  3353. if (do_sleep) {
  3354. vc->runner->stat.halt_wait_ns +=
  3355. ktime_to_ns(cur) - ktime_to_ns(start_wait);
  3356. /* Attribute failed poll time */
  3357. if (vc->halt_poll_ns)
  3358. vc->runner->stat.halt_poll_fail_ns +=
  3359. ktime_to_ns(start_wait) -
  3360. ktime_to_ns(start_poll);
  3361. } else {
  3362. /* Attribute successful poll time */
  3363. if (vc->halt_poll_ns)
  3364. vc->runner->stat.halt_poll_success_ns +=
  3365. ktime_to_ns(cur) -
  3366. ktime_to_ns(start_poll);
  3367. }
  3368. /* Adjust poll time */
  3369. if (halt_poll_ns) {
  3370. if (block_ns <= vc->halt_poll_ns)
  3371. ;
  3372. /* We slept and blocked for longer than the max halt time */
  3373. else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
  3374. shrink_halt_poll_ns(vc);
  3375. /* We slept and our poll time is too small */
  3376. else if (vc->halt_poll_ns < halt_poll_ns &&
  3377. block_ns < halt_poll_ns)
  3378. grow_halt_poll_ns(vc);
  3379. if (vc->halt_poll_ns > halt_poll_ns)
  3380. vc->halt_poll_ns = halt_poll_ns;
  3381. } else
  3382. vc->halt_poll_ns = 0;
  3383. trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
  3384. }
  3385. /*
  3386. * This never fails for a radix guest, as none of the operations it does
  3387. * for a radix guest can fail or have a way to report failure.
  3388. * kvmhv_run_single_vcpu() relies on this fact.
  3389. */
  3390. static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
  3391. {
  3392. int r = 0;
  3393. struct kvm *kvm = vcpu->kvm;
  3394. mutex_lock(&kvm->lock);
  3395. if (!kvm->arch.mmu_ready) {
  3396. if (!kvm_is_radix(kvm))
  3397. r = kvmppc_hv_setup_htab_rma(vcpu);
  3398. if (!r) {
  3399. if (cpu_has_feature(CPU_FTR_ARCH_300))
  3400. kvmppc_setup_partition_table(kvm);
  3401. kvm->arch.mmu_ready = 1;
  3402. }
  3403. }
  3404. mutex_unlock(&kvm->lock);
  3405. return r;
  3406. }
  3407. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  3408. {
  3409. int n_ceded, i, r;
  3410. struct kvmppc_vcore *vc;
  3411. struct kvm_vcpu *v;
  3412. trace_kvmppc_run_vcpu_enter(vcpu);
  3413. kvm_run->exit_reason = 0;
  3414. vcpu->arch.ret = RESUME_GUEST;
  3415. vcpu->arch.trap = 0;
  3416. kvmppc_update_vpas(vcpu);
  3417. /*
  3418. * Synchronize with other threads in this virtual core
  3419. */
  3420. vc = vcpu->arch.vcore;
  3421. spin_lock(&vc->lock);
  3422. vcpu->arch.ceded = 0;
  3423. vcpu->arch.run_task = current;
  3424. vcpu->arch.kvm_run = kvm_run;
  3425. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  3426. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  3427. vcpu->arch.busy_preempt = TB_NIL;
  3428. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
  3429. ++vc->n_runnable;
  3430. /*
  3431. * This happens the first time this is called for a vcpu.
  3432. * If the vcore is already running, we may be able to start
  3433. * this thread straight away and have it join in.
  3434. */
  3435. if (!signal_pending(current)) {
  3436. if ((vc->vcore_state == VCORE_PIGGYBACK ||
  3437. vc->vcore_state == VCORE_RUNNING) &&
  3438. !VCORE_IS_EXITING(vc)) {
  3439. kvmppc_create_dtl_entry(vcpu, vc);
  3440. kvmppc_start_thread(vcpu, vc);
  3441. trace_kvm_guest_enter(vcpu);
  3442. } else if (vc->vcore_state == VCORE_SLEEPING) {
  3443. swake_up_one(&vc->wq);
  3444. }
  3445. }
  3446. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  3447. !signal_pending(current)) {
  3448. /* See if the MMU is ready to go */
  3449. if (!vcpu->kvm->arch.mmu_ready) {
  3450. spin_unlock(&vc->lock);
  3451. r = kvmhv_setup_mmu(vcpu);
  3452. spin_lock(&vc->lock);
  3453. if (r) {
  3454. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3455. kvm_run->fail_entry.
  3456. hardware_entry_failure_reason = 0;
  3457. vcpu->arch.ret = r;
  3458. break;
  3459. }
  3460. }
  3461. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  3462. kvmppc_vcore_end_preempt(vc);
  3463. if (vc->vcore_state != VCORE_INACTIVE) {
  3464. kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
  3465. continue;
  3466. }
  3467. for_each_runnable_thread(i, v, vc) {
  3468. kvmppc_core_prepare_to_enter(v);
  3469. if (signal_pending(v->arch.run_task)) {
  3470. kvmppc_remove_runnable(vc, v);
  3471. v->stat.signal_exits++;
  3472. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  3473. v->arch.ret = -EINTR;
  3474. wake_up(&v->arch.cpu_run);
  3475. }
  3476. }
  3477. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  3478. break;
  3479. n_ceded = 0;
  3480. for_each_runnable_thread(i, v, vc) {
  3481. if (!kvmppc_vcpu_woken(v))
  3482. n_ceded += v->arch.ceded;
  3483. else
  3484. v->arch.ceded = 0;
  3485. }
  3486. vc->runner = vcpu;
  3487. if (n_ceded == vc->n_runnable) {
  3488. kvmppc_vcore_blocked(vc);
  3489. } else if (need_resched()) {
  3490. kvmppc_vcore_preempt(vc);
  3491. /* Let something else run */
  3492. cond_resched_lock(&vc->lock);
  3493. if (vc->vcore_state == VCORE_PREEMPT)
  3494. kvmppc_vcore_end_preempt(vc);
  3495. } else {
  3496. kvmppc_run_core(vc);
  3497. }
  3498. vc->runner = NULL;
  3499. }
  3500. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  3501. (vc->vcore_state == VCORE_RUNNING ||
  3502. vc->vcore_state == VCORE_EXITING ||
  3503. vc->vcore_state == VCORE_PIGGYBACK))
  3504. kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
  3505. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  3506. kvmppc_vcore_end_preempt(vc);
  3507. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  3508. kvmppc_remove_runnable(vc, vcpu);
  3509. vcpu->stat.signal_exits++;
  3510. kvm_run->exit_reason = KVM_EXIT_INTR;
  3511. vcpu->arch.ret = -EINTR;
  3512. }
  3513. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  3514. /* Wake up some vcpu to run the core */
  3515. i = -1;
  3516. v = next_runnable_thread(vc, &i);
  3517. wake_up(&v->arch.cpu_run);
  3518. }
  3519. trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
  3520. spin_unlock(&vc->lock);
  3521. return vcpu->arch.ret;
  3522. }
  3523. int kvmhv_run_single_vcpu(struct kvm_run *kvm_run,
  3524. struct kvm_vcpu *vcpu, u64 time_limit,
  3525. unsigned long lpcr)
  3526. {
  3527. int trap, r, pcpu;
  3528. int srcu_idx;
  3529. struct kvmppc_vcore *vc;
  3530. struct kvm *kvm = vcpu->kvm;
  3531. struct kvm_nested_guest *nested = vcpu->arch.nested;
  3532. trace_kvmppc_run_vcpu_enter(vcpu);
  3533. kvm_run->exit_reason = 0;
  3534. vcpu->arch.ret = RESUME_GUEST;
  3535. vcpu->arch.trap = 0;
  3536. vc = vcpu->arch.vcore;
  3537. vcpu->arch.ceded = 0;
  3538. vcpu->arch.run_task = current;
  3539. vcpu->arch.kvm_run = kvm_run;
  3540. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  3541. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  3542. vcpu->arch.busy_preempt = TB_NIL;
  3543. vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;
  3544. vc->runnable_threads[0] = vcpu;
  3545. vc->n_runnable = 1;
  3546. vc->runner = vcpu;
  3547. /* See if the MMU is ready to go */
  3548. if (!kvm->arch.mmu_ready)
  3549. kvmhv_setup_mmu(vcpu);
  3550. if (need_resched())
  3551. cond_resched();
  3552. kvmppc_update_vpas(vcpu);
  3553. init_vcore_to_run(vc);
  3554. vc->preempt_tb = TB_NIL;
  3555. preempt_disable();
  3556. pcpu = smp_processor_id();
  3557. vc->pcpu = pcpu;
  3558. kvmppc_prepare_radix_vcpu(vcpu, pcpu);
  3559. local_irq_disable();
  3560. hard_irq_disable();
  3561. if (signal_pending(current))
  3562. goto sigpend;
  3563. if (lazy_irq_pending() || need_resched() || !kvm->arch.mmu_ready)
  3564. goto out;
  3565. if (!nested) {
  3566. kvmppc_core_prepare_to_enter(vcpu);
  3567. if (vcpu->arch.doorbell_request) {
  3568. vc->dpdes = 1;
  3569. smp_wmb();
  3570. vcpu->arch.doorbell_request = 0;
  3571. }
  3572. if (test_bit(BOOK3S_IRQPRIO_EXTERNAL,
  3573. &vcpu->arch.pending_exceptions))
  3574. lpcr |= LPCR_MER;
  3575. } else if (vcpu->arch.pending_exceptions ||
  3576. vcpu->arch.doorbell_request ||
  3577. xive_interrupt_pending(vcpu)) {
  3578. vcpu->arch.ret = RESUME_HOST;
  3579. goto out;
  3580. }
  3581. kvmppc_clear_host_core(pcpu);
  3582. local_paca->kvm_hstate.tid = 0;
  3583. local_paca->kvm_hstate.napping = 0;
  3584. local_paca->kvm_hstate.kvm_split_mode = NULL;
  3585. kvmppc_start_thread(vcpu, vc);
  3586. kvmppc_create_dtl_entry(vcpu, vc);
  3587. trace_kvm_guest_enter(vcpu);
  3588. vc->vcore_state = VCORE_RUNNING;
  3589. trace_kvmppc_run_core(vc, 0);
  3590. if (cpu_has_feature(CPU_FTR_HVMODE))
  3591. kvmppc_radix_check_need_tlb_flush(kvm, pcpu, nested);
  3592. trace_hardirqs_on();
  3593. guest_enter_irqoff();
  3594. srcu_idx = srcu_read_lock(&kvm->srcu);
  3595. this_cpu_disable_ftrace();
  3596. trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr);
  3597. vcpu->arch.trap = trap;
  3598. this_cpu_enable_ftrace();
  3599. srcu_read_unlock(&kvm->srcu, srcu_idx);
  3600. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  3601. mtspr(SPRN_LPID, kvm->arch.host_lpid);
  3602. isync();
  3603. }
  3604. trace_hardirqs_off();
  3605. set_irq_happened(trap);
  3606. kvmppc_set_host_core(pcpu);
  3607. local_irq_enable();
  3608. guest_exit();
  3609. cpumask_clear_cpu(pcpu, &kvm->arch.cpu_in_guest);
  3610. preempt_enable();
  3611. /* cancel pending decrementer exception if DEC is now positive */
  3612. if (get_tb() < vcpu->arch.dec_expires && kvmppc_core_pending_dec(vcpu))
  3613. kvmppc_core_dequeue_dec(vcpu);
  3614. trace_kvm_guest_exit(vcpu);
  3615. r = RESUME_GUEST;
  3616. if (trap) {
  3617. if (!nested)
  3618. r = kvmppc_handle_exit_hv(kvm_run, vcpu, current);
  3619. else
  3620. r = kvmppc_handle_nested_exit(vcpu);
  3621. }
  3622. vcpu->arch.ret = r;
  3623. if (is_kvmppc_resume_guest(r) && vcpu->arch.ceded &&
  3624. !kvmppc_vcpu_woken(vcpu)) {
  3625. kvmppc_set_timer(vcpu);
  3626. while (vcpu->arch.ceded && !kvmppc_vcpu_woken(vcpu)) {
  3627. if (signal_pending(current)) {
  3628. vcpu->stat.signal_exits++;
  3629. kvm_run->exit_reason = KVM_EXIT_INTR;
  3630. vcpu->arch.ret = -EINTR;
  3631. break;
  3632. }
  3633. spin_lock(&vc->lock);
  3634. kvmppc_vcore_blocked(vc);
  3635. spin_unlock(&vc->lock);
  3636. }
  3637. }
  3638. vcpu->arch.ceded = 0;
  3639. vc->vcore_state = VCORE_INACTIVE;
  3640. trace_kvmppc_run_core(vc, 1);
  3641. done:
  3642. kvmppc_remove_runnable(vc, vcpu);
  3643. trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
  3644. return vcpu->arch.ret;
  3645. sigpend:
  3646. vcpu->stat.signal_exits++;
  3647. kvm_run->exit_reason = KVM_EXIT_INTR;
  3648. vcpu->arch.ret = -EINTR;
  3649. out:
  3650. local_irq_enable();
  3651. preempt_enable();
  3652. goto done;
  3653. }
  3654. static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
  3655. {
  3656. int r;
  3657. int srcu_idx;
  3658. unsigned long ebb_regs[3] = {}; /* shut up GCC */
  3659. unsigned long user_tar = 0;
  3660. unsigned int user_vrsave;
  3661. struct kvm *kvm;
  3662. if (!vcpu->arch.sane) {
  3663. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3664. return -EINVAL;
  3665. }
  3666. /*
  3667. * Don't allow entry with a suspended transaction, because
  3668. * the guest entry/exit code will lose it.
  3669. * If the guest has TM enabled, save away their TM-related SPRs
  3670. * (they will get restored by the TM unavailable interrupt).
  3671. */
  3672. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  3673. if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
  3674. (current->thread.regs->msr & MSR_TM)) {
  3675. if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
  3676. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3677. run->fail_entry.hardware_entry_failure_reason = 0;
  3678. return -EINVAL;
  3679. }
  3680. /* Enable TM so we can read the TM SPRs */
  3681. mtmsr(mfmsr() | MSR_TM);
  3682. current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
  3683. current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
  3684. current->thread.tm_texasr = mfspr(SPRN_TEXASR);
  3685. current->thread.regs->msr &= ~MSR_TM;
  3686. }
  3687. #endif
  3688. /*
  3689. * Force online to 1 for the sake of old userspace which doesn't
  3690. * set it.
  3691. */
  3692. if (!vcpu->arch.online) {
  3693. atomic_inc(&vcpu->arch.vcore->online_count);
  3694. vcpu->arch.online = 1;
  3695. }
  3696. kvmppc_core_prepare_to_enter(vcpu);
  3697. /* No need to go into the guest when all we'll do is come back out */
  3698. if (signal_pending(current)) {
  3699. run->exit_reason = KVM_EXIT_INTR;
  3700. return -EINTR;
  3701. }
  3702. kvm = vcpu->kvm;
  3703. atomic_inc(&kvm->arch.vcpus_running);
  3704. /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
  3705. smp_mb();
  3706. flush_all_to_thread(current);
  3707. /* Save userspace EBB and other register values */
  3708. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  3709. ebb_regs[0] = mfspr(SPRN_EBBHR);
  3710. ebb_regs[1] = mfspr(SPRN_EBBRR);
  3711. ebb_regs[2] = mfspr(SPRN_BESCR);
  3712. user_tar = mfspr(SPRN_TAR);
  3713. }
  3714. user_vrsave = mfspr(SPRN_VRSAVE);
  3715. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  3716. vcpu->arch.pgdir = current->mm->pgd;
  3717. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  3718. do {
  3719. /*
  3720. * The early POWER9 chips that can't mix radix and HPT threads
  3721. * on the same core also need the workaround for the problem
  3722. * where the TLB would prefetch entries in the guest exit path
  3723. * for radix guests using the guest PIDR value and LPID 0.
  3724. * The workaround is in the old path (kvmppc_run_vcpu())
  3725. * but not the new path (kvmhv_run_single_vcpu()).
  3726. */
  3727. if (kvm->arch.threads_indep && kvm_is_radix(kvm) &&
  3728. !no_mixing_hpt_and_radix)
  3729. r = kvmhv_run_single_vcpu(run, vcpu, ~(u64)0,
  3730. vcpu->arch.vcore->lpcr);
  3731. else
  3732. r = kvmppc_run_vcpu(run, vcpu);
  3733. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  3734. !(vcpu->arch.shregs.msr & MSR_PR)) {
  3735. trace_kvm_hcall_enter(vcpu);
  3736. r = kvmppc_pseries_do_hcall(vcpu);
  3737. trace_kvm_hcall_exit(vcpu, r);
  3738. kvmppc_core_prepare_to_enter(vcpu);
  3739. } else if (r == RESUME_PAGE_FAULT) {
  3740. srcu_idx = srcu_read_lock(&kvm->srcu);
  3741. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  3742. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  3743. srcu_read_unlock(&kvm->srcu, srcu_idx);
  3744. } else if (r == RESUME_PASSTHROUGH) {
  3745. if (WARN_ON(xive_enabled()))
  3746. r = H_SUCCESS;
  3747. else
  3748. r = kvmppc_xics_rm_complete(vcpu, 0);
  3749. }
  3750. } while (is_kvmppc_resume_guest(r));
  3751. /* Restore userspace EBB and other register values */
  3752. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  3753. mtspr(SPRN_EBBHR, ebb_regs[0]);
  3754. mtspr(SPRN_EBBRR, ebb_regs[1]);
  3755. mtspr(SPRN_BESCR, ebb_regs[2]);
  3756. mtspr(SPRN_TAR, user_tar);
  3757. mtspr(SPRN_FSCR, current->thread.fscr);
  3758. }
  3759. mtspr(SPRN_VRSAVE, user_vrsave);
  3760. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  3761. atomic_dec(&kvm->arch.vcpus_running);
  3762. return r;
  3763. }
  3764. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  3765. int shift, int sllp)
  3766. {
  3767. (*sps)->page_shift = shift;
  3768. (*sps)->slb_enc = sllp;
  3769. (*sps)->enc[0].page_shift = shift;
  3770. (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
  3771. /*
  3772. * Add 16MB MPSS support (may get filtered out by userspace)
  3773. */
  3774. if (shift != 24) {
  3775. int penc = kvmppc_pgsize_lp_encoding(shift, 24);
  3776. if (penc != -1) {
  3777. (*sps)->enc[1].page_shift = 24;
  3778. (*sps)->enc[1].pte_enc = penc;
  3779. }
  3780. }
  3781. (*sps)++;
  3782. }
  3783. static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
  3784. struct kvm_ppc_smmu_info *info)
  3785. {
  3786. struct kvm_ppc_one_seg_page_size *sps;
  3787. /*
  3788. * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
  3789. * POWER7 doesn't support keys for instruction accesses,
  3790. * POWER8 and POWER9 do.
  3791. */
  3792. info->data_keys = 32;
  3793. info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
  3794. /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
  3795. info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
  3796. info->slb_size = 32;
  3797. /* We only support these sizes for now, and no muti-size segments */
  3798. sps = &info->sps[0];
  3799. kvmppc_add_seg_page_size(&sps, 12, 0);
  3800. kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
  3801. kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
  3802. /* If running as a nested hypervisor, we don't support HPT guests */
  3803. if (kvmhv_on_pseries())
  3804. info->flags |= KVM_PPC_NO_HASH;
  3805. return 0;
  3806. }
  3807. /*
  3808. * Get (and clear) the dirty memory log for a memory slot.
  3809. */
  3810. static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
  3811. struct kvm_dirty_log *log)
  3812. {
  3813. struct kvm_memslots *slots;
  3814. struct kvm_memory_slot *memslot;
  3815. int i, r;
  3816. unsigned long n;
  3817. unsigned long *buf, *p;
  3818. struct kvm_vcpu *vcpu;
  3819. mutex_lock(&kvm->slots_lock);
  3820. r = -EINVAL;
  3821. if (log->slot >= KVM_USER_MEM_SLOTS)
  3822. goto out;
  3823. slots = kvm_memslots(kvm);
  3824. memslot = id_to_memslot(slots, log->slot);
  3825. r = -ENOENT;
  3826. if (!memslot->dirty_bitmap)
  3827. goto out;
  3828. /*
  3829. * Use second half of bitmap area because both HPT and radix
  3830. * accumulate bits in the first half.
  3831. */
  3832. n = kvm_dirty_bitmap_bytes(memslot);
  3833. buf = memslot->dirty_bitmap + n / sizeof(long);
  3834. memset(buf, 0, n);
  3835. if (kvm_is_radix(kvm))
  3836. r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
  3837. else
  3838. r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
  3839. if (r)
  3840. goto out;
  3841. /*
  3842. * We accumulate dirty bits in the first half of the
  3843. * memslot's dirty_bitmap area, for when pages are paged
  3844. * out or modified by the host directly. Pick up these
  3845. * bits and add them to the map.
  3846. */
  3847. p = memslot->dirty_bitmap;
  3848. for (i = 0; i < n / sizeof(long); ++i)
  3849. buf[i] |= xchg(&p[i], 0);
  3850. /* Harvest dirty bits from VPA and DTL updates */
  3851. /* Note: we never modify the SLB shadow buffer areas */
  3852. kvm_for_each_vcpu(i, vcpu, kvm) {
  3853. spin_lock(&vcpu->arch.vpa_update_lock);
  3854. kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
  3855. kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
  3856. spin_unlock(&vcpu->arch.vpa_update_lock);
  3857. }
  3858. r = -EFAULT;
  3859. if (copy_to_user(log->dirty_bitmap, buf, n))
  3860. goto out;
  3861. r = 0;
  3862. out:
  3863. mutex_unlock(&kvm->slots_lock);
  3864. return r;
  3865. }
  3866. static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
  3867. struct kvm_memory_slot *dont)
  3868. {
  3869. if (!dont || free->arch.rmap != dont->arch.rmap) {
  3870. vfree(free->arch.rmap);
  3871. free->arch.rmap = NULL;
  3872. }
  3873. }
  3874. static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
  3875. unsigned long npages)
  3876. {
  3877. slot->arch.rmap = vzalloc(array_size(npages, sizeof(*slot->arch.rmap)));
  3878. if (!slot->arch.rmap)
  3879. return -ENOMEM;
  3880. return 0;
  3881. }
  3882. static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
  3883. struct kvm_memory_slot *memslot,
  3884. const struct kvm_userspace_memory_region *mem)
  3885. {
  3886. return 0;
  3887. }
  3888. static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
  3889. const struct kvm_userspace_memory_region *mem,
  3890. const struct kvm_memory_slot *old,
  3891. const struct kvm_memory_slot *new)
  3892. {
  3893. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  3894. /*
  3895. * If we are making a new memslot, it might make
  3896. * some address that was previously cached as emulated
  3897. * MMIO be no longer emulated MMIO, so invalidate
  3898. * all the caches of emulated MMIO translations.
  3899. */
  3900. if (npages)
  3901. atomic64_inc(&kvm->arch.mmio_update);
  3902. }
  3903. /*
  3904. * Update LPCR values in kvm->arch and in vcores.
  3905. * Caller must hold kvm->lock.
  3906. */
  3907. void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
  3908. {
  3909. long int i;
  3910. u32 cores_done = 0;
  3911. if ((kvm->arch.lpcr & mask) == lpcr)
  3912. return;
  3913. kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
  3914. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  3915. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  3916. if (!vc)
  3917. continue;
  3918. spin_lock(&vc->lock);
  3919. vc->lpcr = (vc->lpcr & ~mask) | lpcr;
  3920. spin_unlock(&vc->lock);
  3921. if (++cores_done >= kvm->arch.online_vcores)
  3922. break;
  3923. }
  3924. }
  3925. static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
  3926. {
  3927. return;
  3928. }
  3929. void kvmppc_setup_partition_table(struct kvm *kvm)
  3930. {
  3931. unsigned long dw0, dw1;
  3932. if (!kvm_is_radix(kvm)) {
  3933. /* PS field - page size for VRMA */
  3934. dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
  3935. ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
  3936. /* HTABSIZE and HTABORG fields */
  3937. dw0 |= kvm->arch.sdr1;
  3938. /* Second dword as set by userspace */
  3939. dw1 = kvm->arch.process_table;
  3940. } else {
  3941. dw0 = PATB_HR | radix__get_tree_size() |
  3942. __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
  3943. dw1 = PATB_GR | kvm->arch.process_table;
  3944. }
  3945. kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1);
  3946. }
  3947. /*
  3948. * Set up HPT (hashed page table) and RMA (real-mode area).
  3949. * Must be called with kvm->lock held.
  3950. */
  3951. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  3952. {
  3953. int err = 0;
  3954. struct kvm *kvm = vcpu->kvm;
  3955. unsigned long hva;
  3956. struct kvm_memory_slot *memslot;
  3957. struct vm_area_struct *vma;
  3958. unsigned long lpcr = 0, senc;
  3959. unsigned long psize, porder;
  3960. int srcu_idx;
  3961. /* Allocate hashed page table (if not done already) and reset it */
  3962. if (!kvm->arch.hpt.virt) {
  3963. int order = KVM_DEFAULT_HPT_ORDER;
  3964. struct kvm_hpt_info info;
  3965. err = kvmppc_allocate_hpt(&info, order);
  3966. /* If we get here, it means userspace didn't specify a
  3967. * size explicitly. So, try successively smaller
  3968. * sizes if the default failed. */
  3969. while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
  3970. err = kvmppc_allocate_hpt(&info, order);
  3971. if (err < 0) {
  3972. pr_err("KVM: Couldn't alloc HPT\n");
  3973. goto out;
  3974. }
  3975. kvmppc_set_hpt(kvm, &info);
  3976. }
  3977. /* Look up the memslot for guest physical address 0 */
  3978. srcu_idx = srcu_read_lock(&kvm->srcu);
  3979. memslot = gfn_to_memslot(kvm, 0);
  3980. /* We must have some memory at 0 by now */
  3981. err = -EINVAL;
  3982. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  3983. goto out_srcu;
  3984. /* Look up the VMA for the start of this memory slot */
  3985. hva = memslot->userspace_addr;
  3986. down_read(&current->mm->mmap_sem);
  3987. vma = find_vma(current->mm, hva);
  3988. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  3989. goto up_out;
  3990. psize = vma_kernel_pagesize(vma);
  3991. up_read(&current->mm->mmap_sem);
  3992. /* We can handle 4k, 64k or 16M pages in the VRMA */
  3993. if (psize >= 0x1000000)
  3994. psize = 0x1000000;
  3995. else if (psize >= 0x10000)
  3996. psize = 0x10000;
  3997. else
  3998. psize = 0x1000;
  3999. porder = __ilog2(psize);
  4000. senc = slb_pgsize_encoding(psize);
  4001. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  4002. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  4003. /* Create HPTEs in the hash page table for the VRMA */
  4004. kvmppc_map_vrma(vcpu, memslot, porder);
  4005. /* Update VRMASD field in the LPCR */
  4006. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  4007. /* the -4 is to account for senc values starting at 0x10 */
  4008. lpcr = senc << (LPCR_VRMASD_SH - 4);
  4009. kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
  4010. }
  4011. /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
  4012. smp_wmb();
  4013. err = 0;
  4014. out_srcu:
  4015. srcu_read_unlock(&kvm->srcu, srcu_idx);
  4016. out:
  4017. return err;
  4018. up_out:
  4019. up_read(&current->mm->mmap_sem);
  4020. goto out_srcu;
  4021. }
  4022. /* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
  4023. int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
  4024. {
  4025. if (nesting_enabled(kvm))
  4026. kvmhv_release_all_nested(kvm);
  4027. kvmppc_free_radix(kvm);
  4028. kvmppc_update_lpcr(kvm, LPCR_VPM1,
  4029. LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
  4030. kvmppc_rmap_reset(kvm);
  4031. kvm->arch.radix = 0;
  4032. kvm->arch.process_table = 0;
  4033. return 0;
  4034. }
  4035. /* Must be called with kvm->lock held and mmu_ready = 0 and no vcpus running */
  4036. int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
  4037. {
  4038. int err;
  4039. err = kvmppc_init_vm_radix(kvm);
  4040. if (err)
  4041. return err;
  4042. kvmppc_free_hpt(&kvm->arch.hpt);
  4043. kvmppc_update_lpcr(kvm, LPCR_UPRT | LPCR_GTSE | LPCR_HR,
  4044. LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
  4045. kvmppc_rmap_reset(kvm);
  4046. kvm->arch.radix = 1;
  4047. return 0;
  4048. }
  4049. #ifdef CONFIG_KVM_XICS
  4050. /*
  4051. * Allocate a per-core structure for managing state about which cores are
  4052. * running in the host versus the guest and for exchanging data between
  4053. * real mode KVM and CPU running in the host.
  4054. * This is only done for the first VM.
  4055. * The allocated structure stays even if all VMs have stopped.
  4056. * It is only freed when the kvm-hv module is unloaded.
  4057. * It's OK for this routine to fail, we just don't support host
  4058. * core operations like redirecting H_IPI wakeups.
  4059. */
  4060. void kvmppc_alloc_host_rm_ops(void)
  4061. {
  4062. struct kvmppc_host_rm_ops *ops;
  4063. unsigned long l_ops;
  4064. int cpu, core;
  4065. int size;
  4066. /* Not the first time here ? */
  4067. if (kvmppc_host_rm_ops_hv != NULL)
  4068. return;
  4069. ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
  4070. if (!ops)
  4071. return;
  4072. size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
  4073. ops->rm_core = kzalloc(size, GFP_KERNEL);
  4074. if (!ops->rm_core) {
  4075. kfree(ops);
  4076. return;
  4077. }
  4078. cpus_read_lock();
  4079. for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
  4080. if (!cpu_online(cpu))
  4081. continue;
  4082. core = cpu >> threads_shift;
  4083. ops->rm_core[core].rm_state.in_host = 1;
  4084. }
  4085. ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
  4086. /*
  4087. * Make the contents of the kvmppc_host_rm_ops structure visible
  4088. * to other CPUs before we assign it to the global variable.
  4089. * Do an atomic assignment (no locks used here), but if someone
  4090. * beats us to it, just free our copy and return.
  4091. */
  4092. smp_wmb();
  4093. l_ops = (unsigned long) ops;
  4094. if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
  4095. cpus_read_unlock();
  4096. kfree(ops->rm_core);
  4097. kfree(ops);
  4098. return;
  4099. }
  4100. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
  4101. "ppc/kvm_book3s:prepare",
  4102. kvmppc_set_host_core,
  4103. kvmppc_clear_host_core);
  4104. cpus_read_unlock();
  4105. }
  4106. void kvmppc_free_host_rm_ops(void)
  4107. {
  4108. if (kvmppc_host_rm_ops_hv) {
  4109. cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
  4110. kfree(kvmppc_host_rm_ops_hv->rm_core);
  4111. kfree(kvmppc_host_rm_ops_hv);
  4112. kvmppc_host_rm_ops_hv = NULL;
  4113. }
  4114. }
  4115. #endif
  4116. static int kvmppc_core_init_vm_hv(struct kvm *kvm)
  4117. {
  4118. unsigned long lpcr, lpid;
  4119. char buf[32];
  4120. int ret;
  4121. /* Allocate the guest's logical partition ID */
  4122. lpid = kvmppc_alloc_lpid();
  4123. if ((long)lpid < 0)
  4124. return -ENOMEM;
  4125. kvm->arch.lpid = lpid;
  4126. kvmppc_alloc_host_rm_ops();
  4127. kvmhv_vm_nested_init(kvm);
  4128. /*
  4129. * Since we don't flush the TLB when tearing down a VM,
  4130. * and this lpid might have previously been used,
  4131. * make sure we flush on each core before running the new VM.
  4132. * On POWER9, the tlbie in mmu_partition_table_set_entry()
  4133. * does this flush for us.
  4134. */
  4135. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  4136. cpumask_setall(&kvm->arch.need_tlb_flush);
  4137. /* Start out with the default set of hcalls enabled */
  4138. memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
  4139. sizeof(kvm->arch.enabled_hcalls));
  4140. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  4141. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  4142. /* Init LPCR for virtual RMA mode */
  4143. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  4144. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  4145. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  4146. lpcr &= LPCR_PECE | LPCR_LPES;
  4147. } else {
  4148. lpcr = 0;
  4149. }
  4150. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  4151. LPCR_VPM0 | LPCR_VPM1;
  4152. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  4153. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  4154. /* On POWER8 turn on online bit to enable PURR/SPURR */
  4155. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  4156. lpcr |= LPCR_ONL;
  4157. /*
  4158. * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
  4159. * Set HVICE bit to enable hypervisor virtualization interrupts.
  4160. * Set HEIC to prevent OS interrupts to go to hypervisor (should
  4161. * be unnecessary but better safe than sorry in case we re-enable
  4162. * EE in HV mode with this LPCR still set)
  4163. */
  4164. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  4165. lpcr &= ~LPCR_VPM0;
  4166. lpcr |= LPCR_HVICE | LPCR_HEIC;
  4167. /*
  4168. * If xive is enabled, we route 0x500 interrupts directly
  4169. * to the guest.
  4170. */
  4171. if (xive_enabled())
  4172. lpcr |= LPCR_LPES;
  4173. }
  4174. /*
  4175. * If the host uses radix, the guest starts out as radix.
  4176. */
  4177. if (radix_enabled()) {
  4178. kvm->arch.radix = 1;
  4179. kvm->arch.mmu_ready = 1;
  4180. lpcr &= ~LPCR_VPM1;
  4181. lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
  4182. ret = kvmppc_init_vm_radix(kvm);
  4183. if (ret) {
  4184. kvmppc_free_lpid(kvm->arch.lpid);
  4185. return ret;
  4186. }
  4187. kvmppc_setup_partition_table(kvm);
  4188. }
  4189. kvm->arch.lpcr = lpcr;
  4190. /* Initialization for future HPT resizes */
  4191. kvm->arch.resize_hpt = NULL;
  4192. /*
  4193. * Work out how many sets the TLB has, for the use of
  4194. * the TLB invalidation loop in book3s_hv_rmhandlers.S.
  4195. */
  4196. if (radix_enabled())
  4197. kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
  4198. else if (cpu_has_feature(CPU_FTR_ARCH_300))
  4199. kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
  4200. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  4201. kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
  4202. else
  4203. kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
  4204. /*
  4205. * Track that we now have a HV mode VM active. This blocks secondary
  4206. * CPU threads from coming online.
  4207. * On POWER9, we only need to do this if the "indep_threads_mode"
  4208. * module parameter has been set to N.
  4209. */
  4210. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  4211. if (!indep_threads_mode && !cpu_has_feature(CPU_FTR_HVMODE)) {
  4212. pr_warn("KVM: Ignoring indep_threads_mode=N in nested hypervisor\n");
  4213. kvm->arch.threads_indep = true;
  4214. } else {
  4215. kvm->arch.threads_indep = indep_threads_mode;
  4216. }
  4217. }
  4218. if (!kvm->arch.threads_indep)
  4219. kvm_hv_vm_activated();
  4220. /*
  4221. * Initialize smt_mode depending on processor.
  4222. * POWER8 and earlier have to use "strict" threading, where
  4223. * all vCPUs in a vcore have to run on the same (sub)core,
  4224. * whereas on POWER9 the threads can each run a different
  4225. * guest.
  4226. */
  4227. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  4228. kvm->arch.smt_mode = threads_per_subcore;
  4229. else
  4230. kvm->arch.smt_mode = 1;
  4231. kvm->arch.emul_smt_mode = 1;
  4232. /*
  4233. * Create a debugfs directory for the VM
  4234. */
  4235. snprintf(buf, sizeof(buf), "vm%d", current->pid);
  4236. kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
  4237. kvmppc_mmu_debugfs_init(kvm);
  4238. if (radix_enabled())
  4239. kvmhv_radix_debugfs_init(kvm);
  4240. return 0;
  4241. }
  4242. static void kvmppc_free_vcores(struct kvm *kvm)
  4243. {
  4244. long int i;
  4245. for (i = 0; i < KVM_MAX_VCORES; ++i)
  4246. kfree(kvm->arch.vcores[i]);
  4247. kvm->arch.online_vcores = 0;
  4248. }
  4249. static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
  4250. {
  4251. debugfs_remove_recursive(kvm->arch.debugfs_dir);
  4252. if (!kvm->arch.threads_indep)
  4253. kvm_hv_vm_deactivated();
  4254. kvmppc_free_vcores(kvm);
  4255. if (kvm_is_radix(kvm))
  4256. kvmppc_free_radix(kvm);
  4257. else
  4258. kvmppc_free_hpt(&kvm->arch.hpt);
  4259. /* Perform global invalidation and return lpid to the pool */
  4260. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  4261. if (nesting_enabled(kvm))
  4262. kvmhv_release_all_nested(kvm);
  4263. kvm->arch.process_table = 0;
  4264. kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
  4265. }
  4266. kvmppc_free_lpid(kvm->arch.lpid);
  4267. kvmppc_free_pimap(kvm);
  4268. }
  4269. /* We don't need to emulate any privileged instructions or dcbz */
  4270. static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  4271. unsigned int inst, int *advance)
  4272. {
  4273. return EMULATE_FAIL;
  4274. }
  4275. static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
  4276. ulong spr_val)
  4277. {
  4278. return EMULATE_FAIL;
  4279. }
  4280. static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
  4281. ulong *spr_val)
  4282. {
  4283. return EMULATE_FAIL;
  4284. }
  4285. static int kvmppc_core_check_processor_compat_hv(void)
  4286. {
  4287. if (cpu_has_feature(CPU_FTR_HVMODE) &&
  4288. cpu_has_feature(CPU_FTR_ARCH_206))
  4289. return 0;
  4290. /* POWER9 in radix mode is capable of being a nested hypervisor. */
  4291. if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
  4292. return 0;
  4293. return -EIO;
  4294. }
  4295. #ifdef CONFIG_KVM_XICS
  4296. void kvmppc_free_pimap(struct kvm *kvm)
  4297. {
  4298. kfree(kvm->arch.pimap);
  4299. }
  4300. static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
  4301. {
  4302. return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
  4303. }
  4304. static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  4305. {
  4306. struct irq_desc *desc;
  4307. struct kvmppc_irq_map *irq_map;
  4308. struct kvmppc_passthru_irqmap *pimap;
  4309. struct irq_chip *chip;
  4310. int i, rc = 0;
  4311. if (!kvm_irq_bypass)
  4312. return 1;
  4313. desc = irq_to_desc(host_irq);
  4314. if (!desc)
  4315. return -EIO;
  4316. mutex_lock(&kvm->lock);
  4317. pimap = kvm->arch.pimap;
  4318. if (pimap == NULL) {
  4319. /* First call, allocate structure to hold IRQ map */
  4320. pimap = kvmppc_alloc_pimap();
  4321. if (pimap == NULL) {
  4322. mutex_unlock(&kvm->lock);
  4323. return -ENOMEM;
  4324. }
  4325. kvm->arch.pimap = pimap;
  4326. }
  4327. /*
  4328. * For now, we only support interrupts for which the EOI operation
  4329. * is an OPAL call followed by a write to XIRR, since that's
  4330. * what our real-mode EOI code does, or a XIVE interrupt
  4331. */
  4332. chip = irq_data_get_irq_chip(&desc->irq_data);
  4333. if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) {
  4334. pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
  4335. host_irq, guest_gsi);
  4336. mutex_unlock(&kvm->lock);
  4337. return -ENOENT;
  4338. }
  4339. /*
  4340. * See if we already have an entry for this guest IRQ number.
  4341. * If it's mapped to a hardware IRQ number, that's an error,
  4342. * otherwise re-use this entry.
  4343. */
  4344. for (i = 0; i < pimap->n_mapped; i++) {
  4345. if (guest_gsi == pimap->mapped[i].v_hwirq) {
  4346. if (pimap->mapped[i].r_hwirq) {
  4347. mutex_unlock(&kvm->lock);
  4348. return -EINVAL;
  4349. }
  4350. break;
  4351. }
  4352. }
  4353. if (i == KVMPPC_PIRQ_MAPPED) {
  4354. mutex_unlock(&kvm->lock);
  4355. return -EAGAIN; /* table is full */
  4356. }
  4357. irq_map = &pimap->mapped[i];
  4358. irq_map->v_hwirq = guest_gsi;
  4359. irq_map->desc = desc;
  4360. /*
  4361. * Order the above two stores before the next to serialize with
  4362. * the KVM real mode handler.
  4363. */
  4364. smp_wmb();
  4365. irq_map->r_hwirq = desc->irq_data.hwirq;
  4366. if (i == pimap->n_mapped)
  4367. pimap->n_mapped++;
  4368. if (xive_enabled())
  4369. rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc);
  4370. else
  4371. kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
  4372. if (rc)
  4373. irq_map->r_hwirq = 0;
  4374. mutex_unlock(&kvm->lock);
  4375. return 0;
  4376. }
  4377. static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  4378. {
  4379. struct irq_desc *desc;
  4380. struct kvmppc_passthru_irqmap *pimap;
  4381. int i, rc = 0;
  4382. if (!kvm_irq_bypass)
  4383. return 0;
  4384. desc = irq_to_desc(host_irq);
  4385. if (!desc)
  4386. return -EIO;
  4387. mutex_lock(&kvm->lock);
  4388. if (!kvm->arch.pimap)
  4389. goto unlock;
  4390. pimap = kvm->arch.pimap;
  4391. for (i = 0; i < pimap->n_mapped; i++) {
  4392. if (guest_gsi == pimap->mapped[i].v_hwirq)
  4393. break;
  4394. }
  4395. if (i == pimap->n_mapped) {
  4396. mutex_unlock(&kvm->lock);
  4397. return -ENODEV;
  4398. }
  4399. if (xive_enabled())
  4400. rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc);
  4401. else
  4402. kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
  4403. /* invalidate the entry (what do do on error from the above ?) */
  4404. pimap->mapped[i].r_hwirq = 0;
  4405. /*
  4406. * We don't free this structure even when the count goes to
  4407. * zero. The structure is freed when we destroy the VM.
  4408. */
  4409. unlock:
  4410. mutex_unlock(&kvm->lock);
  4411. return rc;
  4412. }
  4413. static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
  4414. struct irq_bypass_producer *prod)
  4415. {
  4416. int ret = 0;
  4417. struct kvm_kernel_irqfd *irqfd =
  4418. container_of(cons, struct kvm_kernel_irqfd, consumer);
  4419. irqfd->producer = prod;
  4420. ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  4421. if (ret)
  4422. pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
  4423. prod->irq, irqfd->gsi, ret);
  4424. return ret;
  4425. }
  4426. static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
  4427. struct irq_bypass_producer *prod)
  4428. {
  4429. int ret;
  4430. struct kvm_kernel_irqfd *irqfd =
  4431. container_of(cons, struct kvm_kernel_irqfd, consumer);
  4432. irqfd->producer = NULL;
  4433. /*
  4434. * When producer of consumer is unregistered, we change back to
  4435. * default external interrupt handling mode - KVM real mode
  4436. * will switch back to host.
  4437. */
  4438. ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  4439. if (ret)
  4440. pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
  4441. prod->irq, irqfd->gsi, ret);
  4442. }
  4443. #endif
  4444. static long kvm_arch_vm_ioctl_hv(struct file *filp,
  4445. unsigned int ioctl, unsigned long arg)
  4446. {
  4447. struct kvm *kvm __maybe_unused = filp->private_data;
  4448. void __user *argp = (void __user *)arg;
  4449. long r;
  4450. switch (ioctl) {
  4451. case KVM_PPC_ALLOCATE_HTAB: {
  4452. u32 htab_order;
  4453. r = -EFAULT;
  4454. if (get_user(htab_order, (u32 __user *)argp))
  4455. break;
  4456. r = kvmppc_alloc_reset_hpt(kvm, htab_order);
  4457. if (r)
  4458. break;
  4459. r = 0;
  4460. break;
  4461. }
  4462. case KVM_PPC_GET_HTAB_FD: {
  4463. struct kvm_get_htab_fd ghf;
  4464. r = -EFAULT;
  4465. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  4466. break;
  4467. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  4468. break;
  4469. }
  4470. case KVM_PPC_RESIZE_HPT_PREPARE: {
  4471. struct kvm_ppc_resize_hpt rhpt;
  4472. r = -EFAULT;
  4473. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  4474. break;
  4475. r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
  4476. break;
  4477. }
  4478. case KVM_PPC_RESIZE_HPT_COMMIT: {
  4479. struct kvm_ppc_resize_hpt rhpt;
  4480. r = -EFAULT;
  4481. if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
  4482. break;
  4483. r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
  4484. break;
  4485. }
  4486. default:
  4487. r = -ENOTTY;
  4488. }
  4489. return r;
  4490. }
  4491. /*
  4492. * List of hcall numbers to enable by default.
  4493. * For compatibility with old userspace, we enable by default
  4494. * all hcalls that were implemented before the hcall-enabling
  4495. * facility was added. Note this list should not include H_RTAS.
  4496. */
  4497. static unsigned int default_hcall_list[] = {
  4498. H_REMOVE,
  4499. H_ENTER,
  4500. H_READ,
  4501. H_PROTECT,
  4502. H_BULK_REMOVE,
  4503. H_GET_TCE,
  4504. H_PUT_TCE,
  4505. H_SET_DABR,
  4506. H_SET_XDABR,
  4507. H_CEDE,
  4508. H_PROD,
  4509. H_CONFER,
  4510. H_REGISTER_VPA,
  4511. #ifdef CONFIG_KVM_XICS
  4512. H_EOI,
  4513. H_CPPR,
  4514. H_IPI,
  4515. H_IPOLL,
  4516. H_XIRR,
  4517. H_XIRR_X,
  4518. #endif
  4519. 0
  4520. };
  4521. static void init_default_hcalls(void)
  4522. {
  4523. int i;
  4524. unsigned int hcall;
  4525. for (i = 0; default_hcall_list[i]; ++i) {
  4526. hcall = default_hcall_list[i];
  4527. WARN_ON(!kvmppc_hcall_impl_hv(hcall));
  4528. __set_bit(hcall / 4, default_enabled_hcalls);
  4529. }
  4530. }
  4531. static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
  4532. {
  4533. unsigned long lpcr;
  4534. int radix;
  4535. int err;
  4536. /* If not on a POWER9, reject it */
  4537. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  4538. return -ENODEV;
  4539. /* If any unknown flags set, reject it */
  4540. if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
  4541. return -EINVAL;
  4542. /* GR (guest radix) bit in process_table field must match */
  4543. radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
  4544. if (!!(cfg->process_table & PATB_GR) != radix)
  4545. return -EINVAL;
  4546. /* Process table size field must be reasonable, i.e. <= 24 */
  4547. if ((cfg->process_table & PRTS_MASK) > 24)
  4548. return -EINVAL;
  4549. /* We can change a guest to/from radix now, if the host is radix */
  4550. if (radix && !radix_enabled())
  4551. return -EINVAL;
  4552. /* If we're a nested hypervisor, we currently only support radix */
  4553. if (kvmhv_on_pseries() && !radix)
  4554. return -EINVAL;
  4555. mutex_lock(&kvm->lock);
  4556. if (radix != kvm_is_radix(kvm)) {
  4557. if (kvm->arch.mmu_ready) {
  4558. kvm->arch.mmu_ready = 0;
  4559. /* order mmu_ready vs. vcpus_running */
  4560. smp_mb();
  4561. if (atomic_read(&kvm->arch.vcpus_running)) {
  4562. kvm->arch.mmu_ready = 1;
  4563. err = -EBUSY;
  4564. goto out_unlock;
  4565. }
  4566. }
  4567. if (radix)
  4568. err = kvmppc_switch_mmu_to_radix(kvm);
  4569. else
  4570. err = kvmppc_switch_mmu_to_hpt(kvm);
  4571. if (err)
  4572. goto out_unlock;
  4573. }
  4574. kvm->arch.process_table = cfg->process_table;
  4575. kvmppc_setup_partition_table(kvm);
  4576. lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
  4577. kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
  4578. err = 0;
  4579. out_unlock:
  4580. mutex_unlock(&kvm->lock);
  4581. return err;
  4582. }
  4583. static int kvmhv_enable_nested(struct kvm *kvm)
  4584. {
  4585. if (!nested)
  4586. return -EPERM;
  4587. if (!cpu_has_feature(CPU_FTR_ARCH_300) || no_mixing_hpt_and_radix)
  4588. return -ENODEV;
  4589. /* kvm == NULL means the caller is testing if the capability exists */
  4590. if (kvm)
  4591. kvm->arch.nested_enable = true;
  4592. return 0;
  4593. }
  4594. static struct kvmppc_ops kvm_ops_hv = {
  4595. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
  4596. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
  4597. .get_one_reg = kvmppc_get_one_reg_hv,
  4598. .set_one_reg = kvmppc_set_one_reg_hv,
  4599. .vcpu_load = kvmppc_core_vcpu_load_hv,
  4600. .vcpu_put = kvmppc_core_vcpu_put_hv,
  4601. .set_msr = kvmppc_set_msr_hv,
  4602. .vcpu_run = kvmppc_vcpu_run_hv,
  4603. .vcpu_create = kvmppc_core_vcpu_create_hv,
  4604. .vcpu_free = kvmppc_core_vcpu_free_hv,
  4605. .check_requests = kvmppc_core_check_requests_hv,
  4606. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
  4607. .flush_memslot = kvmppc_core_flush_memslot_hv,
  4608. .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
  4609. .commit_memory_region = kvmppc_core_commit_memory_region_hv,
  4610. .unmap_hva_range = kvm_unmap_hva_range_hv,
  4611. .age_hva = kvm_age_hva_hv,
  4612. .test_age_hva = kvm_test_age_hva_hv,
  4613. .set_spte_hva = kvm_set_spte_hva_hv,
  4614. .mmu_destroy = kvmppc_mmu_destroy_hv,
  4615. .free_memslot = kvmppc_core_free_memslot_hv,
  4616. .create_memslot = kvmppc_core_create_memslot_hv,
  4617. .init_vm = kvmppc_core_init_vm_hv,
  4618. .destroy_vm = kvmppc_core_destroy_vm_hv,
  4619. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
  4620. .emulate_op = kvmppc_core_emulate_op_hv,
  4621. .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
  4622. .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
  4623. .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
  4624. .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
  4625. .hcall_implemented = kvmppc_hcall_impl_hv,
  4626. #ifdef CONFIG_KVM_XICS
  4627. .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
  4628. .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
  4629. #endif
  4630. .configure_mmu = kvmhv_configure_mmu,
  4631. .get_rmmu_info = kvmhv_get_rmmu_info,
  4632. .set_smt_mode = kvmhv_set_smt_mode,
  4633. .enable_nested = kvmhv_enable_nested,
  4634. };
  4635. static int kvm_init_subcore_bitmap(void)
  4636. {
  4637. int i, j;
  4638. int nr_cores = cpu_nr_cores();
  4639. struct sibling_subcore_state *sibling_subcore_state;
  4640. for (i = 0; i < nr_cores; i++) {
  4641. int first_cpu = i * threads_per_core;
  4642. int node = cpu_to_node(first_cpu);
  4643. /* Ignore if it is already allocated. */
  4644. if (paca_ptrs[first_cpu]->sibling_subcore_state)
  4645. continue;
  4646. sibling_subcore_state =
  4647. kmalloc_node(sizeof(struct sibling_subcore_state),
  4648. GFP_KERNEL, node);
  4649. if (!sibling_subcore_state)
  4650. return -ENOMEM;
  4651. memset(sibling_subcore_state, 0,
  4652. sizeof(struct sibling_subcore_state));
  4653. for (j = 0; j < threads_per_core; j++) {
  4654. int cpu = first_cpu + j;
  4655. paca_ptrs[cpu]->sibling_subcore_state =
  4656. sibling_subcore_state;
  4657. }
  4658. }
  4659. return 0;
  4660. }
  4661. static int kvmppc_radix_possible(void)
  4662. {
  4663. return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
  4664. }
  4665. static int kvmppc_book3s_init_hv(void)
  4666. {
  4667. int r;
  4668. /*
  4669. * FIXME!! Do we need to check on all cpus ?
  4670. */
  4671. r = kvmppc_core_check_processor_compat_hv();
  4672. if (r < 0)
  4673. return -ENODEV;
  4674. r = kvmhv_nested_init();
  4675. if (r)
  4676. return r;
  4677. r = kvm_init_subcore_bitmap();
  4678. if (r)
  4679. return r;
  4680. /*
  4681. * We need a way of accessing the XICS interrupt controller,
  4682. * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
  4683. * indirectly, via OPAL.
  4684. */
  4685. #ifdef CONFIG_SMP
  4686. if (!xive_enabled() && !kvmhv_on_pseries() &&
  4687. !local_paca->kvm_hstate.xics_phys) {
  4688. struct device_node *np;
  4689. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  4690. if (!np) {
  4691. pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
  4692. return -ENODEV;
  4693. }
  4694. /* presence of intc confirmed - node can be dropped again */
  4695. of_node_put(np);
  4696. }
  4697. #endif
  4698. kvm_ops_hv.owner = THIS_MODULE;
  4699. kvmppc_hv_ops = &kvm_ops_hv;
  4700. init_default_hcalls();
  4701. init_vcore_lists();
  4702. r = kvmppc_mmu_hv_init();
  4703. if (r)
  4704. return r;
  4705. if (kvmppc_radix_possible())
  4706. r = kvmppc_radix_init();
  4707. /*
  4708. * POWER9 chips before version 2.02 can't have some threads in
  4709. * HPT mode and some in radix mode on the same core.
  4710. */
  4711. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  4712. unsigned int pvr = mfspr(SPRN_PVR);
  4713. if ((pvr >> 16) == PVR_POWER9 &&
  4714. (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
  4715. ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
  4716. no_mixing_hpt_and_radix = true;
  4717. }
  4718. return r;
  4719. }
  4720. static void kvmppc_book3s_exit_hv(void)
  4721. {
  4722. kvmppc_free_host_rm_ops();
  4723. if (kvmppc_radix_possible())
  4724. kvmppc_radix_exit();
  4725. kvmppc_hv_ops = NULL;
  4726. kvmhv_nested_exit();
  4727. }
  4728. module_init(kvmppc_book3s_init_hv);
  4729. module_exit(kvmppc_book3s_exit_hv);
  4730. MODULE_LICENSE("GPL");
  4731. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  4732. MODULE_ALIAS("devname:kvm");