powerpc.c 12 KB

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