powerpc.c 13 KB

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