powerpc.c 12 KB

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