book3s_hv.c 94 KB

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