powerpc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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/module.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/hrtimer.h>
  26. #include <linux/fs.h>
  27. #include <linux/slab.h>
  28. #include <asm/cputable.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/kvm_ppc.h>
  31. #include <asm/tlbflush.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 = 0;
  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) /* XXX Missing magic page on BookE */
  69. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  70. #endif
  71. /* Second return value is in r4 */
  72. break;
  73. default:
  74. r = HC_EV_UNIMPLEMENTED;
  75. break;
  76. }
  77. kvmppc_set_gpr(vcpu, 4, r2);
  78. return r;
  79. }
  80. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  81. {
  82. enum emulation_result er;
  83. int r;
  84. er = kvmppc_emulate_instruction(run, vcpu);
  85. switch (er) {
  86. case EMULATE_DONE:
  87. /* Future optimization: only reload non-volatiles if they were
  88. * actually modified. */
  89. r = RESUME_GUEST_NV;
  90. break;
  91. case EMULATE_DO_MMIO:
  92. run->exit_reason = KVM_EXIT_MMIO;
  93. /* We must reload nonvolatiles because "update" load/store
  94. * instructions modify register state. */
  95. /* Future optimization: only reload non-volatiles if they were
  96. * actually modified. */
  97. r = RESUME_HOST_NV;
  98. break;
  99. case EMULATE_FAIL:
  100. /* XXX Deliver Program interrupt to guest. */
  101. printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
  102. kvmppc_get_last_inst(vcpu));
  103. r = RESUME_HOST;
  104. break;
  105. default:
  106. BUG();
  107. }
  108. return r;
  109. }
  110. int kvm_arch_hardware_enable(void *garbage)
  111. {
  112. return 0;
  113. }
  114. void kvm_arch_hardware_disable(void *garbage)
  115. {
  116. }
  117. int kvm_arch_hardware_setup(void)
  118. {
  119. return 0;
  120. }
  121. void kvm_arch_hardware_unsetup(void)
  122. {
  123. }
  124. void kvm_arch_check_processor_compat(void *rtn)
  125. {
  126. *(int *)rtn = kvmppc_core_check_processor_compat();
  127. }
  128. struct kvm *kvm_arch_create_vm(void)
  129. {
  130. struct kvm *kvm;
  131. kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  132. if (!kvm)
  133. return ERR_PTR(-ENOMEM);
  134. return kvm;
  135. }
  136. static void kvmppc_free_vcpus(struct kvm *kvm)
  137. {
  138. unsigned int i;
  139. struct kvm_vcpu *vcpu;
  140. kvm_for_each_vcpu(i, vcpu, kvm)
  141. kvm_arch_vcpu_free(vcpu);
  142. mutex_lock(&kvm->lock);
  143. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  144. kvm->vcpus[i] = NULL;
  145. atomic_set(&kvm->online_vcpus, 0);
  146. mutex_unlock(&kvm->lock);
  147. }
  148. void kvm_arch_sync_events(struct kvm *kvm)
  149. {
  150. }
  151. void kvm_arch_destroy_vm(struct kvm *kvm)
  152. {
  153. kvmppc_free_vcpus(kvm);
  154. kvm_free_physmem(kvm);
  155. cleanup_srcu_struct(&kvm->srcu);
  156. kfree(kvm);
  157. }
  158. int kvm_dev_ioctl_check_extension(long ext)
  159. {
  160. int r;
  161. switch (ext) {
  162. case KVM_CAP_PPC_SEGSTATE:
  163. case KVM_CAP_PPC_PAIRED_SINGLES:
  164. case KVM_CAP_PPC_UNSET_IRQ:
  165. case KVM_CAP_ENABLE_CAP:
  166. case KVM_CAP_PPC_OSI:
  167. case KVM_CAP_PPC_GET_PVINFO:
  168. r = 1;
  169. break;
  170. case KVM_CAP_COALESCED_MMIO:
  171. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  172. break;
  173. default:
  174. r = 0;
  175. break;
  176. }
  177. return r;
  178. }
  179. long kvm_arch_dev_ioctl(struct file *filp,
  180. unsigned int ioctl, unsigned long arg)
  181. {
  182. return -EINVAL;
  183. }
  184. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  185. struct kvm_memory_slot *memslot,
  186. struct kvm_memory_slot old,
  187. struct kvm_userspace_memory_region *mem,
  188. int user_alloc)
  189. {
  190. return 0;
  191. }
  192. void kvm_arch_commit_memory_region(struct kvm *kvm,
  193. struct kvm_userspace_memory_region *mem,
  194. struct kvm_memory_slot old,
  195. int user_alloc)
  196. {
  197. return;
  198. }
  199. void kvm_arch_flush_shadow(struct kvm *kvm)
  200. {
  201. }
  202. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  203. {
  204. struct kvm_vcpu *vcpu;
  205. vcpu = kvmppc_core_vcpu_create(kvm, id);
  206. if (!IS_ERR(vcpu))
  207. kvmppc_create_vcpu_debugfs(vcpu, id);
  208. return vcpu;
  209. }
  210. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  211. {
  212. /* Make sure we're not using the vcpu anymore */
  213. hrtimer_cancel(&vcpu->arch.dec_timer);
  214. tasklet_kill(&vcpu->arch.tasklet);
  215. kvmppc_remove_vcpu_debugfs(vcpu);
  216. kvmppc_core_vcpu_free(vcpu);
  217. }
  218. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  219. {
  220. kvm_arch_vcpu_free(vcpu);
  221. }
  222. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  223. {
  224. return kvmppc_core_pending_dec(vcpu);
  225. }
  226. static void kvmppc_decrementer_func(unsigned long data)
  227. {
  228. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  229. kvmppc_core_queue_dec(vcpu);
  230. if (waitqueue_active(&vcpu->wq)) {
  231. wake_up_interruptible(&vcpu->wq);
  232. vcpu->stat.halt_wakeup++;
  233. }
  234. }
  235. /*
  236. * low level hrtimer wake routine. Because this runs in hardirq context
  237. * we schedule a tasklet to do the real work.
  238. */
  239. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  240. {
  241. struct kvm_vcpu *vcpu;
  242. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  243. tasklet_schedule(&vcpu->arch.tasklet);
  244. return HRTIMER_NORESTART;
  245. }
  246. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  247. {
  248. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  249. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  250. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  251. return 0;
  252. }
  253. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  254. {
  255. kvmppc_mmu_destroy(vcpu);
  256. }
  257. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  258. {
  259. kvmppc_core_vcpu_load(vcpu, cpu);
  260. }
  261. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  262. {
  263. kvmppc_core_vcpu_put(vcpu);
  264. }
  265. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  266. struct kvm_guest_debug *dbg)
  267. {
  268. return -EINVAL;
  269. }
  270. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  271. struct kvm_run *run)
  272. {
  273. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  274. }
  275. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  276. struct kvm_run *run)
  277. {
  278. u64 uninitialized_var(gpr);
  279. if (run->mmio.len > sizeof(gpr)) {
  280. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  281. return;
  282. }
  283. if (vcpu->arch.mmio_is_bigendian) {
  284. switch (run->mmio.len) {
  285. case 8: gpr = *(u64 *)run->mmio.data; break;
  286. case 4: gpr = *(u32 *)run->mmio.data; break;
  287. case 2: gpr = *(u16 *)run->mmio.data; break;
  288. case 1: gpr = *(u8 *)run->mmio.data; break;
  289. }
  290. } else {
  291. /* Convert BE data from userland back to LE. */
  292. switch (run->mmio.len) {
  293. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  294. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  295. case 1: gpr = *(u8 *)run->mmio.data; break;
  296. }
  297. }
  298. if (vcpu->arch.mmio_sign_extend) {
  299. switch (run->mmio.len) {
  300. #ifdef CONFIG_PPC64
  301. case 4:
  302. gpr = (s64)(s32)gpr;
  303. break;
  304. #endif
  305. case 2:
  306. gpr = (s64)(s16)gpr;
  307. break;
  308. case 1:
  309. gpr = (s64)(s8)gpr;
  310. break;
  311. }
  312. }
  313. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  314. switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
  315. case KVM_REG_GPR:
  316. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  317. break;
  318. case KVM_REG_FPR:
  319. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  320. break;
  321. #ifdef CONFIG_PPC_BOOK3S
  322. case KVM_REG_QPR:
  323. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  324. break;
  325. case KVM_REG_FQPR:
  326. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  327. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  328. break;
  329. #endif
  330. default:
  331. BUG();
  332. }
  333. }
  334. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  335. unsigned int rt, unsigned int bytes, int is_bigendian)
  336. {
  337. if (bytes > sizeof(run->mmio.data)) {
  338. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  339. run->mmio.len);
  340. }
  341. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  342. run->mmio.len = bytes;
  343. run->mmio.is_write = 0;
  344. vcpu->arch.io_gpr = rt;
  345. vcpu->arch.mmio_is_bigendian = is_bigendian;
  346. vcpu->mmio_needed = 1;
  347. vcpu->mmio_is_write = 0;
  348. vcpu->arch.mmio_sign_extend = 0;
  349. return EMULATE_DO_MMIO;
  350. }
  351. /* Same as above, but sign extends */
  352. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  353. unsigned int rt, unsigned int bytes, int is_bigendian)
  354. {
  355. int r;
  356. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
  357. vcpu->arch.mmio_sign_extend = 1;
  358. return r;
  359. }
  360. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  361. u64 val, unsigned int bytes, int is_bigendian)
  362. {
  363. void *data = run->mmio.data;
  364. if (bytes > sizeof(run->mmio.data)) {
  365. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  366. run->mmio.len);
  367. }
  368. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  369. run->mmio.len = bytes;
  370. run->mmio.is_write = 1;
  371. vcpu->mmio_needed = 1;
  372. vcpu->mmio_is_write = 1;
  373. /* Store the value at the lowest bytes in 'data'. */
  374. if (is_bigendian) {
  375. switch (bytes) {
  376. case 8: *(u64 *)data = val; break;
  377. case 4: *(u32 *)data = val; break;
  378. case 2: *(u16 *)data = val; break;
  379. case 1: *(u8 *)data = val; break;
  380. }
  381. } else {
  382. /* Store LE value into 'data'. */
  383. switch (bytes) {
  384. case 4: st_le32(data, val); break;
  385. case 2: st_le16(data, val); break;
  386. case 1: *(u8 *)data = val; break;
  387. }
  388. }
  389. return EMULATE_DO_MMIO;
  390. }
  391. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  392. {
  393. int r;
  394. sigset_t sigsaved;
  395. if (vcpu->sigset_active)
  396. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  397. if (vcpu->mmio_needed) {
  398. if (!vcpu->mmio_is_write)
  399. kvmppc_complete_mmio_load(vcpu, run);
  400. vcpu->mmio_needed = 0;
  401. } else if (vcpu->arch.dcr_needed) {
  402. if (!vcpu->arch.dcr_is_write)
  403. kvmppc_complete_dcr_load(vcpu, run);
  404. vcpu->arch.dcr_needed = 0;
  405. } else if (vcpu->arch.osi_needed) {
  406. u64 *gprs = run->osi.gprs;
  407. int i;
  408. for (i = 0; i < 32; i++)
  409. kvmppc_set_gpr(vcpu, i, gprs[i]);
  410. vcpu->arch.osi_needed = 0;
  411. }
  412. kvmppc_core_deliver_interrupts(vcpu);
  413. local_irq_disable();
  414. kvm_guest_enter();
  415. r = __kvmppc_vcpu_run(run, vcpu);
  416. kvm_guest_exit();
  417. local_irq_enable();
  418. if (vcpu->sigset_active)
  419. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  420. return r;
  421. }
  422. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  423. {
  424. if (irq->irq == KVM_INTERRUPT_UNSET)
  425. kvmppc_core_dequeue_external(vcpu, irq);
  426. else
  427. kvmppc_core_queue_external(vcpu, irq);
  428. if (waitqueue_active(&vcpu->wq)) {
  429. wake_up_interruptible(&vcpu->wq);
  430. vcpu->stat.halt_wakeup++;
  431. }
  432. return 0;
  433. }
  434. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  435. struct kvm_enable_cap *cap)
  436. {
  437. int r;
  438. if (cap->flags)
  439. return -EINVAL;
  440. switch (cap->cap) {
  441. case KVM_CAP_PPC_OSI:
  442. r = 0;
  443. vcpu->arch.osi_enabled = true;
  444. break;
  445. default:
  446. r = -EINVAL;
  447. break;
  448. }
  449. return r;
  450. }
  451. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  452. struct kvm_mp_state *mp_state)
  453. {
  454. return -EINVAL;
  455. }
  456. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  457. struct kvm_mp_state *mp_state)
  458. {
  459. return -EINVAL;
  460. }
  461. long kvm_arch_vcpu_ioctl(struct file *filp,
  462. unsigned int ioctl, unsigned long arg)
  463. {
  464. struct kvm_vcpu *vcpu = filp->private_data;
  465. void __user *argp = (void __user *)arg;
  466. long r;
  467. switch (ioctl) {
  468. case KVM_INTERRUPT: {
  469. struct kvm_interrupt irq;
  470. r = -EFAULT;
  471. if (copy_from_user(&irq, argp, sizeof(irq)))
  472. goto out;
  473. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  474. goto out;
  475. }
  476. case KVM_ENABLE_CAP:
  477. {
  478. struct kvm_enable_cap cap;
  479. r = -EFAULT;
  480. if (copy_from_user(&cap, argp, sizeof(cap)))
  481. goto out;
  482. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  483. break;
  484. }
  485. default:
  486. r = -EINVAL;
  487. }
  488. out:
  489. return r;
  490. }
  491. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  492. {
  493. u32 inst_lis = 0x3c000000;
  494. u32 inst_ori = 0x60000000;
  495. u32 inst_nop = 0x60000000;
  496. u32 inst_sc = 0x44000002;
  497. u32 inst_imm_mask = 0xffff;
  498. /*
  499. * The hypercall to get into KVM from within guest context is as
  500. * follows:
  501. *
  502. * lis r0, r0, KVM_SC_MAGIC_R0@h
  503. * ori r0, KVM_SC_MAGIC_R0@l
  504. * sc
  505. * nop
  506. */
  507. pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
  508. pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
  509. pvinfo->hcall[2] = inst_sc;
  510. pvinfo->hcall[3] = inst_nop;
  511. return 0;
  512. }
  513. long kvm_arch_vm_ioctl(struct file *filp,
  514. unsigned int ioctl, unsigned long arg)
  515. {
  516. void __user *argp = (void __user *)arg;
  517. long r;
  518. switch (ioctl) {
  519. case KVM_PPC_GET_PVINFO: {
  520. struct kvm_ppc_pvinfo pvinfo;
  521. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  522. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  523. r = -EFAULT;
  524. goto out;
  525. }
  526. break;
  527. }
  528. default:
  529. r = -ENOTTY;
  530. }
  531. out:
  532. return r;
  533. }
  534. int kvm_arch_init(void *opaque)
  535. {
  536. return 0;
  537. }
  538. void kvm_arch_exit(void)
  539. {
  540. }