xref: /linux/arch/arm64/kvm/arm.c (revision 4ff910be01c0ca28c2ea8b354dd47a3a17524489)
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>
96caa5812SOliver Upton #include <linux/entry-kvm.h>
109ed24f4bSMarc Zyngier #include <linux/errno.h>
119ed24f4bSMarc Zyngier #include <linux/err.h>
129ed24f4bSMarc Zyngier #include <linux/kvm_host.h>
139ed24f4bSMarc Zyngier #include <linux/list.h>
149ed24f4bSMarc Zyngier #include <linux/module.h>
159ed24f4bSMarc Zyngier #include <linux/vmalloc.h>
169ed24f4bSMarc Zyngier #include <linux/fs.h>
179ed24f4bSMarc Zyngier #include <linux/mman.h>
189ed24f4bSMarc Zyngier #include <linux/sched.h>
1947e6223cSMarc Zyngier #include <linux/kmemleak.h>
209ed24f4bSMarc Zyngier #include <linux/kvm.h>
219ed24f4bSMarc Zyngier #include <linux/kvm_irqfd.h>
229ed24f4bSMarc Zyngier #include <linux/irqbypass.h>
239ed24f4bSMarc Zyngier #include <linux/sched/stat.h>
24eeeee719SDavid Brazdil #include <linux/psci.h>
259ed24f4bSMarc Zyngier #include <trace/events/kvm.h>
269ed24f4bSMarc Zyngier 
279ed24f4bSMarc Zyngier #define CREATE_TRACE_POINTS
289ed24f4bSMarc Zyngier #include "trace_arm.h"
299ed24f4bSMarc Zyngier 
309ed24f4bSMarc Zyngier #include <linux/uaccess.h>
319ed24f4bSMarc Zyngier #include <asm/ptrace.h>
329ed24f4bSMarc Zyngier #include <asm/mman.h>
339ed24f4bSMarc Zyngier #include <asm/tlbflush.h>
349ed24f4bSMarc Zyngier #include <asm/cacheflush.h>
359ed24f4bSMarc Zyngier #include <asm/cpufeature.h>
369ed24f4bSMarc Zyngier #include <asm/virt.h>
379ed24f4bSMarc Zyngier #include <asm/kvm_arm.h>
389ed24f4bSMarc Zyngier #include <asm/kvm_asm.h>
399ed24f4bSMarc Zyngier #include <asm/kvm_mmu.h>
409d0c063aSFuad Tabba #include <asm/kvm_pkvm.h>
419ed24f4bSMarc Zyngier #include <asm/kvm_emulate.h>
429ed24f4bSMarc Zyngier #include <asm/sections.h>
439ed24f4bSMarc Zyngier 
449ed24f4bSMarc Zyngier #include <kvm/arm_hypercalls.h>
459ed24f4bSMarc Zyngier #include <kvm/arm_pmu.h>
469ed24f4bSMarc Zyngier #include <kvm/arm_psci.h>
479ed24f4bSMarc Zyngier 
48d8b369c4SDavid Brazdil static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
49f19f6644SDavid Brazdil DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
50d8b369c4SDavid Brazdil 
5114ef9d04SMarc Zyngier DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
5214ef9d04SMarc Zyngier 
53db129d48SKalesh Singh DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
5463fec243SDavid Brazdil DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
559ed24f4bSMarc Zyngier 
569ed24f4bSMarc Zyngier static bool vgic_present;
579ed24f4bSMarc Zyngier 
589ed24f4bSMarc Zyngier static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
599ed24f4bSMarc Zyngier DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
609ed24f4bSMarc Zyngier 
619ed24f4bSMarc Zyngier int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
629ed24f4bSMarc Zyngier {
639ed24f4bSMarc Zyngier 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
649ed24f4bSMarc Zyngier }
659ed24f4bSMarc Zyngier 
669ed24f4bSMarc Zyngier int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
679ed24f4bSMarc Zyngier 			    struct kvm_enable_cap *cap)
689ed24f4bSMarc Zyngier {
699ed24f4bSMarc Zyngier 	int r;
709ed24f4bSMarc Zyngier 
719ed24f4bSMarc Zyngier 	if (cap->flags)
729ed24f4bSMarc Zyngier 		return -EINVAL;
739ed24f4bSMarc Zyngier 
749ed24f4bSMarc Zyngier 	switch (cap->cap) {
759ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
769ed24f4bSMarc Zyngier 		r = 0;
7706394531SMarc Zyngier 		set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
7806394531SMarc Zyngier 			&kvm->arch.flags);
799ed24f4bSMarc Zyngier 		break;
80673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
81c4d7c518SSteven Price 		mutex_lock(&kvm->lock);
82c4d7c518SSteven Price 		if (!system_supports_mte() || kvm->created_vcpus) {
83c4d7c518SSteven Price 			r = -EINVAL;
84c4d7c518SSteven Price 		} else {
85673638f4SSteven Price 			r = 0;
8606394531SMarc Zyngier 			set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
87c4d7c518SSteven Price 		}
88c4d7c518SSteven Price 		mutex_unlock(&kvm->lock);
89673638f4SSteven Price 		break;
90bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
91bfbab445SOliver Upton 		r = 0;
92bfbab445SOliver Upton 		set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
93bfbab445SOliver Upton 		break;
949ed24f4bSMarc Zyngier 	default:
959ed24f4bSMarc Zyngier 		r = -EINVAL;
969ed24f4bSMarc Zyngier 		break;
979ed24f4bSMarc Zyngier 	}
989ed24f4bSMarc Zyngier 
999ed24f4bSMarc Zyngier 	return r;
1009ed24f4bSMarc Zyngier }
1019ed24f4bSMarc Zyngier 
1025107000fSMarc Zyngier static int kvm_arm_default_max_vcpus(void)
1035107000fSMarc Zyngier {
1045107000fSMarc Zyngier 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
1055107000fSMarc Zyngier }
1065107000fSMarc Zyngier 
1074f1df628SMarc Zyngier static void set_default_spectre(struct kvm *kvm)
10823711a5eSMarc Zyngier {
10923711a5eSMarc Zyngier 	/*
11023711a5eSMarc Zyngier 	 * The default is to expose CSV2 == 1 if the HW isn't affected.
11123711a5eSMarc Zyngier 	 * Although this is a per-CPU feature, we make it global because
11223711a5eSMarc Zyngier 	 * asymmetric systems are just a nuisance.
11323711a5eSMarc Zyngier 	 *
11423711a5eSMarc Zyngier 	 * Userspace can override this as long as it doesn't promise
11523711a5eSMarc Zyngier 	 * the impossible.
11623711a5eSMarc Zyngier 	 */
11723711a5eSMarc Zyngier 	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
11823711a5eSMarc Zyngier 		kvm->arch.pfr0_csv2 = 1;
1194f1df628SMarc Zyngier 	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
1204f1df628SMarc Zyngier 		kvm->arch.pfr0_csv3 = 1;
12123711a5eSMarc Zyngier }
12223711a5eSMarc Zyngier 
1239ed24f4bSMarc Zyngier /**
1249ed24f4bSMarc Zyngier  * kvm_arch_init_vm - initializes a VM data structure
1259ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1269ed24f4bSMarc Zyngier  */
1279ed24f4bSMarc Zyngier int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1289ed24f4bSMarc Zyngier {
129a0e50aa3SChristoffer Dall 	int ret;
1309ed24f4bSMarc Zyngier 
131c43120afSOliver Upton 	mutex_init(&kvm->arch.config_lock);
132c43120afSOliver Upton 
133c43120afSOliver Upton #ifdef CONFIG_LOCKDEP
134c43120afSOliver Upton 	/* Clue in lockdep that the config_lock must be taken inside kvm->lock */
135c43120afSOliver Upton 	mutex_lock(&kvm->lock);
136c43120afSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
137c43120afSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
138c43120afSOliver Upton 	mutex_unlock(&kvm->lock);
139c43120afSOliver Upton #endif
140c43120afSOliver Upton 
1413f868e14SQuentin Perret 	ret = kvm_share_hyp(kvm, kvm + 1);
1429ed24f4bSMarc Zyngier 	if (ret)
143315775ffSQuentin Perret 		return ret;
1449ed24f4bSMarc Zyngier 
1459d0c063aSFuad Tabba 	ret = pkvm_init_host_vm(kvm);
1469d0c063aSFuad Tabba 	if (ret)
147315775ffSQuentin Perret 		goto err_unshare_kvm;
1489ed24f4bSMarc Zyngier 
1495f623a59SOliver Upton 	if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
150ae187fecSWill Deacon 		ret = -ENOMEM;
151315775ffSQuentin Perret 		goto err_unshare_kvm;
152ae187fecSWill Deacon 	}
153583cda1bSAlexandru Elisei 	cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
154583cda1bSAlexandru Elisei 
155315775ffSQuentin Perret 	ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
156315775ffSQuentin Perret 	if (ret)
157315775ffSQuentin Perret 		goto err_free_cpumask;
158315775ffSQuentin Perret 
1599ed24f4bSMarc Zyngier 	kvm_vgic_early_init(kvm);
1609ed24f4bSMarc Zyngier 
1619ed24f4bSMarc Zyngier 	/* The maximum number of VCPUs is limited by the host's GIC model */
162f502cc56SSean Christopherson 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
1639ed24f4bSMarc Zyngier 
1644f1df628SMarc Zyngier 	set_default_spectre(kvm);
16505714cabSRaghavendra Rao Ananta 	kvm_arm_init_hypercalls(kvm);
16623711a5eSMarc Zyngier 
1673d0dba57SMarc Zyngier 	/*
1683d0dba57SMarc Zyngier 	 * Initialise the default PMUver before there is a chance to
1693d0dba57SMarc Zyngier 	 * create an actual PMU.
1703d0dba57SMarc Zyngier 	 */
1713d0dba57SMarc Zyngier 	kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
1723d0dba57SMarc Zyngier 
173315775ffSQuentin Perret 	return 0;
174315775ffSQuentin Perret 
175315775ffSQuentin Perret err_free_cpumask:
176315775ffSQuentin Perret 	free_cpumask_var(kvm->arch.supported_cpus);
177315775ffSQuentin Perret err_unshare_kvm:
178315775ffSQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
1799ed24f4bSMarc Zyngier 	return ret;
1809ed24f4bSMarc Zyngier }
1819ed24f4bSMarc Zyngier 
1829ed24f4bSMarc Zyngier vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1839ed24f4bSMarc Zyngier {
1849ed24f4bSMarc Zyngier 	return VM_FAULT_SIGBUS;
1859ed24f4bSMarc Zyngier }
1869ed24f4bSMarc Zyngier 
1879ed24f4bSMarc Zyngier 
1889ed24f4bSMarc Zyngier /**
1899ed24f4bSMarc Zyngier  * kvm_arch_destroy_vm - destroy the VM data structure
1909ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1919ed24f4bSMarc Zyngier  */
1929ed24f4bSMarc Zyngier void kvm_arch_destroy_vm(struct kvm *kvm)
1939ed24f4bSMarc Zyngier {
194d7eec236SMarc Zyngier 	bitmap_free(kvm->arch.pmu_filter);
195583cda1bSAlexandru Elisei 	free_cpumask_var(kvm->arch.supported_cpus);
196d7eec236SMarc Zyngier 
1979ed24f4bSMarc Zyngier 	kvm_vgic_destroy(kvm);
1989ed24f4bSMarc Zyngier 
1999d0c063aSFuad Tabba 	if (is_protected_kvm_enabled())
2009d0c063aSFuad Tabba 		pkvm_destroy_hyp_vm(kvm);
2019d0c063aSFuad Tabba 
20227592ae8SMarc Zyngier 	kvm_destroy_vcpus(kvm);
20352b28657SQuentin Perret 
20452b28657SQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
2059ed24f4bSMarc Zyngier }
2069ed24f4bSMarc Zyngier 
2079ed24f4bSMarc Zyngier int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
2089ed24f4bSMarc Zyngier {
2099ed24f4bSMarc Zyngier 	int r;
2109ed24f4bSMarc Zyngier 	switch (ext) {
2119ed24f4bSMarc Zyngier 	case KVM_CAP_IRQCHIP:
2129ed24f4bSMarc Zyngier 		r = vgic_present;
2139ed24f4bSMarc Zyngier 		break;
2149ed24f4bSMarc Zyngier 	case KVM_CAP_IOEVENTFD:
2159ed24f4bSMarc Zyngier 	case KVM_CAP_DEVICE_CTRL:
2169ed24f4bSMarc Zyngier 	case KVM_CAP_USER_MEMORY:
2179ed24f4bSMarc Zyngier 	case KVM_CAP_SYNC_MMU:
2189ed24f4bSMarc Zyngier 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2199ed24f4bSMarc Zyngier 	case KVM_CAP_ONE_REG:
2209ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI:
2219ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI_0_2:
2229ed24f4bSMarc Zyngier 	case KVM_CAP_READONLY_MEM:
2239ed24f4bSMarc Zyngier 	case KVM_CAP_MP_STATE:
2249ed24f4bSMarc Zyngier 	case KVM_CAP_IMMEDIATE_EXIT:
2259ed24f4bSMarc Zyngier 	case KVM_CAP_VCPU_EVENTS:
2269ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
2279ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
2289ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_INJECT_EXT_DABT:
22936fb4cd5SWill Deacon 	case KVM_CAP_SET_GUEST_DEBUG:
23036fb4cd5SWill Deacon 	case KVM_CAP_VCPU_ATTRIBUTES:
2313bf72569SJianyong Wu 	case KVM_CAP_PTP_KVM:
232bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
2339ed24f4bSMarc Zyngier 		r = 1;
2349ed24f4bSMarc Zyngier 		break;
235fa18aca9SMaxim Levitsky 	case KVM_CAP_SET_GUEST_DEBUG2:
236fa18aca9SMaxim Levitsky 		return KVM_GUESTDBG_VALID_MASK;
2379ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
2389ed24f4bSMarc Zyngier 		r = 1;
2399ed24f4bSMarc Zyngier 		break;
2409ed24f4bSMarc Zyngier 	case KVM_CAP_NR_VCPUS:
241f60a00d7SVitaly Kuznetsov 		/*
242f60a00d7SVitaly Kuznetsov 		 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
243f60a00d7SVitaly Kuznetsov 		 * architectures, as it does not always bound it to
244f60a00d7SVitaly Kuznetsov 		 * KVM_CAP_MAX_VCPUS. It should not matter much because
245f60a00d7SVitaly Kuznetsov 		 * this is just an advisory value.
246f60a00d7SVitaly Kuznetsov 		 */
247f60a00d7SVitaly Kuznetsov 		r = min_t(unsigned int, num_online_cpus(),
248f60a00d7SVitaly Kuznetsov 			  kvm_arm_default_max_vcpus());
2499ed24f4bSMarc Zyngier 		break;
2509ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPUS:
2519ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPU_ID:
2525107000fSMarc Zyngier 		if (kvm)
253f502cc56SSean Christopherson 			r = kvm->max_vcpus;
2545107000fSMarc Zyngier 		else
2555107000fSMarc Zyngier 			r = kvm_arm_default_max_vcpus();
2569ed24f4bSMarc Zyngier 		break;
2579ed24f4bSMarc Zyngier 	case KVM_CAP_MSI_DEVID:
2589ed24f4bSMarc Zyngier 		if (!kvm)
2599ed24f4bSMarc Zyngier 			r = -EINVAL;
2609ed24f4bSMarc Zyngier 		else
2619ed24f4bSMarc Zyngier 			r = kvm->arch.vgic.msis_require_devid;
2629ed24f4bSMarc Zyngier 		break;
2639ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_USER_IRQ:
2649ed24f4bSMarc Zyngier 		/*
2659ed24f4bSMarc Zyngier 		 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
2669ed24f4bSMarc Zyngier 		 * (bump this number if adding more devices)
2679ed24f4bSMarc Zyngier 		 */
2689ed24f4bSMarc Zyngier 		r = 1;
2699ed24f4bSMarc Zyngier 		break;
270673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
271673638f4SSteven Price 		r = system_supports_mte();
272673638f4SSteven Price 		break;
273004a0124SAndrew Jones 	case KVM_CAP_STEAL_TIME:
274004a0124SAndrew Jones 		r = kvm_arm_pvtime_supported();
275004a0124SAndrew Jones 		break;
27636fb4cd5SWill Deacon 	case KVM_CAP_ARM_EL1_32BIT:
27736fb4cd5SWill Deacon 		r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
2789ed24f4bSMarc Zyngier 		break;
27936fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
28036fb4cd5SWill Deacon 		r = get_num_brps();
28136fb4cd5SWill Deacon 		break;
28236fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
28336fb4cd5SWill Deacon 		r = get_num_wrps();
28436fb4cd5SWill Deacon 		break;
28536fb4cd5SWill Deacon 	case KVM_CAP_ARM_PMU_V3:
28636fb4cd5SWill Deacon 		r = kvm_arm_support_pmu_v3();
28736fb4cd5SWill Deacon 		break;
28836fb4cd5SWill Deacon 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
28936fb4cd5SWill Deacon 		r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
29036fb4cd5SWill Deacon 		break;
29136fb4cd5SWill Deacon 	case KVM_CAP_ARM_VM_IPA_SIZE:
29236fb4cd5SWill Deacon 		r = get_kvm_ipa_limit();
29336fb4cd5SWill Deacon 		break;
29436fb4cd5SWill Deacon 	case KVM_CAP_ARM_SVE:
29536fb4cd5SWill Deacon 		r = system_supports_sve();
29636fb4cd5SWill Deacon 		break;
29736fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
29836fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
29936fb4cd5SWill Deacon 		r = system_has_full_ptr_auth();
30036fb4cd5SWill Deacon 		break;
30136fb4cd5SWill Deacon 	default:
30236fb4cd5SWill Deacon 		r = 0;
3039ed24f4bSMarc Zyngier 	}
30436fb4cd5SWill Deacon 
3059ed24f4bSMarc Zyngier 	return r;
3069ed24f4bSMarc Zyngier }
3079ed24f4bSMarc Zyngier 
3089ed24f4bSMarc Zyngier long kvm_arch_dev_ioctl(struct file *filp,
3099ed24f4bSMarc Zyngier 			unsigned int ioctl, unsigned long arg)
3109ed24f4bSMarc Zyngier {
3119ed24f4bSMarc Zyngier 	return -EINVAL;
3129ed24f4bSMarc Zyngier }
3139ed24f4bSMarc Zyngier 
3149ed24f4bSMarc Zyngier struct kvm *kvm_arch_alloc_vm(void)
3159ed24f4bSMarc Zyngier {
316115bae92SJia He 	size_t sz = sizeof(struct kvm);
3179ed24f4bSMarc Zyngier 
318115bae92SJia He 	if (!has_vhe())
319115bae92SJia He 		return kzalloc(sz, GFP_KERNEL_ACCOUNT);
320115bae92SJia He 
321115bae92SJia He 	return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
3229ed24f4bSMarc Zyngier }
3239ed24f4bSMarc Zyngier 
3249ed24f4bSMarc Zyngier int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3259ed24f4bSMarc Zyngier {
3269ed24f4bSMarc Zyngier 	if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
3279ed24f4bSMarc Zyngier 		return -EBUSY;
3289ed24f4bSMarc Zyngier 
329f502cc56SSean Christopherson 	if (id >= kvm->max_vcpus)
3309ed24f4bSMarc Zyngier 		return -EINVAL;
3319ed24f4bSMarc Zyngier 
3329ed24f4bSMarc Zyngier 	return 0;
3339ed24f4bSMarc Zyngier }
3349ed24f4bSMarc Zyngier 
3359ed24f4bSMarc Zyngier int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3369ed24f4bSMarc Zyngier {
3379ed24f4bSMarc Zyngier 	int err;
3389ed24f4bSMarc Zyngier 
3390acc7239SOliver Upton 	spin_lock_init(&vcpu->arch.mp_state_lock);
3400acc7239SOliver Upton 
341c43120afSOliver Upton #ifdef CONFIG_LOCKDEP
342c43120afSOliver Upton 	/* Inform lockdep that the config_lock is acquired after vcpu->mutex */
343c43120afSOliver Upton 	mutex_lock(&vcpu->mutex);
344c43120afSOliver Upton 	mutex_lock(&vcpu->kvm->arch.config_lock);
345c43120afSOliver Upton 	mutex_unlock(&vcpu->kvm->arch.config_lock);
346c43120afSOliver Upton 	mutex_unlock(&vcpu->mutex);
347c43120afSOliver Upton #endif
348c43120afSOliver Upton 
3499ed24f4bSMarc Zyngier 	/* Force users to call KVM_ARM_VCPU_INIT */
3509ed24f4bSMarc Zyngier 	vcpu->arch.target = -1;
3519ed24f4bSMarc Zyngier 	bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
3529ed24f4bSMarc Zyngier 
353e539451bSSean Christopherson 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
354e539451bSSean Christopherson 
355b4da9187SMarc Zyngier 	/*
356b4da9187SMarc Zyngier 	 * Default value for the FP state, will be overloaded at load
357b4da9187SMarc Zyngier 	 * time if we support FP (pretty likely)
358b4da9187SMarc Zyngier 	 */
359b4da9187SMarc Zyngier 	vcpu->arch.fp_state = FP_STATE_FREE;
360b4da9187SMarc Zyngier 
3619ed24f4bSMarc Zyngier 	/* Set up the timer */
3629ed24f4bSMarc Zyngier 	kvm_timer_vcpu_init(vcpu);
3639ed24f4bSMarc Zyngier 
3649ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_init(vcpu);
3659ed24f4bSMarc Zyngier 
3669ed24f4bSMarc Zyngier 	kvm_arm_reset_debug_ptr(vcpu);
3679ed24f4bSMarc Zyngier 
3689ed24f4bSMarc Zyngier 	kvm_arm_pvtime_vcpu_init(&vcpu->arch);
3699ed24f4bSMarc Zyngier 
370a0e50aa3SChristoffer Dall 	vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
371a0e50aa3SChristoffer Dall 
3729ed24f4bSMarc Zyngier 	err = kvm_vgic_vcpu_init(vcpu);
3739ed24f4bSMarc Zyngier 	if (err)
3749ed24f4bSMarc Zyngier 		return err;
3759ed24f4bSMarc Zyngier 
3763f868e14SQuentin Perret 	return kvm_share_hyp(vcpu, vcpu + 1);
3779ed24f4bSMarc Zyngier }
3789ed24f4bSMarc Zyngier 
3799ed24f4bSMarc Zyngier void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
3809ed24f4bSMarc Zyngier {
3819ed24f4bSMarc Zyngier }
3829ed24f4bSMarc Zyngier 
3839ed24f4bSMarc Zyngier void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3849ed24f4bSMarc Zyngier {
385cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
3869ed24f4bSMarc Zyngier 		static_branch_dec(&userspace_irqchip_in_use);
3879ed24f4bSMarc Zyngier 
3889af3e08bSWill Deacon 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
3899ed24f4bSMarc Zyngier 	kvm_timer_vcpu_terminate(vcpu);
3909ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_destroy(vcpu);
3919ed24f4bSMarc Zyngier 
3929ed24f4bSMarc Zyngier 	kvm_arm_vcpu_destroy(vcpu);
3939ed24f4bSMarc Zyngier }
3949ed24f4bSMarc Zyngier 
3959ed24f4bSMarc Zyngier void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
3969ed24f4bSMarc Zyngier {
3976109c5a6SSean Christopherson 
3989ed24f4bSMarc Zyngier }
3999ed24f4bSMarc Zyngier 
4009ed24f4bSMarc Zyngier void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
4019ed24f4bSMarc Zyngier {
4026109c5a6SSean Christopherson 
4039ed24f4bSMarc Zyngier }
4049ed24f4bSMarc Zyngier 
4059ed24f4bSMarc Zyngier void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
4069ed24f4bSMarc Zyngier {
407a0e50aa3SChristoffer Dall 	struct kvm_s2_mmu *mmu;
4089ed24f4bSMarc Zyngier 	int *last_ran;
4099ed24f4bSMarc Zyngier 
410a0e50aa3SChristoffer Dall 	mmu = vcpu->arch.hw_mmu;
411a0e50aa3SChristoffer Dall 	last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
4129ed24f4bSMarc Zyngier 
4139ed24f4bSMarc Zyngier 	/*
41401dc9262SMarc Zyngier 	 * We guarantee that both TLBs and I-cache are private to each
41501dc9262SMarc Zyngier 	 * vcpu. If detecting that a vcpu from the same VM has
41601dc9262SMarc Zyngier 	 * previously run on the same physical CPU, call into the
41701dc9262SMarc Zyngier 	 * hypervisor code to nuke the relevant contexts.
41801dc9262SMarc Zyngier 	 *
4199ed24f4bSMarc Zyngier 	 * We might get preempted before the vCPU actually runs, but
4209ed24f4bSMarc Zyngier 	 * over-invalidation doesn't affect correctness.
4219ed24f4bSMarc Zyngier 	 */
4229ed24f4bSMarc Zyngier 	if (*last_ran != vcpu->vcpu_id) {
42301dc9262SMarc Zyngier 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
4249ed24f4bSMarc Zyngier 		*last_ran = vcpu->vcpu_id;
4259ed24f4bSMarc Zyngier 	}
4269ed24f4bSMarc Zyngier 
4279ed24f4bSMarc Zyngier 	vcpu->cpu = cpu;
4289ed24f4bSMarc Zyngier 
4299ed24f4bSMarc Zyngier 	kvm_vgic_load(vcpu);
4309ed24f4bSMarc Zyngier 	kvm_timer_vcpu_load(vcpu);
43113aeb9b4SDavid Brazdil 	if (has_vhe())
43213aeb9b4SDavid Brazdil 		kvm_vcpu_load_sysregs_vhe(vcpu);
4339ed24f4bSMarc Zyngier 	kvm_arch_vcpu_load_fp(vcpu);
4349ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_guest(vcpu);
4359ed24f4bSMarc Zyngier 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
4369ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
4379ed24f4bSMarc Zyngier 
4389ed24f4bSMarc Zyngier 	if (single_task_running())
4399ed24f4bSMarc Zyngier 		vcpu_clear_wfx_traps(vcpu);
4409ed24f4bSMarc Zyngier 	else
4419ed24f4bSMarc Zyngier 		vcpu_set_wfx_traps(vcpu);
4429ed24f4bSMarc Zyngier 
44329eb5a3cSMarc Zyngier 	if (vcpu_has_ptrauth(vcpu))
444ef3e40a7SMarc Zyngier 		vcpu_ptrauth_disable(vcpu);
445d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_load_debug_state_flags(vcpu);
446583cda1bSAlexandru Elisei 
447583cda1bSAlexandru Elisei 	if (!cpumask_test_cpu(smp_processor_id(), vcpu->kvm->arch.supported_cpus))
448583cda1bSAlexandru Elisei 		vcpu_set_on_unsupported_cpu(vcpu);
4499ed24f4bSMarc Zyngier }
4509ed24f4bSMarc Zyngier 
4519ed24f4bSMarc Zyngier void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4529ed24f4bSMarc Zyngier {
453d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_put_debug_state_flags(vcpu);
4549ed24f4bSMarc Zyngier 	kvm_arch_vcpu_put_fp(vcpu);
45513aeb9b4SDavid Brazdil 	if (has_vhe())
45613aeb9b4SDavid Brazdil 		kvm_vcpu_put_sysregs_vhe(vcpu);
4579ed24f4bSMarc Zyngier 	kvm_timer_vcpu_put(vcpu);
4589ed24f4bSMarc Zyngier 	kvm_vgic_put(vcpu);
4599ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_host(vcpu);
460100b4f09SShameer Kolothum 	kvm_arm_vmid_clear_active();
4619ed24f4bSMarc Zyngier 
462583cda1bSAlexandru Elisei 	vcpu_clear_on_unsupported_cpu(vcpu);
4639ed24f4bSMarc Zyngier 	vcpu->cpu = -1;
4649ed24f4bSMarc Zyngier }
4659ed24f4bSMarc Zyngier 
4660acc7239SOliver Upton static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
4679ed24f4bSMarc Zyngier {
4680acc7239SOliver Upton 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
4699ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
4709ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
4719ed24f4bSMarc Zyngier }
4729ed24f4bSMarc Zyngier 
4730acc7239SOliver Upton void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
4740acc7239SOliver Upton {
4750acc7239SOliver Upton 	spin_lock(&vcpu->arch.mp_state_lock);
4760acc7239SOliver Upton 	__kvm_arm_vcpu_power_off(vcpu);
4770acc7239SOliver Upton 	spin_unlock(&vcpu->arch.mp_state_lock);
4780acc7239SOliver Upton }
4790acc7239SOliver Upton 
480b171f9bbSOliver Upton bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
481b171f9bbSOliver Upton {
4820acc7239SOliver Upton 	return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
483b171f9bbSOliver Upton }
484b171f9bbSOliver Upton 
4857b33a09dSOliver Upton static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
4867b33a09dSOliver Upton {
4870acc7239SOliver Upton 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
4887b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
4897b33a09dSOliver Upton 	kvm_vcpu_kick(vcpu);
4907b33a09dSOliver Upton }
4917b33a09dSOliver Upton 
4927b33a09dSOliver Upton static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
4937b33a09dSOliver Upton {
4940acc7239SOliver Upton 	return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
4957b33a09dSOliver Upton }
4967b33a09dSOliver Upton 
4979ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4989ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
4999ed24f4bSMarc Zyngier {
5000acc7239SOliver Upton 	*mp_state = READ_ONCE(vcpu->arch.mp_state);
5019ed24f4bSMarc Zyngier 
5029ed24f4bSMarc Zyngier 	return 0;
5039ed24f4bSMarc Zyngier }
5049ed24f4bSMarc Zyngier 
5059ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
5069ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
5079ed24f4bSMarc Zyngier {
5089ed24f4bSMarc Zyngier 	int ret = 0;
5099ed24f4bSMarc Zyngier 
5100acc7239SOliver Upton 	spin_lock(&vcpu->arch.mp_state_lock);
5110acc7239SOliver Upton 
5129ed24f4bSMarc Zyngier 	switch (mp_state->mp_state) {
5139ed24f4bSMarc Zyngier 	case KVM_MP_STATE_RUNNABLE:
5140acc7239SOliver Upton 		WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
5159ed24f4bSMarc Zyngier 		break;
5169ed24f4bSMarc Zyngier 	case KVM_MP_STATE_STOPPED:
5170acc7239SOliver Upton 		__kvm_arm_vcpu_power_off(vcpu);
5189ed24f4bSMarc Zyngier 		break;
5197b33a09dSOliver Upton 	case KVM_MP_STATE_SUSPENDED:
5207b33a09dSOliver Upton 		kvm_arm_vcpu_suspend(vcpu);
5219ed24f4bSMarc Zyngier 		break;
5229ed24f4bSMarc Zyngier 	default:
5239ed24f4bSMarc Zyngier 		ret = -EINVAL;
5249ed24f4bSMarc Zyngier 	}
5259ed24f4bSMarc Zyngier 
5260acc7239SOliver Upton 	spin_unlock(&vcpu->arch.mp_state_lock);
5270acc7239SOliver Upton 
5289ed24f4bSMarc Zyngier 	return ret;
5299ed24f4bSMarc Zyngier }
5309ed24f4bSMarc Zyngier 
5319ed24f4bSMarc Zyngier /**
5329ed24f4bSMarc Zyngier  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
5339ed24f4bSMarc Zyngier  * @v:		The VCPU pointer
5349ed24f4bSMarc Zyngier  *
5359ed24f4bSMarc Zyngier  * If the guest CPU is not waiting for interrupts or an interrupt line is
5369ed24f4bSMarc Zyngier  * asserted, the CPU is by definition runnable.
5379ed24f4bSMarc Zyngier  */
5389ed24f4bSMarc Zyngier int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
5399ed24f4bSMarc Zyngier {
5409ed24f4bSMarc Zyngier 	bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
5419ed24f4bSMarc Zyngier 	return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
542b171f9bbSOliver Upton 		&& !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
5439ed24f4bSMarc Zyngier }
5449ed24f4bSMarc Zyngier 
5459ed24f4bSMarc Zyngier bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
5469ed24f4bSMarc Zyngier {
5479ed24f4bSMarc Zyngier 	return vcpu_mode_priv(vcpu);
5489ed24f4bSMarc Zyngier }
5499ed24f4bSMarc Zyngier 
5508e5b0adeSLinus Torvalds #ifdef CONFIG_GUEST_PERF_EVENTS
551e1bfc245SSean Christopherson unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
552e1bfc245SSean Christopherson {
553e1bfc245SSean Christopherson 	return *vcpu_pc(vcpu);
554e1bfc245SSean Christopherson }
5558e5b0adeSLinus Torvalds #endif
556e1bfc245SSean Christopherson 
557b5aa368aSMarc Zyngier static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
558052f064dSMarc Zyngier {
559b5aa368aSMarc Zyngier 	return vcpu->arch.target >= 0;
560052f064dSMarc Zyngier }
561052f064dSMarc Zyngier 
562b5aa368aSMarc Zyngier /*
563b5aa368aSMarc Zyngier  * Handle both the initialisation that is being done when the vcpu is
564b5aa368aSMarc Zyngier  * run for the first time, as well as the updates that must be
565b5aa368aSMarc Zyngier  * performed each time we get a new thread dealing with this vcpu.
566b5aa368aSMarc Zyngier  */
567b5aa368aSMarc Zyngier int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
5689ed24f4bSMarc Zyngier {
5699ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
570b5aa368aSMarc Zyngier 	int ret;
5719ed24f4bSMarc Zyngier 
572b5aa368aSMarc Zyngier 	if (!kvm_vcpu_initialized(vcpu))
573b5aa368aSMarc Zyngier 		return -ENOEXEC;
5749ed24f4bSMarc Zyngier 
5759ed24f4bSMarc Zyngier 	if (!kvm_arm_vcpu_is_finalized(vcpu))
5769ed24f4bSMarc Zyngier 		return -EPERM;
5779ed24f4bSMarc Zyngier 
578b5aa368aSMarc Zyngier 	ret = kvm_arch_vcpu_run_map_fp(vcpu);
579b5aa368aSMarc Zyngier 	if (ret)
580b5aa368aSMarc Zyngier 		return ret;
581b5aa368aSMarc Zyngier 
582cc5705fbSMarc Zyngier 	if (likely(vcpu_has_run_once(vcpu)))
583b5aa368aSMarc Zyngier 		return 0;
5849ed24f4bSMarc Zyngier 
585263d6287SAlexandru Elisei 	kvm_arm_vcpu_init_debug(vcpu);
586263d6287SAlexandru Elisei 
5879ed24f4bSMarc Zyngier 	if (likely(irqchip_in_kernel(kvm))) {
5889ed24f4bSMarc Zyngier 		/*
5899ed24f4bSMarc Zyngier 		 * Map the VGIC hardware resources before running a vcpu the
5909ed24f4bSMarc Zyngier 		 * first time on this VM.
5919ed24f4bSMarc Zyngier 		 */
5929ed24f4bSMarc Zyngier 		ret = kvm_vgic_map_resources(kvm);
5939ed24f4bSMarc Zyngier 		if (ret)
5949ed24f4bSMarc Zyngier 			return ret;
5959ed24f4bSMarc Zyngier 	}
5969ed24f4bSMarc Zyngier 
5979ed24f4bSMarc Zyngier 	ret = kvm_timer_enable(vcpu);
5989ed24f4bSMarc Zyngier 	if (ret)
5999ed24f4bSMarc Zyngier 		return ret;
6009ed24f4bSMarc Zyngier 
6019ed24f4bSMarc Zyngier 	ret = kvm_arm_pmu_v3_enable(vcpu);
6021408e73dSMarc Zyngier 	if (ret)
6031408e73dSMarc Zyngier 		return ret;
6041408e73dSMarc Zyngier 
6059d0c063aSFuad Tabba 	if (is_protected_kvm_enabled()) {
6069d0c063aSFuad Tabba 		ret = pkvm_create_hyp_vm(kvm);
6079d0c063aSFuad Tabba 		if (ret)
6089d0c063aSFuad Tabba 			return ret;
6099d0c063aSFuad Tabba 	}
6109d0c063aSFuad Tabba 
6111408e73dSMarc Zyngier 	if (!irqchip_in_kernel(kvm)) {
6121408e73dSMarc Zyngier 		/*
6131408e73dSMarc Zyngier 		 * Tell the rest of the code that there are userspace irqchip
6141408e73dSMarc Zyngier 		 * VMs in the wild.
6151408e73dSMarc Zyngier 		 */
6161408e73dSMarc Zyngier 		static_branch_inc(&userspace_irqchip_in_use);
6171408e73dSMarc Zyngier 	}
6189ed24f4bSMarc Zyngier 
6192a0c3433SFuad Tabba 	/*
6202a0c3433SFuad Tabba 	 * Initialize traps for protected VMs.
6212a0c3433SFuad Tabba 	 * NOTE: Move to run in EL2 directly, rather than via a hypercall, once
6222a0c3433SFuad Tabba 	 * the code is in place for first run initialization at EL2.
6232a0c3433SFuad Tabba 	 */
6242a0c3433SFuad Tabba 	if (kvm_vm_is_protected(kvm))
6252a0c3433SFuad Tabba 		kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu);
6262a0c3433SFuad Tabba 
6274bba7f7dSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
62806394531SMarc Zyngier 	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
6294bba7f7dSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
6305177fe91SMarc Zyngier 
6319ed24f4bSMarc Zyngier 	return ret;
6329ed24f4bSMarc Zyngier }
6339ed24f4bSMarc Zyngier 
6349ed24f4bSMarc Zyngier bool kvm_arch_intc_initialized(struct kvm *kvm)
6359ed24f4bSMarc Zyngier {
6369ed24f4bSMarc Zyngier 	return vgic_initialized(kvm);
6379ed24f4bSMarc Zyngier }
6389ed24f4bSMarc Zyngier 
6399ed24f4bSMarc Zyngier void kvm_arm_halt_guest(struct kvm *kvm)
6409ed24f4bSMarc Zyngier {
64146808a4cSMarc Zyngier 	unsigned long i;
6429ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
6439ed24f4bSMarc Zyngier 
6449ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm)
6459ed24f4bSMarc Zyngier 		vcpu->arch.pause = true;
6469ed24f4bSMarc Zyngier 	kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
6479ed24f4bSMarc Zyngier }
6489ed24f4bSMarc Zyngier 
6499ed24f4bSMarc Zyngier void kvm_arm_resume_guest(struct kvm *kvm)
6509ed24f4bSMarc Zyngier {
65146808a4cSMarc Zyngier 	unsigned long i;
6529ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
6539ed24f4bSMarc Zyngier 
6549ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
6559ed24f4bSMarc Zyngier 		vcpu->arch.pause = false;
656d92a5d1cSSean Christopherson 		__kvm_vcpu_wake_up(vcpu);
6579ed24f4bSMarc Zyngier 	}
6589ed24f4bSMarc Zyngier }
6599ed24f4bSMarc Zyngier 
6601c6219e3SOliver Upton static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
6619ed24f4bSMarc Zyngier {
66238060944SPaolo Bonzini 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
6639ed24f4bSMarc Zyngier 
66438060944SPaolo Bonzini 	rcuwait_wait_event(wait,
665b171f9bbSOliver Upton 			   (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
66638060944SPaolo Bonzini 			   TASK_INTERRUPTIBLE);
6679ed24f4bSMarc Zyngier 
668b171f9bbSOliver Upton 	if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
6699ed24f4bSMarc Zyngier 		/* Awaken to handle a signal, request we sleep again later. */
6709ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_SLEEP, vcpu);
6719ed24f4bSMarc Zyngier 	}
6729ed24f4bSMarc Zyngier 
6739ed24f4bSMarc Zyngier 	/*
6749ed24f4bSMarc Zyngier 	 * Make sure we will observe a potential reset request if we've
6759ed24f4bSMarc Zyngier 	 * observed a change to the power state. Pairs with the smp_wmb() in
6769ed24f4bSMarc Zyngier 	 * kvm_psci_vcpu_on().
6779ed24f4bSMarc Zyngier 	 */
6789ed24f4bSMarc Zyngier 	smp_rmb();
6799ed24f4bSMarc Zyngier }
6809ed24f4bSMarc Zyngier 
6816109c5a6SSean Christopherson /**
6826109c5a6SSean Christopherson  * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
6836109c5a6SSean Christopherson  * @vcpu:	The VCPU pointer
6846109c5a6SSean Christopherson  *
6856109c5a6SSean Christopherson  * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
6866109c5a6SSean Christopherson  * the vCPU is runnable.  The vCPU may or may not be scheduled out, depending
6876109c5a6SSean Christopherson  * on when a wake event arrives, e.g. there may already be a pending wake event.
6886109c5a6SSean Christopherson  */
6896109c5a6SSean Christopherson void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
6906109c5a6SSean Christopherson {
6916109c5a6SSean Christopherson 	/*
6926109c5a6SSean Christopherson 	 * Sync back the state of the GIC CPU interface so that we have
6936109c5a6SSean Christopherson 	 * the latest PMR and group enables. This ensures that
6946109c5a6SSean Christopherson 	 * kvm_arch_vcpu_runnable has up-to-date data to decide whether
6956109c5a6SSean Christopherson 	 * we have pending interrupts, e.g. when determining if the
6966109c5a6SSean Christopherson 	 * vCPU should block.
6976109c5a6SSean Christopherson 	 *
6986109c5a6SSean Christopherson 	 * For the same reason, we want to tell GICv4 that we need
6996109c5a6SSean Christopherson 	 * doorbells to be signalled, should an interrupt become pending.
7006109c5a6SSean Christopherson 	 */
7016109c5a6SSean Christopherson 	preempt_disable();
7026109c5a6SSean Christopherson 	kvm_vgic_vmcr_sync(vcpu);
7036109c5a6SSean Christopherson 	vgic_v4_put(vcpu, true);
7046109c5a6SSean Christopherson 	preempt_enable();
7056109c5a6SSean Christopherson 
70691b99ea7SSean Christopherson 	kvm_vcpu_halt(vcpu);
707eebc538dSMarc Zyngier 	vcpu_clear_flag(vcpu, IN_WFIT);
7086109c5a6SSean Christopherson 
7096109c5a6SSean Christopherson 	preempt_disable();
7106109c5a6SSean Christopherson 	vgic_v4_load(vcpu);
7116109c5a6SSean Christopherson 	preempt_enable();
7126109c5a6SSean Christopherson }
7136109c5a6SSean Christopherson 
7147b33a09dSOliver Upton static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
7157b33a09dSOliver Upton {
7167b33a09dSOliver Upton 	if (!kvm_arm_vcpu_suspended(vcpu))
7177b33a09dSOliver Upton 		return 1;
7187b33a09dSOliver Upton 
7197b33a09dSOliver Upton 	kvm_vcpu_wfi(vcpu);
7207b33a09dSOliver Upton 
7217b33a09dSOliver Upton 	/*
7227b33a09dSOliver Upton 	 * The suspend state is sticky; we do not leave it until userspace
7237b33a09dSOliver Upton 	 * explicitly marks the vCPU as runnable. Request that we suspend again
7247b33a09dSOliver Upton 	 * later.
7257b33a09dSOliver Upton 	 */
7267b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
7277b33a09dSOliver Upton 
7287b33a09dSOliver Upton 	/*
7297b33a09dSOliver Upton 	 * Check to make sure the vCPU is actually runnable. If so, exit to
7307b33a09dSOliver Upton 	 * userspace informing it of the wakeup condition.
7317b33a09dSOliver Upton 	 */
7327b33a09dSOliver Upton 	if (kvm_arch_vcpu_runnable(vcpu)) {
7337b33a09dSOliver Upton 		memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
7347b33a09dSOliver Upton 		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
7357b33a09dSOliver Upton 		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
7367b33a09dSOliver Upton 		return 0;
7377b33a09dSOliver Upton 	}
7387b33a09dSOliver Upton 
7397b33a09dSOliver Upton 	/*
7407b33a09dSOliver Upton 	 * Otherwise, we were unblocked to process a different event, such as a
7417b33a09dSOliver Upton 	 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
7427b33a09dSOliver Upton 	 * process the event.
7437b33a09dSOliver Upton 	 */
7447b33a09dSOliver Upton 	return 1;
7457b33a09dSOliver Upton }
7467b33a09dSOliver Upton 
7473fdd0459SOliver Upton /**
7483fdd0459SOliver Upton  * check_vcpu_requests - check and handle pending vCPU requests
7493fdd0459SOliver Upton  * @vcpu:	the VCPU pointer
7503fdd0459SOliver Upton  *
7513fdd0459SOliver Upton  * Return: 1 if we should enter the guest
7523fdd0459SOliver Upton  *	   0 if we should exit to userspace
7533fdd0459SOliver Upton  *	   < 0 if we should exit to userspace, where the return value indicates
7543fdd0459SOliver Upton  *	   an error
7553fdd0459SOliver Upton  */
7563fdd0459SOliver Upton static int check_vcpu_requests(struct kvm_vcpu *vcpu)
7579ed24f4bSMarc Zyngier {
7589ed24f4bSMarc Zyngier 	if (kvm_request_pending(vcpu)) {
7599ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
7601c6219e3SOliver Upton 			kvm_vcpu_sleep(vcpu);
7619ed24f4bSMarc Zyngier 
7629ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
7639ed24f4bSMarc Zyngier 			kvm_reset_vcpu(vcpu);
7649ed24f4bSMarc Zyngier 
7659ed24f4bSMarc Zyngier 		/*
7669ed24f4bSMarc Zyngier 		 * Clear IRQ_PENDING requests that were made to guarantee
7679ed24f4bSMarc Zyngier 		 * that a VCPU sees new virtual interrupts.
7689ed24f4bSMarc Zyngier 		 */
7699ed24f4bSMarc Zyngier 		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
7709ed24f4bSMarc Zyngier 
7719ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
7729ed24f4bSMarc Zyngier 			kvm_update_stolen_time(vcpu);
7739ed24f4bSMarc Zyngier 
7749ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
7759ed24f4bSMarc Zyngier 			/* The distributor enable bits were changed */
7769ed24f4bSMarc Zyngier 			preempt_disable();
7779ed24f4bSMarc Zyngier 			vgic_v4_put(vcpu, false);
7789ed24f4bSMarc Zyngier 			vgic_v4_load(vcpu);
7799ed24f4bSMarc Zyngier 			preempt_enable();
7809ed24f4bSMarc Zyngier 		}
781d0c94c49SMarc Zyngier 
782d0c94c49SMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
783d0c94c49SMarc Zyngier 			kvm_pmu_handle_pmcr(vcpu,
784d0c94c49SMarc Zyngier 					    __vcpu_sys_reg(vcpu, PMCR_EL0));
7857b33a09dSOliver Upton 
7867b33a09dSOliver Upton 		if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
7877b33a09dSOliver Upton 			return kvm_vcpu_suspend(vcpu);
7889cb1096fSGavin Shan 
7899cb1096fSGavin Shan 		if (kvm_dirty_ring_check_request(vcpu))
7909cb1096fSGavin Shan 			return 0;
7919ed24f4bSMarc Zyngier 	}
7923fdd0459SOliver Upton 
7933fdd0459SOliver Upton 	return 1;
7949ed24f4bSMarc Zyngier }
7959ed24f4bSMarc Zyngier 
7962f6a49bbSWill Deacon static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
7972f6a49bbSWill Deacon {
7982f6a49bbSWill Deacon 	if (likely(!vcpu_mode_is_32bit(vcpu)))
7992f6a49bbSWill Deacon 		return false;
8002f6a49bbSWill Deacon 
801f3c6efc7SOliver Upton 	return !kvm_supports_32bit_el0();
8022f6a49bbSWill Deacon }
8032f6a49bbSWill Deacon 
8049ed24f4bSMarc Zyngier /**
8056caa5812SOliver Upton  * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
8066caa5812SOliver Upton  * @vcpu:	The VCPU pointer
8076caa5812SOliver Upton  * @ret:	Pointer to write optional return code
8086caa5812SOliver Upton  *
8096caa5812SOliver Upton  * Returns: true if the VCPU needs to return to a preemptible + interruptible
8106caa5812SOliver Upton  *	    and skip guest entry.
8116caa5812SOliver Upton  *
8126caa5812SOliver Upton  * This function disambiguates between two different types of exits: exits to a
8136caa5812SOliver Upton  * preemptible + interruptible kernel context and exits to userspace. For an
8146caa5812SOliver Upton  * exit to userspace, this function will write the return code to ret and return
8156caa5812SOliver Upton  * true. For an exit to preemptible + interruptible kernel context (i.e. check
8166caa5812SOliver Upton  * for pending work and re-enter), return true without writing to ret.
8176caa5812SOliver Upton  */
8186caa5812SOliver Upton static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
8196caa5812SOliver Upton {
8206caa5812SOliver Upton 	struct kvm_run *run = vcpu->run;
8216caa5812SOliver Upton 
8226caa5812SOliver Upton 	/*
8236caa5812SOliver Upton 	 * If we're using a userspace irqchip, then check if we need
8246caa5812SOliver Upton 	 * to tell a userspace irqchip about timer or PMU level
8256caa5812SOliver Upton 	 * changes and if so, exit to userspace (the actual level
8266caa5812SOliver Upton 	 * state gets updated in kvm_timer_update_run and
8276caa5812SOliver Upton 	 * kvm_pmu_update_run below).
8286caa5812SOliver Upton 	 */
8296caa5812SOliver Upton 	if (static_branch_unlikely(&userspace_irqchip_in_use)) {
8306caa5812SOliver Upton 		if (kvm_timer_should_notify_user(vcpu) ||
8316caa5812SOliver Upton 		    kvm_pmu_should_notify_user(vcpu)) {
8326caa5812SOliver Upton 			*ret = -EINTR;
8336caa5812SOliver Upton 			run->exit_reason = KVM_EXIT_INTR;
8346caa5812SOliver Upton 			return true;
8356caa5812SOliver Upton 		}
8366caa5812SOliver Upton 	}
8376caa5812SOliver Upton 
838583cda1bSAlexandru Elisei 	if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
839583cda1bSAlexandru Elisei 		run->exit_reason = KVM_EXIT_FAIL_ENTRY;
840583cda1bSAlexandru Elisei 		run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
841583cda1bSAlexandru Elisei 		run->fail_entry.cpu = smp_processor_id();
842583cda1bSAlexandru Elisei 		*ret = 0;
843583cda1bSAlexandru Elisei 		return true;
844583cda1bSAlexandru Elisei 	}
845583cda1bSAlexandru Elisei 
8466caa5812SOliver Upton 	return kvm_request_pending(vcpu) ||
8476caa5812SOliver Upton 			xfer_to_guest_mode_work_pending();
8486caa5812SOliver Upton }
8496caa5812SOliver Upton 
8508cfe148aSMark Rutland /*
8518cfe148aSMark Rutland  * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
8528cfe148aSMark Rutland  * the vCPU is running.
8538cfe148aSMark Rutland  *
8548cfe148aSMark Rutland  * This must be noinstr as instrumentation may make use of RCU, and this is not
8558cfe148aSMark Rutland  * safe during the EQS.
8568cfe148aSMark Rutland  */
8578cfe148aSMark Rutland static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
8588cfe148aSMark Rutland {
8598cfe148aSMark Rutland 	int ret;
8608cfe148aSMark Rutland 
8618cfe148aSMark Rutland 	guest_state_enter_irqoff();
8628cfe148aSMark Rutland 	ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
8638cfe148aSMark Rutland 	guest_state_exit_irqoff();
8648cfe148aSMark Rutland 
8658cfe148aSMark Rutland 	return ret;
8668cfe148aSMark Rutland }
8678cfe148aSMark Rutland 
8686caa5812SOliver Upton /**
8699ed24f4bSMarc Zyngier  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
8709ed24f4bSMarc Zyngier  * @vcpu:	The VCPU pointer
8719ed24f4bSMarc Zyngier  *
8729ed24f4bSMarc Zyngier  * This function is called through the VCPU_RUN ioctl called from user space. It
8739ed24f4bSMarc Zyngier  * will execute VM code in a loop until the time slice for the process is used
8749ed24f4bSMarc Zyngier  * or some emulation is needed from user space in which case the function will
8759ed24f4bSMarc Zyngier  * return with return value 0 and with the kvm_run structure filled in with the
8769ed24f4bSMarc Zyngier  * required data for the requested emulation.
8779ed24f4bSMarc Zyngier  */
87838060944SPaolo Bonzini int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
8799ed24f4bSMarc Zyngier {
88038060944SPaolo Bonzini 	struct kvm_run *run = vcpu->run;
8819ed24f4bSMarc Zyngier 	int ret;
8829ed24f4bSMarc Zyngier 
8839ed24f4bSMarc Zyngier 	if (run->exit_reason == KVM_EXIT_MMIO) {
88474cc7e0cSTianjia Zhang 		ret = kvm_handle_mmio_return(vcpu);
8859ed24f4bSMarc Zyngier 		if (ret)
8869ed24f4bSMarc Zyngier 			return ret;
8879ed24f4bSMarc Zyngier 	}
8889ed24f4bSMarc Zyngier 
8899ed24f4bSMarc Zyngier 	vcpu_load(vcpu);
8909ed24f4bSMarc Zyngier 
891e3e880bbSZenghui Yu 	if (run->immediate_exit) {
892e3e880bbSZenghui Yu 		ret = -EINTR;
893e3e880bbSZenghui Yu 		goto out;
894e3e880bbSZenghui Yu 	}
895e3e880bbSZenghui Yu 
8969ed24f4bSMarc Zyngier 	kvm_sigset_activate(vcpu);
8979ed24f4bSMarc Zyngier 
8989ed24f4bSMarc Zyngier 	ret = 1;
8999ed24f4bSMarc Zyngier 	run->exit_reason = KVM_EXIT_UNKNOWN;
90018f3976fSAlexandru Elisei 	run->flags = 0;
9019ed24f4bSMarc Zyngier 	while (ret > 0) {
9029ed24f4bSMarc Zyngier 		/*
9039ed24f4bSMarc Zyngier 		 * Check conditions before entering the guest
9049ed24f4bSMarc Zyngier 		 */
9056caa5812SOliver Upton 		ret = xfer_to_guest_mode_handle_work(vcpu);
9066caa5812SOliver Upton 		if (!ret)
9076caa5812SOliver Upton 			ret = 1;
9089ed24f4bSMarc Zyngier 
9093fdd0459SOliver Upton 		if (ret > 0)
9103fdd0459SOliver Upton 			ret = check_vcpu_requests(vcpu);
9119ed24f4bSMarc Zyngier 
9129ed24f4bSMarc Zyngier 		/*
9139ed24f4bSMarc Zyngier 		 * Preparing the interrupts to be injected also
9149ed24f4bSMarc Zyngier 		 * involves poking the GIC, which must be done in a
9159ed24f4bSMarc Zyngier 		 * non-preemptible context.
9169ed24f4bSMarc Zyngier 		 */
9179ed24f4bSMarc Zyngier 		preempt_disable();
9189ed24f4bSMarc Zyngier 
9193248136bSJulien Grall 		/*
9203248136bSJulien Grall 		 * The VMID allocator only tracks active VMIDs per
9213248136bSJulien Grall 		 * physical CPU, and therefore the VMID allocated may not be
9223248136bSJulien Grall 		 * preserved on VMID roll-over if the task was preempted,
9233248136bSJulien Grall 		 * making a thread's VMID inactive. So we need to call
9243248136bSJulien Grall 		 * kvm_arm_vmid_update() in non-premptible context.
9253248136bSJulien Grall 		 */
9263248136bSJulien Grall 		kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid);
9273248136bSJulien Grall 
9289ed24f4bSMarc Zyngier 		kvm_pmu_flush_hwstate(vcpu);
9299ed24f4bSMarc Zyngier 
9309ed24f4bSMarc Zyngier 		local_irq_disable();
9319ed24f4bSMarc Zyngier 
9329ed24f4bSMarc Zyngier 		kvm_vgic_flush_hwstate(vcpu);
9339ed24f4bSMarc Zyngier 
93484d751a0SFuad Tabba 		kvm_pmu_update_vcpu_events(vcpu);
93584d751a0SFuad Tabba 
9369ed24f4bSMarc Zyngier 		/*
9379ed24f4bSMarc Zyngier 		 * Ensure we set mode to IN_GUEST_MODE after we disable
9389ed24f4bSMarc Zyngier 		 * interrupts and before the final VCPU requests check.
9399ed24f4bSMarc Zyngier 		 * See the comment in kvm_vcpu_exiting_guest_mode() and
9409ed24f4bSMarc Zyngier 		 * Documentation/virt/kvm/vcpu-requests.rst
9419ed24f4bSMarc Zyngier 		 */
9429ed24f4bSMarc Zyngier 		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
9439ed24f4bSMarc Zyngier 
9446caa5812SOliver Upton 		if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
9459ed24f4bSMarc Zyngier 			vcpu->mode = OUTSIDE_GUEST_MODE;
9469ed24f4bSMarc Zyngier 			isb(); /* Ensure work in x_flush_hwstate is committed */
9479ed24f4bSMarc Zyngier 			kvm_pmu_sync_hwstate(vcpu);
9489ed24f4bSMarc Zyngier 			if (static_branch_unlikely(&userspace_irqchip_in_use))
9493c5ff0c6SMarc Zyngier 				kvm_timer_sync_user(vcpu);
9509ed24f4bSMarc Zyngier 			kvm_vgic_sync_hwstate(vcpu);
9519ed24f4bSMarc Zyngier 			local_irq_enable();
9529ed24f4bSMarc Zyngier 			preempt_enable();
9539ed24f4bSMarc Zyngier 			continue;
9549ed24f4bSMarc Zyngier 		}
9559ed24f4bSMarc Zyngier 
9569ed24f4bSMarc Zyngier 		kvm_arm_setup_debug(vcpu);
957af9a0e21SMarc Zyngier 		kvm_arch_vcpu_ctxflush_fp(vcpu);
9589ed24f4bSMarc Zyngier 
9599ed24f4bSMarc Zyngier 		/**************************************************************
9609ed24f4bSMarc Zyngier 		 * Enter the guest
9619ed24f4bSMarc Zyngier 		 */
9629ed24f4bSMarc Zyngier 		trace_kvm_entry(*vcpu_pc(vcpu));
9638cfe148aSMark Rutland 		guest_timing_enter_irqoff();
9649ed24f4bSMarc Zyngier 
9658cfe148aSMark Rutland 		ret = kvm_arm_vcpu_enter_exit(vcpu);
9669ed24f4bSMarc Zyngier 
9679ed24f4bSMarc Zyngier 		vcpu->mode = OUTSIDE_GUEST_MODE;
9689ed24f4bSMarc Zyngier 		vcpu->stat.exits++;
9699ed24f4bSMarc Zyngier 		/*
9709ed24f4bSMarc Zyngier 		 * Back from guest
9719ed24f4bSMarc Zyngier 		 *************************************************************/
9729ed24f4bSMarc Zyngier 
9739ed24f4bSMarc Zyngier 		kvm_arm_clear_debug(vcpu);
9749ed24f4bSMarc Zyngier 
9759ed24f4bSMarc Zyngier 		/*
9769ed24f4bSMarc Zyngier 		 * We must sync the PMU state before the vgic state so
9779ed24f4bSMarc Zyngier 		 * that the vgic can properly sample the updated state of the
9789ed24f4bSMarc Zyngier 		 * interrupt line.
9799ed24f4bSMarc Zyngier 		 */
9809ed24f4bSMarc Zyngier 		kvm_pmu_sync_hwstate(vcpu);
9819ed24f4bSMarc Zyngier 
9829ed24f4bSMarc Zyngier 		/*
9839ed24f4bSMarc Zyngier 		 * Sync the vgic state before syncing the timer state because
9849ed24f4bSMarc Zyngier 		 * the timer code needs to know if the virtual timer
9859ed24f4bSMarc Zyngier 		 * interrupts are active.
9869ed24f4bSMarc Zyngier 		 */
9879ed24f4bSMarc Zyngier 		kvm_vgic_sync_hwstate(vcpu);
9889ed24f4bSMarc Zyngier 
9899ed24f4bSMarc Zyngier 		/*
9909ed24f4bSMarc Zyngier 		 * Sync the timer hardware state before enabling interrupts as
9919ed24f4bSMarc Zyngier 		 * we don't want vtimer interrupts to race with syncing the
9929ed24f4bSMarc Zyngier 		 * timer virtual interrupt state.
9939ed24f4bSMarc Zyngier 		 */
9949ed24f4bSMarc Zyngier 		if (static_branch_unlikely(&userspace_irqchip_in_use))
9953c5ff0c6SMarc Zyngier 			kvm_timer_sync_user(vcpu);
9969ed24f4bSMarc Zyngier 
9979ed24f4bSMarc Zyngier 		kvm_arch_vcpu_ctxsync_fp(vcpu);
9989ed24f4bSMarc Zyngier 
9999ed24f4bSMarc Zyngier 		/*
10008cfe148aSMark Rutland 		 * We must ensure that any pending interrupts are taken before
10018cfe148aSMark Rutland 		 * we exit guest timing so that timer ticks are accounted as
10028cfe148aSMark Rutland 		 * guest time. Transiently unmask interrupts so that any
10038cfe148aSMark Rutland 		 * pending interrupts are taken.
10049ed24f4bSMarc Zyngier 		 *
10058cfe148aSMark Rutland 		 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
10068cfe148aSMark Rutland 		 * context synchronization event) is necessary to ensure that
10078cfe148aSMark Rutland 		 * pending interrupts are taken.
10089ed24f4bSMarc Zyngier 		 */
1009f7659f8bSMarc Zyngier 		if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
10109ed24f4bSMarc Zyngier 			local_irq_enable();
10118cfe148aSMark Rutland 			isb();
10128cfe148aSMark Rutland 			local_irq_disable();
1013f7659f8bSMarc Zyngier 		}
10149ed24f4bSMarc Zyngier 
10158cfe148aSMark Rutland 		guest_timing_exit_irqoff();
10168cfe148aSMark Rutland 
10178cfe148aSMark Rutland 		local_irq_enable();
10188cfe148aSMark Rutland 
10199ed24f4bSMarc Zyngier 		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
10209ed24f4bSMarc Zyngier 
10219ed24f4bSMarc Zyngier 		/* Exit types that need handling before we can be preempted */
102274cc7e0cSTianjia Zhang 		handle_exit_early(vcpu, ret);
10239ed24f4bSMarc Zyngier 
10249ed24f4bSMarc Zyngier 		preempt_enable();
10259ed24f4bSMarc Zyngier 
102622f55384SQais Yousef 		/*
102722f55384SQais Yousef 		 * The ARMv8 architecture doesn't give the hypervisor
102822f55384SQais Yousef 		 * a mechanism to prevent a guest from dropping to AArch32 EL0
102922f55384SQais Yousef 		 * if implemented by the CPU. If we spot the guest in such
103022f55384SQais Yousef 		 * state and that we decided it wasn't supposed to do so (like
103122f55384SQais Yousef 		 * with the asymmetric AArch32 case), return to userspace with
103222f55384SQais Yousef 		 * a fatal error.
103322f55384SQais Yousef 		 */
10342f6a49bbSWill Deacon 		if (vcpu_mode_is_bad_32bit(vcpu)) {
103522f55384SQais Yousef 			/*
103622f55384SQais Yousef 			 * As we have caught the guest red-handed, decide that
103722f55384SQais Yousef 			 * it isn't fit for purpose anymore by making the vcpu
103822f55384SQais Yousef 			 * invalid. The VMM can try and fix it by issuing  a
103922f55384SQais Yousef 			 * KVM_ARM_VCPU_INIT if it really wants to.
104022f55384SQais Yousef 			 */
104122f55384SQais Yousef 			vcpu->arch.target = -1;
104222f55384SQais Yousef 			ret = ARM_EXCEPTION_IL;
104322f55384SQais Yousef 		}
104422f55384SQais Yousef 
104574cc7e0cSTianjia Zhang 		ret = handle_exit(vcpu, ret);
10469ed24f4bSMarc Zyngier 	}
10479ed24f4bSMarc Zyngier 
10489ed24f4bSMarc Zyngier 	/* Tell userspace about in-kernel device output levels */
10499ed24f4bSMarc Zyngier 	if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
10509ed24f4bSMarc Zyngier 		kvm_timer_update_run(vcpu);
10519ed24f4bSMarc Zyngier 		kvm_pmu_update_run(vcpu);
10529ed24f4bSMarc Zyngier 	}
10539ed24f4bSMarc Zyngier 
10549ed24f4bSMarc Zyngier 	kvm_sigset_deactivate(vcpu);
10559ed24f4bSMarc Zyngier 
1056e3e880bbSZenghui Yu out:
105726778aaaSMarc Zyngier 	/*
105826778aaaSMarc Zyngier 	 * In the unlikely event that we are returning to userspace
105926778aaaSMarc Zyngier 	 * with pending exceptions or PC adjustment, commit these
106026778aaaSMarc Zyngier 	 * adjustments in order to give userspace a consistent view of
106126778aaaSMarc Zyngier 	 * the vcpu state. Note that this relies on __kvm_adjust_pc()
106226778aaaSMarc Zyngier 	 * being preempt-safe on VHE.
106326778aaaSMarc Zyngier 	 */
1064699bb2e0SMarc Zyngier 	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1065699bb2e0SMarc Zyngier 		     vcpu_get_flag(vcpu, INCREMENT_PC)))
106626778aaaSMarc Zyngier 		kvm_call_hyp(__kvm_adjust_pc, vcpu);
106726778aaaSMarc Zyngier 
10689ed24f4bSMarc Zyngier 	vcpu_put(vcpu);
10699ed24f4bSMarc Zyngier 	return ret;
10709ed24f4bSMarc Zyngier }
10719ed24f4bSMarc Zyngier 
10729ed24f4bSMarc Zyngier static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
10739ed24f4bSMarc Zyngier {
10749ed24f4bSMarc Zyngier 	int bit_index;
10759ed24f4bSMarc Zyngier 	bool set;
10769ed24f4bSMarc Zyngier 	unsigned long *hcr;
10779ed24f4bSMarc Zyngier 
10789ed24f4bSMarc Zyngier 	if (number == KVM_ARM_IRQ_CPU_IRQ)
10799ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VI);
10809ed24f4bSMarc Zyngier 	else /* KVM_ARM_IRQ_CPU_FIQ */
10819ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VF);
10829ed24f4bSMarc Zyngier 
10839ed24f4bSMarc Zyngier 	hcr = vcpu_hcr(vcpu);
10849ed24f4bSMarc Zyngier 	if (level)
10859ed24f4bSMarc Zyngier 		set = test_and_set_bit(bit_index, hcr);
10869ed24f4bSMarc Zyngier 	else
10879ed24f4bSMarc Zyngier 		set = test_and_clear_bit(bit_index, hcr);
10889ed24f4bSMarc Zyngier 
10899ed24f4bSMarc Zyngier 	/*
10909ed24f4bSMarc Zyngier 	 * If we didn't change anything, no need to wake up or kick other CPUs
10919ed24f4bSMarc Zyngier 	 */
10929ed24f4bSMarc Zyngier 	if (set == level)
10939ed24f4bSMarc Zyngier 		return 0;
10949ed24f4bSMarc Zyngier 
10959ed24f4bSMarc Zyngier 	/*
10969ed24f4bSMarc Zyngier 	 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
10979ed24f4bSMarc Zyngier 	 * trigger a world-switch round on the running physical CPU to set the
10989ed24f4bSMarc Zyngier 	 * virtual IRQ/FIQ fields in the HCR appropriately.
10999ed24f4bSMarc Zyngier 	 */
11009ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
11019ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
11029ed24f4bSMarc Zyngier 
11039ed24f4bSMarc Zyngier 	return 0;
11049ed24f4bSMarc Zyngier }
11059ed24f4bSMarc Zyngier 
11069ed24f4bSMarc Zyngier int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
11079ed24f4bSMarc Zyngier 			  bool line_status)
11089ed24f4bSMarc Zyngier {
11099ed24f4bSMarc Zyngier 	u32 irq = irq_level->irq;
11109ed24f4bSMarc Zyngier 	unsigned int irq_type, vcpu_idx, irq_num;
11119ed24f4bSMarc Zyngier 	int nrcpus = atomic_read(&kvm->online_vcpus);
11129ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = NULL;
11139ed24f4bSMarc Zyngier 	bool level = irq_level->level;
11149ed24f4bSMarc Zyngier 
11159ed24f4bSMarc Zyngier 	irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
11169ed24f4bSMarc Zyngier 	vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
11179ed24f4bSMarc Zyngier 	vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
11189ed24f4bSMarc Zyngier 	irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
11199ed24f4bSMarc Zyngier 
11209ed24f4bSMarc Zyngier 	trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
11219ed24f4bSMarc Zyngier 
11229ed24f4bSMarc Zyngier 	switch (irq_type) {
11239ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_CPU:
11249ed24f4bSMarc Zyngier 		if (irqchip_in_kernel(kvm))
11259ed24f4bSMarc Zyngier 			return -ENXIO;
11269ed24f4bSMarc Zyngier 
11279ed24f4bSMarc Zyngier 		if (vcpu_idx >= nrcpus)
11289ed24f4bSMarc Zyngier 			return -EINVAL;
11299ed24f4bSMarc Zyngier 
11309ed24f4bSMarc Zyngier 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
11319ed24f4bSMarc Zyngier 		if (!vcpu)
11329ed24f4bSMarc Zyngier 			return -EINVAL;
11339ed24f4bSMarc Zyngier 
11349ed24f4bSMarc Zyngier 		if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
11359ed24f4bSMarc Zyngier 			return -EINVAL;
11369ed24f4bSMarc Zyngier 
11379ed24f4bSMarc Zyngier 		return vcpu_interrupt_line(vcpu, irq_num, level);
11389ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_PPI:
11399ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
11409ed24f4bSMarc Zyngier 			return -ENXIO;
11419ed24f4bSMarc Zyngier 
11429ed24f4bSMarc Zyngier 		if (vcpu_idx >= nrcpus)
11439ed24f4bSMarc Zyngier 			return -EINVAL;
11449ed24f4bSMarc Zyngier 
11459ed24f4bSMarc Zyngier 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
11469ed24f4bSMarc Zyngier 		if (!vcpu)
11479ed24f4bSMarc Zyngier 			return -EINVAL;
11489ed24f4bSMarc Zyngier 
11499ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
11509ed24f4bSMarc Zyngier 			return -EINVAL;
11519ed24f4bSMarc Zyngier 
11529ed24f4bSMarc Zyngier 		return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
11539ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_SPI:
11549ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
11559ed24f4bSMarc Zyngier 			return -ENXIO;
11569ed24f4bSMarc Zyngier 
11579ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_PRIVATE_IRQS)
11589ed24f4bSMarc Zyngier 			return -EINVAL;
11599ed24f4bSMarc Zyngier 
11609ed24f4bSMarc Zyngier 		return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
11619ed24f4bSMarc Zyngier 	}
11629ed24f4bSMarc Zyngier 
11639ed24f4bSMarc Zyngier 	return -EINVAL;
11649ed24f4bSMarc Zyngier }
11659ed24f4bSMarc Zyngier 
11669ed24f4bSMarc Zyngier static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
11679ed24f4bSMarc Zyngier 			       const struct kvm_vcpu_init *init)
11689ed24f4bSMarc Zyngier {
11699ed24f4bSMarc Zyngier 	unsigned int i, ret;
11706b7982feSAnshuman Khandual 	u32 phys_target = kvm_target_cpu();
11719ed24f4bSMarc Zyngier 
11729ed24f4bSMarc Zyngier 	if (init->target != phys_target)
11739ed24f4bSMarc Zyngier 		return -EINVAL;
11749ed24f4bSMarc Zyngier 
11759ed24f4bSMarc Zyngier 	/*
11769ed24f4bSMarc Zyngier 	 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
11779ed24f4bSMarc Zyngier 	 * use the same target.
11789ed24f4bSMarc Zyngier 	 */
11799ed24f4bSMarc Zyngier 	if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
11809ed24f4bSMarc Zyngier 		return -EINVAL;
11819ed24f4bSMarc Zyngier 
11829ed24f4bSMarc Zyngier 	/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
11839ed24f4bSMarc Zyngier 	for (i = 0; i < sizeof(init->features) * 8; i++) {
11849ed24f4bSMarc Zyngier 		bool set = (init->features[i / 32] & (1 << (i % 32)));
11859ed24f4bSMarc Zyngier 
11869ed24f4bSMarc Zyngier 		if (set && i >= KVM_VCPU_MAX_FEATURES)
11879ed24f4bSMarc Zyngier 			return -ENOENT;
11889ed24f4bSMarc Zyngier 
11899ed24f4bSMarc Zyngier 		/*
11909ed24f4bSMarc Zyngier 		 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
11919ed24f4bSMarc Zyngier 		 * use the same feature set.
11929ed24f4bSMarc Zyngier 		 */
11939ed24f4bSMarc Zyngier 		if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
11949ed24f4bSMarc Zyngier 		    test_bit(i, vcpu->arch.features) != set)
11959ed24f4bSMarc Zyngier 			return -EINVAL;
11969ed24f4bSMarc Zyngier 
11979ed24f4bSMarc Zyngier 		if (set)
11989ed24f4bSMarc Zyngier 			set_bit(i, vcpu->arch.features);
11999ed24f4bSMarc Zyngier 	}
12009ed24f4bSMarc Zyngier 
12019ed24f4bSMarc Zyngier 	vcpu->arch.target = phys_target;
12029ed24f4bSMarc Zyngier 
12039ed24f4bSMarc Zyngier 	/* Now we know what it is, we can reset it. */
12049ed24f4bSMarc Zyngier 	ret = kvm_reset_vcpu(vcpu);
12059ed24f4bSMarc Zyngier 	if (ret) {
12069ed24f4bSMarc Zyngier 		vcpu->arch.target = -1;
12079ed24f4bSMarc Zyngier 		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
12089ed24f4bSMarc Zyngier 	}
12099ed24f4bSMarc Zyngier 
12109ed24f4bSMarc Zyngier 	return ret;
12119ed24f4bSMarc Zyngier }
12129ed24f4bSMarc Zyngier 
12139ed24f4bSMarc Zyngier static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
12149ed24f4bSMarc Zyngier 					 struct kvm_vcpu_init *init)
12159ed24f4bSMarc Zyngier {
12169ed24f4bSMarc Zyngier 	int ret;
12179ed24f4bSMarc Zyngier 
12189ed24f4bSMarc Zyngier 	ret = kvm_vcpu_set_target(vcpu, init);
12199ed24f4bSMarc Zyngier 	if (ret)
12209ed24f4bSMarc Zyngier 		return ret;
12219ed24f4bSMarc Zyngier 
12229ed24f4bSMarc Zyngier 	/*
12239ed24f4bSMarc Zyngier 	 * Ensure a rebooted VM will fault in RAM pages and detect if the
12249ed24f4bSMarc Zyngier 	 * guest MMU is turned off and flush the caches as needed.
1225892713e9SZenghui Yu 	 *
12267ae2f3dbSMarc Zyngier 	 * S2FWB enforces all memory accesses to RAM being cacheable,
12277ae2f3dbSMarc Zyngier 	 * ensuring that the data side is always coherent. We still
12287ae2f3dbSMarc Zyngier 	 * need to invalidate the I-cache though, as FWB does *not*
12297ae2f3dbSMarc Zyngier 	 * imply CTR_EL0.DIC.
12309ed24f4bSMarc Zyngier 	 */
1231cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu)) {
12327ae2f3dbSMarc Zyngier 		if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
12339ed24f4bSMarc Zyngier 			stage2_unmap_vm(vcpu->kvm);
12347ae2f3dbSMarc Zyngier 		else
1235fade9c2cSFuad Tabba 			icache_inval_all_pou();
12367ae2f3dbSMarc Zyngier 	}
12379ed24f4bSMarc Zyngier 
12389ed24f4bSMarc Zyngier 	vcpu_reset_hcr(vcpu);
1239cd496228SFuad Tabba 	vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT;
12409ed24f4bSMarc Zyngier 
12419ed24f4bSMarc Zyngier 	/*
12429ed24f4bSMarc Zyngier 	 * Handle the "start in power-off" case.
12439ed24f4bSMarc Zyngier 	 */
1244*4ff910beSReiji Watanabe 	spin_lock(&vcpu->arch.mp_state_lock);
1245*4ff910beSReiji Watanabe 
12469ed24f4bSMarc Zyngier 	if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
1247*4ff910beSReiji Watanabe 		__kvm_arm_vcpu_power_off(vcpu);
12489ed24f4bSMarc Zyngier 	else
12490acc7239SOliver Upton 		WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
12509ed24f4bSMarc Zyngier 
1251*4ff910beSReiji Watanabe 	spin_unlock(&vcpu->arch.mp_state_lock);
1252*4ff910beSReiji Watanabe 
12539ed24f4bSMarc Zyngier 	return 0;
12549ed24f4bSMarc Zyngier }
12559ed24f4bSMarc Zyngier 
12569ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
12579ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
12589ed24f4bSMarc Zyngier {
12599ed24f4bSMarc Zyngier 	int ret = -ENXIO;
12609ed24f4bSMarc Zyngier 
12619ed24f4bSMarc Zyngier 	switch (attr->group) {
12629ed24f4bSMarc Zyngier 	default:
12639ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
12649ed24f4bSMarc Zyngier 		break;
12659ed24f4bSMarc Zyngier 	}
12669ed24f4bSMarc Zyngier 
12679ed24f4bSMarc Zyngier 	return ret;
12689ed24f4bSMarc Zyngier }
12699ed24f4bSMarc Zyngier 
12709ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
12719ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
12729ed24f4bSMarc Zyngier {
12739ed24f4bSMarc Zyngier 	int ret = -ENXIO;
12749ed24f4bSMarc Zyngier 
12759ed24f4bSMarc Zyngier 	switch (attr->group) {
12769ed24f4bSMarc Zyngier 	default:
12779ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
12789ed24f4bSMarc Zyngier 		break;
12799ed24f4bSMarc Zyngier 	}
12809ed24f4bSMarc Zyngier 
12819ed24f4bSMarc Zyngier 	return ret;
12829ed24f4bSMarc Zyngier }
12839ed24f4bSMarc Zyngier 
12849ed24f4bSMarc Zyngier static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
12859ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
12869ed24f4bSMarc Zyngier {
12879ed24f4bSMarc Zyngier 	int ret = -ENXIO;
12889ed24f4bSMarc Zyngier 
12899ed24f4bSMarc Zyngier 	switch (attr->group) {
12909ed24f4bSMarc Zyngier 	default:
12919ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
12929ed24f4bSMarc Zyngier 		break;
12939ed24f4bSMarc Zyngier 	}
12949ed24f4bSMarc Zyngier 
12959ed24f4bSMarc Zyngier 	return ret;
12969ed24f4bSMarc Zyngier }
12979ed24f4bSMarc Zyngier 
12989ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
12999ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
13009ed24f4bSMarc Zyngier {
13019ed24f4bSMarc Zyngier 	memset(events, 0, sizeof(*events));
13029ed24f4bSMarc Zyngier 
13039ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_get_events(vcpu, events);
13049ed24f4bSMarc Zyngier }
13059ed24f4bSMarc Zyngier 
13069ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
13079ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
13089ed24f4bSMarc Zyngier {
13099ed24f4bSMarc Zyngier 	int i;
13109ed24f4bSMarc Zyngier 
13119ed24f4bSMarc Zyngier 	/* check whether the reserved field is zero */
13129ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
13139ed24f4bSMarc Zyngier 		if (events->reserved[i])
13149ed24f4bSMarc Zyngier 			return -EINVAL;
13159ed24f4bSMarc Zyngier 
13169ed24f4bSMarc Zyngier 	/* check whether the pad field is zero */
13179ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
13189ed24f4bSMarc Zyngier 		if (events->exception.pad[i])
13199ed24f4bSMarc Zyngier 			return -EINVAL;
13209ed24f4bSMarc Zyngier 
13219ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_set_events(vcpu, events);
13229ed24f4bSMarc Zyngier }
13239ed24f4bSMarc Zyngier 
13249ed24f4bSMarc Zyngier long kvm_arch_vcpu_ioctl(struct file *filp,
13259ed24f4bSMarc Zyngier 			 unsigned int ioctl, unsigned long arg)
13269ed24f4bSMarc Zyngier {
13279ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = filp->private_data;
13289ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
13299ed24f4bSMarc Zyngier 	struct kvm_device_attr attr;
13309ed24f4bSMarc Zyngier 	long r;
13319ed24f4bSMarc Zyngier 
13329ed24f4bSMarc Zyngier 	switch (ioctl) {
13339ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_INIT: {
13349ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
13359ed24f4bSMarc Zyngier 
13369ed24f4bSMarc Zyngier 		r = -EFAULT;
13379ed24f4bSMarc Zyngier 		if (copy_from_user(&init, argp, sizeof(init)))
13389ed24f4bSMarc Zyngier 			break;
13399ed24f4bSMarc Zyngier 
13409ed24f4bSMarc Zyngier 		r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
13419ed24f4bSMarc Zyngier 		break;
13429ed24f4bSMarc Zyngier 	}
13439ed24f4bSMarc Zyngier 	case KVM_SET_ONE_REG:
13449ed24f4bSMarc Zyngier 	case KVM_GET_ONE_REG: {
13459ed24f4bSMarc Zyngier 		struct kvm_one_reg reg;
13469ed24f4bSMarc Zyngier 
13479ed24f4bSMarc Zyngier 		r = -ENOEXEC;
13489ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
13499ed24f4bSMarc Zyngier 			break;
13509ed24f4bSMarc Zyngier 
13519ed24f4bSMarc Zyngier 		r = -EFAULT;
13529ed24f4bSMarc Zyngier 		if (copy_from_user(&reg, argp, sizeof(reg)))
13539ed24f4bSMarc Zyngier 			break;
13549ed24f4bSMarc Zyngier 
13556826c684SOliver Upton 		/*
13566826c684SOliver Upton 		 * We could owe a reset due to PSCI. Handle the pending reset
13576826c684SOliver Upton 		 * here to ensure userspace register accesses are ordered after
13586826c684SOliver Upton 		 * the reset.
13596826c684SOliver Upton 		 */
13606826c684SOliver Upton 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
13616826c684SOliver Upton 			kvm_reset_vcpu(vcpu);
13626826c684SOliver Upton 
13639ed24f4bSMarc Zyngier 		if (ioctl == KVM_SET_ONE_REG)
13649ed24f4bSMarc Zyngier 			r = kvm_arm_set_reg(vcpu, &reg);
13659ed24f4bSMarc Zyngier 		else
13669ed24f4bSMarc Zyngier 			r = kvm_arm_get_reg(vcpu, &reg);
13679ed24f4bSMarc Zyngier 		break;
13689ed24f4bSMarc Zyngier 	}
13699ed24f4bSMarc Zyngier 	case KVM_GET_REG_LIST: {
13709ed24f4bSMarc Zyngier 		struct kvm_reg_list __user *user_list = argp;
13719ed24f4bSMarc Zyngier 		struct kvm_reg_list reg_list;
13729ed24f4bSMarc Zyngier 		unsigned n;
13739ed24f4bSMarc Zyngier 
13749ed24f4bSMarc Zyngier 		r = -ENOEXEC;
13759ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
13769ed24f4bSMarc Zyngier 			break;
13779ed24f4bSMarc Zyngier 
13789ed24f4bSMarc Zyngier 		r = -EPERM;
13799ed24f4bSMarc Zyngier 		if (!kvm_arm_vcpu_is_finalized(vcpu))
13809ed24f4bSMarc Zyngier 			break;
13819ed24f4bSMarc Zyngier 
13829ed24f4bSMarc Zyngier 		r = -EFAULT;
13839ed24f4bSMarc Zyngier 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
13849ed24f4bSMarc Zyngier 			break;
13859ed24f4bSMarc Zyngier 		n = reg_list.n;
13869ed24f4bSMarc Zyngier 		reg_list.n = kvm_arm_num_regs(vcpu);
13879ed24f4bSMarc Zyngier 		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
13889ed24f4bSMarc Zyngier 			break;
13899ed24f4bSMarc Zyngier 		r = -E2BIG;
13909ed24f4bSMarc Zyngier 		if (n < reg_list.n)
13919ed24f4bSMarc Zyngier 			break;
13929ed24f4bSMarc Zyngier 		r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
13939ed24f4bSMarc Zyngier 		break;
13949ed24f4bSMarc Zyngier 	}
13959ed24f4bSMarc Zyngier 	case KVM_SET_DEVICE_ATTR: {
13969ed24f4bSMarc Zyngier 		r = -EFAULT;
13979ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
13989ed24f4bSMarc Zyngier 			break;
13999ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_set_attr(vcpu, &attr);
14009ed24f4bSMarc Zyngier 		break;
14019ed24f4bSMarc Zyngier 	}
14029ed24f4bSMarc Zyngier 	case KVM_GET_DEVICE_ATTR: {
14039ed24f4bSMarc Zyngier 		r = -EFAULT;
14049ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
14059ed24f4bSMarc Zyngier 			break;
14069ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_get_attr(vcpu, &attr);
14079ed24f4bSMarc Zyngier 		break;
14089ed24f4bSMarc Zyngier 	}
14099ed24f4bSMarc Zyngier 	case KVM_HAS_DEVICE_ATTR: {
14109ed24f4bSMarc Zyngier 		r = -EFAULT;
14119ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
14129ed24f4bSMarc Zyngier 			break;
14139ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_has_attr(vcpu, &attr);
14149ed24f4bSMarc Zyngier 		break;
14159ed24f4bSMarc Zyngier 	}
14169ed24f4bSMarc Zyngier 	case KVM_GET_VCPU_EVENTS: {
14179ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
14189ed24f4bSMarc Zyngier 
14199ed24f4bSMarc Zyngier 		if (kvm_arm_vcpu_get_events(vcpu, &events))
14209ed24f4bSMarc Zyngier 			return -EINVAL;
14219ed24f4bSMarc Zyngier 
14229ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &events, sizeof(events)))
14239ed24f4bSMarc Zyngier 			return -EFAULT;
14249ed24f4bSMarc Zyngier 
14259ed24f4bSMarc Zyngier 		return 0;
14269ed24f4bSMarc Zyngier 	}
14279ed24f4bSMarc Zyngier 	case KVM_SET_VCPU_EVENTS: {
14289ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
14299ed24f4bSMarc Zyngier 
14309ed24f4bSMarc Zyngier 		if (copy_from_user(&events, argp, sizeof(events)))
14319ed24f4bSMarc Zyngier 			return -EFAULT;
14329ed24f4bSMarc Zyngier 
14339ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_set_events(vcpu, &events);
14349ed24f4bSMarc Zyngier 	}
14359ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_FINALIZE: {
14369ed24f4bSMarc Zyngier 		int what;
14379ed24f4bSMarc Zyngier 
14389ed24f4bSMarc Zyngier 		if (!kvm_vcpu_initialized(vcpu))
14399ed24f4bSMarc Zyngier 			return -ENOEXEC;
14409ed24f4bSMarc Zyngier 
14419ed24f4bSMarc Zyngier 		if (get_user(what, (const int __user *)argp))
14429ed24f4bSMarc Zyngier 			return -EFAULT;
14439ed24f4bSMarc Zyngier 
14449ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_finalize(vcpu, what);
14459ed24f4bSMarc Zyngier 	}
14469ed24f4bSMarc Zyngier 	default:
14479ed24f4bSMarc Zyngier 		r = -EINVAL;
14489ed24f4bSMarc Zyngier 	}
14499ed24f4bSMarc Zyngier 
14509ed24f4bSMarc Zyngier 	return r;
14519ed24f4bSMarc Zyngier }
14529ed24f4bSMarc Zyngier 
14539ed24f4bSMarc Zyngier void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
14549ed24f4bSMarc Zyngier {
14559ed24f4bSMarc Zyngier 
14569ed24f4bSMarc Zyngier }
14579ed24f4bSMarc Zyngier 
14589ed24f4bSMarc Zyngier void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
14596c9dd6d2SPaolo Bonzini 					const struct kvm_memory_slot *memslot)
14609ed24f4bSMarc Zyngier {
14619ed24f4bSMarc Zyngier 	kvm_flush_remote_tlbs(kvm);
14629ed24f4bSMarc Zyngier }
14639ed24f4bSMarc Zyngier 
14649ed24f4bSMarc Zyngier static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
14659ed24f4bSMarc Zyngier 					struct kvm_arm_device_addr *dev_addr)
14669ed24f4bSMarc Zyngier {
14679f968c92SMarc Zyngier 	switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
14689ed24f4bSMarc Zyngier 	case KVM_ARM_DEVICE_VGIC_V2:
14699ed24f4bSMarc Zyngier 		if (!vgic_present)
14709ed24f4bSMarc Zyngier 			return -ENXIO;
14719f968c92SMarc Zyngier 		return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
14729ed24f4bSMarc Zyngier 	default:
14739ed24f4bSMarc Zyngier 		return -ENODEV;
14749ed24f4bSMarc Zyngier 	}
14759ed24f4bSMarc Zyngier }
14769ed24f4bSMarc Zyngier 
14779ed24f4bSMarc Zyngier long kvm_arch_vm_ioctl(struct file *filp,
14789ed24f4bSMarc Zyngier 		       unsigned int ioctl, unsigned long arg)
14799ed24f4bSMarc Zyngier {
14809ed24f4bSMarc Zyngier 	struct kvm *kvm = filp->private_data;
14819ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
14829ed24f4bSMarc Zyngier 
14839ed24f4bSMarc Zyngier 	switch (ioctl) {
14849ed24f4bSMarc Zyngier 	case KVM_CREATE_IRQCHIP: {
14859ed24f4bSMarc Zyngier 		int ret;
14869ed24f4bSMarc Zyngier 		if (!vgic_present)
14879ed24f4bSMarc Zyngier 			return -ENXIO;
14889ed24f4bSMarc Zyngier 		mutex_lock(&kvm->lock);
14899ed24f4bSMarc Zyngier 		ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
14909ed24f4bSMarc Zyngier 		mutex_unlock(&kvm->lock);
14919ed24f4bSMarc Zyngier 		return ret;
14929ed24f4bSMarc Zyngier 	}
14939ed24f4bSMarc Zyngier 	case KVM_ARM_SET_DEVICE_ADDR: {
14949ed24f4bSMarc Zyngier 		struct kvm_arm_device_addr dev_addr;
14959ed24f4bSMarc Zyngier 
14969ed24f4bSMarc Zyngier 		if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
14979ed24f4bSMarc Zyngier 			return -EFAULT;
14989ed24f4bSMarc Zyngier 		return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
14999ed24f4bSMarc Zyngier 	}
15009ed24f4bSMarc Zyngier 	case KVM_ARM_PREFERRED_TARGET: {
15019ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
15029ed24f4bSMarc Zyngier 
150308e873cbSYueHaibing 		kvm_vcpu_preferred_target(&init);
15049ed24f4bSMarc Zyngier 
15059ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &init, sizeof(init)))
15069ed24f4bSMarc Zyngier 			return -EFAULT;
15079ed24f4bSMarc Zyngier 
15089ed24f4bSMarc Zyngier 		return 0;
15099ed24f4bSMarc Zyngier 	}
1510f0376edbSSteven Price 	case KVM_ARM_MTE_COPY_TAGS: {
1511f0376edbSSteven Price 		struct kvm_arm_copy_mte_tags copy_tags;
1512f0376edbSSteven Price 
1513f0376edbSSteven Price 		if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
1514f0376edbSSteven Price 			return -EFAULT;
1515f0376edbSSteven Price 		return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
1516f0376edbSSteven Price 	}
15179ed24f4bSMarc Zyngier 	default:
15189ed24f4bSMarc Zyngier 		return -EINVAL;
15199ed24f4bSMarc Zyngier 	}
15209ed24f4bSMarc Zyngier }
15219ed24f4bSMarc Zyngier 
152230c95391SDavid Brazdil static unsigned long nvhe_percpu_size(void)
152330c95391SDavid Brazdil {
152430c95391SDavid Brazdil 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
152530c95391SDavid Brazdil 		(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
152630c95391SDavid Brazdil }
152730c95391SDavid Brazdil 
152830c95391SDavid Brazdil static unsigned long nvhe_percpu_order(void)
152930c95391SDavid Brazdil {
153030c95391SDavid Brazdil 	unsigned long size = nvhe_percpu_size();
153130c95391SDavid Brazdil 
153230c95391SDavid Brazdil 	return size ? get_order(size) : 0;
153330c95391SDavid Brazdil }
153430c95391SDavid Brazdil 
1535b881cdceSWill Deacon /* A lookup table holding the hypervisor VA for each vector slot */
1536b881cdceSWill Deacon static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1537de5bcdb4SWill Deacon 
1538b881cdceSWill Deacon static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1539b881cdceSWill Deacon {
1540bc1d2892SQuentin Perret 	hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
15419ef2b48bSWill Deacon }
15429ef2b48bSWill Deacon 
1543b881cdceSWill Deacon static int kvm_init_vector_slots(void)
1544b881cdceSWill Deacon {
1545b881cdceSWill Deacon 	int err;
1546b881cdceSWill Deacon 	void *base;
1547b881cdceSWill Deacon 
1548b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1549b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1550b881cdceSWill Deacon 
1551b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1552b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1553b881cdceSWill Deacon 
15542e403167SQuentin Perret 	if (kvm_system_needs_idmapped_vectors() &&
15552e403167SQuentin Perret 	    !is_protected_kvm_enabled()) {
1556b881cdceSWill Deacon 		err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1557b881cdceSWill Deacon 					       __BP_HARDEN_HYP_VECS_SZ, &base);
1558b881cdceSWill Deacon 		if (err)
1559b881cdceSWill Deacon 			return err;
1560b881cdceSWill Deacon 	}
15619ef2b48bSWill Deacon 
1562b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1563b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
15649ef2b48bSWill Deacon 	return 0;
15659ef2b48bSWill Deacon }
15669ef2b48bSWill Deacon 
156753bf620aSSean Christopherson static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
15689ed24f4bSMarc Zyngier {
15699cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1570d3e1086cSDavid Brazdil 	unsigned long tcr;
15719ed24f4bSMarc Zyngier 
157271b3ec5fSDavid Brazdil 	/*
157371b3ec5fSDavid Brazdil 	 * Calculate the raw per-cpu offset without a translation from the
157471b3ec5fSDavid Brazdil 	 * kernel's mapping to the linear mapping, and store it in tpidr_el2
157571b3ec5fSDavid Brazdil 	 * so that we can use adr_l to access per-cpu variables in EL2.
1576e1663372SSteven Price 	 * Also drop the KASAN tag which gets in the way...
157771b3ec5fSDavid Brazdil 	 */
15789cc77581SQuentin Perret 	params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
157930c95391SDavid Brazdil 			    (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
158071b3ec5fSDavid Brazdil 
1581d3e1086cSDavid Brazdil 	params->mair_el2 = read_sysreg(mair_el1);
1582d3e1086cSDavid Brazdil 
1583d3e1086cSDavid Brazdil 	tcr = (read_sysreg(tcr_el1) & TCR_EL2_MASK) | TCR_EL2_RES1;
1584d3e1086cSDavid Brazdil 	tcr &= ~TCR_T0SZ_MASK;
1585579d7ebeSRyan Roberts 	tcr |= TCR_T0SZ(hyp_va_bits);
1586d3e1086cSDavid Brazdil 	params->tcr_el2 = tcr;
1587d3e1086cSDavid Brazdil 
158863fec243SDavid Brazdil 	params->pgd_pa = kvm_mmu_get_httbr();
1589734864c1SQuentin Perret 	if (is_protected_kvm_enabled())
1590734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1591734864c1SQuentin Perret 	else
1592734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1593734864c1SQuentin Perret 	params->vttbr = params->vtcr = 0;
159463fec243SDavid Brazdil 
159563fec243SDavid Brazdil 	/*
159663fec243SDavid Brazdil 	 * Flush the init params from the data cache because the struct will
159763fec243SDavid Brazdil 	 * be read while the MMU is off.
159863fec243SDavid Brazdil 	 */
159963fec243SDavid Brazdil 	kvm_flush_dcache_to_poc(params, sizeof(*params));
16009cc77581SQuentin Perret }
16019cc77581SQuentin Perret 
1602bfa79a80SQuentin Perret static void hyp_install_host_vector(void)
16039cc77581SQuentin Perret {
16049cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params;
16059cc77581SQuentin Perret 	struct arm_smccc_res res;
16069cc77581SQuentin Perret 
16079cc77581SQuentin Perret 	/* Switch from the HYP stub to our own HYP init vector */
16089cc77581SQuentin Perret 	__hyp_set_vectors(kvm_get_idmap_vector());
16099ed24f4bSMarc Zyngier 
161071b3ec5fSDavid Brazdil 	/*
161171b3ec5fSDavid Brazdil 	 * Call initialization code, and switch to the full blown HYP code.
161271b3ec5fSDavid Brazdil 	 * If the cpucaps haven't been finalized yet, something has gone very
161371b3ec5fSDavid Brazdil 	 * wrong, and hyp will crash and burn when it uses any
161471b3ec5fSDavid Brazdil 	 * cpus_have_const_cap() wrapper.
161571b3ec5fSDavid Brazdil 	 */
161671b3ec5fSDavid Brazdil 	BUG_ON(!system_capabilities_finalized());
16179cc77581SQuentin Perret 	params = this_cpu_ptr_nvhe_sym(kvm_init_params);
161863fec243SDavid Brazdil 	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
161904e4caa8SAndrew Scull 	WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1620bfa79a80SQuentin Perret }
1621bfa79a80SQuentin Perret 
1622bfa79a80SQuentin Perret static void cpu_init_hyp_mode(void)
1623bfa79a80SQuentin Perret {
1624bfa79a80SQuentin Perret 	hyp_install_host_vector();
162571b3ec5fSDavid Brazdil 
162671b3ec5fSDavid Brazdil 	/*
162771b3ec5fSDavid Brazdil 	 * Disabling SSBD on a non-VHE system requires us to enable SSBS
162871b3ec5fSDavid Brazdil 	 * at EL2.
162971b3ec5fSDavid Brazdil 	 */
163071b3ec5fSDavid Brazdil 	if (this_cpu_has_cap(ARM64_SSBS) &&
1631d63d975aSMarc Zyngier 	    arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
163213aeb9b4SDavid Brazdil 		kvm_call_hyp_nvhe(__kvm_enable_ssbs);
163371b3ec5fSDavid Brazdil 	}
16349ed24f4bSMarc Zyngier }
16359ed24f4bSMarc Zyngier 
16369ed24f4bSMarc Zyngier static void cpu_hyp_reset(void)
16379ed24f4bSMarc Zyngier {
16389ed24f4bSMarc Zyngier 	if (!is_kernel_in_hyp_mode())
16399ed24f4bSMarc Zyngier 		__hyp_reset_vectors();
16409ed24f4bSMarc Zyngier }
16419ed24f4bSMarc Zyngier 
1642042c76a9SWill Deacon /*
1643042c76a9SWill Deacon  * EL2 vectors can be mapped and rerouted in a number of ways,
1644042c76a9SWill Deacon  * depending on the kernel configuration and CPU present:
1645042c76a9SWill Deacon  *
1646042c76a9SWill Deacon  * - If the CPU is affected by Spectre-v2, the hardening sequence is
1647042c76a9SWill Deacon  *   placed in one of the vector slots, which is executed before jumping
1648042c76a9SWill Deacon  *   to the real vectors.
1649042c76a9SWill Deacon  *
1650c4792b6dSWill Deacon  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1651042c76a9SWill Deacon  *   containing the hardening sequence is mapped next to the idmap page,
1652042c76a9SWill Deacon  *   and executed before jumping to the real vectors.
1653042c76a9SWill Deacon  *
1654c4792b6dSWill Deacon  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1655042c76a9SWill Deacon  *   empty slot is selected, mapped next to the idmap page, and
1656042c76a9SWill Deacon  *   executed before jumping to the real vectors.
1657042c76a9SWill Deacon  *
1658c4792b6dSWill Deacon  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1659042c76a9SWill Deacon  * VHE, as we don't have hypervisor-specific mappings. If the system
1660042c76a9SWill Deacon  * is VHE and yet selects this capability, it will be ignored.
1661042c76a9SWill Deacon  */
1662042c76a9SWill Deacon static void cpu_set_hyp_vector(void)
1663042c76a9SWill Deacon {
16646279017eSWill Deacon 	struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1665b881cdceSWill Deacon 	void *vector = hyp_spectre_vector_selector[data->slot];
1666042c76a9SWill Deacon 
1667bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
1668b881cdceSWill Deacon 		*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1669bfa79a80SQuentin Perret 	else
1670bfa79a80SQuentin Perret 		kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1671042c76a9SWill Deacon }
1672042c76a9SWill Deacon 
16738579a185SWill Deacon static void cpu_hyp_init_context(void)
16749ed24f4bSMarc Zyngier {
16752a1198c9SDavid Brazdil 	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
16769ed24f4bSMarc Zyngier 
16778579a185SWill Deacon 	if (!is_kernel_in_hyp_mode())
16788579a185SWill Deacon 		cpu_init_hyp_mode();
16798579a185SWill Deacon }
16808579a185SWill Deacon 
16818579a185SWill Deacon static void cpu_hyp_init_features(void)
16828579a185SWill Deacon {
16838579a185SWill Deacon 	cpu_set_hyp_vector();
16848579a185SWill Deacon 	kvm_arm_init_debug();
1685a0e47952SAndrew Scull 
16869ed24f4bSMarc Zyngier 	if (is_kernel_in_hyp_mode())
16879ed24f4bSMarc Zyngier 		kvm_timer_init_vhe();
16889ed24f4bSMarc Zyngier 
16899ed24f4bSMarc Zyngier 	if (vgic_present)
16909ed24f4bSMarc Zyngier 		kvm_vgic_init_cpu_hardware();
16919ed24f4bSMarc Zyngier }
16929ed24f4bSMarc Zyngier 
16938579a185SWill Deacon static void cpu_hyp_reinit(void)
16948579a185SWill Deacon {
16958579a185SWill Deacon 	cpu_hyp_reset();
16968579a185SWill Deacon 	cpu_hyp_init_context();
16978579a185SWill Deacon 	cpu_hyp_init_features();
16988579a185SWill Deacon }
16998579a185SWill Deacon 
17009ed24f4bSMarc Zyngier static void _kvm_arch_hardware_enable(void *discard)
17019ed24f4bSMarc Zyngier {
17029ed24f4bSMarc Zyngier 	if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
17039ed24f4bSMarc Zyngier 		cpu_hyp_reinit();
17049ed24f4bSMarc Zyngier 		__this_cpu_write(kvm_arm_hardware_enabled, 1);
17059ed24f4bSMarc Zyngier 	}
17069ed24f4bSMarc Zyngier }
17079ed24f4bSMarc Zyngier 
17089ed24f4bSMarc Zyngier int kvm_arch_hardware_enable(void)
17099ed24f4bSMarc Zyngier {
1710466d27e4SMarc Zyngier 	int was_enabled = __this_cpu_read(kvm_arm_hardware_enabled);
1711466d27e4SMarc Zyngier 
17129ed24f4bSMarc Zyngier 	_kvm_arch_hardware_enable(NULL);
1713466d27e4SMarc Zyngier 
1714466d27e4SMarc Zyngier 	if (!was_enabled) {
1715466d27e4SMarc Zyngier 		kvm_vgic_cpu_up();
1716466d27e4SMarc Zyngier 		kvm_timer_cpu_up();
1717466d27e4SMarc Zyngier 	}
1718466d27e4SMarc Zyngier 
17199ed24f4bSMarc Zyngier 	return 0;
17209ed24f4bSMarc Zyngier }
17219ed24f4bSMarc Zyngier 
17229ed24f4bSMarc Zyngier static void _kvm_arch_hardware_disable(void *discard)
17239ed24f4bSMarc Zyngier {
17249ed24f4bSMarc Zyngier 	if (__this_cpu_read(kvm_arm_hardware_enabled)) {
17259ed24f4bSMarc Zyngier 		cpu_hyp_reset();
17269ed24f4bSMarc Zyngier 		__this_cpu_write(kvm_arm_hardware_enabled, 0);
17279ed24f4bSMarc Zyngier 	}
17289ed24f4bSMarc Zyngier }
17299ed24f4bSMarc Zyngier 
17309ed24f4bSMarc Zyngier void kvm_arch_hardware_disable(void)
17319ed24f4bSMarc Zyngier {
1732466d27e4SMarc Zyngier 	if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1733466d27e4SMarc Zyngier 		kvm_timer_cpu_down();
1734466d27e4SMarc Zyngier 		kvm_vgic_cpu_down();
1735466d27e4SMarc Zyngier 	}
1736466d27e4SMarc Zyngier 
1737fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
17389ed24f4bSMarc Zyngier 		_kvm_arch_hardware_disable(NULL);
17399ed24f4bSMarc Zyngier }
17409ed24f4bSMarc Zyngier 
17419ed24f4bSMarc Zyngier #ifdef CONFIG_CPU_PM
17429ed24f4bSMarc Zyngier static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
17439ed24f4bSMarc Zyngier 				    unsigned long cmd,
17449ed24f4bSMarc Zyngier 				    void *v)
17459ed24f4bSMarc Zyngier {
17469ed24f4bSMarc Zyngier 	/*
17479ed24f4bSMarc Zyngier 	 * kvm_arm_hardware_enabled is left with its old value over
17489ed24f4bSMarc Zyngier 	 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
17499ed24f4bSMarc Zyngier 	 * re-enable hyp.
17509ed24f4bSMarc Zyngier 	 */
17519ed24f4bSMarc Zyngier 	switch (cmd) {
17529ed24f4bSMarc Zyngier 	case CPU_PM_ENTER:
17539ed24f4bSMarc Zyngier 		if (__this_cpu_read(kvm_arm_hardware_enabled))
17549ed24f4bSMarc Zyngier 			/*
17559ed24f4bSMarc Zyngier 			 * don't update kvm_arm_hardware_enabled here
17569ed24f4bSMarc Zyngier 			 * so that the hardware will be re-enabled
17579ed24f4bSMarc Zyngier 			 * when we resume. See below.
17589ed24f4bSMarc Zyngier 			 */
17599ed24f4bSMarc Zyngier 			cpu_hyp_reset();
17609ed24f4bSMarc Zyngier 
17619ed24f4bSMarc Zyngier 		return NOTIFY_OK;
17629ed24f4bSMarc Zyngier 	case CPU_PM_ENTER_FAILED:
17639ed24f4bSMarc Zyngier 	case CPU_PM_EXIT:
17649ed24f4bSMarc Zyngier 		if (__this_cpu_read(kvm_arm_hardware_enabled))
17659ed24f4bSMarc Zyngier 			/* The hardware was enabled before suspend. */
17669ed24f4bSMarc Zyngier 			cpu_hyp_reinit();
17679ed24f4bSMarc Zyngier 
17689ed24f4bSMarc Zyngier 		return NOTIFY_OK;
17699ed24f4bSMarc Zyngier 
17709ed24f4bSMarc Zyngier 	default:
17719ed24f4bSMarc Zyngier 		return NOTIFY_DONE;
17729ed24f4bSMarc Zyngier 	}
17739ed24f4bSMarc Zyngier }
17749ed24f4bSMarc Zyngier 
17759ed24f4bSMarc Zyngier static struct notifier_block hyp_init_cpu_pm_nb = {
17769ed24f4bSMarc Zyngier 	.notifier_call = hyp_init_cpu_pm_notifier,
17779ed24f4bSMarc Zyngier };
17789ed24f4bSMarc Zyngier 
177953bf620aSSean Christopherson static void __init hyp_cpu_pm_init(void)
17809ed24f4bSMarc Zyngier {
1781fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
17829ed24f4bSMarc Zyngier 		cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
17839ed24f4bSMarc Zyngier }
178453bf620aSSean Christopherson static void __init hyp_cpu_pm_exit(void)
17859ed24f4bSMarc Zyngier {
1786fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
17879ed24f4bSMarc Zyngier 		cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
17889ed24f4bSMarc Zyngier }
17899ed24f4bSMarc Zyngier #else
179053bf620aSSean Christopherson static inline void __init hyp_cpu_pm_init(void)
17919ed24f4bSMarc Zyngier {
17929ed24f4bSMarc Zyngier }
179353bf620aSSean Christopherson static inline void __init hyp_cpu_pm_exit(void)
17949ed24f4bSMarc Zyngier {
17959ed24f4bSMarc Zyngier }
17969ed24f4bSMarc Zyngier #endif
17979ed24f4bSMarc Zyngier 
179853bf620aSSean Christopherson static void __init init_cpu_logical_map(void)
179994f5e8a4SDavid Brazdil {
180094f5e8a4SDavid Brazdil 	unsigned int cpu;
180194f5e8a4SDavid Brazdil 
180294f5e8a4SDavid Brazdil 	/*
180394f5e8a4SDavid Brazdil 	 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
180421ea4578SJulia Lawall 	 * Only copy the set of online CPUs whose features have been checked
180594f5e8a4SDavid Brazdil 	 * against the finalized system capabilities. The hypervisor will not
180694f5e8a4SDavid Brazdil 	 * allow any other CPUs from the `possible` set to boot.
180794f5e8a4SDavid Brazdil 	 */
180894f5e8a4SDavid Brazdil 	for_each_online_cpu(cpu)
180961fe0c37SDavid Brazdil 		hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
181094f5e8a4SDavid Brazdil }
181194f5e8a4SDavid Brazdil 
1812767c973fSMarc Zyngier #define init_psci_0_1_impl_state(config, what)	\
1813767c973fSMarc Zyngier 	config.psci_0_1_ ## what ## _implemented = psci_ops.what
1814767c973fSMarc Zyngier 
181553bf620aSSean Christopherson static bool __init init_psci_relay(void)
1816eeeee719SDavid Brazdil {
1817eeeee719SDavid Brazdil 	/*
1818eeeee719SDavid Brazdil 	 * If PSCI has not been initialized, protected KVM cannot install
1819eeeee719SDavid Brazdil 	 * itself on newly booted CPUs.
1820eeeee719SDavid Brazdil 	 */
1821eeeee719SDavid Brazdil 	if (!psci_ops.get_version) {
1822eeeee719SDavid Brazdil 		kvm_err("Cannot initialize protected mode without PSCI\n");
1823eeeee719SDavid Brazdil 		return false;
1824eeeee719SDavid Brazdil 	}
1825eeeee719SDavid Brazdil 
1826ff367fe4SDavid Brazdil 	kvm_host_psci_config.version = psci_ops.get_version();
1827ff367fe4SDavid Brazdil 
1828ff367fe4SDavid Brazdil 	if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
1829ff367fe4SDavid Brazdil 		kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
1830767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
1831767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
1832767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
1833767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
1834ff367fe4SDavid Brazdil 	}
1835eeeee719SDavid Brazdil 	return true;
1836eeeee719SDavid Brazdil }
1837eeeee719SDavid Brazdil 
183853bf620aSSean Christopherson static int __init init_subsystems(void)
18399ed24f4bSMarc Zyngier {
18409ed24f4bSMarc Zyngier 	int err = 0;
18419ed24f4bSMarc Zyngier 
18429ed24f4bSMarc Zyngier 	/*
18439ed24f4bSMarc Zyngier 	 * Enable hardware so that subsystem initialisation can access EL2.
18449ed24f4bSMarc Zyngier 	 */
18459ed24f4bSMarc Zyngier 	on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
18469ed24f4bSMarc Zyngier 
18479ed24f4bSMarc Zyngier 	/*
18489ed24f4bSMarc Zyngier 	 * Register CPU lower-power notifier
18499ed24f4bSMarc Zyngier 	 */
18509ed24f4bSMarc Zyngier 	hyp_cpu_pm_init();
18519ed24f4bSMarc Zyngier 
18529ed24f4bSMarc Zyngier 	/*
18539ed24f4bSMarc Zyngier 	 * Init HYP view of VGIC
18549ed24f4bSMarc Zyngier 	 */
18559ed24f4bSMarc Zyngier 	err = kvm_vgic_hyp_init();
18569ed24f4bSMarc Zyngier 	switch (err) {
18579ed24f4bSMarc Zyngier 	case 0:
18589ed24f4bSMarc Zyngier 		vgic_present = true;
18599ed24f4bSMarc Zyngier 		break;
18609ed24f4bSMarc Zyngier 	case -ENODEV:
18619ed24f4bSMarc Zyngier 	case -ENXIO:
18629ed24f4bSMarc Zyngier 		vgic_present = false;
18639ed24f4bSMarc Zyngier 		err = 0;
18649ed24f4bSMarc Zyngier 		break;
18659ed24f4bSMarc Zyngier 	default:
18669ed24f4bSMarc Zyngier 		goto out;
18679ed24f4bSMarc Zyngier 	}
18689ed24f4bSMarc Zyngier 
18699ed24f4bSMarc Zyngier 	/*
18709ed24f4bSMarc Zyngier 	 * Init HYP architected timer support
18719ed24f4bSMarc Zyngier 	 */
18729ed24f4bSMarc Zyngier 	err = kvm_timer_hyp_init(vgic_present);
18739ed24f4bSMarc Zyngier 	if (err)
18749ed24f4bSMarc Zyngier 		goto out;
18759ed24f4bSMarc Zyngier 
187617ed14ebSSean Christopherson 	kvm_register_perf_callbacks(NULL);
187717ed14ebSSean Christopherson 
18789ed24f4bSMarc Zyngier out:
187978b3bf48SSean Christopherson 	if (err)
188078b3bf48SSean Christopherson 		hyp_cpu_pm_exit();
188178b3bf48SSean Christopherson 
1882fa8c3d65SDavid Brazdil 	if (err || !is_protected_kvm_enabled())
18839ed24f4bSMarc Zyngier 		on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
18849ed24f4bSMarc Zyngier 
18859ed24f4bSMarc Zyngier 	return err;
18869ed24f4bSMarc Zyngier }
18879ed24f4bSMarc Zyngier 
188853bf620aSSean Christopherson static void __init teardown_subsystems(void)
188978b3bf48SSean Christopherson {
189078b3bf48SSean Christopherson 	kvm_unregister_perf_callbacks();
189178b3bf48SSean Christopherson 	hyp_cpu_pm_exit();
189278b3bf48SSean Christopherson }
189378b3bf48SSean Christopherson 
189453bf620aSSean Christopherson static void __init teardown_hyp_mode(void)
18959ed24f4bSMarc Zyngier {
18969ed24f4bSMarc Zyngier 	int cpu;
18979ed24f4bSMarc Zyngier 
18989ed24f4bSMarc Zyngier 	free_hyp_pgds();
189930c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
19009ed24f4bSMarc Zyngier 		free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1901fe41a7f8SQuentin Perret 		free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
190230c95391SDavid Brazdil 	}
19039ed24f4bSMarc Zyngier }
19049ed24f4bSMarc Zyngier 
190553bf620aSSean Christopherson static int __init do_pkvm_init(u32 hyp_va_bits)
1906bfa79a80SQuentin Perret {
1907fe41a7f8SQuentin Perret 	void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
1908bfa79a80SQuentin Perret 	int ret;
1909bfa79a80SQuentin Perret 
1910bfa79a80SQuentin Perret 	preempt_disable();
19118579a185SWill Deacon 	cpu_hyp_init_context();
1912bfa79a80SQuentin Perret 	ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
1913bfa79a80SQuentin Perret 				num_possible_cpus(), kern_hyp_va(per_cpu_base),
1914bfa79a80SQuentin Perret 				hyp_va_bits);
19158579a185SWill Deacon 	cpu_hyp_init_features();
19168579a185SWill Deacon 
19178579a185SWill Deacon 	/*
19188579a185SWill Deacon 	 * The stub hypercalls are now disabled, so set our local flag to
19198579a185SWill Deacon 	 * prevent a later re-init attempt in kvm_arch_hardware_enable().
19208579a185SWill Deacon 	 */
19218579a185SWill Deacon 	__this_cpu_write(kvm_arm_hardware_enabled, 1);
1922bfa79a80SQuentin Perret 	preempt_enable();
1923bfa79a80SQuentin Perret 
1924bfa79a80SQuentin Perret 	return ret;
1925bfa79a80SQuentin Perret }
1926bfa79a80SQuentin Perret 
19276c165223SWill Deacon static void kvm_hyp_init_symbols(void)
1928bfa79a80SQuentin Perret {
19296c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
19306c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
19316c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
19326c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
1933def8c222SVladimir Murzin 	kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
19347c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
19357c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
19366c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
19378669651cSQuentin Perret 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
193813e248aaSWill Deacon 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
193973f38ef2SWill Deacon 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
19406c165223SWill Deacon }
19416c165223SWill Deacon 
194253bf620aSSean Christopherson static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
19436c165223SWill Deacon {
19446c165223SWill Deacon 	void *addr = phys_to_virt(hyp_mem_base);
19456c165223SWill Deacon 	int ret;
19467c419937SMarc Zyngier 
1947bfa79a80SQuentin Perret 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
1948bfa79a80SQuentin Perret 	if (ret)
1949bfa79a80SQuentin Perret 		return ret;
1950bfa79a80SQuentin Perret 
1951bfa79a80SQuentin Perret 	ret = do_pkvm_init(hyp_va_bits);
1952bfa79a80SQuentin Perret 	if (ret)
1953bfa79a80SQuentin Perret 		return ret;
1954bfa79a80SQuentin Perret 
1955bfa79a80SQuentin Perret 	free_hyp_pgds();
1956bfa79a80SQuentin Perret 
1957bfa79a80SQuentin Perret 	return 0;
1958bfa79a80SQuentin Perret }
1959bfa79a80SQuentin Perret 
196067d953d4SMarc Zyngier /* Inits Hyp-mode on all online CPUs */
196153bf620aSSean Christopherson static int __init init_hyp_mode(void)
19629ed24f4bSMarc Zyngier {
1963bfa79a80SQuentin Perret 	u32 hyp_va_bits;
19649ed24f4bSMarc Zyngier 	int cpu;
1965bfa79a80SQuentin Perret 	int err = -ENOMEM;
1966bfa79a80SQuentin Perret 
1967bfa79a80SQuentin Perret 	/*
1968bfa79a80SQuentin Perret 	 * The protected Hyp-mode cannot be initialized if the memory pool
1969bfa79a80SQuentin Perret 	 * allocation has failed.
1970bfa79a80SQuentin Perret 	 */
1971bfa79a80SQuentin Perret 	if (is_protected_kvm_enabled() && !hyp_mem_base)
1972bfa79a80SQuentin Perret 		goto out_err;
19739ed24f4bSMarc Zyngier 
19749ed24f4bSMarc Zyngier 	/*
19759ed24f4bSMarc Zyngier 	 * Allocate Hyp PGD and setup Hyp identity mapping
19769ed24f4bSMarc Zyngier 	 */
1977bfa79a80SQuentin Perret 	err = kvm_mmu_init(&hyp_va_bits);
19789ed24f4bSMarc Zyngier 	if (err)
19799ed24f4bSMarc Zyngier 		goto out_err;
19809ed24f4bSMarc Zyngier 
19819ed24f4bSMarc Zyngier 	/*
19829ed24f4bSMarc Zyngier 	 * Allocate stack pages for Hypervisor-mode
19839ed24f4bSMarc Zyngier 	 */
19849ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
19859ed24f4bSMarc Zyngier 		unsigned long stack_page;
19869ed24f4bSMarc Zyngier 
19879ed24f4bSMarc Zyngier 		stack_page = __get_free_page(GFP_KERNEL);
19889ed24f4bSMarc Zyngier 		if (!stack_page) {
19899ed24f4bSMarc Zyngier 			err = -ENOMEM;
19909ed24f4bSMarc Zyngier 			goto out_err;
19919ed24f4bSMarc Zyngier 		}
19929ed24f4bSMarc Zyngier 
19939ed24f4bSMarc Zyngier 		per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
19949ed24f4bSMarc Zyngier 	}
19959ed24f4bSMarc Zyngier 
19969ed24f4bSMarc Zyngier 	/*
199730c95391SDavid Brazdil 	 * Allocate and initialize pages for Hypervisor-mode percpu regions.
199830c95391SDavid Brazdil 	 */
199930c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
200030c95391SDavid Brazdil 		struct page *page;
200130c95391SDavid Brazdil 		void *page_addr;
200230c95391SDavid Brazdil 
200330c95391SDavid Brazdil 		page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
200430c95391SDavid Brazdil 		if (!page) {
200530c95391SDavid Brazdil 			err = -ENOMEM;
200630c95391SDavid Brazdil 			goto out_err;
200730c95391SDavid Brazdil 		}
200830c95391SDavid Brazdil 
200930c95391SDavid Brazdil 		page_addr = page_address(page);
201030c95391SDavid Brazdil 		memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2011fe41a7f8SQuentin Perret 		kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
201230c95391SDavid Brazdil 	}
201330c95391SDavid Brazdil 
201430c95391SDavid Brazdil 	/*
20159ed24f4bSMarc Zyngier 	 * Map the Hyp-code called directly from the host
20169ed24f4bSMarc Zyngier 	 */
20179ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
20189ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
20199ed24f4bSMarc Zyngier 	if (err) {
20209ed24f4bSMarc Zyngier 		kvm_err("Cannot map world-switch code\n");
20219ed24f4bSMarc Zyngier 		goto out_err;
20229ed24f4bSMarc Zyngier 	}
20239ed24f4bSMarc Zyngier 
202416174eeaSDavid Brazdil 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
202516174eeaSDavid Brazdil 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
20262d7bf218SDavid Brazdil 	if (err) {
202716174eeaSDavid Brazdil 		kvm_err("Cannot map .hyp.rodata section\n");
20282d7bf218SDavid Brazdil 		goto out_err;
20292d7bf218SDavid Brazdil 	}
20302d7bf218SDavid Brazdil 
20319ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
20329ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
20339ed24f4bSMarc Zyngier 	if (err) {
20349ed24f4bSMarc Zyngier 		kvm_err("Cannot map rodata section\n");
20359ed24f4bSMarc Zyngier 		goto out_err;
20369ed24f4bSMarc Zyngier 	}
20379ed24f4bSMarc Zyngier 
2038380e18adSQuentin Perret 	/*
2039380e18adSQuentin Perret 	 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2040380e18adSQuentin Perret 	 * section thanks to an assertion in the linker script. Map it RW and
2041380e18adSQuentin Perret 	 * the rest of .bss RO.
2042380e18adSQuentin Perret 	 */
2043380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2044380e18adSQuentin Perret 				  kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2045380e18adSQuentin Perret 	if (err) {
2046380e18adSQuentin Perret 		kvm_err("Cannot map hyp bss section: %d\n", err);
2047380e18adSQuentin Perret 		goto out_err;
2048380e18adSQuentin Perret 	}
2049380e18adSQuentin Perret 
2050380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
20519ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
20529ed24f4bSMarc Zyngier 	if (err) {
20539ed24f4bSMarc Zyngier 		kvm_err("Cannot map bss section\n");
20549ed24f4bSMarc Zyngier 		goto out_err;
20559ed24f4bSMarc Zyngier 	}
20569ed24f4bSMarc Zyngier 
20579ed24f4bSMarc Zyngier 	/*
20589ed24f4bSMarc Zyngier 	 * Map the Hyp stack pages
20599ed24f4bSMarc Zyngier 	 */
20609ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2061ce335431SKalesh Singh 		struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
20629ed24f4bSMarc Zyngier 		char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
2063ce335431SKalesh Singh 		unsigned long hyp_addr;
20649ed24f4bSMarc Zyngier 
2065ce335431SKalesh Singh 		/*
2066ce335431SKalesh Singh 		 * Allocate a contiguous HYP private VA range for the stack
2067ce335431SKalesh Singh 		 * and guard page. The allocation is also aligned based on
2068ce335431SKalesh Singh 		 * the order of its size.
2069ce335431SKalesh Singh 		 */
2070ce335431SKalesh Singh 		err = hyp_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr);
2071ce335431SKalesh Singh 		if (err) {
2072ce335431SKalesh Singh 			kvm_err("Cannot allocate hyp stack guard page\n");
2073ce335431SKalesh Singh 			goto out_err;
2074ce335431SKalesh Singh 		}
2075ce335431SKalesh Singh 
2076ce335431SKalesh Singh 		/*
2077ce335431SKalesh Singh 		 * Since the stack grows downwards, map the stack to the page
2078ce335431SKalesh Singh 		 * at the higher address and leave the lower guard page
2079ce335431SKalesh Singh 		 * unbacked.
2080ce335431SKalesh Singh 		 *
2081ce335431SKalesh Singh 		 * Any valid stack address now has the PAGE_SHIFT bit as 1
2082ce335431SKalesh Singh 		 * and addresses corresponding to the guard page have the
2083ce335431SKalesh Singh 		 * PAGE_SHIFT bit as 0 - this is used for overflow detection.
2084ce335431SKalesh Singh 		 */
2085ce335431SKalesh Singh 		err = __create_hyp_mappings(hyp_addr + PAGE_SIZE, PAGE_SIZE,
2086ce335431SKalesh Singh 					    __pa(stack_page), PAGE_HYP);
20879ed24f4bSMarc Zyngier 		if (err) {
20889ed24f4bSMarc Zyngier 			kvm_err("Cannot map hyp stack\n");
20899ed24f4bSMarc Zyngier 			goto out_err;
20909ed24f4bSMarc Zyngier 		}
2091ce335431SKalesh Singh 
2092ce335431SKalesh Singh 		/*
2093ce335431SKalesh Singh 		 * Save the stack PA in nvhe_init_params. This will be needed
2094ce335431SKalesh Singh 		 * to recreate the stack mapping in protected nVHE mode.
2095ce335431SKalesh Singh 		 * __hyp_pa() won't do the right thing there, since the stack
2096ce335431SKalesh Singh 		 * has been mapped in the flexible private VA space.
2097ce335431SKalesh Singh 		 */
2098ce335431SKalesh Singh 		params->stack_pa = __pa(stack_page);
2099ce335431SKalesh Singh 
2100ce335431SKalesh Singh 		params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE);
21019ed24f4bSMarc Zyngier 	}
21029ed24f4bSMarc Zyngier 
21039ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2104fe41a7f8SQuentin Perret 		char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
210530c95391SDavid Brazdil 		char *percpu_end = percpu_begin + nvhe_percpu_size();
21069ed24f4bSMarc Zyngier 
21079cc77581SQuentin Perret 		/* Map Hyp percpu pages */
210830c95391SDavid Brazdil 		err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
21099ed24f4bSMarc Zyngier 		if (err) {
211030c95391SDavid Brazdil 			kvm_err("Cannot map hyp percpu region\n");
21116e3bfbb2SAndrew Scull 			goto out_err;
21126e3bfbb2SAndrew Scull 		}
21139cc77581SQuentin Perret 
21149cc77581SQuentin Perret 		/* Prepare the CPU initialization parameters */
2115579d7ebeSRyan Roberts 		cpu_prepare_hyp_mode(cpu, hyp_va_bits);
21169ed24f4bSMarc Zyngier 	}
21179ed24f4bSMarc Zyngier 
21186c165223SWill Deacon 	kvm_hyp_init_symbols();
21196c165223SWill Deacon 
2120eeeee719SDavid Brazdil 	if (is_protected_kvm_enabled()) {
212194f5e8a4SDavid Brazdil 		init_cpu_logical_map();
212294f5e8a4SDavid Brazdil 
212352b9e265SWang Wensheng 		if (!init_psci_relay()) {
212452b9e265SWang Wensheng 			err = -ENODEV;
2125eeeee719SDavid Brazdil 			goto out_err;
2126eeeee719SDavid Brazdil 		}
2127eeeee719SDavid Brazdil 
2128bfa79a80SQuentin Perret 		err = kvm_hyp_init_protection(hyp_va_bits);
2129bfa79a80SQuentin Perret 		if (err) {
2130bfa79a80SQuentin Perret 			kvm_err("Failed to init hyp memory protection\n");
2131bfa79a80SQuentin Perret 			goto out_err;
2132bfa79a80SQuentin Perret 		}
2133bfa79a80SQuentin Perret 	}
21349ed24f4bSMarc Zyngier 
21359ed24f4bSMarc Zyngier 	return 0;
21369ed24f4bSMarc Zyngier 
21379ed24f4bSMarc Zyngier out_err:
21389ed24f4bSMarc Zyngier 	teardown_hyp_mode();
21399ed24f4bSMarc Zyngier 	kvm_err("error initializing Hyp mode: %d\n", err);
21409ed24f4bSMarc Zyngier 	return err;
21419ed24f4bSMarc Zyngier }
21429ed24f4bSMarc Zyngier 
214353bf620aSSean Christopherson static void __init _kvm_host_prot_finalize(void *arg)
21441025c8c0SQuentin Perret {
21452f2e1a50SWill Deacon 	int *err = arg;
21462f2e1a50SWill Deacon 
21472f2e1a50SWill Deacon 	if (WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize)))
21482f2e1a50SWill Deacon 		WRITE_ONCE(*err, -EINVAL);
21492f2e1a50SWill Deacon }
21502f2e1a50SWill Deacon 
215153bf620aSSean Christopherson static int __init pkvm_drop_host_privileges(void)
21522f2e1a50SWill Deacon {
21532f2e1a50SWill Deacon 	int ret = 0;
21542f2e1a50SWill Deacon 
21552f2e1a50SWill Deacon 	/*
21562f2e1a50SWill Deacon 	 * Flip the static key upfront as that may no longer be possible
21572f2e1a50SWill Deacon 	 * once the host stage 2 is installed.
21582f2e1a50SWill Deacon 	 */
21592f2e1a50SWill Deacon 	static_branch_enable(&kvm_protected_mode_initialized);
21602f2e1a50SWill Deacon 	on_each_cpu(_kvm_host_prot_finalize, &ret, 1);
21612f2e1a50SWill Deacon 	return ret;
21621025c8c0SQuentin Perret }
21631025c8c0SQuentin Perret 
216453bf620aSSean Christopherson static int __init finalize_hyp_mode(void)
2165bfa79a80SQuentin Perret {
2166bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
2167bfa79a80SQuentin Perret 		return 0;
2168bfa79a80SQuentin Perret 
216947e6223cSMarc Zyngier 	/*
217056961c63SQuentin Perret 	 * Exclude HYP sections from kmemleak so that they don't get peeked
217156961c63SQuentin Perret 	 * at, which would end badly once inaccessible.
217247e6223cSMarc Zyngier 	 */
217347e6223cSMarc Zyngier 	kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start);
2174522c9a64SZenghui Yu 	kmemleak_free_part_phys(hyp_mem_base, hyp_mem_size);
21752f2e1a50SWill Deacon 	return pkvm_drop_host_privileges();
2176bfa79a80SQuentin Perret }
2177bfa79a80SQuentin Perret 
21789ed24f4bSMarc Zyngier struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
21799ed24f4bSMarc Zyngier {
21809ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
218146808a4cSMarc Zyngier 	unsigned long i;
21829ed24f4bSMarc Zyngier 
21839ed24f4bSMarc Zyngier 	mpidr &= MPIDR_HWID_BITMASK;
21849ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
21859ed24f4bSMarc Zyngier 		if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
21869ed24f4bSMarc Zyngier 			return vcpu;
21879ed24f4bSMarc Zyngier 	}
21889ed24f4bSMarc Zyngier 	return NULL;
21899ed24f4bSMarc Zyngier }
21909ed24f4bSMarc Zyngier 
2191d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2192d663b8a2SPaolo Bonzini {
2193d663b8a2SPaolo Bonzini 	return irqchip_in_kernel(kvm);
2194d663b8a2SPaolo Bonzini }
2195d663b8a2SPaolo Bonzini 
21969ed24f4bSMarc Zyngier bool kvm_arch_has_irq_bypass(void)
21979ed24f4bSMarc Zyngier {
21989ed24f4bSMarc Zyngier 	return true;
21999ed24f4bSMarc Zyngier }
22009ed24f4bSMarc Zyngier 
22019ed24f4bSMarc Zyngier int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
22029ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
22039ed24f4bSMarc Zyngier {
22049ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
22059ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
22069ed24f4bSMarc Zyngier 
22079ed24f4bSMarc Zyngier 	return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
22089ed24f4bSMarc Zyngier 					  &irqfd->irq_entry);
22099ed24f4bSMarc Zyngier }
22109ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
22119ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
22129ed24f4bSMarc Zyngier {
22139ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
22149ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
22159ed24f4bSMarc Zyngier 
22169ed24f4bSMarc Zyngier 	kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
22179ed24f4bSMarc Zyngier 				     &irqfd->irq_entry);
22189ed24f4bSMarc Zyngier }
22199ed24f4bSMarc Zyngier 
22209ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
22219ed24f4bSMarc Zyngier {
22229ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
22239ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
22249ed24f4bSMarc Zyngier 
22259ed24f4bSMarc Zyngier 	kvm_arm_halt_guest(irqfd->kvm);
22269ed24f4bSMarc Zyngier }
22279ed24f4bSMarc Zyngier 
22289ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
22299ed24f4bSMarc Zyngier {
22309ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
22319ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
22329ed24f4bSMarc Zyngier 
22339ed24f4bSMarc Zyngier 	kvm_arm_resume_guest(irqfd->kvm);
22349ed24f4bSMarc Zyngier }
22359ed24f4bSMarc Zyngier 
223667d953d4SMarc Zyngier /* Initialize Hyp-mode and memory mappings on all CPUs */
223753bf620aSSean Christopherson static __init int kvm_arm_init(void)
22389ed24f4bSMarc Zyngier {
22399ed24f4bSMarc Zyngier 	int err;
22409ed24f4bSMarc Zyngier 	bool in_hyp_mode;
22419ed24f4bSMarc Zyngier 
22429ed24f4bSMarc Zyngier 	if (!is_hyp_mode_available()) {
22439ed24f4bSMarc Zyngier 		kvm_info("HYP mode not available\n");
22449ed24f4bSMarc Zyngier 		return -ENODEV;
22459ed24f4bSMarc Zyngier 	}
22469ed24f4bSMarc Zyngier 
2247b6a68b97SMarc Zyngier 	if (kvm_get_mode() == KVM_MODE_NONE) {
2248b6a68b97SMarc Zyngier 		kvm_info("KVM disabled from command line\n");
2249b6a68b97SMarc Zyngier 		return -ENODEV;
2250b6a68b97SMarc Zyngier 	}
2251b6a68b97SMarc Zyngier 
2252f1f0c0cfSAlexandru Elisei 	err = kvm_sys_reg_table_init();
2253f1f0c0cfSAlexandru Elisei 	if (err) {
2254f1f0c0cfSAlexandru Elisei 		kvm_info("Error initializing system register tables");
2255f1f0c0cfSAlexandru Elisei 		return err;
2256f1f0c0cfSAlexandru Elisei 	}
2257f1f0c0cfSAlexandru Elisei 
22589ed24f4bSMarc Zyngier 	in_hyp_mode = is_kernel_in_hyp_mode();
22599ed24f4bSMarc Zyngier 
226096d389caSRob Herring 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
226196d389caSRob Herring 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2262abf532ccSRob Herring 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2263abf532ccSRob Herring 			 "Only trusted guests should be used on this system.\n");
2264abf532ccSRob Herring 
2265bf249d9eSAnshuman Khandual 	err = kvm_set_ipa_limit();
22669ed24f4bSMarc Zyngier 	if (err)
22679ed24f4bSMarc Zyngier 		return err;
22689ed24f4bSMarc Zyngier 
22699ed24f4bSMarc Zyngier 	err = kvm_arm_init_sve();
22709ed24f4bSMarc Zyngier 	if (err)
22719ed24f4bSMarc Zyngier 		return err;
22729ed24f4bSMarc Zyngier 
22733248136bSJulien Grall 	err = kvm_arm_vmid_alloc_init();
22743248136bSJulien Grall 	if (err) {
22753248136bSJulien Grall 		kvm_err("Failed to initialize VMID allocator.\n");
22763248136bSJulien Grall 		return err;
22773248136bSJulien Grall 	}
22783248136bSJulien Grall 
22799ed24f4bSMarc Zyngier 	if (!in_hyp_mode) {
22809ed24f4bSMarc Zyngier 		err = init_hyp_mode();
22819ed24f4bSMarc Zyngier 		if (err)
22829ed24f4bSMarc Zyngier 			goto out_err;
22839ed24f4bSMarc Zyngier 	}
22849ed24f4bSMarc Zyngier 
2285b881cdceSWill Deacon 	err = kvm_init_vector_slots();
2286b881cdceSWill Deacon 	if (err) {
2287b881cdceSWill Deacon 		kvm_err("Cannot initialise vector slots\n");
22886baaeda8SSean Christopherson 		goto out_hyp;
2289b881cdceSWill Deacon 	}
2290b881cdceSWill Deacon 
22919ed24f4bSMarc Zyngier 	err = init_subsystems();
22929ed24f4bSMarc Zyngier 	if (err)
229378b3bf48SSean Christopherson 		goto out_hyp;
22949ed24f4bSMarc Zyngier 
2295bfa79a80SQuentin Perret 	if (!in_hyp_mode) {
2296bfa79a80SQuentin Perret 		err = finalize_hyp_mode();
2297bfa79a80SQuentin Perret 		if (err) {
2298bfa79a80SQuentin Perret 			kvm_err("Failed to finalize Hyp protection\n");
22996baaeda8SSean Christopherson 			goto out_subs;
2300bfa79a80SQuentin Perret 		}
2301bfa79a80SQuentin Perret 	}
2302bfa79a80SQuentin Perret 
2303f19f6644SDavid Brazdil 	if (is_protected_kvm_enabled()) {
23043eb681fbSDavid Brazdil 		kvm_info("Protected nVHE mode initialized successfully\n");
2305f19f6644SDavid Brazdil 	} else if (in_hyp_mode) {
23069ed24f4bSMarc Zyngier 		kvm_info("VHE mode initialized successfully\n");
2307f19f6644SDavid Brazdil 	} else {
23089ed24f4bSMarc Zyngier 		kvm_info("Hyp mode initialized successfully\n");
2309f19f6644SDavid Brazdil 	}
23109ed24f4bSMarc Zyngier 
23111dc0f02dSSean Christopherson 	/*
23121dc0f02dSSean Christopherson 	 * FIXME: Do something reasonable if kvm_init() fails after pKVM
23131dc0f02dSSean Christopherson 	 * hypervisor protection is finalized.
23141dc0f02dSSean Christopherson 	 */
231581a1cf9fSSean Christopherson 	err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
23161dc0f02dSSean Christopherson 	if (err)
23171dc0f02dSSean Christopherson 		goto out_subs;
23181dc0f02dSSean Christopherson 
23199ed24f4bSMarc Zyngier 	return 0;
23209ed24f4bSMarc Zyngier 
23216baaeda8SSean Christopherson out_subs:
232278b3bf48SSean Christopherson 	teardown_subsystems();
23236baaeda8SSean Christopherson out_hyp:
23249ed24f4bSMarc Zyngier 	if (!in_hyp_mode)
23259ed24f4bSMarc Zyngier 		teardown_hyp_mode();
23269ed24f4bSMarc Zyngier out_err:
23273248136bSJulien Grall 	kvm_arm_vmid_alloc_free();
23289ed24f4bSMarc Zyngier 	return err;
23299ed24f4bSMarc Zyngier }
23309ed24f4bSMarc Zyngier 
2331d8b369c4SDavid Brazdil static int __init early_kvm_mode_cfg(char *arg)
2332d8b369c4SDavid Brazdil {
2333d8b369c4SDavid Brazdil 	if (!arg)
2334d8b369c4SDavid Brazdil 		return -EINVAL;
2335d8b369c4SDavid Brazdil 
2336b2a4d007SElliot Berman 	if (strcmp(arg, "none") == 0) {
2337b2a4d007SElliot Berman 		kvm_mode = KVM_MODE_NONE;
2338b2a4d007SElliot Berman 		return 0;
2339b2a4d007SElliot Berman 	}
2340b2a4d007SElliot Berman 
2341b2a4d007SElliot Berman 	if (!is_hyp_mode_available()) {
2342b2a4d007SElliot Berman 		pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2343b2a4d007SElliot Berman 		return 0;
2344b2a4d007SElliot Berman 	}
2345b2a4d007SElliot Berman 
2346d8b369c4SDavid Brazdil 	if (strcmp(arg, "protected") == 0) {
2347cde5042aSWill Deacon 		if (!is_kernel_in_hyp_mode())
2348d8b369c4SDavid Brazdil 			kvm_mode = KVM_MODE_PROTECTED;
2349cde5042aSWill Deacon 		else
2350cde5042aSWill Deacon 			pr_warn_once("Protected KVM not available with VHE\n");
2351cde5042aSWill Deacon 
2352d8b369c4SDavid Brazdil 		return 0;
2353d8b369c4SDavid Brazdil 	}
2354d8b369c4SDavid Brazdil 
2355b6a68b97SMarc Zyngier 	if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2356b6a68b97SMarc Zyngier 		kvm_mode = KVM_MODE_DEFAULT;
23571945a067SMarc Zyngier 		return 0;
2358b6a68b97SMarc Zyngier 	}
2359b6a68b97SMarc Zyngier 
2360675cabc8SJintack Lim 	if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
2361675cabc8SJintack Lim 		kvm_mode = KVM_MODE_NV;
2362675cabc8SJintack Lim 		return 0;
2363675cabc8SJintack Lim 	}
2364675cabc8SJintack Lim 
2365d8b369c4SDavid Brazdil 	return -EINVAL;
2366d8b369c4SDavid Brazdil }
2367d8b369c4SDavid Brazdil early_param("kvm-arm.mode", early_kvm_mode_cfg);
2368d8b369c4SDavid Brazdil 
23693eb681fbSDavid Brazdil enum kvm_mode kvm_get_mode(void)
23703eb681fbSDavid Brazdil {
23713eb681fbSDavid Brazdil 	return kvm_mode;
23723eb681fbSDavid Brazdil }
23733eb681fbSDavid Brazdil 
23741dc0f02dSSean Christopherson module_init(kvm_arm_init);
2375