powerpc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2007
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/hrtimer.h>
  25. #include <linux/fs.h>
  26. #include <linux/slab.h>
  27. #include <asm/cputable.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/kvm_ppc.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/cputhreads.h>
  32. #include "timing.h"
  33. #include "../mm/mmu_decl.h"
  34. #define CREATE_TRACE_POINTS
  35. #include "trace.h"
  36. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  37. {
  38. return !(v->arch.shared->msr & MSR_WE) ||
  39. !!(v->arch.pending_exceptions);
  40. }
  41. int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
  42. {
  43. int nr = kvmppc_get_gpr(vcpu, 11);
  44. int r;
  45. unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
  46. unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
  47. unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
  48. unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
  49. unsigned long r2 = 0;
  50. if (!(vcpu->arch.shared->msr & MSR_SF)) {
  51. /* 32 bit mode */
  52. param1 &= 0xffffffff;
  53. param2 &= 0xffffffff;
  54. param3 &= 0xffffffff;
  55. param4 &= 0xffffffff;
  56. }
  57. switch (nr) {
  58. case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE:
  59. {
  60. vcpu->arch.magic_page_pa = param1;
  61. vcpu->arch.magic_page_ea = param2;
  62. r2 = KVM_MAGIC_FEAT_SR;
  63. r = HC_EV_SUCCESS;
  64. break;
  65. }
  66. case HC_VENDOR_KVM | KVM_HC_FEATURES:
  67. r = HC_EV_SUCCESS;
  68. #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500)
  69. /* XXX Missing magic page on 44x */
  70. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  71. #endif
  72. /* Second return value is in r4 */
  73. break;
  74. default:
  75. r = HC_EV_UNIMPLEMENTED;
  76. break;
  77. }
  78. kvmppc_set_gpr(vcpu, 4, r2);
  79. return r;
  80. }
  81. int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
  82. {
  83. int r = false;
  84. /* We have to know what CPU to virtualize */
  85. if (!vcpu->arch.pvr)
  86. goto out;
  87. /* PAPR only works with book3s_64 */
  88. if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
  89. goto out;
  90. #ifdef CONFIG_KVM_BOOK3S_64_HV
  91. /* HV KVM can only do PAPR mode for now */
  92. if (!vcpu->arch.papr_enabled)
  93. goto out;
  94. #endif
  95. r = true;
  96. out:
  97. vcpu->arch.sane = r;
  98. return r ? 0 : -EINVAL;
  99. }
  100. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  101. {
  102. enum emulation_result er;
  103. int r;
  104. er = kvmppc_emulate_instruction(run, vcpu);
  105. switch (er) {
  106. case EMULATE_DONE:
  107. /* Future optimization: only reload non-volatiles if they were
  108. * actually modified. */
  109. r = RESUME_GUEST_NV;
  110. break;
  111. case EMULATE_DO_MMIO:
  112. run->exit_reason = KVM_EXIT_MMIO;
  113. /* We must reload nonvolatiles because "update" load/store
  114. * instructions modify register state. */
  115. /* Future optimization: only reload non-volatiles if they were
  116. * actually modified. */
  117. r = RESUME_HOST_NV;
  118. break;
  119. case EMULATE_FAIL:
  120. /* XXX Deliver Program interrupt to guest. */
  121. printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
  122. kvmppc_get_last_inst(vcpu));
  123. r = RESUME_HOST;
  124. break;
  125. default:
  126. BUG();
  127. }
  128. return r;
  129. }
  130. int kvm_arch_hardware_enable(void *garbage)
  131. {
  132. return 0;
  133. }
  134. void kvm_arch_hardware_disable(void *garbage)
  135. {
  136. }
  137. int kvm_arch_hardware_setup(void)
  138. {
  139. return 0;
  140. }
  141. void kvm_arch_hardware_unsetup(void)
  142. {
  143. }
  144. void kvm_arch_check_processor_compat(void *rtn)
  145. {
  146. *(int *)rtn = kvmppc_core_check_processor_compat();
  147. }
  148. int kvm_arch_init_vm(struct kvm *kvm)
  149. {
  150. return kvmppc_core_init_vm(kvm);
  151. }
  152. void kvm_arch_destroy_vm(struct kvm *kvm)
  153. {
  154. unsigned int i;
  155. struct kvm_vcpu *vcpu;
  156. kvm_for_each_vcpu(i, vcpu, kvm)
  157. kvm_arch_vcpu_free(vcpu);
  158. mutex_lock(&kvm->lock);
  159. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  160. kvm->vcpus[i] = NULL;
  161. atomic_set(&kvm->online_vcpus, 0);
  162. kvmppc_core_destroy_vm(kvm);
  163. mutex_unlock(&kvm->lock);
  164. }
  165. void kvm_arch_sync_events(struct kvm *kvm)
  166. {
  167. }
  168. int kvm_dev_ioctl_check_extension(long ext)
  169. {
  170. int r;
  171. switch (ext) {
  172. #ifdef CONFIG_BOOKE
  173. case KVM_CAP_PPC_BOOKE_SREGS:
  174. #else
  175. case KVM_CAP_PPC_SEGSTATE:
  176. case KVM_CAP_PPC_PAPR:
  177. #endif
  178. case KVM_CAP_PPC_UNSET_IRQ:
  179. case KVM_CAP_PPC_IRQ_LEVEL:
  180. case KVM_CAP_ENABLE_CAP:
  181. r = 1;
  182. break;
  183. #ifndef CONFIG_KVM_BOOK3S_64_HV
  184. case KVM_CAP_PPC_PAIRED_SINGLES:
  185. case KVM_CAP_PPC_OSI:
  186. case KVM_CAP_PPC_GET_PVINFO:
  187. r = 1;
  188. break;
  189. case KVM_CAP_COALESCED_MMIO:
  190. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  191. break;
  192. #endif
  193. #ifdef CONFIG_KVM_BOOK3S_64_HV
  194. case KVM_CAP_SPAPR_TCE:
  195. r = 1;
  196. break;
  197. case KVM_CAP_PPC_SMT:
  198. r = threads_per_core;
  199. break;
  200. case KVM_CAP_PPC_RMA:
  201. r = 1;
  202. /* PPC970 requires an RMA */
  203. if (cpu_has_feature(CPU_FTR_ARCH_201))
  204. r = 2;
  205. break;
  206. #endif
  207. default:
  208. r = 0;
  209. break;
  210. }
  211. return r;
  212. }
  213. long kvm_arch_dev_ioctl(struct file *filp,
  214. unsigned int ioctl, unsigned long arg)
  215. {
  216. return -EINVAL;
  217. }
  218. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  219. struct kvm_memory_slot *memslot,
  220. struct kvm_memory_slot old,
  221. struct kvm_userspace_memory_region *mem,
  222. int user_alloc)
  223. {
  224. return kvmppc_core_prepare_memory_region(kvm, mem);
  225. }
  226. void kvm_arch_commit_memory_region(struct kvm *kvm,
  227. struct kvm_userspace_memory_region *mem,
  228. struct kvm_memory_slot old,
  229. int user_alloc)
  230. {
  231. kvmppc_core_commit_memory_region(kvm, mem);
  232. }
  233. void kvm_arch_flush_shadow(struct kvm *kvm)
  234. {
  235. }
  236. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  237. {
  238. struct kvm_vcpu *vcpu;
  239. vcpu = kvmppc_core_vcpu_create(kvm, id);
  240. vcpu->arch.wqp = &vcpu->wq;
  241. if (!IS_ERR(vcpu))
  242. kvmppc_create_vcpu_debugfs(vcpu, id);
  243. return vcpu;
  244. }
  245. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  246. {
  247. /* Make sure we're not using the vcpu anymore */
  248. hrtimer_cancel(&vcpu->arch.dec_timer);
  249. tasklet_kill(&vcpu->arch.tasklet);
  250. kvmppc_remove_vcpu_debugfs(vcpu);
  251. kvmppc_core_vcpu_free(vcpu);
  252. }
  253. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  254. {
  255. kvm_arch_vcpu_free(vcpu);
  256. }
  257. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  258. {
  259. return kvmppc_core_pending_dec(vcpu);
  260. }
  261. static void kvmppc_decrementer_func(unsigned long data)
  262. {
  263. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  264. kvmppc_core_queue_dec(vcpu);
  265. if (waitqueue_active(vcpu->arch.wqp)) {
  266. wake_up_interruptible(vcpu->arch.wqp);
  267. vcpu->stat.halt_wakeup++;
  268. }
  269. }
  270. /*
  271. * low level hrtimer wake routine. Because this runs in hardirq context
  272. * we schedule a tasklet to do the real work.
  273. */
  274. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  275. {
  276. struct kvm_vcpu *vcpu;
  277. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  278. tasklet_schedule(&vcpu->arch.tasklet);
  279. return HRTIMER_NORESTART;
  280. }
  281. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  282. {
  283. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  284. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  285. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  286. vcpu->arch.dec_expires = ~(u64)0;
  287. #ifdef CONFIG_KVM_EXIT_TIMING
  288. mutex_init(&vcpu->arch.exit_timing_lock);
  289. #endif
  290. return 0;
  291. }
  292. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  293. {
  294. kvmppc_mmu_destroy(vcpu);
  295. }
  296. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  297. {
  298. #ifdef CONFIG_BOOKE
  299. /*
  300. * vrsave (formerly usprg0) isn't used by Linux, but may
  301. * be used by the guest.
  302. *
  303. * On non-booke this is associated with Altivec and
  304. * is handled by code in book3s.c.
  305. */
  306. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  307. #endif
  308. kvmppc_core_vcpu_load(vcpu, cpu);
  309. vcpu->cpu = smp_processor_id();
  310. }
  311. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  312. {
  313. kvmppc_core_vcpu_put(vcpu);
  314. #ifdef CONFIG_BOOKE
  315. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  316. #endif
  317. vcpu->cpu = -1;
  318. }
  319. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  320. struct kvm_guest_debug *dbg)
  321. {
  322. return -EINVAL;
  323. }
  324. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  325. struct kvm_run *run)
  326. {
  327. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  328. }
  329. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  330. struct kvm_run *run)
  331. {
  332. u64 uninitialized_var(gpr);
  333. if (run->mmio.len > sizeof(gpr)) {
  334. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  335. return;
  336. }
  337. if (vcpu->arch.mmio_is_bigendian) {
  338. switch (run->mmio.len) {
  339. case 8: gpr = *(u64 *)run->mmio.data; break;
  340. case 4: gpr = *(u32 *)run->mmio.data; break;
  341. case 2: gpr = *(u16 *)run->mmio.data; break;
  342. case 1: gpr = *(u8 *)run->mmio.data; break;
  343. }
  344. } else {
  345. /* Convert BE data from userland back to LE. */
  346. switch (run->mmio.len) {
  347. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  348. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  349. case 1: gpr = *(u8 *)run->mmio.data; break;
  350. }
  351. }
  352. if (vcpu->arch.mmio_sign_extend) {
  353. switch (run->mmio.len) {
  354. #ifdef CONFIG_PPC64
  355. case 4:
  356. gpr = (s64)(s32)gpr;
  357. break;
  358. #endif
  359. case 2:
  360. gpr = (s64)(s16)gpr;
  361. break;
  362. case 1:
  363. gpr = (s64)(s8)gpr;
  364. break;
  365. }
  366. }
  367. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  368. switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
  369. case KVM_REG_GPR:
  370. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  371. break;
  372. case KVM_REG_FPR:
  373. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  374. break;
  375. #ifdef CONFIG_PPC_BOOK3S
  376. case KVM_REG_QPR:
  377. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  378. break;
  379. case KVM_REG_FQPR:
  380. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  381. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  382. break;
  383. #endif
  384. default:
  385. BUG();
  386. }
  387. }
  388. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  389. unsigned int rt, unsigned int bytes, int is_bigendian)
  390. {
  391. if (bytes > sizeof(run->mmio.data)) {
  392. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  393. run->mmio.len);
  394. }
  395. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  396. run->mmio.len = bytes;
  397. run->mmio.is_write = 0;
  398. vcpu->arch.io_gpr = rt;
  399. vcpu->arch.mmio_is_bigendian = is_bigendian;
  400. vcpu->mmio_needed = 1;
  401. vcpu->mmio_is_write = 0;
  402. vcpu->arch.mmio_sign_extend = 0;
  403. return EMULATE_DO_MMIO;
  404. }
  405. /* Same as above, but sign extends */
  406. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  407. unsigned int rt, unsigned int bytes, int is_bigendian)
  408. {
  409. int r;
  410. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
  411. vcpu->arch.mmio_sign_extend = 1;
  412. return r;
  413. }
  414. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  415. u64 val, unsigned int bytes, int is_bigendian)
  416. {
  417. void *data = run->mmio.data;
  418. if (bytes > sizeof(run->mmio.data)) {
  419. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  420. run->mmio.len);
  421. }
  422. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  423. run->mmio.len = bytes;
  424. run->mmio.is_write = 1;
  425. vcpu->mmio_needed = 1;
  426. vcpu->mmio_is_write = 1;
  427. /* Store the value at the lowest bytes in 'data'. */
  428. if (is_bigendian) {
  429. switch (bytes) {
  430. case 8: *(u64 *)data = val; break;
  431. case 4: *(u32 *)data = val; break;
  432. case 2: *(u16 *)data = val; break;
  433. case 1: *(u8 *)data = val; break;
  434. }
  435. } else {
  436. /* Store LE value into 'data'. */
  437. switch (bytes) {
  438. case 4: st_le32(data, val); break;
  439. case 2: st_le16(data, val); break;
  440. case 1: *(u8 *)data = val; break;
  441. }
  442. }
  443. return EMULATE_DO_MMIO;
  444. }
  445. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  446. {
  447. int r;
  448. sigset_t sigsaved;
  449. if (vcpu->sigset_active)
  450. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  451. if (vcpu->mmio_needed) {
  452. if (!vcpu->mmio_is_write)
  453. kvmppc_complete_mmio_load(vcpu, run);
  454. vcpu->mmio_needed = 0;
  455. } else if (vcpu->arch.dcr_needed) {
  456. if (!vcpu->arch.dcr_is_write)
  457. kvmppc_complete_dcr_load(vcpu, run);
  458. vcpu->arch.dcr_needed = 0;
  459. } else if (vcpu->arch.osi_needed) {
  460. u64 *gprs = run->osi.gprs;
  461. int i;
  462. for (i = 0; i < 32; i++)
  463. kvmppc_set_gpr(vcpu, i, gprs[i]);
  464. vcpu->arch.osi_needed = 0;
  465. } else if (vcpu->arch.hcall_needed) {
  466. int i;
  467. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  468. for (i = 0; i < 9; ++i)
  469. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  470. vcpu->arch.hcall_needed = 0;
  471. }
  472. kvmppc_core_deliver_interrupts(vcpu);
  473. r = kvmppc_vcpu_run(run, vcpu);
  474. if (vcpu->sigset_active)
  475. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  476. return r;
  477. }
  478. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  479. {
  480. if (irq->irq == KVM_INTERRUPT_UNSET) {
  481. kvmppc_core_dequeue_external(vcpu, irq);
  482. return 0;
  483. }
  484. kvmppc_core_queue_external(vcpu, irq);
  485. if (waitqueue_active(vcpu->arch.wqp)) {
  486. wake_up_interruptible(vcpu->arch.wqp);
  487. vcpu->stat.halt_wakeup++;
  488. } else if (vcpu->cpu != -1) {
  489. smp_send_reschedule(vcpu->cpu);
  490. }
  491. return 0;
  492. }
  493. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  494. struct kvm_enable_cap *cap)
  495. {
  496. int r;
  497. if (cap->flags)
  498. return -EINVAL;
  499. switch (cap->cap) {
  500. case KVM_CAP_PPC_OSI:
  501. r = 0;
  502. vcpu->arch.osi_enabled = true;
  503. break;
  504. case KVM_CAP_PPC_PAPR:
  505. r = 0;
  506. vcpu->arch.papr_enabled = true;
  507. break;
  508. default:
  509. r = -EINVAL;
  510. break;
  511. }
  512. if (!r)
  513. r = kvmppc_sanity_check(vcpu);
  514. return r;
  515. }
  516. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  517. struct kvm_mp_state *mp_state)
  518. {
  519. return -EINVAL;
  520. }
  521. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  522. struct kvm_mp_state *mp_state)
  523. {
  524. return -EINVAL;
  525. }
  526. long kvm_arch_vcpu_ioctl(struct file *filp,
  527. unsigned int ioctl, unsigned long arg)
  528. {
  529. struct kvm_vcpu *vcpu = filp->private_data;
  530. void __user *argp = (void __user *)arg;
  531. long r;
  532. switch (ioctl) {
  533. case KVM_INTERRUPT: {
  534. struct kvm_interrupt irq;
  535. r = -EFAULT;
  536. if (copy_from_user(&irq, argp, sizeof(irq)))
  537. goto out;
  538. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  539. goto out;
  540. }
  541. case KVM_ENABLE_CAP:
  542. {
  543. struct kvm_enable_cap cap;
  544. r = -EFAULT;
  545. if (copy_from_user(&cap, argp, sizeof(cap)))
  546. goto out;
  547. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  548. break;
  549. }
  550. default:
  551. r = -EINVAL;
  552. }
  553. out:
  554. return r;
  555. }
  556. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  557. {
  558. u32 inst_lis = 0x3c000000;
  559. u32 inst_ori = 0x60000000;
  560. u32 inst_nop = 0x60000000;
  561. u32 inst_sc = 0x44000002;
  562. u32 inst_imm_mask = 0xffff;
  563. /*
  564. * The hypercall to get into KVM from within guest context is as
  565. * follows:
  566. *
  567. * lis r0, r0, KVM_SC_MAGIC_R0@h
  568. * ori r0, KVM_SC_MAGIC_R0@l
  569. * sc
  570. * nop
  571. */
  572. pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
  573. pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
  574. pvinfo->hcall[2] = inst_sc;
  575. pvinfo->hcall[3] = inst_nop;
  576. return 0;
  577. }
  578. long kvm_arch_vm_ioctl(struct file *filp,
  579. unsigned int ioctl, unsigned long arg)
  580. {
  581. void __user *argp = (void __user *)arg;
  582. long r;
  583. switch (ioctl) {
  584. case KVM_PPC_GET_PVINFO: {
  585. struct kvm_ppc_pvinfo pvinfo;
  586. memset(&pvinfo, 0, sizeof(pvinfo));
  587. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  588. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  589. r = -EFAULT;
  590. goto out;
  591. }
  592. break;
  593. }
  594. #ifdef CONFIG_KVM_BOOK3S_64_HV
  595. case KVM_CREATE_SPAPR_TCE: {
  596. struct kvm_create_spapr_tce create_tce;
  597. struct kvm *kvm = filp->private_data;
  598. r = -EFAULT;
  599. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  600. goto out;
  601. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  602. goto out;
  603. }
  604. case KVM_ALLOCATE_RMA: {
  605. struct kvm *kvm = filp->private_data;
  606. struct kvm_allocate_rma rma;
  607. r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
  608. if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
  609. r = -EFAULT;
  610. break;
  611. }
  612. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  613. default:
  614. r = -ENOTTY;
  615. }
  616. out:
  617. return r;
  618. }
  619. int kvm_arch_init(void *opaque)
  620. {
  621. return 0;
  622. }
  623. void kvm_arch_exit(void)
  624. {
  625. }