Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/cpu_pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/kmemleak.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/kvm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/kvm_irqfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/irqbypass.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/sched/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <trace/events/kvm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "trace_arm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <asm/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <asm/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <asm/virt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <asm/kvm_arm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <asm/kvm_asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <asm/kvm_mmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <asm/kvm_emulate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <kvm/arm_hypercalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <kvm/arm_pmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <kvm/arm_psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #ifdef REQUIRES_VIRT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) __asm__(".arch_extension	virt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) unsigned long kvm_arm_hyp_percpu_base[NR_CPUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) /* The VMID used in the VTTBR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) static u32 kvm_next_vmid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static DEFINE_SPINLOCK(kvm_vmid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static bool vgic_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) int kvm_arch_hardware_setup(void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) int kvm_arch_check_processor_compat(void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 			    struct kvm_enable_cap *cap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	if (cap->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	switch (cap->cap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	case KVM_CAP_ARM_NISV_TO_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		kvm->arch.return_nisv_io_abort_to_user = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) static int kvm_arm_default_max_vcpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) static void set_default_spectre(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	 * The default is to expose CSV2 == 1 if the HW isn't affected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	 * Although this is a per-CPU feature, we make it global because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	 * asymmetric systems are just a nuisance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	 * Userspace can override this as long as it doesn't promise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	 * the impossible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		kvm->arch.pfr0_csv2 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		kvm->arch.pfr0_csv3 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  * kvm_arch_init_vm - initializes a VM data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  * @kvm:	pointer to the KVM struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	ret = kvm_arm_setup_stage2(kvm, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		goto out_free_stage2_pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	kvm_vgic_early_init(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	/* The maximum number of VCPUs is limited by the host's GIC model */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	kvm->arch.max_vcpus = kvm_arm_default_max_vcpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	set_default_spectre(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) out_free_stage2_pgd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	kvm_free_stage2_pgd(&kvm->arch.mmu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * kvm_arch_destroy_vm - destroy the VM data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * @kvm:	pointer to the KVM struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) void kvm_arch_destroy_vm(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	bitmap_free(kvm->arch.pmu_filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	kvm_vgic_destroy(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		if (kvm->vcpus[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			kvm_vcpu_destroy(kvm->vcpus[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			kvm->vcpus[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	atomic_set(&kvm->online_vcpus, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	switch (ext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	case KVM_CAP_IRQCHIP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		r = vgic_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	case KVM_CAP_IOEVENTFD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	case KVM_CAP_DEVICE_CTRL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	case KVM_CAP_USER_MEMORY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	case KVM_CAP_SYNC_MMU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	case KVM_CAP_ONE_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	case KVM_CAP_ARM_PSCI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	case KVM_CAP_ARM_PSCI_0_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	case KVM_CAP_READONLY_MEM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	case KVM_CAP_MP_STATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	case KVM_CAP_IMMEDIATE_EXIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	case KVM_CAP_VCPU_EVENTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	case KVM_CAP_ARM_NISV_TO_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	case KVM_CAP_ARM_INJECT_EXT_DABT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	case KVM_CAP_SET_GUEST_DEBUG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	case KVM_CAP_VCPU_ATTRIBUTES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		r = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	case KVM_CAP_ARM_SET_DEVICE_ADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		r = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	case KVM_CAP_NR_VCPUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		r = num_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	case KVM_CAP_MAX_VCPUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	case KVM_CAP_MAX_VCPU_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		if (kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			r = kvm->arch.max_vcpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 			r = kvm_arm_default_max_vcpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	case KVM_CAP_MSI_DEVID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		if (!kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			r = kvm->arch.vgic.msis_require_devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	case KVM_CAP_ARM_USER_IRQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		 * 1: EL1_VTIMER, EL1_PTIMER, and PMU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		 * (bump this number if adding more devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		r = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	case KVM_CAP_STEAL_TIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		r = kvm_arm_pvtime_supported();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	case KVM_CAP_ARM_EL1_32BIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	case KVM_CAP_GUEST_DEBUG_HW_BPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		r = get_num_brps();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	case KVM_CAP_GUEST_DEBUG_HW_WPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		r = get_num_wrps();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	case KVM_CAP_ARM_PMU_V3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		r = kvm_arm_support_pmu_v3();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	case KVM_CAP_ARM_INJECT_SERROR_ESR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	case KVM_CAP_ARM_VM_IPA_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		r = get_kvm_ipa_limit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	case KVM_CAP_ARM_SVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		r = system_supports_sve();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	case KVM_CAP_ARM_PTRAUTH_ADDRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	case KVM_CAP_ARM_PTRAUTH_GENERIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		r = system_has_full_ptr_auth();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) long kvm_arch_dev_ioctl(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 			unsigned int ioctl, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) struct kvm *kvm_arch_alloc_vm(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	if (!has_vhe())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		return kzalloc(sizeof(struct kvm), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	return vzalloc(sizeof(struct kvm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) void kvm_arch_free_vm(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (!has_vhe())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		kfree(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		vfree(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	if (id >= kvm->arch.max_vcpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	/* Force users to call KVM_ARM_VCPU_INIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	vcpu->arch.target = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	/* Set up the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	kvm_timer_vcpu_init(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	kvm_pmu_vcpu_init(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	kvm_arm_reset_debug_ptr(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	kvm_arm_pvtime_vcpu_init(&vcpu->arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	err = kvm_vgic_vcpu_init(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		static_branch_dec(&userspace_irqchip_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	kvm_timer_vcpu_terminate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	kvm_pmu_vcpu_destroy(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	kvm_arm_vcpu_destroy(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	return kvm_timer_is_pending(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	 * If we're about to block (most likely because we've just hit a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	 * WFI), we need to sync back the state of the GIC CPU interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	 * so that we have the latest PMR and group enables. This ensures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	 * that kvm_arch_vcpu_runnable has up-to-date data to decide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	 * whether we have pending interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	 * For the same reason, we want to tell GICv4 that we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	 * doorbells to be signalled, should an interrupt become pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	kvm_vgic_vmcr_sync(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	vgic_v4_put(vcpu, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	vgic_v4_load(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	struct kvm_s2_mmu *mmu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	int *last_ran;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	mmu = vcpu->arch.hw_mmu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	last_ran = this_cpu_ptr(mmu->last_vcpu_ran);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	 * We guarantee that both TLBs and I-cache are private to each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	 * vcpu. If detecting that a vcpu from the same VM has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	 * previously run on the same physical CPU, call into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	 * hypervisor code to nuke the relevant contexts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	 * We might get preempted before the vCPU actually runs, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	 * over-invalidation doesn't affect correctness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (*last_ran != vcpu->vcpu_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		kvm_call_hyp(__kvm_flush_cpu_context, mmu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		*last_ran = vcpu->vcpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	vcpu->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	kvm_vgic_load(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	kvm_timer_vcpu_load(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (has_vhe())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		kvm_vcpu_load_sysregs_vhe(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	kvm_arch_vcpu_load_fp(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	kvm_vcpu_pmu_restore_guest(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	if (single_task_running())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		vcpu_clear_wfx_traps(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		vcpu_set_wfx_traps(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	if (vcpu_has_ptrauth(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		vcpu_ptrauth_disable(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	kvm_arch_vcpu_load_debug_state_flags(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	kvm_arch_vcpu_put_debug_state_flags(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	kvm_arch_vcpu_put_fp(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (has_vhe())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		kvm_vcpu_put_sysregs_vhe(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	kvm_timer_vcpu_put(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	kvm_vgic_put(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	kvm_vcpu_pmu_restore_host(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	vcpu->cpu = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) static void vcpu_power_off(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	vcpu->arch.power_off = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	kvm_vcpu_kick(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 				    struct kvm_mp_state *mp_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	if (vcpu->arch.power_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		mp_state->mp_state = KVM_MP_STATE_STOPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 				    struct kvm_mp_state *mp_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	switch (mp_state->mp_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	case KVM_MP_STATE_RUNNABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		vcpu->arch.power_off = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	case KVM_MP_STATE_STOPPED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		vcpu_power_off(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474)  * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475)  * @v:		The VCPU pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477)  * If the guest CPU is not waiting for interrupts or an interrupt line is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478)  * asserted, the CPU is by definition runnable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	return ((irq_lines || kvm_vgic_vcpu_pending_irq(v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		&& !v->arch.power_off && !v->arch.pause);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	return vcpu_mode_priv(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) /* Just ensure a guest exit from a particular CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) static void exit_vm_noop(void *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) void force_vm_exit(const cpumask_t *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	smp_call_function_many(mask, exit_vm_noop, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505)  * need_new_vmid_gen - check that the VMID is still valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506)  * @vmid: The VMID to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508)  * return true if there is a new generation of VMIDs being used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510)  * The hardware supports a limited set of values with the value zero reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511)  * for the host, so we check if an assigned value belongs to a previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  * generation, which requires us to assign a new value. If we're the first to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513)  * use a VMID for the new generation, we must flush necessary caches and TLBs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514)  * on all CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) static bool need_new_vmid_gen(struct kvm_vmid *vmid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  * update_vmid - Update the vmid with a valid VMID for the current generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525)  * @vmid: The stage-2 VMID information struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) static void update_vmid(struct kvm_vmid *vmid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	if (!need_new_vmid_gen(vmid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	spin_lock(&kvm_vmid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	 * We need to re-check the vmid_gen here to ensure that if another vcpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	 * already allocated a valid vmid for this vm, then this vcpu should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	 * use the same vmid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (!need_new_vmid_gen(vmid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		spin_unlock(&kvm_vmid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	/* First user of a new VMID generation? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (unlikely(kvm_next_vmid == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		atomic64_inc(&kvm_vmid_gen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		kvm_next_vmid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		 * On SMP we know no other CPUs can use this CPU's or each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		 * other's VMID after force_vm_exit returns since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		 * kvm_vmid_lock blocks them from reentry to the guest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		force_vm_exit(cpu_all_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		 * Now broadcast TLB + ICACHE invalidation over the inner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		 * shareable domain to make sure all data structures are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		 * clean.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		kvm_call_hyp(__kvm_flush_vm_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	vmid->vmid = kvm_next_vmid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	kvm_next_vmid++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	spin_unlock(&kvm_vmid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	struct kvm *kvm = vcpu->kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (likely(vcpu->arch.has_run_once))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (!kvm_arm_vcpu_is_finalized(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	vcpu->arch.has_run_once = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	kvm_arm_vcpu_init_debug(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	if (likely(irqchip_in_kernel(kvm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		 * Map the VGIC hardware resources before running a vcpu the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		 * first time on this VM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		ret = kvm_vgic_map_resources(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		 * Tell the rest of the code that there are userspace irqchip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		 * VMs in the wild.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		static_branch_inc(&userspace_irqchip_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	ret = kvm_timer_enable(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	ret = kvm_arm_pmu_v3_enable(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) bool kvm_arch_intc_initialized(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	return vgic_initialized(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) void kvm_arm_halt_guest(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	kvm_for_each_vcpu(i, vcpu, kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		vcpu->arch.pause = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) void kvm_arm_resume_guest(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	kvm_for_each_vcpu(i, vcpu, kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		vcpu->arch.pause = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) static void vcpu_req_sleep(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	rcuwait_wait_event(wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			   (!vcpu->arch.power_off) &&(!vcpu->arch.pause),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			   TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	if (vcpu->arch.power_off || vcpu->arch.pause) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		/* Awaken to handle a signal, request we sleep again later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		kvm_make_request(KVM_REQ_SLEEP, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	 * Make sure we will observe a potential reset request if we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	 * observed a change to the power state. Pairs with the smp_wmb() in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	 * kvm_psci_vcpu_on().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	return vcpu->arch.target >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) static void check_vcpu_requests(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (kvm_request_pending(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			vcpu_req_sleep(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			kvm_reset_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		 * Clear IRQ_PENDING requests that were made to guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		 * that a VCPU sees new virtual interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			kvm_update_stolen_time(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			/* The distributor enable bits were changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			vgic_v4_put(vcpu, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			vgic_v4_load(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 			preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	if (likely(!vcpu_mode_is_32bit(vcpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	return !system_supports_32bit_el0() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		static_branch_unlikely(&arm64_mismatched_32bit_el0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703)  * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704)  * @vcpu:	The VCPU pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706)  * This function is called through the VCPU_RUN ioctl called from user space. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707)  * will execute VM code in a loop until the time slice for the process is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  * or some emulation is needed from user space in which case the function will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709)  * return with return value 0 and with the kvm_run structure filled in with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  * required data for the requested emulation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	struct kvm_run *run = vcpu->run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (unlikely(!kvm_vcpu_initialized(vcpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	ret = kvm_vcpu_first_run_init(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	if (run->exit_reason == KVM_EXIT_MMIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		ret = kvm_handle_mmio_return(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	if (run->immediate_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	vcpu_load(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	kvm_sigset_activate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	run->exit_reason = KVM_EXIT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	while (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		 * Check conditions before entering the guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		update_vmid(&vcpu->arch.hw_mmu->vmid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		check_vcpu_requests(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		 * Preparing the interrupts to be injected also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		 * involves poking the GIC, which must be done in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		 * non-preemptible context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		kvm_pmu_flush_hwstate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		kvm_vgic_flush_hwstate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		 * Exit if we have a signal pending so that we can deliver the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		 * signal to user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 			ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			run->exit_reason = KVM_EXIT_INTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		 * If we're using a userspace irqchip, then check if we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		 * to tell a userspace irqchip about timer or PMU level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		 * changes and if so, exit to userspace (the actual level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		 * state gets updated in kvm_timer_update_run and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		 * kvm_pmu_update_run below).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		if (static_branch_unlikely(&userspace_irqchip_in_use)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			if (kvm_timer_should_notify_user(vcpu) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 			    kvm_pmu_should_notify_user(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 				ret = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 				run->exit_reason = KVM_EXIT_INTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		 * Ensure we set mode to IN_GUEST_MODE after we disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		 * interrupts and before the final VCPU requests check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		 * See the comment in kvm_vcpu_exiting_guest_mode() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		 * Documentation/virt/kvm/vcpu-requests.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		smp_store_mb(vcpu->mode, IN_GUEST_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		if (ret <= 0 || need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		    kvm_request_pending(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			vcpu->mode = OUTSIDE_GUEST_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 			isb(); /* Ensure work in x_flush_hwstate is committed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			kvm_pmu_sync_hwstate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			if (static_branch_unlikely(&userspace_irqchip_in_use))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 				kvm_timer_sync_user(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			kvm_vgic_sync_hwstate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 			local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		kvm_arm_setup_debug(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		/**************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		 * Enter the guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		trace_kvm_entry(*vcpu_pc(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		guest_enter_irqoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		vcpu->mode = OUTSIDE_GUEST_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		vcpu->stat.exits++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		 * Back from guest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		 *************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		kvm_arm_clear_debug(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		 * We must sync the PMU state before the vgic state so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		 * that the vgic can properly sample the updated state of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		 * interrupt line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		kvm_pmu_sync_hwstate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		 * Sync the vgic state before syncing the timer state because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		 * the timer code needs to know if the virtual timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		 * interrupts are active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		kvm_vgic_sync_hwstate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		 * Sync the timer hardware state before enabling interrupts as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		 * we don't want vtimer interrupts to race with syncing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		 * timer virtual interrupt state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		if (static_branch_unlikely(&userspace_irqchip_in_use))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			kvm_timer_sync_user(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		kvm_arch_vcpu_ctxsync_fp(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		 * We may have taken a host interrupt in HYP mode (ie
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		 * while executing the guest). This interrupt is still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		 * pending, as we haven't serviced it yet!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		 * We're now back in SVC mode, with interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		 * disabled.  Enabling the interrupts now will have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		 * the effect of taking the interrupt again, in SVC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		 * mode this time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		 * We do local_irq_enable() before calling guest_exit() so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		 * that if a timer interrupt hits while running the guest we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		 * account that tick as being spent in the guest.  We enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		 * preemption after calling guest_exit() so that if we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		 * preempted we make sure ticks after that is not counted as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		 * guest time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		guest_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		/* Exit types that need handling before we can be preempted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		handle_exit_early(vcpu, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		 * The ARMv8 architecture doesn't give the hypervisor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		 * a mechanism to prevent a guest from dropping to AArch32 EL0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		 * if implemented by the CPU. If we spot the guest in such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		 * state and that we decided it wasn't supposed to do so (like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		 * with the asymmetric AArch32 case), return to userspace with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		 * a fatal error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		if (vcpu_mode_is_bad_32bit(vcpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			 * As we have caught the guest red-handed, decide that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 			 * it isn't fit for purpose anymore by making the vcpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			 * invalid. The VMM can try and fix it by issuing  a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			 * KVM_ARM_VCPU_INIT if it really wants to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			vcpu->arch.target = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			ret = ARM_EXCEPTION_IL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		ret = handle_exit(vcpu, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/* Tell userspace about in-kernel device output levels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		kvm_timer_update_run(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		kvm_pmu_update_run(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	kvm_sigset_deactivate(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	vcpu_put(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	int bit_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	bool set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	unsigned long *hcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	if (number == KVM_ARM_IRQ_CPU_IRQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		bit_index = __ffs(HCR_VI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	else /* KVM_ARM_IRQ_CPU_FIQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		bit_index = __ffs(HCR_VF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	hcr = vcpu_hcr(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	if (level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		set = test_and_set_bit(bit_index, hcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		set = test_and_clear_bit(bit_index, hcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	 * If we didn't change anything, no need to wake up or kick other CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	if (set == level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	 * trigger a world-switch round on the running physical CPU to set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	 * virtual IRQ/FIQ fields in the HCR appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	kvm_vcpu_kick(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			  bool line_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	u32 irq = irq_level->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	unsigned int irq_type, vcpu_idx, irq_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	int nrcpus = atomic_read(&kvm->online_vcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	struct kvm_vcpu *vcpu = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	bool level = irq_level->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	switch (irq_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	case KVM_ARM_IRQ_TYPE_CPU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		if (irqchip_in_kernel(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		if (vcpu_idx >= nrcpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		if (!vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		return vcpu_interrupt_line(vcpu, irq_num, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	case KVM_ARM_IRQ_TYPE_PPI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		if (!irqchip_in_kernel(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		if (vcpu_idx >= nrcpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		vcpu = kvm_get_vcpu(kvm, vcpu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		if (!vcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	case KVM_ARM_IRQ_TYPE_SPI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		if (!irqchip_in_kernel(kvm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		if (irq_num < VGIC_NR_PRIVATE_IRQS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 			       const struct kvm_vcpu_init *init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	unsigned int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	int phys_target = kvm_target_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (init->target != phys_target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	 * use the same target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	for (i = 0; i < sizeof(init->features) * 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		bool set = (init->features[i / 32] & (1 << (i % 32)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		if (set && i >= KVM_VCPU_MAX_FEATURES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 			return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		 * use the same feature set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		    test_bit(i, vcpu->arch.features) != set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		if (set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 			set_bit(i, vcpu->arch.features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	vcpu->arch.target = phys_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	/* Now we know what it is, we can reset it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	ret = kvm_reset_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		vcpu->arch.target = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 					 struct kvm_vcpu_init *init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	ret = kvm_vcpu_set_target(vcpu, init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	 * Ensure a rebooted VM will fault in RAM pages and detect if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	 * guest MMU is turned off and flush the caches as needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	 * S2FWB enforces all memory accesses to RAM being cacheable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	 * ensuring that the data side is always coherent. We still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	 * need to invalidate the I-cache though, as FWB does *not*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	 * imply CTR_EL0.DIC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	if (vcpu->arch.has_run_once) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			stage2_unmap_vm(vcpu->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			__flush_icache_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	vcpu_reset_hcr(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	 * Handle the "start in power-off" case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		vcpu_power_off(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		vcpu->arch.power_off = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 				 struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	int ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	switch (attr->group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 				 struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	int ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	switch (attr->group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 				 struct kvm_device_attr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	int ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	switch (attr->group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 				   struct kvm_vcpu_events *events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	memset(events, 0, sizeof(*events));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	return __kvm_arm_vcpu_get_events(vcpu, events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 				   struct kvm_vcpu_events *events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	/* check whether the reserved field is zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	for (i = 0; i < ARRAY_SIZE(events->reserved); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		if (events->reserved[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	/* check whether the pad field is zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		if (events->exception.pad[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	return __kvm_arm_vcpu_set_events(vcpu, events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) long kvm_arch_vcpu_ioctl(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			 unsigned int ioctl, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	struct kvm_vcpu *vcpu = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	struct kvm_device_attr attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	long r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	switch (ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	case KVM_ARM_VCPU_INIT: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 		struct kvm_vcpu_init init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		if (copy_from_user(&init, argp, sizeof(init)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	case KVM_SET_ONE_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	case KVM_GET_ONE_REG: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		struct kvm_one_reg reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		r = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		if (copy_from_user(&reg, argp, sizeof(reg)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		 * We could owe a reset due to PSCI. Handle the pending reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		 * here to ensure userspace register accesses are ordered after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		 * the reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 			kvm_reset_vcpu(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		if (ioctl == KVM_SET_ONE_REG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			r = kvm_arm_set_reg(vcpu, &reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			r = kvm_arm_get_reg(vcpu, &reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	case KVM_GET_REG_LIST: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		struct kvm_reg_list __user *user_list = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		struct kvm_reg_list reg_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		unsigned n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		r = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if (unlikely(!kvm_vcpu_initialized(vcpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		r = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		if (!kvm_arm_vcpu_is_finalized(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		n = reg_list.n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		reg_list.n = kvm_arm_num_regs(vcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		r = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		if (n < reg_list.n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	case KVM_SET_DEVICE_ATTR: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		if (copy_from_user(&attr, argp, sizeof(attr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		r = kvm_arm_vcpu_set_attr(vcpu, &attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	case KVM_GET_DEVICE_ATTR: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		if (copy_from_user(&attr, argp, sizeof(attr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		r = kvm_arm_vcpu_get_attr(vcpu, &attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	case KVM_HAS_DEVICE_ATTR: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		r = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		if (copy_from_user(&attr, argp, sizeof(attr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		r = kvm_arm_vcpu_has_attr(vcpu, &attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	case KVM_GET_VCPU_EVENTS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		struct kvm_vcpu_events events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		if (kvm_arm_vcpu_get_events(vcpu, &events))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		if (copy_to_user(argp, &events, sizeof(events)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	case KVM_SET_VCPU_EVENTS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		struct kvm_vcpu_events events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		if (copy_from_user(&events, argp, sizeof(events)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		return kvm_arm_vcpu_set_events(vcpu, &events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	case KVM_ARM_VCPU_FINALIZE: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		int what;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		if (!kvm_vcpu_initialized(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		if (get_user(what, (const int __user *)argp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		return kvm_arm_vcpu_finalize(vcpu, what);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 					struct kvm_memory_slot *memslot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	kvm_flush_remote_tlbs(kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 					struct kvm_arm_device_addr *dev_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	unsigned long dev_id, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		KVM_ARM_DEVICE_ID_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		KVM_ARM_DEVICE_TYPE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	switch (dev_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	case KVM_ARM_DEVICE_VGIC_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		if (!vgic_present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		return kvm_vgic_addr(kvm, type, &dev_addr->addr, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) long kvm_arch_vm_ioctl(struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		       unsigned int ioctl, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	struct kvm *kvm = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	switch (ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	case KVM_CREATE_IRQCHIP: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		if (!vgic_present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		mutex_lock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		mutex_unlock(&kvm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	case KVM_ARM_SET_DEVICE_ADDR: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 		struct kvm_arm_device_addr dev_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		if (copy_from_user(&dev_addr, argp, sizeof(dev_addr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	case KVM_ARM_PREFERRED_TARGET: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		struct kvm_vcpu_init init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		err = kvm_vcpu_preferred_target(&init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		if (copy_to_user(argp, &init, sizeof(init)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) static unsigned long nvhe_percpu_size(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		(unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) static unsigned long nvhe_percpu_order(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	unsigned long size = nvhe_percpu_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	return size ? get_order(size) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) /* A lookup table holding the hypervisor VA for each vector slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) static int kvm_init_vector_slots(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	void *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	kvm_init_vector_slot(base, HYP_VECTOR_DIRECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	if (kvm_system_needs_idmapped_vectors() && !has_vhe()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 					       __BP_HARDEN_HYP_VECS_SZ, &base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) static void cpu_prepare_hyp_mode(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	unsigned long tcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	 * Calculate the raw per-cpu offset without a translation from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	 * kernel's mapping to the linear mapping, and store it in tpidr_el2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	 * so that we can use adr_l to access per-cpu variables in EL2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	 * Also drop the KASAN tag which gets in the way...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 			    (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	params->mair_el2 = read_sysreg(mair_el1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	 * The ID map may be configured to use an extended virtual address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	 * range. This is only the case if system RAM is out of range for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	 * currently configured page size and VA_BITS, in which case we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	 * also need the extended virtual range for the HYP ID map, or we won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	 * be able to enable the EL2 MMU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	 * However, at EL2, there is only one TTBR register, and we can't switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	 * between translation tables *and* update TCR_EL2.T0SZ at the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	 * time. Bottom line: we need to use the extended range with *both* our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	 * translation tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	 * So use the same T0SZ value we use for the ID map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	tcr = (read_sysreg(tcr_el1) & TCR_EL2_MASK) | TCR_EL2_RES1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	tcr &= ~TCR_T0SZ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	params->tcr_el2 = tcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	params->stack_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu) + PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	params->pgd_pa = kvm_mmu_get_httbr();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	if (is_protected_kvm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		params->hcr_el2 = HCR_HOST_NVHE_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	params->vttbr = params->vtcr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	 * Flush the init params from the data cache because the struct will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	 * be read while the MMU is off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	kvm_flush_dcache_to_poc(params, sizeof(*params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) static void hyp_install_host_vector(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	struct kvm_nvhe_init_params *params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	struct arm_smccc_res res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	/* Switch from the HYP stub to our own HYP init vector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	__hyp_set_vectors(kvm_get_idmap_vector());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	 * Call initialization code, and switch to the full blown HYP code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	 * If the cpucaps haven't been finalized yet, something has gone very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	 * wrong, and hyp will crash and burn when it uses any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	 * cpus_have_const_cap() wrapper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	BUG_ON(!system_capabilities_finalized());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	params = this_cpu_ptr_nvhe_sym(kvm_init_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	WARN_ON(res.a0 != SMCCC_RET_SUCCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) static void cpu_init_hyp_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	hyp_install_host_vector();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	 * Disabling SSBD on a non-VHE system requires us to enable SSBS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	 * at EL2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	if (this_cpu_has_cap(ARM64_SSBS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	    arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		kvm_call_hyp_nvhe(__kvm_enable_ssbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) static void cpu_hyp_reset(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	if (!is_kernel_in_hyp_mode())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		__hyp_reset_vectors();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)  * EL2 vectors can be mapped and rerouted in a number of ways,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)  * depending on the kernel configuration and CPU present:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)  * - If the CPU is affected by Spectre-v2, the hardening sequence is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)  *   placed in one of the vector slots, which is executed before jumping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499)  *   to the real vectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)  * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502)  *   containing the hardening sequence is mapped next to the idmap page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)  *   and executed before jumping to the real vectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)  * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  *   empty slot is selected, mapped next to the idmap page, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  *   executed before jumping to the real vectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)  * Note that ARM64_SPECTRE_V3A is somewhat incompatible with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)  * VHE, as we don't have hypervisor-specific mappings. If the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)  * is VHE and yet selects this capability, it will be ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) static void cpu_set_hyp_vector(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	void *vector = hyp_spectre_vector_selector[data->slot];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	if (!is_protected_kvm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		*this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) static void cpu_hyp_reinit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	cpu_hyp_reset();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	if (is_kernel_in_hyp_mode())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		kvm_timer_init_vhe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		cpu_init_hyp_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	cpu_set_hyp_vector();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	kvm_arm_init_debug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	if (vgic_present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		kvm_vgic_init_cpu_hardware();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) static void _kvm_arch_hardware_enable(void *discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		cpu_hyp_reinit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		__this_cpu_write(kvm_arm_hardware_enabled, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) int kvm_arch_hardware_enable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	_kvm_arch_hardware_enable(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) static void _kvm_arch_hardware_disable(void *discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	if (__this_cpu_read(kvm_arm_hardware_enabled)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		cpu_hyp_reset();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		__this_cpu_write(kvm_arm_hardware_enabled, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) void kvm_arch_hardware_disable(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	if (!is_protected_kvm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		_kvm_arch_hardware_disable(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) #ifdef CONFIG_CPU_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 				    unsigned long cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 				    void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	 * kvm_arm_hardware_enabled is left with its old value over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	 * re-enable hyp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	case CPU_PM_ENTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 		if (__this_cpu_read(kvm_arm_hardware_enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			 * don't update kvm_arm_hardware_enabled here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 			 * so that the hardware will be re-enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 			 * when we resume. See below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 			cpu_hyp_reset();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	case CPU_PM_ENTER_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	case CPU_PM_EXIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		if (__this_cpu_read(kvm_arm_hardware_enabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 			/* The hardware was enabled before suspend. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			cpu_hyp_reinit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) static struct notifier_block hyp_init_cpu_pm_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	.notifier_call = hyp_init_cpu_pm_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) static void hyp_cpu_pm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	if (!is_protected_kvm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		cpu_pm_register_notifier(&hyp_init_cpu_pm_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) static void hyp_cpu_pm_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 	if (!is_protected_kvm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 		cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) static inline void hyp_cpu_pm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) static inline void hyp_cpu_pm_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) static void init_cpu_logical_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	 * Copy the MPIDR <-> logical CPU ID mapping to hyp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	 * Only copy the set of online CPUs whose features have been chacked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	 * against the finalized system capabilities. The hypervisor will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	 * allow any other CPUs from the `possible` set to boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	for_each_online_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) #define init_psci_0_1_impl_state(config, what)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	config.psci_0_1_ ## what ## _implemented = psci_ops.what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) static bool init_psci_relay(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 	 * If PSCI has not been initialized, protected KVM cannot install
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	 * itself on newly booted CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	if (!psci_ops.get_version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		kvm_err("Cannot initialize protected mode without PSCI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	kvm_host_psci_config.version = psci_ops.get_version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		init_psci_0_1_impl_state(kvm_host_psci_config, migrate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) static int init_common_resources(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	return kvm_set_ipa_limit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) static int init_subsystems(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	 * Enable hardware so that subsystem initialisation can access EL2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	 * Register CPU lower-power notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	hyp_cpu_pm_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	 * Init HYP view of VGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	err = kvm_vgic_hyp_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	switch (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		vgic_present = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	case -ENODEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	case -ENXIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		vgic_present = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	 * Init HYP architected timer support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	err = kvm_timer_hyp_init(vgic_present);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	kvm_perf_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	kvm_sys_reg_table_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	if (err || !is_protected_kvm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) static void teardown_hyp_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	free_hyp_pgds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		free_page(per_cpu(kvm_arm_hyp_stack_page, cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		free_pages(kvm_arm_hyp_percpu_base[cpu], nvhe_percpu_order());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static int do_pkvm_init(u32 hyp_va_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	void *per_cpu_base = kvm_ksym_ref(kvm_arm_hyp_percpu_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	hyp_install_host_vector();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 				num_possible_cpus(), kern_hyp_va(per_cpu_base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 				hyp_va_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) static int kvm_hyp_init_protection(u32 hyp_va_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	void *addr = phys_to_virt(hyp_mem_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	ret = do_pkvm_init(hyp_va_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	free_hyp_pgds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769)  * Inits Hyp-mode on all online CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) static int init_hyp_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	u32 hyp_va_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	 * The protected Hyp-mode cannot be initialized if the memory pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	 * allocation has failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	if (is_protected_kvm_enabled() && !hyp_mem_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	 * Allocate Hyp PGD and setup Hyp identity mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	err = kvm_mmu_init(&hyp_va_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	 * Allocate stack pages for Hypervisor-mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		unsigned long stack_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		stack_page = __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		if (!stack_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	 * Allocate and initialize pages for Hypervisor-mode percpu regions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		void *page_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		page = alloc_pages(GFP_KERNEL, nvhe_percpu_order());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		page_addr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		kvm_arm_hyp_percpu_base[cpu] = (unsigned long)page_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	 * Map the Hyp-code called directly from the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 				  kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		kvm_err("Cannot map world-switch code\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 				  kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 		kvm_err("Cannot map .hyp.rodata section\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	err = create_hyp_mappings(kvm_ksym_ref(__start_rodata),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 				  kvm_ksym_ref(__end_rodata), PAGE_HYP_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		kvm_err("Cannot map rodata section\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	 * .hyp.bss is guaranteed to be placed at the beginning of the .bss
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	 * section thanks to an assertion in the linker script. Map it RW and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	 * the rest of .bss RO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 				  kvm_ksym_ref(__hyp_bss_end), PAGE_HYP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		kvm_err("Cannot map hyp bss section: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 				  kvm_ksym_ref(__bss_stop), PAGE_HYP_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		kvm_err("Cannot map bss section\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	 * Map the Hyp stack pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 					  PAGE_HYP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 			kvm_err("Cannot map hyp stack\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		char *percpu_begin = (char *)kvm_arm_hyp_percpu_base[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		char *percpu_end = percpu_begin + nvhe_percpu_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		/* Map Hyp percpu pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 			kvm_err("Cannot map hyp percpu region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		/* Prepare the CPU initialization parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		cpu_prepare_hyp_mode(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	if (is_protected_kvm_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		init_cpu_logical_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 		if (!init_psci_relay()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 			err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	if (is_protected_kvm_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		err = kvm_hyp_init_protection(hyp_va_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 			kvm_err("Failed to init hyp memory protection\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	teardown_hyp_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	kvm_err("error initializing Hyp mode: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) void _kvm_host_prot_finalize(void *discard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) static inline int pkvm_mark_hyp(phys_addr_t start, phys_addr_t end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	return kvm_call_hyp_nvhe(__pkvm_mark_hyp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) #define pkvm_mark_hyp_section(__section)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	pkvm_mark_hyp(__pa_symbol(__section##_start),	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 			__pa_symbol(__section##_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) static int finalize_hyp_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	int cpu, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	if (!is_protected_kvm_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 	ret = pkvm_mark_hyp_section(__hyp_idmap_text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	ret = pkvm_mark_hyp_section(__hyp_text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	ret = pkvm_mark_hyp_section(__hyp_rodata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	 * Exclude HYP BSS from kmemleak so that it doesn't get peeked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	 * at, which would end badly once the section is inaccessible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	 * None of other sections should ever be introspected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	ret = pkvm_mark_hyp_section(__hyp_bss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	ret = pkvm_mark_hyp(hyp_mem_base, hyp_mem_base + hyp_mem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 		phys_addr_t start = virt_to_phys((void *)kvm_arm_hyp_percpu_base[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		phys_addr_t end = start + (PAGE_SIZE << nvhe_percpu_order());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 		ret = pkvm_mark_hyp(start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		start = virt_to_phys((void *)per_cpu(kvm_arm_hyp_stack_page, cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		end = start + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		ret = pkvm_mark_hyp(start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	 * Flip the static key upfront as that may no longer be possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	 * once the host stage 2 is installed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	static_branch_enable(&kvm_protected_mode_initialized);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	on_each_cpu(_kvm_host_prot_finalize, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) static void check_kvm_target_cpu(void *ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	*(int *)ret = kvm_target_cpu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	struct kvm_vcpu *vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 	mpidr &= MPIDR_HWID_BITMASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	kvm_for_each_vcpu(i, vcpu, kvm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 			return vcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) bool kvm_arch_has_irq_bypass(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 				      struct irq_bypass_producer *prod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	struct kvm_kernel_irqfd *irqfd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		container_of(cons, struct kvm_kernel_irqfd, consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 					  &irqfd->irq_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 				      struct irq_bypass_producer *prod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	struct kvm_kernel_irqfd *irqfd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		container_of(cons, struct kvm_kernel_irqfd, consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 				     &irqfd->irq_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	struct kvm_kernel_irqfd *irqfd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		container_of(cons, struct kvm_kernel_irqfd, consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	kvm_arm_halt_guest(irqfd->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	struct kvm_kernel_irqfd *irqfd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		container_of(cons, struct kvm_kernel_irqfd, consumer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	kvm_arm_resume_guest(irqfd->kvm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052)  * Initialize Hyp-mode and memory mappings on all CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) int kvm_arch_init(void *opaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	int ret, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	bool in_hyp_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	if (!is_hyp_mode_available()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 		kvm_info("HYP mode not available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	in_hyp_mode = is_kernel_in_hyp_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	    cpus_have_final_cap(ARM64_WORKAROUND_1508412))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 			 "Only trusted guests should be used on this system.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 		smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			kvm_err("Error, CPU %d not supported!\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	err = init_common_resources();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	err = kvm_arm_init_sve();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	if (!in_hyp_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		err = init_hyp_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	err = kvm_init_vector_slots();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 		kvm_err("Cannot initialise vector slots\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	err = init_subsystems();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 		goto out_hyp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	if (!in_hyp_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 		err = finalize_hyp_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 			kvm_err("Failed to finalize Hyp protection\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 			goto out_hyp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	if (is_protected_kvm_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		kvm_info("Protected nVHE mode initialized successfully\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	} else if (in_hyp_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		kvm_info("VHE mode initialized successfully\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		kvm_info("Hyp mode initialized successfully\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) out_hyp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	hyp_cpu_pm_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	if (!in_hyp_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		teardown_hyp_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) /* NOP: Compiling as a module not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) void kvm_arch_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	kvm_perf_teardown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) static int __init early_kvm_mode_cfg(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	if (!arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	if (strcmp(arg, "protected") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 		kvm_mode = KVM_MODE_PROTECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) early_param("kvm-arm.mode", early_kvm_mode_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) enum kvm_mode kvm_get_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	return kvm_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) static int arm_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) module_init(arm_init);