powerpc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. vcpu->arch.last_inst);
  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. 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. kvmppc_create_vcpu_debugfs(vcpu, id);
  166. return vcpu;
  167. }
  168. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  169. {
  170. kvmppc_remove_vcpu_debugfs(vcpu);
  171. kvmppc_core_vcpu_free(vcpu);
  172. }
  173. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  174. {
  175. kvm_arch_vcpu_free(vcpu);
  176. }
  177. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  178. {
  179. return kvmppc_core_pending_dec(vcpu);
  180. }
  181. static void kvmppc_decrementer_func(unsigned long data)
  182. {
  183. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  184. kvmppc_core_queue_dec(vcpu);
  185. if (waitqueue_active(&vcpu->wq)) {
  186. wake_up_interruptible(&vcpu->wq);
  187. vcpu->stat.halt_wakeup++;
  188. }
  189. }
  190. /*
  191. * low level hrtimer wake routine. Because this runs in hardirq context
  192. * we schedule a tasklet to do the real work.
  193. */
  194. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  195. {
  196. struct kvm_vcpu *vcpu;
  197. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  198. tasklet_schedule(&vcpu->arch.tasklet);
  199. return HRTIMER_NORESTART;
  200. }
  201. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  202. {
  203. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  204. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  205. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  206. return 0;
  207. }
  208. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  209. {
  210. kvmppc_mmu_destroy(vcpu);
  211. }
  212. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  213. {
  214. kvmppc_core_vcpu_load(vcpu, cpu);
  215. }
  216. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  217. {
  218. kvmppc_core_vcpu_put(vcpu);
  219. }
  220. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  221. struct kvm_guest_debug *dbg)
  222. {
  223. return -EINVAL;
  224. }
  225. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  226. struct kvm_run *run)
  227. {
  228. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  229. }
  230. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  231. struct kvm_run *run)
  232. {
  233. ulong gpr;
  234. if (run->mmio.len > sizeof(gpr)) {
  235. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  236. return;
  237. }
  238. if (vcpu->arch.mmio_is_bigendian) {
  239. switch (run->mmio.len) {
  240. case 4: gpr = *(u32 *)run->mmio.data; break;
  241. case 2: gpr = *(u16 *)run->mmio.data; break;
  242. case 1: gpr = *(u8 *)run->mmio.data; break;
  243. }
  244. } else {
  245. /* Convert BE data from userland back to LE. */
  246. switch (run->mmio.len) {
  247. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  248. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  249. case 1: gpr = *(u8 *)run->mmio.data; break;
  250. }
  251. }
  252. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  253. }
  254. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  255. unsigned int rt, unsigned int bytes, int is_bigendian)
  256. {
  257. if (bytes > sizeof(run->mmio.data)) {
  258. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  259. run->mmio.len);
  260. }
  261. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  262. run->mmio.len = bytes;
  263. run->mmio.is_write = 0;
  264. vcpu->arch.io_gpr = rt;
  265. vcpu->arch.mmio_is_bigendian = is_bigendian;
  266. vcpu->mmio_needed = 1;
  267. vcpu->mmio_is_write = 0;
  268. return EMULATE_DO_MMIO;
  269. }
  270. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  271. u32 val, unsigned int bytes, int is_bigendian)
  272. {
  273. void *data = run->mmio.data;
  274. if (bytes > sizeof(run->mmio.data)) {
  275. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  276. run->mmio.len);
  277. }
  278. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  279. run->mmio.len = bytes;
  280. run->mmio.is_write = 1;
  281. vcpu->mmio_needed = 1;
  282. vcpu->mmio_is_write = 1;
  283. /* Store the value at the lowest bytes in 'data'. */
  284. if (is_bigendian) {
  285. switch (bytes) {
  286. case 4: *(u32 *)data = val; break;
  287. case 2: *(u16 *)data = val; break;
  288. case 1: *(u8 *)data = val; break;
  289. }
  290. } else {
  291. /* Store LE value into 'data'. */
  292. switch (bytes) {
  293. case 4: st_le32(data, val); break;
  294. case 2: st_le16(data, val); break;
  295. case 1: *(u8 *)data = val; break;
  296. }
  297. }
  298. return EMULATE_DO_MMIO;
  299. }
  300. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  301. {
  302. int r;
  303. sigset_t sigsaved;
  304. vcpu_load(vcpu);
  305. if (vcpu->sigset_active)
  306. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  307. if (vcpu->mmio_needed) {
  308. if (!vcpu->mmio_is_write)
  309. kvmppc_complete_mmio_load(vcpu, run);
  310. vcpu->mmio_needed = 0;
  311. } else if (vcpu->arch.dcr_needed) {
  312. if (!vcpu->arch.dcr_is_write)
  313. kvmppc_complete_dcr_load(vcpu, run);
  314. vcpu->arch.dcr_needed = 0;
  315. }
  316. kvmppc_core_deliver_interrupts(vcpu);
  317. local_irq_disable();
  318. kvm_guest_enter();
  319. r = __kvmppc_vcpu_run(run, vcpu);
  320. kvm_guest_exit();
  321. local_irq_enable();
  322. if (vcpu->sigset_active)
  323. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  324. vcpu_put(vcpu);
  325. return r;
  326. }
  327. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  328. {
  329. kvmppc_core_queue_external(vcpu, irq);
  330. if (waitqueue_active(&vcpu->wq)) {
  331. wake_up_interruptible(&vcpu->wq);
  332. vcpu->stat.halt_wakeup++;
  333. }
  334. return 0;
  335. }
  336. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  337. struct kvm_mp_state *mp_state)
  338. {
  339. return -EINVAL;
  340. }
  341. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  342. struct kvm_mp_state *mp_state)
  343. {
  344. return -EINVAL;
  345. }
  346. long kvm_arch_vcpu_ioctl(struct file *filp,
  347. unsigned int ioctl, unsigned long arg)
  348. {
  349. struct kvm_vcpu *vcpu = filp->private_data;
  350. void __user *argp = (void __user *)arg;
  351. long r;
  352. switch (ioctl) {
  353. case KVM_INTERRUPT: {
  354. struct kvm_interrupt irq;
  355. r = -EFAULT;
  356. if (copy_from_user(&irq, argp, sizeof(irq)))
  357. goto out;
  358. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  359. break;
  360. }
  361. default:
  362. r = -EINVAL;
  363. }
  364. out:
  365. return r;
  366. }
  367. long kvm_arch_vm_ioctl(struct file *filp,
  368. unsigned int ioctl, unsigned long arg)
  369. {
  370. long r;
  371. switch (ioctl) {
  372. default:
  373. r = -ENOTTY;
  374. }
  375. return r;
  376. }
  377. int kvm_arch_init(void *opaque)
  378. {
  379. return 0;
  380. }
  381. void kvm_arch_exit(void)
  382. {
  383. }