book3s_hv.c 34 KB

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