powerpc.c 14 KB

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