powerpc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 = 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) /* 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. int kvm_arch_init_vm(struct kvm *kvm)
  129. {
  130. return 0;
  131. }
  132. void kvm_arch_destroy_vm(struct kvm *kvm)
  133. {
  134. unsigned int i;
  135. struct kvm_vcpu *vcpu;
  136. kvm_for_each_vcpu(i, vcpu, kvm)
  137. kvm_arch_vcpu_free(vcpu);
  138. mutex_lock(&kvm->lock);
  139. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  140. kvm->vcpus[i] = NULL;
  141. atomic_set(&kvm->online_vcpus, 0);
  142. mutex_unlock(&kvm->lock);
  143. }
  144. void kvm_arch_sync_events(struct kvm *kvm)
  145. {
  146. }
  147. int kvm_dev_ioctl_check_extension(long ext)
  148. {
  149. int r;
  150. switch (ext) {
  151. #ifdef CONFIG_BOOKE
  152. case KVM_CAP_PPC_BOOKE_SREGS:
  153. #else
  154. case KVM_CAP_PPC_SEGSTATE:
  155. #endif
  156. case KVM_CAP_PPC_PAIRED_SINGLES:
  157. case KVM_CAP_PPC_UNSET_IRQ:
  158. case KVM_CAP_PPC_IRQ_LEVEL:
  159. case KVM_CAP_ENABLE_CAP:
  160. case KVM_CAP_PPC_OSI:
  161. case KVM_CAP_PPC_GET_PVINFO:
  162. r = 1;
  163. break;
  164. case KVM_CAP_COALESCED_MMIO:
  165. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  166. break;
  167. default:
  168. r = 0;
  169. break;
  170. }
  171. return r;
  172. }
  173. long kvm_arch_dev_ioctl(struct file *filp,
  174. unsigned int ioctl, unsigned long arg)
  175. {
  176. return -EINVAL;
  177. }
  178. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  179. struct kvm_memory_slot *memslot,
  180. struct kvm_memory_slot old,
  181. struct kvm_userspace_memory_region *mem,
  182. int user_alloc)
  183. {
  184. return 0;
  185. }
  186. void kvm_arch_commit_memory_region(struct kvm *kvm,
  187. struct kvm_userspace_memory_region *mem,
  188. struct kvm_memory_slot old,
  189. int user_alloc)
  190. {
  191. return;
  192. }
  193. void kvm_arch_flush_shadow(struct kvm *kvm)
  194. {
  195. }
  196. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  197. {
  198. struct kvm_vcpu *vcpu;
  199. vcpu = kvmppc_core_vcpu_create(kvm, id);
  200. if (!IS_ERR(vcpu))
  201. kvmppc_create_vcpu_debugfs(vcpu, id);
  202. return vcpu;
  203. }
  204. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  205. {
  206. /* Make sure we're not using the vcpu anymore */
  207. hrtimer_cancel(&vcpu->arch.dec_timer);
  208. tasklet_kill(&vcpu->arch.tasklet);
  209. kvmppc_remove_vcpu_debugfs(vcpu);
  210. kvmppc_core_vcpu_free(vcpu);
  211. }
  212. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  213. {
  214. kvm_arch_vcpu_free(vcpu);
  215. }
  216. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  217. {
  218. return kvmppc_core_pending_dec(vcpu);
  219. }
  220. static void kvmppc_decrementer_func(unsigned long data)
  221. {
  222. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  223. kvmppc_core_queue_dec(vcpu);
  224. if (waitqueue_active(&vcpu->wq)) {
  225. wake_up_interruptible(&vcpu->wq);
  226. vcpu->stat.halt_wakeup++;
  227. }
  228. }
  229. /*
  230. * low level hrtimer wake routine. Because this runs in hardirq context
  231. * we schedule a tasklet to do the real work.
  232. */
  233. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  234. {
  235. struct kvm_vcpu *vcpu;
  236. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  237. tasklet_schedule(&vcpu->arch.tasklet);
  238. return HRTIMER_NORESTART;
  239. }
  240. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  241. {
  242. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  243. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  244. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  245. #ifdef CONFIG_KVM_EXIT_TIMING
  246. mutex_init(&vcpu->arch.exit_timing_lock);
  247. #endif
  248. return 0;
  249. }
  250. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  251. {
  252. kvmppc_mmu_destroy(vcpu);
  253. }
  254. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  255. {
  256. #ifdef CONFIG_BOOKE
  257. /*
  258. * vrsave (formerly usprg0) isn't used by Linux, but may
  259. * be used by the guest.
  260. *
  261. * On non-booke this is associated with Altivec and
  262. * is handled by code in book3s.c.
  263. */
  264. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  265. #endif
  266. kvmppc_core_vcpu_load(vcpu, cpu);
  267. }
  268. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  269. {
  270. kvmppc_core_vcpu_put(vcpu);
  271. #ifdef CONFIG_BOOKE
  272. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  273. #endif
  274. }
  275. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  276. struct kvm_guest_debug *dbg)
  277. {
  278. return -EINVAL;
  279. }
  280. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  281. struct kvm_run *run)
  282. {
  283. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  284. }
  285. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  286. struct kvm_run *run)
  287. {
  288. u64 uninitialized_var(gpr);
  289. if (run->mmio.len > sizeof(gpr)) {
  290. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  291. return;
  292. }
  293. if (vcpu->arch.mmio_is_bigendian) {
  294. switch (run->mmio.len) {
  295. case 8: gpr = *(u64 *)run->mmio.data; break;
  296. case 4: gpr = *(u32 *)run->mmio.data; break;
  297. case 2: gpr = *(u16 *)run->mmio.data; break;
  298. case 1: gpr = *(u8 *)run->mmio.data; break;
  299. }
  300. } else {
  301. /* Convert BE data from userland back to LE. */
  302. switch (run->mmio.len) {
  303. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  304. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  305. case 1: gpr = *(u8 *)run->mmio.data; break;
  306. }
  307. }
  308. if (vcpu->arch.mmio_sign_extend) {
  309. switch (run->mmio.len) {
  310. #ifdef CONFIG_PPC64
  311. case 4:
  312. gpr = (s64)(s32)gpr;
  313. break;
  314. #endif
  315. case 2:
  316. gpr = (s64)(s16)gpr;
  317. break;
  318. case 1:
  319. gpr = (s64)(s8)gpr;
  320. break;
  321. }
  322. }
  323. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  324. switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) {
  325. case KVM_REG_GPR:
  326. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  327. break;
  328. case KVM_REG_FPR:
  329. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  330. break;
  331. #ifdef CONFIG_PPC_BOOK3S
  332. case KVM_REG_QPR:
  333. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  334. break;
  335. case KVM_REG_FQPR:
  336. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  337. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr;
  338. break;
  339. #endif
  340. default:
  341. BUG();
  342. }
  343. }
  344. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  345. unsigned int rt, unsigned int bytes, int is_bigendian)
  346. {
  347. if (bytes > sizeof(run->mmio.data)) {
  348. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  349. run->mmio.len);
  350. }
  351. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  352. run->mmio.len = bytes;
  353. run->mmio.is_write = 0;
  354. vcpu->arch.io_gpr = rt;
  355. vcpu->arch.mmio_is_bigendian = is_bigendian;
  356. vcpu->mmio_needed = 1;
  357. vcpu->mmio_is_write = 0;
  358. vcpu->arch.mmio_sign_extend = 0;
  359. return EMULATE_DO_MMIO;
  360. }
  361. /* Same as above, but sign extends */
  362. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  363. unsigned int rt, unsigned int bytes, int is_bigendian)
  364. {
  365. int r;
  366. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
  367. vcpu->arch.mmio_sign_extend = 1;
  368. return r;
  369. }
  370. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  371. u64 val, unsigned int bytes, int is_bigendian)
  372. {
  373. void *data = run->mmio.data;
  374. if (bytes > sizeof(run->mmio.data)) {
  375. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  376. run->mmio.len);
  377. }
  378. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  379. run->mmio.len = bytes;
  380. run->mmio.is_write = 1;
  381. vcpu->mmio_needed = 1;
  382. vcpu->mmio_is_write = 1;
  383. /* Store the value at the lowest bytes in 'data'. */
  384. if (is_bigendian) {
  385. switch (bytes) {
  386. case 8: *(u64 *)data = val; break;
  387. case 4: *(u32 *)data = val; break;
  388. case 2: *(u16 *)data = val; break;
  389. case 1: *(u8 *)data = val; break;
  390. }
  391. } else {
  392. /* Store LE value into 'data'. */
  393. switch (bytes) {
  394. case 4: st_le32(data, val); break;
  395. case 2: st_le16(data, val); break;
  396. case 1: *(u8 *)data = val; break;
  397. }
  398. }
  399. return EMULATE_DO_MMIO;
  400. }
  401. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  402. {
  403. int r;
  404. sigset_t sigsaved;
  405. if (vcpu->sigset_active)
  406. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  407. if (vcpu->mmio_needed) {
  408. if (!vcpu->mmio_is_write)
  409. kvmppc_complete_mmio_load(vcpu, run);
  410. vcpu->mmio_needed = 0;
  411. } else if (vcpu->arch.dcr_needed) {
  412. if (!vcpu->arch.dcr_is_write)
  413. kvmppc_complete_dcr_load(vcpu, run);
  414. vcpu->arch.dcr_needed = 0;
  415. } else if (vcpu->arch.osi_needed) {
  416. u64 *gprs = run->osi.gprs;
  417. int i;
  418. for (i = 0; i < 32; i++)
  419. kvmppc_set_gpr(vcpu, i, gprs[i]);
  420. vcpu->arch.osi_needed = 0;
  421. }
  422. kvmppc_core_deliver_interrupts(vcpu);
  423. local_irq_disable();
  424. kvm_guest_enter();
  425. r = __kvmppc_vcpu_run(run, vcpu);
  426. kvm_guest_exit();
  427. local_irq_enable();
  428. if (vcpu->sigset_active)
  429. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  430. return r;
  431. }
  432. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  433. {
  434. if (irq->irq == KVM_INTERRUPT_UNSET)
  435. kvmppc_core_dequeue_external(vcpu, irq);
  436. else
  437. kvmppc_core_queue_external(vcpu, irq);
  438. if (waitqueue_active(&vcpu->wq)) {
  439. wake_up_interruptible(&vcpu->wq);
  440. vcpu->stat.halt_wakeup++;
  441. }
  442. return 0;
  443. }
  444. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  445. struct kvm_enable_cap *cap)
  446. {
  447. int r;
  448. if (cap->flags)
  449. return -EINVAL;
  450. switch (cap->cap) {
  451. case KVM_CAP_PPC_OSI:
  452. r = 0;
  453. vcpu->arch.osi_enabled = true;
  454. break;
  455. default:
  456. r = -EINVAL;
  457. break;
  458. }
  459. return r;
  460. }
  461. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  462. struct kvm_mp_state *mp_state)
  463. {
  464. return -EINVAL;
  465. }
  466. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  467. struct kvm_mp_state *mp_state)
  468. {
  469. return -EINVAL;
  470. }
  471. long kvm_arch_vcpu_ioctl(struct file *filp,
  472. unsigned int ioctl, unsigned long arg)
  473. {
  474. struct kvm_vcpu *vcpu = filp->private_data;
  475. void __user *argp = (void __user *)arg;
  476. long r;
  477. switch (ioctl) {
  478. case KVM_INTERRUPT: {
  479. struct kvm_interrupt irq;
  480. r = -EFAULT;
  481. if (copy_from_user(&irq, argp, sizeof(irq)))
  482. goto out;
  483. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  484. goto out;
  485. }
  486. case KVM_ENABLE_CAP:
  487. {
  488. struct kvm_enable_cap cap;
  489. r = -EFAULT;
  490. if (copy_from_user(&cap, argp, sizeof(cap)))
  491. goto out;
  492. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  493. break;
  494. }
  495. default:
  496. r = -EINVAL;
  497. }
  498. out:
  499. return r;
  500. }
  501. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  502. {
  503. u32 inst_lis = 0x3c000000;
  504. u32 inst_ori = 0x60000000;
  505. u32 inst_nop = 0x60000000;
  506. u32 inst_sc = 0x44000002;
  507. u32 inst_imm_mask = 0xffff;
  508. /*
  509. * The hypercall to get into KVM from within guest context is as
  510. * follows:
  511. *
  512. * lis r0, r0, KVM_SC_MAGIC_R0@h
  513. * ori r0, KVM_SC_MAGIC_R0@l
  514. * sc
  515. * nop
  516. */
  517. pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
  518. pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
  519. pvinfo->hcall[2] = inst_sc;
  520. pvinfo->hcall[3] = inst_nop;
  521. return 0;
  522. }
  523. long kvm_arch_vm_ioctl(struct file *filp,
  524. unsigned int ioctl, unsigned long arg)
  525. {
  526. void __user *argp = (void __user *)arg;
  527. long r;
  528. switch (ioctl) {
  529. case KVM_PPC_GET_PVINFO: {
  530. struct kvm_ppc_pvinfo pvinfo;
  531. memset(&pvinfo, 0, sizeof(pvinfo));
  532. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  533. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  534. r = -EFAULT;
  535. goto out;
  536. }
  537. break;
  538. }
  539. default:
  540. r = -ENOTTY;
  541. }
  542. out:
  543. return r;
  544. }
  545. int kvm_arch_init(void *opaque)
  546. {
  547. return 0;
  548. }
  549. void kvm_arch_exit(void)
  550. {
  551. }