book3s_hv.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784
  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/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/preempt.h>
  24. #include <linux/sched.h>
  25. #include <linux/delay.h>
  26. #include <linux/export.h>
  27. #include <linux/fs.h>
  28. #include <linux/anon_inodes.h>
  29. #include <linux/cpu.h>
  30. #include <linux/cpumask.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/page-flags.h>
  33. #include <linux/srcu.h>
  34. #include <linux/miscdevice.h>
  35. #include <linux/debugfs.h>
  36. #include <asm/reg.h>
  37. #include <asm/cputable.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/tlbflush.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/io.h>
  42. #include <asm/kvm_ppc.h>
  43. #include <asm/kvm_book3s.h>
  44. #include <asm/mmu_context.h>
  45. #include <asm/lppaca.h>
  46. #include <asm/processor.h>
  47. #include <asm/cputhreads.h>
  48. #include <asm/page.h>
  49. #include <asm/hvcall.h>
  50. #include <asm/switch_to.h>
  51. #include <asm/smp.h>
  52. #include <asm/dbell.h>
  53. #include <asm/hmi.h>
  54. #include <asm/pnv-pci.h>
  55. #include <asm/mmu.h>
  56. #include <asm/opal.h>
  57. #include <asm/xics.h>
  58. #include <linux/gfp.h>
  59. #include <linux/vmalloc.h>
  60. #include <linux/highmem.h>
  61. #include <linux/hugetlb.h>
  62. #include <linux/kvm_irqfd.h>
  63. #include <linux/irqbypass.h>
  64. #include <linux/module.h>
  65. #include <linux/compiler.h>
  66. #include <linux/of.h>
  67. #include "book3s.h"
  68. #define CREATE_TRACE_POINTS
  69. #include "trace_hv.h"
  70. /* #define EXIT_DEBUG */
  71. /* #define EXIT_DEBUG_SIMPLE */
  72. /* #define EXIT_DEBUG_INT */
  73. /* Used to indicate that a guest page fault needs to be handled */
  74. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  75. /* Used to indicate that a guest passthrough interrupt needs to be handled */
  76. #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
  77. /* Used as a "null" value for timebase values */
  78. #define TB_NIL (~(u64)0)
  79. static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
  80. static int dynamic_mt_modes = 6;
  81. module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
  82. MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
  83. static int target_smt_mode;
  84. module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
  85. MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
  86. #ifdef CONFIG_KVM_XICS
  87. static struct kernel_param_ops module_param_ops = {
  88. .set = param_set_int,
  89. .get = param_get_int,
  90. };
  91. module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass,
  92. S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
  94. module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
  95. S_IRUGO | S_IWUSR);
  96. MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
  97. #endif
  98. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  99. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  100. static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
  101. int *ip)
  102. {
  103. int i = *ip;
  104. struct kvm_vcpu *vcpu;
  105. while (++i < MAX_SMT_THREADS) {
  106. vcpu = READ_ONCE(vc->runnable_threads[i]);
  107. if (vcpu) {
  108. *ip = i;
  109. return vcpu;
  110. }
  111. }
  112. return NULL;
  113. }
  114. /* Used to traverse the list of runnable threads for a given vcore */
  115. #define for_each_runnable_thread(i, vcpu, vc) \
  116. for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
  117. static bool kvmppc_ipi_thread(int cpu)
  118. {
  119. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  120. /* On POWER9 we can use msgsnd to IPI any cpu */
  121. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  122. msg |= get_hard_smp_processor_id(cpu);
  123. smp_mb();
  124. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  125. return true;
  126. }
  127. /* On POWER8 for IPIs to threads in the same core, use msgsnd */
  128. if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
  129. preempt_disable();
  130. if (cpu_first_thread_sibling(cpu) ==
  131. cpu_first_thread_sibling(smp_processor_id())) {
  132. msg |= cpu_thread_in_core(cpu);
  133. smp_mb();
  134. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  135. preempt_enable();
  136. return true;
  137. }
  138. preempt_enable();
  139. }
  140. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  141. if (cpu >= 0 && cpu < nr_cpu_ids) {
  142. if (paca[cpu].kvm_hstate.xics_phys) {
  143. xics_wake_cpu(cpu);
  144. return true;
  145. }
  146. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
  147. return true;
  148. }
  149. #endif
  150. return false;
  151. }
  152. static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
  153. {
  154. int cpu;
  155. struct swait_queue_head *wqp;
  156. wqp = kvm_arch_vcpu_wq(vcpu);
  157. if (swait_active(wqp)) {
  158. swake_up(wqp);
  159. ++vcpu->stat.halt_wakeup;
  160. }
  161. if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
  162. return;
  163. /* CPU points to the first thread of the core */
  164. cpu = vcpu->cpu;
  165. if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
  166. smp_send_reschedule(cpu);
  167. }
  168. /*
  169. * We use the vcpu_load/put functions to measure stolen time.
  170. * Stolen time is counted as time when either the vcpu is able to
  171. * run as part of a virtual core, but the task running the vcore
  172. * is preempted or sleeping, or when the vcpu needs something done
  173. * in the kernel by the task running the vcpu, but that task is
  174. * preempted or sleeping. Those two things have to be counted
  175. * separately, since one of the vcpu tasks will take on the job
  176. * of running the core, and the other vcpu tasks in the vcore will
  177. * sleep waiting for it to do that, but that sleep shouldn't count
  178. * as stolen time.
  179. *
  180. * Hence we accumulate stolen time when the vcpu can run as part of
  181. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  182. * needs its task to do other things in the kernel (for example,
  183. * service a page fault) in busy_stolen. We don't accumulate
  184. * stolen time for a vcore when it is inactive, or for a vcpu
  185. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  186. * a misnomer; it means that the vcpu task is not executing in
  187. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  188. * the kernel. We don't have any way of dividing up that time
  189. * between time that the vcpu is genuinely stopped, time that
  190. * the task is actively working on behalf of the vcpu, and time
  191. * that the task is preempted, so we don't count any of it as
  192. * stolen.
  193. *
  194. * Updates to busy_stolen are protected by arch.tbacct_lock;
  195. * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
  196. * lock. The stolen times are measured in units of timebase ticks.
  197. * (Note that the != TB_NIL checks below are purely defensive;
  198. * they should never fail.)
  199. */
  200. static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
  201. {
  202. unsigned long flags;
  203. spin_lock_irqsave(&vc->stoltb_lock, flags);
  204. vc->preempt_tb = mftb();
  205. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  206. }
  207. static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
  208. {
  209. unsigned long flags;
  210. spin_lock_irqsave(&vc->stoltb_lock, flags);
  211. if (vc->preempt_tb != TB_NIL) {
  212. vc->stolen_tb += mftb() - vc->preempt_tb;
  213. vc->preempt_tb = TB_NIL;
  214. }
  215. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  216. }
  217. static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
  218. {
  219. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  220. unsigned long flags;
  221. /*
  222. * We can test vc->runner without taking the vcore lock,
  223. * because only this task ever sets vc->runner to this
  224. * vcpu, and once it is set to this vcpu, only this task
  225. * ever sets it to NULL.
  226. */
  227. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  228. kvmppc_core_end_stolen(vc);
  229. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  230. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  231. vcpu->arch.busy_preempt != TB_NIL) {
  232. vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
  233. vcpu->arch.busy_preempt = TB_NIL;
  234. }
  235. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  236. }
  237. static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
  238. {
  239. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  240. unsigned long flags;
  241. if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
  242. kvmppc_core_start_stolen(vc);
  243. spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
  244. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  245. vcpu->arch.busy_preempt = mftb();
  246. spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
  247. }
  248. static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
  249. {
  250. /*
  251. * Check for illegal transactional state bit combination
  252. * and if we find it, force the TS field to a safe state.
  253. */
  254. if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
  255. msr &= ~MSR_TS_MASK;
  256. vcpu->arch.shregs.msr = msr;
  257. kvmppc_end_cede(vcpu);
  258. }
  259. static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
  260. {
  261. vcpu->arch.pvr = pvr;
  262. }
  263. /* Dummy value used in computing PCR value below */
  264. #define PCR_ARCH_300 (PCR_ARCH_207 << 1)
  265. static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
  266. {
  267. unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
  268. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  269. /* We can (emulate) our own architecture version and anything older */
  270. if (cpu_has_feature(CPU_FTR_ARCH_300))
  271. host_pcr_bit = PCR_ARCH_300;
  272. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  273. host_pcr_bit = PCR_ARCH_207;
  274. else if (cpu_has_feature(CPU_FTR_ARCH_206))
  275. host_pcr_bit = PCR_ARCH_206;
  276. else
  277. host_pcr_bit = PCR_ARCH_205;
  278. /* Determine lowest PCR bit needed to run guest in given PVR level */
  279. guest_pcr_bit = host_pcr_bit;
  280. if (arch_compat) {
  281. switch (arch_compat) {
  282. case PVR_ARCH_205:
  283. guest_pcr_bit = PCR_ARCH_205;
  284. break;
  285. case PVR_ARCH_206:
  286. case PVR_ARCH_206p:
  287. guest_pcr_bit = PCR_ARCH_206;
  288. break;
  289. case PVR_ARCH_207:
  290. guest_pcr_bit = PCR_ARCH_207;
  291. break;
  292. case PVR_ARCH_300:
  293. guest_pcr_bit = PCR_ARCH_300;
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. }
  299. /* Check requested PCR bits don't exceed our capabilities */
  300. if (guest_pcr_bit > host_pcr_bit)
  301. return -EINVAL;
  302. spin_lock(&vc->lock);
  303. vc->arch_compat = arch_compat;
  304. /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */
  305. vc->pcr = host_pcr_bit - guest_pcr_bit;
  306. spin_unlock(&vc->lock);
  307. return 0;
  308. }
  309. static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  310. {
  311. int r;
  312. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  313. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  314. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  315. for (r = 0; r < 16; ++r)
  316. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  317. r, kvmppc_get_gpr(vcpu, r),
  318. r+16, kvmppc_get_gpr(vcpu, r+16));
  319. pr_err("ctr = %.16lx lr = %.16lx\n",
  320. vcpu->arch.ctr, vcpu->arch.lr);
  321. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  322. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  323. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  324. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  325. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  326. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  327. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  328. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  329. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  330. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  331. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  332. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  333. for (r = 0; r < vcpu->arch.slb_max; ++r)
  334. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  335. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  336. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  337. vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
  338. vcpu->arch.last_inst);
  339. }
  340. static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  341. {
  342. struct kvm_vcpu *ret;
  343. mutex_lock(&kvm->lock);
  344. ret = kvm_get_vcpu_by_id(kvm, id);
  345. mutex_unlock(&kvm->lock);
  346. return ret;
  347. }
  348. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  349. {
  350. vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
  351. vpa->yield_count = cpu_to_be32(1);
  352. }
  353. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  354. unsigned long addr, unsigned long len)
  355. {
  356. /* check address is cacheline aligned */
  357. if (addr & (L1_CACHE_BYTES - 1))
  358. return -EINVAL;
  359. spin_lock(&vcpu->arch.vpa_update_lock);
  360. if (v->next_gpa != addr || v->len != len) {
  361. v->next_gpa = addr;
  362. v->len = addr ? len : 0;
  363. v->update_pending = 1;
  364. }
  365. spin_unlock(&vcpu->arch.vpa_update_lock);
  366. return 0;
  367. }
  368. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  369. struct reg_vpa {
  370. u32 dummy;
  371. union {
  372. __be16 hword;
  373. __be32 word;
  374. } length;
  375. };
  376. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  377. {
  378. if (vpap->update_pending)
  379. return vpap->next_gpa != 0;
  380. return vpap->pinned_addr != NULL;
  381. }
  382. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  383. unsigned long flags,
  384. unsigned long vcpuid, unsigned long vpa)
  385. {
  386. struct kvm *kvm = vcpu->kvm;
  387. unsigned long len, nb;
  388. void *va;
  389. struct kvm_vcpu *tvcpu;
  390. int err;
  391. int subfunc;
  392. struct kvmppc_vpa *vpap;
  393. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  394. if (!tvcpu)
  395. return H_PARAMETER;
  396. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  397. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  398. subfunc == H_VPA_REG_SLB) {
  399. /* Registering new area - address must be cache-line aligned */
  400. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  401. return H_PARAMETER;
  402. /* convert logical addr to kernel addr and read length */
  403. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  404. if (va == NULL)
  405. return H_PARAMETER;
  406. if (subfunc == H_VPA_REG_VPA)
  407. len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
  408. else
  409. len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
  410. kvmppc_unpin_guest_page(kvm, va, vpa, false);
  411. /* Check length */
  412. if (len > nb || len < sizeof(struct reg_vpa))
  413. return H_PARAMETER;
  414. } else {
  415. vpa = 0;
  416. len = 0;
  417. }
  418. err = H_PARAMETER;
  419. vpap = NULL;
  420. spin_lock(&tvcpu->arch.vpa_update_lock);
  421. switch (subfunc) {
  422. case H_VPA_REG_VPA: /* register VPA */
  423. if (len < sizeof(struct lppaca))
  424. break;
  425. vpap = &tvcpu->arch.vpa;
  426. err = 0;
  427. break;
  428. case H_VPA_REG_DTL: /* register DTL */
  429. if (len < sizeof(struct dtl_entry))
  430. break;
  431. len -= len % sizeof(struct dtl_entry);
  432. /* Check that they have previously registered a VPA */
  433. err = H_RESOURCE;
  434. if (!vpa_is_registered(&tvcpu->arch.vpa))
  435. break;
  436. vpap = &tvcpu->arch.dtl;
  437. err = 0;
  438. break;
  439. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  440. /* Check that they have previously registered a VPA */
  441. err = H_RESOURCE;
  442. if (!vpa_is_registered(&tvcpu->arch.vpa))
  443. break;
  444. vpap = &tvcpu->arch.slb_shadow;
  445. err = 0;
  446. break;
  447. case H_VPA_DEREG_VPA: /* deregister VPA */
  448. /* Check they don't still have a DTL or SLB buf registered */
  449. err = H_RESOURCE;
  450. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  451. vpa_is_registered(&tvcpu->arch.slb_shadow))
  452. break;
  453. vpap = &tvcpu->arch.vpa;
  454. err = 0;
  455. break;
  456. case H_VPA_DEREG_DTL: /* deregister DTL */
  457. vpap = &tvcpu->arch.dtl;
  458. err = 0;
  459. break;
  460. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  461. vpap = &tvcpu->arch.slb_shadow;
  462. err = 0;
  463. break;
  464. }
  465. if (vpap) {
  466. vpap->next_gpa = vpa;
  467. vpap->len = len;
  468. vpap->update_pending = 1;
  469. }
  470. spin_unlock(&tvcpu->arch.vpa_update_lock);
  471. return err;
  472. }
  473. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  474. {
  475. struct kvm *kvm = vcpu->kvm;
  476. void *va;
  477. unsigned long nb;
  478. unsigned long gpa;
  479. /*
  480. * We need to pin the page pointed to by vpap->next_gpa,
  481. * but we can't call kvmppc_pin_guest_page under the lock
  482. * as it does get_user_pages() and down_read(). So we
  483. * have to drop the lock, pin the page, then get the lock
  484. * again and check that a new area didn't get registered
  485. * in the meantime.
  486. */
  487. for (;;) {
  488. gpa = vpap->next_gpa;
  489. spin_unlock(&vcpu->arch.vpa_update_lock);
  490. va = NULL;
  491. nb = 0;
  492. if (gpa)
  493. va = kvmppc_pin_guest_page(kvm, gpa, &nb);
  494. spin_lock(&vcpu->arch.vpa_update_lock);
  495. if (gpa == vpap->next_gpa)
  496. break;
  497. /* sigh... unpin that one and try again */
  498. if (va)
  499. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  500. }
  501. vpap->update_pending = 0;
  502. if (va && nb < vpap->len) {
  503. /*
  504. * If it's now too short, it must be that userspace
  505. * has changed the mappings underlying guest memory,
  506. * so unregister the region.
  507. */
  508. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  509. va = NULL;
  510. }
  511. if (vpap->pinned_addr)
  512. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
  513. vpap->dirty);
  514. vpap->gpa = gpa;
  515. vpap->pinned_addr = va;
  516. vpap->dirty = false;
  517. if (va)
  518. vpap->pinned_end = va + vpap->len;
  519. }
  520. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  521. {
  522. if (!(vcpu->arch.vpa.update_pending ||
  523. vcpu->arch.slb_shadow.update_pending ||
  524. vcpu->arch.dtl.update_pending))
  525. return;
  526. spin_lock(&vcpu->arch.vpa_update_lock);
  527. if (vcpu->arch.vpa.update_pending) {
  528. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  529. if (vcpu->arch.vpa.pinned_addr)
  530. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  531. }
  532. if (vcpu->arch.dtl.update_pending) {
  533. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  534. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  535. vcpu->arch.dtl_index = 0;
  536. }
  537. if (vcpu->arch.slb_shadow.update_pending)
  538. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  539. spin_unlock(&vcpu->arch.vpa_update_lock);
  540. }
  541. /*
  542. * Return the accumulated stolen time for the vcore up until `now'.
  543. * The caller should hold the vcore lock.
  544. */
  545. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  546. {
  547. u64 p;
  548. unsigned long flags;
  549. spin_lock_irqsave(&vc->stoltb_lock, flags);
  550. p = vc->stolen_tb;
  551. if (vc->vcore_state != VCORE_INACTIVE &&
  552. vc->preempt_tb != TB_NIL)
  553. p += now - vc->preempt_tb;
  554. spin_unlock_irqrestore(&vc->stoltb_lock, flags);
  555. return p;
  556. }
  557. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  558. struct kvmppc_vcore *vc)
  559. {
  560. struct dtl_entry *dt;
  561. struct lppaca *vpa;
  562. unsigned long stolen;
  563. unsigned long core_stolen;
  564. u64 now;
  565. dt = vcpu->arch.dtl_ptr;
  566. vpa = vcpu->arch.vpa.pinned_addr;
  567. now = mftb();
  568. core_stolen = vcore_stolen_time(vc, now);
  569. stolen = core_stolen - vcpu->arch.stolen_logged;
  570. vcpu->arch.stolen_logged = core_stolen;
  571. spin_lock_irq(&vcpu->arch.tbacct_lock);
  572. stolen += vcpu->arch.busy_stolen;
  573. vcpu->arch.busy_stolen = 0;
  574. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  575. if (!dt || !vpa)
  576. return;
  577. memset(dt, 0, sizeof(struct dtl_entry));
  578. dt->dispatch_reason = 7;
  579. dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
  580. dt->timebase = cpu_to_be64(now + vc->tb_offset);
  581. dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
  582. dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
  583. dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
  584. ++dt;
  585. if (dt == vcpu->arch.dtl.pinned_end)
  586. dt = vcpu->arch.dtl.pinned_addr;
  587. vcpu->arch.dtl_ptr = dt;
  588. /* order writing *dt vs. writing vpa->dtl_idx */
  589. smp_wmb();
  590. vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
  591. vcpu->arch.dtl.dirty = true;
  592. }
  593. static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
  594. {
  595. if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
  596. return true;
  597. if ((!vcpu->arch.vcore->arch_compat) &&
  598. cpu_has_feature(CPU_FTR_ARCH_207S))
  599. return true;
  600. return false;
  601. }
  602. static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
  603. unsigned long resource, unsigned long value1,
  604. unsigned long value2)
  605. {
  606. switch (resource) {
  607. case H_SET_MODE_RESOURCE_SET_CIABR:
  608. if (!kvmppc_power8_compatible(vcpu))
  609. return H_P2;
  610. if (value2)
  611. return H_P4;
  612. if (mflags)
  613. return H_UNSUPPORTED_FLAG_START;
  614. /* Guests can't breakpoint the hypervisor */
  615. if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
  616. return H_P3;
  617. vcpu->arch.ciabr = value1;
  618. return H_SUCCESS;
  619. case H_SET_MODE_RESOURCE_SET_DAWR:
  620. if (!kvmppc_power8_compatible(vcpu))
  621. return H_P2;
  622. if (mflags)
  623. return H_UNSUPPORTED_FLAG_START;
  624. if (value2 & DABRX_HYP)
  625. return H_P4;
  626. vcpu->arch.dawr = value1;
  627. vcpu->arch.dawrx = value2;
  628. return H_SUCCESS;
  629. default:
  630. return H_TOO_HARD;
  631. }
  632. }
  633. static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
  634. {
  635. struct kvmppc_vcore *vcore = target->arch.vcore;
  636. /*
  637. * We expect to have been called by the real mode handler
  638. * (kvmppc_rm_h_confer()) which would have directly returned
  639. * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
  640. * have useful work to do and should not confer) so we don't
  641. * recheck that here.
  642. */
  643. spin_lock(&vcore->lock);
  644. if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
  645. vcore->vcore_state != VCORE_INACTIVE &&
  646. vcore->runner)
  647. target = vcore->runner;
  648. spin_unlock(&vcore->lock);
  649. return kvm_vcpu_yield_to(target);
  650. }
  651. static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
  652. {
  653. int yield_count = 0;
  654. struct lppaca *lppaca;
  655. spin_lock(&vcpu->arch.vpa_update_lock);
  656. lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
  657. if (lppaca)
  658. yield_count = be32_to_cpu(lppaca->yield_count);
  659. spin_unlock(&vcpu->arch.vpa_update_lock);
  660. return yield_count;
  661. }
  662. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  663. {
  664. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  665. unsigned long target, ret = H_SUCCESS;
  666. int yield_count;
  667. struct kvm_vcpu *tvcpu;
  668. int idx, rc;
  669. if (req <= MAX_HCALL_OPCODE &&
  670. !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
  671. return RESUME_HOST;
  672. switch (req) {
  673. case H_CEDE:
  674. break;
  675. case H_PROD:
  676. target = kvmppc_get_gpr(vcpu, 4);
  677. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  678. if (!tvcpu) {
  679. ret = H_PARAMETER;
  680. break;
  681. }
  682. tvcpu->arch.prodded = 1;
  683. smp_mb();
  684. if (vcpu->arch.ceded) {
  685. if (swait_active(&vcpu->wq)) {
  686. swake_up(&vcpu->wq);
  687. vcpu->stat.halt_wakeup++;
  688. }
  689. }
  690. break;
  691. case H_CONFER:
  692. target = kvmppc_get_gpr(vcpu, 4);
  693. if (target == -1)
  694. break;
  695. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  696. if (!tvcpu) {
  697. ret = H_PARAMETER;
  698. break;
  699. }
  700. yield_count = kvmppc_get_gpr(vcpu, 5);
  701. if (kvmppc_get_yield_count(tvcpu) != yield_count)
  702. break;
  703. kvm_arch_vcpu_yield_to(tvcpu);
  704. break;
  705. case H_REGISTER_VPA:
  706. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  707. kvmppc_get_gpr(vcpu, 5),
  708. kvmppc_get_gpr(vcpu, 6));
  709. break;
  710. case H_RTAS:
  711. if (list_empty(&vcpu->kvm->arch.rtas_tokens))
  712. return RESUME_HOST;
  713. idx = srcu_read_lock(&vcpu->kvm->srcu);
  714. rc = kvmppc_rtas_hcall(vcpu);
  715. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  716. if (rc == -ENOENT)
  717. return RESUME_HOST;
  718. else if (rc == 0)
  719. break;
  720. /* Send the error out to userspace via KVM_RUN */
  721. return rc;
  722. case H_LOGICAL_CI_LOAD:
  723. ret = kvmppc_h_logical_ci_load(vcpu);
  724. if (ret == H_TOO_HARD)
  725. return RESUME_HOST;
  726. break;
  727. case H_LOGICAL_CI_STORE:
  728. ret = kvmppc_h_logical_ci_store(vcpu);
  729. if (ret == H_TOO_HARD)
  730. return RESUME_HOST;
  731. break;
  732. case H_SET_MODE:
  733. ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
  734. kvmppc_get_gpr(vcpu, 5),
  735. kvmppc_get_gpr(vcpu, 6),
  736. kvmppc_get_gpr(vcpu, 7));
  737. if (ret == H_TOO_HARD)
  738. return RESUME_HOST;
  739. break;
  740. case H_XIRR:
  741. case H_CPPR:
  742. case H_EOI:
  743. case H_IPI:
  744. case H_IPOLL:
  745. case H_XIRR_X:
  746. if (kvmppc_xics_enabled(vcpu)) {
  747. ret = kvmppc_xics_hcall(vcpu, req);
  748. break;
  749. }
  750. return RESUME_HOST;
  751. case H_PUT_TCE:
  752. ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  753. kvmppc_get_gpr(vcpu, 5),
  754. kvmppc_get_gpr(vcpu, 6));
  755. if (ret == H_TOO_HARD)
  756. return RESUME_HOST;
  757. break;
  758. case H_PUT_TCE_INDIRECT:
  759. ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
  760. kvmppc_get_gpr(vcpu, 5),
  761. kvmppc_get_gpr(vcpu, 6),
  762. kvmppc_get_gpr(vcpu, 7));
  763. if (ret == H_TOO_HARD)
  764. return RESUME_HOST;
  765. break;
  766. case H_STUFF_TCE:
  767. ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
  768. kvmppc_get_gpr(vcpu, 5),
  769. kvmppc_get_gpr(vcpu, 6),
  770. kvmppc_get_gpr(vcpu, 7));
  771. if (ret == H_TOO_HARD)
  772. return RESUME_HOST;
  773. break;
  774. default:
  775. return RESUME_HOST;
  776. }
  777. kvmppc_set_gpr(vcpu, 3, ret);
  778. vcpu->arch.hcall_needed = 0;
  779. return RESUME_GUEST;
  780. }
  781. static int kvmppc_hcall_impl_hv(unsigned long cmd)
  782. {
  783. switch (cmd) {
  784. case H_CEDE:
  785. case H_PROD:
  786. case H_CONFER:
  787. case H_REGISTER_VPA:
  788. case H_SET_MODE:
  789. case H_LOGICAL_CI_LOAD:
  790. case H_LOGICAL_CI_STORE:
  791. #ifdef CONFIG_KVM_XICS
  792. case H_XIRR:
  793. case H_CPPR:
  794. case H_EOI:
  795. case H_IPI:
  796. case H_IPOLL:
  797. case H_XIRR_X:
  798. #endif
  799. return 1;
  800. }
  801. /* See if it's in the real-mode table */
  802. return kvmppc_hcall_impl_hv_realmode(cmd);
  803. }
  804. static int kvmppc_emulate_debug_inst(struct kvm_run *run,
  805. struct kvm_vcpu *vcpu)
  806. {
  807. u32 last_inst;
  808. if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
  809. EMULATE_DONE) {
  810. /*
  811. * Fetch failed, so return to guest and
  812. * try executing it again.
  813. */
  814. return RESUME_GUEST;
  815. }
  816. if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
  817. run->exit_reason = KVM_EXIT_DEBUG;
  818. run->debug.arch.address = kvmppc_get_pc(vcpu);
  819. return RESUME_HOST;
  820. } else {
  821. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  822. return RESUME_GUEST;
  823. }
  824. }
  825. static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  826. struct task_struct *tsk)
  827. {
  828. int r = RESUME_HOST;
  829. vcpu->stat.sum_exits++;
  830. /*
  831. * This can happen if an interrupt occurs in the last stages
  832. * of guest entry or the first stages of guest exit (i.e. after
  833. * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
  834. * and before setting it to KVM_GUEST_MODE_HOST_HV).
  835. * That can happen due to a bug, or due to a machine check
  836. * occurring at just the wrong time.
  837. */
  838. if (vcpu->arch.shregs.msr & MSR_HV) {
  839. printk(KERN_EMERG "KVM trap in HV mode!\n");
  840. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  841. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  842. vcpu->arch.shregs.msr);
  843. kvmppc_dump_regs(vcpu);
  844. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  845. run->hw.hardware_exit_reason = vcpu->arch.trap;
  846. return RESUME_HOST;
  847. }
  848. run->exit_reason = KVM_EXIT_UNKNOWN;
  849. run->ready_for_interrupt_injection = 1;
  850. switch (vcpu->arch.trap) {
  851. /* We're good on these - the host merely wanted to get our attention */
  852. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  853. vcpu->stat.dec_exits++;
  854. r = RESUME_GUEST;
  855. break;
  856. case BOOK3S_INTERRUPT_EXTERNAL:
  857. case BOOK3S_INTERRUPT_H_DOORBELL:
  858. case BOOK3S_INTERRUPT_H_VIRT:
  859. vcpu->stat.ext_intr_exits++;
  860. r = RESUME_GUEST;
  861. break;
  862. /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
  863. case BOOK3S_INTERRUPT_HMI:
  864. case BOOK3S_INTERRUPT_PERFMON:
  865. r = RESUME_GUEST;
  866. break;
  867. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  868. /*
  869. * Deliver a machine check interrupt to the guest.
  870. * We have to do this, even if the host has handled the
  871. * machine check, because machine checks use SRR0/1 and
  872. * the interrupt might have trashed guest state in them.
  873. */
  874. kvmppc_book3s_queue_irqprio(vcpu,
  875. BOOK3S_INTERRUPT_MACHINE_CHECK);
  876. r = RESUME_GUEST;
  877. break;
  878. case BOOK3S_INTERRUPT_PROGRAM:
  879. {
  880. ulong flags;
  881. /*
  882. * Normally program interrupts are delivered directly
  883. * to the guest by the hardware, but we can get here
  884. * as a result of a hypervisor emulation interrupt
  885. * (e40) getting turned into a 700 by BML RTAS.
  886. */
  887. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  888. kvmppc_core_queue_program(vcpu, flags);
  889. r = RESUME_GUEST;
  890. break;
  891. }
  892. case BOOK3S_INTERRUPT_SYSCALL:
  893. {
  894. /* hcall - punt to userspace */
  895. int i;
  896. /* hypercall with MSR_PR has already been handled in rmode,
  897. * and never reaches here.
  898. */
  899. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  900. for (i = 0; i < 9; ++i)
  901. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  902. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  903. vcpu->arch.hcall_needed = 1;
  904. r = RESUME_HOST;
  905. break;
  906. }
  907. /*
  908. * We get these next two if the guest accesses a page which it thinks
  909. * it has mapped but which is not actually present, either because
  910. * it is for an emulated I/O device or because the corresonding
  911. * host page has been paged out. Any other HDSI/HISI interrupts
  912. * have been handled already.
  913. */
  914. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  915. r = RESUME_PAGE_FAULT;
  916. break;
  917. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  918. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  919. vcpu->arch.fault_dsisr = 0;
  920. r = RESUME_PAGE_FAULT;
  921. break;
  922. /*
  923. * This occurs if the guest executes an illegal instruction.
  924. * If the guest debug is disabled, generate a program interrupt
  925. * to the guest. If guest debug is enabled, we need to check
  926. * whether the instruction is a software breakpoint instruction.
  927. * Accordingly return to Guest or Host.
  928. */
  929. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  930. if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
  931. vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
  932. swab32(vcpu->arch.emul_inst) :
  933. vcpu->arch.emul_inst;
  934. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
  935. r = kvmppc_emulate_debug_inst(run, vcpu);
  936. } else {
  937. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  938. r = RESUME_GUEST;
  939. }
  940. break;
  941. /*
  942. * This occurs if the guest (kernel or userspace), does something that
  943. * is prohibited by HFSCR. We just generate a program interrupt to
  944. * the guest.
  945. */
  946. case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
  947. kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
  948. r = RESUME_GUEST;
  949. break;
  950. case BOOK3S_INTERRUPT_HV_RM_HARD:
  951. r = RESUME_PASSTHROUGH;
  952. break;
  953. default:
  954. kvmppc_dump_regs(vcpu);
  955. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  956. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  957. vcpu->arch.shregs.msr);
  958. run->hw.hardware_exit_reason = vcpu->arch.trap;
  959. r = RESUME_HOST;
  960. break;
  961. }
  962. return r;
  963. }
  964. static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
  965. struct kvm_sregs *sregs)
  966. {
  967. int i;
  968. memset(sregs, 0, sizeof(struct kvm_sregs));
  969. sregs->pvr = vcpu->arch.pvr;
  970. for (i = 0; i < vcpu->arch.slb_max; i++) {
  971. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  972. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  973. }
  974. return 0;
  975. }
  976. static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
  977. struct kvm_sregs *sregs)
  978. {
  979. int i, j;
  980. /* Only accept the same PVR as the host's, since we can't spoof it */
  981. if (sregs->pvr != vcpu->arch.pvr)
  982. return -EINVAL;
  983. j = 0;
  984. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  985. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  986. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  987. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  988. ++j;
  989. }
  990. }
  991. vcpu->arch.slb_max = j;
  992. return 0;
  993. }
  994. static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
  995. bool preserve_top32)
  996. {
  997. struct kvm *kvm = vcpu->kvm;
  998. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  999. u64 mask;
  1000. mutex_lock(&kvm->lock);
  1001. spin_lock(&vc->lock);
  1002. /*
  1003. * If ILE (interrupt little-endian) has changed, update the
  1004. * MSR_LE bit in the intr_msr for each vcpu in this vcore.
  1005. */
  1006. if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
  1007. struct kvm_vcpu *vcpu;
  1008. int i;
  1009. kvm_for_each_vcpu(i, vcpu, kvm) {
  1010. if (vcpu->arch.vcore != vc)
  1011. continue;
  1012. if (new_lpcr & LPCR_ILE)
  1013. vcpu->arch.intr_msr |= MSR_LE;
  1014. else
  1015. vcpu->arch.intr_msr &= ~MSR_LE;
  1016. }
  1017. }
  1018. /*
  1019. * Userspace can only modify DPFD (default prefetch depth),
  1020. * ILE (interrupt little-endian) and TC (translation control).
  1021. * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
  1022. */
  1023. mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
  1024. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  1025. mask |= LPCR_AIL;
  1026. /* Broken 32-bit version of LPCR must not clear top bits */
  1027. if (preserve_top32)
  1028. mask &= 0xFFFFFFFF;
  1029. vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
  1030. spin_unlock(&vc->lock);
  1031. mutex_unlock(&kvm->lock);
  1032. }
  1033. static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1034. union kvmppc_one_reg *val)
  1035. {
  1036. int r = 0;
  1037. long int i;
  1038. switch (id) {
  1039. case KVM_REG_PPC_DEBUG_INST:
  1040. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  1041. break;
  1042. case KVM_REG_PPC_HIOR:
  1043. *val = get_reg_val(id, 0);
  1044. break;
  1045. case KVM_REG_PPC_DABR:
  1046. *val = get_reg_val(id, vcpu->arch.dabr);
  1047. break;
  1048. case KVM_REG_PPC_DABRX:
  1049. *val = get_reg_val(id, vcpu->arch.dabrx);
  1050. break;
  1051. case KVM_REG_PPC_DSCR:
  1052. *val = get_reg_val(id, vcpu->arch.dscr);
  1053. break;
  1054. case KVM_REG_PPC_PURR:
  1055. *val = get_reg_val(id, vcpu->arch.purr);
  1056. break;
  1057. case KVM_REG_PPC_SPURR:
  1058. *val = get_reg_val(id, vcpu->arch.spurr);
  1059. break;
  1060. case KVM_REG_PPC_AMR:
  1061. *val = get_reg_val(id, vcpu->arch.amr);
  1062. break;
  1063. case KVM_REG_PPC_UAMOR:
  1064. *val = get_reg_val(id, vcpu->arch.uamor);
  1065. break;
  1066. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1067. i = id - KVM_REG_PPC_MMCR0;
  1068. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  1069. break;
  1070. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1071. i = id - KVM_REG_PPC_PMC1;
  1072. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  1073. break;
  1074. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1075. i = id - KVM_REG_PPC_SPMC1;
  1076. *val = get_reg_val(id, vcpu->arch.spmc[i]);
  1077. break;
  1078. case KVM_REG_PPC_SIAR:
  1079. *val = get_reg_val(id, vcpu->arch.siar);
  1080. break;
  1081. case KVM_REG_PPC_SDAR:
  1082. *val = get_reg_val(id, vcpu->arch.sdar);
  1083. break;
  1084. case KVM_REG_PPC_SIER:
  1085. *val = get_reg_val(id, vcpu->arch.sier);
  1086. break;
  1087. case KVM_REG_PPC_IAMR:
  1088. *val = get_reg_val(id, vcpu->arch.iamr);
  1089. break;
  1090. case KVM_REG_PPC_PSPB:
  1091. *val = get_reg_val(id, vcpu->arch.pspb);
  1092. break;
  1093. case KVM_REG_PPC_DPDES:
  1094. *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
  1095. break;
  1096. case KVM_REG_PPC_VTB:
  1097. *val = get_reg_val(id, vcpu->arch.vcore->vtb);
  1098. break;
  1099. case KVM_REG_PPC_DAWR:
  1100. *val = get_reg_val(id, vcpu->arch.dawr);
  1101. break;
  1102. case KVM_REG_PPC_DAWRX:
  1103. *val = get_reg_val(id, vcpu->arch.dawrx);
  1104. break;
  1105. case KVM_REG_PPC_CIABR:
  1106. *val = get_reg_val(id, vcpu->arch.ciabr);
  1107. break;
  1108. case KVM_REG_PPC_CSIGR:
  1109. *val = get_reg_val(id, vcpu->arch.csigr);
  1110. break;
  1111. case KVM_REG_PPC_TACR:
  1112. *val = get_reg_val(id, vcpu->arch.tacr);
  1113. break;
  1114. case KVM_REG_PPC_TCSCR:
  1115. *val = get_reg_val(id, vcpu->arch.tcscr);
  1116. break;
  1117. case KVM_REG_PPC_PID:
  1118. *val = get_reg_val(id, vcpu->arch.pid);
  1119. break;
  1120. case KVM_REG_PPC_ACOP:
  1121. *val = get_reg_val(id, vcpu->arch.acop);
  1122. break;
  1123. case KVM_REG_PPC_WORT:
  1124. *val = get_reg_val(id, vcpu->arch.wort);
  1125. break;
  1126. case KVM_REG_PPC_TIDR:
  1127. *val = get_reg_val(id, vcpu->arch.tid);
  1128. break;
  1129. case KVM_REG_PPC_PSSCR:
  1130. *val = get_reg_val(id, vcpu->arch.psscr);
  1131. break;
  1132. case KVM_REG_PPC_VPA_ADDR:
  1133. spin_lock(&vcpu->arch.vpa_update_lock);
  1134. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  1135. spin_unlock(&vcpu->arch.vpa_update_lock);
  1136. break;
  1137. case KVM_REG_PPC_VPA_SLB:
  1138. spin_lock(&vcpu->arch.vpa_update_lock);
  1139. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  1140. val->vpaval.length = vcpu->arch.slb_shadow.len;
  1141. spin_unlock(&vcpu->arch.vpa_update_lock);
  1142. break;
  1143. case KVM_REG_PPC_VPA_DTL:
  1144. spin_lock(&vcpu->arch.vpa_update_lock);
  1145. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  1146. val->vpaval.length = vcpu->arch.dtl.len;
  1147. spin_unlock(&vcpu->arch.vpa_update_lock);
  1148. break;
  1149. case KVM_REG_PPC_TB_OFFSET:
  1150. *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
  1151. break;
  1152. case KVM_REG_PPC_LPCR:
  1153. case KVM_REG_PPC_LPCR_64:
  1154. *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
  1155. break;
  1156. case KVM_REG_PPC_PPR:
  1157. *val = get_reg_val(id, vcpu->arch.ppr);
  1158. break;
  1159. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1160. case KVM_REG_PPC_TFHAR:
  1161. *val = get_reg_val(id, vcpu->arch.tfhar);
  1162. break;
  1163. case KVM_REG_PPC_TFIAR:
  1164. *val = get_reg_val(id, vcpu->arch.tfiar);
  1165. break;
  1166. case KVM_REG_PPC_TEXASR:
  1167. *val = get_reg_val(id, vcpu->arch.texasr);
  1168. break;
  1169. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1170. i = id - KVM_REG_PPC_TM_GPR0;
  1171. *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
  1172. break;
  1173. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1174. {
  1175. int j;
  1176. i = id - KVM_REG_PPC_TM_VSR0;
  1177. if (i < 32)
  1178. for (j = 0; j < TS_FPRWIDTH; j++)
  1179. val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
  1180. else {
  1181. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1182. val->vval = vcpu->arch.vr_tm.vr[i-32];
  1183. else
  1184. r = -ENXIO;
  1185. }
  1186. break;
  1187. }
  1188. case KVM_REG_PPC_TM_CR:
  1189. *val = get_reg_val(id, vcpu->arch.cr_tm);
  1190. break;
  1191. case KVM_REG_PPC_TM_XER:
  1192. *val = get_reg_val(id, vcpu->arch.xer_tm);
  1193. break;
  1194. case KVM_REG_PPC_TM_LR:
  1195. *val = get_reg_val(id, vcpu->arch.lr_tm);
  1196. break;
  1197. case KVM_REG_PPC_TM_CTR:
  1198. *val = get_reg_val(id, vcpu->arch.ctr_tm);
  1199. break;
  1200. case KVM_REG_PPC_TM_FPSCR:
  1201. *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
  1202. break;
  1203. case KVM_REG_PPC_TM_AMR:
  1204. *val = get_reg_val(id, vcpu->arch.amr_tm);
  1205. break;
  1206. case KVM_REG_PPC_TM_PPR:
  1207. *val = get_reg_val(id, vcpu->arch.ppr_tm);
  1208. break;
  1209. case KVM_REG_PPC_TM_VRSAVE:
  1210. *val = get_reg_val(id, vcpu->arch.vrsave_tm);
  1211. break;
  1212. case KVM_REG_PPC_TM_VSCR:
  1213. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1214. *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
  1215. else
  1216. r = -ENXIO;
  1217. break;
  1218. case KVM_REG_PPC_TM_DSCR:
  1219. *val = get_reg_val(id, vcpu->arch.dscr_tm);
  1220. break;
  1221. case KVM_REG_PPC_TM_TAR:
  1222. *val = get_reg_val(id, vcpu->arch.tar_tm);
  1223. break;
  1224. #endif
  1225. case KVM_REG_PPC_ARCH_COMPAT:
  1226. *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
  1227. break;
  1228. default:
  1229. r = -EINVAL;
  1230. break;
  1231. }
  1232. return r;
  1233. }
  1234. static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
  1235. union kvmppc_one_reg *val)
  1236. {
  1237. int r = 0;
  1238. long int i;
  1239. unsigned long addr, len;
  1240. switch (id) {
  1241. case KVM_REG_PPC_HIOR:
  1242. /* Only allow this to be set to zero */
  1243. if (set_reg_val(id, *val))
  1244. r = -EINVAL;
  1245. break;
  1246. case KVM_REG_PPC_DABR:
  1247. vcpu->arch.dabr = set_reg_val(id, *val);
  1248. break;
  1249. case KVM_REG_PPC_DABRX:
  1250. vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
  1251. break;
  1252. case KVM_REG_PPC_DSCR:
  1253. vcpu->arch.dscr = set_reg_val(id, *val);
  1254. break;
  1255. case KVM_REG_PPC_PURR:
  1256. vcpu->arch.purr = set_reg_val(id, *val);
  1257. break;
  1258. case KVM_REG_PPC_SPURR:
  1259. vcpu->arch.spurr = set_reg_val(id, *val);
  1260. break;
  1261. case KVM_REG_PPC_AMR:
  1262. vcpu->arch.amr = set_reg_val(id, *val);
  1263. break;
  1264. case KVM_REG_PPC_UAMOR:
  1265. vcpu->arch.uamor = set_reg_val(id, *val);
  1266. break;
  1267. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
  1268. i = id - KVM_REG_PPC_MMCR0;
  1269. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  1270. break;
  1271. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  1272. i = id - KVM_REG_PPC_PMC1;
  1273. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  1274. break;
  1275. case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
  1276. i = id - KVM_REG_PPC_SPMC1;
  1277. vcpu->arch.spmc[i] = set_reg_val(id, *val);
  1278. break;
  1279. case KVM_REG_PPC_SIAR:
  1280. vcpu->arch.siar = set_reg_val(id, *val);
  1281. break;
  1282. case KVM_REG_PPC_SDAR:
  1283. vcpu->arch.sdar = set_reg_val(id, *val);
  1284. break;
  1285. case KVM_REG_PPC_SIER:
  1286. vcpu->arch.sier = set_reg_val(id, *val);
  1287. break;
  1288. case KVM_REG_PPC_IAMR:
  1289. vcpu->arch.iamr = set_reg_val(id, *val);
  1290. break;
  1291. case KVM_REG_PPC_PSPB:
  1292. vcpu->arch.pspb = set_reg_val(id, *val);
  1293. break;
  1294. case KVM_REG_PPC_DPDES:
  1295. vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
  1296. break;
  1297. case KVM_REG_PPC_VTB:
  1298. vcpu->arch.vcore->vtb = set_reg_val(id, *val);
  1299. break;
  1300. case KVM_REG_PPC_DAWR:
  1301. vcpu->arch.dawr = set_reg_val(id, *val);
  1302. break;
  1303. case KVM_REG_PPC_DAWRX:
  1304. vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
  1305. break;
  1306. case KVM_REG_PPC_CIABR:
  1307. vcpu->arch.ciabr = set_reg_val(id, *val);
  1308. /* Don't allow setting breakpoints in hypervisor code */
  1309. if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
  1310. vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
  1311. break;
  1312. case KVM_REG_PPC_CSIGR:
  1313. vcpu->arch.csigr = set_reg_val(id, *val);
  1314. break;
  1315. case KVM_REG_PPC_TACR:
  1316. vcpu->arch.tacr = set_reg_val(id, *val);
  1317. break;
  1318. case KVM_REG_PPC_TCSCR:
  1319. vcpu->arch.tcscr = set_reg_val(id, *val);
  1320. break;
  1321. case KVM_REG_PPC_PID:
  1322. vcpu->arch.pid = set_reg_val(id, *val);
  1323. break;
  1324. case KVM_REG_PPC_ACOP:
  1325. vcpu->arch.acop = set_reg_val(id, *val);
  1326. break;
  1327. case KVM_REG_PPC_WORT:
  1328. vcpu->arch.wort = set_reg_val(id, *val);
  1329. break;
  1330. case KVM_REG_PPC_TIDR:
  1331. vcpu->arch.tid = set_reg_val(id, *val);
  1332. break;
  1333. case KVM_REG_PPC_PSSCR:
  1334. vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
  1335. break;
  1336. case KVM_REG_PPC_VPA_ADDR:
  1337. addr = set_reg_val(id, *val);
  1338. r = -EINVAL;
  1339. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  1340. vcpu->arch.dtl.next_gpa))
  1341. break;
  1342. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  1343. break;
  1344. case KVM_REG_PPC_VPA_SLB:
  1345. addr = val->vpaval.addr;
  1346. len = val->vpaval.length;
  1347. r = -EINVAL;
  1348. if (addr && !vcpu->arch.vpa.next_gpa)
  1349. break;
  1350. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  1351. break;
  1352. case KVM_REG_PPC_VPA_DTL:
  1353. addr = val->vpaval.addr;
  1354. len = val->vpaval.length;
  1355. r = -EINVAL;
  1356. if (addr && (len < sizeof(struct dtl_entry) ||
  1357. !vcpu->arch.vpa.next_gpa))
  1358. break;
  1359. len -= len % sizeof(struct dtl_entry);
  1360. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  1361. break;
  1362. case KVM_REG_PPC_TB_OFFSET:
  1363. /* round up to multiple of 2^24 */
  1364. vcpu->arch.vcore->tb_offset =
  1365. ALIGN(set_reg_val(id, *val), 1UL << 24);
  1366. break;
  1367. case KVM_REG_PPC_LPCR:
  1368. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
  1369. break;
  1370. case KVM_REG_PPC_LPCR_64:
  1371. kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
  1372. break;
  1373. case KVM_REG_PPC_PPR:
  1374. vcpu->arch.ppr = set_reg_val(id, *val);
  1375. break;
  1376. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1377. case KVM_REG_PPC_TFHAR:
  1378. vcpu->arch.tfhar = set_reg_val(id, *val);
  1379. break;
  1380. case KVM_REG_PPC_TFIAR:
  1381. vcpu->arch.tfiar = set_reg_val(id, *val);
  1382. break;
  1383. case KVM_REG_PPC_TEXASR:
  1384. vcpu->arch.texasr = set_reg_val(id, *val);
  1385. break;
  1386. case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
  1387. i = id - KVM_REG_PPC_TM_GPR0;
  1388. vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
  1389. break;
  1390. case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
  1391. {
  1392. int j;
  1393. i = id - KVM_REG_PPC_TM_VSR0;
  1394. if (i < 32)
  1395. for (j = 0; j < TS_FPRWIDTH; j++)
  1396. vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
  1397. else
  1398. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1399. vcpu->arch.vr_tm.vr[i-32] = val->vval;
  1400. else
  1401. r = -ENXIO;
  1402. break;
  1403. }
  1404. case KVM_REG_PPC_TM_CR:
  1405. vcpu->arch.cr_tm = set_reg_val(id, *val);
  1406. break;
  1407. case KVM_REG_PPC_TM_XER:
  1408. vcpu->arch.xer_tm = set_reg_val(id, *val);
  1409. break;
  1410. case KVM_REG_PPC_TM_LR:
  1411. vcpu->arch.lr_tm = set_reg_val(id, *val);
  1412. break;
  1413. case KVM_REG_PPC_TM_CTR:
  1414. vcpu->arch.ctr_tm = set_reg_val(id, *val);
  1415. break;
  1416. case KVM_REG_PPC_TM_FPSCR:
  1417. vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
  1418. break;
  1419. case KVM_REG_PPC_TM_AMR:
  1420. vcpu->arch.amr_tm = set_reg_val(id, *val);
  1421. break;
  1422. case KVM_REG_PPC_TM_PPR:
  1423. vcpu->arch.ppr_tm = set_reg_val(id, *val);
  1424. break;
  1425. case KVM_REG_PPC_TM_VRSAVE:
  1426. vcpu->arch.vrsave_tm = set_reg_val(id, *val);
  1427. break;
  1428. case KVM_REG_PPC_TM_VSCR:
  1429. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  1430. vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
  1431. else
  1432. r = - ENXIO;
  1433. break;
  1434. case KVM_REG_PPC_TM_DSCR:
  1435. vcpu->arch.dscr_tm = set_reg_val(id, *val);
  1436. break;
  1437. case KVM_REG_PPC_TM_TAR:
  1438. vcpu->arch.tar_tm = set_reg_val(id, *val);
  1439. break;
  1440. #endif
  1441. case KVM_REG_PPC_ARCH_COMPAT:
  1442. r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
  1443. break;
  1444. default:
  1445. r = -EINVAL;
  1446. break;
  1447. }
  1448. return r;
  1449. }
  1450. /*
  1451. * On POWER9, threads are independent and can be in different partitions.
  1452. * Therefore we consider each thread to be a subcore.
  1453. * There is a restriction that all threads have to be in the same
  1454. * MMU mode (radix or HPT), unfortunately, but since we only support
  1455. * HPT guests on a HPT host so far, that isn't an impediment yet.
  1456. */
  1457. static int threads_per_vcore(void)
  1458. {
  1459. if (cpu_has_feature(CPU_FTR_ARCH_300))
  1460. return 1;
  1461. return threads_per_subcore;
  1462. }
  1463. static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
  1464. {
  1465. struct kvmppc_vcore *vcore;
  1466. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  1467. if (vcore == NULL)
  1468. return NULL;
  1469. spin_lock_init(&vcore->lock);
  1470. spin_lock_init(&vcore->stoltb_lock);
  1471. init_swait_queue_head(&vcore->wq);
  1472. vcore->preempt_tb = TB_NIL;
  1473. vcore->lpcr = kvm->arch.lpcr;
  1474. vcore->first_vcpuid = core * threads_per_vcore();
  1475. vcore->kvm = kvm;
  1476. INIT_LIST_HEAD(&vcore->preempt_list);
  1477. return vcore;
  1478. }
  1479. #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
  1480. static struct debugfs_timings_element {
  1481. const char *name;
  1482. size_t offset;
  1483. } timings[] = {
  1484. {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
  1485. {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
  1486. {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
  1487. {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
  1488. {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
  1489. };
  1490. #define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
  1491. struct debugfs_timings_state {
  1492. struct kvm_vcpu *vcpu;
  1493. unsigned int buflen;
  1494. char buf[N_TIMINGS * 100];
  1495. };
  1496. static int debugfs_timings_open(struct inode *inode, struct file *file)
  1497. {
  1498. struct kvm_vcpu *vcpu = inode->i_private;
  1499. struct debugfs_timings_state *p;
  1500. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1501. if (!p)
  1502. return -ENOMEM;
  1503. kvm_get_kvm(vcpu->kvm);
  1504. p->vcpu = vcpu;
  1505. file->private_data = p;
  1506. return nonseekable_open(inode, file);
  1507. }
  1508. static int debugfs_timings_release(struct inode *inode, struct file *file)
  1509. {
  1510. struct debugfs_timings_state *p = file->private_data;
  1511. kvm_put_kvm(p->vcpu->kvm);
  1512. kfree(p);
  1513. return 0;
  1514. }
  1515. static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
  1516. size_t len, loff_t *ppos)
  1517. {
  1518. struct debugfs_timings_state *p = file->private_data;
  1519. struct kvm_vcpu *vcpu = p->vcpu;
  1520. char *s, *buf_end;
  1521. struct kvmhv_tb_accumulator tb;
  1522. u64 count;
  1523. loff_t pos;
  1524. ssize_t n;
  1525. int i, loops;
  1526. bool ok;
  1527. if (!p->buflen) {
  1528. s = p->buf;
  1529. buf_end = s + sizeof(p->buf);
  1530. for (i = 0; i < N_TIMINGS; ++i) {
  1531. struct kvmhv_tb_accumulator *acc;
  1532. acc = (struct kvmhv_tb_accumulator *)
  1533. ((unsigned long)vcpu + timings[i].offset);
  1534. ok = false;
  1535. for (loops = 0; loops < 1000; ++loops) {
  1536. count = acc->seqcount;
  1537. if (!(count & 1)) {
  1538. smp_rmb();
  1539. tb = *acc;
  1540. smp_rmb();
  1541. if (count == acc->seqcount) {
  1542. ok = true;
  1543. break;
  1544. }
  1545. }
  1546. udelay(1);
  1547. }
  1548. if (!ok)
  1549. snprintf(s, buf_end - s, "%s: stuck\n",
  1550. timings[i].name);
  1551. else
  1552. snprintf(s, buf_end - s,
  1553. "%s: %llu %llu %llu %llu\n",
  1554. timings[i].name, count / 2,
  1555. tb_to_ns(tb.tb_total),
  1556. tb_to_ns(tb.tb_min),
  1557. tb_to_ns(tb.tb_max));
  1558. s += strlen(s);
  1559. }
  1560. p->buflen = s - p->buf;
  1561. }
  1562. pos = *ppos;
  1563. if (pos >= p->buflen)
  1564. return 0;
  1565. if (len > p->buflen - pos)
  1566. len = p->buflen - pos;
  1567. n = copy_to_user(buf, p->buf + pos, len);
  1568. if (n) {
  1569. if (n == len)
  1570. return -EFAULT;
  1571. len -= n;
  1572. }
  1573. *ppos = pos + len;
  1574. return len;
  1575. }
  1576. static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
  1577. size_t len, loff_t *ppos)
  1578. {
  1579. return -EACCES;
  1580. }
  1581. static const struct file_operations debugfs_timings_ops = {
  1582. .owner = THIS_MODULE,
  1583. .open = debugfs_timings_open,
  1584. .release = debugfs_timings_release,
  1585. .read = debugfs_timings_read,
  1586. .write = debugfs_timings_write,
  1587. .llseek = generic_file_llseek,
  1588. };
  1589. /* Create a debugfs directory for the vcpu */
  1590. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1591. {
  1592. char buf[16];
  1593. struct kvm *kvm = vcpu->kvm;
  1594. snprintf(buf, sizeof(buf), "vcpu%u", id);
  1595. if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  1596. return;
  1597. vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
  1598. if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
  1599. return;
  1600. vcpu->arch.debugfs_timings =
  1601. debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
  1602. vcpu, &debugfs_timings_ops);
  1603. }
  1604. #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1605. static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
  1606. {
  1607. }
  1608. #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
  1609. static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
  1610. unsigned int id)
  1611. {
  1612. struct kvm_vcpu *vcpu;
  1613. int err = -EINVAL;
  1614. int core;
  1615. struct kvmppc_vcore *vcore;
  1616. core = id / threads_per_vcore();
  1617. if (core >= KVM_MAX_VCORES)
  1618. goto out;
  1619. err = -ENOMEM;
  1620. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1621. if (!vcpu)
  1622. goto out;
  1623. err = kvm_vcpu_init(vcpu, kvm, id);
  1624. if (err)
  1625. goto free_vcpu;
  1626. vcpu->arch.shared = &vcpu->arch.shregs;
  1627. #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
  1628. /*
  1629. * The shared struct is never shared on HV,
  1630. * so we can always use host endianness
  1631. */
  1632. #ifdef __BIG_ENDIAN__
  1633. vcpu->arch.shared_big_endian = true;
  1634. #else
  1635. vcpu->arch.shared_big_endian = false;
  1636. #endif
  1637. #endif
  1638. vcpu->arch.mmcr[0] = MMCR0_FC;
  1639. vcpu->arch.ctrl = CTRL_RUNLATCH;
  1640. /* default to host PVR, since we can't spoof it */
  1641. kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
  1642. spin_lock_init(&vcpu->arch.vpa_update_lock);
  1643. spin_lock_init(&vcpu->arch.tbacct_lock);
  1644. vcpu->arch.busy_preempt = TB_NIL;
  1645. vcpu->arch.intr_msr = MSR_SF | MSR_ME;
  1646. kvmppc_mmu_book3s_hv_init(vcpu);
  1647. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  1648. init_waitqueue_head(&vcpu->arch.cpu_run);
  1649. mutex_lock(&kvm->lock);
  1650. vcore = kvm->arch.vcores[core];
  1651. if (!vcore) {
  1652. vcore = kvmppc_vcore_create(kvm, core);
  1653. kvm->arch.vcores[core] = vcore;
  1654. kvm->arch.online_vcores++;
  1655. }
  1656. mutex_unlock(&kvm->lock);
  1657. if (!vcore)
  1658. goto free_vcpu;
  1659. spin_lock(&vcore->lock);
  1660. ++vcore->num_threads;
  1661. spin_unlock(&vcore->lock);
  1662. vcpu->arch.vcore = vcore;
  1663. vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
  1664. vcpu->arch.thread_cpu = -1;
  1665. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  1666. kvmppc_sanity_check(vcpu);
  1667. debugfs_vcpu_init(vcpu, id);
  1668. return vcpu;
  1669. free_vcpu:
  1670. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1671. out:
  1672. return ERR_PTR(err);
  1673. }
  1674. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  1675. {
  1676. if (vpa->pinned_addr)
  1677. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  1678. vpa->dirty);
  1679. }
  1680. static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
  1681. {
  1682. spin_lock(&vcpu->arch.vpa_update_lock);
  1683. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  1684. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  1685. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  1686. spin_unlock(&vcpu->arch.vpa_update_lock);
  1687. kvm_vcpu_uninit(vcpu);
  1688. kmem_cache_free(kvm_vcpu_cache, vcpu);
  1689. }
  1690. static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
  1691. {
  1692. /* Indicate we want to get back into the guest */
  1693. return 1;
  1694. }
  1695. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  1696. {
  1697. unsigned long dec_nsec, now;
  1698. now = get_tb();
  1699. if (now > vcpu->arch.dec_expires) {
  1700. /* decrementer has already gone negative */
  1701. kvmppc_core_queue_dec(vcpu);
  1702. kvmppc_core_prepare_to_enter(vcpu);
  1703. return;
  1704. }
  1705. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  1706. / tb_ticks_per_sec;
  1707. hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
  1708. vcpu->arch.timer_running = 1;
  1709. }
  1710. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  1711. {
  1712. vcpu->arch.ceded = 0;
  1713. if (vcpu->arch.timer_running) {
  1714. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1715. vcpu->arch.timer_running = 0;
  1716. }
  1717. }
  1718. extern void __kvmppc_vcore_entry(void);
  1719. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  1720. struct kvm_vcpu *vcpu)
  1721. {
  1722. u64 now;
  1723. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1724. return;
  1725. spin_lock_irq(&vcpu->arch.tbacct_lock);
  1726. now = mftb();
  1727. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  1728. vcpu->arch.stolen_logged;
  1729. vcpu->arch.busy_preempt = now;
  1730. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  1731. spin_unlock_irq(&vcpu->arch.tbacct_lock);
  1732. --vc->n_runnable;
  1733. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
  1734. }
  1735. static int kvmppc_grab_hwthread(int cpu)
  1736. {
  1737. struct paca_struct *tpaca;
  1738. long timeout = 10000;
  1739. tpaca = &paca[cpu];
  1740. /* Ensure the thread won't go into the kernel if it wakes */
  1741. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1742. tpaca->kvm_hstate.kvm_vcore = NULL;
  1743. tpaca->kvm_hstate.napping = 0;
  1744. smp_wmb();
  1745. tpaca->kvm_hstate.hwthread_req = 1;
  1746. /*
  1747. * If the thread is already executing in the kernel (e.g. handling
  1748. * a stray interrupt), wait for it to get back to nap mode.
  1749. * The smp_mb() is to ensure that our setting of hwthread_req
  1750. * is visible before we look at hwthread_state, so if this
  1751. * races with the code at system_reset_pSeries and the thread
  1752. * misses our setting of hwthread_req, we are sure to see its
  1753. * setting of hwthread_state, and vice versa.
  1754. */
  1755. smp_mb();
  1756. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  1757. if (--timeout <= 0) {
  1758. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  1759. return -EBUSY;
  1760. }
  1761. udelay(1);
  1762. }
  1763. return 0;
  1764. }
  1765. static void kvmppc_release_hwthread(int cpu)
  1766. {
  1767. struct paca_struct *tpaca;
  1768. tpaca = &paca[cpu];
  1769. tpaca->kvm_hstate.hwthread_req = 0;
  1770. tpaca->kvm_hstate.kvm_vcpu = NULL;
  1771. tpaca->kvm_hstate.kvm_vcore = NULL;
  1772. tpaca->kvm_hstate.kvm_split_mode = NULL;
  1773. }
  1774. static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
  1775. {
  1776. int cpu;
  1777. struct paca_struct *tpaca;
  1778. struct kvmppc_vcore *mvc = vc->master_vcore;
  1779. cpu = vc->pcpu;
  1780. if (vcpu) {
  1781. if (vcpu->arch.timer_running) {
  1782. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  1783. vcpu->arch.timer_running = 0;
  1784. }
  1785. cpu += vcpu->arch.ptid;
  1786. vcpu->cpu = mvc->pcpu;
  1787. vcpu->arch.thread_cpu = cpu;
  1788. }
  1789. tpaca = &paca[cpu];
  1790. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  1791. tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
  1792. /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
  1793. smp_wmb();
  1794. tpaca->kvm_hstate.kvm_vcore = mvc;
  1795. if (cpu != smp_processor_id())
  1796. kvmppc_ipi_thread(cpu);
  1797. }
  1798. static void kvmppc_wait_for_nap(void)
  1799. {
  1800. int cpu = smp_processor_id();
  1801. int i, loops;
  1802. int n_threads = threads_per_vcore();
  1803. if (n_threads <= 1)
  1804. return;
  1805. for (loops = 0; loops < 1000000; ++loops) {
  1806. /*
  1807. * Check if all threads are finished.
  1808. * We set the vcore pointer when starting a thread
  1809. * and the thread clears it when finished, so we look
  1810. * for any threads that still have a non-NULL vcore ptr.
  1811. */
  1812. for (i = 1; i < n_threads; ++i)
  1813. if (paca[cpu + i].kvm_hstate.kvm_vcore)
  1814. break;
  1815. if (i == n_threads) {
  1816. HMT_medium();
  1817. return;
  1818. }
  1819. HMT_low();
  1820. }
  1821. HMT_medium();
  1822. for (i = 1; i < n_threads; ++i)
  1823. if (paca[cpu + i].kvm_hstate.kvm_vcore)
  1824. pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
  1825. }
  1826. /*
  1827. * Check that we are on thread 0 and that any other threads in
  1828. * this core are off-line. Then grab the threads so they can't
  1829. * enter the kernel.
  1830. */
  1831. static int on_primary_thread(void)
  1832. {
  1833. int cpu = smp_processor_id();
  1834. int thr;
  1835. /* Are we on a primary subcore? */
  1836. if (cpu_thread_in_subcore(cpu))
  1837. return 0;
  1838. thr = 0;
  1839. while (++thr < threads_per_subcore)
  1840. if (cpu_online(cpu + thr))
  1841. return 0;
  1842. /* Grab all hw threads so they can't go into the kernel */
  1843. for (thr = 1; thr < threads_per_subcore; ++thr) {
  1844. if (kvmppc_grab_hwthread(cpu + thr)) {
  1845. /* Couldn't grab one; let the others go */
  1846. do {
  1847. kvmppc_release_hwthread(cpu + thr);
  1848. } while (--thr > 0);
  1849. return 0;
  1850. }
  1851. }
  1852. return 1;
  1853. }
  1854. /*
  1855. * A list of virtual cores for each physical CPU.
  1856. * These are vcores that could run but their runner VCPU tasks are
  1857. * (or may be) preempted.
  1858. */
  1859. struct preempted_vcore_list {
  1860. struct list_head list;
  1861. spinlock_t lock;
  1862. };
  1863. static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
  1864. static void init_vcore_lists(void)
  1865. {
  1866. int cpu;
  1867. for_each_possible_cpu(cpu) {
  1868. struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
  1869. spin_lock_init(&lp->lock);
  1870. INIT_LIST_HEAD(&lp->list);
  1871. }
  1872. }
  1873. static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
  1874. {
  1875. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  1876. vc->vcore_state = VCORE_PREEMPT;
  1877. vc->pcpu = smp_processor_id();
  1878. if (vc->num_threads < threads_per_vcore()) {
  1879. spin_lock(&lp->lock);
  1880. list_add_tail(&vc->preempt_list, &lp->list);
  1881. spin_unlock(&lp->lock);
  1882. }
  1883. /* Start accumulating stolen time */
  1884. kvmppc_core_start_stolen(vc);
  1885. }
  1886. static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
  1887. {
  1888. struct preempted_vcore_list *lp;
  1889. kvmppc_core_end_stolen(vc);
  1890. if (!list_empty(&vc->preempt_list)) {
  1891. lp = &per_cpu(preempted_vcores, vc->pcpu);
  1892. spin_lock(&lp->lock);
  1893. list_del_init(&vc->preempt_list);
  1894. spin_unlock(&lp->lock);
  1895. }
  1896. vc->vcore_state = VCORE_INACTIVE;
  1897. }
  1898. /*
  1899. * This stores information about the virtual cores currently
  1900. * assigned to a physical core.
  1901. */
  1902. struct core_info {
  1903. int n_subcores;
  1904. int max_subcore_threads;
  1905. int total_threads;
  1906. int subcore_threads[MAX_SUBCORES];
  1907. struct kvm *subcore_vm[MAX_SUBCORES];
  1908. struct list_head vcs[MAX_SUBCORES];
  1909. };
  1910. /*
  1911. * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
  1912. * respectively in 2-way micro-threading (split-core) mode.
  1913. */
  1914. static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
  1915. static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
  1916. {
  1917. int sub;
  1918. memset(cip, 0, sizeof(*cip));
  1919. cip->n_subcores = 1;
  1920. cip->max_subcore_threads = vc->num_threads;
  1921. cip->total_threads = vc->num_threads;
  1922. cip->subcore_threads[0] = vc->num_threads;
  1923. cip->subcore_vm[0] = vc->kvm;
  1924. for (sub = 0; sub < MAX_SUBCORES; ++sub)
  1925. INIT_LIST_HEAD(&cip->vcs[sub]);
  1926. list_add_tail(&vc->preempt_list, &cip->vcs[0]);
  1927. }
  1928. static bool subcore_config_ok(int n_subcores, int n_threads)
  1929. {
  1930. /* Can only dynamically split if unsplit to begin with */
  1931. if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
  1932. return false;
  1933. if (n_subcores > MAX_SUBCORES)
  1934. return false;
  1935. if (n_subcores > 1) {
  1936. if (!(dynamic_mt_modes & 2))
  1937. n_subcores = 4;
  1938. if (n_subcores > 2 && !(dynamic_mt_modes & 4))
  1939. return false;
  1940. }
  1941. return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
  1942. }
  1943. static void init_master_vcore(struct kvmppc_vcore *vc)
  1944. {
  1945. vc->master_vcore = vc;
  1946. vc->entry_exit_map = 0;
  1947. vc->in_guest = 0;
  1948. vc->napping_threads = 0;
  1949. vc->conferring_threads = 0;
  1950. }
  1951. static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
  1952. {
  1953. int n_threads = vc->num_threads;
  1954. int sub;
  1955. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1956. return false;
  1957. if (n_threads < cip->max_subcore_threads)
  1958. n_threads = cip->max_subcore_threads;
  1959. if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
  1960. return false;
  1961. cip->max_subcore_threads = n_threads;
  1962. sub = cip->n_subcores;
  1963. ++cip->n_subcores;
  1964. cip->total_threads += vc->num_threads;
  1965. cip->subcore_threads[sub] = vc->num_threads;
  1966. cip->subcore_vm[sub] = vc->kvm;
  1967. init_master_vcore(vc);
  1968. list_move_tail(&vc->preempt_list, &cip->vcs[sub]);
  1969. return true;
  1970. }
  1971. /*
  1972. * Work out whether it is possible to piggyback the execution of
  1973. * vcore *pvc onto the execution of the other vcores described in *cip.
  1974. */
  1975. static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
  1976. int target_threads)
  1977. {
  1978. if (cip->total_threads + pvc->num_threads > target_threads)
  1979. return false;
  1980. return can_dynamic_split(pvc, cip);
  1981. }
  1982. static void prepare_threads(struct kvmppc_vcore *vc)
  1983. {
  1984. int i;
  1985. struct kvm_vcpu *vcpu;
  1986. for_each_runnable_thread(i, vcpu, vc) {
  1987. if (signal_pending(vcpu->arch.run_task))
  1988. vcpu->arch.ret = -EINTR;
  1989. else if (vcpu->arch.vpa.update_pending ||
  1990. vcpu->arch.slb_shadow.update_pending ||
  1991. vcpu->arch.dtl.update_pending)
  1992. vcpu->arch.ret = RESUME_GUEST;
  1993. else
  1994. continue;
  1995. kvmppc_remove_runnable(vc, vcpu);
  1996. wake_up(&vcpu->arch.cpu_run);
  1997. }
  1998. }
  1999. static void collect_piggybacks(struct core_info *cip, int target_threads)
  2000. {
  2001. struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
  2002. struct kvmppc_vcore *pvc, *vcnext;
  2003. spin_lock(&lp->lock);
  2004. list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
  2005. if (!spin_trylock(&pvc->lock))
  2006. continue;
  2007. prepare_threads(pvc);
  2008. if (!pvc->n_runnable) {
  2009. list_del_init(&pvc->preempt_list);
  2010. if (pvc->runner == NULL) {
  2011. pvc->vcore_state = VCORE_INACTIVE;
  2012. kvmppc_core_end_stolen(pvc);
  2013. }
  2014. spin_unlock(&pvc->lock);
  2015. continue;
  2016. }
  2017. if (!can_piggyback(pvc, cip, target_threads)) {
  2018. spin_unlock(&pvc->lock);
  2019. continue;
  2020. }
  2021. kvmppc_core_end_stolen(pvc);
  2022. pvc->vcore_state = VCORE_PIGGYBACK;
  2023. if (cip->total_threads >= target_threads)
  2024. break;
  2025. }
  2026. spin_unlock(&lp->lock);
  2027. }
  2028. static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
  2029. {
  2030. int still_running = 0, i;
  2031. u64 now;
  2032. long ret;
  2033. struct kvm_vcpu *vcpu;
  2034. spin_lock(&vc->lock);
  2035. now = get_tb();
  2036. for_each_runnable_thread(i, vcpu, vc) {
  2037. /* cancel pending dec exception if dec is positive */
  2038. if (now < vcpu->arch.dec_expires &&
  2039. kvmppc_core_pending_dec(vcpu))
  2040. kvmppc_core_dequeue_dec(vcpu);
  2041. trace_kvm_guest_exit(vcpu);
  2042. ret = RESUME_GUEST;
  2043. if (vcpu->arch.trap)
  2044. ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
  2045. vcpu->arch.run_task);
  2046. vcpu->arch.ret = ret;
  2047. vcpu->arch.trap = 0;
  2048. if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
  2049. if (vcpu->arch.pending_exceptions)
  2050. kvmppc_core_prepare_to_enter(vcpu);
  2051. if (vcpu->arch.ceded)
  2052. kvmppc_set_timer(vcpu);
  2053. else
  2054. ++still_running;
  2055. } else {
  2056. kvmppc_remove_runnable(vc, vcpu);
  2057. wake_up(&vcpu->arch.cpu_run);
  2058. }
  2059. }
  2060. list_del_init(&vc->preempt_list);
  2061. if (!is_master) {
  2062. if (still_running > 0) {
  2063. kvmppc_vcore_preempt(vc);
  2064. } else if (vc->runner) {
  2065. vc->vcore_state = VCORE_PREEMPT;
  2066. kvmppc_core_start_stolen(vc);
  2067. } else {
  2068. vc->vcore_state = VCORE_INACTIVE;
  2069. }
  2070. if (vc->n_runnable > 0 && vc->runner == NULL) {
  2071. /* make sure there's a candidate runner awake */
  2072. i = -1;
  2073. vcpu = next_runnable_thread(vc, &i);
  2074. wake_up(&vcpu->arch.cpu_run);
  2075. }
  2076. }
  2077. spin_unlock(&vc->lock);
  2078. }
  2079. /*
  2080. * Clear core from the list of active host cores as we are about to
  2081. * enter the guest. Only do this if it is the primary thread of the
  2082. * core (not if a subcore) that is entering the guest.
  2083. */
  2084. static inline int kvmppc_clear_host_core(unsigned int cpu)
  2085. {
  2086. int core;
  2087. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2088. return 0;
  2089. /*
  2090. * Memory barrier can be omitted here as we will do a smp_wmb()
  2091. * later in kvmppc_start_thread and we need ensure that state is
  2092. * visible to other CPUs only after we enter guest.
  2093. */
  2094. core = cpu >> threads_shift;
  2095. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
  2096. return 0;
  2097. }
  2098. /*
  2099. * Advertise this core as an active host core since we exited the guest
  2100. * Only need to do this if it is the primary thread of the core that is
  2101. * exiting.
  2102. */
  2103. static inline int kvmppc_set_host_core(unsigned int cpu)
  2104. {
  2105. int core;
  2106. if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
  2107. return 0;
  2108. /*
  2109. * Memory barrier can be omitted here because we do a spin_unlock
  2110. * immediately after this which provides the memory barrier.
  2111. */
  2112. core = cpu >> threads_shift;
  2113. kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
  2114. return 0;
  2115. }
  2116. /*
  2117. * Run a set of guest threads on a physical core.
  2118. * Called with vc->lock held.
  2119. */
  2120. static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
  2121. {
  2122. struct kvm_vcpu *vcpu;
  2123. int i;
  2124. int srcu_idx;
  2125. struct core_info core_info;
  2126. struct kvmppc_vcore *pvc, *vcnext;
  2127. struct kvm_split_mode split_info, *sip;
  2128. int split, subcore_size, active;
  2129. int sub;
  2130. bool thr0_done;
  2131. unsigned long cmd_bit, stat_bit;
  2132. int pcpu, thr;
  2133. int target_threads;
  2134. int controlled_threads;
  2135. /*
  2136. * Remove from the list any threads that have a signal pending
  2137. * or need a VPA update done
  2138. */
  2139. prepare_threads(vc);
  2140. /* if the runner is no longer runnable, let the caller pick a new one */
  2141. if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
  2142. return;
  2143. /*
  2144. * Initialize *vc.
  2145. */
  2146. init_master_vcore(vc);
  2147. vc->preempt_tb = TB_NIL;
  2148. /*
  2149. * Number of threads that we will be controlling: the same as
  2150. * the number of threads per subcore, except on POWER9,
  2151. * where it's 1 because the threads are (mostly) independent.
  2152. */
  2153. controlled_threads = threads_per_vcore();
  2154. /*
  2155. * Make sure we are running on primary threads, and that secondary
  2156. * threads are offline. Also check if the number of threads in this
  2157. * guest are greater than the current system threads per guest.
  2158. */
  2159. if ((controlled_threads > 1) &&
  2160. ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
  2161. for_each_runnable_thread(i, vcpu, vc) {
  2162. vcpu->arch.ret = -EBUSY;
  2163. kvmppc_remove_runnable(vc, vcpu);
  2164. wake_up(&vcpu->arch.cpu_run);
  2165. }
  2166. goto out;
  2167. }
  2168. /*
  2169. * See if we could run any other vcores on the physical core
  2170. * along with this one.
  2171. */
  2172. init_core_info(&core_info, vc);
  2173. pcpu = smp_processor_id();
  2174. target_threads = controlled_threads;
  2175. if (target_smt_mode && target_smt_mode < target_threads)
  2176. target_threads = target_smt_mode;
  2177. if (vc->num_threads < target_threads)
  2178. collect_piggybacks(&core_info, target_threads);
  2179. /* Decide on micro-threading (split-core) mode */
  2180. subcore_size = threads_per_subcore;
  2181. cmd_bit = stat_bit = 0;
  2182. split = core_info.n_subcores;
  2183. sip = NULL;
  2184. if (split > 1) {
  2185. /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
  2186. if (split == 2 && (dynamic_mt_modes & 2)) {
  2187. cmd_bit = HID0_POWER8_1TO2LPAR;
  2188. stat_bit = HID0_POWER8_2LPARMODE;
  2189. } else {
  2190. split = 4;
  2191. cmd_bit = HID0_POWER8_1TO4LPAR;
  2192. stat_bit = HID0_POWER8_4LPARMODE;
  2193. }
  2194. subcore_size = MAX_SMT_THREADS / split;
  2195. sip = &split_info;
  2196. memset(&split_info, 0, sizeof(split_info));
  2197. split_info.rpr = mfspr(SPRN_RPR);
  2198. split_info.pmmar = mfspr(SPRN_PMMAR);
  2199. split_info.ldbar = mfspr(SPRN_LDBAR);
  2200. split_info.subcore_size = subcore_size;
  2201. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2202. split_info.master_vcs[sub] =
  2203. list_first_entry(&core_info.vcs[sub],
  2204. struct kvmppc_vcore, preempt_list);
  2205. /* order writes to split_info before kvm_split_mode pointer */
  2206. smp_wmb();
  2207. }
  2208. pcpu = smp_processor_id();
  2209. for (thr = 0; thr < controlled_threads; ++thr)
  2210. paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
  2211. /* Initiate micro-threading (split-core) if required */
  2212. if (cmd_bit) {
  2213. unsigned long hid0 = mfspr(SPRN_HID0);
  2214. hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
  2215. mb();
  2216. mtspr(SPRN_HID0, hid0);
  2217. isync();
  2218. for (;;) {
  2219. hid0 = mfspr(SPRN_HID0);
  2220. if (hid0 & stat_bit)
  2221. break;
  2222. cpu_relax();
  2223. }
  2224. }
  2225. kvmppc_clear_host_core(pcpu);
  2226. /* Start all the threads */
  2227. active = 0;
  2228. for (sub = 0; sub < core_info.n_subcores; ++sub) {
  2229. thr = subcore_thread_map[sub];
  2230. thr0_done = false;
  2231. active |= 1 << thr;
  2232. list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
  2233. pvc->pcpu = pcpu + thr;
  2234. for_each_runnable_thread(i, vcpu, pvc) {
  2235. kvmppc_start_thread(vcpu, pvc);
  2236. kvmppc_create_dtl_entry(vcpu, pvc);
  2237. trace_kvm_guest_enter(vcpu);
  2238. if (!vcpu->arch.ptid)
  2239. thr0_done = true;
  2240. active |= 1 << (thr + vcpu->arch.ptid);
  2241. }
  2242. /*
  2243. * We need to start the first thread of each subcore
  2244. * even if it doesn't have a vcpu.
  2245. */
  2246. if (pvc->master_vcore == pvc && !thr0_done)
  2247. kvmppc_start_thread(NULL, pvc);
  2248. thr += pvc->num_threads;
  2249. }
  2250. }
  2251. /*
  2252. * Ensure that split_info.do_nap is set after setting
  2253. * the vcore pointer in the PACA of the secondaries.
  2254. */
  2255. smp_mb();
  2256. if (cmd_bit)
  2257. split_info.do_nap = 1; /* ask secondaries to nap when done */
  2258. /*
  2259. * When doing micro-threading, poke the inactive threads as well.
  2260. * This gets them to the nap instruction after kvm_do_nap,
  2261. * which reduces the time taken to unsplit later.
  2262. */
  2263. if (split > 1)
  2264. for (thr = 1; thr < threads_per_subcore; ++thr)
  2265. if (!(active & (1 << thr)))
  2266. kvmppc_ipi_thread(pcpu + thr);
  2267. vc->vcore_state = VCORE_RUNNING;
  2268. preempt_disable();
  2269. trace_kvmppc_run_core(vc, 0);
  2270. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2271. list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
  2272. spin_unlock(&pvc->lock);
  2273. guest_enter();
  2274. srcu_idx = srcu_read_lock(&vc->kvm->srcu);
  2275. __kvmppc_vcore_entry();
  2276. srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
  2277. spin_lock(&vc->lock);
  2278. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  2279. vc->vcore_state = VCORE_EXITING;
  2280. /* wait for secondary threads to finish writing their state to memory */
  2281. kvmppc_wait_for_nap();
  2282. /* Return to whole-core mode if we split the core earlier */
  2283. if (split > 1) {
  2284. unsigned long hid0 = mfspr(SPRN_HID0);
  2285. unsigned long loops = 0;
  2286. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  2287. stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  2288. mb();
  2289. mtspr(SPRN_HID0, hid0);
  2290. isync();
  2291. for (;;) {
  2292. hid0 = mfspr(SPRN_HID0);
  2293. if (!(hid0 & stat_bit))
  2294. break;
  2295. cpu_relax();
  2296. ++loops;
  2297. }
  2298. split_info.do_nap = 0;
  2299. }
  2300. /* Let secondaries go back to the offline loop */
  2301. for (i = 0; i < controlled_threads; ++i) {
  2302. kvmppc_release_hwthread(pcpu + i);
  2303. if (sip && sip->napped[i])
  2304. kvmppc_ipi_thread(pcpu + i);
  2305. }
  2306. kvmppc_set_host_core(pcpu);
  2307. spin_unlock(&vc->lock);
  2308. /* make sure updates to secondary vcpu structs are visible now */
  2309. smp_mb();
  2310. guest_exit();
  2311. for (sub = 0; sub < core_info.n_subcores; ++sub)
  2312. list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
  2313. preempt_list)
  2314. post_guest_process(pvc, pvc == vc);
  2315. spin_lock(&vc->lock);
  2316. preempt_enable();
  2317. out:
  2318. vc->vcore_state = VCORE_INACTIVE;
  2319. trace_kvmppc_run_core(vc, 1);
  2320. }
  2321. /*
  2322. * Wait for some other vcpu thread to execute us, and
  2323. * wake us up when we need to handle something in the host.
  2324. */
  2325. static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
  2326. struct kvm_vcpu *vcpu, int wait_state)
  2327. {
  2328. DEFINE_WAIT(wait);
  2329. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  2330. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  2331. spin_unlock(&vc->lock);
  2332. schedule();
  2333. spin_lock(&vc->lock);
  2334. }
  2335. finish_wait(&vcpu->arch.cpu_run, &wait);
  2336. }
  2337. static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
  2338. {
  2339. /* 10us base */
  2340. if (vc->halt_poll_ns == 0 && halt_poll_ns_grow)
  2341. vc->halt_poll_ns = 10000;
  2342. else
  2343. vc->halt_poll_ns *= halt_poll_ns_grow;
  2344. }
  2345. static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
  2346. {
  2347. if (halt_poll_ns_shrink == 0)
  2348. vc->halt_poll_ns = 0;
  2349. else
  2350. vc->halt_poll_ns /= halt_poll_ns_shrink;
  2351. }
  2352. /*
  2353. * Check to see if any of the runnable vcpus on the vcore have pending
  2354. * exceptions or are no longer ceded
  2355. */
  2356. static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  2357. {
  2358. struct kvm_vcpu *vcpu;
  2359. int i;
  2360. for_each_runnable_thread(i, vcpu, vc) {
  2361. if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded)
  2362. return 1;
  2363. }
  2364. return 0;
  2365. }
  2366. /*
  2367. * All the vcpus in this vcore are idle, so wait for a decrementer
  2368. * or external interrupt to one of the vcpus. vc->lock is held.
  2369. */
  2370. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  2371. {
  2372. ktime_t cur, start_poll, start_wait;
  2373. int do_sleep = 1;
  2374. u64 block_ns;
  2375. DECLARE_SWAITQUEUE(wait);
  2376. /* Poll for pending exceptions and ceded state */
  2377. cur = start_poll = ktime_get();
  2378. if (vc->halt_poll_ns) {
  2379. ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
  2380. ++vc->runner->stat.halt_attempted_poll;
  2381. vc->vcore_state = VCORE_POLLING;
  2382. spin_unlock(&vc->lock);
  2383. do {
  2384. if (kvmppc_vcore_check_block(vc)) {
  2385. do_sleep = 0;
  2386. break;
  2387. }
  2388. cur = ktime_get();
  2389. } while (single_task_running() && ktime_before(cur, stop));
  2390. spin_lock(&vc->lock);
  2391. vc->vcore_state = VCORE_INACTIVE;
  2392. if (!do_sleep) {
  2393. ++vc->runner->stat.halt_successful_poll;
  2394. goto out;
  2395. }
  2396. }
  2397. prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  2398. if (kvmppc_vcore_check_block(vc)) {
  2399. finish_swait(&vc->wq, &wait);
  2400. do_sleep = 0;
  2401. /* If we polled, count this as a successful poll */
  2402. if (vc->halt_poll_ns)
  2403. ++vc->runner->stat.halt_successful_poll;
  2404. goto out;
  2405. }
  2406. start_wait = ktime_get();
  2407. vc->vcore_state = VCORE_SLEEPING;
  2408. trace_kvmppc_vcore_blocked(vc, 0);
  2409. spin_unlock(&vc->lock);
  2410. schedule();
  2411. finish_swait(&vc->wq, &wait);
  2412. spin_lock(&vc->lock);
  2413. vc->vcore_state = VCORE_INACTIVE;
  2414. trace_kvmppc_vcore_blocked(vc, 1);
  2415. ++vc->runner->stat.halt_successful_wait;
  2416. cur = ktime_get();
  2417. out:
  2418. block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
  2419. /* Attribute wait time */
  2420. if (do_sleep) {
  2421. vc->runner->stat.halt_wait_ns +=
  2422. ktime_to_ns(cur) - ktime_to_ns(start_wait);
  2423. /* Attribute failed poll time */
  2424. if (vc->halt_poll_ns)
  2425. vc->runner->stat.halt_poll_fail_ns +=
  2426. ktime_to_ns(start_wait) -
  2427. ktime_to_ns(start_poll);
  2428. } else {
  2429. /* Attribute successful poll time */
  2430. if (vc->halt_poll_ns)
  2431. vc->runner->stat.halt_poll_success_ns +=
  2432. ktime_to_ns(cur) -
  2433. ktime_to_ns(start_poll);
  2434. }
  2435. /* Adjust poll time */
  2436. if (halt_poll_ns) {
  2437. if (block_ns <= vc->halt_poll_ns)
  2438. ;
  2439. /* We slept and blocked for longer than the max halt time */
  2440. else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
  2441. shrink_halt_poll_ns(vc);
  2442. /* We slept and our poll time is too small */
  2443. else if (vc->halt_poll_ns < halt_poll_ns &&
  2444. block_ns < halt_poll_ns)
  2445. grow_halt_poll_ns(vc);
  2446. if (vc->halt_poll_ns > halt_poll_ns)
  2447. vc->halt_poll_ns = halt_poll_ns;
  2448. } else
  2449. vc->halt_poll_ns = 0;
  2450. trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
  2451. }
  2452. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  2453. {
  2454. int n_ceded, i;
  2455. struct kvmppc_vcore *vc;
  2456. struct kvm_vcpu *v;
  2457. trace_kvmppc_run_vcpu_enter(vcpu);
  2458. kvm_run->exit_reason = 0;
  2459. vcpu->arch.ret = RESUME_GUEST;
  2460. vcpu->arch.trap = 0;
  2461. kvmppc_update_vpas(vcpu);
  2462. /*
  2463. * Synchronize with other threads in this virtual core
  2464. */
  2465. vc = vcpu->arch.vcore;
  2466. spin_lock(&vc->lock);
  2467. vcpu->arch.ceded = 0;
  2468. vcpu->arch.run_task = current;
  2469. vcpu->arch.kvm_run = kvm_run;
  2470. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  2471. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  2472. vcpu->arch.busy_preempt = TB_NIL;
  2473. WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
  2474. ++vc->n_runnable;
  2475. /*
  2476. * This happens the first time this is called for a vcpu.
  2477. * If the vcore is already running, we may be able to start
  2478. * this thread straight away and have it join in.
  2479. */
  2480. if (!signal_pending(current)) {
  2481. if (vc->vcore_state == VCORE_PIGGYBACK) {
  2482. struct kvmppc_vcore *mvc = vc->master_vcore;
  2483. if (spin_trylock(&mvc->lock)) {
  2484. if (mvc->vcore_state == VCORE_RUNNING &&
  2485. !VCORE_IS_EXITING(mvc)) {
  2486. kvmppc_create_dtl_entry(vcpu, vc);
  2487. kvmppc_start_thread(vcpu, vc);
  2488. trace_kvm_guest_enter(vcpu);
  2489. }
  2490. spin_unlock(&mvc->lock);
  2491. }
  2492. } else if (vc->vcore_state == VCORE_RUNNING &&
  2493. !VCORE_IS_EXITING(vc)) {
  2494. kvmppc_create_dtl_entry(vcpu, vc);
  2495. kvmppc_start_thread(vcpu, vc);
  2496. trace_kvm_guest_enter(vcpu);
  2497. } else if (vc->vcore_state == VCORE_SLEEPING) {
  2498. swake_up(&vc->wq);
  2499. }
  2500. }
  2501. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  2502. !signal_pending(current)) {
  2503. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  2504. kvmppc_vcore_end_preempt(vc);
  2505. if (vc->vcore_state != VCORE_INACTIVE) {
  2506. kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
  2507. continue;
  2508. }
  2509. for_each_runnable_thread(i, v, vc) {
  2510. kvmppc_core_prepare_to_enter(v);
  2511. if (signal_pending(v->arch.run_task)) {
  2512. kvmppc_remove_runnable(vc, v);
  2513. v->stat.signal_exits++;
  2514. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  2515. v->arch.ret = -EINTR;
  2516. wake_up(&v->arch.cpu_run);
  2517. }
  2518. }
  2519. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  2520. break;
  2521. n_ceded = 0;
  2522. for_each_runnable_thread(i, v, vc) {
  2523. if (!v->arch.pending_exceptions)
  2524. n_ceded += v->arch.ceded;
  2525. else
  2526. v->arch.ceded = 0;
  2527. }
  2528. vc->runner = vcpu;
  2529. if (n_ceded == vc->n_runnable) {
  2530. kvmppc_vcore_blocked(vc);
  2531. } else if (need_resched()) {
  2532. kvmppc_vcore_preempt(vc);
  2533. /* Let something else run */
  2534. cond_resched_lock(&vc->lock);
  2535. if (vc->vcore_state == VCORE_PREEMPT)
  2536. kvmppc_vcore_end_preempt(vc);
  2537. } else {
  2538. kvmppc_run_core(vc);
  2539. }
  2540. vc->runner = NULL;
  2541. }
  2542. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  2543. (vc->vcore_state == VCORE_RUNNING ||
  2544. vc->vcore_state == VCORE_EXITING ||
  2545. vc->vcore_state == VCORE_PIGGYBACK))
  2546. kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
  2547. if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
  2548. kvmppc_vcore_end_preempt(vc);
  2549. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  2550. kvmppc_remove_runnable(vc, vcpu);
  2551. vcpu->stat.signal_exits++;
  2552. kvm_run->exit_reason = KVM_EXIT_INTR;
  2553. vcpu->arch.ret = -EINTR;
  2554. }
  2555. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  2556. /* Wake up some vcpu to run the core */
  2557. i = -1;
  2558. v = next_runnable_thread(vc, &i);
  2559. wake_up(&v->arch.cpu_run);
  2560. }
  2561. trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
  2562. spin_unlock(&vc->lock);
  2563. return vcpu->arch.ret;
  2564. }
  2565. static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
  2566. {
  2567. int r;
  2568. int srcu_idx;
  2569. if (!vcpu->arch.sane) {
  2570. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2571. return -EINVAL;
  2572. }
  2573. kvmppc_core_prepare_to_enter(vcpu);
  2574. /* No need to go into the guest when all we'll do is come back out */
  2575. if (signal_pending(current)) {
  2576. run->exit_reason = KVM_EXIT_INTR;
  2577. return -EINTR;
  2578. }
  2579. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  2580. /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
  2581. smp_mb();
  2582. /* On the first time here, set up HTAB and VRMA */
  2583. if (!vcpu->kvm->arch.hpte_setup_done) {
  2584. r = kvmppc_hv_setup_htab_rma(vcpu);
  2585. if (r)
  2586. goto out;
  2587. }
  2588. flush_all_to_thread(current);
  2589. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  2590. vcpu->arch.pgdir = current->mm->pgd;
  2591. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  2592. do {
  2593. r = kvmppc_run_vcpu(run, vcpu);
  2594. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  2595. !(vcpu->arch.shregs.msr & MSR_PR)) {
  2596. trace_kvm_hcall_enter(vcpu);
  2597. r = kvmppc_pseries_do_hcall(vcpu);
  2598. trace_kvm_hcall_exit(vcpu, r);
  2599. kvmppc_core_prepare_to_enter(vcpu);
  2600. } else if (r == RESUME_PAGE_FAULT) {
  2601. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  2602. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  2603. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  2604. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  2605. } else if (r == RESUME_PASSTHROUGH)
  2606. r = kvmppc_xics_rm_complete(vcpu, 0);
  2607. } while (is_kvmppc_resume_guest(r));
  2608. out:
  2609. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  2610. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  2611. return r;
  2612. }
  2613. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  2614. int linux_psize)
  2615. {
  2616. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  2617. if (!def->shift)
  2618. return;
  2619. (*sps)->page_shift = def->shift;
  2620. (*sps)->slb_enc = def->sllp;
  2621. (*sps)->enc[0].page_shift = def->shift;
  2622. (*sps)->enc[0].pte_enc = def->penc[linux_psize];
  2623. /*
  2624. * Add 16MB MPSS support if host supports it
  2625. */
  2626. if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
  2627. (*sps)->enc[1].page_shift = 24;
  2628. (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
  2629. }
  2630. (*sps)++;
  2631. }
  2632. static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
  2633. struct kvm_ppc_smmu_info *info)
  2634. {
  2635. struct kvm_ppc_one_seg_page_size *sps;
  2636. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  2637. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  2638. info->flags |= KVM_PPC_1T_SEGMENTS;
  2639. info->slb_size = mmu_slb_size;
  2640. /* We only support these sizes for now, and no muti-size segments */
  2641. sps = &info->sps[0];
  2642. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  2643. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  2644. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  2645. return 0;
  2646. }
  2647. /*
  2648. * Get (and clear) the dirty memory log for a memory slot.
  2649. */
  2650. static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
  2651. struct kvm_dirty_log *log)
  2652. {
  2653. struct kvm_memslots *slots;
  2654. struct kvm_memory_slot *memslot;
  2655. int r;
  2656. unsigned long n;
  2657. mutex_lock(&kvm->slots_lock);
  2658. r = -EINVAL;
  2659. if (log->slot >= KVM_USER_MEM_SLOTS)
  2660. goto out;
  2661. slots = kvm_memslots(kvm);
  2662. memslot = id_to_memslot(slots, log->slot);
  2663. r = -ENOENT;
  2664. if (!memslot->dirty_bitmap)
  2665. goto out;
  2666. n = kvm_dirty_bitmap_bytes(memslot);
  2667. memset(memslot->dirty_bitmap, 0, n);
  2668. r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
  2669. if (r)
  2670. goto out;
  2671. r = -EFAULT;
  2672. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  2673. goto out;
  2674. r = 0;
  2675. out:
  2676. mutex_unlock(&kvm->slots_lock);
  2677. return r;
  2678. }
  2679. static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
  2680. struct kvm_memory_slot *dont)
  2681. {
  2682. if (!dont || free->arch.rmap != dont->arch.rmap) {
  2683. vfree(free->arch.rmap);
  2684. free->arch.rmap = NULL;
  2685. }
  2686. }
  2687. static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
  2688. unsigned long npages)
  2689. {
  2690. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  2691. if (!slot->arch.rmap)
  2692. return -ENOMEM;
  2693. return 0;
  2694. }
  2695. static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
  2696. struct kvm_memory_slot *memslot,
  2697. const struct kvm_userspace_memory_region *mem)
  2698. {
  2699. return 0;
  2700. }
  2701. static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
  2702. const struct kvm_userspace_memory_region *mem,
  2703. const struct kvm_memory_slot *old,
  2704. const struct kvm_memory_slot *new)
  2705. {
  2706. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  2707. struct kvm_memslots *slots;
  2708. struct kvm_memory_slot *memslot;
  2709. /*
  2710. * If we are making a new memslot, it might make
  2711. * some address that was previously cached as emulated
  2712. * MMIO be no longer emulated MMIO, so invalidate
  2713. * all the caches of emulated MMIO translations.
  2714. */
  2715. if (npages)
  2716. atomic64_inc(&kvm->arch.mmio_update);
  2717. if (npages && old->npages) {
  2718. /*
  2719. * If modifying a memslot, reset all the rmap dirty bits.
  2720. * If this is a new memslot, we don't need to do anything
  2721. * since the rmap array starts out as all zeroes,
  2722. * i.e. no pages are dirty.
  2723. */
  2724. slots = kvm_memslots(kvm);
  2725. memslot = id_to_memslot(slots, mem->slot);
  2726. kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
  2727. }
  2728. }
  2729. /*
  2730. * Update LPCR values in kvm->arch and in vcores.
  2731. * Caller must hold kvm->lock.
  2732. */
  2733. void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
  2734. {
  2735. long int i;
  2736. u32 cores_done = 0;
  2737. if ((kvm->arch.lpcr & mask) == lpcr)
  2738. return;
  2739. kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
  2740. for (i = 0; i < KVM_MAX_VCORES; ++i) {
  2741. struct kvmppc_vcore *vc = kvm->arch.vcores[i];
  2742. if (!vc)
  2743. continue;
  2744. spin_lock(&vc->lock);
  2745. vc->lpcr = (vc->lpcr & ~mask) | lpcr;
  2746. spin_unlock(&vc->lock);
  2747. if (++cores_done >= kvm->arch.online_vcores)
  2748. break;
  2749. }
  2750. }
  2751. static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
  2752. {
  2753. return;
  2754. }
  2755. static void kvmppc_setup_partition_table(struct kvm *kvm)
  2756. {
  2757. unsigned long dw0, dw1;
  2758. /* PS field - page size for VRMA */
  2759. dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
  2760. ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
  2761. /* HTABSIZE and HTABORG fields */
  2762. dw0 |= kvm->arch.sdr1;
  2763. /* Second dword has GR=0; other fields are unused since UPRT=0 */
  2764. dw1 = 0;
  2765. mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1);
  2766. }
  2767. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  2768. {
  2769. int err = 0;
  2770. struct kvm *kvm = vcpu->kvm;
  2771. unsigned long hva;
  2772. struct kvm_memory_slot *memslot;
  2773. struct vm_area_struct *vma;
  2774. unsigned long lpcr = 0, senc;
  2775. unsigned long psize, porder;
  2776. int srcu_idx;
  2777. mutex_lock(&kvm->lock);
  2778. if (kvm->arch.hpte_setup_done)
  2779. goto out; /* another vcpu beat us to it */
  2780. /* Allocate hashed page table (if not done already) and reset it */
  2781. if (!kvm->arch.hpt_virt) {
  2782. err = kvmppc_alloc_hpt(kvm, NULL);
  2783. if (err) {
  2784. pr_err("KVM: Couldn't alloc HPT\n");
  2785. goto out;
  2786. }
  2787. }
  2788. /* Look up the memslot for guest physical address 0 */
  2789. srcu_idx = srcu_read_lock(&kvm->srcu);
  2790. memslot = gfn_to_memslot(kvm, 0);
  2791. /* We must have some memory at 0 by now */
  2792. err = -EINVAL;
  2793. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  2794. goto out_srcu;
  2795. /* Look up the VMA for the start of this memory slot */
  2796. hva = memslot->userspace_addr;
  2797. down_read(&current->mm->mmap_sem);
  2798. vma = find_vma(current->mm, hva);
  2799. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  2800. goto up_out;
  2801. psize = vma_kernel_pagesize(vma);
  2802. porder = __ilog2(psize);
  2803. up_read(&current->mm->mmap_sem);
  2804. /* We can handle 4k, 64k or 16M pages in the VRMA */
  2805. err = -EINVAL;
  2806. if (!(psize == 0x1000 || psize == 0x10000 ||
  2807. psize == 0x1000000))
  2808. goto out_srcu;
  2809. senc = slb_pgsize_encoding(psize);
  2810. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  2811. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  2812. /* Create HPTEs in the hash page table for the VRMA */
  2813. kvmppc_map_vrma(vcpu, memslot, porder);
  2814. /* Update VRMASD field in the LPCR */
  2815. if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
  2816. /* the -4 is to account for senc values starting at 0x10 */
  2817. lpcr = senc << (LPCR_VRMASD_SH - 4);
  2818. kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
  2819. } else {
  2820. kvmppc_setup_partition_table(kvm);
  2821. }
  2822. /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
  2823. smp_wmb();
  2824. kvm->arch.hpte_setup_done = 1;
  2825. err = 0;
  2826. out_srcu:
  2827. srcu_read_unlock(&kvm->srcu, srcu_idx);
  2828. out:
  2829. mutex_unlock(&kvm->lock);
  2830. return err;
  2831. up_out:
  2832. up_read(&current->mm->mmap_sem);
  2833. goto out_srcu;
  2834. }
  2835. #ifdef CONFIG_KVM_XICS
  2836. /*
  2837. * Allocate a per-core structure for managing state about which cores are
  2838. * running in the host versus the guest and for exchanging data between
  2839. * real mode KVM and CPU running in the host.
  2840. * This is only done for the first VM.
  2841. * The allocated structure stays even if all VMs have stopped.
  2842. * It is only freed when the kvm-hv module is unloaded.
  2843. * It's OK for this routine to fail, we just don't support host
  2844. * core operations like redirecting H_IPI wakeups.
  2845. */
  2846. void kvmppc_alloc_host_rm_ops(void)
  2847. {
  2848. struct kvmppc_host_rm_ops *ops;
  2849. unsigned long l_ops;
  2850. int cpu, core;
  2851. int size;
  2852. /* Not the first time here ? */
  2853. if (kvmppc_host_rm_ops_hv != NULL)
  2854. return;
  2855. ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
  2856. if (!ops)
  2857. return;
  2858. size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
  2859. ops->rm_core = kzalloc(size, GFP_KERNEL);
  2860. if (!ops->rm_core) {
  2861. kfree(ops);
  2862. return;
  2863. }
  2864. get_online_cpus();
  2865. for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
  2866. if (!cpu_online(cpu))
  2867. continue;
  2868. core = cpu >> threads_shift;
  2869. ops->rm_core[core].rm_state.in_host = 1;
  2870. }
  2871. ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
  2872. /*
  2873. * Make the contents of the kvmppc_host_rm_ops structure visible
  2874. * to other CPUs before we assign it to the global variable.
  2875. * Do an atomic assignment (no locks used here), but if someone
  2876. * beats us to it, just free our copy and return.
  2877. */
  2878. smp_wmb();
  2879. l_ops = (unsigned long) ops;
  2880. if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
  2881. put_online_cpus();
  2882. kfree(ops->rm_core);
  2883. kfree(ops);
  2884. return;
  2885. }
  2886. cpuhp_setup_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE,
  2887. "ppc/kvm_book3s:prepare",
  2888. kvmppc_set_host_core,
  2889. kvmppc_clear_host_core);
  2890. put_online_cpus();
  2891. }
  2892. void kvmppc_free_host_rm_ops(void)
  2893. {
  2894. if (kvmppc_host_rm_ops_hv) {
  2895. cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
  2896. kfree(kvmppc_host_rm_ops_hv->rm_core);
  2897. kfree(kvmppc_host_rm_ops_hv);
  2898. kvmppc_host_rm_ops_hv = NULL;
  2899. }
  2900. }
  2901. #endif
  2902. static int kvmppc_core_init_vm_hv(struct kvm *kvm)
  2903. {
  2904. unsigned long lpcr, lpid;
  2905. char buf[32];
  2906. /* Allocate the guest's logical partition ID */
  2907. lpid = kvmppc_alloc_lpid();
  2908. if ((long)lpid < 0)
  2909. return -ENOMEM;
  2910. kvm->arch.lpid = lpid;
  2911. kvmppc_alloc_host_rm_ops();
  2912. /*
  2913. * Since we don't flush the TLB when tearing down a VM,
  2914. * and this lpid might have previously been used,
  2915. * make sure we flush on each core before running the new VM.
  2916. * On POWER9, the tlbie in mmu_partition_table_set_entry()
  2917. * does this flush for us.
  2918. */
  2919. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  2920. cpumask_setall(&kvm->arch.need_tlb_flush);
  2921. /* Start out with the default set of hcalls enabled */
  2922. memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
  2923. sizeof(kvm->arch.enabled_hcalls));
  2924. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  2925. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  2926. /* Init LPCR for virtual RMA mode */
  2927. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  2928. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  2929. lpcr &= LPCR_PECE | LPCR_LPES;
  2930. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  2931. LPCR_VPM0 | LPCR_VPM1;
  2932. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  2933. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  2934. /* On POWER8 turn on online bit to enable PURR/SPURR */
  2935. if (cpu_has_feature(CPU_FTR_ARCH_207S))
  2936. lpcr |= LPCR_ONL;
  2937. /*
  2938. * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
  2939. * Set HVICE bit to enable hypervisor virtualization interrupts.
  2940. */
  2941. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  2942. lpcr &= ~LPCR_VPM0;
  2943. lpcr |= LPCR_HVICE;
  2944. }
  2945. kvm->arch.lpcr = lpcr;
  2946. /*
  2947. * Work out how many sets the TLB has, for the use of
  2948. * the TLB invalidation loop in book3s_hv_rmhandlers.S.
  2949. */
  2950. if (cpu_has_feature(CPU_FTR_ARCH_300))
  2951. kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
  2952. else if (cpu_has_feature(CPU_FTR_ARCH_207S))
  2953. kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
  2954. else
  2955. kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
  2956. /*
  2957. * Track that we now have a HV mode VM active. This blocks secondary
  2958. * CPU threads from coming online.
  2959. */
  2960. kvm_hv_vm_activated();
  2961. /*
  2962. * Create a debugfs directory for the VM
  2963. */
  2964. snprintf(buf, sizeof(buf), "vm%d", current->pid);
  2965. kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
  2966. if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
  2967. kvmppc_mmu_debugfs_init(kvm);
  2968. return 0;
  2969. }
  2970. static void kvmppc_free_vcores(struct kvm *kvm)
  2971. {
  2972. long int i;
  2973. for (i = 0; i < KVM_MAX_VCORES; ++i)
  2974. kfree(kvm->arch.vcores[i]);
  2975. kvm->arch.online_vcores = 0;
  2976. }
  2977. static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
  2978. {
  2979. debugfs_remove_recursive(kvm->arch.debugfs_dir);
  2980. kvm_hv_vm_deactivated();
  2981. kvmppc_free_vcores(kvm);
  2982. kvmppc_free_hpt(kvm);
  2983. kvmppc_free_pimap(kvm);
  2984. }
  2985. /* We don't need to emulate any privileged instructions or dcbz */
  2986. static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
  2987. unsigned int inst, int *advance)
  2988. {
  2989. return EMULATE_FAIL;
  2990. }
  2991. static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
  2992. ulong spr_val)
  2993. {
  2994. return EMULATE_FAIL;
  2995. }
  2996. static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
  2997. ulong *spr_val)
  2998. {
  2999. return EMULATE_FAIL;
  3000. }
  3001. static int kvmppc_core_check_processor_compat_hv(void)
  3002. {
  3003. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  3004. !cpu_has_feature(CPU_FTR_ARCH_206))
  3005. return -EIO;
  3006. /*
  3007. * Disable KVM for Power9 in radix mode.
  3008. */
  3009. if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
  3010. return -EIO;
  3011. return 0;
  3012. }
  3013. #ifdef CONFIG_KVM_XICS
  3014. void kvmppc_free_pimap(struct kvm *kvm)
  3015. {
  3016. kfree(kvm->arch.pimap);
  3017. }
  3018. static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
  3019. {
  3020. return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
  3021. }
  3022. static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3023. {
  3024. struct irq_desc *desc;
  3025. struct kvmppc_irq_map *irq_map;
  3026. struct kvmppc_passthru_irqmap *pimap;
  3027. struct irq_chip *chip;
  3028. int i;
  3029. if (!kvm_irq_bypass)
  3030. return 1;
  3031. desc = irq_to_desc(host_irq);
  3032. if (!desc)
  3033. return -EIO;
  3034. mutex_lock(&kvm->lock);
  3035. pimap = kvm->arch.pimap;
  3036. if (pimap == NULL) {
  3037. /* First call, allocate structure to hold IRQ map */
  3038. pimap = kvmppc_alloc_pimap();
  3039. if (pimap == NULL) {
  3040. mutex_unlock(&kvm->lock);
  3041. return -ENOMEM;
  3042. }
  3043. kvm->arch.pimap = pimap;
  3044. }
  3045. /*
  3046. * For now, we only support interrupts for which the EOI operation
  3047. * is an OPAL call followed by a write to XIRR, since that's
  3048. * what our real-mode EOI code does.
  3049. */
  3050. chip = irq_data_get_irq_chip(&desc->irq_data);
  3051. if (!chip || !is_pnv_opal_msi(chip)) {
  3052. pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
  3053. host_irq, guest_gsi);
  3054. mutex_unlock(&kvm->lock);
  3055. return -ENOENT;
  3056. }
  3057. /*
  3058. * See if we already have an entry for this guest IRQ number.
  3059. * If it's mapped to a hardware IRQ number, that's an error,
  3060. * otherwise re-use this entry.
  3061. */
  3062. for (i = 0; i < pimap->n_mapped; i++) {
  3063. if (guest_gsi == pimap->mapped[i].v_hwirq) {
  3064. if (pimap->mapped[i].r_hwirq) {
  3065. mutex_unlock(&kvm->lock);
  3066. return -EINVAL;
  3067. }
  3068. break;
  3069. }
  3070. }
  3071. if (i == KVMPPC_PIRQ_MAPPED) {
  3072. mutex_unlock(&kvm->lock);
  3073. return -EAGAIN; /* table is full */
  3074. }
  3075. irq_map = &pimap->mapped[i];
  3076. irq_map->v_hwirq = guest_gsi;
  3077. irq_map->desc = desc;
  3078. /*
  3079. * Order the above two stores before the next to serialize with
  3080. * the KVM real mode handler.
  3081. */
  3082. smp_wmb();
  3083. irq_map->r_hwirq = desc->irq_data.hwirq;
  3084. if (i == pimap->n_mapped)
  3085. pimap->n_mapped++;
  3086. kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq);
  3087. mutex_unlock(&kvm->lock);
  3088. return 0;
  3089. }
  3090. static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
  3091. {
  3092. struct irq_desc *desc;
  3093. struct kvmppc_passthru_irqmap *pimap;
  3094. int i;
  3095. if (!kvm_irq_bypass)
  3096. return 0;
  3097. desc = irq_to_desc(host_irq);
  3098. if (!desc)
  3099. return -EIO;
  3100. mutex_lock(&kvm->lock);
  3101. if (kvm->arch.pimap == NULL) {
  3102. mutex_unlock(&kvm->lock);
  3103. return 0;
  3104. }
  3105. pimap = kvm->arch.pimap;
  3106. for (i = 0; i < pimap->n_mapped; i++) {
  3107. if (guest_gsi == pimap->mapped[i].v_hwirq)
  3108. break;
  3109. }
  3110. if (i == pimap->n_mapped) {
  3111. mutex_unlock(&kvm->lock);
  3112. return -ENODEV;
  3113. }
  3114. kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
  3115. /* invalidate the entry */
  3116. pimap->mapped[i].r_hwirq = 0;
  3117. /*
  3118. * We don't free this structure even when the count goes to
  3119. * zero. The structure is freed when we destroy the VM.
  3120. */
  3121. mutex_unlock(&kvm->lock);
  3122. return 0;
  3123. }
  3124. static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
  3125. struct irq_bypass_producer *prod)
  3126. {
  3127. int ret = 0;
  3128. struct kvm_kernel_irqfd *irqfd =
  3129. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3130. irqfd->producer = prod;
  3131. ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3132. if (ret)
  3133. pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3134. prod->irq, irqfd->gsi, ret);
  3135. return ret;
  3136. }
  3137. static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
  3138. struct irq_bypass_producer *prod)
  3139. {
  3140. int ret;
  3141. struct kvm_kernel_irqfd *irqfd =
  3142. container_of(cons, struct kvm_kernel_irqfd, consumer);
  3143. irqfd->producer = NULL;
  3144. /*
  3145. * When producer of consumer is unregistered, we change back to
  3146. * default external interrupt handling mode - KVM real mode
  3147. * will switch back to host.
  3148. */
  3149. ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
  3150. if (ret)
  3151. pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
  3152. prod->irq, irqfd->gsi, ret);
  3153. }
  3154. #endif
  3155. static long kvm_arch_vm_ioctl_hv(struct file *filp,
  3156. unsigned int ioctl, unsigned long arg)
  3157. {
  3158. struct kvm *kvm __maybe_unused = filp->private_data;
  3159. void __user *argp = (void __user *)arg;
  3160. long r;
  3161. switch (ioctl) {
  3162. case KVM_PPC_ALLOCATE_HTAB: {
  3163. u32 htab_order;
  3164. r = -EFAULT;
  3165. if (get_user(htab_order, (u32 __user *)argp))
  3166. break;
  3167. r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
  3168. if (r)
  3169. break;
  3170. r = -EFAULT;
  3171. if (put_user(htab_order, (u32 __user *)argp))
  3172. break;
  3173. r = 0;
  3174. break;
  3175. }
  3176. case KVM_PPC_GET_HTAB_FD: {
  3177. struct kvm_get_htab_fd ghf;
  3178. r = -EFAULT;
  3179. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  3180. break;
  3181. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  3182. break;
  3183. }
  3184. default:
  3185. r = -ENOTTY;
  3186. }
  3187. return r;
  3188. }
  3189. /*
  3190. * List of hcall numbers to enable by default.
  3191. * For compatibility with old userspace, we enable by default
  3192. * all hcalls that were implemented before the hcall-enabling
  3193. * facility was added. Note this list should not include H_RTAS.
  3194. */
  3195. static unsigned int default_hcall_list[] = {
  3196. H_REMOVE,
  3197. H_ENTER,
  3198. H_READ,
  3199. H_PROTECT,
  3200. H_BULK_REMOVE,
  3201. H_GET_TCE,
  3202. H_PUT_TCE,
  3203. H_SET_DABR,
  3204. H_SET_XDABR,
  3205. H_CEDE,
  3206. H_PROD,
  3207. H_CONFER,
  3208. H_REGISTER_VPA,
  3209. #ifdef CONFIG_KVM_XICS
  3210. H_EOI,
  3211. H_CPPR,
  3212. H_IPI,
  3213. H_IPOLL,
  3214. H_XIRR,
  3215. H_XIRR_X,
  3216. #endif
  3217. 0
  3218. };
  3219. static void init_default_hcalls(void)
  3220. {
  3221. int i;
  3222. unsigned int hcall;
  3223. for (i = 0; default_hcall_list[i]; ++i) {
  3224. hcall = default_hcall_list[i];
  3225. WARN_ON(!kvmppc_hcall_impl_hv(hcall));
  3226. __set_bit(hcall / 4, default_enabled_hcalls);
  3227. }
  3228. }
  3229. static struct kvmppc_ops kvm_ops_hv = {
  3230. .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
  3231. .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
  3232. .get_one_reg = kvmppc_get_one_reg_hv,
  3233. .set_one_reg = kvmppc_set_one_reg_hv,
  3234. .vcpu_load = kvmppc_core_vcpu_load_hv,
  3235. .vcpu_put = kvmppc_core_vcpu_put_hv,
  3236. .set_msr = kvmppc_set_msr_hv,
  3237. .vcpu_run = kvmppc_vcpu_run_hv,
  3238. .vcpu_create = kvmppc_core_vcpu_create_hv,
  3239. .vcpu_free = kvmppc_core_vcpu_free_hv,
  3240. .check_requests = kvmppc_core_check_requests_hv,
  3241. .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
  3242. .flush_memslot = kvmppc_core_flush_memslot_hv,
  3243. .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
  3244. .commit_memory_region = kvmppc_core_commit_memory_region_hv,
  3245. .unmap_hva = kvm_unmap_hva_hv,
  3246. .unmap_hva_range = kvm_unmap_hva_range_hv,
  3247. .age_hva = kvm_age_hva_hv,
  3248. .test_age_hva = kvm_test_age_hva_hv,
  3249. .set_spte_hva = kvm_set_spte_hva_hv,
  3250. .mmu_destroy = kvmppc_mmu_destroy_hv,
  3251. .free_memslot = kvmppc_core_free_memslot_hv,
  3252. .create_memslot = kvmppc_core_create_memslot_hv,
  3253. .init_vm = kvmppc_core_init_vm_hv,
  3254. .destroy_vm = kvmppc_core_destroy_vm_hv,
  3255. .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
  3256. .emulate_op = kvmppc_core_emulate_op_hv,
  3257. .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
  3258. .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
  3259. .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
  3260. .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
  3261. .hcall_implemented = kvmppc_hcall_impl_hv,
  3262. #ifdef CONFIG_KVM_XICS
  3263. .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
  3264. .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
  3265. #endif
  3266. };
  3267. static int kvm_init_subcore_bitmap(void)
  3268. {
  3269. int i, j;
  3270. int nr_cores = cpu_nr_cores();
  3271. struct sibling_subcore_state *sibling_subcore_state;
  3272. for (i = 0; i < nr_cores; i++) {
  3273. int first_cpu = i * threads_per_core;
  3274. int node = cpu_to_node(first_cpu);
  3275. /* Ignore if it is already allocated. */
  3276. if (paca[first_cpu].sibling_subcore_state)
  3277. continue;
  3278. sibling_subcore_state =
  3279. kmalloc_node(sizeof(struct sibling_subcore_state),
  3280. GFP_KERNEL, node);
  3281. if (!sibling_subcore_state)
  3282. return -ENOMEM;
  3283. memset(sibling_subcore_state, 0,
  3284. sizeof(struct sibling_subcore_state));
  3285. for (j = 0; j < threads_per_core; j++) {
  3286. int cpu = first_cpu + j;
  3287. paca[cpu].sibling_subcore_state = sibling_subcore_state;
  3288. }
  3289. }
  3290. return 0;
  3291. }
  3292. static int kvmppc_book3s_init_hv(void)
  3293. {
  3294. int r;
  3295. /*
  3296. * FIXME!! Do we need to check on all cpus ?
  3297. */
  3298. r = kvmppc_core_check_processor_compat_hv();
  3299. if (r < 0)
  3300. return -ENODEV;
  3301. r = kvm_init_subcore_bitmap();
  3302. if (r)
  3303. return r;
  3304. /*
  3305. * We need a way of accessing the XICS interrupt controller,
  3306. * either directly, via paca[cpu].kvm_hstate.xics_phys, or
  3307. * indirectly, via OPAL.
  3308. */
  3309. #ifdef CONFIG_SMP
  3310. if (!get_paca()->kvm_hstate.xics_phys) {
  3311. struct device_node *np;
  3312. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  3313. if (!np) {
  3314. pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
  3315. return -ENODEV;
  3316. }
  3317. }
  3318. #endif
  3319. kvm_ops_hv.owner = THIS_MODULE;
  3320. kvmppc_hv_ops = &kvm_ops_hv;
  3321. init_default_hcalls();
  3322. init_vcore_lists();
  3323. r = kvmppc_mmu_hv_init();
  3324. return r;
  3325. }
  3326. static void kvmppc_book3s_exit_hv(void)
  3327. {
  3328. kvmppc_free_host_rm_ops();
  3329. kvmppc_hv_ops = NULL;
  3330. }
  3331. module_init(kvmppc_book3s_init_hv);
  3332. module_exit(kvmppc_book3s_exit_hv);
  3333. MODULE_LICENSE("GPL");
  3334. MODULE_ALIAS_MISCDEV(KVM_MINOR);
  3335. MODULE_ALIAS("devname:kvm");