book3s_hv.c 82 KB

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