powerpc.c 9.3 KB

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