book3s_hv.c 88 KB

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