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.
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.
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.
15 * Copyright IBM Corp. 2007
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/vmalloc.h>
25 #include <linux/hrtimer.h>
26 #include <linux/sched/signal.h>
28 #include <linux/slab.h>
29 #include <linux/file.h>
30 #include <linux/module.h>
31 #include <linux/irqbypass.h>
32 #include <linux/kvm_irqfd.h>
33 #include <asm/cputable.h>
34 #include <linux/uaccess.h>
35 #include <asm/kvm_ppc.h>
36 #include <asm/cputhreads.h>
37 #include <asm/irqflags.h>
38 #include <asm/iommu.h>
39 #include <asm/switch_to.h>
41 #ifdef CONFIG_PPC_PSERIES
42 #include <asm/hvcall.h>
43 #include <asm/plpar_wrappers.h>
48 #include "../mm/mmu_decl.h"
50 #define CREATE_TRACE_POINTS
53 struct kvmppc_ops *kvmppc_hv_ops;
54 EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
55 struct kvmppc_ops *kvmppc_pr_ops;
56 EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
59 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
61 return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
64 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
69 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
75 * Common checks before entering the guest world. Call with interrupts
80 * == 1 if we're ready to go into guest state
81 * <= 0 if we need to go back to the host with return value
83 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
87 WARN_ON(irqs_disabled());
98 if (signal_pending(current)) {
99 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
100 vcpu->run->exit_reason = KVM_EXIT_INTR;
105 vcpu->mode = IN_GUEST_MODE;
108 * Reading vcpu->requests must happen after setting vcpu->mode,
109 * so we don't miss a request because the requester sees
110 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
111 * before next entering the guest (and thus doesn't IPI).
112 * This also orders the write to mode from any reads
113 * to the page tables done while the VCPU is running.
114 * Please see the comment in kvm_flush_remote_tlbs.
118 if (kvm_request_pending(vcpu)) {
119 /* Make sure we process requests preemptable */
121 trace_kvm_check_requests(vcpu);
122 r = kvmppc_core_check_requests(vcpu);
129 if (kvmppc_core_prepare_to_enter(vcpu)) {
130 /* interrupts got enabled in between, so we
131 are back at square 1 */
135 guest_enter_irqoff();
143 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
145 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
146 static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
148 struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
151 shared->sprg0 = swab64(shared->sprg0);
152 shared->sprg1 = swab64(shared->sprg1);
153 shared->sprg2 = swab64(shared->sprg2);
154 shared->sprg3 = swab64(shared->sprg3);
155 shared->srr0 = swab64(shared->srr0);
156 shared->srr1 = swab64(shared->srr1);
157 shared->dar = swab64(shared->dar);
158 shared->msr = swab64(shared->msr);
159 shared->dsisr = swab32(shared->dsisr);
160 shared->int_pending = swab32(shared->int_pending);
161 for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
162 shared->sr[i] = swab32(shared->sr[i]);
166 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
168 int nr = kvmppc_get_gpr(vcpu, 11);
170 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
171 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
172 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
173 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
174 unsigned long r2 = 0;
176 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
178 param1 &= 0xffffffff;
179 param2 &= 0xffffffff;
180 param3 &= 0xffffffff;
181 param4 &= 0xffffffff;
185 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
187 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
188 /* Book3S can be little endian, find it out here */
189 int shared_big_endian = true;
190 if (vcpu->arch.intr_msr & MSR_LE)
191 shared_big_endian = false;
192 if (shared_big_endian != vcpu->arch.shared_big_endian)
193 kvmppc_swab_shared(vcpu);
194 vcpu->arch.shared_big_endian = shared_big_endian;
197 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
199 * Older versions of the Linux magic page code had
200 * a bug where they would map their trampoline code
201 * NX. If that's the case, remove !PR NX capability.
203 vcpu->arch.disable_kernel_nx = true;
204 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
207 vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
208 vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
210 #ifdef CONFIG_PPC_64K_PAGES
212 * Make sure our 4k magic page is in the same window of a 64k
213 * page within the guest and within the host's page.
215 if ((vcpu->arch.magic_page_pa & 0xf000) !=
216 ((ulong)vcpu->arch.shared & 0xf000)) {
217 void *old_shared = vcpu->arch.shared;
218 ulong shared = (ulong)vcpu->arch.shared;
222 shared |= vcpu->arch.magic_page_pa & 0xf000;
223 new_shared = (void*)shared;
224 memcpy(new_shared, old_shared, 0x1000);
225 vcpu->arch.shared = new_shared;
229 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
234 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
236 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
237 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
240 /* Second return value is in r4 */
242 case EV_HCALL_TOKEN(EV_IDLE):
244 kvm_vcpu_block(vcpu);
245 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
248 r = EV_UNIMPLEMENTED;
252 kvmppc_set_gpr(vcpu, 4, r2);
256 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
258 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
262 /* We have to know what CPU to virtualize */
266 /* PAPR only works with book3s_64 */
267 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
270 /* HV KVM can only do PAPR mode for now */
271 if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
274 #ifdef CONFIG_KVM_BOOKE_HV
275 if (!cpu_has_feature(CPU_FTR_EMB_HV))
283 return r ? 0 : -EINVAL;
285 EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
287 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
289 enum emulation_result er;
292 er = kvmppc_emulate_loadstore(vcpu);
295 /* Future optimization: only reload non-volatiles if they were
296 * actually modified. */
302 case EMULATE_DO_MMIO:
303 run->exit_reason = KVM_EXIT_MMIO;
304 /* We must reload nonvolatiles because "update" load/store
305 * instructions modify register state. */
306 /* Future optimization: only reload non-volatiles if they were
307 * actually modified. */
314 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
315 /* XXX Deliver Program interrupt to guest. */
316 pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
327 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
329 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
332 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
333 struct kvmppc_pte pte;
338 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
348 /* Magic page override */
349 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
350 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
351 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
352 void *magic = vcpu->arch.shared;
353 magic += pte.eaddr & 0xfff;
354 memcpy(magic, ptr, size);
358 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
359 return EMULATE_DO_MMIO;
363 EXPORT_SYMBOL_GPL(kvmppc_st);
365 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
368 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
369 struct kvmppc_pte pte;
374 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
384 if (!data && !pte.may_execute)
387 /* Magic page override */
388 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
389 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
390 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
391 void *magic = vcpu->arch.shared;
392 magic += pte.eaddr & 0xfff;
393 memcpy(ptr, magic, size);
397 if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
398 return EMULATE_DO_MMIO;
402 EXPORT_SYMBOL_GPL(kvmppc_ld);
404 int kvm_arch_hardware_enable(void)
409 int kvm_arch_hardware_setup(void)
414 void kvm_arch_check_processor_compat(void *rtn)
416 *(int *)rtn = kvmppc_core_check_processor_compat();
419 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
421 struct kvmppc_ops *kvm_ops = NULL;
423 * if we have both HV and PR enabled, default is HV
427 kvm_ops = kvmppc_hv_ops;
429 kvm_ops = kvmppc_pr_ops;
432 } else if (type == KVM_VM_PPC_HV) {
435 kvm_ops = kvmppc_hv_ops;
436 } else if (type == KVM_VM_PPC_PR) {
439 kvm_ops = kvmppc_pr_ops;
443 if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
446 kvm->arch.kvm_ops = kvm_ops;
447 return kvmppc_core_init_vm(kvm);
452 bool kvm_arch_has_vcpu_debugfs(void)
457 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
462 void kvm_arch_destroy_vm(struct kvm *kvm)
465 struct kvm_vcpu *vcpu;
467 #ifdef CONFIG_KVM_XICS
469 * We call kick_all_cpus_sync() to ensure that all
470 * CPUs have executed any pending IPIs before we
471 * continue and free VCPUs structures below.
473 if (is_kvmppc_hv_enabled(kvm))
474 kick_all_cpus_sync();
477 kvm_for_each_vcpu(i, vcpu, kvm)
478 kvm_arch_vcpu_free(vcpu);
480 mutex_lock(&kvm->lock);
481 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
482 kvm->vcpus[i] = NULL;
484 atomic_set(&kvm->online_vcpus, 0);
486 kvmppc_core_destroy_vm(kvm);
488 mutex_unlock(&kvm->lock);
490 /* drop the module reference */
491 module_put(kvm->arch.kvm_ops->owner);
494 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
497 /* Assume we're using HV mode when the HV module is loaded */
498 int hv_enabled = kvmppc_hv_ops ? 1 : 0;
502 * Hooray - we know which VM type we're running on. Depend on
503 * that rather than the guess above.
505 hv_enabled = is_kvmppc_hv_enabled(kvm);
510 case KVM_CAP_PPC_BOOKE_SREGS:
511 case KVM_CAP_PPC_BOOKE_WATCHDOG:
512 case KVM_CAP_PPC_EPR:
514 case KVM_CAP_PPC_SEGSTATE:
515 case KVM_CAP_PPC_HIOR:
516 case KVM_CAP_PPC_PAPR:
518 case KVM_CAP_PPC_UNSET_IRQ:
519 case KVM_CAP_PPC_IRQ_LEVEL:
520 case KVM_CAP_ENABLE_CAP:
521 case KVM_CAP_ENABLE_CAP_VM:
522 case KVM_CAP_ONE_REG:
523 case KVM_CAP_IOEVENTFD:
524 case KVM_CAP_DEVICE_CTRL:
525 case KVM_CAP_IMMEDIATE_EXIT:
528 case KVM_CAP_PPC_PAIRED_SINGLES:
529 case KVM_CAP_PPC_OSI:
530 case KVM_CAP_PPC_GET_PVINFO:
531 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
534 /* We support this only for PR */
537 #ifdef CONFIG_KVM_MPIC
538 case KVM_CAP_IRQ_MPIC:
543 #ifdef CONFIG_PPC_BOOK3S_64
544 case KVM_CAP_SPAPR_TCE:
545 case KVM_CAP_SPAPR_TCE_64:
547 case KVM_CAP_SPAPR_TCE_VFIO:
548 case KVM_CAP_PPC_RTAS:
549 case KVM_CAP_PPC_FIXUP_HCALL:
550 case KVM_CAP_PPC_ENABLE_HCALL:
551 #ifdef CONFIG_KVM_XICS
552 case KVM_CAP_IRQ_XICS:
554 case KVM_CAP_PPC_GET_CPU_CHAR:
558 case KVM_CAP_PPC_ALLOC_HTAB:
561 #endif /* CONFIG_PPC_BOOK3S_64 */
562 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
563 case KVM_CAP_PPC_SMT:
566 if (kvm->arch.emul_smt_mode > 1)
567 r = kvm->arch.emul_smt_mode;
569 r = kvm->arch.smt_mode;
570 } else if (hv_enabled) {
571 if (cpu_has_feature(CPU_FTR_ARCH_300))
574 r = threads_per_subcore;
577 case KVM_CAP_PPC_SMT_POSSIBLE:
580 if (!cpu_has_feature(CPU_FTR_ARCH_300))
581 r = ((threads_per_subcore << 1) - 1);
583 /* P9 can emulate dbells, so allow any mode */
587 case KVM_CAP_PPC_RMA:
590 case KVM_CAP_PPC_HWRNG:
591 r = kvmppc_hwrng_present();
593 case KVM_CAP_PPC_MMU_RADIX:
594 r = !!(hv_enabled && radix_enabled());
596 case KVM_CAP_PPC_MMU_HASH_V3:
597 r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
598 cpu_has_feature(CPU_FTR_HVMODE));
600 case KVM_CAP_PPC_NESTED_HV:
601 r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
602 !kvmppc_hv_ops->enable_nested(NULL));
605 case KVM_CAP_SYNC_MMU:
606 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
608 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
614 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
615 case KVM_CAP_PPC_HTAB_FD:
619 case KVM_CAP_NR_VCPUS:
621 * Recommending a number of CPUs is somewhat arbitrary; we
622 * return the number of present CPUs for -HV (since a host
623 * will have secondary threads "offline"), and for other KVM
624 * implementations just count online CPUs.
627 r = num_present_cpus();
629 r = num_online_cpus();
631 case KVM_CAP_NR_MEMSLOTS:
632 r = KVM_USER_MEM_SLOTS;
634 case KVM_CAP_MAX_VCPUS:
637 #ifdef CONFIG_PPC_BOOK3S_64
638 case KVM_CAP_PPC_GET_SMMU_INFO:
641 case KVM_CAP_SPAPR_MULTITCE:
644 case KVM_CAP_SPAPR_RESIZE_HPT:
648 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
649 case KVM_CAP_PPC_FWNMI:
653 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
654 case KVM_CAP_PPC_HTM:
655 r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) ||
656 (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST));
667 long kvm_arch_dev_ioctl(struct file *filp,
668 unsigned int ioctl, unsigned long arg)
673 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
674 struct kvm_memory_slot *dont)
676 kvmppc_core_free_memslot(kvm, free, dont);
679 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
680 unsigned long npages)
682 return kvmppc_core_create_memslot(kvm, slot, npages);
685 int kvm_arch_prepare_memory_region(struct kvm *kvm,
686 struct kvm_memory_slot *memslot,
687 const struct kvm_userspace_memory_region *mem,
688 enum kvm_mr_change change)
690 return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
693 void kvm_arch_commit_memory_region(struct kvm *kvm,
694 const struct kvm_userspace_memory_region *mem,
695 const struct kvm_memory_slot *old,
696 const struct kvm_memory_slot *new,
697 enum kvm_mr_change change)
699 kvmppc_core_commit_memory_region(kvm, mem, old, new);
702 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
703 struct kvm_memory_slot *slot)
705 kvmppc_core_flush_memslot(kvm, slot);
708 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
710 struct kvm_vcpu *vcpu;
711 vcpu = kvmppc_core_vcpu_create(kvm, id);
713 vcpu->arch.wqp = &vcpu->wq;
714 kvmppc_create_vcpu_debugfs(vcpu, id);
719 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
723 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
725 /* Make sure we're not using the vcpu anymore */
726 hrtimer_cancel(&vcpu->arch.dec_timer);
728 kvmppc_remove_vcpu_debugfs(vcpu);
730 switch (vcpu->arch.irq_type) {
731 case KVMPPC_IRQ_MPIC:
732 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
734 case KVMPPC_IRQ_XICS:
736 kvmppc_xive_cleanup_vcpu(vcpu);
738 kvmppc_xics_free_icp(vcpu);
742 kvmppc_core_vcpu_free(vcpu);
745 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
747 kvm_arch_vcpu_free(vcpu);
750 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
752 return kvmppc_core_pending_dec(vcpu);
755 static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
757 struct kvm_vcpu *vcpu;
759 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
760 kvmppc_decrementer_func(vcpu);
762 return HRTIMER_NORESTART;
765 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
769 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
770 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
771 vcpu->arch.dec_expires = get_tb();
773 #ifdef CONFIG_KVM_EXIT_TIMING
774 mutex_init(&vcpu->arch.exit_timing_lock);
776 ret = kvmppc_subarch_vcpu_init(vcpu);
780 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
782 kvmppc_mmu_destroy(vcpu);
783 kvmppc_subarch_vcpu_uninit(vcpu);
786 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
790 * vrsave (formerly usprg0) isn't used by Linux, but may
791 * be used by the guest.
793 * On non-booke this is associated with Altivec and
794 * is handled by code in book3s.c.
796 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
798 kvmppc_core_vcpu_load(vcpu, cpu);
801 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
803 kvmppc_core_vcpu_put(vcpu);
805 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
810 * irq_bypass_add_producer and irq_bypass_del_producer are only
811 * useful if the architecture supports PCI passthrough.
812 * irq_bypass_stop and irq_bypass_start are not needed and so
813 * kvm_ops are not defined for them.
815 bool kvm_arch_has_irq_bypass(void)
817 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
818 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
821 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
822 struct irq_bypass_producer *prod)
824 struct kvm_kernel_irqfd *irqfd =
825 container_of(cons, struct kvm_kernel_irqfd, consumer);
826 struct kvm *kvm = irqfd->kvm;
828 if (kvm->arch.kvm_ops->irq_bypass_add_producer)
829 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
834 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
835 struct irq_bypass_producer *prod)
837 struct kvm_kernel_irqfd *irqfd =
838 container_of(cons, struct kvm_kernel_irqfd, consumer);
839 struct kvm *kvm = irqfd->kvm;
841 if (kvm->arch.kvm_ops->irq_bypass_del_producer)
842 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
846 static inline int kvmppc_get_vsr_dword_offset(int index)
850 if ((index != 0) && (index != 1))
862 static inline int kvmppc_get_vsr_word_offset(int index)
866 if ((index > 3) || (index < 0))
877 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
880 union kvmppc_one_reg val;
881 int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
882 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
888 val.vval = VCPU_VSX_VR(vcpu, index - 32);
889 val.vsxval[offset] = gpr;
890 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
892 VCPU_VSX_FPR(vcpu, index, offset) = gpr;
896 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
899 union kvmppc_one_reg val;
900 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
903 val.vval = VCPU_VSX_VR(vcpu, index - 32);
906 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
908 VCPU_VSX_FPR(vcpu, index, 0) = gpr;
909 VCPU_VSX_FPR(vcpu, index, 1) = gpr;
913 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu,
916 union kvmppc_one_reg val;
917 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
920 val.vsx32val[0] = gpr;
921 val.vsx32val[1] = gpr;
922 val.vsx32val[2] = gpr;
923 val.vsx32val[3] = gpr;
924 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
926 val.vsx32val[0] = gpr;
927 val.vsx32val[1] = gpr;
928 VCPU_VSX_FPR(vcpu, index, 0) = val.vsxval[0];
929 VCPU_VSX_FPR(vcpu, index, 1) = val.vsxval[0];
933 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
936 union kvmppc_one_reg val;
937 int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
938 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
939 int dword_offset, word_offset;
945 val.vval = VCPU_VSX_VR(vcpu, index - 32);
946 val.vsx32val[offset] = gpr32;
947 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
949 dword_offset = offset / 2;
950 word_offset = offset % 2;
951 val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
952 val.vsx32val[word_offset] = gpr32;
953 VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
956 #endif /* CONFIG_VSX */
958 #ifdef CONFIG_ALTIVEC
959 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu,
960 int index, int element_size)
963 int elts = sizeof(vector128)/element_size;
965 if ((index < 0) || (index >= elts))
968 if (kvmppc_need_byteswap(vcpu))
969 offset = elts - index - 1;
976 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu,
979 return kvmppc_get_vmx_offset_generic(vcpu, index, 8);
982 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu,
985 return kvmppc_get_vmx_offset_generic(vcpu, index, 4);
988 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu,
991 return kvmppc_get_vmx_offset_generic(vcpu, index, 2);
994 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu,
997 return kvmppc_get_vmx_offset_generic(vcpu, index, 1);
1001 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
1004 union kvmppc_one_reg val;
1005 int offset = kvmppc_get_vmx_dword_offset(vcpu,
1006 vcpu->arch.mmio_vmx_offset);
1007 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1012 val.vval = VCPU_VSX_VR(vcpu, index);
1013 val.vsxval[offset] = gpr;
1014 VCPU_VSX_VR(vcpu, index) = val.vval;
1017 static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu,
1020 union kvmppc_one_reg val;
1021 int offset = kvmppc_get_vmx_word_offset(vcpu,
1022 vcpu->arch.mmio_vmx_offset);
1023 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1028 val.vval = VCPU_VSX_VR(vcpu, index);
1029 val.vsx32val[offset] = gpr32;
1030 VCPU_VSX_VR(vcpu, index) = val.vval;
1033 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu,
1036 union kvmppc_one_reg val;
1037 int offset = kvmppc_get_vmx_hword_offset(vcpu,
1038 vcpu->arch.mmio_vmx_offset);
1039 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1044 val.vval = VCPU_VSX_VR(vcpu, index);
1045 val.vsx16val[offset] = gpr16;
1046 VCPU_VSX_VR(vcpu, index) = val.vval;
1049 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu,
1052 union kvmppc_one_reg val;
1053 int offset = kvmppc_get_vmx_byte_offset(vcpu,
1054 vcpu->arch.mmio_vmx_offset);
1055 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1060 val.vval = VCPU_VSX_VR(vcpu, index);
1061 val.vsx8val[offset] = gpr8;
1062 VCPU_VSX_VR(vcpu, index) = val.vval;
1064 #endif /* CONFIG_ALTIVEC */
1066 #ifdef CONFIG_PPC_FPU
1067 static inline u64 sp_to_dp(u32 fprs)
1073 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
1079 static inline u32 dp_to_sp(u64 fprd)
1085 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
1092 #define sp_to_dp(x) (x)
1093 #define dp_to_sp(x) (x)
1094 #endif /* CONFIG_PPC_FPU */
1096 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
1097 struct kvm_run *run)
1099 u64 uninitialized_var(gpr);
1101 if (run->mmio.len > sizeof(gpr)) {
1102 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
1106 if (!vcpu->arch.mmio_host_swabbed) {
1107 switch (run->mmio.len) {
1108 case 8: gpr = *(u64 *)run->mmio.data; break;
1109 case 4: gpr = *(u32 *)run->mmio.data; break;
1110 case 2: gpr = *(u16 *)run->mmio.data; break;
1111 case 1: gpr = *(u8 *)run->mmio.data; break;
1114 switch (run->mmio.len) {
1115 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
1116 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
1117 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
1118 case 1: gpr = *(u8 *)run->mmio.data; break;
1122 /* conversion between single and double precision */
1123 if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
1124 gpr = sp_to_dp(gpr);
1126 if (vcpu->arch.mmio_sign_extend) {
1127 switch (run->mmio.len) {
1130 gpr = (s64)(s32)gpr;
1134 gpr = (s64)(s16)gpr;
1142 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
1143 case KVM_MMIO_REG_GPR:
1144 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
1146 case KVM_MMIO_REG_FPR:
1147 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1148 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP);
1150 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1152 #ifdef CONFIG_PPC_BOOK3S
1153 case KVM_MMIO_REG_QPR:
1154 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1156 case KVM_MMIO_REG_FQPR:
1157 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1158 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1162 case KVM_MMIO_REG_VSX:
1163 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1164 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX);
1166 if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD)
1167 kvmppc_set_vsr_dword(vcpu, gpr);
1168 else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD)
1169 kvmppc_set_vsr_word(vcpu, gpr);
1170 else if (vcpu->arch.mmio_copy_type ==
1171 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
1172 kvmppc_set_vsr_dword_dump(vcpu, gpr);
1173 else if (vcpu->arch.mmio_copy_type ==
1174 KVMPPC_VSX_COPY_WORD_LOAD_DUMP)
1175 kvmppc_set_vsr_word_dump(vcpu, gpr);
1178 #ifdef CONFIG_ALTIVEC
1179 case KVM_MMIO_REG_VMX:
1180 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1181 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC);
1183 if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD)
1184 kvmppc_set_vmx_dword(vcpu, gpr);
1185 else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD)
1186 kvmppc_set_vmx_word(vcpu, gpr);
1187 else if (vcpu->arch.mmio_copy_type ==
1188 KVMPPC_VMX_COPY_HWORD)
1189 kvmppc_set_vmx_hword(vcpu, gpr);
1190 else if (vcpu->arch.mmio_copy_type ==
1191 KVMPPC_VMX_COPY_BYTE)
1192 kvmppc_set_vmx_byte(vcpu, gpr);
1200 static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1201 unsigned int rt, unsigned int bytes,
1202 int is_default_endian, int sign_extend)
1207 /* Pity C doesn't have a logical XOR operator */
1208 if (kvmppc_need_byteswap(vcpu)) {
1209 host_swabbed = is_default_endian;
1211 host_swabbed = !is_default_endian;
1214 if (bytes > sizeof(run->mmio.data)) {
1215 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1219 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1220 run->mmio.len = bytes;
1221 run->mmio.is_write = 0;
1223 vcpu->arch.io_gpr = rt;
1224 vcpu->arch.mmio_host_swabbed = host_swabbed;
1225 vcpu->mmio_needed = 1;
1226 vcpu->mmio_is_write = 0;
1227 vcpu->arch.mmio_sign_extend = sign_extend;
1229 idx = srcu_read_lock(&vcpu->kvm->srcu);
1231 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1232 bytes, &run->mmio.data);
1234 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1237 kvmppc_complete_mmio_load(vcpu, run);
1238 vcpu->mmio_needed = 0;
1239 return EMULATE_DONE;
1242 return EMULATE_DO_MMIO;
1245 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1246 unsigned int rt, unsigned int bytes,
1247 int is_default_endian)
1249 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
1251 EXPORT_SYMBOL_GPL(kvmppc_handle_load);
1253 /* Same as above, but sign extends */
1254 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
1255 unsigned int rt, unsigned int bytes,
1256 int is_default_endian)
1258 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
1262 int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1263 unsigned int rt, unsigned int bytes,
1264 int is_default_endian, int mmio_sign_extend)
1266 enum emulation_result emulated = EMULATE_DONE;
1268 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1269 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1270 return EMULATE_FAIL;
1272 while (vcpu->arch.mmio_vsx_copy_nums) {
1273 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1274 is_default_endian, mmio_sign_extend);
1276 if (emulated != EMULATE_DONE)
1279 vcpu->arch.paddr_accessed += run->mmio.len;
1281 vcpu->arch.mmio_vsx_copy_nums--;
1282 vcpu->arch.mmio_vsx_offset++;
1286 #endif /* CONFIG_VSX */
1288 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1289 u64 val, unsigned int bytes, int is_default_endian)
1291 void *data = run->mmio.data;
1295 /* Pity C doesn't have a logical XOR operator */
1296 if (kvmppc_need_byteswap(vcpu)) {
1297 host_swabbed = is_default_endian;
1299 host_swabbed = !is_default_endian;
1302 if (bytes > sizeof(run->mmio.data)) {
1303 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1307 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1308 run->mmio.len = bytes;
1309 run->mmio.is_write = 1;
1310 vcpu->mmio_needed = 1;
1311 vcpu->mmio_is_write = 1;
1313 if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
1314 val = dp_to_sp(val);
1316 /* Store the value at the lowest bytes in 'data'. */
1317 if (!host_swabbed) {
1319 case 8: *(u64 *)data = val; break;
1320 case 4: *(u32 *)data = val; break;
1321 case 2: *(u16 *)data = val; break;
1322 case 1: *(u8 *)data = val; break;
1326 case 8: *(u64 *)data = swab64(val); break;
1327 case 4: *(u32 *)data = swab32(val); break;
1328 case 2: *(u16 *)data = swab16(val); break;
1329 case 1: *(u8 *)data = val; break;
1333 idx = srcu_read_lock(&vcpu->kvm->srcu);
1335 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1336 bytes, &run->mmio.data);
1338 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1341 vcpu->mmio_needed = 0;
1342 return EMULATE_DONE;
1345 return EMULATE_DO_MMIO;
1347 EXPORT_SYMBOL_GPL(kvmppc_handle_store);
1350 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
1352 u32 dword_offset, word_offset;
1353 union kvmppc_one_reg reg;
1355 int copy_type = vcpu->arch.mmio_copy_type;
1358 switch (copy_type) {
1359 case KVMPPC_VSX_COPY_DWORD:
1361 kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
1363 if (vsx_offset == -1) {
1369 *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
1371 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1372 *val = reg.vsxval[vsx_offset];
1376 case KVMPPC_VSX_COPY_WORD:
1378 kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
1380 if (vsx_offset == -1) {
1386 dword_offset = vsx_offset / 2;
1387 word_offset = vsx_offset % 2;
1388 reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
1389 *val = reg.vsx32val[word_offset];
1391 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1392 *val = reg.vsx32val[vsx_offset];
1404 int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1405 int rs, unsigned int bytes, int is_default_endian)
1408 enum emulation_result emulated = EMULATE_DONE;
1410 vcpu->arch.io_gpr = rs;
1412 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1413 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1414 return EMULATE_FAIL;
1416 while (vcpu->arch.mmio_vsx_copy_nums) {
1417 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
1418 return EMULATE_FAIL;
1420 emulated = kvmppc_handle_store(run, vcpu,
1421 val, bytes, is_default_endian);
1423 if (emulated != EMULATE_DONE)
1426 vcpu->arch.paddr_accessed += run->mmio.len;
1428 vcpu->arch.mmio_vsx_copy_nums--;
1429 vcpu->arch.mmio_vsx_offset++;
1435 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
1436 struct kvm_run *run)
1438 enum emulation_result emulated = EMULATE_FAIL;
1441 vcpu->arch.paddr_accessed += run->mmio.len;
1443 if (!vcpu->mmio_is_write) {
1444 emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
1445 run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
1447 emulated = kvmppc_handle_vsx_store(run, vcpu,
1448 vcpu->arch.io_gpr, run->mmio.len, 1);
1452 case EMULATE_DO_MMIO:
1453 run->exit_reason = KVM_EXIT_MMIO;
1457 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1458 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1459 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1468 #endif /* CONFIG_VSX */
1470 #ifdef CONFIG_ALTIVEC
1471 int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1472 unsigned int rt, unsigned int bytes, int is_default_endian)
1474 enum emulation_result emulated = EMULATE_DONE;
1476 if (vcpu->arch.mmio_vsx_copy_nums > 2)
1477 return EMULATE_FAIL;
1479 while (vcpu->arch.mmio_vmx_copy_nums) {
1480 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1481 is_default_endian, 0);
1483 if (emulated != EMULATE_DONE)
1486 vcpu->arch.paddr_accessed += run->mmio.len;
1487 vcpu->arch.mmio_vmx_copy_nums--;
1488 vcpu->arch.mmio_vmx_offset++;
1494 int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val)
1496 union kvmppc_one_reg reg;
1501 kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1503 if (vmx_offset == -1)
1506 reg.vval = VCPU_VSX_VR(vcpu, index);
1507 *val = reg.vsxval[vmx_offset];
1512 int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val)
1514 union kvmppc_one_reg reg;
1519 kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1521 if (vmx_offset == -1)
1524 reg.vval = VCPU_VSX_VR(vcpu, index);
1525 *val = reg.vsx32val[vmx_offset];
1530 int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val)
1532 union kvmppc_one_reg reg;
1537 kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1539 if (vmx_offset == -1)
1542 reg.vval = VCPU_VSX_VR(vcpu, index);
1543 *val = reg.vsx16val[vmx_offset];
1548 int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val)
1550 union kvmppc_one_reg reg;
1555 kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1557 if (vmx_offset == -1)
1560 reg.vval = VCPU_VSX_VR(vcpu, index);
1561 *val = reg.vsx8val[vmx_offset];
1566 int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1567 unsigned int rs, unsigned int bytes, int is_default_endian)
1570 unsigned int index = rs & KVM_MMIO_REG_MASK;
1571 enum emulation_result emulated = EMULATE_DONE;
1573 if (vcpu->arch.mmio_vsx_copy_nums > 2)
1574 return EMULATE_FAIL;
1576 vcpu->arch.io_gpr = rs;
1578 while (vcpu->arch.mmio_vmx_copy_nums) {
1579 switch (vcpu->arch.mmio_copy_type) {
1580 case KVMPPC_VMX_COPY_DWORD:
1581 if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1)
1582 return EMULATE_FAIL;
1585 case KVMPPC_VMX_COPY_WORD:
1586 if (kvmppc_get_vmx_word(vcpu, index, &val) == -1)
1587 return EMULATE_FAIL;
1589 case KVMPPC_VMX_COPY_HWORD:
1590 if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1)
1591 return EMULATE_FAIL;
1593 case KVMPPC_VMX_COPY_BYTE:
1594 if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1)
1595 return EMULATE_FAIL;
1598 return EMULATE_FAIL;
1601 emulated = kvmppc_handle_store(run, vcpu, val, bytes,
1603 if (emulated != EMULATE_DONE)
1606 vcpu->arch.paddr_accessed += run->mmio.len;
1607 vcpu->arch.mmio_vmx_copy_nums--;
1608 vcpu->arch.mmio_vmx_offset++;
1614 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu,
1615 struct kvm_run *run)
1617 enum emulation_result emulated = EMULATE_FAIL;
1620 vcpu->arch.paddr_accessed += run->mmio.len;
1622 if (!vcpu->mmio_is_write) {
1623 emulated = kvmppc_handle_vmx_load(run, vcpu,
1624 vcpu->arch.io_gpr, run->mmio.len, 1);
1626 emulated = kvmppc_handle_vmx_store(run, vcpu,
1627 vcpu->arch.io_gpr, run->mmio.len, 1);
1631 case EMULATE_DO_MMIO:
1632 run->exit_reason = KVM_EXIT_MMIO;
1636 pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
1637 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1638 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1647 #endif /* CONFIG_ALTIVEC */
1649 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1652 union kvmppc_one_reg val;
1655 size = one_reg_size(reg->id);
1656 if (size > sizeof(val))
1659 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1663 #ifdef CONFIG_ALTIVEC
1664 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1665 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1669 val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
1671 case KVM_REG_PPC_VSCR:
1672 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1676 val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
1678 case KVM_REG_PPC_VRSAVE:
1679 val = get_reg_val(reg->id, vcpu->arch.vrsave);
1681 #endif /* CONFIG_ALTIVEC */
1691 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1697 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1700 union kvmppc_one_reg val;
1703 size = one_reg_size(reg->id);
1704 if (size > sizeof(val))
1707 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1710 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1714 #ifdef CONFIG_ALTIVEC
1715 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1716 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1720 vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
1722 case KVM_REG_PPC_VSCR:
1723 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1727 vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
1729 case KVM_REG_PPC_VRSAVE:
1730 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1734 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1736 #endif /* CONFIG_ALTIVEC */
1746 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
1752 if (vcpu->mmio_needed) {
1753 vcpu->mmio_needed = 0;
1754 if (!vcpu->mmio_is_write)
1755 kvmppc_complete_mmio_load(vcpu, run);
1757 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1758 vcpu->arch.mmio_vsx_copy_nums--;
1759 vcpu->arch.mmio_vsx_offset++;
1762 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1763 r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
1764 if (r == RESUME_HOST) {
1765 vcpu->mmio_needed = 1;
1770 #ifdef CONFIG_ALTIVEC
1771 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1772 vcpu->arch.mmio_vmx_copy_nums--;
1773 vcpu->arch.mmio_vmx_offset++;
1776 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1777 r = kvmppc_emulate_mmio_vmx_loadstore(vcpu, run);
1778 if (r == RESUME_HOST) {
1779 vcpu->mmio_needed = 1;
1784 } else if (vcpu->arch.osi_needed) {
1785 u64 *gprs = run->osi.gprs;
1788 for (i = 0; i < 32; i++)
1789 kvmppc_set_gpr(vcpu, i, gprs[i]);
1790 vcpu->arch.osi_needed = 0;
1791 } else if (vcpu->arch.hcall_needed) {
1794 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1795 for (i = 0; i < 9; ++i)
1796 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1797 vcpu->arch.hcall_needed = 0;
1799 } else if (vcpu->arch.epr_needed) {
1800 kvmppc_set_epr(vcpu, run->epr.epr);
1801 vcpu->arch.epr_needed = 0;
1805 kvm_sigset_activate(vcpu);
1807 if (run->immediate_exit)
1810 r = kvmppc_vcpu_run(run, vcpu);
1812 kvm_sigset_deactivate(vcpu);
1814 #ifdef CONFIG_ALTIVEC
1821 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1823 if (irq->irq == KVM_INTERRUPT_UNSET) {
1824 kvmppc_core_dequeue_external(vcpu);
1828 kvmppc_core_queue_external(vcpu, irq);
1830 kvm_vcpu_kick(vcpu);
1835 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1836 struct kvm_enable_cap *cap)
1844 case KVM_CAP_PPC_OSI:
1846 vcpu->arch.osi_enabled = true;
1848 case KVM_CAP_PPC_PAPR:
1850 vcpu->arch.papr_enabled = true;
1852 case KVM_CAP_PPC_EPR:
1855 vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1857 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
1860 case KVM_CAP_PPC_BOOKE_WATCHDOG:
1862 vcpu->arch.watchdog_enabled = true;
1865 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1866 case KVM_CAP_SW_TLB: {
1867 struct kvm_config_tlb cfg;
1868 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1871 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1874 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1878 #ifdef CONFIG_KVM_MPIC
1879 case KVM_CAP_IRQ_MPIC: {
1881 struct kvm_device *dev;
1884 f = fdget(cap->args[0]);
1889 dev = kvm_device_from_filp(f.file);
1891 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1897 #ifdef CONFIG_KVM_XICS
1898 case KVM_CAP_IRQ_XICS: {
1900 struct kvm_device *dev;
1903 f = fdget(cap->args[0]);
1908 dev = kvm_device_from_filp(f.file);
1911 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
1913 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1919 #endif /* CONFIG_KVM_XICS */
1920 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1921 case KVM_CAP_PPC_FWNMI:
1923 if (!is_kvmppc_hv_enabled(vcpu->kvm))
1926 vcpu->kvm->arch.fwnmi_enabled = true;
1928 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1935 r = kvmppc_sanity_check(vcpu);
1940 bool kvm_arch_intc_initialized(struct kvm *kvm)
1942 #ifdef CONFIG_KVM_MPIC
1946 #ifdef CONFIG_KVM_XICS
1947 if (kvm->arch.xics || kvm->arch.xive)
1953 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1954 struct kvm_mp_state *mp_state)
1959 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1960 struct kvm_mp_state *mp_state)
1965 long kvm_arch_vcpu_async_ioctl(struct file *filp,
1966 unsigned int ioctl, unsigned long arg)
1968 struct kvm_vcpu *vcpu = filp->private_data;
1969 void __user *argp = (void __user *)arg;
1971 if (ioctl == KVM_INTERRUPT) {
1972 struct kvm_interrupt irq;
1973 if (copy_from_user(&irq, argp, sizeof(irq)))
1975 return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1977 return -ENOIOCTLCMD;
1980 long kvm_arch_vcpu_ioctl(struct file *filp,
1981 unsigned int ioctl, unsigned long arg)
1983 struct kvm_vcpu *vcpu = filp->private_data;
1984 void __user *argp = (void __user *)arg;
1988 case KVM_ENABLE_CAP:
1990 struct kvm_enable_cap cap;
1993 if (copy_from_user(&cap, argp, sizeof(cap)))
1995 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2000 case KVM_SET_ONE_REG:
2001 case KVM_GET_ONE_REG:
2003 struct kvm_one_reg reg;
2005 if (copy_from_user(®, argp, sizeof(reg)))
2007 if (ioctl == KVM_SET_ONE_REG)
2008 r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®);
2010 r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®);
2014 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
2015 case KVM_DIRTY_TLB: {
2016 struct kvm_dirty_tlb dirty;
2019 if (copy_from_user(&dirty, argp, sizeof(dirty)))
2021 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
2034 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2036 return VM_FAULT_SIGBUS;
2039 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
2041 u32 inst_nop = 0x60000000;
2042 #ifdef CONFIG_KVM_BOOKE_HV
2043 u32 inst_sc1 = 0x44000022;
2044 pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
2045 pvinfo->hcall[1] = cpu_to_be32(inst_nop);
2046 pvinfo->hcall[2] = cpu_to_be32(inst_nop);
2047 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2049 u32 inst_lis = 0x3c000000;
2050 u32 inst_ori = 0x60000000;
2051 u32 inst_sc = 0x44000002;
2052 u32 inst_imm_mask = 0xffff;
2055 * The hypercall to get into KVM from within guest context is as
2058 * lis r0, r0, KVM_SC_MAGIC_R0@h
2059 * ori r0, KVM_SC_MAGIC_R0@l
2063 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
2064 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
2065 pvinfo->hcall[2] = cpu_to_be32(inst_sc);
2066 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2069 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
2074 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
2077 if (!irqchip_in_kernel(kvm))
2080 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2081 irq_event->irq, irq_event->level,
2087 static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
2088 struct kvm_enable_cap *cap)
2096 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2097 case KVM_CAP_PPC_ENABLE_HCALL: {
2098 unsigned long hcall = cap->args[0];
2101 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
2104 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
2107 set_bit(hcall / 4, kvm->arch.enabled_hcalls);
2109 clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
2113 case KVM_CAP_PPC_SMT: {
2114 unsigned long mode = cap->args[0];
2115 unsigned long flags = cap->args[1];
2118 if (kvm->arch.kvm_ops->set_smt_mode)
2119 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
2123 case KVM_CAP_PPC_NESTED_HV:
2125 if (!is_kvmppc_hv_enabled(kvm) ||
2126 !kvm->arch.kvm_ops->enable_nested)
2128 r = kvm->arch.kvm_ops->enable_nested(kvm);
2139 #ifdef CONFIG_PPC_BOOK3S_64
2141 * These functions check whether the underlying hardware is safe
2142 * against attacks based on observing the effects of speculatively
2143 * executed instructions, and whether it supplies instructions for
2144 * use in workarounds. The information comes from firmware, either
2145 * via the device tree on powernv platforms or from an hcall on
2146 * pseries platforms.
2148 #ifdef CONFIG_PPC_PSERIES
2149 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2151 struct h_cpu_char_result c;
2154 if (!machine_is(pseries))
2157 rc = plpar_get_cpu_characteristics(&c);
2158 if (rc == H_SUCCESS) {
2159 cp->character = c.character;
2160 cp->behaviour = c.behaviour;
2161 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2162 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2163 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2164 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2165 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2166 KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
2167 KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
2168 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2169 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2170 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2171 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2176 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2182 static inline bool have_fw_feat(struct device_node *fw_features,
2183 const char *state, const char *name)
2185 struct device_node *np;
2188 np = of_get_child_by_name(fw_features, name);
2190 r = of_property_read_bool(np, state);
2196 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2198 struct device_node *np, *fw_features;
2201 memset(cp, 0, sizeof(*cp));
2202 r = pseries_get_cpu_char(cp);
2206 np = of_find_node_by_name(NULL, "ibm,opal");
2208 fw_features = of_get_child_by_name(np, "fw-features");
2212 if (have_fw_feat(fw_features, "enabled",
2213 "inst-spec-barrier-ori31,31,0"))
2214 cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
2215 if (have_fw_feat(fw_features, "enabled",
2216 "fw-bcctrl-serialized"))
2217 cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
2218 if (have_fw_feat(fw_features, "enabled",
2219 "inst-l1d-flush-ori30,30,0"))
2220 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
2221 if (have_fw_feat(fw_features, "enabled",
2222 "inst-l1d-flush-trig2"))
2223 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
2224 if (have_fw_feat(fw_features, "enabled",
2225 "fw-l1d-thread-split"))
2226 cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
2227 if (have_fw_feat(fw_features, "enabled",
2228 "fw-count-cache-disabled"))
2229 cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2230 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2231 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2232 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2233 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2234 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2235 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2237 if (have_fw_feat(fw_features, "enabled",
2238 "speculation-policy-favor-security"))
2239 cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
2240 if (!have_fw_feat(fw_features, "disabled",
2241 "needs-l1d-flush-msr-pr-0-to-1"))
2242 cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
2243 if (!have_fw_feat(fw_features, "disabled",
2244 "needs-spec-barrier-for-bound-checks"))
2245 cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2246 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2247 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2248 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2250 of_node_put(fw_features);
2257 long kvm_arch_vm_ioctl(struct file *filp,
2258 unsigned int ioctl, unsigned long arg)
2260 struct kvm *kvm __maybe_unused = filp->private_data;
2261 void __user *argp = (void __user *)arg;
2265 case KVM_PPC_GET_PVINFO: {
2266 struct kvm_ppc_pvinfo pvinfo;
2267 memset(&pvinfo, 0, sizeof(pvinfo));
2268 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
2269 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
2276 case KVM_ENABLE_CAP:
2278 struct kvm_enable_cap cap;
2280 if (copy_from_user(&cap, argp, sizeof(cap)))
2282 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
2285 #ifdef CONFIG_SPAPR_TCE_IOMMU
2286 case KVM_CREATE_SPAPR_TCE_64: {
2287 struct kvm_create_spapr_tce_64 create_tce_64;
2290 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
2292 if (create_tce_64.flags) {
2296 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2299 case KVM_CREATE_SPAPR_TCE: {
2300 struct kvm_create_spapr_tce create_tce;
2301 struct kvm_create_spapr_tce_64 create_tce_64;
2304 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
2307 create_tce_64.liobn = create_tce.liobn;
2308 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
2309 create_tce_64.offset = 0;
2310 create_tce_64.size = create_tce.window_size >>
2311 IOMMU_PAGE_SHIFT_4K;
2312 create_tce_64.flags = 0;
2313 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2317 #ifdef CONFIG_PPC_BOOK3S_64
2318 case KVM_PPC_GET_SMMU_INFO: {
2319 struct kvm_ppc_smmu_info info;
2320 struct kvm *kvm = filp->private_data;
2322 memset(&info, 0, sizeof(info));
2323 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
2324 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2328 case KVM_PPC_RTAS_DEFINE_TOKEN: {
2329 struct kvm *kvm = filp->private_data;
2331 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
2334 case KVM_PPC_CONFIGURE_V3_MMU: {
2335 struct kvm *kvm = filp->private_data;
2336 struct kvm_ppc_mmuv3_cfg cfg;
2339 if (!kvm->arch.kvm_ops->configure_mmu)
2342 if (copy_from_user(&cfg, argp, sizeof(cfg)))
2344 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
2347 case KVM_PPC_GET_RMMU_INFO: {
2348 struct kvm *kvm = filp->private_data;
2349 struct kvm_ppc_rmmu_info info;
2352 if (!kvm->arch.kvm_ops->get_rmmu_info)
2354 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
2355 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2359 case KVM_PPC_GET_CPU_CHAR: {
2360 struct kvm_ppc_cpu_char cpuchar;
2362 r = kvmppc_get_cpu_char(&cpuchar);
2363 if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
2368 struct kvm *kvm = filp->private_data;
2369 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
2371 #else /* CONFIG_PPC_BOOK3S_64 */
2380 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
2381 static unsigned long nr_lpids;
2383 long kvmppc_alloc_lpid(void)
2388 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
2389 if (lpid >= nr_lpids) {
2390 pr_err("%s: No LPIDs free\n", __func__);
2393 } while (test_and_set_bit(lpid, lpid_inuse));
2397 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
2399 void kvmppc_claim_lpid(long lpid)
2401 set_bit(lpid, lpid_inuse);
2403 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
2405 void kvmppc_free_lpid(long lpid)
2407 clear_bit(lpid, lpid_inuse);
2409 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
2411 void kvmppc_init_lpid(unsigned long nr_lpids_param)
2413 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
2414 memset(lpid_inuse, 0, sizeof(lpid_inuse));
2416 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
2418 int kvm_arch_init(void *opaque)
2423 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);