book3s_hv.c 32 KB

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