powerpc.c 12 KB

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