xref: /linux/arch/arm64/kvm/arm.c (revision 0aa34b37a78d063da58838b84b20a68a94d919fd)
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>
199ed24f4bSMarc Zyngier #include <linux/kvm.h>
209ed24f4bSMarc Zyngier #include <linux/kvm_irqfd.h>
219ed24f4bSMarc Zyngier #include <linux/irqbypass.h>
229ed24f4bSMarc Zyngier #include <linux/sched/stat.h>
23eeeee719SDavid Brazdil #include <linux/psci.h>
249ed24f4bSMarc Zyngier #include <trace/events/kvm.h>
259ed24f4bSMarc Zyngier 
269ed24f4bSMarc Zyngier #define CREATE_TRACE_POINTS
279ed24f4bSMarc Zyngier #include "trace_arm.h"
289ed24f4bSMarc Zyngier 
299ed24f4bSMarc Zyngier #include <linux/uaccess.h>
309ed24f4bSMarc Zyngier #include <asm/ptrace.h>
319ed24f4bSMarc Zyngier #include <asm/mman.h>
329ed24f4bSMarc Zyngier #include <asm/tlbflush.h>
339ed24f4bSMarc Zyngier #include <asm/cacheflush.h>
349ed24f4bSMarc Zyngier #include <asm/cpufeature.h>
359ed24f4bSMarc Zyngier #include <asm/virt.h>
369ed24f4bSMarc Zyngier #include <asm/kvm_arm.h>
379ed24f4bSMarc Zyngier #include <asm/kvm_asm.h>
38814ad8f9SMarc Zyngier #include <asm/kvm_emulate.h>
399ed24f4bSMarc Zyngier #include <asm/kvm_mmu.h>
40a77b31dcSMarc Zyngier #include <asm/kvm_nested.h>
419d0c063aSFuad Tabba #include <asm/kvm_pkvm.h>
42814ad8f9SMarc Zyngier #include <asm/kvm_ptrauth.h>
439ed24f4bSMarc Zyngier #include <asm/sections.h>
449ed24f4bSMarc Zyngier 
459ed24f4bSMarc Zyngier #include <kvm/arm_hypercalls.h>
469ed24f4bSMarc Zyngier #include <kvm/arm_pmu.h>
479ed24f4bSMarc Zyngier #include <kvm/arm_psci.h>
489ed24f4bSMarc Zyngier 
49d8b369c4SDavid Brazdil static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
50d8b369c4SDavid Brazdil 
510b5afe05SColton Lewis enum kvm_wfx_trap_policy {
520b5afe05SColton Lewis 	KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */
530b5afe05SColton Lewis 	KVM_WFX_NOTRAP,
540b5afe05SColton Lewis 	KVM_WFX_TRAP,
550b5afe05SColton Lewis };
560b5afe05SColton Lewis 
570b5afe05SColton Lewis static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
580b5afe05SColton Lewis static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK;
590b5afe05SColton Lewis 
6014ef9d04SMarc Zyngier DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
6114ef9d04SMarc Zyngier 
62db129d48SKalesh Singh DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
6363fec243SDavid Brazdil DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
649ed24f4bSMarc Zyngier 
658c15c2a0SMostafa Saleh DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
668c15c2a0SMostafa Saleh 
67fa729bc7SSudeep Holla static bool vgic_present, kvm_arm_initialised;
689ed24f4bSMarc Zyngier 
69733c758eSOliver Upton static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
709ed24f4bSMarc Zyngier DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
719ed24f4bSMarc Zyngier 
72fa729bc7SSudeep Holla bool is_kvm_arm_initialised(void)
73fa729bc7SSudeep Holla {
74fa729bc7SSudeep Holla 	return kvm_arm_initialised;
75fa729bc7SSudeep Holla }
76fa729bc7SSudeep Holla 
779ed24f4bSMarc Zyngier int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
789ed24f4bSMarc Zyngier {
799ed24f4bSMarc Zyngier 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
809ed24f4bSMarc Zyngier }
819ed24f4bSMarc Zyngier 
8292536992SFuad Tabba /*
8392536992SFuad Tabba  * This functions as an allow-list of protected VM capabilities.
8492536992SFuad Tabba  * Features not explicitly allowed by this function are denied.
8592536992SFuad Tabba  */
8692536992SFuad Tabba static bool pkvm_ext_allowed(struct kvm *kvm, long ext)
8792536992SFuad Tabba {
8892536992SFuad Tabba 	switch (ext) {
8992536992SFuad Tabba 	case KVM_CAP_IRQCHIP:
9092536992SFuad Tabba 	case KVM_CAP_ARM_PSCI:
9192536992SFuad Tabba 	case KVM_CAP_ARM_PSCI_0_2:
9292536992SFuad Tabba 	case KVM_CAP_NR_VCPUS:
9392536992SFuad Tabba 	case KVM_CAP_MAX_VCPUS:
9492536992SFuad Tabba 	case KVM_CAP_MAX_VCPU_ID:
9592536992SFuad Tabba 	case KVM_CAP_MSI_DEVID:
9692536992SFuad Tabba 	case KVM_CAP_ARM_VM_IPA_SIZE:
9792536992SFuad Tabba 	case KVM_CAP_ARM_PMU_V3:
9892536992SFuad Tabba 	case KVM_CAP_ARM_SVE:
9992536992SFuad Tabba 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
10092536992SFuad Tabba 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
10192536992SFuad Tabba 		return true;
10292536992SFuad Tabba 	default:
10392536992SFuad Tabba 		return false;
10492536992SFuad Tabba 	}
10592536992SFuad Tabba }
10692536992SFuad Tabba 
1079ed24f4bSMarc Zyngier int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
1089ed24f4bSMarc Zyngier 			    struct kvm_enable_cap *cap)
1099ed24f4bSMarc Zyngier {
11097a3dee1SFuad Tabba 	int r = -EINVAL;
1119ed24f4bSMarc Zyngier 
1129ed24f4bSMarc Zyngier 	if (cap->flags)
1139ed24f4bSMarc Zyngier 		return -EINVAL;
1149ed24f4bSMarc Zyngier 
11592536992SFuad Tabba 	if (kvm_vm_is_protected(kvm) && !pkvm_ext_allowed(kvm, cap->cap))
11692536992SFuad Tabba 		return -EINVAL;
11792536992SFuad Tabba 
1189ed24f4bSMarc Zyngier 	switch (cap->cap) {
1199ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
1209ed24f4bSMarc Zyngier 		r = 0;
12106394531SMarc Zyngier 		set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
12206394531SMarc Zyngier 			&kvm->arch.flags);
1239ed24f4bSMarc Zyngier 		break;
124673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
125c4d7c518SSteven Price 		mutex_lock(&kvm->lock);
12697a3dee1SFuad Tabba 		if (system_supports_mte() && !kvm->created_vcpus) {
127673638f4SSteven Price 			r = 0;
12806394531SMarc Zyngier 			set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
129c4d7c518SSteven Price 		}
130c4d7c518SSteven Price 		mutex_unlock(&kvm->lock);
131673638f4SSteven Price 		break;
132bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
133bfbab445SOliver Upton 		r = 0;
134bfbab445SOliver Upton 		set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
135bfbab445SOliver Upton 		break;
1362f440b72SRicardo Koller 	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
1372f440b72SRicardo Koller 		mutex_lock(&kvm->slots_lock);
1382f440b72SRicardo Koller 		/*
1392f440b72SRicardo Koller 		 * To keep things simple, allow changing the chunk
1402f440b72SRicardo Koller 		 * size only when no memory slots have been created.
1412f440b72SRicardo Koller 		 */
14297a3dee1SFuad Tabba 		if (kvm_are_all_memslots_empty(kvm)) {
14397a3dee1SFuad Tabba 			u64 new_cap = cap->args[0];
14497a3dee1SFuad Tabba 
14597a3dee1SFuad Tabba 			if (!new_cap || kvm_is_block_size_supported(new_cap)) {
1462f440b72SRicardo Koller 				r = 0;
1472f440b72SRicardo Koller 				kvm->arch.mmu.split_page_chunk_size = new_cap;
1482f440b72SRicardo Koller 			}
14997a3dee1SFuad Tabba 		}
1502f440b72SRicardo Koller 		mutex_unlock(&kvm->slots_lock);
1512f440b72SRicardo Koller 		break;
1529ed24f4bSMarc Zyngier 	default:
1539ed24f4bSMarc Zyngier 		break;
1549ed24f4bSMarc Zyngier 	}
1559ed24f4bSMarc Zyngier 
1569ed24f4bSMarc Zyngier 	return r;
1579ed24f4bSMarc Zyngier }
1589ed24f4bSMarc Zyngier 
1595107000fSMarc Zyngier static int kvm_arm_default_max_vcpus(void)
1605107000fSMarc Zyngier {
1615107000fSMarc Zyngier 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
1625107000fSMarc Zyngier }
1635107000fSMarc Zyngier 
1649ed24f4bSMarc Zyngier /**
1659ed24f4bSMarc Zyngier  * kvm_arch_init_vm - initializes a VM data structure
1669ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
167*0aa34b37SSebastian Ott  * @type:	kvm device type
1689ed24f4bSMarc Zyngier  */
1699ed24f4bSMarc Zyngier int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1709ed24f4bSMarc Zyngier {
171a0e50aa3SChristoffer Dall 	int ret;
1729ed24f4bSMarc Zyngier 
173c43120afSOliver Upton 	mutex_init(&kvm->arch.config_lock);
174c43120afSOliver Upton 
175c43120afSOliver Upton #ifdef CONFIG_LOCKDEP
176c43120afSOliver Upton 	/* Clue in lockdep that the config_lock must be taken inside kvm->lock */
177c43120afSOliver Upton 	mutex_lock(&kvm->lock);
178c43120afSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
179c43120afSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
180c43120afSOliver Upton 	mutex_unlock(&kvm->lock);
181c43120afSOliver Upton #endif
182c43120afSOliver Upton 
1834f128f8eSMarc Zyngier 	kvm_init_nested(kvm);
1844f128f8eSMarc Zyngier 
1853f868e14SQuentin Perret 	ret = kvm_share_hyp(kvm, kvm + 1);
1869ed24f4bSMarc Zyngier 	if (ret)
187315775ffSQuentin Perret 		return ret;
1889ed24f4bSMarc Zyngier 
1899d0c063aSFuad Tabba 	ret = pkvm_init_host_vm(kvm);
1909d0c063aSFuad Tabba 	if (ret)
191315775ffSQuentin Perret 		goto err_unshare_kvm;
1929ed24f4bSMarc Zyngier 
1935f623a59SOliver Upton 	if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
194ae187fecSWill Deacon 		ret = -ENOMEM;
195315775ffSQuentin Perret 		goto err_unshare_kvm;
196ae187fecSWill Deacon 	}
197583cda1bSAlexandru Elisei 	cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
198583cda1bSAlexandru Elisei 
199315775ffSQuentin Perret 	ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
200315775ffSQuentin Perret 	if (ret)
201315775ffSQuentin Perret 		goto err_free_cpumask;
202315775ffSQuentin Perret 
2039ed24f4bSMarc Zyngier 	kvm_vgic_early_init(kvm);
2049ed24f4bSMarc Zyngier 
2058a5eb2d2SMarc Zyngier 	kvm_timer_init_vm(kvm);
2068a5eb2d2SMarc Zyngier 
2079ed24f4bSMarc Zyngier 	/* The maximum number of VCPUs is limited by the host's GIC model */
208f502cc56SSean Christopherson 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
2099ed24f4bSMarc Zyngier 
21005714cabSRaghavendra Rao Ananta 	kvm_arm_init_hypercalls(kvm);
21123711a5eSMarc Zyngier 
2122251e9ffSOliver Upton 	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
2133d0dba57SMarc Zyngier 
214315775ffSQuentin Perret 	return 0;
215315775ffSQuentin Perret 
216315775ffSQuentin Perret err_free_cpumask:
217315775ffSQuentin Perret 	free_cpumask_var(kvm->arch.supported_cpus);
218315775ffSQuentin Perret err_unshare_kvm:
219315775ffSQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
2209ed24f4bSMarc Zyngier 	return ret;
2219ed24f4bSMarc Zyngier }
2229ed24f4bSMarc Zyngier 
2239ed24f4bSMarc Zyngier vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
2249ed24f4bSMarc Zyngier {
2259ed24f4bSMarc Zyngier 	return VM_FAULT_SIGBUS;
2269ed24f4bSMarc Zyngier }
2279ed24f4bSMarc Zyngier 
2280d874858SOliver Upton void kvm_arch_create_vm_debugfs(struct kvm *kvm)
2295c1ebe9aSOliver Upton {
2305c1ebe9aSOliver Upton 	kvm_sys_regs_create_debugfs(kvm);
2315c1ebe9aSOliver Upton }
2329ed24f4bSMarc Zyngier 
233ce5d2448SOliver Upton static void kvm_destroy_mpidr_data(struct kvm *kvm)
234ce5d2448SOliver Upton {
235ce5d2448SOliver Upton 	struct kvm_mpidr_data *data;
236ce5d2448SOliver Upton 
237ce5d2448SOliver Upton 	mutex_lock(&kvm->arch.config_lock);
238ce5d2448SOliver Upton 
239ce5d2448SOliver Upton 	data = rcu_dereference_protected(kvm->arch.mpidr_data,
240ce5d2448SOliver Upton 					 lockdep_is_held(&kvm->arch.config_lock));
241ce5d2448SOliver Upton 	if (data) {
242ce5d2448SOliver Upton 		rcu_assign_pointer(kvm->arch.mpidr_data, NULL);
243ce5d2448SOliver Upton 		synchronize_rcu();
244ce5d2448SOliver Upton 		kfree(data);
245ce5d2448SOliver Upton 	}
246ce5d2448SOliver Upton 
247ce5d2448SOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
248ce5d2448SOliver Upton }
249ce5d2448SOliver Upton 
2509ed24f4bSMarc Zyngier /**
2519ed24f4bSMarc Zyngier  * kvm_arch_destroy_vm - destroy the VM data structure
2529ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
2539ed24f4bSMarc Zyngier  */
2549ed24f4bSMarc Zyngier void kvm_arch_destroy_vm(struct kvm *kvm)
2559ed24f4bSMarc Zyngier {
256d7eec236SMarc Zyngier 	bitmap_free(kvm->arch.pmu_filter);
257583cda1bSAlexandru Elisei 	free_cpumask_var(kvm->arch.supported_cpus);
258d7eec236SMarc Zyngier 
2599ed24f4bSMarc Zyngier 	kvm_vgic_destroy(kvm);
2609ed24f4bSMarc Zyngier 
2619d0c063aSFuad Tabba 	if (is_protected_kvm_enabled())
2629d0c063aSFuad Tabba 		pkvm_destroy_hyp_vm(kvm);
2639d0c063aSFuad Tabba 
264ce5d2448SOliver Upton 	kvm_destroy_mpidr_data(kvm);
265ce5d2448SOliver Upton 
266888f0880SMarc Zyngier 	kfree(kvm->arch.sysreg_masks);
26727592ae8SMarc Zyngier 	kvm_destroy_vcpus(kvm);
26852b28657SQuentin Perret 
26952b28657SQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
270fb88707dSOliver Upton 
271fb88707dSOliver Upton 	kvm_arm_teardown_hypercalls(kvm);
2729ed24f4bSMarc Zyngier }
2739ed24f4bSMarc Zyngier 
274a07e9345SMarc Zyngier static bool kvm_has_full_ptr_auth(void)
275a07e9345SMarc Zyngier {
276a07e9345SMarc Zyngier 	bool apa, gpa, api, gpi, apa3, gpa3;
277a07e9345SMarc Zyngier 	u64 isar1, isar2, val;
278a07e9345SMarc Zyngier 
279a07e9345SMarc Zyngier 	/*
280a07e9345SMarc Zyngier 	 * Check that:
281a07e9345SMarc Zyngier 	 *
282a07e9345SMarc Zyngier 	 * - both Address and Generic auth are implemented for a given
283a07e9345SMarc Zyngier          *   algorithm (Q5, IMPDEF or Q3)
284a07e9345SMarc Zyngier 	 * - only a single algorithm is implemented.
285a07e9345SMarc Zyngier 	 */
286a07e9345SMarc Zyngier 	if (!system_has_full_ptr_auth())
287a07e9345SMarc Zyngier 		return false;
288a07e9345SMarc Zyngier 
289a07e9345SMarc Zyngier 	isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
290a07e9345SMarc Zyngier 	isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
291a07e9345SMarc Zyngier 
292a07e9345SMarc Zyngier 	apa = !!FIELD_GET(ID_AA64ISAR1_EL1_APA_MASK, isar1);
293a07e9345SMarc Zyngier 	val = FIELD_GET(ID_AA64ISAR1_EL1_GPA_MASK, isar1);
294a07e9345SMarc Zyngier 	gpa = (val == ID_AA64ISAR1_EL1_GPA_IMP);
295a07e9345SMarc Zyngier 
296a07e9345SMarc Zyngier 	api = !!FIELD_GET(ID_AA64ISAR1_EL1_API_MASK, isar1);
297a07e9345SMarc Zyngier 	val = FIELD_GET(ID_AA64ISAR1_EL1_GPI_MASK, isar1);
298a07e9345SMarc Zyngier 	gpi = (val == ID_AA64ISAR1_EL1_GPI_IMP);
299a07e9345SMarc Zyngier 
300a07e9345SMarc Zyngier 	apa3 = !!FIELD_GET(ID_AA64ISAR2_EL1_APA3_MASK, isar2);
301a07e9345SMarc Zyngier 	val  = FIELD_GET(ID_AA64ISAR2_EL1_GPA3_MASK, isar2);
302a07e9345SMarc Zyngier 	gpa3 = (val == ID_AA64ISAR2_EL1_GPA3_IMP);
303a07e9345SMarc Zyngier 
304a07e9345SMarc Zyngier 	return (apa == gpa && api == gpi && apa3 == gpa3 &&
305a07e9345SMarc Zyngier 		(apa + api + apa3) == 1);
306a07e9345SMarc Zyngier }
307a07e9345SMarc Zyngier 
3089ed24f4bSMarc Zyngier int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
3099ed24f4bSMarc Zyngier {
3109ed24f4bSMarc Zyngier 	int r;
31192536992SFuad Tabba 
31292536992SFuad Tabba 	if (kvm && kvm_vm_is_protected(kvm) && !pkvm_ext_allowed(kvm, ext))
31392536992SFuad Tabba 		return 0;
31492536992SFuad Tabba 
3159ed24f4bSMarc Zyngier 	switch (ext) {
3169ed24f4bSMarc Zyngier 	case KVM_CAP_IRQCHIP:
3179ed24f4bSMarc Zyngier 		r = vgic_present;
3189ed24f4bSMarc Zyngier 		break;
3199ed24f4bSMarc Zyngier 	case KVM_CAP_IOEVENTFD:
3209ed24f4bSMarc Zyngier 	case KVM_CAP_USER_MEMORY:
3219ed24f4bSMarc Zyngier 	case KVM_CAP_SYNC_MMU:
3229ed24f4bSMarc Zyngier 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
3239ed24f4bSMarc Zyngier 	case KVM_CAP_ONE_REG:
3249ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI:
3259ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI_0_2:
3269ed24f4bSMarc Zyngier 	case KVM_CAP_READONLY_MEM:
3279ed24f4bSMarc Zyngier 	case KVM_CAP_MP_STATE:
3289ed24f4bSMarc Zyngier 	case KVM_CAP_IMMEDIATE_EXIT:
3299ed24f4bSMarc Zyngier 	case KVM_CAP_VCPU_EVENTS:
3309ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
3319ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
3329ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_INJECT_EXT_DABT:
33336fb4cd5SWill Deacon 	case KVM_CAP_SET_GUEST_DEBUG:
33436fb4cd5SWill Deacon 	case KVM_CAP_VCPU_ATTRIBUTES:
3353bf72569SJianyong Wu 	case KVM_CAP_PTP_KVM:
336bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
33752882b9cSAlexey Kardashevskiy 	case KVM_CAP_IRQFD_RESAMPLE:
33830ec7997SMarc Zyngier 	case KVM_CAP_COUNTER_OFFSET:
3399ed24f4bSMarc Zyngier 		r = 1;
3409ed24f4bSMarc Zyngier 		break;
341fa18aca9SMaxim Levitsky 	case KVM_CAP_SET_GUEST_DEBUG2:
342fa18aca9SMaxim Levitsky 		return KVM_GUESTDBG_VALID_MASK;
3439ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
3449ed24f4bSMarc Zyngier 		r = 1;
3459ed24f4bSMarc Zyngier 		break;
3469ed24f4bSMarc Zyngier 	case KVM_CAP_NR_VCPUS:
347f60a00d7SVitaly Kuznetsov 		/*
348f60a00d7SVitaly Kuznetsov 		 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
349f60a00d7SVitaly Kuznetsov 		 * architectures, as it does not always bound it to
350f60a00d7SVitaly Kuznetsov 		 * KVM_CAP_MAX_VCPUS. It should not matter much because
351f60a00d7SVitaly Kuznetsov 		 * this is just an advisory value.
352f60a00d7SVitaly Kuznetsov 		 */
353f60a00d7SVitaly Kuznetsov 		r = min_t(unsigned int, num_online_cpus(),
354f60a00d7SVitaly Kuznetsov 			  kvm_arm_default_max_vcpus());
3559ed24f4bSMarc Zyngier 		break;
3569ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPUS:
3579ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPU_ID:
3585107000fSMarc Zyngier 		if (kvm)
359f502cc56SSean Christopherson 			r = kvm->max_vcpus;
3605107000fSMarc Zyngier 		else
3615107000fSMarc Zyngier 			r = kvm_arm_default_max_vcpus();
3629ed24f4bSMarc Zyngier 		break;
3639ed24f4bSMarc Zyngier 	case KVM_CAP_MSI_DEVID:
3649ed24f4bSMarc Zyngier 		if (!kvm)
3659ed24f4bSMarc Zyngier 			r = -EINVAL;
3669ed24f4bSMarc Zyngier 		else
3679ed24f4bSMarc Zyngier 			r = kvm->arch.vgic.msis_require_devid;
3689ed24f4bSMarc Zyngier 		break;
3699ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_USER_IRQ:
3709ed24f4bSMarc Zyngier 		/*
3719ed24f4bSMarc Zyngier 		 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
3729ed24f4bSMarc Zyngier 		 * (bump this number if adding more devices)
3739ed24f4bSMarc Zyngier 		 */
3749ed24f4bSMarc Zyngier 		r = 1;
3759ed24f4bSMarc Zyngier 		break;
376673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
377673638f4SSteven Price 		r = system_supports_mte();
378673638f4SSteven Price 		break;
379004a0124SAndrew Jones 	case KVM_CAP_STEAL_TIME:
380004a0124SAndrew Jones 		r = kvm_arm_pvtime_supported();
381004a0124SAndrew Jones 		break;
38236fb4cd5SWill Deacon 	case KVM_CAP_ARM_EL1_32BIT:
383d8569fbaSMark Rutland 		r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1);
3849ed24f4bSMarc Zyngier 		break;
38536fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
38636fb4cd5SWill Deacon 		r = get_num_brps();
38736fb4cd5SWill Deacon 		break;
38836fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
38936fb4cd5SWill Deacon 		r = get_num_wrps();
39036fb4cd5SWill Deacon 		break;
39136fb4cd5SWill Deacon 	case KVM_CAP_ARM_PMU_V3:
39236fb4cd5SWill Deacon 		r = kvm_arm_support_pmu_v3();
39336fb4cd5SWill Deacon 		break;
39436fb4cd5SWill Deacon 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
395d8569fbaSMark Rutland 		r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN);
39636fb4cd5SWill Deacon 		break;
39736fb4cd5SWill Deacon 	case KVM_CAP_ARM_VM_IPA_SIZE:
39836fb4cd5SWill Deacon 		r = get_kvm_ipa_limit();
39936fb4cd5SWill Deacon 		break;
40036fb4cd5SWill Deacon 	case KVM_CAP_ARM_SVE:
40136fb4cd5SWill Deacon 		r = system_supports_sve();
40236fb4cd5SWill Deacon 		break;
40336fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
40436fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
405a07e9345SMarc Zyngier 		r = kvm_has_full_ptr_auth();
40636fb4cd5SWill Deacon 		break;
4072f440b72SRicardo Koller 	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
4082f440b72SRicardo Koller 		if (kvm)
4092f440b72SRicardo Koller 			r = kvm->arch.mmu.split_page_chunk_size;
4102f440b72SRicardo Koller 		else
4112f440b72SRicardo Koller 			r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
4122f440b72SRicardo Koller 		break;
4132f440b72SRicardo Koller 	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
4142f440b72SRicardo Koller 		r = kvm_supported_block_sizes();
4152f440b72SRicardo Koller 		break;
4163f9cd0caSJing Zhang 	case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
4173f9cd0caSJing Zhang 		r = BIT(0);
4183f9cd0caSJing Zhang 		break;
41936fb4cd5SWill Deacon 	default:
42036fb4cd5SWill Deacon 		r = 0;
4219ed24f4bSMarc Zyngier 	}
42236fb4cd5SWill Deacon 
4239ed24f4bSMarc Zyngier 	return r;
4249ed24f4bSMarc Zyngier }
4259ed24f4bSMarc Zyngier 
4269ed24f4bSMarc Zyngier long kvm_arch_dev_ioctl(struct file *filp,
4279ed24f4bSMarc Zyngier 			unsigned int ioctl, unsigned long arg)
4289ed24f4bSMarc Zyngier {
4299ed24f4bSMarc Zyngier 	return -EINVAL;
4309ed24f4bSMarc Zyngier }
4319ed24f4bSMarc Zyngier 
4329ed24f4bSMarc Zyngier struct kvm *kvm_arch_alloc_vm(void)
4339ed24f4bSMarc Zyngier {
434115bae92SJia He 	size_t sz = sizeof(struct kvm);
4359ed24f4bSMarc Zyngier 
436115bae92SJia He 	if (!has_vhe())
437115bae92SJia He 		return kzalloc(sz, GFP_KERNEL_ACCOUNT);
438115bae92SJia He 
439115bae92SJia He 	return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
4409ed24f4bSMarc Zyngier }
4419ed24f4bSMarc Zyngier 
4429ed24f4bSMarc Zyngier int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
4439ed24f4bSMarc Zyngier {
4449ed24f4bSMarc Zyngier 	if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
4459ed24f4bSMarc Zyngier 		return -EBUSY;
4469ed24f4bSMarc Zyngier 
447f502cc56SSean Christopherson 	if (id >= kvm->max_vcpus)
4489ed24f4bSMarc Zyngier 		return -EINVAL;
4499ed24f4bSMarc Zyngier 
4509ed24f4bSMarc Zyngier 	return 0;
4519ed24f4bSMarc Zyngier }
4529ed24f4bSMarc Zyngier 
4539ed24f4bSMarc Zyngier int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
4549ed24f4bSMarc Zyngier {
4559ed24f4bSMarc Zyngier 	int err;
4569ed24f4bSMarc Zyngier 
4570acc7239SOliver Upton 	spin_lock_init(&vcpu->arch.mp_state_lock);
4580acc7239SOliver Upton 
459c43120afSOliver Upton #ifdef CONFIG_LOCKDEP
460c43120afSOliver Upton 	/* Inform lockdep that the config_lock is acquired after vcpu->mutex */
461c43120afSOliver Upton 	mutex_lock(&vcpu->mutex);
462c43120afSOliver Upton 	mutex_lock(&vcpu->kvm->arch.config_lock);
463c43120afSOliver Upton 	mutex_unlock(&vcpu->kvm->arch.config_lock);
464c43120afSOliver Upton 	mutex_unlock(&vcpu->mutex);
465c43120afSOliver Upton #endif
466c43120afSOliver Upton 
4679ed24f4bSMarc Zyngier 	/* Force users to call KVM_ARM_VCPU_INIT */
468ef984060SOliver Upton 	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
4699ed24f4bSMarc Zyngier 
470e539451bSSean Christopherson 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
471e539451bSSean Christopherson 
4729ed24f4bSMarc Zyngier 	/* Set up the timer */
4739ed24f4bSMarc Zyngier 	kvm_timer_vcpu_init(vcpu);
4749ed24f4bSMarc Zyngier 
4759ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_init(vcpu);
4769ed24f4bSMarc Zyngier 
4779ed24f4bSMarc Zyngier 	kvm_arm_reset_debug_ptr(vcpu);
4789ed24f4bSMarc Zyngier 
4799ed24f4bSMarc Zyngier 	kvm_arm_pvtime_vcpu_init(&vcpu->arch);
4809ed24f4bSMarc Zyngier 
481a0e50aa3SChristoffer Dall 	vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
482a0e50aa3SChristoffer Dall 
483ce5d2448SOliver Upton 	/*
484ce5d2448SOliver Upton 	 * This vCPU may have been created after mpidr_data was initialized.
485ce5d2448SOliver Upton 	 * Throw out the pre-computed mappings if that is the case which forces
486ce5d2448SOliver Upton 	 * KVM to fall back to iteratively searching the vCPUs.
487ce5d2448SOliver Upton 	 */
488ce5d2448SOliver Upton 	kvm_destroy_mpidr_data(vcpu->kvm);
489ce5d2448SOliver Upton 
4909ed24f4bSMarc Zyngier 	err = kvm_vgic_vcpu_init(vcpu);
4919ed24f4bSMarc Zyngier 	if (err)
4929ed24f4bSMarc Zyngier 		return err;
4939ed24f4bSMarc Zyngier 
4943f868e14SQuentin Perret 	return kvm_share_hyp(vcpu, vcpu + 1);
4959ed24f4bSMarc Zyngier }
4969ed24f4bSMarc Zyngier 
4979ed24f4bSMarc Zyngier void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
4989ed24f4bSMarc Zyngier {
4999ed24f4bSMarc Zyngier }
5009ed24f4bSMarc Zyngier 
5019ed24f4bSMarc Zyngier void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
5029ed24f4bSMarc Zyngier {
503cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
5049ed24f4bSMarc Zyngier 		static_branch_dec(&userspace_irqchip_in_use);
5059ed24f4bSMarc Zyngier 
5069af3e08bSWill Deacon 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
5079ed24f4bSMarc Zyngier 	kvm_timer_vcpu_terminate(vcpu);
5089ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_destroy(vcpu);
50902e3858fSMarc Zyngier 	kvm_vgic_vcpu_destroy(vcpu);
5109ed24f4bSMarc Zyngier 	kvm_arm_vcpu_destroy(vcpu);
5119ed24f4bSMarc Zyngier }
5129ed24f4bSMarc Zyngier 
5139ed24f4bSMarc Zyngier void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
5149ed24f4bSMarc Zyngier {
5156109c5a6SSean Christopherson 
5169ed24f4bSMarc Zyngier }
5179ed24f4bSMarc Zyngier 
5189ed24f4bSMarc Zyngier void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
5199ed24f4bSMarc Zyngier {
5206109c5a6SSean Christopherson 
5219ed24f4bSMarc Zyngier }
5229ed24f4bSMarc Zyngier 
523814ad8f9SMarc Zyngier static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu)
524814ad8f9SMarc Zyngier {
525814ad8f9SMarc Zyngier 	if (vcpu_has_ptrauth(vcpu)) {
526814ad8f9SMarc Zyngier 		/*
527814ad8f9SMarc Zyngier 		 * Either we're running running an L2 guest, and the API/APK
528814ad8f9SMarc Zyngier 		 * bits come from L1's HCR_EL2, or API/APK are both set.
529814ad8f9SMarc Zyngier 		 */
530814ad8f9SMarc Zyngier 		if (unlikely(vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))) {
531814ad8f9SMarc Zyngier 			u64 val;
532814ad8f9SMarc Zyngier 
533814ad8f9SMarc Zyngier 			val = __vcpu_sys_reg(vcpu, HCR_EL2);
534814ad8f9SMarc Zyngier 			val &= (HCR_API | HCR_APK);
535814ad8f9SMarc Zyngier 			vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK);
536814ad8f9SMarc Zyngier 			vcpu->arch.hcr_el2 |= val;
537814ad8f9SMarc Zyngier 		} else {
538814ad8f9SMarc Zyngier 			vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK);
539814ad8f9SMarc Zyngier 		}
540814ad8f9SMarc Zyngier 
541814ad8f9SMarc Zyngier 		/*
542814ad8f9SMarc Zyngier 		 * Save the host keys if there is any chance for the guest
543814ad8f9SMarc Zyngier 		 * to use pauth, as the entry code will reload the guest
544814ad8f9SMarc Zyngier 		 * keys in that case.
545814ad8f9SMarc Zyngier 		 * Protected mode is the exception to that rule, as the
546814ad8f9SMarc Zyngier 		 * entry into the EL2 code eagerly switch back and forth
547814ad8f9SMarc Zyngier 		 * between host and hyp keys (and kvm_hyp_ctxt is out of
548814ad8f9SMarc Zyngier 		 * reach anyway).
549814ad8f9SMarc Zyngier 		 */
550814ad8f9SMarc Zyngier 		if (is_protected_kvm_enabled())
551814ad8f9SMarc Zyngier 			return;
552814ad8f9SMarc Zyngier 
553814ad8f9SMarc Zyngier 		if (vcpu->arch.hcr_el2 & (HCR_API | HCR_APK)) {
554814ad8f9SMarc Zyngier 			struct kvm_cpu_context *ctxt;
555814ad8f9SMarc Zyngier 			ctxt = this_cpu_ptr_hyp_sym(kvm_hyp_ctxt);
556814ad8f9SMarc Zyngier 			ptrauth_save_keys(ctxt);
557814ad8f9SMarc Zyngier 		}
558814ad8f9SMarc Zyngier 	}
559814ad8f9SMarc Zyngier }
560814ad8f9SMarc Zyngier 
5610b5afe05SColton Lewis static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu)
5620b5afe05SColton Lewis {
5630b5afe05SColton Lewis 	if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
5640b5afe05SColton Lewis 		return kvm_wfi_trap_policy == KVM_WFX_NOTRAP;
5650b5afe05SColton Lewis 
5660b5afe05SColton Lewis 	return single_task_running() &&
5670b5afe05SColton Lewis 	       (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) ||
5680b5afe05SColton Lewis 		vcpu->kvm->arch.vgic.nassgireq);
5690b5afe05SColton Lewis }
5700b5afe05SColton Lewis 
5710b5afe05SColton Lewis static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu)
5720b5afe05SColton Lewis {
5730b5afe05SColton Lewis 	if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK))
5740b5afe05SColton Lewis 		return kvm_wfe_trap_policy == KVM_WFX_NOTRAP;
5750b5afe05SColton Lewis 
5760b5afe05SColton Lewis 	return single_task_running();
5770b5afe05SColton Lewis }
5780b5afe05SColton Lewis 
5799ed24f4bSMarc Zyngier void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
5809ed24f4bSMarc Zyngier {
581a0e50aa3SChristoffer Dall 	struct kvm_s2_mmu *mmu;
5829ed24f4bSMarc Zyngier 	int *last_ran;
5839ed24f4bSMarc Zyngier 
5844f128f8eSMarc Zyngier 	if (vcpu_has_nv(vcpu))
5854f128f8eSMarc Zyngier 		kvm_vcpu_load_hw_mmu(vcpu);
5864f128f8eSMarc Zyngier 
587a0e50aa3SChristoffer Dall 	mmu = vcpu->arch.hw_mmu;
588a0e50aa3SChristoffer Dall 	last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
5899ed24f4bSMarc Zyngier 
5909ed24f4bSMarc Zyngier 	/*
59101dc9262SMarc Zyngier 	 * We guarantee that both TLBs and I-cache are private to each
59201dc9262SMarc Zyngier 	 * vcpu. If detecting that a vcpu from the same VM has
59301dc9262SMarc Zyngier 	 * previously run on the same physical CPU, call into the
59401dc9262SMarc Zyngier 	 * hypervisor code to nuke the relevant contexts.
59501dc9262SMarc Zyngier 	 *
5969ed24f4bSMarc Zyngier 	 * We might get preempted before the vCPU actually runs, but
5979ed24f4bSMarc Zyngier 	 * over-invalidation doesn't affect correctness.
5989ed24f4bSMarc Zyngier 	 */
5995f4bd815SMarc Zyngier 	if (*last_ran != vcpu->vcpu_idx) {
60001dc9262SMarc Zyngier 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
6015f4bd815SMarc Zyngier 		*last_ran = vcpu->vcpu_idx;
6029ed24f4bSMarc Zyngier 	}
6039ed24f4bSMarc Zyngier 
6049ed24f4bSMarc Zyngier 	vcpu->cpu = cpu;
6059ed24f4bSMarc Zyngier 
6069ed24f4bSMarc Zyngier 	kvm_vgic_load(vcpu);
6079ed24f4bSMarc Zyngier 	kvm_timer_vcpu_load(vcpu);
60813aeb9b4SDavid Brazdil 	if (has_vhe())
60927cde4c0SOliver Upton 		kvm_vcpu_load_vhe(vcpu);
6109ed24f4bSMarc Zyngier 	kvm_arch_vcpu_load_fp(vcpu);
6119ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_guest(vcpu);
6129ed24f4bSMarc Zyngier 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
6139ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
6149ed24f4bSMarc Zyngier 
6150b5afe05SColton Lewis 	if (kvm_vcpu_should_clear_twe(vcpu))
6160b5afe05SColton Lewis 		vcpu->arch.hcr_el2 &= ~HCR_TWE;
6179ed24f4bSMarc Zyngier 	else
6180b5afe05SColton Lewis 		vcpu->arch.hcr_el2 |= HCR_TWE;
6190b5afe05SColton Lewis 
6200b5afe05SColton Lewis 	if (kvm_vcpu_should_clear_twi(vcpu))
6210b5afe05SColton Lewis 		vcpu->arch.hcr_el2 &= ~HCR_TWI;
6220b5afe05SColton Lewis 	else
6230b5afe05SColton Lewis 		vcpu->arch.hcr_el2 |= HCR_TWI;
6249ed24f4bSMarc Zyngier 
625814ad8f9SMarc Zyngier 	vcpu_set_pauth_traps(vcpu);
626814ad8f9SMarc Zyngier 
627d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_load_debug_state_flags(vcpu);
628583cda1bSAlexandru Elisei 
6291ba11daeSShaoqin Huang 	if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
630583cda1bSAlexandru Elisei 		vcpu_set_on_unsupported_cpu(vcpu);
6319ed24f4bSMarc Zyngier }
6329ed24f4bSMarc Zyngier 
6339ed24f4bSMarc Zyngier void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
6349ed24f4bSMarc Zyngier {
635d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_put_debug_state_flags(vcpu);
6369ed24f4bSMarc Zyngier 	kvm_arch_vcpu_put_fp(vcpu);
63713aeb9b4SDavid Brazdil 	if (has_vhe())
63827cde4c0SOliver Upton 		kvm_vcpu_put_vhe(vcpu);
6399ed24f4bSMarc Zyngier 	kvm_timer_vcpu_put(vcpu);
6409ed24f4bSMarc Zyngier 	kvm_vgic_put(vcpu);
6419ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_host(vcpu);
6424f128f8eSMarc Zyngier 	if (vcpu_has_nv(vcpu))
6434f128f8eSMarc Zyngier 		kvm_vcpu_put_hw_mmu(vcpu);
644100b4f09SShameer Kolothum 	kvm_arm_vmid_clear_active();
6459ed24f4bSMarc Zyngier 
646583cda1bSAlexandru Elisei 	vcpu_clear_on_unsupported_cpu(vcpu);
6479ed24f4bSMarc Zyngier 	vcpu->cpu = -1;
6489ed24f4bSMarc Zyngier }
6499ed24f4bSMarc Zyngier 
6500acc7239SOliver Upton static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
6519ed24f4bSMarc Zyngier {
6520acc7239SOliver Upton 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
6539ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
6549ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
6559ed24f4bSMarc Zyngier }
6569ed24f4bSMarc Zyngier 
6570acc7239SOliver Upton void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
6580acc7239SOliver Upton {
6590acc7239SOliver Upton 	spin_lock(&vcpu->arch.mp_state_lock);
6600acc7239SOliver Upton 	__kvm_arm_vcpu_power_off(vcpu);
6610acc7239SOliver Upton 	spin_unlock(&vcpu->arch.mp_state_lock);
6620acc7239SOliver Upton }
6630acc7239SOliver Upton 
664b171f9bbSOliver Upton bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
665b171f9bbSOliver Upton {
6660acc7239SOliver Upton 	return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
667b171f9bbSOliver Upton }
668b171f9bbSOliver Upton 
6697b33a09dSOliver Upton static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
6707b33a09dSOliver Upton {
6710acc7239SOliver Upton 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
6727b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
6737b33a09dSOliver Upton 	kvm_vcpu_kick(vcpu);
6747b33a09dSOliver Upton }
6757b33a09dSOliver Upton 
6767b33a09dSOliver Upton static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
6777b33a09dSOliver Upton {
6780acc7239SOliver Upton 	return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
6797b33a09dSOliver Upton }
6807b33a09dSOliver Upton 
6819ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
6829ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
6839ed24f4bSMarc Zyngier {
6840acc7239SOliver Upton 	*mp_state = READ_ONCE(vcpu->arch.mp_state);
6859ed24f4bSMarc Zyngier 
6869ed24f4bSMarc Zyngier 	return 0;
6879ed24f4bSMarc Zyngier }
6889ed24f4bSMarc Zyngier 
6899ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
6909ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
6919ed24f4bSMarc Zyngier {
6929ed24f4bSMarc Zyngier 	int ret = 0;
6939ed24f4bSMarc Zyngier 
6940acc7239SOliver Upton 	spin_lock(&vcpu->arch.mp_state_lock);
6950acc7239SOliver Upton 
6969ed24f4bSMarc Zyngier 	switch (mp_state->mp_state) {
6979ed24f4bSMarc Zyngier 	case KVM_MP_STATE_RUNNABLE:
6980acc7239SOliver Upton 		WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
6999ed24f4bSMarc Zyngier 		break;
7009ed24f4bSMarc Zyngier 	case KVM_MP_STATE_STOPPED:
7010acc7239SOliver Upton 		__kvm_arm_vcpu_power_off(vcpu);
7029ed24f4bSMarc Zyngier 		break;
7037b33a09dSOliver Upton 	case KVM_MP_STATE_SUSPENDED:
7047b33a09dSOliver Upton 		kvm_arm_vcpu_suspend(vcpu);
7059ed24f4bSMarc Zyngier 		break;
7069ed24f4bSMarc Zyngier 	default:
7079ed24f4bSMarc Zyngier 		ret = -EINVAL;
7089ed24f4bSMarc Zyngier 	}
7099ed24f4bSMarc Zyngier 
7100acc7239SOliver Upton 	spin_unlock(&vcpu->arch.mp_state_lock);
7110acc7239SOliver Upton 
7129ed24f4bSMarc Zyngier 	return ret;
7139ed24f4bSMarc Zyngier }
7149ed24f4bSMarc Zyngier 
7159ed24f4bSMarc Zyngier /**
7169ed24f4bSMarc Zyngier  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
7179ed24f4bSMarc Zyngier  * @v:		The VCPU pointer
7189ed24f4bSMarc Zyngier  *
7199ed24f4bSMarc Zyngier  * If the guest CPU is not waiting for interrupts or an interrupt line is
7209ed24f4bSMarc Zyngier  * asserted, the CPU is by definition runnable.
7219ed24f4bSMarc Zyngier  */
7229ed24f4bSMarc Zyngier int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
7239ed24f4bSMarc Zyngier {
7249ed24f4bSMarc Zyngier 	bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
7259ed24f4bSMarc Zyngier 	return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
726b171f9bbSOliver Upton 		&& !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
7279ed24f4bSMarc Zyngier }
7289ed24f4bSMarc Zyngier 
7299ed24f4bSMarc Zyngier bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
7309ed24f4bSMarc Zyngier {
7319ed24f4bSMarc Zyngier 	return vcpu_mode_priv(vcpu);
7329ed24f4bSMarc Zyngier }
7339ed24f4bSMarc Zyngier 
7348e5b0adeSLinus Torvalds #ifdef CONFIG_GUEST_PERF_EVENTS
735e1bfc245SSean Christopherson unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
736e1bfc245SSean Christopherson {
737e1bfc245SSean Christopherson 	return *vcpu_pc(vcpu);
738e1bfc245SSean Christopherson }
7398e5b0adeSLinus Torvalds #endif
740e1bfc245SSean Christopherson 
7415544750eSMarc Zyngier static void kvm_init_mpidr_data(struct kvm *kvm)
7425544750eSMarc Zyngier {
7435544750eSMarc Zyngier 	struct kvm_mpidr_data *data = NULL;
7445544750eSMarc Zyngier 	unsigned long c, mask, nr_entries;
7455544750eSMarc Zyngier 	u64 aff_set = 0, aff_clr = ~0UL;
7465544750eSMarc Zyngier 	struct kvm_vcpu *vcpu;
7475544750eSMarc Zyngier 
7485544750eSMarc Zyngier 	mutex_lock(&kvm->arch.config_lock);
7495544750eSMarc Zyngier 
750ce5d2448SOliver Upton 	if (rcu_access_pointer(kvm->arch.mpidr_data) ||
751ce5d2448SOliver Upton 	    atomic_read(&kvm->online_vcpus) == 1)
7525544750eSMarc Zyngier 		goto out;
7535544750eSMarc Zyngier 
7545544750eSMarc Zyngier 	kvm_for_each_vcpu(c, vcpu, kvm) {
7555544750eSMarc Zyngier 		u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
7565544750eSMarc Zyngier 		aff_set |= aff;
7575544750eSMarc Zyngier 		aff_clr &= aff;
7585544750eSMarc Zyngier 	}
7595544750eSMarc Zyngier 
7605544750eSMarc Zyngier 	/*
7615544750eSMarc Zyngier 	 * A significant bit can be either 0 or 1, and will only appear in
7625544750eSMarc Zyngier 	 * aff_set. Use aff_clr to weed out the useless stuff.
7635544750eSMarc Zyngier 	 */
7645544750eSMarc Zyngier 	mask = aff_set ^ aff_clr;
7655544750eSMarc Zyngier 	nr_entries = BIT_ULL(hweight_long(mask));
7665544750eSMarc Zyngier 
7675544750eSMarc Zyngier 	/*
7685544750eSMarc Zyngier 	 * Don't let userspace fool us. If we need more than a single page
7695544750eSMarc Zyngier 	 * to describe the compressed MPIDR array, just fall back to the
7705544750eSMarc Zyngier 	 * iterative method. Single vcpu VMs do not need this either.
7715544750eSMarc Zyngier 	 */
7725544750eSMarc Zyngier 	if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE)
7735544750eSMarc Zyngier 		data = kzalloc(struct_size(data, cmpidr_to_idx, nr_entries),
7745544750eSMarc Zyngier 			       GFP_KERNEL_ACCOUNT);
7755544750eSMarc Zyngier 
7765544750eSMarc Zyngier 	if (!data)
7775544750eSMarc Zyngier 		goto out;
7785544750eSMarc Zyngier 
7795544750eSMarc Zyngier 	data->mpidr_mask = mask;
7805544750eSMarc Zyngier 
7815544750eSMarc Zyngier 	kvm_for_each_vcpu(c, vcpu, kvm) {
7825544750eSMarc Zyngier 		u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
7835544750eSMarc Zyngier 		u16 index = kvm_mpidr_index(data, aff);
7845544750eSMarc Zyngier 
7855544750eSMarc Zyngier 		data->cmpidr_to_idx[index] = c;
7865544750eSMarc Zyngier 	}
7875544750eSMarc Zyngier 
788ce5d2448SOliver Upton 	rcu_assign_pointer(kvm->arch.mpidr_data, data);
7895544750eSMarc Zyngier out:
7905544750eSMarc Zyngier 	mutex_unlock(&kvm->arch.config_lock);
7915544750eSMarc Zyngier }
7925544750eSMarc Zyngier 
793b5aa368aSMarc Zyngier /*
794b5aa368aSMarc Zyngier  * Handle both the initialisation that is being done when the vcpu is
795b5aa368aSMarc Zyngier  * run for the first time, as well as the updates that must be
796b5aa368aSMarc Zyngier  * performed each time we get a new thread dealing with this vcpu.
797b5aa368aSMarc Zyngier  */
798b5aa368aSMarc Zyngier int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
7999ed24f4bSMarc Zyngier {
8009ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
801b5aa368aSMarc Zyngier 	int ret;
8029ed24f4bSMarc Zyngier 
803b5aa368aSMarc Zyngier 	if (!kvm_vcpu_initialized(vcpu))
804b5aa368aSMarc Zyngier 		return -ENOEXEC;
8059ed24f4bSMarc Zyngier 
8069ed24f4bSMarc Zyngier 	if (!kvm_arm_vcpu_is_finalized(vcpu))
8079ed24f4bSMarc Zyngier 		return -EPERM;
8089ed24f4bSMarc Zyngier 
809b5aa368aSMarc Zyngier 	ret = kvm_arch_vcpu_run_map_fp(vcpu);
810b5aa368aSMarc Zyngier 	if (ret)
811b5aa368aSMarc Zyngier 		return ret;
812b5aa368aSMarc Zyngier 
813cc5705fbSMarc Zyngier 	if (likely(vcpu_has_run_once(vcpu)))
814b5aa368aSMarc Zyngier 		return 0;
8159ed24f4bSMarc Zyngier 
8165544750eSMarc Zyngier 	kvm_init_mpidr_data(kvm);
8175544750eSMarc Zyngier 
818263d6287SAlexandru Elisei 	kvm_arm_vcpu_init_debug(vcpu);
819263d6287SAlexandru Elisei 
8209ed24f4bSMarc Zyngier 	if (likely(irqchip_in_kernel(kvm))) {
8219ed24f4bSMarc Zyngier 		/*
8229ed24f4bSMarc Zyngier 		 * Map the VGIC hardware resources before running a vcpu the
8239ed24f4bSMarc Zyngier 		 * first time on this VM.
8249ed24f4bSMarc Zyngier 		 */
8259ed24f4bSMarc Zyngier 		ret = kvm_vgic_map_resources(kvm);
8269ed24f4bSMarc Zyngier 		if (ret)
8279ed24f4bSMarc Zyngier 			return ret;
8289ed24f4bSMarc Zyngier 	}
8299ed24f4bSMarc Zyngier 
8303ed0b512SMarc Zyngier 	if (vcpu_has_nv(vcpu)) {
8313ed0b512SMarc Zyngier 		ret = kvm_init_nv_sysregs(vcpu->kvm);
8323ed0b512SMarc Zyngier 		if (ret)
8333ed0b512SMarc Zyngier 			return ret;
8343ed0b512SMarc Zyngier 	}
8353ed0b512SMarc Zyngier 
836c5bac1efSMarc Zyngier 	/*
837c5bac1efSMarc Zyngier 	 * This needs to happen after NV has imposed its own restrictions on
838c5bac1efSMarc Zyngier 	 * the feature set
839c5bac1efSMarc Zyngier 	 */
840f1ff3fc5SSebastian Ott 	kvm_calculate_traps(vcpu);
841c5bac1efSMarc Zyngier 
8429ed24f4bSMarc Zyngier 	ret = kvm_timer_enable(vcpu);
8439ed24f4bSMarc Zyngier 	if (ret)
8449ed24f4bSMarc Zyngier 		return ret;
8459ed24f4bSMarc Zyngier 
8469ed24f4bSMarc Zyngier 	ret = kvm_arm_pmu_v3_enable(vcpu);
8471408e73dSMarc Zyngier 	if (ret)
8481408e73dSMarc Zyngier 		return ret;
8491408e73dSMarc Zyngier 
8509d0c063aSFuad Tabba 	if (is_protected_kvm_enabled()) {
8519d0c063aSFuad Tabba 		ret = pkvm_create_hyp_vm(kvm);
8529d0c063aSFuad Tabba 		if (ret)
8539d0c063aSFuad Tabba 			return ret;
8549d0c063aSFuad Tabba 	}
8559d0c063aSFuad Tabba 
8561408e73dSMarc Zyngier 	if (!irqchip_in_kernel(kvm)) {
8571408e73dSMarc Zyngier 		/*
8581408e73dSMarc Zyngier 		 * Tell the rest of the code that there are userspace irqchip
8591408e73dSMarc Zyngier 		 * VMs in the wild.
8601408e73dSMarc Zyngier 		 */
8611408e73dSMarc Zyngier 		static_branch_inc(&userspace_irqchip_in_use);
8621408e73dSMarc Zyngier 	}
8639ed24f4bSMarc Zyngier 
8642a0c3433SFuad Tabba 	/*
8652a0c3433SFuad Tabba 	 * Initialize traps for protected VMs.
8662a0c3433SFuad Tabba 	 * NOTE: Move to run in EL2 directly, rather than via a hypercall, once
8672a0c3433SFuad Tabba 	 * the code is in place for first run initialization at EL2.
8682a0c3433SFuad Tabba 	 */
8692a0c3433SFuad Tabba 	if (kvm_vm_is_protected(kvm))
8702a0c3433SFuad Tabba 		kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu);
8712a0c3433SFuad Tabba 
8724bba7f7dSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
87306394531SMarc Zyngier 	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
8744bba7f7dSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
8755177fe91SMarc Zyngier 
8769ed24f4bSMarc Zyngier 	return ret;
8779ed24f4bSMarc Zyngier }
8789ed24f4bSMarc Zyngier 
8799ed24f4bSMarc Zyngier bool kvm_arch_intc_initialized(struct kvm *kvm)
8809ed24f4bSMarc Zyngier {
8819ed24f4bSMarc Zyngier 	return vgic_initialized(kvm);
8829ed24f4bSMarc Zyngier }
8839ed24f4bSMarc Zyngier 
8849ed24f4bSMarc Zyngier void kvm_arm_halt_guest(struct kvm *kvm)
8859ed24f4bSMarc Zyngier {
88646808a4cSMarc Zyngier 	unsigned long i;
8879ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
8889ed24f4bSMarc Zyngier 
8899ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm)
8909ed24f4bSMarc Zyngier 		vcpu->arch.pause = true;
8919ed24f4bSMarc Zyngier 	kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
8929ed24f4bSMarc Zyngier }
8939ed24f4bSMarc Zyngier 
8949ed24f4bSMarc Zyngier void kvm_arm_resume_guest(struct kvm *kvm)
8959ed24f4bSMarc Zyngier {
89646808a4cSMarc Zyngier 	unsigned long i;
8979ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
8989ed24f4bSMarc Zyngier 
8999ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
9009ed24f4bSMarc Zyngier 		vcpu->arch.pause = false;
901d92a5d1cSSean Christopherson 		__kvm_vcpu_wake_up(vcpu);
9029ed24f4bSMarc Zyngier 	}
9039ed24f4bSMarc Zyngier }
9049ed24f4bSMarc Zyngier 
9051c6219e3SOliver Upton static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
9069ed24f4bSMarc Zyngier {
90738060944SPaolo Bonzini 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
9089ed24f4bSMarc Zyngier 
90938060944SPaolo Bonzini 	rcuwait_wait_event(wait,
910b171f9bbSOliver Upton 			   (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
91138060944SPaolo Bonzini 			   TASK_INTERRUPTIBLE);
9129ed24f4bSMarc Zyngier 
913b171f9bbSOliver Upton 	if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
9149ed24f4bSMarc Zyngier 		/* Awaken to handle a signal, request we sleep again later. */
9159ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_SLEEP, vcpu);
9169ed24f4bSMarc Zyngier 	}
9179ed24f4bSMarc Zyngier 
9189ed24f4bSMarc Zyngier 	/*
9199ed24f4bSMarc Zyngier 	 * Make sure we will observe a potential reset request if we've
9209ed24f4bSMarc Zyngier 	 * observed a change to the power state. Pairs with the smp_wmb() in
9219ed24f4bSMarc Zyngier 	 * kvm_psci_vcpu_on().
9229ed24f4bSMarc Zyngier 	 */
9239ed24f4bSMarc Zyngier 	smp_rmb();
9249ed24f4bSMarc Zyngier }
9259ed24f4bSMarc Zyngier 
9266109c5a6SSean Christopherson /**
9276109c5a6SSean Christopherson  * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
9286109c5a6SSean Christopherson  * @vcpu:	The VCPU pointer
9296109c5a6SSean Christopherson  *
9306109c5a6SSean Christopherson  * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
9316109c5a6SSean Christopherson  * the vCPU is runnable.  The vCPU may or may not be scheduled out, depending
9326109c5a6SSean Christopherson  * on when a wake event arrives, e.g. there may already be a pending wake event.
9336109c5a6SSean Christopherson  */
9346109c5a6SSean Christopherson void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
9356109c5a6SSean Christopherson {
9366109c5a6SSean Christopherson 	/*
9376109c5a6SSean Christopherson 	 * Sync back the state of the GIC CPU interface so that we have
9386109c5a6SSean Christopherson 	 * the latest PMR and group enables. This ensures that
9396109c5a6SSean Christopherson 	 * kvm_arch_vcpu_runnable has up-to-date data to decide whether
9406109c5a6SSean Christopherson 	 * we have pending interrupts, e.g. when determining if the
9416109c5a6SSean Christopherson 	 * vCPU should block.
9426109c5a6SSean Christopherson 	 *
9436109c5a6SSean Christopherson 	 * For the same reason, we want to tell GICv4 that we need
9446109c5a6SSean Christopherson 	 * doorbells to be signalled, should an interrupt become pending.
9456109c5a6SSean Christopherson 	 */
9466109c5a6SSean Christopherson 	preempt_disable();
947b321c31cSMarc Zyngier 	vcpu_set_flag(vcpu, IN_WFI);
948948e1a53SMarc Zyngier 	kvm_vgic_put(vcpu);
9496109c5a6SSean Christopherson 	preempt_enable();
9506109c5a6SSean Christopherson 
95191b99ea7SSean Christopherson 	kvm_vcpu_halt(vcpu);
952eebc538dSMarc Zyngier 	vcpu_clear_flag(vcpu, IN_WFIT);
9536109c5a6SSean Christopherson 
9546109c5a6SSean Christopherson 	preempt_disable();
955b321c31cSMarc Zyngier 	vcpu_clear_flag(vcpu, IN_WFI);
956948e1a53SMarc Zyngier 	kvm_vgic_load(vcpu);
9576109c5a6SSean Christopherson 	preempt_enable();
9586109c5a6SSean Christopherson }
9596109c5a6SSean Christopherson 
9607b33a09dSOliver Upton static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
9617b33a09dSOliver Upton {
9627b33a09dSOliver Upton 	if (!kvm_arm_vcpu_suspended(vcpu))
9637b33a09dSOliver Upton 		return 1;
9647b33a09dSOliver Upton 
9657b33a09dSOliver Upton 	kvm_vcpu_wfi(vcpu);
9667b33a09dSOliver Upton 
9677b33a09dSOliver Upton 	/*
9687b33a09dSOliver Upton 	 * The suspend state is sticky; we do not leave it until userspace
9697b33a09dSOliver Upton 	 * explicitly marks the vCPU as runnable. Request that we suspend again
9707b33a09dSOliver Upton 	 * later.
9717b33a09dSOliver Upton 	 */
9727b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
9737b33a09dSOliver Upton 
9747b33a09dSOliver Upton 	/*
9757b33a09dSOliver Upton 	 * Check to make sure the vCPU is actually runnable. If so, exit to
9767b33a09dSOliver Upton 	 * userspace informing it of the wakeup condition.
9777b33a09dSOliver Upton 	 */
9787b33a09dSOliver Upton 	if (kvm_arch_vcpu_runnable(vcpu)) {
9797b33a09dSOliver Upton 		memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
9807b33a09dSOliver Upton 		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
9817b33a09dSOliver Upton 		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
9827b33a09dSOliver Upton 		return 0;
9837b33a09dSOliver Upton 	}
9847b33a09dSOliver Upton 
9857b33a09dSOliver Upton 	/*
9867b33a09dSOliver Upton 	 * Otherwise, we were unblocked to process a different event, such as a
9877b33a09dSOliver Upton 	 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
9887b33a09dSOliver Upton 	 * process the event.
9897b33a09dSOliver Upton 	 */
9907b33a09dSOliver Upton 	return 1;
9917b33a09dSOliver Upton }
9927b33a09dSOliver Upton 
9933fdd0459SOliver Upton /**
9943fdd0459SOliver Upton  * check_vcpu_requests - check and handle pending vCPU requests
9953fdd0459SOliver Upton  * @vcpu:	the VCPU pointer
9963fdd0459SOliver Upton  *
9973fdd0459SOliver Upton  * Return: 1 if we should enter the guest
9983fdd0459SOliver Upton  *	   0 if we should exit to userspace
9993fdd0459SOliver Upton  *	   < 0 if we should exit to userspace, where the return value indicates
10003fdd0459SOliver Upton  *	   an error
10013fdd0459SOliver Upton  */
10023fdd0459SOliver Upton static int check_vcpu_requests(struct kvm_vcpu *vcpu)
10039ed24f4bSMarc Zyngier {
10049ed24f4bSMarc Zyngier 	if (kvm_request_pending(vcpu)) {
10059ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
10061c6219e3SOliver Upton 			kvm_vcpu_sleep(vcpu);
10079ed24f4bSMarc Zyngier 
10089ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
10099ed24f4bSMarc Zyngier 			kvm_reset_vcpu(vcpu);
10109ed24f4bSMarc Zyngier 
10119ed24f4bSMarc Zyngier 		/*
10129ed24f4bSMarc Zyngier 		 * Clear IRQ_PENDING requests that were made to guarantee
10139ed24f4bSMarc Zyngier 		 * that a VCPU sees new virtual interrupts.
10149ed24f4bSMarc Zyngier 		 */
10159ed24f4bSMarc Zyngier 		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
10169ed24f4bSMarc Zyngier 
10179ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
10189ed24f4bSMarc Zyngier 			kvm_update_stolen_time(vcpu);
10199ed24f4bSMarc Zyngier 
10209ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
10219ed24f4bSMarc Zyngier 			/* The distributor enable bits were changed */
10229ed24f4bSMarc Zyngier 			preempt_disable();
1023b321c31cSMarc Zyngier 			vgic_v4_put(vcpu);
10249ed24f4bSMarc Zyngier 			vgic_v4_load(vcpu);
10259ed24f4bSMarc Zyngier 			preempt_enable();
10269ed24f4bSMarc Zyngier 		}
1027d0c94c49SMarc Zyngier 
1028d0c94c49SMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
102927131b19SRaghavendra Rao Ananta 			kvm_vcpu_reload_pmu(vcpu);
10307b33a09dSOliver Upton 
1031b1f778a2SMarc Zyngier 		if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu))
1032b1f778a2SMarc Zyngier 			kvm_vcpu_pmu_restore_guest(vcpu);
1033b1f778a2SMarc Zyngier 
10347b33a09dSOliver Upton 		if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
10357b33a09dSOliver Upton 			return kvm_vcpu_suspend(vcpu);
10369cb1096fSGavin Shan 
10379cb1096fSGavin Shan 		if (kvm_dirty_ring_check_request(vcpu))
10389cb1096fSGavin Shan 			return 0;
10399ed24f4bSMarc Zyngier 	}
10403fdd0459SOliver Upton 
10413fdd0459SOliver Upton 	return 1;
10429ed24f4bSMarc Zyngier }
10439ed24f4bSMarc Zyngier 
10442f6a49bbSWill Deacon static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
10452f6a49bbSWill Deacon {
10462f6a49bbSWill Deacon 	if (likely(!vcpu_mode_is_32bit(vcpu)))
10472f6a49bbSWill Deacon 		return false;
10482f6a49bbSWill Deacon 
1049a77b31dcSMarc Zyngier 	if (vcpu_has_nv(vcpu))
1050a77b31dcSMarc Zyngier 		return true;
1051a77b31dcSMarc Zyngier 
1052f3c6efc7SOliver Upton 	return !kvm_supports_32bit_el0();
10532f6a49bbSWill Deacon }
10542f6a49bbSWill Deacon 
10559ed24f4bSMarc Zyngier /**
10566caa5812SOliver Upton  * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
10576caa5812SOliver Upton  * @vcpu:	The VCPU pointer
10586caa5812SOliver Upton  * @ret:	Pointer to write optional return code
10596caa5812SOliver Upton  *
10606caa5812SOliver Upton  * Returns: true if the VCPU needs to return to a preemptible + interruptible
10616caa5812SOliver Upton  *	    and skip guest entry.
10626caa5812SOliver Upton  *
10636caa5812SOliver Upton  * This function disambiguates between two different types of exits: exits to a
10646caa5812SOliver Upton  * preemptible + interruptible kernel context and exits to userspace. For an
10656caa5812SOliver Upton  * exit to userspace, this function will write the return code to ret and return
10666caa5812SOliver Upton  * true. For an exit to preemptible + interruptible kernel context (i.e. check
10676caa5812SOliver Upton  * for pending work and re-enter), return true without writing to ret.
10686caa5812SOliver Upton  */
10696caa5812SOliver Upton static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
10706caa5812SOliver Upton {
10716caa5812SOliver Upton 	struct kvm_run *run = vcpu->run;
10726caa5812SOliver Upton 
10736caa5812SOliver Upton 	/*
10746caa5812SOliver Upton 	 * If we're using a userspace irqchip, then check if we need
10756caa5812SOliver Upton 	 * to tell a userspace irqchip about timer or PMU level
10766caa5812SOliver Upton 	 * changes and if so, exit to userspace (the actual level
10776caa5812SOliver Upton 	 * state gets updated in kvm_timer_update_run and
10786caa5812SOliver Upton 	 * kvm_pmu_update_run below).
10796caa5812SOliver Upton 	 */
10806caa5812SOliver Upton 	if (static_branch_unlikely(&userspace_irqchip_in_use)) {
10816caa5812SOliver Upton 		if (kvm_timer_should_notify_user(vcpu) ||
10826caa5812SOliver Upton 		    kvm_pmu_should_notify_user(vcpu)) {
10836caa5812SOliver Upton 			*ret = -EINTR;
10846caa5812SOliver Upton 			run->exit_reason = KVM_EXIT_INTR;
10856caa5812SOliver Upton 			return true;
10866caa5812SOliver Upton 		}
10876caa5812SOliver Upton 	}
10886caa5812SOliver Upton 
1089583cda1bSAlexandru Elisei 	if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
1090583cda1bSAlexandru Elisei 		run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1091583cda1bSAlexandru Elisei 		run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
1092583cda1bSAlexandru Elisei 		run->fail_entry.cpu = smp_processor_id();
1093583cda1bSAlexandru Elisei 		*ret = 0;
1094583cda1bSAlexandru Elisei 		return true;
1095583cda1bSAlexandru Elisei 	}
1096583cda1bSAlexandru Elisei 
10976caa5812SOliver Upton 	return kvm_request_pending(vcpu) ||
10986caa5812SOliver Upton 			xfer_to_guest_mode_work_pending();
10996caa5812SOliver Upton }
11006caa5812SOliver Upton 
11018cfe148aSMark Rutland /*
11028cfe148aSMark Rutland  * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
11038cfe148aSMark Rutland  * the vCPU is running.
11048cfe148aSMark Rutland  *
11058cfe148aSMark Rutland  * This must be noinstr as instrumentation may make use of RCU, and this is not
11068cfe148aSMark Rutland  * safe during the EQS.
11078cfe148aSMark Rutland  */
11088cfe148aSMark Rutland static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
11098cfe148aSMark Rutland {
11108cfe148aSMark Rutland 	int ret;
11118cfe148aSMark Rutland 
11128cfe148aSMark Rutland 	guest_state_enter_irqoff();
11138cfe148aSMark Rutland 	ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
11148cfe148aSMark Rutland 	guest_state_exit_irqoff();
11158cfe148aSMark Rutland 
11168cfe148aSMark Rutland 	return ret;
11178cfe148aSMark Rutland }
11188cfe148aSMark Rutland 
11196caa5812SOliver Upton /**
11209ed24f4bSMarc Zyngier  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
11219ed24f4bSMarc Zyngier  * @vcpu:	The VCPU pointer
11229ed24f4bSMarc Zyngier  *
11239ed24f4bSMarc Zyngier  * This function is called through the VCPU_RUN ioctl called from user space. It
11249ed24f4bSMarc Zyngier  * will execute VM code in a loop until the time slice for the process is used
11259ed24f4bSMarc Zyngier  * or some emulation is needed from user space in which case the function will
11269ed24f4bSMarc Zyngier  * return with return value 0 and with the kvm_run structure filled in with the
11279ed24f4bSMarc Zyngier  * required data for the requested emulation.
11289ed24f4bSMarc Zyngier  */
112938060944SPaolo Bonzini int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
11309ed24f4bSMarc Zyngier {
113138060944SPaolo Bonzini 	struct kvm_run *run = vcpu->run;
11329ed24f4bSMarc Zyngier 	int ret;
11339ed24f4bSMarc Zyngier 
11349ed24f4bSMarc Zyngier 	if (run->exit_reason == KVM_EXIT_MMIO) {
113574cc7e0cSTianjia Zhang 		ret = kvm_handle_mmio_return(vcpu);
1136cc81b6dfSFuad Tabba 		if (ret <= 0)
11379ed24f4bSMarc Zyngier 			return ret;
11389ed24f4bSMarc Zyngier 	}
11399ed24f4bSMarc Zyngier 
11409ed24f4bSMarc Zyngier 	vcpu_load(vcpu);
11419ed24f4bSMarc Zyngier 
1142a6816314SDavid Matlack 	if (!vcpu->wants_to_run) {
1143e3e880bbSZenghui Yu 		ret = -EINTR;
1144e3e880bbSZenghui Yu 		goto out;
1145e3e880bbSZenghui Yu 	}
1146e3e880bbSZenghui Yu 
11479ed24f4bSMarc Zyngier 	kvm_sigset_activate(vcpu);
11489ed24f4bSMarc Zyngier 
11499ed24f4bSMarc Zyngier 	ret = 1;
11509ed24f4bSMarc Zyngier 	run->exit_reason = KVM_EXIT_UNKNOWN;
115118f3976fSAlexandru Elisei 	run->flags = 0;
11529ed24f4bSMarc Zyngier 	while (ret > 0) {
11539ed24f4bSMarc Zyngier 		/*
11549ed24f4bSMarc Zyngier 		 * Check conditions before entering the guest
11559ed24f4bSMarc Zyngier 		 */
11566caa5812SOliver Upton 		ret = xfer_to_guest_mode_handle_work(vcpu);
11576caa5812SOliver Upton 		if (!ret)
11586caa5812SOliver Upton 			ret = 1;
11599ed24f4bSMarc Zyngier 
11603fdd0459SOliver Upton 		if (ret > 0)
11613fdd0459SOliver Upton 			ret = check_vcpu_requests(vcpu);
11629ed24f4bSMarc Zyngier 
11639ed24f4bSMarc Zyngier 		/*
11649ed24f4bSMarc Zyngier 		 * Preparing the interrupts to be injected also
11659ed24f4bSMarc Zyngier 		 * involves poking the GIC, which must be done in a
11669ed24f4bSMarc Zyngier 		 * non-preemptible context.
11679ed24f4bSMarc Zyngier 		 */
11689ed24f4bSMarc Zyngier 		preempt_disable();
11699ed24f4bSMarc Zyngier 
11703248136bSJulien Grall 		/*
11713248136bSJulien Grall 		 * The VMID allocator only tracks active VMIDs per
11723248136bSJulien Grall 		 * physical CPU, and therefore the VMID allocated may not be
11733248136bSJulien Grall 		 * preserved on VMID roll-over if the task was preempted,
11743248136bSJulien Grall 		 * making a thread's VMID inactive. So we need to call
11753248136bSJulien Grall 		 * kvm_arm_vmid_update() in non-premptible context.
11763248136bSJulien Grall 		 */
11775eba523eSMarc Zyngier 		if (kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid) &&
11785eba523eSMarc Zyngier 		    has_vhe())
11795eba523eSMarc Zyngier 			__load_stage2(vcpu->arch.hw_mmu,
11805eba523eSMarc Zyngier 				      vcpu->arch.hw_mmu->arch);
11813248136bSJulien Grall 
11829ed24f4bSMarc Zyngier 		kvm_pmu_flush_hwstate(vcpu);
11839ed24f4bSMarc Zyngier 
11849ed24f4bSMarc Zyngier 		local_irq_disable();
11859ed24f4bSMarc Zyngier 
11869ed24f4bSMarc Zyngier 		kvm_vgic_flush_hwstate(vcpu);
11879ed24f4bSMarc Zyngier 
118884d751a0SFuad Tabba 		kvm_pmu_update_vcpu_events(vcpu);
118984d751a0SFuad Tabba 
11909ed24f4bSMarc Zyngier 		/*
11919ed24f4bSMarc Zyngier 		 * Ensure we set mode to IN_GUEST_MODE after we disable
11929ed24f4bSMarc Zyngier 		 * interrupts and before the final VCPU requests check.
11939ed24f4bSMarc Zyngier 		 * See the comment in kvm_vcpu_exiting_guest_mode() and
11949ed24f4bSMarc Zyngier 		 * Documentation/virt/kvm/vcpu-requests.rst
11959ed24f4bSMarc Zyngier 		 */
11969ed24f4bSMarc Zyngier 		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
11979ed24f4bSMarc Zyngier 
11986caa5812SOliver Upton 		if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
11999ed24f4bSMarc Zyngier 			vcpu->mode = OUTSIDE_GUEST_MODE;
12009ed24f4bSMarc Zyngier 			isb(); /* Ensure work in x_flush_hwstate is committed */
12019ed24f4bSMarc Zyngier 			kvm_pmu_sync_hwstate(vcpu);
12029ed24f4bSMarc Zyngier 			if (static_branch_unlikely(&userspace_irqchip_in_use))
12033c5ff0c6SMarc Zyngier 				kvm_timer_sync_user(vcpu);
12049ed24f4bSMarc Zyngier 			kvm_vgic_sync_hwstate(vcpu);
12059ed24f4bSMarc Zyngier 			local_irq_enable();
12069ed24f4bSMarc Zyngier 			preempt_enable();
12079ed24f4bSMarc Zyngier 			continue;
12089ed24f4bSMarc Zyngier 		}
12099ed24f4bSMarc Zyngier 
12109ed24f4bSMarc Zyngier 		kvm_arm_setup_debug(vcpu);
1211af9a0e21SMarc Zyngier 		kvm_arch_vcpu_ctxflush_fp(vcpu);
12129ed24f4bSMarc Zyngier 
12139ed24f4bSMarc Zyngier 		/**************************************************************
12149ed24f4bSMarc Zyngier 		 * Enter the guest
12159ed24f4bSMarc Zyngier 		 */
12169ed24f4bSMarc Zyngier 		trace_kvm_entry(*vcpu_pc(vcpu));
12178cfe148aSMark Rutland 		guest_timing_enter_irqoff();
12189ed24f4bSMarc Zyngier 
12198cfe148aSMark Rutland 		ret = kvm_arm_vcpu_enter_exit(vcpu);
12209ed24f4bSMarc Zyngier 
12219ed24f4bSMarc Zyngier 		vcpu->mode = OUTSIDE_GUEST_MODE;
12229ed24f4bSMarc Zyngier 		vcpu->stat.exits++;
12239ed24f4bSMarc Zyngier 		/*
12249ed24f4bSMarc Zyngier 		 * Back from guest
12259ed24f4bSMarc Zyngier 		 *************************************************************/
12269ed24f4bSMarc Zyngier 
12279ed24f4bSMarc Zyngier 		kvm_arm_clear_debug(vcpu);
12289ed24f4bSMarc Zyngier 
12299ed24f4bSMarc Zyngier 		/*
12309ed24f4bSMarc Zyngier 		 * We must sync the PMU state before the vgic state so
12319ed24f4bSMarc Zyngier 		 * that the vgic can properly sample the updated state of the
12329ed24f4bSMarc Zyngier 		 * interrupt line.
12339ed24f4bSMarc Zyngier 		 */
12349ed24f4bSMarc Zyngier 		kvm_pmu_sync_hwstate(vcpu);
12359ed24f4bSMarc Zyngier 
12369ed24f4bSMarc Zyngier 		/*
12379ed24f4bSMarc Zyngier 		 * Sync the vgic state before syncing the timer state because
12389ed24f4bSMarc Zyngier 		 * the timer code needs to know if the virtual timer
12399ed24f4bSMarc Zyngier 		 * interrupts are active.
12409ed24f4bSMarc Zyngier 		 */
12419ed24f4bSMarc Zyngier 		kvm_vgic_sync_hwstate(vcpu);
12429ed24f4bSMarc Zyngier 
12439ed24f4bSMarc Zyngier 		/*
12449ed24f4bSMarc Zyngier 		 * Sync the timer hardware state before enabling interrupts as
12459ed24f4bSMarc Zyngier 		 * we don't want vtimer interrupts to race with syncing the
12469ed24f4bSMarc Zyngier 		 * timer virtual interrupt state.
12479ed24f4bSMarc Zyngier 		 */
12489ed24f4bSMarc Zyngier 		if (static_branch_unlikely(&userspace_irqchip_in_use))
12493c5ff0c6SMarc Zyngier 			kvm_timer_sync_user(vcpu);
12509ed24f4bSMarc Zyngier 
12519ed24f4bSMarc Zyngier 		kvm_arch_vcpu_ctxsync_fp(vcpu);
12529ed24f4bSMarc Zyngier 
12539ed24f4bSMarc Zyngier 		/*
12548cfe148aSMark Rutland 		 * We must ensure that any pending interrupts are taken before
12558cfe148aSMark Rutland 		 * we exit guest timing so that timer ticks are accounted as
12568cfe148aSMark Rutland 		 * guest time. Transiently unmask interrupts so that any
12578cfe148aSMark Rutland 		 * pending interrupts are taken.
12589ed24f4bSMarc Zyngier 		 *
12598cfe148aSMark Rutland 		 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
12608cfe148aSMark Rutland 		 * context synchronization event) is necessary to ensure that
12618cfe148aSMark Rutland 		 * pending interrupts are taken.
12629ed24f4bSMarc Zyngier 		 */
1263f7659f8bSMarc Zyngier 		if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
12649ed24f4bSMarc Zyngier 			local_irq_enable();
12658cfe148aSMark Rutland 			isb();
12668cfe148aSMark Rutland 			local_irq_disable();
1267f7659f8bSMarc Zyngier 		}
12689ed24f4bSMarc Zyngier 
12698cfe148aSMark Rutland 		guest_timing_exit_irqoff();
12708cfe148aSMark Rutland 
12718cfe148aSMark Rutland 		local_irq_enable();
12728cfe148aSMark Rutland 
12739ed24f4bSMarc Zyngier 		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
12749ed24f4bSMarc Zyngier 
12759ed24f4bSMarc Zyngier 		/* Exit types that need handling before we can be preempted */
127674cc7e0cSTianjia Zhang 		handle_exit_early(vcpu, ret);
12779ed24f4bSMarc Zyngier 
12789ed24f4bSMarc Zyngier 		preempt_enable();
12799ed24f4bSMarc Zyngier 
128022f55384SQais Yousef 		/*
128122f55384SQais Yousef 		 * The ARMv8 architecture doesn't give the hypervisor
128222f55384SQais Yousef 		 * a mechanism to prevent a guest from dropping to AArch32 EL0
128322f55384SQais Yousef 		 * if implemented by the CPU. If we spot the guest in such
128422f55384SQais Yousef 		 * state and that we decided it wasn't supposed to do so (like
128522f55384SQais Yousef 		 * with the asymmetric AArch32 case), return to userspace with
128622f55384SQais Yousef 		 * a fatal error.
128722f55384SQais Yousef 		 */
12882f6a49bbSWill Deacon 		if (vcpu_mode_is_bad_32bit(vcpu)) {
128922f55384SQais Yousef 			/*
129022f55384SQais Yousef 			 * As we have caught the guest red-handed, decide that
129122f55384SQais Yousef 			 * it isn't fit for purpose anymore by making the vcpu
129222f55384SQais Yousef 			 * invalid. The VMM can try and fix it by issuing  a
129322f55384SQais Yousef 			 * KVM_ARM_VCPU_INIT if it really wants to.
129422f55384SQais Yousef 			 */
1295ef984060SOliver Upton 			vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
129622f55384SQais Yousef 			ret = ARM_EXCEPTION_IL;
129722f55384SQais Yousef 		}
129822f55384SQais Yousef 
129974cc7e0cSTianjia Zhang 		ret = handle_exit(vcpu, ret);
13009ed24f4bSMarc Zyngier 	}
13019ed24f4bSMarc Zyngier 
13029ed24f4bSMarc Zyngier 	/* Tell userspace about in-kernel device output levels */
13039ed24f4bSMarc Zyngier 	if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
13049ed24f4bSMarc Zyngier 		kvm_timer_update_run(vcpu);
13059ed24f4bSMarc Zyngier 		kvm_pmu_update_run(vcpu);
13069ed24f4bSMarc Zyngier 	}
13079ed24f4bSMarc Zyngier 
13089ed24f4bSMarc Zyngier 	kvm_sigset_deactivate(vcpu);
13099ed24f4bSMarc Zyngier 
1310e3e880bbSZenghui Yu out:
131126778aaaSMarc Zyngier 	/*
131226778aaaSMarc Zyngier 	 * In the unlikely event that we are returning to userspace
131326778aaaSMarc Zyngier 	 * with pending exceptions or PC adjustment, commit these
131426778aaaSMarc Zyngier 	 * adjustments in order to give userspace a consistent view of
131526778aaaSMarc Zyngier 	 * the vcpu state. Note that this relies on __kvm_adjust_pc()
131626778aaaSMarc Zyngier 	 * being preempt-safe on VHE.
131726778aaaSMarc Zyngier 	 */
1318699bb2e0SMarc Zyngier 	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1319699bb2e0SMarc Zyngier 		     vcpu_get_flag(vcpu, INCREMENT_PC)))
132026778aaaSMarc Zyngier 		kvm_call_hyp(__kvm_adjust_pc, vcpu);
132126778aaaSMarc Zyngier 
13229ed24f4bSMarc Zyngier 	vcpu_put(vcpu);
13239ed24f4bSMarc Zyngier 	return ret;
13249ed24f4bSMarc Zyngier }
13259ed24f4bSMarc Zyngier 
13269ed24f4bSMarc Zyngier static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
13279ed24f4bSMarc Zyngier {
13289ed24f4bSMarc Zyngier 	int bit_index;
13299ed24f4bSMarc Zyngier 	bool set;
13309ed24f4bSMarc Zyngier 	unsigned long *hcr;
13319ed24f4bSMarc Zyngier 
13329ed24f4bSMarc Zyngier 	if (number == KVM_ARM_IRQ_CPU_IRQ)
13339ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VI);
13349ed24f4bSMarc Zyngier 	else /* KVM_ARM_IRQ_CPU_FIQ */
13359ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VF);
13369ed24f4bSMarc Zyngier 
13379ed24f4bSMarc Zyngier 	hcr = vcpu_hcr(vcpu);
13389ed24f4bSMarc Zyngier 	if (level)
13399ed24f4bSMarc Zyngier 		set = test_and_set_bit(bit_index, hcr);
13409ed24f4bSMarc Zyngier 	else
13419ed24f4bSMarc Zyngier 		set = test_and_clear_bit(bit_index, hcr);
13429ed24f4bSMarc Zyngier 
13439ed24f4bSMarc Zyngier 	/*
13449ed24f4bSMarc Zyngier 	 * If we didn't change anything, no need to wake up or kick other CPUs
13459ed24f4bSMarc Zyngier 	 */
13469ed24f4bSMarc Zyngier 	if (set == level)
13479ed24f4bSMarc Zyngier 		return 0;
13489ed24f4bSMarc Zyngier 
13499ed24f4bSMarc Zyngier 	/*
13509ed24f4bSMarc Zyngier 	 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
13519ed24f4bSMarc Zyngier 	 * trigger a world-switch round on the running physical CPU to set the
13529ed24f4bSMarc Zyngier 	 * virtual IRQ/FIQ fields in the HCR appropriately.
13539ed24f4bSMarc Zyngier 	 */
13549ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
13559ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
13569ed24f4bSMarc Zyngier 
13579ed24f4bSMarc Zyngier 	return 0;
13589ed24f4bSMarc Zyngier }
13599ed24f4bSMarc Zyngier 
13609ed24f4bSMarc Zyngier int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
13619ed24f4bSMarc Zyngier 			  bool line_status)
13629ed24f4bSMarc Zyngier {
13639ed24f4bSMarc Zyngier 	u32 irq = irq_level->irq;
13649a0a75d3SMarc Zyngier 	unsigned int irq_type, vcpu_id, irq_num;
13659ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = NULL;
13669ed24f4bSMarc Zyngier 	bool level = irq_level->level;
13679ed24f4bSMarc Zyngier 
13689ed24f4bSMarc Zyngier 	irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
13699a0a75d3SMarc Zyngier 	vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
13709a0a75d3SMarc Zyngier 	vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
13719ed24f4bSMarc Zyngier 	irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
13729ed24f4bSMarc Zyngier 
13739a0a75d3SMarc Zyngier 	trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level);
13749ed24f4bSMarc Zyngier 
13759ed24f4bSMarc Zyngier 	switch (irq_type) {
13769ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_CPU:
13779ed24f4bSMarc Zyngier 		if (irqchip_in_kernel(kvm))
13789ed24f4bSMarc Zyngier 			return -ENXIO;
13799ed24f4bSMarc Zyngier 
13809a0a75d3SMarc Zyngier 		vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
13819ed24f4bSMarc Zyngier 		if (!vcpu)
13829ed24f4bSMarc Zyngier 			return -EINVAL;
13839ed24f4bSMarc Zyngier 
13849ed24f4bSMarc Zyngier 		if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
13859ed24f4bSMarc Zyngier 			return -EINVAL;
13869ed24f4bSMarc Zyngier 
13879ed24f4bSMarc Zyngier 		return vcpu_interrupt_line(vcpu, irq_num, level);
13889ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_PPI:
13899ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
13909ed24f4bSMarc Zyngier 			return -ENXIO;
13919ed24f4bSMarc Zyngier 
13929a0a75d3SMarc Zyngier 		vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
13939ed24f4bSMarc Zyngier 		if (!vcpu)
13949ed24f4bSMarc Zyngier 			return -EINVAL;
13959ed24f4bSMarc Zyngier 
13969ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
13979ed24f4bSMarc Zyngier 			return -EINVAL;
13989ed24f4bSMarc Zyngier 
13999a0a75d3SMarc Zyngier 		return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL);
14009ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_SPI:
14019ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
14029ed24f4bSMarc Zyngier 			return -ENXIO;
14039ed24f4bSMarc Zyngier 
14049ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_PRIVATE_IRQS)
14059ed24f4bSMarc Zyngier 			return -EINVAL;
14069ed24f4bSMarc Zyngier 
14079a0a75d3SMarc Zyngier 		return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL);
14089ed24f4bSMarc Zyngier 	}
14099ed24f4bSMarc Zyngier 
14109ed24f4bSMarc Zyngier 	return -EINVAL;
14119ed24f4bSMarc Zyngier }
14129ed24f4bSMarc Zyngier 
1413ef150908SOliver Upton static unsigned long system_supported_vcpu_features(void)
1414ef150908SOliver Upton {
1415ef150908SOliver Upton 	unsigned long features = KVM_VCPU_VALID_FEATURES;
1416ef150908SOliver Upton 
1417ef150908SOliver Upton 	if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1))
1418ef150908SOliver Upton 		clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features);
1419ef150908SOliver Upton 
14209116db11SOliver Upton 	if (!kvm_arm_support_pmu_v3())
14219116db11SOliver Upton 		clear_bit(KVM_ARM_VCPU_PMU_V3, &features);
14229116db11SOliver Upton 
1423be9c0c01SOliver Upton 	if (!system_supports_sve())
1424be9c0c01SOliver Upton 		clear_bit(KVM_ARM_VCPU_SVE, &features);
1425be9c0c01SOliver Upton 
1426a07e9345SMarc Zyngier 	if (!kvm_has_full_ptr_auth()) {
1427baa28a53SOliver Upton 		clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features);
1428baa28a53SOliver Upton 		clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features);
1429baa28a53SOliver Upton 	}
1430baa28a53SOliver Upton 
143112405b09SOliver Upton 	if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
143212405b09SOliver Upton 		clear_bit(KVM_ARM_VCPU_HAS_EL2, &features);
143312405b09SOliver Upton 
1434ef150908SOliver Upton 	return features;
1435ef150908SOliver Upton }
1436ef150908SOliver Upton 
1437a7a2c72aSOliver Upton static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
14389ed24f4bSMarc Zyngier 					const struct kvm_vcpu_init *init)
14399ed24f4bSMarc Zyngier {
1440a7a2c72aSOliver Upton 	unsigned long features = init->features[0];
1441a7a2c72aSOliver Upton 	int i;
14429ed24f4bSMarc Zyngier 
1443a7a2c72aSOliver Upton 	if (features & ~KVM_VCPU_VALID_FEATURES)
14449ed24f4bSMarc Zyngier 		return -ENOENT;
14459ed24f4bSMarc Zyngier 
1446a7a2c72aSOliver Upton 	for (i = 1; i < ARRAY_SIZE(init->features); i++) {
1447a7a2c72aSOliver Upton 		if (init->features[i])
1448a7a2c72aSOliver Upton 			return -ENOENT;
14499ed24f4bSMarc Zyngier 	}
14509ed24f4bSMarc Zyngier 
1451ef150908SOliver Upton 	if (features & ~system_supported_vcpu_features())
1452ef150908SOliver Upton 		return -EINVAL;
1453ef150908SOliver Upton 
1454baa28a53SOliver Upton 	/*
1455baa28a53SOliver Upton 	 * For now make sure that both address/generic pointer authentication
1456baa28a53SOliver Upton 	 * features are requested by the userspace together.
1457baa28a53SOliver Upton 	 */
1458baa28a53SOliver Upton 	if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) !=
1459baa28a53SOliver Upton 	    test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features))
1460baa28a53SOliver Upton 		return -EINVAL;
1461baa28a53SOliver Upton 
14622251e9ffSOliver Upton 	if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features))
14632251e9ffSOliver Upton 		return 0;
14642251e9ffSOliver Upton 
14652251e9ffSOliver Upton 	/* MTE is incompatible with AArch32 */
14662251e9ffSOliver Upton 	if (kvm_has_mte(vcpu->kvm))
14672251e9ffSOliver Upton 		return -EINVAL;
14682251e9ffSOliver Upton 
14692251e9ffSOliver Upton 	/* NV is incompatible with AArch32 */
14702251e9ffSOliver Upton 	if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
14712251e9ffSOliver Upton 		return -EINVAL;
14722251e9ffSOliver Upton 
1473a7a2c72aSOliver Upton 	return 0;
1474a7a2c72aSOliver Upton }
1475a7a2c72aSOliver Upton 
1476a7a2c72aSOliver Upton static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
1477a7a2c72aSOliver Upton 				  const struct kvm_vcpu_init *init)
1478a7a2c72aSOliver Upton {
1479a7a2c72aSOliver Upton 	unsigned long features = init->features[0];
1480a7a2c72aSOliver Upton 
14811de10b7dSOliver Upton 	return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features,
14821de10b7dSOliver Upton 			     KVM_VCPU_MAX_FEATURES);
1483a7a2c72aSOliver Upton }
1484a7a2c72aSOliver Upton 
148542773357SReiji Watanabe static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
148642773357SReiji Watanabe {
148742773357SReiji Watanabe 	struct kvm *kvm = vcpu->kvm;
148842773357SReiji Watanabe 	int ret = 0;
148942773357SReiji Watanabe 
149042773357SReiji Watanabe 	/*
149142773357SReiji Watanabe 	 * When the vCPU has a PMU, but no PMU is set for the guest
149242773357SReiji Watanabe 	 * yet, set the default one.
149342773357SReiji Watanabe 	 */
149442773357SReiji Watanabe 	if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu)
149542773357SReiji Watanabe 		ret = kvm_arm_set_default_pmu(kvm);
149642773357SReiji Watanabe 
14974f128f8eSMarc Zyngier 	/* Prepare for nested if required */
14984f128f8eSMarc Zyngier 	if (!ret && vcpu_has_nv(vcpu))
14994f128f8eSMarc Zyngier 		ret = kvm_vcpu_init_nested(vcpu);
15004f128f8eSMarc Zyngier 
150142773357SReiji Watanabe 	return ret;
1502a7a2c72aSOliver Upton }
1503a7a2c72aSOliver Upton 
1504a7a2c72aSOliver Upton static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1505a7a2c72aSOliver Upton 				 const struct kvm_vcpu_init *init)
1506a7a2c72aSOliver Upton {
1507a7a2c72aSOliver Upton 	unsigned long features = init->features[0];
15082251e9ffSOliver Upton 	struct kvm *kvm = vcpu->kvm;
15092251e9ffSOliver Upton 	int ret = -EINVAL;
15102251e9ffSOliver Upton 
15112251e9ffSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
15122251e9ffSOliver Upton 
15132251e9ffSOliver Upton 	if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
15141de10b7dSOliver Upton 	    kvm_vcpu_init_changed(vcpu, init))
15152251e9ffSOliver Upton 		goto out_unlock;
1516a7a2c72aSOliver Upton 
15172251e9ffSOliver Upton 	bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
15189ed24f4bSMarc Zyngier 
151942773357SReiji Watanabe 	ret = kvm_setup_vcpu(vcpu);
152042773357SReiji Watanabe 	if (ret)
152142773357SReiji Watanabe 		goto out_unlock;
152242773357SReiji Watanabe 
15239ed24f4bSMarc Zyngier 	/* Now we know what it is, we can reset it. */
15243d4b2a4cSOliver Upton 	kvm_reset_vcpu(vcpu);
15259ed24f4bSMarc Zyngier 
15262251e9ffSOliver Upton 	set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
1527ef984060SOliver Upton 	vcpu_set_flag(vcpu, VCPU_INITIALIZED);
15283d4b2a4cSOliver Upton 	ret = 0;
15292251e9ffSOliver Upton out_unlock:
15302251e9ffSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
15319ed24f4bSMarc Zyngier 	return ret;
15329ed24f4bSMarc Zyngier }
15339ed24f4bSMarc Zyngier 
1534a7a2c72aSOliver Upton static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1535a7a2c72aSOliver Upton 			       const struct kvm_vcpu_init *init)
1536a7a2c72aSOliver Upton {
1537a7a2c72aSOliver Upton 	int ret;
1538a7a2c72aSOliver Upton 
15395346f7e1SOliver Upton 	if (init->target != KVM_ARM_TARGET_GENERIC_V8 &&
15405346f7e1SOliver Upton 	    init->target != kvm_target_cpu())
1541a7a2c72aSOliver Upton 		return -EINVAL;
1542a7a2c72aSOliver Upton 
1543a7a2c72aSOliver Upton 	ret = kvm_vcpu_init_check_features(vcpu, init);
1544a7a2c72aSOliver Upton 	if (ret)
1545a7a2c72aSOliver Upton 		return ret;
1546a7a2c72aSOliver Upton 
1547ef984060SOliver Upton 	if (!kvm_vcpu_initialized(vcpu))
1548a7a2c72aSOliver Upton 		return __kvm_vcpu_set_target(vcpu, init);
1549a7a2c72aSOliver Upton 
1550a7a2c72aSOliver Upton 	if (kvm_vcpu_init_changed(vcpu, init))
1551a7a2c72aSOliver Upton 		return -EINVAL;
1552a7a2c72aSOliver Upton 
15533d4b2a4cSOliver Upton 	kvm_reset_vcpu(vcpu);
15543d4b2a4cSOliver Upton 	return 0;
1555a7a2c72aSOliver Upton }
1556a7a2c72aSOliver Upton 
15579ed24f4bSMarc Zyngier static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
15589ed24f4bSMarc Zyngier 					 struct kvm_vcpu_init *init)
15599ed24f4bSMarc Zyngier {
1560e3c1c0caSOliver Upton 	bool power_off = false;
15619ed24f4bSMarc Zyngier 	int ret;
15629ed24f4bSMarc Zyngier 
1563e3c1c0caSOliver Upton 	/*
1564e3c1c0caSOliver Upton 	 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid
1565e3c1c0caSOliver Upton 	 * reflecting it in the finalized feature set, thus limiting its scope
1566e3c1c0caSOliver Upton 	 * to a single KVM_ARM_VCPU_INIT call.
1567e3c1c0caSOliver Upton 	 */
1568192df2aaSOliver Upton 	if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
1569192df2aaSOliver Upton 		init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
1570e3c1c0caSOliver Upton 		power_off = true;
1571e3c1c0caSOliver Upton 	}
1572e3c1c0caSOliver Upton 
15739ed24f4bSMarc Zyngier 	ret = kvm_vcpu_set_target(vcpu, init);
15749ed24f4bSMarc Zyngier 	if (ret)
15759ed24f4bSMarc Zyngier 		return ret;
15769ed24f4bSMarc Zyngier 
15779ed24f4bSMarc Zyngier 	/*
15789ed24f4bSMarc Zyngier 	 * Ensure a rebooted VM will fault in RAM pages and detect if the
15799ed24f4bSMarc Zyngier 	 * guest MMU is turned off and flush the caches as needed.
1580892713e9SZenghui Yu 	 *
15817ae2f3dbSMarc Zyngier 	 * S2FWB enforces all memory accesses to RAM being cacheable,
15827ae2f3dbSMarc Zyngier 	 * ensuring that the data side is always coherent. We still
15837ae2f3dbSMarc Zyngier 	 * need to invalidate the I-cache though, as FWB does *not*
15847ae2f3dbSMarc Zyngier 	 * imply CTR_EL0.DIC.
15859ed24f4bSMarc Zyngier 	 */
1586cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu)) {
15877ae2f3dbSMarc Zyngier 		if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
15889ed24f4bSMarc Zyngier 			stage2_unmap_vm(vcpu->kvm);
15897ae2f3dbSMarc Zyngier 		else
1590fade9c2cSFuad Tabba 			icache_inval_all_pou();
15917ae2f3dbSMarc Zyngier 	}
15929ed24f4bSMarc Zyngier 
15939ed24f4bSMarc Zyngier 	vcpu_reset_hcr(vcpu);
159475c76ab5SMarc Zyngier 	vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu);
15959ed24f4bSMarc Zyngier 
15969ed24f4bSMarc Zyngier 	/*
15979ed24f4bSMarc Zyngier 	 * Handle the "start in power-off" case.
15989ed24f4bSMarc Zyngier 	 */
15994ff910beSReiji Watanabe 	spin_lock(&vcpu->arch.mp_state_lock);
16004ff910beSReiji Watanabe 
1601e3c1c0caSOliver Upton 	if (power_off)
16024ff910beSReiji Watanabe 		__kvm_arm_vcpu_power_off(vcpu);
16039ed24f4bSMarc Zyngier 	else
16040acc7239SOliver Upton 		WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
16059ed24f4bSMarc Zyngier 
16064ff910beSReiji Watanabe 	spin_unlock(&vcpu->arch.mp_state_lock);
16079ed24f4bSMarc Zyngier 
16089ed24f4bSMarc Zyngier 	return 0;
16099ed24f4bSMarc Zyngier }
16109ed24f4bSMarc Zyngier 
16119ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
16129ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
16139ed24f4bSMarc Zyngier {
16149ed24f4bSMarc Zyngier 	int ret = -ENXIO;
16159ed24f4bSMarc Zyngier 
16169ed24f4bSMarc Zyngier 	switch (attr->group) {
16179ed24f4bSMarc Zyngier 	default:
16189ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
16199ed24f4bSMarc Zyngier 		break;
16209ed24f4bSMarc Zyngier 	}
16219ed24f4bSMarc Zyngier 
16229ed24f4bSMarc Zyngier 	return ret;
16239ed24f4bSMarc Zyngier }
16249ed24f4bSMarc Zyngier 
16259ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
16269ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
16279ed24f4bSMarc Zyngier {
16289ed24f4bSMarc Zyngier 	int ret = -ENXIO;
16299ed24f4bSMarc Zyngier 
16309ed24f4bSMarc Zyngier 	switch (attr->group) {
16319ed24f4bSMarc Zyngier 	default:
16329ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
16339ed24f4bSMarc Zyngier 		break;
16349ed24f4bSMarc Zyngier 	}
16359ed24f4bSMarc Zyngier 
16369ed24f4bSMarc Zyngier 	return ret;
16379ed24f4bSMarc Zyngier }
16389ed24f4bSMarc Zyngier 
16399ed24f4bSMarc Zyngier static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
16409ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
16419ed24f4bSMarc Zyngier {
16429ed24f4bSMarc Zyngier 	int ret = -ENXIO;
16439ed24f4bSMarc Zyngier 
16449ed24f4bSMarc Zyngier 	switch (attr->group) {
16459ed24f4bSMarc Zyngier 	default:
16469ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
16479ed24f4bSMarc Zyngier 		break;
16489ed24f4bSMarc Zyngier 	}
16499ed24f4bSMarc Zyngier 
16509ed24f4bSMarc Zyngier 	return ret;
16519ed24f4bSMarc Zyngier }
16529ed24f4bSMarc Zyngier 
16539ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
16549ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
16559ed24f4bSMarc Zyngier {
16569ed24f4bSMarc Zyngier 	memset(events, 0, sizeof(*events));
16579ed24f4bSMarc Zyngier 
16589ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_get_events(vcpu, events);
16599ed24f4bSMarc Zyngier }
16609ed24f4bSMarc Zyngier 
16619ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
16629ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
16639ed24f4bSMarc Zyngier {
16649ed24f4bSMarc Zyngier 	int i;
16659ed24f4bSMarc Zyngier 
16669ed24f4bSMarc Zyngier 	/* check whether the reserved field is zero */
16679ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
16689ed24f4bSMarc Zyngier 		if (events->reserved[i])
16699ed24f4bSMarc Zyngier 			return -EINVAL;
16709ed24f4bSMarc Zyngier 
16719ed24f4bSMarc Zyngier 	/* check whether the pad field is zero */
16729ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
16739ed24f4bSMarc Zyngier 		if (events->exception.pad[i])
16749ed24f4bSMarc Zyngier 			return -EINVAL;
16759ed24f4bSMarc Zyngier 
16769ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_set_events(vcpu, events);
16779ed24f4bSMarc Zyngier }
16789ed24f4bSMarc Zyngier 
16799ed24f4bSMarc Zyngier long kvm_arch_vcpu_ioctl(struct file *filp,
16809ed24f4bSMarc Zyngier 			 unsigned int ioctl, unsigned long arg)
16819ed24f4bSMarc Zyngier {
16829ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = filp->private_data;
16839ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
16849ed24f4bSMarc Zyngier 	struct kvm_device_attr attr;
16859ed24f4bSMarc Zyngier 	long r;
16869ed24f4bSMarc Zyngier 
16879ed24f4bSMarc Zyngier 	switch (ioctl) {
16889ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_INIT: {
16899ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
16909ed24f4bSMarc Zyngier 
16919ed24f4bSMarc Zyngier 		r = -EFAULT;
16929ed24f4bSMarc Zyngier 		if (copy_from_user(&init, argp, sizeof(init)))
16939ed24f4bSMarc Zyngier 			break;
16949ed24f4bSMarc Zyngier 
16959ed24f4bSMarc Zyngier 		r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
16969ed24f4bSMarc Zyngier 		break;
16979ed24f4bSMarc Zyngier 	}
16989ed24f4bSMarc Zyngier 	case KVM_SET_ONE_REG:
16999ed24f4bSMarc Zyngier 	case KVM_GET_ONE_REG: {
17009ed24f4bSMarc Zyngier 		struct kvm_one_reg reg;
17019ed24f4bSMarc Zyngier 
17029ed24f4bSMarc Zyngier 		r = -ENOEXEC;
17039ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
17049ed24f4bSMarc Zyngier 			break;
17059ed24f4bSMarc Zyngier 
17069ed24f4bSMarc Zyngier 		r = -EFAULT;
17079ed24f4bSMarc Zyngier 		if (copy_from_user(&reg, argp, sizeof(reg)))
17089ed24f4bSMarc Zyngier 			break;
17099ed24f4bSMarc Zyngier 
17106826c684SOliver Upton 		/*
17116826c684SOliver Upton 		 * We could owe a reset due to PSCI. Handle the pending reset
17126826c684SOliver Upton 		 * here to ensure userspace register accesses are ordered after
17136826c684SOliver Upton 		 * the reset.
17146826c684SOliver Upton 		 */
17156826c684SOliver Upton 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
17166826c684SOliver Upton 			kvm_reset_vcpu(vcpu);
17176826c684SOliver Upton 
17189ed24f4bSMarc Zyngier 		if (ioctl == KVM_SET_ONE_REG)
17199ed24f4bSMarc Zyngier 			r = kvm_arm_set_reg(vcpu, &reg);
17209ed24f4bSMarc Zyngier 		else
17219ed24f4bSMarc Zyngier 			r = kvm_arm_get_reg(vcpu, &reg);
17229ed24f4bSMarc Zyngier 		break;
17239ed24f4bSMarc Zyngier 	}
17249ed24f4bSMarc Zyngier 	case KVM_GET_REG_LIST: {
17259ed24f4bSMarc Zyngier 		struct kvm_reg_list __user *user_list = argp;
17269ed24f4bSMarc Zyngier 		struct kvm_reg_list reg_list;
17279ed24f4bSMarc Zyngier 		unsigned n;
17289ed24f4bSMarc Zyngier 
17299ed24f4bSMarc Zyngier 		r = -ENOEXEC;
17309ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
17319ed24f4bSMarc Zyngier 			break;
17329ed24f4bSMarc Zyngier 
17339ed24f4bSMarc Zyngier 		r = -EPERM;
17349ed24f4bSMarc Zyngier 		if (!kvm_arm_vcpu_is_finalized(vcpu))
17359ed24f4bSMarc Zyngier 			break;
17369ed24f4bSMarc Zyngier 
17379ed24f4bSMarc Zyngier 		r = -EFAULT;
17389ed24f4bSMarc Zyngier 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
17399ed24f4bSMarc Zyngier 			break;
17409ed24f4bSMarc Zyngier 		n = reg_list.n;
17419ed24f4bSMarc Zyngier 		reg_list.n = kvm_arm_num_regs(vcpu);
17429ed24f4bSMarc Zyngier 		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
17439ed24f4bSMarc Zyngier 			break;
17449ed24f4bSMarc Zyngier 		r = -E2BIG;
17459ed24f4bSMarc Zyngier 		if (n < reg_list.n)
17469ed24f4bSMarc Zyngier 			break;
17479ed24f4bSMarc Zyngier 		r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
17489ed24f4bSMarc Zyngier 		break;
17499ed24f4bSMarc Zyngier 	}
17509ed24f4bSMarc Zyngier 	case KVM_SET_DEVICE_ATTR: {
17519ed24f4bSMarc Zyngier 		r = -EFAULT;
17529ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
17539ed24f4bSMarc Zyngier 			break;
17549ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_set_attr(vcpu, &attr);
17559ed24f4bSMarc Zyngier 		break;
17569ed24f4bSMarc Zyngier 	}
17579ed24f4bSMarc Zyngier 	case KVM_GET_DEVICE_ATTR: {
17589ed24f4bSMarc Zyngier 		r = -EFAULT;
17599ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
17609ed24f4bSMarc Zyngier 			break;
17619ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_get_attr(vcpu, &attr);
17629ed24f4bSMarc Zyngier 		break;
17639ed24f4bSMarc Zyngier 	}
17649ed24f4bSMarc Zyngier 	case KVM_HAS_DEVICE_ATTR: {
17659ed24f4bSMarc Zyngier 		r = -EFAULT;
17669ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
17679ed24f4bSMarc Zyngier 			break;
17689ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_has_attr(vcpu, &attr);
17699ed24f4bSMarc Zyngier 		break;
17709ed24f4bSMarc Zyngier 	}
17719ed24f4bSMarc Zyngier 	case KVM_GET_VCPU_EVENTS: {
17729ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
17739ed24f4bSMarc Zyngier 
17749ed24f4bSMarc Zyngier 		if (kvm_arm_vcpu_get_events(vcpu, &events))
17759ed24f4bSMarc Zyngier 			return -EINVAL;
17769ed24f4bSMarc Zyngier 
17779ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &events, sizeof(events)))
17789ed24f4bSMarc Zyngier 			return -EFAULT;
17799ed24f4bSMarc Zyngier 
17809ed24f4bSMarc Zyngier 		return 0;
17819ed24f4bSMarc Zyngier 	}
17829ed24f4bSMarc Zyngier 	case KVM_SET_VCPU_EVENTS: {
17839ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
17849ed24f4bSMarc Zyngier 
17859ed24f4bSMarc Zyngier 		if (copy_from_user(&events, argp, sizeof(events)))
17869ed24f4bSMarc Zyngier 			return -EFAULT;
17879ed24f4bSMarc Zyngier 
17889ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_set_events(vcpu, &events);
17899ed24f4bSMarc Zyngier 	}
17909ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_FINALIZE: {
17919ed24f4bSMarc Zyngier 		int what;
17929ed24f4bSMarc Zyngier 
17939ed24f4bSMarc Zyngier 		if (!kvm_vcpu_initialized(vcpu))
17949ed24f4bSMarc Zyngier 			return -ENOEXEC;
17959ed24f4bSMarc Zyngier 
17969ed24f4bSMarc Zyngier 		if (get_user(what, (const int __user *)argp))
17979ed24f4bSMarc Zyngier 			return -EFAULT;
17989ed24f4bSMarc Zyngier 
17999ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_finalize(vcpu, what);
18009ed24f4bSMarc Zyngier 	}
18019ed24f4bSMarc Zyngier 	default:
18029ed24f4bSMarc Zyngier 		r = -EINVAL;
18039ed24f4bSMarc Zyngier 	}
18049ed24f4bSMarc Zyngier 
18059ed24f4bSMarc Zyngier 	return r;
18069ed24f4bSMarc Zyngier }
18079ed24f4bSMarc Zyngier 
18089ed24f4bSMarc Zyngier void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
18099ed24f4bSMarc Zyngier {
18109ed24f4bSMarc Zyngier 
18119ed24f4bSMarc Zyngier }
18129ed24f4bSMarc Zyngier 
18139ed24f4bSMarc Zyngier static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
18149ed24f4bSMarc Zyngier 					struct kvm_arm_device_addr *dev_addr)
18159ed24f4bSMarc Zyngier {
18169f968c92SMarc Zyngier 	switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
18179ed24f4bSMarc Zyngier 	case KVM_ARM_DEVICE_VGIC_V2:
18189ed24f4bSMarc Zyngier 		if (!vgic_present)
18199ed24f4bSMarc Zyngier 			return -ENXIO;
18209f968c92SMarc Zyngier 		return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
18219ed24f4bSMarc Zyngier 	default:
18229ed24f4bSMarc Zyngier 		return -ENODEV;
18239ed24f4bSMarc Zyngier 	}
18249ed24f4bSMarc Zyngier }
18259ed24f4bSMarc Zyngier 
1826e0fc6b21SOliver Upton static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1827e0fc6b21SOliver Upton {
1828e0fc6b21SOliver Upton 	switch (attr->group) {
1829821d935cSOliver Upton 	case KVM_ARM_VM_SMCCC_CTRL:
1830821d935cSOliver Upton 		return kvm_vm_smccc_has_attr(kvm, attr);
1831e0fc6b21SOliver Upton 	default:
1832e0fc6b21SOliver Upton 		return -ENXIO;
1833e0fc6b21SOliver Upton 	}
1834e0fc6b21SOliver Upton }
1835e0fc6b21SOliver Upton 
1836e0fc6b21SOliver Upton static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1837e0fc6b21SOliver Upton {
1838e0fc6b21SOliver Upton 	switch (attr->group) {
1839821d935cSOliver Upton 	case KVM_ARM_VM_SMCCC_CTRL:
1840821d935cSOliver Upton 		return kvm_vm_smccc_set_attr(kvm, attr);
1841e0fc6b21SOliver Upton 	default:
1842e0fc6b21SOliver Upton 		return -ENXIO;
1843e0fc6b21SOliver Upton 	}
1844e0fc6b21SOliver Upton }
1845e0fc6b21SOliver Upton 
1846d8708b80SThomas Huth int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
18479ed24f4bSMarc Zyngier {
18489ed24f4bSMarc Zyngier 	struct kvm *kvm = filp->private_data;
18499ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
1850e0fc6b21SOliver Upton 	struct kvm_device_attr attr;
18519ed24f4bSMarc Zyngier 
18529ed24f4bSMarc Zyngier 	switch (ioctl) {
18539ed24f4bSMarc Zyngier 	case KVM_CREATE_IRQCHIP: {
18549ed24f4bSMarc Zyngier 		int ret;
18559ed24f4bSMarc Zyngier 		if (!vgic_present)
18569ed24f4bSMarc Zyngier 			return -ENXIO;
18579ed24f4bSMarc Zyngier 		mutex_lock(&kvm->lock);
18589ed24f4bSMarc Zyngier 		ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
18599ed24f4bSMarc Zyngier 		mutex_unlock(&kvm->lock);
18609ed24f4bSMarc Zyngier 		return ret;
18619ed24f4bSMarc Zyngier 	}
18629ed24f4bSMarc Zyngier 	case KVM_ARM_SET_DEVICE_ADDR: {
18639ed24f4bSMarc Zyngier 		struct kvm_arm_device_addr dev_addr;
18649ed24f4bSMarc Zyngier 
18659ed24f4bSMarc Zyngier 		if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
18669ed24f4bSMarc Zyngier 			return -EFAULT;
18679ed24f4bSMarc Zyngier 		return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
18689ed24f4bSMarc Zyngier 	}
18699ed24f4bSMarc Zyngier 	case KVM_ARM_PREFERRED_TARGET: {
18705346f7e1SOliver Upton 		struct kvm_vcpu_init init = {
18715346f7e1SOliver Upton 			.target = KVM_ARM_TARGET_GENERIC_V8,
18725346f7e1SOliver Upton 		};
18739ed24f4bSMarc Zyngier 
18749ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &init, sizeof(init)))
18759ed24f4bSMarc Zyngier 			return -EFAULT;
18769ed24f4bSMarc Zyngier 
18779ed24f4bSMarc Zyngier 		return 0;
18789ed24f4bSMarc Zyngier 	}
1879f0376edbSSteven Price 	case KVM_ARM_MTE_COPY_TAGS: {
1880f0376edbSSteven Price 		struct kvm_arm_copy_mte_tags copy_tags;
1881f0376edbSSteven Price 
1882f0376edbSSteven Price 		if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
1883f0376edbSSteven Price 			return -EFAULT;
1884f0376edbSSteven Price 		return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
1885f0376edbSSteven Price 	}
188630ec7997SMarc Zyngier 	case KVM_ARM_SET_COUNTER_OFFSET: {
188730ec7997SMarc Zyngier 		struct kvm_arm_counter_offset offset;
188830ec7997SMarc Zyngier 
188930ec7997SMarc Zyngier 		if (copy_from_user(&offset, argp, sizeof(offset)))
189030ec7997SMarc Zyngier 			return -EFAULT;
189130ec7997SMarc Zyngier 		return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
189230ec7997SMarc Zyngier 	}
1893e0fc6b21SOliver Upton 	case KVM_HAS_DEVICE_ATTR: {
1894e0fc6b21SOliver Upton 		if (copy_from_user(&attr, argp, sizeof(attr)))
1895e0fc6b21SOliver Upton 			return -EFAULT;
1896e0fc6b21SOliver Upton 
1897e0fc6b21SOliver Upton 		return kvm_vm_has_attr(kvm, &attr);
1898e0fc6b21SOliver Upton 	}
1899e0fc6b21SOliver Upton 	case KVM_SET_DEVICE_ATTR: {
1900e0fc6b21SOliver Upton 		if (copy_from_user(&attr, argp, sizeof(attr)))
1901e0fc6b21SOliver Upton 			return -EFAULT;
1902e0fc6b21SOliver Upton 
1903e0fc6b21SOliver Upton 		return kvm_vm_set_attr(kvm, &attr);
1904e0fc6b21SOliver Upton 	}
19053f9cd0caSJing Zhang 	case KVM_ARM_GET_REG_WRITABLE_MASKS: {
19063f9cd0caSJing Zhang 		struct reg_mask_range range;
19073f9cd0caSJing Zhang 
19083f9cd0caSJing Zhang 		if (copy_from_user(&range, argp, sizeof(range)))
19093f9cd0caSJing Zhang 			return -EFAULT;
19103f9cd0caSJing Zhang 		return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range);
19113f9cd0caSJing Zhang 	}
19129ed24f4bSMarc Zyngier 	default:
19139ed24f4bSMarc Zyngier 		return -EINVAL;
19149ed24f4bSMarc Zyngier 	}
19159ed24f4bSMarc Zyngier }
19169ed24f4bSMarc Zyngier 
191796906a91SMarc Zyngier /* unlocks vcpus from @vcpu_lock_idx and smaller */
191896906a91SMarc Zyngier static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx)
191996906a91SMarc Zyngier {
192096906a91SMarc Zyngier 	struct kvm_vcpu *tmp_vcpu;
192196906a91SMarc Zyngier 
192296906a91SMarc Zyngier 	for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
192396906a91SMarc Zyngier 		tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
192496906a91SMarc Zyngier 		mutex_unlock(&tmp_vcpu->mutex);
192596906a91SMarc Zyngier 	}
192696906a91SMarc Zyngier }
192796906a91SMarc Zyngier 
192896906a91SMarc Zyngier void unlock_all_vcpus(struct kvm *kvm)
192996906a91SMarc Zyngier {
193096906a91SMarc Zyngier 	lockdep_assert_held(&kvm->lock);
193196906a91SMarc Zyngier 
193296906a91SMarc Zyngier 	unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1);
193396906a91SMarc Zyngier }
193496906a91SMarc Zyngier 
193596906a91SMarc Zyngier /* Returns true if all vcpus were locked, false otherwise */
193696906a91SMarc Zyngier bool lock_all_vcpus(struct kvm *kvm)
193796906a91SMarc Zyngier {
193896906a91SMarc Zyngier 	struct kvm_vcpu *tmp_vcpu;
193996906a91SMarc Zyngier 	unsigned long c;
194096906a91SMarc Zyngier 
194196906a91SMarc Zyngier 	lockdep_assert_held(&kvm->lock);
194296906a91SMarc Zyngier 
194396906a91SMarc Zyngier 	/*
194496906a91SMarc Zyngier 	 * Any time a vcpu is in an ioctl (including running), the
194596906a91SMarc Zyngier 	 * core KVM code tries to grab the vcpu->mutex.
194696906a91SMarc Zyngier 	 *
194796906a91SMarc Zyngier 	 * By grabbing the vcpu->mutex of all VCPUs we ensure that no
194896906a91SMarc Zyngier 	 * other VCPUs can fiddle with the state while we access it.
194996906a91SMarc Zyngier 	 */
195096906a91SMarc Zyngier 	kvm_for_each_vcpu(c, tmp_vcpu, kvm) {
195196906a91SMarc Zyngier 		if (!mutex_trylock(&tmp_vcpu->mutex)) {
195296906a91SMarc Zyngier 			unlock_vcpus(kvm, c - 1);
195396906a91SMarc Zyngier 			return false;
195496906a91SMarc Zyngier 		}
195596906a91SMarc Zyngier 	}
195696906a91SMarc Zyngier 
195796906a91SMarc Zyngier 	return true;
195896906a91SMarc Zyngier }
195996906a91SMarc Zyngier 
196030c95391SDavid Brazdil static unsigned long nvhe_percpu_size(void)
196130c95391SDavid Brazdil {
196230c95391SDavid Brazdil 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
196330c95391SDavid Brazdil 		(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
196430c95391SDavid Brazdil }
196530c95391SDavid Brazdil 
196630c95391SDavid Brazdil static unsigned long nvhe_percpu_order(void)
196730c95391SDavid Brazdil {
196830c95391SDavid Brazdil 	unsigned long size = nvhe_percpu_size();
196930c95391SDavid Brazdil 
197030c95391SDavid Brazdil 	return size ? get_order(size) : 0;
197130c95391SDavid Brazdil }
197230c95391SDavid Brazdil 
197366d5b53eSFuad Tabba static size_t pkvm_host_sve_state_order(void)
197466d5b53eSFuad Tabba {
197566d5b53eSFuad Tabba 	return get_order(pkvm_host_sve_state_size());
197666d5b53eSFuad Tabba }
197766d5b53eSFuad Tabba 
1978b881cdceSWill Deacon /* A lookup table holding the hypervisor VA for each vector slot */
1979b881cdceSWill Deacon static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1980de5bcdb4SWill Deacon 
1981b881cdceSWill Deacon static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1982b881cdceSWill Deacon {
1983bc1d2892SQuentin Perret 	hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
19849ef2b48bSWill Deacon }
19859ef2b48bSWill Deacon 
1986b881cdceSWill Deacon static int kvm_init_vector_slots(void)
1987b881cdceSWill Deacon {
1988b881cdceSWill Deacon 	int err;
1989b881cdceSWill Deacon 	void *base;
1990b881cdceSWill Deacon 
1991b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1992b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1993b881cdceSWill Deacon 
1994b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1995b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1996b881cdceSWill Deacon 
19972e403167SQuentin Perret 	if (kvm_system_needs_idmapped_vectors() &&
19982e403167SQuentin Perret 	    !is_protected_kvm_enabled()) {
1999b881cdceSWill Deacon 		err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
2000b881cdceSWill Deacon 					       __BP_HARDEN_HYP_VECS_SZ, &base);
2001b881cdceSWill Deacon 		if (err)
2002b881cdceSWill Deacon 			return err;
2003b881cdceSWill Deacon 	}
20049ef2b48bSWill Deacon 
2005b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
2006b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
20079ef2b48bSWill Deacon 	return 0;
20089ef2b48bSWill Deacon }
20099ef2b48bSWill Deacon 
201053bf620aSSean Christopherson static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
20119ed24f4bSMarc Zyngier {
20129cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
2013bd412e2aSRyan Roberts 	u64 mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
2014d3e1086cSDavid Brazdil 	unsigned long tcr;
20159ed24f4bSMarc Zyngier 
201671b3ec5fSDavid Brazdil 	/*
201771b3ec5fSDavid Brazdil 	 * Calculate the raw per-cpu offset without a translation from the
201871b3ec5fSDavid Brazdil 	 * kernel's mapping to the linear mapping, and store it in tpidr_el2
201971b3ec5fSDavid Brazdil 	 * so that we can use adr_l to access per-cpu variables in EL2.
2020e1663372SSteven Price 	 * Also drop the KASAN tag which gets in the way...
202171b3ec5fSDavid Brazdil 	 */
20229cc77581SQuentin Perret 	params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
202330c95391SDavid Brazdil 			    (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
202471b3ec5fSDavid Brazdil 
2025d3e1086cSDavid Brazdil 	params->mair_el2 = read_sysreg(mair_el1);
2026d3e1086cSDavid Brazdil 
2027cff3b5cfSMarc Zyngier 	tcr = read_sysreg(tcr_el1);
2028cff3b5cfSMarc Zyngier 	if (cpus_have_final_cap(ARM64_KVM_HVHE)) {
2029cff3b5cfSMarc Zyngier 		tcr |= TCR_EPD1_MASK;
2030cff3b5cfSMarc Zyngier 	} else {
2031cff3b5cfSMarc Zyngier 		tcr &= TCR_EL2_MASK;
2032cff3b5cfSMarc Zyngier 		tcr |= TCR_EL2_RES1;
2033cff3b5cfSMarc Zyngier 	}
2034d3e1086cSDavid Brazdil 	tcr &= ~TCR_T0SZ_MASK;
2035579d7ebeSRyan Roberts 	tcr |= TCR_T0SZ(hyp_va_bits);
2036bd412e2aSRyan Roberts 	tcr &= ~TCR_EL2_PS_MASK;
2037bd412e2aSRyan Roberts 	tcr |= FIELD_PREP(TCR_EL2_PS_MASK, kvm_get_parange(mmfr0));
2038bd412e2aSRyan Roberts 	if (kvm_lpa2_is_enabled())
2039bd412e2aSRyan Roberts 		tcr |= TCR_EL2_DS;
2040d3e1086cSDavid Brazdil 	params->tcr_el2 = tcr;
2041d3e1086cSDavid Brazdil 
204263fec243SDavid Brazdil 	params->pgd_pa = kvm_mmu_get_httbr();
2043734864c1SQuentin Perret 	if (is_protected_kvm_enabled())
2044734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
2045734864c1SQuentin Perret 	else
2046734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
2047d0daf5a2SMarc Zyngier 	if (cpus_have_final_cap(ARM64_KVM_HVHE))
2048d0daf5a2SMarc Zyngier 		params->hcr_el2 |= HCR_E2H;
2049734864c1SQuentin Perret 	params->vttbr = params->vtcr = 0;
205063fec243SDavid Brazdil 
205163fec243SDavid Brazdil 	/*
205263fec243SDavid Brazdil 	 * Flush the init params from the data cache because the struct will
205363fec243SDavid Brazdil 	 * be read while the MMU is off.
205463fec243SDavid Brazdil 	 */
205563fec243SDavid Brazdil 	kvm_flush_dcache_to_poc(params, sizeof(*params));
20569cc77581SQuentin Perret }
20579cc77581SQuentin Perret 
2058bfa79a80SQuentin Perret static void hyp_install_host_vector(void)
20599cc77581SQuentin Perret {
20609cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params;
20619cc77581SQuentin Perret 	struct arm_smccc_res res;
20629cc77581SQuentin Perret 
20639cc77581SQuentin Perret 	/* Switch from the HYP stub to our own HYP init vector */
20649cc77581SQuentin Perret 	__hyp_set_vectors(kvm_get_idmap_vector());
20659ed24f4bSMarc Zyngier 
206671b3ec5fSDavid Brazdil 	/*
206771b3ec5fSDavid Brazdil 	 * Call initialization code, and switch to the full blown HYP code.
206871b3ec5fSDavid Brazdil 	 * If the cpucaps haven't been finalized yet, something has gone very
206971b3ec5fSDavid Brazdil 	 * wrong, and hyp will crash and burn when it uses any
2070d8569fbaSMark Rutland 	 * cpus_have_*_cap() wrapper.
207171b3ec5fSDavid Brazdil 	 */
207271b3ec5fSDavid Brazdil 	BUG_ON(!system_capabilities_finalized());
20739cc77581SQuentin Perret 	params = this_cpu_ptr_nvhe_sym(kvm_init_params);
207463fec243SDavid Brazdil 	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
207504e4caa8SAndrew Scull 	WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
2076bfa79a80SQuentin Perret }
2077bfa79a80SQuentin Perret 
2078bfa79a80SQuentin Perret static void cpu_init_hyp_mode(void)
2079bfa79a80SQuentin Perret {
2080bfa79a80SQuentin Perret 	hyp_install_host_vector();
208171b3ec5fSDavid Brazdil 
208271b3ec5fSDavid Brazdil 	/*
208371b3ec5fSDavid Brazdil 	 * Disabling SSBD on a non-VHE system requires us to enable SSBS
208471b3ec5fSDavid Brazdil 	 * at EL2.
208571b3ec5fSDavid Brazdil 	 */
208671b3ec5fSDavid Brazdil 	if (this_cpu_has_cap(ARM64_SSBS) &&
2087d63d975aSMarc Zyngier 	    arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
208813aeb9b4SDavid Brazdil 		kvm_call_hyp_nvhe(__kvm_enable_ssbs);
208971b3ec5fSDavid Brazdil 	}
20909ed24f4bSMarc Zyngier }
20919ed24f4bSMarc Zyngier 
20929ed24f4bSMarc Zyngier static void cpu_hyp_reset(void)
20939ed24f4bSMarc Zyngier {
20949ed24f4bSMarc Zyngier 	if (!is_kernel_in_hyp_mode())
20959ed24f4bSMarc Zyngier 		__hyp_reset_vectors();
20969ed24f4bSMarc Zyngier }
20979ed24f4bSMarc Zyngier 
2098042c76a9SWill Deacon /*
2099042c76a9SWill Deacon  * EL2 vectors can be mapped and rerouted in a number of ways,
2100042c76a9SWill Deacon  * depending on the kernel configuration and CPU present:
2101042c76a9SWill Deacon  *
2102042c76a9SWill Deacon  * - If the CPU is affected by Spectre-v2, the hardening sequence is
2103042c76a9SWill Deacon  *   placed in one of the vector slots, which is executed before jumping
2104042c76a9SWill Deacon  *   to the real vectors.
2105042c76a9SWill Deacon  *
2106c4792b6dSWill Deacon  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
2107042c76a9SWill Deacon  *   containing the hardening sequence is mapped next to the idmap page,
2108042c76a9SWill Deacon  *   and executed before jumping to the real vectors.
2109042c76a9SWill Deacon  *
2110c4792b6dSWill Deacon  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
2111042c76a9SWill Deacon  *   empty slot is selected, mapped next to the idmap page, and
2112042c76a9SWill Deacon  *   executed before jumping to the real vectors.
2113042c76a9SWill Deacon  *
2114c4792b6dSWill Deacon  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
2115042c76a9SWill Deacon  * VHE, as we don't have hypervisor-specific mappings. If the system
2116042c76a9SWill Deacon  * is VHE and yet selects this capability, it will be ignored.
2117042c76a9SWill Deacon  */
2118042c76a9SWill Deacon static void cpu_set_hyp_vector(void)
2119042c76a9SWill Deacon {
21206279017eSWill Deacon 	struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
2121b881cdceSWill Deacon 	void *vector = hyp_spectre_vector_selector[data->slot];
2122042c76a9SWill Deacon 
2123bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
2124b881cdceSWill Deacon 		*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
2125bfa79a80SQuentin Perret 	else
2126bfa79a80SQuentin Perret 		kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
2127042c76a9SWill Deacon }
2128042c76a9SWill Deacon 
21298579a185SWill Deacon static void cpu_hyp_init_context(void)
21309ed24f4bSMarc Zyngier {
213187f842c6SMarc Zyngier 	kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
21329ed24f4bSMarc Zyngier 
21338579a185SWill Deacon 	if (!is_kernel_in_hyp_mode())
21348579a185SWill Deacon 		cpu_init_hyp_mode();
21358579a185SWill Deacon }
21368579a185SWill Deacon 
21378579a185SWill Deacon static void cpu_hyp_init_features(void)
21388579a185SWill Deacon {
21398579a185SWill Deacon 	cpu_set_hyp_vector();
21408579a185SWill Deacon 	kvm_arm_init_debug();
2141a0e47952SAndrew Scull 
21429ed24f4bSMarc Zyngier 	if (is_kernel_in_hyp_mode())
21439ed24f4bSMarc Zyngier 		kvm_timer_init_vhe();
21449ed24f4bSMarc Zyngier 
21459ed24f4bSMarc Zyngier 	if (vgic_present)
21469ed24f4bSMarc Zyngier 		kvm_vgic_init_cpu_hardware();
21479ed24f4bSMarc Zyngier }
21489ed24f4bSMarc Zyngier 
21498579a185SWill Deacon static void cpu_hyp_reinit(void)
21508579a185SWill Deacon {
21518579a185SWill Deacon 	cpu_hyp_reset();
21528579a185SWill Deacon 	cpu_hyp_init_context();
21538579a185SWill Deacon 	cpu_hyp_init_features();
21548579a185SWill Deacon }
21558579a185SWill Deacon 
2156733c758eSOliver Upton static void cpu_hyp_init(void *discard)
21579ed24f4bSMarc Zyngier {
2158733c758eSOliver Upton 	if (!__this_cpu_read(kvm_hyp_initialized)) {
21599ed24f4bSMarc Zyngier 		cpu_hyp_reinit();
2160733c758eSOliver Upton 		__this_cpu_write(kvm_hyp_initialized, 1);
2161733c758eSOliver Upton 	}
2162733c758eSOliver Upton }
2163733c758eSOliver Upton 
2164733c758eSOliver Upton static void cpu_hyp_uninit(void *discard)
2165733c758eSOliver Upton {
2166733c758eSOliver Upton 	if (__this_cpu_read(kvm_hyp_initialized)) {
2167733c758eSOliver Upton 		cpu_hyp_reset();
2168733c758eSOliver Upton 		__this_cpu_write(kvm_hyp_initialized, 0);
21699ed24f4bSMarc Zyngier 	}
21709ed24f4bSMarc Zyngier }
21719ed24f4bSMarc Zyngier 
21729ed24f4bSMarc Zyngier int kvm_arch_hardware_enable(void)
21739ed24f4bSMarc Zyngier {
2174970dee09SMarc Zyngier 	/*
2175970dee09SMarc Zyngier 	 * Most calls to this function are made with migration
2176970dee09SMarc Zyngier 	 * disabled, but not with preemption disabled. The former is
2177970dee09SMarc Zyngier 	 * enough to ensure correctness, but most of the helpers
2178970dee09SMarc Zyngier 	 * expect the later and will throw a tantrum otherwise.
2179970dee09SMarc Zyngier 	 */
2180970dee09SMarc Zyngier 	preempt_disable();
2181970dee09SMarc Zyngier 
2182733c758eSOliver Upton 	cpu_hyp_init(NULL);
2183466d27e4SMarc Zyngier 
2184466d27e4SMarc Zyngier 	kvm_vgic_cpu_up();
2185466d27e4SMarc Zyngier 	kvm_timer_cpu_up();
2186466d27e4SMarc Zyngier 
2187970dee09SMarc Zyngier 	preempt_enable();
2188970dee09SMarc Zyngier 
21899ed24f4bSMarc Zyngier 	return 0;
21909ed24f4bSMarc Zyngier }
21919ed24f4bSMarc Zyngier 
21929ed24f4bSMarc Zyngier void kvm_arch_hardware_disable(void)
21939ed24f4bSMarc Zyngier {
2194466d27e4SMarc Zyngier 	kvm_timer_cpu_down();
2195466d27e4SMarc Zyngier 	kvm_vgic_cpu_down();
2196466d27e4SMarc Zyngier 
2197fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
2198733c758eSOliver Upton 		cpu_hyp_uninit(NULL);
21999ed24f4bSMarc Zyngier }
22009ed24f4bSMarc Zyngier 
22019ed24f4bSMarc Zyngier #ifdef CONFIG_CPU_PM
22029ed24f4bSMarc Zyngier static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
22039ed24f4bSMarc Zyngier 				    unsigned long cmd,
22049ed24f4bSMarc Zyngier 				    void *v)
22059ed24f4bSMarc Zyngier {
22069ed24f4bSMarc Zyngier 	/*
2207733c758eSOliver Upton 	 * kvm_hyp_initialized is left with its old value over
22089ed24f4bSMarc Zyngier 	 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
22099ed24f4bSMarc Zyngier 	 * re-enable hyp.
22109ed24f4bSMarc Zyngier 	 */
22119ed24f4bSMarc Zyngier 	switch (cmd) {
22129ed24f4bSMarc Zyngier 	case CPU_PM_ENTER:
2213733c758eSOliver Upton 		if (__this_cpu_read(kvm_hyp_initialized))
22149ed24f4bSMarc Zyngier 			/*
2215733c758eSOliver Upton 			 * don't update kvm_hyp_initialized here
2216733c758eSOliver Upton 			 * so that the hyp will be re-enabled
22179ed24f4bSMarc Zyngier 			 * when we resume. See below.
22189ed24f4bSMarc Zyngier 			 */
22199ed24f4bSMarc Zyngier 			cpu_hyp_reset();
22209ed24f4bSMarc Zyngier 
22219ed24f4bSMarc Zyngier 		return NOTIFY_OK;
22229ed24f4bSMarc Zyngier 	case CPU_PM_ENTER_FAILED:
22239ed24f4bSMarc Zyngier 	case CPU_PM_EXIT:
2224733c758eSOliver Upton 		if (__this_cpu_read(kvm_hyp_initialized))
2225733c758eSOliver Upton 			/* The hyp was enabled before suspend. */
22269ed24f4bSMarc Zyngier 			cpu_hyp_reinit();
22279ed24f4bSMarc Zyngier 
22289ed24f4bSMarc Zyngier 		return NOTIFY_OK;
22299ed24f4bSMarc Zyngier 
22309ed24f4bSMarc Zyngier 	default:
22319ed24f4bSMarc Zyngier 		return NOTIFY_DONE;
22329ed24f4bSMarc Zyngier 	}
22339ed24f4bSMarc Zyngier }
22349ed24f4bSMarc Zyngier 
22359ed24f4bSMarc Zyngier static struct notifier_block hyp_init_cpu_pm_nb = {
22369ed24f4bSMarc Zyngier 	.notifier_call = hyp_init_cpu_pm_notifier,
22379ed24f4bSMarc Zyngier };
22389ed24f4bSMarc Zyngier 
223953bf620aSSean Christopherson static void __init hyp_cpu_pm_init(void)
22409ed24f4bSMarc Zyngier {
2241fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
22429ed24f4bSMarc Zyngier 		cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
22439ed24f4bSMarc Zyngier }
224453bf620aSSean Christopherson static void __init hyp_cpu_pm_exit(void)
22459ed24f4bSMarc Zyngier {
2246fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
22479ed24f4bSMarc Zyngier 		cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
22489ed24f4bSMarc Zyngier }
22499ed24f4bSMarc Zyngier #else
225053bf620aSSean Christopherson static inline void __init hyp_cpu_pm_init(void)
22519ed24f4bSMarc Zyngier {
22529ed24f4bSMarc Zyngier }
225353bf620aSSean Christopherson static inline void __init hyp_cpu_pm_exit(void)
22549ed24f4bSMarc Zyngier {
22559ed24f4bSMarc Zyngier }
22569ed24f4bSMarc Zyngier #endif
22579ed24f4bSMarc Zyngier 
225853bf620aSSean Christopherson static void __init init_cpu_logical_map(void)
225994f5e8a4SDavid Brazdil {
226094f5e8a4SDavid Brazdil 	unsigned int cpu;
226194f5e8a4SDavid Brazdil 
226294f5e8a4SDavid Brazdil 	/*
226394f5e8a4SDavid Brazdil 	 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
226421ea4578SJulia Lawall 	 * Only copy the set of online CPUs whose features have been checked
226594f5e8a4SDavid Brazdil 	 * against the finalized system capabilities. The hypervisor will not
226694f5e8a4SDavid Brazdil 	 * allow any other CPUs from the `possible` set to boot.
226794f5e8a4SDavid Brazdil 	 */
226894f5e8a4SDavid Brazdil 	for_each_online_cpu(cpu)
226961fe0c37SDavid Brazdil 		hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
227094f5e8a4SDavid Brazdil }
227194f5e8a4SDavid Brazdil 
2272767c973fSMarc Zyngier #define init_psci_0_1_impl_state(config, what)	\
2273767c973fSMarc Zyngier 	config.psci_0_1_ ## what ## _implemented = psci_ops.what
2274767c973fSMarc Zyngier 
227553bf620aSSean Christopherson static bool __init init_psci_relay(void)
2276eeeee719SDavid Brazdil {
2277eeeee719SDavid Brazdil 	/*
2278eeeee719SDavid Brazdil 	 * If PSCI has not been initialized, protected KVM cannot install
2279eeeee719SDavid Brazdil 	 * itself on newly booted CPUs.
2280eeeee719SDavid Brazdil 	 */
2281eeeee719SDavid Brazdil 	if (!psci_ops.get_version) {
2282eeeee719SDavid Brazdil 		kvm_err("Cannot initialize protected mode without PSCI\n");
2283eeeee719SDavid Brazdil 		return false;
2284eeeee719SDavid Brazdil 	}
2285eeeee719SDavid Brazdil 
2286ff367fe4SDavid Brazdil 	kvm_host_psci_config.version = psci_ops.get_version();
228712bdce4fSWill Deacon 	kvm_host_psci_config.smccc_version = arm_smccc_get_version();
2288ff367fe4SDavid Brazdil 
2289ff367fe4SDavid Brazdil 	if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
2290ff367fe4SDavid Brazdil 		kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
2291767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
2292767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
2293767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
2294767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
2295ff367fe4SDavid Brazdil 	}
2296eeeee719SDavid Brazdil 	return true;
2297eeeee719SDavid Brazdil }
2298eeeee719SDavid Brazdil 
229953bf620aSSean Christopherson static int __init init_subsystems(void)
23009ed24f4bSMarc Zyngier {
23019ed24f4bSMarc Zyngier 	int err = 0;
23029ed24f4bSMarc Zyngier 
23039ed24f4bSMarc Zyngier 	/*
23049ed24f4bSMarc Zyngier 	 * Enable hardware so that subsystem initialisation can access EL2.
23059ed24f4bSMarc Zyngier 	 */
2306733c758eSOliver Upton 	on_each_cpu(cpu_hyp_init, NULL, 1);
23079ed24f4bSMarc Zyngier 
23089ed24f4bSMarc Zyngier 	/*
23099ed24f4bSMarc Zyngier 	 * Register CPU lower-power notifier
23109ed24f4bSMarc Zyngier 	 */
23119ed24f4bSMarc Zyngier 	hyp_cpu_pm_init();
23129ed24f4bSMarc Zyngier 
23139ed24f4bSMarc Zyngier 	/*
23149ed24f4bSMarc Zyngier 	 * Init HYP view of VGIC
23159ed24f4bSMarc Zyngier 	 */
23169ed24f4bSMarc Zyngier 	err = kvm_vgic_hyp_init();
23179ed24f4bSMarc Zyngier 	switch (err) {
23189ed24f4bSMarc Zyngier 	case 0:
23199ed24f4bSMarc Zyngier 		vgic_present = true;
23209ed24f4bSMarc Zyngier 		break;
23219ed24f4bSMarc Zyngier 	case -ENODEV:
23229ed24f4bSMarc Zyngier 	case -ENXIO:
23239ed24f4bSMarc Zyngier 		vgic_present = false;
23249ed24f4bSMarc Zyngier 		err = 0;
23259ed24f4bSMarc Zyngier 		break;
23269ed24f4bSMarc Zyngier 	default:
23279ed24f4bSMarc Zyngier 		goto out;
23289ed24f4bSMarc Zyngier 	}
23299ed24f4bSMarc Zyngier 
23309ed24f4bSMarc Zyngier 	/*
23319ed24f4bSMarc Zyngier 	 * Init HYP architected timer support
23329ed24f4bSMarc Zyngier 	 */
23339ed24f4bSMarc Zyngier 	err = kvm_timer_hyp_init(vgic_present);
23349ed24f4bSMarc Zyngier 	if (err)
23359ed24f4bSMarc Zyngier 		goto out;
23369ed24f4bSMarc Zyngier 
233717ed14ebSSean Christopherson 	kvm_register_perf_callbacks(NULL);
233817ed14ebSSean Christopherson 
23399ed24f4bSMarc Zyngier out:
234078b3bf48SSean Christopherson 	if (err)
234178b3bf48SSean Christopherson 		hyp_cpu_pm_exit();
234278b3bf48SSean Christopherson 
2343fa8c3d65SDavid Brazdil 	if (err || !is_protected_kvm_enabled())
2344733c758eSOliver Upton 		on_each_cpu(cpu_hyp_uninit, NULL, 1);
23459ed24f4bSMarc Zyngier 
23469ed24f4bSMarc Zyngier 	return err;
23479ed24f4bSMarc Zyngier }
23489ed24f4bSMarc Zyngier 
234953bf620aSSean Christopherson static void __init teardown_subsystems(void)
235078b3bf48SSean Christopherson {
235178b3bf48SSean Christopherson 	kvm_unregister_perf_callbacks();
235278b3bf48SSean Christopherson 	hyp_cpu_pm_exit();
235378b3bf48SSean Christopherson }
235478b3bf48SSean Christopherson 
235553bf620aSSean Christopherson static void __init teardown_hyp_mode(void)
23569ed24f4bSMarc Zyngier {
235766d5b53eSFuad Tabba 	bool free_sve = system_supports_sve() && is_protected_kvm_enabled();
23589ed24f4bSMarc Zyngier 	int cpu;
23599ed24f4bSMarc Zyngier 
23609ed24f4bSMarc Zyngier 	free_hyp_pgds();
236130c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
23629ed24f4bSMarc Zyngier 		free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
2363fe41a7f8SQuentin Perret 		free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
236466d5b53eSFuad Tabba 
236566d5b53eSFuad Tabba 		if (free_sve) {
236666d5b53eSFuad Tabba 			struct cpu_sve_state *sve_state;
236766d5b53eSFuad Tabba 
236866d5b53eSFuad Tabba 			sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
236966d5b53eSFuad Tabba 			free_pages((unsigned long) sve_state, pkvm_host_sve_state_order());
237066d5b53eSFuad Tabba 		}
237130c95391SDavid Brazdil 	}
23729ed24f4bSMarc Zyngier }
23739ed24f4bSMarc Zyngier 
237453bf620aSSean Christopherson static int __init do_pkvm_init(u32 hyp_va_bits)
2375bfa79a80SQuentin Perret {
2376fe41a7f8SQuentin Perret 	void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
2377bfa79a80SQuentin Perret 	int ret;
2378bfa79a80SQuentin Perret 
2379bfa79a80SQuentin Perret 	preempt_disable();
23808579a185SWill Deacon 	cpu_hyp_init_context();
2381bfa79a80SQuentin Perret 	ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
2382bfa79a80SQuentin Perret 				num_possible_cpus(), kern_hyp_va(per_cpu_base),
2383bfa79a80SQuentin Perret 				hyp_va_bits);
23848579a185SWill Deacon 	cpu_hyp_init_features();
23858579a185SWill Deacon 
23868579a185SWill Deacon 	/*
23878579a185SWill Deacon 	 * The stub hypercalls are now disabled, so set our local flag to
23888579a185SWill Deacon 	 * prevent a later re-init attempt in kvm_arch_hardware_enable().
23898579a185SWill Deacon 	 */
2390733c758eSOliver Upton 	__this_cpu_write(kvm_hyp_initialized, 1);
2391bfa79a80SQuentin Perret 	preempt_enable();
2392bfa79a80SQuentin Perret 
2393bfa79a80SQuentin Perret 	return ret;
2394bfa79a80SQuentin Perret }
2395bfa79a80SQuentin Perret 
2396e8162521SFuad Tabba static u64 get_hyp_id_aa64pfr0_el1(void)
2397e8162521SFuad Tabba {
2398e8162521SFuad Tabba 	/*
2399e8162521SFuad Tabba 	 * Track whether the system isn't affected by spectre/meltdown in the
2400e8162521SFuad Tabba 	 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs.
2401e8162521SFuad Tabba 	 * Although this is per-CPU, we make it global for simplicity, e.g., not
2402e8162521SFuad Tabba 	 * to have to worry about vcpu migration.
2403e8162521SFuad Tabba 	 *
2404e8162521SFuad Tabba 	 * Unlike for non-protected VMs, userspace cannot override this for
2405e8162521SFuad Tabba 	 * protected VMs.
2406e8162521SFuad Tabba 	 */
2407e8162521SFuad Tabba 	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
2408e8162521SFuad Tabba 
2409e8162521SFuad Tabba 	val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
2410e8162521SFuad Tabba 		 ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
2411e8162521SFuad Tabba 
2412e8162521SFuad Tabba 	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
2413e8162521SFuad Tabba 			  arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
2414e8162521SFuad Tabba 	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
2415e8162521SFuad Tabba 			  arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
2416e8162521SFuad Tabba 
2417e8162521SFuad Tabba 	return val;
2418e8162521SFuad Tabba }
2419e8162521SFuad Tabba 
24206c165223SWill Deacon static void kvm_hyp_init_symbols(void)
2421bfa79a80SQuentin Perret {
2422e8162521SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1();
24236c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
24246c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
24256c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
2426def8c222SVladimir Murzin 	kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
24277c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
24287c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
24296c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
24308669651cSQuentin Perret 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
243113e248aaSWill Deacon 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
243273f38ef2SWill Deacon 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
24336c165223SWill Deacon }
24346c165223SWill Deacon 
243553bf620aSSean Christopherson static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
24366c165223SWill Deacon {
24376c165223SWill Deacon 	void *addr = phys_to_virt(hyp_mem_base);
24386c165223SWill Deacon 	int ret;
24397c419937SMarc Zyngier 
2440bfa79a80SQuentin Perret 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
2441bfa79a80SQuentin Perret 	if (ret)
2442bfa79a80SQuentin Perret 		return ret;
2443bfa79a80SQuentin Perret 
2444bfa79a80SQuentin Perret 	ret = do_pkvm_init(hyp_va_bits);
2445bfa79a80SQuentin Perret 	if (ret)
2446bfa79a80SQuentin Perret 		return ret;
2447bfa79a80SQuentin Perret 
2448bfa79a80SQuentin Perret 	free_hyp_pgds();
2449bfa79a80SQuentin Perret 
2450bfa79a80SQuentin Perret 	return 0;
2451bfa79a80SQuentin Perret }
2452bfa79a80SQuentin Perret 
245366d5b53eSFuad Tabba static int init_pkvm_host_sve_state(void)
245466d5b53eSFuad Tabba {
245566d5b53eSFuad Tabba 	int cpu;
245666d5b53eSFuad Tabba 
245766d5b53eSFuad Tabba 	if (!system_supports_sve())
245866d5b53eSFuad Tabba 		return 0;
245966d5b53eSFuad Tabba 
246066d5b53eSFuad Tabba 	/* Allocate pages for host sve state in protected mode. */
246166d5b53eSFuad Tabba 	for_each_possible_cpu(cpu) {
246266d5b53eSFuad Tabba 		struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order());
246366d5b53eSFuad Tabba 
246466d5b53eSFuad Tabba 		if (!page)
246566d5b53eSFuad Tabba 			return -ENOMEM;
246666d5b53eSFuad Tabba 
246766d5b53eSFuad Tabba 		per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = page_address(page);
246866d5b53eSFuad Tabba 	}
246966d5b53eSFuad Tabba 
247066d5b53eSFuad Tabba 	/*
247166d5b53eSFuad Tabba 	 * Don't map the pages in hyp since these are only used in protected
247266d5b53eSFuad Tabba 	 * mode, which will (re)create its own mapping when initialized.
247366d5b53eSFuad Tabba 	 */
247466d5b53eSFuad Tabba 
247566d5b53eSFuad Tabba 	return 0;
247666d5b53eSFuad Tabba }
247766d5b53eSFuad Tabba 
247866d5b53eSFuad Tabba /*
247966d5b53eSFuad Tabba  * Finalizes the initialization of hyp mode, once everything else is initialized
248066d5b53eSFuad Tabba  * and the initialziation process cannot fail.
248166d5b53eSFuad Tabba  */
248266d5b53eSFuad Tabba static void finalize_init_hyp_mode(void)
248366d5b53eSFuad Tabba {
248466d5b53eSFuad Tabba 	int cpu;
248566d5b53eSFuad Tabba 
24861696fc21SFuad Tabba 	if (system_supports_sve() && is_protected_kvm_enabled()) {
248766d5b53eSFuad Tabba 		for_each_possible_cpu(cpu) {
248866d5b53eSFuad Tabba 			struct cpu_sve_state *sve_state;
248966d5b53eSFuad Tabba 
249066d5b53eSFuad Tabba 			sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state;
24911696fc21SFuad Tabba 			per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state =
24921696fc21SFuad Tabba 				kern_hyp_va(sve_state);
24931696fc21SFuad Tabba 		}
24941696fc21SFuad Tabba 	} else {
24951696fc21SFuad Tabba 		for_each_possible_cpu(cpu) {
24961696fc21SFuad Tabba 			struct user_fpsimd_state *fpsimd_state;
24971696fc21SFuad Tabba 
24981696fc21SFuad Tabba 			fpsimd_state = &per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->host_ctxt.fp_regs;
24991696fc21SFuad Tabba 			per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->fpsimd_state =
25001696fc21SFuad Tabba 				kern_hyp_va(fpsimd_state);
25011696fc21SFuad Tabba 		}
250266d5b53eSFuad Tabba 	}
250366d5b53eSFuad Tabba }
250466d5b53eSFuad Tabba 
25058c15c2a0SMostafa Saleh static void pkvm_hyp_init_ptrauth(void)
25068c15c2a0SMostafa Saleh {
25078c15c2a0SMostafa Saleh 	struct kvm_cpu_context *hyp_ctxt;
25088c15c2a0SMostafa Saleh 	int cpu;
25098c15c2a0SMostafa Saleh 
25108c15c2a0SMostafa Saleh 	for_each_possible_cpu(cpu) {
25118c15c2a0SMostafa Saleh 		hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu);
25128c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long();
25138c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long();
25148c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long();
25158c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long();
25168c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long();
25178c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long();
25188c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long();
25198c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long();
25208c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long();
25218c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long();
25228c15c2a0SMostafa Saleh 	}
25238c15c2a0SMostafa Saleh }
25248c15c2a0SMostafa Saleh 
252567d953d4SMarc Zyngier /* Inits Hyp-mode on all online CPUs */
252653bf620aSSean Christopherson static int __init init_hyp_mode(void)
25279ed24f4bSMarc Zyngier {
2528bfa79a80SQuentin Perret 	u32 hyp_va_bits;
25299ed24f4bSMarc Zyngier 	int cpu;
2530bfa79a80SQuentin Perret 	int err = -ENOMEM;
2531bfa79a80SQuentin Perret 
2532bfa79a80SQuentin Perret 	/*
2533bfa79a80SQuentin Perret 	 * The protected Hyp-mode cannot be initialized if the memory pool
2534bfa79a80SQuentin Perret 	 * allocation has failed.
2535bfa79a80SQuentin Perret 	 */
2536bfa79a80SQuentin Perret 	if (is_protected_kvm_enabled() && !hyp_mem_base)
2537bfa79a80SQuentin Perret 		goto out_err;
25389ed24f4bSMarc Zyngier 
25399ed24f4bSMarc Zyngier 	/*
25409ed24f4bSMarc Zyngier 	 * Allocate Hyp PGD and setup Hyp identity mapping
25419ed24f4bSMarc Zyngier 	 */
2542bfa79a80SQuentin Perret 	err = kvm_mmu_init(&hyp_va_bits);
25439ed24f4bSMarc Zyngier 	if (err)
25449ed24f4bSMarc Zyngier 		goto out_err;
25459ed24f4bSMarc Zyngier 
25469ed24f4bSMarc Zyngier 	/*
25479ed24f4bSMarc Zyngier 	 * Allocate stack pages for Hypervisor-mode
25489ed24f4bSMarc Zyngier 	 */
25499ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
25509ed24f4bSMarc Zyngier 		unsigned long stack_page;
25519ed24f4bSMarc Zyngier 
25529ed24f4bSMarc Zyngier 		stack_page = __get_free_page(GFP_KERNEL);
25539ed24f4bSMarc Zyngier 		if (!stack_page) {
25549ed24f4bSMarc Zyngier 			err = -ENOMEM;
25559ed24f4bSMarc Zyngier 			goto out_err;
25569ed24f4bSMarc Zyngier 		}
25579ed24f4bSMarc Zyngier 
25589ed24f4bSMarc Zyngier 		per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
25599ed24f4bSMarc Zyngier 	}
25609ed24f4bSMarc Zyngier 
25619ed24f4bSMarc Zyngier 	/*
256230c95391SDavid Brazdil 	 * Allocate and initialize pages for Hypervisor-mode percpu regions.
256330c95391SDavid Brazdil 	 */
256430c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
256530c95391SDavid Brazdil 		struct page *page;
256630c95391SDavid Brazdil 		void *page_addr;
256730c95391SDavid Brazdil 
256830c95391SDavid Brazdil 		page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
256930c95391SDavid Brazdil 		if (!page) {
257030c95391SDavid Brazdil 			err = -ENOMEM;
257130c95391SDavid Brazdil 			goto out_err;
257230c95391SDavid Brazdil 		}
257330c95391SDavid Brazdil 
257430c95391SDavid Brazdil 		page_addr = page_address(page);
257530c95391SDavid Brazdil 		memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2576fe41a7f8SQuentin Perret 		kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
257730c95391SDavid Brazdil 	}
257830c95391SDavid Brazdil 
257930c95391SDavid Brazdil 	/*
25809ed24f4bSMarc Zyngier 	 * Map the Hyp-code called directly from the host
25819ed24f4bSMarc Zyngier 	 */
25829ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
25839ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
25849ed24f4bSMarc Zyngier 	if (err) {
25859ed24f4bSMarc Zyngier 		kvm_err("Cannot map world-switch code\n");
25869ed24f4bSMarc Zyngier 		goto out_err;
25879ed24f4bSMarc Zyngier 	}
25889ed24f4bSMarc Zyngier 
258916174eeaSDavid Brazdil 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
259016174eeaSDavid Brazdil 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
25912d7bf218SDavid Brazdil 	if (err) {
259216174eeaSDavid Brazdil 		kvm_err("Cannot map .hyp.rodata section\n");
25932d7bf218SDavid Brazdil 		goto out_err;
25942d7bf218SDavid Brazdil 	}
25952d7bf218SDavid Brazdil 
25969ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
25979ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
25989ed24f4bSMarc Zyngier 	if (err) {
25999ed24f4bSMarc Zyngier 		kvm_err("Cannot map rodata section\n");
26009ed24f4bSMarc Zyngier 		goto out_err;
26019ed24f4bSMarc Zyngier 	}
26029ed24f4bSMarc Zyngier 
2603380e18adSQuentin Perret 	/*
2604380e18adSQuentin Perret 	 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2605380e18adSQuentin Perret 	 * section thanks to an assertion in the linker script. Map it RW and
2606380e18adSQuentin Perret 	 * the rest of .bss RO.
2607380e18adSQuentin Perret 	 */
2608380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2609380e18adSQuentin Perret 				  kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2610380e18adSQuentin Perret 	if (err) {
2611380e18adSQuentin Perret 		kvm_err("Cannot map hyp bss section: %d\n", err);
2612380e18adSQuentin Perret 		goto out_err;
2613380e18adSQuentin Perret 	}
2614380e18adSQuentin Perret 
2615380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
26169ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
26179ed24f4bSMarc Zyngier 	if (err) {
26189ed24f4bSMarc Zyngier 		kvm_err("Cannot map bss section\n");
26199ed24f4bSMarc Zyngier 		goto out_err;
26209ed24f4bSMarc Zyngier 	}
26219ed24f4bSMarc Zyngier 
26229ed24f4bSMarc Zyngier 	/*
26239ed24f4bSMarc Zyngier 	 * Map the Hyp stack pages
26249ed24f4bSMarc Zyngier 	 */
26259ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2626ce335431SKalesh Singh 		struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
26279ed24f4bSMarc Zyngier 		char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
26289ed24f4bSMarc Zyngier 
2629f156a7d1SVincent Donnefort 		err = create_hyp_stack(__pa(stack_page), &params->stack_hyp_va);
26309ed24f4bSMarc Zyngier 		if (err) {
26319ed24f4bSMarc Zyngier 			kvm_err("Cannot map hyp stack\n");
26329ed24f4bSMarc Zyngier 			goto out_err;
26339ed24f4bSMarc Zyngier 		}
2634ce335431SKalesh Singh 
2635ce335431SKalesh Singh 		/*
2636ce335431SKalesh Singh 		 * Save the stack PA in nvhe_init_params. This will be needed
2637ce335431SKalesh Singh 		 * to recreate the stack mapping in protected nVHE mode.
2638ce335431SKalesh Singh 		 * __hyp_pa() won't do the right thing there, since the stack
2639ce335431SKalesh Singh 		 * has been mapped in the flexible private VA space.
2640ce335431SKalesh Singh 		 */
2641ce335431SKalesh Singh 		params->stack_pa = __pa(stack_page);
26429ed24f4bSMarc Zyngier 	}
26439ed24f4bSMarc Zyngier 
26449ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2645fe41a7f8SQuentin Perret 		char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
264630c95391SDavid Brazdil 		char *percpu_end = percpu_begin + nvhe_percpu_size();
26479ed24f4bSMarc Zyngier 
26489cc77581SQuentin Perret 		/* Map Hyp percpu pages */
264930c95391SDavid Brazdil 		err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
26509ed24f4bSMarc Zyngier 		if (err) {
265130c95391SDavid Brazdil 			kvm_err("Cannot map hyp percpu region\n");
26526e3bfbb2SAndrew Scull 			goto out_err;
26536e3bfbb2SAndrew Scull 		}
26549cc77581SQuentin Perret 
26559cc77581SQuentin Perret 		/* Prepare the CPU initialization parameters */
2656579d7ebeSRyan Roberts 		cpu_prepare_hyp_mode(cpu, hyp_va_bits);
26579ed24f4bSMarc Zyngier 	}
26589ed24f4bSMarc Zyngier 
26596c165223SWill Deacon 	kvm_hyp_init_symbols();
26606c165223SWill Deacon 
2661eeeee719SDavid Brazdil 	if (is_protected_kvm_enabled()) {
26628c15c2a0SMostafa Saleh 		if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) &&
2663d8569fbaSMark Rutland 		    cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH))
26648c15c2a0SMostafa Saleh 			pkvm_hyp_init_ptrauth();
26658c15c2a0SMostafa Saleh 
266694f5e8a4SDavid Brazdil 		init_cpu_logical_map();
266794f5e8a4SDavid Brazdil 
266852b9e265SWang Wensheng 		if (!init_psci_relay()) {
266952b9e265SWang Wensheng 			err = -ENODEV;
2670eeeee719SDavid Brazdil 			goto out_err;
2671eeeee719SDavid Brazdil 		}
2672eeeee719SDavid Brazdil 
267366d5b53eSFuad Tabba 		err = init_pkvm_host_sve_state();
267466d5b53eSFuad Tabba 		if (err)
267566d5b53eSFuad Tabba 			goto out_err;
267666d5b53eSFuad Tabba 
2677bfa79a80SQuentin Perret 		err = kvm_hyp_init_protection(hyp_va_bits);
2678bfa79a80SQuentin Perret 		if (err) {
2679bfa79a80SQuentin Perret 			kvm_err("Failed to init hyp memory protection\n");
2680bfa79a80SQuentin Perret 			goto out_err;
2681bfa79a80SQuentin Perret 		}
2682bfa79a80SQuentin Perret 	}
26839ed24f4bSMarc Zyngier 
26849ed24f4bSMarc Zyngier 	return 0;
26859ed24f4bSMarc Zyngier 
26869ed24f4bSMarc Zyngier out_err:
26879ed24f4bSMarc Zyngier 	teardown_hyp_mode();
26889ed24f4bSMarc Zyngier 	kvm_err("error initializing Hyp mode: %d\n", err);
26899ed24f4bSMarc Zyngier 	return err;
26909ed24f4bSMarc Zyngier }
26919ed24f4bSMarc Zyngier 
26929ed24f4bSMarc Zyngier struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
26939ed24f4bSMarc Zyngier {
2694ce5d2448SOliver Upton 	struct kvm_vcpu *vcpu = NULL;
2695ce5d2448SOliver Upton 	struct kvm_mpidr_data *data;
269646808a4cSMarc Zyngier 	unsigned long i;
26979ed24f4bSMarc Zyngier 
26989ed24f4bSMarc Zyngier 	mpidr &= MPIDR_HWID_BITMASK;
269954a8006dSMarc Zyngier 
2700ce5d2448SOliver Upton 	rcu_read_lock();
2701ce5d2448SOliver Upton 	data = rcu_dereference(kvm->arch.mpidr_data);
270254a8006dSMarc Zyngier 
2703ce5d2448SOliver Upton 	if (data) {
2704ce5d2448SOliver Upton 		u16 idx = kvm_mpidr_index(data, mpidr);
2705ce5d2448SOliver Upton 
2706ce5d2448SOliver Upton 		vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]);
270754a8006dSMarc Zyngier 		if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu))
270854a8006dSMarc Zyngier 			vcpu = NULL;
270954a8006dSMarc Zyngier 	}
271054a8006dSMarc Zyngier 
2711ce5d2448SOliver Upton 	rcu_read_unlock();
2712ce5d2448SOliver Upton 
2713ce5d2448SOliver Upton 	if (vcpu)
2714ce5d2448SOliver Upton 		return vcpu;
2715ce5d2448SOliver Upton 
27169ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
27179ed24f4bSMarc Zyngier 		if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
27189ed24f4bSMarc Zyngier 			return vcpu;
27199ed24f4bSMarc Zyngier 	}
27209ed24f4bSMarc Zyngier 	return NULL;
27219ed24f4bSMarc Zyngier }
27229ed24f4bSMarc Zyngier 
2723d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2724d663b8a2SPaolo Bonzini {
2725d663b8a2SPaolo Bonzini 	return irqchip_in_kernel(kvm);
2726d663b8a2SPaolo Bonzini }
2727d663b8a2SPaolo Bonzini 
27289ed24f4bSMarc Zyngier bool kvm_arch_has_irq_bypass(void)
27299ed24f4bSMarc Zyngier {
27309ed24f4bSMarc Zyngier 	return true;
27319ed24f4bSMarc Zyngier }
27329ed24f4bSMarc Zyngier 
27339ed24f4bSMarc Zyngier int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
27349ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
27359ed24f4bSMarc Zyngier {
27369ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
27379ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
27389ed24f4bSMarc Zyngier 
27399ed24f4bSMarc Zyngier 	return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
27409ed24f4bSMarc Zyngier 					  &irqfd->irq_entry);
27419ed24f4bSMarc Zyngier }
27429ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
27439ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
27449ed24f4bSMarc Zyngier {
27459ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
27469ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
27479ed24f4bSMarc Zyngier 
27489ed24f4bSMarc Zyngier 	kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
27499ed24f4bSMarc Zyngier 				     &irqfd->irq_entry);
27509ed24f4bSMarc Zyngier }
27519ed24f4bSMarc Zyngier 
27529ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
27539ed24f4bSMarc Zyngier {
27549ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
27559ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
27569ed24f4bSMarc Zyngier 
27579ed24f4bSMarc Zyngier 	kvm_arm_halt_guest(irqfd->kvm);
27589ed24f4bSMarc Zyngier }
27599ed24f4bSMarc Zyngier 
27609ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
27619ed24f4bSMarc Zyngier {
27629ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
27639ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
27649ed24f4bSMarc Zyngier 
27659ed24f4bSMarc Zyngier 	kvm_arm_resume_guest(irqfd->kvm);
27669ed24f4bSMarc Zyngier }
27679ed24f4bSMarc Zyngier 
276867d953d4SMarc Zyngier /* Initialize Hyp-mode and memory mappings on all CPUs */
276953bf620aSSean Christopherson static __init int kvm_arm_init(void)
27709ed24f4bSMarc Zyngier {
27719ed24f4bSMarc Zyngier 	int err;
27729ed24f4bSMarc Zyngier 	bool in_hyp_mode;
27739ed24f4bSMarc Zyngier 
27749ed24f4bSMarc Zyngier 	if (!is_hyp_mode_available()) {
27759ed24f4bSMarc Zyngier 		kvm_info("HYP mode not available\n");
27769ed24f4bSMarc Zyngier 		return -ENODEV;
27779ed24f4bSMarc Zyngier 	}
27789ed24f4bSMarc Zyngier 
2779b6a68b97SMarc Zyngier 	if (kvm_get_mode() == KVM_MODE_NONE) {
2780b6a68b97SMarc Zyngier 		kvm_info("KVM disabled from command line\n");
2781b6a68b97SMarc Zyngier 		return -ENODEV;
2782b6a68b97SMarc Zyngier 	}
2783b6a68b97SMarc Zyngier 
2784f1f0c0cfSAlexandru Elisei 	err = kvm_sys_reg_table_init();
2785f1f0c0cfSAlexandru Elisei 	if (err) {
2786f1f0c0cfSAlexandru Elisei 		kvm_info("Error initializing system register tables");
2787f1f0c0cfSAlexandru Elisei 		return err;
2788f1f0c0cfSAlexandru Elisei 	}
2789f1f0c0cfSAlexandru Elisei 
27909ed24f4bSMarc Zyngier 	in_hyp_mode = is_kernel_in_hyp_mode();
27919ed24f4bSMarc Zyngier 
279296d389caSRob Herring 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
279396d389caSRob Herring 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2794abf532ccSRob Herring 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2795abf532ccSRob Herring 			 "Only trusted guests should be used on this system.\n");
2796abf532ccSRob Herring 
2797bf249d9eSAnshuman Khandual 	err = kvm_set_ipa_limit();
27989ed24f4bSMarc Zyngier 	if (err)
27999ed24f4bSMarc Zyngier 		return err;
28009ed24f4bSMarc Zyngier 
28019ed24f4bSMarc Zyngier 	err = kvm_arm_init_sve();
28029ed24f4bSMarc Zyngier 	if (err)
28039ed24f4bSMarc Zyngier 		return err;
28049ed24f4bSMarc Zyngier 
28053248136bSJulien Grall 	err = kvm_arm_vmid_alloc_init();
28063248136bSJulien Grall 	if (err) {
28073248136bSJulien Grall 		kvm_err("Failed to initialize VMID allocator.\n");
28083248136bSJulien Grall 		return err;
28093248136bSJulien Grall 	}
28103248136bSJulien Grall 
28119ed24f4bSMarc Zyngier 	if (!in_hyp_mode) {
28129ed24f4bSMarc Zyngier 		err = init_hyp_mode();
28139ed24f4bSMarc Zyngier 		if (err)
28149ed24f4bSMarc Zyngier 			goto out_err;
28159ed24f4bSMarc Zyngier 	}
28169ed24f4bSMarc Zyngier 
2817b881cdceSWill Deacon 	err = kvm_init_vector_slots();
2818b881cdceSWill Deacon 	if (err) {
2819b881cdceSWill Deacon 		kvm_err("Cannot initialise vector slots\n");
28206baaeda8SSean Christopherson 		goto out_hyp;
2821b881cdceSWill Deacon 	}
2822b881cdceSWill Deacon 
28239ed24f4bSMarc Zyngier 	err = init_subsystems();
28249ed24f4bSMarc Zyngier 	if (err)
282578b3bf48SSean Christopherson 		goto out_hyp;
28269ed24f4bSMarc Zyngier 
2827d96c66abSMarc Zyngier 	kvm_info("%s%sVHE mode initialized successfully\n",
2828d96c66abSMarc Zyngier 		 in_hyp_mode ? "" : (is_protected_kvm_enabled() ?
2829d96c66abSMarc Zyngier 				     "Protected " : "Hyp "),
2830d96c66abSMarc Zyngier 		 in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ?
2831d96c66abSMarc Zyngier 				     "h" : "n"));
28329ed24f4bSMarc Zyngier 
28331dc0f02dSSean Christopherson 	/*
28341dc0f02dSSean Christopherson 	 * FIXME: Do something reasonable if kvm_init() fails after pKVM
28351dc0f02dSSean Christopherson 	 * hypervisor protection is finalized.
28361dc0f02dSSean Christopherson 	 */
283781a1cf9fSSean Christopherson 	err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
28381dc0f02dSSean Christopherson 	if (err)
28391dc0f02dSSean Christopherson 		goto out_subs;
28401dc0f02dSSean Christopherson 
284166d5b53eSFuad Tabba 	/*
284266d5b53eSFuad Tabba 	 * This should be called after initialization is done and failure isn't
284366d5b53eSFuad Tabba 	 * possible anymore.
284466d5b53eSFuad Tabba 	 */
284566d5b53eSFuad Tabba 	if (!in_hyp_mode)
284666d5b53eSFuad Tabba 		finalize_init_hyp_mode();
284766d5b53eSFuad Tabba 
2848fa729bc7SSudeep Holla 	kvm_arm_initialised = true;
2849fa729bc7SSudeep Holla 
28509ed24f4bSMarc Zyngier 	return 0;
28519ed24f4bSMarc Zyngier 
28526baaeda8SSean Christopherson out_subs:
285378b3bf48SSean Christopherson 	teardown_subsystems();
28546baaeda8SSean Christopherson out_hyp:
28559ed24f4bSMarc Zyngier 	if (!in_hyp_mode)
28569ed24f4bSMarc Zyngier 		teardown_hyp_mode();
28579ed24f4bSMarc Zyngier out_err:
28583248136bSJulien Grall 	kvm_arm_vmid_alloc_free();
28599ed24f4bSMarc Zyngier 	return err;
28609ed24f4bSMarc Zyngier }
28619ed24f4bSMarc Zyngier 
2862d8b369c4SDavid Brazdil static int __init early_kvm_mode_cfg(char *arg)
2863d8b369c4SDavid Brazdil {
2864d8b369c4SDavid Brazdil 	if (!arg)
2865d8b369c4SDavid Brazdil 		return -EINVAL;
2866d8b369c4SDavid Brazdil 
2867b2a4d007SElliot Berman 	if (strcmp(arg, "none") == 0) {
2868b2a4d007SElliot Berman 		kvm_mode = KVM_MODE_NONE;
2869b2a4d007SElliot Berman 		return 0;
2870b2a4d007SElliot Berman 	}
2871b2a4d007SElliot Berman 
2872b2a4d007SElliot Berman 	if (!is_hyp_mode_available()) {
2873b2a4d007SElliot Berman 		pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2874b2a4d007SElliot Berman 		return 0;
2875b2a4d007SElliot Berman 	}
2876b2a4d007SElliot Berman 
2877d8b369c4SDavid Brazdil 	if (strcmp(arg, "protected") == 0) {
2878cde5042aSWill Deacon 		if (!is_kernel_in_hyp_mode())
2879d8b369c4SDavid Brazdil 			kvm_mode = KVM_MODE_PROTECTED;
2880cde5042aSWill Deacon 		else
2881cde5042aSWill Deacon 			pr_warn_once("Protected KVM not available with VHE\n");
2882cde5042aSWill Deacon 
2883d8b369c4SDavid Brazdil 		return 0;
2884d8b369c4SDavid Brazdil 	}
2885d8b369c4SDavid Brazdil 
2886b6a68b97SMarc Zyngier 	if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2887b6a68b97SMarc Zyngier 		kvm_mode = KVM_MODE_DEFAULT;
28881945a067SMarc Zyngier 		return 0;
2889b6a68b97SMarc Zyngier 	}
2890b6a68b97SMarc Zyngier 
2891675cabc8SJintack Lim 	if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
2892675cabc8SJintack Lim 		kvm_mode = KVM_MODE_NV;
2893675cabc8SJintack Lim 		return 0;
2894675cabc8SJintack Lim 	}
2895675cabc8SJintack Lim 
2896d8b369c4SDavid Brazdil 	return -EINVAL;
2897d8b369c4SDavid Brazdil }
2898d8b369c4SDavid Brazdil early_param("kvm-arm.mode", early_kvm_mode_cfg);
2899d8b369c4SDavid Brazdil 
29000b5afe05SColton Lewis static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p)
29010b5afe05SColton Lewis {
29020b5afe05SColton Lewis 	if (!arg)
29030b5afe05SColton Lewis 		return -EINVAL;
29040b5afe05SColton Lewis 
29050b5afe05SColton Lewis 	if (strcmp(arg, "trap") == 0) {
29060b5afe05SColton Lewis 		*p = KVM_WFX_TRAP;
29070b5afe05SColton Lewis 		return 0;
29080b5afe05SColton Lewis 	}
29090b5afe05SColton Lewis 
29100b5afe05SColton Lewis 	if (strcmp(arg, "notrap") == 0) {
29110b5afe05SColton Lewis 		*p = KVM_WFX_NOTRAP;
29120b5afe05SColton Lewis 		return 0;
29130b5afe05SColton Lewis 	}
29140b5afe05SColton Lewis 
29150b5afe05SColton Lewis 	return -EINVAL;
29160b5afe05SColton Lewis }
29170b5afe05SColton Lewis 
29180b5afe05SColton Lewis static int __init early_kvm_wfi_trap_policy_cfg(char *arg)
29190b5afe05SColton Lewis {
29200b5afe05SColton Lewis 	return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy);
29210b5afe05SColton Lewis }
29220b5afe05SColton Lewis early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg);
29230b5afe05SColton Lewis 
29240b5afe05SColton Lewis static int __init early_kvm_wfe_trap_policy_cfg(char *arg)
29250b5afe05SColton Lewis {
29260b5afe05SColton Lewis 	return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy);
29270b5afe05SColton Lewis }
29280b5afe05SColton Lewis early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg);
29290b5afe05SColton Lewis 
29303eb681fbSDavid Brazdil enum kvm_mode kvm_get_mode(void)
29313eb681fbSDavid Brazdil {
29323eb681fbSDavid Brazdil 	return kvm_mode;
29333eb681fbSDavid Brazdil }
29343eb681fbSDavid Brazdil 
29351dc0f02dSSean Christopherson module_init(kvm_arm_init);
2936