powerpc.c 12 KB

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