powerpc.c 12 KB

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