book3s_hv.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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 <asm/reg.h>
  33. #include <asm/cputable.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. #include <asm/kvm_ppc.h>
  39. #include <asm/kvm_book3s.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/lppaca.h>
  42. #include <asm/processor.h>
  43. #include <asm/cputhreads.h>
  44. #include <asm/page.h>
  45. #include <asm/hvcall.h>
  46. #include <linux/gfp.h>
  47. #include <linux/sched.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/highmem.h>
  50. #include <linux/hugetlb.h>
  51. #define LARGE_PAGE_ORDER 24 /* 16MB pages */
  52. /* #define EXIT_DEBUG */
  53. /* #define EXIT_DEBUG_SIMPLE */
  54. /* #define EXIT_DEBUG_INT */
  55. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  56. static int kvmppc_hv_setup_rma(struct kvm_vcpu *vcpu);
  57. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  58. {
  59. local_paca->kvm_hstate.kvm_vcpu = vcpu;
  60. local_paca->kvm_hstate.kvm_vcore = vcpu->arch.vcore;
  61. }
  62. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  63. {
  64. }
  65. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
  66. {
  67. vcpu->arch.shregs.msr = msr;
  68. kvmppc_end_cede(vcpu);
  69. }
  70. void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
  71. {
  72. vcpu->arch.pvr = pvr;
  73. }
  74. void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  75. {
  76. int r;
  77. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  78. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  79. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  80. for (r = 0; r < 16; ++r)
  81. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  82. r, kvmppc_get_gpr(vcpu, r),
  83. r+16, kvmppc_get_gpr(vcpu, r+16));
  84. pr_err("ctr = %.16lx lr = %.16lx\n",
  85. vcpu->arch.ctr, vcpu->arch.lr);
  86. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  87. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  88. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  89. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  90. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  91. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  92. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  93. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  94. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  95. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  96. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  97. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  98. for (r = 0; r < vcpu->arch.slb_max; ++r)
  99. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  100. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  101. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  102. vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
  103. vcpu->arch.last_inst);
  104. }
  105. struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  106. {
  107. int r;
  108. struct kvm_vcpu *v, *ret = NULL;
  109. mutex_lock(&kvm->lock);
  110. kvm_for_each_vcpu(r, v, kvm) {
  111. if (v->vcpu_id == id) {
  112. ret = v;
  113. break;
  114. }
  115. }
  116. mutex_unlock(&kvm->lock);
  117. return ret;
  118. }
  119. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  120. {
  121. vpa->shared_proc = 1;
  122. vpa->yield_count = 1;
  123. }
  124. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  125. unsigned long flags,
  126. unsigned long vcpuid, unsigned long vpa)
  127. {
  128. struct kvm *kvm = vcpu->kvm;
  129. unsigned long len, nb;
  130. void *va;
  131. struct kvm_vcpu *tvcpu;
  132. int err = H_PARAMETER;
  133. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  134. if (!tvcpu)
  135. return H_PARAMETER;
  136. flags >>= 63 - 18;
  137. flags &= 7;
  138. if (flags == 0 || flags == 4)
  139. return H_PARAMETER;
  140. if (flags < 4) {
  141. if (vpa & 0x7f)
  142. return H_PARAMETER;
  143. if (flags >= 2 && !tvcpu->arch.vpa)
  144. return H_RESOURCE;
  145. /* registering new area; convert logical addr to real */
  146. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  147. if (va == NULL)
  148. return H_PARAMETER;
  149. if (flags <= 1)
  150. len = *(unsigned short *)(va + 4);
  151. else
  152. len = *(unsigned int *)(va + 4);
  153. if (len > nb)
  154. goto out_unpin;
  155. switch (flags) {
  156. case 1: /* register VPA */
  157. if (len < 640)
  158. goto out_unpin;
  159. if (tvcpu->arch.vpa)
  160. kvmppc_unpin_guest_page(kvm, vcpu->arch.vpa);
  161. tvcpu->arch.vpa = va;
  162. init_vpa(vcpu, va);
  163. break;
  164. case 2: /* register DTL */
  165. if (len < 48)
  166. goto out_unpin;
  167. len -= len % 48;
  168. if (tvcpu->arch.dtl)
  169. kvmppc_unpin_guest_page(kvm, vcpu->arch.dtl);
  170. tvcpu->arch.dtl = va;
  171. tvcpu->arch.dtl_end = va + len;
  172. break;
  173. case 3: /* register SLB shadow buffer */
  174. if (len < 16)
  175. goto out_unpin;
  176. if (tvcpu->arch.slb_shadow)
  177. kvmppc_unpin_guest_page(kvm, vcpu->arch.slb_shadow);
  178. tvcpu->arch.slb_shadow = va;
  179. break;
  180. }
  181. } else {
  182. switch (flags) {
  183. case 5: /* unregister VPA */
  184. if (tvcpu->arch.slb_shadow || tvcpu->arch.dtl)
  185. return H_RESOURCE;
  186. if (!tvcpu->arch.vpa)
  187. break;
  188. kvmppc_unpin_guest_page(kvm, tvcpu->arch.vpa);
  189. tvcpu->arch.vpa = NULL;
  190. break;
  191. case 6: /* unregister DTL */
  192. if (!tvcpu->arch.dtl)
  193. break;
  194. kvmppc_unpin_guest_page(kvm, tvcpu->arch.dtl);
  195. tvcpu->arch.dtl = NULL;
  196. break;
  197. case 7: /* unregister SLB shadow buffer */
  198. if (!tvcpu->arch.slb_shadow)
  199. break;
  200. kvmppc_unpin_guest_page(kvm, tvcpu->arch.slb_shadow);
  201. tvcpu->arch.slb_shadow = NULL;
  202. break;
  203. }
  204. }
  205. return H_SUCCESS;
  206. out_unpin:
  207. kvmppc_unpin_guest_page(kvm, va);
  208. return err;
  209. }
  210. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  211. {
  212. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  213. unsigned long target, ret = H_SUCCESS;
  214. struct kvm_vcpu *tvcpu;
  215. switch (req) {
  216. case H_ENTER:
  217. ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
  218. kvmppc_get_gpr(vcpu, 5),
  219. kvmppc_get_gpr(vcpu, 6),
  220. kvmppc_get_gpr(vcpu, 7));
  221. break;
  222. case H_CEDE:
  223. break;
  224. case H_PROD:
  225. target = kvmppc_get_gpr(vcpu, 4);
  226. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  227. if (!tvcpu) {
  228. ret = H_PARAMETER;
  229. break;
  230. }
  231. tvcpu->arch.prodded = 1;
  232. smp_mb();
  233. if (vcpu->arch.ceded) {
  234. if (waitqueue_active(&vcpu->wq)) {
  235. wake_up_interruptible(&vcpu->wq);
  236. vcpu->stat.halt_wakeup++;
  237. }
  238. }
  239. break;
  240. case H_CONFER:
  241. break;
  242. case H_REGISTER_VPA:
  243. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  244. kvmppc_get_gpr(vcpu, 5),
  245. kvmppc_get_gpr(vcpu, 6));
  246. break;
  247. default:
  248. return RESUME_HOST;
  249. }
  250. kvmppc_set_gpr(vcpu, 3, ret);
  251. vcpu->arch.hcall_needed = 0;
  252. return RESUME_GUEST;
  253. }
  254. static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  255. struct task_struct *tsk)
  256. {
  257. int r = RESUME_HOST;
  258. vcpu->stat.sum_exits++;
  259. run->exit_reason = KVM_EXIT_UNKNOWN;
  260. run->ready_for_interrupt_injection = 1;
  261. switch (vcpu->arch.trap) {
  262. /* We're good on these - the host merely wanted to get our attention */
  263. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  264. vcpu->stat.dec_exits++;
  265. r = RESUME_GUEST;
  266. break;
  267. case BOOK3S_INTERRUPT_EXTERNAL:
  268. vcpu->stat.ext_intr_exits++;
  269. r = RESUME_GUEST;
  270. break;
  271. case BOOK3S_INTERRUPT_PERFMON:
  272. r = RESUME_GUEST;
  273. break;
  274. case BOOK3S_INTERRUPT_PROGRAM:
  275. {
  276. ulong flags;
  277. /*
  278. * Normally program interrupts are delivered directly
  279. * to the guest by the hardware, but we can get here
  280. * as a result of a hypervisor emulation interrupt
  281. * (e40) getting turned into a 700 by BML RTAS.
  282. */
  283. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  284. kvmppc_core_queue_program(vcpu, flags);
  285. r = RESUME_GUEST;
  286. break;
  287. }
  288. case BOOK3S_INTERRUPT_SYSCALL:
  289. {
  290. /* hcall - punt to userspace */
  291. int i;
  292. if (vcpu->arch.shregs.msr & MSR_PR) {
  293. /* sc 1 from userspace - reflect to guest syscall */
  294. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
  295. r = RESUME_GUEST;
  296. break;
  297. }
  298. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  299. for (i = 0; i < 9; ++i)
  300. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  301. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  302. vcpu->arch.hcall_needed = 1;
  303. r = RESUME_HOST;
  304. break;
  305. }
  306. /*
  307. * We get these next two if the guest does a bad real-mode access,
  308. * as we have enabled VRMA (virtualized real mode area) mode in the
  309. * LPCR. We just generate an appropriate DSI/ISI to the guest.
  310. */
  311. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  312. vcpu->arch.shregs.dsisr = vcpu->arch.fault_dsisr;
  313. vcpu->arch.shregs.dar = vcpu->arch.fault_dar;
  314. kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE, 0);
  315. r = RESUME_GUEST;
  316. break;
  317. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  318. kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE,
  319. 0x08000000);
  320. r = RESUME_GUEST;
  321. break;
  322. /*
  323. * This occurs if the guest executes an illegal instruction.
  324. * We just generate a program interrupt to the guest, since
  325. * we don't emulate any guest instructions at this stage.
  326. */
  327. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  328. kvmppc_core_queue_program(vcpu, 0x80000);
  329. r = RESUME_GUEST;
  330. break;
  331. default:
  332. kvmppc_dump_regs(vcpu);
  333. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  334. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  335. vcpu->arch.shregs.msr);
  336. r = RESUME_HOST;
  337. BUG();
  338. break;
  339. }
  340. return r;
  341. }
  342. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  343. struct kvm_sregs *sregs)
  344. {
  345. int i;
  346. sregs->pvr = vcpu->arch.pvr;
  347. memset(sregs, 0, sizeof(struct kvm_sregs));
  348. for (i = 0; i < vcpu->arch.slb_max; i++) {
  349. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  350. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  351. }
  352. return 0;
  353. }
  354. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  355. struct kvm_sregs *sregs)
  356. {
  357. int i, j;
  358. kvmppc_set_pvr(vcpu, sregs->pvr);
  359. j = 0;
  360. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  361. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  362. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  363. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  364. ++j;
  365. }
  366. }
  367. vcpu->arch.slb_max = j;
  368. return 0;
  369. }
  370. int kvmppc_core_check_processor_compat(void)
  371. {
  372. if (cpu_has_feature(CPU_FTR_HVMODE))
  373. return 0;
  374. return -EIO;
  375. }
  376. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  377. {
  378. struct kvm_vcpu *vcpu;
  379. int err = -EINVAL;
  380. int core;
  381. struct kvmppc_vcore *vcore;
  382. core = id / threads_per_core;
  383. if (core >= KVM_MAX_VCORES)
  384. goto out;
  385. err = -ENOMEM;
  386. vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
  387. if (!vcpu)
  388. goto out;
  389. err = kvm_vcpu_init(vcpu, kvm, id);
  390. if (err)
  391. goto free_vcpu;
  392. vcpu->arch.shared = &vcpu->arch.shregs;
  393. vcpu->arch.last_cpu = -1;
  394. vcpu->arch.mmcr[0] = MMCR0_FC;
  395. vcpu->arch.ctrl = CTRL_RUNLATCH;
  396. /* default to host PVR, since we can't spoof it */
  397. vcpu->arch.pvr = mfspr(SPRN_PVR);
  398. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  399. kvmppc_mmu_book3s_hv_init(vcpu);
  400. /*
  401. * We consider the vcpu stopped until we see the first run ioctl for it.
  402. */
  403. vcpu->arch.state = KVMPPC_VCPU_STOPPED;
  404. init_waitqueue_head(&vcpu->arch.cpu_run);
  405. mutex_lock(&kvm->lock);
  406. vcore = kvm->arch.vcores[core];
  407. if (!vcore) {
  408. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  409. if (vcore) {
  410. INIT_LIST_HEAD(&vcore->runnable_threads);
  411. spin_lock_init(&vcore->lock);
  412. init_waitqueue_head(&vcore->wq);
  413. }
  414. kvm->arch.vcores[core] = vcore;
  415. }
  416. mutex_unlock(&kvm->lock);
  417. if (!vcore)
  418. goto free_vcpu;
  419. spin_lock(&vcore->lock);
  420. ++vcore->num_threads;
  421. spin_unlock(&vcore->lock);
  422. vcpu->arch.vcore = vcore;
  423. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  424. kvmppc_sanity_check(vcpu);
  425. return vcpu;
  426. free_vcpu:
  427. kfree(vcpu);
  428. out:
  429. return ERR_PTR(err);
  430. }
  431. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  432. {
  433. if (vcpu->arch.dtl)
  434. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl);
  435. if (vcpu->arch.slb_shadow)
  436. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow);
  437. if (vcpu->arch.vpa)
  438. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa);
  439. kvm_vcpu_uninit(vcpu);
  440. kfree(vcpu);
  441. }
  442. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  443. {
  444. unsigned long dec_nsec, now;
  445. now = get_tb();
  446. if (now > vcpu->arch.dec_expires) {
  447. /* decrementer has already gone negative */
  448. kvmppc_core_queue_dec(vcpu);
  449. kvmppc_core_prepare_to_enter(vcpu);
  450. return;
  451. }
  452. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  453. / tb_ticks_per_sec;
  454. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  455. HRTIMER_MODE_REL);
  456. vcpu->arch.timer_running = 1;
  457. }
  458. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  459. {
  460. vcpu->arch.ceded = 0;
  461. if (vcpu->arch.timer_running) {
  462. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  463. vcpu->arch.timer_running = 0;
  464. }
  465. }
  466. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  467. extern void xics_wake_cpu(int cpu);
  468. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  469. struct kvm_vcpu *vcpu)
  470. {
  471. struct kvm_vcpu *v;
  472. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  473. return;
  474. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  475. --vc->n_runnable;
  476. ++vc->n_busy;
  477. /* decrement the physical thread id of each following vcpu */
  478. v = vcpu;
  479. list_for_each_entry_continue(v, &vc->runnable_threads, arch.run_list)
  480. --v->arch.ptid;
  481. list_del(&vcpu->arch.run_list);
  482. }
  483. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  484. {
  485. int cpu;
  486. struct paca_struct *tpaca;
  487. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  488. if (vcpu->arch.timer_running) {
  489. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  490. vcpu->arch.timer_running = 0;
  491. }
  492. cpu = vc->pcpu + vcpu->arch.ptid;
  493. tpaca = &paca[cpu];
  494. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  495. tpaca->kvm_hstate.kvm_vcore = vc;
  496. tpaca->kvm_hstate.napping = 0;
  497. vcpu->cpu = vc->pcpu;
  498. smp_wmb();
  499. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  500. if (vcpu->arch.ptid) {
  501. tpaca->cpu_start = 0x80;
  502. wmb();
  503. xics_wake_cpu(cpu);
  504. ++vc->n_woken;
  505. }
  506. #endif
  507. }
  508. static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
  509. {
  510. int i;
  511. HMT_low();
  512. i = 0;
  513. while (vc->nap_count < vc->n_woken) {
  514. if (++i >= 1000000) {
  515. pr_err("kvmppc_wait_for_nap timeout %d %d\n",
  516. vc->nap_count, vc->n_woken);
  517. break;
  518. }
  519. cpu_relax();
  520. }
  521. HMT_medium();
  522. }
  523. /*
  524. * Check that we are on thread 0 and that any other threads in
  525. * this core are off-line.
  526. */
  527. static int on_primary_thread(void)
  528. {
  529. int cpu = smp_processor_id();
  530. int thr = cpu_thread_in_core(cpu);
  531. if (thr)
  532. return 0;
  533. while (++thr < threads_per_core)
  534. if (cpu_online(cpu + thr))
  535. return 0;
  536. return 1;
  537. }
  538. /*
  539. * Run a set of guest threads on a physical core.
  540. * Called with vc->lock held.
  541. */
  542. static int kvmppc_run_core(struct kvmppc_vcore *vc)
  543. {
  544. struct kvm_vcpu *vcpu, *vcpu0, *vnext;
  545. long ret;
  546. u64 now;
  547. int ptid;
  548. /* don't start if any threads have a signal pending */
  549. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  550. if (signal_pending(vcpu->arch.run_task))
  551. return 0;
  552. /*
  553. * Make sure we are running on thread 0, and that
  554. * secondary threads are offline.
  555. * XXX we should also block attempts to bring any
  556. * secondary threads online.
  557. */
  558. if (threads_per_core > 1 && !on_primary_thread()) {
  559. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  560. vcpu->arch.ret = -EBUSY;
  561. goto out;
  562. }
  563. /*
  564. * Assign physical thread IDs, first to non-ceded vcpus
  565. * and then to ceded ones.
  566. */
  567. ptid = 0;
  568. vcpu0 = NULL;
  569. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  570. if (!vcpu->arch.ceded) {
  571. if (!ptid)
  572. vcpu0 = vcpu;
  573. vcpu->arch.ptid = ptid++;
  574. }
  575. }
  576. if (!vcpu0)
  577. return 0; /* nothing to run */
  578. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  579. if (vcpu->arch.ceded)
  580. vcpu->arch.ptid = ptid++;
  581. vc->n_woken = 0;
  582. vc->nap_count = 0;
  583. vc->entry_exit_count = 0;
  584. vc->vcore_state = VCORE_RUNNING;
  585. vc->in_guest = 0;
  586. vc->pcpu = smp_processor_id();
  587. vc->napping_threads = 0;
  588. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  589. kvmppc_start_thread(vcpu);
  590. preempt_disable();
  591. spin_unlock(&vc->lock);
  592. kvm_guest_enter();
  593. __kvmppc_vcore_entry(NULL, vcpu0);
  594. spin_lock(&vc->lock);
  595. /* disable sending of IPIs on virtual external irqs */
  596. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  597. vcpu->cpu = -1;
  598. /* wait for secondary threads to finish writing their state to memory */
  599. if (vc->nap_count < vc->n_woken)
  600. kvmppc_wait_for_nap(vc);
  601. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  602. vc->vcore_state = VCORE_EXITING;
  603. spin_unlock(&vc->lock);
  604. /* make sure updates to secondary vcpu structs are visible now */
  605. smp_mb();
  606. kvm_guest_exit();
  607. preempt_enable();
  608. kvm_resched(vcpu);
  609. now = get_tb();
  610. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  611. /* cancel pending dec exception if dec is positive */
  612. if (now < vcpu->arch.dec_expires &&
  613. kvmppc_core_pending_dec(vcpu))
  614. kvmppc_core_dequeue_dec(vcpu);
  615. ret = RESUME_GUEST;
  616. if (vcpu->arch.trap)
  617. ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
  618. vcpu->arch.run_task);
  619. vcpu->arch.ret = ret;
  620. vcpu->arch.trap = 0;
  621. if (vcpu->arch.ceded) {
  622. if (ret != RESUME_GUEST)
  623. kvmppc_end_cede(vcpu);
  624. else
  625. kvmppc_set_timer(vcpu);
  626. }
  627. }
  628. spin_lock(&vc->lock);
  629. out:
  630. vc->vcore_state = VCORE_INACTIVE;
  631. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  632. arch.run_list) {
  633. if (vcpu->arch.ret != RESUME_GUEST) {
  634. kvmppc_remove_runnable(vc, vcpu);
  635. wake_up(&vcpu->arch.cpu_run);
  636. }
  637. }
  638. return 1;
  639. }
  640. /*
  641. * Wait for some other vcpu thread to execute us, and
  642. * wake us up when we need to handle something in the host.
  643. */
  644. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  645. {
  646. DEFINE_WAIT(wait);
  647. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  648. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  649. schedule();
  650. finish_wait(&vcpu->arch.cpu_run, &wait);
  651. }
  652. /*
  653. * All the vcpus in this vcore are idle, so wait for a decrementer
  654. * or external interrupt to one of the vcpus. vc->lock is held.
  655. */
  656. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  657. {
  658. DEFINE_WAIT(wait);
  659. struct kvm_vcpu *v;
  660. int all_idle = 1;
  661. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  662. vc->vcore_state = VCORE_SLEEPING;
  663. spin_unlock(&vc->lock);
  664. list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
  665. if (!v->arch.ceded || v->arch.pending_exceptions) {
  666. all_idle = 0;
  667. break;
  668. }
  669. }
  670. if (all_idle)
  671. schedule();
  672. finish_wait(&vc->wq, &wait);
  673. spin_lock(&vc->lock);
  674. vc->vcore_state = VCORE_INACTIVE;
  675. }
  676. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  677. {
  678. int n_ceded;
  679. int prev_state;
  680. struct kvmppc_vcore *vc;
  681. struct kvm_vcpu *v, *vn;
  682. kvm_run->exit_reason = 0;
  683. vcpu->arch.ret = RESUME_GUEST;
  684. vcpu->arch.trap = 0;
  685. /*
  686. * Synchronize with other threads in this virtual core
  687. */
  688. vc = vcpu->arch.vcore;
  689. spin_lock(&vc->lock);
  690. vcpu->arch.ceded = 0;
  691. vcpu->arch.run_task = current;
  692. vcpu->arch.kvm_run = kvm_run;
  693. prev_state = vcpu->arch.state;
  694. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  695. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  696. ++vc->n_runnable;
  697. /*
  698. * This happens the first time this is called for a vcpu.
  699. * If the vcore is already running, we may be able to start
  700. * this thread straight away and have it join in.
  701. */
  702. if (prev_state == KVMPPC_VCPU_STOPPED) {
  703. if (vc->vcore_state == VCORE_RUNNING &&
  704. VCORE_EXIT_COUNT(vc) == 0) {
  705. vcpu->arch.ptid = vc->n_runnable - 1;
  706. kvmppc_start_thread(vcpu);
  707. }
  708. } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
  709. --vc->n_busy;
  710. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  711. !signal_pending(current)) {
  712. if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
  713. spin_unlock(&vc->lock);
  714. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  715. spin_lock(&vc->lock);
  716. continue;
  717. }
  718. n_ceded = 0;
  719. list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
  720. n_ceded += v->arch.ceded;
  721. if (n_ceded == vc->n_runnable)
  722. kvmppc_vcore_blocked(vc);
  723. else
  724. kvmppc_run_core(vc);
  725. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  726. arch.run_list) {
  727. kvmppc_core_prepare_to_enter(v);
  728. if (signal_pending(v->arch.run_task)) {
  729. kvmppc_remove_runnable(vc, v);
  730. v->stat.signal_exits++;
  731. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  732. v->arch.ret = -EINTR;
  733. wake_up(&v->arch.cpu_run);
  734. }
  735. }
  736. }
  737. if (signal_pending(current)) {
  738. if (vc->vcore_state == VCORE_RUNNING ||
  739. vc->vcore_state == VCORE_EXITING) {
  740. spin_unlock(&vc->lock);
  741. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  742. spin_lock(&vc->lock);
  743. }
  744. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  745. kvmppc_remove_runnable(vc, vcpu);
  746. vcpu->stat.signal_exits++;
  747. kvm_run->exit_reason = KVM_EXIT_INTR;
  748. vcpu->arch.ret = -EINTR;
  749. }
  750. }
  751. spin_unlock(&vc->lock);
  752. return vcpu->arch.ret;
  753. }
  754. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  755. {
  756. int r;
  757. if (!vcpu->arch.sane) {
  758. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  759. return -EINVAL;
  760. }
  761. kvmppc_core_prepare_to_enter(vcpu);
  762. /* No need to go into the guest when all we'll do is come back out */
  763. if (signal_pending(current)) {
  764. run->exit_reason = KVM_EXIT_INTR;
  765. return -EINTR;
  766. }
  767. /* On the first time here, set up VRMA or RMA */
  768. if (!vcpu->kvm->arch.rma_setup_done) {
  769. r = kvmppc_hv_setup_rma(vcpu);
  770. if (r)
  771. return r;
  772. }
  773. flush_fp_to_thread(current);
  774. flush_altivec_to_thread(current);
  775. flush_vsx_to_thread(current);
  776. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  777. do {
  778. r = kvmppc_run_vcpu(run, vcpu);
  779. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  780. !(vcpu->arch.shregs.msr & MSR_PR)) {
  781. r = kvmppc_pseries_do_hcall(vcpu);
  782. kvmppc_core_prepare_to_enter(vcpu);
  783. }
  784. } while (r == RESUME_GUEST);
  785. return r;
  786. }
  787. static long kvmppc_stt_npages(unsigned long window_size)
  788. {
  789. return ALIGN((window_size >> SPAPR_TCE_SHIFT)
  790. * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
  791. }
  792. static void release_spapr_tce_table(struct kvmppc_spapr_tce_table *stt)
  793. {
  794. struct kvm *kvm = stt->kvm;
  795. int i;
  796. mutex_lock(&kvm->lock);
  797. list_del(&stt->list);
  798. for (i = 0; i < kvmppc_stt_npages(stt->window_size); i++)
  799. __free_page(stt->pages[i]);
  800. kfree(stt);
  801. mutex_unlock(&kvm->lock);
  802. kvm_put_kvm(kvm);
  803. }
  804. static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  805. {
  806. struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
  807. struct page *page;
  808. if (vmf->pgoff >= kvmppc_stt_npages(stt->window_size))
  809. return VM_FAULT_SIGBUS;
  810. page = stt->pages[vmf->pgoff];
  811. get_page(page);
  812. vmf->page = page;
  813. return 0;
  814. }
  815. static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
  816. .fault = kvm_spapr_tce_fault,
  817. };
  818. static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
  819. {
  820. vma->vm_ops = &kvm_spapr_tce_vm_ops;
  821. return 0;
  822. }
  823. static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
  824. {
  825. struct kvmppc_spapr_tce_table *stt = filp->private_data;
  826. release_spapr_tce_table(stt);
  827. return 0;
  828. }
  829. static struct file_operations kvm_spapr_tce_fops = {
  830. .mmap = kvm_spapr_tce_mmap,
  831. .release = kvm_spapr_tce_release,
  832. };
  833. long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  834. struct kvm_create_spapr_tce *args)
  835. {
  836. struct kvmppc_spapr_tce_table *stt = NULL;
  837. long npages;
  838. int ret = -ENOMEM;
  839. int i;
  840. /* Check this LIOBN hasn't been previously allocated */
  841. list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
  842. if (stt->liobn == args->liobn)
  843. return -EBUSY;
  844. }
  845. npages = kvmppc_stt_npages(args->window_size);
  846. stt = kzalloc(sizeof(*stt) + npages* sizeof(struct page *),
  847. GFP_KERNEL);
  848. if (!stt)
  849. goto fail;
  850. stt->liobn = args->liobn;
  851. stt->window_size = args->window_size;
  852. stt->kvm = kvm;
  853. for (i = 0; i < npages; i++) {
  854. stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  855. if (!stt->pages[i])
  856. goto fail;
  857. }
  858. kvm_get_kvm(kvm);
  859. mutex_lock(&kvm->lock);
  860. list_add(&stt->list, &kvm->arch.spapr_tce_tables);
  861. mutex_unlock(&kvm->lock);
  862. return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
  863. stt, O_RDWR);
  864. fail:
  865. if (stt) {
  866. for (i = 0; i < npages; i++)
  867. if (stt->pages[i])
  868. __free_page(stt->pages[i]);
  869. kfree(stt);
  870. }
  871. return ret;
  872. }
  873. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  874. Assumes POWER7 or PPC970. */
  875. static inline int lpcr_rmls(unsigned long rma_size)
  876. {
  877. switch (rma_size) {
  878. case 32ul << 20: /* 32 MB */
  879. if (cpu_has_feature(CPU_FTR_ARCH_206))
  880. return 8; /* only supported on POWER7 */
  881. return -1;
  882. case 64ul << 20: /* 64 MB */
  883. return 3;
  884. case 128ul << 20: /* 128 MB */
  885. return 7;
  886. case 256ul << 20: /* 256 MB */
  887. return 4;
  888. case 1ul << 30: /* 1 GB */
  889. return 2;
  890. case 16ul << 30: /* 16 GB */
  891. return 1;
  892. case 256ul << 30: /* 256 GB */
  893. return 0;
  894. default:
  895. return -1;
  896. }
  897. }
  898. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  899. {
  900. struct kvmppc_rma_info *ri = vma->vm_file->private_data;
  901. struct page *page;
  902. if (vmf->pgoff >= ri->npages)
  903. return VM_FAULT_SIGBUS;
  904. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  905. get_page(page);
  906. vmf->page = page;
  907. return 0;
  908. }
  909. static const struct vm_operations_struct kvm_rma_vm_ops = {
  910. .fault = kvm_rma_fault,
  911. };
  912. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  913. {
  914. vma->vm_flags |= VM_RESERVED;
  915. vma->vm_ops = &kvm_rma_vm_ops;
  916. return 0;
  917. }
  918. static int kvm_rma_release(struct inode *inode, struct file *filp)
  919. {
  920. struct kvmppc_rma_info *ri = filp->private_data;
  921. kvm_release_rma(ri);
  922. return 0;
  923. }
  924. static struct file_operations kvm_rma_fops = {
  925. .mmap = kvm_rma_mmap,
  926. .release = kvm_rma_release,
  927. };
  928. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  929. {
  930. struct kvmppc_rma_info *ri;
  931. long fd;
  932. ri = kvm_alloc_rma();
  933. if (!ri)
  934. return -ENOMEM;
  935. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
  936. if (fd < 0)
  937. kvm_release_rma(ri);
  938. ret->rma_size = ri->npages << PAGE_SHIFT;
  939. return fd;
  940. }
  941. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  942. struct kvm_userspace_memory_region *mem)
  943. {
  944. unsigned long psize;
  945. unsigned long npages;
  946. unsigned long *phys;
  947. /* For now, only allow 16MB-aligned slots */
  948. psize = kvm->arch.ram_psize;
  949. if ((mem->memory_size & (psize - 1)) ||
  950. (mem->guest_phys_addr & (psize - 1))) {
  951. pr_err("bad memory_size=%llx @ %llx\n",
  952. mem->memory_size, mem->guest_phys_addr);
  953. return -EINVAL;
  954. }
  955. /* Allocate a slot_phys array */
  956. npages = mem->memory_size >> kvm->arch.ram_porder;
  957. phys = kvm->arch.slot_phys[mem->slot];
  958. if (!phys) {
  959. phys = vzalloc(npages * sizeof(unsigned long));
  960. if (!phys)
  961. return -ENOMEM;
  962. kvm->arch.slot_phys[mem->slot] = phys;
  963. kvm->arch.slot_npages[mem->slot] = npages;
  964. }
  965. return 0;
  966. }
  967. static void unpin_slot(struct kvm *kvm, int slot_id)
  968. {
  969. unsigned long *physp;
  970. unsigned long j, npages, pfn;
  971. struct page *page;
  972. physp = kvm->arch.slot_phys[slot_id];
  973. npages = kvm->arch.slot_npages[slot_id];
  974. if (physp) {
  975. spin_lock(&kvm->arch.slot_phys_lock);
  976. for (j = 0; j < npages; j++) {
  977. if (!(physp[j] & KVMPPC_GOT_PAGE))
  978. continue;
  979. pfn = physp[j] >> PAGE_SHIFT;
  980. page = pfn_to_page(pfn);
  981. SetPageDirty(page);
  982. put_page(page);
  983. }
  984. kvm->arch.slot_phys[slot_id] = NULL;
  985. spin_unlock(&kvm->arch.slot_phys_lock);
  986. vfree(physp);
  987. }
  988. }
  989. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  990. struct kvm_userspace_memory_region *mem)
  991. {
  992. }
  993. static int kvmppc_hv_setup_rma(struct kvm_vcpu *vcpu)
  994. {
  995. int err = 0;
  996. struct kvm *kvm = vcpu->kvm;
  997. struct kvmppc_rma_info *ri = NULL;
  998. unsigned long hva;
  999. struct kvm_memory_slot *memslot;
  1000. struct vm_area_struct *vma;
  1001. unsigned long lpcr;
  1002. unsigned long psize, porder;
  1003. unsigned long rma_size;
  1004. unsigned long rmls;
  1005. unsigned long *physp;
  1006. unsigned long i, npages, pa;
  1007. mutex_lock(&kvm->lock);
  1008. if (kvm->arch.rma_setup_done)
  1009. goto out; /* another vcpu beat us to it */
  1010. /* Look up the memslot for guest physical address 0 */
  1011. memslot = gfn_to_memslot(kvm, 0);
  1012. /* We must have some memory at 0 by now */
  1013. err = -EINVAL;
  1014. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1015. goto out;
  1016. /* Look up the VMA for the start of this memory slot */
  1017. hva = memslot->userspace_addr;
  1018. down_read(&current->mm->mmap_sem);
  1019. vma = find_vma(current->mm, hva);
  1020. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  1021. goto up_out;
  1022. psize = vma_kernel_pagesize(vma);
  1023. if (psize != kvm->arch.ram_psize)
  1024. goto up_out;
  1025. /* Is this one of our preallocated RMAs? */
  1026. if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
  1027. hva == vma->vm_start)
  1028. ri = vma->vm_file->private_data;
  1029. up_read(&current->mm->mmap_sem);
  1030. if (!ri) {
  1031. /* On POWER7, use VRMA; on PPC970, give up */
  1032. err = -EPERM;
  1033. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1034. pr_err("KVM: CPU requires an RMO\n");
  1035. goto out;
  1036. }
  1037. /* Update VRMASD field in the LPCR */
  1038. lpcr = kvm->arch.lpcr & ~(0x1fUL << LPCR_VRMASD_SH);
  1039. lpcr |= LPCR_VRMA_L;
  1040. kvm->arch.lpcr = lpcr;
  1041. /* Create HPTEs in the hash page table for the VRMA */
  1042. kvmppc_map_vrma(vcpu, memslot);
  1043. } else {
  1044. /* Set up to use an RMO region */
  1045. rma_size = ri->npages;
  1046. if (rma_size > memslot->npages)
  1047. rma_size = memslot->npages;
  1048. rma_size <<= PAGE_SHIFT;
  1049. rmls = lpcr_rmls(rma_size);
  1050. err = -EINVAL;
  1051. if (rmls < 0) {
  1052. pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
  1053. goto out;
  1054. }
  1055. atomic_inc(&ri->use_count);
  1056. kvm->arch.rma = ri;
  1057. /* Update LPCR and RMOR */
  1058. lpcr = kvm->arch.lpcr;
  1059. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1060. /* PPC970; insert RMLS value (split field) in HID4 */
  1061. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  1062. (3ul << HID4_RMLS2_SH));
  1063. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  1064. ((rmls & 3) << HID4_RMLS2_SH);
  1065. /* RMOR is also in HID4 */
  1066. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1067. << HID4_RMOR_SH;
  1068. } else {
  1069. /* POWER7 */
  1070. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1071. lpcr |= rmls << LPCR_RMLS_SH;
  1072. kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
  1073. }
  1074. kvm->arch.lpcr = lpcr;
  1075. pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
  1076. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1077. /* Initialize phys addrs of pages in RMO */
  1078. porder = kvm->arch.ram_porder;
  1079. npages = rma_size >> porder;
  1080. pa = ri->base_pfn << PAGE_SHIFT;
  1081. physp = kvm->arch.slot_phys[memslot->id];
  1082. spin_lock(&kvm->arch.slot_phys_lock);
  1083. for (i = 0; i < npages; ++i)
  1084. physp[i] = pa + (i << porder);
  1085. spin_unlock(&kvm->arch.slot_phys_lock);
  1086. }
  1087. /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
  1088. smp_wmb();
  1089. kvm->arch.rma_setup_done = 1;
  1090. err = 0;
  1091. out:
  1092. mutex_unlock(&kvm->lock);
  1093. return err;
  1094. up_out:
  1095. up_read(&current->mm->mmap_sem);
  1096. goto out;
  1097. }
  1098. int kvmppc_core_init_vm(struct kvm *kvm)
  1099. {
  1100. long r;
  1101. unsigned long lpcr;
  1102. /* Allocate hashed page table */
  1103. r = kvmppc_alloc_hpt(kvm);
  1104. if (r)
  1105. return r;
  1106. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1107. kvm->arch.ram_psize = 1ul << LARGE_PAGE_ORDER;
  1108. kvm->arch.ram_porder = LARGE_PAGE_ORDER;
  1109. kvm->arch.rma = NULL;
  1110. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1111. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1112. /* PPC970; HID4 is effectively the LPCR */
  1113. unsigned long lpid = kvm->arch.lpid;
  1114. kvm->arch.host_lpid = 0;
  1115. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1116. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1117. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1118. ((lpid & 0xf) << HID4_LPID5_SH);
  1119. } else {
  1120. /* POWER7; init LPCR for virtual RMA mode */
  1121. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1122. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1123. lpcr &= LPCR_PECE | LPCR_LPES;
  1124. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1125. LPCR_VPM0 | LPCR_VRMA_L;
  1126. }
  1127. kvm->arch.lpcr = lpcr;
  1128. spin_lock_init(&kvm->arch.slot_phys_lock);
  1129. return 0;
  1130. }
  1131. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1132. {
  1133. unsigned long i;
  1134. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  1135. unpin_slot(kvm, i);
  1136. if (kvm->arch.rma) {
  1137. kvm_release_rma(kvm->arch.rma);
  1138. kvm->arch.rma = NULL;
  1139. }
  1140. kvmppc_free_hpt(kvm);
  1141. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1142. }
  1143. /* These are stubs for now */
  1144. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1145. {
  1146. }
  1147. /* We don't need to emulate any privileged instructions or dcbz */
  1148. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1149. unsigned int inst, int *advance)
  1150. {
  1151. return EMULATE_FAIL;
  1152. }
  1153. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  1154. {
  1155. return EMULATE_FAIL;
  1156. }
  1157. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  1158. {
  1159. return EMULATE_FAIL;
  1160. }
  1161. static int kvmppc_book3s_hv_init(void)
  1162. {
  1163. int r;
  1164. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1165. if (r)
  1166. return r;
  1167. r = kvmppc_mmu_hv_init();
  1168. return r;
  1169. }
  1170. static void kvmppc_book3s_hv_exit(void)
  1171. {
  1172. kvm_exit();
  1173. }
  1174. module_init(kvmppc_book3s_hv_init);
  1175. module_exit(kvmppc_book3s_hv_exit);