powerpc.c 9.9 KB

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