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