book3s_hv.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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/fs.h>
  27. #include <linux/anon_inodes.h>
  28. #include <linux/cpumask.h>
  29. #include <asm/reg.h>
  30. #include <asm/cputable.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/io.h>
  35. #include <asm/kvm_ppc.h>
  36. #include <asm/kvm_book3s.h>
  37. #include <asm/mmu_context.h>
  38. #include <asm/lppaca.h>
  39. #include <asm/processor.h>
  40. #include <linux/gfp.h>
  41. #include <linux/sched.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/highmem.h>
  44. /* #define EXIT_DEBUG */
  45. /* #define EXIT_DEBUG_SIMPLE */
  46. /* #define EXIT_DEBUG_INT */
  47. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  48. {
  49. local_paca->kvm_hstate.kvm_vcpu = vcpu;
  50. }
  51. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  52. {
  53. }
  54. void kvmppc_vcpu_block(struct kvm_vcpu *vcpu)
  55. {
  56. u64 now;
  57. unsigned long dec_nsec;
  58. now = get_tb();
  59. if (now >= vcpu->arch.dec_expires && !kvmppc_core_pending_dec(vcpu))
  60. kvmppc_core_queue_dec(vcpu);
  61. if (vcpu->arch.pending_exceptions)
  62. return;
  63. if (vcpu->arch.dec_expires != ~(u64)0) {
  64. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC /
  65. tb_ticks_per_sec;
  66. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  67. HRTIMER_MODE_REL);
  68. }
  69. kvm_vcpu_block(vcpu);
  70. vcpu->stat.halt_wakeup++;
  71. if (vcpu->arch.dec_expires != ~(u64)0)
  72. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  73. }
  74. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
  75. {
  76. vcpu->arch.shregs.msr = msr;
  77. }
  78. void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
  79. {
  80. vcpu->arch.pvr = pvr;
  81. }
  82. void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  83. {
  84. int r;
  85. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  86. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  87. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  88. for (r = 0; r < 16; ++r)
  89. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  90. r, kvmppc_get_gpr(vcpu, r),
  91. r+16, kvmppc_get_gpr(vcpu, r+16));
  92. pr_err("ctr = %.16lx lr = %.16lx\n",
  93. vcpu->arch.ctr, vcpu->arch.lr);
  94. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  95. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  96. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  97. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  98. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  99. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  100. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  101. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  102. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  103. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  104. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  105. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  106. for (r = 0; r < vcpu->arch.slb_max; ++r)
  107. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  108. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  109. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  110. vcpu->arch.lpcr, vcpu->kvm->arch.sdr1,
  111. vcpu->arch.last_inst);
  112. }
  113. struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  114. {
  115. int r;
  116. struct kvm_vcpu *v, *ret = NULL;
  117. mutex_lock(&kvm->lock);
  118. kvm_for_each_vcpu(r, v, kvm) {
  119. if (v->vcpu_id == id) {
  120. ret = v;
  121. break;
  122. }
  123. }
  124. mutex_unlock(&kvm->lock);
  125. return ret;
  126. }
  127. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  128. {
  129. vpa->shared_proc = 1;
  130. vpa->yield_count = 1;
  131. }
  132. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  133. unsigned long flags,
  134. unsigned long vcpuid, unsigned long vpa)
  135. {
  136. struct kvm *kvm = vcpu->kvm;
  137. unsigned long pg_index, ra, len;
  138. unsigned long pg_offset;
  139. void *va;
  140. struct kvm_vcpu *tvcpu;
  141. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  142. if (!tvcpu)
  143. return H_PARAMETER;
  144. flags >>= 63 - 18;
  145. flags &= 7;
  146. if (flags == 0 || flags == 4)
  147. return H_PARAMETER;
  148. if (flags < 4) {
  149. if (vpa & 0x7f)
  150. return H_PARAMETER;
  151. /* registering new area; convert logical addr to real */
  152. pg_index = vpa >> kvm->arch.ram_porder;
  153. pg_offset = vpa & (kvm->arch.ram_psize - 1);
  154. if (pg_index >= kvm->arch.ram_npages)
  155. return H_PARAMETER;
  156. if (kvm->arch.ram_pginfo[pg_index].pfn == 0)
  157. return H_PARAMETER;
  158. ra = kvm->arch.ram_pginfo[pg_index].pfn << PAGE_SHIFT;
  159. ra |= pg_offset;
  160. va = __va(ra);
  161. if (flags <= 1)
  162. len = *(unsigned short *)(va + 4);
  163. else
  164. len = *(unsigned int *)(va + 4);
  165. if (pg_offset + len > kvm->arch.ram_psize)
  166. return H_PARAMETER;
  167. switch (flags) {
  168. case 1: /* register VPA */
  169. if (len < 640)
  170. return H_PARAMETER;
  171. tvcpu->arch.vpa = va;
  172. init_vpa(vcpu, va);
  173. break;
  174. case 2: /* register DTL */
  175. if (len < 48)
  176. return H_PARAMETER;
  177. if (!tvcpu->arch.vpa)
  178. return H_RESOURCE;
  179. len -= len % 48;
  180. tvcpu->arch.dtl = va;
  181. tvcpu->arch.dtl_end = va + len;
  182. break;
  183. case 3: /* register SLB shadow buffer */
  184. if (len < 8)
  185. return H_PARAMETER;
  186. if (!tvcpu->arch.vpa)
  187. return H_RESOURCE;
  188. tvcpu->arch.slb_shadow = va;
  189. len = (len - 16) / 16;
  190. tvcpu->arch.slb_shadow = va;
  191. break;
  192. }
  193. } else {
  194. switch (flags) {
  195. case 5: /* unregister VPA */
  196. if (tvcpu->arch.slb_shadow || tvcpu->arch.dtl)
  197. return H_RESOURCE;
  198. tvcpu->arch.vpa = NULL;
  199. break;
  200. case 6: /* unregister DTL */
  201. tvcpu->arch.dtl = NULL;
  202. break;
  203. case 7: /* unregister SLB shadow buffer */
  204. tvcpu->arch.slb_shadow = NULL;
  205. break;
  206. }
  207. }
  208. return H_SUCCESS;
  209. }
  210. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  211. {
  212. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  213. unsigned long target, ret = H_SUCCESS;
  214. struct kvm_vcpu *tvcpu;
  215. switch (req) {
  216. case H_CEDE:
  217. vcpu->arch.shregs.msr |= MSR_EE;
  218. vcpu->arch.ceded = 1;
  219. smp_mb();
  220. if (!vcpu->arch.prodded)
  221. kvmppc_vcpu_block(vcpu);
  222. else
  223. vcpu->arch.prodded = 0;
  224. smp_mb();
  225. vcpu->arch.ceded = 0;
  226. break;
  227. case H_PROD:
  228. target = kvmppc_get_gpr(vcpu, 4);
  229. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  230. if (!tvcpu) {
  231. ret = H_PARAMETER;
  232. break;
  233. }
  234. tvcpu->arch.prodded = 1;
  235. smp_mb();
  236. if (vcpu->arch.ceded) {
  237. if (waitqueue_active(&vcpu->wq)) {
  238. wake_up_interruptible(&vcpu->wq);
  239. vcpu->stat.halt_wakeup++;
  240. }
  241. }
  242. break;
  243. case H_CONFER:
  244. break;
  245. case H_REGISTER_VPA:
  246. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  247. kvmppc_get_gpr(vcpu, 5),
  248. kvmppc_get_gpr(vcpu, 6));
  249. break;
  250. default:
  251. return RESUME_HOST;
  252. }
  253. kvmppc_set_gpr(vcpu, 3, ret);
  254. vcpu->arch.hcall_needed = 0;
  255. return RESUME_GUEST;
  256. }
  257. static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  258. struct task_struct *tsk)
  259. {
  260. int r = RESUME_HOST;
  261. vcpu->stat.sum_exits++;
  262. run->exit_reason = KVM_EXIT_UNKNOWN;
  263. run->ready_for_interrupt_injection = 1;
  264. switch (vcpu->arch.trap) {
  265. /* We're good on these - the host merely wanted to get our attention */
  266. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  267. vcpu->stat.dec_exits++;
  268. r = RESUME_GUEST;
  269. break;
  270. case BOOK3S_INTERRUPT_EXTERNAL:
  271. vcpu->stat.ext_intr_exits++;
  272. r = RESUME_GUEST;
  273. break;
  274. case BOOK3S_INTERRUPT_PERFMON:
  275. r = RESUME_GUEST;
  276. break;
  277. case BOOK3S_INTERRUPT_PROGRAM:
  278. {
  279. ulong flags;
  280. /*
  281. * Normally program interrupts are delivered directly
  282. * to the guest by the hardware, but we can get here
  283. * as a result of a hypervisor emulation interrupt
  284. * (e40) getting turned into a 700 by BML RTAS.
  285. */
  286. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  287. kvmppc_core_queue_program(vcpu, flags);
  288. r = RESUME_GUEST;
  289. break;
  290. }
  291. case BOOK3S_INTERRUPT_SYSCALL:
  292. {
  293. /* hcall - punt to userspace */
  294. int i;
  295. if (vcpu->arch.shregs.msr & MSR_PR) {
  296. /* sc 1 from userspace - reflect to guest syscall */
  297. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
  298. r = RESUME_GUEST;
  299. break;
  300. }
  301. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  302. for (i = 0; i < 9; ++i)
  303. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  304. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  305. vcpu->arch.hcall_needed = 1;
  306. r = RESUME_HOST;
  307. break;
  308. }
  309. /*
  310. * We get these next two if the guest does a bad real-mode access,
  311. * as we have enabled VRMA (virtualized real mode area) mode in the
  312. * LPCR. We just generate an appropriate DSI/ISI to the guest.
  313. */
  314. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  315. vcpu->arch.shregs.dsisr = vcpu->arch.fault_dsisr;
  316. vcpu->arch.shregs.dar = vcpu->arch.fault_dar;
  317. kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE, 0);
  318. r = RESUME_GUEST;
  319. break;
  320. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  321. kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE,
  322. 0x08000000);
  323. r = RESUME_GUEST;
  324. break;
  325. /*
  326. * This occurs if the guest executes an illegal instruction.
  327. * We just generate a program interrupt to the guest, since
  328. * we don't emulate any guest instructions at this stage.
  329. */
  330. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  331. kvmppc_core_queue_program(vcpu, 0x80000);
  332. r = RESUME_GUEST;
  333. break;
  334. default:
  335. kvmppc_dump_regs(vcpu);
  336. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  337. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  338. vcpu->arch.shregs.msr);
  339. r = RESUME_HOST;
  340. BUG();
  341. break;
  342. }
  343. if (!(r & RESUME_HOST)) {
  344. /* To avoid clobbering exit_reason, only check for signals if
  345. * we aren't already exiting to userspace for some other
  346. * reason. */
  347. if (signal_pending(tsk)) {
  348. vcpu->stat.signal_exits++;
  349. run->exit_reason = KVM_EXIT_INTR;
  350. r = -EINTR;
  351. } else {
  352. kvmppc_core_deliver_interrupts(vcpu);
  353. }
  354. }
  355. return r;
  356. }
  357. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  358. struct kvm_sregs *sregs)
  359. {
  360. int i;
  361. sregs->pvr = vcpu->arch.pvr;
  362. memset(sregs, 0, sizeof(struct kvm_sregs));
  363. for (i = 0; i < vcpu->arch.slb_max; i++) {
  364. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  365. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  366. }
  367. return 0;
  368. }
  369. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  370. struct kvm_sregs *sregs)
  371. {
  372. int i, j;
  373. kvmppc_set_pvr(vcpu, sregs->pvr);
  374. j = 0;
  375. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  376. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  377. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  378. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  379. ++j;
  380. }
  381. }
  382. vcpu->arch.slb_max = j;
  383. return 0;
  384. }
  385. int kvmppc_core_check_processor_compat(void)
  386. {
  387. if (cpu_has_feature(CPU_FTR_HVMODE_206))
  388. return 0;
  389. return -EIO;
  390. }
  391. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  392. {
  393. struct kvm_vcpu *vcpu;
  394. int err = -ENOMEM;
  395. unsigned long lpcr;
  396. vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
  397. if (!vcpu)
  398. goto out;
  399. err = kvm_vcpu_init(vcpu, kvm, id);
  400. if (err)
  401. goto free_vcpu;
  402. vcpu->arch.shared = &vcpu->arch.shregs;
  403. vcpu->arch.last_cpu = -1;
  404. vcpu->arch.mmcr[0] = MMCR0_FC;
  405. vcpu->arch.ctrl = CTRL_RUNLATCH;
  406. /* default to host PVR, since we can't spoof it */
  407. vcpu->arch.pvr = mfspr(SPRN_PVR);
  408. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  409. lpcr = kvm->arch.host_lpcr & (LPCR_PECE | LPCR_LPES);
  410. lpcr |= LPCR_VPM0 | LPCR_VRMA_L | (4UL << LPCR_DPFD_SH) | LPCR_HDICE;
  411. vcpu->arch.lpcr = lpcr;
  412. kvmppc_mmu_book3s_hv_init(vcpu);
  413. return vcpu;
  414. free_vcpu:
  415. kfree(vcpu);
  416. out:
  417. return ERR_PTR(err);
  418. }
  419. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  420. {
  421. kvm_vcpu_uninit(vcpu);
  422. kfree(vcpu);
  423. }
  424. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  425. static int kvmppc_run_vcpu(struct kvm_run *run, struct kvm_vcpu *vcpu)
  426. {
  427. u64 now;
  428. if (signal_pending(current)) {
  429. run->exit_reason = KVM_EXIT_INTR;
  430. return -EINTR;
  431. }
  432. flush_fp_to_thread(current);
  433. flush_altivec_to_thread(current);
  434. flush_vsx_to_thread(current);
  435. preempt_disable();
  436. /*
  437. * Make sure we are running on thread 0, and that
  438. * secondary threads are offline.
  439. * XXX we should also block attempts to bring any
  440. * secondary threads online.
  441. */
  442. if (threads_per_core > 1) {
  443. int cpu = smp_processor_id();
  444. int thr = cpu_thread_in_core(cpu);
  445. if (thr)
  446. goto out;
  447. while (++thr < threads_per_core)
  448. if (cpu_online(cpu + thr))
  449. goto out;
  450. }
  451. kvm_guest_enter();
  452. __kvmppc_vcore_entry(NULL, vcpu);
  453. kvm_guest_exit();
  454. preempt_enable();
  455. kvm_resched(vcpu);
  456. now = get_tb();
  457. /* cancel pending dec exception if dec is positive */
  458. if (now < vcpu->arch.dec_expires && kvmppc_core_pending_dec(vcpu))
  459. kvmppc_core_dequeue_dec(vcpu);
  460. return kvmppc_handle_exit(run, vcpu, current);
  461. out:
  462. preempt_enable();
  463. return -EBUSY;
  464. }
  465. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  466. {
  467. int r;
  468. do {
  469. r = kvmppc_run_vcpu(run, vcpu);
  470. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  471. !(vcpu->arch.shregs.msr & MSR_PR)) {
  472. r = kvmppc_pseries_do_hcall(vcpu);
  473. kvmppc_core_deliver_interrupts(vcpu);
  474. }
  475. } while (r == RESUME_GUEST);
  476. return r;
  477. }
  478. static long kvmppc_stt_npages(unsigned long window_size)
  479. {
  480. return ALIGN((window_size >> SPAPR_TCE_SHIFT)
  481. * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
  482. }
  483. static void release_spapr_tce_table(struct kvmppc_spapr_tce_table *stt)
  484. {
  485. struct kvm *kvm = stt->kvm;
  486. int i;
  487. mutex_lock(&kvm->lock);
  488. list_del(&stt->list);
  489. for (i = 0; i < kvmppc_stt_npages(stt->window_size); i++)
  490. __free_page(stt->pages[i]);
  491. kfree(stt);
  492. mutex_unlock(&kvm->lock);
  493. kvm_put_kvm(kvm);
  494. }
  495. static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  496. {
  497. struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
  498. struct page *page;
  499. if (vmf->pgoff >= kvmppc_stt_npages(stt->window_size))
  500. return VM_FAULT_SIGBUS;
  501. page = stt->pages[vmf->pgoff];
  502. get_page(page);
  503. vmf->page = page;
  504. return 0;
  505. }
  506. static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
  507. .fault = kvm_spapr_tce_fault,
  508. };
  509. static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
  510. {
  511. vma->vm_ops = &kvm_spapr_tce_vm_ops;
  512. return 0;
  513. }
  514. static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
  515. {
  516. struct kvmppc_spapr_tce_table *stt = filp->private_data;
  517. release_spapr_tce_table(stt);
  518. return 0;
  519. }
  520. static struct file_operations kvm_spapr_tce_fops = {
  521. .mmap = kvm_spapr_tce_mmap,
  522. .release = kvm_spapr_tce_release,
  523. };
  524. long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  525. struct kvm_create_spapr_tce *args)
  526. {
  527. struct kvmppc_spapr_tce_table *stt = NULL;
  528. long npages;
  529. int ret = -ENOMEM;
  530. int i;
  531. /* Check this LIOBN hasn't been previously allocated */
  532. list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
  533. if (stt->liobn == args->liobn)
  534. return -EBUSY;
  535. }
  536. npages = kvmppc_stt_npages(args->window_size);
  537. stt = kzalloc(sizeof(*stt) + npages* sizeof(struct page *),
  538. GFP_KERNEL);
  539. if (!stt)
  540. goto fail;
  541. stt->liobn = args->liobn;
  542. stt->window_size = args->window_size;
  543. stt->kvm = kvm;
  544. for (i = 0; i < npages; i++) {
  545. stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  546. if (!stt->pages[i])
  547. goto fail;
  548. }
  549. kvm_get_kvm(kvm);
  550. mutex_lock(&kvm->lock);
  551. list_add(&stt->list, &kvm->arch.spapr_tce_tables);
  552. mutex_unlock(&kvm->lock);
  553. return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
  554. stt, O_RDWR);
  555. fail:
  556. if (stt) {
  557. for (i = 0; i < npages; i++)
  558. if (stt->pages[i])
  559. __free_page(stt->pages[i]);
  560. kfree(stt);
  561. }
  562. return ret;
  563. }
  564. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  565. struct kvm_userspace_memory_region *mem)
  566. {
  567. if (mem->guest_phys_addr == 0 && mem->memory_size != 0)
  568. return kvmppc_prepare_vrma(kvm, mem);
  569. return 0;
  570. }
  571. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  572. struct kvm_userspace_memory_region *mem)
  573. {
  574. if (mem->guest_phys_addr == 0 && mem->memory_size != 0)
  575. kvmppc_map_vrma(kvm, mem);
  576. }
  577. int kvmppc_core_init_vm(struct kvm *kvm)
  578. {
  579. long r;
  580. /* Allocate hashed page table */
  581. r = kvmppc_alloc_hpt(kvm);
  582. if (r)
  583. return r;
  584. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  585. return 0;
  586. }
  587. void kvmppc_core_destroy_vm(struct kvm *kvm)
  588. {
  589. kvmppc_free_hpt(kvm);
  590. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  591. }
  592. /* These are stubs for now */
  593. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  594. {
  595. }
  596. /* We don't need to emulate any privileged instructions or dcbz */
  597. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  598. unsigned int inst, int *advance)
  599. {
  600. return EMULATE_FAIL;
  601. }
  602. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  603. {
  604. return EMULATE_FAIL;
  605. }
  606. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  607. {
  608. return EMULATE_FAIL;
  609. }
  610. static int kvmppc_book3s_hv_init(void)
  611. {
  612. int r;
  613. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  614. if (r)
  615. return r;
  616. r = kvmppc_mmu_hv_init();
  617. return r;
  618. }
  619. static void kvmppc_book3s_hv_exit(void)
  620. {
  621. kvm_exit();
  622. }
  623. module_init(kvmppc_book3s_hv_init);
  624. module_exit(kvmppc_book3s_hv_exit);