xref: /linux/arch/arm64/kvm/arm.c (revision c5bac1ef7df6bcce4feaa5a609119cab5d5e4730)
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>
389ed24f4bSMarc Zyngier #include <asm/kvm_mmu.h>
39a77b31dcSMarc Zyngier #include <asm/kvm_nested.h>
409d0c063aSFuad Tabba #include <asm/kvm_pkvm.h>
419ed24f4bSMarc Zyngier #include <asm/kvm_emulate.h>
429ed24f4bSMarc Zyngier #include <asm/sections.h>
439ed24f4bSMarc Zyngier 
449ed24f4bSMarc Zyngier #include <kvm/arm_hypercalls.h>
459ed24f4bSMarc Zyngier #include <kvm/arm_pmu.h>
469ed24f4bSMarc Zyngier #include <kvm/arm_psci.h>
479ed24f4bSMarc Zyngier 
48d8b369c4SDavid Brazdil static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
49d8b369c4SDavid Brazdil 
5014ef9d04SMarc Zyngier DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
5114ef9d04SMarc Zyngier 
52db129d48SKalesh Singh DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
5363fec243SDavid Brazdil DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
549ed24f4bSMarc Zyngier 
558c15c2a0SMostafa Saleh DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
568c15c2a0SMostafa Saleh 
57fa729bc7SSudeep Holla static bool vgic_present, kvm_arm_initialised;
589ed24f4bSMarc Zyngier 
59733c758eSOliver Upton static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized);
609ed24f4bSMarc Zyngier DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
619ed24f4bSMarc Zyngier 
62fa729bc7SSudeep Holla bool is_kvm_arm_initialised(void)
63fa729bc7SSudeep Holla {
64fa729bc7SSudeep Holla 	return kvm_arm_initialised;
65fa729bc7SSudeep Holla }
66fa729bc7SSudeep Holla 
679ed24f4bSMarc Zyngier int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
689ed24f4bSMarc Zyngier {
699ed24f4bSMarc Zyngier 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
709ed24f4bSMarc Zyngier }
719ed24f4bSMarc Zyngier 
729ed24f4bSMarc Zyngier int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
739ed24f4bSMarc Zyngier 			    struct kvm_enable_cap *cap)
749ed24f4bSMarc Zyngier {
759ed24f4bSMarc Zyngier 	int r;
762f440b72SRicardo Koller 	u64 new_cap;
779ed24f4bSMarc Zyngier 
789ed24f4bSMarc Zyngier 	if (cap->flags)
799ed24f4bSMarc Zyngier 		return -EINVAL;
809ed24f4bSMarc Zyngier 
819ed24f4bSMarc Zyngier 	switch (cap->cap) {
829ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
839ed24f4bSMarc Zyngier 		r = 0;
8406394531SMarc Zyngier 		set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
8506394531SMarc Zyngier 			&kvm->arch.flags);
869ed24f4bSMarc Zyngier 		break;
87673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
88c4d7c518SSteven Price 		mutex_lock(&kvm->lock);
89c4d7c518SSteven Price 		if (!system_supports_mte() || kvm->created_vcpus) {
90c4d7c518SSteven Price 			r = -EINVAL;
91c4d7c518SSteven Price 		} else {
92673638f4SSteven Price 			r = 0;
9306394531SMarc Zyngier 			set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
94c4d7c518SSteven Price 		}
95c4d7c518SSteven Price 		mutex_unlock(&kvm->lock);
96673638f4SSteven Price 		break;
97bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
98bfbab445SOliver Upton 		r = 0;
99bfbab445SOliver Upton 		set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
100bfbab445SOliver Upton 		break;
1012f440b72SRicardo Koller 	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
1022f440b72SRicardo Koller 		new_cap = cap->args[0];
1032f440b72SRicardo Koller 
1042f440b72SRicardo Koller 		mutex_lock(&kvm->slots_lock);
1052f440b72SRicardo Koller 		/*
1062f440b72SRicardo Koller 		 * To keep things simple, allow changing the chunk
1072f440b72SRicardo Koller 		 * size only when no memory slots have been created.
1082f440b72SRicardo Koller 		 */
1092f440b72SRicardo Koller 		if (!kvm_are_all_memslots_empty(kvm)) {
1102f440b72SRicardo Koller 			r = -EINVAL;
1112f440b72SRicardo Koller 		} else if (new_cap && !kvm_is_block_size_supported(new_cap)) {
1122f440b72SRicardo Koller 			r = -EINVAL;
1132f440b72SRicardo Koller 		} else {
1142f440b72SRicardo Koller 			r = 0;
1152f440b72SRicardo Koller 			kvm->arch.mmu.split_page_chunk_size = new_cap;
1162f440b72SRicardo Koller 		}
1172f440b72SRicardo Koller 		mutex_unlock(&kvm->slots_lock);
1182f440b72SRicardo Koller 		break;
1199ed24f4bSMarc Zyngier 	default:
1209ed24f4bSMarc Zyngier 		r = -EINVAL;
1219ed24f4bSMarc Zyngier 		break;
1229ed24f4bSMarc Zyngier 	}
1239ed24f4bSMarc Zyngier 
1249ed24f4bSMarc Zyngier 	return r;
1259ed24f4bSMarc Zyngier }
1269ed24f4bSMarc Zyngier 
1275107000fSMarc Zyngier static int kvm_arm_default_max_vcpus(void)
1285107000fSMarc Zyngier {
1295107000fSMarc Zyngier 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
1305107000fSMarc Zyngier }
1315107000fSMarc Zyngier 
1329ed24f4bSMarc Zyngier /**
1339ed24f4bSMarc Zyngier  * kvm_arch_init_vm - initializes a VM data structure
1349ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1359ed24f4bSMarc Zyngier  */
1369ed24f4bSMarc Zyngier int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1379ed24f4bSMarc Zyngier {
138a0e50aa3SChristoffer Dall 	int ret;
1399ed24f4bSMarc Zyngier 
140c43120afSOliver Upton 	mutex_init(&kvm->arch.config_lock);
141c43120afSOliver Upton 
142c43120afSOliver Upton #ifdef CONFIG_LOCKDEP
143c43120afSOliver Upton 	/* Clue in lockdep that the config_lock must be taken inside kvm->lock */
144c43120afSOliver Upton 	mutex_lock(&kvm->lock);
145c43120afSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
146c43120afSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
147c43120afSOliver Upton 	mutex_unlock(&kvm->lock);
148c43120afSOliver Upton #endif
149c43120afSOliver Upton 
1503f868e14SQuentin Perret 	ret = kvm_share_hyp(kvm, kvm + 1);
1519ed24f4bSMarc Zyngier 	if (ret)
152315775ffSQuentin Perret 		return ret;
1539ed24f4bSMarc Zyngier 
1549d0c063aSFuad Tabba 	ret = pkvm_init_host_vm(kvm);
1559d0c063aSFuad Tabba 	if (ret)
156315775ffSQuentin Perret 		goto err_unshare_kvm;
1579ed24f4bSMarc Zyngier 
1585f623a59SOliver Upton 	if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) {
159ae187fecSWill Deacon 		ret = -ENOMEM;
160315775ffSQuentin Perret 		goto err_unshare_kvm;
161ae187fecSWill Deacon 	}
162583cda1bSAlexandru Elisei 	cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
163583cda1bSAlexandru Elisei 
164315775ffSQuentin Perret 	ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
165315775ffSQuentin Perret 	if (ret)
166315775ffSQuentin Perret 		goto err_free_cpumask;
167315775ffSQuentin Perret 
1689ed24f4bSMarc Zyngier 	kvm_vgic_early_init(kvm);
1699ed24f4bSMarc Zyngier 
1708a5eb2d2SMarc Zyngier 	kvm_timer_init_vm(kvm);
1718a5eb2d2SMarc Zyngier 
1729ed24f4bSMarc Zyngier 	/* The maximum number of VCPUs is limited by the host's GIC model */
173f502cc56SSean Christopherson 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
1749ed24f4bSMarc Zyngier 
17505714cabSRaghavendra Rao Ananta 	kvm_arm_init_hypercalls(kvm);
17623711a5eSMarc Zyngier 
1772251e9ffSOliver Upton 	bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES);
1783d0dba57SMarc Zyngier 
179315775ffSQuentin Perret 	return 0;
180315775ffSQuentin Perret 
181315775ffSQuentin Perret err_free_cpumask:
182315775ffSQuentin Perret 	free_cpumask_var(kvm->arch.supported_cpus);
183315775ffSQuentin Perret err_unshare_kvm:
184315775ffSQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
1859ed24f4bSMarc Zyngier 	return ret;
1869ed24f4bSMarc Zyngier }
1879ed24f4bSMarc Zyngier 
1889ed24f4bSMarc Zyngier vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1899ed24f4bSMarc Zyngier {
1909ed24f4bSMarc Zyngier 	return VM_FAULT_SIGBUS;
1919ed24f4bSMarc Zyngier }
1929ed24f4bSMarc Zyngier 
1939ed24f4bSMarc Zyngier 
1949ed24f4bSMarc Zyngier /**
1959ed24f4bSMarc Zyngier  * kvm_arch_destroy_vm - destroy the VM data structure
1969ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1979ed24f4bSMarc Zyngier  */
1989ed24f4bSMarc Zyngier void kvm_arch_destroy_vm(struct kvm *kvm)
1999ed24f4bSMarc Zyngier {
200d7eec236SMarc Zyngier 	bitmap_free(kvm->arch.pmu_filter);
201583cda1bSAlexandru Elisei 	free_cpumask_var(kvm->arch.supported_cpus);
202d7eec236SMarc Zyngier 
2039ed24f4bSMarc Zyngier 	kvm_vgic_destroy(kvm);
2049ed24f4bSMarc Zyngier 
2059d0c063aSFuad Tabba 	if (is_protected_kvm_enabled())
2069d0c063aSFuad Tabba 		pkvm_destroy_hyp_vm(kvm);
2079d0c063aSFuad Tabba 
2085544750eSMarc Zyngier 	kfree(kvm->arch.mpidr_data);
209888f0880SMarc Zyngier 	kfree(kvm->arch.sysreg_masks);
21027592ae8SMarc Zyngier 	kvm_destroy_vcpus(kvm);
21152b28657SQuentin Perret 
21252b28657SQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
213fb88707dSOliver Upton 
214fb88707dSOliver Upton 	kvm_arm_teardown_hypercalls(kvm);
2159ed24f4bSMarc Zyngier }
2169ed24f4bSMarc Zyngier 
2179ed24f4bSMarc Zyngier int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
2189ed24f4bSMarc Zyngier {
2199ed24f4bSMarc Zyngier 	int r;
2209ed24f4bSMarc Zyngier 	switch (ext) {
2219ed24f4bSMarc Zyngier 	case KVM_CAP_IRQCHIP:
2229ed24f4bSMarc Zyngier 		r = vgic_present;
2239ed24f4bSMarc Zyngier 		break;
2249ed24f4bSMarc Zyngier 	case KVM_CAP_IOEVENTFD:
2259ed24f4bSMarc Zyngier 	case KVM_CAP_USER_MEMORY:
2269ed24f4bSMarc Zyngier 	case KVM_CAP_SYNC_MMU:
2279ed24f4bSMarc Zyngier 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2289ed24f4bSMarc Zyngier 	case KVM_CAP_ONE_REG:
2299ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI:
2309ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI_0_2:
2319ed24f4bSMarc Zyngier 	case KVM_CAP_READONLY_MEM:
2329ed24f4bSMarc Zyngier 	case KVM_CAP_MP_STATE:
2339ed24f4bSMarc Zyngier 	case KVM_CAP_IMMEDIATE_EXIT:
2349ed24f4bSMarc Zyngier 	case KVM_CAP_VCPU_EVENTS:
2359ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
2369ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
2379ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_INJECT_EXT_DABT:
23836fb4cd5SWill Deacon 	case KVM_CAP_SET_GUEST_DEBUG:
23936fb4cd5SWill Deacon 	case KVM_CAP_VCPU_ATTRIBUTES:
2403bf72569SJianyong Wu 	case KVM_CAP_PTP_KVM:
241bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
24252882b9cSAlexey Kardashevskiy 	case KVM_CAP_IRQFD_RESAMPLE:
24330ec7997SMarc Zyngier 	case KVM_CAP_COUNTER_OFFSET:
2449ed24f4bSMarc Zyngier 		r = 1;
2459ed24f4bSMarc Zyngier 		break;
246fa18aca9SMaxim Levitsky 	case KVM_CAP_SET_GUEST_DEBUG2:
247fa18aca9SMaxim Levitsky 		return KVM_GUESTDBG_VALID_MASK;
2489ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
2499ed24f4bSMarc Zyngier 		r = 1;
2509ed24f4bSMarc Zyngier 		break;
2519ed24f4bSMarc Zyngier 	case KVM_CAP_NR_VCPUS:
252f60a00d7SVitaly Kuznetsov 		/*
253f60a00d7SVitaly Kuznetsov 		 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
254f60a00d7SVitaly Kuznetsov 		 * architectures, as it does not always bound it to
255f60a00d7SVitaly Kuznetsov 		 * KVM_CAP_MAX_VCPUS. It should not matter much because
256f60a00d7SVitaly Kuznetsov 		 * this is just an advisory value.
257f60a00d7SVitaly Kuznetsov 		 */
258f60a00d7SVitaly Kuznetsov 		r = min_t(unsigned int, num_online_cpus(),
259f60a00d7SVitaly Kuznetsov 			  kvm_arm_default_max_vcpus());
2609ed24f4bSMarc Zyngier 		break;
2619ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPUS:
2629ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPU_ID:
2635107000fSMarc Zyngier 		if (kvm)
264f502cc56SSean Christopherson 			r = kvm->max_vcpus;
2655107000fSMarc Zyngier 		else
2665107000fSMarc Zyngier 			r = kvm_arm_default_max_vcpus();
2679ed24f4bSMarc Zyngier 		break;
2689ed24f4bSMarc Zyngier 	case KVM_CAP_MSI_DEVID:
2699ed24f4bSMarc Zyngier 		if (!kvm)
2709ed24f4bSMarc Zyngier 			r = -EINVAL;
2719ed24f4bSMarc Zyngier 		else
2729ed24f4bSMarc Zyngier 			r = kvm->arch.vgic.msis_require_devid;
2739ed24f4bSMarc Zyngier 		break;
2749ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_USER_IRQ:
2759ed24f4bSMarc Zyngier 		/*
2769ed24f4bSMarc Zyngier 		 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
2779ed24f4bSMarc Zyngier 		 * (bump this number if adding more devices)
2789ed24f4bSMarc Zyngier 		 */
2799ed24f4bSMarc Zyngier 		r = 1;
2809ed24f4bSMarc Zyngier 		break;
281673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
282673638f4SSteven Price 		r = system_supports_mte();
283673638f4SSteven Price 		break;
284004a0124SAndrew Jones 	case KVM_CAP_STEAL_TIME:
285004a0124SAndrew Jones 		r = kvm_arm_pvtime_supported();
286004a0124SAndrew Jones 		break;
28736fb4cd5SWill Deacon 	case KVM_CAP_ARM_EL1_32BIT:
288d8569fbaSMark Rutland 		r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1);
2899ed24f4bSMarc Zyngier 		break;
29036fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
29136fb4cd5SWill Deacon 		r = get_num_brps();
29236fb4cd5SWill Deacon 		break;
29336fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
29436fb4cd5SWill Deacon 		r = get_num_wrps();
29536fb4cd5SWill Deacon 		break;
29636fb4cd5SWill Deacon 	case KVM_CAP_ARM_PMU_V3:
29736fb4cd5SWill Deacon 		r = kvm_arm_support_pmu_v3();
29836fb4cd5SWill Deacon 		break;
29936fb4cd5SWill Deacon 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
300d8569fbaSMark Rutland 		r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN);
30136fb4cd5SWill Deacon 		break;
30236fb4cd5SWill Deacon 	case KVM_CAP_ARM_VM_IPA_SIZE:
30336fb4cd5SWill Deacon 		r = get_kvm_ipa_limit();
30436fb4cd5SWill Deacon 		break;
30536fb4cd5SWill Deacon 	case KVM_CAP_ARM_SVE:
30636fb4cd5SWill Deacon 		r = system_supports_sve();
30736fb4cd5SWill Deacon 		break;
30836fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
30936fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
31036fb4cd5SWill Deacon 		r = system_has_full_ptr_auth();
31136fb4cd5SWill Deacon 		break;
3122f440b72SRicardo Koller 	case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE:
3132f440b72SRicardo Koller 		if (kvm)
3142f440b72SRicardo Koller 			r = kvm->arch.mmu.split_page_chunk_size;
3152f440b72SRicardo Koller 		else
3162f440b72SRicardo Koller 			r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
3172f440b72SRicardo Koller 		break;
3182f440b72SRicardo Koller 	case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES:
3192f440b72SRicardo Koller 		r = kvm_supported_block_sizes();
3202f440b72SRicardo Koller 		break;
3213f9cd0caSJing Zhang 	case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
3223f9cd0caSJing Zhang 		r = BIT(0);
3233f9cd0caSJing Zhang 		break;
32436fb4cd5SWill Deacon 	default:
32536fb4cd5SWill Deacon 		r = 0;
3269ed24f4bSMarc Zyngier 	}
32736fb4cd5SWill Deacon 
3289ed24f4bSMarc Zyngier 	return r;
3299ed24f4bSMarc Zyngier }
3309ed24f4bSMarc Zyngier 
3319ed24f4bSMarc Zyngier long kvm_arch_dev_ioctl(struct file *filp,
3329ed24f4bSMarc Zyngier 			unsigned int ioctl, unsigned long arg)
3339ed24f4bSMarc Zyngier {
3349ed24f4bSMarc Zyngier 	return -EINVAL;
3359ed24f4bSMarc Zyngier }
3369ed24f4bSMarc Zyngier 
3379ed24f4bSMarc Zyngier struct kvm *kvm_arch_alloc_vm(void)
3389ed24f4bSMarc Zyngier {
339115bae92SJia He 	size_t sz = sizeof(struct kvm);
3409ed24f4bSMarc Zyngier 
341115bae92SJia He 	if (!has_vhe())
342115bae92SJia He 		return kzalloc(sz, GFP_KERNEL_ACCOUNT);
343115bae92SJia He 
344115bae92SJia He 	return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
3459ed24f4bSMarc Zyngier }
3469ed24f4bSMarc Zyngier 
3479ed24f4bSMarc Zyngier int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3489ed24f4bSMarc Zyngier {
3499ed24f4bSMarc Zyngier 	if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
3509ed24f4bSMarc Zyngier 		return -EBUSY;
3519ed24f4bSMarc Zyngier 
352f502cc56SSean Christopherson 	if (id >= kvm->max_vcpus)
3539ed24f4bSMarc Zyngier 		return -EINVAL;
3549ed24f4bSMarc Zyngier 
3559ed24f4bSMarc Zyngier 	return 0;
3569ed24f4bSMarc Zyngier }
3579ed24f4bSMarc Zyngier 
3589ed24f4bSMarc Zyngier int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3599ed24f4bSMarc Zyngier {
3609ed24f4bSMarc Zyngier 	int err;
3619ed24f4bSMarc Zyngier 
3620acc7239SOliver Upton 	spin_lock_init(&vcpu->arch.mp_state_lock);
3630acc7239SOliver Upton 
364c43120afSOliver Upton #ifdef CONFIG_LOCKDEP
365c43120afSOliver Upton 	/* Inform lockdep that the config_lock is acquired after vcpu->mutex */
366c43120afSOliver Upton 	mutex_lock(&vcpu->mutex);
367c43120afSOliver Upton 	mutex_lock(&vcpu->kvm->arch.config_lock);
368c43120afSOliver Upton 	mutex_unlock(&vcpu->kvm->arch.config_lock);
369c43120afSOliver Upton 	mutex_unlock(&vcpu->mutex);
370c43120afSOliver Upton #endif
371c43120afSOliver Upton 
3729ed24f4bSMarc Zyngier 	/* Force users to call KVM_ARM_VCPU_INIT */
373ef984060SOliver Upton 	vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
3749ed24f4bSMarc Zyngier 
375e539451bSSean Christopherson 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
376e539451bSSean Christopherson 
377b4da9187SMarc Zyngier 	/*
378b4da9187SMarc Zyngier 	 * Default value for the FP state, will be overloaded at load
379b4da9187SMarc Zyngier 	 * time if we support FP (pretty likely)
380b4da9187SMarc Zyngier 	 */
381b4da9187SMarc Zyngier 	vcpu->arch.fp_state = FP_STATE_FREE;
382b4da9187SMarc Zyngier 
3839ed24f4bSMarc Zyngier 	/* Set up the timer */
3849ed24f4bSMarc Zyngier 	kvm_timer_vcpu_init(vcpu);
3859ed24f4bSMarc Zyngier 
3869ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_init(vcpu);
3879ed24f4bSMarc Zyngier 
3889ed24f4bSMarc Zyngier 	kvm_arm_reset_debug_ptr(vcpu);
3899ed24f4bSMarc Zyngier 
3909ed24f4bSMarc Zyngier 	kvm_arm_pvtime_vcpu_init(&vcpu->arch);
3919ed24f4bSMarc Zyngier 
392a0e50aa3SChristoffer Dall 	vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
393a0e50aa3SChristoffer Dall 
3949ed24f4bSMarc Zyngier 	err = kvm_vgic_vcpu_init(vcpu);
3959ed24f4bSMarc Zyngier 	if (err)
3969ed24f4bSMarc Zyngier 		return err;
3979ed24f4bSMarc Zyngier 
3983f868e14SQuentin Perret 	return kvm_share_hyp(vcpu, vcpu + 1);
3999ed24f4bSMarc Zyngier }
4009ed24f4bSMarc Zyngier 
4019ed24f4bSMarc Zyngier void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
4029ed24f4bSMarc Zyngier {
4039ed24f4bSMarc Zyngier }
4049ed24f4bSMarc Zyngier 
4059ed24f4bSMarc Zyngier void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
4069ed24f4bSMarc Zyngier {
407cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
4089ed24f4bSMarc Zyngier 		static_branch_dec(&userspace_irqchip_in_use);
4099ed24f4bSMarc Zyngier 
4109af3e08bSWill Deacon 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
4119ed24f4bSMarc Zyngier 	kvm_timer_vcpu_terminate(vcpu);
4129ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_destroy(vcpu);
41302e3858fSMarc Zyngier 	kvm_vgic_vcpu_destroy(vcpu);
4149ed24f4bSMarc Zyngier 	kvm_arm_vcpu_destroy(vcpu);
4159ed24f4bSMarc Zyngier }
4169ed24f4bSMarc Zyngier 
4179ed24f4bSMarc Zyngier void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
4189ed24f4bSMarc Zyngier {
4196109c5a6SSean Christopherson 
4209ed24f4bSMarc Zyngier }
4219ed24f4bSMarc Zyngier 
4229ed24f4bSMarc Zyngier void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
4239ed24f4bSMarc Zyngier {
4246109c5a6SSean Christopherson 
4259ed24f4bSMarc Zyngier }
4269ed24f4bSMarc Zyngier 
4279ed24f4bSMarc Zyngier void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
4289ed24f4bSMarc Zyngier {
429a0e50aa3SChristoffer Dall 	struct kvm_s2_mmu *mmu;
4309ed24f4bSMarc Zyngier 	int *last_ran;
4319ed24f4bSMarc Zyngier 
432a0e50aa3SChristoffer Dall 	mmu = vcpu->arch.hw_mmu;
433a0e50aa3SChristoffer Dall 	last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
4349ed24f4bSMarc Zyngier 
4359ed24f4bSMarc Zyngier 	/*
43601dc9262SMarc Zyngier 	 * We guarantee that both TLBs and I-cache are private to each
43701dc9262SMarc Zyngier 	 * vcpu. If detecting that a vcpu from the same VM has
43801dc9262SMarc Zyngier 	 * previously run on the same physical CPU, call into the
43901dc9262SMarc Zyngier 	 * hypervisor code to nuke the relevant contexts.
44001dc9262SMarc Zyngier 	 *
4419ed24f4bSMarc Zyngier 	 * We might get preempted before the vCPU actually runs, but
4429ed24f4bSMarc Zyngier 	 * over-invalidation doesn't affect correctness.
4439ed24f4bSMarc Zyngier 	 */
4445f4bd815SMarc Zyngier 	if (*last_ran != vcpu->vcpu_idx) {
44501dc9262SMarc Zyngier 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
4465f4bd815SMarc Zyngier 		*last_ran = vcpu->vcpu_idx;
4479ed24f4bSMarc Zyngier 	}
4489ed24f4bSMarc Zyngier 
4499ed24f4bSMarc Zyngier 	vcpu->cpu = cpu;
4509ed24f4bSMarc Zyngier 
4519ed24f4bSMarc Zyngier 	kvm_vgic_load(vcpu);
4529ed24f4bSMarc Zyngier 	kvm_timer_vcpu_load(vcpu);
45313aeb9b4SDavid Brazdil 	if (has_vhe())
45427cde4c0SOliver Upton 		kvm_vcpu_load_vhe(vcpu);
4559ed24f4bSMarc Zyngier 	kvm_arch_vcpu_load_fp(vcpu);
4569ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_guest(vcpu);
4579ed24f4bSMarc Zyngier 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
4589ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
4599ed24f4bSMarc Zyngier 
4609ed24f4bSMarc Zyngier 	if (single_task_running())
4619ed24f4bSMarc Zyngier 		vcpu_clear_wfx_traps(vcpu);
4629ed24f4bSMarc Zyngier 	else
4639ed24f4bSMarc Zyngier 		vcpu_set_wfx_traps(vcpu);
4649ed24f4bSMarc Zyngier 
46529eb5a3cSMarc Zyngier 	if (vcpu_has_ptrauth(vcpu))
466ef3e40a7SMarc Zyngier 		vcpu_ptrauth_disable(vcpu);
467d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_load_debug_state_flags(vcpu);
468583cda1bSAlexandru Elisei 
4691ba11daeSShaoqin Huang 	if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
470583cda1bSAlexandru Elisei 		vcpu_set_on_unsupported_cpu(vcpu);
4719ed24f4bSMarc Zyngier }
4729ed24f4bSMarc Zyngier 
4739ed24f4bSMarc Zyngier void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4749ed24f4bSMarc Zyngier {
475d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_put_debug_state_flags(vcpu);
4769ed24f4bSMarc Zyngier 	kvm_arch_vcpu_put_fp(vcpu);
47713aeb9b4SDavid Brazdil 	if (has_vhe())
47827cde4c0SOliver Upton 		kvm_vcpu_put_vhe(vcpu);
4799ed24f4bSMarc Zyngier 	kvm_timer_vcpu_put(vcpu);
4809ed24f4bSMarc Zyngier 	kvm_vgic_put(vcpu);
4819ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_host(vcpu);
482100b4f09SShameer Kolothum 	kvm_arm_vmid_clear_active();
4839ed24f4bSMarc Zyngier 
484583cda1bSAlexandru Elisei 	vcpu_clear_on_unsupported_cpu(vcpu);
4859ed24f4bSMarc Zyngier 	vcpu->cpu = -1;
4869ed24f4bSMarc Zyngier }
4879ed24f4bSMarc Zyngier 
4880acc7239SOliver Upton static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
4899ed24f4bSMarc Zyngier {
4900acc7239SOliver Upton 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
4919ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
4929ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
4939ed24f4bSMarc Zyngier }
4949ed24f4bSMarc Zyngier 
4950acc7239SOliver Upton void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
4960acc7239SOliver Upton {
4970acc7239SOliver Upton 	spin_lock(&vcpu->arch.mp_state_lock);
4980acc7239SOliver Upton 	__kvm_arm_vcpu_power_off(vcpu);
4990acc7239SOliver Upton 	spin_unlock(&vcpu->arch.mp_state_lock);
5000acc7239SOliver Upton }
5010acc7239SOliver Upton 
502b171f9bbSOliver Upton bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
503b171f9bbSOliver Upton {
5040acc7239SOliver Upton 	return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED;
505b171f9bbSOliver Upton }
506b171f9bbSOliver Upton 
5077b33a09dSOliver Upton static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
5087b33a09dSOliver Upton {
5090acc7239SOliver Upton 	WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED);
5107b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
5117b33a09dSOliver Upton 	kvm_vcpu_kick(vcpu);
5127b33a09dSOliver Upton }
5137b33a09dSOliver Upton 
5147b33a09dSOliver Upton static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
5157b33a09dSOliver Upton {
5160acc7239SOliver Upton 	return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
5177b33a09dSOliver Upton }
5187b33a09dSOliver Upton 
5199ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
5209ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
5219ed24f4bSMarc Zyngier {
5220acc7239SOliver Upton 	*mp_state = READ_ONCE(vcpu->arch.mp_state);
5239ed24f4bSMarc Zyngier 
5249ed24f4bSMarc Zyngier 	return 0;
5259ed24f4bSMarc Zyngier }
5269ed24f4bSMarc Zyngier 
5279ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
5289ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
5299ed24f4bSMarc Zyngier {
5309ed24f4bSMarc Zyngier 	int ret = 0;
5319ed24f4bSMarc Zyngier 
5320acc7239SOliver Upton 	spin_lock(&vcpu->arch.mp_state_lock);
5330acc7239SOliver Upton 
5349ed24f4bSMarc Zyngier 	switch (mp_state->mp_state) {
5359ed24f4bSMarc Zyngier 	case KVM_MP_STATE_RUNNABLE:
5360acc7239SOliver Upton 		WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
5379ed24f4bSMarc Zyngier 		break;
5389ed24f4bSMarc Zyngier 	case KVM_MP_STATE_STOPPED:
5390acc7239SOliver Upton 		__kvm_arm_vcpu_power_off(vcpu);
5409ed24f4bSMarc Zyngier 		break;
5417b33a09dSOliver Upton 	case KVM_MP_STATE_SUSPENDED:
5427b33a09dSOliver Upton 		kvm_arm_vcpu_suspend(vcpu);
5439ed24f4bSMarc Zyngier 		break;
5449ed24f4bSMarc Zyngier 	default:
5459ed24f4bSMarc Zyngier 		ret = -EINVAL;
5469ed24f4bSMarc Zyngier 	}
5479ed24f4bSMarc Zyngier 
5480acc7239SOliver Upton 	spin_unlock(&vcpu->arch.mp_state_lock);
5490acc7239SOliver Upton 
5509ed24f4bSMarc Zyngier 	return ret;
5519ed24f4bSMarc Zyngier }
5529ed24f4bSMarc Zyngier 
5539ed24f4bSMarc Zyngier /**
5549ed24f4bSMarc Zyngier  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
5559ed24f4bSMarc Zyngier  * @v:		The VCPU pointer
5569ed24f4bSMarc Zyngier  *
5579ed24f4bSMarc Zyngier  * If the guest CPU is not waiting for interrupts or an interrupt line is
5589ed24f4bSMarc Zyngier  * asserted, the CPU is by definition runnable.
5599ed24f4bSMarc Zyngier  */
5609ed24f4bSMarc Zyngier int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
5619ed24f4bSMarc Zyngier {
5629ed24f4bSMarc Zyngier 	bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
5639ed24f4bSMarc Zyngier 	return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
564b171f9bbSOliver Upton 		&& !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
5659ed24f4bSMarc Zyngier }
5669ed24f4bSMarc Zyngier 
5679ed24f4bSMarc Zyngier bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
5689ed24f4bSMarc Zyngier {
5699ed24f4bSMarc Zyngier 	return vcpu_mode_priv(vcpu);
5709ed24f4bSMarc Zyngier }
5719ed24f4bSMarc Zyngier 
5728e5b0adeSLinus Torvalds #ifdef CONFIG_GUEST_PERF_EVENTS
573e1bfc245SSean Christopherson unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
574e1bfc245SSean Christopherson {
575e1bfc245SSean Christopherson 	return *vcpu_pc(vcpu);
576e1bfc245SSean Christopherson }
5778e5b0adeSLinus Torvalds #endif
578e1bfc245SSean Christopherson 
579b5aa368aSMarc Zyngier static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
580052f064dSMarc Zyngier {
581ef984060SOliver Upton 	return vcpu_get_flag(vcpu, VCPU_INITIALIZED);
582052f064dSMarc Zyngier }
583052f064dSMarc Zyngier 
5845544750eSMarc Zyngier static void kvm_init_mpidr_data(struct kvm *kvm)
5855544750eSMarc Zyngier {
5865544750eSMarc Zyngier 	struct kvm_mpidr_data *data = NULL;
5875544750eSMarc Zyngier 	unsigned long c, mask, nr_entries;
5885544750eSMarc Zyngier 	u64 aff_set = 0, aff_clr = ~0UL;
5895544750eSMarc Zyngier 	struct kvm_vcpu *vcpu;
5905544750eSMarc Zyngier 
5915544750eSMarc Zyngier 	mutex_lock(&kvm->arch.config_lock);
5925544750eSMarc Zyngier 
5935544750eSMarc Zyngier 	if (kvm->arch.mpidr_data || atomic_read(&kvm->online_vcpus) == 1)
5945544750eSMarc Zyngier 		goto out;
5955544750eSMarc Zyngier 
5965544750eSMarc Zyngier 	kvm_for_each_vcpu(c, vcpu, kvm) {
5975544750eSMarc Zyngier 		u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
5985544750eSMarc Zyngier 		aff_set |= aff;
5995544750eSMarc Zyngier 		aff_clr &= aff;
6005544750eSMarc Zyngier 	}
6015544750eSMarc Zyngier 
6025544750eSMarc Zyngier 	/*
6035544750eSMarc Zyngier 	 * A significant bit can be either 0 or 1, and will only appear in
6045544750eSMarc Zyngier 	 * aff_set. Use aff_clr to weed out the useless stuff.
6055544750eSMarc Zyngier 	 */
6065544750eSMarc Zyngier 	mask = aff_set ^ aff_clr;
6075544750eSMarc Zyngier 	nr_entries = BIT_ULL(hweight_long(mask));
6085544750eSMarc Zyngier 
6095544750eSMarc Zyngier 	/*
6105544750eSMarc Zyngier 	 * Don't let userspace fool us. If we need more than a single page
6115544750eSMarc Zyngier 	 * to describe the compressed MPIDR array, just fall back to the
6125544750eSMarc Zyngier 	 * iterative method. Single vcpu VMs do not need this either.
6135544750eSMarc Zyngier 	 */
6145544750eSMarc Zyngier 	if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE)
6155544750eSMarc Zyngier 		data = kzalloc(struct_size(data, cmpidr_to_idx, nr_entries),
6165544750eSMarc Zyngier 			       GFP_KERNEL_ACCOUNT);
6175544750eSMarc Zyngier 
6185544750eSMarc Zyngier 	if (!data)
6195544750eSMarc Zyngier 		goto out;
6205544750eSMarc Zyngier 
6215544750eSMarc Zyngier 	data->mpidr_mask = mask;
6225544750eSMarc Zyngier 
6235544750eSMarc Zyngier 	kvm_for_each_vcpu(c, vcpu, kvm) {
6245544750eSMarc Zyngier 		u64 aff = kvm_vcpu_get_mpidr_aff(vcpu);
6255544750eSMarc Zyngier 		u16 index = kvm_mpidr_index(data, aff);
6265544750eSMarc Zyngier 
6275544750eSMarc Zyngier 		data->cmpidr_to_idx[index] = c;
6285544750eSMarc Zyngier 	}
6295544750eSMarc Zyngier 
6305544750eSMarc Zyngier 	kvm->arch.mpidr_data = data;
6315544750eSMarc Zyngier out:
6325544750eSMarc Zyngier 	mutex_unlock(&kvm->arch.config_lock);
6335544750eSMarc Zyngier }
6345544750eSMarc Zyngier 
635b5aa368aSMarc Zyngier /*
636b5aa368aSMarc Zyngier  * Handle both the initialisation that is being done when the vcpu is
637b5aa368aSMarc Zyngier  * run for the first time, as well as the updates that must be
638b5aa368aSMarc Zyngier  * performed each time we get a new thread dealing with this vcpu.
639b5aa368aSMarc Zyngier  */
640b5aa368aSMarc Zyngier int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
6419ed24f4bSMarc Zyngier {
6429ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
643b5aa368aSMarc Zyngier 	int ret;
6449ed24f4bSMarc Zyngier 
645b5aa368aSMarc Zyngier 	if (!kvm_vcpu_initialized(vcpu))
646b5aa368aSMarc Zyngier 		return -ENOEXEC;
6479ed24f4bSMarc Zyngier 
6489ed24f4bSMarc Zyngier 	if (!kvm_arm_vcpu_is_finalized(vcpu))
6499ed24f4bSMarc Zyngier 		return -EPERM;
6509ed24f4bSMarc Zyngier 
651b5aa368aSMarc Zyngier 	ret = kvm_arch_vcpu_run_map_fp(vcpu);
652b5aa368aSMarc Zyngier 	if (ret)
653b5aa368aSMarc Zyngier 		return ret;
654b5aa368aSMarc Zyngier 
655cc5705fbSMarc Zyngier 	if (likely(vcpu_has_run_once(vcpu)))
656b5aa368aSMarc Zyngier 		return 0;
6579ed24f4bSMarc Zyngier 
6585544750eSMarc Zyngier 	kvm_init_mpidr_data(kvm);
6595544750eSMarc Zyngier 
660263d6287SAlexandru Elisei 	kvm_arm_vcpu_init_debug(vcpu);
661263d6287SAlexandru Elisei 
6629ed24f4bSMarc Zyngier 	if (likely(irqchip_in_kernel(kvm))) {
6639ed24f4bSMarc Zyngier 		/*
6649ed24f4bSMarc Zyngier 		 * Map the VGIC hardware resources before running a vcpu the
6659ed24f4bSMarc Zyngier 		 * first time on this VM.
6669ed24f4bSMarc Zyngier 		 */
6679ed24f4bSMarc Zyngier 		ret = kvm_vgic_map_resources(kvm);
6689ed24f4bSMarc Zyngier 		if (ret)
6699ed24f4bSMarc Zyngier 			return ret;
6709ed24f4bSMarc Zyngier 	}
6719ed24f4bSMarc Zyngier 
6723ed0b512SMarc Zyngier 	if (vcpu_has_nv(vcpu)) {
6733ed0b512SMarc Zyngier 		ret = kvm_init_nv_sysregs(vcpu->kvm);
6743ed0b512SMarc Zyngier 		if (ret)
6753ed0b512SMarc Zyngier 			return ret;
6763ed0b512SMarc Zyngier 	}
6773ed0b512SMarc Zyngier 
678*c5bac1efSMarc Zyngier 	/*
679*c5bac1efSMarc Zyngier 	 * This needs to happen after NV has imposed its own restrictions on
680*c5bac1efSMarc Zyngier 	 * the feature set
681*c5bac1efSMarc Zyngier 	 */
682*c5bac1efSMarc Zyngier 	kvm_init_sysreg(vcpu);
683*c5bac1efSMarc Zyngier 
6849ed24f4bSMarc Zyngier 	ret = kvm_timer_enable(vcpu);
6859ed24f4bSMarc Zyngier 	if (ret)
6869ed24f4bSMarc Zyngier 		return ret;
6879ed24f4bSMarc Zyngier 
6889ed24f4bSMarc Zyngier 	ret = kvm_arm_pmu_v3_enable(vcpu);
6891408e73dSMarc Zyngier 	if (ret)
6901408e73dSMarc Zyngier 		return ret;
6911408e73dSMarc Zyngier 
6929d0c063aSFuad Tabba 	if (is_protected_kvm_enabled()) {
6939d0c063aSFuad Tabba 		ret = pkvm_create_hyp_vm(kvm);
6949d0c063aSFuad Tabba 		if (ret)
6959d0c063aSFuad Tabba 			return ret;
6969d0c063aSFuad Tabba 	}
6979d0c063aSFuad Tabba 
6981408e73dSMarc Zyngier 	if (!irqchip_in_kernel(kvm)) {
6991408e73dSMarc Zyngier 		/*
7001408e73dSMarc Zyngier 		 * Tell the rest of the code that there are userspace irqchip
7011408e73dSMarc Zyngier 		 * VMs in the wild.
7021408e73dSMarc Zyngier 		 */
7031408e73dSMarc Zyngier 		static_branch_inc(&userspace_irqchip_in_use);
7041408e73dSMarc Zyngier 	}
7059ed24f4bSMarc Zyngier 
7062a0c3433SFuad Tabba 	/*
7072a0c3433SFuad Tabba 	 * Initialize traps for protected VMs.
7082a0c3433SFuad Tabba 	 * NOTE: Move to run in EL2 directly, rather than via a hypercall, once
7092a0c3433SFuad Tabba 	 * the code is in place for first run initialization at EL2.
7102a0c3433SFuad Tabba 	 */
7112a0c3433SFuad Tabba 	if (kvm_vm_is_protected(kvm))
7122a0c3433SFuad Tabba 		kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu);
7132a0c3433SFuad Tabba 
7144bba7f7dSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
71506394531SMarc Zyngier 	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
7164bba7f7dSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
7175177fe91SMarc Zyngier 
7189ed24f4bSMarc Zyngier 	return ret;
7199ed24f4bSMarc Zyngier }
7209ed24f4bSMarc Zyngier 
7219ed24f4bSMarc Zyngier bool kvm_arch_intc_initialized(struct kvm *kvm)
7229ed24f4bSMarc Zyngier {
7239ed24f4bSMarc Zyngier 	return vgic_initialized(kvm);
7249ed24f4bSMarc Zyngier }
7259ed24f4bSMarc Zyngier 
7269ed24f4bSMarc Zyngier void kvm_arm_halt_guest(struct kvm *kvm)
7279ed24f4bSMarc Zyngier {
72846808a4cSMarc Zyngier 	unsigned long i;
7299ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
7309ed24f4bSMarc Zyngier 
7319ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm)
7329ed24f4bSMarc Zyngier 		vcpu->arch.pause = true;
7339ed24f4bSMarc Zyngier 	kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
7349ed24f4bSMarc Zyngier }
7359ed24f4bSMarc Zyngier 
7369ed24f4bSMarc Zyngier void kvm_arm_resume_guest(struct kvm *kvm)
7379ed24f4bSMarc Zyngier {
73846808a4cSMarc Zyngier 	unsigned long i;
7399ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
7409ed24f4bSMarc Zyngier 
7419ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
7429ed24f4bSMarc Zyngier 		vcpu->arch.pause = false;
743d92a5d1cSSean Christopherson 		__kvm_vcpu_wake_up(vcpu);
7449ed24f4bSMarc Zyngier 	}
7459ed24f4bSMarc Zyngier }
7469ed24f4bSMarc Zyngier 
7471c6219e3SOliver Upton static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
7489ed24f4bSMarc Zyngier {
74938060944SPaolo Bonzini 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
7509ed24f4bSMarc Zyngier 
75138060944SPaolo Bonzini 	rcuwait_wait_event(wait,
752b171f9bbSOliver Upton 			   (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
75338060944SPaolo Bonzini 			   TASK_INTERRUPTIBLE);
7549ed24f4bSMarc Zyngier 
755b171f9bbSOliver Upton 	if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
7569ed24f4bSMarc Zyngier 		/* Awaken to handle a signal, request we sleep again later. */
7579ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_SLEEP, vcpu);
7589ed24f4bSMarc Zyngier 	}
7599ed24f4bSMarc Zyngier 
7609ed24f4bSMarc Zyngier 	/*
7619ed24f4bSMarc Zyngier 	 * Make sure we will observe a potential reset request if we've
7629ed24f4bSMarc Zyngier 	 * observed a change to the power state. Pairs with the smp_wmb() in
7639ed24f4bSMarc Zyngier 	 * kvm_psci_vcpu_on().
7649ed24f4bSMarc Zyngier 	 */
7659ed24f4bSMarc Zyngier 	smp_rmb();
7669ed24f4bSMarc Zyngier }
7679ed24f4bSMarc Zyngier 
7686109c5a6SSean Christopherson /**
7696109c5a6SSean Christopherson  * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
7706109c5a6SSean Christopherson  * @vcpu:	The VCPU pointer
7716109c5a6SSean Christopherson  *
7726109c5a6SSean Christopherson  * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
7736109c5a6SSean Christopherson  * the vCPU is runnable.  The vCPU may or may not be scheduled out, depending
7746109c5a6SSean Christopherson  * on when a wake event arrives, e.g. there may already be a pending wake event.
7756109c5a6SSean Christopherson  */
7766109c5a6SSean Christopherson void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
7776109c5a6SSean Christopherson {
7786109c5a6SSean Christopherson 	/*
7796109c5a6SSean Christopherson 	 * Sync back the state of the GIC CPU interface so that we have
7806109c5a6SSean Christopherson 	 * the latest PMR and group enables. This ensures that
7816109c5a6SSean Christopherson 	 * kvm_arch_vcpu_runnable has up-to-date data to decide whether
7826109c5a6SSean Christopherson 	 * we have pending interrupts, e.g. when determining if the
7836109c5a6SSean Christopherson 	 * vCPU should block.
7846109c5a6SSean Christopherson 	 *
7856109c5a6SSean Christopherson 	 * For the same reason, we want to tell GICv4 that we need
7866109c5a6SSean Christopherson 	 * doorbells to be signalled, should an interrupt become pending.
7876109c5a6SSean Christopherson 	 */
7886109c5a6SSean Christopherson 	preempt_disable();
7896109c5a6SSean Christopherson 	kvm_vgic_vmcr_sync(vcpu);
790b321c31cSMarc Zyngier 	vcpu_set_flag(vcpu, IN_WFI);
791b321c31cSMarc Zyngier 	vgic_v4_put(vcpu);
7926109c5a6SSean Christopherson 	preempt_enable();
7936109c5a6SSean Christopherson 
79491b99ea7SSean Christopherson 	kvm_vcpu_halt(vcpu);
795eebc538dSMarc Zyngier 	vcpu_clear_flag(vcpu, IN_WFIT);
7966109c5a6SSean Christopherson 
7976109c5a6SSean Christopherson 	preempt_disable();
798b321c31cSMarc Zyngier 	vcpu_clear_flag(vcpu, IN_WFI);
7996109c5a6SSean Christopherson 	vgic_v4_load(vcpu);
8006109c5a6SSean Christopherson 	preempt_enable();
8016109c5a6SSean Christopherson }
8026109c5a6SSean Christopherson 
8037b33a09dSOliver Upton static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
8047b33a09dSOliver Upton {
8057b33a09dSOliver Upton 	if (!kvm_arm_vcpu_suspended(vcpu))
8067b33a09dSOliver Upton 		return 1;
8077b33a09dSOliver Upton 
8087b33a09dSOliver Upton 	kvm_vcpu_wfi(vcpu);
8097b33a09dSOliver Upton 
8107b33a09dSOliver Upton 	/*
8117b33a09dSOliver Upton 	 * The suspend state is sticky; we do not leave it until userspace
8127b33a09dSOliver Upton 	 * explicitly marks the vCPU as runnable. Request that we suspend again
8137b33a09dSOliver Upton 	 * later.
8147b33a09dSOliver Upton 	 */
8157b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
8167b33a09dSOliver Upton 
8177b33a09dSOliver Upton 	/*
8187b33a09dSOliver Upton 	 * Check to make sure the vCPU is actually runnable. If so, exit to
8197b33a09dSOliver Upton 	 * userspace informing it of the wakeup condition.
8207b33a09dSOliver Upton 	 */
8217b33a09dSOliver Upton 	if (kvm_arch_vcpu_runnable(vcpu)) {
8227b33a09dSOliver Upton 		memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
8237b33a09dSOliver Upton 		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
8247b33a09dSOliver Upton 		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8257b33a09dSOliver Upton 		return 0;
8267b33a09dSOliver Upton 	}
8277b33a09dSOliver Upton 
8287b33a09dSOliver Upton 	/*
8297b33a09dSOliver Upton 	 * Otherwise, we were unblocked to process a different event, such as a
8307b33a09dSOliver Upton 	 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
8317b33a09dSOliver Upton 	 * process the event.
8327b33a09dSOliver Upton 	 */
8337b33a09dSOliver Upton 	return 1;
8347b33a09dSOliver Upton }
8357b33a09dSOliver Upton 
8363fdd0459SOliver Upton /**
8373fdd0459SOliver Upton  * check_vcpu_requests - check and handle pending vCPU requests
8383fdd0459SOliver Upton  * @vcpu:	the VCPU pointer
8393fdd0459SOliver Upton  *
8403fdd0459SOliver Upton  * Return: 1 if we should enter the guest
8413fdd0459SOliver Upton  *	   0 if we should exit to userspace
8423fdd0459SOliver Upton  *	   < 0 if we should exit to userspace, where the return value indicates
8433fdd0459SOliver Upton  *	   an error
8443fdd0459SOliver Upton  */
8453fdd0459SOliver Upton static int check_vcpu_requests(struct kvm_vcpu *vcpu)
8469ed24f4bSMarc Zyngier {
8479ed24f4bSMarc Zyngier 	if (kvm_request_pending(vcpu)) {
8489ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
8491c6219e3SOliver Upton 			kvm_vcpu_sleep(vcpu);
8509ed24f4bSMarc Zyngier 
8519ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
8529ed24f4bSMarc Zyngier 			kvm_reset_vcpu(vcpu);
8539ed24f4bSMarc Zyngier 
8549ed24f4bSMarc Zyngier 		/*
8559ed24f4bSMarc Zyngier 		 * Clear IRQ_PENDING requests that were made to guarantee
8569ed24f4bSMarc Zyngier 		 * that a VCPU sees new virtual interrupts.
8579ed24f4bSMarc Zyngier 		 */
8589ed24f4bSMarc Zyngier 		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
8599ed24f4bSMarc Zyngier 
8609ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
8619ed24f4bSMarc Zyngier 			kvm_update_stolen_time(vcpu);
8629ed24f4bSMarc Zyngier 
8639ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
8649ed24f4bSMarc Zyngier 			/* The distributor enable bits were changed */
8659ed24f4bSMarc Zyngier 			preempt_disable();
866b321c31cSMarc Zyngier 			vgic_v4_put(vcpu);
8679ed24f4bSMarc Zyngier 			vgic_v4_load(vcpu);
8689ed24f4bSMarc Zyngier 			preempt_enable();
8699ed24f4bSMarc Zyngier 		}
870d0c94c49SMarc Zyngier 
871d0c94c49SMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
87227131b19SRaghavendra Rao Ananta 			kvm_vcpu_reload_pmu(vcpu);
8737b33a09dSOliver Upton 
874b1f778a2SMarc Zyngier 		if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu))
875b1f778a2SMarc Zyngier 			kvm_vcpu_pmu_restore_guest(vcpu);
876b1f778a2SMarc Zyngier 
8777b33a09dSOliver Upton 		if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
8787b33a09dSOliver Upton 			return kvm_vcpu_suspend(vcpu);
8799cb1096fSGavin Shan 
8809cb1096fSGavin Shan 		if (kvm_dirty_ring_check_request(vcpu))
8819cb1096fSGavin Shan 			return 0;
8829ed24f4bSMarc Zyngier 	}
8833fdd0459SOliver Upton 
8843fdd0459SOliver Upton 	return 1;
8859ed24f4bSMarc Zyngier }
8869ed24f4bSMarc Zyngier 
8872f6a49bbSWill Deacon static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
8882f6a49bbSWill Deacon {
8892f6a49bbSWill Deacon 	if (likely(!vcpu_mode_is_32bit(vcpu)))
8902f6a49bbSWill Deacon 		return false;
8912f6a49bbSWill Deacon 
892a77b31dcSMarc Zyngier 	if (vcpu_has_nv(vcpu))
893a77b31dcSMarc Zyngier 		return true;
894a77b31dcSMarc Zyngier 
895f3c6efc7SOliver Upton 	return !kvm_supports_32bit_el0();
8962f6a49bbSWill Deacon }
8972f6a49bbSWill Deacon 
8989ed24f4bSMarc Zyngier /**
8996caa5812SOliver Upton  * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
9006caa5812SOliver Upton  * @vcpu:	The VCPU pointer
9016caa5812SOliver Upton  * @ret:	Pointer to write optional return code
9026caa5812SOliver Upton  *
9036caa5812SOliver Upton  * Returns: true if the VCPU needs to return to a preemptible + interruptible
9046caa5812SOliver Upton  *	    and skip guest entry.
9056caa5812SOliver Upton  *
9066caa5812SOliver Upton  * This function disambiguates between two different types of exits: exits to a
9076caa5812SOliver Upton  * preemptible + interruptible kernel context and exits to userspace. For an
9086caa5812SOliver Upton  * exit to userspace, this function will write the return code to ret and return
9096caa5812SOliver Upton  * true. For an exit to preemptible + interruptible kernel context (i.e. check
9106caa5812SOliver Upton  * for pending work and re-enter), return true without writing to ret.
9116caa5812SOliver Upton  */
9126caa5812SOliver Upton static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
9136caa5812SOliver Upton {
9146caa5812SOliver Upton 	struct kvm_run *run = vcpu->run;
9156caa5812SOliver Upton 
9166caa5812SOliver Upton 	/*
9176caa5812SOliver Upton 	 * If we're using a userspace irqchip, then check if we need
9186caa5812SOliver Upton 	 * to tell a userspace irqchip about timer or PMU level
9196caa5812SOliver Upton 	 * changes and if so, exit to userspace (the actual level
9206caa5812SOliver Upton 	 * state gets updated in kvm_timer_update_run and
9216caa5812SOliver Upton 	 * kvm_pmu_update_run below).
9226caa5812SOliver Upton 	 */
9236caa5812SOliver Upton 	if (static_branch_unlikely(&userspace_irqchip_in_use)) {
9246caa5812SOliver Upton 		if (kvm_timer_should_notify_user(vcpu) ||
9256caa5812SOliver Upton 		    kvm_pmu_should_notify_user(vcpu)) {
9266caa5812SOliver Upton 			*ret = -EINTR;
9276caa5812SOliver Upton 			run->exit_reason = KVM_EXIT_INTR;
9286caa5812SOliver Upton 			return true;
9296caa5812SOliver Upton 		}
9306caa5812SOliver Upton 	}
9316caa5812SOliver Upton 
932583cda1bSAlexandru Elisei 	if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
933583cda1bSAlexandru Elisei 		run->exit_reason = KVM_EXIT_FAIL_ENTRY;
934583cda1bSAlexandru Elisei 		run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
935583cda1bSAlexandru Elisei 		run->fail_entry.cpu = smp_processor_id();
936583cda1bSAlexandru Elisei 		*ret = 0;
937583cda1bSAlexandru Elisei 		return true;
938583cda1bSAlexandru Elisei 	}
939583cda1bSAlexandru Elisei 
9406caa5812SOliver Upton 	return kvm_request_pending(vcpu) ||
9416caa5812SOliver Upton 			xfer_to_guest_mode_work_pending();
9426caa5812SOliver Upton }
9436caa5812SOliver Upton 
9448cfe148aSMark Rutland /*
9458cfe148aSMark Rutland  * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
9468cfe148aSMark Rutland  * the vCPU is running.
9478cfe148aSMark Rutland  *
9488cfe148aSMark Rutland  * This must be noinstr as instrumentation may make use of RCU, and this is not
9498cfe148aSMark Rutland  * safe during the EQS.
9508cfe148aSMark Rutland  */
9518cfe148aSMark Rutland static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
9528cfe148aSMark Rutland {
9538cfe148aSMark Rutland 	int ret;
9548cfe148aSMark Rutland 
9558cfe148aSMark Rutland 	guest_state_enter_irqoff();
9568cfe148aSMark Rutland 	ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
9578cfe148aSMark Rutland 	guest_state_exit_irqoff();
9588cfe148aSMark Rutland 
9598cfe148aSMark Rutland 	return ret;
9608cfe148aSMark Rutland }
9618cfe148aSMark Rutland 
9626caa5812SOliver Upton /**
9639ed24f4bSMarc Zyngier  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
9649ed24f4bSMarc Zyngier  * @vcpu:	The VCPU pointer
9659ed24f4bSMarc Zyngier  *
9669ed24f4bSMarc Zyngier  * This function is called through the VCPU_RUN ioctl called from user space. It
9679ed24f4bSMarc Zyngier  * will execute VM code in a loop until the time slice for the process is used
9689ed24f4bSMarc Zyngier  * or some emulation is needed from user space in which case the function will
9699ed24f4bSMarc Zyngier  * return with return value 0 and with the kvm_run structure filled in with the
9709ed24f4bSMarc Zyngier  * required data for the requested emulation.
9719ed24f4bSMarc Zyngier  */
97238060944SPaolo Bonzini int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
9739ed24f4bSMarc Zyngier {
97438060944SPaolo Bonzini 	struct kvm_run *run = vcpu->run;
9759ed24f4bSMarc Zyngier 	int ret;
9769ed24f4bSMarc Zyngier 
9779ed24f4bSMarc Zyngier 	if (run->exit_reason == KVM_EXIT_MMIO) {
97874cc7e0cSTianjia Zhang 		ret = kvm_handle_mmio_return(vcpu);
9799ed24f4bSMarc Zyngier 		if (ret)
9809ed24f4bSMarc Zyngier 			return ret;
9819ed24f4bSMarc Zyngier 	}
9829ed24f4bSMarc Zyngier 
9839ed24f4bSMarc Zyngier 	vcpu_load(vcpu);
9849ed24f4bSMarc Zyngier 
985e3e880bbSZenghui Yu 	if (run->immediate_exit) {
986e3e880bbSZenghui Yu 		ret = -EINTR;
987e3e880bbSZenghui Yu 		goto out;
988e3e880bbSZenghui Yu 	}
989e3e880bbSZenghui Yu 
9909ed24f4bSMarc Zyngier 	kvm_sigset_activate(vcpu);
9919ed24f4bSMarc Zyngier 
9929ed24f4bSMarc Zyngier 	ret = 1;
9939ed24f4bSMarc Zyngier 	run->exit_reason = KVM_EXIT_UNKNOWN;
99418f3976fSAlexandru Elisei 	run->flags = 0;
9959ed24f4bSMarc Zyngier 	while (ret > 0) {
9969ed24f4bSMarc Zyngier 		/*
9979ed24f4bSMarc Zyngier 		 * Check conditions before entering the guest
9989ed24f4bSMarc Zyngier 		 */
9996caa5812SOliver Upton 		ret = xfer_to_guest_mode_handle_work(vcpu);
10006caa5812SOliver Upton 		if (!ret)
10016caa5812SOliver Upton 			ret = 1;
10029ed24f4bSMarc Zyngier 
10033fdd0459SOliver Upton 		if (ret > 0)
10043fdd0459SOliver Upton 			ret = check_vcpu_requests(vcpu);
10059ed24f4bSMarc Zyngier 
10069ed24f4bSMarc Zyngier 		/*
10079ed24f4bSMarc Zyngier 		 * Preparing the interrupts to be injected also
10089ed24f4bSMarc Zyngier 		 * involves poking the GIC, which must be done in a
10099ed24f4bSMarc Zyngier 		 * non-preemptible context.
10109ed24f4bSMarc Zyngier 		 */
10119ed24f4bSMarc Zyngier 		preempt_disable();
10129ed24f4bSMarc Zyngier 
10133248136bSJulien Grall 		/*
10143248136bSJulien Grall 		 * The VMID allocator only tracks active VMIDs per
10153248136bSJulien Grall 		 * physical CPU, and therefore the VMID allocated may not be
10163248136bSJulien Grall 		 * preserved on VMID roll-over if the task was preempted,
10173248136bSJulien Grall 		 * making a thread's VMID inactive. So we need to call
10183248136bSJulien Grall 		 * kvm_arm_vmid_update() in non-premptible context.
10193248136bSJulien Grall 		 */
10205eba523eSMarc Zyngier 		if (kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid) &&
10215eba523eSMarc Zyngier 		    has_vhe())
10225eba523eSMarc Zyngier 			__load_stage2(vcpu->arch.hw_mmu,
10235eba523eSMarc Zyngier 				      vcpu->arch.hw_mmu->arch);
10243248136bSJulien Grall 
10259ed24f4bSMarc Zyngier 		kvm_pmu_flush_hwstate(vcpu);
10269ed24f4bSMarc Zyngier 
10279ed24f4bSMarc Zyngier 		local_irq_disable();
10289ed24f4bSMarc Zyngier 
10299ed24f4bSMarc Zyngier 		kvm_vgic_flush_hwstate(vcpu);
10309ed24f4bSMarc Zyngier 
103184d751a0SFuad Tabba 		kvm_pmu_update_vcpu_events(vcpu);
103284d751a0SFuad Tabba 
10339ed24f4bSMarc Zyngier 		/*
10349ed24f4bSMarc Zyngier 		 * Ensure we set mode to IN_GUEST_MODE after we disable
10359ed24f4bSMarc Zyngier 		 * interrupts and before the final VCPU requests check.
10369ed24f4bSMarc Zyngier 		 * See the comment in kvm_vcpu_exiting_guest_mode() and
10379ed24f4bSMarc Zyngier 		 * Documentation/virt/kvm/vcpu-requests.rst
10389ed24f4bSMarc Zyngier 		 */
10399ed24f4bSMarc Zyngier 		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
10409ed24f4bSMarc Zyngier 
10416caa5812SOliver Upton 		if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
10429ed24f4bSMarc Zyngier 			vcpu->mode = OUTSIDE_GUEST_MODE;
10439ed24f4bSMarc Zyngier 			isb(); /* Ensure work in x_flush_hwstate is committed */
10449ed24f4bSMarc Zyngier 			kvm_pmu_sync_hwstate(vcpu);
10459ed24f4bSMarc Zyngier 			if (static_branch_unlikely(&userspace_irqchip_in_use))
10463c5ff0c6SMarc Zyngier 				kvm_timer_sync_user(vcpu);
10479ed24f4bSMarc Zyngier 			kvm_vgic_sync_hwstate(vcpu);
10489ed24f4bSMarc Zyngier 			local_irq_enable();
10499ed24f4bSMarc Zyngier 			preempt_enable();
10509ed24f4bSMarc Zyngier 			continue;
10519ed24f4bSMarc Zyngier 		}
10529ed24f4bSMarc Zyngier 
10539ed24f4bSMarc Zyngier 		kvm_arm_setup_debug(vcpu);
1054af9a0e21SMarc Zyngier 		kvm_arch_vcpu_ctxflush_fp(vcpu);
10559ed24f4bSMarc Zyngier 
10569ed24f4bSMarc Zyngier 		/**************************************************************
10579ed24f4bSMarc Zyngier 		 * Enter the guest
10589ed24f4bSMarc Zyngier 		 */
10599ed24f4bSMarc Zyngier 		trace_kvm_entry(*vcpu_pc(vcpu));
10608cfe148aSMark Rutland 		guest_timing_enter_irqoff();
10619ed24f4bSMarc Zyngier 
10628cfe148aSMark Rutland 		ret = kvm_arm_vcpu_enter_exit(vcpu);
10639ed24f4bSMarc Zyngier 
10649ed24f4bSMarc Zyngier 		vcpu->mode = OUTSIDE_GUEST_MODE;
10659ed24f4bSMarc Zyngier 		vcpu->stat.exits++;
10669ed24f4bSMarc Zyngier 		/*
10679ed24f4bSMarc Zyngier 		 * Back from guest
10689ed24f4bSMarc Zyngier 		 *************************************************************/
10699ed24f4bSMarc Zyngier 
10709ed24f4bSMarc Zyngier 		kvm_arm_clear_debug(vcpu);
10719ed24f4bSMarc Zyngier 
10729ed24f4bSMarc Zyngier 		/*
10739ed24f4bSMarc Zyngier 		 * We must sync the PMU state before the vgic state so
10749ed24f4bSMarc Zyngier 		 * that the vgic can properly sample the updated state of the
10759ed24f4bSMarc Zyngier 		 * interrupt line.
10769ed24f4bSMarc Zyngier 		 */
10779ed24f4bSMarc Zyngier 		kvm_pmu_sync_hwstate(vcpu);
10789ed24f4bSMarc Zyngier 
10799ed24f4bSMarc Zyngier 		/*
10809ed24f4bSMarc Zyngier 		 * Sync the vgic state before syncing the timer state because
10819ed24f4bSMarc Zyngier 		 * the timer code needs to know if the virtual timer
10829ed24f4bSMarc Zyngier 		 * interrupts are active.
10839ed24f4bSMarc Zyngier 		 */
10849ed24f4bSMarc Zyngier 		kvm_vgic_sync_hwstate(vcpu);
10859ed24f4bSMarc Zyngier 
10869ed24f4bSMarc Zyngier 		/*
10879ed24f4bSMarc Zyngier 		 * Sync the timer hardware state before enabling interrupts as
10889ed24f4bSMarc Zyngier 		 * we don't want vtimer interrupts to race with syncing the
10899ed24f4bSMarc Zyngier 		 * timer virtual interrupt state.
10909ed24f4bSMarc Zyngier 		 */
10919ed24f4bSMarc Zyngier 		if (static_branch_unlikely(&userspace_irqchip_in_use))
10923c5ff0c6SMarc Zyngier 			kvm_timer_sync_user(vcpu);
10939ed24f4bSMarc Zyngier 
10949ed24f4bSMarc Zyngier 		kvm_arch_vcpu_ctxsync_fp(vcpu);
10959ed24f4bSMarc Zyngier 
10969ed24f4bSMarc Zyngier 		/*
10978cfe148aSMark Rutland 		 * We must ensure that any pending interrupts are taken before
10988cfe148aSMark Rutland 		 * we exit guest timing so that timer ticks are accounted as
10998cfe148aSMark Rutland 		 * guest time. Transiently unmask interrupts so that any
11008cfe148aSMark Rutland 		 * pending interrupts are taken.
11019ed24f4bSMarc Zyngier 		 *
11028cfe148aSMark Rutland 		 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
11038cfe148aSMark Rutland 		 * context synchronization event) is necessary to ensure that
11048cfe148aSMark Rutland 		 * pending interrupts are taken.
11059ed24f4bSMarc Zyngier 		 */
1106f7659f8bSMarc Zyngier 		if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
11079ed24f4bSMarc Zyngier 			local_irq_enable();
11088cfe148aSMark Rutland 			isb();
11098cfe148aSMark Rutland 			local_irq_disable();
1110f7659f8bSMarc Zyngier 		}
11119ed24f4bSMarc Zyngier 
11128cfe148aSMark Rutland 		guest_timing_exit_irqoff();
11138cfe148aSMark Rutland 
11148cfe148aSMark Rutland 		local_irq_enable();
11158cfe148aSMark Rutland 
11169ed24f4bSMarc Zyngier 		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
11179ed24f4bSMarc Zyngier 
11189ed24f4bSMarc Zyngier 		/* Exit types that need handling before we can be preempted */
111974cc7e0cSTianjia Zhang 		handle_exit_early(vcpu, ret);
11209ed24f4bSMarc Zyngier 
11219ed24f4bSMarc Zyngier 		preempt_enable();
11229ed24f4bSMarc Zyngier 
112322f55384SQais Yousef 		/*
112422f55384SQais Yousef 		 * The ARMv8 architecture doesn't give the hypervisor
112522f55384SQais Yousef 		 * a mechanism to prevent a guest from dropping to AArch32 EL0
112622f55384SQais Yousef 		 * if implemented by the CPU. If we spot the guest in such
112722f55384SQais Yousef 		 * state and that we decided it wasn't supposed to do so (like
112822f55384SQais Yousef 		 * with the asymmetric AArch32 case), return to userspace with
112922f55384SQais Yousef 		 * a fatal error.
113022f55384SQais Yousef 		 */
11312f6a49bbSWill Deacon 		if (vcpu_mode_is_bad_32bit(vcpu)) {
113222f55384SQais Yousef 			/*
113322f55384SQais Yousef 			 * As we have caught the guest red-handed, decide that
113422f55384SQais Yousef 			 * it isn't fit for purpose anymore by making the vcpu
113522f55384SQais Yousef 			 * invalid. The VMM can try and fix it by issuing  a
113622f55384SQais Yousef 			 * KVM_ARM_VCPU_INIT if it really wants to.
113722f55384SQais Yousef 			 */
1138ef984060SOliver Upton 			vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
113922f55384SQais Yousef 			ret = ARM_EXCEPTION_IL;
114022f55384SQais Yousef 		}
114122f55384SQais Yousef 
114274cc7e0cSTianjia Zhang 		ret = handle_exit(vcpu, ret);
11439ed24f4bSMarc Zyngier 	}
11449ed24f4bSMarc Zyngier 
11459ed24f4bSMarc Zyngier 	/* Tell userspace about in-kernel device output levels */
11469ed24f4bSMarc Zyngier 	if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
11479ed24f4bSMarc Zyngier 		kvm_timer_update_run(vcpu);
11489ed24f4bSMarc Zyngier 		kvm_pmu_update_run(vcpu);
11499ed24f4bSMarc Zyngier 	}
11509ed24f4bSMarc Zyngier 
11519ed24f4bSMarc Zyngier 	kvm_sigset_deactivate(vcpu);
11529ed24f4bSMarc Zyngier 
1153e3e880bbSZenghui Yu out:
115426778aaaSMarc Zyngier 	/*
115526778aaaSMarc Zyngier 	 * In the unlikely event that we are returning to userspace
115626778aaaSMarc Zyngier 	 * with pending exceptions or PC adjustment, commit these
115726778aaaSMarc Zyngier 	 * adjustments in order to give userspace a consistent view of
115826778aaaSMarc Zyngier 	 * the vcpu state. Note that this relies on __kvm_adjust_pc()
115926778aaaSMarc Zyngier 	 * being preempt-safe on VHE.
116026778aaaSMarc Zyngier 	 */
1161699bb2e0SMarc Zyngier 	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1162699bb2e0SMarc Zyngier 		     vcpu_get_flag(vcpu, INCREMENT_PC)))
116326778aaaSMarc Zyngier 		kvm_call_hyp(__kvm_adjust_pc, vcpu);
116426778aaaSMarc Zyngier 
11659ed24f4bSMarc Zyngier 	vcpu_put(vcpu);
11669ed24f4bSMarc Zyngier 	return ret;
11679ed24f4bSMarc Zyngier }
11689ed24f4bSMarc Zyngier 
11699ed24f4bSMarc Zyngier static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
11709ed24f4bSMarc Zyngier {
11719ed24f4bSMarc Zyngier 	int bit_index;
11729ed24f4bSMarc Zyngier 	bool set;
11739ed24f4bSMarc Zyngier 	unsigned long *hcr;
11749ed24f4bSMarc Zyngier 
11759ed24f4bSMarc Zyngier 	if (number == KVM_ARM_IRQ_CPU_IRQ)
11769ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VI);
11779ed24f4bSMarc Zyngier 	else /* KVM_ARM_IRQ_CPU_FIQ */
11789ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VF);
11799ed24f4bSMarc Zyngier 
11809ed24f4bSMarc Zyngier 	hcr = vcpu_hcr(vcpu);
11819ed24f4bSMarc Zyngier 	if (level)
11829ed24f4bSMarc Zyngier 		set = test_and_set_bit(bit_index, hcr);
11839ed24f4bSMarc Zyngier 	else
11849ed24f4bSMarc Zyngier 		set = test_and_clear_bit(bit_index, hcr);
11859ed24f4bSMarc Zyngier 
11869ed24f4bSMarc Zyngier 	/*
11879ed24f4bSMarc Zyngier 	 * If we didn't change anything, no need to wake up or kick other CPUs
11889ed24f4bSMarc Zyngier 	 */
11899ed24f4bSMarc Zyngier 	if (set == level)
11909ed24f4bSMarc Zyngier 		return 0;
11919ed24f4bSMarc Zyngier 
11929ed24f4bSMarc Zyngier 	/*
11939ed24f4bSMarc Zyngier 	 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
11949ed24f4bSMarc Zyngier 	 * trigger a world-switch round on the running physical CPU to set the
11959ed24f4bSMarc Zyngier 	 * virtual IRQ/FIQ fields in the HCR appropriately.
11969ed24f4bSMarc Zyngier 	 */
11979ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
11989ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
11999ed24f4bSMarc Zyngier 
12009ed24f4bSMarc Zyngier 	return 0;
12019ed24f4bSMarc Zyngier }
12029ed24f4bSMarc Zyngier 
12039ed24f4bSMarc Zyngier int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
12049ed24f4bSMarc Zyngier 			  bool line_status)
12059ed24f4bSMarc Zyngier {
12069ed24f4bSMarc Zyngier 	u32 irq = irq_level->irq;
12079a0a75d3SMarc Zyngier 	unsigned int irq_type, vcpu_id, irq_num;
12089ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = NULL;
12099ed24f4bSMarc Zyngier 	bool level = irq_level->level;
12109ed24f4bSMarc Zyngier 
12119ed24f4bSMarc Zyngier 	irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
12129a0a75d3SMarc Zyngier 	vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
12139a0a75d3SMarc Zyngier 	vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
12149ed24f4bSMarc Zyngier 	irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
12159ed24f4bSMarc Zyngier 
12169a0a75d3SMarc Zyngier 	trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level);
12179ed24f4bSMarc Zyngier 
12189ed24f4bSMarc Zyngier 	switch (irq_type) {
12199ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_CPU:
12209ed24f4bSMarc Zyngier 		if (irqchip_in_kernel(kvm))
12219ed24f4bSMarc Zyngier 			return -ENXIO;
12229ed24f4bSMarc Zyngier 
12239a0a75d3SMarc Zyngier 		vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
12249ed24f4bSMarc Zyngier 		if (!vcpu)
12259ed24f4bSMarc Zyngier 			return -EINVAL;
12269ed24f4bSMarc Zyngier 
12279ed24f4bSMarc Zyngier 		if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
12289ed24f4bSMarc Zyngier 			return -EINVAL;
12299ed24f4bSMarc Zyngier 
12309ed24f4bSMarc Zyngier 		return vcpu_interrupt_line(vcpu, irq_num, level);
12319ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_PPI:
12329ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
12339ed24f4bSMarc Zyngier 			return -ENXIO;
12349ed24f4bSMarc Zyngier 
12359a0a75d3SMarc Zyngier 		vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
12369ed24f4bSMarc Zyngier 		if (!vcpu)
12379ed24f4bSMarc Zyngier 			return -EINVAL;
12389ed24f4bSMarc Zyngier 
12399ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
12409ed24f4bSMarc Zyngier 			return -EINVAL;
12419ed24f4bSMarc Zyngier 
12429a0a75d3SMarc Zyngier 		return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL);
12439ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_SPI:
12449ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
12459ed24f4bSMarc Zyngier 			return -ENXIO;
12469ed24f4bSMarc Zyngier 
12479ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_PRIVATE_IRQS)
12489ed24f4bSMarc Zyngier 			return -EINVAL;
12499ed24f4bSMarc Zyngier 
12509a0a75d3SMarc Zyngier 		return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL);
12519ed24f4bSMarc Zyngier 	}
12529ed24f4bSMarc Zyngier 
12539ed24f4bSMarc Zyngier 	return -EINVAL;
12549ed24f4bSMarc Zyngier }
12559ed24f4bSMarc Zyngier 
1256ef150908SOliver Upton static unsigned long system_supported_vcpu_features(void)
1257ef150908SOliver Upton {
1258ef150908SOliver Upton 	unsigned long features = KVM_VCPU_VALID_FEATURES;
1259ef150908SOliver Upton 
1260ef150908SOliver Upton 	if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1))
1261ef150908SOliver Upton 		clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features);
1262ef150908SOliver Upton 
12639116db11SOliver Upton 	if (!kvm_arm_support_pmu_v3())
12649116db11SOliver Upton 		clear_bit(KVM_ARM_VCPU_PMU_V3, &features);
12659116db11SOliver Upton 
1266be9c0c01SOliver Upton 	if (!system_supports_sve())
1267be9c0c01SOliver Upton 		clear_bit(KVM_ARM_VCPU_SVE, &features);
1268be9c0c01SOliver Upton 
1269baa28a53SOliver Upton 	if (!system_has_full_ptr_auth()) {
1270baa28a53SOliver Upton 		clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features);
1271baa28a53SOliver Upton 		clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features);
1272baa28a53SOliver Upton 	}
1273baa28a53SOliver Upton 
127412405b09SOliver Upton 	if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT))
127512405b09SOliver Upton 		clear_bit(KVM_ARM_VCPU_HAS_EL2, &features);
127612405b09SOliver Upton 
1277ef150908SOliver Upton 	return features;
1278ef150908SOliver Upton }
1279ef150908SOliver Upton 
1280a7a2c72aSOliver Upton static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,
12819ed24f4bSMarc Zyngier 					const struct kvm_vcpu_init *init)
12829ed24f4bSMarc Zyngier {
1283a7a2c72aSOliver Upton 	unsigned long features = init->features[0];
1284a7a2c72aSOliver Upton 	int i;
12859ed24f4bSMarc Zyngier 
1286a7a2c72aSOliver Upton 	if (features & ~KVM_VCPU_VALID_FEATURES)
12879ed24f4bSMarc Zyngier 		return -ENOENT;
12889ed24f4bSMarc Zyngier 
1289a7a2c72aSOliver Upton 	for (i = 1; i < ARRAY_SIZE(init->features); i++) {
1290a7a2c72aSOliver Upton 		if (init->features[i])
1291a7a2c72aSOliver Upton 			return -ENOENT;
12929ed24f4bSMarc Zyngier 	}
12939ed24f4bSMarc Zyngier 
1294ef150908SOliver Upton 	if (features & ~system_supported_vcpu_features())
1295ef150908SOliver Upton 		return -EINVAL;
1296ef150908SOliver Upton 
1297baa28a53SOliver Upton 	/*
1298baa28a53SOliver Upton 	 * For now make sure that both address/generic pointer authentication
1299baa28a53SOliver Upton 	 * features are requested by the userspace together.
1300baa28a53SOliver Upton 	 */
1301baa28a53SOliver Upton 	if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) !=
1302baa28a53SOliver Upton 	    test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features))
1303baa28a53SOliver Upton 		return -EINVAL;
1304baa28a53SOliver Upton 
1305d99fb82fSOliver Upton 	/* Disallow NV+SVE for the time being */
1306d99fb82fSOliver Upton 	if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features) &&
1307d99fb82fSOliver Upton 	    test_bit(KVM_ARM_VCPU_SVE, &features))
1308d99fb82fSOliver Upton 		return -EINVAL;
1309d99fb82fSOliver Upton 
13102251e9ffSOliver Upton 	if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features))
13112251e9ffSOliver Upton 		return 0;
13122251e9ffSOliver Upton 
13132251e9ffSOliver Upton 	/* MTE is incompatible with AArch32 */
13142251e9ffSOliver Upton 	if (kvm_has_mte(vcpu->kvm))
13152251e9ffSOliver Upton 		return -EINVAL;
13162251e9ffSOliver Upton 
13172251e9ffSOliver Upton 	/* NV is incompatible with AArch32 */
13182251e9ffSOliver Upton 	if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features))
13192251e9ffSOliver Upton 		return -EINVAL;
13202251e9ffSOliver Upton 
1321a7a2c72aSOliver Upton 	return 0;
1322a7a2c72aSOliver Upton }
1323a7a2c72aSOliver Upton 
1324a7a2c72aSOliver Upton static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
1325a7a2c72aSOliver Upton 				  const struct kvm_vcpu_init *init)
1326a7a2c72aSOliver Upton {
1327a7a2c72aSOliver Upton 	unsigned long features = init->features[0];
1328a7a2c72aSOliver Upton 
13291de10b7dSOliver Upton 	return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features,
13301de10b7dSOliver Upton 			     KVM_VCPU_MAX_FEATURES);
1331a7a2c72aSOliver Upton }
1332a7a2c72aSOliver Upton 
133342773357SReiji Watanabe static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
133442773357SReiji Watanabe {
133542773357SReiji Watanabe 	struct kvm *kvm = vcpu->kvm;
133642773357SReiji Watanabe 	int ret = 0;
133742773357SReiji Watanabe 
133842773357SReiji Watanabe 	/*
133942773357SReiji Watanabe 	 * When the vCPU has a PMU, but no PMU is set for the guest
134042773357SReiji Watanabe 	 * yet, set the default one.
134142773357SReiji Watanabe 	 */
134242773357SReiji Watanabe 	if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu)
134342773357SReiji Watanabe 		ret = kvm_arm_set_default_pmu(kvm);
134442773357SReiji Watanabe 
134542773357SReiji Watanabe 	return ret;
1346a7a2c72aSOliver Upton }
1347a7a2c72aSOliver Upton 
1348a7a2c72aSOliver Upton static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1349a7a2c72aSOliver Upton 				 const struct kvm_vcpu_init *init)
1350a7a2c72aSOliver Upton {
1351a7a2c72aSOliver Upton 	unsigned long features = init->features[0];
13522251e9ffSOliver Upton 	struct kvm *kvm = vcpu->kvm;
13532251e9ffSOliver Upton 	int ret = -EINVAL;
13542251e9ffSOliver Upton 
13552251e9ffSOliver Upton 	mutex_lock(&kvm->arch.config_lock);
13562251e9ffSOliver Upton 
13572251e9ffSOliver Upton 	if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) &&
13581de10b7dSOliver Upton 	    kvm_vcpu_init_changed(vcpu, init))
13592251e9ffSOliver Upton 		goto out_unlock;
1360a7a2c72aSOliver Upton 
13612251e9ffSOliver Upton 	bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES);
13629ed24f4bSMarc Zyngier 
136342773357SReiji Watanabe 	ret = kvm_setup_vcpu(vcpu);
136442773357SReiji Watanabe 	if (ret)
136542773357SReiji Watanabe 		goto out_unlock;
136642773357SReiji Watanabe 
13679ed24f4bSMarc Zyngier 	/* Now we know what it is, we can reset it. */
13683d4b2a4cSOliver Upton 	kvm_reset_vcpu(vcpu);
13699ed24f4bSMarc Zyngier 
13702251e9ffSOliver Upton 	set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags);
1371ef984060SOliver Upton 	vcpu_set_flag(vcpu, VCPU_INITIALIZED);
13723d4b2a4cSOliver Upton 	ret = 0;
13732251e9ffSOliver Upton out_unlock:
13742251e9ffSOliver Upton 	mutex_unlock(&kvm->arch.config_lock);
13759ed24f4bSMarc Zyngier 	return ret;
13769ed24f4bSMarc Zyngier }
13779ed24f4bSMarc Zyngier 
1378a7a2c72aSOliver Upton static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
1379a7a2c72aSOliver Upton 			       const struct kvm_vcpu_init *init)
1380a7a2c72aSOliver Upton {
1381a7a2c72aSOliver Upton 	int ret;
1382a7a2c72aSOliver Upton 
13835346f7e1SOliver Upton 	if (init->target != KVM_ARM_TARGET_GENERIC_V8 &&
13845346f7e1SOliver Upton 	    init->target != kvm_target_cpu())
1385a7a2c72aSOliver Upton 		return -EINVAL;
1386a7a2c72aSOliver Upton 
1387a7a2c72aSOliver Upton 	ret = kvm_vcpu_init_check_features(vcpu, init);
1388a7a2c72aSOliver Upton 	if (ret)
1389a7a2c72aSOliver Upton 		return ret;
1390a7a2c72aSOliver Upton 
1391ef984060SOliver Upton 	if (!kvm_vcpu_initialized(vcpu))
1392a7a2c72aSOliver Upton 		return __kvm_vcpu_set_target(vcpu, init);
1393a7a2c72aSOliver Upton 
1394a7a2c72aSOliver Upton 	if (kvm_vcpu_init_changed(vcpu, init))
1395a7a2c72aSOliver Upton 		return -EINVAL;
1396a7a2c72aSOliver Upton 
13973d4b2a4cSOliver Upton 	kvm_reset_vcpu(vcpu);
13983d4b2a4cSOliver Upton 	return 0;
1399a7a2c72aSOliver Upton }
1400a7a2c72aSOliver Upton 
14019ed24f4bSMarc Zyngier static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
14029ed24f4bSMarc Zyngier 					 struct kvm_vcpu_init *init)
14039ed24f4bSMarc Zyngier {
1404e3c1c0caSOliver Upton 	bool power_off = false;
14059ed24f4bSMarc Zyngier 	int ret;
14069ed24f4bSMarc Zyngier 
1407e3c1c0caSOliver Upton 	/*
1408e3c1c0caSOliver Upton 	 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid
1409e3c1c0caSOliver Upton 	 * reflecting it in the finalized feature set, thus limiting its scope
1410e3c1c0caSOliver Upton 	 * to a single KVM_ARM_VCPU_INIT call.
1411e3c1c0caSOliver Upton 	 */
1412192df2aaSOliver Upton 	if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
1413192df2aaSOliver Upton 		init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
1414e3c1c0caSOliver Upton 		power_off = true;
1415e3c1c0caSOliver Upton 	}
1416e3c1c0caSOliver Upton 
14179ed24f4bSMarc Zyngier 	ret = kvm_vcpu_set_target(vcpu, init);
14189ed24f4bSMarc Zyngier 	if (ret)
14199ed24f4bSMarc Zyngier 		return ret;
14209ed24f4bSMarc Zyngier 
14219ed24f4bSMarc Zyngier 	/*
14229ed24f4bSMarc Zyngier 	 * Ensure a rebooted VM will fault in RAM pages and detect if the
14239ed24f4bSMarc Zyngier 	 * guest MMU is turned off and flush the caches as needed.
1424892713e9SZenghui Yu 	 *
14257ae2f3dbSMarc Zyngier 	 * S2FWB enforces all memory accesses to RAM being cacheable,
14267ae2f3dbSMarc Zyngier 	 * ensuring that the data side is always coherent. We still
14277ae2f3dbSMarc Zyngier 	 * need to invalidate the I-cache though, as FWB does *not*
14287ae2f3dbSMarc Zyngier 	 * imply CTR_EL0.DIC.
14299ed24f4bSMarc Zyngier 	 */
1430cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu)) {
14317ae2f3dbSMarc Zyngier 		if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
14329ed24f4bSMarc Zyngier 			stage2_unmap_vm(vcpu->kvm);
14337ae2f3dbSMarc Zyngier 		else
1434fade9c2cSFuad Tabba 			icache_inval_all_pou();
14357ae2f3dbSMarc Zyngier 	}
14369ed24f4bSMarc Zyngier 
14379ed24f4bSMarc Zyngier 	vcpu_reset_hcr(vcpu);
143875c76ab5SMarc Zyngier 	vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu);
14399ed24f4bSMarc Zyngier 
14409ed24f4bSMarc Zyngier 	/*
14419ed24f4bSMarc Zyngier 	 * Handle the "start in power-off" case.
14429ed24f4bSMarc Zyngier 	 */
14434ff910beSReiji Watanabe 	spin_lock(&vcpu->arch.mp_state_lock);
14444ff910beSReiji Watanabe 
1445e3c1c0caSOliver Upton 	if (power_off)
14464ff910beSReiji Watanabe 		__kvm_arm_vcpu_power_off(vcpu);
14479ed24f4bSMarc Zyngier 	else
14480acc7239SOliver Upton 		WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
14499ed24f4bSMarc Zyngier 
14504ff910beSReiji Watanabe 	spin_unlock(&vcpu->arch.mp_state_lock);
14519ed24f4bSMarc Zyngier 
14529ed24f4bSMarc Zyngier 	return 0;
14539ed24f4bSMarc Zyngier }
14549ed24f4bSMarc Zyngier 
14559ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
14569ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
14579ed24f4bSMarc Zyngier {
14589ed24f4bSMarc Zyngier 	int ret = -ENXIO;
14599ed24f4bSMarc Zyngier 
14609ed24f4bSMarc Zyngier 	switch (attr->group) {
14619ed24f4bSMarc Zyngier 	default:
14629ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
14639ed24f4bSMarc Zyngier 		break;
14649ed24f4bSMarc Zyngier 	}
14659ed24f4bSMarc Zyngier 
14669ed24f4bSMarc Zyngier 	return ret;
14679ed24f4bSMarc Zyngier }
14689ed24f4bSMarc Zyngier 
14699ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
14709ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
14719ed24f4bSMarc Zyngier {
14729ed24f4bSMarc Zyngier 	int ret = -ENXIO;
14739ed24f4bSMarc Zyngier 
14749ed24f4bSMarc Zyngier 	switch (attr->group) {
14759ed24f4bSMarc Zyngier 	default:
14769ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
14779ed24f4bSMarc Zyngier 		break;
14789ed24f4bSMarc Zyngier 	}
14799ed24f4bSMarc Zyngier 
14809ed24f4bSMarc Zyngier 	return ret;
14819ed24f4bSMarc Zyngier }
14829ed24f4bSMarc Zyngier 
14839ed24f4bSMarc Zyngier static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
14849ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
14859ed24f4bSMarc Zyngier {
14869ed24f4bSMarc Zyngier 	int ret = -ENXIO;
14879ed24f4bSMarc Zyngier 
14889ed24f4bSMarc Zyngier 	switch (attr->group) {
14899ed24f4bSMarc Zyngier 	default:
14909ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
14919ed24f4bSMarc Zyngier 		break;
14929ed24f4bSMarc Zyngier 	}
14939ed24f4bSMarc Zyngier 
14949ed24f4bSMarc Zyngier 	return ret;
14959ed24f4bSMarc Zyngier }
14969ed24f4bSMarc Zyngier 
14979ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
14989ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
14999ed24f4bSMarc Zyngier {
15009ed24f4bSMarc Zyngier 	memset(events, 0, sizeof(*events));
15019ed24f4bSMarc Zyngier 
15029ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_get_events(vcpu, events);
15039ed24f4bSMarc Zyngier }
15049ed24f4bSMarc Zyngier 
15059ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
15069ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
15079ed24f4bSMarc Zyngier {
15089ed24f4bSMarc Zyngier 	int i;
15099ed24f4bSMarc Zyngier 
15109ed24f4bSMarc Zyngier 	/* check whether the reserved field is zero */
15119ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
15129ed24f4bSMarc Zyngier 		if (events->reserved[i])
15139ed24f4bSMarc Zyngier 			return -EINVAL;
15149ed24f4bSMarc Zyngier 
15159ed24f4bSMarc Zyngier 	/* check whether the pad field is zero */
15169ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
15179ed24f4bSMarc Zyngier 		if (events->exception.pad[i])
15189ed24f4bSMarc Zyngier 			return -EINVAL;
15199ed24f4bSMarc Zyngier 
15209ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_set_events(vcpu, events);
15219ed24f4bSMarc Zyngier }
15229ed24f4bSMarc Zyngier 
15239ed24f4bSMarc Zyngier long kvm_arch_vcpu_ioctl(struct file *filp,
15249ed24f4bSMarc Zyngier 			 unsigned int ioctl, unsigned long arg)
15259ed24f4bSMarc Zyngier {
15269ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = filp->private_data;
15279ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
15289ed24f4bSMarc Zyngier 	struct kvm_device_attr attr;
15299ed24f4bSMarc Zyngier 	long r;
15309ed24f4bSMarc Zyngier 
15319ed24f4bSMarc Zyngier 	switch (ioctl) {
15329ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_INIT: {
15339ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
15349ed24f4bSMarc Zyngier 
15359ed24f4bSMarc Zyngier 		r = -EFAULT;
15369ed24f4bSMarc Zyngier 		if (copy_from_user(&init, argp, sizeof(init)))
15379ed24f4bSMarc Zyngier 			break;
15389ed24f4bSMarc Zyngier 
15399ed24f4bSMarc Zyngier 		r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
15409ed24f4bSMarc Zyngier 		break;
15419ed24f4bSMarc Zyngier 	}
15429ed24f4bSMarc Zyngier 	case KVM_SET_ONE_REG:
15439ed24f4bSMarc Zyngier 	case KVM_GET_ONE_REG: {
15449ed24f4bSMarc Zyngier 		struct kvm_one_reg reg;
15459ed24f4bSMarc Zyngier 
15469ed24f4bSMarc Zyngier 		r = -ENOEXEC;
15479ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
15489ed24f4bSMarc Zyngier 			break;
15499ed24f4bSMarc Zyngier 
15509ed24f4bSMarc Zyngier 		r = -EFAULT;
15519ed24f4bSMarc Zyngier 		if (copy_from_user(&reg, argp, sizeof(reg)))
15529ed24f4bSMarc Zyngier 			break;
15539ed24f4bSMarc Zyngier 
15546826c684SOliver Upton 		/*
15556826c684SOliver Upton 		 * We could owe a reset due to PSCI. Handle the pending reset
15566826c684SOliver Upton 		 * here to ensure userspace register accesses are ordered after
15576826c684SOliver Upton 		 * the reset.
15586826c684SOliver Upton 		 */
15596826c684SOliver Upton 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
15606826c684SOliver Upton 			kvm_reset_vcpu(vcpu);
15616826c684SOliver Upton 
15629ed24f4bSMarc Zyngier 		if (ioctl == KVM_SET_ONE_REG)
15639ed24f4bSMarc Zyngier 			r = kvm_arm_set_reg(vcpu, &reg);
15649ed24f4bSMarc Zyngier 		else
15659ed24f4bSMarc Zyngier 			r = kvm_arm_get_reg(vcpu, &reg);
15669ed24f4bSMarc Zyngier 		break;
15679ed24f4bSMarc Zyngier 	}
15689ed24f4bSMarc Zyngier 	case KVM_GET_REG_LIST: {
15699ed24f4bSMarc Zyngier 		struct kvm_reg_list __user *user_list = argp;
15709ed24f4bSMarc Zyngier 		struct kvm_reg_list reg_list;
15719ed24f4bSMarc Zyngier 		unsigned n;
15729ed24f4bSMarc Zyngier 
15739ed24f4bSMarc Zyngier 		r = -ENOEXEC;
15749ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
15759ed24f4bSMarc Zyngier 			break;
15769ed24f4bSMarc Zyngier 
15779ed24f4bSMarc Zyngier 		r = -EPERM;
15789ed24f4bSMarc Zyngier 		if (!kvm_arm_vcpu_is_finalized(vcpu))
15799ed24f4bSMarc Zyngier 			break;
15809ed24f4bSMarc Zyngier 
15819ed24f4bSMarc Zyngier 		r = -EFAULT;
15829ed24f4bSMarc Zyngier 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
15839ed24f4bSMarc Zyngier 			break;
15849ed24f4bSMarc Zyngier 		n = reg_list.n;
15859ed24f4bSMarc Zyngier 		reg_list.n = kvm_arm_num_regs(vcpu);
15869ed24f4bSMarc Zyngier 		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
15879ed24f4bSMarc Zyngier 			break;
15889ed24f4bSMarc Zyngier 		r = -E2BIG;
15899ed24f4bSMarc Zyngier 		if (n < reg_list.n)
15909ed24f4bSMarc Zyngier 			break;
15919ed24f4bSMarc Zyngier 		r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
15929ed24f4bSMarc Zyngier 		break;
15939ed24f4bSMarc Zyngier 	}
15949ed24f4bSMarc Zyngier 	case KVM_SET_DEVICE_ATTR: {
15959ed24f4bSMarc Zyngier 		r = -EFAULT;
15969ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
15979ed24f4bSMarc Zyngier 			break;
15989ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_set_attr(vcpu, &attr);
15999ed24f4bSMarc Zyngier 		break;
16009ed24f4bSMarc Zyngier 	}
16019ed24f4bSMarc Zyngier 	case KVM_GET_DEVICE_ATTR: {
16029ed24f4bSMarc Zyngier 		r = -EFAULT;
16039ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
16049ed24f4bSMarc Zyngier 			break;
16059ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_get_attr(vcpu, &attr);
16069ed24f4bSMarc Zyngier 		break;
16079ed24f4bSMarc Zyngier 	}
16089ed24f4bSMarc Zyngier 	case KVM_HAS_DEVICE_ATTR: {
16099ed24f4bSMarc Zyngier 		r = -EFAULT;
16109ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
16119ed24f4bSMarc Zyngier 			break;
16129ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_has_attr(vcpu, &attr);
16139ed24f4bSMarc Zyngier 		break;
16149ed24f4bSMarc Zyngier 	}
16159ed24f4bSMarc Zyngier 	case KVM_GET_VCPU_EVENTS: {
16169ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
16179ed24f4bSMarc Zyngier 
16189ed24f4bSMarc Zyngier 		if (kvm_arm_vcpu_get_events(vcpu, &events))
16199ed24f4bSMarc Zyngier 			return -EINVAL;
16209ed24f4bSMarc Zyngier 
16219ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &events, sizeof(events)))
16229ed24f4bSMarc Zyngier 			return -EFAULT;
16239ed24f4bSMarc Zyngier 
16249ed24f4bSMarc Zyngier 		return 0;
16259ed24f4bSMarc Zyngier 	}
16269ed24f4bSMarc Zyngier 	case KVM_SET_VCPU_EVENTS: {
16279ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
16289ed24f4bSMarc Zyngier 
16299ed24f4bSMarc Zyngier 		if (copy_from_user(&events, argp, sizeof(events)))
16309ed24f4bSMarc Zyngier 			return -EFAULT;
16319ed24f4bSMarc Zyngier 
16329ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_set_events(vcpu, &events);
16339ed24f4bSMarc Zyngier 	}
16349ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_FINALIZE: {
16359ed24f4bSMarc Zyngier 		int what;
16369ed24f4bSMarc Zyngier 
16379ed24f4bSMarc Zyngier 		if (!kvm_vcpu_initialized(vcpu))
16389ed24f4bSMarc Zyngier 			return -ENOEXEC;
16399ed24f4bSMarc Zyngier 
16409ed24f4bSMarc Zyngier 		if (get_user(what, (const int __user *)argp))
16419ed24f4bSMarc Zyngier 			return -EFAULT;
16429ed24f4bSMarc Zyngier 
16439ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_finalize(vcpu, what);
16449ed24f4bSMarc Zyngier 	}
16459ed24f4bSMarc Zyngier 	default:
16469ed24f4bSMarc Zyngier 		r = -EINVAL;
16479ed24f4bSMarc Zyngier 	}
16489ed24f4bSMarc Zyngier 
16499ed24f4bSMarc Zyngier 	return r;
16509ed24f4bSMarc Zyngier }
16519ed24f4bSMarc Zyngier 
16529ed24f4bSMarc Zyngier void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
16539ed24f4bSMarc Zyngier {
16549ed24f4bSMarc Zyngier 
16559ed24f4bSMarc Zyngier }
16569ed24f4bSMarc Zyngier 
16579ed24f4bSMarc Zyngier static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
16589ed24f4bSMarc Zyngier 					struct kvm_arm_device_addr *dev_addr)
16599ed24f4bSMarc Zyngier {
16609f968c92SMarc Zyngier 	switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
16619ed24f4bSMarc Zyngier 	case KVM_ARM_DEVICE_VGIC_V2:
16629ed24f4bSMarc Zyngier 		if (!vgic_present)
16639ed24f4bSMarc Zyngier 			return -ENXIO;
16649f968c92SMarc Zyngier 		return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
16659ed24f4bSMarc Zyngier 	default:
16669ed24f4bSMarc Zyngier 		return -ENODEV;
16679ed24f4bSMarc Zyngier 	}
16689ed24f4bSMarc Zyngier }
16699ed24f4bSMarc Zyngier 
1670e0fc6b21SOliver Upton static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1671e0fc6b21SOliver Upton {
1672e0fc6b21SOliver Upton 	switch (attr->group) {
1673821d935cSOliver Upton 	case KVM_ARM_VM_SMCCC_CTRL:
1674821d935cSOliver Upton 		return kvm_vm_smccc_has_attr(kvm, attr);
1675e0fc6b21SOliver Upton 	default:
1676e0fc6b21SOliver Upton 		return -ENXIO;
1677e0fc6b21SOliver Upton 	}
1678e0fc6b21SOliver Upton }
1679e0fc6b21SOliver Upton 
1680e0fc6b21SOliver Upton static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1681e0fc6b21SOliver Upton {
1682e0fc6b21SOliver Upton 	switch (attr->group) {
1683821d935cSOliver Upton 	case KVM_ARM_VM_SMCCC_CTRL:
1684821d935cSOliver Upton 		return kvm_vm_smccc_set_attr(kvm, attr);
1685e0fc6b21SOliver Upton 	default:
1686e0fc6b21SOliver Upton 		return -ENXIO;
1687e0fc6b21SOliver Upton 	}
1688e0fc6b21SOliver Upton }
1689e0fc6b21SOliver Upton 
1690d8708b80SThomas Huth int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
16919ed24f4bSMarc Zyngier {
16929ed24f4bSMarc Zyngier 	struct kvm *kvm = filp->private_data;
16939ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
1694e0fc6b21SOliver Upton 	struct kvm_device_attr attr;
16959ed24f4bSMarc Zyngier 
16969ed24f4bSMarc Zyngier 	switch (ioctl) {
16979ed24f4bSMarc Zyngier 	case KVM_CREATE_IRQCHIP: {
16989ed24f4bSMarc Zyngier 		int ret;
16999ed24f4bSMarc Zyngier 		if (!vgic_present)
17009ed24f4bSMarc Zyngier 			return -ENXIO;
17019ed24f4bSMarc Zyngier 		mutex_lock(&kvm->lock);
17029ed24f4bSMarc Zyngier 		ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
17039ed24f4bSMarc Zyngier 		mutex_unlock(&kvm->lock);
17049ed24f4bSMarc Zyngier 		return ret;
17059ed24f4bSMarc Zyngier 	}
17069ed24f4bSMarc Zyngier 	case KVM_ARM_SET_DEVICE_ADDR: {
17079ed24f4bSMarc Zyngier 		struct kvm_arm_device_addr dev_addr;
17089ed24f4bSMarc Zyngier 
17099ed24f4bSMarc Zyngier 		if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
17109ed24f4bSMarc Zyngier 			return -EFAULT;
17119ed24f4bSMarc Zyngier 		return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
17129ed24f4bSMarc Zyngier 	}
17139ed24f4bSMarc Zyngier 	case KVM_ARM_PREFERRED_TARGET: {
17145346f7e1SOliver Upton 		struct kvm_vcpu_init init = {
17155346f7e1SOliver Upton 			.target = KVM_ARM_TARGET_GENERIC_V8,
17165346f7e1SOliver Upton 		};
17179ed24f4bSMarc Zyngier 
17189ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &init, sizeof(init)))
17199ed24f4bSMarc Zyngier 			return -EFAULT;
17209ed24f4bSMarc Zyngier 
17219ed24f4bSMarc Zyngier 		return 0;
17229ed24f4bSMarc Zyngier 	}
1723f0376edbSSteven Price 	case KVM_ARM_MTE_COPY_TAGS: {
1724f0376edbSSteven Price 		struct kvm_arm_copy_mte_tags copy_tags;
1725f0376edbSSteven Price 
1726f0376edbSSteven Price 		if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
1727f0376edbSSteven Price 			return -EFAULT;
1728f0376edbSSteven Price 		return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
1729f0376edbSSteven Price 	}
173030ec7997SMarc Zyngier 	case KVM_ARM_SET_COUNTER_OFFSET: {
173130ec7997SMarc Zyngier 		struct kvm_arm_counter_offset offset;
173230ec7997SMarc Zyngier 
173330ec7997SMarc Zyngier 		if (copy_from_user(&offset, argp, sizeof(offset)))
173430ec7997SMarc Zyngier 			return -EFAULT;
173530ec7997SMarc Zyngier 		return kvm_vm_ioctl_set_counter_offset(kvm, &offset);
173630ec7997SMarc Zyngier 	}
1737e0fc6b21SOliver Upton 	case KVM_HAS_DEVICE_ATTR: {
1738e0fc6b21SOliver Upton 		if (copy_from_user(&attr, argp, sizeof(attr)))
1739e0fc6b21SOliver Upton 			return -EFAULT;
1740e0fc6b21SOliver Upton 
1741e0fc6b21SOliver Upton 		return kvm_vm_has_attr(kvm, &attr);
1742e0fc6b21SOliver Upton 	}
1743e0fc6b21SOliver Upton 	case KVM_SET_DEVICE_ATTR: {
1744e0fc6b21SOliver Upton 		if (copy_from_user(&attr, argp, sizeof(attr)))
1745e0fc6b21SOliver Upton 			return -EFAULT;
1746e0fc6b21SOliver Upton 
1747e0fc6b21SOliver Upton 		return kvm_vm_set_attr(kvm, &attr);
1748e0fc6b21SOliver Upton 	}
17493f9cd0caSJing Zhang 	case KVM_ARM_GET_REG_WRITABLE_MASKS: {
17503f9cd0caSJing Zhang 		struct reg_mask_range range;
17513f9cd0caSJing Zhang 
17523f9cd0caSJing Zhang 		if (copy_from_user(&range, argp, sizeof(range)))
17533f9cd0caSJing Zhang 			return -EFAULT;
17543f9cd0caSJing Zhang 		return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range);
17553f9cd0caSJing Zhang 	}
17569ed24f4bSMarc Zyngier 	default:
17579ed24f4bSMarc Zyngier 		return -EINVAL;
17589ed24f4bSMarc Zyngier 	}
17599ed24f4bSMarc Zyngier }
17609ed24f4bSMarc Zyngier 
176196906a91SMarc Zyngier /* unlocks vcpus from @vcpu_lock_idx and smaller */
176296906a91SMarc Zyngier static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx)
176396906a91SMarc Zyngier {
176496906a91SMarc Zyngier 	struct kvm_vcpu *tmp_vcpu;
176596906a91SMarc Zyngier 
176696906a91SMarc Zyngier 	for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
176796906a91SMarc Zyngier 		tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
176896906a91SMarc Zyngier 		mutex_unlock(&tmp_vcpu->mutex);
176996906a91SMarc Zyngier 	}
177096906a91SMarc Zyngier }
177196906a91SMarc Zyngier 
177296906a91SMarc Zyngier void unlock_all_vcpus(struct kvm *kvm)
177396906a91SMarc Zyngier {
177496906a91SMarc Zyngier 	lockdep_assert_held(&kvm->lock);
177596906a91SMarc Zyngier 
177696906a91SMarc Zyngier 	unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1);
177796906a91SMarc Zyngier }
177896906a91SMarc Zyngier 
177996906a91SMarc Zyngier /* Returns true if all vcpus were locked, false otherwise */
178096906a91SMarc Zyngier bool lock_all_vcpus(struct kvm *kvm)
178196906a91SMarc Zyngier {
178296906a91SMarc Zyngier 	struct kvm_vcpu *tmp_vcpu;
178396906a91SMarc Zyngier 	unsigned long c;
178496906a91SMarc Zyngier 
178596906a91SMarc Zyngier 	lockdep_assert_held(&kvm->lock);
178696906a91SMarc Zyngier 
178796906a91SMarc Zyngier 	/*
178896906a91SMarc Zyngier 	 * Any time a vcpu is in an ioctl (including running), the
178996906a91SMarc Zyngier 	 * core KVM code tries to grab the vcpu->mutex.
179096906a91SMarc Zyngier 	 *
179196906a91SMarc Zyngier 	 * By grabbing the vcpu->mutex of all VCPUs we ensure that no
179296906a91SMarc Zyngier 	 * other VCPUs can fiddle with the state while we access it.
179396906a91SMarc Zyngier 	 */
179496906a91SMarc Zyngier 	kvm_for_each_vcpu(c, tmp_vcpu, kvm) {
179596906a91SMarc Zyngier 		if (!mutex_trylock(&tmp_vcpu->mutex)) {
179696906a91SMarc Zyngier 			unlock_vcpus(kvm, c - 1);
179796906a91SMarc Zyngier 			return false;
179896906a91SMarc Zyngier 		}
179996906a91SMarc Zyngier 	}
180096906a91SMarc Zyngier 
180196906a91SMarc Zyngier 	return true;
180296906a91SMarc Zyngier }
180396906a91SMarc Zyngier 
180430c95391SDavid Brazdil static unsigned long nvhe_percpu_size(void)
180530c95391SDavid Brazdil {
180630c95391SDavid Brazdil 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
180730c95391SDavid Brazdil 		(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
180830c95391SDavid Brazdil }
180930c95391SDavid Brazdil 
181030c95391SDavid Brazdil static unsigned long nvhe_percpu_order(void)
181130c95391SDavid Brazdil {
181230c95391SDavid Brazdil 	unsigned long size = nvhe_percpu_size();
181330c95391SDavid Brazdil 
181430c95391SDavid Brazdil 	return size ? get_order(size) : 0;
181530c95391SDavid Brazdil }
181630c95391SDavid Brazdil 
1817b881cdceSWill Deacon /* A lookup table holding the hypervisor VA for each vector slot */
1818b881cdceSWill Deacon static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1819de5bcdb4SWill Deacon 
1820b881cdceSWill Deacon static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1821b881cdceSWill Deacon {
1822bc1d2892SQuentin Perret 	hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
18239ef2b48bSWill Deacon }
18249ef2b48bSWill Deacon 
1825b881cdceSWill Deacon static int kvm_init_vector_slots(void)
1826b881cdceSWill Deacon {
1827b881cdceSWill Deacon 	int err;
1828b881cdceSWill Deacon 	void *base;
1829b881cdceSWill Deacon 
1830b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1831b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1832b881cdceSWill Deacon 
1833b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1834b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1835b881cdceSWill Deacon 
18362e403167SQuentin Perret 	if (kvm_system_needs_idmapped_vectors() &&
18372e403167SQuentin Perret 	    !is_protected_kvm_enabled()) {
1838b881cdceSWill Deacon 		err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1839b881cdceSWill Deacon 					       __BP_HARDEN_HYP_VECS_SZ, &base);
1840b881cdceSWill Deacon 		if (err)
1841b881cdceSWill Deacon 			return err;
1842b881cdceSWill Deacon 	}
18439ef2b48bSWill Deacon 
1844b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1845b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
18469ef2b48bSWill Deacon 	return 0;
18479ef2b48bSWill Deacon }
18489ef2b48bSWill Deacon 
184953bf620aSSean Christopherson static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
18509ed24f4bSMarc Zyngier {
18519cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1852bd412e2aSRyan Roberts 	u64 mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
1853d3e1086cSDavid Brazdil 	unsigned long tcr;
18549ed24f4bSMarc Zyngier 
185571b3ec5fSDavid Brazdil 	/*
185671b3ec5fSDavid Brazdil 	 * Calculate the raw per-cpu offset without a translation from the
185771b3ec5fSDavid Brazdil 	 * kernel's mapping to the linear mapping, and store it in tpidr_el2
185871b3ec5fSDavid Brazdil 	 * so that we can use adr_l to access per-cpu variables in EL2.
1859e1663372SSteven Price 	 * Also drop the KASAN tag which gets in the way...
186071b3ec5fSDavid Brazdil 	 */
18619cc77581SQuentin Perret 	params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
186230c95391SDavid Brazdil 			    (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
186371b3ec5fSDavid Brazdil 
1864d3e1086cSDavid Brazdil 	params->mair_el2 = read_sysreg(mair_el1);
1865d3e1086cSDavid Brazdil 
1866cff3b5cfSMarc Zyngier 	tcr = read_sysreg(tcr_el1);
1867cff3b5cfSMarc Zyngier 	if (cpus_have_final_cap(ARM64_KVM_HVHE)) {
1868cff3b5cfSMarc Zyngier 		tcr |= TCR_EPD1_MASK;
1869cff3b5cfSMarc Zyngier 	} else {
1870cff3b5cfSMarc Zyngier 		tcr &= TCR_EL2_MASK;
1871cff3b5cfSMarc Zyngier 		tcr |= TCR_EL2_RES1;
1872cff3b5cfSMarc Zyngier 	}
1873d3e1086cSDavid Brazdil 	tcr &= ~TCR_T0SZ_MASK;
1874579d7ebeSRyan Roberts 	tcr |= TCR_T0SZ(hyp_va_bits);
1875bd412e2aSRyan Roberts 	tcr &= ~TCR_EL2_PS_MASK;
1876bd412e2aSRyan Roberts 	tcr |= FIELD_PREP(TCR_EL2_PS_MASK, kvm_get_parange(mmfr0));
1877bd412e2aSRyan Roberts 	if (kvm_lpa2_is_enabled())
1878bd412e2aSRyan Roberts 		tcr |= TCR_EL2_DS;
1879d3e1086cSDavid Brazdil 	params->tcr_el2 = tcr;
1880d3e1086cSDavid Brazdil 
188163fec243SDavid Brazdil 	params->pgd_pa = kvm_mmu_get_httbr();
1882734864c1SQuentin Perret 	if (is_protected_kvm_enabled())
1883734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1884734864c1SQuentin Perret 	else
1885734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1886d0daf5a2SMarc Zyngier 	if (cpus_have_final_cap(ARM64_KVM_HVHE))
1887d0daf5a2SMarc Zyngier 		params->hcr_el2 |= HCR_E2H;
1888734864c1SQuentin Perret 	params->vttbr = params->vtcr = 0;
188963fec243SDavid Brazdil 
189063fec243SDavid Brazdil 	/*
189163fec243SDavid Brazdil 	 * Flush the init params from the data cache because the struct will
189263fec243SDavid Brazdil 	 * be read while the MMU is off.
189363fec243SDavid Brazdil 	 */
189463fec243SDavid Brazdil 	kvm_flush_dcache_to_poc(params, sizeof(*params));
18959cc77581SQuentin Perret }
18969cc77581SQuentin Perret 
1897bfa79a80SQuentin Perret static void hyp_install_host_vector(void)
18989cc77581SQuentin Perret {
18999cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params;
19009cc77581SQuentin Perret 	struct arm_smccc_res res;
19019cc77581SQuentin Perret 
19029cc77581SQuentin Perret 	/* Switch from the HYP stub to our own HYP init vector */
19039cc77581SQuentin Perret 	__hyp_set_vectors(kvm_get_idmap_vector());
19049ed24f4bSMarc Zyngier 
190571b3ec5fSDavid Brazdil 	/*
190671b3ec5fSDavid Brazdil 	 * Call initialization code, and switch to the full blown HYP code.
190771b3ec5fSDavid Brazdil 	 * If the cpucaps haven't been finalized yet, something has gone very
190871b3ec5fSDavid Brazdil 	 * wrong, and hyp will crash and burn when it uses any
1909d8569fbaSMark Rutland 	 * cpus_have_*_cap() wrapper.
191071b3ec5fSDavid Brazdil 	 */
191171b3ec5fSDavid Brazdil 	BUG_ON(!system_capabilities_finalized());
19129cc77581SQuentin Perret 	params = this_cpu_ptr_nvhe_sym(kvm_init_params);
191363fec243SDavid Brazdil 	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
191404e4caa8SAndrew Scull 	WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1915bfa79a80SQuentin Perret }
1916bfa79a80SQuentin Perret 
1917bfa79a80SQuentin Perret static void cpu_init_hyp_mode(void)
1918bfa79a80SQuentin Perret {
1919bfa79a80SQuentin Perret 	hyp_install_host_vector();
192071b3ec5fSDavid Brazdil 
192171b3ec5fSDavid Brazdil 	/*
192271b3ec5fSDavid Brazdil 	 * Disabling SSBD on a non-VHE system requires us to enable SSBS
192371b3ec5fSDavid Brazdil 	 * at EL2.
192471b3ec5fSDavid Brazdil 	 */
192571b3ec5fSDavid Brazdil 	if (this_cpu_has_cap(ARM64_SSBS) &&
1926d63d975aSMarc Zyngier 	    arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
192713aeb9b4SDavid Brazdil 		kvm_call_hyp_nvhe(__kvm_enable_ssbs);
192871b3ec5fSDavid Brazdil 	}
19299ed24f4bSMarc Zyngier }
19309ed24f4bSMarc Zyngier 
19319ed24f4bSMarc Zyngier static void cpu_hyp_reset(void)
19329ed24f4bSMarc Zyngier {
19339ed24f4bSMarc Zyngier 	if (!is_kernel_in_hyp_mode())
19349ed24f4bSMarc Zyngier 		__hyp_reset_vectors();
19359ed24f4bSMarc Zyngier }
19369ed24f4bSMarc Zyngier 
1937042c76a9SWill Deacon /*
1938042c76a9SWill Deacon  * EL2 vectors can be mapped and rerouted in a number of ways,
1939042c76a9SWill Deacon  * depending on the kernel configuration and CPU present:
1940042c76a9SWill Deacon  *
1941042c76a9SWill Deacon  * - If the CPU is affected by Spectre-v2, the hardening sequence is
1942042c76a9SWill Deacon  *   placed in one of the vector slots, which is executed before jumping
1943042c76a9SWill Deacon  *   to the real vectors.
1944042c76a9SWill Deacon  *
1945c4792b6dSWill Deacon  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1946042c76a9SWill Deacon  *   containing the hardening sequence is mapped next to the idmap page,
1947042c76a9SWill Deacon  *   and executed before jumping to the real vectors.
1948042c76a9SWill Deacon  *
1949c4792b6dSWill Deacon  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1950042c76a9SWill Deacon  *   empty slot is selected, mapped next to the idmap page, and
1951042c76a9SWill Deacon  *   executed before jumping to the real vectors.
1952042c76a9SWill Deacon  *
1953c4792b6dSWill Deacon  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1954042c76a9SWill Deacon  * VHE, as we don't have hypervisor-specific mappings. If the system
1955042c76a9SWill Deacon  * is VHE and yet selects this capability, it will be ignored.
1956042c76a9SWill Deacon  */
1957042c76a9SWill Deacon static void cpu_set_hyp_vector(void)
1958042c76a9SWill Deacon {
19596279017eSWill Deacon 	struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1960b881cdceSWill Deacon 	void *vector = hyp_spectre_vector_selector[data->slot];
1961042c76a9SWill Deacon 
1962bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
1963b881cdceSWill Deacon 		*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1964bfa79a80SQuentin Perret 	else
1965bfa79a80SQuentin Perret 		kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1966042c76a9SWill Deacon }
1967042c76a9SWill Deacon 
19688579a185SWill Deacon static void cpu_hyp_init_context(void)
19699ed24f4bSMarc Zyngier {
19702a1198c9SDavid Brazdil 	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
19719ed24f4bSMarc Zyngier 
19728579a185SWill Deacon 	if (!is_kernel_in_hyp_mode())
19738579a185SWill Deacon 		cpu_init_hyp_mode();
19748579a185SWill Deacon }
19758579a185SWill Deacon 
19768579a185SWill Deacon static void cpu_hyp_init_features(void)
19778579a185SWill Deacon {
19788579a185SWill Deacon 	cpu_set_hyp_vector();
19798579a185SWill Deacon 	kvm_arm_init_debug();
1980a0e47952SAndrew Scull 
19819ed24f4bSMarc Zyngier 	if (is_kernel_in_hyp_mode())
19829ed24f4bSMarc Zyngier 		kvm_timer_init_vhe();
19839ed24f4bSMarc Zyngier 
19849ed24f4bSMarc Zyngier 	if (vgic_present)
19859ed24f4bSMarc Zyngier 		kvm_vgic_init_cpu_hardware();
19869ed24f4bSMarc Zyngier }
19879ed24f4bSMarc Zyngier 
19888579a185SWill Deacon static void cpu_hyp_reinit(void)
19898579a185SWill Deacon {
19908579a185SWill Deacon 	cpu_hyp_reset();
19918579a185SWill Deacon 	cpu_hyp_init_context();
19928579a185SWill Deacon 	cpu_hyp_init_features();
19938579a185SWill Deacon }
19948579a185SWill Deacon 
1995733c758eSOliver Upton static void cpu_hyp_init(void *discard)
19969ed24f4bSMarc Zyngier {
1997733c758eSOliver Upton 	if (!__this_cpu_read(kvm_hyp_initialized)) {
19989ed24f4bSMarc Zyngier 		cpu_hyp_reinit();
1999733c758eSOliver Upton 		__this_cpu_write(kvm_hyp_initialized, 1);
2000733c758eSOliver Upton 	}
2001733c758eSOliver Upton }
2002733c758eSOliver Upton 
2003733c758eSOliver Upton static void cpu_hyp_uninit(void *discard)
2004733c758eSOliver Upton {
2005733c758eSOliver Upton 	if (__this_cpu_read(kvm_hyp_initialized)) {
2006733c758eSOliver Upton 		cpu_hyp_reset();
2007733c758eSOliver Upton 		__this_cpu_write(kvm_hyp_initialized, 0);
20089ed24f4bSMarc Zyngier 	}
20099ed24f4bSMarc Zyngier }
20109ed24f4bSMarc Zyngier 
20119ed24f4bSMarc Zyngier int kvm_arch_hardware_enable(void)
20129ed24f4bSMarc Zyngier {
2013970dee09SMarc Zyngier 	/*
2014970dee09SMarc Zyngier 	 * Most calls to this function are made with migration
2015970dee09SMarc Zyngier 	 * disabled, but not with preemption disabled. The former is
2016970dee09SMarc Zyngier 	 * enough to ensure correctness, but most of the helpers
2017970dee09SMarc Zyngier 	 * expect the later and will throw a tantrum otherwise.
2018970dee09SMarc Zyngier 	 */
2019970dee09SMarc Zyngier 	preempt_disable();
2020970dee09SMarc Zyngier 
2021733c758eSOliver Upton 	cpu_hyp_init(NULL);
2022466d27e4SMarc Zyngier 
2023466d27e4SMarc Zyngier 	kvm_vgic_cpu_up();
2024466d27e4SMarc Zyngier 	kvm_timer_cpu_up();
2025466d27e4SMarc Zyngier 
2026970dee09SMarc Zyngier 	preempt_enable();
2027970dee09SMarc Zyngier 
20289ed24f4bSMarc Zyngier 	return 0;
20299ed24f4bSMarc Zyngier }
20309ed24f4bSMarc Zyngier 
20319ed24f4bSMarc Zyngier void kvm_arch_hardware_disable(void)
20329ed24f4bSMarc Zyngier {
2033466d27e4SMarc Zyngier 	kvm_timer_cpu_down();
2034466d27e4SMarc Zyngier 	kvm_vgic_cpu_down();
2035466d27e4SMarc Zyngier 
2036fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
2037733c758eSOliver Upton 		cpu_hyp_uninit(NULL);
20389ed24f4bSMarc Zyngier }
20399ed24f4bSMarc Zyngier 
20409ed24f4bSMarc Zyngier #ifdef CONFIG_CPU_PM
20419ed24f4bSMarc Zyngier static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
20429ed24f4bSMarc Zyngier 				    unsigned long cmd,
20439ed24f4bSMarc Zyngier 				    void *v)
20449ed24f4bSMarc Zyngier {
20459ed24f4bSMarc Zyngier 	/*
2046733c758eSOliver Upton 	 * kvm_hyp_initialized is left with its old value over
20479ed24f4bSMarc Zyngier 	 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
20489ed24f4bSMarc Zyngier 	 * re-enable hyp.
20499ed24f4bSMarc Zyngier 	 */
20509ed24f4bSMarc Zyngier 	switch (cmd) {
20519ed24f4bSMarc Zyngier 	case CPU_PM_ENTER:
2052733c758eSOliver Upton 		if (__this_cpu_read(kvm_hyp_initialized))
20539ed24f4bSMarc Zyngier 			/*
2054733c758eSOliver Upton 			 * don't update kvm_hyp_initialized here
2055733c758eSOliver Upton 			 * so that the hyp will be re-enabled
20569ed24f4bSMarc Zyngier 			 * when we resume. See below.
20579ed24f4bSMarc Zyngier 			 */
20589ed24f4bSMarc Zyngier 			cpu_hyp_reset();
20599ed24f4bSMarc Zyngier 
20609ed24f4bSMarc Zyngier 		return NOTIFY_OK;
20619ed24f4bSMarc Zyngier 	case CPU_PM_ENTER_FAILED:
20629ed24f4bSMarc Zyngier 	case CPU_PM_EXIT:
2063733c758eSOliver Upton 		if (__this_cpu_read(kvm_hyp_initialized))
2064733c758eSOliver Upton 			/* The hyp was enabled before suspend. */
20659ed24f4bSMarc Zyngier 			cpu_hyp_reinit();
20669ed24f4bSMarc Zyngier 
20679ed24f4bSMarc Zyngier 		return NOTIFY_OK;
20689ed24f4bSMarc Zyngier 
20699ed24f4bSMarc Zyngier 	default:
20709ed24f4bSMarc Zyngier 		return NOTIFY_DONE;
20719ed24f4bSMarc Zyngier 	}
20729ed24f4bSMarc Zyngier }
20739ed24f4bSMarc Zyngier 
20749ed24f4bSMarc Zyngier static struct notifier_block hyp_init_cpu_pm_nb = {
20759ed24f4bSMarc Zyngier 	.notifier_call = hyp_init_cpu_pm_notifier,
20769ed24f4bSMarc Zyngier };
20779ed24f4bSMarc Zyngier 
207853bf620aSSean Christopherson static void __init hyp_cpu_pm_init(void)
20799ed24f4bSMarc Zyngier {
2080fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
20819ed24f4bSMarc Zyngier 		cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
20829ed24f4bSMarc Zyngier }
208353bf620aSSean Christopherson static void __init hyp_cpu_pm_exit(void)
20849ed24f4bSMarc Zyngier {
2085fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
20869ed24f4bSMarc Zyngier 		cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
20879ed24f4bSMarc Zyngier }
20889ed24f4bSMarc Zyngier #else
208953bf620aSSean Christopherson static inline void __init hyp_cpu_pm_init(void)
20909ed24f4bSMarc Zyngier {
20919ed24f4bSMarc Zyngier }
209253bf620aSSean Christopherson static inline void __init hyp_cpu_pm_exit(void)
20939ed24f4bSMarc Zyngier {
20949ed24f4bSMarc Zyngier }
20959ed24f4bSMarc Zyngier #endif
20969ed24f4bSMarc Zyngier 
209753bf620aSSean Christopherson static void __init init_cpu_logical_map(void)
209894f5e8a4SDavid Brazdil {
209994f5e8a4SDavid Brazdil 	unsigned int cpu;
210094f5e8a4SDavid Brazdil 
210194f5e8a4SDavid Brazdil 	/*
210294f5e8a4SDavid Brazdil 	 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
210321ea4578SJulia Lawall 	 * Only copy the set of online CPUs whose features have been checked
210494f5e8a4SDavid Brazdil 	 * against the finalized system capabilities. The hypervisor will not
210594f5e8a4SDavid Brazdil 	 * allow any other CPUs from the `possible` set to boot.
210694f5e8a4SDavid Brazdil 	 */
210794f5e8a4SDavid Brazdil 	for_each_online_cpu(cpu)
210861fe0c37SDavid Brazdil 		hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
210994f5e8a4SDavid Brazdil }
211094f5e8a4SDavid Brazdil 
2111767c973fSMarc Zyngier #define init_psci_0_1_impl_state(config, what)	\
2112767c973fSMarc Zyngier 	config.psci_0_1_ ## what ## _implemented = psci_ops.what
2113767c973fSMarc Zyngier 
211453bf620aSSean Christopherson static bool __init init_psci_relay(void)
2115eeeee719SDavid Brazdil {
2116eeeee719SDavid Brazdil 	/*
2117eeeee719SDavid Brazdil 	 * If PSCI has not been initialized, protected KVM cannot install
2118eeeee719SDavid Brazdil 	 * itself on newly booted CPUs.
2119eeeee719SDavid Brazdil 	 */
2120eeeee719SDavid Brazdil 	if (!psci_ops.get_version) {
2121eeeee719SDavid Brazdil 		kvm_err("Cannot initialize protected mode without PSCI\n");
2122eeeee719SDavid Brazdil 		return false;
2123eeeee719SDavid Brazdil 	}
2124eeeee719SDavid Brazdil 
2125ff367fe4SDavid Brazdil 	kvm_host_psci_config.version = psci_ops.get_version();
212612bdce4fSWill Deacon 	kvm_host_psci_config.smccc_version = arm_smccc_get_version();
2127ff367fe4SDavid Brazdil 
2128ff367fe4SDavid Brazdil 	if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
2129ff367fe4SDavid Brazdil 		kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
2130767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
2131767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
2132767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
2133767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
2134ff367fe4SDavid Brazdil 	}
2135eeeee719SDavid Brazdil 	return true;
2136eeeee719SDavid Brazdil }
2137eeeee719SDavid Brazdil 
213853bf620aSSean Christopherson static int __init init_subsystems(void)
21399ed24f4bSMarc Zyngier {
21409ed24f4bSMarc Zyngier 	int err = 0;
21419ed24f4bSMarc Zyngier 
21429ed24f4bSMarc Zyngier 	/*
21439ed24f4bSMarc Zyngier 	 * Enable hardware so that subsystem initialisation can access EL2.
21449ed24f4bSMarc Zyngier 	 */
2145733c758eSOliver Upton 	on_each_cpu(cpu_hyp_init, NULL, 1);
21469ed24f4bSMarc Zyngier 
21479ed24f4bSMarc Zyngier 	/*
21489ed24f4bSMarc Zyngier 	 * Register CPU lower-power notifier
21499ed24f4bSMarc Zyngier 	 */
21509ed24f4bSMarc Zyngier 	hyp_cpu_pm_init();
21519ed24f4bSMarc Zyngier 
21529ed24f4bSMarc Zyngier 	/*
21539ed24f4bSMarc Zyngier 	 * Init HYP view of VGIC
21549ed24f4bSMarc Zyngier 	 */
21559ed24f4bSMarc Zyngier 	err = kvm_vgic_hyp_init();
21569ed24f4bSMarc Zyngier 	switch (err) {
21579ed24f4bSMarc Zyngier 	case 0:
21589ed24f4bSMarc Zyngier 		vgic_present = true;
21599ed24f4bSMarc Zyngier 		break;
21609ed24f4bSMarc Zyngier 	case -ENODEV:
21619ed24f4bSMarc Zyngier 	case -ENXIO:
21629ed24f4bSMarc Zyngier 		vgic_present = false;
21639ed24f4bSMarc Zyngier 		err = 0;
21649ed24f4bSMarc Zyngier 		break;
21659ed24f4bSMarc Zyngier 	default:
21669ed24f4bSMarc Zyngier 		goto out;
21679ed24f4bSMarc Zyngier 	}
21689ed24f4bSMarc Zyngier 
21699ed24f4bSMarc Zyngier 	/*
21709ed24f4bSMarc Zyngier 	 * Init HYP architected timer support
21719ed24f4bSMarc Zyngier 	 */
21729ed24f4bSMarc Zyngier 	err = kvm_timer_hyp_init(vgic_present);
21739ed24f4bSMarc Zyngier 	if (err)
21749ed24f4bSMarc Zyngier 		goto out;
21759ed24f4bSMarc Zyngier 
217617ed14ebSSean Christopherson 	kvm_register_perf_callbacks(NULL);
217717ed14ebSSean Christopherson 
21789ed24f4bSMarc Zyngier out:
217978b3bf48SSean Christopherson 	if (err)
218078b3bf48SSean Christopherson 		hyp_cpu_pm_exit();
218178b3bf48SSean Christopherson 
2182fa8c3d65SDavid Brazdil 	if (err || !is_protected_kvm_enabled())
2183733c758eSOliver Upton 		on_each_cpu(cpu_hyp_uninit, NULL, 1);
21849ed24f4bSMarc Zyngier 
21859ed24f4bSMarc Zyngier 	return err;
21869ed24f4bSMarc Zyngier }
21879ed24f4bSMarc Zyngier 
218853bf620aSSean Christopherson static void __init teardown_subsystems(void)
218978b3bf48SSean Christopherson {
219078b3bf48SSean Christopherson 	kvm_unregister_perf_callbacks();
219178b3bf48SSean Christopherson 	hyp_cpu_pm_exit();
219278b3bf48SSean Christopherson }
219378b3bf48SSean Christopherson 
219453bf620aSSean Christopherson static void __init teardown_hyp_mode(void)
21959ed24f4bSMarc Zyngier {
21969ed24f4bSMarc Zyngier 	int cpu;
21979ed24f4bSMarc Zyngier 
21989ed24f4bSMarc Zyngier 	free_hyp_pgds();
219930c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
22009ed24f4bSMarc Zyngier 		free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
2201fe41a7f8SQuentin Perret 		free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
220230c95391SDavid Brazdil 	}
22039ed24f4bSMarc Zyngier }
22049ed24f4bSMarc Zyngier 
220553bf620aSSean Christopherson static int __init do_pkvm_init(u32 hyp_va_bits)
2206bfa79a80SQuentin Perret {
2207fe41a7f8SQuentin Perret 	void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
2208bfa79a80SQuentin Perret 	int ret;
2209bfa79a80SQuentin Perret 
2210bfa79a80SQuentin Perret 	preempt_disable();
22118579a185SWill Deacon 	cpu_hyp_init_context();
2212bfa79a80SQuentin Perret 	ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
2213bfa79a80SQuentin Perret 				num_possible_cpus(), kern_hyp_va(per_cpu_base),
2214bfa79a80SQuentin Perret 				hyp_va_bits);
22158579a185SWill Deacon 	cpu_hyp_init_features();
22168579a185SWill Deacon 
22178579a185SWill Deacon 	/*
22188579a185SWill Deacon 	 * The stub hypercalls are now disabled, so set our local flag to
22198579a185SWill Deacon 	 * prevent a later re-init attempt in kvm_arch_hardware_enable().
22208579a185SWill Deacon 	 */
2221733c758eSOliver Upton 	__this_cpu_write(kvm_hyp_initialized, 1);
2222bfa79a80SQuentin Perret 	preempt_enable();
2223bfa79a80SQuentin Perret 
2224bfa79a80SQuentin Perret 	return ret;
2225bfa79a80SQuentin Perret }
2226bfa79a80SQuentin Perret 
2227e8162521SFuad Tabba static u64 get_hyp_id_aa64pfr0_el1(void)
2228e8162521SFuad Tabba {
2229e8162521SFuad Tabba 	/*
2230e8162521SFuad Tabba 	 * Track whether the system isn't affected by spectre/meltdown in the
2231e8162521SFuad Tabba 	 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs.
2232e8162521SFuad Tabba 	 * Although this is per-CPU, we make it global for simplicity, e.g., not
2233e8162521SFuad Tabba 	 * to have to worry about vcpu migration.
2234e8162521SFuad Tabba 	 *
2235e8162521SFuad Tabba 	 * Unlike for non-protected VMs, userspace cannot override this for
2236e8162521SFuad Tabba 	 * protected VMs.
2237e8162521SFuad Tabba 	 */
2238e8162521SFuad Tabba 	u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
2239e8162521SFuad Tabba 
2240e8162521SFuad Tabba 	val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) |
2241e8162521SFuad Tabba 		 ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3));
2242e8162521SFuad Tabba 
2243e8162521SFuad Tabba 	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2),
2244e8162521SFuad Tabba 			  arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED);
2245e8162521SFuad Tabba 	val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3),
2246e8162521SFuad Tabba 			  arm64_get_meltdown_state() == SPECTRE_UNAFFECTED);
2247e8162521SFuad Tabba 
2248e8162521SFuad Tabba 	return val;
2249e8162521SFuad Tabba }
2250e8162521SFuad Tabba 
22516c165223SWill Deacon static void kvm_hyp_init_symbols(void)
2252bfa79a80SQuentin Perret {
2253e8162521SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1();
22546c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
22556c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
22566c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
2257def8c222SVladimir Murzin 	kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
22587c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
22597c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
22606c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
22618669651cSQuentin Perret 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
226213e248aaSWill Deacon 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
226373f38ef2SWill Deacon 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
22646c165223SWill Deacon }
22656c165223SWill Deacon 
226653bf620aSSean Christopherson static int __init kvm_hyp_init_protection(u32 hyp_va_bits)
22676c165223SWill Deacon {
22686c165223SWill Deacon 	void *addr = phys_to_virt(hyp_mem_base);
22696c165223SWill Deacon 	int ret;
22707c419937SMarc Zyngier 
2271bfa79a80SQuentin Perret 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
2272bfa79a80SQuentin Perret 	if (ret)
2273bfa79a80SQuentin Perret 		return ret;
2274bfa79a80SQuentin Perret 
2275bfa79a80SQuentin Perret 	ret = do_pkvm_init(hyp_va_bits);
2276bfa79a80SQuentin Perret 	if (ret)
2277bfa79a80SQuentin Perret 		return ret;
2278bfa79a80SQuentin Perret 
2279bfa79a80SQuentin Perret 	free_hyp_pgds();
2280bfa79a80SQuentin Perret 
2281bfa79a80SQuentin Perret 	return 0;
2282bfa79a80SQuentin Perret }
2283bfa79a80SQuentin Perret 
22848c15c2a0SMostafa Saleh static void pkvm_hyp_init_ptrauth(void)
22858c15c2a0SMostafa Saleh {
22868c15c2a0SMostafa Saleh 	struct kvm_cpu_context *hyp_ctxt;
22878c15c2a0SMostafa Saleh 	int cpu;
22888c15c2a0SMostafa Saleh 
22898c15c2a0SMostafa Saleh 	for_each_possible_cpu(cpu) {
22908c15c2a0SMostafa Saleh 		hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu);
22918c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long();
22928c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long();
22938c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long();
22948c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long();
22958c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long();
22968c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long();
22978c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long();
22988c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long();
22998c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long();
23008c15c2a0SMostafa Saleh 		hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long();
23018c15c2a0SMostafa Saleh 	}
23028c15c2a0SMostafa Saleh }
23038c15c2a0SMostafa Saleh 
230467d953d4SMarc Zyngier /* Inits Hyp-mode on all online CPUs */
230553bf620aSSean Christopherson static int __init init_hyp_mode(void)
23069ed24f4bSMarc Zyngier {
2307bfa79a80SQuentin Perret 	u32 hyp_va_bits;
23089ed24f4bSMarc Zyngier 	int cpu;
2309bfa79a80SQuentin Perret 	int err = -ENOMEM;
2310bfa79a80SQuentin Perret 
2311bfa79a80SQuentin Perret 	/*
2312bfa79a80SQuentin Perret 	 * The protected Hyp-mode cannot be initialized if the memory pool
2313bfa79a80SQuentin Perret 	 * allocation has failed.
2314bfa79a80SQuentin Perret 	 */
2315bfa79a80SQuentin Perret 	if (is_protected_kvm_enabled() && !hyp_mem_base)
2316bfa79a80SQuentin Perret 		goto out_err;
23179ed24f4bSMarc Zyngier 
23189ed24f4bSMarc Zyngier 	/*
23199ed24f4bSMarc Zyngier 	 * Allocate Hyp PGD and setup Hyp identity mapping
23209ed24f4bSMarc Zyngier 	 */
2321bfa79a80SQuentin Perret 	err = kvm_mmu_init(&hyp_va_bits);
23229ed24f4bSMarc Zyngier 	if (err)
23239ed24f4bSMarc Zyngier 		goto out_err;
23249ed24f4bSMarc Zyngier 
23259ed24f4bSMarc Zyngier 	/*
23269ed24f4bSMarc Zyngier 	 * Allocate stack pages for Hypervisor-mode
23279ed24f4bSMarc Zyngier 	 */
23289ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
23299ed24f4bSMarc Zyngier 		unsigned long stack_page;
23309ed24f4bSMarc Zyngier 
23319ed24f4bSMarc Zyngier 		stack_page = __get_free_page(GFP_KERNEL);
23329ed24f4bSMarc Zyngier 		if (!stack_page) {
23339ed24f4bSMarc Zyngier 			err = -ENOMEM;
23349ed24f4bSMarc Zyngier 			goto out_err;
23359ed24f4bSMarc Zyngier 		}
23369ed24f4bSMarc Zyngier 
23379ed24f4bSMarc Zyngier 		per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
23389ed24f4bSMarc Zyngier 	}
23399ed24f4bSMarc Zyngier 
23409ed24f4bSMarc Zyngier 	/*
234130c95391SDavid Brazdil 	 * Allocate and initialize pages for Hypervisor-mode percpu regions.
234230c95391SDavid Brazdil 	 */
234330c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
234430c95391SDavid Brazdil 		struct page *page;
234530c95391SDavid Brazdil 		void *page_addr;
234630c95391SDavid Brazdil 
234730c95391SDavid Brazdil 		page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
234830c95391SDavid Brazdil 		if (!page) {
234930c95391SDavid Brazdil 			err = -ENOMEM;
235030c95391SDavid Brazdil 			goto out_err;
235130c95391SDavid Brazdil 		}
235230c95391SDavid Brazdil 
235330c95391SDavid Brazdil 		page_addr = page_address(page);
235430c95391SDavid Brazdil 		memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
2355fe41a7f8SQuentin Perret 		kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
235630c95391SDavid Brazdil 	}
235730c95391SDavid Brazdil 
235830c95391SDavid Brazdil 	/*
23599ed24f4bSMarc Zyngier 	 * Map the Hyp-code called directly from the host
23609ed24f4bSMarc Zyngier 	 */
23619ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
23629ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
23639ed24f4bSMarc Zyngier 	if (err) {
23649ed24f4bSMarc Zyngier 		kvm_err("Cannot map world-switch code\n");
23659ed24f4bSMarc Zyngier 		goto out_err;
23669ed24f4bSMarc Zyngier 	}
23679ed24f4bSMarc Zyngier 
236816174eeaSDavid Brazdil 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
236916174eeaSDavid Brazdil 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
23702d7bf218SDavid Brazdil 	if (err) {
237116174eeaSDavid Brazdil 		kvm_err("Cannot map .hyp.rodata section\n");
23722d7bf218SDavid Brazdil 		goto out_err;
23732d7bf218SDavid Brazdil 	}
23742d7bf218SDavid Brazdil 
23759ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
23769ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
23779ed24f4bSMarc Zyngier 	if (err) {
23789ed24f4bSMarc Zyngier 		kvm_err("Cannot map rodata section\n");
23799ed24f4bSMarc Zyngier 		goto out_err;
23809ed24f4bSMarc Zyngier 	}
23819ed24f4bSMarc Zyngier 
2382380e18adSQuentin Perret 	/*
2383380e18adSQuentin Perret 	 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
2384380e18adSQuentin Perret 	 * section thanks to an assertion in the linker script. Map it RW and
2385380e18adSQuentin Perret 	 * the rest of .bss RO.
2386380e18adSQuentin Perret 	 */
2387380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
2388380e18adSQuentin Perret 				  kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2389380e18adSQuentin Perret 	if (err) {
2390380e18adSQuentin Perret 		kvm_err("Cannot map hyp bss section: %d\n", err);
2391380e18adSQuentin Perret 		goto out_err;
2392380e18adSQuentin Perret 	}
2393380e18adSQuentin Perret 
2394380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
23959ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
23969ed24f4bSMarc Zyngier 	if (err) {
23979ed24f4bSMarc Zyngier 		kvm_err("Cannot map bss section\n");
23989ed24f4bSMarc Zyngier 		goto out_err;
23999ed24f4bSMarc Zyngier 	}
24009ed24f4bSMarc Zyngier 
24019ed24f4bSMarc Zyngier 	/*
24029ed24f4bSMarc Zyngier 	 * Map the Hyp stack pages
24039ed24f4bSMarc Zyngier 	 */
24049ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2405ce335431SKalesh Singh 		struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
24069ed24f4bSMarc Zyngier 		char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
24079ed24f4bSMarc Zyngier 
2408f156a7d1SVincent Donnefort 		err = create_hyp_stack(__pa(stack_page), &params->stack_hyp_va);
24099ed24f4bSMarc Zyngier 		if (err) {
24109ed24f4bSMarc Zyngier 			kvm_err("Cannot map hyp stack\n");
24119ed24f4bSMarc Zyngier 			goto out_err;
24129ed24f4bSMarc Zyngier 		}
2413ce335431SKalesh Singh 
2414ce335431SKalesh Singh 		/*
2415ce335431SKalesh Singh 		 * Save the stack PA in nvhe_init_params. This will be needed
2416ce335431SKalesh Singh 		 * to recreate the stack mapping in protected nVHE mode.
2417ce335431SKalesh Singh 		 * __hyp_pa() won't do the right thing there, since the stack
2418ce335431SKalesh Singh 		 * has been mapped in the flexible private VA space.
2419ce335431SKalesh Singh 		 */
2420ce335431SKalesh Singh 		params->stack_pa = __pa(stack_page);
24219ed24f4bSMarc Zyngier 	}
24229ed24f4bSMarc Zyngier 
24239ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2424fe41a7f8SQuentin Perret 		char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
242530c95391SDavid Brazdil 		char *percpu_end = percpu_begin + nvhe_percpu_size();
24269ed24f4bSMarc Zyngier 
24279cc77581SQuentin Perret 		/* Map Hyp percpu pages */
242830c95391SDavid Brazdil 		err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
24299ed24f4bSMarc Zyngier 		if (err) {
243030c95391SDavid Brazdil 			kvm_err("Cannot map hyp percpu region\n");
24316e3bfbb2SAndrew Scull 			goto out_err;
24326e3bfbb2SAndrew Scull 		}
24339cc77581SQuentin Perret 
24349cc77581SQuentin Perret 		/* Prepare the CPU initialization parameters */
2435579d7ebeSRyan Roberts 		cpu_prepare_hyp_mode(cpu, hyp_va_bits);
24369ed24f4bSMarc Zyngier 	}
24379ed24f4bSMarc Zyngier 
24386c165223SWill Deacon 	kvm_hyp_init_symbols();
24396c165223SWill Deacon 
2440eeeee719SDavid Brazdil 	if (is_protected_kvm_enabled()) {
24418c15c2a0SMostafa Saleh 		if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) &&
2442d8569fbaSMark Rutland 		    cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH))
24438c15c2a0SMostafa Saleh 			pkvm_hyp_init_ptrauth();
24448c15c2a0SMostafa Saleh 
244594f5e8a4SDavid Brazdil 		init_cpu_logical_map();
244694f5e8a4SDavid Brazdil 
244752b9e265SWang Wensheng 		if (!init_psci_relay()) {
244852b9e265SWang Wensheng 			err = -ENODEV;
2449eeeee719SDavid Brazdil 			goto out_err;
2450eeeee719SDavid Brazdil 		}
2451eeeee719SDavid Brazdil 
2452bfa79a80SQuentin Perret 		err = kvm_hyp_init_protection(hyp_va_bits);
2453bfa79a80SQuentin Perret 		if (err) {
2454bfa79a80SQuentin Perret 			kvm_err("Failed to init hyp memory protection\n");
2455bfa79a80SQuentin Perret 			goto out_err;
2456bfa79a80SQuentin Perret 		}
2457bfa79a80SQuentin Perret 	}
24589ed24f4bSMarc Zyngier 
24599ed24f4bSMarc Zyngier 	return 0;
24609ed24f4bSMarc Zyngier 
24619ed24f4bSMarc Zyngier out_err:
24629ed24f4bSMarc Zyngier 	teardown_hyp_mode();
24639ed24f4bSMarc Zyngier 	kvm_err("error initializing Hyp mode: %d\n", err);
24649ed24f4bSMarc Zyngier 	return err;
24659ed24f4bSMarc Zyngier }
24669ed24f4bSMarc Zyngier 
24679ed24f4bSMarc Zyngier struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
24689ed24f4bSMarc Zyngier {
24699ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
247046808a4cSMarc Zyngier 	unsigned long i;
24719ed24f4bSMarc Zyngier 
24729ed24f4bSMarc Zyngier 	mpidr &= MPIDR_HWID_BITMASK;
247354a8006dSMarc Zyngier 
247454a8006dSMarc Zyngier 	if (kvm->arch.mpidr_data) {
247554a8006dSMarc Zyngier 		u16 idx = kvm_mpidr_index(kvm->arch.mpidr_data, mpidr);
247654a8006dSMarc Zyngier 
247754a8006dSMarc Zyngier 		vcpu = kvm_get_vcpu(kvm,
247854a8006dSMarc Zyngier 				    kvm->arch.mpidr_data->cmpidr_to_idx[idx]);
247954a8006dSMarc Zyngier 		if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu))
248054a8006dSMarc Zyngier 			vcpu = NULL;
248154a8006dSMarc Zyngier 
248254a8006dSMarc Zyngier 		return vcpu;
248354a8006dSMarc Zyngier 	}
248454a8006dSMarc Zyngier 
24859ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
24869ed24f4bSMarc Zyngier 		if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
24879ed24f4bSMarc Zyngier 			return vcpu;
24889ed24f4bSMarc Zyngier 	}
24899ed24f4bSMarc Zyngier 	return NULL;
24909ed24f4bSMarc Zyngier }
24919ed24f4bSMarc Zyngier 
2492d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2493d663b8a2SPaolo Bonzini {
2494d663b8a2SPaolo Bonzini 	return irqchip_in_kernel(kvm);
2495d663b8a2SPaolo Bonzini }
2496d663b8a2SPaolo Bonzini 
24979ed24f4bSMarc Zyngier bool kvm_arch_has_irq_bypass(void)
24989ed24f4bSMarc Zyngier {
24999ed24f4bSMarc Zyngier 	return true;
25009ed24f4bSMarc Zyngier }
25019ed24f4bSMarc Zyngier 
25029ed24f4bSMarc Zyngier int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
25039ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
25049ed24f4bSMarc Zyngier {
25059ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
25069ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
25079ed24f4bSMarc Zyngier 
25089ed24f4bSMarc Zyngier 	return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
25099ed24f4bSMarc Zyngier 					  &irqfd->irq_entry);
25109ed24f4bSMarc Zyngier }
25119ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
25129ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
25139ed24f4bSMarc Zyngier {
25149ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
25159ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
25169ed24f4bSMarc Zyngier 
25179ed24f4bSMarc Zyngier 	kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
25189ed24f4bSMarc Zyngier 				     &irqfd->irq_entry);
25199ed24f4bSMarc Zyngier }
25209ed24f4bSMarc Zyngier 
25219ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
25229ed24f4bSMarc Zyngier {
25239ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
25249ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
25259ed24f4bSMarc Zyngier 
25269ed24f4bSMarc Zyngier 	kvm_arm_halt_guest(irqfd->kvm);
25279ed24f4bSMarc Zyngier }
25289ed24f4bSMarc Zyngier 
25299ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
25309ed24f4bSMarc Zyngier {
25319ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
25329ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
25339ed24f4bSMarc Zyngier 
25349ed24f4bSMarc Zyngier 	kvm_arm_resume_guest(irqfd->kvm);
25359ed24f4bSMarc Zyngier }
25369ed24f4bSMarc Zyngier 
253767d953d4SMarc Zyngier /* Initialize Hyp-mode and memory mappings on all CPUs */
253853bf620aSSean Christopherson static __init int kvm_arm_init(void)
25399ed24f4bSMarc Zyngier {
25409ed24f4bSMarc Zyngier 	int err;
25419ed24f4bSMarc Zyngier 	bool in_hyp_mode;
25429ed24f4bSMarc Zyngier 
25439ed24f4bSMarc Zyngier 	if (!is_hyp_mode_available()) {
25449ed24f4bSMarc Zyngier 		kvm_info("HYP mode not available\n");
25459ed24f4bSMarc Zyngier 		return -ENODEV;
25469ed24f4bSMarc Zyngier 	}
25479ed24f4bSMarc Zyngier 
2548b6a68b97SMarc Zyngier 	if (kvm_get_mode() == KVM_MODE_NONE) {
2549b6a68b97SMarc Zyngier 		kvm_info("KVM disabled from command line\n");
2550b6a68b97SMarc Zyngier 		return -ENODEV;
2551b6a68b97SMarc Zyngier 	}
2552b6a68b97SMarc Zyngier 
2553f1f0c0cfSAlexandru Elisei 	err = kvm_sys_reg_table_init();
2554f1f0c0cfSAlexandru Elisei 	if (err) {
2555f1f0c0cfSAlexandru Elisei 		kvm_info("Error initializing system register tables");
2556f1f0c0cfSAlexandru Elisei 		return err;
2557f1f0c0cfSAlexandru Elisei 	}
2558f1f0c0cfSAlexandru Elisei 
25599ed24f4bSMarc Zyngier 	in_hyp_mode = is_kernel_in_hyp_mode();
25609ed24f4bSMarc Zyngier 
256196d389caSRob Herring 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
256296d389caSRob Herring 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2563abf532ccSRob Herring 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2564abf532ccSRob Herring 			 "Only trusted guests should be used on this system.\n");
2565abf532ccSRob Herring 
2566bf249d9eSAnshuman Khandual 	err = kvm_set_ipa_limit();
25679ed24f4bSMarc Zyngier 	if (err)
25689ed24f4bSMarc Zyngier 		return err;
25699ed24f4bSMarc Zyngier 
25709ed24f4bSMarc Zyngier 	err = kvm_arm_init_sve();
25719ed24f4bSMarc Zyngier 	if (err)
25729ed24f4bSMarc Zyngier 		return err;
25739ed24f4bSMarc Zyngier 
25743248136bSJulien Grall 	err = kvm_arm_vmid_alloc_init();
25753248136bSJulien Grall 	if (err) {
25763248136bSJulien Grall 		kvm_err("Failed to initialize VMID allocator.\n");
25773248136bSJulien Grall 		return err;
25783248136bSJulien Grall 	}
25793248136bSJulien Grall 
25809ed24f4bSMarc Zyngier 	if (!in_hyp_mode) {
25819ed24f4bSMarc Zyngier 		err = init_hyp_mode();
25829ed24f4bSMarc Zyngier 		if (err)
25839ed24f4bSMarc Zyngier 			goto out_err;
25849ed24f4bSMarc Zyngier 	}
25859ed24f4bSMarc Zyngier 
2586b881cdceSWill Deacon 	err = kvm_init_vector_slots();
2587b881cdceSWill Deacon 	if (err) {
2588b881cdceSWill Deacon 		kvm_err("Cannot initialise vector slots\n");
25896baaeda8SSean Christopherson 		goto out_hyp;
2590b881cdceSWill Deacon 	}
2591b881cdceSWill Deacon 
25929ed24f4bSMarc Zyngier 	err = init_subsystems();
25939ed24f4bSMarc Zyngier 	if (err)
259478b3bf48SSean Christopherson 		goto out_hyp;
25959ed24f4bSMarc Zyngier 
2596f19f6644SDavid Brazdil 	if (is_protected_kvm_enabled()) {
25973eb681fbSDavid Brazdil 		kvm_info("Protected nVHE mode initialized successfully\n");
2598f19f6644SDavid Brazdil 	} else if (in_hyp_mode) {
25999ed24f4bSMarc Zyngier 		kvm_info("VHE mode initialized successfully\n");
2600f19f6644SDavid Brazdil 	} else {
26019ed24f4bSMarc Zyngier 		kvm_info("Hyp mode initialized successfully\n");
2602f19f6644SDavid Brazdil 	}
26039ed24f4bSMarc Zyngier 
26041dc0f02dSSean Christopherson 	/*
26051dc0f02dSSean Christopherson 	 * FIXME: Do something reasonable if kvm_init() fails after pKVM
26061dc0f02dSSean Christopherson 	 * hypervisor protection is finalized.
26071dc0f02dSSean Christopherson 	 */
260881a1cf9fSSean Christopherson 	err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
26091dc0f02dSSean Christopherson 	if (err)
26101dc0f02dSSean Christopherson 		goto out_subs;
26111dc0f02dSSean Christopherson 
2612fa729bc7SSudeep Holla 	kvm_arm_initialised = true;
2613fa729bc7SSudeep Holla 
26149ed24f4bSMarc Zyngier 	return 0;
26159ed24f4bSMarc Zyngier 
26166baaeda8SSean Christopherson out_subs:
261778b3bf48SSean Christopherson 	teardown_subsystems();
26186baaeda8SSean Christopherson out_hyp:
26199ed24f4bSMarc Zyngier 	if (!in_hyp_mode)
26209ed24f4bSMarc Zyngier 		teardown_hyp_mode();
26219ed24f4bSMarc Zyngier out_err:
26223248136bSJulien Grall 	kvm_arm_vmid_alloc_free();
26239ed24f4bSMarc Zyngier 	return err;
26249ed24f4bSMarc Zyngier }
26259ed24f4bSMarc Zyngier 
2626d8b369c4SDavid Brazdil static int __init early_kvm_mode_cfg(char *arg)
2627d8b369c4SDavid Brazdil {
2628d8b369c4SDavid Brazdil 	if (!arg)
2629d8b369c4SDavid Brazdil 		return -EINVAL;
2630d8b369c4SDavid Brazdil 
2631b2a4d007SElliot Berman 	if (strcmp(arg, "none") == 0) {
2632b2a4d007SElliot Berman 		kvm_mode = KVM_MODE_NONE;
2633b2a4d007SElliot Berman 		return 0;
2634b2a4d007SElliot Berman 	}
2635b2a4d007SElliot Berman 
2636b2a4d007SElliot Berman 	if (!is_hyp_mode_available()) {
2637b2a4d007SElliot Berman 		pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2638b2a4d007SElliot Berman 		return 0;
2639b2a4d007SElliot Berman 	}
2640b2a4d007SElliot Berman 
2641d8b369c4SDavid Brazdil 	if (strcmp(arg, "protected") == 0) {
2642cde5042aSWill Deacon 		if (!is_kernel_in_hyp_mode())
2643d8b369c4SDavid Brazdil 			kvm_mode = KVM_MODE_PROTECTED;
2644cde5042aSWill Deacon 		else
2645cde5042aSWill Deacon 			pr_warn_once("Protected KVM not available with VHE\n");
2646cde5042aSWill Deacon 
2647d8b369c4SDavid Brazdil 		return 0;
2648d8b369c4SDavid Brazdil 	}
2649d8b369c4SDavid Brazdil 
2650b6a68b97SMarc Zyngier 	if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2651b6a68b97SMarc Zyngier 		kvm_mode = KVM_MODE_DEFAULT;
26521945a067SMarc Zyngier 		return 0;
2653b6a68b97SMarc Zyngier 	}
2654b6a68b97SMarc Zyngier 
2655675cabc8SJintack Lim 	if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) {
2656675cabc8SJintack Lim 		kvm_mode = KVM_MODE_NV;
2657675cabc8SJintack Lim 		return 0;
2658675cabc8SJintack Lim 	}
2659675cabc8SJintack Lim 
2660d8b369c4SDavid Brazdil 	return -EINVAL;
2661d8b369c4SDavid Brazdil }
2662d8b369c4SDavid Brazdil early_param("kvm-arm.mode", early_kvm_mode_cfg);
2663d8b369c4SDavid Brazdil 
26643eb681fbSDavid Brazdil enum kvm_mode kvm_get_mode(void)
26653eb681fbSDavid Brazdil {
26663eb681fbSDavid Brazdil 	return kvm_mode;
26673eb681fbSDavid Brazdil }
26683eb681fbSDavid Brazdil 
26691dc0f02dSSean Christopherson module_init(kvm_arm_init);
2670