book3s_hv.c 31 KB

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