xref: /linux/arch/arm64/kvm/arm.c (revision 2f6a49bbc01da17867c26f6f650b1142e1d7c69d)
19ed24f4bSMarc Zyngier // SPDX-License-Identifier: GPL-2.0-only
29ed24f4bSMarc Zyngier /*
39ed24f4bSMarc Zyngier  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
49ed24f4bSMarc Zyngier  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
59ed24f4bSMarc Zyngier  */
69ed24f4bSMarc Zyngier 
79ed24f4bSMarc Zyngier #include <linux/bug.h>
89ed24f4bSMarc Zyngier #include <linux/cpu_pm.h>
99ed24f4bSMarc Zyngier #include <linux/errno.h>
109ed24f4bSMarc Zyngier #include <linux/err.h>
119ed24f4bSMarc Zyngier #include <linux/kvm_host.h>
129ed24f4bSMarc Zyngier #include <linux/list.h>
139ed24f4bSMarc Zyngier #include <linux/module.h>
149ed24f4bSMarc Zyngier #include <linux/vmalloc.h>
159ed24f4bSMarc Zyngier #include <linux/fs.h>
169ed24f4bSMarc Zyngier #include <linux/mman.h>
179ed24f4bSMarc Zyngier #include <linux/sched.h>
189ed24f4bSMarc Zyngier #include <linux/kvm.h>
199ed24f4bSMarc Zyngier #include <linux/kvm_irqfd.h>
209ed24f4bSMarc Zyngier #include <linux/irqbypass.h>
219ed24f4bSMarc Zyngier #include <linux/sched/stat.h>
22eeeee719SDavid Brazdil #include <linux/psci.h>
239ed24f4bSMarc Zyngier #include <trace/events/kvm.h>
249ed24f4bSMarc Zyngier 
259ed24f4bSMarc Zyngier #define CREATE_TRACE_POINTS
269ed24f4bSMarc Zyngier #include "trace_arm.h"
279ed24f4bSMarc Zyngier 
289ed24f4bSMarc Zyngier #include <linux/uaccess.h>
299ed24f4bSMarc Zyngier #include <asm/ptrace.h>
309ed24f4bSMarc Zyngier #include <asm/mman.h>
319ed24f4bSMarc Zyngier #include <asm/tlbflush.h>
329ed24f4bSMarc Zyngier #include <asm/cacheflush.h>
339ed24f4bSMarc Zyngier #include <asm/cpufeature.h>
349ed24f4bSMarc Zyngier #include <asm/virt.h>
359ed24f4bSMarc Zyngier #include <asm/kvm_arm.h>
369ed24f4bSMarc Zyngier #include <asm/kvm_asm.h>
379ed24f4bSMarc Zyngier #include <asm/kvm_mmu.h>
389ed24f4bSMarc Zyngier #include <asm/kvm_emulate.h>
399ed24f4bSMarc Zyngier #include <asm/sections.h>
409ed24f4bSMarc Zyngier 
419ed24f4bSMarc Zyngier #include <kvm/arm_hypercalls.h>
429ed24f4bSMarc Zyngier #include <kvm/arm_pmu.h>
439ed24f4bSMarc Zyngier #include <kvm/arm_psci.h>
449ed24f4bSMarc Zyngier 
459ed24f4bSMarc Zyngier #ifdef REQUIRES_VIRT
469ed24f4bSMarc Zyngier __asm__(".arch_extension	virt");
479ed24f4bSMarc Zyngier #endif
489ed24f4bSMarc Zyngier 
49d8b369c4SDavid Brazdil static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
50f19f6644SDavid Brazdil DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
51d8b369c4SDavid Brazdil 
5214ef9d04SMarc Zyngier DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
5314ef9d04SMarc Zyngier 
549ed24f4bSMarc Zyngier static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
5530c95391SDavid Brazdil unsigned long kvm_arm_hyp_percpu_base[NR_CPUS];
5663fec243SDavid Brazdil DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
579ed24f4bSMarc Zyngier 
589ed24f4bSMarc Zyngier /* The VMID used in the VTTBR */
599ed24f4bSMarc Zyngier static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
609ed24f4bSMarc Zyngier static u32 kvm_next_vmid;
619ed24f4bSMarc Zyngier static DEFINE_SPINLOCK(kvm_vmid_lock);
629ed24f4bSMarc Zyngier 
639ed24f4bSMarc Zyngier static bool vgic_present;
649ed24f4bSMarc Zyngier 
659ed24f4bSMarc Zyngier static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
669ed24f4bSMarc Zyngier DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
679ed24f4bSMarc Zyngier 
689ed24f4bSMarc Zyngier int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
699ed24f4bSMarc Zyngier {
709ed24f4bSMarc Zyngier 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
719ed24f4bSMarc Zyngier }
729ed24f4bSMarc Zyngier 
739ed24f4bSMarc Zyngier int kvm_arch_hardware_setup(void *opaque)
749ed24f4bSMarc Zyngier {
759ed24f4bSMarc Zyngier 	return 0;
769ed24f4bSMarc Zyngier }
779ed24f4bSMarc Zyngier 
789ed24f4bSMarc Zyngier int kvm_arch_check_processor_compat(void *opaque)
799ed24f4bSMarc Zyngier {
809ed24f4bSMarc Zyngier 	return 0;
819ed24f4bSMarc Zyngier }
829ed24f4bSMarc Zyngier 
839ed24f4bSMarc Zyngier int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
849ed24f4bSMarc Zyngier 			    struct kvm_enable_cap *cap)
859ed24f4bSMarc Zyngier {
869ed24f4bSMarc Zyngier 	int r;
879ed24f4bSMarc Zyngier 
889ed24f4bSMarc Zyngier 	if (cap->flags)
899ed24f4bSMarc Zyngier 		return -EINVAL;
909ed24f4bSMarc Zyngier 
919ed24f4bSMarc Zyngier 	switch (cap->cap) {
929ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
939ed24f4bSMarc Zyngier 		r = 0;
949ed24f4bSMarc Zyngier 		kvm->arch.return_nisv_io_abort_to_user = true;
959ed24f4bSMarc Zyngier 		break;
969ed24f4bSMarc Zyngier 	default:
979ed24f4bSMarc Zyngier 		r = -EINVAL;
989ed24f4bSMarc Zyngier 		break;
999ed24f4bSMarc Zyngier 	}
1009ed24f4bSMarc Zyngier 
1019ed24f4bSMarc Zyngier 	return r;
1029ed24f4bSMarc Zyngier }
1039ed24f4bSMarc Zyngier 
1045107000fSMarc Zyngier static int kvm_arm_default_max_vcpus(void)
1055107000fSMarc Zyngier {
1065107000fSMarc Zyngier 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
1075107000fSMarc Zyngier }
1085107000fSMarc Zyngier 
1094f1df628SMarc Zyngier static void set_default_spectre(struct kvm *kvm)
11023711a5eSMarc Zyngier {
11123711a5eSMarc Zyngier 	/*
11223711a5eSMarc Zyngier 	 * The default is to expose CSV2 == 1 if the HW isn't affected.
11323711a5eSMarc Zyngier 	 * Although this is a per-CPU feature, we make it global because
11423711a5eSMarc Zyngier 	 * asymmetric systems are just a nuisance.
11523711a5eSMarc Zyngier 	 *
11623711a5eSMarc Zyngier 	 * Userspace can override this as long as it doesn't promise
11723711a5eSMarc Zyngier 	 * the impossible.
11823711a5eSMarc Zyngier 	 */
11923711a5eSMarc Zyngier 	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
12023711a5eSMarc Zyngier 		kvm->arch.pfr0_csv2 = 1;
1214f1df628SMarc Zyngier 	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
1224f1df628SMarc Zyngier 		kvm->arch.pfr0_csv3 = 1;
12323711a5eSMarc Zyngier }
12423711a5eSMarc Zyngier 
1259ed24f4bSMarc Zyngier /**
1269ed24f4bSMarc Zyngier  * kvm_arch_init_vm - initializes a VM data structure
1279ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1289ed24f4bSMarc Zyngier  */
1299ed24f4bSMarc Zyngier int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1309ed24f4bSMarc Zyngier {
131a0e50aa3SChristoffer Dall 	int ret;
1329ed24f4bSMarc Zyngier 
1339ed24f4bSMarc Zyngier 	ret = kvm_arm_setup_stage2(kvm, type);
1349ed24f4bSMarc Zyngier 	if (ret)
1359ed24f4bSMarc Zyngier 		return ret;
1369ed24f4bSMarc Zyngier 
137a0e50aa3SChristoffer Dall 	ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu);
1389ed24f4bSMarc Zyngier 	if (ret)
139a0e50aa3SChristoffer Dall 		return ret;
1409ed24f4bSMarc Zyngier 
1419ed24f4bSMarc Zyngier 	ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
1429ed24f4bSMarc Zyngier 	if (ret)
1439ed24f4bSMarc Zyngier 		goto out_free_stage2_pgd;
1449ed24f4bSMarc Zyngier 
1459ed24f4bSMarc Zyngier 	kvm_vgic_early_init(kvm);
1469ed24f4bSMarc Zyngier 
1479ed24f4bSMarc Zyngier 	/* The maximum number of VCPUs is limited by the host's GIC model */
1485107000fSMarc Zyngier 	kvm->arch.max_vcpus = kvm_arm_default_max_vcpus();
1499ed24f4bSMarc Zyngier 
1504f1df628SMarc Zyngier 	set_default_spectre(kvm);
15123711a5eSMarc Zyngier 
1529ed24f4bSMarc Zyngier 	return ret;
1539ed24f4bSMarc Zyngier out_free_stage2_pgd:
154a0e50aa3SChristoffer Dall 	kvm_free_stage2_pgd(&kvm->arch.mmu);
1559ed24f4bSMarc Zyngier 	return ret;
1569ed24f4bSMarc Zyngier }
1579ed24f4bSMarc Zyngier 
1589ed24f4bSMarc Zyngier vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1599ed24f4bSMarc Zyngier {
1609ed24f4bSMarc Zyngier 	return VM_FAULT_SIGBUS;
1619ed24f4bSMarc Zyngier }
1629ed24f4bSMarc Zyngier 
1639ed24f4bSMarc Zyngier 
1649ed24f4bSMarc Zyngier /**
1659ed24f4bSMarc Zyngier  * kvm_arch_destroy_vm - destroy the VM data structure
1669ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1679ed24f4bSMarc Zyngier  */
1689ed24f4bSMarc Zyngier void kvm_arch_destroy_vm(struct kvm *kvm)
1699ed24f4bSMarc Zyngier {
1709ed24f4bSMarc Zyngier 	int i;
1719ed24f4bSMarc Zyngier 
172d7eec236SMarc Zyngier 	bitmap_free(kvm->arch.pmu_filter);
173d7eec236SMarc Zyngier 
1749ed24f4bSMarc Zyngier 	kvm_vgic_destroy(kvm);
1759ed24f4bSMarc Zyngier 
1769ed24f4bSMarc Zyngier 	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
1779ed24f4bSMarc Zyngier 		if (kvm->vcpus[i]) {
1789ed24f4bSMarc Zyngier 			kvm_vcpu_destroy(kvm->vcpus[i]);
1799ed24f4bSMarc Zyngier 			kvm->vcpus[i] = NULL;
1809ed24f4bSMarc Zyngier 		}
1819ed24f4bSMarc Zyngier 	}
1829ed24f4bSMarc Zyngier 	atomic_set(&kvm->online_vcpus, 0);
1839ed24f4bSMarc Zyngier }
1849ed24f4bSMarc Zyngier 
1859ed24f4bSMarc Zyngier int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
1869ed24f4bSMarc Zyngier {
1879ed24f4bSMarc Zyngier 	int r;
1889ed24f4bSMarc Zyngier 	switch (ext) {
1899ed24f4bSMarc Zyngier 	case KVM_CAP_IRQCHIP:
1909ed24f4bSMarc Zyngier 		r = vgic_present;
1919ed24f4bSMarc Zyngier 		break;
1929ed24f4bSMarc Zyngier 	case KVM_CAP_IOEVENTFD:
1939ed24f4bSMarc Zyngier 	case KVM_CAP_DEVICE_CTRL:
1949ed24f4bSMarc Zyngier 	case KVM_CAP_USER_MEMORY:
1959ed24f4bSMarc Zyngier 	case KVM_CAP_SYNC_MMU:
1969ed24f4bSMarc Zyngier 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
1979ed24f4bSMarc Zyngier 	case KVM_CAP_ONE_REG:
1989ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI:
1999ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI_0_2:
2009ed24f4bSMarc Zyngier 	case KVM_CAP_READONLY_MEM:
2019ed24f4bSMarc Zyngier 	case KVM_CAP_MP_STATE:
2029ed24f4bSMarc Zyngier 	case KVM_CAP_IMMEDIATE_EXIT:
2039ed24f4bSMarc Zyngier 	case KVM_CAP_VCPU_EVENTS:
2049ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
2059ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
2069ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_INJECT_EXT_DABT:
20736fb4cd5SWill Deacon 	case KVM_CAP_SET_GUEST_DEBUG:
20836fb4cd5SWill Deacon 	case KVM_CAP_VCPU_ATTRIBUTES:
2093bf72569SJianyong Wu 	case KVM_CAP_PTP_KVM:
2109ed24f4bSMarc Zyngier 		r = 1;
2119ed24f4bSMarc Zyngier 		break;
212fa18aca9SMaxim Levitsky 	case KVM_CAP_SET_GUEST_DEBUG2:
213fa18aca9SMaxim Levitsky 		return KVM_GUESTDBG_VALID_MASK;
2149ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
2159ed24f4bSMarc Zyngier 		r = 1;
2169ed24f4bSMarc Zyngier 		break;
2179ed24f4bSMarc Zyngier 	case KVM_CAP_NR_VCPUS:
2189ed24f4bSMarc Zyngier 		r = num_online_cpus();
2199ed24f4bSMarc Zyngier 		break;
2209ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPUS:
2219ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPU_ID:
2225107000fSMarc Zyngier 		if (kvm)
2235107000fSMarc Zyngier 			r = kvm->arch.max_vcpus;
2245107000fSMarc Zyngier 		else
2255107000fSMarc Zyngier 			r = kvm_arm_default_max_vcpus();
2269ed24f4bSMarc Zyngier 		break;
2279ed24f4bSMarc Zyngier 	case KVM_CAP_MSI_DEVID:
2289ed24f4bSMarc Zyngier 		if (!kvm)
2299ed24f4bSMarc Zyngier 			r = -EINVAL;
2309ed24f4bSMarc Zyngier 		else
2319ed24f4bSMarc Zyngier 			r = kvm->arch.vgic.msis_require_devid;
2329ed24f4bSMarc Zyngier 		break;
2339ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_USER_IRQ:
2349ed24f4bSMarc Zyngier 		/*
2359ed24f4bSMarc Zyngier 		 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
2369ed24f4bSMarc Zyngier 		 * (bump this number if adding more devices)
2379ed24f4bSMarc Zyngier 		 */
2389ed24f4bSMarc Zyngier 		r = 1;
2399ed24f4bSMarc Zyngier 		break;
240004a0124SAndrew Jones 	case KVM_CAP_STEAL_TIME:
241004a0124SAndrew Jones 		r = kvm_arm_pvtime_supported();
242004a0124SAndrew Jones 		break;
24336fb4cd5SWill Deacon 	case KVM_CAP_ARM_EL1_32BIT:
24436fb4cd5SWill Deacon 		r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
2459ed24f4bSMarc Zyngier 		break;
24636fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
24736fb4cd5SWill Deacon 		r = get_num_brps();
24836fb4cd5SWill Deacon 		break;
24936fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
25036fb4cd5SWill Deacon 		r = get_num_wrps();
25136fb4cd5SWill Deacon 		break;
25236fb4cd5SWill Deacon 	case KVM_CAP_ARM_PMU_V3:
25336fb4cd5SWill Deacon 		r = kvm_arm_support_pmu_v3();
25436fb4cd5SWill Deacon 		break;
25536fb4cd5SWill Deacon 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
25636fb4cd5SWill Deacon 		r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
25736fb4cd5SWill Deacon 		break;
25836fb4cd5SWill Deacon 	case KVM_CAP_ARM_VM_IPA_SIZE:
25936fb4cd5SWill Deacon 		r = get_kvm_ipa_limit();
26036fb4cd5SWill Deacon 		break;
26136fb4cd5SWill Deacon 	case KVM_CAP_ARM_SVE:
26236fb4cd5SWill Deacon 		r = system_supports_sve();
26336fb4cd5SWill Deacon 		break;
26436fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
26536fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
26636fb4cd5SWill Deacon 		r = system_has_full_ptr_auth();
26736fb4cd5SWill Deacon 		break;
26836fb4cd5SWill Deacon 	default:
26936fb4cd5SWill Deacon 		r = 0;
2709ed24f4bSMarc Zyngier 	}
27136fb4cd5SWill Deacon 
2729ed24f4bSMarc Zyngier 	return r;
2739ed24f4bSMarc Zyngier }
2749ed24f4bSMarc Zyngier 
2759ed24f4bSMarc Zyngier long kvm_arch_dev_ioctl(struct file *filp,
2769ed24f4bSMarc Zyngier 			unsigned int ioctl, unsigned long arg)
2779ed24f4bSMarc Zyngier {
2789ed24f4bSMarc Zyngier 	return -EINVAL;
2799ed24f4bSMarc Zyngier }
2809ed24f4bSMarc Zyngier 
2819ed24f4bSMarc Zyngier struct kvm *kvm_arch_alloc_vm(void)
2829ed24f4bSMarc Zyngier {
2839ed24f4bSMarc Zyngier 	if (!has_vhe())
2849ed24f4bSMarc Zyngier 		return kzalloc(sizeof(struct kvm), GFP_KERNEL);
2859ed24f4bSMarc Zyngier 
2869ed24f4bSMarc Zyngier 	return vzalloc(sizeof(struct kvm));
2879ed24f4bSMarc Zyngier }
2889ed24f4bSMarc Zyngier 
2899ed24f4bSMarc Zyngier void kvm_arch_free_vm(struct kvm *kvm)
2909ed24f4bSMarc Zyngier {
2919ed24f4bSMarc Zyngier 	if (!has_vhe())
2929ed24f4bSMarc Zyngier 		kfree(kvm);
2939ed24f4bSMarc Zyngier 	else
2949ed24f4bSMarc Zyngier 		vfree(kvm);
2959ed24f4bSMarc Zyngier }
2969ed24f4bSMarc Zyngier 
2979ed24f4bSMarc Zyngier int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
2989ed24f4bSMarc Zyngier {
2999ed24f4bSMarc Zyngier 	if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
3009ed24f4bSMarc Zyngier 		return -EBUSY;
3019ed24f4bSMarc Zyngier 
3029ed24f4bSMarc Zyngier 	if (id >= kvm->arch.max_vcpus)
3039ed24f4bSMarc Zyngier 		return -EINVAL;
3049ed24f4bSMarc Zyngier 
3059ed24f4bSMarc Zyngier 	return 0;
3069ed24f4bSMarc Zyngier }
3079ed24f4bSMarc Zyngier 
3089ed24f4bSMarc Zyngier int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3099ed24f4bSMarc Zyngier {
3109ed24f4bSMarc Zyngier 	int err;
3119ed24f4bSMarc Zyngier 
3129ed24f4bSMarc Zyngier 	/* Force users to call KVM_ARM_VCPU_INIT */
3139ed24f4bSMarc Zyngier 	vcpu->arch.target = -1;
3149ed24f4bSMarc Zyngier 	bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
3159ed24f4bSMarc Zyngier 
316e539451bSSean Christopherson 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
317e539451bSSean Christopherson 
3189ed24f4bSMarc Zyngier 	/* Set up the timer */
3199ed24f4bSMarc Zyngier 	kvm_timer_vcpu_init(vcpu);
3209ed24f4bSMarc Zyngier 
3219ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_init(vcpu);
3229ed24f4bSMarc Zyngier 
3239ed24f4bSMarc Zyngier 	kvm_arm_reset_debug_ptr(vcpu);
3249ed24f4bSMarc Zyngier 
3259ed24f4bSMarc Zyngier 	kvm_arm_pvtime_vcpu_init(&vcpu->arch);
3269ed24f4bSMarc Zyngier 
327a0e50aa3SChristoffer Dall 	vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
328a0e50aa3SChristoffer Dall 
3299ed24f4bSMarc Zyngier 	err = kvm_vgic_vcpu_init(vcpu);
3309ed24f4bSMarc Zyngier 	if (err)
3319ed24f4bSMarc Zyngier 		return err;
3329ed24f4bSMarc Zyngier 
3339ed24f4bSMarc Zyngier 	return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
3349ed24f4bSMarc Zyngier }
3359ed24f4bSMarc Zyngier 
3369ed24f4bSMarc Zyngier void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
3379ed24f4bSMarc Zyngier {
3389ed24f4bSMarc Zyngier }
3399ed24f4bSMarc Zyngier 
3409ed24f4bSMarc Zyngier void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3419ed24f4bSMarc Zyngier {
3429ed24f4bSMarc Zyngier 	if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
3439ed24f4bSMarc Zyngier 		static_branch_dec(&userspace_irqchip_in_use);
3449ed24f4bSMarc Zyngier 
3459af3e08bSWill Deacon 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
3469ed24f4bSMarc Zyngier 	kvm_timer_vcpu_terminate(vcpu);
3479ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_destroy(vcpu);
3489ed24f4bSMarc Zyngier 
3499ed24f4bSMarc Zyngier 	kvm_arm_vcpu_destroy(vcpu);
3509ed24f4bSMarc Zyngier }
3519ed24f4bSMarc Zyngier 
3529ed24f4bSMarc Zyngier int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
3539ed24f4bSMarc Zyngier {
3549ed24f4bSMarc Zyngier 	return kvm_timer_is_pending(vcpu);
3559ed24f4bSMarc Zyngier }
3569ed24f4bSMarc Zyngier 
3579ed24f4bSMarc Zyngier void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
3589ed24f4bSMarc Zyngier {
3599ed24f4bSMarc Zyngier 	/*
3609ed24f4bSMarc Zyngier 	 * If we're about to block (most likely because we've just hit a
3619ed24f4bSMarc Zyngier 	 * WFI), we need to sync back the state of the GIC CPU interface
3629ed24f4bSMarc Zyngier 	 * so that we have the latest PMR and group enables. This ensures
3639ed24f4bSMarc Zyngier 	 * that kvm_arch_vcpu_runnable has up-to-date data to decide
3649ed24f4bSMarc Zyngier 	 * whether we have pending interrupts.
3659ed24f4bSMarc Zyngier 	 *
3669ed24f4bSMarc Zyngier 	 * For the same reason, we want to tell GICv4 that we need
3679ed24f4bSMarc Zyngier 	 * doorbells to be signalled, should an interrupt become pending.
3689ed24f4bSMarc Zyngier 	 */
3699ed24f4bSMarc Zyngier 	preempt_disable();
3709ed24f4bSMarc Zyngier 	kvm_vgic_vmcr_sync(vcpu);
3719ed24f4bSMarc Zyngier 	vgic_v4_put(vcpu, true);
3729ed24f4bSMarc Zyngier 	preempt_enable();
3739ed24f4bSMarc Zyngier }
3749ed24f4bSMarc Zyngier 
3759ed24f4bSMarc Zyngier void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
3769ed24f4bSMarc Zyngier {
3779ed24f4bSMarc Zyngier 	preempt_disable();
3789ed24f4bSMarc Zyngier 	vgic_v4_load(vcpu);
3799ed24f4bSMarc Zyngier 	preempt_enable();
3809ed24f4bSMarc Zyngier }
3819ed24f4bSMarc Zyngier 
3829ed24f4bSMarc Zyngier void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3839ed24f4bSMarc Zyngier {
384a0e50aa3SChristoffer Dall 	struct kvm_s2_mmu *mmu;
3859ed24f4bSMarc Zyngier 	int *last_ran;
3869ed24f4bSMarc Zyngier 
387a0e50aa3SChristoffer Dall 	mmu = vcpu->arch.hw_mmu;
388a0e50aa3SChristoffer Dall 	last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
3899ed24f4bSMarc Zyngier 
3909ed24f4bSMarc Zyngier 	/*
39101dc9262SMarc Zyngier 	 * We guarantee that both TLBs and I-cache are private to each
39201dc9262SMarc Zyngier 	 * vcpu. If detecting that a vcpu from the same VM has
39301dc9262SMarc Zyngier 	 * previously run on the same physical CPU, call into the
39401dc9262SMarc Zyngier 	 * hypervisor code to nuke the relevant contexts.
39501dc9262SMarc Zyngier 	 *
3969ed24f4bSMarc Zyngier 	 * We might get preempted before the vCPU actually runs, but
3979ed24f4bSMarc Zyngier 	 * over-invalidation doesn't affect correctness.
3989ed24f4bSMarc Zyngier 	 */
3999ed24f4bSMarc Zyngier 	if (*last_ran != vcpu->vcpu_id) {
40001dc9262SMarc Zyngier 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
4019ed24f4bSMarc Zyngier 		*last_ran = vcpu->vcpu_id;
4029ed24f4bSMarc Zyngier 	}
4039ed24f4bSMarc Zyngier 
4049ed24f4bSMarc Zyngier 	vcpu->cpu = cpu;
4059ed24f4bSMarc Zyngier 
4069ed24f4bSMarc Zyngier 	kvm_vgic_load(vcpu);
4079ed24f4bSMarc Zyngier 	kvm_timer_vcpu_load(vcpu);
40813aeb9b4SDavid Brazdil 	if (has_vhe())
40913aeb9b4SDavid Brazdil 		kvm_vcpu_load_sysregs_vhe(vcpu);
4109ed24f4bSMarc Zyngier 	kvm_arch_vcpu_load_fp(vcpu);
4119ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_guest(vcpu);
4129ed24f4bSMarc Zyngier 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
4139ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
4149ed24f4bSMarc Zyngier 
4159ed24f4bSMarc Zyngier 	if (single_task_running())
4169ed24f4bSMarc Zyngier 		vcpu_clear_wfx_traps(vcpu);
4179ed24f4bSMarc Zyngier 	else
4189ed24f4bSMarc Zyngier 		vcpu_set_wfx_traps(vcpu);
4199ed24f4bSMarc Zyngier 
42029eb5a3cSMarc Zyngier 	if (vcpu_has_ptrauth(vcpu))
421ef3e40a7SMarc Zyngier 		vcpu_ptrauth_disable(vcpu);
422d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_load_debug_state_flags(vcpu);
4239ed24f4bSMarc Zyngier }
4249ed24f4bSMarc Zyngier 
4259ed24f4bSMarc Zyngier void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4269ed24f4bSMarc Zyngier {
427d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_put_debug_state_flags(vcpu);
4289ed24f4bSMarc Zyngier 	kvm_arch_vcpu_put_fp(vcpu);
42913aeb9b4SDavid Brazdil 	if (has_vhe())
43013aeb9b4SDavid Brazdil 		kvm_vcpu_put_sysregs_vhe(vcpu);
4319ed24f4bSMarc Zyngier 	kvm_timer_vcpu_put(vcpu);
4329ed24f4bSMarc Zyngier 	kvm_vgic_put(vcpu);
4339ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_host(vcpu);
4349ed24f4bSMarc Zyngier 
4359ed24f4bSMarc Zyngier 	vcpu->cpu = -1;
4369ed24f4bSMarc Zyngier }
4379ed24f4bSMarc Zyngier 
4389ed24f4bSMarc Zyngier static void vcpu_power_off(struct kvm_vcpu *vcpu)
4399ed24f4bSMarc Zyngier {
4409ed24f4bSMarc Zyngier 	vcpu->arch.power_off = true;
4419ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
4429ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
4439ed24f4bSMarc Zyngier }
4449ed24f4bSMarc Zyngier 
4459ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4469ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
4479ed24f4bSMarc Zyngier {
4489ed24f4bSMarc Zyngier 	if (vcpu->arch.power_off)
4499ed24f4bSMarc Zyngier 		mp_state->mp_state = KVM_MP_STATE_STOPPED;
4509ed24f4bSMarc Zyngier 	else
4519ed24f4bSMarc Zyngier 		mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
4529ed24f4bSMarc Zyngier 
4539ed24f4bSMarc Zyngier 	return 0;
4549ed24f4bSMarc Zyngier }
4559ed24f4bSMarc Zyngier 
4569ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4579ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
4589ed24f4bSMarc Zyngier {
4599ed24f4bSMarc Zyngier 	int ret = 0;
4609ed24f4bSMarc Zyngier 
4619ed24f4bSMarc Zyngier 	switch (mp_state->mp_state) {
4629ed24f4bSMarc Zyngier 	case KVM_MP_STATE_RUNNABLE:
4639ed24f4bSMarc Zyngier 		vcpu->arch.power_off = false;
4649ed24f4bSMarc Zyngier 		break;
4659ed24f4bSMarc Zyngier 	case KVM_MP_STATE_STOPPED:
4669ed24f4bSMarc Zyngier 		vcpu_power_off(vcpu);
4679ed24f4bSMarc Zyngier 		break;
4689ed24f4bSMarc Zyngier 	default:
4699ed24f4bSMarc Zyngier 		ret = -EINVAL;
4709ed24f4bSMarc Zyngier 	}
4719ed24f4bSMarc Zyngier 
4729ed24f4bSMarc Zyngier 	return ret;
4739ed24f4bSMarc Zyngier }
4749ed24f4bSMarc Zyngier 
4759ed24f4bSMarc Zyngier /**
4769ed24f4bSMarc Zyngier  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
4779ed24f4bSMarc Zyngier  * @v:		The VCPU pointer
4789ed24f4bSMarc Zyngier  *
4799ed24f4bSMarc Zyngier  * If the guest CPU is not waiting for interrupts or an interrupt line is
4809ed24f4bSMarc Zyngier  * asserted, the CPU is by definition runnable.
4819ed24f4bSMarc Zyngier  */
4829ed24f4bSMarc Zyngier int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
4839ed24f4bSMarc Zyngier {
4849ed24f4bSMarc Zyngier 	bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
4859ed24f4bSMarc Zyngier 	return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
4869ed24f4bSMarc Zyngier 		&& !v->arch.power_off && !v->arch.pause);
4879ed24f4bSMarc Zyngier }
4889ed24f4bSMarc Zyngier 
4899ed24f4bSMarc Zyngier bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
4909ed24f4bSMarc Zyngier {
4919ed24f4bSMarc Zyngier 	return vcpu_mode_priv(vcpu);
4929ed24f4bSMarc Zyngier }
4939ed24f4bSMarc Zyngier 
4949ed24f4bSMarc Zyngier /* Just ensure a guest exit from a particular CPU */
4959ed24f4bSMarc Zyngier static void exit_vm_noop(void *info)
4969ed24f4bSMarc Zyngier {
4979ed24f4bSMarc Zyngier }
4989ed24f4bSMarc Zyngier 
4999ed24f4bSMarc Zyngier void force_vm_exit(const cpumask_t *mask)
5009ed24f4bSMarc Zyngier {
5019ed24f4bSMarc Zyngier 	preempt_disable();
5029ed24f4bSMarc Zyngier 	smp_call_function_many(mask, exit_vm_noop, NULL, true);
5039ed24f4bSMarc Zyngier 	preempt_enable();
5049ed24f4bSMarc Zyngier }
5059ed24f4bSMarc Zyngier 
5069ed24f4bSMarc Zyngier /**
5079ed24f4bSMarc Zyngier  * need_new_vmid_gen - check that the VMID is still valid
5089ed24f4bSMarc Zyngier  * @vmid: The VMID to check
5099ed24f4bSMarc Zyngier  *
5109ed24f4bSMarc Zyngier  * return true if there is a new generation of VMIDs being used
5119ed24f4bSMarc Zyngier  *
5129ed24f4bSMarc Zyngier  * The hardware supports a limited set of values with the value zero reserved
5139ed24f4bSMarc Zyngier  * for the host, so we check if an assigned value belongs to a previous
514656012c7SFuad Tabba  * generation, which requires us to assign a new value. If we're the first to
515656012c7SFuad Tabba  * use a VMID for the new generation, we must flush necessary caches and TLBs
516656012c7SFuad Tabba  * on all CPUs.
5179ed24f4bSMarc Zyngier  */
5189ed24f4bSMarc Zyngier static bool need_new_vmid_gen(struct kvm_vmid *vmid)
5199ed24f4bSMarc Zyngier {
5209ed24f4bSMarc Zyngier 	u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
5219ed24f4bSMarc Zyngier 	smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
5229ed24f4bSMarc Zyngier 	return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
5239ed24f4bSMarc Zyngier }
5249ed24f4bSMarc Zyngier 
5259ed24f4bSMarc Zyngier /**
5269ed24f4bSMarc Zyngier  * update_vmid - Update the vmid with a valid VMID for the current generation
5279ed24f4bSMarc Zyngier  * @vmid: The stage-2 VMID information struct
5289ed24f4bSMarc Zyngier  */
5299ed24f4bSMarc Zyngier static void update_vmid(struct kvm_vmid *vmid)
5309ed24f4bSMarc Zyngier {
5319ed24f4bSMarc Zyngier 	if (!need_new_vmid_gen(vmid))
5329ed24f4bSMarc Zyngier 		return;
5339ed24f4bSMarc Zyngier 
5349ed24f4bSMarc Zyngier 	spin_lock(&kvm_vmid_lock);
5359ed24f4bSMarc Zyngier 
5369ed24f4bSMarc Zyngier 	/*
5379ed24f4bSMarc Zyngier 	 * We need to re-check the vmid_gen here to ensure that if another vcpu
5389ed24f4bSMarc Zyngier 	 * already allocated a valid vmid for this vm, then this vcpu should
5399ed24f4bSMarc Zyngier 	 * use the same vmid.
5409ed24f4bSMarc Zyngier 	 */
5419ed24f4bSMarc Zyngier 	if (!need_new_vmid_gen(vmid)) {
5429ed24f4bSMarc Zyngier 		spin_unlock(&kvm_vmid_lock);
5439ed24f4bSMarc Zyngier 		return;
5449ed24f4bSMarc Zyngier 	}
5459ed24f4bSMarc Zyngier 
5469ed24f4bSMarc Zyngier 	/* First user of a new VMID generation? */
5479ed24f4bSMarc Zyngier 	if (unlikely(kvm_next_vmid == 0)) {
5489ed24f4bSMarc Zyngier 		atomic64_inc(&kvm_vmid_gen);
5499ed24f4bSMarc Zyngier 		kvm_next_vmid = 1;
5509ed24f4bSMarc Zyngier 
5519ed24f4bSMarc Zyngier 		/*
5529ed24f4bSMarc Zyngier 		 * On SMP we know no other CPUs can use this CPU's or each
5539ed24f4bSMarc Zyngier 		 * other's VMID after force_vm_exit returns since the
5549ed24f4bSMarc Zyngier 		 * kvm_vmid_lock blocks them from reentry to the guest.
5559ed24f4bSMarc Zyngier 		 */
5569ed24f4bSMarc Zyngier 		force_vm_exit(cpu_all_mask);
5579ed24f4bSMarc Zyngier 		/*
5589ed24f4bSMarc Zyngier 		 * Now broadcast TLB + ICACHE invalidation over the inner
5599ed24f4bSMarc Zyngier 		 * shareable domain to make sure all data structures are
5609ed24f4bSMarc Zyngier 		 * clean.
5619ed24f4bSMarc Zyngier 		 */
5629ed24f4bSMarc Zyngier 		kvm_call_hyp(__kvm_flush_vm_context);
5639ed24f4bSMarc Zyngier 	}
5649ed24f4bSMarc Zyngier 
5659ed24f4bSMarc Zyngier 	vmid->vmid = kvm_next_vmid;
5669ed24f4bSMarc Zyngier 	kvm_next_vmid++;
5679ed24f4bSMarc Zyngier 	kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
5689ed24f4bSMarc Zyngier 
5699ed24f4bSMarc Zyngier 	smp_wmb();
5709ed24f4bSMarc Zyngier 	WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
5719ed24f4bSMarc Zyngier 
5729ed24f4bSMarc Zyngier 	spin_unlock(&kvm_vmid_lock);
5739ed24f4bSMarc Zyngier }
5749ed24f4bSMarc Zyngier 
5759ed24f4bSMarc Zyngier static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
5769ed24f4bSMarc Zyngier {
5779ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
5789ed24f4bSMarc Zyngier 	int ret = 0;
5799ed24f4bSMarc Zyngier 
5809ed24f4bSMarc Zyngier 	if (likely(vcpu->arch.has_run_once))
5819ed24f4bSMarc Zyngier 		return 0;
5829ed24f4bSMarc Zyngier 
5839ed24f4bSMarc Zyngier 	if (!kvm_arm_vcpu_is_finalized(vcpu))
5849ed24f4bSMarc Zyngier 		return -EPERM;
5859ed24f4bSMarc Zyngier 
5869ed24f4bSMarc Zyngier 	vcpu->arch.has_run_once = true;
5879ed24f4bSMarc Zyngier 
588263d6287SAlexandru Elisei 	kvm_arm_vcpu_init_debug(vcpu);
589263d6287SAlexandru Elisei 
5909ed24f4bSMarc Zyngier 	if (likely(irqchip_in_kernel(kvm))) {
5919ed24f4bSMarc Zyngier 		/*
5929ed24f4bSMarc Zyngier 		 * Map the VGIC hardware resources before running a vcpu the
5939ed24f4bSMarc Zyngier 		 * first time on this VM.
5949ed24f4bSMarc Zyngier 		 */
5959ed24f4bSMarc Zyngier 		ret = kvm_vgic_map_resources(kvm);
5969ed24f4bSMarc Zyngier 		if (ret)
5979ed24f4bSMarc Zyngier 			return ret;
5989ed24f4bSMarc Zyngier 	} else {
5999ed24f4bSMarc Zyngier 		/*
6009ed24f4bSMarc Zyngier 		 * Tell the rest of the code that there are userspace irqchip
6019ed24f4bSMarc Zyngier 		 * VMs in the wild.
6029ed24f4bSMarc Zyngier 		 */
6039ed24f4bSMarc Zyngier 		static_branch_inc(&userspace_irqchip_in_use);
6049ed24f4bSMarc Zyngier 	}
6059ed24f4bSMarc Zyngier 
6069ed24f4bSMarc Zyngier 	ret = kvm_timer_enable(vcpu);
6079ed24f4bSMarc Zyngier 	if (ret)
6089ed24f4bSMarc Zyngier 		return ret;
6099ed24f4bSMarc Zyngier 
6109ed24f4bSMarc Zyngier 	ret = kvm_arm_pmu_v3_enable(vcpu);
6119ed24f4bSMarc Zyngier 
6129ed24f4bSMarc Zyngier 	return ret;
6139ed24f4bSMarc Zyngier }
6149ed24f4bSMarc Zyngier 
6159ed24f4bSMarc Zyngier bool kvm_arch_intc_initialized(struct kvm *kvm)
6169ed24f4bSMarc Zyngier {
6179ed24f4bSMarc Zyngier 	return vgic_initialized(kvm);
6189ed24f4bSMarc Zyngier }
6199ed24f4bSMarc Zyngier 
6209ed24f4bSMarc Zyngier void kvm_arm_halt_guest(struct kvm *kvm)
6219ed24f4bSMarc Zyngier {
6229ed24f4bSMarc Zyngier 	int i;
6239ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
6249ed24f4bSMarc Zyngier 
6259ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm)
6269ed24f4bSMarc Zyngier 		vcpu->arch.pause = true;
6279ed24f4bSMarc Zyngier 	kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
6289ed24f4bSMarc Zyngier }
6299ed24f4bSMarc Zyngier 
6309ed24f4bSMarc Zyngier void kvm_arm_resume_guest(struct kvm *kvm)
6319ed24f4bSMarc Zyngier {
6329ed24f4bSMarc Zyngier 	int i;
6339ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
6349ed24f4bSMarc Zyngier 
6359ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
6369ed24f4bSMarc Zyngier 		vcpu->arch.pause = false;
63738060944SPaolo Bonzini 		rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu));
6389ed24f4bSMarc Zyngier 	}
6399ed24f4bSMarc Zyngier }
6409ed24f4bSMarc Zyngier 
6419ed24f4bSMarc Zyngier static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
6429ed24f4bSMarc Zyngier {
64338060944SPaolo Bonzini 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
6449ed24f4bSMarc Zyngier 
64538060944SPaolo Bonzini 	rcuwait_wait_event(wait,
64638060944SPaolo Bonzini 			   (!vcpu->arch.power_off) &&(!vcpu->arch.pause),
64738060944SPaolo Bonzini 			   TASK_INTERRUPTIBLE);
6489ed24f4bSMarc Zyngier 
6499ed24f4bSMarc Zyngier 	if (vcpu->arch.power_off || vcpu->arch.pause) {
6509ed24f4bSMarc Zyngier 		/* Awaken to handle a signal, request we sleep again later. */
6519ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_SLEEP, vcpu);
6529ed24f4bSMarc Zyngier 	}
6539ed24f4bSMarc Zyngier 
6549ed24f4bSMarc Zyngier 	/*
6559ed24f4bSMarc Zyngier 	 * Make sure we will observe a potential reset request if we've
6569ed24f4bSMarc Zyngier 	 * observed a change to the power state. Pairs with the smp_wmb() in
6579ed24f4bSMarc Zyngier 	 * kvm_psci_vcpu_on().
6589ed24f4bSMarc Zyngier 	 */
6599ed24f4bSMarc Zyngier 	smp_rmb();
6609ed24f4bSMarc Zyngier }
6619ed24f4bSMarc Zyngier 
6629ed24f4bSMarc Zyngier static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
6639ed24f4bSMarc Zyngier {
6649ed24f4bSMarc Zyngier 	return vcpu->arch.target >= 0;
6659ed24f4bSMarc Zyngier }
6669ed24f4bSMarc Zyngier 
6679ed24f4bSMarc Zyngier static void check_vcpu_requests(struct kvm_vcpu *vcpu)
6689ed24f4bSMarc Zyngier {
6699ed24f4bSMarc Zyngier 	if (kvm_request_pending(vcpu)) {
6709ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
6719ed24f4bSMarc Zyngier 			vcpu_req_sleep(vcpu);
6729ed24f4bSMarc Zyngier 
6739ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
6749ed24f4bSMarc Zyngier 			kvm_reset_vcpu(vcpu);
6759ed24f4bSMarc Zyngier 
6769ed24f4bSMarc Zyngier 		/*
6779ed24f4bSMarc Zyngier 		 * Clear IRQ_PENDING requests that were made to guarantee
6789ed24f4bSMarc Zyngier 		 * that a VCPU sees new virtual interrupts.
6799ed24f4bSMarc Zyngier 		 */
6809ed24f4bSMarc Zyngier 		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
6819ed24f4bSMarc Zyngier 
6829ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
6839ed24f4bSMarc Zyngier 			kvm_update_stolen_time(vcpu);
6849ed24f4bSMarc Zyngier 
6859ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
6869ed24f4bSMarc Zyngier 			/* The distributor enable bits were changed */
6879ed24f4bSMarc Zyngier 			preempt_disable();
6889ed24f4bSMarc Zyngier 			vgic_v4_put(vcpu, false);
6899ed24f4bSMarc Zyngier 			vgic_v4_load(vcpu);
6909ed24f4bSMarc Zyngier 			preempt_enable();
6919ed24f4bSMarc Zyngier 		}
6929ed24f4bSMarc Zyngier 	}
6939ed24f4bSMarc Zyngier }
6949ed24f4bSMarc Zyngier 
695*2f6a49bbSWill Deacon static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
696*2f6a49bbSWill Deacon {
697*2f6a49bbSWill Deacon 	if (likely(!vcpu_mode_is_32bit(vcpu)))
698*2f6a49bbSWill Deacon 		return false;
699*2f6a49bbSWill Deacon 
700*2f6a49bbSWill Deacon 	return !system_supports_32bit_el0() ||
701*2f6a49bbSWill Deacon 		static_branch_unlikely(&arm64_mismatched_32bit_el0);
702*2f6a49bbSWill Deacon }
703*2f6a49bbSWill Deacon 
7049ed24f4bSMarc Zyngier /**
7059ed24f4bSMarc Zyngier  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
7069ed24f4bSMarc Zyngier  * @vcpu:	The VCPU pointer
7079ed24f4bSMarc Zyngier  *
7089ed24f4bSMarc Zyngier  * This function is called through the VCPU_RUN ioctl called from user space. It
7099ed24f4bSMarc Zyngier  * will execute VM code in a loop until the time slice for the process is used
7109ed24f4bSMarc Zyngier  * or some emulation is needed from user space in which case the function will
7119ed24f4bSMarc Zyngier  * return with return value 0 and with the kvm_run structure filled in with the
7129ed24f4bSMarc Zyngier  * required data for the requested emulation.
7139ed24f4bSMarc Zyngier  */
71438060944SPaolo Bonzini int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
7159ed24f4bSMarc Zyngier {
71638060944SPaolo Bonzini 	struct kvm_run *run = vcpu->run;
7179ed24f4bSMarc Zyngier 	int ret;
7189ed24f4bSMarc Zyngier 
7199ed24f4bSMarc Zyngier 	if (unlikely(!kvm_vcpu_initialized(vcpu)))
7209ed24f4bSMarc Zyngier 		return -ENOEXEC;
7219ed24f4bSMarc Zyngier 
7229ed24f4bSMarc Zyngier 	ret = kvm_vcpu_first_run_init(vcpu);
7239ed24f4bSMarc Zyngier 	if (ret)
7249ed24f4bSMarc Zyngier 		return ret;
7259ed24f4bSMarc Zyngier 
7269ed24f4bSMarc Zyngier 	if (run->exit_reason == KVM_EXIT_MMIO) {
72774cc7e0cSTianjia Zhang 		ret = kvm_handle_mmio_return(vcpu);
7289ed24f4bSMarc Zyngier 		if (ret)
7299ed24f4bSMarc Zyngier 			return ret;
7309ed24f4bSMarc Zyngier 	}
7319ed24f4bSMarc Zyngier 
7329ed24f4bSMarc Zyngier 	if (run->immediate_exit)
7339ed24f4bSMarc Zyngier 		return -EINTR;
7349ed24f4bSMarc Zyngier 
7359ed24f4bSMarc Zyngier 	vcpu_load(vcpu);
7369ed24f4bSMarc Zyngier 
7379ed24f4bSMarc Zyngier 	kvm_sigset_activate(vcpu);
7389ed24f4bSMarc Zyngier 
7399ed24f4bSMarc Zyngier 	ret = 1;
7409ed24f4bSMarc Zyngier 	run->exit_reason = KVM_EXIT_UNKNOWN;
7419ed24f4bSMarc Zyngier 	while (ret > 0) {
7429ed24f4bSMarc Zyngier 		/*
7439ed24f4bSMarc Zyngier 		 * Check conditions before entering the guest
7449ed24f4bSMarc Zyngier 		 */
7459ed24f4bSMarc Zyngier 		cond_resched();
7469ed24f4bSMarc Zyngier 
747a0e50aa3SChristoffer Dall 		update_vmid(&vcpu->arch.hw_mmu->vmid);
7489ed24f4bSMarc Zyngier 
7499ed24f4bSMarc Zyngier 		check_vcpu_requests(vcpu);
7509ed24f4bSMarc Zyngier 
7519ed24f4bSMarc Zyngier 		/*
7529ed24f4bSMarc Zyngier 		 * Preparing the interrupts to be injected also
7539ed24f4bSMarc Zyngier 		 * involves poking the GIC, which must be done in a
7549ed24f4bSMarc Zyngier 		 * non-preemptible context.
7559ed24f4bSMarc Zyngier 		 */
7569ed24f4bSMarc Zyngier 		preempt_disable();
7579ed24f4bSMarc Zyngier 
7589ed24f4bSMarc Zyngier 		kvm_pmu_flush_hwstate(vcpu);
7599ed24f4bSMarc Zyngier 
7609ed24f4bSMarc Zyngier 		local_irq_disable();
7619ed24f4bSMarc Zyngier 
7629ed24f4bSMarc Zyngier 		kvm_vgic_flush_hwstate(vcpu);
7639ed24f4bSMarc Zyngier 
7649ed24f4bSMarc Zyngier 		/*
7659ed24f4bSMarc Zyngier 		 * Exit if we have a signal pending so that we can deliver the
7669ed24f4bSMarc Zyngier 		 * signal to user space.
7679ed24f4bSMarc Zyngier 		 */
7689ed24f4bSMarc Zyngier 		if (signal_pending(current)) {
7699ed24f4bSMarc Zyngier 			ret = -EINTR;
7709ed24f4bSMarc Zyngier 			run->exit_reason = KVM_EXIT_INTR;
7719ed24f4bSMarc Zyngier 		}
7729ed24f4bSMarc Zyngier 
7739ed24f4bSMarc Zyngier 		/*
7749ed24f4bSMarc Zyngier 		 * If we're using a userspace irqchip, then check if we need
7759ed24f4bSMarc Zyngier 		 * to tell a userspace irqchip about timer or PMU level
7769ed24f4bSMarc Zyngier 		 * changes and if so, exit to userspace (the actual level
7779ed24f4bSMarc Zyngier 		 * state gets updated in kvm_timer_update_run and
7789ed24f4bSMarc Zyngier 		 * kvm_pmu_update_run below).
7799ed24f4bSMarc Zyngier 		 */
7809ed24f4bSMarc Zyngier 		if (static_branch_unlikely(&userspace_irqchip_in_use)) {
7819ed24f4bSMarc Zyngier 			if (kvm_timer_should_notify_user(vcpu) ||
7829ed24f4bSMarc Zyngier 			    kvm_pmu_should_notify_user(vcpu)) {
7839ed24f4bSMarc Zyngier 				ret = -EINTR;
7849ed24f4bSMarc Zyngier 				run->exit_reason = KVM_EXIT_INTR;
7859ed24f4bSMarc Zyngier 			}
7869ed24f4bSMarc Zyngier 		}
7879ed24f4bSMarc Zyngier 
7889ed24f4bSMarc Zyngier 		/*
7899ed24f4bSMarc Zyngier 		 * Ensure we set mode to IN_GUEST_MODE after we disable
7909ed24f4bSMarc Zyngier 		 * interrupts and before the final VCPU requests check.
7919ed24f4bSMarc Zyngier 		 * See the comment in kvm_vcpu_exiting_guest_mode() and
7929ed24f4bSMarc Zyngier 		 * Documentation/virt/kvm/vcpu-requests.rst
7939ed24f4bSMarc Zyngier 		 */
7949ed24f4bSMarc Zyngier 		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
7959ed24f4bSMarc Zyngier 
796a0e50aa3SChristoffer Dall 		if (ret <= 0 || need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) ||
7979ed24f4bSMarc Zyngier 		    kvm_request_pending(vcpu)) {
7989ed24f4bSMarc Zyngier 			vcpu->mode = OUTSIDE_GUEST_MODE;
7999ed24f4bSMarc Zyngier 			isb(); /* Ensure work in x_flush_hwstate is committed */
8009ed24f4bSMarc Zyngier 			kvm_pmu_sync_hwstate(vcpu);
8019ed24f4bSMarc Zyngier 			if (static_branch_unlikely(&userspace_irqchip_in_use))
8023c5ff0c6SMarc Zyngier 				kvm_timer_sync_user(vcpu);
8039ed24f4bSMarc Zyngier 			kvm_vgic_sync_hwstate(vcpu);
8049ed24f4bSMarc Zyngier 			local_irq_enable();
8059ed24f4bSMarc Zyngier 			preempt_enable();
8069ed24f4bSMarc Zyngier 			continue;
8079ed24f4bSMarc Zyngier 		}
8089ed24f4bSMarc Zyngier 
8099ed24f4bSMarc Zyngier 		kvm_arm_setup_debug(vcpu);
8109ed24f4bSMarc Zyngier 
8119ed24f4bSMarc Zyngier 		/**************************************************************
8129ed24f4bSMarc Zyngier 		 * Enter the guest
8139ed24f4bSMarc Zyngier 		 */
8149ed24f4bSMarc Zyngier 		trace_kvm_entry(*vcpu_pc(vcpu));
8159ed24f4bSMarc Zyngier 		guest_enter_irqoff();
8169ed24f4bSMarc Zyngier 
81709cf57ebSDavid Brazdil 		ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
8189ed24f4bSMarc Zyngier 
8199ed24f4bSMarc Zyngier 		vcpu->mode = OUTSIDE_GUEST_MODE;
8209ed24f4bSMarc Zyngier 		vcpu->stat.exits++;
8219ed24f4bSMarc Zyngier 		/*
8229ed24f4bSMarc Zyngier 		 * Back from guest
8239ed24f4bSMarc Zyngier 		 *************************************************************/
8249ed24f4bSMarc Zyngier 
8259ed24f4bSMarc Zyngier 		kvm_arm_clear_debug(vcpu);
8269ed24f4bSMarc Zyngier 
8279ed24f4bSMarc Zyngier 		/*
8289ed24f4bSMarc Zyngier 		 * We must sync the PMU state before the vgic state so
8299ed24f4bSMarc Zyngier 		 * that the vgic can properly sample the updated state of the
8309ed24f4bSMarc Zyngier 		 * interrupt line.
8319ed24f4bSMarc Zyngier 		 */
8329ed24f4bSMarc Zyngier 		kvm_pmu_sync_hwstate(vcpu);
8339ed24f4bSMarc Zyngier 
8349ed24f4bSMarc Zyngier 		/*
8359ed24f4bSMarc Zyngier 		 * Sync the vgic state before syncing the timer state because
8369ed24f4bSMarc Zyngier 		 * the timer code needs to know if the virtual timer
8379ed24f4bSMarc Zyngier 		 * interrupts are active.
8389ed24f4bSMarc Zyngier 		 */
8399ed24f4bSMarc Zyngier 		kvm_vgic_sync_hwstate(vcpu);
8409ed24f4bSMarc Zyngier 
8419ed24f4bSMarc Zyngier 		/*
8429ed24f4bSMarc Zyngier 		 * Sync the timer hardware state before enabling interrupts as
8439ed24f4bSMarc Zyngier 		 * we don't want vtimer interrupts to race with syncing the
8449ed24f4bSMarc Zyngier 		 * timer virtual interrupt state.
8459ed24f4bSMarc Zyngier 		 */
8469ed24f4bSMarc Zyngier 		if (static_branch_unlikely(&userspace_irqchip_in_use))
8473c5ff0c6SMarc Zyngier 			kvm_timer_sync_user(vcpu);
8489ed24f4bSMarc Zyngier 
8499ed24f4bSMarc Zyngier 		kvm_arch_vcpu_ctxsync_fp(vcpu);
8509ed24f4bSMarc Zyngier 
8519ed24f4bSMarc Zyngier 		/*
8529ed24f4bSMarc Zyngier 		 * We may have taken a host interrupt in HYP mode (ie
8539ed24f4bSMarc Zyngier 		 * while executing the guest). This interrupt is still
8549ed24f4bSMarc Zyngier 		 * pending, as we haven't serviced it yet!
8559ed24f4bSMarc Zyngier 		 *
8569ed24f4bSMarc Zyngier 		 * We're now back in SVC mode, with interrupts
8579ed24f4bSMarc Zyngier 		 * disabled.  Enabling the interrupts now will have
8589ed24f4bSMarc Zyngier 		 * the effect of taking the interrupt again, in SVC
8599ed24f4bSMarc Zyngier 		 * mode this time.
8609ed24f4bSMarc Zyngier 		 */
8619ed24f4bSMarc Zyngier 		local_irq_enable();
8629ed24f4bSMarc Zyngier 
8639ed24f4bSMarc Zyngier 		/*
8649ed24f4bSMarc Zyngier 		 * We do local_irq_enable() before calling guest_exit() so
8659ed24f4bSMarc Zyngier 		 * that if a timer interrupt hits while running the guest we
8669ed24f4bSMarc Zyngier 		 * account that tick as being spent in the guest.  We enable
8679ed24f4bSMarc Zyngier 		 * preemption after calling guest_exit() so that if we get
8689ed24f4bSMarc Zyngier 		 * preempted we make sure ticks after that is not counted as
8699ed24f4bSMarc Zyngier 		 * guest time.
8709ed24f4bSMarc Zyngier 		 */
8719ed24f4bSMarc Zyngier 		guest_exit();
8729ed24f4bSMarc Zyngier 		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
8739ed24f4bSMarc Zyngier 
8749ed24f4bSMarc Zyngier 		/* Exit types that need handling before we can be preempted */
87574cc7e0cSTianjia Zhang 		handle_exit_early(vcpu, ret);
8769ed24f4bSMarc Zyngier 
8779ed24f4bSMarc Zyngier 		preempt_enable();
8789ed24f4bSMarc Zyngier 
87922f55384SQais Yousef 		/*
88022f55384SQais Yousef 		 * The ARMv8 architecture doesn't give the hypervisor
88122f55384SQais Yousef 		 * a mechanism to prevent a guest from dropping to AArch32 EL0
88222f55384SQais Yousef 		 * if implemented by the CPU. If we spot the guest in such
88322f55384SQais Yousef 		 * state and that we decided it wasn't supposed to do so (like
88422f55384SQais Yousef 		 * with the asymmetric AArch32 case), return to userspace with
88522f55384SQais Yousef 		 * a fatal error.
88622f55384SQais Yousef 		 */
887*2f6a49bbSWill Deacon 		if (vcpu_mode_is_bad_32bit(vcpu)) {
88822f55384SQais Yousef 			/*
88922f55384SQais Yousef 			 * As we have caught the guest red-handed, decide that
89022f55384SQais Yousef 			 * it isn't fit for purpose anymore by making the vcpu
89122f55384SQais Yousef 			 * invalid. The VMM can try and fix it by issuing  a
89222f55384SQais Yousef 			 * KVM_ARM_VCPU_INIT if it really wants to.
89322f55384SQais Yousef 			 */
89422f55384SQais Yousef 			vcpu->arch.target = -1;
89522f55384SQais Yousef 			ret = ARM_EXCEPTION_IL;
89622f55384SQais Yousef 		}
89722f55384SQais Yousef 
89874cc7e0cSTianjia Zhang 		ret = handle_exit(vcpu, ret);
8999ed24f4bSMarc Zyngier 	}
9009ed24f4bSMarc Zyngier 
9019ed24f4bSMarc Zyngier 	/* Tell userspace about in-kernel device output levels */
9029ed24f4bSMarc Zyngier 	if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
9039ed24f4bSMarc Zyngier 		kvm_timer_update_run(vcpu);
9049ed24f4bSMarc Zyngier 		kvm_pmu_update_run(vcpu);
9059ed24f4bSMarc Zyngier 	}
9069ed24f4bSMarc Zyngier 
9079ed24f4bSMarc Zyngier 	kvm_sigset_deactivate(vcpu);
9089ed24f4bSMarc Zyngier 
9099ed24f4bSMarc Zyngier 	vcpu_put(vcpu);
9109ed24f4bSMarc Zyngier 	return ret;
9119ed24f4bSMarc Zyngier }
9129ed24f4bSMarc Zyngier 
9139ed24f4bSMarc Zyngier static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
9149ed24f4bSMarc Zyngier {
9159ed24f4bSMarc Zyngier 	int bit_index;
9169ed24f4bSMarc Zyngier 	bool set;
9179ed24f4bSMarc Zyngier 	unsigned long *hcr;
9189ed24f4bSMarc Zyngier 
9199ed24f4bSMarc Zyngier 	if (number == KVM_ARM_IRQ_CPU_IRQ)
9209ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VI);
9219ed24f4bSMarc Zyngier 	else /* KVM_ARM_IRQ_CPU_FIQ */
9229ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VF);
9239ed24f4bSMarc Zyngier 
9249ed24f4bSMarc Zyngier 	hcr = vcpu_hcr(vcpu);
9259ed24f4bSMarc Zyngier 	if (level)
9269ed24f4bSMarc Zyngier 		set = test_and_set_bit(bit_index, hcr);
9279ed24f4bSMarc Zyngier 	else
9289ed24f4bSMarc Zyngier 		set = test_and_clear_bit(bit_index, hcr);
9299ed24f4bSMarc Zyngier 
9309ed24f4bSMarc Zyngier 	/*
9319ed24f4bSMarc Zyngier 	 * If we didn't change anything, no need to wake up or kick other CPUs
9329ed24f4bSMarc Zyngier 	 */
9339ed24f4bSMarc Zyngier 	if (set == level)
9349ed24f4bSMarc Zyngier 		return 0;
9359ed24f4bSMarc Zyngier 
9369ed24f4bSMarc Zyngier 	/*
9379ed24f4bSMarc Zyngier 	 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
9389ed24f4bSMarc Zyngier 	 * trigger a world-switch round on the running physical CPU to set the
9399ed24f4bSMarc Zyngier 	 * virtual IRQ/FIQ fields in the HCR appropriately.
9409ed24f4bSMarc Zyngier 	 */
9419ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
9429ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
9439ed24f4bSMarc Zyngier 
9449ed24f4bSMarc Zyngier 	return 0;
9459ed24f4bSMarc Zyngier }
9469ed24f4bSMarc Zyngier 
9479ed24f4bSMarc Zyngier int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
9489ed24f4bSMarc Zyngier 			  bool line_status)
9499ed24f4bSMarc Zyngier {
9509ed24f4bSMarc Zyngier 	u32 irq = irq_level->irq;
9519ed24f4bSMarc Zyngier 	unsigned int irq_type, vcpu_idx, irq_num;
9529ed24f4bSMarc Zyngier 	int nrcpus = atomic_read(&kvm->online_vcpus);
9539ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = NULL;
9549ed24f4bSMarc Zyngier 	bool level = irq_level->level;
9559ed24f4bSMarc Zyngier 
9569ed24f4bSMarc Zyngier 	irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
9579ed24f4bSMarc Zyngier 	vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
9589ed24f4bSMarc Zyngier 	vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
9599ed24f4bSMarc Zyngier 	irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
9609ed24f4bSMarc Zyngier 
9619ed24f4bSMarc Zyngier 	trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
9629ed24f4bSMarc Zyngier 
9639ed24f4bSMarc Zyngier 	switch (irq_type) {
9649ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_CPU:
9659ed24f4bSMarc Zyngier 		if (irqchip_in_kernel(kvm))
9669ed24f4bSMarc Zyngier 			return -ENXIO;
9679ed24f4bSMarc Zyngier 
9689ed24f4bSMarc Zyngier 		if (vcpu_idx >= nrcpus)
9699ed24f4bSMarc Zyngier 			return -EINVAL;
9709ed24f4bSMarc Zyngier 
9719ed24f4bSMarc Zyngier 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
9729ed24f4bSMarc Zyngier 		if (!vcpu)
9739ed24f4bSMarc Zyngier 			return -EINVAL;
9749ed24f4bSMarc Zyngier 
9759ed24f4bSMarc Zyngier 		if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
9769ed24f4bSMarc Zyngier 			return -EINVAL;
9779ed24f4bSMarc Zyngier 
9789ed24f4bSMarc Zyngier 		return vcpu_interrupt_line(vcpu, irq_num, level);
9799ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_PPI:
9809ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
9819ed24f4bSMarc Zyngier 			return -ENXIO;
9829ed24f4bSMarc Zyngier 
9839ed24f4bSMarc Zyngier 		if (vcpu_idx >= nrcpus)
9849ed24f4bSMarc Zyngier 			return -EINVAL;
9859ed24f4bSMarc Zyngier 
9869ed24f4bSMarc Zyngier 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
9879ed24f4bSMarc Zyngier 		if (!vcpu)
9889ed24f4bSMarc Zyngier 			return -EINVAL;
9899ed24f4bSMarc Zyngier 
9909ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
9919ed24f4bSMarc Zyngier 			return -EINVAL;
9929ed24f4bSMarc Zyngier 
9939ed24f4bSMarc Zyngier 		return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
9949ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_SPI:
9959ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
9969ed24f4bSMarc Zyngier 			return -ENXIO;
9979ed24f4bSMarc Zyngier 
9989ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_PRIVATE_IRQS)
9999ed24f4bSMarc Zyngier 			return -EINVAL;
10009ed24f4bSMarc Zyngier 
10019ed24f4bSMarc Zyngier 		return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
10029ed24f4bSMarc Zyngier 	}
10039ed24f4bSMarc Zyngier 
10049ed24f4bSMarc Zyngier 	return -EINVAL;
10059ed24f4bSMarc Zyngier }
10069ed24f4bSMarc Zyngier 
10079ed24f4bSMarc Zyngier static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
10089ed24f4bSMarc Zyngier 			       const struct kvm_vcpu_init *init)
10099ed24f4bSMarc Zyngier {
10109ed24f4bSMarc Zyngier 	unsigned int i, ret;
10119ed24f4bSMarc Zyngier 	int phys_target = kvm_target_cpu();
10129ed24f4bSMarc Zyngier 
10139ed24f4bSMarc Zyngier 	if (init->target != phys_target)
10149ed24f4bSMarc Zyngier 		return -EINVAL;
10159ed24f4bSMarc Zyngier 
10169ed24f4bSMarc Zyngier 	/*
10179ed24f4bSMarc Zyngier 	 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
10189ed24f4bSMarc Zyngier 	 * use the same target.
10199ed24f4bSMarc Zyngier 	 */
10209ed24f4bSMarc Zyngier 	if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
10219ed24f4bSMarc Zyngier 		return -EINVAL;
10229ed24f4bSMarc Zyngier 
10239ed24f4bSMarc Zyngier 	/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
10249ed24f4bSMarc Zyngier 	for (i = 0; i < sizeof(init->features) * 8; i++) {
10259ed24f4bSMarc Zyngier 		bool set = (init->features[i / 32] & (1 << (i % 32)));
10269ed24f4bSMarc Zyngier 
10279ed24f4bSMarc Zyngier 		if (set && i >= KVM_VCPU_MAX_FEATURES)
10289ed24f4bSMarc Zyngier 			return -ENOENT;
10299ed24f4bSMarc Zyngier 
10309ed24f4bSMarc Zyngier 		/*
10319ed24f4bSMarc Zyngier 		 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
10329ed24f4bSMarc Zyngier 		 * use the same feature set.
10339ed24f4bSMarc Zyngier 		 */
10349ed24f4bSMarc Zyngier 		if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
10359ed24f4bSMarc Zyngier 		    test_bit(i, vcpu->arch.features) != set)
10369ed24f4bSMarc Zyngier 			return -EINVAL;
10379ed24f4bSMarc Zyngier 
10389ed24f4bSMarc Zyngier 		if (set)
10399ed24f4bSMarc Zyngier 			set_bit(i, vcpu->arch.features);
10409ed24f4bSMarc Zyngier 	}
10419ed24f4bSMarc Zyngier 
10429ed24f4bSMarc Zyngier 	vcpu->arch.target = phys_target;
10439ed24f4bSMarc Zyngier 
10449ed24f4bSMarc Zyngier 	/* Now we know what it is, we can reset it. */
10459ed24f4bSMarc Zyngier 	ret = kvm_reset_vcpu(vcpu);
10469ed24f4bSMarc Zyngier 	if (ret) {
10479ed24f4bSMarc Zyngier 		vcpu->arch.target = -1;
10489ed24f4bSMarc Zyngier 		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
10499ed24f4bSMarc Zyngier 	}
10509ed24f4bSMarc Zyngier 
10519ed24f4bSMarc Zyngier 	return ret;
10529ed24f4bSMarc Zyngier }
10539ed24f4bSMarc Zyngier 
10549ed24f4bSMarc Zyngier static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
10559ed24f4bSMarc Zyngier 					 struct kvm_vcpu_init *init)
10569ed24f4bSMarc Zyngier {
10579ed24f4bSMarc Zyngier 	int ret;
10589ed24f4bSMarc Zyngier 
10599ed24f4bSMarc Zyngier 	ret = kvm_vcpu_set_target(vcpu, init);
10609ed24f4bSMarc Zyngier 	if (ret)
10619ed24f4bSMarc Zyngier 		return ret;
10629ed24f4bSMarc Zyngier 
10639ed24f4bSMarc Zyngier 	/*
10649ed24f4bSMarc Zyngier 	 * Ensure a rebooted VM will fault in RAM pages and detect if the
10659ed24f4bSMarc Zyngier 	 * guest MMU is turned off and flush the caches as needed.
1066892713e9SZenghui Yu 	 *
10677ae2f3dbSMarc Zyngier 	 * S2FWB enforces all memory accesses to RAM being cacheable,
10687ae2f3dbSMarc Zyngier 	 * ensuring that the data side is always coherent. We still
10697ae2f3dbSMarc Zyngier 	 * need to invalidate the I-cache though, as FWB does *not*
10707ae2f3dbSMarc Zyngier 	 * imply CTR_EL0.DIC.
10719ed24f4bSMarc Zyngier 	 */
10727ae2f3dbSMarc Zyngier 	if (vcpu->arch.has_run_once) {
10737ae2f3dbSMarc Zyngier 		if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
10749ed24f4bSMarc Zyngier 			stage2_unmap_vm(vcpu->kvm);
10757ae2f3dbSMarc Zyngier 		else
10767ae2f3dbSMarc Zyngier 			__flush_icache_all();
10777ae2f3dbSMarc Zyngier 	}
10789ed24f4bSMarc Zyngier 
10799ed24f4bSMarc Zyngier 	vcpu_reset_hcr(vcpu);
10809ed24f4bSMarc Zyngier 
10819ed24f4bSMarc Zyngier 	/*
10829ed24f4bSMarc Zyngier 	 * Handle the "start in power-off" case.
10839ed24f4bSMarc Zyngier 	 */
10849ed24f4bSMarc Zyngier 	if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
10859ed24f4bSMarc Zyngier 		vcpu_power_off(vcpu);
10869ed24f4bSMarc Zyngier 	else
10879ed24f4bSMarc Zyngier 		vcpu->arch.power_off = false;
10889ed24f4bSMarc Zyngier 
10899ed24f4bSMarc Zyngier 	return 0;
10909ed24f4bSMarc Zyngier }
10919ed24f4bSMarc Zyngier 
10929ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
10939ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
10949ed24f4bSMarc Zyngier {
10959ed24f4bSMarc Zyngier 	int ret = -ENXIO;
10969ed24f4bSMarc Zyngier 
10979ed24f4bSMarc Zyngier 	switch (attr->group) {
10989ed24f4bSMarc Zyngier 	default:
10999ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
11009ed24f4bSMarc Zyngier 		break;
11019ed24f4bSMarc Zyngier 	}
11029ed24f4bSMarc Zyngier 
11039ed24f4bSMarc Zyngier 	return ret;
11049ed24f4bSMarc Zyngier }
11059ed24f4bSMarc Zyngier 
11069ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
11079ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
11089ed24f4bSMarc Zyngier {
11099ed24f4bSMarc Zyngier 	int ret = -ENXIO;
11109ed24f4bSMarc Zyngier 
11119ed24f4bSMarc Zyngier 	switch (attr->group) {
11129ed24f4bSMarc Zyngier 	default:
11139ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
11149ed24f4bSMarc Zyngier 		break;
11159ed24f4bSMarc Zyngier 	}
11169ed24f4bSMarc Zyngier 
11179ed24f4bSMarc Zyngier 	return ret;
11189ed24f4bSMarc Zyngier }
11199ed24f4bSMarc Zyngier 
11209ed24f4bSMarc Zyngier static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
11219ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
11229ed24f4bSMarc Zyngier {
11239ed24f4bSMarc Zyngier 	int ret = -ENXIO;
11249ed24f4bSMarc Zyngier 
11259ed24f4bSMarc Zyngier 	switch (attr->group) {
11269ed24f4bSMarc Zyngier 	default:
11279ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
11289ed24f4bSMarc Zyngier 		break;
11299ed24f4bSMarc Zyngier 	}
11309ed24f4bSMarc Zyngier 
11319ed24f4bSMarc Zyngier 	return ret;
11329ed24f4bSMarc Zyngier }
11339ed24f4bSMarc Zyngier 
11349ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
11359ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
11369ed24f4bSMarc Zyngier {
11379ed24f4bSMarc Zyngier 	memset(events, 0, sizeof(*events));
11389ed24f4bSMarc Zyngier 
11399ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_get_events(vcpu, events);
11409ed24f4bSMarc Zyngier }
11419ed24f4bSMarc Zyngier 
11429ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
11439ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
11449ed24f4bSMarc Zyngier {
11459ed24f4bSMarc Zyngier 	int i;
11469ed24f4bSMarc Zyngier 
11479ed24f4bSMarc Zyngier 	/* check whether the reserved field is zero */
11489ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
11499ed24f4bSMarc Zyngier 		if (events->reserved[i])
11509ed24f4bSMarc Zyngier 			return -EINVAL;
11519ed24f4bSMarc Zyngier 
11529ed24f4bSMarc Zyngier 	/* check whether the pad field is zero */
11539ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
11549ed24f4bSMarc Zyngier 		if (events->exception.pad[i])
11559ed24f4bSMarc Zyngier 			return -EINVAL;
11569ed24f4bSMarc Zyngier 
11579ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_set_events(vcpu, events);
11589ed24f4bSMarc Zyngier }
11599ed24f4bSMarc Zyngier 
11609ed24f4bSMarc Zyngier long kvm_arch_vcpu_ioctl(struct file *filp,
11619ed24f4bSMarc Zyngier 			 unsigned int ioctl, unsigned long arg)
11629ed24f4bSMarc Zyngier {
11639ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = filp->private_data;
11649ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
11659ed24f4bSMarc Zyngier 	struct kvm_device_attr attr;
11669ed24f4bSMarc Zyngier 	long r;
11679ed24f4bSMarc Zyngier 
11689ed24f4bSMarc Zyngier 	switch (ioctl) {
11699ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_INIT: {
11709ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
11719ed24f4bSMarc Zyngier 
11729ed24f4bSMarc Zyngier 		r = -EFAULT;
11739ed24f4bSMarc Zyngier 		if (copy_from_user(&init, argp, sizeof(init)))
11749ed24f4bSMarc Zyngier 			break;
11759ed24f4bSMarc Zyngier 
11769ed24f4bSMarc Zyngier 		r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
11779ed24f4bSMarc Zyngier 		break;
11789ed24f4bSMarc Zyngier 	}
11799ed24f4bSMarc Zyngier 	case KVM_SET_ONE_REG:
11809ed24f4bSMarc Zyngier 	case KVM_GET_ONE_REG: {
11819ed24f4bSMarc Zyngier 		struct kvm_one_reg reg;
11829ed24f4bSMarc Zyngier 
11839ed24f4bSMarc Zyngier 		r = -ENOEXEC;
11849ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
11859ed24f4bSMarc Zyngier 			break;
11869ed24f4bSMarc Zyngier 
11879ed24f4bSMarc Zyngier 		r = -EFAULT;
11889ed24f4bSMarc Zyngier 		if (copy_from_user(&reg, argp, sizeof(reg)))
11899ed24f4bSMarc Zyngier 			break;
11909ed24f4bSMarc Zyngier 
11919ed24f4bSMarc Zyngier 		if (ioctl == KVM_SET_ONE_REG)
11929ed24f4bSMarc Zyngier 			r = kvm_arm_set_reg(vcpu, &reg);
11939ed24f4bSMarc Zyngier 		else
11949ed24f4bSMarc Zyngier 			r = kvm_arm_get_reg(vcpu, &reg);
11959ed24f4bSMarc Zyngier 		break;
11969ed24f4bSMarc Zyngier 	}
11979ed24f4bSMarc Zyngier 	case KVM_GET_REG_LIST: {
11989ed24f4bSMarc Zyngier 		struct kvm_reg_list __user *user_list = argp;
11999ed24f4bSMarc Zyngier 		struct kvm_reg_list reg_list;
12009ed24f4bSMarc Zyngier 		unsigned n;
12019ed24f4bSMarc Zyngier 
12029ed24f4bSMarc Zyngier 		r = -ENOEXEC;
12039ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
12049ed24f4bSMarc Zyngier 			break;
12059ed24f4bSMarc Zyngier 
12069ed24f4bSMarc Zyngier 		r = -EPERM;
12079ed24f4bSMarc Zyngier 		if (!kvm_arm_vcpu_is_finalized(vcpu))
12089ed24f4bSMarc Zyngier 			break;
12099ed24f4bSMarc Zyngier 
12109ed24f4bSMarc Zyngier 		r = -EFAULT;
12119ed24f4bSMarc Zyngier 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
12129ed24f4bSMarc Zyngier 			break;
12139ed24f4bSMarc Zyngier 		n = reg_list.n;
12149ed24f4bSMarc Zyngier 		reg_list.n = kvm_arm_num_regs(vcpu);
12159ed24f4bSMarc Zyngier 		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
12169ed24f4bSMarc Zyngier 			break;
12179ed24f4bSMarc Zyngier 		r = -E2BIG;
12189ed24f4bSMarc Zyngier 		if (n < reg_list.n)
12199ed24f4bSMarc Zyngier 			break;
12209ed24f4bSMarc Zyngier 		r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
12219ed24f4bSMarc Zyngier 		break;
12229ed24f4bSMarc Zyngier 	}
12239ed24f4bSMarc Zyngier 	case KVM_SET_DEVICE_ATTR: {
12249ed24f4bSMarc Zyngier 		r = -EFAULT;
12259ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
12269ed24f4bSMarc Zyngier 			break;
12279ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_set_attr(vcpu, &attr);
12289ed24f4bSMarc Zyngier 		break;
12299ed24f4bSMarc Zyngier 	}
12309ed24f4bSMarc Zyngier 	case KVM_GET_DEVICE_ATTR: {
12319ed24f4bSMarc Zyngier 		r = -EFAULT;
12329ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
12339ed24f4bSMarc Zyngier 			break;
12349ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_get_attr(vcpu, &attr);
12359ed24f4bSMarc Zyngier 		break;
12369ed24f4bSMarc Zyngier 	}
12379ed24f4bSMarc Zyngier 	case KVM_HAS_DEVICE_ATTR: {
12389ed24f4bSMarc Zyngier 		r = -EFAULT;
12399ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
12409ed24f4bSMarc Zyngier 			break;
12419ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_has_attr(vcpu, &attr);
12429ed24f4bSMarc Zyngier 		break;
12439ed24f4bSMarc Zyngier 	}
12449ed24f4bSMarc Zyngier 	case KVM_GET_VCPU_EVENTS: {
12459ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
12469ed24f4bSMarc Zyngier 
12479ed24f4bSMarc Zyngier 		if (kvm_arm_vcpu_get_events(vcpu, &events))
12489ed24f4bSMarc Zyngier 			return -EINVAL;
12499ed24f4bSMarc Zyngier 
12509ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &events, sizeof(events)))
12519ed24f4bSMarc Zyngier 			return -EFAULT;
12529ed24f4bSMarc Zyngier 
12539ed24f4bSMarc Zyngier 		return 0;
12549ed24f4bSMarc Zyngier 	}
12559ed24f4bSMarc Zyngier 	case KVM_SET_VCPU_EVENTS: {
12569ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
12579ed24f4bSMarc Zyngier 
12589ed24f4bSMarc Zyngier 		if (copy_from_user(&events, argp, sizeof(events)))
12599ed24f4bSMarc Zyngier 			return -EFAULT;
12609ed24f4bSMarc Zyngier 
12619ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_set_events(vcpu, &events);
12629ed24f4bSMarc Zyngier 	}
12639ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_FINALIZE: {
12649ed24f4bSMarc Zyngier 		int what;
12659ed24f4bSMarc Zyngier 
12669ed24f4bSMarc Zyngier 		if (!kvm_vcpu_initialized(vcpu))
12679ed24f4bSMarc Zyngier 			return -ENOEXEC;
12689ed24f4bSMarc Zyngier 
12699ed24f4bSMarc Zyngier 		if (get_user(what, (const int __user *)argp))
12709ed24f4bSMarc Zyngier 			return -EFAULT;
12719ed24f4bSMarc Zyngier 
12729ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_finalize(vcpu, what);
12739ed24f4bSMarc Zyngier 	}
12749ed24f4bSMarc Zyngier 	default:
12759ed24f4bSMarc Zyngier 		r = -EINVAL;
12769ed24f4bSMarc Zyngier 	}
12779ed24f4bSMarc Zyngier 
12789ed24f4bSMarc Zyngier 	return r;
12799ed24f4bSMarc Zyngier }
12809ed24f4bSMarc Zyngier 
12819ed24f4bSMarc Zyngier void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
12829ed24f4bSMarc Zyngier {
12839ed24f4bSMarc Zyngier 
12849ed24f4bSMarc Zyngier }
12859ed24f4bSMarc Zyngier 
12869ed24f4bSMarc Zyngier void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
12876c9dd6d2SPaolo Bonzini 					const struct kvm_memory_slot *memslot)
12889ed24f4bSMarc Zyngier {
12899ed24f4bSMarc Zyngier 	kvm_flush_remote_tlbs(kvm);
12909ed24f4bSMarc Zyngier }
12919ed24f4bSMarc Zyngier 
12929ed24f4bSMarc Zyngier static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
12939ed24f4bSMarc Zyngier 					struct kvm_arm_device_addr *dev_addr)
12949ed24f4bSMarc Zyngier {
12959ed24f4bSMarc Zyngier 	unsigned long dev_id, type;
12969ed24f4bSMarc Zyngier 
12979ed24f4bSMarc Zyngier 	dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
12989ed24f4bSMarc Zyngier 		KVM_ARM_DEVICE_ID_SHIFT;
12999ed24f4bSMarc Zyngier 	type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
13009ed24f4bSMarc Zyngier 		KVM_ARM_DEVICE_TYPE_SHIFT;
13019ed24f4bSMarc Zyngier 
13029ed24f4bSMarc Zyngier 	switch (dev_id) {
13039ed24f4bSMarc Zyngier 	case KVM_ARM_DEVICE_VGIC_V2:
13049ed24f4bSMarc Zyngier 		if (!vgic_present)
13059ed24f4bSMarc Zyngier 			return -ENXIO;
13069ed24f4bSMarc Zyngier 		return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
13079ed24f4bSMarc Zyngier 	default:
13089ed24f4bSMarc Zyngier 		return -ENODEV;
13099ed24f4bSMarc Zyngier 	}
13109ed24f4bSMarc Zyngier }
13119ed24f4bSMarc Zyngier 
13129ed24f4bSMarc Zyngier long kvm_arch_vm_ioctl(struct file *filp,
13139ed24f4bSMarc Zyngier 		       unsigned int ioctl, unsigned long arg)
13149ed24f4bSMarc Zyngier {
13159ed24f4bSMarc Zyngier 	struct kvm *kvm = filp->private_data;
13169ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
13179ed24f4bSMarc Zyngier 
13189ed24f4bSMarc Zyngier 	switch (ioctl) {
13199ed24f4bSMarc Zyngier 	case KVM_CREATE_IRQCHIP: {
13209ed24f4bSMarc Zyngier 		int ret;
13219ed24f4bSMarc Zyngier 		if (!vgic_present)
13229ed24f4bSMarc Zyngier 			return -ENXIO;
13239ed24f4bSMarc Zyngier 		mutex_lock(&kvm->lock);
13249ed24f4bSMarc Zyngier 		ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
13259ed24f4bSMarc Zyngier 		mutex_unlock(&kvm->lock);
13269ed24f4bSMarc Zyngier 		return ret;
13279ed24f4bSMarc Zyngier 	}
13289ed24f4bSMarc Zyngier 	case KVM_ARM_SET_DEVICE_ADDR: {
13299ed24f4bSMarc Zyngier 		struct kvm_arm_device_addr dev_addr;
13309ed24f4bSMarc Zyngier 
13319ed24f4bSMarc Zyngier 		if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
13329ed24f4bSMarc Zyngier 			return -EFAULT;
13339ed24f4bSMarc Zyngier 		return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
13349ed24f4bSMarc Zyngier 	}
13359ed24f4bSMarc Zyngier 	case KVM_ARM_PREFERRED_TARGET: {
13369ed24f4bSMarc Zyngier 		int err;
13379ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
13389ed24f4bSMarc Zyngier 
13399ed24f4bSMarc Zyngier 		err = kvm_vcpu_preferred_target(&init);
13409ed24f4bSMarc Zyngier 		if (err)
13419ed24f4bSMarc Zyngier 			return err;
13429ed24f4bSMarc Zyngier 
13439ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &init, sizeof(init)))
13449ed24f4bSMarc Zyngier 			return -EFAULT;
13459ed24f4bSMarc Zyngier 
13469ed24f4bSMarc Zyngier 		return 0;
13479ed24f4bSMarc Zyngier 	}
13489ed24f4bSMarc Zyngier 	default:
13499ed24f4bSMarc Zyngier 		return -EINVAL;
13509ed24f4bSMarc Zyngier 	}
13519ed24f4bSMarc Zyngier }
13529ed24f4bSMarc Zyngier 
135330c95391SDavid Brazdil static unsigned long nvhe_percpu_size(void)
135430c95391SDavid Brazdil {
135530c95391SDavid Brazdil 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
135630c95391SDavid Brazdil 		(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
135730c95391SDavid Brazdil }
135830c95391SDavid Brazdil 
135930c95391SDavid Brazdil static unsigned long nvhe_percpu_order(void)
136030c95391SDavid Brazdil {
136130c95391SDavid Brazdil 	unsigned long size = nvhe_percpu_size();
136230c95391SDavid Brazdil 
136330c95391SDavid Brazdil 	return size ? get_order(size) : 0;
136430c95391SDavid Brazdil }
136530c95391SDavid Brazdil 
1366b881cdceSWill Deacon /* A lookup table holding the hypervisor VA for each vector slot */
1367b881cdceSWill Deacon static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1368de5bcdb4SWill Deacon 
1369b881cdceSWill Deacon static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1370b881cdceSWill Deacon {
1371bc1d2892SQuentin Perret 	hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
13729ef2b48bSWill Deacon }
13739ef2b48bSWill Deacon 
1374b881cdceSWill Deacon static int kvm_init_vector_slots(void)
1375b881cdceSWill Deacon {
1376b881cdceSWill Deacon 	int err;
1377b881cdceSWill Deacon 	void *base;
1378b881cdceSWill Deacon 
1379b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1380b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1381b881cdceSWill Deacon 
1382b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1383b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1384b881cdceSWill Deacon 
1385c4792b6dSWill Deacon 	if (!cpus_have_const_cap(ARM64_SPECTRE_V3A))
1386de5bcdb4SWill Deacon 		return 0;
13879ef2b48bSWill Deacon 
1388b881cdceSWill Deacon 	if (!has_vhe()) {
1389b881cdceSWill Deacon 		err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1390b881cdceSWill Deacon 					       __BP_HARDEN_HYP_VECS_SZ, &base);
1391b881cdceSWill Deacon 		if (err)
1392b881cdceSWill Deacon 			return err;
1393b881cdceSWill Deacon 	}
13949ef2b48bSWill Deacon 
1395b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1396b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
13979ef2b48bSWill Deacon 	return 0;
13989ef2b48bSWill Deacon }
13999ef2b48bSWill Deacon 
14009cc77581SQuentin Perret static void cpu_prepare_hyp_mode(int cpu)
14019ed24f4bSMarc Zyngier {
14029cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1403d3e1086cSDavid Brazdil 	unsigned long tcr;
14049ed24f4bSMarc Zyngier 
140571b3ec5fSDavid Brazdil 	/*
140671b3ec5fSDavid Brazdil 	 * Calculate the raw per-cpu offset without a translation from the
140771b3ec5fSDavid Brazdil 	 * kernel's mapping to the linear mapping, and store it in tpidr_el2
140871b3ec5fSDavid Brazdil 	 * so that we can use adr_l to access per-cpu variables in EL2.
1409e1663372SSteven Price 	 * Also drop the KASAN tag which gets in the way...
141071b3ec5fSDavid Brazdil 	 */
14119cc77581SQuentin Perret 	params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
141230c95391SDavid Brazdil 			    (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
141371b3ec5fSDavid Brazdil 
1414d3e1086cSDavid Brazdil 	params->mair_el2 = read_sysreg(mair_el1);
1415d3e1086cSDavid Brazdil 
1416d3e1086cSDavid Brazdil 	/*
1417d3e1086cSDavid Brazdil 	 * The ID map may be configured to use an extended virtual address
1418d3e1086cSDavid Brazdil 	 * range. This is only the case if system RAM is out of range for the
1419d3e1086cSDavid Brazdil 	 * currently configured page size and VA_BITS, in which case we will
1420d3e1086cSDavid Brazdil 	 * also need the extended virtual range for the HYP ID map, or we won't
1421d3e1086cSDavid Brazdil 	 * be able to enable the EL2 MMU.
1422d3e1086cSDavid Brazdil 	 *
1423d3e1086cSDavid Brazdil 	 * However, at EL2, there is only one TTBR register, and we can't switch
1424d3e1086cSDavid Brazdil 	 * between translation tables *and* update TCR_EL2.T0SZ at the same
1425d3e1086cSDavid Brazdil 	 * time. Bottom line: we need to use the extended range with *both* our
1426d3e1086cSDavid Brazdil 	 * translation tables.
1427d3e1086cSDavid Brazdil 	 *
1428d3e1086cSDavid Brazdil 	 * So use the same T0SZ value we use for the ID map.
1429d3e1086cSDavid Brazdil 	 */
1430d3e1086cSDavid Brazdil 	tcr = (read_sysreg(tcr_el1) & TCR_EL2_MASK) | TCR_EL2_RES1;
1431d3e1086cSDavid Brazdil 	tcr &= ~TCR_T0SZ_MASK;
1432d3e1086cSDavid Brazdil 	tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET;
1433d3e1086cSDavid Brazdil 	params->tcr_el2 = tcr;
1434d3e1086cSDavid Brazdil 
14359cc77581SQuentin Perret 	params->stack_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu) + PAGE_SIZE);
143663fec243SDavid Brazdil 	params->pgd_pa = kvm_mmu_get_httbr();
1437734864c1SQuentin Perret 	if (is_protected_kvm_enabled())
1438734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1439734864c1SQuentin Perret 	else
1440734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1441734864c1SQuentin Perret 	params->vttbr = params->vtcr = 0;
144263fec243SDavid Brazdil 
144363fec243SDavid Brazdil 	/*
144463fec243SDavid Brazdil 	 * Flush the init params from the data cache because the struct will
144563fec243SDavid Brazdil 	 * be read while the MMU is off.
144663fec243SDavid Brazdil 	 */
144763fec243SDavid Brazdil 	kvm_flush_dcache_to_poc(params, sizeof(*params));
14489cc77581SQuentin Perret }
14499cc77581SQuentin Perret 
1450bfa79a80SQuentin Perret static void hyp_install_host_vector(void)
14519cc77581SQuentin Perret {
14529cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params;
14539cc77581SQuentin Perret 	struct arm_smccc_res res;
14549cc77581SQuentin Perret 
14559cc77581SQuentin Perret 	/* Switch from the HYP stub to our own HYP init vector */
14569cc77581SQuentin Perret 	__hyp_set_vectors(kvm_get_idmap_vector());
14579ed24f4bSMarc Zyngier 
145871b3ec5fSDavid Brazdil 	/*
145971b3ec5fSDavid Brazdil 	 * Call initialization code, and switch to the full blown HYP code.
146071b3ec5fSDavid Brazdil 	 * If the cpucaps haven't been finalized yet, something has gone very
146171b3ec5fSDavid Brazdil 	 * wrong, and hyp will crash and burn when it uses any
146271b3ec5fSDavid Brazdil 	 * cpus_have_const_cap() wrapper.
146371b3ec5fSDavid Brazdil 	 */
146471b3ec5fSDavid Brazdil 	BUG_ON(!system_capabilities_finalized());
14659cc77581SQuentin Perret 	params = this_cpu_ptr_nvhe_sym(kvm_init_params);
146663fec243SDavid Brazdil 	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
146704e4caa8SAndrew Scull 	WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1468bfa79a80SQuentin Perret }
1469bfa79a80SQuentin Perret 
1470bfa79a80SQuentin Perret static void cpu_init_hyp_mode(void)
1471bfa79a80SQuentin Perret {
1472bfa79a80SQuentin Perret 	hyp_install_host_vector();
147371b3ec5fSDavid Brazdil 
147471b3ec5fSDavid Brazdil 	/*
147571b3ec5fSDavid Brazdil 	 * Disabling SSBD on a non-VHE system requires us to enable SSBS
147671b3ec5fSDavid Brazdil 	 * at EL2.
147771b3ec5fSDavid Brazdil 	 */
147871b3ec5fSDavid Brazdil 	if (this_cpu_has_cap(ARM64_SSBS) &&
1479d63d975aSMarc Zyngier 	    arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
148013aeb9b4SDavid Brazdil 		kvm_call_hyp_nvhe(__kvm_enable_ssbs);
148171b3ec5fSDavid Brazdil 	}
14829ed24f4bSMarc Zyngier }
14839ed24f4bSMarc Zyngier 
14849ed24f4bSMarc Zyngier static void cpu_hyp_reset(void)
14859ed24f4bSMarc Zyngier {
14869ed24f4bSMarc Zyngier 	if (!is_kernel_in_hyp_mode())
14879ed24f4bSMarc Zyngier 		__hyp_reset_vectors();
14889ed24f4bSMarc Zyngier }
14899ed24f4bSMarc Zyngier 
1490042c76a9SWill Deacon /*
1491042c76a9SWill Deacon  * EL2 vectors can be mapped and rerouted in a number of ways,
1492042c76a9SWill Deacon  * depending on the kernel configuration and CPU present:
1493042c76a9SWill Deacon  *
1494042c76a9SWill Deacon  * - If the CPU is affected by Spectre-v2, the hardening sequence is
1495042c76a9SWill Deacon  *   placed in one of the vector slots, which is executed before jumping
1496042c76a9SWill Deacon  *   to the real vectors.
1497042c76a9SWill Deacon  *
1498c4792b6dSWill Deacon  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1499042c76a9SWill Deacon  *   containing the hardening sequence is mapped next to the idmap page,
1500042c76a9SWill Deacon  *   and executed before jumping to the real vectors.
1501042c76a9SWill Deacon  *
1502c4792b6dSWill Deacon  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1503042c76a9SWill Deacon  *   empty slot is selected, mapped next to the idmap page, and
1504042c76a9SWill Deacon  *   executed before jumping to the real vectors.
1505042c76a9SWill Deacon  *
1506c4792b6dSWill Deacon  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1507042c76a9SWill Deacon  * VHE, as we don't have hypervisor-specific mappings. If the system
1508042c76a9SWill Deacon  * is VHE and yet selects this capability, it will be ignored.
1509042c76a9SWill Deacon  */
1510042c76a9SWill Deacon static void cpu_set_hyp_vector(void)
1511042c76a9SWill Deacon {
15126279017eSWill Deacon 	struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1513b881cdceSWill Deacon 	void *vector = hyp_spectre_vector_selector[data->slot];
1514042c76a9SWill Deacon 
1515bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
1516b881cdceSWill Deacon 		*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1517bfa79a80SQuentin Perret 	else
1518bfa79a80SQuentin Perret 		kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1519042c76a9SWill Deacon }
1520042c76a9SWill Deacon 
15219ed24f4bSMarc Zyngier static void cpu_hyp_reinit(void)
15229ed24f4bSMarc Zyngier {
15232a1198c9SDavid Brazdil 	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
15249ed24f4bSMarc Zyngier 
15259ed24f4bSMarc Zyngier 	cpu_hyp_reset();
1526a0e47952SAndrew Scull 
15279ed24f4bSMarc Zyngier 	if (is_kernel_in_hyp_mode())
15289ed24f4bSMarc Zyngier 		kvm_timer_init_vhe();
15299ed24f4bSMarc Zyngier 	else
15309ed24f4bSMarc Zyngier 		cpu_init_hyp_mode();
15319ed24f4bSMarc Zyngier 
1532bfa79a80SQuentin Perret 	cpu_set_hyp_vector();
1533bfa79a80SQuentin Perret 
15349ed24f4bSMarc Zyngier 	kvm_arm_init_debug();
15359ed24f4bSMarc Zyngier 
15369ed24f4bSMarc Zyngier 	if (vgic_present)
15379ed24f4bSMarc Zyngier 		kvm_vgic_init_cpu_hardware();
15389ed24f4bSMarc Zyngier }
15399ed24f4bSMarc Zyngier 
15409ed24f4bSMarc Zyngier static void _kvm_arch_hardware_enable(void *discard)
15419ed24f4bSMarc Zyngier {
15429ed24f4bSMarc Zyngier 	if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
15439ed24f4bSMarc Zyngier 		cpu_hyp_reinit();
15449ed24f4bSMarc Zyngier 		__this_cpu_write(kvm_arm_hardware_enabled, 1);
15459ed24f4bSMarc Zyngier 	}
15469ed24f4bSMarc Zyngier }
15479ed24f4bSMarc Zyngier 
15489ed24f4bSMarc Zyngier int kvm_arch_hardware_enable(void)
15499ed24f4bSMarc Zyngier {
15509ed24f4bSMarc Zyngier 	_kvm_arch_hardware_enable(NULL);
15519ed24f4bSMarc Zyngier 	return 0;
15529ed24f4bSMarc Zyngier }
15539ed24f4bSMarc Zyngier 
15549ed24f4bSMarc Zyngier static void _kvm_arch_hardware_disable(void *discard)
15559ed24f4bSMarc Zyngier {
15569ed24f4bSMarc Zyngier 	if (__this_cpu_read(kvm_arm_hardware_enabled)) {
15579ed24f4bSMarc Zyngier 		cpu_hyp_reset();
15589ed24f4bSMarc Zyngier 		__this_cpu_write(kvm_arm_hardware_enabled, 0);
15599ed24f4bSMarc Zyngier 	}
15609ed24f4bSMarc Zyngier }
15619ed24f4bSMarc Zyngier 
15629ed24f4bSMarc Zyngier void kvm_arch_hardware_disable(void)
15639ed24f4bSMarc Zyngier {
1564fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
15659ed24f4bSMarc Zyngier 		_kvm_arch_hardware_disable(NULL);
15669ed24f4bSMarc Zyngier }
15679ed24f4bSMarc Zyngier 
15689ed24f4bSMarc Zyngier #ifdef CONFIG_CPU_PM
15699ed24f4bSMarc Zyngier static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
15709ed24f4bSMarc Zyngier 				    unsigned long cmd,
15719ed24f4bSMarc Zyngier 				    void *v)
15729ed24f4bSMarc Zyngier {
15739ed24f4bSMarc Zyngier 	/*
15749ed24f4bSMarc Zyngier 	 * kvm_arm_hardware_enabled is left with its old value over
15759ed24f4bSMarc Zyngier 	 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
15769ed24f4bSMarc Zyngier 	 * re-enable hyp.
15779ed24f4bSMarc Zyngier 	 */
15789ed24f4bSMarc Zyngier 	switch (cmd) {
15799ed24f4bSMarc Zyngier 	case CPU_PM_ENTER:
15809ed24f4bSMarc Zyngier 		if (__this_cpu_read(kvm_arm_hardware_enabled))
15819ed24f4bSMarc Zyngier 			/*
15829ed24f4bSMarc Zyngier 			 * don't update kvm_arm_hardware_enabled here
15839ed24f4bSMarc Zyngier 			 * so that the hardware will be re-enabled
15849ed24f4bSMarc Zyngier 			 * when we resume. See below.
15859ed24f4bSMarc Zyngier 			 */
15869ed24f4bSMarc Zyngier 			cpu_hyp_reset();
15879ed24f4bSMarc Zyngier 
15889ed24f4bSMarc Zyngier 		return NOTIFY_OK;
15899ed24f4bSMarc Zyngier 	case CPU_PM_ENTER_FAILED:
15909ed24f4bSMarc Zyngier 	case CPU_PM_EXIT:
15919ed24f4bSMarc Zyngier 		if (__this_cpu_read(kvm_arm_hardware_enabled))
15929ed24f4bSMarc Zyngier 			/* The hardware was enabled before suspend. */
15939ed24f4bSMarc Zyngier 			cpu_hyp_reinit();
15949ed24f4bSMarc Zyngier 
15959ed24f4bSMarc Zyngier 		return NOTIFY_OK;
15969ed24f4bSMarc Zyngier 
15979ed24f4bSMarc Zyngier 	default:
15989ed24f4bSMarc Zyngier 		return NOTIFY_DONE;
15999ed24f4bSMarc Zyngier 	}
16009ed24f4bSMarc Zyngier }
16019ed24f4bSMarc Zyngier 
16029ed24f4bSMarc Zyngier static struct notifier_block hyp_init_cpu_pm_nb = {
16039ed24f4bSMarc Zyngier 	.notifier_call = hyp_init_cpu_pm_notifier,
16049ed24f4bSMarc Zyngier };
16059ed24f4bSMarc Zyngier 
160644362a3cSMarc Zyngier static void hyp_cpu_pm_init(void)
16079ed24f4bSMarc Zyngier {
1608fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
16099ed24f4bSMarc Zyngier 		cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
16109ed24f4bSMarc Zyngier }
161144362a3cSMarc Zyngier static void hyp_cpu_pm_exit(void)
16129ed24f4bSMarc Zyngier {
1613fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
16149ed24f4bSMarc Zyngier 		cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
16159ed24f4bSMarc Zyngier }
16169ed24f4bSMarc Zyngier #else
16179ed24f4bSMarc Zyngier static inline void hyp_cpu_pm_init(void)
16189ed24f4bSMarc Zyngier {
16199ed24f4bSMarc Zyngier }
16209ed24f4bSMarc Zyngier static inline void hyp_cpu_pm_exit(void)
16219ed24f4bSMarc Zyngier {
16229ed24f4bSMarc Zyngier }
16239ed24f4bSMarc Zyngier #endif
16249ed24f4bSMarc Zyngier 
162594f5e8a4SDavid Brazdil static void init_cpu_logical_map(void)
162694f5e8a4SDavid Brazdil {
162794f5e8a4SDavid Brazdil 	unsigned int cpu;
162894f5e8a4SDavid Brazdil 
162994f5e8a4SDavid Brazdil 	/*
163094f5e8a4SDavid Brazdil 	 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
163194f5e8a4SDavid Brazdil 	 * Only copy the set of online CPUs whose features have been chacked
163294f5e8a4SDavid Brazdil 	 * against the finalized system capabilities. The hypervisor will not
163394f5e8a4SDavid Brazdil 	 * allow any other CPUs from the `possible` set to boot.
163494f5e8a4SDavid Brazdil 	 */
163594f5e8a4SDavid Brazdil 	for_each_online_cpu(cpu)
163661fe0c37SDavid Brazdil 		hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
163794f5e8a4SDavid Brazdil }
163894f5e8a4SDavid Brazdil 
1639767c973fSMarc Zyngier #define init_psci_0_1_impl_state(config, what)	\
1640767c973fSMarc Zyngier 	config.psci_0_1_ ## what ## _implemented = psci_ops.what
1641767c973fSMarc Zyngier 
1642eeeee719SDavid Brazdil static bool init_psci_relay(void)
1643eeeee719SDavid Brazdil {
1644eeeee719SDavid Brazdil 	/*
1645eeeee719SDavid Brazdil 	 * If PSCI has not been initialized, protected KVM cannot install
1646eeeee719SDavid Brazdil 	 * itself on newly booted CPUs.
1647eeeee719SDavid Brazdil 	 */
1648eeeee719SDavid Brazdil 	if (!psci_ops.get_version) {
1649eeeee719SDavid Brazdil 		kvm_err("Cannot initialize protected mode without PSCI\n");
1650eeeee719SDavid Brazdil 		return false;
1651eeeee719SDavid Brazdil 	}
1652eeeee719SDavid Brazdil 
1653ff367fe4SDavid Brazdil 	kvm_host_psci_config.version = psci_ops.get_version();
1654ff367fe4SDavid Brazdil 
1655ff367fe4SDavid Brazdil 	if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
1656ff367fe4SDavid Brazdil 		kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
1657767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
1658767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
1659767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
1660767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
1661ff367fe4SDavid Brazdil 	}
1662eeeee719SDavid Brazdil 	return true;
1663eeeee719SDavid Brazdil }
1664eeeee719SDavid Brazdil 
16659ed24f4bSMarc Zyngier static int init_common_resources(void)
16669ed24f4bSMarc Zyngier {
1667039aeb9dSLinus Torvalds 	return kvm_set_ipa_limit();
16689ed24f4bSMarc Zyngier }
16699ed24f4bSMarc Zyngier 
16709ed24f4bSMarc Zyngier static int init_subsystems(void)
16719ed24f4bSMarc Zyngier {
16729ed24f4bSMarc Zyngier 	int err = 0;
16739ed24f4bSMarc Zyngier 
16749ed24f4bSMarc Zyngier 	/*
16759ed24f4bSMarc Zyngier 	 * Enable hardware so that subsystem initialisation can access EL2.
16769ed24f4bSMarc Zyngier 	 */
16779ed24f4bSMarc Zyngier 	on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
16789ed24f4bSMarc Zyngier 
16799ed24f4bSMarc Zyngier 	/*
16809ed24f4bSMarc Zyngier 	 * Register CPU lower-power notifier
16819ed24f4bSMarc Zyngier 	 */
16829ed24f4bSMarc Zyngier 	hyp_cpu_pm_init();
16839ed24f4bSMarc Zyngier 
16849ed24f4bSMarc Zyngier 	/*
16859ed24f4bSMarc Zyngier 	 * Init HYP view of VGIC
16869ed24f4bSMarc Zyngier 	 */
16879ed24f4bSMarc Zyngier 	err = kvm_vgic_hyp_init();
16889ed24f4bSMarc Zyngier 	switch (err) {
16899ed24f4bSMarc Zyngier 	case 0:
16909ed24f4bSMarc Zyngier 		vgic_present = true;
16919ed24f4bSMarc Zyngier 		break;
16929ed24f4bSMarc Zyngier 	case -ENODEV:
16939ed24f4bSMarc Zyngier 	case -ENXIO:
16949ed24f4bSMarc Zyngier 		vgic_present = false;
16959ed24f4bSMarc Zyngier 		err = 0;
16969ed24f4bSMarc Zyngier 		break;
16979ed24f4bSMarc Zyngier 	default:
16989ed24f4bSMarc Zyngier 		goto out;
16999ed24f4bSMarc Zyngier 	}
17009ed24f4bSMarc Zyngier 
17019ed24f4bSMarc Zyngier 	/*
17029ed24f4bSMarc Zyngier 	 * Init HYP architected timer support
17039ed24f4bSMarc Zyngier 	 */
17049ed24f4bSMarc Zyngier 	err = kvm_timer_hyp_init(vgic_present);
17059ed24f4bSMarc Zyngier 	if (err)
17069ed24f4bSMarc Zyngier 		goto out;
17079ed24f4bSMarc Zyngier 
17089ed24f4bSMarc Zyngier 	kvm_perf_init();
17096ac4a5acSMarc Zyngier 	kvm_sys_reg_table_init();
17109ed24f4bSMarc Zyngier 
17119ed24f4bSMarc Zyngier out:
1712fa8c3d65SDavid Brazdil 	if (err || !is_protected_kvm_enabled())
17139ed24f4bSMarc Zyngier 		on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
17149ed24f4bSMarc Zyngier 
17159ed24f4bSMarc Zyngier 	return err;
17169ed24f4bSMarc Zyngier }
17179ed24f4bSMarc Zyngier 
17189ed24f4bSMarc Zyngier static void teardown_hyp_mode(void)
17199ed24f4bSMarc Zyngier {
17209ed24f4bSMarc Zyngier 	int cpu;
17219ed24f4bSMarc Zyngier 
17229ed24f4bSMarc Zyngier 	free_hyp_pgds();
172330c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
17249ed24f4bSMarc Zyngier 		free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
172530c95391SDavid Brazdil 		free_pages(kvm_arm_hyp_percpu_base[cpu], nvhe_percpu_order());
172630c95391SDavid Brazdil 	}
17279ed24f4bSMarc Zyngier }
17289ed24f4bSMarc Zyngier 
1729bfa79a80SQuentin Perret static int do_pkvm_init(u32 hyp_va_bits)
1730bfa79a80SQuentin Perret {
1731bfa79a80SQuentin Perret 	void *per_cpu_base = kvm_ksym_ref(kvm_arm_hyp_percpu_base);
1732bfa79a80SQuentin Perret 	int ret;
1733bfa79a80SQuentin Perret 
1734bfa79a80SQuentin Perret 	preempt_disable();
1735bfa79a80SQuentin Perret 	hyp_install_host_vector();
1736bfa79a80SQuentin Perret 	ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
1737bfa79a80SQuentin Perret 				num_possible_cpus(), kern_hyp_va(per_cpu_base),
1738bfa79a80SQuentin Perret 				hyp_va_bits);
1739bfa79a80SQuentin Perret 	preempt_enable();
1740bfa79a80SQuentin Perret 
1741bfa79a80SQuentin Perret 	return ret;
1742bfa79a80SQuentin Perret }
1743bfa79a80SQuentin Perret 
1744bfa79a80SQuentin Perret static int kvm_hyp_init_protection(u32 hyp_va_bits)
1745bfa79a80SQuentin Perret {
1746bfa79a80SQuentin Perret 	void *addr = phys_to_virt(hyp_mem_base);
1747bfa79a80SQuentin Perret 	int ret;
1748bfa79a80SQuentin Perret 
17497c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
17507c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
17517c419937SMarc Zyngier 
1752bfa79a80SQuentin Perret 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
1753bfa79a80SQuentin Perret 	if (ret)
1754bfa79a80SQuentin Perret 		return ret;
1755bfa79a80SQuentin Perret 
1756bfa79a80SQuentin Perret 	ret = do_pkvm_init(hyp_va_bits);
1757bfa79a80SQuentin Perret 	if (ret)
1758bfa79a80SQuentin Perret 		return ret;
1759bfa79a80SQuentin Perret 
1760bfa79a80SQuentin Perret 	free_hyp_pgds();
1761bfa79a80SQuentin Perret 
1762bfa79a80SQuentin Perret 	return 0;
1763bfa79a80SQuentin Perret }
1764bfa79a80SQuentin Perret 
17659ed24f4bSMarc Zyngier /**
17669ed24f4bSMarc Zyngier  * Inits Hyp-mode on all online CPUs
17679ed24f4bSMarc Zyngier  */
17689ed24f4bSMarc Zyngier static int init_hyp_mode(void)
17699ed24f4bSMarc Zyngier {
1770bfa79a80SQuentin Perret 	u32 hyp_va_bits;
17719ed24f4bSMarc Zyngier 	int cpu;
1772bfa79a80SQuentin Perret 	int err = -ENOMEM;
1773bfa79a80SQuentin Perret 
1774bfa79a80SQuentin Perret 	/*
1775bfa79a80SQuentin Perret 	 * The protected Hyp-mode cannot be initialized if the memory pool
1776bfa79a80SQuentin Perret 	 * allocation has failed.
1777bfa79a80SQuentin Perret 	 */
1778bfa79a80SQuentin Perret 	if (is_protected_kvm_enabled() && !hyp_mem_base)
1779bfa79a80SQuentin Perret 		goto out_err;
17809ed24f4bSMarc Zyngier 
17819ed24f4bSMarc Zyngier 	/*
17829ed24f4bSMarc Zyngier 	 * Allocate Hyp PGD and setup Hyp identity mapping
17839ed24f4bSMarc Zyngier 	 */
1784bfa79a80SQuentin Perret 	err = kvm_mmu_init(&hyp_va_bits);
17859ed24f4bSMarc Zyngier 	if (err)
17869ed24f4bSMarc Zyngier 		goto out_err;
17879ed24f4bSMarc Zyngier 
17889ed24f4bSMarc Zyngier 	/*
17899ed24f4bSMarc Zyngier 	 * Allocate stack pages for Hypervisor-mode
17909ed24f4bSMarc Zyngier 	 */
17919ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
17929ed24f4bSMarc Zyngier 		unsigned long stack_page;
17939ed24f4bSMarc Zyngier 
17949ed24f4bSMarc Zyngier 		stack_page = __get_free_page(GFP_KERNEL);
17959ed24f4bSMarc Zyngier 		if (!stack_page) {
17969ed24f4bSMarc Zyngier 			err = -ENOMEM;
17979ed24f4bSMarc Zyngier 			goto out_err;
17989ed24f4bSMarc Zyngier 		}
17999ed24f4bSMarc Zyngier 
18009ed24f4bSMarc Zyngier 		per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
18019ed24f4bSMarc Zyngier 	}
18029ed24f4bSMarc Zyngier 
18039ed24f4bSMarc Zyngier 	/*
180430c95391SDavid Brazdil 	 * Allocate and initialize pages for Hypervisor-mode percpu regions.
180530c95391SDavid Brazdil 	 */
180630c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
180730c95391SDavid Brazdil 		struct page *page;
180830c95391SDavid Brazdil 		void *page_addr;
180930c95391SDavid Brazdil 
181030c95391SDavid Brazdil 		page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
181130c95391SDavid Brazdil 		if (!page) {
181230c95391SDavid Brazdil 			err = -ENOMEM;
181330c95391SDavid Brazdil 			goto out_err;
181430c95391SDavid Brazdil 		}
181530c95391SDavid Brazdil 
181630c95391SDavid Brazdil 		page_addr = page_address(page);
181730c95391SDavid Brazdil 		memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
181830c95391SDavid Brazdil 		kvm_arm_hyp_percpu_base[cpu] = (unsigned long)page_addr;
181930c95391SDavid Brazdil 	}
182030c95391SDavid Brazdil 
182130c95391SDavid Brazdil 	/*
18229ed24f4bSMarc Zyngier 	 * Map the Hyp-code called directly from the host
18239ed24f4bSMarc Zyngier 	 */
18249ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
18259ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
18269ed24f4bSMarc Zyngier 	if (err) {
18279ed24f4bSMarc Zyngier 		kvm_err("Cannot map world-switch code\n");
18289ed24f4bSMarc Zyngier 		goto out_err;
18299ed24f4bSMarc Zyngier 	}
18309ed24f4bSMarc Zyngier 
183116174eeaSDavid Brazdil 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
183216174eeaSDavid Brazdil 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
18332d7bf218SDavid Brazdil 	if (err) {
183416174eeaSDavid Brazdil 		kvm_err("Cannot map .hyp.rodata section\n");
18352d7bf218SDavid Brazdil 		goto out_err;
18362d7bf218SDavid Brazdil 	}
18372d7bf218SDavid Brazdil 
18389ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
18399ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
18409ed24f4bSMarc Zyngier 	if (err) {
18419ed24f4bSMarc Zyngier 		kvm_err("Cannot map rodata section\n");
18429ed24f4bSMarc Zyngier 		goto out_err;
18439ed24f4bSMarc Zyngier 	}
18449ed24f4bSMarc Zyngier 
1845380e18adSQuentin Perret 	/*
1846380e18adSQuentin Perret 	 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
1847380e18adSQuentin Perret 	 * section thanks to an assertion in the linker script. Map it RW and
1848380e18adSQuentin Perret 	 * the rest of .bss RO.
1849380e18adSQuentin Perret 	 */
1850380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
1851380e18adSQuentin Perret 				  kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
1852380e18adSQuentin Perret 	if (err) {
1853380e18adSQuentin Perret 		kvm_err("Cannot map hyp bss section: %d\n", err);
1854380e18adSQuentin Perret 		goto out_err;
1855380e18adSQuentin Perret 	}
1856380e18adSQuentin Perret 
1857380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
18589ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
18599ed24f4bSMarc Zyngier 	if (err) {
18609ed24f4bSMarc Zyngier 		kvm_err("Cannot map bss section\n");
18619ed24f4bSMarc Zyngier 		goto out_err;
18629ed24f4bSMarc Zyngier 	}
18639ed24f4bSMarc Zyngier 
18649ed24f4bSMarc Zyngier 	/*
18659ed24f4bSMarc Zyngier 	 * Map the Hyp stack pages
18669ed24f4bSMarc Zyngier 	 */
18679ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
18689ed24f4bSMarc Zyngier 		char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
18699ed24f4bSMarc Zyngier 		err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
18709ed24f4bSMarc Zyngier 					  PAGE_HYP);
18719ed24f4bSMarc Zyngier 
18729ed24f4bSMarc Zyngier 		if (err) {
18739ed24f4bSMarc Zyngier 			kvm_err("Cannot map hyp stack\n");
18749ed24f4bSMarc Zyngier 			goto out_err;
18759ed24f4bSMarc Zyngier 		}
18769ed24f4bSMarc Zyngier 	}
18779ed24f4bSMarc Zyngier 
18789ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
187930c95391SDavid Brazdil 		char *percpu_begin = (char *)kvm_arm_hyp_percpu_base[cpu];
188030c95391SDavid Brazdil 		char *percpu_end = percpu_begin + nvhe_percpu_size();
18819ed24f4bSMarc Zyngier 
18829cc77581SQuentin Perret 		/* Map Hyp percpu pages */
188330c95391SDavid Brazdil 		err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
18849ed24f4bSMarc Zyngier 		if (err) {
188530c95391SDavid Brazdil 			kvm_err("Cannot map hyp percpu region\n");
18866e3bfbb2SAndrew Scull 			goto out_err;
18876e3bfbb2SAndrew Scull 		}
18889cc77581SQuentin Perret 
18899cc77581SQuentin Perret 		/* Prepare the CPU initialization parameters */
18909cc77581SQuentin Perret 		cpu_prepare_hyp_mode(cpu);
18919ed24f4bSMarc Zyngier 	}
18929ed24f4bSMarc Zyngier 
1893eeeee719SDavid Brazdil 	if (is_protected_kvm_enabled()) {
189494f5e8a4SDavid Brazdil 		init_cpu_logical_map();
189594f5e8a4SDavid Brazdil 
189652b9e265SWang Wensheng 		if (!init_psci_relay()) {
189752b9e265SWang Wensheng 			err = -ENODEV;
1898eeeee719SDavid Brazdil 			goto out_err;
1899eeeee719SDavid Brazdil 		}
190052b9e265SWang Wensheng 	}
1901eeeee719SDavid Brazdil 
1902bfa79a80SQuentin Perret 	if (is_protected_kvm_enabled()) {
1903bfa79a80SQuentin Perret 		err = kvm_hyp_init_protection(hyp_va_bits);
1904bfa79a80SQuentin Perret 		if (err) {
1905bfa79a80SQuentin Perret 			kvm_err("Failed to init hyp memory protection\n");
1906bfa79a80SQuentin Perret 			goto out_err;
1907bfa79a80SQuentin Perret 		}
1908bfa79a80SQuentin Perret 	}
19099ed24f4bSMarc Zyngier 
19109ed24f4bSMarc Zyngier 	return 0;
19119ed24f4bSMarc Zyngier 
19129ed24f4bSMarc Zyngier out_err:
19139ed24f4bSMarc Zyngier 	teardown_hyp_mode();
19149ed24f4bSMarc Zyngier 	kvm_err("error initializing Hyp mode: %d\n", err);
19159ed24f4bSMarc Zyngier 	return err;
19169ed24f4bSMarc Zyngier }
19179ed24f4bSMarc Zyngier 
1918b1306fefSXu Jia static void _kvm_host_prot_finalize(void *discard)
19191025c8c0SQuentin Perret {
19201025c8c0SQuentin Perret 	WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize));
19211025c8c0SQuentin Perret }
19221025c8c0SQuentin Perret 
192390134ac9SQuentin Perret static inline int pkvm_mark_hyp(phys_addr_t start, phys_addr_t end)
192490134ac9SQuentin Perret {
192590134ac9SQuentin Perret 	return kvm_call_hyp_nvhe(__pkvm_mark_hyp, start, end);
192690134ac9SQuentin Perret }
192790134ac9SQuentin Perret 
192890134ac9SQuentin Perret #define pkvm_mark_hyp_section(__section)		\
192990134ac9SQuentin Perret 	pkvm_mark_hyp(__pa_symbol(__section##_start),	\
193090134ac9SQuentin Perret 			__pa_symbol(__section##_end))
193190134ac9SQuentin Perret 
1932bfa79a80SQuentin Perret static int finalize_hyp_mode(void)
1933bfa79a80SQuentin Perret {
193490134ac9SQuentin Perret 	int cpu, ret;
193590134ac9SQuentin Perret 
1936bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
1937bfa79a80SQuentin Perret 		return 0;
1938bfa79a80SQuentin Perret 
193990134ac9SQuentin Perret 	ret = pkvm_mark_hyp_section(__hyp_idmap_text);
194090134ac9SQuentin Perret 	if (ret)
194190134ac9SQuentin Perret 		return ret;
194290134ac9SQuentin Perret 
194390134ac9SQuentin Perret 	ret = pkvm_mark_hyp_section(__hyp_text);
194490134ac9SQuentin Perret 	if (ret)
194590134ac9SQuentin Perret 		return ret;
194690134ac9SQuentin Perret 
194790134ac9SQuentin Perret 	ret = pkvm_mark_hyp_section(__hyp_rodata);
194890134ac9SQuentin Perret 	if (ret)
194990134ac9SQuentin Perret 		return ret;
195090134ac9SQuentin Perret 
195190134ac9SQuentin Perret 	ret = pkvm_mark_hyp_section(__hyp_bss);
195290134ac9SQuentin Perret 	if (ret)
195390134ac9SQuentin Perret 		return ret;
195490134ac9SQuentin Perret 
195590134ac9SQuentin Perret 	ret = pkvm_mark_hyp(hyp_mem_base, hyp_mem_base + hyp_mem_size);
195690134ac9SQuentin Perret 	if (ret)
195790134ac9SQuentin Perret 		return ret;
195890134ac9SQuentin Perret 
195990134ac9SQuentin Perret 	for_each_possible_cpu(cpu) {
196090134ac9SQuentin Perret 		phys_addr_t start = virt_to_phys((void *)kvm_arm_hyp_percpu_base[cpu]);
196190134ac9SQuentin Perret 		phys_addr_t end = start + (PAGE_SIZE << nvhe_percpu_order());
196290134ac9SQuentin Perret 
196390134ac9SQuentin Perret 		ret = pkvm_mark_hyp(start, end);
196490134ac9SQuentin Perret 		if (ret)
196590134ac9SQuentin Perret 			return ret;
196690134ac9SQuentin Perret 
196790134ac9SQuentin Perret 		start = virt_to_phys((void *)per_cpu(kvm_arm_hyp_stack_page, cpu));
196890134ac9SQuentin Perret 		end = start + PAGE_SIZE;
196990134ac9SQuentin Perret 		ret = pkvm_mark_hyp(start, end);
197090134ac9SQuentin Perret 		if (ret)
197190134ac9SQuentin Perret 			return ret;
197290134ac9SQuentin Perret 	}
197390134ac9SQuentin Perret 
19741025c8c0SQuentin Perret 	/*
19751025c8c0SQuentin Perret 	 * Flip the static key upfront as that may no longer be possible
19761025c8c0SQuentin Perret 	 * once the host stage 2 is installed.
19771025c8c0SQuentin Perret 	 */
1978bfa79a80SQuentin Perret 	static_branch_enable(&kvm_protected_mode_initialized);
19791025c8c0SQuentin Perret 	on_each_cpu(_kvm_host_prot_finalize, NULL, 1);
1980bfa79a80SQuentin Perret 
1981bfa79a80SQuentin Perret 	return 0;
1982bfa79a80SQuentin Perret }
1983bfa79a80SQuentin Perret 
19849ed24f4bSMarc Zyngier static void check_kvm_target_cpu(void *ret)
19859ed24f4bSMarc Zyngier {
19869ed24f4bSMarc Zyngier 	*(int *)ret = kvm_target_cpu();
19879ed24f4bSMarc Zyngier }
19889ed24f4bSMarc Zyngier 
19899ed24f4bSMarc Zyngier struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
19909ed24f4bSMarc Zyngier {
19919ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
19929ed24f4bSMarc Zyngier 	int i;
19939ed24f4bSMarc Zyngier 
19949ed24f4bSMarc Zyngier 	mpidr &= MPIDR_HWID_BITMASK;
19959ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
19969ed24f4bSMarc Zyngier 		if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
19979ed24f4bSMarc Zyngier 			return vcpu;
19989ed24f4bSMarc Zyngier 	}
19999ed24f4bSMarc Zyngier 	return NULL;
20009ed24f4bSMarc Zyngier }
20019ed24f4bSMarc Zyngier 
20029ed24f4bSMarc Zyngier bool kvm_arch_has_irq_bypass(void)
20039ed24f4bSMarc Zyngier {
20049ed24f4bSMarc Zyngier 	return true;
20059ed24f4bSMarc Zyngier }
20069ed24f4bSMarc Zyngier 
20079ed24f4bSMarc Zyngier int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
20089ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
20099ed24f4bSMarc Zyngier {
20109ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
20119ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
20129ed24f4bSMarc Zyngier 
20139ed24f4bSMarc Zyngier 	return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
20149ed24f4bSMarc Zyngier 					  &irqfd->irq_entry);
20159ed24f4bSMarc Zyngier }
20169ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
20179ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
20189ed24f4bSMarc Zyngier {
20199ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
20209ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
20219ed24f4bSMarc Zyngier 
20229ed24f4bSMarc Zyngier 	kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
20239ed24f4bSMarc Zyngier 				     &irqfd->irq_entry);
20249ed24f4bSMarc Zyngier }
20259ed24f4bSMarc Zyngier 
20269ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
20279ed24f4bSMarc Zyngier {
20289ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
20299ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
20309ed24f4bSMarc Zyngier 
20319ed24f4bSMarc Zyngier 	kvm_arm_halt_guest(irqfd->kvm);
20329ed24f4bSMarc Zyngier }
20339ed24f4bSMarc Zyngier 
20349ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
20359ed24f4bSMarc Zyngier {
20369ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
20379ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
20389ed24f4bSMarc Zyngier 
20399ed24f4bSMarc Zyngier 	kvm_arm_resume_guest(irqfd->kvm);
20409ed24f4bSMarc Zyngier }
20419ed24f4bSMarc Zyngier 
20429ed24f4bSMarc Zyngier /**
20439ed24f4bSMarc Zyngier  * Initialize Hyp-mode and memory mappings on all CPUs.
20449ed24f4bSMarc Zyngier  */
20459ed24f4bSMarc Zyngier int kvm_arch_init(void *opaque)
20469ed24f4bSMarc Zyngier {
20479ed24f4bSMarc Zyngier 	int err;
20489ed24f4bSMarc Zyngier 	int ret, cpu;
20499ed24f4bSMarc Zyngier 	bool in_hyp_mode;
20509ed24f4bSMarc Zyngier 
20519ed24f4bSMarc Zyngier 	if (!is_hyp_mode_available()) {
20529ed24f4bSMarc Zyngier 		kvm_info("HYP mode not available\n");
20539ed24f4bSMarc Zyngier 		return -ENODEV;
20549ed24f4bSMarc Zyngier 	}
20559ed24f4bSMarc Zyngier 
20569ed24f4bSMarc Zyngier 	in_hyp_mode = is_kernel_in_hyp_mode();
20579ed24f4bSMarc Zyngier 
205896d389caSRob Herring 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
205996d389caSRob Herring 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2060abf532ccSRob Herring 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2061abf532ccSRob Herring 			 "Only trusted guests should be used on this system.\n");
2062abf532ccSRob Herring 
20639ed24f4bSMarc Zyngier 	for_each_online_cpu(cpu) {
20649ed24f4bSMarc Zyngier 		smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
20659ed24f4bSMarc Zyngier 		if (ret < 0) {
20669ed24f4bSMarc Zyngier 			kvm_err("Error, CPU %d not supported!\n", cpu);
20679ed24f4bSMarc Zyngier 			return -ENODEV;
20689ed24f4bSMarc Zyngier 		}
20699ed24f4bSMarc Zyngier 	}
20709ed24f4bSMarc Zyngier 
20719ed24f4bSMarc Zyngier 	err = init_common_resources();
20729ed24f4bSMarc Zyngier 	if (err)
20739ed24f4bSMarc Zyngier 		return err;
20749ed24f4bSMarc Zyngier 
20759ed24f4bSMarc Zyngier 	err = kvm_arm_init_sve();
20769ed24f4bSMarc Zyngier 	if (err)
20779ed24f4bSMarc Zyngier 		return err;
20789ed24f4bSMarc Zyngier 
20799ed24f4bSMarc Zyngier 	if (!in_hyp_mode) {
20809ed24f4bSMarc Zyngier 		err = init_hyp_mode();
20819ed24f4bSMarc Zyngier 		if (err)
20829ed24f4bSMarc Zyngier 			goto out_err;
20839ed24f4bSMarc Zyngier 	}
20849ed24f4bSMarc Zyngier 
2085b881cdceSWill Deacon 	err = kvm_init_vector_slots();
2086b881cdceSWill Deacon 	if (err) {
2087b881cdceSWill Deacon 		kvm_err("Cannot initialise vector slots\n");
2088b881cdceSWill Deacon 		goto out_err;
2089b881cdceSWill Deacon 	}
2090b881cdceSWill Deacon 
20919ed24f4bSMarc Zyngier 	err = init_subsystems();
20929ed24f4bSMarc Zyngier 	if (err)
20939ed24f4bSMarc Zyngier 		goto out_hyp;
20949ed24f4bSMarc Zyngier 
2095bfa79a80SQuentin Perret 	if (!in_hyp_mode) {
2096bfa79a80SQuentin Perret 		err = finalize_hyp_mode();
2097bfa79a80SQuentin Perret 		if (err) {
2098bfa79a80SQuentin Perret 			kvm_err("Failed to finalize Hyp protection\n");
2099bfa79a80SQuentin Perret 			goto out_hyp;
2100bfa79a80SQuentin Perret 		}
2101bfa79a80SQuentin Perret 	}
2102bfa79a80SQuentin Perret 
2103f19f6644SDavid Brazdil 	if (is_protected_kvm_enabled()) {
21043eb681fbSDavid Brazdil 		kvm_info("Protected nVHE mode initialized successfully\n");
2105f19f6644SDavid Brazdil 	} else if (in_hyp_mode) {
21069ed24f4bSMarc Zyngier 		kvm_info("VHE mode initialized successfully\n");
2107f19f6644SDavid Brazdil 	} else {
21089ed24f4bSMarc Zyngier 		kvm_info("Hyp mode initialized successfully\n");
2109f19f6644SDavid Brazdil 	}
21109ed24f4bSMarc Zyngier 
21119ed24f4bSMarc Zyngier 	return 0;
21129ed24f4bSMarc Zyngier 
21139ed24f4bSMarc Zyngier out_hyp:
21149ed24f4bSMarc Zyngier 	hyp_cpu_pm_exit();
21159ed24f4bSMarc Zyngier 	if (!in_hyp_mode)
21169ed24f4bSMarc Zyngier 		teardown_hyp_mode();
21179ed24f4bSMarc Zyngier out_err:
21189ed24f4bSMarc Zyngier 	return err;
21199ed24f4bSMarc Zyngier }
21209ed24f4bSMarc Zyngier 
21219ed24f4bSMarc Zyngier /* NOP: Compiling as a module not supported */
21229ed24f4bSMarc Zyngier void kvm_arch_exit(void)
21239ed24f4bSMarc Zyngier {
21249ed24f4bSMarc Zyngier 	kvm_perf_teardown();
21259ed24f4bSMarc Zyngier }
21269ed24f4bSMarc Zyngier 
2127d8b369c4SDavid Brazdil static int __init early_kvm_mode_cfg(char *arg)
2128d8b369c4SDavid Brazdil {
2129d8b369c4SDavid Brazdil 	if (!arg)
2130d8b369c4SDavid Brazdil 		return -EINVAL;
2131d8b369c4SDavid Brazdil 
2132d8b369c4SDavid Brazdil 	if (strcmp(arg, "protected") == 0) {
2133d8b369c4SDavid Brazdil 		kvm_mode = KVM_MODE_PROTECTED;
2134d8b369c4SDavid Brazdil 		return 0;
2135d8b369c4SDavid Brazdil 	}
2136d8b369c4SDavid Brazdil 
21371945a067SMarc Zyngier 	if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode()))
21381945a067SMarc Zyngier 		return 0;
21391945a067SMarc Zyngier 
2140d8b369c4SDavid Brazdil 	return -EINVAL;
2141d8b369c4SDavid Brazdil }
2142d8b369c4SDavid Brazdil early_param("kvm-arm.mode", early_kvm_mode_cfg);
2143d8b369c4SDavid Brazdil 
21443eb681fbSDavid Brazdil enum kvm_mode kvm_get_mode(void)
21453eb681fbSDavid Brazdil {
21463eb681fbSDavid Brazdil 	return kvm_mode;
21473eb681fbSDavid Brazdil }
21483eb681fbSDavid Brazdil 
21499ed24f4bSMarc Zyngier static int arm_init(void)
21509ed24f4bSMarc Zyngier {
21519ed24f4bSMarc Zyngier 	int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
21529ed24f4bSMarc Zyngier 	return rc;
21539ed24f4bSMarc Zyngier }
21549ed24f4bSMarc Zyngier 
21559ed24f4bSMarc Zyngier module_init(arm_init);
2156