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/tlbflush.h>
37 #include <asm/cputhreads.h>
38 #include <asm/irqflags.h>
39 #include <asm/iommu.h>
40 #include <asm/switch_to.h>
45 #include "../mm/mmu_decl.h"
47 #define CREATE_TRACE_POINTS
50 struct kvmppc_ops *kvmppc_hv_ops;
51 EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
52 struct kvmppc_ops *kvmppc_pr_ops;
53 EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
56 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
58 return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
61 bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
63 return kvm_arch_vcpu_runnable(vcpu);
66 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
71 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
77 * Common checks before entering the guest world. Call with interrupts
82 * == 1 if we're ready to go into guest state
83 * <= 0 if we need to go back to the host with return value
85 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
89 WARN_ON(irqs_disabled());
100 if (signal_pending(current)) {
101 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
102 vcpu->run->exit_reason = KVM_EXIT_INTR;
107 vcpu->mode = IN_GUEST_MODE;
110 * Reading vcpu->requests must happen after setting vcpu->mode,
111 * so we don't miss a request because the requester sees
112 * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
113 * before next entering the guest (and thus doesn't IPI).
114 * This also orders the write to mode from any reads
115 * to the page tables done while the VCPU is running.
116 * Please see the comment in kvm_flush_remote_tlbs.
120 if (kvm_request_pending(vcpu)) {
121 /* Make sure we process requests preemptable */
123 trace_kvm_check_requests(vcpu);
124 r = kvmppc_core_check_requests(vcpu);
131 if (kvmppc_core_prepare_to_enter(vcpu)) {
132 /* interrupts got enabled in between, so we
133 are back at square 1 */
137 guest_enter_irqoff();
145 EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
147 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
148 static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
150 struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
153 shared->sprg0 = swab64(shared->sprg0);
154 shared->sprg1 = swab64(shared->sprg1);
155 shared->sprg2 = swab64(shared->sprg2);
156 shared->sprg3 = swab64(shared->sprg3);
157 shared->srr0 = swab64(shared->srr0);
158 shared->srr1 = swab64(shared->srr1);
159 shared->dar = swab64(shared->dar);
160 shared->msr = swab64(shared->msr);
161 shared->dsisr = swab32(shared->dsisr);
162 shared->int_pending = swab32(shared->int_pending);
163 for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
164 shared->sr[i] = swab32(shared->sr[i]);
168 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
170 int nr = kvmppc_get_gpr(vcpu, 11);
172 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
173 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
174 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
175 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
176 unsigned long r2 = 0;
178 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
180 param1 &= 0xffffffff;
181 param2 &= 0xffffffff;
182 param3 &= 0xffffffff;
183 param4 &= 0xffffffff;
187 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
189 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
190 /* Book3S can be little endian, find it out here */
191 int shared_big_endian = true;
192 if (vcpu->arch.intr_msr & MSR_LE)
193 shared_big_endian = false;
194 if (shared_big_endian != vcpu->arch.shared_big_endian)
195 kvmppc_swab_shared(vcpu);
196 vcpu->arch.shared_big_endian = shared_big_endian;
199 if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
201 * Older versions of the Linux magic page code had
202 * a bug where they would map their trampoline code
203 * NX. If that's the case, remove !PR NX capability.
205 vcpu->arch.disable_kernel_nx = true;
206 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
209 vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
210 vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
212 #ifdef CONFIG_PPC_64K_PAGES
214 * Make sure our 4k magic page is in the same window of a 64k
215 * page within the guest and within the host's page.
217 if ((vcpu->arch.magic_page_pa & 0xf000) !=
218 ((ulong)vcpu->arch.shared & 0xf000)) {
219 void *old_shared = vcpu->arch.shared;
220 ulong shared = (ulong)vcpu->arch.shared;
224 shared |= vcpu->arch.magic_page_pa & 0xf000;
225 new_shared = (void*)shared;
226 memcpy(new_shared, old_shared, 0x1000);
227 vcpu->arch.shared = new_shared;
231 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
236 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
238 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
239 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
242 /* Second return value is in r4 */
244 case EV_HCALL_TOKEN(EV_IDLE):
246 kvm_vcpu_block(vcpu);
247 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
250 r = EV_UNIMPLEMENTED;
254 kvmppc_set_gpr(vcpu, 4, r2);
258 EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
260 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
264 /* We have to know what CPU to virtualize */
268 /* PAPR only works with book3s_64 */
269 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
272 /* HV KVM can only do PAPR mode for now */
273 if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
276 #ifdef CONFIG_KVM_BOOKE_HV
277 if (!cpu_has_feature(CPU_FTR_EMB_HV))
285 return r ? 0 : -EINVAL;
287 EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
289 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
291 enum emulation_result er;
294 er = kvmppc_emulate_loadstore(vcpu);
297 /* Future optimization: only reload non-volatiles if they were
298 * actually modified. */
304 case EMULATE_DO_MMIO:
305 run->exit_reason = KVM_EXIT_MMIO;
306 /* We must reload nonvolatiles because "update" load/store
307 * instructions modify register state. */
308 /* Future optimization: only reload non-volatiles if they were
309 * actually modified. */
316 kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
317 /* XXX Deliver Program interrupt to guest. */
318 pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
329 EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
331 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
334 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
335 struct kvmppc_pte pte;
340 r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
350 /* Magic page override */
351 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
352 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
353 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
354 void *magic = vcpu->arch.shared;
355 magic += pte.eaddr & 0xfff;
356 memcpy(magic, ptr, size);
360 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
361 return EMULATE_DO_MMIO;
365 EXPORT_SYMBOL_GPL(kvmppc_st);
367 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
370 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
371 struct kvmppc_pte pte;
376 rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
386 if (!data && !pte.may_execute)
389 /* Magic page override */
390 if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
391 ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
392 !(kvmppc_get_msr(vcpu) & MSR_PR)) {
393 void *magic = vcpu->arch.shared;
394 magic += pte.eaddr & 0xfff;
395 memcpy(ptr, magic, size);
399 if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
400 return EMULATE_DO_MMIO;
404 EXPORT_SYMBOL_GPL(kvmppc_ld);
406 int kvm_arch_hardware_enable(void)
411 int kvm_arch_hardware_setup(void)
416 void kvm_arch_check_processor_compat(void *rtn)
418 *(int *)rtn = kvmppc_core_check_processor_compat();
421 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
423 struct kvmppc_ops *kvm_ops = NULL;
425 * if we have both HV and PR enabled, default is HV
429 kvm_ops = kvmppc_hv_ops;
431 kvm_ops = kvmppc_pr_ops;
434 } else if (type == KVM_VM_PPC_HV) {
437 kvm_ops = kvmppc_hv_ops;
438 } else if (type == KVM_VM_PPC_PR) {
441 kvm_ops = kvmppc_pr_ops;
445 if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
448 kvm->arch.kvm_ops = kvm_ops;
449 return kvmppc_core_init_vm(kvm);
454 bool kvm_arch_has_vcpu_debugfs(void)
459 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
464 void kvm_arch_destroy_vm(struct kvm *kvm)
467 struct kvm_vcpu *vcpu;
469 #ifdef CONFIG_KVM_XICS
471 * We call kick_all_cpus_sync() to ensure that all
472 * CPUs have executed any pending IPIs before we
473 * continue and free VCPUs structures below.
475 if (is_kvmppc_hv_enabled(kvm))
476 kick_all_cpus_sync();
479 kvm_for_each_vcpu(i, vcpu, kvm)
480 kvm_arch_vcpu_free(vcpu);
482 mutex_lock(&kvm->lock);
483 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
484 kvm->vcpus[i] = NULL;
486 atomic_set(&kvm->online_vcpus, 0);
488 kvmppc_core_destroy_vm(kvm);
490 mutex_unlock(&kvm->lock);
492 /* drop the module reference */
493 module_put(kvm->arch.kvm_ops->owner);
496 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
499 /* Assume we're using HV mode when the HV module is loaded */
500 int hv_enabled = kvmppc_hv_ops ? 1 : 0;
504 * Hooray - we know which VM type we're running on. Depend on
505 * that rather than the guess above.
507 hv_enabled = is_kvmppc_hv_enabled(kvm);
512 case KVM_CAP_PPC_BOOKE_SREGS:
513 case KVM_CAP_PPC_BOOKE_WATCHDOG:
514 case KVM_CAP_PPC_EPR:
516 case KVM_CAP_PPC_SEGSTATE:
517 case KVM_CAP_PPC_HIOR:
518 case KVM_CAP_PPC_PAPR:
520 case KVM_CAP_PPC_UNSET_IRQ:
521 case KVM_CAP_PPC_IRQ_LEVEL:
522 case KVM_CAP_ENABLE_CAP:
523 case KVM_CAP_ENABLE_CAP_VM:
524 case KVM_CAP_ONE_REG:
525 case KVM_CAP_IOEVENTFD:
526 case KVM_CAP_DEVICE_CTRL:
527 case KVM_CAP_IMMEDIATE_EXIT:
530 case KVM_CAP_PPC_PAIRED_SINGLES:
531 case KVM_CAP_PPC_OSI:
532 case KVM_CAP_PPC_GET_PVINFO:
533 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
536 /* We support this only for PR */
539 #ifdef CONFIG_KVM_MPIC
540 case KVM_CAP_IRQ_MPIC:
545 #ifdef CONFIG_PPC_BOOK3S_64
546 case KVM_CAP_SPAPR_TCE:
547 case KVM_CAP_SPAPR_TCE_64:
550 case KVM_CAP_SPAPR_TCE_VFIO:
551 r = !!cpu_has_feature(CPU_FTR_HVMODE);
553 case KVM_CAP_PPC_RTAS:
554 case KVM_CAP_PPC_FIXUP_HCALL:
555 case KVM_CAP_PPC_ENABLE_HCALL:
556 #ifdef CONFIG_KVM_XICS
557 case KVM_CAP_IRQ_XICS:
562 case KVM_CAP_PPC_ALLOC_HTAB:
565 #endif /* CONFIG_PPC_BOOK3S_64 */
566 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
567 case KVM_CAP_PPC_SMT:
570 if (kvm->arch.emul_smt_mode > 1)
571 r = kvm->arch.emul_smt_mode;
573 r = kvm->arch.smt_mode;
574 } else if (hv_enabled) {
575 if (cpu_has_feature(CPU_FTR_ARCH_300))
578 r = threads_per_subcore;
581 case KVM_CAP_PPC_SMT_POSSIBLE:
584 if (!cpu_has_feature(CPU_FTR_ARCH_300))
585 r = ((threads_per_subcore << 1) - 1);
587 /* P9 can emulate dbells, so allow any mode */
591 case KVM_CAP_PPC_RMA:
594 case KVM_CAP_PPC_HWRNG:
595 r = kvmppc_hwrng_present();
597 case KVM_CAP_PPC_MMU_RADIX:
598 r = !!(hv_enabled && radix_enabled());
600 case KVM_CAP_PPC_MMU_HASH_V3:
601 r = !!(hv_enabled && !radix_enabled() &&
602 cpu_has_feature(CPU_FTR_ARCH_300));
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 case KVM_CAP_MAX_VCPU_ID:
640 #ifdef CONFIG_PPC_BOOK3S_64
641 case KVM_CAP_PPC_GET_SMMU_INFO:
644 case KVM_CAP_SPAPR_MULTITCE:
647 case KVM_CAP_SPAPR_RESIZE_HPT:
648 /* Disable this on POWER9 until code handles new HPTE format */
649 r = !!hv_enabled && !cpu_has_feature(CPU_FTR_ARCH_300);
652 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
653 case KVM_CAP_PPC_FWNMI:
657 case KVM_CAP_PPC_HTM:
658 r = cpu_has_feature(CPU_FTR_TM_COMP) && hv_enabled;
668 long kvm_arch_dev_ioctl(struct file *filp,
669 unsigned int ioctl, unsigned long arg)
674 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
675 struct kvm_memory_slot *dont)
677 kvmppc_core_free_memslot(kvm, free, dont);
680 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
681 unsigned long npages)
683 return kvmppc_core_create_memslot(kvm, slot, npages);
686 int kvm_arch_prepare_memory_region(struct kvm *kvm,
687 struct kvm_memory_slot *memslot,
688 const struct kvm_userspace_memory_region *mem,
689 enum kvm_mr_change change)
691 return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
694 void kvm_arch_commit_memory_region(struct kvm *kvm,
695 const struct kvm_userspace_memory_region *mem,
696 const struct kvm_memory_slot *old,
697 const struct kvm_memory_slot *new,
698 enum kvm_mr_change change)
700 kvmppc_core_commit_memory_region(kvm, mem, old, new);
703 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
704 struct kvm_memory_slot *slot)
706 kvmppc_core_flush_memslot(kvm, slot);
709 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
711 struct kvm_vcpu *vcpu;
712 vcpu = kvmppc_core_vcpu_create(kvm, id);
714 vcpu->arch.wqp = &vcpu->wq;
715 kvmppc_create_vcpu_debugfs(vcpu, id);
720 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
724 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
726 /* Make sure we're not using the vcpu anymore */
727 hrtimer_cancel(&vcpu->arch.dec_timer);
729 kvmppc_remove_vcpu_debugfs(vcpu);
731 switch (vcpu->arch.irq_type) {
732 case KVMPPC_IRQ_MPIC:
733 kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
735 case KVMPPC_IRQ_XICS:
737 kvmppc_xive_cleanup_vcpu(vcpu);
739 kvmppc_xics_free_icp(vcpu);
743 kvmppc_core_vcpu_free(vcpu);
746 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
748 kvm_arch_vcpu_free(vcpu);
751 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
753 return kvmppc_core_pending_dec(vcpu);
756 static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
758 struct kvm_vcpu *vcpu;
760 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
761 kvmppc_decrementer_func(vcpu);
763 return HRTIMER_NORESTART;
766 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
770 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
771 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
772 vcpu->arch.dec_expires = get_tb();
774 #ifdef CONFIG_KVM_EXIT_TIMING
775 mutex_init(&vcpu->arch.exit_timing_lock);
777 ret = kvmppc_subarch_vcpu_init(vcpu);
781 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
783 kvmppc_mmu_destroy(vcpu);
784 kvmppc_subarch_vcpu_uninit(vcpu);
787 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
791 * vrsave (formerly usprg0) isn't used by Linux, but may
792 * be used by the guest.
794 * On non-booke this is associated with Altivec and
795 * is handled by code in book3s.c.
797 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
799 kvmppc_core_vcpu_load(vcpu, cpu);
802 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
804 kvmppc_core_vcpu_put(vcpu);
806 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
811 * irq_bypass_add_producer and irq_bypass_del_producer are only
812 * useful if the architecture supports PCI passthrough.
813 * irq_bypass_stop and irq_bypass_start are not needed and so
814 * kvm_ops are not defined for them.
816 bool kvm_arch_has_irq_bypass(void)
818 return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
819 (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
822 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
823 struct irq_bypass_producer *prod)
825 struct kvm_kernel_irqfd *irqfd =
826 container_of(cons, struct kvm_kernel_irqfd, consumer);
827 struct kvm *kvm = irqfd->kvm;
829 if (kvm->arch.kvm_ops->irq_bypass_add_producer)
830 return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
835 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
836 struct irq_bypass_producer *prod)
838 struct kvm_kernel_irqfd *irqfd =
839 container_of(cons, struct kvm_kernel_irqfd, consumer);
840 struct kvm *kvm = irqfd->kvm;
842 if (kvm->arch.kvm_ops->irq_bypass_del_producer)
843 kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
847 static inline int kvmppc_get_vsr_dword_offset(int index)
851 if ((index != 0) && (index != 1))
863 static inline int kvmppc_get_vsr_word_offset(int index)
867 if ((index > 3) || (index < 0))
878 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
881 union kvmppc_one_reg val;
882 int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
883 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
888 if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
889 val.vval = VCPU_VSX_VR(vcpu, index);
890 val.vsxval[offset] = gpr;
891 VCPU_VSX_VR(vcpu, index) = val.vval;
893 VCPU_VSX_FPR(vcpu, index, offset) = gpr;
897 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
900 union kvmppc_one_reg val;
901 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
903 if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
904 val.vval = VCPU_VSX_VR(vcpu, index);
907 VCPU_VSX_VR(vcpu, index) = val.vval;
909 VCPU_VSX_FPR(vcpu, index, 0) = gpr;
910 VCPU_VSX_FPR(vcpu, index, 1) = gpr;
914 static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
917 union kvmppc_one_reg val;
918 int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
919 int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
920 int dword_offset, word_offset;
925 if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
926 val.vval = VCPU_VSX_VR(vcpu, index);
927 val.vsx32val[offset] = gpr32;
928 VCPU_VSX_VR(vcpu, index) = val.vval;
930 dword_offset = offset / 2;
931 word_offset = offset % 2;
932 val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
933 val.vsx32val[word_offset] = gpr32;
934 VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
937 #endif /* CONFIG_VSX */
939 #ifdef CONFIG_PPC_FPU
940 static inline u64 sp_to_dp(u32 fprs)
946 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
952 static inline u32 dp_to_sp(u64 fprd)
958 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
965 #define sp_to_dp(x) (x)
966 #define dp_to_sp(x) (x)
967 #endif /* CONFIG_PPC_FPU */
969 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
972 u64 uninitialized_var(gpr);
974 if (run->mmio.len > sizeof(gpr)) {
975 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
979 if (!vcpu->arch.mmio_host_swabbed) {
980 switch (run->mmio.len) {
981 case 8: gpr = *(u64 *)run->mmio.data; break;
982 case 4: gpr = *(u32 *)run->mmio.data; break;
983 case 2: gpr = *(u16 *)run->mmio.data; break;
984 case 1: gpr = *(u8 *)run->mmio.data; break;
987 switch (run->mmio.len) {
988 case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
989 case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
990 case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
991 case 1: gpr = *(u8 *)run->mmio.data; break;
995 /* conversion between single and double precision */
996 if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
999 if (vcpu->arch.mmio_sign_extend) {
1000 switch (run->mmio.len) {
1003 gpr = (s64)(s32)gpr;
1007 gpr = (s64)(s16)gpr;
1015 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
1016 case KVM_MMIO_REG_GPR:
1017 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
1019 case KVM_MMIO_REG_FPR:
1020 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1022 #ifdef CONFIG_PPC_BOOK3S
1023 case KVM_MMIO_REG_QPR:
1024 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1026 case KVM_MMIO_REG_FQPR:
1027 VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
1028 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
1032 case KVM_MMIO_REG_VSX:
1033 if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_DWORD)
1034 kvmppc_set_vsr_dword(vcpu, gpr);
1035 else if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_WORD)
1036 kvmppc_set_vsr_word(vcpu, gpr);
1037 else if (vcpu->arch.mmio_vsx_copy_type ==
1038 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
1039 kvmppc_set_vsr_dword_dump(vcpu, gpr);
1047 static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1048 unsigned int rt, unsigned int bytes,
1049 int is_default_endian, int sign_extend)
1054 /* Pity C doesn't have a logical XOR operator */
1055 if (kvmppc_need_byteswap(vcpu)) {
1056 host_swabbed = is_default_endian;
1058 host_swabbed = !is_default_endian;
1061 if (bytes > sizeof(run->mmio.data)) {
1062 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1066 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1067 run->mmio.len = bytes;
1068 run->mmio.is_write = 0;
1070 vcpu->arch.io_gpr = rt;
1071 vcpu->arch.mmio_host_swabbed = host_swabbed;
1072 vcpu->mmio_needed = 1;
1073 vcpu->mmio_is_write = 0;
1074 vcpu->arch.mmio_sign_extend = sign_extend;
1076 idx = srcu_read_lock(&vcpu->kvm->srcu);
1078 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1079 bytes, &run->mmio.data);
1081 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1084 kvmppc_complete_mmio_load(vcpu, run);
1085 vcpu->mmio_needed = 0;
1086 return EMULATE_DONE;
1089 return EMULATE_DO_MMIO;
1092 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1093 unsigned int rt, unsigned int bytes,
1094 int is_default_endian)
1096 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
1098 EXPORT_SYMBOL_GPL(kvmppc_handle_load);
1100 /* Same as above, but sign extends */
1101 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
1102 unsigned int rt, unsigned int bytes,
1103 int is_default_endian)
1105 return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
1109 int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
1110 unsigned int rt, unsigned int bytes,
1111 int is_default_endian, int mmio_sign_extend)
1113 enum emulation_result emulated = EMULATE_DONE;
1115 /* Currently, mmio_vsx_copy_nums only allowed to be less than 4 */
1116 if ( (vcpu->arch.mmio_vsx_copy_nums > 4) ||
1117 (vcpu->arch.mmio_vsx_copy_nums < 0) ) {
1118 return EMULATE_FAIL;
1121 while (vcpu->arch.mmio_vsx_copy_nums) {
1122 emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
1123 is_default_endian, mmio_sign_extend);
1125 if (emulated != EMULATE_DONE)
1128 vcpu->arch.paddr_accessed += run->mmio.len;
1130 vcpu->arch.mmio_vsx_copy_nums--;
1131 vcpu->arch.mmio_vsx_offset++;
1135 #endif /* CONFIG_VSX */
1137 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1138 u64 val, unsigned int bytes, int is_default_endian)
1140 void *data = run->mmio.data;
1144 /* Pity C doesn't have a logical XOR operator */
1145 if (kvmppc_need_byteswap(vcpu)) {
1146 host_swabbed = is_default_endian;
1148 host_swabbed = !is_default_endian;
1151 if (bytes > sizeof(run->mmio.data)) {
1152 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
1156 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
1157 run->mmio.len = bytes;
1158 run->mmio.is_write = 1;
1159 vcpu->mmio_needed = 1;
1160 vcpu->mmio_is_write = 1;
1162 if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
1163 val = dp_to_sp(val);
1165 /* Store the value at the lowest bytes in 'data'. */
1166 if (!host_swabbed) {
1168 case 8: *(u64 *)data = val; break;
1169 case 4: *(u32 *)data = val; break;
1170 case 2: *(u16 *)data = val; break;
1171 case 1: *(u8 *)data = val; break;
1175 case 8: *(u64 *)data = swab64(val); break;
1176 case 4: *(u32 *)data = swab32(val); break;
1177 case 2: *(u16 *)data = swab16(val); break;
1178 case 1: *(u8 *)data = val; break;
1182 idx = srcu_read_lock(&vcpu->kvm->srcu);
1184 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
1185 bytes, &run->mmio.data);
1187 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1190 vcpu->mmio_needed = 0;
1191 return EMULATE_DONE;
1194 return EMULATE_DO_MMIO;
1196 EXPORT_SYMBOL_GPL(kvmppc_handle_store);
1199 static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
1201 u32 dword_offset, word_offset;
1202 union kvmppc_one_reg reg;
1204 int copy_type = vcpu->arch.mmio_vsx_copy_type;
1207 switch (copy_type) {
1208 case KVMPPC_VSX_COPY_DWORD:
1210 kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
1212 if (vsx_offset == -1) {
1217 if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
1218 *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
1220 reg.vval = VCPU_VSX_VR(vcpu, rs);
1221 *val = reg.vsxval[vsx_offset];
1225 case KVMPPC_VSX_COPY_WORD:
1227 kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
1229 if (vsx_offset == -1) {
1234 if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
1235 dword_offset = vsx_offset / 2;
1236 word_offset = vsx_offset % 2;
1237 reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
1238 *val = reg.vsx32val[word_offset];
1240 reg.vval = VCPU_VSX_VR(vcpu, rs);
1241 *val = reg.vsx32val[vsx_offset];
1253 int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
1254 int rs, unsigned int bytes, int is_default_endian)
1257 enum emulation_result emulated = EMULATE_DONE;
1259 vcpu->arch.io_gpr = rs;
1261 /* Currently, mmio_vsx_copy_nums only allowed to be less than 4 */
1262 if ( (vcpu->arch.mmio_vsx_copy_nums > 4) ||
1263 (vcpu->arch.mmio_vsx_copy_nums < 0) ) {
1264 return EMULATE_FAIL;
1267 while (vcpu->arch.mmio_vsx_copy_nums) {
1268 if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
1269 return EMULATE_FAIL;
1271 emulated = kvmppc_handle_store(run, vcpu,
1272 val, bytes, is_default_endian);
1274 if (emulated != EMULATE_DONE)
1277 vcpu->arch.paddr_accessed += run->mmio.len;
1279 vcpu->arch.mmio_vsx_copy_nums--;
1280 vcpu->arch.mmio_vsx_offset++;
1286 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
1287 struct kvm_run *run)
1289 enum emulation_result emulated = EMULATE_FAIL;
1292 vcpu->arch.paddr_accessed += run->mmio.len;
1294 if (!vcpu->mmio_is_write) {
1295 emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
1296 run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
1298 emulated = kvmppc_handle_vsx_store(run, vcpu,
1299 vcpu->arch.io_gpr, run->mmio.len, 1);
1303 case EMULATE_DO_MMIO:
1304 run->exit_reason = KVM_EXIT_MMIO;
1308 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1309 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1310 run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1319 #endif /* CONFIG_VSX */
1321 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1324 union kvmppc_one_reg val;
1327 size = one_reg_size(reg->id);
1328 if (size > sizeof(val))
1331 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1335 #ifdef CONFIG_ALTIVEC
1336 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1337 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1341 val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
1343 case KVM_REG_PPC_VSCR:
1344 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1348 val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
1350 case KVM_REG_PPC_VRSAVE:
1351 val = get_reg_val(reg->id, vcpu->arch.vrsave);
1353 #endif /* CONFIG_ALTIVEC */
1363 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1369 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1372 union kvmppc_one_reg val;
1375 size = one_reg_size(reg->id);
1376 if (size > sizeof(val))
1379 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1382 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1386 #ifdef CONFIG_ALTIVEC
1387 case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
1388 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1392 vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
1394 case KVM_REG_PPC_VSCR:
1395 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1399 vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
1401 case KVM_REG_PPC_VRSAVE:
1402 if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
1406 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1408 #endif /* CONFIG_ALTIVEC */
1418 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
1422 if (vcpu->mmio_needed) {
1423 vcpu->mmio_needed = 0;
1424 if (!vcpu->mmio_is_write)
1425 kvmppc_complete_mmio_load(vcpu, run);
1427 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1428 vcpu->arch.mmio_vsx_copy_nums--;
1429 vcpu->arch.mmio_vsx_offset++;
1432 if (vcpu->arch.mmio_vsx_copy_nums > 0) {
1433 r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
1434 if (r == RESUME_HOST) {
1435 vcpu->mmio_needed = 1;
1440 } else if (vcpu->arch.osi_needed) {
1441 u64 *gprs = run->osi.gprs;
1444 for (i = 0; i < 32; i++)
1445 kvmppc_set_gpr(vcpu, i, gprs[i]);
1446 vcpu->arch.osi_needed = 0;
1447 } else if (vcpu->arch.hcall_needed) {
1450 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
1451 for (i = 0; i < 9; ++i)
1452 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
1453 vcpu->arch.hcall_needed = 0;
1455 } else if (vcpu->arch.epr_needed) {
1456 kvmppc_set_epr(vcpu, run->epr.epr);
1457 vcpu->arch.epr_needed = 0;
1461 kvm_sigset_activate(vcpu);
1463 if (run->immediate_exit)
1466 r = kvmppc_vcpu_run(run, vcpu);
1468 kvm_sigset_deactivate(vcpu);
1473 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
1475 if (irq->irq == KVM_INTERRUPT_UNSET) {
1476 kvmppc_core_dequeue_external(vcpu);
1480 kvmppc_core_queue_external(vcpu, irq);
1482 kvm_vcpu_kick(vcpu);
1487 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
1488 struct kvm_enable_cap *cap)
1496 case KVM_CAP_PPC_OSI:
1498 vcpu->arch.osi_enabled = true;
1500 case KVM_CAP_PPC_PAPR:
1502 vcpu->arch.papr_enabled = true;
1504 case KVM_CAP_PPC_EPR:
1507 vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
1509 vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
1512 case KVM_CAP_PPC_BOOKE_WATCHDOG:
1514 vcpu->arch.watchdog_enabled = true;
1517 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1518 case KVM_CAP_SW_TLB: {
1519 struct kvm_config_tlb cfg;
1520 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
1523 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
1526 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
1530 #ifdef CONFIG_KVM_MPIC
1531 case KVM_CAP_IRQ_MPIC: {
1533 struct kvm_device *dev;
1536 f = fdget(cap->args[0]);
1541 dev = kvm_device_from_filp(f.file);
1543 r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
1549 #ifdef CONFIG_KVM_XICS
1550 case KVM_CAP_IRQ_XICS: {
1552 struct kvm_device *dev;
1555 f = fdget(cap->args[0]);
1560 dev = kvm_device_from_filp(f.file);
1563 r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
1565 r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
1571 #endif /* CONFIG_KVM_XICS */
1572 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1573 case KVM_CAP_PPC_FWNMI:
1575 if (!is_kvmppc_hv_enabled(vcpu->kvm))
1578 vcpu->kvm->arch.fwnmi_enabled = true;
1580 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1587 r = kvmppc_sanity_check(vcpu);
1592 bool kvm_arch_intc_initialized(struct kvm *kvm)
1594 #ifdef CONFIG_KVM_MPIC
1598 #ifdef CONFIG_KVM_XICS
1599 if (kvm->arch.xics || kvm->arch.xive)
1605 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1606 struct kvm_mp_state *mp_state)
1611 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1612 struct kvm_mp_state *mp_state)
1617 long kvm_arch_vcpu_ioctl(struct file *filp,
1618 unsigned int ioctl, unsigned long arg)
1620 struct kvm_vcpu *vcpu = filp->private_data;
1621 void __user *argp = (void __user *)arg;
1625 case KVM_INTERRUPT: {
1626 struct kvm_interrupt irq;
1628 if (copy_from_user(&irq, argp, sizeof(irq)))
1630 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
1634 case KVM_ENABLE_CAP:
1636 struct kvm_enable_cap cap;
1638 if (copy_from_user(&cap, argp, sizeof(cap)))
1640 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1644 case KVM_SET_ONE_REG:
1645 case KVM_GET_ONE_REG:
1647 struct kvm_one_reg reg;
1649 if (copy_from_user(®, argp, sizeof(reg)))
1651 if (ioctl == KVM_SET_ONE_REG)
1652 r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®);
1654 r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®);
1658 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1659 case KVM_DIRTY_TLB: {
1660 struct kvm_dirty_tlb dirty;
1662 if (copy_from_user(&dirty, argp, sizeof(dirty)))
1664 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
1676 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1678 return VM_FAULT_SIGBUS;
1681 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
1683 u32 inst_nop = 0x60000000;
1684 #ifdef CONFIG_KVM_BOOKE_HV
1685 u32 inst_sc1 = 0x44000022;
1686 pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
1687 pvinfo->hcall[1] = cpu_to_be32(inst_nop);
1688 pvinfo->hcall[2] = cpu_to_be32(inst_nop);
1689 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
1691 u32 inst_lis = 0x3c000000;
1692 u32 inst_ori = 0x60000000;
1693 u32 inst_sc = 0x44000002;
1694 u32 inst_imm_mask = 0xffff;
1697 * The hypercall to get into KVM from within guest context is as
1700 * lis r0, r0, KVM_SC_MAGIC_R0@h
1701 * ori r0, KVM_SC_MAGIC_R0@l
1705 pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
1706 pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
1707 pvinfo->hcall[2] = cpu_to_be32(inst_sc);
1708 pvinfo->hcall[3] = cpu_to_be32(inst_nop);
1711 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
1716 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
1719 if (!irqchip_in_kernel(kvm))
1722 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
1723 irq_event->irq, irq_event->level,
1729 static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
1730 struct kvm_enable_cap *cap)
1738 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
1739 case KVM_CAP_PPC_ENABLE_HCALL: {
1740 unsigned long hcall = cap->args[0];
1743 if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
1746 if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
1749 set_bit(hcall / 4, kvm->arch.enabled_hcalls);
1751 clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
1755 case KVM_CAP_PPC_SMT: {
1756 unsigned long mode = cap->args[0];
1757 unsigned long flags = cap->args[1];
1760 if (kvm->arch.kvm_ops->set_smt_mode)
1761 r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
1773 long kvm_arch_vm_ioctl(struct file *filp,
1774 unsigned int ioctl, unsigned long arg)
1776 struct kvm *kvm __maybe_unused = filp->private_data;
1777 void __user *argp = (void __user *)arg;
1781 case KVM_PPC_GET_PVINFO: {
1782 struct kvm_ppc_pvinfo pvinfo;
1783 memset(&pvinfo, 0, sizeof(pvinfo));
1784 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
1785 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
1792 case KVM_ENABLE_CAP:
1794 struct kvm_enable_cap cap;
1796 if (copy_from_user(&cap, argp, sizeof(cap)))
1798 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1801 #ifdef CONFIG_SPAPR_TCE_IOMMU
1802 case KVM_CREATE_SPAPR_TCE_64: {
1803 struct kvm_create_spapr_tce_64 create_tce_64;
1806 if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
1808 if (create_tce_64.flags) {
1812 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
1815 case KVM_CREATE_SPAPR_TCE: {
1816 struct kvm_create_spapr_tce create_tce;
1817 struct kvm_create_spapr_tce_64 create_tce_64;
1820 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
1823 create_tce_64.liobn = create_tce.liobn;
1824 create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
1825 create_tce_64.offset = 0;
1826 create_tce_64.size = create_tce.window_size >>
1827 IOMMU_PAGE_SHIFT_4K;
1828 create_tce_64.flags = 0;
1829 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
1833 #ifdef CONFIG_PPC_BOOK3S_64
1834 case KVM_PPC_GET_SMMU_INFO: {
1835 struct kvm_ppc_smmu_info info;
1836 struct kvm *kvm = filp->private_data;
1838 memset(&info, 0, sizeof(info));
1839 r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
1840 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
1844 case KVM_PPC_RTAS_DEFINE_TOKEN: {
1845 struct kvm *kvm = filp->private_data;
1847 r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
1850 case KVM_PPC_CONFIGURE_V3_MMU: {
1851 struct kvm *kvm = filp->private_data;
1852 struct kvm_ppc_mmuv3_cfg cfg;
1855 if (!kvm->arch.kvm_ops->configure_mmu)
1858 if (copy_from_user(&cfg, argp, sizeof(cfg)))
1860 r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
1863 case KVM_PPC_GET_RMMU_INFO: {
1864 struct kvm *kvm = filp->private_data;
1865 struct kvm_ppc_rmmu_info info;
1868 if (!kvm->arch.kvm_ops->get_rmmu_info)
1870 r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
1871 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
1876 struct kvm *kvm = filp->private_data;
1877 r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
1879 #else /* CONFIG_PPC_BOOK3S_64 */
1888 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
1889 static unsigned long nr_lpids;
1891 long kvmppc_alloc_lpid(void)
1896 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
1897 if (lpid >= nr_lpids) {
1898 pr_err("%s: No LPIDs free\n", __func__);
1901 } while (test_and_set_bit(lpid, lpid_inuse));
1905 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
1907 void kvmppc_claim_lpid(long lpid)
1909 set_bit(lpid, lpid_inuse);
1911 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
1913 void kvmppc_free_lpid(long lpid)
1915 clear_bit(lpid, lpid_inuse);
1917 EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
1919 void kvmppc_init_lpid(unsigned long nr_lpids_param)
1921 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
1922 memset(lpid_inuse, 0, sizeof(lpid_inuse));
1924 EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
1926 int kvm_arch_init(void *opaque)
1931 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);