powerpc.c 9.7 KB

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