book3s_hv.c 70 KB

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