powerpc.c 9.3 KB

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