powerpc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  31. {
  32. return gfn;
  33. }
  34. int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
  35. {
  36. return !!(v->arch.pending_exceptions);
  37. }
  38. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  39. {
  40. return !(v->arch.msr & MSR_WE);
  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. case KVM_CAP_COALESCED_MMIO:
  126. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  127. break;
  128. default:
  129. r = 0;
  130. break;
  131. }
  132. return r;
  133. }
  134. long kvm_arch_dev_ioctl(struct file *filp,
  135. unsigned int ioctl, unsigned long arg)
  136. {
  137. return -EINVAL;
  138. }
  139. int kvm_arch_set_memory_region(struct kvm *kvm,
  140. struct kvm_userspace_memory_region *mem,
  141. struct kvm_memory_slot old,
  142. int user_alloc)
  143. {
  144. return 0;
  145. }
  146. void kvm_arch_flush_shadow(struct kvm *kvm)
  147. {
  148. }
  149. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  150. {
  151. struct kvm_vcpu *vcpu;
  152. int err;
  153. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  154. if (!vcpu) {
  155. err = -ENOMEM;
  156. goto out;
  157. }
  158. err = kvm_vcpu_init(vcpu, kvm, id);
  159. if (err)
  160. goto free_vcpu;
  161. return vcpu;
  162. free_vcpu:
  163. kmem_cache_free(kvm_vcpu_cache, vcpu);
  164. out:
  165. return ERR_PTR(err);
  166. }
  167. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  168. {
  169. kvm_vcpu_uninit(vcpu);
  170. kmem_cache_free(kvm_vcpu_cache, 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. unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
  179. return test_bit(priority, &vcpu->arch.pending_exceptions);
  180. }
  181. static void kvmppc_decrementer_func(unsigned long data)
  182. {
  183. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  184. kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
  185. if (waitqueue_active(&vcpu->wq)) {
  186. wake_up_interruptible(&vcpu->wq);
  187. vcpu->stat.halt_wakeup++;
  188. }
  189. }
  190. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  191. {
  192. setup_timer(&vcpu->arch.dec_timer, kvmppc_decrementer_func,
  193. (unsigned long)vcpu);
  194. return 0;
  195. }
  196. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  197. {
  198. }
  199. /* Note: clearing MSR[DE] just means that the debug interrupt will not be
  200. * delivered *immediately*. Instead, it simply sets the appropriate DBSR bits.
  201. * If those DBSR bits are still set when MSR[DE] is re-enabled, the interrupt
  202. * will be delivered as an "imprecise debug event" (which is indicated by
  203. * DBSR[IDE].
  204. */
  205. static void kvmppc_disable_debug_interrupts(void)
  206. {
  207. mtmsr(mfmsr() & ~MSR_DE);
  208. }
  209. static void kvmppc_restore_host_debug_state(struct kvm_vcpu *vcpu)
  210. {
  211. kvmppc_disable_debug_interrupts();
  212. mtspr(SPRN_IAC1, vcpu->arch.host_iac[0]);
  213. mtspr(SPRN_IAC2, vcpu->arch.host_iac[1]);
  214. mtspr(SPRN_IAC3, vcpu->arch.host_iac[2]);
  215. mtspr(SPRN_IAC4, vcpu->arch.host_iac[3]);
  216. mtspr(SPRN_DBCR1, vcpu->arch.host_dbcr1);
  217. mtspr(SPRN_DBCR2, vcpu->arch.host_dbcr2);
  218. mtspr(SPRN_DBCR0, vcpu->arch.host_dbcr0);
  219. mtmsr(vcpu->arch.host_msr);
  220. }
  221. static void kvmppc_load_guest_debug_registers(struct kvm_vcpu *vcpu)
  222. {
  223. struct kvm_guest_debug *dbg = &vcpu->guest_debug;
  224. u32 dbcr0 = 0;
  225. vcpu->arch.host_msr = mfmsr();
  226. kvmppc_disable_debug_interrupts();
  227. /* Save host debug register state. */
  228. vcpu->arch.host_iac[0] = mfspr(SPRN_IAC1);
  229. vcpu->arch.host_iac[1] = mfspr(SPRN_IAC2);
  230. vcpu->arch.host_iac[2] = mfspr(SPRN_IAC3);
  231. vcpu->arch.host_iac[3] = mfspr(SPRN_IAC4);
  232. vcpu->arch.host_dbcr0 = mfspr(SPRN_DBCR0);
  233. vcpu->arch.host_dbcr1 = mfspr(SPRN_DBCR1);
  234. vcpu->arch.host_dbcr2 = mfspr(SPRN_DBCR2);
  235. /* set registers up for guest */
  236. if (dbg->bp[0]) {
  237. mtspr(SPRN_IAC1, dbg->bp[0]);
  238. dbcr0 |= DBCR0_IAC1 | DBCR0_IDM;
  239. }
  240. if (dbg->bp[1]) {
  241. mtspr(SPRN_IAC2, dbg->bp[1]);
  242. dbcr0 |= DBCR0_IAC2 | DBCR0_IDM;
  243. }
  244. if (dbg->bp[2]) {
  245. mtspr(SPRN_IAC3, dbg->bp[2]);
  246. dbcr0 |= DBCR0_IAC3 | DBCR0_IDM;
  247. }
  248. if (dbg->bp[3]) {
  249. mtspr(SPRN_IAC4, dbg->bp[3]);
  250. dbcr0 |= DBCR0_IAC4 | DBCR0_IDM;
  251. }
  252. mtspr(SPRN_DBCR0, dbcr0);
  253. mtspr(SPRN_DBCR1, 0);
  254. mtspr(SPRN_DBCR2, 0);
  255. }
  256. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  257. {
  258. int i;
  259. if (vcpu->guest_debug.enabled)
  260. kvmppc_load_guest_debug_registers(vcpu);
  261. /* Mark every guest entry in the shadow TLB entry modified, so that they
  262. * will all be reloaded on the next vcpu run (instead of being
  263. * demand-faulted). */
  264. for (i = 0; i <= tlb_44x_hwater; i++)
  265. kvmppc_tlbe_set_modified(vcpu, i);
  266. }
  267. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  268. {
  269. if (vcpu->guest_debug.enabled)
  270. kvmppc_restore_host_debug_state(vcpu);
  271. /* Don't leave guest TLB entries resident when being de-scheduled. */
  272. /* XXX It would be nice to differentiate between heavyweight exit and
  273. * sched_out here, since we could avoid the TLB flush for heavyweight
  274. * exits. */
  275. _tlbia();
  276. }
  277. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  278. struct kvm_debug_guest *dbg)
  279. {
  280. int i;
  281. vcpu->guest_debug.enabled = dbg->enabled;
  282. if (vcpu->guest_debug.enabled) {
  283. for (i=0; i < ARRAY_SIZE(vcpu->guest_debug.bp); i++) {
  284. if (dbg->breakpoints[i].enabled)
  285. vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
  286. else
  287. vcpu->guest_debug.bp[i] = 0;
  288. }
  289. }
  290. return 0;
  291. }
  292. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  293. struct kvm_run *run)
  294. {
  295. u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
  296. *gpr = run->dcr.data;
  297. }
  298. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  299. struct kvm_run *run)
  300. {
  301. u32 *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr];
  302. if (run->mmio.len > sizeof(*gpr)) {
  303. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  304. return;
  305. }
  306. if (vcpu->arch.mmio_is_bigendian) {
  307. switch (run->mmio.len) {
  308. case 4: *gpr = *(u32 *)run->mmio.data; break;
  309. case 2: *gpr = *(u16 *)run->mmio.data; break;
  310. case 1: *gpr = *(u8 *)run->mmio.data; break;
  311. }
  312. } else {
  313. /* Convert BE data from userland back to LE. */
  314. switch (run->mmio.len) {
  315. case 4: *gpr = ld_le32((u32 *)run->mmio.data); break;
  316. case 2: *gpr = ld_le16((u16 *)run->mmio.data); break;
  317. case 1: *gpr = *(u8 *)run->mmio.data; break;
  318. }
  319. }
  320. }
  321. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  322. unsigned int rt, unsigned int bytes, int is_bigendian)
  323. {
  324. if (bytes > sizeof(run->mmio.data)) {
  325. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  326. run->mmio.len);
  327. }
  328. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  329. run->mmio.len = bytes;
  330. run->mmio.is_write = 0;
  331. vcpu->arch.io_gpr = rt;
  332. vcpu->arch.mmio_is_bigendian = is_bigendian;
  333. vcpu->mmio_needed = 1;
  334. vcpu->mmio_is_write = 0;
  335. return EMULATE_DO_MMIO;
  336. }
  337. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  338. u32 val, unsigned int bytes, int is_bigendian)
  339. {
  340. void *data = run->mmio.data;
  341. if (bytes > sizeof(run->mmio.data)) {
  342. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  343. run->mmio.len);
  344. }
  345. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  346. run->mmio.len = bytes;
  347. run->mmio.is_write = 1;
  348. vcpu->mmio_needed = 1;
  349. vcpu->mmio_is_write = 1;
  350. /* Store the value at the lowest bytes in 'data'. */
  351. if (is_bigendian) {
  352. switch (bytes) {
  353. case 4: *(u32 *)data = val; break;
  354. case 2: *(u16 *)data = val; break;
  355. case 1: *(u8 *)data = val; break;
  356. }
  357. } else {
  358. /* Store LE value into 'data'. */
  359. switch (bytes) {
  360. case 4: st_le32(data, val); break;
  361. case 2: st_le16(data, val); break;
  362. case 1: *(u8 *)data = val; break;
  363. }
  364. }
  365. return EMULATE_DO_MMIO;
  366. }
  367. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  368. {
  369. int r;
  370. sigset_t sigsaved;
  371. vcpu_load(vcpu);
  372. if (vcpu->sigset_active)
  373. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  374. if (vcpu->mmio_needed) {
  375. if (!vcpu->mmio_is_write)
  376. kvmppc_complete_mmio_load(vcpu, run);
  377. vcpu->mmio_needed = 0;
  378. } else if (vcpu->arch.dcr_needed) {
  379. if (!vcpu->arch.dcr_is_write)
  380. kvmppc_complete_dcr_load(vcpu, run);
  381. vcpu->arch.dcr_needed = 0;
  382. }
  383. kvmppc_check_and_deliver_interrupts(vcpu);
  384. local_irq_disable();
  385. kvm_guest_enter();
  386. r = __kvmppc_vcpu_run(run, vcpu);
  387. kvm_guest_exit();
  388. local_irq_enable();
  389. if (vcpu->sigset_active)
  390. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  391. vcpu_put(vcpu);
  392. return r;
  393. }
  394. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  395. {
  396. kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
  397. if (waitqueue_active(&vcpu->wq)) {
  398. wake_up_interruptible(&vcpu->wq);
  399. vcpu->stat.halt_wakeup++;
  400. }
  401. return 0;
  402. }
  403. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  404. struct kvm_mp_state *mp_state)
  405. {
  406. return -EINVAL;
  407. }
  408. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  409. struct kvm_mp_state *mp_state)
  410. {
  411. return -EINVAL;
  412. }
  413. long kvm_arch_vcpu_ioctl(struct file *filp,
  414. unsigned int ioctl, unsigned long arg)
  415. {
  416. struct kvm_vcpu *vcpu = filp->private_data;
  417. void __user *argp = (void __user *)arg;
  418. long r;
  419. switch (ioctl) {
  420. case KVM_INTERRUPT: {
  421. struct kvm_interrupt irq;
  422. r = -EFAULT;
  423. if (copy_from_user(&irq, argp, sizeof(irq)))
  424. goto out;
  425. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  426. break;
  427. }
  428. default:
  429. r = -EINVAL;
  430. }
  431. out:
  432. return r;
  433. }
  434. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  435. {
  436. return -ENOTSUPP;
  437. }
  438. long kvm_arch_vm_ioctl(struct file *filp,
  439. unsigned int ioctl, unsigned long arg)
  440. {
  441. long r;
  442. switch (ioctl) {
  443. default:
  444. r = -EINVAL;
  445. }
  446. return r;
  447. }
  448. int kvm_arch_init(void *opaque)
  449. {
  450. return 0;
  451. }
  452. void kvm_arch_exit(void)
  453. {
  454. }