powerpc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/fs.h>
  26. #include <asm/cputable.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/kvm_ppc.h>
  29. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  30. {
  31. return gfn;
  32. }
  33. int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
  34. {
  35. /* XXX implement me */
  36. return 0;
  37. }
  38. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  39. {
  40. return 1;
  41. }
  42. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  43. {
  44. enum emulation_result er;
  45. int r;
  46. er = kvmppc_emulate_instruction(run, vcpu);
  47. switch (er) {
  48. case EMULATE_DONE:
  49. /* Future optimization: only reload non-volatiles if they were
  50. * actually modified. */
  51. r = RESUME_GUEST_NV;
  52. break;
  53. case EMULATE_DO_MMIO:
  54. run->exit_reason = KVM_EXIT_MMIO;
  55. /* We must reload nonvolatiles because "update" load/store
  56. * instructions modify register state. */
  57. /* Future optimization: only reload non-volatiles if they were
  58. * actually modified. */
  59. r = RESUME_HOST_NV;
  60. break;
  61. case EMULATE_FAIL:
  62. /* XXX Deliver Program interrupt to guest. */
  63. printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
  64. vcpu->arch.last_inst);
  65. r = RESUME_HOST;
  66. break;
  67. default:
  68. BUG();
  69. }
  70. return r;
  71. }
  72. void kvm_arch_hardware_enable(void *garbage)
  73. {
  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 r;
  88. if (strcmp(cur_cpu_spec->platform, "ppc440") == 0)
  89. r = 0;
  90. else
  91. r = -ENOTSUPP;
  92. *(int *)rtn = r;
  93. }
  94. struct kvm *kvm_arch_create_vm(void)
  95. {
  96. struct kvm *kvm;
  97. kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  98. if (!kvm)
  99. return ERR_PTR(-ENOMEM);
  100. return kvm;
  101. }
  102. static void kvmppc_free_vcpus(struct kvm *kvm)
  103. {
  104. unsigned int i;
  105. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  106. if (kvm->vcpus[i]) {
  107. kvm_arch_vcpu_free(kvm->vcpus[i]);
  108. kvm->vcpus[i] = NULL;
  109. }
  110. }
  111. }
  112. void kvm_arch_destroy_vm(struct kvm *kvm)
  113. {
  114. kvmppc_free_vcpus(kvm);
  115. kvm_free_physmem(kvm);
  116. kfree(kvm);
  117. }
  118. int kvm_dev_ioctl_check_extension(long ext)
  119. {
  120. int r;
  121. switch (ext) {
  122. case KVM_CAP_USER_MEMORY:
  123. r = 1;
  124. break;
  125. default:
  126. r = 0;
  127. break;
  128. }
  129. return r;
  130. }
  131. long kvm_arch_dev_ioctl(struct file *filp,
  132. unsigned int ioctl, unsigned long arg)
  133. {
  134. return -EINVAL;
  135. }
  136. int kvm_arch_set_memory_region(struct kvm *kvm,
  137. struct kvm_userspace_memory_region *mem,
  138. struct kvm_memory_slot old,
  139. int user_alloc)
  140. {
  141. return 0;
  142. }
  143. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  144. {
  145. struct kvm_vcpu *vcpu;
  146. int err;
  147. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  148. if (!vcpu) {
  149. err = -ENOMEM;
  150. goto out;
  151. }
  152. err = kvm_vcpu_init(vcpu, kvm, id);
  153. if (err)
  154. goto free_vcpu;
  155. return vcpu;
  156. free_vcpu:
  157. kmem_cache_free(kvm_vcpu_cache, vcpu);
  158. out:
  159. return ERR_PTR(err);
  160. }
  161. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  162. {
  163. kvm_vcpu_uninit(vcpu);
  164. kmem_cache_free(kvm_vcpu_cache, vcpu);
  165. }
  166. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  167. {
  168. kvm_arch_vcpu_free(vcpu);
  169. }
  170. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  171. {
  172. unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
  173. return test_bit(priority, &vcpu->arch.pending_exceptions);
  174. }
  175. static void kvmppc_decrementer_func(unsigned long data)
  176. {
  177. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  178. kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
  179. }
  180. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  181. {
  182. setup_timer(&vcpu->arch.dec_timer, kvmppc_decrementer_func,
  183. (unsigned long)vcpu);
  184. return 0;
  185. }
  186. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  187. {
  188. }
  189. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  190. {
  191. }
  192. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  193. {
  194. }
  195. void decache_vcpus_on_cpu(int cpu)
  196. {
  197. }
  198. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  199. struct kvm_debug_guest *dbg)
  200. {
  201. return -ENOTSUPP;
  202. }
  203. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  204. struct kvm_run *run)
  205. {
  206. u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
  207. *gpr = run->dcr.data;
  208. }
  209. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  210. struct kvm_run *run)
  211. {
  212. u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
  213. if (run->mmio.len > sizeof(*gpr)) {
  214. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  215. return;
  216. }
  217. if (vcpu->arch.mmio_is_bigendian) {
  218. switch (run->mmio.len) {
  219. case 4: *gpr = *(u32 *)run->mmio.data; break;
  220. case 2: *gpr = *(u16 *)run->mmio.data; break;
  221. case 1: *gpr = *(u8 *)run->mmio.data; break;
  222. }
  223. } else {
  224. /* Convert BE data from userland back to LE. */
  225. switch (run->mmio.len) {
  226. case 4: *gpr = ld_le32((u32 *)run->mmio.data); break;
  227. case 2: *gpr = ld_le16((u16 *)run->mmio.data); break;
  228. case 1: *gpr = *(u8 *)run->mmio.data; break;
  229. }
  230. }
  231. }
  232. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  233. unsigned int rt, unsigned int bytes, int is_bigendian)
  234. {
  235. if (bytes > sizeof(run->mmio.data)) {
  236. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  237. run->mmio.len);
  238. }
  239. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  240. run->mmio.len = bytes;
  241. run->mmio.is_write = 0;
  242. vcpu->arch.io_gpr = rt;
  243. vcpu->arch.mmio_is_bigendian = is_bigendian;
  244. vcpu->mmio_needed = 1;
  245. vcpu->mmio_is_write = 0;
  246. return EMULATE_DO_MMIO;
  247. }
  248. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  249. u32 val, unsigned int bytes, int is_bigendian)
  250. {
  251. void *data = run->mmio.data;
  252. if (bytes > sizeof(run->mmio.data)) {
  253. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  254. run->mmio.len);
  255. }
  256. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  257. run->mmio.len = bytes;
  258. run->mmio.is_write = 1;
  259. vcpu->mmio_needed = 1;
  260. vcpu->mmio_is_write = 1;
  261. /* Store the value at the lowest bytes in 'data'. */
  262. if (is_bigendian) {
  263. switch (bytes) {
  264. case 4: *(u32 *)data = val; break;
  265. case 2: *(u16 *)data = val; break;
  266. case 1: *(u8 *)data = val; break;
  267. }
  268. } else {
  269. /* Store LE value into 'data'. */
  270. switch (bytes) {
  271. case 4: st_le32(data, val); break;
  272. case 2: st_le16(data, val); break;
  273. case 1: *(u8 *)data = val; break;
  274. }
  275. }
  276. return EMULATE_DO_MMIO;
  277. }
  278. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  279. {
  280. int r;
  281. sigset_t sigsaved;
  282. if (vcpu->sigset_active)
  283. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  284. if (vcpu->mmio_needed) {
  285. if (!vcpu->mmio_is_write)
  286. kvmppc_complete_mmio_load(vcpu, run);
  287. vcpu->mmio_needed = 0;
  288. } else if (vcpu->arch.dcr_needed) {
  289. if (!vcpu->arch.dcr_is_write)
  290. kvmppc_complete_dcr_load(vcpu, run);
  291. vcpu->arch.dcr_needed = 0;
  292. }
  293. kvmppc_check_and_deliver_interrupts(vcpu);
  294. local_irq_disable();
  295. kvm_guest_enter();
  296. r = __kvmppc_vcpu_run(run, vcpu);
  297. kvm_guest_exit();
  298. local_irq_enable();
  299. if (vcpu->sigset_active)
  300. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  301. return r;
  302. }
  303. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  304. {
  305. kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
  306. return 0;
  307. }
  308. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  309. struct kvm_mp_state *mp_state)
  310. {
  311. return -EINVAL;
  312. }
  313. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  314. struct kvm_mp_state *mp_state)
  315. {
  316. return -EINVAL;
  317. }
  318. long kvm_arch_vcpu_ioctl(struct file *filp,
  319. unsigned int ioctl, unsigned long arg)
  320. {
  321. struct kvm_vcpu *vcpu = filp->private_data;
  322. void __user *argp = (void __user *)arg;
  323. long r;
  324. switch (ioctl) {
  325. case KVM_INTERRUPT: {
  326. struct kvm_interrupt irq;
  327. r = -EFAULT;
  328. if (copy_from_user(&irq, argp, sizeof(irq)))
  329. goto out;
  330. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  331. break;
  332. }
  333. default:
  334. r = -EINVAL;
  335. }
  336. out:
  337. return r;
  338. }
  339. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  340. {
  341. return -ENOTSUPP;
  342. }
  343. long kvm_arch_vm_ioctl(struct file *filp,
  344. unsigned int ioctl, unsigned long arg)
  345. {
  346. long r;
  347. switch (ioctl) {
  348. default:
  349. r = -EINVAL;
  350. }
  351. return r;
  352. }
  353. int kvm_arch_init(void *opaque)
  354. {
  355. return 0;
  356. }
  357. void kvm_arch_exit(void)
  358. {
  359. }