powerpc.c 14 KB

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