book3s_hv.c 84 KB

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