book3s_hv.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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 <asm/reg.h>
  34. #include <asm/cputable.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/io.h>
  39. #include <asm/kvm_ppc.h>
  40. #include <asm/kvm_book3s.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/lppaca.h>
  43. #include <asm/processor.h>
  44. #include <asm/cputhreads.h>
  45. #include <asm/page.h>
  46. #include <asm/hvcall.h>
  47. #include <asm/switch_to.h>
  48. #include <asm/smp.h>
  49. #include <linux/gfp.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/highmem.h>
  52. #include <linux/hugetlb.h>
  53. /* #define EXIT_DEBUG */
  54. /* #define EXIT_DEBUG_SIMPLE */
  55. /* #define EXIT_DEBUG_INT */
  56. /* Used to indicate that a guest page fault needs to be handled */
  57. #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
  58. /* Used as a "null" value for timebase values */
  59. #define TB_NIL (~(u64)0)
  60. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  61. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  62. void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
  63. {
  64. int me;
  65. int cpu = vcpu->cpu;
  66. wait_queue_head_t *wqp;
  67. wqp = kvm_arch_vcpu_wq(vcpu);
  68. if (waitqueue_active(wqp)) {
  69. wake_up_interruptible(wqp);
  70. ++vcpu->stat.halt_wakeup;
  71. }
  72. me = get_cpu();
  73. /* CPU points to the first thread of the core */
  74. if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
  75. int real_cpu = cpu + vcpu->arch.ptid;
  76. if (paca[real_cpu].kvm_hstate.xics_phys)
  77. xics_wake_cpu(real_cpu);
  78. else if (cpu_online(cpu))
  79. smp_send_reschedule(cpu);
  80. }
  81. put_cpu();
  82. }
  83. /*
  84. * We use the vcpu_load/put functions to measure stolen time.
  85. * Stolen time is counted as time when either the vcpu is able to
  86. * run as part of a virtual core, but the task running the vcore
  87. * is preempted or sleeping, or when the vcpu needs something done
  88. * in the kernel by the task running the vcpu, but that task is
  89. * preempted or sleeping. Those two things have to be counted
  90. * separately, since one of the vcpu tasks will take on the job
  91. * of running the core, and the other vcpu tasks in the vcore will
  92. * sleep waiting for it to do that, but that sleep shouldn't count
  93. * as stolen time.
  94. *
  95. * Hence we accumulate stolen time when the vcpu can run as part of
  96. * a vcore using vc->stolen_tb, and the stolen time when the vcpu
  97. * needs its task to do other things in the kernel (for example,
  98. * service a page fault) in busy_stolen. We don't accumulate
  99. * stolen time for a vcore when it is inactive, or for a vcpu
  100. * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
  101. * a misnomer; it means that the vcpu task is not executing in
  102. * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
  103. * the kernel. We don't have any way of dividing up that time
  104. * between time that the vcpu is genuinely stopped, time that
  105. * the task is actively working on behalf of the vcpu, and time
  106. * that the task is preempted, so we don't count any of it as
  107. * stolen.
  108. *
  109. * Updates to busy_stolen are protected by arch.tbacct_lock;
  110. * updates to vc->stolen_tb are protected by the arch.tbacct_lock
  111. * of the vcpu that has taken responsibility for running the vcore
  112. * (i.e. vc->runner). The stolen times are measured in units of
  113. * timebase ticks. (Note that the != TB_NIL checks below are
  114. * purely defensive; they should never fail.)
  115. */
  116. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  117. {
  118. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  119. spin_lock(&vcpu->arch.tbacct_lock);
  120. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
  121. vc->preempt_tb != TB_NIL) {
  122. vc->stolen_tb += mftb() - vc->preempt_tb;
  123. vc->preempt_tb = TB_NIL;
  124. }
  125. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
  126. vcpu->arch.busy_preempt != TB_NIL) {
  127. vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
  128. vcpu->arch.busy_preempt = TB_NIL;
  129. }
  130. spin_unlock(&vcpu->arch.tbacct_lock);
  131. }
  132. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  133. {
  134. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  135. spin_lock(&vcpu->arch.tbacct_lock);
  136. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
  137. vc->preempt_tb = mftb();
  138. if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
  139. vcpu->arch.busy_preempt = mftb();
  140. spin_unlock(&vcpu->arch.tbacct_lock);
  141. }
  142. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
  143. {
  144. vcpu->arch.shregs.msr = msr;
  145. kvmppc_end_cede(vcpu);
  146. }
  147. void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
  148. {
  149. vcpu->arch.pvr = pvr;
  150. }
  151. void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  152. {
  153. int r;
  154. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  155. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  156. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  157. for (r = 0; r < 16; ++r)
  158. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  159. r, kvmppc_get_gpr(vcpu, r),
  160. r+16, kvmppc_get_gpr(vcpu, r+16));
  161. pr_err("ctr = %.16lx lr = %.16lx\n",
  162. vcpu->arch.ctr, vcpu->arch.lr);
  163. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  164. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  165. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  166. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  167. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  168. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  169. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  170. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  171. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  172. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  173. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  174. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  175. for (r = 0; r < vcpu->arch.slb_max; ++r)
  176. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  177. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  178. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  179. vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
  180. vcpu->arch.last_inst);
  181. }
  182. struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  183. {
  184. int r;
  185. struct kvm_vcpu *v, *ret = NULL;
  186. mutex_lock(&kvm->lock);
  187. kvm_for_each_vcpu(r, v, kvm) {
  188. if (v->vcpu_id == id) {
  189. ret = v;
  190. break;
  191. }
  192. }
  193. mutex_unlock(&kvm->lock);
  194. return ret;
  195. }
  196. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  197. {
  198. vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
  199. vpa->yield_count = 1;
  200. }
  201. static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
  202. unsigned long addr, unsigned long len)
  203. {
  204. /* check address is cacheline aligned */
  205. if (addr & (L1_CACHE_BYTES - 1))
  206. return -EINVAL;
  207. spin_lock(&vcpu->arch.vpa_update_lock);
  208. if (v->next_gpa != addr || v->len != len) {
  209. v->next_gpa = addr;
  210. v->len = addr ? len : 0;
  211. v->update_pending = 1;
  212. }
  213. spin_unlock(&vcpu->arch.vpa_update_lock);
  214. return 0;
  215. }
  216. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  217. struct reg_vpa {
  218. u32 dummy;
  219. union {
  220. u16 hword;
  221. u32 word;
  222. } length;
  223. };
  224. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  225. {
  226. if (vpap->update_pending)
  227. return vpap->next_gpa != 0;
  228. return vpap->pinned_addr != NULL;
  229. }
  230. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  231. unsigned long flags,
  232. unsigned long vcpuid, unsigned long vpa)
  233. {
  234. struct kvm *kvm = vcpu->kvm;
  235. unsigned long len, nb;
  236. void *va;
  237. struct kvm_vcpu *tvcpu;
  238. int err;
  239. int subfunc;
  240. struct kvmppc_vpa *vpap;
  241. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  242. if (!tvcpu)
  243. return H_PARAMETER;
  244. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  245. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  246. subfunc == H_VPA_REG_SLB) {
  247. /* Registering new area - address must be cache-line aligned */
  248. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  249. return H_PARAMETER;
  250. /* convert logical addr to kernel addr and read length */
  251. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  252. if (va == NULL)
  253. return H_PARAMETER;
  254. if (subfunc == H_VPA_REG_VPA)
  255. len = ((struct reg_vpa *)va)->length.hword;
  256. else
  257. len = ((struct reg_vpa *)va)->length.word;
  258. kvmppc_unpin_guest_page(kvm, va, vpa, false);
  259. /* Check length */
  260. if (len > nb || len < sizeof(struct reg_vpa))
  261. return H_PARAMETER;
  262. } else {
  263. vpa = 0;
  264. len = 0;
  265. }
  266. err = H_PARAMETER;
  267. vpap = NULL;
  268. spin_lock(&tvcpu->arch.vpa_update_lock);
  269. switch (subfunc) {
  270. case H_VPA_REG_VPA: /* register VPA */
  271. if (len < sizeof(struct lppaca))
  272. break;
  273. vpap = &tvcpu->arch.vpa;
  274. err = 0;
  275. break;
  276. case H_VPA_REG_DTL: /* register DTL */
  277. if (len < sizeof(struct dtl_entry))
  278. break;
  279. len -= len % sizeof(struct dtl_entry);
  280. /* Check that they have previously registered a VPA */
  281. err = H_RESOURCE;
  282. if (!vpa_is_registered(&tvcpu->arch.vpa))
  283. break;
  284. vpap = &tvcpu->arch.dtl;
  285. err = 0;
  286. break;
  287. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  288. /* Check that they have previously registered a VPA */
  289. err = H_RESOURCE;
  290. if (!vpa_is_registered(&tvcpu->arch.vpa))
  291. break;
  292. vpap = &tvcpu->arch.slb_shadow;
  293. err = 0;
  294. break;
  295. case H_VPA_DEREG_VPA: /* deregister VPA */
  296. /* Check they don't still have a DTL or SLB buf registered */
  297. err = H_RESOURCE;
  298. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  299. vpa_is_registered(&tvcpu->arch.slb_shadow))
  300. break;
  301. vpap = &tvcpu->arch.vpa;
  302. err = 0;
  303. break;
  304. case H_VPA_DEREG_DTL: /* deregister DTL */
  305. vpap = &tvcpu->arch.dtl;
  306. err = 0;
  307. break;
  308. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  309. vpap = &tvcpu->arch.slb_shadow;
  310. err = 0;
  311. break;
  312. }
  313. if (vpap) {
  314. vpap->next_gpa = vpa;
  315. vpap->len = len;
  316. vpap->update_pending = 1;
  317. }
  318. spin_unlock(&tvcpu->arch.vpa_update_lock);
  319. return err;
  320. }
  321. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  322. {
  323. struct kvm *kvm = vcpu->kvm;
  324. void *va;
  325. unsigned long nb;
  326. unsigned long gpa;
  327. /*
  328. * We need to pin the page pointed to by vpap->next_gpa,
  329. * but we can't call kvmppc_pin_guest_page under the lock
  330. * as it does get_user_pages() and down_read(). So we
  331. * have to drop the lock, pin the page, then get the lock
  332. * again and check that a new area didn't get registered
  333. * in the meantime.
  334. */
  335. for (;;) {
  336. gpa = vpap->next_gpa;
  337. spin_unlock(&vcpu->arch.vpa_update_lock);
  338. va = NULL;
  339. nb = 0;
  340. if (gpa)
  341. va = kvmppc_pin_guest_page(kvm, gpa, &nb);
  342. spin_lock(&vcpu->arch.vpa_update_lock);
  343. if (gpa == vpap->next_gpa)
  344. break;
  345. /* sigh... unpin that one and try again */
  346. if (va)
  347. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  348. }
  349. vpap->update_pending = 0;
  350. if (va && nb < vpap->len) {
  351. /*
  352. * If it's now too short, it must be that userspace
  353. * has changed the mappings underlying guest memory,
  354. * so unregister the region.
  355. */
  356. kvmppc_unpin_guest_page(kvm, va, gpa, false);
  357. va = NULL;
  358. }
  359. if (vpap->pinned_addr)
  360. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
  361. vpap->dirty);
  362. vpap->gpa = gpa;
  363. vpap->pinned_addr = va;
  364. vpap->dirty = false;
  365. if (va)
  366. vpap->pinned_end = va + vpap->len;
  367. }
  368. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  369. {
  370. if (!(vcpu->arch.vpa.update_pending ||
  371. vcpu->arch.slb_shadow.update_pending ||
  372. vcpu->arch.dtl.update_pending))
  373. return;
  374. spin_lock(&vcpu->arch.vpa_update_lock);
  375. if (vcpu->arch.vpa.update_pending) {
  376. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  377. if (vcpu->arch.vpa.pinned_addr)
  378. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  379. }
  380. if (vcpu->arch.dtl.update_pending) {
  381. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  382. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  383. vcpu->arch.dtl_index = 0;
  384. }
  385. if (vcpu->arch.slb_shadow.update_pending)
  386. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  387. spin_unlock(&vcpu->arch.vpa_update_lock);
  388. }
  389. /*
  390. * Return the accumulated stolen time for the vcore up until `now'.
  391. * The caller should hold the vcore lock.
  392. */
  393. static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
  394. {
  395. u64 p;
  396. /*
  397. * If we are the task running the vcore, then since we hold
  398. * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
  399. * can't be updated, so we don't need the tbacct_lock.
  400. * If the vcore is inactive, it can't become active (since we
  401. * hold the vcore lock), so the vcpu load/put functions won't
  402. * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
  403. */
  404. if (vc->vcore_state != VCORE_INACTIVE &&
  405. vc->runner->arch.run_task != current) {
  406. spin_lock(&vc->runner->arch.tbacct_lock);
  407. p = vc->stolen_tb;
  408. if (vc->preempt_tb != TB_NIL)
  409. p += now - vc->preempt_tb;
  410. spin_unlock(&vc->runner->arch.tbacct_lock);
  411. } else {
  412. p = vc->stolen_tb;
  413. }
  414. return p;
  415. }
  416. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  417. struct kvmppc_vcore *vc)
  418. {
  419. struct dtl_entry *dt;
  420. struct lppaca *vpa;
  421. unsigned long stolen;
  422. unsigned long core_stolen;
  423. u64 now;
  424. dt = vcpu->arch.dtl_ptr;
  425. vpa = vcpu->arch.vpa.pinned_addr;
  426. now = mftb();
  427. core_stolen = vcore_stolen_time(vc, now);
  428. stolen = core_stolen - vcpu->arch.stolen_logged;
  429. vcpu->arch.stolen_logged = core_stolen;
  430. spin_lock(&vcpu->arch.tbacct_lock);
  431. stolen += vcpu->arch.busy_stolen;
  432. vcpu->arch.busy_stolen = 0;
  433. spin_unlock(&vcpu->arch.tbacct_lock);
  434. if (!dt || !vpa)
  435. return;
  436. memset(dt, 0, sizeof(struct dtl_entry));
  437. dt->dispatch_reason = 7;
  438. dt->processor_id = vc->pcpu + vcpu->arch.ptid;
  439. dt->timebase = now;
  440. dt->enqueue_to_dispatch_time = stolen;
  441. dt->srr0 = kvmppc_get_pc(vcpu);
  442. dt->srr1 = vcpu->arch.shregs.msr;
  443. ++dt;
  444. if (dt == vcpu->arch.dtl.pinned_end)
  445. dt = vcpu->arch.dtl.pinned_addr;
  446. vcpu->arch.dtl_ptr = dt;
  447. /* order writing *dt vs. writing vpa->dtl_idx */
  448. smp_wmb();
  449. vpa->dtl_idx = ++vcpu->arch.dtl_index;
  450. vcpu->arch.dtl.dirty = true;
  451. }
  452. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  453. {
  454. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  455. unsigned long target, ret = H_SUCCESS;
  456. struct kvm_vcpu *tvcpu;
  457. int idx, rc;
  458. switch (req) {
  459. case H_ENTER:
  460. idx = srcu_read_lock(&vcpu->kvm->srcu);
  461. ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
  462. kvmppc_get_gpr(vcpu, 5),
  463. kvmppc_get_gpr(vcpu, 6),
  464. kvmppc_get_gpr(vcpu, 7));
  465. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  466. break;
  467. case H_CEDE:
  468. break;
  469. case H_PROD:
  470. target = kvmppc_get_gpr(vcpu, 4);
  471. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  472. if (!tvcpu) {
  473. ret = H_PARAMETER;
  474. break;
  475. }
  476. tvcpu->arch.prodded = 1;
  477. smp_mb();
  478. if (vcpu->arch.ceded) {
  479. if (waitqueue_active(&vcpu->wq)) {
  480. wake_up_interruptible(&vcpu->wq);
  481. vcpu->stat.halt_wakeup++;
  482. }
  483. }
  484. break;
  485. case H_CONFER:
  486. break;
  487. case H_REGISTER_VPA:
  488. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  489. kvmppc_get_gpr(vcpu, 5),
  490. kvmppc_get_gpr(vcpu, 6));
  491. break;
  492. case H_RTAS:
  493. if (list_empty(&vcpu->kvm->arch.rtas_tokens))
  494. return RESUME_HOST;
  495. rc = kvmppc_rtas_hcall(vcpu);
  496. if (rc == -ENOENT)
  497. return RESUME_HOST;
  498. else if (rc == 0)
  499. break;
  500. /* Send the error out to userspace via KVM_RUN */
  501. return rc;
  502. case H_XIRR:
  503. case H_CPPR:
  504. case H_EOI:
  505. case H_IPI:
  506. case H_IPOLL:
  507. case H_XIRR_X:
  508. if (kvmppc_xics_enabled(vcpu)) {
  509. ret = kvmppc_xics_hcall(vcpu, req);
  510. break;
  511. } /* fallthrough */
  512. default:
  513. return RESUME_HOST;
  514. }
  515. kvmppc_set_gpr(vcpu, 3, ret);
  516. vcpu->arch.hcall_needed = 0;
  517. return RESUME_GUEST;
  518. }
  519. static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  520. struct task_struct *tsk)
  521. {
  522. int r = RESUME_HOST;
  523. vcpu->stat.sum_exits++;
  524. run->exit_reason = KVM_EXIT_UNKNOWN;
  525. run->ready_for_interrupt_injection = 1;
  526. switch (vcpu->arch.trap) {
  527. /* We're good on these - the host merely wanted to get our attention */
  528. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  529. vcpu->stat.dec_exits++;
  530. r = RESUME_GUEST;
  531. break;
  532. case BOOK3S_INTERRUPT_EXTERNAL:
  533. vcpu->stat.ext_intr_exits++;
  534. r = RESUME_GUEST;
  535. break;
  536. case BOOK3S_INTERRUPT_PERFMON:
  537. r = RESUME_GUEST;
  538. break;
  539. case BOOK3S_INTERRUPT_MACHINE_CHECK:
  540. /*
  541. * Deliver a machine check interrupt to the guest.
  542. * We have to do this, even if the host has handled the
  543. * machine check, because machine checks use SRR0/1 and
  544. * the interrupt might have trashed guest state in them.
  545. */
  546. kvmppc_book3s_queue_irqprio(vcpu,
  547. BOOK3S_INTERRUPT_MACHINE_CHECK);
  548. r = RESUME_GUEST;
  549. break;
  550. case BOOK3S_INTERRUPT_PROGRAM:
  551. {
  552. ulong flags;
  553. /*
  554. * Normally program interrupts are delivered directly
  555. * to the guest by the hardware, but we can get here
  556. * as a result of a hypervisor emulation interrupt
  557. * (e40) getting turned into a 700 by BML RTAS.
  558. */
  559. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  560. kvmppc_core_queue_program(vcpu, flags);
  561. r = RESUME_GUEST;
  562. break;
  563. }
  564. case BOOK3S_INTERRUPT_SYSCALL:
  565. {
  566. /* hcall - punt to userspace */
  567. int i;
  568. if (vcpu->arch.shregs.msr & MSR_PR) {
  569. /* sc 1 from userspace - reflect to guest syscall */
  570. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
  571. r = RESUME_GUEST;
  572. break;
  573. }
  574. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  575. for (i = 0; i < 9; ++i)
  576. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  577. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  578. vcpu->arch.hcall_needed = 1;
  579. r = RESUME_HOST;
  580. break;
  581. }
  582. /*
  583. * We get these next two if the guest accesses a page which it thinks
  584. * it has mapped but which is not actually present, either because
  585. * it is for an emulated I/O device or because the corresonding
  586. * host page has been paged out. Any other HDSI/HISI interrupts
  587. * have been handled already.
  588. */
  589. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  590. r = RESUME_PAGE_FAULT;
  591. break;
  592. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  593. vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
  594. vcpu->arch.fault_dsisr = 0;
  595. r = RESUME_PAGE_FAULT;
  596. break;
  597. /*
  598. * This occurs if the guest executes an illegal instruction.
  599. * We just generate a program interrupt to the guest, since
  600. * we don't emulate any guest instructions at this stage.
  601. */
  602. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  603. kvmppc_core_queue_program(vcpu, 0x80000);
  604. r = RESUME_GUEST;
  605. break;
  606. default:
  607. kvmppc_dump_regs(vcpu);
  608. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  609. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  610. vcpu->arch.shregs.msr);
  611. r = RESUME_HOST;
  612. BUG();
  613. break;
  614. }
  615. return r;
  616. }
  617. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  618. struct kvm_sregs *sregs)
  619. {
  620. int i;
  621. memset(sregs, 0, sizeof(struct kvm_sregs));
  622. sregs->pvr = vcpu->arch.pvr;
  623. for (i = 0; i < vcpu->arch.slb_max; i++) {
  624. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  625. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  626. }
  627. return 0;
  628. }
  629. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  630. struct kvm_sregs *sregs)
  631. {
  632. int i, j;
  633. kvmppc_set_pvr(vcpu, sregs->pvr);
  634. j = 0;
  635. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  636. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  637. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  638. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  639. ++j;
  640. }
  641. }
  642. vcpu->arch.slb_max = j;
  643. return 0;
  644. }
  645. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  646. {
  647. int r = 0;
  648. long int i;
  649. switch (id) {
  650. case KVM_REG_PPC_HIOR:
  651. *val = get_reg_val(id, 0);
  652. break;
  653. case KVM_REG_PPC_DABR:
  654. *val = get_reg_val(id, vcpu->arch.dabr);
  655. break;
  656. case KVM_REG_PPC_DSCR:
  657. *val = get_reg_val(id, vcpu->arch.dscr);
  658. break;
  659. case KVM_REG_PPC_PURR:
  660. *val = get_reg_val(id, vcpu->arch.purr);
  661. break;
  662. case KVM_REG_PPC_SPURR:
  663. *val = get_reg_val(id, vcpu->arch.spurr);
  664. break;
  665. case KVM_REG_PPC_AMR:
  666. *val = get_reg_val(id, vcpu->arch.amr);
  667. break;
  668. case KVM_REG_PPC_UAMOR:
  669. *val = get_reg_val(id, vcpu->arch.uamor);
  670. break;
  671. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
  672. i = id - KVM_REG_PPC_MMCR0;
  673. *val = get_reg_val(id, vcpu->arch.mmcr[i]);
  674. break;
  675. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  676. i = id - KVM_REG_PPC_PMC1;
  677. *val = get_reg_val(id, vcpu->arch.pmc[i]);
  678. break;
  679. #ifdef CONFIG_VSX
  680. case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
  681. if (cpu_has_feature(CPU_FTR_VSX)) {
  682. /* VSX => FP reg i is stored in arch.vsr[2*i] */
  683. long int i = id - KVM_REG_PPC_FPR0;
  684. *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
  685. } else {
  686. /* let generic code handle it */
  687. r = -EINVAL;
  688. }
  689. break;
  690. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
  691. if (cpu_has_feature(CPU_FTR_VSX)) {
  692. long int i = id - KVM_REG_PPC_VSR0;
  693. val->vsxval[0] = vcpu->arch.vsr[2 * i];
  694. val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
  695. } else {
  696. r = -ENXIO;
  697. }
  698. break;
  699. #endif /* CONFIG_VSX */
  700. case KVM_REG_PPC_VPA_ADDR:
  701. spin_lock(&vcpu->arch.vpa_update_lock);
  702. *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
  703. spin_unlock(&vcpu->arch.vpa_update_lock);
  704. break;
  705. case KVM_REG_PPC_VPA_SLB:
  706. spin_lock(&vcpu->arch.vpa_update_lock);
  707. val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
  708. val->vpaval.length = vcpu->arch.slb_shadow.len;
  709. spin_unlock(&vcpu->arch.vpa_update_lock);
  710. break;
  711. case KVM_REG_PPC_VPA_DTL:
  712. spin_lock(&vcpu->arch.vpa_update_lock);
  713. val->vpaval.addr = vcpu->arch.dtl.next_gpa;
  714. val->vpaval.length = vcpu->arch.dtl.len;
  715. spin_unlock(&vcpu->arch.vpa_update_lock);
  716. break;
  717. default:
  718. r = -EINVAL;
  719. break;
  720. }
  721. return r;
  722. }
  723. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *val)
  724. {
  725. int r = 0;
  726. long int i;
  727. unsigned long addr, len;
  728. switch (id) {
  729. case KVM_REG_PPC_HIOR:
  730. /* Only allow this to be set to zero */
  731. if (set_reg_val(id, *val))
  732. r = -EINVAL;
  733. break;
  734. case KVM_REG_PPC_DABR:
  735. vcpu->arch.dabr = set_reg_val(id, *val);
  736. break;
  737. case KVM_REG_PPC_DSCR:
  738. vcpu->arch.dscr = set_reg_val(id, *val);
  739. break;
  740. case KVM_REG_PPC_PURR:
  741. vcpu->arch.purr = set_reg_val(id, *val);
  742. break;
  743. case KVM_REG_PPC_SPURR:
  744. vcpu->arch.spurr = set_reg_val(id, *val);
  745. break;
  746. case KVM_REG_PPC_AMR:
  747. vcpu->arch.amr = set_reg_val(id, *val);
  748. break;
  749. case KVM_REG_PPC_UAMOR:
  750. vcpu->arch.uamor = set_reg_val(id, *val);
  751. break;
  752. case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
  753. i = id - KVM_REG_PPC_MMCR0;
  754. vcpu->arch.mmcr[i] = set_reg_val(id, *val);
  755. break;
  756. case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
  757. i = id - KVM_REG_PPC_PMC1;
  758. vcpu->arch.pmc[i] = set_reg_val(id, *val);
  759. break;
  760. #ifdef CONFIG_VSX
  761. case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
  762. if (cpu_has_feature(CPU_FTR_VSX)) {
  763. /* VSX => FP reg i is stored in arch.vsr[2*i] */
  764. long int i = id - KVM_REG_PPC_FPR0;
  765. vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
  766. } else {
  767. /* let generic code handle it */
  768. r = -EINVAL;
  769. }
  770. break;
  771. case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
  772. if (cpu_has_feature(CPU_FTR_VSX)) {
  773. long int i = id - KVM_REG_PPC_VSR0;
  774. vcpu->arch.vsr[2 * i] = val->vsxval[0];
  775. vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
  776. } else {
  777. r = -ENXIO;
  778. }
  779. break;
  780. #endif /* CONFIG_VSX */
  781. case KVM_REG_PPC_VPA_ADDR:
  782. addr = set_reg_val(id, *val);
  783. r = -EINVAL;
  784. if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
  785. vcpu->arch.dtl.next_gpa))
  786. break;
  787. r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
  788. break;
  789. case KVM_REG_PPC_VPA_SLB:
  790. addr = val->vpaval.addr;
  791. len = val->vpaval.length;
  792. r = -EINVAL;
  793. if (addr && !vcpu->arch.vpa.next_gpa)
  794. break;
  795. r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
  796. break;
  797. case KVM_REG_PPC_VPA_DTL:
  798. addr = val->vpaval.addr;
  799. len = val->vpaval.length;
  800. r = -EINVAL;
  801. if (addr && (len < sizeof(struct dtl_entry) ||
  802. !vcpu->arch.vpa.next_gpa))
  803. break;
  804. len -= len % sizeof(struct dtl_entry);
  805. r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
  806. break;
  807. default:
  808. r = -EINVAL;
  809. break;
  810. }
  811. return r;
  812. }
  813. int kvmppc_core_check_processor_compat(void)
  814. {
  815. if (cpu_has_feature(CPU_FTR_HVMODE))
  816. return 0;
  817. return -EIO;
  818. }
  819. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  820. {
  821. struct kvm_vcpu *vcpu;
  822. int err = -EINVAL;
  823. int core;
  824. struct kvmppc_vcore *vcore;
  825. core = id / threads_per_core;
  826. if (core >= KVM_MAX_VCORES)
  827. goto out;
  828. err = -ENOMEM;
  829. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  830. if (!vcpu)
  831. goto out;
  832. err = kvm_vcpu_init(vcpu, kvm, id);
  833. if (err)
  834. goto free_vcpu;
  835. vcpu->arch.shared = &vcpu->arch.shregs;
  836. vcpu->arch.mmcr[0] = MMCR0_FC;
  837. vcpu->arch.ctrl = CTRL_RUNLATCH;
  838. /* default to host PVR, since we can't spoof it */
  839. vcpu->arch.pvr = mfspr(SPRN_PVR);
  840. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  841. spin_lock_init(&vcpu->arch.vpa_update_lock);
  842. spin_lock_init(&vcpu->arch.tbacct_lock);
  843. vcpu->arch.busy_preempt = TB_NIL;
  844. kvmppc_mmu_book3s_hv_init(vcpu);
  845. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  846. init_waitqueue_head(&vcpu->arch.cpu_run);
  847. mutex_lock(&kvm->lock);
  848. vcore = kvm->arch.vcores[core];
  849. if (!vcore) {
  850. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  851. if (vcore) {
  852. INIT_LIST_HEAD(&vcore->runnable_threads);
  853. spin_lock_init(&vcore->lock);
  854. init_waitqueue_head(&vcore->wq);
  855. vcore->preempt_tb = TB_NIL;
  856. }
  857. kvm->arch.vcores[core] = vcore;
  858. kvm->arch.online_vcores++;
  859. }
  860. mutex_unlock(&kvm->lock);
  861. if (!vcore)
  862. goto free_vcpu;
  863. spin_lock(&vcore->lock);
  864. ++vcore->num_threads;
  865. spin_unlock(&vcore->lock);
  866. vcpu->arch.vcore = vcore;
  867. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  868. kvmppc_sanity_check(vcpu);
  869. return vcpu;
  870. free_vcpu:
  871. kmem_cache_free(kvm_vcpu_cache, vcpu);
  872. out:
  873. return ERR_PTR(err);
  874. }
  875. static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
  876. {
  877. if (vpa->pinned_addr)
  878. kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
  879. vpa->dirty);
  880. }
  881. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  882. {
  883. spin_lock(&vcpu->arch.vpa_update_lock);
  884. unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
  885. unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
  886. unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
  887. spin_unlock(&vcpu->arch.vpa_update_lock);
  888. kvm_vcpu_uninit(vcpu);
  889. kmem_cache_free(kvm_vcpu_cache, vcpu);
  890. }
  891. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  892. {
  893. unsigned long dec_nsec, now;
  894. now = get_tb();
  895. if (now > vcpu->arch.dec_expires) {
  896. /* decrementer has already gone negative */
  897. kvmppc_core_queue_dec(vcpu);
  898. kvmppc_core_prepare_to_enter(vcpu);
  899. return;
  900. }
  901. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  902. / tb_ticks_per_sec;
  903. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  904. HRTIMER_MODE_REL);
  905. vcpu->arch.timer_running = 1;
  906. }
  907. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  908. {
  909. vcpu->arch.ceded = 0;
  910. if (vcpu->arch.timer_running) {
  911. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  912. vcpu->arch.timer_running = 0;
  913. }
  914. }
  915. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  916. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  917. struct kvm_vcpu *vcpu)
  918. {
  919. u64 now;
  920. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  921. return;
  922. spin_lock(&vcpu->arch.tbacct_lock);
  923. now = mftb();
  924. vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
  925. vcpu->arch.stolen_logged;
  926. vcpu->arch.busy_preempt = now;
  927. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  928. spin_unlock(&vcpu->arch.tbacct_lock);
  929. --vc->n_runnable;
  930. list_del(&vcpu->arch.run_list);
  931. }
  932. static int kvmppc_grab_hwthread(int cpu)
  933. {
  934. struct paca_struct *tpaca;
  935. long timeout = 1000;
  936. tpaca = &paca[cpu];
  937. /* Ensure the thread won't go into the kernel if it wakes */
  938. tpaca->kvm_hstate.hwthread_req = 1;
  939. tpaca->kvm_hstate.kvm_vcpu = NULL;
  940. /*
  941. * If the thread is already executing in the kernel (e.g. handling
  942. * a stray interrupt), wait for it to get back to nap mode.
  943. * The smp_mb() is to ensure that our setting of hwthread_req
  944. * is visible before we look at hwthread_state, so if this
  945. * races with the code at system_reset_pSeries and the thread
  946. * misses our setting of hwthread_req, we are sure to see its
  947. * setting of hwthread_state, and vice versa.
  948. */
  949. smp_mb();
  950. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  951. if (--timeout <= 0) {
  952. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  953. return -EBUSY;
  954. }
  955. udelay(1);
  956. }
  957. return 0;
  958. }
  959. static void kvmppc_release_hwthread(int cpu)
  960. {
  961. struct paca_struct *tpaca;
  962. tpaca = &paca[cpu];
  963. tpaca->kvm_hstate.hwthread_req = 0;
  964. tpaca->kvm_hstate.kvm_vcpu = NULL;
  965. }
  966. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  967. {
  968. int cpu;
  969. struct paca_struct *tpaca;
  970. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  971. if (vcpu->arch.timer_running) {
  972. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  973. vcpu->arch.timer_running = 0;
  974. }
  975. cpu = vc->pcpu + vcpu->arch.ptid;
  976. tpaca = &paca[cpu];
  977. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  978. tpaca->kvm_hstate.kvm_vcore = vc;
  979. tpaca->kvm_hstate.napping = 0;
  980. vcpu->cpu = vc->pcpu;
  981. smp_wmb();
  982. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  983. if (vcpu->arch.ptid) {
  984. xics_wake_cpu(cpu);
  985. ++vc->n_woken;
  986. }
  987. #endif
  988. }
  989. static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
  990. {
  991. int i;
  992. HMT_low();
  993. i = 0;
  994. while (vc->nap_count < vc->n_woken) {
  995. if (++i >= 1000000) {
  996. pr_err("kvmppc_wait_for_nap timeout %d %d\n",
  997. vc->nap_count, vc->n_woken);
  998. break;
  999. }
  1000. cpu_relax();
  1001. }
  1002. HMT_medium();
  1003. }
  1004. /*
  1005. * Check that we are on thread 0 and that any other threads in
  1006. * this core are off-line. Then grab the threads so they can't
  1007. * enter the kernel.
  1008. */
  1009. static int on_primary_thread(void)
  1010. {
  1011. int cpu = smp_processor_id();
  1012. int thr = cpu_thread_in_core(cpu);
  1013. if (thr)
  1014. return 0;
  1015. while (++thr < threads_per_core)
  1016. if (cpu_online(cpu + thr))
  1017. return 0;
  1018. /* Grab all hw threads so they can't go into the kernel */
  1019. for (thr = 1; thr < threads_per_core; ++thr) {
  1020. if (kvmppc_grab_hwthread(cpu + thr)) {
  1021. /* Couldn't grab one; let the others go */
  1022. do {
  1023. kvmppc_release_hwthread(cpu + thr);
  1024. } while (--thr > 0);
  1025. return 0;
  1026. }
  1027. }
  1028. return 1;
  1029. }
  1030. /*
  1031. * Run a set of guest threads on a physical core.
  1032. * Called with vc->lock held.
  1033. */
  1034. static void kvmppc_run_core(struct kvmppc_vcore *vc)
  1035. {
  1036. struct kvm_vcpu *vcpu, *vcpu0, *vnext;
  1037. long ret;
  1038. u64 now;
  1039. int ptid, i, need_vpa_update;
  1040. int srcu_idx;
  1041. struct kvm_vcpu *vcpus_to_update[threads_per_core];
  1042. /* don't start if any threads have a signal pending */
  1043. need_vpa_update = 0;
  1044. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1045. if (signal_pending(vcpu->arch.run_task))
  1046. return;
  1047. if (vcpu->arch.vpa.update_pending ||
  1048. vcpu->arch.slb_shadow.update_pending ||
  1049. vcpu->arch.dtl.update_pending)
  1050. vcpus_to_update[need_vpa_update++] = vcpu;
  1051. }
  1052. /*
  1053. * Initialize *vc, in particular vc->vcore_state, so we can
  1054. * drop the vcore lock if necessary.
  1055. */
  1056. vc->n_woken = 0;
  1057. vc->nap_count = 0;
  1058. vc->entry_exit_count = 0;
  1059. vc->vcore_state = VCORE_STARTING;
  1060. vc->in_guest = 0;
  1061. vc->napping_threads = 0;
  1062. /*
  1063. * Updating any of the vpas requires calling kvmppc_pin_guest_page,
  1064. * which can't be called with any spinlocks held.
  1065. */
  1066. if (need_vpa_update) {
  1067. spin_unlock(&vc->lock);
  1068. for (i = 0; i < need_vpa_update; ++i)
  1069. kvmppc_update_vpas(vcpus_to_update[i]);
  1070. spin_lock(&vc->lock);
  1071. }
  1072. /*
  1073. * Assign physical thread IDs, first to non-ceded vcpus
  1074. * and then to ceded ones.
  1075. */
  1076. ptid = 0;
  1077. vcpu0 = NULL;
  1078. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1079. if (!vcpu->arch.ceded) {
  1080. if (!ptid)
  1081. vcpu0 = vcpu;
  1082. vcpu->arch.ptid = ptid++;
  1083. }
  1084. }
  1085. if (!vcpu0)
  1086. goto out; /* nothing to run; should never happen */
  1087. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1088. if (vcpu->arch.ceded)
  1089. vcpu->arch.ptid = ptid++;
  1090. /*
  1091. * Make sure we are running on thread 0, and that
  1092. * secondary threads are offline.
  1093. */
  1094. if (threads_per_core > 1 && !on_primary_thread()) {
  1095. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1096. vcpu->arch.ret = -EBUSY;
  1097. goto out;
  1098. }
  1099. vc->pcpu = smp_processor_id();
  1100. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1101. kvmppc_start_thread(vcpu);
  1102. kvmppc_create_dtl_entry(vcpu, vc);
  1103. }
  1104. vc->vcore_state = VCORE_RUNNING;
  1105. preempt_disable();
  1106. spin_unlock(&vc->lock);
  1107. kvm_guest_enter();
  1108. srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
  1109. __kvmppc_vcore_entry(NULL, vcpu0);
  1110. spin_lock(&vc->lock);
  1111. /* disable sending of IPIs on virtual external irqs */
  1112. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  1113. vcpu->cpu = -1;
  1114. /* wait for secondary threads to finish writing their state to memory */
  1115. if (vc->nap_count < vc->n_woken)
  1116. kvmppc_wait_for_nap(vc);
  1117. for (i = 0; i < threads_per_core; ++i)
  1118. kvmppc_release_hwthread(vc->pcpu + i);
  1119. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  1120. vc->vcore_state = VCORE_EXITING;
  1121. spin_unlock(&vc->lock);
  1122. srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
  1123. /* make sure updates to secondary vcpu structs are visible now */
  1124. smp_mb();
  1125. kvm_guest_exit();
  1126. preempt_enable();
  1127. kvm_resched(vcpu);
  1128. spin_lock(&vc->lock);
  1129. now = get_tb();
  1130. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  1131. /* cancel pending dec exception if dec is positive */
  1132. if (now < vcpu->arch.dec_expires &&
  1133. kvmppc_core_pending_dec(vcpu))
  1134. kvmppc_core_dequeue_dec(vcpu);
  1135. ret = RESUME_GUEST;
  1136. if (vcpu->arch.trap)
  1137. ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
  1138. vcpu->arch.run_task);
  1139. vcpu->arch.ret = ret;
  1140. vcpu->arch.trap = 0;
  1141. if (vcpu->arch.ceded) {
  1142. if (ret != RESUME_GUEST)
  1143. kvmppc_end_cede(vcpu);
  1144. else
  1145. kvmppc_set_timer(vcpu);
  1146. }
  1147. }
  1148. out:
  1149. vc->vcore_state = VCORE_INACTIVE;
  1150. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  1151. arch.run_list) {
  1152. if (vcpu->arch.ret != RESUME_GUEST) {
  1153. kvmppc_remove_runnable(vc, vcpu);
  1154. wake_up(&vcpu->arch.cpu_run);
  1155. }
  1156. }
  1157. }
  1158. /*
  1159. * Wait for some other vcpu thread to execute us, and
  1160. * wake us up when we need to handle something in the host.
  1161. */
  1162. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  1163. {
  1164. DEFINE_WAIT(wait);
  1165. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  1166. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  1167. schedule();
  1168. finish_wait(&vcpu->arch.cpu_run, &wait);
  1169. }
  1170. /*
  1171. * All the vcpus in this vcore are idle, so wait for a decrementer
  1172. * or external interrupt to one of the vcpus. vc->lock is held.
  1173. */
  1174. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  1175. {
  1176. DEFINE_WAIT(wait);
  1177. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  1178. vc->vcore_state = VCORE_SLEEPING;
  1179. spin_unlock(&vc->lock);
  1180. schedule();
  1181. finish_wait(&vc->wq, &wait);
  1182. spin_lock(&vc->lock);
  1183. vc->vcore_state = VCORE_INACTIVE;
  1184. }
  1185. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1186. {
  1187. int n_ceded;
  1188. struct kvmppc_vcore *vc;
  1189. struct kvm_vcpu *v, *vn;
  1190. kvm_run->exit_reason = 0;
  1191. vcpu->arch.ret = RESUME_GUEST;
  1192. vcpu->arch.trap = 0;
  1193. kvmppc_update_vpas(vcpu);
  1194. /*
  1195. * Synchronize with other threads in this virtual core
  1196. */
  1197. vc = vcpu->arch.vcore;
  1198. spin_lock(&vc->lock);
  1199. vcpu->arch.ceded = 0;
  1200. vcpu->arch.run_task = current;
  1201. vcpu->arch.kvm_run = kvm_run;
  1202. vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
  1203. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  1204. vcpu->arch.busy_preempt = TB_NIL;
  1205. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  1206. ++vc->n_runnable;
  1207. /*
  1208. * This happens the first time this is called for a vcpu.
  1209. * If the vcore is already running, we may be able to start
  1210. * this thread straight away and have it join in.
  1211. */
  1212. if (!signal_pending(current)) {
  1213. if (vc->vcore_state == VCORE_RUNNING &&
  1214. VCORE_EXIT_COUNT(vc) == 0) {
  1215. vcpu->arch.ptid = vc->n_runnable - 1;
  1216. kvmppc_create_dtl_entry(vcpu, vc);
  1217. kvmppc_start_thread(vcpu);
  1218. } else if (vc->vcore_state == VCORE_SLEEPING) {
  1219. wake_up(&vc->wq);
  1220. }
  1221. }
  1222. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1223. !signal_pending(current)) {
  1224. if (vc->vcore_state != VCORE_INACTIVE) {
  1225. spin_unlock(&vc->lock);
  1226. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  1227. spin_lock(&vc->lock);
  1228. continue;
  1229. }
  1230. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  1231. arch.run_list) {
  1232. kvmppc_core_prepare_to_enter(v);
  1233. if (signal_pending(v->arch.run_task)) {
  1234. kvmppc_remove_runnable(vc, v);
  1235. v->stat.signal_exits++;
  1236. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  1237. v->arch.ret = -EINTR;
  1238. wake_up(&v->arch.cpu_run);
  1239. }
  1240. }
  1241. if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  1242. break;
  1243. vc->runner = vcpu;
  1244. n_ceded = 0;
  1245. list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
  1246. if (!v->arch.pending_exceptions)
  1247. n_ceded += v->arch.ceded;
  1248. else
  1249. v->arch.ceded = 0;
  1250. }
  1251. if (n_ceded == vc->n_runnable)
  1252. kvmppc_vcore_blocked(vc);
  1253. else
  1254. kvmppc_run_core(vc);
  1255. vc->runner = NULL;
  1256. }
  1257. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  1258. (vc->vcore_state == VCORE_RUNNING ||
  1259. vc->vcore_state == VCORE_EXITING)) {
  1260. spin_unlock(&vc->lock);
  1261. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  1262. spin_lock(&vc->lock);
  1263. }
  1264. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  1265. kvmppc_remove_runnable(vc, vcpu);
  1266. vcpu->stat.signal_exits++;
  1267. kvm_run->exit_reason = KVM_EXIT_INTR;
  1268. vcpu->arch.ret = -EINTR;
  1269. }
  1270. if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
  1271. /* Wake up some vcpu to run the core */
  1272. v = list_first_entry(&vc->runnable_threads,
  1273. struct kvm_vcpu, arch.run_list);
  1274. wake_up(&v->arch.cpu_run);
  1275. }
  1276. spin_unlock(&vc->lock);
  1277. return vcpu->arch.ret;
  1278. }
  1279. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  1280. {
  1281. int r;
  1282. int srcu_idx;
  1283. if (!vcpu->arch.sane) {
  1284. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1285. return -EINVAL;
  1286. }
  1287. kvmppc_core_prepare_to_enter(vcpu);
  1288. /* No need to go into the guest when all we'll do is come back out */
  1289. if (signal_pending(current)) {
  1290. run->exit_reason = KVM_EXIT_INTR;
  1291. return -EINTR;
  1292. }
  1293. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  1294. /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
  1295. smp_mb();
  1296. /* On the first time here, set up HTAB and VRMA or RMA */
  1297. if (!vcpu->kvm->arch.rma_setup_done) {
  1298. r = kvmppc_hv_setup_htab_rma(vcpu);
  1299. if (r)
  1300. goto out;
  1301. }
  1302. flush_fp_to_thread(current);
  1303. flush_altivec_to_thread(current);
  1304. flush_vsx_to_thread(current);
  1305. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  1306. vcpu->arch.pgdir = current->mm->pgd;
  1307. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  1308. do {
  1309. r = kvmppc_run_vcpu(run, vcpu);
  1310. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  1311. !(vcpu->arch.shregs.msr & MSR_PR)) {
  1312. r = kvmppc_pseries_do_hcall(vcpu);
  1313. kvmppc_core_prepare_to_enter(vcpu);
  1314. } else if (r == RESUME_PAGE_FAULT) {
  1315. srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1316. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  1317. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  1318. srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
  1319. }
  1320. } while (r == RESUME_GUEST);
  1321. out:
  1322. vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
  1323. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  1324. return r;
  1325. }
  1326. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  1327. Assumes POWER7 or PPC970. */
  1328. static inline int lpcr_rmls(unsigned long rma_size)
  1329. {
  1330. switch (rma_size) {
  1331. case 32ul << 20: /* 32 MB */
  1332. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1333. return 8; /* only supported on POWER7 */
  1334. return -1;
  1335. case 64ul << 20: /* 64 MB */
  1336. return 3;
  1337. case 128ul << 20: /* 128 MB */
  1338. return 7;
  1339. case 256ul << 20: /* 256 MB */
  1340. return 4;
  1341. case 1ul << 30: /* 1 GB */
  1342. return 2;
  1343. case 16ul << 30: /* 16 GB */
  1344. return 1;
  1345. case 256ul << 30: /* 256 GB */
  1346. return 0;
  1347. default:
  1348. return -1;
  1349. }
  1350. }
  1351. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1352. {
  1353. struct page *page;
  1354. struct kvm_rma_info *ri = vma->vm_file->private_data;
  1355. if (vmf->pgoff >= kvm_rma_pages)
  1356. return VM_FAULT_SIGBUS;
  1357. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  1358. get_page(page);
  1359. vmf->page = page;
  1360. return 0;
  1361. }
  1362. static const struct vm_operations_struct kvm_rma_vm_ops = {
  1363. .fault = kvm_rma_fault,
  1364. };
  1365. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  1366. {
  1367. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1368. vma->vm_ops = &kvm_rma_vm_ops;
  1369. return 0;
  1370. }
  1371. static int kvm_rma_release(struct inode *inode, struct file *filp)
  1372. {
  1373. struct kvm_rma_info *ri = filp->private_data;
  1374. kvm_release_rma(ri);
  1375. return 0;
  1376. }
  1377. static const struct file_operations kvm_rma_fops = {
  1378. .mmap = kvm_rma_mmap,
  1379. .release = kvm_rma_release,
  1380. };
  1381. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  1382. {
  1383. long fd;
  1384. struct kvm_rma_info *ri;
  1385. /*
  1386. * Only do this on PPC970 in HV mode
  1387. */
  1388. if (!cpu_has_feature(CPU_FTR_HVMODE) ||
  1389. !cpu_has_feature(CPU_FTR_ARCH_201))
  1390. return -EINVAL;
  1391. if (!kvm_rma_pages)
  1392. return -EINVAL;
  1393. ri = kvm_alloc_rma();
  1394. if (!ri)
  1395. return -ENOMEM;
  1396. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
  1397. if (fd < 0)
  1398. kvm_release_rma(ri);
  1399. ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
  1400. return fd;
  1401. }
  1402. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  1403. int linux_psize)
  1404. {
  1405. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  1406. if (!def->shift)
  1407. return;
  1408. (*sps)->page_shift = def->shift;
  1409. (*sps)->slb_enc = def->sllp;
  1410. (*sps)->enc[0].page_shift = def->shift;
  1411. /*
  1412. * Only return base page encoding. We don't want to return
  1413. * all the supporting pte_enc, because our H_ENTER doesn't
  1414. * support MPSS yet. Once they do, we can start passing all
  1415. * support pte_enc here
  1416. */
  1417. (*sps)->enc[0].pte_enc = def->penc[linux_psize];
  1418. (*sps)++;
  1419. }
  1420. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1421. {
  1422. struct kvm_ppc_one_seg_page_size *sps;
  1423. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  1424. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1425. info->flags |= KVM_PPC_1T_SEGMENTS;
  1426. info->slb_size = mmu_slb_size;
  1427. /* We only support these sizes for now, and no muti-size segments */
  1428. sps = &info->sps[0];
  1429. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  1430. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  1431. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  1432. return 0;
  1433. }
  1434. /*
  1435. * Get (and clear) the dirty memory log for a memory slot.
  1436. */
  1437. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1438. {
  1439. struct kvm_memory_slot *memslot;
  1440. int r;
  1441. unsigned long n;
  1442. mutex_lock(&kvm->slots_lock);
  1443. r = -EINVAL;
  1444. if (log->slot >= KVM_USER_MEM_SLOTS)
  1445. goto out;
  1446. memslot = id_to_memslot(kvm->memslots, log->slot);
  1447. r = -ENOENT;
  1448. if (!memslot->dirty_bitmap)
  1449. goto out;
  1450. n = kvm_dirty_bitmap_bytes(memslot);
  1451. memset(memslot->dirty_bitmap, 0, n);
  1452. r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
  1453. if (r)
  1454. goto out;
  1455. r = -EFAULT;
  1456. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  1457. goto out;
  1458. r = 0;
  1459. out:
  1460. mutex_unlock(&kvm->slots_lock);
  1461. return r;
  1462. }
  1463. static void unpin_slot(struct kvm_memory_slot *memslot)
  1464. {
  1465. unsigned long *physp;
  1466. unsigned long j, npages, pfn;
  1467. struct page *page;
  1468. physp = memslot->arch.slot_phys;
  1469. npages = memslot->npages;
  1470. if (!physp)
  1471. return;
  1472. for (j = 0; j < npages; j++) {
  1473. if (!(physp[j] & KVMPPC_GOT_PAGE))
  1474. continue;
  1475. pfn = physp[j] >> PAGE_SHIFT;
  1476. page = pfn_to_page(pfn);
  1477. SetPageDirty(page);
  1478. put_page(page);
  1479. }
  1480. }
  1481. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1482. struct kvm_memory_slot *dont)
  1483. {
  1484. if (!dont || free->arch.rmap != dont->arch.rmap) {
  1485. vfree(free->arch.rmap);
  1486. free->arch.rmap = NULL;
  1487. }
  1488. if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
  1489. unpin_slot(free);
  1490. vfree(free->arch.slot_phys);
  1491. free->arch.slot_phys = NULL;
  1492. }
  1493. }
  1494. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1495. unsigned long npages)
  1496. {
  1497. slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
  1498. if (!slot->arch.rmap)
  1499. return -ENOMEM;
  1500. slot->arch.slot_phys = NULL;
  1501. return 0;
  1502. }
  1503. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1504. struct kvm_memory_slot *memslot,
  1505. struct kvm_userspace_memory_region *mem)
  1506. {
  1507. unsigned long *phys;
  1508. /* Allocate a slot_phys array if needed */
  1509. phys = memslot->arch.slot_phys;
  1510. if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
  1511. phys = vzalloc(memslot->npages * sizeof(unsigned long));
  1512. if (!phys)
  1513. return -ENOMEM;
  1514. memslot->arch.slot_phys = phys;
  1515. }
  1516. return 0;
  1517. }
  1518. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1519. struct kvm_userspace_memory_region *mem,
  1520. const struct kvm_memory_slot *old)
  1521. {
  1522. unsigned long npages = mem->memory_size >> PAGE_SHIFT;
  1523. struct kvm_memory_slot *memslot;
  1524. if (npages && old->npages) {
  1525. /*
  1526. * If modifying a memslot, reset all the rmap dirty bits.
  1527. * If this is a new memslot, we don't need to do anything
  1528. * since the rmap array starts out as all zeroes,
  1529. * i.e. no pages are dirty.
  1530. */
  1531. memslot = id_to_memslot(kvm->memslots, mem->slot);
  1532. kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
  1533. }
  1534. }
  1535. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  1536. {
  1537. int err = 0;
  1538. struct kvm *kvm = vcpu->kvm;
  1539. struct kvm_rma_info *ri = NULL;
  1540. unsigned long hva;
  1541. struct kvm_memory_slot *memslot;
  1542. struct vm_area_struct *vma;
  1543. unsigned long lpcr, senc;
  1544. unsigned long psize, porder;
  1545. unsigned long rma_size;
  1546. unsigned long rmls;
  1547. unsigned long *physp;
  1548. unsigned long i, npages;
  1549. int srcu_idx;
  1550. mutex_lock(&kvm->lock);
  1551. if (kvm->arch.rma_setup_done)
  1552. goto out; /* another vcpu beat us to it */
  1553. /* Allocate hashed page table (if not done already) and reset it */
  1554. if (!kvm->arch.hpt_virt) {
  1555. err = kvmppc_alloc_hpt(kvm, NULL);
  1556. if (err) {
  1557. pr_err("KVM: Couldn't alloc HPT\n");
  1558. goto out;
  1559. }
  1560. }
  1561. /* Look up the memslot for guest physical address 0 */
  1562. srcu_idx = srcu_read_lock(&kvm->srcu);
  1563. memslot = gfn_to_memslot(kvm, 0);
  1564. /* We must have some memory at 0 by now */
  1565. err = -EINVAL;
  1566. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1567. goto out_srcu;
  1568. /* Look up the VMA for the start of this memory slot */
  1569. hva = memslot->userspace_addr;
  1570. down_read(&current->mm->mmap_sem);
  1571. vma = find_vma(current->mm, hva);
  1572. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  1573. goto up_out;
  1574. psize = vma_kernel_pagesize(vma);
  1575. porder = __ilog2(psize);
  1576. /* Is this one of our preallocated RMAs? */
  1577. if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
  1578. hva == vma->vm_start)
  1579. ri = vma->vm_file->private_data;
  1580. up_read(&current->mm->mmap_sem);
  1581. if (!ri) {
  1582. /* On POWER7, use VRMA; on PPC970, give up */
  1583. err = -EPERM;
  1584. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1585. pr_err("KVM: CPU requires an RMO\n");
  1586. goto out_srcu;
  1587. }
  1588. /* We can handle 4k, 64k or 16M pages in the VRMA */
  1589. err = -EINVAL;
  1590. if (!(psize == 0x1000 || psize == 0x10000 ||
  1591. psize == 0x1000000))
  1592. goto out_srcu;
  1593. /* Update VRMASD field in the LPCR */
  1594. senc = slb_pgsize_encoding(psize);
  1595. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1596. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1597. lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
  1598. lpcr |= senc << (LPCR_VRMASD_SH - 4);
  1599. kvm->arch.lpcr = lpcr;
  1600. /* Create HPTEs in the hash page table for the VRMA */
  1601. kvmppc_map_vrma(vcpu, memslot, porder);
  1602. } else {
  1603. /* Set up to use an RMO region */
  1604. rma_size = kvm_rma_pages;
  1605. if (rma_size > memslot->npages)
  1606. rma_size = memslot->npages;
  1607. rma_size <<= PAGE_SHIFT;
  1608. rmls = lpcr_rmls(rma_size);
  1609. err = -EINVAL;
  1610. if ((long)rmls < 0) {
  1611. pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
  1612. goto out_srcu;
  1613. }
  1614. atomic_inc(&ri->use_count);
  1615. kvm->arch.rma = ri;
  1616. /* Update LPCR and RMOR */
  1617. lpcr = kvm->arch.lpcr;
  1618. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1619. /* PPC970; insert RMLS value (split field) in HID4 */
  1620. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  1621. (3ul << HID4_RMLS2_SH));
  1622. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  1623. ((rmls & 3) << HID4_RMLS2_SH);
  1624. /* RMOR is also in HID4 */
  1625. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1626. << HID4_RMOR_SH;
  1627. } else {
  1628. /* POWER7 */
  1629. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1630. lpcr |= rmls << LPCR_RMLS_SH;
  1631. kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
  1632. }
  1633. kvm->arch.lpcr = lpcr;
  1634. pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
  1635. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1636. /* Initialize phys addrs of pages in RMO */
  1637. npages = kvm_rma_pages;
  1638. porder = __ilog2(npages);
  1639. physp = memslot->arch.slot_phys;
  1640. if (physp) {
  1641. if (npages > memslot->npages)
  1642. npages = memslot->npages;
  1643. spin_lock(&kvm->arch.slot_phys_lock);
  1644. for (i = 0; i < npages; ++i)
  1645. physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
  1646. porder;
  1647. spin_unlock(&kvm->arch.slot_phys_lock);
  1648. }
  1649. }
  1650. /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
  1651. smp_wmb();
  1652. kvm->arch.rma_setup_done = 1;
  1653. err = 0;
  1654. out_srcu:
  1655. srcu_read_unlock(&kvm->srcu, srcu_idx);
  1656. out:
  1657. mutex_unlock(&kvm->lock);
  1658. return err;
  1659. up_out:
  1660. up_read(&current->mm->mmap_sem);
  1661. goto out_srcu;
  1662. }
  1663. int kvmppc_core_init_vm(struct kvm *kvm)
  1664. {
  1665. unsigned long lpcr, lpid;
  1666. /* Allocate the guest's logical partition ID */
  1667. lpid = kvmppc_alloc_lpid();
  1668. if ((long)lpid < 0)
  1669. return -ENOMEM;
  1670. kvm->arch.lpid = lpid;
  1671. /*
  1672. * Since we don't flush the TLB when tearing down a VM,
  1673. * and this lpid might have previously been used,
  1674. * make sure we flush on each core before running the new VM.
  1675. */
  1676. cpumask_setall(&kvm->arch.need_tlb_flush);
  1677. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1678. INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
  1679. kvm->arch.rma = NULL;
  1680. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1681. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1682. /* PPC970; HID4 is effectively the LPCR */
  1683. kvm->arch.host_lpid = 0;
  1684. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1685. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1686. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1687. ((lpid & 0xf) << HID4_LPID5_SH);
  1688. } else {
  1689. /* POWER7; init LPCR for virtual RMA mode */
  1690. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1691. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1692. lpcr &= LPCR_PECE | LPCR_LPES;
  1693. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1694. LPCR_VPM0 | LPCR_VPM1;
  1695. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  1696. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1697. }
  1698. kvm->arch.lpcr = lpcr;
  1699. kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
  1700. spin_lock_init(&kvm->arch.slot_phys_lock);
  1701. /*
  1702. * Don't allow secondary CPU threads to come online
  1703. * while any KVM VMs exist.
  1704. */
  1705. inhibit_secondary_onlining();
  1706. return 0;
  1707. }
  1708. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1709. {
  1710. uninhibit_secondary_onlining();
  1711. if (kvm->arch.rma) {
  1712. kvm_release_rma(kvm->arch.rma);
  1713. kvm->arch.rma = NULL;
  1714. }
  1715. kvmppc_rtas_tokens_free(kvm);
  1716. kvmppc_free_hpt(kvm);
  1717. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1718. }
  1719. /* These are stubs for now */
  1720. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1721. {
  1722. }
  1723. /* We don't need to emulate any privileged instructions or dcbz */
  1724. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1725. unsigned int inst, int *advance)
  1726. {
  1727. return EMULATE_FAIL;
  1728. }
  1729. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  1730. {
  1731. return EMULATE_FAIL;
  1732. }
  1733. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  1734. {
  1735. return EMULATE_FAIL;
  1736. }
  1737. static int kvmppc_book3s_hv_init(void)
  1738. {
  1739. int r;
  1740. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1741. if (r)
  1742. return r;
  1743. r = kvmppc_mmu_hv_init();
  1744. return r;
  1745. }
  1746. static void kvmppc_book3s_hv_exit(void)
  1747. {
  1748. kvm_exit();
  1749. }
  1750. module_init(kvmppc_book3s_hv_init);
  1751. module_exit(kvmppc_book3s_hv_exit);