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
));
600 case KVM_CAP_SYNC_MMU
:
601 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
603 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
609 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
610 case KVM_CAP_PPC_HTAB_FD
:
614 case KVM_CAP_NR_VCPUS
:
616 * Recommending a number of CPUs is somewhat arbitrary; we
617 * return the number of present CPUs for -HV (since a host
618 * will have secondary threads "offline"), and for other KVM
619 * implementations just count online CPUs.
622 r
= num_present_cpus();
624 r
= num_online_cpus();
626 case KVM_CAP_NR_MEMSLOTS
:
627 r
= KVM_USER_MEM_SLOTS
;
629 case KVM_CAP_MAX_VCPUS
:
632 #ifdef CONFIG_PPC_BOOK3S_64
633 case KVM_CAP_PPC_GET_SMMU_INFO
:
636 case KVM_CAP_SPAPR_MULTITCE
:
639 case KVM_CAP_SPAPR_RESIZE_HPT
:
643 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
644 case KVM_CAP_PPC_FWNMI
:
648 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
649 case KVM_CAP_PPC_HTM
:
650 r
= !!(cur_cpu_spec
->cpu_user_features2
& PPC_FEATURE2_HTM
) ||
651 (hv_enabled
&& cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST
));
662 long kvm_arch_dev_ioctl(struct file
*filp
,
663 unsigned int ioctl
, unsigned long arg
)
668 void kvm_arch_free_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*free
,
669 struct kvm_memory_slot
*dont
)
671 kvmppc_core_free_memslot(kvm
, free
, dont
);
674 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
675 unsigned long npages
)
677 return kvmppc_core_create_memslot(kvm
, slot
, npages
);
680 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
681 struct kvm_memory_slot
*memslot
,
682 const struct kvm_userspace_memory_region
*mem
,
683 enum kvm_mr_change change
)
685 return kvmppc_core_prepare_memory_region(kvm
, memslot
, mem
);
688 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
689 const struct kvm_userspace_memory_region
*mem
,
690 const struct kvm_memory_slot
*old
,
691 const struct kvm_memory_slot
*new,
692 enum kvm_mr_change change
)
694 kvmppc_core_commit_memory_region(kvm
, mem
, old
, new);
697 void kvm_arch_flush_shadow_memslot(struct kvm
*kvm
,
698 struct kvm_memory_slot
*slot
)
700 kvmppc_core_flush_memslot(kvm
, slot
);
703 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
, unsigned int id
)
705 struct kvm_vcpu
*vcpu
;
706 vcpu
= kvmppc_core_vcpu_create(kvm
, id
);
708 vcpu
->arch
.wqp
= &vcpu
->wq
;
709 kvmppc_create_vcpu_debugfs(vcpu
, id
);
714 void kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
718 void kvm_arch_vcpu_free(struct kvm_vcpu
*vcpu
)
720 /* Make sure we're not using the vcpu anymore */
721 hrtimer_cancel(&vcpu
->arch
.dec_timer
);
723 kvmppc_remove_vcpu_debugfs(vcpu
);
725 switch (vcpu
->arch
.irq_type
) {
726 case KVMPPC_IRQ_MPIC
:
727 kvmppc_mpic_disconnect_vcpu(vcpu
->arch
.mpic
, vcpu
);
729 case KVMPPC_IRQ_XICS
:
731 kvmppc_xive_cleanup_vcpu(vcpu
);
733 kvmppc_xics_free_icp(vcpu
);
737 kvmppc_core_vcpu_free(vcpu
);
740 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
742 kvm_arch_vcpu_free(vcpu
);
745 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
747 return kvmppc_core_pending_dec(vcpu
);
750 static enum hrtimer_restart
kvmppc_decrementer_wakeup(struct hrtimer
*timer
)
752 struct kvm_vcpu
*vcpu
;
754 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.dec_timer
);
755 kvmppc_decrementer_func(vcpu
);
757 return HRTIMER_NORESTART
;
760 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
764 hrtimer_init(&vcpu
->arch
.dec_timer
, CLOCK_REALTIME
, HRTIMER_MODE_ABS
);
765 vcpu
->arch
.dec_timer
.function
= kvmppc_decrementer_wakeup
;
766 vcpu
->arch
.dec_expires
= get_tb();
768 #ifdef CONFIG_KVM_EXIT_TIMING
769 mutex_init(&vcpu
->arch
.exit_timing_lock
);
771 ret
= kvmppc_subarch_vcpu_init(vcpu
);
775 void kvm_arch_vcpu_uninit(struct kvm_vcpu
*vcpu
)
777 kvmppc_mmu_destroy(vcpu
);
778 kvmppc_subarch_vcpu_uninit(vcpu
);
781 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
785 * vrsave (formerly usprg0) isn't used by Linux, but may
786 * be used by the guest.
788 * On non-booke this is associated with Altivec and
789 * is handled by code in book3s.c.
791 mtspr(SPRN_VRSAVE
, vcpu
->arch
.vrsave
);
793 kvmppc_core_vcpu_load(vcpu
, cpu
);
796 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
798 kvmppc_core_vcpu_put(vcpu
);
800 vcpu
->arch
.vrsave
= mfspr(SPRN_VRSAVE
);
805 * irq_bypass_add_producer and irq_bypass_del_producer are only
806 * useful if the architecture supports PCI passthrough.
807 * irq_bypass_stop and irq_bypass_start are not needed and so
808 * kvm_ops are not defined for them.
810 bool kvm_arch_has_irq_bypass(void)
812 return ((kvmppc_hv_ops
&& kvmppc_hv_ops
->irq_bypass_add_producer
) ||
813 (kvmppc_pr_ops
&& kvmppc_pr_ops
->irq_bypass_add_producer
));
816 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer
*cons
,
817 struct irq_bypass_producer
*prod
)
819 struct kvm_kernel_irqfd
*irqfd
=
820 container_of(cons
, struct kvm_kernel_irqfd
, consumer
);
821 struct kvm
*kvm
= irqfd
->kvm
;
823 if (kvm
->arch
.kvm_ops
->irq_bypass_add_producer
)
824 return kvm
->arch
.kvm_ops
->irq_bypass_add_producer(cons
, prod
);
829 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer
*cons
,
830 struct irq_bypass_producer
*prod
)
832 struct kvm_kernel_irqfd
*irqfd
=
833 container_of(cons
, struct kvm_kernel_irqfd
, consumer
);
834 struct kvm
*kvm
= irqfd
->kvm
;
836 if (kvm
->arch
.kvm_ops
->irq_bypass_del_producer
)
837 kvm
->arch
.kvm_ops
->irq_bypass_del_producer(cons
, prod
);
841 static inline int kvmppc_get_vsr_dword_offset(int index
)
845 if ((index
!= 0) && (index
!= 1))
857 static inline int kvmppc_get_vsr_word_offset(int index
)
861 if ((index
> 3) || (index
< 0))
872 static inline void kvmppc_set_vsr_dword(struct kvm_vcpu
*vcpu
,
875 union kvmppc_one_reg val
;
876 int offset
= kvmppc_get_vsr_dword_offset(vcpu
->arch
.mmio_vsx_offset
);
877 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
883 val
.vval
= VCPU_VSX_VR(vcpu
, index
- 32);
884 val
.vsxval
[offset
] = gpr
;
885 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
887 VCPU_VSX_FPR(vcpu
, index
, offset
) = gpr
;
891 static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu
*vcpu
,
894 union kvmppc_one_reg val
;
895 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
898 val
.vval
= VCPU_VSX_VR(vcpu
, index
- 32);
901 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
903 VCPU_VSX_FPR(vcpu
, index
, 0) = gpr
;
904 VCPU_VSX_FPR(vcpu
, index
, 1) = gpr
;
908 static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu
*vcpu
,
911 union kvmppc_one_reg val
;
912 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
915 val
.vsx32val
[0] = gpr
;
916 val
.vsx32val
[1] = gpr
;
917 val
.vsx32val
[2] = gpr
;
918 val
.vsx32val
[3] = gpr
;
919 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
921 val
.vsx32val
[0] = gpr
;
922 val
.vsx32val
[1] = gpr
;
923 VCPU_VSX_FPR(vcpu
, index
, 0) = val
.vsxval
[0];
924 VCPU_VSX_FPR(vcpu
, index
, 1) = val
.vsxval
[0];
928 static inline void kvmppc_set_vsr_word(struct kvm_vcpu
*vcpu
,
931 union kvmppc_one_reg val
;
932 int offset
= kvmppc_get_vsr_word_offset(vcpu
->arch
.mmio_vsx_offset
);
933 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
934 int dword_offset
, word_offset
;
940 val
.vval
= VCPU_VSX_VR(vcpu
, index
- 32);
941 val
.vsx32val
[offset
] = gpr32
;
942 VCPU_VSX_VR(vcpu
, index
- 32) = val
.vval
;
944 dword_offset
= offset
/ 2;
945 word_offset
= offset
% 2;
946 val
.vsxval
[0] = VCPU_VSX_FPR(vcpu
, index
, dword_offset
);
947 val
.vsx32val
[word_offset
] = gpr32
;
948 VCPU_VSX_FPR(vcpu
, index
, dword_offset
) = val
.vsxval
[0];
951 #endif /* CONFIG_VSX */
953 #ifdef CONFIG_ALTIVEC
954 static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu
*vcpu
,
955 int index
, int element_size
)
958 int elts
= sizeof(vector128
)/element_size
;
960 if ((index
< 0) || (index
>= elts
))
963 if (kvmppc_need_byteswap(vcpu
))
964 offset
= elts
- index
- 1;
971 static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu
*vcpu
,
974 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 8);
977 static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu
*vcpu
,
980 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 4);
983 static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu
*vcpu
,
986 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 2);
989 static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu
*vcpu
,
992 return kvmppc_get_vmx_offset_generic(vcpu
, index
, 1);
996 static inline void kvmppc_set_vmx_dword(struct kvm_vcpu
*vcpu
,
999 union kvmppc_one_reg val
;
1000 int offset
= kvmppc_get_vmx_dword_offset(vcpu
,
1001 vcpu
->arch
.mmio_vmx_offset
);
1002 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1007 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1008 val
.vsxval
[offset
] = gpr
;
1009 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1012 static inline void kvmppc_set_vmx_word(struct kvm_vcpu
*vcpu
,
1015 union kvmppc_one_reg val
;
1016 int offset
= kvmppc_get_vmx_word_offset(vcpu
,
1017 vcpu
->arch
.mmio_vmx_offset
);
1018 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1023 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1024 val
.vsx32val
[offset
] = gpr32
;
1025 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1028 static inline void kvmppc_set_vmx_hword(struct kvm_vcpu
*vcpu
,
1031 union kvmppc_one_reg val
;
1032 int offset
= kvmppc_get_vmx_hword_offset(vcpu
,
1033 vcpu
->arch
.mmio_vmx_offset
);
1034 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1039 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1040 val
.vsx16val
[offset
] = gpr16
;
1041 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1044 static inline void kvmppc_set_vmx_byte(struct kvm_vcpu
*vcpu
,
1047 union kvmppc_one_reg val
;
1048 int offset
= kvmppc_get_vmx_byte_offset(vcpu
,
1049 vcpu
->arch
.mmio_vmx_offset
);
1050 int index
= vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
;
1055 val
.vval
= VCPU_VSX_VR(vcpu
, index
);
1056 val
.vsx8val
[offset
] = gpr8
;
1057 VCPU_VSX_VR(vcpu
, index
) = val
.vval
;
1059 #endif /* CONFIG_ALTIVEC */
1061 #ifdef CONFIG_PPC_FPU
1062 static inline u64
sp_to_dp(u32 fprs
)
1068 asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd
) : "m" (fprs
)
1074 static inline u32
dp_to_sp(u64 fprd
)
1080 asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs
) : "m" (fprd
)
1087 #define sp_to_dp(x) (x)
1088 #define dp_to_sp(x) (x)
1089 #endif /* CONFIG_PPC_FPU */
1091 static void kvmppc_complete_mmio_load(struct kvm_vcpu
*vcpu
,
1092 struct kvm_run
*run
)
1094 u64
uninitialized_var(gpr
);
1096 if (run
->mmio
.len
> sizeof(gpr
)) {
1097 printk(KERN_ERR
"bad MMIO length: %d\n", run
->mmio
.len
);
1101 if (!vcpu
->arch
.mmio_host_swabbed
) {
1102 switch (run
->mmio
.len
) {
1103 case 8: gpr
= *(u64
*)run
->mmio
.data
; break;
1104 case 4: gpr
= *(u32
*)run
->mmio
.data
; break;
1105 case 2: gpr
= *(u16
*)run
->mmio
.data
; break;
1106 case 1: gpr
= *(u8
*)run
->mmio
.data
; break;
1109 switch (run
->mmio
.len
) {
1110 case 8: gpr
= swab64(*(u64
*)run
->mmio
.data
); break;
1111 case 4: gpr
= swab32(*(u32
*)run
->mmio
.data
); break;
1112 case 2: gpr
= swab16(*(u16
*)run
->mmio
.data
); break;
1113 case 1: gpr
= *(u8
*)run
->mmio
.data
; break;
1117 /* conversion between single and double precision */
1118 if ((vcpu
->arch
.mmio_sp64_extend
) && (run
->mmio
.len
== 4))
1119 gpr
= sp_to_dp(gpr
);
1121 if (vcpu
->arch
.mmio_sign_extend
) {
1122 switch (run
->mmio
.len
) {
1125 gpr
= (s64
)(s32
)gpr
;
1129 gpr
= (s64
)(s16
)gpr
;
1137 switch (vcpu
->arch
.io_gpr
& KVM_MMIO_REG_EXT_MASK
) {
1138 case KVM_MMIO_REG_GPR
:
1139 kvmppc_set_gpr(vcpu
, vcpu
->arch
.io_gpr
, gpr
);
1141 case KVM_MMIO_REG_FPR
:
1142 if (vcpu
->kvm
->arch
.kvm_ops
->giveup_ext
)
1143 vcpu
->kvm
->arch
.kvm_ops
->giveup_ext(vcpu
, MSR_FP
);
1145 VCPU_FPR(vcpu
, vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
) = gpr
;
1147 #ifdef CONFIG_PPC_BOOK3S
1148 case KVM_MMIO_REG_QPR
:
1149 vcpu
->arch
.qpr
[vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
] = gpr
;
1151 case KVM_MMIO_REG_FQPR
:
1152 VCPU_FPR(vcpu
, vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
) = gpr
;
1153 vcpu
->arch
.qpr
[vcpu
->arch
.io_gpr
& KVM_MMIO_REG_MASK
] = gpr
;
1157 case KVM_MMIO_REG_VSX
:
1158 if (vcpu
->kvm
->arch
.kvm_ops
->giveup_ext
)
1159 vcpu
->kvm
->arch
.kvm_ops
->giveup_ext(vcpu
, MSR_VSX
);
1161 if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VSX_COPY_DWORD
)
1162 kvmppc_set_vsr_dword(vcpu
, gpr
);
1163 else if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VSX_COPY_WORD
)
1164 kvmppc_set_vsr_word(vcpu
, gpr
);
1165 else if (vcpu
->arch
.mmio_copy_type
==
1166 KVMPPC_VSX_COPY_DWORD_LOAD_DUMP
)
1167 kvmppc_set_vsr_dword_dump(vcpu
, gpr
);
1168 else if (vcpu
->arch
.mmio_copy_type
==
1169 KVMPPC_VSX_COPY_WORD_LOAD_DUMP
)
1170 kvmppc_set_vsr_word_dump(vcpu
, gpr
);
1173 #ifdef CONFIG_ALTIVEC
1174 case KVM_MMIO_REG_VMX
:
1175 if (vcpu
->kvm
->arch
.kvm_ops
->giveup_ext
)
1176 vcpu
->kvm
->arch
.kvm_ops
->giveup_ext(vcpu
, MSR_VEC
);
1178 if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VMX_COPY_DWORD
)
1179 kvmppc_set_vmx_dword(vcpu
, gpr
);
1180 else if (vcpu
->arch
.mmio_copy_type
== KVMPPC_VMX_COPY_WORD
)
1181 kvmppc_set_vmx_word(vcpu
, gpr
);
1182 else if (vcpu
->arch
.mmio_copy_type
==
1183 KVMPPC_VMX_COPY_HWORD
)
1184 kvmppc_set_vmx_hword(vcpu
, gpr
);
1185 else if (vcpu
->arch
.mmio_copy_type
==
1186 KVMPPC_VMX_COPY_BYTE
)
1187 kvmppc_set_vmx_byte(vcpu
, gpr
);
1195 static int __kvmppc_handle_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1196 unsigned int rt
, unsigned int bytes
,
1197 int is_default_endian
, int sign_extend
)
1202 /* Pity C doesn't have a logical XOR operator */
1203 if (kvmppc_need_byteswap(vcpu
)) {
1204 host_swabbed
= is_default_endian
;
1206 host_swabbed
= !is_default_endian
;
1209 if (bytes
> sizeof(run
->mmio
.data
)) {
1210 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
1214 run
->mmio
.phys_addr
= vcpu
->arch
.paddr_accessed
;
1215 run
->mmio
.len
= bytes
;
1216 run
->mmio
.is_write
= 0;
1218 vcpu
->arch
.io_gpr
= rt
;
1219 vcpu
->arch
.mmio_host_swabbed
= host_swabbed
;
1220 vcpu
->mmio_needed
= 1;
1221 vcpu
->mmio_is_write
= 0;
1222 vcpu
->arch
.mmio_sign_extend
= sign_extend
;
1224 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1226 ret
= kvm_io_bus_read(vcpu
, KVM_MMIO_BUS
, run
->mmio
.phys_addr
,
1227 bytes
, &run
->mmio
.data
);
1229 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1232 kvmppc_complete_mmio_load(vcpu
, run
);
1233 vcpu
->mmio_needed
= 0;
1234 return EMULATE_DONE
;
1237 return EMULATE_DO_MMIO
;
1240 int kvmppc_handle_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1241 unsigned int rt
, unsigned int bytes
,
1242 int is_default_endian
)
1244 return __kvmppc_handle_load(run
, vcpu
, rt
, bytes
, is_default_endian
, 0);
1246 EXPORT_SYMBOL_GPL(kvmppc_handle_load
);
1248 /* Same as above, but sign extends */
1249 int kvmppc_handle_loads(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1250 unsigned int rt
, unsigned int bytes
,
1251 int is_default_endian
)
1253 return __kvmppc_handle_load(run
, vcpu
, rt
, bytes
, is_default_endian
, 1);
1257 int kvmppc_handle_vsx_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1258 unsigned int rt
, unsigned int bytes
,
1259 int is_default_endian
, int mmio_sign_extend
)
1261 enum emulation_result emulated
= EMULATE_DONE
;
1263 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1264 if (vcpu
->arch
.mmio_vsx_copy_nums
> 4)
1265 return EMULATE_FAIL
;
1267 while (vcpu
->arch
.mmio_vsx_copy_nums
) {
1268 emulated
= __kvmppc_handle_load(run
, vcpu
, rt
, bytes
,
1269 is_default_endian
, mmio_sign_extend
);
1271 if (emulated
!= EMULATE_DONE
)
1274 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1276 vcpu
->arch
.mmio_vsx_copy_nums
--;
1277 vcpu
->arch
.mmio_vsx_offset
++;
1281 #endif /* CONFIG_VSX */
1283 int kvmppc_handle_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1284 u64 val
, unsigned int bytes
, int is_default_endian
)
1286 void *data
= run
->mmio
.data
;
1290 /* Pity C doesn't have a logical XOR operator */
1291 if (kvmppc_need_byteswap(vcpu
)) {
1292 host_swabbed
= is_default_endian
;
1294 host_swabbed
= !is_default_endian
;
1297 if (bytes
> sizeof(run
->mmio
.data
)) {
1298 printk(KERN_ERR
"%s: bad MMIO length: %d\n", __func__
,
1302 run
->mmio
.phys_addr
= vcpu
->arch
.paddr_accessed
;
1303 run
->mmio
.len
= bytes
;
1304 run
->mmio
.is_write
= 1;
1305 vcpu
->mmio_needed
= 1;
1306 vcpu
->mmio_is_write
= 1;
1308 if ((vcpu
->arch
.mmio_sp64_extend
) && (bytes
== 4))
1309 val
= dp_to_sp(val
);
1311 /* Store the value at the lowest bytes in 'data'. */
1312 if (!host_swabbed
) {
1314 case 8: *(u64
*)data
= val
; break;
1315 case 4: *(u32
*)data
= val
; break;
1316 case 2: *(u16
*)data
= val
; break;
1317 case 1: *(u8
*)data
= val
; break;
1321 case 8: *(u64
*)data
= swab64(val
); break;
1322 case 4: *(u32
*)data
= swab32(val
); break;
1323 case 2: *(u16
*)data
= swab16(val
); break;
1324 case 1: *(u8
*)data
= val
; break;
1328 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1330 ret
= kvm_io_bus_write(vcpu
, KVM_MMIO_BUS
, run
->mmio
.phys_addr
,
1331 bytes
, &run
->mmio
.data
);
1333 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
1336 vcpu
->mmio_needed
= 0;
1337 return EMULATE_DONE
;
1340 return EMULATE_DO_MMIO
;
1342 EXPORT_SYMBOL_GPL(kvmppc_handle_store
);
1345 static inline int kvmppc_get_vsr_data(struct kvm_vcpu
*vcpu
, int rs
, u64
*val
)
1347 u32 dword_offset
, word_offset
;
1348 union kvmppc_one_reg reg
;
1350 int copy_type
= vcpu
->arch
.mmio_copy_type
;
1353 switch (copy_type
) {
1354 case KVMPPC_VSX_COPY_DWORD
:
1356 kvmppc_get_vsr_dword_offset(vcpu
->arch
.mmio_vsx_offset
);
1358 if (vsx_offset
== -1) {
1364 *val
= VCPU_VSX_FPR(vcpu
, rs
, vsx_offset
);
1366 reg
.vval
= VCPU_VSX_VR(vcpu
, rs
- 32);
1367 *val
= reg
.vsxval
[vsx_offset
];
1371 case KVMPPC_VSX_COPY_WORD
:
1373 kvmppc_get_vsr_word_offset(vcpu
->arch
.mmio_vsx_offset
);
1375 if (vsx_offset
== -1) {
1381 dword_offset
= vsx_offset
/ 2;
1382 word_offset
= vsx_offset
% 2;
1383 reg
.vsxval
[0] = VCPU_VSX_FPR(vcpu
, rs
, dword_offset
);
1384 *val
= reg
.vsx32val
[word_offset
];
1386 reg
.vval
= VCPU_VSX_VR(vcpu
, rs
- 32);
1387 *val
= reg
.vsx32val
[vsx_offset
];
1399 int kvmppc_handle_vsx_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1400 int rs
, unsigned int bytes
, int is_default_endian
)
1403 enum emulation_result emulated
= EMULATE_DONE
;
1405 vcpu
->arch
.io_gpr
= rs
;
1407 /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
1408 if (vcpu
->arch
.mmio_vsx_copy_nums
> 4)
1409 return EMULATE_FAIL
;
1411 while (vcpu
->arch
.mmio_vsx_copy_nums
) {
1412 if (kvmppc_get_vsr_data(vcpu
, rs
, &val
) == -1)
1413 return EMULATE_FAIL
;
1415 emulated
= kvmppc_handle_store(run
, vcpu
,
1416 val
, bytes
, is_default_endian
);
1418 if (emulated
!= EMULATE_DONE
)
1421 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1423 vcpu
->arch
.mmio_vsx_copy_nums
--;
1424 vcpu
->arch
.mmio_vsx_offset
++;
1430 static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu
*vcpu
,
1431 struct kvm_run
*run
)
1433 enum emulation_result emulated
= EMULATE_FAIL
;
1436 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1438 if (!vcpu
->mmio_is_write
) {
1439 emulated
= kvmppc_handle_vsx_load(run
, vcpu
, vcpu
->arch
.io_gpr
,
1440 run
->mmio
.len
, 1, vcpu
->arch
.mmio_sign_extend
);
1442 emulated
= kvmppc_handle_vsx_store(run
, vcpu
,
1443 vcpu
->arch
.io_gpr
, run
->mmio
.len
, 1);
1447 case EMULATE_DO_MMIO
:
1448 run
->exit_reason
= KVM_EXIT_MMIO
;
1452 pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
1453 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1454 run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
1463 #endif /* CONFIG_VSX */
1465 #ifdef CONFIG_ALTIVEC
1466 int kvmppc_handle_vmx_load(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1467 unsigned int rt
, unsigned int bytes
, int is_default_endian
)
1469 enum emulation_result emulated
= EMULATE_DONE
;
1471 if (vcpu
->arch
.mmio_vsx_copy_nums
> 2)
1472 return EMULATE_FAIL
;
1474 while (vcpu
->arch
.mmio_vmx_copy_nums
) {
1475 emulated
= __kvmppc_handle_load(run
, vcpu
, rt
, bytes
,
1476 is_default_endian
, 0);
1478 if (emulated
!= EMULATE_DONE
)
1481 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1482 vcpu
->arch
.mmio_vmx_copy_nums
--;
1483 vcpu
->arch
.mmio_vmx_offset
++;
1489 int kvmppc_get_vmx_dword(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1491 union kvmppc_one_reg reg
;
1496 kvmppc_get_vmx_dword_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1498 if (vmx_offset
== -1)
1501 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1502 *val
= reg
.vsxval
[vmx_offset
];
1507 int kvmppc_get_vmx_word(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1509 union kvmppc_one_reg reg
;
1514 kvmppc_get_vmx_word_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1516 if (vmx_offset
== -1)
1519 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1520 *val
= reg
.vsx32val
[vmx_offset
];
1525 int kvmppc_get_vmx_hword(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1527 union kvmppc_one_reg reg
;
1532 kvmppc_get_vmx_hword_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1534 if (vmx_offset
== -1)
1537 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1538 *val
= reg
.vsx16val
[vmx_offset
];
1543 int kvmppc_get_vmx_byte(struct kvm_vcpu
*vcpu
, int index
, u64
*val
)
1545 union kvmppc_one_reg reg
;
1550 kvmppc_get_vmx_byte_offset(vcpu
, vcpu
->arch
.mmio_vmx_offset
);
1552 if (vmx_offset
== -1)
1555 reg
.vval
= VCPU_VSX_VR(vcpu
, index
);
1556 *val
= reg
.vsx8val
[vmx_offset
];
1561 int kvmppc_handle_vmx_store(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1562 unsigned int rs
, unsigned int bytes
, int is_default_endian
)
1565 unsigned int index
= rs
& KVM_MMIO_REG_MASK
;
1566 enum emulation_result emulated
= EMULATE_DONE
;
1568 if (vcpu
->arch
.mmio_vsx_copy_nums
> 2)
1569 return EMULATE_FAIL
;
1571 vcpu
->arch
.io_gpr
= rs
;
1573 while (vcpu
->arch
.mmio_vmx_copy_nums
) {
1574 switch (vcpu
->arch
.mmio_copy_type
) {
1575 case KVMPPC_VMX_COPY_DWORD
:
1576 if (kvmppc_get_vmx_dword(vcpu
, index
, &val
) == -1)
1577 return EMULATE_FAIL
;
1580 case KVMPPC_VMX_COPY_WORD
:
1581 if (kvmppc_get_vmx_word(vcpu
, index
, &val
) == -1)
1582 return EMULATE_FAIL
;
1584 case KVMPPC_VMX_COPY_HWORD
:
1585 if (kvmppc_get_vmx_hword(vcpu
, index
, &val
) == -1)
1586 return EMULATE_FAIL
;
1588 case KVMPPC_VMX_COPY_BYTE
:
1589 if (kvmppc_get_vmx_byte(vcpu
, index
, &val
) == -1)
1590 return EMULATE_FAIL
;
1593 return EMULATE_FAIL
;
1596 emulated
= kvmppc_handle_store(run
, vcpu
, val
, bytes
,
1598 if (emulated
!= EMULATE_DONE
)
1601 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1602 vcpu
->arch
.mmio_vmx_copy_nums
--;
1603 vcpu
->arch
.mmio_vmx_offset
++;
1609 static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu
*vcpu
,
1610 struct kvm_run
*run
)
1612 enum emulation_result emulated
= EMULATE_FAIL
;
1615 vcpu
->arch
.paddr_accessed
+= run
->mmio
.len
;
1617 if (!vcpu
->mmio_is_write
) {
1618 emulated
= kvmppc_handle_vmx_load(run
, vcpu
,
1619 vcpu
->arch
.io_gpr
, run
->mmio
.len
, 1);
1621 emulated
= kvmppc_handle_vmx_store(run
, vcpu
,
1622 vcpu
->arch
.io_gpr
, run
->mmio
.len
, 1);
1626 case EMULATE_DO_MMIO
:
1627 run
->exit_reason
= KVM_EXIT_MMIO
;
1631 pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
1632 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1633 run
->internal
.suberror
= KVM_INTERNAL_ERROR_EMULATION
;
1642 #endif /* CONFIG_ALTIVEC */
1644 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu
*vcpu
, struct kvm_one_reg
*reg
)
1647 union kvmppc_one_reg val
;
1650 size
= one_reg_size(reg
->id
);
1651 if (size
> sizeof(val
))
1654 r
= kvmppc_get_one_reg(vcpu
, reg
->id
, &val
);
1658 #ifdef CONFIG_ALTIVEC
1659 case KVM_REG_PPC_VR0
... KVM_REG_PPC_VR31
:
1660 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1664 val
.vval
= vcpu
->arch
.vr
.vr
[reg
->id
- KVM_REG_PPC_VR0
];
1666 case KVM_REG_PPC_VSCR
:
1667 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1671 val
= get_reg_val(reg
->id
, vcpu
->arch
.vr
.vscr
.u
[3]);
1673 case KVM_REG_PPC_VRSAVE
:
1674 val
= get_reg_val(reg
->id
, vcpu
->arch
.vrsave
);
1676 #endif /* CONFIG_ALTIVEC */
1686 if (copy_to_user((char __user
*)(unsigned long)reg
->addr
, &val
, size
))
1692 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu
*vcpu
, struct kvm_one_reg
*reg
)
1695 union kvmppc_one_reg val
;
1698 size
= one_reg_size(reg
->id
);
1699 if (size
> sizeof(val
))
1702 if (copy_from_user(&val
, (char __user
*)(unsigned long)reg
->addr
, size
))
1705 r
= kvmppc_set_one_reg(vcpu
, reg
->id
, &val
);
1709 #ifdef CONFIG_ALTIVEC
1710 case KVM_REG_PPC_VR0
... KVM_REG_PPC_VR31
:
1711 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1715 vcpu
->arch
.vr
.vr
[reg
->id
- KVM_REG_PPC_VR0
] = val
.vval
;
1717 case KVM_REG_PPC_VSCR
:
1718 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1722 vcpu
->arch
.vr
.vscr
.u
[3] = set_reg_val(reg
->id
, val
);
1724 case KVM_REG_PPC_VRSAVE
:
1725 if (!cpu_has_feature(CPU_FTR_ALTIVEC
)) {
1729 vcpu
->arch
.vrsave
= set_reg_val(reg
->id
, val
);
1731 #endif /* CONFIG_ALTIVEC */
1741 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1747 if (vcpu
->mmio_needed
) {
1748 vcpu
->mmio_needed
= 0;
1749 if (!vcpu
->mmio_is_write
)
1750 kvmppc_complete_mmio_load(vcpu
, run
);
1752 if (vcpu
->arch
.mmio_vsx_copy_nums
> 0) {
1753 vcpu
->arch
.mmio_vsx_copy_nums
--;
1754 vcpu
->arch
.mmio_vsx_offset
++;
1757 if (vcpu
->arch
.mmio_vsx_copy_nums
> 0) {
1758 r
= kvmppc_emulate_mmio_vsx_loadstore(vcpu
, run
);
1759 if (r
== RESUME_HOST
) {
1760 vcpu
->mmio_needed
= 1;
1765 #ifdef CONFIG_ALTIVEC
1766 if (vcpu
->arch
.mmio_vmx_copy_nums
> 0) {
1767 vcpu
->arch
.mmio_vmx_copy_nums
--;
1768 vcpu
->arch
.mmio_vmx_offset
++;
1771 if (vcpu
->arch
.mmio_vmx_copy_nums
> 0) {
1772 r
= kvmppc_emulate_mmio_vmx_loadstore(vcpu
, run
);
1773 if (r
== RESUME_HOST
) {
1774 vcpu
->mmio_needed
= 1;
1779 } else if (vcpu
->arch
.osi_needed
) {
1780 u64
*gprs
= run
->osi
.gprs
;
1783 for (i
= 0; i
< 32; i
++)
1784 kvmppc_set_gpr(vcpu
, i
, gprs
[i
]);
1785 vcpu
->arch
.osi_needed
= 0;
1786 } else if (vcpu
->arch
.hcall_needed
) {
1789 kvmppc_set_gpr(vcpu
, 3, run
->papr_hcall
.ret
);
1790 for (i
= 0; i
< 9; ++i
)
1791 kvmppc_set_gpr(vcpu
, 4 + i
, run
->papr_hcall
.args
[i
]);
1792 vcpu
->arch
.hcall_needed
= 0;
1794 } else if (vcpu
->arch
.epr_needed
) {
1795 kvmppc_set_epr(vcpu
, run
->epr
.epr
);
1796 vcpu
->arch
.epr_needed
= 0;
1800 kvm_sigset_activate(vcpu
);
1802 if (run
->immediate_exit
)
1805 r
= kvmppc_vcpu_run(run
, vcpu
);
1807 kvm_sigset_deactivate(vcpu
);
1809 #ifdef CONFIG_ALTIVEC
1816 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu
*vcpu
, struct kvm_interrupt
*irq
)
1818 if (irq
->irq
== KVM_INTERRUPT_UNSET
) {
1819 kvmppc_core_dequeue_external(vcpu
);
1823 kvmppc_core_queue_external(vcpu
, irq
);
1825 kvm_vcpu_kick(vcpu
);
1830 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu
*vcpu
,
1831 struct kvm_enable_cap
*cap
)
1839 case KVM_CAP_PPC_OSI
:
1841 vcpu
->arch
.osi_enabled
= true;
1843 case KVM_CAP_PPC_PAPR
:
1845 vcpu
->arch
.papr_enabled
= true;
1847 case KVM_CAP_PPC_EPR
:
1850 vcpu
->arch
.epr_flags
|= KVMPPC_EPR_USER
;
1852 vcpu
->arch
.epr_flags
&= ~KVMPPC_EPR_USER
;
1855 case KVM_CAP_PPC_BOOKE_WATCHDOG
:
1857 vcpu
->arch
.watchdog_enabled
= true;
1860 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
1861 case KVM_CAP_SW_TLB
: {
1862 struct kvm_config_tlb cfg
;
1863 void __user
*user_ptr
= (void __user
*)(uintptr_t)cap
->args
[0];
1866 if (copy_from_user(&cfg
, user_ptr
, sizeof(cfg
)))
1869 r
= kvm_vcpu_ioctl_config_tlb(vcpu
, &cfg
);
1873 #ifdef CONFIG_KVM_MPIC
1874 case KVM_CAP_IRQ_MPIC
: {
1876 struct kvm_device
*dev
;
1879 f
= fdget(cap
->args
[0]);
1884 dev
= kvm_device_from_filp(f
.file
);
1886 r
= kvmppc_mpic_connect_vcpu(dev
, vcpu
, cap
->args
[1]);
1892 #ifdef CONFIG_KVM_XICS
1893 case KVM_CAP_IRQ_XICS
: {
1895 struct kvm_device
*dev
;
1898 f
= fdget(cap
->args
[0]);
1903 dev
= kvm_device_from_filp(f
.file
);
1906 r
= kvmppc_xive_connect_vcpu(dev
, vcpu
, cap
->args
[1]);
1908 r
= kvmppc_xics_connect_vcpu(dev
, vcpu
, cap
->args
[1]);
1914 #endif /* CONFIG_KVM_XICS */
1915 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
1916 case KVM_CAP_PPC_FWNMI
:
1918 if (!is_kvmppc_hv_enabled(vcpu
->kvm
))
1921 vcpu
->kvm
->arch
.fwnmi_enabled
= true;
1923 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
1930 r
= kvmppc_sanity_check(vcpu
);
1935 bool kvm_arch_intc_initialized(struct kvm
*kvm
)
1937 #ifdef CONFIG_KVM_MPIC
1941 #ifdef CONFIG_KVM_XICS
1942 if (kvm
->arch
.xics
|| kvm
->arch
.xive
)
1948 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
1949 struct kvm_mp_state
*mp_state
)
1954 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
1955 struct kvm_mp_state
*mp_state
)
1960 long kvm_arch_vcpu_async_ioctl(struct file
*filp
,
1961 unsigned int ioctl
, unsigned long arg
)
1963 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1964 void __user
*argp
= (void __user
*)arg
;
1966 if (ioctl
== KVM_INTERRUPT
) {
1967 struct kvm_interrupt irq
;
1968 if (copy_from_user(&irq
, argp
, sizeof(irq
)))
1970 return kvm_vcpu_ioctl_interrupt(vcpu
, &irq
);
1972 return -ENOIOCTLCMD
;
1975 long kvm_arch_vcpu_ioctl(struct file
*filp
,
1976 unsigned int ioctl
, unsigned long arg
)
1978 struct kvm_vcpu
*vcpu
= filp
->private_data
;
1979 void __user
*argp
= (void __user
*)arg
;
1983 case KVM_ENABLE_CAP
:
1985 struct kvm_enable_cap cap
;
1988 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
1990 r
= kvm_vcpu_ioctl_enable_cap(vcpu
, &cap
);
1995 case KVM_SET_ONE_REG
:
1996 case KVM_GET_ONE_REG
:
1998 struct kvm_one_reg reg
;
2000 if (copy_from_user(®
, argp
, sizeof(reg
)))
2002 if (ioctl
== KVM_SET_ONE_REG
)
2003 r
= kvm_vcpu_ioctl_set_one_reg(vcpu
, ®
);
2005 r
= kvm_vcpu_ioctl_get_one_reg(vcpu
, ®
);
2009 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
2010 case KVM_DIRTY_TLB
: {
2011 struct kvm_dirty_tlb dirty
;
2014 if (copy_from_user(&dirty
, argp
, sizeof(dirty
)))
2016 r
= kvm_vcpu_ioctl_dirty_tlb(vcpu
, &dirty
);
2029 vm_fault_t
kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
2031 return VM_FAULT_SIGBUS
;
2034 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo
*pvinfo
)
2036 u32 inst_nop
= 0x60000000;
2037 #ifdef CONFIG_KVM_BOOKE_HV
2038 u32 inst_sc1
= 0x44000022;
2039 pvinfo
->hcall
[0] = cpu_to_be32(inst_sc1
);
2040 pvinfo
->hcall
[1] = cpu_to_be32(inst_nop
);
2041 pvinfo
->hcall
[2] = cpu_to_be32(inst_nop
);
2042 pvinfo
->hcall
[3] = cpu_to_be32(inst_nop
);
2044 u32 inst_lis
= 0x3c000000;
2045 u32 inst_ori
= 0x60000000;
2046 u32 inst_sc
= 0x44000002;
2047 u32 inst_imm_mask
= 0xffff;
2050 * The hypercall to get into KVM from within guest context is as
2053 * lis r0, r0, KVM_SC_MAGIC_R0@h
2054 * ori r0, KVM_SC_MAGIC_R0@l
2058 pvinfo
->hcall
[0] = cpu_to_be32(inst_lis
| ((KVM_SC_MAGIC_R0
>> 16) & inst_imm_mask
));
2059 pvinfo
->hcall
[1] = cpu_to_be32(inst_ori
| (KVM_SC_MAGIC_R0
& inst_imm_mask
));
2060 pvinfo
->hcall
[2] = cpu_to_be32(inst_sc
);
2061 pvinfo
->hcall
[3] = cpu_to_be32(inst_nop
);
2064 pvinfo
->flags
= KVM_PPC_PVINFO_FLAGS_EV_IDLE
;
2069 int kvm_vm_ioctl_irq_line(struct kvm
*kvm
, struct kvm_irq_level
*irq_event
,
2072 if (!irqchip_in_kernel(kvm
))
2075 irq_event
->status
= kvm_set_irq(kvm
, KVM_USERSPACE_IRQ_SOURCE_ID
,
2076 irq_event
->irq
, irq_event
->level
,
2082 static int kvm_vm_ioctl_enable_cap(struct kvm
*kvm
,
2083 struct kvm_enable_cap
*cap
)
2091 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
2092 case KVM_CAP_PPC_ENABLE_HCALL
: {
2093 unsigned long hcall
= cap
->args
[0];
2096 if (hcall
> MAX_HCALL_OPCODE
|| (hcall
& 3) ||
2099 if (!kvmppc_book3s_hcall_implemented(kvm
, hcall
))
2102 set_bit(hcall
/ 4, kvm
->arch
.enabled_hcalls
);
2104 clear_bit(hcall
/ 4, kvm
->arch
.enabled_hcalls
);
2108 case KVM_CAP_PPC_SMT
: {
2109 unsigned long mode
= cap
->args
[0];
2110 unsigned long flags
= cap
->args
[1];
2113 if (kvm
->arch
.kvm_ops
->set_smt_mode
)
2114 r
= kvm
->arch
.kvm_ops
->set_smt_mode(kvm
, mode
, flags
);
2126 #ifdef CONFIG_PPC_BOOK3S_64
2128 * These functions check whether the underlying hardware is safe
2129 * against attacks based on observing the effects of speculatively
2130 * executed instructions, and whether it supplies instructions for
2131 * use in workarounds. The information comes from firmware, either
2132 * via the device tree on powernv platforms or from an hcall on
2133 * pseries platforms.
2135 #ifdef CONFIG_PPC_PSERIES
2136 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char
*cp
)
2138 struct h_cpu_char_result c
;
2141 if (!machine_is(pseries
))
2144 rc
= plpar_get_cpu_characteristics(&c
);
2145 if (rc
== H_SUCCESS
) {
2146 cp
->character
= c
.character
;
2147 cp
->behaviour
= c
.behaviour
;
2148 cp
->character_mask
= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31
|
2149 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED
|
2150 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30
|
2151 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2
|
2152 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV
|
2153 KVM_PPC_CPU_CHAR_BR_HINT_HONOURED
|
2154 KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF
|
2155 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS
;
2156 cp
->behaviour_mask
= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY
|
2157 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR
|
2158 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR
;
2163 static int pseries_get_cpu_char(struct kvm_ppc_cpu_char
*cp
)
2169 static inline bool have_fw_feat(struct device_node
*fw_features
,
2170 const char *state
, const char *name
)
2172 struct device_node
*np
;
2175 np
= of_get_child_by_name(fw_features
, name
);
2177 r
= of_property_read_bool(np
, state
);
2183 static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char
*cp
)
2185 struct device_node
*np
, *fw_features
;
2188 memset(cp
, 0, sizeof(*cp
));
2189 r
= pseries_get_cpu_char(cp
);
2193 np
= of_find_node_by_name(NULL
, "ibm,opal");
2195 fw_features
= of_get_child_by_name(np
, "fw-features");
2199 if (have_fw_feat(fw_features
, "enabled",
2200 "inst-spec-barrier-ori31,31,0"))
2201 cp
->character
|= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31
;
2202 if (have_fw_feat(fw_features
, "enabled",
2203 "fw-bcctrl-serialized"))
2204 cp
->character
|= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED
;
2205 if (have_fw_feat(fw_features
, "enabled",
2206 "inst-l1d-flush-ori30,30,0"))
2207 cp
->character
|= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30
;
2208 if (have_fw_feat(fw_features
, "enabled",
2209 "inst-l1d-flush-trig2"))
2210 cp
->character
|= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2
;
2211 if (have_fw_feat(fw_features
, "enabled",
2212 "fw-l1d-thread-split"))
2213 cp
->character
|= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV
;
2214 if (have_fw_feat(fw_features
, "enabled",
2215 "fw-count-cache-disabled"))
2216 cp
->character
|= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS
;
2217 cp
->character_mask
= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31
|
2218 KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED
|
2219 KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30
|
2220 KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2
|
2221 KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV
|
2222 KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS
;
2224 if (have_fw_feat(fw_features
, "enabled",
2225 "speculation-policy-favor-security"))
2226 cp
->behaviour
|= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY
;
2227 if (!have_fw_feat(fw_features
, "disabled",
2228 "needs-l1d-flush-msr-pr-0-to-1"))
2229 cp
->behaviour
|= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR
;
2230 if (!have_fw_feat(fw_features
, "disabled",
2231 "needs-spec-barrier-for-bound-checks"))
2232 cp
->behaviour
|= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR
;
2233 cp
->behaviour_mask
= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY
|
2234 KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR
|
2235 KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR
;
2237 of_node_put(fw_features
);
2244 long kvm_arch_vm_ioctl(struct file
*filp
,
2245 unsigned int ioctl
, unsigned long arg
)
2247 struct kvm
*kvm __maybe_unused
= filp
->private_data
;
2248 void __user
*argp
= (void __user
*)arg
;
2252 case KVM_PPC_GET_PVINFO
: {
2253 struct kvm_ppc_pvinfo pvinfo
;
2254 memset(&pvinfo
, 0, sizeof(pvinfo
));
2255 r
= kvm_vm_ioctl_get_pvinfo(&pvinfo
);
2256 if (copy_to_user(argp
, &pvinfo
, sizeof(pvinfo
))) {
2263 case KVM_ENABLE_CAP
:
2265 struct kvm_enable_cap cap
;
2267 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
2269 r
= kvm_vm_ioctl_enable_cap(kvm
, &cap
);
2272 #ifdef CONFIG_SPAPR_TCE_IOMMU
2273 case KVM_CREATE_SPAPR_TCE_64
: {
2274 struct kvm_create_spapr_tce_64 create_tce_64
;
2277 if (copy_from_user(&create_tce_64
, argp
, sizeof(create_tce_64
)))
2279 if (create_tce_64
.flags
) {
2283 r
= kvm_vm_ioctl_create_spapr_tce(kvm
, &create_tce_64
);
2286 case KVM_CREATE_SPAPR_TCE
: {
2287 struct kvm_create_spapr_tce create_tce
;
2288 struct kvm_create_spapr_tce_64 create_tce_64
;
2291 if (copy_from_user(&create_tce
, argp
, sizeof(create_tce
)))
2294 create_tce_64
.liobn
= create_tce
.liobn
;
2295 create_tce_64
.page_shift
= IOMMU_PAGE_SHIFT_4K
;
2296 create_tce_64
.offset
= 0;
2297 create_tce_64
.size
= create_tce
.window_size
>>
2298 IOMMU_PAGE_SHIFT_4K
;
2299 create_tce_64
.flags
= 0;
2300 r
= kvm_vm_ioctl_create_spapr_tce(kvm
, &create_tce_64
);
2304 #ifdef CONFIG_PPC_BOOK3S_64
2305 case KVM_PPC_GET_SMMU_INFO
: {
2306 struct kvm_ppc_smmu_info info
;
2307 struct kvm
*kvm
= filp
->private_data
;
2309 memset(&info
, 0, sizeof(info
));
2310 r
= kvm
->arch
.kvm_ops
->get_smmu_info(kvm
, &info
);
2311 if (r
>= 0 && copy_to_user(argp
, &info
, sizeof(info
)))
2315 case KVM_PPC_RTAS_DEFINE_TOKEN
: {
2316 struct kvm
*kvm
= filp
->private_data
;
2318 r
= kvm_vm_ioctl_rtas_define_token(kvm
, argp
);
2321 case KVM_PPC_CONFIGURE_V3_MMU
: {
2322 struct kvm
*kvm
= filp
->private_data
;
2323 struct kvm_ppc_mmuv3_cfg cfg
;
2326 if (!kvm
->arch
.kvm_ops
->configure_mmu
)
2329 if (copy_from_user(&cfg
, argp
, sizeof(cfg
)))
2331 r
= kvm
->arch
.kvm_ops
->configure_mmu(kvm
, &cfg
);
2334 case KVM_PPC_GET_RMMU_INFO
: {
2335 struct kvm
*kvm
= filp
->private_data
;
2336 struct kvm_ppc_rmmu_info info
;
2339 if (!kvm
->arch
.kvm_ops
->get_rmmu_info
)
2341 r
= kvm
->arch
.kvm_ops
->get_rmmu_info(kvm
, &info
);
2342 if (r
>= 0 && copy_to_user(argp
, &info
, sizeof(info
)))
2346 case KVM_PPC_GET_CPU_CHAR
: {
2347 struct kvm_ppc_cpu_char cpuchar
;
2349 r
= kvmppc_get_cpu_char(&cpuchar
);
2350 if (r
>= 0 && copy_to_user(argp
, &cpuchar
, sizeof(cpuchar
)))
2355 struct kvm
*kvm
= filp
->private_data
;
2356 r
= kvm
->arch
.kvm_ops
->arch_vm_ioctl(filp
, ioctl
, arg
);
2358 #else /* CONFIG_PPC_BOOK3S_64 */
2367 static unsigned long lpid_inuse
[BITS_TO_LONGS(KVMPPC_NR_LPIDS
)];
2368 static unsigned long nr_lpids
;
2370 long kvmppc_alloc_lpid(void)
2375 lpid
= find_first_zero_bit(lpid_inuse
, KVMPPC_NR_LPIDS
);
2376 if (lpid
>= nr_lpids
) {
2377 pr_err("%s: No LPIDs free\n", __func__
);
2380 } while (test_and_set_bit(lpid
, lpid_inuse
));
2384 EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid
);
2386 void kvmppc_claim_lpid(long lpid
)
2388 set_bit(lpid
, lpid_inuse
);
2390 EXPORT_SYMBOL_GPL(kvmppc_claim_lpid
);
2392 void kvmppc_free_lpid(long lpid
)
2394 clear_bit(lpid
, lpid_inuse
);
2396 EXPORT_SYMBOL_GPL(kvmppc_free_lpid
);
2398 void kvmppc_init_lpid(unsigned long nr_lpids_param
)
2400 nr_lpids
= min_t(unsigned long, KVMPPC_NR_LPIDS
, nr_lpids_param
);
2401 memset(lpid_inuse
, 0, sizeof(lpid_inuse
));
2403 EXPORT_SYMBOL_GPL(kvmppc_init_lpid
);
2405 int kvm_arch_init(void *opaque
)
2410 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr
);