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
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 if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->store_to_eaddr)
339 r = vcpu->kvm->arch.kvm_ops->store_to_eaddr(vcpu, eaddr, ptr,
342 if ((!r) || (r == -EAGAIN))
345 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
355 /* Magic page override */
356 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
357 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
358 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
359 void *magic = vcpu->arch.shared;
360 magic += pte.eaddr & 0xfff;
361 memcpy(magic, ptr, size);
365 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
366 return EMULATE_DO_MMIO;
370 EXPORT_SYMBOL_GPL(kvmppc_st);
372 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
375 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
376 struct kvmppc_pte pte;
381 if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->load_from_eaddr)
382 rc = vcpu->kvm->arch.kvm_ops->load_from_eaddr(vcpu, eaddr, ptr,
385 if ((!rc) || (rc == -EAGAIN))
388 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
398 if (!data && !pte.may_execute)
401 /* Magic page override */
402 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
403 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
404 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
405 void *magic = vcpu->arch.shared;
406 magic += pte.eaddr & 0xfff;
407 memcpy(ptr, magic, size);
411 if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
412 return EMULATE_DO_MMIO;
416 EXPORT_SYMBOL_GPL(kvmppc_ld);
418 int kvm_arch_hardware_enable(void)
423 int kvm_arch_hardware_setup(void)
428 void kvm_arch_check_processor_compat(void *rtn)
430 *(int *)rtn = kvmppc_core_check_processor_compat();
433 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
435 struct kvmppc_ops *kvm_ops = NULL;
437 * if we have both HV and PR enabled, default is HV
441 kvm_ops = kvmppc_hv_ops;
443 kvm_ops = kvmppc_pr_ops;
446 } else if (type == KVM_VM_PPC_HV) {
449 kvm_ops = kvmppc_hv_ops;
450 } else if (type == KVM_VM_PPC_PR) {
453 kvm_ops = kvmppc_pr_ops;
457 if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
460 kvm->arch.kvm_ops = kvm_ops;
461 return kvmppc_core_init_vm(kvm);
466 bool kvm_arch_has_vcpu_debugfs(void)
471 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
476 void kvm_arch_destroy_vm(struct kvm *kvm)
479 struct kvm_vcpu *vcpu;
481 #ifdef CONFIG_KVM_XICS
483 * We call kick_all_cpus_sync() to ensure that all
484 * CPUs have executed any pending IPIs before we
485 * continue and free VCPUs structures below.
487 if (is_kvmppc_hv_enabled(kvm))
488 kick_all_cpus_sync();
491 kvm_for_each_vcpu(i, vcpu, kvm)
492 kvm_arch_vcpu_free(vcpu);
494 mutex_lock(&kvm->lock);
495 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
496 kvm->vcpus[i] = NULL;
498 atomic_set(&kvm->online_vcpus, 0);
500 kvmppc_core_destroy_vm(kvm);
502 mutex_unlock(&kvm->lock);
504 /* drop the module reference */
505 module_put(kvm->arch.kvm_ops->owner);
508 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
511 /* Assume we're using HV mode when the HV module is loaded */
512 int hv_enabled = kvmppc_hv_ops ? 1 : 0;
516 * Hooray - we know which VM type we're running on. Depend on
517 * that rather than the guess above.
519 hv_enabled = is_kvmppc_hv_enabled(kvm);
524 case KVM_CAP_PPC_BOOKE_SREGS:
525 case KVM_CAP_PPC_BOOKE_WATCHDOG:
526 case KVM_CAP_PPC_EPR:
528 case KVM_CAP_PPC_SEGSTATE:
529 case KVM_CAP_PPC_HIOR:
530 case KVM_CAP_PPC_PAPR:
532 case KVM_CAP_PPC_UNSET_IRQ:
533 case KVM_CAP_PPC_IRQ_LEVEL:
534 case KVM_CAP_ENABLE_CAP:
535 case KVM_CAP_ONE_REG:
536 case KVM_CAP_IOEVENTFD:
537 case KVM_CAP_DEVICE_CTRL:
538 case KVM_CAP_IMMEDIATE_EXIT:
541 case KVM_CAP_PPC_PAIRED_SINGLES:
542 case KVM_CAP_PPC_OSI:
543 case KVM_CAP_PPC_GET_PVINFO:
544 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
547 /* We support this only for PR */
550 #ifdef CONFIG_KVM_MPIC
551 case KVM_CAP_IRQ_MPIC:
556 #ifdef CONFIG_PPC_BOOK3S_64
557 case KVM_CAP_SPAPR_TCE:
558 case KVM_CAP_SPAPR_TCE_64:
561 case KVM_CAP_SPAPR_TCE_VFIO:
562 r = !!cpu_has_feature(CPU_FTR_HVMODE);
564 case KVM_CAP_PPC_RTAS:
565 case KVM_CAP_PPC_FIXUP_HCALL:
566 case KVM_CAP_PPC_ENABLE_HCALL:
567 #ifdef CONFIG_KVM_XICS
568 case KVM_CAP_IRQ_XICS:
570 case KVM_CAP_PPC_GET_CPU_CHAR:
573 #ifdef CONFIG_KVM_XIVE
574 case KVM_CAP_PPC_IRQ_XIVE:
576 * We need XIVE to be enabled on the platform (implies
577 * a POWER9 processor) and the PowerNV platform, as
578 * nested is not yet supported.
580 r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE);
584 case KVM_CAP_PPC_ALLOC_HTAB:
587 #endif /* CONFIG_PPC_BOOK3S_64 */
588 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
589 case KVM_CAP_PPC_SMT:
592 if (kvm->arch.emul_smt_mode > 1)
593 r = kvm->arch.emul_smt_mode;
595 r = kvm->arch.smt_mode;
596 } else if (hv_enabled) {
597 if (cpu_has_feature(CPU_FTR_ARCH_300))
600 r = threads_per_subcore;
603 case KVM_CAP_PPC_SMT_POSSIBLE:
606 if (!cpu_has_feature(CPU_FTR_ARCH_300))
607 r = ((threads_per_subcore << 1) - 1);
609 /* P9 can emulate dbells, so allow any mode */
613 case KVM_CAP_PPC_RMA:
616 case KVM_CAP_PPC_HWRNG:
617 r = kvmppc_hwrng_present();
619 case KVM_CAP_PPC_MMU_RADIX:
620 r = !!(hv_enabled && radix_enabled());
622 case KVM_CAP_PPC_MMU_HASH_V3:
623 r = !!(hv_enabled && cpu_has_feature(CPU_FTR_ARCH_300) &&
624 cpu_has_feature(CPU_FTR_HVMODE));
626 case KVM_CAP_PPC_NESTED_HV:
627 r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
628 !kvmppc_hv_ops->enable_nested(NULL));
631 case KVM_CAP_SYNC_MMU:
632 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
634 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
640 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
641 case KVM_CAP_PPC_HTAB_FD:
645 case KVM_CAP_NR_VCPUS:
647 * Recommending a number of CPUs is somewhat arbitrary; we
648 * return the number of present CPUs for -HV (since a host
649 * will have secondary threads "offline"), and for other KVM
650 * implementations just count online CPUs.
653 r = num_present_cpus();
655 r = num_online_cpus();
657 case KVM_CAP_MAX_VCPUS:
660 case KVM_CAP_MAX_VCPU_ID:
663 #ifdef CONFIG_PPC_BOOK3S_64
664 case KVM_CAP_PPC_GET_SMMU_INFO:
667 case KVM_CAP_SPAPR_MULTITCE:
670 case KVM_CAP_SPAPR_RESIZE_HPT:
674 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
675 case KVM_CAP_PPC_FWNMI:
679 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
680 case KVM_CAP_PPC_HTM:
681 r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) ||
682 (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST));
693 long kvm_arch_dev_ioctl(struct file *filp,
694 unsigned int ioctl, unsigned long arg)
699 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
700 struct kvm_memory_slot *dont)
702 kvmppc_core_free_memslot(kvm, free, dont);
705 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
706 unsigned long npages)
708 return kvmppc_core_create_memslot(kvm, slot, npages);
711 int kvm_arch_prepare_memory_region(struct kvm *kvm,
712 struct kvm_memory_slot *memslot,
713 const struct kvm_userspace_memory_region *mem,
714 enum kvm_mr_change change)
716 return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
719 void kvm_arch_commit_memory_region(struct kvm *kvm,
720 const struct kvm_userspace_memory_region *mem,
721 const struct kvm_memory_slot *old,
722 const struct kvm_memory_slot *new,
723 enum kvm_mr_change change)
725 kvmppc_core_commit_memory_region(kvm, mem, old, new, change);
728 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
729 struct kvm_memory_slot *slot)
731 kvmppc_core_flush_memslot(kvm, slot);
734 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
736 struct kvm_vcpu *vcpu;
737 vcpu = kvmppc_core_vcpu_create(kvm, id);
739 vcpu->arch.wqp = &vcpu->wq;
740 kvmppc_create_vcpu_debugfs(vcpu, id);
745 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
749 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
751 /* Make sure we're not using the vcpu anymore */
752 hrtimer_cancel(&vcpu->arch.dec_timer);
754 kvmppc_remove_vcpu_debugfs(vcpu);
756 switch (vcpu->arch.irq_type) {
757 case KVMPPC_IRQ_MPIC:
758 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
760 case KVMPPC_IRQ_XICS:
762 kvmppc_xive_cleanup_vcpu(vcpu);
764 kvmppc_xics_free_icp(vcpu);
766 case KVMPPC_IRQ_XIVE:
767 kvmppc_xive_native_cleanup_vcpu(vcpu);
771 kvmppc_core_vcpu_free(vcpu);
774 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
776 kvm_arch_vcpu_free(vcpu);
779 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
781 return kvmppc_core_pending_dec(vcpu);
784 static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
786 struct kvm_vcpu *vcpu;
788 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
789 kvmppc_decrementer_func(vcpu);
791 return HRTIMER_NORESTART;
794 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
798 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
799 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
800 vcpu->arch.dec_expires = get_tb();
802 #ifdef CONFIG_KVM_EXIT_TIMING
803 mutex_init(&vcpu->arch.exit_timing_lock);
805 ret = kvmppc_subarch_vcpu_init(vcpu);
809 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
811 kvmppc_mmu_destroy(vcpu);
812 kvmppc_subarch_vcpu_uninit(vcpu);
815 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
819 * vrsave (formerly usprg0) isn't used by Linux, but may
820 * be used by the guest.
822 * On non-booke this is associated with Altivec and
823 * is handled by code in book3s.c.
825 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
827 kvmppc_core_vcpu_load(vcpu, cpu);
830 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
832 kvmppc_core_vcpu_put(vcpu);
834 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
839 * irq_bypass_add_producer and irq_bypass_del_producer are only
840 * useful if the architecture supports PCI passthrough.
841 * irq_bypass_stop and irq_bypass_start are not needed and so
842 * kvm_ops are not defined for them.
844 bool kvm_arch_has_irq_bypass(void)
846 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
847 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
850 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
851 struct irq_bypass_producer *prod)
853 struct kvm_kernel_irqfd *irqfd =
854 container_of(cons, struct kvm_kernel_irqfd, consumer);
855 struct kvm *kvm = irqfd->kvm;
857 if (kvm->arch.kvm_ops->irq_bypass_add_producer)
858 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
863 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
864 struct irq_bypass_producer *prod)
866 struct kvm_kernel_irqfd *irqfd =
867 container_of(cons, struct kvm_kernel_irqfd, consumer);
868 struct kvm *kvm = irqfd->kvm;
870 if (kvm->arch.kvm_ops->irq_bypass_del_producer)
871 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
875 static inline int kvmppc_get_vsr_dword_offset(int index)
879 if ((index != 0) && (index != 1))
891 static inline int kvmppc_get_vsr_word_offset(int index)
895 if ((index > 3) || (index < 0))
906 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
909 union kvmppc_one_reg val;
910 int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
911 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
917 val.vval = VCPU_VSX_VR(vcpu, index - 32);
918 val.vsxval[offset] = gpr;
919 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
921 VCPU_VSX_FPR(vcpu, index, offset) = gpr;
925 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
928 union kvmppc_one_reg val;
929 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
932 val.vval = VCPU_VSX_VR(vcpu, index - 32);
935 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
937 VCPU_VSX_FPR(vcpu, index, 0) = gpr;
938 VCPU_VSX_FPR(vcpu, index, 1) = gpr;
942 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu,
945 union kvmppc_one_reg val;
946 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
949 val.vsx32val[0] = gpr;
950 val.vsx32val[1] = gpr;
951 val.vsx32val[2] = gpr;
952 val.vsx32val[3] = gpr;
953 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
955 val.vsx32val[0] = gpr;
956 val.vsx32val[1] = gpr;
957 VCPU_VSX_FPR(vcpu, index, 0) = val.vsxval[0];
958 VCPU_VSX_FPR(vcpu, index, 1) = val.vsxval[0];
962 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
965 union kvmppc_one_reg val;
966 int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
967 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
968 int dword_offset, word_offset;
974 val.vval = VCPU_VSX_VR(vcpu, index - 32);
975 val.vsx32val[offset] = gpr32;
976 VCPU_VSX_VR(vcpu, index - 32) = val.vval;
978 dword_offset = offset / 2;
979 word_offset = offset % 2;
980 val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
981 val.vsx32val[word_offset] = gpr32;
982 VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
985 #endif /* CONFIG_VSX */
987 #ifdef CONFIG_ALTIVEC
988 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu,
989 int index, int element_size)
992 int elts = sizeof(vector128)/element_size;
994 if ((index < 0) || (index >= elts))
997 if (kvmppc_need_byteswap(vcpu))
998 offset = elts - index - 1;
1005 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu,
1008 return kvmppc_get_vmx_offset_generic(vcpu, index, 8);
1011 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu,
1014 return kvmppc_get_vmx_offset_generic(vcpu, index, 4);
1017 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu,
1020 return kvmppc_get_vmx_offset_generic(vcpu, index, 2);
1023 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu,
1026 return kvmppc_get_vmx_offset_generic(vcpu, index, 1);
1030 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
1033 union kvmppc_one_reg val;
1034 int offset = kvmppc_get_vmx_dword_offset(vcpu,
1035 vcpu->arch.mmio_vmx_offset);
1036 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1041 val.vval = VCPU_VSX_VR(vcpu, index);
1042 val.vsxval[offset] = gpr;
1043 VCPU_VSX_VR(vcpu, index) = val.vval;
1046 static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu,
1049 union kvmppc_one_reg val;
1050 int offset = kvmppc_get_vmx_word_offset(vcpu,
1051 vcpu->arch.mmio_vmx_offset);
1052 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1057 val.vval = VCPU_VSX_VR(vcpu, index);
1058 val.vsx32val[offset] = gpr32;
1059 VCPU_VSX_VR(vcpu, index) = val.vval;
1062 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu,
1065 union kvmppc_one_reg val;
1066 int offset = kvmppc_get_vmx_hword_offset(vcpu,
1067 vcpu->arch.mmio_vmx_offset);
1068 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1073 val.vval = VCPU_VSX_VR(vcpu, index);
1074 val.vsx16val[offset] = gpr16;
1075 VCPU_VSX_VR(vcpu, index) = val.vval;
1078 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu,
1081 union kvmppc_one_reg val;
1082 int offset = kvmppc_get_vmx_byte_offset(vcpu,
1083 vcpu->arch.mmio_vmx_offset);
1084 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
1089 val.vval = VCPU_VSX_VR(vcpu, index);
1090 val.vsx8val[offset] = gpr8;
1091 VCPU_VSX_VR(vcpu, index) = val.vval;
1093 #endif /* CONFIG_ALTIVEC */
1095 #ifdef CONFIG_PPC_FPU
1096 static inline u64 sp_to_dp(u32 fprs)
1102 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
1108 static inline u32 dp_to_sp(u64 fprd)
1114 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
1121 #define sp_to_dp(x) (x)
1122 #define dp_to_sp(x) (x)
1123 #endif /* CONFIG_PPC_FPU */
1125 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
1126 struct kvm_run *run)
1128 u64 uninitialized_var(gpr);
1130 if (run->mmio.len > sizeof(gpr)) {
1131 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
1135 if (!vcpu->arch.mmio_host_swabbed) {
1136 switch (run->mmio.len) {
1137 case 8: gpr = *(u64 *)run->mmio.data; break;
1138 case 4: gpr = *(u32 *)run->mmio.data; break;
1139 case 2: gpr = *(u16 *)run->mmio.data; break;
1140 case 1: gpr = *(u8 *)run->mmio.data; break;
1143 switch (run->mmio.len) {
1144 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
1145 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
1146 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
1147 case 1: gpr = *(u8 *)run->mmio.data; break;
1151 /* conversion between single and double precision */
1152 if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
1153 gpr = sp_to_dp(gpr);
1155 if (vcpu->arch.mmio_sign_extend) {
1156 switch (run->mmio.len) {
1159 gpr = (s64)(s32)gpr;
1163 gpr = (s64)(s16)gpr;
1171 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
1172 case KVM_MMIO_REG_GPR:
1173 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
1175 case KVM_MMIO_REG_FPR:
1176 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1177 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP);
1179 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1181 #ifdef CONFIG_PPC_BOOK3S
1182 case KVM_MMIO_REG_QPR:
1183 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1185 case KVM_MMIO_REG_FQPR:
1186 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1187 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1191 case KVM_MMIO_REG_VSX:
1192 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1193 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX);
1195 if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD)
1196 kvmppc_set_vsr_dword(vcpu, gpr);
1197 else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD)
1198 kvmppc_set_vsr_word(vcpu, gpr);
1199 else if (vcpu->arch.mmio_copy_type ==
1200 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
1201 kvmppc_set_vsr_dword_dump(vcpu, gpr);
1202 else if (vcpu->arch.mmio_copy_type ==
1203 KVMPPC_VSX_COPY_WORD_LOAD_DUMP)
1204 kvmppc_set_vsr_word_dump(vcpu, gpr);
1207 #ifdef CONFIG_ALTIVEC
1208 case KVM_MMIO_REG_VMX:
1209 if (vcpu->kvm->arch.kvm_ops->giveup_ext)
1210 vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC);
1212 if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD)
1213 kvmppc_set_vmx_dword(vcpu, gpr);
1214 else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD)
1215 kvmppc_set_vmx_word(vcpu, gpr);
1216 else if (vcpu->arch.mmio_copy_type ==
1217 KVMPPC_VMX_COPY_HWORD)
1218 kvmppc_set_vmx_hword(vcpu, gpr);
1219 else if (vcpu->arch.mmio_copy_type ==
1220 KVMPPC_VMX_COPY_BYTE)
1221 kvmppc_set_vmx_byte(vcpu, gpr);
1224 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1225 case KVM_MMIO_REG_NESTED_GPR:
1226 if (kvmppc_need_byteswap(vcpu))
1228 kvm_vcpu_write_guest(vcpu, vcpu->arch.nested_io_gpr, &gpr,
1237 static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1238 unsigned int rt, unsigned int bytes,
1239 int is_default_endian, int sign_extend)
1244 /* Pity C doesn't have a logical XOR operator */
1245 if (kvmppc_need_byteswap(vcpu)) {
1246 host_swabbed = is_default_endian;
1248 host_swabbed = !is_default_endian;
1251 if (bytes > sizeof(run->mmio.data)) {
1252 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1256 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1257 run->mmio.len = bytes;
1258 run->mmio.is_write = 0;
1260 vcpu->arch.io_gpr = rt;
1261 vcpu->arch.mmio_host_swabbed = host_swabbed;
1262 vcpu->mmio_needed = 1;
1263 vcpu->mmio_is_write = 0;
1264 vcpu->arch.mmio_sign_extend = sign_extend;
1266 idx = srcu_read_lock(&vcpu->kvm->srcu);
1268 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1269 bytes, &run->mmio.data);
1271 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1274 kvmppc_complete_mmio_load(vcpu, run);
1275 vcpu->mmio_needed = 0;
1276 return EMULATE_DONE;
1279 return EMULATE_DO_MMIO;
1282 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1283 unsigned int rt, unsigned int bytes,
1284 int is_default_endian)
1286 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
1288 EXPORT_SYMBOL_GPL(kvmppc_handle_load);
1290 /* Same as above, but sign extends */
1291 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
1292 unsigned int rt, unsigned int bytes,
1293 int is_default_endian)
1295 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
1299 int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1300 unsigned int rt, unsigned int bytes,
1301 int is_default_endian, int mmio_sign_extend)
1303 enum emulation_result emulated = EMULATE_DONE;
1305 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1306 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1307 return EMULATE_FAIL;
1309 while (vcpu->arch.mmio_vsx_copy_nums) {
1310 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1311 is_default_endian, mmio_sign_extend);
1313 if (emulated != EMULATE_DONE)
1316 vcpu->arch.paddr_accessed += run->mmio.len;
1318 vcpu->arch.mmio_vsx_copy_nums--;
1319 vcpu->arch.mmio_vsx_offset++;
1323 #endif /* CONFIG_VSX */
1325 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1326 u64 val, unsigned int bytes, int is_default_endian)
1328 void *data = run->mmio.data;
1332 /* Pity C doesn't have a logical XOR operator */
1333 if (kvmppc_need_byteswap(vcpu)) {
1334 host_swabbed = is_default_endian;
1336 host_swabbed = !is_default_endian;
1339 if (bytes > sizeof(run->mmio.data)) {
1340 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1344 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1345 run->mmio.len = bytes;
1346 run->mmio.is_write = 1;
1347 vcpu->mmio_needed = 1;
1348 vcpu->mmio_is_write = 1;
1350 if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
1351 val = dp_to_sp(val);
1353 /* Store the value at the lowest bytes in 'data'. */
1354 if (!host_swabbed) {
1356 case 8: *(u64 *)data = val; break;
1357 case 4: *(u32 *)data = val; break;
1358 case 2: *(u16 *)data = val; break;
1359 case 1: *(u8 *)data = val; break;
1363 case 8: *(u64 *)data = swab64(val); break;
1364 case 4: *(u32 *)data = swab32(val); break;
1365 case 2: *(u16 *)data = swab16(val); break;
1366 case 1: *(u8 *)data = val; break;
1370 idx = srcu_read_lock(&vcpu->kvm->srcu);
1372 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1373 bytes, &run->mmio.data);
1375 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1378 vcpu->mmio_needed = 0;
1379 return EMULATE_DONE;
1382 return EMULATE_DO_MMIO;
1384 EXPORT_SYMBOL_GPL(kvmppc_handle_store);
1387 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
1389 u32 dword_offset, word_offset;
1390 union kvmppc_one_reg reg;
1392 int copy_type = vcpu->arch.mmio_copy_type;
1395 switch (copy_type) {
1396 case KVMPPC_VSX_COPY_DWORD:
1398 kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
1400 if (vsx_offset == -1) {
1406 *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
1408 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1409 *val = reg.vsxval[vsx_offset];
1413 case KVMPPC_VSX_COPY_WORD:
1415 kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
1417 if (vsx_offset == -1) {
1423 dword_offset = vsx_offset / 2;
1424 word_offset = vsx_offset % 2;
1425 reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
1426 *val = reg.vsx32val[word_offset];
1428 reg.vval = VCPU_VSX_VR(vcpu, rs - 32);
1429 *val = reg.vsx32val[vsx_offset];
1441 int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1442 int rs, unsigned int bytes, int is_default_endian)
1445 enum emulation_result emulated = EMULATE_DONE;
1447 vcpu->arch.io_gpr = rs;
1449 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1450 if (vcpu->arch.mmio_vsx_copy_nums > 4)
1451 return EMULATE_FAIL;
1453 while (vcpu->arch.mmio_vsx_copy_nums) {
1454 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
1455 return EMULATE_FAIL;
1457 emulated = kvmppc_handle_store(run, vcpu,
1458 val, bytes, is_default_endian);
1460 if (emulated != EMULATE_DONE)
1463 vcpu->arch.paddr_accessed += run->mmio.len;
1465 vcpu->arch.mmio_vsx_copy_nums--;
1466 vcpu->arch.mmio_vsx_offset++;
1472 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
1473 struct kvm_run *run)
1475 enum emulation_result emulated = EMULATE_FAIL;
1478 vcpu->arch.paddr_accessed += run->mmio.len;
1480 if (!vcpu->mmio_is_write) {
1481 emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
1482 run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
1484 emulated = kvmppc_handle_vsx_store(run, vcpu,
1485 vcpu->arch.io_gpr, run->mmio.len, 1);
1489 case EMULATE_DO_MMIO:
1490 run->exit_reason = KVM_EXIT_MMIO;
1494 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1495 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1496 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1505 #endif /* CONFIG_VSX */
1507 #ifdef CONFIG_ALTIVEC
1508 int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1509 unsigned int rt, unsigned int bytes, int is_default_endian)
1511 enum emulation_result emulated = EMULATE_DONE;
1513 if (vcpu->arch.mmio_vsx_copy_nums > 2)
1514 return EMULATE_FAIL;
1516 while (vcpu->arch.mmio_vmx_copy_nums) {
1517 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1518 is_default_endian, 0);
1520 if (emulated != EMULATE_DONE)
1523 vcpu->arch.paddr_accessed += run->mmio.len;
1524 vcpu->arch.mmio_vmx_copy_nums--;
1525 vcpu->arch.mmio_vmx_offset++;
1531 int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val)
1533 union kvmppc_one_reg reg;
1538 kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1540 if (vmx_offset == -1)
1543 reg.vval = VCPU_VSX_VR(vcpu, index);
1544 *val = reg.vsxval[vmx_offset];
1549 int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val)
1551 union kvmppc_one_reg reg;
1556 kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1558 if (vmx_offset == -1)
1561 reg.vval = VCPU_VSX_VR(vcpu, index);
1562 *val = reg.vsx32val[vmx_offset];
1567 int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val)
1569 union kvmppc_one_reg reg;
1574 kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1576 if (vmx_offset == -1)
1579 reg.vval = VCPU_VSX_VR(vcpu, index);
1580 *val = reg.vsx16val[vmx_offset];
1585 int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val)
1587 union kvmppc_one_reg reg;
1592 kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset);
1594 if (vmx_offset == -1)
1597 reg.vval = VCPU_VSX_VR(vcpu, index);
1598 *val = reg.vsx8val[vmx_offset];
1603 int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1604 unsigned int rs, unsigned int bytes, int is_default_endian)
1607 unsigned int index = rs & KVM_MMIO_REG_MASK;
1608 enum emulation_result emulated = EMULATE_DONE;
1610 if (vcpu->arch.mmio_vsx_copy_nums > 2)
1611 return EMULATE_FAIL;
1613 vcpu->arch.io_gpr = rs;
1615 while (vcpu->arch.mmio_vmx_copy_nums) {
1616 switch (vcpu->arch.mmio_copy_type) {
1617 case KVMPPC_VMX_COPY_DWORD:
1618 if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1)
1619 return EMULATE_FAIL;
1622 case KVMPPC_VMX_COPY_WORD:
1623 if (kvmppc_get_vmx_word(vcpu, index, &val) == -1)
1624 return EMULATE_FAIL;
1626 case KVMPPC_VMX_COPY_HWORD:
1627 if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1)
1628 return EMULATE_FAIL;
1630 case KVMPPC_VMX_COPY_BYTE:
1631 if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1)
1632 return EMULATE_FAIL;
1635 return EMULATE_FAIL;
1638 emulated = kvmppc_handle_store(run, vcpu, val, bytes,
1640 if (emulated != EMULATE_DONE)
1643 vcpu->arch.paddr_accessed += run->mmio.len;
1644 vcpu->arch.mmio_vmx_copy_nums--;
1645 vcpu->arch.mmio_vmx_offset++;
1651 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu,
1652 struct kvm_run *run)
1654 enum emulation_result emulated = EMULATE_FAIL;
1657 vcpu->arch.paddr_accessed += run->mmio.len;
1659 if (!vcpu->mmio_is_write) {
1660 emulated = kvmppc_handle_vmx_load(run, vcpu,
1661 vcpu->arch.io_gpr, run->mmio.len, 1);
1663 emulated = kvmppc_handle_vmx_store(run, vcpu,
1664 vcpu->arch.io_gpr, run->mmio.len, 1);
1668 case EMULATE_DO_MMIO:
1669 run->exit_reason = KVM_EXIT_MMIO;
1673 pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
1674 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1675 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1684 #endif /* CONFIG_ALTIVEC */
1686 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1689 union kvmppc_one_reg val;
1692 size = one_reg_size(reg->id);
1693 if (size > sizeof(val))
1696 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1700 #ifdef CONFIG_ALTIVEC
1701 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1702 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1706 val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
1708 case KVM_REG_PPC_VSCR:
1709 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1713 val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
1715 case KVM_REG_PPC_VRSAVE:
1716 val = get_reg_val(reg->id, vcpu->arch.vrsave);
1718 #endif /* CONFIG_ALTIVEC */
1728 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1734 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1737 union kvmppc_one_reg val;
1740 size = one_reg_size(reg->id);
1741 if (size > sizeof(val))
1744 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1747 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1751 #ifdef CONFIG_ALTIVEC
1752 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1753 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1757 vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
1759 case KVM_REG_PPC_VSCR:
1760 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1764 vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
1766 case KVM_REG_PPC_VRSAVE:
1767 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1771 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1773 #endif /* CONFIG_ALTIVEC */
1783 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
1789 if (vcpu->mmio_needed) {
1790 vcpu->mmio_needed = 0;
1791 if (!vcpu->mmio_is_write)
1792 kvmppc_complete_mmio_load(vcpu, run);
1794 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1795 vcpu->arch.mmio_vsx_copy_nums--;
1796 vcpu->arch.mmio_vsx_offset++;
1799 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1800 r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
1801 if (r == RESUME_HOST) {
1802 vcpu->mmio_needed = 1;
1807 #ifdef CONFIG_ALTIVEC
1808 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1809 vcpu->arch.mmio_vmx_copy_nums--;
1810 vcpu->arch.mmio_vmx_offset++;
1813 if (vcpu->arch.mmio_vmx_copy_nums > 0) {
1814 r = kvmppc_emulate_mmio_vmx_loadstore(vcpu, run);
1815 if (r == RESUME_HOST) {
1816 vcpu->mmio_needed = 1;
1821 } else if (vcpu->arch.osi_needed) {
1822 u64 *gprs = run->osi.gprs;
1825 for (i = 0; i < 32; i++)
1826 kvmppc_set_gpr(vcpu, i, gprs[i]);
1827 vcpu->arch.osi_needed = 0;
1828 } else if (vcpu->arch.hcall_needed) {
1831 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1832 for (i = 0; i < 9; ++i)
1833 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1834 vcpu->arch.hcall_needed = 0;
1836 } else if (vcpu->arch.epr_needed) {
1837 kvmppc_set_epr(vcpu, run->epr.epr);
1838 vcpu->arch.epr_needed = 0;
1842 kvm_sigset_activate(vcpu);
1844 if (run->immediate_exit)
1847 r = kvmppc_vcpu_run(run, vcpu);
1849 kvm_sigset_deactivate(vcpu);
1851 #ifdef CONFIG_ALTIVEC
1858 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1860 if (irq->irq == KVM_INTERRUPT_UNSET) {
1861 kvmppc_core_dequeue_external(vcpu);
1865 kvmppc_core_queue_external(vcpu, irq);
1867 kvm_vcpu_kick(vcpu);
1872 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1873 struct kvm_enable_cap *cap)
1881 case KVM_CAP_PPC_OSI:
1883 vcpu->arch.osi_enabled = true;
1885 case KVM_CAP_PPC_PAPR:
1887 vcpu->arch.papr_enabled = true;
1889 case KVM_CAP_PPC_EPR:
1892 vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1894 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
1897 case KVM_CAP_PPC_BOOKE_WATCHDOG:
1899 vcpu->arch.watchdog_enabled = true;
1902 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1903 case KVM_CAP_SW_TLB: {
1904 struct kvm_config_tlb cfg;
1905 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1908 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1911 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1915 #ifdef CONFIG_KVM_MPIC
1916 case KVM_CAP_IRQ_MPIC: {
1918 struct kvm_device *dev;
1921 f = fdget(cap->args[0]);
1926 dev = kvm_device_from_filp(f.file);
1928 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1934 #ifdef CONFIG_KVM_XICS
1935 case KVM_CAP_IRQ_XICS: {
1937 struct kvm_device *dev;
1940 f = fdget(cap->args[0]);
1945 dev = kvm_device_from_filp(f.file);
1948 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
1950 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1956 #endif /* CONFIG_KVM_XICS */
1957 #ifdef CONFIG_KVM_XIVE
1958 case KVM_CAP_PPC_IRQ_XIVE: {
1960 struct kvm_device *dev;
1963 f = fdget(cap->args[0]);
1968 if (!xive_enabled())
1972 dev = kvm_device_from_filp(f.file);
1974 r = kvmppc_xive_native_connect_vcpu(dev, vcpu,
1980 #endif /* CONFIG_KVM_XIVE */
1981 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1982 case KVM_CAP_PPC_FWNMI:
1984 if (!is_kvmppc_hv_enabled(vcpu->kvm))
1987 vcpu->kvm->arch.fwnmi_enabled = true;
1989 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1996 r = kvmppc_sanity_check(vcpu);
2001 bool kvm_arch_intc_initialized(struct kvm *kvm)
2003 #ifdef CONFIG_KVM_MPIC
2007 #ifdef CONFIG_KVM_XICS
2008 if (kvm->arch.xics || kvm->arch.xive)
2014 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
2015 struct kvm_mp_state *mp_state)
2020 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2021 struct kvm_mp_state *mp_state)
2026 long kvm_arch_vcpu_async_ioctl(struct file *filp,
2027 unsigned int ioctl, unsigned long arg)
2029 struct kvm_vcpu *vcpu = filp->private_data;
2030 void __user *argp = (void __user *)arg;
2032 if (ioctl == KVM_INTERRUPT) {
2033 struct kvm_interrupt irq;
2034 if (copy_from_user(&irq, argp, sizeof(irq)))
2036 return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
2038 return -ENOIOCTLCMD;
2041 long kvm_arch_vcpu_ioctl(struct file *filp,
2042 unsigned int ioctl, unsigned long arg)
2044 struct kvm_vcpu *vcpu = filp->private_data;
2045 void __user *argp = (void __user *)arg;
2049 case KVM_ENABLE_CAP:
2051 struct kvm_enable_cap cap;
2054 if (copy_from_user(&cap, argp, sizeof(cap)))
2056 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2061 case KVM_SET_ONE_REG:
2062 case KVM_GET_ONE_REG:
2064 struct kvm_one_reg reg;
2066 if (copy_from_user(®, argp, sizeof(reg)))
2068 if (ioctl == KVM_SET_ONE_REG)
2069 r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®);
2071 r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®);
2075 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
2076 case KVM_DIRTY_TLB: {
2077 struct kvm_dirty_tlb dirty;
2080 if (copy_from_user(&dirty, argp, sizeof(dirty)))
2082 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
2095 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2097 return VM_FAULT_SIGBUS;
2100 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
2102 u32 inst_nop = 0x60000000;
2103 #ifdef CONFIG_KVM_BOOKE_HV
2104 u32 inst_sc1 = 0x44000022;
2105 pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
2106 pvinfo->hcall[1] = cpu_to_be32(inst_nop);
2107 pvinfo->hcall[2] = cpu_to_be32(inst_nop);
2108 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2110 u32 inst_lis = 0x3c000000;
2111 u32 inst_ori = 0x60000000;
2112 u32 inst_sc = 0x44000002;
2113 u32 inst_imm_mask = 0xffff;
2116 * The hypercall to get into KVM from within guest context is as
2119 * lis r0, r0, KVM_SC_MAGIC_R0@h
2120 * ori r0, KVM_SC_MAGIC_R0@l
2124 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
2125 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
2126 pvinfo->hcall[2] = cpu_to_be32(inst_sc);
2127 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
2130 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
2135 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
2138 if (!irqchip_in_kernel(kvm))
2141 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
2142 irq_event->irq, irq_event->level,
2148 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
2149 struct kvm_enable_cap *cap)
2157 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2158 case KVM_CAP_PPC_ENABLE_HCALL: {
2159 unsigned long hcall = cap->args[0];
2162 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
2165 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
2168 set_bit(hcall / 4, kvm->arch.enabled_hcalls);
2170 clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
2174 case KVM_CAP_PPC_SMT: {
2175 unsigned long mode = cap->args[0];
2176 unsigned long flags = cap->args[1];
2179 if (kvm->arch.kvm_ops->set_smt_mode)
2180 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
2184 case KVM_CAP_PPC_NESTED_HV:
2186 if (!is_kvmppc_hv_enabled(kvm) ||
2187 !kvm->arch.kvm_ops->enable_nested)
2189 r = kvm->arch.kvm_ops->enable_nested(kvm);
2200 #ifdef CONFIG_PPC_BOOK3S_64
2202 * These functions check whether the underlying hardware is safe
2203 * against attacks based on observing the effects of speculatively
2204 * executed instructions, and whether it supplies instructions for
2205 * use in workarounds. The information comes from firmware, either
2206 * via the device tree on powernv platforms or from an hcall on
2207 * pseries platforms.
2209 #ifdef CONFIG_PPC_PSERIES
2210 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2212 struct h_cpu_char_result c;
2215 if (!machine_is(pseries))
2218 rc = plpar_get_cpu_characteristics(&c);
2219 if (rc == H_SUCCESS) {
2220 cp->character = c.character;
2221 cp->behaviour = c.behaviour;
2222 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2223 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2224 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2225 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2226 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2227 KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
2228 KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
2229 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
2230 KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
2231 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2232 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2233 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
2234 KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
2239 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2245 static inline bool have_fw_feat(struct device_node *fw_features,
2246 const char *state, const char *name)
2248 struct device_node *np;
2251 np = of_get_child_by_name(fw_features, name);
2253 r = of_property_read_bool(np, state);
2259 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
2261 struct device_node *np, *fw_features;
2264 memset(cp, 0, sizeof(*cp));
2265 r = pseries_get_cpu_char(cp);
2269 np = of_find_node_by_name(NULL, "ibm,opal");
2271 fw_features = of_get_child_by_name(np, "fw-features");
2275 if (have_fw_feat(fw_features, "enabled",
2276 "inst-spec-barrier-ori31,31,0"))
2277 cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
2278 if (have_fw_feat(fw_features, "enabled",
2279 "fw-bcctrl-serialized"))
2280 cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
2281 if (have_fw_feat(fw_features, "enabled",
2282 "inst-l1d-flush-ori30,30,0"))
2283 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
2284 if (have_fw_feat(fw_features, "enabled",
2285 "inst-l1d-flush-trig2"))
2286 cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
2287 if (have_fw_feat(fw_features, "enabled",
2288 "fw-l1d-thread-split"))
2289 cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
2290 if (have_fw_feat(fw_features, "enabled",
2291 "fw-count-cache-disabled"))
2292 cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
2293 if (have_fw_feat(fw_features, "enabled",
2294 "fw-count-cache-flush-bcctr2,0,0"))
2295 cp->character |= KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
2296 cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
2297 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
2298 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
2299 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
2300 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
2301 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
2302 KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
2304 if (have_fw_feat(fw_features, "enabled",
2305 "speculation-policy-favor-security"))
2306 cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
2307 if (!have_fw_feat(fw_features, "disabled",
2308 "needs-l1d-flush-msr-pr-0-to-1"))
2309 cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
2310 if (!have_fw_feat(fw_features, "disabled",
2311 "needs-spec-barrier-for-bound-checks"))
2312 cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
2313 if (have_fw_feat(fw_features, "enabled",
2314 "needs-count-cache-flush-on-context-switch"))
2315 cp->behaviour |= KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
2316 cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
2317 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
2318 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
2319 KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
2321 of_node_put(fw_features);
2328 long kvm_arch_vm_ioctl(struct file *filp,
2329 unsigned int ioctl, unsigned long arg)
2331 struct kvm *kvm __maybe_unused = filp->private_data;
2332 void __user *argp = (void __user *)arg;
2336 case KVM_PPC_GET_PVINFO: {
2337 struct kvm_ppc_pvinfo pvinfo;
2338 memset(&pvinfo, 0, sizeof(pvinfo));
2339 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
2340 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
2347 #ifdef CONFIG_SPAPR_TCE_IOMMU
2348 case KVM_CREATE_SPAPR_TCE_64: {
2349 struct kvm_create_spapr_tce_64 create_tce_64;
2352 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
2354 if (create_tce_64.flags) {
2358 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2361 case KVM_CREATE_SPAPR_TCE: {
2362 struct kvm_create_spapr_tce create_tce;
2363 struct kvm_create_spapr_tce_64 create_tce_64;
2366 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
2369 create_tce_64.liobn = create_tce.liobn;
2370 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
2371 create_tce_64.offset = 0;
2372 create_tce_64.size = create_tce.window_size >>
2373 IOMMU_PAGE_SHIFT_4K;
2374 create_tce_64.flags = 0;
2375 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
2379 #ifdef CONFIG_PPC_BOOK3S_64
2380 case KVM_PPC_GET_SMMU_INFO: {
2381 struct kvm_ppc_smmu_info info;
2382 struct kvm *kvm = filp->private_data;
2384 memset(&info, 0, sizeof(info));
2385 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
2386 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2390 case KVM_PPC_RTAS_DEFINE_TOKEN: {
2391 struct kvm *kvm = filp->private_data;
2393 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
2396 case KVM_PPC_CONFIGURE_V3_MMU: {
2397 struct kvm *kvm = filp->private_data;
2398 struct kvm_ppc_mmuv3_cfg cfg;
2401 if (!kvm->arch.kvm_ops->configure_mmu)
2404 if (copy_from_user(&cfg, argp, sizeof(cfg)))
2406 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
2409 case KVM_PPC_GET_RMMU_INFO: {
2410 struct kvm *kvm = filp->private_data;
2411 struct kvm_ppc_rmmu_info info;
2414 if (!kvm->arch.kvm_ops->get_rmmu_info)
2416 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
2417 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
2421 case KVM_PPC_GET_CPU_CHAR: {
2422 struct kvm_ppc_cpu_char cpuchar;
2424 r = kvmppc_get_cpu_char(&cpuchar);
2425 if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
2430 struct kvm *kvm = filp->private_data;
2431 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
2433 #else /* CONFIG_PPC_BOOK3S_64 */
2442 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
2443 static unsigned long nr_lpids;
2445 long kvmppc_alloc_lpid(void)
2450 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
2451 if (lpid >= nr_lpids) {
2452 pr_err("%s: No LPIDs free\n", __func__);
2455 } while (test_and_set_bit(lpid, lpid_inuse));
2459 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
2461 void kvmppc_claim_lpid(long lpid)
2463 set_bit(lpid, lpid_inuse);
2465 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
2467 void kvmppc_free_lpid(long lpid)
2469 clear_bit(lpid, lpid_inuse);
2471 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
2473 void kvmppc_init_lpid(unsigned long nr_lpids_param)
2475 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
2476 memset(lpid_inuse, 0, sizeof(lpid_inuse));
2478 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
2480 int kvm_arch_init(void *opaque)
2485 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);