xref: /linux/arch/arm64/kvm/arm.c (revision 8669651ce0d90a79bab22dfe2758fe07e070a02e)
19ed24f4bSMarc Zyngier // SPDX-License-Identifier: GPL-2.0-only
29ed24f4bSMarc Zyngier /*
39ed24f4bSMarc Zyngier  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
49ed24f4bSMarc Zyngier  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
59ed24f4bSMarc Zyngier  */
69ed24f4bSMarc Zyngier 
79ed24f4bSMarc Zyngier #include <linux/bug.h>
89ed24f4bSMarc Zyngier #include <linux/cpu_pm.h>
96caa5812SOliver Upton #include <linux/entry-kvm.h>
109ed24f4bSMarc Zyngier #include <linux/errno.h>
119ed24f4bSMarc Zyngier #include <linux/err.h>
129ed24f4bSMarc Zyngier #include <linux/kvm_host.h>
139ed24f4bSMarc Zyngier #include <linux/list.h>
149ed24f4bSMarc Zyngier #include <linux/module.h>
159ed24f4bSMarc Zyngier #include <linux/vmalloc.h>
169ed24f4bSMarc Zyngier #include <linux/fs.h>
179ed24f4bSMarc Zyngier #include <linux/mman.h>
189ed24f4bSMarc Zyngier #include <linux/sched.h>
1947e6223cSMarc Zyngier #include <linux/kmemleak.h>
209ed24f4bSMarc Zyngier #include <linux/kvm.h>
219ed24f4bSMarc Zyngier #include <linux/kvm_irqfd.h>
229ed24f4bSMarc Zyngier #include <linux/irqbypass.h>
239ed24f4bSMarc Zyngier #include <linux/sched/stat.h>
24eeeee719SDavid Brazdil #include <linux/psci.h>
259ed24f4bSMarc Zyngier #include <trace/events/kvm.h>
269ed24f4bSMarc Zyngier 
279ed24f4bSMarc Zyngier #define CREATE_TRACE_POINTS
289ed24f4bSMarc Zyngier #include "trace_arm.h"
299ed24f4bSMarc Zyngier 
309ed24f4bSMarc Zyngier #include <linux/uaccess.h>
319ed24f4bSMarc Zyngier #include <asm/ptrace.h>
329ed24f4bSMarc Zyngier #include <asm/mman.h>
339ed24f4bSMarc Zyngier #include <asm/tlbflush.h>
349ed24f4bSMarc Zyngier #include <asm/cacheflush.h>
359ed24f4bSMarc Zyngier #include <asm/cpufeature.h>
369ed24f4bSMarc Zyngier #include <asm/virt.h>
379ed24f4bSMarc Zyngier #include <asm/kvm_arm.h>
389ed24f4bSMarc Zyngier #include <asm/kvm_asm.h>
399ed24f4bSMarc Zyngier #include <asm/kvm_mmu.h>
409d0c063aSFuad Tabba #include <asm/kvm_pkvm.h>
419ed24f4bSMarc Zyngier #include <asm/kvm_emulate.h>
429ed24f4bSMarc Zyngier #include <asm/sections.h>
439ed24f4bSMarc Zyngier 
449ed24f4bSMarc Zyngier #include <kvm/arm_hypercalls.h>
459ed24f4bSMarc Zyngier #include <kvm/arm_pmu.h>
469ed24f4bSMarc Zyngier #include <kvm/arm_psci.h>
479ed24f4bSMarc Zyngier 
48d8b369c4SDavid Brazdil static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
49f19f6644SDavid Brazdil DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
50d8b369c4SDavid Brazdil 
5114ef9d04SMarc Zyngier DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
5214ef9d04SMarc Zyngier 
53db129d48SKalesh Singh DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
5463fec243SDavid Brazdil DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
559ed24f4bSMarc Zyngier 
569ed24f4bSMarc Zyngier static bool vgic_present;
579ed24f4bSMarc Zyngier 
589ed24f4bSMarc Zyngier static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
599ed24f4bSMarc Zyngier DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
609ed24f4bSMarc Zyngier 
619ed24f4bSMarc Zyngier int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
629ed24f4bSMarc Zyngier {
639ed24f4bSMarc Zyngier 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
649ed24f4bSMarc Zyngier }
659ed24f4bSMarc Zyngier 
669ed24f4bSMarc Zyngier int kvm_arch_hardware_setup(void *opaque)
679ed24f4bSMarc Zyngier {
689ed24f4bSMarc Zyngier 	return 0;
699ed24f4bSMarc Zyngier }
709ed24f4bSMarc Zyngier 
719ed24f4bSMarc Zyngier int kvm_arch_check_processor_compat(void *opaque)
729ed24f4bSMarc Zyngier {
739ed24f4bSMarc Zyngier 	return 0;
749ed24f4bSMarc Zyngier }
759ed24f4bSMarc Zyngier 
769ed24f4bSMarc Zyngier int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
779ed24f4bSMarc Zyngier 			    struct kvm_enable_cap *cap)
789ed24f4bSMarc Zyngier {
799ed24f4bSMarc Zyngier 	int r;
809ed24f4bSMarc Zyngier 
819ed24f4bSMarc Zyngier 	if (cap->flags)
829ed24f4bSMarc Zyngier 		return -EINVAL;
839ed24f4bSMarc Zyngier 
849ed24f4bSMarc Zyngier 	switch (cap->cap) {
859ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
869ed24f4bSMarc Zyngier 		r = 0;
8706394531SMarc Zyngier 		set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
8806394531SMarc Zyngier 			&kvm->arch.flags);
899ed24f4bSMarc Zyngier 		break;
90673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
91c4d7c518SSteven Price 		mutex_lock(&kvm->lock);
92c4d7c518SSteven Price 		if (!system_supports_mte() || kvm->created_vcpus) {
93c4d7c518SSteven Price 			r = -EINVAL;
94c4d7c518SSteven Price 		} else {
95673638f4SSteven Price 			r = 0;
9606394531SMarc Zyngier 			set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
97c4d7c518SSteven Price 		}
98c4d7c518SSteven Price 		mutex_unlock(&kvm->lock);
99673638f4SSteven Price 		break;
100bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
101bfbab445SOliver Upton 		r = 0;
102bfbab445SOliver Upton 		set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
103bfbab445SOliver Upton 		break;
1049ed24f4bSMarc Zyngier 	default:
1059ed24f4bSMarc Zyngier 		r = -EINVAL;
1069ed24f4bSMarc Zyngier 		break;
1079ed24f4bSMarc Zyngier 	}
1089ed24f4bSMarc Zyngier 
1099ed24f4bSMarc Zyngier 	return r;
1109ed24f4bSMarc Zyngier }
1119ed24f4bSMarc Zyngier 
1125107000fSMarc Zyngier static int kvm_arm_default_max_vcpus(void)
1135107000fSMarc Zyngier {
1145107000fSMarc Zyngier 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
1155107000fSMarc Zyngier }
1165107000fSMarc Zyngier 
1174f1df628SMarc Zyngier static void set_default_spectre(struct kvm *kvm)
11823711a5eSMarc Zyngier {
11923711a5eSMarc Zyngier 	/*
12023711a5eSMarc Zyngier 	 * The default is to expose CSV2 == 1 if the HW isn't affected.
12123711a5eSMarc Zyngier 	 * Although this is a per-CPU feature, we make it global because
12223711a5eSMarc Zyngier 	 * asymmetric systems are just a nuisance.
12323711a5eSMarc Zyngier 	 *
12423711a5eSMarc Zyngier 	 * Userspace can override this as long as it doesn't promise
12523711a5eSMarc Zyngier 	 * the impossible.
12623711a5eSMarc Zyngier 	 */
12723711a5eSMarc Zyngier 	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
12823711a5eSMarc Zyngier 		kvm->arch.pfr0_csv2 = 1;
1294f1df628SMarc Zyngier 	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
1304f1df628SMarc Zyngier 		kvm->arch.pfr0_csv3 = 1;
13123711a5eSMarc Zyngier }
13223711a5eSMarc Zyngier 
1339ed24f4bSMarc Zyngier /**
1349ed24f4bSMarc Zyngier  * kvm_arch_init_vm - initializes a VM data structure
1359ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1369ed24f4bSMarc Zyngier  */
1379ed24f4bSMarc Zyngier int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1389ed24f4bSMarc Zyngier {
139a0e50aa3SChristoffer Dall 	int ret;
1409ed24f4bSMarc Zyngier 
1413f868e14SQuentin Perret 	ret = kvm_share_hyp(kvm, kvm + 1);
1429ed24f4bSMarc Zyngier 	if (ret)
143315775ffSQuentin Perret 		return ret;
1449ed24f4bSMarc Zyngier 
1459d0c063aSFuad Tabba 	ret = pkvm_init_host_vm(kvm);
1469d0c063aSFuad Tabba 	if (ret)
147315775ffSQuentin Perret 		goto err_unshare_kvm;
1489ed24f4bSMarc Zyngier 
149ae187fecSWill Deacon 	if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL)) {
150ae187fecSWill Deacon 		ret = -ENOMEM;
151315775ffSQuentin Perret 		goto err_unshare_kvm;
152ae187fecSWill Deacon 	}
153583cda1bSAlexandru Elisei 	cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask);
154583cda1bSAlexandru Elisei 
155315775ffSQuentin Perret 	ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type);
156315775ffSQuentin Perret 	if (ret)
157315775ffSQuentin Perret 		goto err_free_cpumask;
158315775ffSQuentin Perret 
1599ed24f4bSMarc Zyngier 	kvm_vgic_early_init(kvm);
1609ed24f4bSMarc Zyngier 
1619ed24f4bSMarc Zyngier 	/* The maximum number of VCPUs is limited by the host's GIC model */
162f502cc56SSean Christopherson 	kvm->max_vcpus = kvm_arm_default_max_vcpus();
1639ed24f4bSMarc Zyngier 
1644f1df628SMarc Zyngier 	set_default_spectre(kvm);
16505714cabSRaghavendra Rao Ananta 	kvm_arm_init_hypercalls(kvm);
16623711a5eSMarc Zyngier 
1673d0dba57SMarc Zyngier 	/*
1683d0dba57SMarc Zyngier 	 * Initialise the default PMUver before there is a chance to
1693d0dba57SMarc Zyngier 	 * create an actual PMU.
1703d0dba57SMarc Zyngier 	 */
1713d0dba57SMarc Zyngier 	kvm->arch.dfr0_pmuver.imp = kvm_arm_pmu_get_pmuver_limit();
1723d0dba57SMarc Zyngier 
173315775ffSQuentin Perret 	return 0;
174315775ffSQuentin Perret 
175315775ffSQuentin Perret err_free_cpumask:
176315775ffSQuentin Perret 	free_cpumask_var(kvm->arch.supported_cpus);
177315775ffSQuentin Perret err_unshare_kvm:
178315775ffSQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
1799ed24f4bSMarc Zyngier 	return ret;
1809ed24f4bSMarc Zyngier }
1819ed24f4bSMarc Zyngier 
1829ed24f4bSMarc Zyngier vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1839ed24f4bSMarc Zyngier {
1849ed24f4bSMarc Zyngier 	return VM_FAULT_SIGBUS;
1859ed24f4bSMarc Zyngier }
1869ed24f4bSMarc Zyngier 
1879ed24f4bSMarc Zyngier 
1889ed24f4bSMarc Zyngier /**
1899ed24f4bSMarc Zyngier  * kvm_arch_destroy_vm - destroy the VM data structure
1909ed24f4bSMarc Zyngier  * @kvm:	pointer to the KVM struct
1919ed24f4bSMarc Zyngier  */
1929ed24f4bSMarc Zyngier void kvm_arch_destroy_vm(struct kvm *kvm)
1939ed24f4bSMarc Zyngier {
194d7eec236SMarc Zyngier 	bitmap_free(kvm->arch.pmu_filter);
195583cda1bSAlexandru Elisei 	free_cpumask_var(kvm->arch.supported_cpus);
196d7eec236SMarc Zyngier 
1979ed24f4bSMarc Zyngier 	kvm_vgic_destroy(kvm);
1989ed24f4bSMarc Zyngier 
1999d0c063aSFuad Tabba 	if (is_protected_kvm_enabled())
2009d0c063aSFuad Tabba 		pkvm_destroy_hyp_vm(kvm);
2019d0c063aSFuad Tabba 
20227592ae8SMarc Zyngier 	kvm_destroy_vcpus(kvm);
20352b28657SQuentin Perret 
20452b28657SQuentin Perret 	kvm_unshare_hyp(kvm, kvm + 1);
2059ed24f4bSMarc Zyngier }
2069ed24f4bSMarc Zyngier 
2079ed24f4bSMarc Zyngier int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
2089ed24f4bSMarc Zyngier {
2099ed24f4bSMarc Zyngier 	int r;
2109ed24f4bSMarc Zyngier 	switch (ext) {
2119ed24f4bSMarc Zyngier 	case KVM_CAP_IRQCHIP:
2129ed24f4bSMarc Zyngier 		r = vgic_present;
2139ed24f4bSMarc Zyngier 		break;
2149ed24f4bSMarc Zyngier 	case KVM_CAP_IOEVENTFD:
2159ed24f4bSMarc Zyngier 	case KVM_CAP_DEVICE_CTRL:
2169ed24f4bSMarc Zyngier 	case KVM_CAP_USER_MEMORY:
2179ed24f4bSMarc Zyngier 	case KVM_CAP_SYNC_MMU:
2189ed24f4bSMarc Zyngier 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
2199ed24f4bSMarc Zyngier 	case KVM_CAP_ONE_REG:
2209ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI:
2219ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_PSCI_0_2:
2229ed24f4bSMarc Zyngier 	case KVM_CAP_READONLY_MEM:
2239ed24f4bSMarc Zyngier 	case KVM_CAP_MP_STATE:
2249ed24f4bSMarc Zyngier 	case KVM_CAP_IMMEDIATE_EXIT:
2259ed24f4bSMarc Zyngier 	case KVM_CAP_VCPU_EVENTS:
2269ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
2279ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_NISV_TO_USER:
2289ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_INJECT_EXT_DABT:
22936fb4cd5SWill Deacon 	case KVM_CAP_SET_GUEST_DEBUG:
23036fb4cd5SWill Deacon 	case KVM_CAP_VCPU_ATTRIBUTES:
2313bf72569SJianyong Wu 	case KVM_CAP_PTP_KVM:
232bfbab445SOliver Upton 	case KVM_CAP_ARM_SYSTEM_SUSPEND:
2339ed24f4bSMarc Zyngier 		r = 1;
2349ed24f4bSMarc Zyngier 		break;
235fa18aca9SMaxim Levitsky 	case KVM_CAP_SET_GUEST_DEBUG2:
236fa18aca9SMaxim Levitsky 		return KVM_GUESTDBG_VALID_MASK;
2379ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
2389ed24f4bSMarc Zyngier 		r = 1;
2399ed24f4bSMarc Zyngier 		break;
2409ed24f4bSMarc Zyngier 	case KVM_CAP_NR_VCPUS:
241f60a00d7SVitaly Kuznetsov 		/*
242f60a00d7SVitaly Kuznetsov 		 * ARM64 treats KVM_CAP_NR_CPUS differently from all other
243f60a00d7SVitaly Kuznetsov 		 * architectures, as it does not always bound it to
244f60a00d7SVitaly Kuznetsov 		 * KVM_CAP_MAX_VCPUS. It should not matter much because
245f60a00d7SVitaly Kuznetsov 		 * this is just an advisory value.
246f60a00d7SVitaly Kuznetsov 		 */
247f60a00d7SVitaly Kuznetsov 		r = min_t(unsigned int, num_online_cpus(),
248f60a00d7SVitaly Kuznetsov 			  kvm_arm_default_max_vcpus());
2499ed24f4bSMarc Zyngier 		break;
2509ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPUS:
2519ed24f4bSMarc Zyngier 	case KVM_CAP_MAX_VCPU_ID:
2525107000fSMarc Zyngier 		if (kvm)
253f502cc56SSean Christopherson 			r = kvm->max_vcpus;
2545107000fSMarc Zyngier 		else
2555107000fSMarc Zyngier 			r = kvm_arm_default_max_vcpus();
2569ed24f4bSMarc Zyngier 		break;
2579ed24f4bSMarc Zyngier 	case KVM_CAP_MSI_DEVID:
2589ed24f4bSMarc Zyngier 		if (!kvm)
2599ed24f4bSMarc Zyngier 			r = -EINVAL;
2609ed24f4bSMarc Zyngier 		else
2619ed24f4bSMarc Zyngier 			r = kvm->arch.vgic.msis_require_devid;
2629ed24f4bSMarc Zyngier 		break;
2639ed24f4bSMarc Zyngier 	case KVM_CAP_ARM_USER_IRQ:
2649ed24f4bSMarc Zyngier 		/*
2659ed24f4bSMarc Zyngier 		 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
2669ed24f4bSMarc Zyngier 		 * (bump this number if adding more devices)
2679ed24f4bSMarc Zyngier 		 */
2689ed24f4bSMarc Zyngier 		r = 1;
2699ed24f4bSMarc Zyngier 		break;
270673638f4SSteven Price 	case KVM_CAP_ARM_MTE:
271673638f4SSteven Price 		r = system_supports_mte();
272673638f4SSteven Price 		break;
273004a0124SAndrew Jones 	case KVM_CAP_STEAL_TIME:
274004a0124SAndrew Jones 		r = kvm_arm_pvtime_supported();
275004a0124SAndrew Jones 		break;
27636fb4cd5SWill Deacon 	case KVM_CAP_ARM_EL1_32BIT:
27736fb4cd5SWill Deacon 		r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
2789ed24f4bSMarc Zyngier 		break;
27936fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
28036fb4cd5SWill Deacon 		r = get_num_brps();
28136fb4cd5SWill Deacon 		break;
28236fb4cd5SWill Deacon 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
28336fb4cd5SWill Deacon 		r = get_num_wrps();
28436fb4cd5SWill Deacon 		break;
28536fb4cd5SWill Deacon 	case KVM_CAP_ARM_PMU_V3:
28636fb4cd5SWill Deacon 		r = kvm_arm_support_pmu_v3();
28736fb4cd5SWill Deacon 		break;
28836fb4cd5SWill Deacon 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
28936fb4cd5SWill Deacon 		r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
29036fb4cd5SWill Deacon 		break;
29136fb4cd5SWill Deacon 	case KVM_CAP_ARM_VM_IPA_SIZE:
29236fb4cd5SWill Deacon 		r = get_kvm_ipa_limit();
29336fb4cd5SWill Deacon 		break;
29436fb4cd5SWill Deacon 	case KVM_CAP_ARM_SVE:
29536fb4cd5SWill Deacon 		r = system_supports_sve();
29636fb4cd5SWill Deacon 		break;
29736fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
29836fb4cd5SWill Deacon 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
29936fb4cd5SWill Deacon 		r = system_has_full_ptr_auth();
30036fb4cd5SWill Deacon 		break;
30136fb4cd5SWill Deacon 	default:
30236fb4cd5SWill Deacon 		r = 0;
3039ed24f4bSMarc Zyngier 	}
30436fb4cd5SWill Deacon 
3059ed24f4bSMarc Zyngier 	return r;
3069ed24f4bSMarc Zyngier }
3079ed24f4bSMarc Zyngier 
3089ed24f4bSMarc Zyngier long kvm_arch_dev_ioctl(struct file *filp,
3099ed24f4bSMarc Zyngier 			unsigned int ioctl, unsigned long arg)
3109ed24f4bSMarc Zyngier {
3119ed24f4bSMarc Zyngier 	return -EINVAL;
3129ed24f4bSMarc Zyngier }
3139ed24f4bSMarc Zyngier 
3149ed24f4bSMarc Zyngier struct kvm *kvm_arch_alloc_vm(void)
3159ed24f4bSMarc Zyngier {
316115bae92SJia He 	size_t sz = sizeof(struct kvm);
3179ed24f4bSMarc Zyngier 
318115bae92SJia He 	if (!has_vhe())
319115bae92SJia He 		return kzalloc(sz, GFP_KERNEL_ACCOUNT);
320115bae92SJia He 
321115bae92SJia He 	return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO);
3229ed24f4bSMarc Zyngier }
3239ed24f4bSMarc Zyngier 
3249ed24f4bSMarc Zyngier int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3259ed24f4bSMarc Zyngier {
3269ed24f4bSMarc Zyngier 	if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
3279ed24f4bSMarc Zyngier 		return -EBUSY;
3289ed24f4bSMarc Zyngier 
329f502cc56SSean Christopherson 	if (id >= kvm->max_vcpus)
3309ed24f4bSMarc Zyngier 		return -EINVAL;
3319ed24f4bSMarc Zyngier 
3329ed24f4bSMarc Zyngier 	return 0;
3339ed24f4bSMarc Zyngier }
3349ed24f4bSMarc Zyngier 
3359ed24f4bSMarc Zyngier int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3369ed24f4bSMarc Zyngier {
3379ed24f4bSMarc Zyngier 	int err;
3389ed24f4bSMarc Zyngier 
3399ed24f4bSMarc Zyngier 	/* Force users to call KVM_ARM_VCPU_INIT */
3409ed24f4bSMarc Zyngier 	vcpu->arch.target = -1;
3419ed24f4bSMarc Zyngier 	bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
3429ed24f4bSMarc Zyngier 
343e539451bSSean Christopherson 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
344e539451bSSean Christopherson 
345b4da9187SMarc Zyngier 	/*
346b4da9187SMarc Zyngier 	 * Default value for the FP state, will be overloaded at load
347b4da9187SMarc Zyngier 	 * time if we support FP (pretty likely)
348b4da9187SMarc Zyngier 	 */
349b4da9187SMarc Zyngier 	vcpu->arch.fp_state = FP_STATE_FREE;
350b4da9187SMarc Zyngier 
3519ed24f4bSMarc Zyngier 	/* Set up the timer */
3529ed24f4bSMarc Zyngier 	kvm_timer_vcpu_init(vcpu);
3539ed24f4bSMarc Zyngier 
3549ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_init(vcpu);
3559ed24f4bSMarc Zyngier 
3569ed24f4bSMarc Zyngier 	kvm_arm_reset_debug_ptr(vcpu);
3579ed24f4bSMarc Zyngier 
3589ed24f4bSMarc Zyngier 	kvm_arm_pvtime_vcpu_init(&vcpu->arch);
3599ed24f4bSMarc Zyngier 
360a0e50aa3SChristoffer Dall 	vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
361a0e50aa3SChristoffer Dall 
3629ed24f4bSMarc Zyngier 	err = kvm_vgic_vcpu_init(vcpu);
3639ed24f4bSMarc Zyngier 	if (err)
3649ed24f4bSMarc Zyngier 		return err;
3659ed24f4bSMarc Zyngier 
3663f868e14SQuentin Perret 	return kvm_share_hyp(vcpu, vcpu + 1);
3679ed24f4bSMarc Zyngier }
3689ed24f4bSMarc Zyngier 
3699ed24f4bSMarc Zyngier void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
3709ed24f4bSMarc Zyngier {
3719ed24f4bSMarc Zyngier }
3729ed24f4bSMarc Zyngier 
3739ed24f4bSMarc Zyngier void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3749ed24f4bSMarc Zyngier {
375cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm)))
3769ed24f4bSMarc Zyngier 		static_branch_dec(&userspace_irqchip_in_use);
3779ed24f4bSMarc Zyngier 
3789af3e08bSWill Deacon 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
3799ed24f4bSMarc Zyngier 	kvm_timer_vcpu_terminate(vcpu);
3809ed24f4bSMarc Zyngier 	kvm_pmu_vcpu_destroy(vcpu);
3819ed24f4bSMarc Zyngier 
3829ed24f4bSMarc Zyngier 	kvm_arm_vcpu_destroy(vcpu);
3839ed24f4bSMarc Zyngier }
3849ed24f4bSMarc Zyngier 
3859ed24f4bSMarc Zyngier void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
3869ed24f4bSMarc Zyngier {
3876109c5a6SSean Christopherson 
3889ed24f4bSMarc Zyngier }
3899ed24f4bSMarc Zyngier 
3909ed24f4bSMarc Zyngier void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
3919ed24f4bSMarc Zyngier {
3926109c5a6SSean Christopherson 
3939ed24f4bSMarc Zyngier }
3949ed24f4bSMarc Zyngier 
3959ed24f4bSMarc Zyngier void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3969ed24f4bSMarc Zyngier {
397a0e50aa3SChristoffer Dall 	struct kvm_s2_mmu *mmu;
3989ed24f4bSMarc Zyngier 	int *last_ran;
3999ed24f4bSMarc Zyngier 
400a0e50aa3SChristoffer Dall 	mmu = vcpu->arch.hw_mmu;
401a0e50aa3SChristoffer Dall 	last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
4029ed24f4bSMarc Zyngier 
4039ed24f4bSMarc Zyngier 	/*
40401dc9262SMarc Zyngier 	 * We guarantee that both TLBs and I-cache are private to each
40501dc9262SMarc Zyngier 	 * vcpu. If detecting that a vcpu from the same VM has
40601dc9262SMarc Zyngier 	 * previously run on the same physical CPU, call into the
40701dc9262SMarc Zyngier 	 * hypervisor code to nuke the relevant contexts.
40801dc9262SMarc Zyngier 	 *
4099ed24f4bSMarc Zyngier 	 * We might get preempted before the vCPU actually runs, but
4109ed24f4bSMarc Zyngier 	 * over-invalidation doesn't affect correctness.
4119ed24f4bSMarc Zyngier 	 */
4129ed24f4bSMarc Zyngier 	if (*last_ran != vcpu->vcpu_id) {
41301dc9262SMarc Zyngier 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
4149ed24f4bSMarc Zyngier 		*last_ran = vcpu->vcpu_id;
4159ed24f4bSMarc Zyngier 	}
4169ed24f4bSMarc Zyngier 
4179ed24f4bSMarc Zyngier 	vcpu->cpu = cpu;
4189ed24f4bSMarc Zyngier 
4199ed24f4bSMarc Zyngier 	kvm_vgic_load(vcpu);
4209ed24f4bSMarc Zyngier 	kvm_timer_vcpu_load(vcpu);
42113aeb9b4SDavid Brazdil 	if (has_vhe())
42213aeb9b4SDavid Brazdil 		kvm_vcpu_load_sysregs_vhe(vcpu);
4239ed24f4bSMarc Zyngier 	kvm_arch_vcpu_load_fp(vcpu);
4249ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_guest(vcpu);
4259ed24f4bSMarc Zyngier 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
4269ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
4279ed24f4bSMarc Zyngier 
4289ed24f4bSMarc Zyngier 	if (single_task_running())
4299ed24f4bSMarc Zyngier 		vcpu_clear_wfx_traps(vcpu);
4309ed24f4bSMarc Zyngier 	else
4319ed24f4bSMarc Zyngier 		vcpu_set_wfx_traps(vcpu);
4329ed24f4bSMarc Zyngier 
43329eb5a3cSMarc Zyngier 	if (vcpu_has_ptrauth(vcpu))
434ef3e40a7SMarc Zyngier 		vcpu_ptrauth_disable(vcpu);
435d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_load_debug_state_flags(vcpu);
436583cda1bSAlexandru Elisei 
437583cda1bSAlexandru Elisei 	if (!cpumask_test_cpu(smp_processor_id(), vcpu->kvm->arch.supported_cpus))
438583cda1bSAlexandru Elisei 		vcpu_set_on_unsupported_cpu(vcpu);
4399ed24f4bSMarc Zyngier }
4409ed24f4bSMarc Zyngier 
4419ed24f4bSMarc Zyngier void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
4429ed24f4bSMarc Zyngier {
443d2602bb4SSuzuki K Poulose 	kvm_arch_vcpu_put_debug_state_flags(vcpu);
4449ed24f4bSMarc Zyngier 	kvm_arch_vcpu_put_fp(vcpu);
44513aeb9b4SDavid Brazdil 	if (has_vhe())
44613aeb9b4SDavid Brazdil 		kvm_vcpu_put_sysregs_vhe(vcpu);
4479ed24f4bSMarc Zyngier 	kvm_timer_vcpu_put(vcpu);
4489ed24f4bSMarc Zyngier 	kvm_vgic_put(vcpu);
4499ed24f4bSMarc Zyngier 	kvm_vcpu_pmu_restore_host(vcpu);
450100b4f09SShameer Kolothum 	kvm_arm_vmid_clear_active();
4519ed24f4bSMarc Zyngier 
452583cda1bSAlexandru Elisei 	vcpu_clear_on_unsupported_cpu(vcpu);
4539ed24f4bSMarc Zyngier 	vcpu->cpu = -1;
4549ed24f4bSMarc Zyngier }
4559ed24f4bSMarc Zyngier 
4561e579429SOliver Upton void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)
4579ed24f4bSMarc Zyngier {
458b171f9bbSOliver Upton 	vcpu->arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
4599ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
4609ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
4619ed24f4bSMarc Zyngier }
4629ed24f4bSMarc Zyngier 
463b171f9bbSOliver Upton bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu)
464b171f9bbSOliver Upton {
465b171f9bbSOliver Upton 	return vcpu->arch.mp_state.mp_state == KVM_MP_STATE_STOPPED;
466b171f9bbSOliver Upton }
467b171f9bbSOliver Upton 
4687b33a09dSOliver Upton static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)
4697b33a09dSOliver Upton {
4707b33a09dSOliver Upton 	vcpu->arch.mp_state.mp_state = KVM_MP_STATE_SUSPENDED;
4717b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
4727b33a09dSOliver Upton 	kvm_vcpu_kick(vcpu);
4737b33a09dSOliver Upton }
4747b33a09dSOliver Upton 
4757b33a09dSOliver Upton static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
4767b33a09dSOliver Upton {
4777b33a09dSOliver Upton 	return vcpu->arch.mp_state.mp_state == KVM_MP_STATE_SUSPENDED;
4787b33a09dSOliver Upton }
4797b33a09dSOliver Upton 
4809ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4819ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
4829ed24f4bSMarc Zyngier {
483b171f9bbSOliver Upton 	*mp_state = vcpu->arch.mp_state;
4849ed24f4bSMarc Zyngier 
4859ed24f4bSMarc Zyngier 	return 0;
4869ed24f4bSMarc Zyngier }
4879ed24f4bSMarc Zyngier 
4889ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4899ed24f4bSMarc Zyngier 				    struct kvm_mp_state *mp_state)
4909ed24f4bSMarc Zyngier {
4919ed24f4bSMarc Zyngier 	int ret = 0;
4929ed24f4bSMarc Zyngier 
4939ed24f4bSMarc Zyngier 	switch (mp_state->mp_state) {
4949ed24f4bSMarc Zyngier 	case KVM_MP_STATE_RUNNABLE:
495b171f9bbSOliver Upton 		vcpu->arch.mp_state = *mp_state;
4969ed24f4bSMarc Zyngier 		break;
4979ed24f4bSMarc Zyngier 	case KVM_MP_STATE_STOPPED:
4981e579429SOliver Upton 		kvm_arm_vcpu_power_off(vcpu);
4999ed24f4bSMarc Zyngier 		break;
5007b33a09dSOliver Upton 	case KVM_MP_STATE_SUSPENDED:
5017b33a09dSOliver Upton 		kvm_arm_vcpu_suspend(vcpu);
5029ed24f4bSMarc Zyngier 		break;
5039ed24f4bSMarc Zyngier 	default:
5049ed24f4bSMarc Zyngier 		ret = -EINVAL;
5059ed24f4bSMarc Zyngier 	}
5069ed24f4bSMarc Zyngier 
5079ed24f4bSMarc Zyngier 	return ret;
5089ed24f4bSMarc Zyngier }
5099ed24f4bSMarc Zyngier 
5109ed24f4bSMarc Zyngier /**
5119ed24f4bSMarc Zyngier  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
5129ed24f4bSMarc Zyngier  * @v:		The VCPU pointer
5139ed24f4bSMarc Zyngier  *
5149ed24f4bSMarc Zyngier  * If the guest CPU is not waiting for interrupts or an interrupt line is
5159ed24f4bSMarc Zyngier  * asserted, the CPU is by definition runnable.
5169ed24f4bSMarc Zyngier  */
5179ed24f4bSMarc Zyngier int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
5189ed24f4bSMarc Zyngier {
5199ed24f4bSMarc Zyngier 	bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
5209ed24f4bSMarc Zyngier 	return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
521b171f9bbSOliver Upton 		&& !kvm_arm_vcpu_stopped(v) && !v->arch.pause);
5229ed24f4bSMarc Zyngier }
5239ed24f4bSMarc Zyngier 
5249ed24f4bSMarc Zyngier bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
5259ed24f4bSMarc Zyngier {
5269ed24f4bSMarc Zyngier 	return vcpu_mode_priv(vcpu);
5279ed24f4bSMarc Zyngier }
5289ed24f4bSMarc Zyngier 
5298e5b0adeSLinus Torvalds #ifdef CONFIG_GUEST_PERF_EVENTS
530e1bfc245SSean Christopherson unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
531e1bfc245SSean Christopherson {
532e1bfc245SSean Christopherson 	return *vcpu_pc(vcpu);
533e1bfc245SSean Christopherson }
5348e5b0adeSLinus Torvalds #endif
535e1bfc245SSean Christopherson 
536b5aa368aSMarc Zyngier static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
537052f064dSMarc Zyngier {
538b5aa368aSMarc Zyngier 	return vcpu->arch.target >= 0;
539052f064dSMarc Zyngier }
540052f064dSMarc Zyngier 
541b5aa368aSMarc Zyngier /*
542b5aa368aSMarc Zyngier  * Handle both the initialisation that is being done when the vcpu is
543b5aa368aSMarc Zyngier  * run for the first time, as well as the updates that must be
544b5aa368aSMarc Zyngier  * performed each time we get a new thread dealing with this vcpu.
545b5aa368aSMarc Zyngier  */
546b5aa368aSMarc Zyngier int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
5479ed24f4bSMarc Zyngier {
5489ed24f4bSMarc Zyngier 	struct kvm *kvm = vcpu->kvm;
549b5aa368aSMarc Zyngier 	int ret;
5509ed24f4bSMarc Zyngier 
551b5aa368aSMarc Zyngier 	if (!kvm_vcpu_initialized(vcpu))
552b5aa368aSMarc Zyngier 		return -ENOEXEC;
5539ed24f4bSMarc Zyngier 
5549ed24f4bSMarc Zyngier 	if (!kvm_arm_vcpu_is_finalized(vcpu))
5559ed24f4bSMarc Zyngier 		return -EPERM;
5569ed24f4bSMarc Zyngier 
557b5aa368aSMarc Zyngier 	ret = kvm_arch_vcpu_run_map_fp(vcpu);
558b5aa368aSMarc Zyngier 	if (ret)
559b5aa368aSMarc Zyngier 		return ret;
560b5aa368aSMarc Zyngier 
561cc5705fbSMarc Zyngier 	if (likely(vcpu_has_run_once(vcpu)))
562b5aa368aSMarc Zyngier 		return 0;
5639ed24f4bSMarc Zyngier 
564263d6287SAlexandru Elisei 	kvm_arm_vcpu_init_debug(vcpu);
565263d6287SAlexandru Elisei 
5669ed24f4bSMarc Zyngier 	if (likely(irqchip_in_kernel(kvm))) {
5679ed24f4bSMarc Zyngier 		/*
5689ed24f4bSMarc Zyngier 		 * Map the VGIC hardware resources before running a vcpu the
5699ed24f4bSMarc Zyngier 		 * first time on this VM.
5709ed24f4bSMarc Zyngier 		 */
5719ed24f4bSMarc Zyngier 		ret = kvm_vgic_map_resources(kvm);
5729ed24f4bSMarc Zyngier 		if (ret)
5739ed24f4bSMarc Zyngier 			return ret;
5749ed24f4bSMarc Zyngier 	}
5759ed24f4bSMarc Zyngier 
5769ed24f4bSMarc Zyngier 	ret = kvm_timer_enable(vcpu);
5779ed24f4bSMarc Zyngier 	if (ret)
5789ed24f4bSMarc Zyngier 		return ret;
5799ed24f4bSMarc Zyngier 
5809ed24f4bSMarc Zyngier 	ret = kvm_arm_pmu_v3_enable(vcpu);
5811408e73dSMarc Zyngier 	if (ret)
5821408e73dSMarc Zyngier 		return ret;
5831408e73dSMarc Zyngier 
5849d0c063aSFuad Tabba 	if (is_protected_kvm_enabled()) {
5859d0c063aSFuad Tabba 		ret = pkvm_create_hyp_vm(kvm);
5869d0c063aSFuad Tabba 		if (ret)
5879d0c063aSFuad Tabba 			return ret;
5889d0c063aSFuad Tabba 	}
5899d0c063aSFuad Tabba 
5901408e73dSMarc Zyngier 	if (!irqchip_in_kernel(kvm)) {
5911408e73dSMarc Zyngier 		/*
5921408e73dSMarc Zyngier 		 * Tell the rest of the code that there are userspace irqchip
5931408e73dSMarc Zyngier 		 * VMs in the wild.
5941408e73dSMarc Zyngier 		 */
5951408e73dSMarc Zyngier 		static_branch_inc(&userspace_irqchip_in_use);
5961408e73dSMarc Zyngier 	}
5979ed24f4bSMarc Zyngier 
5982a0c3433SFuad Tabba 	/*
5992a0c3433SFuad Tabba 	 * Initialize traps for protected VMs.
6002a0c3433SFuad Tabba 	 * NOTE: Move to run in EL2 directly, rather than via a hypercall, once
6012a0c3433SFuad Tabba 	 * the code is in place for first run initialization at EL2.
6022a0c3433SFuad Tabba 	 */
6032a0c3433SFuad Tabba 	if (kvm_vm_is_protected(kvm))
6042a0c3433SFuad Tabba 		kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu);
6052a0c3433SFuad Tabba 
6065177fe91SMarc Zyngier 	mutex_lock(&kvm->lock);
60706394531SMarc Zyngier 	set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags);
6085177fe91SMarc Zyngier 	mutex_unlock(&kvm->lock);
6095177fe91SMarc Zyngier 
6109ed24f4bSMarc Zyngier 	return ret;
6119ed24f4bSMarc Zyngier }
6129ed24f4bSMarc Zyngier 
6139ed24f4bSMarc Zyngier bool kvm_arch_intc_initialized(struct kvm *kvm)
6149ed24f4bSMarc Zyngier {
6159ed24f4bSMarc Zyngier 	return vgic_initialized(kvm);
6169ed24f4bSMarc Zyngier }
6179ed24f4bSMarc Zyngier 
6189ed24f4bSMarc Zyngier void kvm_arm_halt_guest(struct kvm *kvm)
6199ed24f4bSMarc Zyngier {
62046808a4cSMarc Zyngier 	unsigned long i;
6219ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
6229ed24f4bSMarc Zyngier 
6239ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm)
6249ed24f4bSMarc Zyngier 		vcpu->arch.pause = true;
6259ed24f4bSMarc Zyngier 	kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
6269ed24f4bSMarc Zyngier }
6279ed24f4bSMarc Zyngier 
6289ed24f4bSMarc Zyngier void kvm_arm_resume_guest(struct kvm *kvm)
6299ed24f4bSMarc Zyngier {
63046808a4cSMarc Zyngier 	unsigned long i;
6319ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
6329ed24f4bSMarc Zyngier 
6339ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
6349ed24f4bSMarc Zyngier 		vcpu->arch.pause = false;
635d92a5d1cSSean Christopherson 		__kvm_vcpu_wake_up(vcpu);
6369ed24f4bSMarc Zyngier 	}
6379ed24f4bSMarc Zyngier }
6389ed24f4bSMarc Zyngier 
6391c6219e3SOliver Upton static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
6409ed24f4bSMarc Zyngier {
64138060944SPaolo Bonzini 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
6429ed24f4bSMarc Zyngier 
64338060944SPaolo Bonzini 	rcuwait_wait_event(wait,
644b171f9bbSOliver Upton 			   (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause),
64538060944SPaolo Bonzini 			   TASK_INTERRUPTIBLE);
6469ed24f4bSMarc Zyngier 
647b171f9bbSOliver Upton 	if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) {
6489ed24f4bSMarc Zyngier 		/* Awaken to handle a signal, request we sleep again later. */
6499ed24f4bSMarc Zyngier 		kvm_make_request(KVM_REQ_SLEEP, vcpu);
6509ed24f4bSMarc Zyngier 	}
6519ed24f4bSMarc Zyngier 
6529ed24f4bSMarc Zyngier 	/*
6539ed24f4bSMarc Zyngier 	 * Make sure we will observe a potential reset request if we've
6549ed24f4bSMarc Zyngier 	 * observed a change to the power state. Pairs with the smp_wmb() in
6559ed24f4bSMarc Zyngier 	 * kvm_psci_vcpu_on().
6569ed24f4bSMarc Zyngier 	 */
6579ed24f4bSMarc Zyngier 	smp_rmb();
6589ed24f4bSMarc Zyngier }
6599ed24f4bSMarc Zyngier 
6606109c5a6SSean Christopherson /**
6616109c5a6SSean Christopherson  * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior
6626109c5a6SSean Christopherson  * @vcpu:	The VCPU pointer
6636109c5a6SSean Christopherson  *
6646109c5a6SSean Christopherson  * Suspend execution of a vCPU until a valid wake event is detected, i.e. until
6656109c5a6SSean Christopherson  * the vCPU is runnable.  The vCPU may or may not be scheduled out, depending
6666109c5a6SSean Christopherson  * on when a wake event arrives, e.g. there may already be a pending wake event.
6676109c5a6SSean Christopherson  */
6686109c5a6SSean Christopherson void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)
6696109c5a6SSean Christopherson {
6706109c5a6SSean Christopherson 	/*
6716109c5a6SSean Christopherson 	 * Sync back the state of the GIC CPU interface so that we have
6726109c5a6SSean Christopherson 	 * the latest PMR and group enables. This ensures that
6736109c5a6SSean Christopherson 	 * kvm_arch_vcpu_runnable has up-to-date data to decide whether
6746109c5a6SSean Christopherson 	 * we have pending interrupts, e.g. when determining if the
6756109c5a6SSean Christopherson 	 * vCPU should block.
6766109c5a6SSean Christopherson 	 *
6776109c5a6SSean Christopherson 	 * For the same reason, we want to tell GICv4 that we need
6786109c5a6SSean Christopherson 	 * doorbells to be signalled, should an interrupt become pending.
6796109c5a6SSean Christopherson 	 */
6806109c5a6SSean Christopherson 	preempt_disable();
6816109c5a6SSean Christopherson 	kvm_vgic_vmcr_sync(vcpu);
6826109c5a6SSean Christopherson 	vgic_v4_put(vcpu, true);
6836109c5a6SSean Christopherson 	preempt_enable();
6846109c5a6SSean Christopherson 
68591b99ea7SSean Christopherson 	kvm_vcpu_halt(vcpu);
686eebc538dSMarc Zyngier 	vcpu_clear_flag(vcpu, IN_WFIT);
6876109c5a6SSean Christopherson 
6886109c5a6SSean Christopherson 	preempt_disable();
6896109c5a6SSean Christopherson 	vgic_v4_load(vcpu);
6906109c5a6SSean Christopherson 	preempt_enable();
6916109c5a6SSean Christopherson }
6926109c5a6SSean Christopherson 
6937b33a09dSOliver Upton static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
6947b33a09dSOliver Upton {
6957b33a09dSOliver Upton 	if (!kvm_arm_vcpu_suspended(vcpu))
6967b33a09dSOliver Upton 		return 1;
6977b33a09dSOliver Upton 
6987b33a09dSOliver Upton 	kvm_vcpu_wfi(vcpu);
6997b33a09dSOliver Upton 
7007b33a09dSOliver Upton 	/*
7017b33a09dSOliver Upton 	 * The suspend state is sticky; we do not leave it until userspace
7027b33a09dSOliver Upton 	 * explicitly marks the vCPU as runnable. Request that we suspend again
7037b33a09dSOliver Upton 	 * later.
7047b33a09dSOliver Upton 	 */
7057b33a09dSOliver Upton 	kvm_make_request(KVM_REQ_SUSPEND, vcpu);
7067b33a09dSOliver Upton 
7077b33a09dSOliver Upton 	/*
7087b33a09dSOliver Upton 	 * Check to make sure the vCPU is actually runnable. If so, exit to
7097b33a09dSOliver Upton 	 * userspace informing it of the wakeup condition.
7107b33a09dSOliver Upton 	 */
7117b33a09dSOliver Upton 	if (kvm_arch_vcpu_runnable(vcpu)) {
7127b33a09dSOliver Upton 		memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
7137b33a09dSOliver Upton 		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP;
7147b33a09dSOliver Upton 		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
7157b33a09dSOliver Upton 		return 0;
7167b33a09dSOliver Upton 	}
7177b33a09dSOliver Upton 
7187b33a09dSOliver Upton 	/*
7197b33a09dSOliver Upton 	 * Otherwise, we were unblocked to process a different event, such as a
7207b33a09dSOliver Upton 	 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to
7217b33a09dSOliver Upton 	 * process the event.
7227b33a09dSOliver Upton 	 */
7237b33a09dSOliver Upton 	return 1;
7247b33a09dSOliver Upton }
7257b33a09dSOliver Upton 
7263fdd0459SOliver Upton /**
7273fdd0459SOliver Upton  * check_vcpu_requests - check and handle pending vCPU requests
7283fdd0459SOliver Upton  * @vcpu:	the VCPU pointer
7293fdd0459SOliver Upton  *
7303fdd0459SOliver Upton  * Return: 1 if we should enter the guest
7313fdd0459SOliver Upton  *	   0 if we should exit to userspace
7323fdd0459SOliver Upton  *	   < 0 if we should exit to userspace, where the return value indicates
7333fdd0459SOliver Upton  *	   an error
7343fdd0459SOliver Upton  */
7353fdd0459SOliver Upton static int check_vcpu_requests(struct kvm_vcpu *vcpu)
7369ed24f4bSMarc Zyngier {
7379ed24f4bSMarc Zyngier 	if (kvm_request_pending(vcpu)) {
7389ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
7391c6219e3SOliver Upton 			kvm_vcpu_sleep(vcpu);
7409ed24f4bSMarc Zyngier 
7419ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
7429ed24f4bSMarc Zyngier 			kvm_reset_vcpu(vcpu);
7439ed24f4bSMarc Zyngier 
7449ed24f4bSMarc Zyngier 		/*
7459ed24f4bSMarc Zyngier 		 * Clear IRQ_PENDING requests that were made to guarantee
7469ed24f4bSMarc Zyngier 		 * that a VCPU sees new virtual interrupts.
7479ed24f4bSMarc Zyngier 		 */
7489ed24f4bSMarc Zyngier 		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
7499ed24f4bSMarc Zyngier 
7509ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
7519ed24f4bSMarc Zyngier 			kvm_update_stolen_time(vcpu);
7529ed24f4bSMarc Zyngier 
7539ed24f4bSMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
7549ed24f4bSMarc Zyngier 			/* The distributor enable bits were changed */
7559ed24f4bSMarc Zyngier 			preempt_disable();
7569ed24f4bSMarc Zyngier 			vgic_v4_put(vcpu, false);
7579ed24f4bSMarc Zyngier 			vgic_v4_load(vcpu);
7589ed24f4bSMarc Zyngier 			preempt_enable();
7599ed24f4bSMarc Zyngier 		}
760d0c94c49SMarc Zyngier 
761d0c94c49SMarc Zyngier 		if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu))
762d0c94c49SMarc Zyngier 			kvm_pmu_handle_pmcr(vcpu,
763d0c94c49SMarc Zyngier 					    __vcpu_sys_reg(vcpu, PMCR_EL0));
7647b33a09dSOliver Upton 
7657b33a09dSOliver Upton 		if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
7667b33a09dSOliver Upton 			return kvm_vcpu_suspend(vcpu);
7679cb1096fSGavin Shan 
7689cb1096fSGavin Shan 		if (kvm_dirty_ring_check_request(vcpu))
7699cb1096fSGavin Shan 			return 0;
7709ed24f4bSMarc Zyngier 	}
7713fdd0459SOliver Upton 
7723fdd0459SOliver Upton 	return 1;
7739ed24f4bSMarc Zyngier }
7749ed24f4bSMarc Zyngier 
7752f6a49bbSWill Deacon static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
7762f6a49bbSWill Deacon {
7772f6a49bbSWill Deacon 	if (likely(!vcpu_mode_is_32bit(vcpu)))
7782f6a49bbSWill Deacon 		return false;
7792f6a49bbSWill Deacon 
780f3c6efc7SOliver Upton 	return !kvm_supports_32bit_el0();
7812f6a49bbSWill Deacon }
7822f6a49bbSWill Deacon 
7839ed24f4bSMarc Zyngier /**
7846caa5812SOliver Upton  * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
7856caa5812SOliver Upton  * @vcpu:	The VCPU pointer
7866caa5812SOliver Upton  * @ret:	Pointer to write optional return code
7876caa5812SOliver Upton  *
7886caa5812SOliver Upton  * Returns: true if the VCPU needs to return to a preemptible + interruptible
7896caa5812SOliver Upton  *	    and skip guest entry.
7906caa5812SOliver Upton  *
7916caa5812SOliver Upton  * This function disambiguates between two different types of exits: exits to a
7926caa5812SOliver Upton  * preemptible + interruptible kernel context and exits to userspace. For an
7936caa5812SOliver Upton  * exit to userspace, this function will write the return code to ret and return
7946caa5812SOliver Upton  * true. For an exit to preemptible + interruptible kernel context (i.e. check
7956caa5812SOliver Upton  * for pending work and re-enter), return true without writing to ret.
7966caa5812SOliver Upton  */
7976caa5812SOliver Upton static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
7986caa5812SOliver Upton {
7996caa5812SOliver Upton 	struct kvm_run *run = vcpu->run;
8006caa5812SOliver Upton 
8016caa5812SOliver Upton 	/*
8026caa5812SOliver Upton 	 * If we're using a userspace irqchip, then check if we need
8036caa5812SOliver Upton 	 * to tell a userspace irqchip about timer or PMU level
8046caa5812SOliver Upton 	 * changes and if so, exit to userspace (the actual level
8056caa5812SOliver Upton 	 * state gets updated in kvm_timer_update_run and
8066caa5812SOliver Upton 	 * kvm_pmu_update_run below).
8076caa5812SOliver Upton 	 */
8086caa5812SOliver Upton 	if (static_branch_unlikely(&userspace_irqchip_in_use)) {
8096caa5812SOliver Upton 		if (kvm_timer_should_notify_user(vcpu) ||
8106caa5812SOliver Upton 		    kvm_pmu_should_notify_user(vcpu)) {
8116caa5812SOliver Upton 			*ret = -EINTR;
8126caa5812SOliver Upton 			run->exit_reason = KVM_EXIT_INTR;
8136caa5812SOliver Upton 			return true;
8146caa5812SOliver Upton 		}
8156caa5812SOliver Upton 	}
8166caa5812SOliver Upton 
817583cda1bSAlexandru Elisei 	if (unlikely(vcpu_on_unsupported_cpu(vcpu))) {
818583cda1bSAlexandru Elisei 		run->exit_reason = KVM_EXIT_FAIL_ENTRY;
819583cda1bSAlexandru Elisei 		run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED;
820583cda1bSAlexandru Elisei 		run->fail_entry.cpu = smp_processor_id();
821583cda1bSAlexandru Elisei 		*ret = 0;
822583cda1bSAlexandru Elisei 		return true;
823583cda1bSAlexandru Elisei 	}
824583cda1bSAlexandru Elisei 
8256caa5812SOliver Upton 	return kvm_request_pending(vcpu) ||
8266caa5812SOliver Upton 			xfer_to_guest_mode_work_pending();
8276caa5812SOliver Upton }
8286caa5812SOliver Upton 
8298cfe148aSMark Rutland /*
8308cfe148aSMark Rutland  * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while
8318cfe148aSMark Rutland  * the vCPU is running.
8328cfe148aSMark Rutland  *
8338cfe148aSMark Rutland  * This must be noinstr as instrumentation may make use of RCU, and this is not
8348cfe148aSMark Rutland  * safe during the EQS.
8358cfe148aSMark Rutland  */
8368cfe148aSMark Rutland static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
8378cfe148aSMark Rutland {
8388cfe148aSMark Rutland 	int ret;
8398cfe148aSMark Rutland 
8408cfe148aSMark Rutland 	guest_state_enter_irqoff();
8418cfe148aSMark Rutland 	ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
8428cfe148aSMark Rutland 	guest_state_exit_irqoff();
8438cfe148aSMark Rutland 
8448cfe148aSMark Rutland 	return ret;
8458cfe148aSMark Rutland }
8468cfe148aSMark Rutland 
8476caa5812SOliver Upton /**
8489ed24f4bSMarc Zyngier  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
8499ed24f4bSMarc Zyngier  * @vcpu:	The VCPU pointer
8509ed24f4bSMarc Zyngier  *
8519ed24f4bSMarc Zyngier  * This function is called through the VCPU_RUN ioctl called from user space. It
8529ed24f4bSMarc Zyngier  * will execute VM code in a loop until the time slice for the process is used
8539ed24f4bSMarc Zyngier  * or some emulation is needed from user space in which case the function will
8549ed24f4bSMarc Zyngier  * return with return value 0 and with the kvm_run structure filled in with the
8559ed24f4bSMarc Zyngier  * required data for the requested emulation.
8569ed24f4bSMarc Zyngier  */
85738060944SPaolo Bonzini int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
8589ed24f4bSMarc Zyngier {
85938060944SPaolo Bonzini 	struct kvm_run *run = vcpu->run;
8609ed24f4bSMarc Zyngier 	int ret;
8619ed24f4bSMarc Zyngier 
8629ed24f4bSMarc Zyngier 	if (run->exit_reason == KVM_EXIT_MMIO) {
86374cc7e0cSTianjia Zhang 		ret = kvm_handle_mmio_return(vcpu);
8649ed24f4bSMarc Zyngier 		if (ret)
8659ed24f4bSMarc Zyngier 			return ret;
8669ed24f4bSMarc Zyngier 	}
8679ed24f4bSMarc Zyngier 
8689ed24f4bSMarc Zyngier 	vcpu_load(vcpu);
8699ed24f4bSMarc Zyngier 
870e3e880bbSZenghui Yu 	if (run->immediate_exit) {
871e3e880bbSZenghui Yu 		ret = -EINTR;
872e3e880bbSZenghui Yu 		goto out;
873e3e880bbSZenghui Yu 	}
874e3e880bbSZenghui Yu 
8759ed24f4bSMarc Zyngier 	kvm_sigset_activate(vcpu);
8769ed24f4bSMarc Zyngier 
8779ed24f4bSMarc Zyngier 	ret = 1;
8789ed24f4bSMarc Zyngier 	run->exit_reason = KVM_EXIT_UNKNOWN;
87918f3976fSAlexandru Elisei 	run->flags = 0;
8809ed24f4bSMarc Zyngier 	while (ret > 0) {
8819ed24f4bSMarc Zyngier 		/*
8829ed24f4bSMarc Zyngier 		 * Check conditions before entering the guest
8839ed24f4bSMarc Zyngier 		 */
8846caa5812SOliver Upton 		ret = xfer_to_guest_mode_handle_work(vcpu);
8856caa5812SOliver Upton 		if (!ret)
8866caa5812SOliver Upton 			ret = 1;
8879ed24f4bSMarc Zyngier 
8883fdd0459SOliver Upton 		if (ret > 0)
8893fdd0459SOliver Upton 			ret = check_vcpu_requests(vcpu);
8909ed24f4bSMarc Zyngier 
8919ed24f4bSMarc Zyngier 		/*
8929ed24f4bSMarc Zyngier 		 * Preparing the interrupts to be injected also
8939ed24f4bSMarc Zyngier 		 * involves poking the GIC, which must be done in a
8949ed24f4bSMarc Zyngier 		 * non-preemptible context.
8959ed24f4bSMarc Zyngier 		 */
8969ed24f4bSMarc Zyngier 		preempt_disable();
8979ed24f4bSMarc Zyngier 
8983248136bSJulien Grall 		/*
8993248136bSJulien Grall 		 * The VMID allocator only tracks active VMIDs per
9003248136bSJulien Grall 		 * physical CPU, and therefore the VMID allocated may not be
9013248136bSJulien Grall 		 * preserved on VMID roll-over if the task was preempted,
9023248136bSJulien Grall 		 * making a thread's VMID inactive. So we need to call
9033248136bSJulien Grall 		 * kvm_arm_vmid_update() in non-premptible context.
9043248136bSJulien Grall 		 */
9053248136bSJulien Grall 		kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid);
9063248136bSJulien Grall 
9079ed24f4bSMarc Zyngier 		kvm_pmu_flush_hwstate(vcpu);
9089ed24f4bSMarc Zyngier 
9099ed24f4bSMarc Zyngier 		local_irq_disable();
9109ed24f4bSMarc Zyngier 
9119ed24f4bSMarc Zyngier 		kvm_vgic_flush_hwstate(vcpu);
9129ed24f4bSMarc Zyngier 
91384d751a0SFuad Tabba 		kvm_pmu_update_vcpu_events(vcpu);
91484d751a0SFuad Tabba 
9159ed24f4bSMarc Zyngier 		/*
9169ed24f4bSMarc Zyngier 		 * Ensure we set mode to IN_GUEST_MODE after we disable
9179ed24f4bSMarc Zyngier 		 * interrupts and before the final VCPU requests check.
9189ed24f4bSMarc Zyngier 		 * See the comment in kvm_vcpu_exiting_guest_mode() and
9199ed24f4bSMarc Zyngier 		 * Documentation/virt/kvm/vcpu-requests.rst
9209ed24f4bSMarc Zyngier 		 */
9219ed24f4bSMarc Zyngier 		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
9229ed24f4bSMarc Zyngier 
9236caa5812SOliver Upton 		if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
9249ed24f4bSMarc Zyngier 			vcpu->mode = OUTSIDE_GUEST_MODE;
9259ed24f4bSMarc Zyngier 			isb(); /* Ensure work in x_flush_hwstate is committed */
9269ed24f4bSMarc Zyngier 			kvm_pmu_sync_hwstate(vcpu);
9279ed24f4bSMarc Zyngier 			if (static_branch_unlikely(&userspace_irqchip_in_use))
9283c5ff0c6SMarc Zyngier 				kvm_timer_sync_user(vcpu);
9299ed24f4bSMarc Zyngier 			kvm_vgic_sync_hwstate(vcpu);
9309ed24f4bSMarc Zyngier 			local_irq_enable();
9319ed24f4bSMarc Zyngier 			preempt_enable();
9329ed24f4bSMarc Zyngier 			continue;
9339ed24f4bSMarc Zyngier 		}
9349ed24f4bSMarc Zyngier 
9359ed24f4bSMarc Zyngier 		kvm_arm_setup_debug(vcpu);
936af9a0e21SMarc Zyngier 		kvm_arch_vcpu_ctxflush_fp(vcpu);
9379ed24f4bSMarc Zyngier 
9389ed24f4bSMarc Zyngier 		/**************************************************************
9399ed24f4bSMarc Zyngier 		 * Enter the guest
9409ed24f4bSMarc Zyngier 		 */
9419ed24f4bSMarc Zyngier 		trace_kvm_entry(*vcpu_pc(vcpu));
9428cfe148aSMark Rutland 		guest_timing_enter_irqoff();
9439ed24f4bSMarc Zyngier 
9448cfe148aSMark Rutland 		ret = kvm_arm_vcpu_enter_exit(vcpu);
9459ed24f4bSMarc Zyngier 
9469ed24f4bSMarc Zyngier 		vcpu->mode = OUTSIDE_GUEST_MODE;
9479ed24f4bSMarc Zyngier 		vcpu->stat.exits++;
9489ed24f4bSMarc Zyngier 		/*
9499ed24f4bSMarc Zyngier 		 * Back from guest
9509ed24f4bSMarc Zyngier 		 *************************************************************/
9519ed24f4bSMarc Zyngier 
9529ed24f4bSMarc Zyngier 		kvm_arm_clear_debug(vcpu);
9539ed24f4bSMarc Zyngier 
9549ed24f4bSMarc Zyngier 		/*
9559ed24f4bSMarc Zyngier 		 * We must sync the PMU state before the vgic state so
9569ed24f4bSMarc Zyngier 		 * that the vgic can properly sample the updated state of the
9579ed24f4bSMarc Zyngier 		 * interrupt line.
9589ed24f4bSMarc Zyngier 		 */
9599ed24f4bSMarc Zyngier 		kvm_pmu_sync_hwstate(vcpu);
9609ed24f4bSMarc Zyngier 
9619ed24f4bSMarc Zyngier 		/*
9629ed24f4bSMarc Zyngier 		 * Sync the vgic state before syncing the timer state because
9639ed24f4bSMarc Zyngier 		 * the timer code needs to know if the virtual timer
9649ed24f4bSMarc Zyngier 		 * interrupts are active.
9659ed24f4bSMarc Zyngier 		 */
9669ed24f4bSMarc Zyngier 		kvm_vgic_sync_hwstate(vcpu);
9679ed24f4bSMarc Zyngier 
9689ed24f4bSMarc Zyngier 		/*
9699ed24f4bSMarc Zyngier 		 * Sync the timer hardware state before enabling interrupts as
9709ed24f4bSMarc Zyngier 		 * we don't want vtimer interrupts to race with syncing the
9719ed24f4bSMarc Zyngier 		 * timer virtual interrupt state.
9729ed24f4bSMarc Zyngier 		 */
9739ed24f4bSMarc Zyngier 		if (static_branch_unlikely(&userspace_irqchip_in_use))
9743c5ff0c6SMarc Zyngier 			kvm_timer_sync_user(vcpu);
9759ed24f4bSMarc Zyngier 
9769ed24f4bSMarc Zyngier 		kvm_arch_vcpu_ctxsync_fp(vcpu);
9779ed24f4bSMarc Zyngier 
9789ed24f4bSMarc Zyngier 		/*
9798cfe148aSMark Rutland 		 * We must ensure that any pending interrupts are taken before
9808cfe148aSMark Rutland 		 * we exit guest timing so that timer ticks are accounted as
9818cfe148aSMark Rutland 		 * guest time. Transiently unmask interrupts so that any
9828cfe148aSMark Rutland 		 * pending interrupts are taken.
9839ed24f4bSMarc Zyngier 		 *
9848cfe148aSMark Rutland 		 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other
9858cfe148aSMark Rutland 		 * context synchronization event) is necessary to ensure that
9868cfe148aSMark Rutland 		 * pending interrupts are taken.
9879ed24f4bSMarc Zyngier 		 */
988f7659f8bSMarc Zyngier 		if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) {
9899ed24f4bSMarc Zyngier 			local_irq_enable();
9908cfe148aSMark Rutland 			isb();
9918cfe148aSMark Rutland 			local_irq_disable();
992f7659f8bSMarc Zyngier 		}
9939ed24f4bSMarc Zyngier 
9948cfe148aSMark Rutland 		guest_timing_exit_irqoff();
9958cfe148aSMark Rutland 
9968cfe148aSMark Rutland 		local_irq_enable();
9978cfe148aSMark Rutland 
9989ed24f4bSMarc Zyngier 		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
9999ed24f4bSMarc Zyngier 
10009ed24f4bSMarc Zyngier 		/* Exit types that need handling before we can be preempted */
100174cc7e0cSTianjia Zhang 		handle_exit_early(vcpu, ret);
10029ed24f4bSMarc Zyngier 
10039ed24f4bSMarc Zyngier 		preempt_enable();
10049ed24f4bSMarc Zyngier 
100522f55384SQais Yousef 		/*
100622f55384SQais Yousef 		 * The ARMv8 architecture doesn't give the hypervisor
100722f55384SQais Yousef 		 * a mechanism to prevent a guest from dropping to AArch32 EL0
100822f55384SQais Yousef 		 * if implemented by the CPU. If we spot the guest in such
100922f55384SQais Yousef 		 * state and that we decided it wasn't supposed to do so (like
101022f55384SQais Yousef 		 * with the asymmetric AArch32 case), return to userspace with
101122f55384SQais Yousef 		 * a fatal error.
101222f55384SQais Yousef 		 */
10132f6a49bbSWill Deacon 		if (vcpu_mode_is_bad_32bit(vcpu)) {
101422f55384SQais Yousef 			/*
101522f55384SQais Yousef 			 * As we have caught the guest red-handed, decide that
101622f55384SQais Yousef 			 * it isn't fit for purpose anymore by making the vcpu
101722f55384SQais Yousef 			 * invalid. The VMM can try and fix it by issuing  a
101822f55384SQais Yousef 			 * KVM_ARM_VCPU_INIT if it really wants to.
101922f55384SQais Yousef 			 */
102022f55384SQais Yousef 			vcpu->arch.target = -1;
102122f55384SQais Yousef 			ret = ARM_EXCEPTION_IL;
102222f55384SQais Yousef 		}
102322f55384SQais Yousef 
102474cc7e0cSTianjia Zhang 		ret = handle_exit(vcpu, ret);
10259ed24f4bSMarc Zyngier 	}
10269ed24f4bSMarc Zyngier 
10279ed24f4bSMarc Zyngier 	/* Tell userspace about in-kernel device output levels */
10289ed24f4bSMarc Zyngier 	if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
10299ed24f4bSMarc Zyngier 		kvm_timer_update_run(vcpu);
10309ed24f4bSMarc Zyngier 		kvm_pmu_update_run(vcpu);
10319ed24f4bSMarc Zyngier 	}
10329ed24f4bSMarc Zyngier 
10339ed24f4bSMarc Zyngier 	kvm_sigset_deactivate(vcpu);
10349ed24f4bSMarc Zyngier 
1035e3e880bbSZenghui Yu out:
103626778aaaSMarc Zyngier 	/*
103726778aaaSMarc Zyngier 	 * In the unlikely event that we are returning to userspace
103826778aaaSMarc Zyngier 	 * with pending exceptions or PC adjustment, commit these
103926778aaaSMarc Zyngier 	 * adjustments in order to give userspace a consistent view of
104026778aaaSMarc Zyngier 	 * the vcpu state. Note that this relies on __kvm_adjust_pc()
104126778aaaSMarc Zyngier 	 * being preempt-safe on VHE.
104226778aaaSMarc Zyngier 	 */
1043699bb2e0SMarc Zyngier 	if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
1044699bb2e0SMarc Zyngier 		     vcpu_get_flag(vcpu, INCREMENT_PC)))
104526778aaaSMarc Zyngier 		kvm_call_hyp(__kvm_adjust_pc, vcpu);
104626778aaaSMarc Zyngier 
10479ed24f4bSMarc Zyngier 	vcpu_put(vcpu);
10489ed24f4bSMarc Zyngier 	return ret;
10499ed24f4bSMarc Zyngier }
10509ed24f4bSMarc Zyngier 
10519ed24f4bSMarc Zyngier static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
10529ed24f4bSMarc Zyngier {
10539ed24f4bSMarc Zyngier 	int bit_index;
10549ed24f4bSMarc Zyngier 	bool set;
10559ed24f4bSMarc Zyngier 	unsigned long *hcr;
10569ed24f4bSMarc Zyngier 
10579ed24f4bSMarc Zyngier 	if (number == KVM_ARM_IRQ_CPU_IRQ)
10589ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VI);
10599ed24f4bSMarc Zyngier 	else /* KVM_ARM_IRQ_CPU_FIQ */
10609ed24f4bSMarc Zyngier 		bit_index = __ffs(HCR_VF);
10619ed24f4bSMarc Zyngier 
10629ed24f4bSMarc Zyngier 	hcr = vcpu_hcr(vcpu);
10639ed24f4bSMarc Zyngier 	if (level)
10649ed24f4bSMarc Zyngier 		set = test_and_set_bit(bit_index, hcr);
10659ed24f4bSMarc Zyngier 	else
10669ed24f4bSMarc Zyngier 		set = test_and_clear_bit(bit_index, hcr);
10679ed24f4bSMarc Zyngier 
10689ed24f4bSMarc Zyngier 	/*
10699ed24f4bSMarc Zyngier 	 * If we didn't change anything, no need to wake up or kick other CPUs
10709ed24f4bSMarc Zyngier 	 */
10719ed24f4bSMarc Zyngier 	if (set == level)
10729ed24f4bSMarc Zyngier 		return 0;
10739ed24f4bSMarc Zyngier 
10749ed24f4bSMarc Zyngier 	/*
10759ed24f4bSMarc Zyngier 	 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
10769ed24f4bSMarc Zyngier 	 * trigger a world-switch round on the running physical CPU to set the
10779ed24f4bSMarc Zyngier 	 * virtual IRQ/FIQ fields in the HCR appropriately.
10789ed24f4bSMarc Zyngier 	 */
10799ed24f4bSMarc Zyngier 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
10809ed24f4bSMarc Zyngier 	kvm_vcpu_kick(vcpu);
10819ed24f4bSMarc Zyngier 
10829ed24f4bSMarc Zyngier 	return 0;
10839ed24f4bSMarc Zyngier }
10849ed24f4bSMarc Zyngier 
10859ed24f4bSMarc Zyngier int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
10869ed24f4bSMarc Zyngier 			  bool line_status)
10879ed24f4bSMarc Zyngier {
10889ed24f4bSMarc Zyngier 	u32 irq = irq_level->irq;
10899ed24f4bSMarc Zyngier 	unsigned int irq_type, vcpu_idx, irq_num;
10909ed24f4bSMarc Zyngier 	int nrcpus = atomic_read(&kvm->online_vcpus);
10919ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = NULL;
10929ed24f4bSMarc Zyngier 	bool level = irq_level->level;
10939ed24f4bSMarc Zyngier 
10949ed24f4bSMarc Zyngier 	irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
10959ed24f4bSMarc Zyngier 	vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
10969ed24f4bSMarc Zyngier 	vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
10979ed24f4bSMarc Zyngier 	irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
10989ed24f4bSMarc Zyngier 
10999ed24f4bSMarc Zyngier 	trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
11009ed24f4bSMarc Zyngier 
11019ed24f4bSMarc Zyngier 	switch (irq_type) {
11029ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_CPU:
11039ed24f4bSMarc Zyngier 		if (irqchip_in_kernel(kvm))
11049ed24f4bSMarc Zyngier 			return -ENXIO;
11059ed24f4bSMarc Zyngier 
11069ed24f4bSMarc Zyngier 		if (vcpu_idx >= nrcpus)
11079ed24f4bSMarc Zyngier 			return -EINVAL;
11089ed24f4bSMarc Zyngier 
11099ed24f4bSMarc Zyngier 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
11109ed24f4bSMarc Zyngier 		if (!vcpu)
11119ed24f4bSMarc Zyngier 			return -EINVAL;
11129ed24f4bSMarc Zyngier 
11139ed24f4bSMarc Zyngier 		if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
11149ed24f4bSMarc Zyngier 			return -EINVAL;
11159ed24f4bSMarc Zyngier 
11169ed24f4bSMarc Zyngier 		return vcpu_interrupt_line(vcpu, irq_num, level);
11179ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_PPI:
11189ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
11199ed24f4bSMarc Zyngier 			return -ENXIO;
11209ed24f4bSMarc Zyngier 
11219ed24f4bSMarc Zyngier 		if (vcpu_idx >= nrcpus)
11229ed24f4bSMarc Zyngier 			return -EINVAL;
11239ed24f4bSMarc Zyngier 
11249ed24f4bSMarc Zyngier 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
11259ed24f4bSMarc Zyngier 		if (!vcpu)
11269ed24f4bSMarc Zyngier 			return -EINVAL;
11279ed24f4bSMarc Zyngier 
11289ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
11299ed24f4bSMarc Zyngier 			return -EINVAL;
11309ed24f4bSMarc Zyngier 
11319ed24f4bSMarc Zyngier 		return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
11329ed24f4bSMarc Zyngier 	case KVM_ARM_IRQ_TYPE_SPI:
11339ed24f4bSMarc Zyngier 		if (!irqchip_in_kernel(kvm))
11349ed24f4bSMarc Zyngier 			return -ENXIO;
11359ed24f4bSMarc Zyngier 
11369ed24f4bSMarc Zyngier 		if (irq_num < VGIC_NR_PRIVATE_IRQS)
11379ed24f4bSMarc Zyngier 			return -EINVAL;
11389ed24f4bSMarc Zyngier 
11399ed24f4bSMarc Zyngier 		return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
11409ed24f4bSMarc Zyngier 	}
11419ed24f4bSMarc Zyngier 
11429ed24f4bSMarc Zyngier 	return -EINVAL;
11439ed24f4bSMarc Zyngier }
11449ed24f4bSMarc Zyngier 
11459ed24f4bSMarc Zyngier static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
11469ed24f4bSMarc Zyngier 			       const struct kvm_vcpu_init *init)
11479ed24f4bSMarc Zyngier {
11489ed24f4bSMarc Zyngier 	unsigned int i, ret;
11496b7982feSAnshuman Khandual 	u32 phys_target = kvm_target_cpu();
11509ed24f4bSMarc Zyngier 
11519ed24f4bSMarc Zyngier 	if (init->target != phys_target)
11529ed24f4bSMarc Zyngier 		return -EINVAL;
11539ed24f4bSMarc Zyngier 
11549ed24f4bSMarc Zyngier 	/*
11559ed24f4bSMarc Zyngier 	 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
11569ed24f4bSMarc Zyngier 	 * use the same target.
11579ed24f4bSMarc Zyngier 	 */
11589ed24f4bSMarc Zyngier 	if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
11599ed24f4bSMarc Zyngier 		return -EINVAL;
11609ed24f4bSMarc Zyngier 
11619ed24f4bSMarc Zyngier 	/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
11629ed24f4bSMarc Zyngier 	for (i = 0; i < sizeof(init->features) * 8; i++) {
11639ed24f4bSMarc Zyngier 		bool set = (init->features[i / 32] & (1 << (i % 32)));
11649ed24f4bSMarc Zyngier 
11659ed24f4bSMarc Zyngier 		if (set && i >= KVM_VCPU_MAX_FEATURES)
11669ed24f4bSMarc Zyngier 			return -ENOENT;
11679ed24f4bSMarc Zyngier 
11689ed24f4bSMarc Zyngier 		/*
11699ed24f4bSMarc Zyngier 		 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
11709ed24f4bSMarc Zyngier 		 * use the same feature set.
11719ed24f4bSMarc Zyngier 		 */
11729ed24f4bSMarc Zyngier 		if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
11739ed24f4bSMarc Zyngier 		    test_bit(i, vcpu->arch.features) != set)
11749ed24f4bSMarc Zyngier 			return -EINVAL;
11759ed24f4bSMarc Zyngier 
11769ed24f4bSMarc Zyngier 		if (set)
11779ed24f4bSMarc Zyngier 			set_bit(i, vcpu->arch.features);
11789ed24f4bSMarc Zyngier 	}
11799ed24f4bSMarc Zyngier 
11809ed24f4bSMarc Zyngier 	vcpu->arch.target = phys_target;
11819ed24f4bSMarc Zyngier 
11829ed24f4bSMarc Zyngier 	/* Now we know what it is, we can reset it. */
11839ed24f4bSMarc Zyngier 	ret = kvm_reset_vcpu(vcpu);
11849ed24f4bSMarc Zyngier 	if (ret) {
11859ed24f4bSMarc Zyngier 		vcpu->arch.target = -1;
11869ed24f4bSMarc Zyngier 		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
11879ed24f4bSMarc Zyngier 	}
11889ed24f4bSMarc Zyngier 
11899ed24f4bSMarc Zyngier 	return ret;
11909ed24f4bSMarc Zyngier }
11919ed24f4bSMarc Zyngier 
11929ed24f4bSMarc Zyngier static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
11939ed24f4bSMarc Zyngier 					 struct kvm_vcpu_init *init)
11949ed24f4bSMarc Zyngier {
11959ed24f4bSMarc Zyngier 	int ret;
11969ed24f4bSMarc Zyngier 
11979ed24f4bSMarc Zyngier 	ret = kvm_vcpu_set_target(vcpu, init);
11989ed24f4bSMarc Zyngier 	if (ret)
11999ed24f4bSMarc Zyngier 		return ret;
12009ed24f4bSMarc Zyngier 
12019ed24f4bSMarc Zyngier 	/*
12029ed24f4bSMarc Zyngier 	 * Ensure a rebooted VM will fault in RAM pages and detect if the
12039ed24f4bSMarc Zyngier 	 * guest MMU is turned off and flush the caches as needed.
1204892713e9SZenghui Yu 	 *
12057ae2f3dbSMarc Zyngier 	 * S2FWB enforces all memory accesses to RAM being cacheable,
12067ae2f3dbSMarc Zyngier 	 * ensuring that the data side is always coherent. We still
12077ae2f3dbSMarc Zyngier 	 * need to invalidate the I-cache though, as FWB does *not*
12087ae2f3dbSMarc Zyngier 	 * imply CTR_EL0.DIC.
12099ed24f4bSMarc Zyngier 	 */
1210cc5705fbSMarc Zyngier 	if (vcpu_has_run_once(vcpu)) {
12117ae2f3dbSMarc Zyngier 		if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
12129ed24f4bSMarc Zyngier 			stage2_unmap_vm(vcpu->kvm);
12137ae2f3dbSMarc Zyngier 		else
1214fade9c2cSFuad Tabba 			icache_inval_all_pou();
12157ae2f3dbSMarc Zyngier 	}
12169ed24f4bSMarc Zyngier 
12179ed24f4bSMarc Zyngier 	vcpu_reset_hcr(vcpu);
1218cd496228SFuad Tabba 	vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT;
12199ed24f4bSMarc Zyngier 
12209ed24f4bSMarc Zyngier 	/*
12219ed24f4bSMarc Zyngier 	 * Handle the "start in power-off" case.
12229ed24f4bSMarc Zyngier 	 */
12239ed24f4bSMarc Zyngier 	if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
12241e579429SOliver Upton 		kvm_arm_vcpu_power_off(vcpu);
12259ed24f4bSMarc Zyngier 	else
1226b171f9bbSOliver Upton 		vcpu->arch.mp_state.mp_state = KVM_MP_STATE_RUNNABLE;
12279ed24f4bSMarc Zyngier 
12289ed24f4bSMarc Zyngier 	return 0;
12299ed24f4bSMarc Zyngier }
12309ed24f4bSMarc Zyngier 
12319ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
12329ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
12339ed24f4bSMarc Zyngier {
12349ed24f4bSMarc Zyngier 	int ret = -ENXIO;
12359ed24f4bSMarc Zyngier 
12369ed24f4bSMarc Zyngier 	switch (attr->group) {
12379ed24f4bSMarc Zyngier 	default:
12389ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
12399ed24f4bSMarc Zyngier 		break;
12409ed24f4bSMarc Zyngier 	}
12419ed24f4bSMarc Zyngier 
12429ed24f4bSMarc Zyngier 	return ret;
12439ed24f4bSMarc Zyngier }
12449ed24f4bSMarc Zyngier 
12459ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
12469ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
12479ed24f4bSMarc Zyngier {
12489ed24f4bSMarc Zyngier 	int ret = -ENXIO;
12499ed24f4bSMarc Zyngier 
12509ed24f4bSMarc Zyngier 	switch (attr->group) {
12519ed24f4bSMarc Zyngier 	default:
12529ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
12539ed24f4bSMarc Zyngier 		break;
12549ed24f4bSMarc Zyngier 	}
12559ed24f4bSMarc Zyngier 
12569ed24f4bSMarc Zyngier 	return ret;
12579ed24f4bSMarc Zyngier }
12589ed24f4bSMarc Zyngier 
12599ed24f4bSMarc Zyngier static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
12609ed24f4bSMarc Zyngier 				 struct kvm_device_attr *attr)
12619ed24f4bSMarc Zyngier {
12629ed24f4bSMarc Zyngier 	int ret = -ENXIO;
12639ed24f4bSMarc Zyngier 
12649ed24f4bSMarc Zyngier 	switch (attr->group) {
12659ed24f4bSMarc Zyngier 	default:
12669ed24f4bSMarc Zyngier 		ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
12679ed24f4bSMarc Zyngier 		break;
12689ed24f4bSMarc Zyngier 	}
12699ed24f4bSMarc Zyngier 
12709ed24f4bSMarc Zyngier 	return ret;
12719ed24f4bSMarc Zyngier }
12729ed24f4bSMarc Zyngier 
12739ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
12749ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
12759ed24f4bSMarc Zyngier {
12769ed24f4bSMarc Zyngier 	memset(events, 0, sizeof(*events));
12779ed24f4bSMarc Zyngier 
12789ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_get_events(vcpu, events);
12799ed24f4bSMarc Zyngier }
12809ed24f4bSMarc Zyngier 
12819ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
12829ed24f4bSMarc Zyngier 				   struct kvm_vcpu_events *events)
12839ed24f4bSMarc Zyngier {
12849ed24f4bSMarc Zyngier 	int i;
12859ed24f4bSMarc Zyngier 
12869ed24f4bSMarc Zyngier 	/* check whether the reserved field is zero */
12879ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
12889ed24f4bSMarc Zyngier 		if (events->reserved[i])
12899ed24f4bSMarc Zyngier 			return -EINVAL;
12909ed24f4bSMarc Zyngier 
12919ed24f4bSMarc Zyngier 	/* check whether the pad field is zero */
12929ed24f4bSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
12939ed24f4bSMarc Zyngier 		if (events->exception.pad[i])
12949ed24f4bSMarc Zyngier 			return -EINVAL;
12959ed24f4bSMarc Zyngier 
12969ed24f4bSMarc Zyngier 	return __kvm_arm_vcpu_set_events(vcpu, events);
12979ed24f4bSMarc Zyngier }
12989ed24f4bSMarc Zyngier 
12999ed24f4bSMarc Zyngier long kvm_arch_vcpu_ioctl(struct file *filp,
13009ed24f4bSMarc Zyngier 			 unsigned int ioctl, unsigned long arg)
13019ed24f4bSMarc Zyngier {
13029ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu = filp->private_data;
13039ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
13049ed24f4bSMarc Zyngier 	struct kvm_device_attr attr;
13059ed24f4bSMarc Zyngier 	long r;
13069ed24f4bSMarc Zyngier 
13079ed24f4bSMarc Zyngier 	switch (ioctl) {
13089ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_INIT: {
13099ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
13109ed24f4bSMarc Zyngier 
13119ed24f4bSMarc Zyngier 		r = -EFAULT;
13129ed24f4bSMarc Zyngier 		if (copy_from_user(&init, argp, sizeof(init)))
13139ed24f4bSMarc Zyngier 			break;
13149ed24f4bSMarc Zyngier 
13159ed24f4bSMarc Zyngier 		r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
13169ed24f4bSMarc Zyngier 		break;
13179ed24f4bSMarc Zyngier 	}
13189ed24f4bSMarc Zyngier 	case KVM_SET_ONE_REG:
13199ed24f4bSMarc Zyngier 	case KVM_GET_ONE_REG: {
13209ed24f4bSMarc Zyngier 		struct kvm_one_reg reg;
13219ed24f4bSMarc Zyngier 
13229ed24f4bSMarc Zyngier 		r = -ENOEXEC;
13239ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
13249ed24f4bSMarc Zyngier 			break;
13259ed24f4bSMarc Zyngier 
13269ed24f4bSMarc Zyngier 		r = -EFAULT;
13279ed24f4bSMarc Zyngier 		if (copy_from_user(&reg, argp, sizeof(reg)))
13289ed24f4bSMarc Zyngier 			break;
13299ed24f4bSMarc Zyngier 
13306826c684SOliver Upton 		/*
13316826c684SOliver Upton 		 * We could owe a reset due to PSCI. Handle the pending reset
13326826c684SOliver Upton 		 * here to ensure userspace register accesses are ordered after
13336826c684SOliver Upton 		 * the reset.
13346826c684SOliver Upton 		 */
13356826c684SOliver Upton 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
13366826c684SOliver Upton 			kvm_reset_vcpu(vcpu);
13376826c684SOliver Upton 
13389ed24f4bSMarc Zyngier 		if (ioctl == KVM_SET_ONE_REG)
13399ed24f4bSMarc Zyngier 			r = kvm_arm_set_reg(vcpu, &reg);
13409ed24f4bSMarc Zyngier 		else
13419ed24f4bSMarc Zyngier 			r = kvm_arm_get_reg(vcpu, &reg);
13429ed24f4bSMarc Zyngier 		break;
13439ed24f4bSMarc Zyngier 	}
13449ed24f4bSMarc Zyngier 	case KVM_GET_REG_LIST: {
13459ed24f4bSMarc Zyngier 		struct kvm_reg_list __user *user_list = argp;
13469ed24f4bSMarc Zyngier 		struct kvm_reg_list reg_list;
13479ed24f4bSMarc Zyngier 		unsigned n;
13489ed24f4bSMarc Zyngier 
13499ed24f4bSMarc Zyngier 		r = -ENOEXEC;
13509ed24f4bSMarc Zyngier 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
13519ed24f4bSMarc Zyngier 			break;
13529ed24f4bSMarc Zyngier 
13539ed24f4bSMarc Zyngier 		r = -EPERM;
13549ed24f4bSMarc Zyngier 		if (!kvm_arm_vcpu_is_finalized(vcpu))
13559ed24f4bSMarc Zyngier 			break;
13569ed24f4bSMarc Zyngier 
13579ed24f4bSMarc Zyngier 		r = -EFAULT;
13589ed24f4bSMarc Zyngier 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
13599ed24f4bSMarc Zyngier 			break;
13609ed24f4bSMarc Zyngier 		n = reg_list.n;
13619ed24f4bSMarc Zyngier 		reg_list.n = kvm_arm_num_regs(vcpu);
13629ed24f4bSMarc Zyngier 		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
13639ed24f4bSMarc Zyngier 			break;
13649ed24f4bSMarc Zyngier 		r = -E2BIG;
13659ed24f4bSMarc Zyngier 		if (n < reg_list.n)
13669ed24f4bSMarc Zyngier 			break;
13679ed24f4bSMarc Zyngier 		r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
13689ed24f4bSMarc Zyngier 		break;
13699ed24f4bSMarc Zyngier 	}
13709ed24f4bSMarc Zyngier 	case KVM_SET_DEVICE_ATTR: {
13719ed24f4bSMarc Zyngier 		r = -EFAULT;
13729ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
13739ed24f4bSMarc Zyngier 			break;
13749ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_set_attr(vcpu, &attr);
13759ed24f4bSMarc Zyngier 		break;
13769ed24f4bSMarc Zyngier 	}
13779ed24f4bSMarc Zyngier 	case KVM_GET_DEVICE_ATTR: {
13789ed24f4bSMarc Zyngier 		r = -EFAULT;
13799ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
13809ed24f4bSMarc Zyngier 			break;
13819ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_get_attr(vcpu, &attr);
13829ed24f4bSMarc Zyngier 		break;
13839ed24f4bSMarc Zyngier 	}
13849ed24f4bSMarc Zyngier 	case KVM_HAS_DEVICE_ATTR: {
13859ed24f4bSMarc Zyngier 		r = -EFAULT;
13869ed24f4bSMarc Zyngier 		if (copy_from_user(&attr, argp, sizeof(attr)))
13879ed24f4bSMarc Zyngier 			break;
13889ed24f4bSMarc Zyngier 		r = kvm_arm_vcpu_has_attr(vcpu, &attr);
13899ed24f4bSMarc Zyngier 		break;
13909ed24f4bSMarc Zyngier 	}
13919ed24f4bSMarc Zyngier 	case KVM_GET_VCPU_EVENTS: {
13929ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
13939ed24f4bSMarc Zyngier 
13949ed24f4bSMarc Zyngier 		if (kvm_arm_vcpu_get_events(vcpu, &events))
13959ed24f4bSMarc Zyngier 			return -EINVAL;
13969ed24f4bSMarc Zyngier 
13979ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &events, sizeof(events)))
13989ed24f4bSMarc Zyngier 			return -EFAULT;
13999ed24f4bSMarc Zyngier 
14009ed24f4bSMarc Zyngier 		return 0;
14019ed24f4bSMarc Zyngier 	}
14029ed24f4bSMarc Zyngier 	case KVM_SET_VCPU_EVENTS: {
14039ed24f4bSMarc Zyngier 		struct kvm_vcpu_events events;
14049ed24f4bSMarc Zyngier 
14059ed24f4bSMarc Zyngier 		if (copy_from_user(&events, argp, sizeof(events)))
14069ed24f4bSMarc Zyngier 			return -EFAULT;
14079ed24f4bSMarc Zyngier 
14089ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_set_events(vcpu, &events);
14099ed24f4bSMarc Zyngier 	}
14109ed24f4bSMarc Zyngier 	case KVM_ARM_VCPU_FINALIZE: {
14119ed24f4bSMarc Zyngier 		int what;
14129ed24f4bSMarc Zyngier 
14139ed24f4bSMarc Zyngier 		if (!kvm_vcpu_initialized(vcpu))
14149ed24f4bSMarc Zyngier 			return -ENOEXEC;
14159ed24f4bSMarc Zyngier 
14169ed24f4bSMarc Zyngier 		if (get_user(what, (const int __user *)argp))
14179ed24f4bSMarc Zyngier 			return -EFAULT;
14189ed24f4bSMarc Zyngier 
14199ed24f4bSMarc Zyngier 		return kvm_arm_vcpu_finalize(vcpu, what);
14209ed24f4bSMarc Zyngier 	}
14219ed24f4bSMarc Zyngier 	default:
14229ed24f4bSMarc Zyngier 		r = -EINVAL;
14239ed24f4bSMarc Zyngier 	}
14249ed24f4bSMarc Zyngier 
14259ed24f4bSMarc Zyngier 	return r;
14269ed24f4bSMarc Zyngier }
14279ed24f4bSMarc Zyngier 
14289ed24f4bSMarc Zyngier void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
14299ed24f4bSMarc Zyngier {
14309ed24f4bSMarc Zyngier 
14319ed24f4bSMarc Zyngier }
14329ed24f4bSMarc Zyngier 
14339ed24f4bSMarc Zyngier void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
14346c9dd6d2SPaolo Bonzini 					const struct kvm_memory_slot *memslot)
14359ed24f4bSMarc Zyngier {
14369ed24f4bSMarc Zyngier 	kvm_flush_remote_tlbs(kvm);
14379ed24f4bSMarc Zyngier }
14389ed24f4bSMarc Zyngier 
14399ed24f4bSMarc Zyngier static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
14409ed24f4bSMarc Zyngier 					struct kvm_arm_device_addr *dev_addr)
14419ed24f4bSMarc Zyngier {
14429f968c92SMarc Zyngier 	switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) {
14439ed24f4bSMarc Zyngier 	case KVM_ARM_DEVICE_VGIC_V2:
14449ed24f4bSMarc Zyngier 		if (!vgic_present)
14459ed24f4bSMarc Zyngier 			return -ENXIO;
14469f968c92SMarc Zyngier 		return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr);
14479ed24f4bSMarc Zyngier 	default:
14489ed24f4bSMarc Zyngier 		return -ENODEV;
14499ed24f4bSMarc Zyngier 	}
14509ed24f4bSMarc Zyngier }
14519ed24f4bSMarc Zyngier 
14529ed24f4bSMarc Zyngier long kvm_arch_vm_ioctl(struct file *filp,
14539ed24f4bSMarc Zyngier 		       unsigned int ioctl, unsigned long arg)
14549ed24f4bSMarc Zyngier {
14559ed24f4bSMarc Zyngier 	struct kvm *kvm = filp->private_data;
14569ed24f4bSMarc Zyngier 	void __user *argp = (void __user *)arg;
14579ed24f4bSMarc Zyngier 
14589ed24f4bSMarc Zyngier 	switch (ioctl) {
14599ed24f4bSMarc Zyngier 	case KVM_CREATE_IRQCHIP: {
14609ed24f4bSMarc Zyngier 		int ret;
14619ed24f4bSMarc Zyngier 		if (!vgic_present)
14629ed24f4bSMarc Zyngier 			return -ENXIO;
14639ed24f4bSMarc Zyngier 		mutex_lock(&kvm->lock);
14649ed24f4bSMarc Zyngier 		ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
14659ed24f4bSMarc Zyngier 		mutex_unlock(&kvm->lock);
14669ed24f4bSMarc Zyngier 		return ret;
14679ed24f4bSMarc Zyngier 	}
14689ed24f4bSMarc Zyngier 	case KVM_ARM_SET_DEVICE_ADDR: {
14699ed24f4bSMarc Zyngier 		struct kvm_arm_device_addr dev_addr;
14709ed24f4bSMarc Zyngier 
14719ed24f4bSMarc Zyngier 		if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
14729ed24f4bSMarc Zyngier 			return -EFAULT;
14739ed24f4bSMarc Zyngier 		return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
14749ed24f4bSMarc Zyngier 	}
14759ed24f4bSMarc Zyngier 	case KVM_ARM_PREFERRED_TARGET: {
14769ed24f4bSMarc Zyngier 		struct kvm_vcpu_init init;
14779ed24f4bSMarc Zyngier 
147808e873cbSYueHaibing 		kvm_vcpu_preferred_target(&init);
14799ed24f4bSMarc Zyngier 
14809ed24f4bSMarc Zyngier 		if (copy_to_user(argp, &init, sizeof(init)))
14819ed24f4bSMarc Zyngier 			return -EFAULT;
14829ed24f4bSMarc Zyngier 
14839ed24f4bSMarc Zyngier 		return 0;
14849ed24f4bSMarc Zyngier 	}
1485f0376edbSSteven Price 	case KVM_ARM_MTE_COPY_TAGS: {
1486f0376edbSSteven Price 		struct kvm_arm_copy_mte_tags copy_tags;
1487f0376edbSSteven Price 
1488f0376edbSSteven Price 		if (copy_from_user(&copy_tags, argp, sizeof(copy_tags)))
1489f0376edbSSteven Price 			return -EFAULT;
1490f0376edbSSteven Price 		return kvm_vm_ioctl_mte_copy_tags(kvm, &copy_tags);
1491f0376edbSSteven Price 	}
14929ed24f4bSMarc Zyngier 	default:
14939ed24f4bSMarc Zyngier 		return -EINVAL;
14949ed24f4bSMarc Zyngier 	}
14959ed24f4bSMarc Zyngier }
14969ed24f4bSMarc Zyngier 
149730c95391SDavid Brazdil static unsigned long nvhe_percpu_size(void)
149830c95391SDavid Brazdil {
149930c95391SDavid Brazdil 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
150030c95391SDavid Brazdil 		(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
150130c95391SDavid Brazdil }
150230c95391SDavid Brazdil 
150330c95391SDavid Brazdil static unsigned long nvhe_percpu_order(void)
150430c95391SDavid Brazdil {
150530c95391SDavid Brazdil 	unsigned long size = nvhe_percpu_size();
150630c95391SDavid Brazdil 
150730c95391SDavid Brazdil 	return size ? get_order(size) : 0;
150830c95391SDavid Brazdil }
150930c95391SDavid Brazdil 
1510b881cdceSWill Deacon /* A lookup table holding the hypervisor VA for each vector slot */
1511b881cdceSWill Deacon static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
1512de5bcdb4SWill Deacon 
1513b881cdceSWill Deacon static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
1514b881cdceSWill Deacon {
1515bc1d2892SQuentin Perret 	hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
15169ef2b48bSWill Deacon }
15179ef2b48bSWill Deacon 
1518b881cdceSWill Deacon static int kvm_init_vector_slots(void)
1519b881cdceSWill Deacon {
1520b881cdceSWill Deacon 	int err;
1521b881cdceSWill Deacon 	void *base;
1522b881cdceSWill Deacon 
1523b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
1524b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
1525b881cdceSWill Deacon 
1526b881cdceSWill Deacon 	base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
1527b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
1528b881cdceSWill Deacon 
15292e403167SQuentin Perret 	if (kvm_system_needs_idmapped_vectors() &&
15302e403167SQuentin Perret 	    !is_protected_kvm_enabled()) {
1531b881cdceSWill Deacon 		err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
1532b881cdceSWill Deacon 					       __BP_HARDEN_HYP_VECS_SZ, &base);
1533b881cdceSWill Deacon 		if (err)
1534b881cdceSWill Deacon 			return err;
1535b881cdceSWill Deacon 	}
15369ef2b48bSWill Deacon 
1537b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
1538b881cdceSWill Deacon 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
15399ef2b48bSWill Deacon 	return 0;
15409ef2b48bSWill Deacon }
15419ef2b48bSWill Deacon 
1542579d7ebeSRyan Roberts static void cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits)
15439ed24f4bSMarc Zyngier {
15449cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
1545d3e1086cSDavid Brazdil 	unsigned long tcr;
15469ed24f4bSMarc Zyngier 
154771b3ec5fSDavid Brazdil 	/*
154871b3ec5fSDavid Brazdil 	 * Calculate the raw per-cpu offset without a translation from the
154971b3ec5fSDavid Brazdil 	 * kernel's mapping to the linear mapping, and store it in tpidr_el2
155071b3ec5fSDavid Brazdil 	 * so that we can use adr_l to access per-cpu variables in EL2.
1551e1663372SSteven Price 	 * Also drop the KASAN tag which gets in the way...
155271b3ec5fSDavid Brazdil 	 */
15539cc77581SQuentin Perret 	params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
155430c95391SDavid Brazdil 			    (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
155571b3ec5fSDavid Brazdil 
1556d3e1086cSDavid Brazdil 	params->mair_el2 = read_sysreg(mair_el1);
1557d3e1086cSDavid Brazdil 
1558d3e1086cSDavid Brazdil 	tcr = (read_sysreg(tcr_el1) & TCR_EL2_MASK) | TCR_EL2_RES1;
1559d3e1086cSDavid Brazdil 	tcr &= ~TCR_T0SZ_MASK;
1560579d7ebeSRyan Roberts 	tcr |= TCR_T0SZ(hyp_va_bits);
1561d3e1086cSDavid Brazdil 	params->tcr_el2 = tcr;
1562d3e1086cSDavid Brazdil 
156363fec243SDavid Brazdil 	params->pgd_pa = kvm_mmu_get_httbr();
1564734864c1SQuentin Perret 	if (is_protected_kvm_enabled())
1565734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
1566734864c1SQuentin Perret 	else
1567734864c1SQuentin Perret 		params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
1568734864c1SQuentin Perret 	params->vttbr = params->vtcr = 0;
156963fec243SDavid Brazdil 
157063fec243SDavid Brazdil 	/*
157163fec243SDavid Brazdil 	 * Flush the init params from the data cache because the struct will
157263fec243SDavid Brazdil 	 * be read while the MMU is off.
157363fec243SDavid Brazdil 	 */
157463fec243SDavid Brazdil 	kvm_flush_dcache_to_poc(params, sizeof(*params));
15759cc77581SQuentin Perret }
15769cc77581SQuentin Perret 
1577bfa79a80SQuentin Perret static void hyp_install_host_vector(void)
15789cc77581SQuentin Perret {
15799cc77581SQuentin Perret 	struct kvm_nvhe_init_params *params;
15809cc77581SQuentin Perret 	struct arm_smccc_res res;
15819cc77581SQuentin Perret 
15829cc77581SQuentin Perret 	/* Switch from the HYP stub to our own HYP init vector */
15839cc77581SQuentin Perret 	__hyp_set_vectors(kvm_get_idmap_vector());
15849ed24f4bSMarc Zyngier 
158571b3ec5fSDavid Brazdil 	/*
158671b3ec5fSDavid Brazdil 	 * Call initialization code, and switch to the full blown HYP code.
158771b3ec5fSDavid Brazdil 	 * If the cpucaps haven't been finalized yet, something has gone very
158871b3ec5fSDavid Brazdil 	 * wrong, and hyp will crash and burn when it uses any
158971b3ec5fSDavid Brazdil 	 * cpus_have_const_cap() wrapper.
159071b3ec5fSDavid Brazdil 	 */
159171b3ec5fSDavid Brazdil 	BUG_ON(!system_capabilities_finalized());
15929cc77581SQuentin Perret 	params = this_cpu_ptr_nvhe_sym(kvm_init_params);
159363fec243SDavid Brazdil 	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
159404e4caa8SAndrew Scull 	WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
1595bfa79a80SQuentin Perret }
1596bfa79a80SQuentin Perret 
1597bfa79a80SQuentin Perret static void cpu_init_hyp_mode(void)
1598bfa79a80SQuentin Perret {
1599bfa79a80SQuentin Perret 	hyp_install_host_vector();
160071b3ec5fSDavid Brazdil 
160171b3ec5fSDavid Brazdil 	/*
160271b3ec5fSDavid Brazdil 	 * Disabling SSBD on a non-VHE system requires us to enable SSBS
160371b3ec5fSDavid Brazdil 	 * at EL2.
160471b3ec5fSDavid Brazdil 	 */
160571b3ec5fSDavid Brazdil 	if (this_cpu_has_cap(ARM64_SSBS) &&
1606d63d975aSMarc Zyngier 	    arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
160713aeb9b4SDavid Brazdil 		kvm_call_hyp_nvhe(__kvm_enable_ssbs);
160871b3ec5fSDavid Brazdil 	}
16099ed24f4bSMarc Zyngier }
16109ed24f4bSMarc Zyngier 
16119ed24f4bSMarc Zyngier static void cpu_hyp_reset(void)
16129ed24f4bSMarc Zyngier {
16139ed24f4bSMarc Zyngier 	if (!is_kernel_in_hyp_mode())
16149ed24f4bSMarc Zyngier 		__hyp_reset_vectors();
16159ed24f4bSMarc Zyngier }
16169ed24f4bSMarc Zyngier 
1617042c76a9SWill Deacon /*
1618042c76a9SWill Deacon  * EL2 vectors can be mapped and rerouted in a number of ways,
1619042c76a9SWill Deacon  * depending on the kernel configuration and CPU present:
1620042c76a9SWill Deacon  *
1621042c76a9SWill Deacon  * - If the CPU is affected by Spectre-v2, the hardening sequence is
1622042c76a9SWill Deacon  *   placed in one of the vector slots, which is executed before jumping
1623042c76a9SWill Deacon  *   to the real vectors.
1624042c76a9SWill Deacon  *
1625c4792b6dSWill Deacon  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
1626042c76a9SWill Deacon  *   containing the hardening sequence is mapped next to the idmap page,
1627042c76a9SWill Deacon  *   and executed before jumping to the real vectors.
1628042c76a9SWill Deacon  *
1629c4792b6dSWill Deacon  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
1630042c76a9SWill Deacon  *   empty slot is selected, mapped next to the idmap page, and
1631042c76a9SWill Deacon  *   executed before jumping to the real vectors.
1632042c76a9SWill Deacon  *
1633c4792b6dSWill Deacon  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
1634042c76a9SWill Deacon  * VHE, as we don't have hypervisor-specific mappings. If the system
1635042c76a9SWill Deacon  * is VHE and yet selects this capability, it will be ignored.
1636042c76a9SWill Deacon  */
1637042c76a9SWill Deacon static void cpu_set_hyp_vector(void)
1638042c76a9SWill Deacon {
16396279017eSWill Deacon 	struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
1640b881cdceSWill Deacon 	void *vector = hyp_spectre_vector_selector[data->slot];
1641042c76a9SWill Deacon 
1642bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
1643b881cdceSWill Deacon 		*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
1644bfa79a80SQuentin Perret 	else
1645bfa79a80SQuentin Perret 		kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
1646042c76a9SWill Deacon }
1647042c76a9SWill Deacon 
16488579a185SWill Deacon static void cpu_hyp_init_context(void)
16499ed24f4bSMarc Zyngier {
16502a1198c9SDavid Brazdil 	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
16519ed24f4bSMarc Zyngier 
16528579a185SWill Deacon 	if (!is_kernel_in_hyp_mode())
16538579a185SWill Deacon 		cpu_init_hyp_mode();
16548579a185SWill Deacon }
16558579a185SWill Deacon 
16568579a185SWill Deacon static void cpu_hyp_init_features(void)
16578579a185SWill Deacon {
16588579a185SWill Deacon 	cpu_set_hyp_vector();
16598579a185SWill Deacon 	kvm_arm_init_debug();
1660a0e47952SAndrew Scull 
16619ed24f4bSMarc Zyngier 	if (is_kernel_in_hyp_mode())
16629ed24f4bSMarc Zyngier 		kvm_timer_init_vhe();
16639ed24f4bSMarc Zyngier 
16649ed24f4bSMarc Zyngier 	if (vgic_present)
16659ed24f4bSMarc Zyngier 		kvm_vgic_init_cpu_hardware();
16669ed24f4bSMarc Zyngier }
16679ed24f4bSMarc Zyngier 
16688579a185SWill Deacon static void cpu_hyp_reinit(void)
16698579a185SWill Deacon {
16708579a185SWill Deacon 	cpu_hyp_reset();
16718579a185SWill Deacon 	cpu_hyp_init_context();
16728579a185SWill Deacon 	cpu_hyp_init_features();
16738579a185SWill Deacon }
16748579a185SWill Deacon 
16759ed24f4bSMarc Zyngier static void _kvm_arch_hardware_enable(void *discard)
16769ed24f4bSMarc Zyngier {
16779ed24f4bSMarc Zyngier 	if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
16789ed24f4bSMarc Zyngier 		cpu_hyp_reinit();
16799ed24f4bSMarc Zyngier 		__this_cpu_write(kvm_arm_hardware_enabled, 1);
16809ed24f4bSMarc Zyngier 	}
16819ed24f4bSMarc Zyngier }
16829ed24f4bSMarc Zyngier 
16839ed24f4bSMarc Zyngier int kvm_arch_hardware_enable(void)
16849ed24f4bSMarc Zyngier {
16859ed24f4bSMarc Zyngier 	_kvm_arch_hardware_enable(NULL);
16869ed24f4bSMarc Zyngier 	return 0;
16879ed24f4bSMarc Zyngier }
16889ed24f4bSMarc Zyngier 
16899ed24f4bSMarc Zyngier static void _kvm_arch_hardware_disable(void *discard)
16909ed24f4bSMarc Zyngier {
16919ed24f4bSMarc Zyngier 	if (__this_cpu_read(kvm_arm_hardware_enabled)) {
16929ed24f4bSMarc Zyngier 		cpu_hyp_reset();
16939ed24f4bSMarc Zyngier 		__this_cpu_write(kvm_arm_hardware_enabled, 0);
16949ed24f4bSMarc Zyngier 	}
16959ed24f4bSMarc Zyngier }
16969ed24f4bSMarc Zyngier 
16979ed24f4bSMarc Zyngier void kvm_arch_hardware_disable(void)
16989ed24f4bSMarc Zyngier {
1699fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
17009ed24f4bSMarc Zyngier 		_kvm_arch_hardware_disable(NULL);
17019ed24f4bSMarc Zyngier }
17029ed24f4bSMarc Zyngier 
17039ed24f4bSMarc Zyngier #ifdef CONFIG_CPU_PM
17049ed24f4bSMarc Zyngier static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
17059ed24f4bSMarc Zyngier 				    unsigned long cmd,
17069ed24f4bSMarc Zyngier 				    void *v)
17079ed24f4bSMarc Zyngier {
17089ed24f4bSMarc Zyngier 	/*
17099ed24f4bSMarc Zyngier 	 * kvm_arm_hardware_enabled is left with its old value over
17109ed24f4bSMarc Zyngier 	 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
17119ed24f4bSMarc Zyngier 	 * re-enable hyp.
17129ed24f4bSMarc Zyngier 	 */
17139ed24f4bSMarc Zyngier 	switch (cmd) {
17149ed24f4bSMarc Zyngier 	case CPU_PM_ENTER:
17159ed24f4bSMarc Zyngier 		if (__this_cpu_read(kvm_arm_hardware_enabled))
17169ed24f4bSMarc Zyngier 			/*
17179ed24f4bSMarc Zyngier 			 * don't update kvm_arm_hardware_enabled here
17189ed24f4bSMarc Zyngier 			 * so that the hardware will be re-enabled
17199ed24f4bSMarc Zyngier 			 * when we resume. See below.
17209ed24f4bSMarc Zyngier 			 */
17219ed24f4bSMarc Zyngier 			cpu_hyp_reset();
17229ed24f4bSMarc Zyngier 
17239ed24f4bSMarc Zyngier 		return NOTIFY_OK;
17249ed24f4bSMarc Zyngier 	case CPU_PM_ENTER_FAILED:
17259ed24f4bSMarc Zyngier 	case CPU_PM_EXIT:
17269ed24f4bSMarc Zyngier 		if (__this_cpu_read(kvm_arm_hardware_enabled))
17279ed24f4bSMarc Zyngier 			/* The hardware was enabled before suspend. */
17289ed24f4bSMarc Zyngier 			cpu_hyp_reinit();
17299ed24f4bSMarc Zyngier 
17309ed24f4bSMarc Zyngier 		return NOTIFY_OK;
17319ed24f4bSMarc Zyngier 
17329ed24f4bSMarc Zyngier 	default:
17339ed24f4bSMarc Zyngier 		return NOTIFY_DONE;
17349ed24f4bSMarc Zyngier 	}
17359ed24f4bSMarc Zyngier }
17369ed24f4bSMarc Zyngier 
17379ed24f4bSMarc Zyngier static struct notifier_block hyp_init_cpu_pm_nb = {
17389ed24f4bSMarc Zyngier 	.notifier_call = hyp_init_cpu_pm_notifier,
17399ed24f4bSMarc Zyngier };
17409ed24f4bSMarc Zyngier 
174144362a3cSMarc Zyngier static void hyp_cpu_pm_init(void)
17429ed24f4bSMarc Zyngier {
1743fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
17449ed24f4bSMarc Zyngier 		cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
17459ed24f4bSMarc Zyngier }
174644362a3cSMarc Zyngier static void hyp_cpu_pm_exit(void)
17479ed24f4bSMarc Zyngier {
1748fa8c3d65SDavid Brazdil 	if (!is_protected_kvm_enabled())
17499ed24f4bSMarc Zyngier 		cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
17509ed24f4bSMarc Zyngier }
17519ed24f4bSMarc Zyngier #else
17529ed24f4bSMarc Zyngier static inline void hyp_cpu_pm_init(void)
17539ed24f4bSMarc Zyngier {
17549ed24f4bSMarc Zyngier }
17559ed24f4bSMarc Zyngier static inline void hyp_cpu_pm_exit(void)
17569ed24f4bSMarc Zyngier {
17579ed24f4bSMarc Zyngier }
17589ed24f4bSMarc Zyngier #endif
17599ed24f4bSMarc Zyngier 
176094f5e8a4SDavid Brazdil static void init_cpu_logical_map(void)
176194f5e8a4SDavid Brazdil {
176294f5e8a4SDavid Brazdil 	unsigned int cpu;
176394f5e8a4SDavid Brazdil 
176494f5e8a4SDavid Brazdil 	/*
176594f5e8a4SDavid Brazdil 	 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
176621ea4578SJulia Lawall 	 * Only copy the set of online CPUs whose features have been checked
176794f5e8a4SDavid Brazdil 	 * against the finalized system capabilities. The hypervisor will not
176894f5e8a4SDavid Brazdil 	 * allow any other CPUs from the `possible` set to boot.
176994f5e8a4SDavid Brazdil 	 */
177094f5e8a4SDavid Brazdil 	for_each_online_cpu(cpu)
177161fe0c37SDavid Brazdil 		hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
177294f5e8a4SDavid Brazdil }
177394f5e8a4SDavid Brazdil 
1774767c973fSMarc Zyngier #define init_psci_0_1_impl_state(config, what)	\
1775767c973fSMarc Zyngier 	config.psci_0_1_ ## what ## _implemented = psci_ops.what
1776767c973fSMarc Zyngier 
1777eeeee719SDavid Brazdil static bool init_psci_relay(void)
1778eeeee719SDavid Brazdil {
1779eeeee719SDavid Brazdil 	/*
1780eeeee719SDavid Brazdil 	 * If PSCI has not been initialized, protected KVM cannot install
1781eeeee719SDavid Brazdil 	 * itself on newly booted CPUs.
1782eeeee719SDavid Brazdil 	 */
1783eeeee719SDavid Brazdil 	if (!psci_ops.get_version) {
1784eeeee719SDavid Brazdil 		kvm_err("Cannot initialize protected mode without PSCI\n");
1785eeeee719SDavid Brazdil 		return false;
1786eeeee719SDavid Brazdil 	}
1787eeeee719SDavid Brazdil 
1788ff367fe4SDavid Brazdil 	kvm_host_psci_config.version = psci_ops.get_version();
1789ff367fe4SDavid Brazdil 
1790ff367fe4SDavid Brazdil 	if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
1791ff367fe4SDavid Brazdil 		kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
1792767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
1793767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
1794767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
1795767c973fSMarc Zyngier 		init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
1796ff367fe4SDavid Brazdil 	}
1797eeeee719SDavid Brazdil 	return true;
1798eeeee719SDavid Brazdil }
1799eeeee719SDavid Brazdil 
18009ed24f4bSMarc Zyngier static int init_subsystems(void)
18019ed24f4bSMarc Zyngier {
18029ed24f4bSMarc Zyngier 	int err = 0;
18039ed24f4bSMarc Zyngier 
18049ed24f4bSMarc Zyngier 	/*
18059ed24f4bSMarc Zyngier 	 * Enable hardware so that subsystem initialisation can access EL2.
18069ed24f4bSMarc Zyngier 	 */
18079ed24f4bSMarc Zyngier 	on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
18089ed24f4bSMarc Zyngier 
18099ed24f4bSMarc Zyngier 	/*
18109ed24f4bSMarc Zyngier 	 * Register CPU lower-power notifier
18119ed24f4bSMarc Zyngier 	 */
18129ed24f4bSMarc Zyngier 	hyp_cpu_pm_init();
18139ed24f4bSMarc Zyngier 
18149ed24f4bSMarc Zyngier 	/*
18159ed24f4bSMarc Zyngier 	 * Init HYP view of VGIC
18169ed24f4bSMarc Zyngier 	 */
18179ed24f4bSMarc Zyngier 	err = kvm_vgic_hyp_init();
18189ed24f4bSMarc Zyngier 	switch (err) {
18199ed24f4bSMarc Zyngier 	case 0:
18209ed24f4bSMarc Zyngier 		vgic_present = true;
18219ed24f4bSMarc Zyngier 		break;
18229ed24f4bSMarc Zyngier 	case -ENODEV:
18239ed24f4bSMarc Zyngier 	case -ENXIO:
18249ed24f4bSMarc Zyngier 		vgic_present = false;
18259ed24f4bSMarc Zyngier 		err = 0;
18269ed24f4bSMarc Zyngier 		break;
18279ed24f4bSMarc Zyngier 	default:
18289ed24f4bSMarc Zyngier 		goto out;
18299ed24f4bSMarc Zyngier 	}
18309ed24f4bSMarc Zyngier 
18319ed24f4bSMarc Zyngier 	/*
18329ed24f4bSMarc Zyngier 	 * Init HYP architected timer support
18339ed24f4bSMarc Zyngier 	 */
18349ed24f4bSMarc Zyngier 	err = kvm_timer_hyp_init(vgic_present);
18359ed24f4bSMarc Zyngier 	if (err)
18369ed24f4bSMarc Zyngier 		goto out;
18379ed24f4bSMarc Zyngier 
183817ed14ebSSean Christopherson 	kvm_register_perf_callbacks(NULL);
183917ed14ebSSean Christopherson 
18409ed24f4bSMarc Zyngier out:
1841fa8c3d65SDavid Brazdil 	if (err || !is_protected_kvm_enabled())
18429ed24f4bSMarc Zyngier 		on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
18439ed24f4bSMarc Zyngier 
18449ed24f4bSMarc Zyngier 	return err;
18459ed24f4bSMarc Zyngier }
18469ed24f4bSMarc Zyngier 
18479ed24f4bSMarc Zyngier static void teardown_hyp_mode(void)
18489ed24f4bSMarc Zyngier {
18499ed24f4bSMarc Zyngier 	int cpu;
18509ed24f4bSMarc Zyngier 
18519ed24f4bSMarc Zyngier 	free_hyp_pgds();
185230c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
18539ed24f4bSMarc Zyngier 		free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
1854fe41a7f8SQuentin Perret 		free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order());
185530c95391SDavid Brazdil 	}
18569ed24f4bSMarc Zyngier }
18579ed24f4bSMarc Zyngier 
1858bfa79a80SQuentin Perret static int do_pkvm_init(u32 hyp_va_bits)
1859bfa79a80SQuentin Perret {
1860fe41a7f8SQuentin Perret 	void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base));
1861bfa79a80SQuentin Perret 	int ret;
1862bfa79a80SQuentin Perret 
1863bfa79a80SQuentin Perret 	preempt_disable();
18648579a185SWill Deacon 	cpu_hyp_init_context();
1865bfa79a80SQuentin Perret 	ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
1866bfa79a80SQuentin Perret 				num_possible_cpus(), kern_hyp_va(per_cpu_base),
1867bfa79a80SQuentin Perret 				hyp_va_bits);
18688579a185SWill Deacon 	cpu_hyp_init_features();
18698579a185SWill Deacon 
18708579a185SWill Deacon 	/*
18718579a185SWill Deacon 	 * The stub hypercalls are now disabled, so set our local flag to
18728579a185SWill Deacon 	 * prevent a later re-init attempt in kvm_arch_hardware_enable().
18738579a185SWill Deacon 	 */
18748579a185SWill Deacon 	__this_cpu_write(kvm_arm_hardware_enabled, 1);
1875bfa79a80SQuentin Perret 	preempt_enable();
1876bfa79a80SQuentin Perret 
1877bfa79a80SQuentin Perret 	return ret;
1878bfa79a80SQuentin Perret }
1879bfa79a80SQuentin Perret 
18806c165223SWill Deacon static void kvm_hyp_init_symbols(void)
1881bfa79a80SQuentin Perret {
18826c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
18836c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1);
18846c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1);
18856c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
1886def8c222SVladimir Murzin 	kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
18877c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
18887c419937SMarc Zyngier 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
18896c30bfb1SFuad Tabba 	kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1);
1890*8669651cSQuentin Perret 	kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1);
189113e248aaSWill Deacon 	kvm_nvhe_sym(__icache_flags) = __icache_flags;
189273f38ef2SWill Deacon 	kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits;
18936c165223SWill Deacon }
18946c165223SWill Deacon 
18956c165223SWill Deacon static int kvm_hyp_init_protection(u32 hyp_va_bits)
18966c165223SWill Deacon {
18976c165223SWill Deacon 	void *addr = phys_to_virt(hyp_mem_base);
18986c165223SWill Deacon 	int ret;
18997c419937SMarc Zyngier 
1900bfa79a80SQuentin Perret 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
1901bfa79a80SQuentin Perret 	if (ret)
1902bfa79a80SQuentin Perret 		return ret;
1903bfa79a80SQuentin Perret 
1904bfa79a80SQuentin Perret 	ret = do_pkvm_init(hyp_va_bits);
1905bfa79a80SQuentin Perret 	if (ret)
1906bfa79a80SQuentin Perret 		return ret;
1907bfa79a80SQuentin Perret 
1908bfa79a80SQuentin Perret 	free_hyp_pgds();
1909bfa79a80SQuentin Perret 
1910bfa79a80SQuentin Perret 	return 0;
1911bfa79a80SQuentin Perret }
1912bfa79a80SQuentin Perret 
19139ed24f4bSMarc Zyngier /**
19149ed24f4bSMarc Zyngier  * Inits Hyp-mode on all online CPUs
19159ed24f4bSMarc Zyngier  */
19169ed24f4bSMarc Zyngier static int init_hyp_mode(void)
19179ed24f4bSMarc Zyngier {
1918bfa79a80SQuentin Perret 	u32 hyp_va_bits;
19199ed24f4bSMarc Zyngier 	int cpu;
1920bfa79a80SQuentin Perret 	int err = -ENOMEM;
1921bfa79a80SQuentin Perret 
1922bfa79a80SQuentin Perret 	/*
1923bfa79a80SQuentin Perret 	 * The protected Hyp-mode cannot be initialized if the memory pool
1924bfa79a80SQuentin Perret 	 * allocation has failed.
1925bfa79a80SQuentin Perret 	 */
1926bfa79a80SQuentin Perret 	if (is_protected_kvm_enabled() && !hyp_mem_base)
1927bfa79a80SQuentin Perret 		goto out_err;
19289ed24f4bSMarc Zyngier 
19299ed24f4bSMarc Zyngier 	/*
19309ed24f4bSMarc Zyngier 	 * Allocate Hyp PGD and setup Hyp identity mapping
19319ed24f4bSMarc Zyngier 	 */
1932bfa79a80SQuentin Perret 	err = kvm_mmu_init(&hyp_va_bits);
19339ed24f4bSMarc Zyngier 	if (err)
19349ed24f4bSMarc Zyngier 		goto out_err;
19359ed24f4bSMarc Zyngier 
19369ed24f4bSMarc Zyngier 	/*
19379ed24f4bSMarc Zyngier 	 * Allocate stack pages for Hypervisor-mode
19389ed24f4bSMarc Zyngier 	 */
19399ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
19409ed24f4bSMarc Zyngier 		unsigned long stack_page;
19419ed24f4bSMarc Zyngier 
19429ed24f4bSMarc Zyngier 		stack_page = __get_free_page(GFP_KERNEL);
19439ed24f4bSMarc Zyngier 		if (!stack_page) {
19449ed24f4bSMarc Zyngier 			err = -ENOMEM;
19459ed24f4bSMarc Zyngier 			goto out_err;
19469ed24f4bSMarc Zyngier 		}
19479ed24f4bSMarc Zyngier 
19489ed24f4bSMarc Zyngier 		per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
19499ed24f4bSMarc Zyngier 	}
19509ed24f4bSMarc Zyngier 
19519ed24f4bSMarc Zyngier 	/*
195230c95391SDavid Brazdil 	 * Allocate and initialize pages for Hypervisor-mode percpu regions.
195330c95391SDavid Brazdil 	 */
195430c95391SDavid Brazdil 	for_each_possible_cpu(cpu) {
195530c95391SDavid Brazdil 		struct page *page;
195630c95391SDavid Brazdil 		void *page_addr;
195730c95391SDavid Brazdil 
195830c95391SDavid Brazdil 		page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
195930c95391SDavid Brazdil 		if (!page) {
196030c95391SDavid Brazdil 			err = -ENOMEM;
196130c95391SDavid Brazdil 			goto out_err;
196230c95391SDavid Brazdil 		}
196330c95391SDavid Brazdil 
196430c95391SDavid Brazdil 		page_addr = page_address(page);
196530c95391SDavid Brazdil 		memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
1966fe41a7f8SQuentin Perret 		kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
196730c95391SDavid Brazdil 	}
196830c95391SDavid Brazdil 
196930c95391SDavid Brazdil 	/*
19709ed24f4bSMarc Zyngier 	 * Map the Hyp-code called directly from the host
19719ed24f4bSMarc Zyngier 	 */
19729ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
19739ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
19749ed24f4bSMarc Zyngier 	if (err) {
19759ed24f4bSMarc Zyngier 		kvm_err("Cannot map world-switch code\n");
19769ed24f4bSMarc Zyngier 		goto out_err;
19779ed24f4bSMarc Zyngier 	}
19789ed24f4bSMarc Zyngier 
197916174eeaSDavid Brazdil 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
198016174eeaSDavid Brazdil 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
19812d7bf218SDavid Brazdil 	if (err) {
198216174eeaSDavid Brazdil 		kvm_err("Cannot map .hyp.rodata section\n");
19832d7bf218SDavid Brazdil 		goto out_err;
19842d7bf218SDavid Brazdil 	}
19852d7bf218SDavid Brazdil 
19869ed24f4bSMarc Zyngier 	err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
19879ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
19889ed24f4bSMarc Zyngier 	if (err) {
19899ed24f4bSMarc Zyngier 		kvm_err("Cannot map rodata section\n");
19909ed24f4bSMarc Zyngier 		goto out_err;
19919ed24f4bSMarc Zyngier 	}
19929ed24f4bSMarc Zyngier 
1993380e18adSQuentin Perret 	/*
1994380e18adSQuentin Perret 	 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
1995380e18adSQuentin Perret 	 * section thanks to an assertion in the linker script. Map it RW and
1996380e18adSQuentin Perret 	 * the rest of .bss RO.
1997380e18adSQuentin Perret 	 */
1998380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
1999380e18adSQuentin Perret 				  kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
2000380e18adSQuentin Perret 	if (err) {
2001380e18adSQuentin Perret 		kvm_err("Cannot map hyp bss section: %d\n", err);
2002380e18adSQuentin Perret 		goto out_err;
2003380e18adSQuentin Perret 	}
2004380e18adSQuentin Perret 
2005380e18adSQuentin Perret 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
20069ed24f4bSMarc Zyngier 				  kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
20079ed24f4bSMarc Zyngier 	if (err) {
20089ed24f4bSMarc Zyngier 		kvm_err("Cannot map bss section\n");
20099ed24f4bSMarc Zyngier 		goto out_err;
20109ed24f4bSMarc Zyngier 	}
20119ed24f4bSMarc Zyngier 
20129ed24f4bSMarc Zyngier 	/*
20139ed24f4bSMarc Zyngier 	 * Map the Hyp stack pages
20149ed24f4bSMarc Zyngier 	 */
20159ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2016ce335431SKalesh Singh 		struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
20179ed24f4bSMarc Zyngier 		char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
2018ce335431SKalesh Singh 		unsigned long hyp_addr;
20199ed24f4bSMarc Zyngier 
2020ce335431SKalesh Singh 		/*
2021ce335431SKalesh Singh 		 * Allocate a contiguous HYP private VA range for the stack
2022ce335431SKalesh Singh 		 * and guard page. The allocation is also aligned based on
2023ce335431SKalesh Singh 		 * the order of its size.
2024ce335431SKalesh Singh 		 */
2025ce335431SKalesh Singh 		err = hyp_alloc_private_va_range(PAGE_SIZE * 2, &hyp_addr);
2026ce335431SKalesh Singh 		if (err) {
2027ce335431SKalesh Singh 			kvm_err("Cannot allocate hyp stack guard page\n");
2028ce335431SKalesh Singh 			goto out_err;
2029ce335431SKalesh Singh 		}
2030ce335431SKalesh Singh 
2031ce335431SKalesh Singh 		/*
2032ce335431SKalesh Singh 		 * Since the stack grows downwards, map the stack to the page
2033ce335431SKalesh Singh 		 * at the higher address and leave the lower guard page
2034ce335431SKalesh Singh 		 * unbacked.
2035ce335431SKalesh Singh 		 *
2036ce335431SKalesh Singh 		 * Any valid stack address now has the PAGE_SHIFT bit as 1
2037ce335431SKalesh Singh 		 * and addresses corresponding to the guard page have the
2038ce335431SKalesh Singh 		 * PAGE_SHIFT bit as 0 - this is used for overflow detection.
2039ce335431SKalesh Singh 		 */
2040ce335431SKalesh Singh 		err = __create_hyp_mappings(hyp_addr + PAGE_SIZE, PAGE_SIZE,
2041ce335431SKalesh Singh 					    __pa(stack_page), PAGE_HYP);
20429ed24f4bSMarc Zyngier 		if (err) {
20439ed24f4bSMarc Zyngier 			kvm_err("Cannot map hyp stack\n");
20449ed24f4bSMarc Zyngier 			goto out_err;
20459ed24f4bSMarc Zyngier 		}
2046ce335431SKalesh Singh 
2047ce335431SKalesh Singh 		/*
2048ce335431SKalesh Singh 		 * Save the stack PA in nvhe_init_params. This will be needed
2049ce335431SKalesh Singh 		 * to recreate the stack mapping in protected nVHE mode.
2050ce335431SKalesh Singh 		 * __hyp_pa() won't do the right thing there, since the stack
2051ce335431SKalesh Singh 		 * has been mapped in the flexible private VA space.
2052ce335431SKalesh Singh 		 */
2053ce335431SKalesh Singh 		params->stack_pa = __pa(stack_page);
2054ce335431SKalesh Singh 
2055ce335431SKalesh Singh 		params->stack_hyp_va = hyp_addr + (2 * PAGE_SIZE);
20569ed24f4bSMarc Zyngier 	}
20579ed24f4bSMarc Zyngier 
20589ed24f4bSMarc Zyngier 	for_each_possible_cpu(cpu) {
2059fe41a7f8SQuentin Perret 		char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu];
206030c95391SDavid Brazdil 		char *percpu_end = percpu_begin + nvhe_percpu_size();
20619ed24f4bSMarc Zyngier 
20629cc77581SQuentin Perret 		/* Map Hyp percpu pages */
206330c95391SDavid Brazdil 		err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
20649ed24f4bSMarc Zyngier 		if (err) {
206530c95391SDavid Brazdil 			kvm_err("Cannot map hyp percpu region\n");
20666e3bfbb2SAndrew Scull 			goto out_err;
20676e3bfbb2SAndrew Scull 		}
20689cc77581SQuentin Perret 
20699cc77581SQuentin Perret 		/* Prepare the CPU initialization parameters */
2070579d7ebeSRyan Roberts 		cpu_prepare_hyp_mode(cpu, hyp_va_bits);
20719ed24f4bSMarc Zyngier 	}
20729ed24f4bSMarc Zyngier 
20736c165223SWill Deacon 	kvm_hyp_init_symbols();
20746c165223SWill Deacon 
2075eeeee719SDavid Brazdil 	if (is_protected_kvm_enabled()) {
207694f5e8a4SDavid Brazdil 		init_cpu_logical_map();
207794f5e8a4SDavid Brazdil 
207852b9e265SWang Wensheng 		if (!init_psci_relay()) {
207952b9e265SWang Wensheng 			err = -ENODEV;
2080eeeee719SDavid Brazdil 			goto out_err;
2081eeeee719SDavid Brazdil 		}
2082eeeee719SDavid Brazdil 
2083bfa79a80SQuentin Perret 		err = kvm_hyp_init_protection(hyp_va_bits);
2084bfa79a80SQuentin Perret 		if (err) {
2085bfa79a80SQuentin Perret 			kvm_err("Failed to init hyp memory protection\n");
2086bfa79a80SQuentin Perret 			goto out_err;
2087bfa79a80SQuentin Perret 		}
2088bfa79a80SQuentin Perret 	}
20899ed24f4bSMarc Zyngier 
20909ed24f4bSMarc Zyngier 	return 0;
20919ed24f4bSMarc Zyngier 
20929ed24f4bSMarc Zyngier out_err:
20939ed24f4bSMarc Zyngier 	teardown_hyp_mode();
20949ed24f4bSMarc Zyngier 	kvm_err("error initializing Hyp mode: %d\n", err);
20959ed24f4bSMarc Zyngier 	return err;
20969ed24f4bSMarc Zyngier }
20979ed24f4bSMarc Zyngier 
20982f2e1a50SWill Deacon static void _kvm_host_prot_finalize(void *arg)
20991025c8c0SQuentin Perret {
21002f2e1a50SWill Deacon 	int *err = arg;
21012f2e1a50SWill Deacon 
21022f2e1a50SWill Deacon 	if (WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize)))
21032f2e1a50SWill Deacon 		WRITE_ONCE(*err, -EINVAL);
21042f2e1a50SWill Deacon }
21052f2e1a50SWill Deacon 
21062f2e1a50SWill Deacon static int pkvm_drop_host_privileges(void)
21072f2e1a50SWill Deacon {
21082f2e1a50SWill Deacon 	int ret = 0;
21092f2e1a50SWill Deacon 
21102f2e1a50SWill Deacon 	/*
21112f2e1a50SWill Deacon 	 * Flip the static key upfront as that may no longer be possible
21122f2e1a50SWill Deacon 	 * once the host stage 2 is installed.
21132f2e1a50SWill Deacon 	 */
21142f2e1a50SWill Deacon 	static_branch_enable(&kvm_protected_mode_initialized);
21152f2e1a50SWill Deacon 	on_each_cpu(_kvm_host_prot_finalize, &ret, 1);
21162f2e1a50SWill Deacon 	return ret;
21171025c8c0SQuentin Perret }
21181025c8c0SQuentin Perret 
2119bfa79a80SQuentin Perret static int finalize_hyp_mode(void)
2120bfa79a80SQuentin Perret {
2121bfa79a80SQuentin Perret 	if (!is_protected_kvm_enabled())
2122bfa79a80SQuentin Perret 		return 0;
2123bfa79a80SQuentin Perret 
212447e6223cSMarc Zyngier 	/*
212556961c63SQuentin Perret 	 * Exclude HYP sections from kmemleak so that they don't get peeked
212656961c63SQuentin Perret 	 * at, which would end badly once inaccessible.
212747e6223cSMarc Zyngier 	 */
212847e6223cSMarc Zyngier 	kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start);
2129522c9a64SZenghui Yu 	kmemleak_free_part_phys(hyp_mem_base, hyp_mem_size);
21302f2e1a50SWill Deacon 	return pkvm_drop_host_privileges();
2131bfa79a80SQuentin Perret }
2132bfa79a80SQuentin Perret 
21339ed24f4bSMarc Zyngier struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
21349ed24f4bSMarc Zyngier {
21359ed24f4bSMarc Zyngier 	struct kvm_vcpu *vcpu;
213646808a4cSMarc Zyngier 	unsigned long i;
21379ed24f4bSMarc Zyngier 
21389ed24f4bSMarc Zyngier 	mpidr &= MPIDR_HWID_BITMASK;
21399ed24f4bSMarc Zyngier 	kvm_for_each_vcpu(i, vcpu, kvm) {
21409ed24f4bSMarc Zyngier 		if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
21419ed24f4bSMarc Zyngier 			return vcpu;
21429ed24f4bSMarc Zyngier 	}
21439ed24f4bSMarc Zyngier 	return NULL;
21449ed24f4bSMarc Zyngier }
21459ed24f4bSMarc Zyngier 
2146d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
2147d663b8a2SPaolo Bonzini {
2148d663b8a2SPaolo Bonzini 	return irqchip_in_kernel(kvm);
2149d663b8a2SPaolo Bonzini }
2150d663b8a2SPaolo Bonzini 
21519ed24f4bSMarc Zyngier bool kvm_arch_has_irq_bypass(void)
21529ed24f4bSMarc Zyngier {
21539ed24f4bSMarc Zyngier 	return true;
21549ed24f4bSMarc Zyngier }
21559ed24f4bSMarc Zyngier 
21569ed24f4bSMarc Zyngier int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
21579ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
21589ed24f4bSMarc Zyngier {
21599ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
21609ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
21619ed24f4bSMarc Zyngier 
21629ed24f4bSMarc Zyngier 	return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
21639ed24f4bSMarc Zyngier 					  &irqfd->irq_entry);
21649ed24f4bSMarc Zyngier }
21659ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
21669ed24f4bSMarc Zyngier 				      struct irq_bypass_producer *prod)
21679ed24f4bSMarc Zyngier {
21689ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
21699ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
21709ed24f4bSMarc Zyngier 
21719ed24f4bSMarc Zyngier 	kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
21729ed24f4bSMarc Zyngier 				     &irqfd->irq_entry);
21739ed24f4bSMarc Zyngier }
21749ed24f4bSMarc Zyngier 
21759ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
21769ed24f4bSMarc Zyngier {
21779ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
21789ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
21799ed24f4bSMarc Zyngier 
21809ed24f4bSMarc Zyngier 	kvm_arm_halt_guest(irqfd->kvm);
21819ed24f4bSMarc Zyngier }
21829ed24f4bSMarc Zyngier 
21839ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
21849ed24f4bSMarc Zyngier {
21859ed24f4bSMarc Zyngier 	struct kvm_kernel_irqfd *irqfd =
21869ed24f4bSMarc Zyngier 		container_of(cons, struct kvm_kernel_irqfd, consumer);
21879ed24f4bSMarc Zyngier 
21889ed24f4bSMarc Zyngier 	kvm_arm_resume_guest(irqfd->kvm);
21899ed24f4bSMarc Zyngier }
21909ed24f4bSMarc Zyngier 
21919ed24f4bSMarc Zyngier /**
21929ed24f4bSMarc Zyngier  * Initialize Hyp-mode and memory mappings on all CPUs.
21939ed24f4bSMarc Zyngier  */
21949ed24f4bSMarc Zyngier int kvm_arch_init(void *opaque)
21959ed24f4bSMarc Zyngier {
21969ed24f4bSMarc Zyngier 	int err;
21979ed24f4bSMarc Zyngier 	bool in_hyp_mode;
21989ed24f4bSMarc Zyngier 
21999ed24f4bSMarc Zyngier 	if (!is_hyp_mode_available()) {
22009ed24f4bSMarc Zyngier 		kvm_info("HYP mode not available\n");
22019ed24f4bSMarc Zyngier 		return -ENODEV;
22029ed24f4bSMarc Zyngier 	}
22039ed24f4bSMarc Zyngier 
2204b6a68b97SMarc Zyngier 	if (kvm_get_mode() == KVM_MODE_NONE) {
2205b6a68b97SMarc Zyngier 		kvm_info("KVM disabled from command line\n");
2206b6a68b97SMarc Zyngier 		return -ENODEV;
2207b6a68b97SMarc Zyngier 	}
2208b6a68b97SMarc Zyngier 
2209f1f0c0cfSAlexandru Elisei 	err = kvm_sys_reg_table_init();
2210f1f0c0cfSAlexandru Elisei 	if (err) {
2211f1f0c0cfSAlexandru Elisei 		kvm_info("Error initializing system register tables");
2212f1f0c0cfSAlexandru Elisei 		return err;
2213f1f0c0cfSAlexandru Elisei 	}
2214f1f0c0cfSAlexandru Elisei 
22159ed24f4bSMarc Zyngier 	in_hyp_mode = is_kernel_in_hyp_mode();
22169ed24f4bSMarc Zyngier 
221796d389caSRob Herring 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
221896d389caSRob Herring 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
2219abf532ccSRob Herring 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
2220abf532ccSRob Herring 			 "Only trusted guests should be used on this system.\n");
2221abf532ccSRob Herring 
2222bf249d9eSAnshuman Khandual 	err = kvm_set_ipa_limit();
22239ed24f4bSMarc Zyngier 	if (err)
22249ed24f4bSMarc Zyngier 		return err;
22259ed24f4bSMarc Zyngier 
22269ed24f4bSMarc Zyngier 	err = kvm_arm_init_sve();
22279ed24f4bSMarc Zyngier 	if (err)
22289ed24f4bSMarc Zyngier 		return err;
22299ed24f4bSMarc Zyngier 
22303248136bSJulien Grall 	err = kvm_arm_vmid_alloc_init();
22313248136bSJulien Grall 	if (err) {
22323248136bSJulien Grall 		kvm_err("Failed to initialize VMID allocator.\n");
22333248136bSJulien Grall 		return err;
22343248136bSJulien Grall 	}
22353248136bSJulien Grall 
22369ed24f4bSMarc Zyngier 	if (!in_hyp_mode) {
22379ed24f4bSMarc Zyngier 		err = init_hyp_mode();
22389ed24f4bSMarc Zyngier 		if (err)
22399ed24f4bSMarc Zyngier 			goto out_err;
22409ed24f4bSMarc Zyngier 	}
22419ed24f4bSMarc Zyngier 
2242b881cdceSWill Deacon 	err = kvm_init_vector_slots();
2243b881cdceSWill Deacon 	if (err) {
2244b881cdceSWill Deacon 		kvm_err("Cannot initialise vector slots\n");
2245b881cdceSWill Deacon 		goto out_err;
2246b881cdceSWill Deacon 	}
2247b881cdceSWill Deacon 
22489ed24f4bSMarc Zyngier 	err = init_subsystems();
22499ed24f4bSMarc Zyngier 	if (err)
22509ed24f4bSMarc Zyngier 		goto out_hyp;
22519ed24f4bSMarc Zyngier 
2252bfa79a80SQuentin Perret 	if (!in_hyp_mode) {
2253bfa79a80SQuentin Perret 		err = finalize_hyp_mode();
2254bfa79a80SQuentin Perret 		if (err) {
2255bfa79a80SQuentin Perret 			kvm_err("Failed to finalize Hyp protection\n");
2256bfa79a80SQuentin Perret 			goto out_hyp;
2257bfa79a80SQuentin Perret 		}
2258bfa79a80SQuentin Perret 	}
2259bfa79a80SQuentin Perret 
2260f19f6644SDavid Brazdil 	if (is_protected_kvm_enabled()) {
22613eb681fbSDavid Brazdil 		kvm_info("Protected nVHE mode initialized successfully\n");
2262f19f6644SDavid Brazdil 	} else if (in_hyp_mode) {
22639ed24f4bSMarc Zyngier 		kvm_info("VHE mode initialized successfully\n");
2264f19f6644SDavid Brazdil 	} else {
22659ed24f4bSMarc Zyngier 		kvm_info("Hyp mode initialized successfully\n");
2266f19f6644SDavid Brazdil 	}
22679ed24f4bSMarc Zyngier 
22689ed24f4bSMarc Zyngier 	return 0;
22699ed24f4bSMarc Zyngier 
22709ed24f4bSMarc Zyngier out_hyp:
22719ed24f4bSMarc Zyngier 	hyp_cpu_pm_exit();
22729ed24f4bSMarc Zyngier 	if (!in_hyp_mode)
22739ed24f4bSMarc Zyngier 		teardown_hyp_mode();
22749ed24f4bSMarc Zyngier out_err:
22753248136bSJulien Grall 	kvm_arm_vmid_alloc_free();
22769ed24f4bSMarc Zyngier 	return err;
22779ed24f4bSMarc Zyngier }
22789ed24f4bSMarc Zyngier 
22799ed24f4bSMarc Zyngier /* NOP: Compiling as a module not supported */
22809ed24f4bSMarc Zyngier void kvm_arch_exit(void)
22819ed24f4bSMarc Zyngier {
228217ed14ebSSean Christopherson 	kvm_unregister_perf_callbacks();
22839ed24f4bSMarc Zyngier }
22849ed24f4bSMarc Zyngier 
2285d8b369c4SDavid Brazdil static int __init early_kvm_mode_cfg(char *arg)
2286d8b369c4SDavid Brazdil {
2287d8b369c4SDavid Brazdil 	if (!arg)
2288d8b369c4SDavid Brazdil 		return -EINVAL;
2289d8b369c4SDavid Brazdil 
2290b2a4d007SElliot Berman 	if (strcmp(arg, "none") == 0) {
2291b2a4d007SElliot Berman 		kvm_mode = KVM_MODE_NONE;
2292b2a4d007SElliot Berman 		return 0;
2293b2a4d007SElliot Berman 	}
2294b2a4d007SElliot Berman 
2295b2a4d007SElliot Berman 	if (!is_hyp_mode_available()) {
2296b2a4d007SElliot Berman 		pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n");
2297b2a4d007SElliot Berman 		return 0;
2298b2a4d007SElliot Berman 	}
2299b2a4d007SElliot Berman 
2300d8b369c4SDavid Brazdil 	if (strcmp(arg, "protected") == 0) {
2301cde5042aSWill Deacon 		if (!is_kernel_in_hyp_mode())
2302d8b369c4SDavid Brazdil 			kvm_mode = KVM_MODE_PROTECTED;
2303cde5042aSWill Deacon 		else
2304cde5042aSWill Deacon 			pr_warn_once("Protected KVM not available with VHE\n");
2305cde5042aSWill Deacon 
2306d8b369c4SDavid Brazdil 		return 0;
2307d8b369c4SDavid Brazdil 	}
2308d8b369c4SDavid Brazdil 
2309b6a68b97SMarc Zyngier 	if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) {
2310b6a68b97SMarc Zyngier 		kvm_mode = KVM_MODE_DEFAULT;
23111945a067SMarc Zyngier 		return 0;
2312b6a68b97SMarc Zyngier 	}
2313b6a68b97SMarc Zyngier 
2314d8b369c4SDavid Brazdil 	return -EINVAL;
2315d8b369c4SDavid Brazdil }
2316d8b369c4SDavid Brazdil early_param("kvm-arm.mode", early_kvm_mode_cfg);
2317d8b369c4SDavid Brazdil 
23183eb681fbSDavid Brazdil enum kvm_mode kvm_get_mode(void)
23193eb681fbSDavid Brazdil {
23203eb681fbSDavid Brazdil 	return kvm_mode;
23213eb681fbSDavid Brazdil }
23223eb681fbSDavid Brazdil 
23239ed24f4bSMarc Zyngier static int arm_init(void)
23249ed24f4bSMarc Zyngier {
23259ed24f4bSMarc Zyngier 	int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
23269ed24f4bSMarc Zyngier 	return rc;
23279ed24f4bSMarc Zyngier }
23289ed24f4bSMarc Zyngier 
23299ed24f4bSMarc Zyngier module_init(arm_init);
2330