19ed24f4bSMarc Zyngier // SPDX-License-Identifier: GPL-2.0-only 29ed24f4bSMarc Zyngier /* 39ed24f4bSMarc Zyngier * Copyright (C) 2012 - Virtual Open Systems and Columbia University 49ed24f4bSMarc Zyngier * Author: Christoffer Dall <c.dall@virtualopensystems.com> 59ed24f4bSMarc Zyngier */ 69ed24f4bSMarc Zyngier 79ed24f4bSMarc Zyngier #include <linux/bug.h> 89ed24f4bSMarc Zyngier #include <linux/cpu_pm.h> 96caa5812SOliver Upton #include <linux/entry-kvm.h> 109ed24f4bSMarc Zyngier #include <linux/errno.h> 119ed24f4bSMarc Zyngier #include <linux/err.h> 129ed24f4bSMarc Zyngier #include <linux/kvm_host.h> 139ed24f4bSMarc Zyngier #include <linux/list.h> 149ed24f4bSMarc Zyngier #include <linux/module.h> 159ed24f4bSMarc Zyngier #include <linux/vmalloc.h> 169ed24f4bSMarc Zyngier #include <linux/fs.h> 179ed24f4bSMarc Zyngier #include <linux/mman.h> 189ed24f4bSMarc Zyngier #include <linux/sched.h> 199ed24f4bSMarc Zyngier #include <linux/kvm.h> 209ed24f4bSMarc Zyngier #include <linux/kvm_irqfd.h> 219ed24f4bSMarc Zyngier #include <linux/irqbypass.h> 229ed24f4bSMarc Zyngier #include <linux/sched/stat.h> 23eeeee719SDavid Brazdil #include <linux/psci.h> 249ed24f4bSMarc Zyngier #include <trace/events/kvm.h> 259ed24f4bSMarc Zyngier 269ed24f4bSMarc Zyngier #define CREATE_TRACE_POINTS 279ed24f4bSMarc Zyngier #include "trace_arm.h" 289ed24f4bSMarc Zyngier 299ed24f4bSMarc Zyngier #include <linux/uaccess.h> 309ed24f4bSMarc Zyngier #include <asm/ptrace.h> 319ed24f4bSMarc Zyngier #include <asm/mman.h> 329ed24f4bSMarc Zyngier #include <asm/tlbflush.h> 339ed24f4bSMarc Zyngier #include <asm/cacheflush.h> 349ed24f4bSMarc Zyngier #include <asm/cpufeature.h> 359ed24f4bSMarc Zyngier #include <asm/virt.h> 369ed24f4bSMarc Zyngier #include <asm/kvm_arm.h> 379ed24f4bSMarc Zyngier #include <asm/kvm_asm.h> 38814ad8f9SMarc Zyngier #include <asm/kvm_emulate.h> 399ed24f4bSMarc Zyngier #include <asm/kvm_mmu.h> 40a77b31dcSMarc Zyngier #include <asm/kvm_nested.h> 419d0c063aSFuad Tabba #include <asm/kvm_pkvm.h> 42814ad8f9SMarc Zyngier #include <asm/kvm_ptrauth.h> 439ed24f4bSMarc Zyngier #include <asm/sections.h> 449ed24f4bSMarc Zyngier 459ed24f4bSMarc Zyngier #include <kvm/arm_hypercalls.h> 469ed24f4bSMarc Zyngier #include <kvm/arm_pmu.h> 479ed24f4bSMarc Zyngier #include <kvm/arm_psci.h> 489ed24f4bSMarc Zyngier 49795a0bbaSMarc Zyngier #include "sys_regs.h" 50795a0bbaSMarc Zyngier 51d8b369c4SDavid Brazdil static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; 52d8b369c4SDavid Brazdil 530b5afe05SColton Lewis enum kvm_wfx_trap_policy { 540b5afe05SColton Lewis KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */ 550b5afe05SColton Lewis KVM_WFX_NOTRAP, 560b5afe05SColton Lewis KVM_WFX_TRAP, 570b5afe05SColton Lewis }; 580b5afe05SColton Lewis 590b5afe05SColton Lewis static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; 600b5afe05SColton Lewis static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; 610b5afe05SColton Lewis 6214ef9d04SMarc Zyngier DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); 6314ef9d04SMarc Zyngier 64db129d48SKalesh Singh DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); 6563fec243SDavid Brazdil DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); 669ed24f4bSMarc Zyngier 678c15c2a0SMostafa Saleh DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); 688c15c2a0SMostafa Saleh 69fa729bc7SSudeep Holla static bool vgic_present, kvm_arm_initialised; 709ed24f4bSMarc Zyngier 71733c758eSOliver Upton static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized); 729ed24f4bSMarc Zyngier DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use); 739ed24f4bSMarc Zyngier 74fa729bc7SSudeep Holla bool is_kvm_arm_initialised(void) 75fa729bc7SSudeep Holla { 76fa729bc7SSudeep Holla return kvm_arm_initialised; 77fa729bc7SSudeep Holla } 78fa729bc7SSudeep Holla 799ed24f4bSMarc Zyngier int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 809ed24f4bSMarc Zyngier { 819ed24f4bSMarc Zyngier return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; 829ed24f4bSMarc Zyngier } 839ed24f4bSMarc Zyngier 8492536992SFuad Tabba /* 8592536992SFuad Tabba * This functions as an allow-list of protected VM capabilities. 8692536992SFuad Tabba * Features not explicitly allowed by this function are denied. 8792536992SFuad Tabba */ 8892536992SFuad Tabba static bool pkvm_ext_allowed(struct kvm *kvm, long ext) 8992536992SFuad Tabba { 9092536992SFuad Tabba switch (ext) { 9192536992SFuad Tabba case KVM_CAP_IRQCHIP: 9292536992SFuad Tabba case KVM_CAP_ARM_PSCI: 9392536992SFuad Tabba case KVM_CAP_ARM_PSCI_0_2: 9492536992SFuad Tabba case KVM_CAP_NR_VCPUS: 9592536992SFuad Tabba case KVM_CAP_MAX_VCPUS: 9692536992SFuad Tabba case KVM_CAP_MAX_VCPU_ID: 9792536992SFuad Tabba case KVM_CAP_MSI_DEVID: 9892536992SFuad Tabba case KVM_CAP_ARM_VM_IPA_SIZE: 9992536992SFuad Tabba case KVM_CAP_ARM_PMU_V3: 10092536992SFuad Tabba case KVM_CAP_ARM_SVE: 10192536992SFuad Tabba case KVM_CAP_ARM_PTRAUTH_ADDRESS: 10292536992SFuad Tabba case KVM_CAP_ARM_PTRAUTH_GENERIC: 10392536992SFuad Tabba return true; 10492536992SFuad Tabba default: 10592536992SFuad Tabba return false; 10692536992SFuad Tabba } 10792536992SFuad Tabba } 10892536992SFuad Tabba 1099ed24f4bSMarc Zyngier int kvm_vm_ioctl_enable_cap(struct kvm *kvm, 1109ed24f4bSMarc Zyngier struct kvm_enable_cap *cap) 1119ed24f4bSMarc Zyngier { 11297a3dee1SFuad Tabba int r = -EINVAL; 1139ed24f4bSMarc Zyngier 1149ed24f4bSMarc Zyngier if (cap->flags) 1159ed24f4bSMarc Zyngier return -EINVAL; 1169ed24f4bSMarc Zyngier 11792536992SFuad Tabba if (kvm_vm_is_protected(kvm) && !pkvm_ext_allowed(kvm, cap->cap)) 11892536992SFuad Tabba return -EINVAL; 11992536992SFuad Tabba 1209ed24f4bSMarc Zyngier switch (cap->cap) { 1219ed24f4bSMarc Zyngier case KVM_CAP_ARM_NISV_TO_USER: 1229ed24f4bSMarc Zyngier r = 0; 12306394531SMarc Zyngier set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, 12406394531SMarc Zyngier &kvm->arch.flags); 1259ed24f4bSMarc Zyngier break; 126673638f4SSteven Price case KVM_CAP_ARM_MTE: 127c4d7c518SSteven Price mutex_lock(&kvm->lock); 12897a3dee1SFuad Tabba if (system_supports_mte() && !kvm->created_vcpus) { 129673638f4SSteven Price r = 0; 13006394531SMarc Zyngier set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags); 131c4d7c518SSteven Price } 132c4d7c518SSteven Price mutex_unlock(&kvm->lock); 133673638f4SSteven Price break; 134bfbab445SOliver Upton case KVM_CAP_ARM_SYSTEM_SUSPEND: 135bfbab445SOliver Upton r = 0; 136bfbab445SOliver Upton set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags); 137bfbab445SOliver Upton break; 1382f440b72SRicardo Koller case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: 1392f440b72SRicardo Koller mutex_lock(&kvm->slots_lock); 1402f440b72SRicardo Koller /* 1412f440b72SRicardo Koller * To keep things simple, allow changing the chunk 1422f440b72SRicardo Koller * size only when no memory slots have been created. 1432f440b72SRicardo Koller */ 14497a3dee1SFuad Tabba if (kvm_are_all_memslots_empty(kvm)) { 14597a3dee1SFuad Tabba u64 new_cap = cap->args[0]; 14697a3dee1SFuad Tabba 14797a3dee1SFuad Tabba if (!new_cap || kvm_is_block_size_supported(new_cap)) { 1482f440b72SRicardo Koller r = 0; 1492f440b72SRicardo Koller kvm->arch.mmu.split_page_chunk_size = new_cap; 1502f440b72SRicardo Koller } 15197a3dee1SFuad Tabba } 1522f440b72SRicardo Koller mutex_unlock(&kvm->slots_lock); 1532f440b72SRicardo Koller break; 1549ed24f4bSMarc Zyngier default: 1559ed24f4bSMarc Zyngier break; 1569ed24f4bSMarc Zyngier } 1579ed24f4bSMarc Zyngier 1589ed24f4bSMarc Zyngier return r; 1599ed24f4bSMarc Zyngier } 1609ed24f4bSMarc Zyngier 1615107000fSMarc Zyngier static int kvm_arm_default_max_vcpus(void) 1625107000fSMarc Zyngier { 1635107000fSMarc Zyngier return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; 1645107000fSMarc Zyngier } 1655107000fSMarc Zyngier 1669ed24f4bSMarc Zyngier /** 1679ed24f4bSMarc Zyngier * kvm_arch_init_vm - initializes a VM data structure 1689ed24f4bSMarc Zyngier * @kvm: pointer to the KVM struct 1690aa34b37SSebastian Ott * @type: kvm device type 1709ed24f4bSMarc Zyngier */ 1719ed24f4bSMarc Zyngier int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 1729ed24f4bSMarc Zyngier { 173a0e50aa3SChristoffer Dall int ret; 1749ed24f4bSMarc Zyngier 175c43120afSOliver Upton mutex_init(&kvm->arch.config_lock); 176c43120afSOliver Upton 177c43120afSOliver Upton #ifdef CONFIG_LOCKDEP 178c43120afSOliver Upton /* Clue in lockdep that the config_lock must be taken inside kvm->lock */ 179c43120afSOliver Upton mutex_lock(&kvm->lock); 180c43120afSOliver Upton mutex_lock(&kvm->arch.config_lock); 181c43120afSOliver Upton mutex_unlock(&kvm->arch.config_lock); 182c43120afSOliver Upton mutex_unlock(&kvm->lock); 183c43120afSOliver Upton #endif 184c43120afSOliver Upton 1854f128f8eSMarc Zyngier kvm_init_nested(kvm); 1864f128f8eSMarc Zyngier 1873f868e14SQuentin Perret ret = kvm_share_hyp(kvm, kvm + 1); 1889ed24f4bSMarc Zyngier if (ret) 189315775ffSQuentin Perret return ret; 1909ed24f4bSMarc Zyngier 1919d0c063aSFuad Tabba ret = pkvm_init_host_vm(kvm); 1929d0c063aSFuad Tabba if (ret) 193315775ffSQuentin Perret goto err_unshare_kvm; 1949ed24f4bSMarc Zyngier 1955f623a59SOliver Upton if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) { 196ae187fecSWill Deacon ret = -ENOMEM; 197315775ffSQuentin Perret goto err_unshare_kvm; 198ae187fecSWill Deacon } 199583cda1bSAlexandru Elisei cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask); 200583cda1bSAlexandru Elisei 201315775ffSQuentin Perret ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type); 202315775ffSQuentin Perret if (ret) 203315775ffSQuentin Perret goto err_free_cpumask; 204315775ffSQuentin Perret 2059ed24f4bSMarc Zyngier kvm_vgic_early_init(kvm); 2069ed24f4bSMarc Zyngier 2078a5eb2d2SMarc Zyngier kvm_timer_init_vm(kvm); 2088a5eb2d2SMarc Zyngier 2099ed24f4bSMarc Zyngier /* The maximum number of VCPUs is limited by the host's GIC model */ 210f502cc56SSean Christopherson kvm->max_vcpus = kvm_arm_default_max_vcpus(); 2119ed24f4bSMarc Zyngier 21205714cabSRaghavendra Rao Ananta kvm_arm_init_hypercalls(kvm); 21323711a5eSMarc Zyngier 2142251e9ffSOliver Upton bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES); 2153d0dba57SMarc Zyngier 216315775ffSQuentin Perret return 0; 217315775ffSQuentin Perret 218315775ffSQuentin Perret err_free_cpumask: 219315775ffSQuentin Perret free_cpumask_var(kvm->arch.supported_cpus); 220315775ffSQuentin Perret err_unshare_kvm: 221315775ffSQuentin Perret kvm_unshare_hyp(kvm, kvm + 1); 2229ed24f4bSMarc Zyngier return ret; 2239ed24f4bSMarc Zyngier } 2249ed24f4bSMarc Zyngier 2259ed24f4bSMarc Zyngier vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 2269ed24f4bSMarc Zyngier { 2279ed24f4bSMarc Zyngier return VM_FAULT_SIGBUS; 2289ed24f4bSMarc Zyngier } 2299ed24f4bSMarc Zyngier 2300d874858SOliver Upton void kvm_arch_create_vm_debugfs(struct kvm *kvm) 2315c1ebe9aSOliver Upton { 2325c1ebe9aSOliver Upton kvm_sys_regs_create_debugfs(kvm); 233*7c4f7354SSebastian Ene kvm_s2_ptdump_create_debugfs(kvm); 2345c1ebe9aSOliver Upton } 2359ed24f4bSMarc Zyngier 236ce5d2448SOliver Upton static void kvm_destroy_mpidr_data(struct kvm *kvm) 237ce5d2448SOliver Upton { 238ce5d2448SOliver Upton struct kvm_mpidr_data *data; 239ce5d2448SOliver Upton 240ce5d2448SOliver Upton mutex_lock(&kvm->arch.config_lock); 241ce5d2448SOliver Upton 242ce5d2448SOliver Upton data = rcu_dereference_protected(kvm->arch.mpidr_data, 243ce5d2448SOliver Upton lockdep_is_held(&kvm->arch.config_lock)); 244ce5d2448SOliver Upton if (data) { 245ce5d2448SOliver Upton rcu_assign_pointer(kvm->arch.mpidr_data, NULL); 246ce5d2448SOliver Upton synchronize_rcu(); 247ce5d2448SOliver Upton kfree(data); 248ce5d2448SOliver Upton } 249ce5d2448SOliver Upton 250ce5d2448SOliver Upton mutex_unlock(&kvm->arch.config_lock); 251ce5d2448SOliver Upton } 252ce5d2448SOliver Upton 2539ed24f4bSMarc Zyngier /** 2549ed24f4bSMarc Zyngier * kvm_arch_destroy_vm - destroy the VM data structure 2559ed24f4bSMarc Zyngier * @kvm: pointer to the KVM struct 2569ed24f4bSMarc Zyngier */ 2579ed24f4bSMarc Zyngier void kvm_arch_destroy_vm(struct kvm *kvm) 2589ed24f4bSMarc Zyngier { 259d7eec236SMarc Zyngier bitmap_free(kvm->arch.pmu_filter); 260583cda1bSAlexandru Elisei free_cpumask_var(kvm->arch.supported_cpus); 261d7eec236SMarc Zyngier 2629ed24f4bSMarc Zyngier kvm_vgic_destroy(kvm); 2639ed24f4bSMarc Zyngier 2649d0c063aSFuad Tabba if (is_protected_kvm_enabled()) 2659d0c063aSFuad Tabba pkvm_destroy_hyp_vm(kvm); 2669d0c063aSFuad Tabba 267ce5d2448SOliver Upton kvm_destroy_mpidr_data(kvm); 268ce5d2448SOliver Upton 269888f0880SMarc Zyngier kfree(kvm->arch.sysreg_masks); 27027592ae8SMarc Zyngier kvm_destroy_vcpus(kvm); 27152b28657SQuentin Perret 27252b28657SQuentin Perret kvm_unshare_hyp(kvm, kvm + 1); 273fb88707dSOliver Upton 274fb88707dSOliver Upton kvm_arm_teardown_hypercalls(kvm); 2759ed24f4bSMarc Zyngier } 2769ed24f4bSMarc Zyngier 277a07e9345SMarc Zyngier static bool kvm_has_full_ptr_auth(void) 278a07e9345SMarc Zyngier { 279a07e9345SMarc Zyngier bool apa, gpa, api, gpi, apa3, gpa3; 280a07e9345SMarc Zyngier u64 isar1, isar2, val; 281a07e9345SMarc Zyngier 282a07e9345SMarc Zyngier /* 283a07e9345SMarc Zyngier * Check that: 284a07e9345SMarc Zyngier * 285a07e9345SMarc Zyngier * - both Address and Generic auth are implemented for a given 286a07e9345SMarc Zyngier * algorithm (Q5, IMPDEF or Q3) 287a07e9345SMarc Zyngier * - only a single algorithm is implemented. 288a07e9345SMarc Zyngier */ 289a07e9345SMarc Zyngier if (!system_has_full_ptr_auth()) 290a07e9345SMarc Zyngier return false; 291a07e9345SMarc Zyngier 292a07e9345SMarc Zyngier isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 293a07e9345SMarc Zyngier isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 294a07e9345SMarc Zyngier 295a07e9345SMarc Zyngier apa = !!FIELD_GET(ID_AA64ISAR1_EL1_APA_MASK, isar1); 296a07e9345SMarc Zyngier val = FIELD_GET(ID_AA64ISAR1_EL1_GPA_MASK, isar1); 297a07e9345SMarc Zyngier gpa = (val == ID_AA64ISAR1_EL1_GPA_IMP); 298a07e9345SMarc Zyngier 299a07e9345SMarc Zyngier api = !!FIELD_GET(ID_AA64ISAR1_EL1_API_MASK, isar1); 300a07e9345SMarc Zyngier val = FIELD_GET(ID_AA64ISAR1_EL1_GPI_MASK, isar1); 301a07e9345SMarc Zyngier gpi = (val == ID_AA64ISAR1_EL1_GPI_IMP); 302a07e9345SMarc Zyngier 303a07e9345SMarc Zyngier apa3 = !!FIELD_GET(ID_AA64ISAR2_EL1_APA3_MASK, isar2); 304a07e9345SMarc Zyngier val = FIELD_GET(ID_AA64ISAR2_EL1_GPA3_MASK, isar2); 305a07e9345SMarc Zyngier gpa3 = (val == ID_AA64ISAR2_EL1_GPA3_IMP); 306a07e9345SMarc Zyngier 307a07e9345SMarc Zyngier return (apa == gpa && api == gpi && apa3 == gpa3 && 308a07e9345SMarc Zyngier (apa + api + apa3) == 1); 309a07e9345SMarc Zyngier } 310a07e9345SMarc Zyngier 3119ed24f4bSMarc Zyngier int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 3129ed24f4bSMarc Zyngier { 3139ed24f4bSMarc Zyngier int r; 31492536992SFuad Tabba 31592536992SFuad Tabba if (kvm && kvm_vm_is_protected(kvm) && !pkvm_ext_allowed(kvm, ext)) 31692536992SFuad Tabba return 0; 31792536992SFuad Tabba 3189ed24f4bSMarc Zyngier switch (ext) { 3199ed24f4bSMarc Zyngier case KVM_CAP_IRQCHIP: 3209ed24f4bSMarc Zyngier r = vgic_present; 3219ed24f4bSMarc Zyngier break; 3229ed24f4bSMarc Zyngier case KVM_CAP_IOEVENTFD: 3239ed24f4bSMarc Zyngier case KVM_CAP_USER_MEMORY: 3249ed24f4bSMarc Zyngier case KVM_CAP_SYNC_MMU: 3259ed24f4bSMarc Zyngier case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: 3269ed24f4bSMarc Zyngier case KVM_CAP_ONE_REG: 3279ed24f4bSMarc Zyngier case KVM_CAP_ARM_PSCI: 3289ed24f4bSMarc Zyngier case KVM_CAP_ARM_PSCI_0_2: 3299ed24f4bSMarc Zyngier case KVM_CAP_READONLY_MEM: 3309ed24f4bSMarc Zyngier case KVM_CAP_MP_STATE: 3319ed24f4bSMarc Zyngier case KVM_CAP_IMMEDIATE_EXIT: 3329ed24f4bSMarc Zyngier case KVM_CAP_VCPU_EVENTS: 3339ed24f4bSMarc Zyngier case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: 3349ed24f4bSMarc Zyngier case KVM_CAP_ARM_NISV_TO_USER: 3359ed24f4bSMarc Zyngier case KVM_CAP_ARM_INJECT_EXT_DABT: 33636fb4cd5SWill Deacon case KVM_CAP_SET_GUEST_DEBUG: 33736fb4cd5SWill Deacon case KVM_CAP_VCPU_ATTRIBUTES: 3383bf72569SJianyong Wu case KVM_CAP_PTP_KVM: 339bfbab445SOliver Upton case KVM_CAP_ARM_SYSTEM_SUSPEND: 34052882b9cSAlexey Kardashevskiy case KVM_CAP_IRQFD_RESAMPLE: 34130ec7997SMarc Zyngier case KVM_CAP_COUNTER_OFFSET: 3429ed24f4bSMarc Zyngier r = 1; 3439ed24f4bSMarc Zyngier break; 344fa18aca9SMaxim Levitsky case KVM_CAP_SET_GUEST_DEBUG2: 345fa18aca9SMaxim Levitsky return KVM_GUESTDBG_VALID_MASK; 3469ed24f4bSMarc Zyngier case KVM_CAP_ARM_SET_DEVICE_ADDR: 3479ed24f4bSMarc Zyngier r = 1; 3489ed24f4bSMarc Zyngier break; 3499ed24f4bSMarc Zyngier case KVM_CAP_NR_VCPUS: 350f60a00d7SVitaly Kuznetsov /* 351f60a00d7SVitaly Kuznetsov * ARM64 treats KVM_CAP_NR_CPUS differently from all other 352f60a00d7SVitaly Kuznetsov * architectures, as it does not always bound it to 353f60a00d7SVitaly Kuznetsov * KVM_CAP_MAX_VCPUS. It should not matter much because 354f60a00d7SVitaly Kuznetsov * this is just an advisory value. 355f60a00d7SVitaly Kuznetsov */ 356f60a00d7SVitaly Kuznetsov r = min_t(unsigned int, num_online_cpus(), 357f60a00d7SVitaly Kuznetsov kvm_arm_default_max_vcpus()); 3589ed24f4bSMarc Zyngier break; 3599ed24f4bSMarc Zyngier case KVM_CAP_MAX_VCPUS: 3609ed24f4bSMarc Zyngier case KVM_CAP_MAX_VCPU_ID: 3615107000fSMarc Zyngier if (kvm) 362f502cc56SSean Christopherson r = kvm->max_vcpus; 3635107000fSMarc Zyngier else 3645107000fSMarc Zyngier r = kvm_arm_default_max_vcpus(); 3659ed24f4bSMarc Zyngier break; 3669ed24f4bSMarc Zyngier case KVM_CAP_MSI_DEVID: 3679ed24f4bSMarc Zyngier if (!kvm) 3689ed24f4bSMarc Zyngier r = -EINVAL; 3699ed24f4bSMarc Zyngier else 3709ed24f4bSMarc Zyngier r = kvm->arch.vgic.msis_require_devid; 3719ed24f4bSMarc Zyngier break; 3729ed24f4bSMarc Zyngier case KVM_CAP_ARM_USER_IRQ: 3739ed24f4bSMarc Zyngier /* 3749ed24f4bSMarc Zyngier * 1: EL1_VTIMER, EL1_PTIMER, and PMU. 3759ed24f4bSMarc Zyngier * (bump this number if adding more devices) 3769ed24f4bSMarc Zyngier */ 3779ed24f4bSMarc Zyngier r = 1; 3789ed24f4bSMarc Zyngier break; 379673638f4SSteven Price case KVM_CAP_ARM_MTE: 380673638f4SSteven Price r = system_supports_mte(); 381673638f4SSteven Price break; 382004a0124SAndrew Jones case KVM_CAP_STEAL_TIME: 383004a0124SAndrew Jones r = kvm_arm_pvtime_supported(); 384004a0124SAndrew Jones break; 38536fb4cd5SWill Deacon case KVM_CAP_ARM_EL1_32BIT: 386d8569fbaSMark Rutland r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1); 3879ed24f4bSMarc Zyngier break; 38836fb4cd5SWill Deacon case KVM_CAP_GUEST_DEBUG_HW_BPS: 38936fb4cd5SWill Deacon r = get_num_brps(); 39036fb4cd5SWill Deacon break; 39136fb4cd5SWill Deacon case KVM_CAP_GUEST_DEBUG_HW_WPS: 39236fb4cd5SWill Deacon r = get_num_wrps(); 39336fb4cd5SWill Deacon break; 39436fb4cd5SWill Deacon case KVM_CAP_ARM_PMU_V3: 39536fb4cd5SWill Deacon r = kvm_arm_support_pmu_v3(); 39636fb4cd5SWill Deacon break; 39736fb4cd5SWill Deacon case KVM_CAP_ARM_INJECT_SERROR_ESR: 398d8569fbaSMark Rutland r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN); 39936fb4cd5SWill Deacon break; 40036fb4cd5SWill Deacon case KVM_CAP_ARM_VM_IPA_SIZE: 40136fb4cd5SWill Deacon r = get_kvm_ipa_limit(); 40236fb4cd5SWill Deacon break; 40336fb4cd5SWill Deacon case KVM_CAP_ARM_SVE: 40436fb4cd5SWill Deacon r = system_supports_sve(); 40536fb4cd5SWill Deacon break; 40636fb4cd5SWill Deacon case KVM_CAP_ARM_PTRAUTH_ADDRESS: 40736fb4cd5SWill Deacon case KVM_CAP_ARM_PTRAUTH_GENERIC: 408a07e9345SMarc Zyngier r = kvm_has_full_ptr_auth(); 40936fb4cd5SWill Deacon break; 4102f440b72SRicardo Koller case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: 4112f440b72SRicardo Koller if (kvm) 4122f440b72SRicardo Koller r = kvm->arch.mmu.split_page_chunk_size; 4132f440b72SRicardo Koller else 4142f440b72SRicardo Koller r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT; 4152f440b72SRicardo Koller break; 4162f440b72SRicardo Koller case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES: 4172f440b72SRicardo Koller r = kvm_supported_block_sizes(); 4182f440b72SRicardo Koller break; 4193f9cd0caSJing Zhang case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES: 4203f9cd0caSJing Zhang r = BIT(0); 4213f9cd0caSJing Zhang break; 42236fb4cd5SWill Deacon default: 42336fb4cd5SWill Deacon r = 0; 4249ed24f4bSMarc Zyngier } 42536fb4cd5SWill Deacon 4269ed24f4bSMarc Zyngier return r; 4279ed24f4bSMarc Zyngier } 4289ed24f4bSMarc Zyngier 4299ed24f4bSMarc Zyngier long kvm_arch_dev_ioctl(struct file *filp, 4309ed24f4bSMarc Zyngier unsigned int ioctl, unsigned long arg) 4319ed24f4bSMarc Zyngier { 4329ed24f4bSMarc Zyngier return -EINVAL; 4339ed24f4bSMarc Zyngier } 4349ed24f4bSMarc Zyngier 4359ed24f4bSMarc Zyngier struct kvm *kvm_arch_alloc_vm(void) 4369ed24f4bSMarc Zyngier { 437115bae92SJia He size_t sz = sizeof(struct kvm); 4389ed24f4bSMarc Zyngier 439115bae92SJia He if (!has_vhe()) 440115bae92SJia He return kzalloc(sz, GFP_KERNEL_ACCOUNT); 441115bae92SJia He 442115bae92SJia He return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO); 4439ed24f4bSMarc Zyngier } 4449ed24f4bSMarc Zyngier 4459ed24f4bSMarc Zyngier int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 4469ed24f4bSMarc Zyngier { 4479ed24f4bSMarc Zyngier if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) 4489ed24f4bSMarc Zyngier return -EBUSY; 4499ed24f4bSMarc Zyngier 450f502cc56SSean Christopherson if (id >= kvm->max_vcpus) 4519ed24f4bSMarc Zyngier return -EINVAL; 4529ed24f4bSMarc Zyngier 4539ed24f4bSMarc Zyngier return 0; 4549ed24f4bSMarc Zyngier } 4559ed24f4bSMarc Zyngier 4569ed24f4bSMarc Zyngier int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 4579ed24f4bSMarc Zyngier { 4589ed24f4bSMarc Zyngier int err; 4599ed24f4bSMarc Zyngier 4600acc7239SOliver Upton spin_lock_init(&vcpu->arch.mp_state_lock); 4610acc7239SOliver Upton 462c43120afSOliver Upton #ifdef CONFIG_LOCKDEP 463c43120afSOliver Upton /* Inform lockdep that the config_lock is acquired after vcpu->mutex */ 464c43120afSOliver Upton mutex_lock(&vcpu->mutex); 465c43120afSOliver Upton mutex_lock(&vcpu->kvm->arch.config_lock); 466c43120afSOliver Upton mutex_unlock(&vcpu->kvm->arch.config_lock); 467c43120afSOliver Upton mutex_unlock(&vcpu->mutex); 468c43120afSOliver Upton #endif 469c43120afSOliver Upton 4709ed24f4bSMarc Zyngier /* Force users to call KVM_ARM_VCPU_INIT */ 471ef984060SOliver Upton vcpu_clear_flag(vcpu, VCPU_INITIALIZED); 4729ed24f4bSMarc Zyngier 473e539451bSSean Christopherson vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO; 474e539451bSSean Christopherson 4759ed24f4bSMarc Zyngier /* Set up the timer */ 4769ed24f4bSMarc Zyngier kvm_timer_vcpu_init(vcpu); 4779ed24f4bSMarc Zyngier 4789ed24f4bSMarc Zyngier kvm_pmu_vcpu_init(vcpu); 4799ed24f4bSMarc Zyngier 4809ed24f4bSMarc Zyngier kvm_arm_reset_debug_ptr(vcpu); 4819ed24f4bSMarc Zyngier 4829ed24f4bSMarc Zyngier kvm_arm_pvtime_vcpu_init(&vcpu->arch); 4839ed24f4bSMarc Zyngier 484a0e50aa3SChristoffer Dall vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; 485a0e50aa3SChristoffer Dall 486ce5d2448SOliver Upton /* 487ce5d2448SOliver Upton * This vCPU may have been created after mpidr_data was initialized. 488ce5d2448SOliver Upton * Throw out the pre-computed mappings if that is the case which forces 489ce5d2448SOliver Upton * KVM to fall back to iteratively searching the vCPUs. 490ce5d2448SOliver Upton */ 491ce5d2448SOliver Upton kvm_destroy_mpidr_data(vcpu->kvm); 492ce5d2448SOliver Upton 4939ed24f4bSMarc Zyngier err = kvm_vgic_vcpu_init(vcpu); 4949ed24f4bSMarc Zyngier if (err) 4959ed24f4bSMarc Zyngier return err; 4969ed24f4bSMarc Zyngier 4973f868e14SQuentin Perret return kvm_share_hyp(vcpu, vcpu + 1); 4989ed24f4bSMarc Zyngier } 4999ed24f4bSMarc Zyngier 5009ed24f4bSMarc Zyngier void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 5019ed24f4bSMarc Zyngier { 5029ed24f4bSMarc Zyngier } 5039ed24f4bSMarc Zyngier 5049ed24f4bSMarc Zyngier void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 5059ed24f4bSMarc Zyngier { 506cc5705fbSMarc Zyngier if (vcpu_has_run_once(vcpu) && unlikely(!irqchip_in_kernel(vcpu->kvm))) 5079ed24f4bSMarc Zyngier static_branch_dec(&userspace_irqchip_in_use); 5089ed24f4bSMarc Zyngier 5099af3e08bSWill Deacon kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); 5109ed24f4bSMarc Zyngier kvm_timer_vcpu_terminate(vcpu); 5119ed24f4bSMarc Zyngier kvm_pmu_vcpu_destroy(vcpu); 51202e3858fSMarc Zyngier kvm_vgic_vcpu_destroy(vcpu); 5139ed24f4bSMarc Zyngier kvm_arm_vcpu_destroy(vcpu); 5149ed24f4bSMarc Zyngier } 5159ed24f4bSMarc Zyngier 5169ed24f4bSMarc Zyngier void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) 5179ed24f4bSMarc Zyngier { 5186109c5a6SSean Christopherson 5199ed24f4bSMarc Zyngier } 5209ed24f4bSMarc Zyngier 5219ed24f4bSMarc Zyngier void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) 5229ed24f4bSMarc Zyngier { 5236109c5a6SSean Christopherson 5249ed24f4bSMarc Zyngier } 5259ed24f4bSMarc Zyngier 526814ad8f9SMarc Zyngier static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu) 527814ad8f9SMarc Zyngier { 5287e814a20SFuad Tabba if (vcpu_has_ptrauth(vcpu) && !is_protected_kvm_enabled()) { 529814ad8f9SMarc Zyngier /* 5307e814a20SFuad Tabba * Either we're running an L2 guest, and the API/APK bits come 5317e814a20SFuad Tabba * from L1's HCR_EL2, or API/APK are both set. 532814ad8f9SMarc Zyngier */ 533814ad8f9SMarc Zyngier if (unlikely(vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))) { 534814ad8f9SMarc Zyngier u64 val; 535814ad8f9SMarc Zyngier 536814ad8f9SMarc Zyngier val = __vcpu_sys_reg(vcpu, HCR_EL2); 537814ad8f9SMarc Zyngier val &= (HCR_API | HCR_APK); 538814ad8f9SMarc Zyngier vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK); 539814ad8f9SMarc Zyngier vcpu->arch.hcr_el2 |= val; 540814ad8f9SMarc Zyngier } else { 541814ad8f9SMarc Zyngier vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK); 542814ad8f9SMarc Zyngier } 543814ad8f9SMarc Zyngier 544814ad8f9SMarc Zyngier /* 545814ad8f9SMarc Zyngier * Save the host keys if there is any chance for the guest 546814ad8f9SMarc Zyngier * to use pauth, as the entry code will reload the guest 547814ad8f9SMarc Zyngier * keys in that case. 548814ad8f9SMarc Zyngier */ 549814ad8f9SMarc Zyngier if (vcpu->arch.hcr_el2 & (HCR_API | HCR_APK)) { 550814ad8f9SMarc Zyngier struct kvm_cpu_context *ctxt; 5517e814a20SFuad Tabba 552814ad8f9SMarc Zyngier ctxt = this_cpu_ptr_hyp_sym(kvm_hyp_ctxt); 553814ad8f9SMarc Zyngier ptrauth_save_keys(ctxt); 554814ad8f9SMarc Zyngier } 555814ad8f9SMarc Zyngier } 556814ad8f9SMarc Zyngier } 557814ad8f9SMarc Zyngier 5580b5afe05SColton Lewis static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu) 5590b5afe05SColton Lewis { 5600b5afe05SColton Lewis if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) 5610b5afe05SColton Lewis return kvm_wfi_trap_policy == KVM_WFX_NOTRAP; 5620b5afe05SColton Lewis 5630b5afe05SColton Lewis return single_task_running() && 5640b5afe05SColton Lewis (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) || 5650b5afe05SColton Lewis vcpu->kvm->arch.vgic.nassgireq); 5660b5afe05SColton Lewis } 5670b5afe05SColton Lewis 5680b5afe05SColton Lewis static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu) 5690b5afe05SColton Lewis { 5700b5afe05SColton Lewis if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) 5710b5afe05SColton Lewis return kvm_wfe_trap_policy == KVM_WFX_NOTRAP; 5720b5afe05SColton Lewis 5730b5afe05SColton Lewis return single_task_running(); 5740b5afe05SColton Lewis } 5750b5afe05SColton Lewis 5769ed24f4bSMarc Zyngier void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 5779ed24f4bSMarc Zyngier { 578a0e50aa3SChristoffer Dall struct kvm_s2_mmu *mmu; 5799ed24f4bSMarc Zyngier int *last_ran; 5809ed24f4bSMarc Zyngier 5814f128f8eSMarc Zyngier if (vcpu_has_nv(vcpu)) 5824f128f8eSMarc Zyngier kvm_vcpu_load_hw_mmu(vcpu); 5834f128f8eSMarc Zyngier 584a0e50aa3SChristoffer Dall mmu = vcpu->arch.hw_mmu; 585a0e50aa3SChristoffer Dall last_ran = this_cpu_ptr(mmu->last_vcpu_ran); 5869ed24f4bSMarc Zyngier 5879ed24f4bSMarc Zyngier /* 58801dc9262SMarc Zyngier * We guarantee that both TLBs and I-cache are private to each 58901dc9262SMarc Zyngier * vcpu. If detecting that a vcpu from the same VM has 59001dc9262SMarc Zyngier * previously run on the same physical CPU, call into the 59101dc9262SMarc Zyngier * hypervisor code to nuke the relevant contexts. 59201dc9262SMarc Zyngier * 5939ed24f4bSMarc Zyngier * We might get preempted before the vCPU actually runs, but 5949ed24f4bSMarc Zyngier * over-invalidation doesn't affect correctness. 5959ed24f4bSMarc Zyngier */ 5965f4bd815SMarc Zyngier if (*last_ran != vcpu->vcpu_idx) { 59701dc9262SMarc Zyngier kvm_call_hyp(__kvm_flush_cpu_context, mmu); 5985f4bd815SMarc Zyngier *last_ran = vcpu->vcpu_idx; 5999ed24f4bSMarc Zyngier } 6009ed24f4bSMarc Zyngier 6019ed24f4bSMarc Zyngier vcpu->cpu = cpu; 6029ed24f4bSMarc Zyngier 6039ed24f4bSMarc Zyngier kvm_vgic_load(vcpu); 6049ed24f4bSMarc Zyngier kvm_timer_vcpu_load(vcpu); 60513aeb9b4SDavid Brazdil if (has_vhe()) 60627cde4c0SOliver Upton kvm_vcpu_load_vhe(vcpu); 6079ed24f4bSMarc Zyngier kvm_arch_vcpu_load_fp(vcpu); 6089ed24f4bSMarc Zyngier kvm_vcpu_pmu_restore_guest(vcpu); 6099ed24f4bSMarc Zyngier if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) 6109ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); 6119ed24f4bSMarc Zyngier 6120b5afe05SColton Lewis if (kvm_vcpu_should_clear_twe(vcpu)) 6130b5afe05SColton Lewis vcpu->arch.hcr_el2 &= ~HCR_TWE; 6149ed24f4bSMarc Zyngier else 6150b5afe05SColton Lewis vcpu->arch.hcr_el2 |= HCR_TWE; 6160b5afe05SColton Lewis 6170b5afe05SColton Lewis if (kvm_vcpu_should_clear_twi(vcpu)) 6180b5afe05SColton Lewis vcpu->arch.hcr_el2 &= ~HCR_TWI; 6190b5afe05SColton Lewis else 6200b5afe05SColton Lewis vcpu->arch.hcr_el2 |= HCR_TWI; 6219ed24f4bSMarc Zyngier 622814ad8f9SMarc Zyngier vcpu_set_pauth_traps(vcpu); 623814ad8f9SMarc Zyngier 624d2602bb4SSuzuki K Poulose kvm_arch_vcpu_load_debug_state_flags(vcpu); 625583cda1bSAlexandru Elisei 6261ba11daeSShaoqin Huang if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus)) 627583cda1bSAlexandru Elisei vcpu_set_on_unsupported_cpu(vcpu); 6289ed24f4bSMarc Zyngier } 6299ed24f4bSMarc Zyngier 6309ed24f4bSMarc Zyngier void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 6319ed24f4bSMarc Zyngier { 632d2602bb4SSuzuki K Poulose kvm_arch_vcpu_put_debug_state_flags(vcpu); 6339ed24f4bSMarc Zyngier kvm_arch_vcpu_put_fp(vcpu); 63413aeb9b4SDavid Brazdil if (has_vhe()) 63527cde4c0SOliver Upton kvm_vcpu_put_vhe(vcpu); 6369ed24f4bSMarc Zyngier kvm_timer_vcpu_put(vcpu); 6379ed24f4bSMarc Zyngier kvm_vgic_put(vcpu); 6389ed24f4bSMarc Zyngier kvm_vcpu_pmu_restore_host(vcpu); 6394f128f8eSMarc Zyngier if (vcpu_has_nv(vcpu)) 6404f128f8eSMarc Zyngier kvm_vcpu_put_hw_mmu(vcpu); 641100b4f09SShameer Kolothum kvm_arm_vmid_clear_active(); 6429ed24f4bSMarc Zyngier 643583cda1bSAlexandru Elisei vcpu_clear_on_unsupported_cpu(vcpu); 6449ed24f4bSMarc Zyngier vcpu->cpu = -1; 6459ed24f4bSMarc Zyngier } 6469ed24f4bSMarc Zyngier 6470acc7239SOliver Upton static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) 6489ed24f4bSMarc Zyngier { 6490acc7239SOliver Upton WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED); 6509ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_SLEEP, vcpu); 6519ed24f4bSMarc Zyngier kvm_vcpu_kick(vcpu); 6529ed24f4bSMarc Zyngier } 6539ed24f4bSMarc Zyngier 6540acc7239SOliver Upton void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) 6550acc7239SOliver Upton { 6560acc7239SOliver Upton spin_lock(&vcpu->arch.mp_state_lock); 6570acc7239SOliver Upton __kvm_arm_vcpu_power_off(vcpu); 6580acc7239SOliver Upton spin_unlock(&vcpu->arch.mp_state_lock); 6590acc7239SOliver Upton } 6600acc7239SOliver Upton 661b171f9bbSOliver Upton bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu) 662b171f9bbSOliver Upton { 6630acc7239SOliver Upton return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED; 664b171f9bbSOliver Upton } 665b171f9bbSOliver Upton 6667b33a09dSOliver Upton static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu) 6677b33a09dSOliver Upton { 6680acc7239SOliver Upton WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED); 6697b33a09dSOliver Upton kvm_make_request(KVM_REQ_SUSPEND, vcpu); 6707b33a09dSOliver Upton kvm_vcpu_kick(vcpu); 6717b33a09dSOliver Upton } 6727b33a09dSOliver Upton 6737b33a09dSOliver Upton static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu) 6747b33a09dSOliver Upton { 6750acc7239SOliver Upton return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED; 6767b33a09dSOliver Upton } 6777b33a09dSOliver Upton 6789ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 6799ed24f4bSMarc Zyngier struct kvm_mp_state *mp_state) 6809ed24f4bSMarc Zyngier { 6810acc7239SOliver Upton *mp_state = READ_ONCE(vcpu->arch.mp_state); 6829ed24f4bSMarc Zyngier 6839ed24f4bSMarc Zyngier return 0; 6849ed24f4bSMarc Zyngier } 6859ed24f4bSMarc Zyngier 6869ed24f4bSMarc Zyngier int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 6879ed24f4bSMarc Zyngier struct kvm_mp_state *mp_state) 6889ed24f4bSMarc Zyngier { 6899ed24f4bSMarc Zyngier int ret = 0; 6909ed24f4bSMarc Zyngier 6910acc7239SOliver Upton spin_lock(&vcpu->arch.mp_state_lock); 6920acc7239SOliver Upton 6939ed24f4bSMarc Zyngier switch (mp_state->mp_state) { 6949ed24f4bSMarc Zyngier case KVM_MP_STATE_RUNNABLE: 6950acc7239SOliver Upton WRITE_ONCE(vcpu->arch.mp_state, *mp_state); 6969ed24f4bSMarc Zyngier break; 6979ed24f4bSMarc Zyngier case KVM_MP_STATE_STOPPED: 6980acc7239SOliver Upton __kvm_arm_vcpu_power_off(vcpu); 6999ed24f4bSMarc Zyngier break; 7007b33a09dSOliver Upton case KVM_MP_STATE_SUSPENDED: 7017b33a09dSOliver Upton kvm_arm_vcpu_suspend(vcpu); 7029ed24f4bSMarc Zyngier break; 7039ed24f4bSMarc Zyngier default: 7049ed24f4bSMarc Zyngier ret = -EINVAL; 7059ed24f4bSMarc Zyngier } 7069ed24f4bSMarc Zyngier 7070acc7239SOliver Upton spin_unlock(&vcpu->arch.mp_state_lock); 7080acc7239SOliver Upton 7099ed24f4bSMarc Zyngier return ret; 7109ed24f4bSMarc Zyngier } 7119ed24f4bSMarc Zyngier 7129ed24f4bSMarc Zyngier /** 7139ed24f4bSMarc Zyngier * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled 7149ed24f4bSMarc Zyngier * @v: The VCPU pointer 7159ed24f4bSMarc Zyngier * 7169ed24f4bSMarc Zyngier * If the guest CPU is not waiting for interrupts or an interrupt line is 7179ed24f4bSMarc Zyngier * asserted, the CPU is by definition runnable. 7189ed24f4bSMarc Zyngier */ 7199ed24f4bSMarc Zyngier int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 7209ed24f4bSMarc Zyngier { 7219ed24f4bSMarc Zyngier bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF); 7229ed24f4bSMarc Zyngier return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) 723b171f9bbSOliver Upton && !kvm_arm_vcpu_stopped(v) && !v->arch.pause); 7249ed24f4bSMarc Zyngier } 7259ed24f4bSMarc Zyngier 7269ed24f4bSMarc Zyngier bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 7279ed24f4bSMarc Zyngier { 7289ed24f4bSMarc Zyngier return vcpu_mode_priv(vcpu); 7299ed24f4bSMarc Zyngier } 7309ed24f4bSMarc Zyngier 7318e5b0adeSLinus Torvalds #ifdef CONFIG_GUEST_PERF_EVENTS 732e1bfc245SSean Christopherson unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) 733e1bfc245SSean Christopherson { 734e1bfc245SSean Christopherson return *vcpu_pc(vcpu); 735e1bfc245SSean Christopherson } 7368e5b0adeSLinus Torvalds #endif 737e1bfc245SSean Christopherson 7385544750eSMarc Zyngier static void kvm_init_mpidr_data(struct kvm *kvm) 7395544750eSMarc Zyngier { 7405544750eSMarc Zyngier struct kvm_mpidr_data *data = NULL; 7415544750eSMarc Zyngier unsigned long c, mask, nr_entries; 7425544750eSMarc Zyngier u64 aff_set = 0, aff_clr = ~0UL; 7435544750eSMarc Zyngier struct kvm_vcpu *vcpu; 7445544750eSMarc Zyngier 7455544750eSMarc Zyngier mutex_lock(&kvm->arch.config_lock); 7465544750eSMarc Zyngier 747ce5d2448SOliver Upton if (rcu_access_pointer(kvm->arch.mpidr_data) || 748ce5d2448SOliver Upton atomic_read(&kvm->online_vcpus) == 1) 7495544750eSMarc Zyngier goto out; 7505544750eSMarc Zyngier 7515544750eSMarc Zyngier kvm_for_each_vcpu(c, vcpu, kvm) { 7525544750eSMarc Zyngier u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); 7535544750eSMarc Zyngier aff_set |= aff; 7545544750eSMarc Zyngier aff_clr &= aff; 7555544750eSMarc Zyngier } 7565544750eSMarc Zyngier 7575544750eSMarc Zyngier /* 7585544750eSMarc Zyngier * A significant bit can be either 0 or 1, and will only appear in 7595544750eSMarc Zyngier * aff_set. Use aff_clr to weed out the useless stuff. 7605544750eSMarc Zyngier */ 7615544750eSMarc Zyngier mask = aff_set ^ aff_clr; 7625544750eSMarc Zyngier nr_entries = BIT_ULL(hweight_long(mask)); 7635544750eSMarc Zyngier 7645544750eSMarc Zyngier /* 7655544750eSMarc Zyngier * Don't let userspace fool us. If we need more than a single page 7665544750eSMarc Zyngier * to describe the compressed MPIDR array, just fall back to the 7675544750eSMarc Zyngier * iterative method. Single vcpu VMs do not need this either. 7685544750eSMarc Zyngier */ 7695544750eSMarc Zyngier if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE) 7705544750eSMarc Zyngier data = kzalloc(struct_size(data, cmpidr_to_idx, nr_entries), 7715544750eSMarc Zyngier GFP_KERNEL_ACCOUNT); 7725544750eSMarc Zyngier 7735544750eSMarc Zyngier if (!data) 7745544750eSMarc Zyngier goto out; 7755544750eSMarc Zyngier 7765544750eSMarc Zyngier data->mpidr_mask = mask; 7775544750eSMarc Zyngier 7785544750eSMarc Zyngier kvm_for_each_vcpu(c, vcpu, kvm) { 7795544750eSMarc Zyngier u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); 7805544750eSMarc Zyngier u16 index = kvm_mpidr_index(data, aff); 7815544750eSMarc Zyngier 7825544750eSMarc Zyngier data->cmpidr_to_idx[index] = c; 7835544750eSMarc Zyngier } 7845544750eSMarc Zyngier 785ce5d2448SOliver Upton rcu_assign_pointer(kvm->arch.mpidr_data, data); 7865544750eSMarc Zyngier out: 7875544750eSMarc Zyngier mutex_unlock(&kvm->arch.config_lock); 7885544750eSMarc Zyngier } 7895544750eSMarc Zyngier 790b5aa368aSMarc Zyngier /* 791b5aa368aSMarc Zyngier * Handle both the initialisation that is being done when the vcpu is 792b5aa368aSMarc Zyngier * run for the first time, as well as the updates that must be 793b5aa368aSMarc Zyngier * performed each time we get a new thread dealing with this vcpu. 794b5aa368aSMarc Zyngier */ 795b5aa368aSMarc Zyngier int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) 7969ed24f4bSMarc Zyngier { 7979ed24f4bSMarc Zyngier struct kvm *kvm = vcpu->kvm; 798b5aa368aSMarc Zyngier int ret; 7999ed24f4bSMarc Zyngier 800b5aa368aSMarc Zyngier if (!kvm_vcpu_initialized(vcpu)) 801b5aa368aSMarc Zyngier return -ENOEXEC; 8029ed24f4bSMarc Zyngier 8039ed24f4bSMarc Zyngier if (!kvm_arm_vcpu_is_finalized(vcpu)) 8049ed24f4bSMarc Zyngier return -EPERM; 8059ed24f4bSMarc Zyngier 806b5aa368aSMarc Zyngier ret = kvm_arch_vcpu_run_map_fp(vcpu); 807b5aa368aSMarc Zyngier if (ret) 808b5aa368aSMarc Zyngier return ret; 809b5aa368aSMarc Zyngier 810cc5705fbSMarc Zyngier if (likely(vcpu_has_run_once(vcpu))) 811b5aa368aSMarc Zyngier return 0; 8129ed24f4bSMarc Zyngier 8135544750eSMarc Zyngier kvm_init_mpidr_data(kvm); 8145544750eSMarc Zyngier 815263d6287SAlexandru Elisei kvm_arm_vcpu_init_debug(vcpu); 816263d6287SAlexandru Elisei 8179ed24f4bSMarc Zyngier if (likely(irqchip_in_kernel(kvm))) { 8189ed24f4bSMarc Zyngier /* 8199ed24f4bSMarc Zyngier * Map the VGIC hardware resources before running a vcpu the 8209ed24f4bSMarc Zyngier * first time on this VM. 8219ed24f4bSMarc Zyngier */ 8229ed24f4bSMarc Zyngier ret = kvm_vgic_map_resources(kvm); 8239ed24f4bSMarc Zyngier if (ret) 8249ed24f4bSMarc Zyngier return ret; 8259ed24f4bSMarc Zyngier } 8269ed24f4bSMarc Zyngier 827795a0bbaSMarc Zyngier ret = kvm_finalize_sys_regs(vcpu); 8283ed0b512SMarc Zyngier if (ret) 8293ed0b512SMarc Zyngier return ret; 8303ed0b512SMarc Zyngier 831c5bac1efSMarc Zyngier /* 832795a0bbaSMarc Zyngier * This needs to happen after any restriction has been applied 833795a0bbaSMarc Zyngier * to the feature set. 834c5bac1efSMarc Zyngier */ 835f1ff3fc5SSebastian Ott kvm_calculate_traps(vcpu); 836c5bac1efSMarc Zyngier 8379ed24f4bSMarc Zyngier ret = kvm_timer_enable(vcpu); 8389ed24f4bSMarc Zyngier if (ret) 8399ed24f4bSMarc Zyngier return ret; 8409ed24f4bSMarc Zyngier 8419ed24f4bSMarc Zyngier ret = kvm_arm_pmu_v3_enable(vcpu); 8421408e73dSMarc Zyngier if (ret) 8431408e73dSMarc Zyngier return ret; 8441408e73dSMarc Zyngier 8459d0c063aSFuad Tabba if (is_protected_kvm_enabled()) { 8469d0c063aSFuad Tabba ret = pkvm_create_hyp_vm(kvm); 8479d0c063aSFuad Tabba if (ret) 8489d0c063aSFuad Tabba return ret; 8499d0c063aSFuad Tabba } 8509d0c063aSFuad Tabba 8511408e73dSMarc Zyngier if (!irqchip_in_kernel(kvm)) { 8521408e73dSMarc Zyngier /* 8531408e73dSMarc Zyngier * Tell the rest of the code that there are userspace irqchip 8541408e73dSMarc Zyngier * VMs in the wild. 8551408e73dSMarc Zyngier */ 8561408e73dSMarc Zyngier static_branch_inc(&userspace_irqchip_in_use); 8571408e73dSMarc Zyngier } 8589ed24f4bSMarc Zyngier 8592a0c3433SFuad Tabba /* 8602a0c3433SFuad Tabba * Initialize traps for protected VMs. 8612a0c3433SFuad Tabba * NOTE: Move to run in EL2 directly, rather than via a hypercall, once 8622a0c3433SFuad Tabba * the code is in place for first run initialization at EL2. 8632a0c3433SFuad Tabba */ 8642a0c3433SFuad Tabba if (kvm_vm_is_protected(kvm)) 8652a0c3433SFuad Tabba kvm_call_hyp_nvhe(__pkvm_vcpu_init_traps, vcpu); 8662a0c3433SFuad Tabba 8674bba7f7dSOliver Upton mutex_lock(&kvm->arch.config_lock); 86806394531SMarc Zyngier set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags); 8694bba7f7dSOliver Upton mutex_unlock(&kvm->arch.config_lock); 8705177fe91SMarc Zyngier 8719ed24f4bSMarc Zyngier return ret; 8729ed24f4bSMarc Zyngier } 8739ed24f4bSMarc Zyngier 8749ed24f4bSMarc Zyngier bool kvm_arch_intc_initialized(struct kvm *kvm) 8759ed24f4bSMarc Zyngier { 8769ed24f4bSMarc Zyngier return vgic_initialized(kvm); 8779ed24f4bSMarc Zyngier } 8789ed24f4bSMarc Zyngier 8799ed24f4bSMarc Zyngier void kvm_arm_halt_guest(struct kvm *kvm) 8809ed24f4bSMarc Zyngier { 88146808a4cSMarc Zyngier unsigned long i; 8829ed24f4bSMarc Zyngier struct kvm_vcpu *vcpu; 8839ed24f4bSMarc Zyngier 8849ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, vcpu, kvm) 8859ed24f4bSMarc Zyngier vcpu->arch.pause = true; 8869ed24f4bSMarc Zyngier kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); 8879ed24f4bSMarc Zyngier } 8889ed24f4bSMarc Zyngier 8899ed24f4bSMarc Zyngier void kvm_arm_resume_guest(struct kvm *kvm) 8909ed24f4bSMarc Zyngier { 89146808a4cSMarc Zyngier unsigned long i; 8929ed24f4bSMarc Zyngier struct kvm_vcpu *vcpu; 8939ed24f4bSMarc Zyngier 8949ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, vcpu, kvm) { 8959ed24f4bSMarc Zyngier vcpu->arch.pause = false; 896d92a5d1cSSean Christopherson __kvm_vcpu_wake_up(vcpu); 8979ed24f4bSMarc Zyngier } 8989ed24f4bSMarc Zyngier } 8999ed24f4bSMarc Zyngier 9001c6219e3SOliver Upton static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu) 9019ed24f4bSMarc Zyngier { 90238060944SPaolo Bonzini struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); 9039ed24f4bSMarc Zyngier 90438060944SPaolo Bonzini rcuwait_wait_event(wait, 905b171f9bbSOliver Upton (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause), 90638060944SPaolo Bonzini TASK_INTERRUPTIBLE); 9079ed24f4bSMarc Zyngier 908b171f9bbSOliver Upton if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) { 9099ed24f4bSMarc Zyngier /* Awaken to handle a signal, request we sleep again later. */ 9109ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_SLEEP, vcpu); 9119ed24f4bSMarc Zyngier } 9129ed24f4bSMarc Zyngier 9139ed24f4bSMarc Zyngier /* 9149ed24f4bSMarc Zyngier * Make sure we will observe a potential reset request if we've 9159ed24f4bSMarc Zyngier * observed a change to the power state. Pairs with the smp_wmb() in 9169ed24f4bSMarc Zyngier * kvm_psci_vcpu_on(). 9179ed24f4bSMarc Zyngier */ 9189ed24f4bSMarc Zyngier smp_rmb(); 9199ed24f4bSMarc Zyngier } 9209ed24f4bSMarc Zyngier 9216109c5a6SSean Christopherson /** 9226109c5a6SSean Christopherson * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior 9236109c5a6SSean Christopherson * @vcpu: The VCPU pointer 9246109c5a6SSean Christopherson * 9256109c5a6SSean Christopherson * Suspend execution of a vCPU until a valid wake event is detected, i.e. until 9266109c5a6SSean Christopherson * the vCPU is runnable. The vCPU may or may not be scheduled out, depending 9276109c5a6SSean Christopherson * on when a wake event arrives, e.g. there may already be a pending wake event. 9286109c5a6SSean Christopherson */ 9296109c5a6SSean Christopherson void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) 9306109c5a6SSean Christopherson { 9316109c5a6SSean Christopherson /* 9326109c5a6SSean Christopherson * Sync back the state of the GIC CPU interface so that we have 9336109c5a6SSean Christopherson * the latest PMR and group enables. This ensures that 9346109c5a6SSean Christopherson * kvm_arch_vcpu_runnable has up-to-date data to decide whether 9356109c5a6SSean Christopherson * we have pending interrupts, e.g. when determining if the 9366109c5a6SSean Christopherson * vCPU should block. 9376109c5a6SSean Christopherson * 9386109c5a6SSean Christopherson * For the same reason, we want to tell GICv4 that we need 9396109c5a6SSean Christopherson * doorbells to be signalled, should an interrupt become pending. 9406109c5a6SSean Christopherson */ 9416109c5a6SSean Christopherson preempt_disable(); 942b321c31cSMarc Zyngier vcpu_set_flag(vcpu, IN_WFI); 943948e1a53SMarc Zyngier kvm_vgic_put(vcpu); 9446109c5a6SSean Christopherson preempt_enable(); 9456109c5a6SSean Christopherson 94691b99ea7SSean Christopherson kvm_vcpu_halt(vcpu); 947eebc538dSMarc Zyngier vcpu_clear_flag(vcpu, IN_WFIT); 9486109c5a6SSean Christopherson 9496109c5a6SSean Christopherson preempt_disable(); 950b321c31cSMarc Zyngier vcpu_clear_flag(vcpu, IN_WFI); 951948e1a53SMarc Zyngier kvm_vgic_load(vcpu); 9526109c5a6SSean Christopherson preempt_enable(); 9536109c5a6SSean Christopherson } 9546109c5a6SSean Christopherson 9557b33a09dSOliver Upton static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu) 9567b33a09dSOliver Upton { 9577b33a09dSOliver Upton if (!kvm_arm_vcpu_suspended(vcpu)) 9587b33a09dSOliver Upton return 1; 9597b33a09dSOliver Upton 9607b33a09dSOliver Upton kvm_vcpu_wfi(vcpu); 9617b33a09dSOliver Upton 9627b33a09dSOliver Upton /* 9637b33a09dSOliver Upton * The suspend state is sticky; we do not leave it until userspace 9647b33a09dSOliver Upton * explicitly marks the vCPU as runnable. Request that we suspend again 9657b33a09dSOliver Upton * later. 9667b33a09dSOliver Upton */ 9677b33a09dSOliver Upton kvm_make_request(KVM_REQ_SUSPEND, vcpu); 9687b33a09dSOliver Upton 9697b33a09dSOliver Upton /* 9707b33a09dSOliver Upton * Check to make sure the vCPU is actually runnable. If so, exit to 9717b33a09dSOliver Upton * userspace informing it of the wakeup condition. 9727b33a09dSOliver Upton */ 9737b33a09dSOliver Upton if (kvm_arch_vcpu_runnable(vcpu)) { 9747b33a09dSOliver Upton memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); 9757b33a09dSOliver Upton vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP; 9767b33a09dSOliver Upton vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; 9777b33a09dSOliver Upton return 0; 9787b33a09dSOliver Upton } 9797b33a09dSOliver Upton 9807b33a09dSOliver Upton /* 9817b33a09dSOliver Upton * Otherwise, we were unblocked to process a different event, such as a 9827b33a09dSOliver Upton * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to 9837b33a09dSOliver Upton * process the event. 9847b33a09dSOliver Upton */ 9857b33a09dSOliver Upton return 1; 9867b33a09dSOliver Upton } 9877b33a09dSOliver Upton 9883fdd0459SOliver Upton /** 9893fdd0459SOliver Upton * check_vcpu_requests - check and handle pending vCPU requests 9903fdd0459SOliver Upton * @vcpu: the VCPU pointer 9913fdd0459SOliver Upton * 9923fdd0459SOliver Upton * Return: 1 if we should enter the guest 9933fdd0459SOliver Upton * 0 if we should exit to userspace 9943fdd0459SOliver Upton * < 0 if we should exit to userspace, where the return value indicates 9953fdd0459SOliver Upton * an error 9963fdd0459SOliver Upton */ 9973fdd0459SOliver Upton static int check_vcpu_requests(struct kvm_vcpu *vcpu) 9989ed24f4bSMarc Zyngier { 9999ed24f4bSMarc Zyngier if (kvm_request_pending(vcpu)) { 10009ed24f4bSMarc Zyngier if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) 10011c6219e3SOliver Upton kvm_vcpu_sleep(vcpu); 10029ed24f4bSMarc Zyngier 10039ed24f4bSMarc Zyngier if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 10049ed24f4bSMarc Zyngier kvm_reset_vcpu(vcpu); 10059ed24f4bSMarc Zyngier 10069ed24f4bSMarc Zyngier /* 10079ed24f4bSMarc Zyngier * Clear IRQ_PENDING requests that were made to guarantee 10089ed24f4bSMarc Zyngier * that a VCPU sees new virtual interrupts. 10099ed24f4bSMarc Zyngier */ 10109ed24f4bSMarc Zyngier kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); 10119ed24f4bSMarc Zyngier 10129ed24f4bSMarc Zyngier if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) 10139ed24f4bSMarc Zyngier kvm_update_stolen_time(vcpu); 10149ed24f4bSMarc Zyngier 10159ed24f4bSMarc Zyngier if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { 10169ed24f4bSMarc Zyngier /* The distributor enable bits were changed */ 10179ed24f4bSMarc Zyngier preempt_disable(); 1018b321c31cSMarc Zyngier vgic_v4_put(vcpu); 10199ed24f4bSMarc Zyngier vgic_v4_load(vcpu); 10209ed24f4bSMarc Zyngier preempt_enable(); 10219ed24f4bSMarc Zyngier } 1022d0c94c49SMarc Zyngier 1023d0c94c49SMarc Zyngier if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu)) 102427131b19SRaghavendra Rao Ananta kvm_vcpu_reload_pmu(vcpu); 10257b33a09dSOliver Upton 1026b1f778a2SMarc Zyngier if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu)) 1027b1f778a2SMarc Zyngier kvm_vcpu_pmu_restore_guest(vcpu); 1028b1f778a2SMarc Zyngier 10297b33a09dSOliver Upton if (kvm_check_request(KVM_REQ_SUSPEND, vcpu)) 10307b33a09dSOliver Upton return kvm_vcpu_suspend(vcpu); 10319cb1096fSGavin Shan 10329cb1096fSGavin Shan if (kvm_dirty_ring_check_request(vcpu)) 10339cb1096fSGavin Shan return 0; 10349ed24f4bSMarc Zyngier } 10353fdd0459SOliver Upton 10363fdd0459SOliver Upton return 1; 10379ed24f4bSMarc Zyngier } 10389ed24f4bSMarc Zyngier 10392f6a49bbSWill Deacon static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu) 10402f6a49bbSWill Deacon { 10412f6a49bbSWill Deacon if (likely(!vcpu_mode_is_32bit(vcpu))) 10422f6a49bbSWill Deacon return false; 10432f6a49bbSWill Deacon 1044a77b31dcSMarc Zyngier if (vcpu_has_nv(vcpu)) 1045a77b31dcSMarc Zyngier return true; 1046a77b31dcSMarc Zyngier 1047f3c6efc7SOliver Upton return !kvm_supports_32bit_el0(); 10482f6a49bbSWill Deacon } 10492f6a49bbSWill Deacon 10509ed24f4bSMarc Zyngier /** 10516caa5812SOliver Upton * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest 10526caa5812SOliver Upton * @vcpu: The VCPU pointer 10536caa5812SOliver Upton * @ret: Pointer to write optional return code 10546caa5812SOliver Upton * 10556caa5812SOliver Upton * Returns: true if the VCPU needs to return to a preemptible + interruptible 10566caa5812SOliver Upton * and skip guest entry. 10576caa5812SOliver Upton * 10586caa5812SOliver Upton * This function disambiguates between two different types of exits: exits to a 10596caa5812SOliver Upton * preemptible + interruptible kernel context and exits to userspace. For an 10606caa5812SOliver Upton * exit to userspace, this function will write the return code to ret and return 10616caa5812SOliver Upton * true. For an exit to preemptible + interruptible kernel context (i.e. check 10626caa5812SOliver Upton * for pending work and re-enter), return true without writing to ret. 10636caa5812SOliver Upton */ 10646caa5812SOliver Upton static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret) 10656caa5812SOliver Upton { 10666caa5812SOliver Upton struct kvm_run *run = vcpu->run; 10676caa5812SOliver Upton 10686caa5812SOliver Upton /* 10696caa5812SOliver Upton * If we're using a userspace irqchip, then check if we need 10706caa5812SOliver Upton * to tell a userspace irqchip about timer or PMU level 10716caa5812SOliver Upton * changes and if so, exit to userspace (the actual level 10726caa5812SOliver Upton * state gets updated in kvm_timer_update_run and 10736caa5812SOliver Upton * kvm_pmu_update_run below). 10746caa5812SOliver Upton */ 10756caa5812SOliver Upton if (static_branch_unlikely(&userspace_irqchip_in_use)) { 10766caa5812SOliver Upton if (kvm_timer_should_notify_user(vcpu) || 10776caa5812SOliver Upton kvm_pmu_should_notify_user(vcpu)) { 10786caa5812SOliver Upton *ret = -EINTR; 10796caa5812SOliver Upton run->exit_reason = KVM_EXIT_INTR; 10806caa5812SOliver Upton return true; 10816caa5812SOliver Upton } 10826caa5812SOliver Upton } 10836caa5812SOliver Upton 1084583cda1bSAlexandru Elisei if (unlikely(vcpu_on_unsupported_cpu(vcpu))) { 1085583cda1bSAlexandru Elisei run->exit_reason = KVM_EXIT_FAIL_ENTRY; 1086583cda1bSAlexandru Elisei run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED; 1087583cda1bSAlexandru Elisei run->fail_entry.cpu = smp_processor_id(); 1088583cda1bSAlexandru Elisei *ret = 0; 1089583cda1bSAlexandru Elisei return true; 1090583cda1bSAlexandru Elisei } 1091583cda1bSAlexandru Elisei 10926caa5812SOliver Upton return kvm_request_pending(vcpu) || 10936caa5812SOliver Upton xfer_to_guest_mode_work_pending(); 10946caa5812SOliver Upton } 10956caa5812SOliver Upton 10968cfe148aSMark Rutland /* 10978cfe148aSMark Rutland * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while 10988cfe148aSMark Rutland * the vCPU is running. 10998cfe148aSMark Rutland * 11008cfe148aSMark Rutland * This must be noinstr as instrumentation may make use of RCU, and this is not 11018cfe148aSMark Rutland * safe during the EQS. 11028cfe148aSMark Rutland */ 11038cfe148aSMark Rutland static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu) 11048cfe148aSMark Rutland { 11058cfe148aSMark Rutland int ret; 11068cfe148aSMark Rutland 11078cfe148aSMark Rutland guest_state_enter_irqoff(); 11088cfe148aSMark Rutland ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu); 11098cfe148aSMark Rutland guest_state_exit_irqoff(); 11108cfe148aSMark Rutland 11118cfe148aSMark Rutland return ret; 11128cfe148aSMark Rutland } 11138cfe148aSMark Rutland 11146caa5812SOliver Upton /** 11159ed24f4bSMarc Zyngier * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code 11169ed24f4bSMarc Zyngier * @vcpu: The VCPU pointer 11179ed24f4bSMarc Zyngier * 11189ed24f4bSMarc Zyngier * This function is called through the VCPU_RUN ioctl called from user space. It 11199ed24f4bSMarc Zyngier * will execute VM code in a loop until the time slice for the process is used 11209ed24f4bSMarc Zyngier * or some emulation is needed from user space in which case the function will 11219ed24f4bSMarc Zyngier * return with return value 0 and with the kvm_run structure filled in with the 11229ed24f4bSMarc Zyngier * required data for the requested emulation. 11239ed24f4bSMarc Zyngier */ 112438060944SPaolo Bonzini int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 11259ed24f4bSMarc Zyngier { 112638060944SPaolo Bonzini struct kvm_run *run = vcpu->run; 11279ed24f4bSMarc Zyngier int ret; 11289ed24f4bSMarc Zyngier 11299ed24f4bSMarc Zyngier if (run->exit_reason == KVM_EXIT_MMIO) { 113074cc7e0cSTianjia Zhang ret = kvm_handle_mmio_return(vcpu); 1131cc81b6dfSFuad Tabba if (ret <= 0) 11329ed24f4bSMarc Zyngier return ret; 11339ed24f4bSMarc Zyngier } 11349ed24f4bSMarc Zyngier 11359ed24f4bSMarc Zyngier vcpu_load(vcpu); 11369ed24f4bSMarc Zyngier 1137a6816314SDavid Matlack if (!vcpu->wants_to_run) { 1138e3e880bbSZenghui Yu ret = -EINTR; 1139e3e880bbSZenghui Yu goto out; 1140e3e880bbSZenghui Yu } 1141e3e880bbSZenghui Yu 11429ed24f4bSMarc Zyngier kvm_sigset_activate(vcpu); 11439ed24f4bSMarc Zyngier 11449ed24f4bSMarc Zyngier ret = 1; 11459ed24f4bSMarc Zyngier run->exit_reason = KVM_EXIT_UNKNOWN; 114618f3976fSAlexandru Elisei run->flags = 0; 11479ed24f4bSMarc Zyngier while (ret > 0) { 11489ed24f4bSMarc Zyngier /* 11499ed24f4bSMarc Zyngier * Check conditions before entering the guest 11509ed24f4bSMarc Zyngier */ 11516caa5812SOliver Upton ret = xfer_to_guest_mode_handle_work(vcpu); 11526caa5812SOliver Upton if (!ret) 11536caa5812SOliver Upton ret = 1; 11549ed24f4bSMarc Zyngier 11553fdd0459SOliver Upton if (ret > 0) 11563fdd0459SOliver Upton ret = check_vcpu_requests(vcpu); 11579ed24f4bSMarc Zyngier 11589ed24f4bSMarc Zyngier /* 11599ed24f4bSMarc Zyngier * Preparing the interrupts to be injected also 11609ed24f4bSMarc Zyngier * involves poking the GIC, which must be done in a 11619ed24f4bSMarc Zyngier * non-preemptible context. 11629ed24f4bSMarc Zyngier */ 11639ed24f4bSMarc Zyngier preempt_disable(); 11649ed24f4bSMarc Zyngier 11653248136bSJulien Grall /* 11663248136bSJulien Grall * The VMID allocator only tracks active VMIDs per 11673248136bSJulien Grall * physical CPU, and therefore the VMID allocated may not be 11683248136bSJulien Grall * preserved on VMID roll-over if the task was preempted, 11693248136bSJulien Grall * making a thread's VMID inactive. So we need to call 11703248136bSJulien Grall * kvm_arm_vmid_update() in non-premptible context. 11713248136bSJulien Grall */ 11725eba523eSMarc Zyngier if (kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid) && 11735eba523eSMarc Zyngier has_vhe()) 11745eba523eSMarc Zyngier __load_stage2(vcpu->arch.hw_mmu, 11755eba523eSMarc Zyngier vcpu->arch.hw_mmu->arch); 11763248136bSJulien Grall 11779ed24f4bSMarc Zyngier kvm_pmu_flush_hwstate(vcpu); 11789ed24f4bSMarc Zyngier 11799ed24f4bSMarc Zyngier local_irq_disable(); 11809ed24f4bSMarc Zyngier 11819ed24f4bSMarc Zyngier kvm_vgic_flush_hwstate(vcpu); 11829ed24f4bSMarc Zyngier 118384d751a0SFuad Tabba kvm_pmu_update_vcpu_events(vcpu); 118484d751a0SFuad Tabba 11859ed24f4bSMarc Zyngier /* 11869ed24f4bSMarc Zyngier * Ensure we set mode to IN_GUEST_MODE after we disable 11879ed24f4bSMarc Zyngier * interrupts and before the final VCPU requests check. 11889ed24f4bSMarc Zyngier * See the comment in kvm_vcpu_exiting_guest_mode() and 11899ed24f4bSMarc Zyngier * Documentation/virt/kvm/vcpu-requests.rst 11909ed24f4bSMarc Zyngier */ 11919ed24f4bSMarc Zyngier smp_store_mb(vcpu->mode, IN_GUEST_MODE); 11929ed24f4bSMarc Zyngier 11936caa5812SOliver Upton if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) { 11949ed24f4bSMarc Zyngier vcpu->mode = OUTSIDE_GUEST_MODE; 11959ed24f4bSMarc Zyngier isb(); /* Ensure work in x_flush_hwstate is committed */ 11969ed24f4bSMarc Zyngier kvm_pmu_sync_hwstate(vcpu); 11979ed24f4bSMarc Zyngier if (static_branch_unlikely(&userspace_irqchip_in_use)) 11983c5ff0c6SMarc Zyngier kvm_timer_sync_user(vcpu); 11999ed24f4bSMarc Zyngier kvm_vgic_sync_hwstate(vcpu); 12009ed24f4bSMarc Zyngier local_irq_enable(); 12019ed24f4bSMarc Zyngier preempt_enable(); 12029ed24f4bSMarc Zyngier continue; 12039ed24f4bSMarc Zyngier } 12049ed24f4bSMarc Zyngier 12059ed24f4bSMarc Zyngier kvm_arm_setup_debug(vcpu); 1206af9a0e21SMarc Zyngier kvm_arch_vcpu_ctxflush_fp(vcpu); 12079ed24f4bSMarc Zyngier 12089ed24f4bSMarc Zyngier /************************************************************** 12099ed24f4bSMarc Zyngier * Enter the guest 12109ed24f4bSMarc Zyngier */ 12119ed24f4bSMarc Zyngier trace_kvm_entry(*vcpu_pc(vcpu)); 12128cfe148aSMark Rutland guest_timing_enter_irqoff(); 12139ed24f4bSMarc Zyngier 12148cfe148aSMark Rutland ret = kvm_arm_vcpu_enter_exit(vcpu); 12159ed24f4bSMarc Zyngier 12169ed24f4bSMarc Zyngier vcpu->mode = OUTSIDE_GUEST_MODE; 12179ed24f4bSMarc Zyngier vcpu->stat.exits++; 12189ed24f4bSMarc Zyngier /* 12199ed24f4bSMarc Zyngier * Back from guest 12209ed24f4bSMarc Zyngier *************************************************************/ 12219ed24f4bSMarc Zyngier 12229ed24f4bSMarc Zyngier kvm_arm_clear_debug(vcpu); 12239ed24f4bSMarc Zyngier 12249ed24f4bSMarc Zyngier /* 12259ed24f4bSMarc Zyngier * We must sync the PMU state before the vgic state so 12269ed24f4bSMarc Zyngier * that the vgic can properly sample the updated state of the 12279ed24f4bSMarc Zyngier * interrupt line. 12289ed24f4bSMarc Zyngier */ 12299ed24f4bSMarc Zyngier kvm_pmu_sync_hwstate(vcpu); 12309ed24f4bSMarc Zyngier 12319ed24f4bSMarc Zyngier /* 12329ed24f4bSMarc Zyngier * Sync the vgic state before syncing the timer state because 12339ed24f4bSMarc Zyngier * the timer code needs to know if the virtual timer 12349ed24f4bSMarc Zyngier * interrupts are active. 12359ed24f4bSMarc Zyngier */ 12369ed24f4bSMarc Zyngier kvm_vgic_sync_hwstate(vcpu); 12379ed24f4bSMarc Zyngier 12389ed24f4bSMarc Zyngier /* 12399ed24f4bSMarc Zyngier * Sync the timer hardware state before enabling interrupts as 12409ed24f4bSMarc Zyngier * we don't want vtimer interrupts to race with syncing the 12419ed24f4bSMarc Zyngier * timer virtual interrupt state. 12429ed24f4bSMarc Zyngier */ 12439ed24f4bSMarc Zyngier if (static_branch_unlikely(&userspace_irqchip_in_use)) 12443c5ff0c6SMarc Zyngier kvm_timer_sync_user(vcpu); 12459ed24f4bSMarc Zyngier 12469ed24f4bSMarc Zyngier kvm_arch_vcpu_ctxsync_fp(vcpu); 12479ed24f4bSMarc Zyngier 12489ed24f4bSMarc Zyngier /* 12498cfe148aSMark Rutland * We must ensure that any pending interrupts are taken before 12508cfe148aSMark Rutland * we exit guest timing so that timer ticks are accounted as 12518cfe148aSMark Rutland * guest time. Transiently unmask interrupts so that any 12528cfe148aSMark Rutland * pending interrupts are taken. 12539ed24f4bSMarc Zyngier * 12548cfe148aSMark Rutland * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other 12558cfe148aSMark Rutland * context synchronization event) is necessary to ensure that 12568cfe148aSMark Rutland * pending interrupts are taken. 12579ed24f4bSMarc Zyngier */ 1258f7659f8bSMarc Zyngier if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) { 12599ed24f4bSMarc Zyngier local_irq_enable(); 12608cfe148aSMark Rutland isb(); 12618cfe148aSMark Rutland local_irq_disable(); 1262f7659f8bSMarc Zyngier } 12639ed24f4bSMarc Zyngier 12648cfe148aSMark Rutland guest_timing_exit_irqoff(); 12658cfe148aSMark Rutland 12668cfe148aSMark Rutland local_irq_enable(); 12678cfe148aSMark Rutland 12689ed24f4bSMarc Zyngier trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); 12699ed24f4bSMarc Zyngier 12709ed24f4bSMarc Zyngier /* Exit types that need handling before we can be preempted */ 127174cc7e0cSTianjia Zhang handle_exit_early(vcpu, ret); 12729ed24f4bSMarc Zyngier 12739ed24f4bSMarc Zyngier preempt_enable(); 12749ed24f4bSMarc Zyngier 127522f55384SQais Yousef /* 127622f55384SQais Yousef * The ARMv8 architecture doesn't give the hypervisor 127722f55384SQais Yousef * a mechanism to prevent a guest from dropping to AArch32 EL0 127822f55384SQais Yousef * if implemented by the CPU. If we spot the guest in such 127922f55384SQais Yousef * state and that we decided it wasn't supposed to do so (like 128022f55384SQais Yousef * with the asymmetric AArch32 case), return to userspace with 128122f55384SQais Yousef * a fatal error. 128222f55384SQais Yousef */ 12832f6a49bbSWill Deacon if (vcpu_mode_is_bad_32bit(vcpu)) { 128422f55384SQais Yousef /* 128522f55384SQais Yousef * As we have caught the guest red-handed, decide that 128622f55384SQais Yousef * it isn't fit for purpose anymore by making the vcpu 128722f55384SQais Yousef * invalid. The VMM can try and fix it by issuing a 128822f55384SQais Yousef * KVM_ARM_VCPU_INIT if it really wants to. 128922f55384SQais Yousef */ 1290ef984060SOliver Upton vcpu_clear_flag(vcpu, VCPU_INITIALIZED); 129122f55384SQais Yousef ret = ARM_EXCEPTION_IL; 129222f55384SQais Yousef } 129322f55384SQais Yousef 129474cc7e0cSTianjia Zhang ret = handle_exit(vcpu, ret); 12959ed24f4bSMarc Zyngier } 12969ed24f4bSMarc Zyngier 12979ed24f4bSMarc Zyngier /* Tell userspace about in-kernel device output levels */ 12989ed24f4bSMarc Zyngier if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { 12999ed24f4bSMarc Zyngier kvm_timer_update_run(vcpu); 13009ed24f4bSMarc Zyngier kvm_pmu_update_run(vcpu); 13019ed24f4bSMarc Zyngier } 13029ed24f4bSMarc Zyngier 13039ed24f4bSMarc Zyngier kvm_sigset_deactivate(vcpu); 13049ed24f4bSMarc Zyngier 1305e3e880bbSZenghui Yu out: 130626778aaaSMarc Zyngier /* 130726778aaaSMarc Zyngier * In the unlikely event that we are returning to userspace 130826778aaaSMarc Zyngier * with pending exceptions or PC adjustment, commit these 130926778aaaSMarc Zyngier * adjustments in order to give userspace a consistent view of 131026778aaaSMarc Zyngier * the vcpu state. Note that this relies on __kvm_adjust_pc() 131126778aaaSMarc Zyngier * being preempt-safe on VHE. 131226778aaaSMarc Zyngier */ 1313699bb2e0SMarc Zyngier if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) || 1314699bb2e0SMarc Zyngier vcpu_get_flag(vcpu, INCREMENT_PC))) 131526778aaaSMarc Zyngier kvm_call_hyp(__kvm_adjust_pc, vcpu); 131626778aaaSMarc Zyngier 13179ed24f4bSMarc Zyngier vcpu_put(vcpu); 13189ed24f4bSMarc Zyngier return ret; 13199ed24f4bSMarc Zyngier } 13209ed24f4bSMarc Zyngier 13219ed24f4bSMarc Zyngier static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) 13229ed24f4bSMarc Zyngier { 13239ed24f4bSMarc Zyngier int bit_index; 13249ed24f4bSMarc Zyngier bool set; 13259ed24f4bSMarc Zyngier unsigned long *hcr; 13269ed24f4bSMarc Zyngier 13279ed24f4bSMarc Zyngier if (number == KVM_ARM_IRQ_CPU_IRQ) 13289ed24f4bSMarc Zyngier bit_index = __ffs(HCR_VI); 13299ed24f4bSMarc Zyngier else /* KVM_ARM_IRQ_CPU_FIQ */ 13309ed24f4bSMarc Zyngier bit_index = __ffs(HCR_VF); 13319ed24f4bSMarc Zyngier 13329ed24f4bSMarc Zyngier hcr = vcpu_hcr(vcpu); 13339ed24f4bSMarc Zyngier if (level) 13349ed24f4bSMarc Zyngier set = test_and_set_bit(bit_index, hcr); 13359ed24f4bSMarc Zyngier else 13369ed24f4bSMarc Zyngier set = test_and_clear_bit(bit_index, hcr); 13379ed24f4bSMarc Zyngier 13389ed24f4bSMarc Zyngier /* 13399ed24f4bSMarc Zyngier * If we didn't change anything, no need to wake up or kick other CPUs 13409ed24f4bSMarc Zyngier */ 13419ed24f4bSMarc Zyngier if (set == level) 13429ed24f4bSMarc Zyngier return 0; 13439ed24f4bSMarc Zyngier 13449ed24f4bSMarc Zyngier /* 13459ed24f4bSMarc Zyngier * The vcpu irq_lines field was updated, wake up sleeping VCPUs and 13469ed24f4bSMarc Zyngier * trigger a world-switch round on the running physical CPU to set the 13479ed24f4bSMarc Zyngier * virtual IRQ/FIQ fields in the HCR appropriately. 13489ed24f4bSMarc Zyngier */ 13499ed24f4bSMarc Zyngier kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); 13509ed24f4bSMarc Zyngier kvm_vcpu_kick(vcpu); 13519ed24f4bSMarc Zyngier 13529ed24f4bSMarc Zyngier return 0; 13539ed24f4bSMarc Zyngier } 13549ed24f4bSMarc Zyngier 13559ed24f4bSMarc Zyngier int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, 13569ed24f4bSMarc Zyngier bool line_status) 13579ed24f4bSMarc Zyngier { 13589ed24f4bSMarc Zyngier u32 irq = irq_level->irq; 13599a0a75d3SMarc Zyngier unsigned int irq_type, vcpu_id, irq_num; 13609ed24f4bSMarc Zyngier struct kvm_vcpu *vcpu = NULL; 13619ed24f4bSMarc Zyngier bool level = irq_level->level; 13629ed24f4bSMarc Zyngier 13639ed24f4bSMarc Zyngier irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; 13649a0a75d3SMarc Zyngier vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; 13659a0a75d3SMarc Zyngier vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); 13669ed24f4bSMarc Zyngier irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; 13679ed24f4bSMarc Zyngier 13689a0a75d3SMarc Zyngier trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level); 13699ed24f4bSMarc Zyngier 13709ed24f4bSMarc Zyngier switch (irq_type) { 13719ed24f4bSMarc Zyngier case KVM_ARM_IRQ_TYPE_CPU: 13729ed24f4bSMarc Zyngier if (irqchip_in_kernel(kvm)) 13739ed24f4bSMarc Zyngier return -ENXIO; 13749ed24f4bSMarc Zyngier 13759a0a75d3SMarc Zyngier vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); 13769ed24f4bSMarc Zyngier if (!vcpu) 13779ed24f4bSMarc Zyngier return -EINVAL; 13789ed24f4bSMarc Zyngier 13799ed24f4bSMarc Zyngier if (irq_num > KVM_ARM_IRQ_CPU_FIQ) 13809ed24f4bSMarc Zyngier return -EINVAL; 13819ed24f4bSMarc Zyngier 13829ed24f4bSMarc Zyngier return vcpu_interrupt_line(vcpu, irq_num, level); 13839ed24f4bSMarc Zyngier case KVM_ARM_IRQ_TYPE_PPI: 13849ed24f4bSMarc Zyngier if (!irqchip_in_kernel(kvm)) 13859ed24f4bSMarc Zyngier return -ENXIO; 13869ed24f4bSMarc Zyngier 13879a0a75d3SMarc Zyngier vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); 13889ed24f4bSMarc Zyngier if (!vcpu) 13899ed24f4bSMarc Zyngier return -EINVAL; 13909ed24f4bSMarc Zyngier 13919ed24f4bSMarc Zyngier if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) 13929ed24f4bSMarc Zyngier return -EINVAL; 13939ed24f4bSMarc Zyngier 13949a0a75d3SMarc Zyngier return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL); 13959ed24f4bSMarc Zyngier case KVM_ARM_IRQ_TYPE_SPI: 13969ed24f4bSMarc Zyngier if (!irqchip_in_kernel(kvm)) 13979ed24f4bSMarc Zyngier return -ENXIO; 13989ed24f4bSMarc Zyngier 13999ed24f4bSMarc Zyngier if (irq_num < VGIC_NR_PRIVATE_IRQS) 14009ed24f4bSMarc Zyngier return -EINVAL; 14019ed24f4bSMarc Zyngier 14029a0a75d3SMarc Zyngier return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL); 14039ed24f4bSMarc Zyngier } 14049ed24f4bSMarc Zyngier 14059ed24f4bSMarc Zyngier return -EINVAL; 14069ed24f4bSMarc Zyngier } 14079ed24f4bSMarc Zyngier 1408ef150908SOliver Upton static unsigned long system_supported_vcpu_features(void) 1409ef150908SOliver Upton { 1410ef150908SOliver Upton unsigned long features = KVM_VCPU_VALID_FEATURES; 1411ef150908SOliver Upton 1412ef150908SOliver Upton if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1)) 1413ef150908SOliver Upton clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features); 1414ef150908SOliver Upton 14159116db11SOliver Upton if (!kvm_arm_support_pmu_v3()) 14169116db11SOliver Upton clear_bit(KVM_ARM_VCPU_PMU_V3, &features); 14179116db11SOliver Upton 1418be9c0c01SOliver Upton if (!system_supports_sve()) 1419be9c0c01SOliver Upton clear_bit(KVM_ARM_VCPU_SVE, &features); 1420be9c0c01SOliver Upton 1421a07e9345SMarc Zyngier if (!kvm_has_full_ptr_auth()) { 1422baa28a53SOliver Upton clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features); 1423baa28a53SOliver Upton clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features); 1424baa28a53SOliver Upton } 1425baa28a53SOliver Upton 142612405b09SOliver Upton if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) 142712405b09SOliver Upton clear_bit(KVM_ARM_VCPU_HAS_EL2, &features); 142812405b09SOliver Upton 1429ef150908SOliver Upton return features; 1430ef150908SOliver Upton } 1431ef150908SOliver Upton 1432a7a2c72aSOliver Upton static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu, 14339ed24f4bSMarc Zyngier const struct kvm_vcpu_init *init) 14349ed24f4bSMarc Zyngier { 1435a7a2c72aSOliver Upton unsigned long features = init->features[0]; 1436a7a2c72aSOliver Upton int i; 14379ed24f4bSMarc Zyngier 1438a7a2c72aSOliver Upton if (features & ~KVM_VCPU_VALID_FEATURES) 14399ed24f4bSMarc Zyngier return -ENOENT; 14409ed24f4bSMarc Zyngier 1441a7a2c72aSOliver Upton for (i = 1; i < ARRAY_SIZE(init->features); i++) { 1442a7a2c72aSOliver Upton if (init->features[i]) 1443a7a2c72aSOliver Upton return -ENOENT; 14449ed24f4bSMarc Zyngier } 14459ed24f4bSMarc Zyngier 1446ef150908SOliver Upton if (features & ~system_supported_vcpu_features()) 1447ef150908SOliver Upton return -EINVAL; 1448ef150908SOliver Upton 1449baa28a53SOliver Upton /* 1450baa28a53SOliver Upton * For now make sure that both address/generic pointer authentication 1451baa28a53SOliver Upton * features are requested by the userspace together. 1452baa28a53SOliver Upton */ 1453baa28a53SOliver Upton if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) != 1454baa28a53SOliver Upton test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features)) 1455baa28a53SOliver Upton return -EINVAL; 1456baa28a53SOliver Upton 14572251e9ffSOliver Upton if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features)) 14582251e9ffSOliver Upton return 0; 14592251e9ffSOliver Upton 14602251e9ffSOliver Upton /* MTE is incompatible with AArch32 */ 14612251e9ffSOliver Upton if (kvm_has_mte(vcpu->kvm)) 14622251e9ffSOliver Upton return -EINVAL; 14632251e9ffSOliver Upton 14642251e9ffSOliver Upton /* NV is incompatible with AArch32 */ 14652251e9ffSOliver Upton if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features)) 14662251e9ffSOliver Upton return -EINVAL; 14672251e9ffSOliver Upton 1468a7a2c72aSOliver Upton return 0; 1469a7a2c72aSOliver Upton } 1470a7a2c72aSOliver Upton 1471a7a2c72aSOliver Upton static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu, 1472a7a2c72aSOliver Upton const struct kvm_vcpu_init *init) 1473a7a2c72aSOliver Upton { 1474a7a2c72aSOliver Upton unsigned long features = init->features[0]; 1475a7a2c72aSOliver Upton 14761de10b7dSOliver Upton return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features, 14771de10b7dSOliver Upton KVM_VCPU_MAX_FEATURES); 1478a7a2c72aSOliver Upton } 1479a7a2c72aSOliver Upton 148042773357SReiji Watanabe static int kvm_setup_vcpu(struct kvm_vcpu *vcpu) 148142773357SReiji Watanabe { 148242773357SReiji Watanabe struct kvm *kvm = vcpu->kvm; 148342773357SReiji Watanabe int ret = 0; 148442773357SReiji Watanabe 148542773357SReiji Watanabe /* 148642773357SReiji Watanabe * When the vCPU has a PMU, but no PMU is set for the guest 148742773357SReiji Watanabe * yet, set the default one. 148842773357SReiji Watanabe */ 148942773357SReiji Watanabe if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu) 149042773357SReiji Watanabe ret = kvm_arm_set_default_pmu(kvm); 149142773357SReiji Watanabe 14924f128f8eSMarc Zyngier /* Prepare for nested if required */ 14934f128f8eSMarc Zyngier if (!ret && vcpu_has_nv(vcpu)) 14944f128f8eSMarc Zyngier ret = kvm_vcpu_init_nested(vcpu); 14954f128f8eSMarc Zyngier 149642773357SReiji Watanabe return ret; 1497a7a2c72aSOliver Upton } 1498a7a2c72aSOliver Upton 1499a7a2c72aSOliver Upton static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1500a7a2c72aSOliver Upton const struct kvm_vcpu_init *init) 1501a7a2c72aSOliver Upton { 1502a7a2c72aSOliver Upton unsigned long features = init->features[0]; 15032251e9ffSOliver Upton struct kvm *kvm = vcpu->kvm; 15042251e9ffSOliver Upton int ret = -EINVAL; 15052251e9ffSOliver Upton 15062251e9ffSOliver Upton mutex_lock(&kvm->arch.config_lock); 15072251e9ffSOliver Upton 15082251e9ffSOliver Upton if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) && 15091de10b7dSOliver Upton kvm_vcpu_init_changed(vcpu, init)) 15102251e9ffSOliver Upton goto out_unlock; 1511a7a2c72aSOliver Upton 15122251e9ffSOliver Upton bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES); 15139ed24f4bSMarc Zyngier 151442773357SReiji Watanabe ret = kvm_setup_vcpu(vcpu); 151542773357SReiji Watanabe if (ret) 151642773357SReiji Watanabe goto out_unlock; 151742773357SReiji Watanabe 15189ed24f4bSMarc Zyngier /* Now we know what it is, we can reset it. */ 15193d4b2a4cSOliver Upton kvm_reset_vcpu(vcpu); 15209ed24f4bSMarc Zyngier 15212251e9ffSOliver Upton set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags); 1522ef984060SOliver Upton vcpu_set_flag(vcpu, VCPU_INITIALIZED); 15233d4b2a4cSOliver Upton ret = 0; 15242251e9ffSOliver Upton out_unlock: 15252251e9ffSOliver Upton mutex_unlock(&kvm->arch.config_lock); 15269ed24f4bSMarc Zyngier return ret; 15279ed24f4bSMarc Zyngier } 15289ed24f4bSMarc Zyngier 1529a7a2c72aSOliver Upton static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1530a7a2c72aSOliver Upton const struct kvm_vcpu_init *init) 1531a7a2c72aSOliver Upton { 1532a7a2c72aSOliver Upton int ret; 1533a7a2c72aSOliver Upton 15345346f7e1SOliver Upton if (init->target != KVM_ARM_TARGET_GENERIC_V8 && 15355346f7e1SOliver Upton init->target != kvm_target_cpu()) 1536a7a2c72aSOliver Upton return -EINVAL; 1537a7a2c72aSOliver Upton 1538a7a2c72aSOliver Upton ret = kvm_vcpu_init_check_features(vcpu, init); 1539a7a2c72aSOliver Upton if (ret) 1540a7a2c72aSOliver Upton return ret; 1541a7a2c72aSOliver Upton 1542ef984060SOliver Upton if (!kvm_vcpu_initialized(vcpu)) 1543a7a2c72aSOliver Upton return __kvm_vcpu_set_target(vcpu, init); 1544a7a2c72aSOliver Upton 1545a7a2c72aSOliver Upton if (kvm_vcpu_init_changed(vcpu, init)) 1546a7a2c72aSOliver Upton return -EINVAL; 1547a7a2c72aSOliver Upton 15483d4b2a4cSOliver Upton kvm_reset_vcpu(vcpu); 15493d4b2a4cSOliver Upton return 0; 1550a7a2c72aSOliver Upton } 1551a7a2c72aSOliver Upton 15529ed24f4bSMarc Zyngier static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, 15539ed24f4bSMarc Zyngier struct kvm_vcpu_init *init) 15549ed24f4bSMarc Zyngier { 1555e3c1c0caSOliver Upton bool power_off = false; 15569ed24f4bSMarc Zyngier int ret; 15579ed24f4bSMarc Zyngier 1558e3c1c0caSOliver Upton /* 1559e3c1c0caSOliver Upton * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid 1560e3c1c0caSOliver Upton * reflecting it in the finalized feature set, thus limiting its scope 1561e3c1c0caSOliver Upton * to a single KVM_ARM_VCPU_INIT call. 1562e3c1c0caSOliver Upton */ 1563192df2aaSOliver Upton if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) { 1564192df2aaSOliver Upton init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF); 1565e3c1c0caSOliver Upton power_off = true; 1566e3c1c0caSOliver Upton } 1567e3c1c0caSOliver Upton 15689ed24f4bSMarc Zyngier ret = kvm_vcpu_set_target(vcpu, init); 15699ed24f4bSMarc Zyngier if (ret) 15709ed24f4bSMarc Zyngier return ret; 15719ed24f4bSMarc Zyngier 15729ed24f4bSMarc Zyngier /* 15739ed24f4bSMarc Zyngier * Ensure a rebooted VM will fault in RAM pages and detect if the 15749ed24f4bSMarc Zyngier * guest MMU is turned off and flush the caches as needed. 1575892713e9SZenghui Yu * 15767ae2f3dbSMarc Zyngier * S2FWB enforces all memory accesses to RAM being cacheable, 15777ae2f3dbSMarc Zyngier * ensuring that the data side is always coherent. We still 15787ae2f3dbSMarc Zyngier * need to invalidate the I-cache though, as FWB does *not* 15797ae2f3dbSMarc Zyngier * imply CTR_EL0.DIC. 15809ed24f4bSMarc Zyngier */ 1581cc5705fbSMarc Zyngier if (vcpu_has_run_once(vcpu)) { 15827ae2f3dbSMarc Zyngier if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 15839ed24f4bSMarc Zyngier stage2_unmap_vm(vcpu->kvm); 15847ae2f3dbSMarc Zyngier else 1585fade9c2cSFuad Tabba icache_inval_all_pou(); 15867ae2f3dbSMarc Zyngier } 15879ed24f4bSMarc Zyngier 15889ed24f4bSMarc Zyngier vcpu_reset_hcr(vcpu); 158975c76ab5SMarc Zyngier vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu); 15909ed24f4bSMarc Zyngier 15919ed24f4bSMarc Zyngier /* 15929ed24f4bSMarc Zyngier * Handle the "start in power-off" case. 15939ed24f4bSMarc Zyngier */ 15944ff910beSReiji Watanabe spin_lock(&vcpu->arch.mp_state_lock); 15954ff910beSReiji Watanabe 1596e3c1c0caSOliver Upton if (power_off) 15974ff910beSReiji Watanabe __kvm_arm_vcpu_power_off(vcpu); 15989ed24f4bSMarc Zyngier else 15990acc7239SOliver Upton WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE); 16009ed24f4bSMarc Zyngier 16014ff910beSReiji Watanabe spin_unlock(&vcpu->arch.mp_state_lock); 16029ed24f4bSMarc Zyngier 16039ed24f4bSMarc Zyngier return 0; 16049ed24f4bSMarc Zyngier } 16059ed24f4bSMarc Zyngier 16069ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, 16079ed24f4bSMarc Zyngier struct kvm_device_attr *attr) 16089ed24f4bSMarc Zyngier { 16099ed24f4bSMarc Zyngier int ret = -ENXIO; 16109ed24f4bSMarc Zyngier 16119ed24f4bSMarc Zyngier switch (attr->group) { 16129ed24f4bSMarc Zyngier default: 16139ed24f4bSMarc Zyngier ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); 16149ed24f4bSMarc Zyngier break; 16159ed24f4bSMarc Zyngier } 16169ed24f4bSMarc Zyngier 16179ed24f4bSMarc Zyngier return ret; 16189ed24f4bSMarc Zyngier } 16199ed24f4bSMarc Zyngier 16209ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, 16219ed24f4bSMarc Zyngier struct kvm_device_attr *attr) 16229ed24f4bSMarc Zyngier { 16239ed24f4bSMarc Zyngier int ret = -ENXIO; 16249ed24f4bSMarc Zyngier 16259ed24f4bSMarc Zyngier switch (attr->group) { 16269ed24f4bSMarc Zyngier default: 16279ed24f4bSMarc Zyngier ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); 16289ed24f4bSMarc Zyngier break; 16299ed24f4bSMarc Zyngier } 16309ed24f4bSMarc Zyngier 16319ed24f4bSMarc Zyngier return ret; 16329ed24f4bSMarc Zyngier } 16339ed24f4bSMarc Zyngier 16349ed24f4bSMarc Zyngier static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, 16359ed24f4bSMarc Zyngier struct kvm_device_attr *attr) 16369ed24f4bSMarc Zyngier { 16379ed24f4bSMarc Zyngier int ret = -ENXIO; 16389ed24f4bSMarc Zyngier 16399ed24f4bSMarc Zyngier switch (attr->group) { 16409ed24f4bSMarc Zyngier default: 16419ed24f4bSMarc Zyngier ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); 16429ed24f4bSMarc Zyngier break; 16439ed24f4bSMarc Zyngier } 16449ed24f4bSMarc Zyngier 16459ed24f4bSMarc Zyngier return ret; 16469ed24f4bSMarc Zyngier } 16479ed24f4bSMarc Zyngier 16489ed24f4bSMarc Zyngier static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 16499ed24f4bSMarc Zyngier struct kvm_vcpu_events *events) 16509ed24f4bSMarc Zyngier { 16519ed24f4bSMarc Zyngier memset(events, 0, sizeof(*events)); 16529ed24f4bSMarc Zyngier 16539ed24f4bSMarc Zyngier return __kvm_arm_vcpu_get_events(vcpu, events); 16549ed24f4bSMarc Zyngier } 16559ed24f4bSMarc Zyngier 16569ed24f4bSMarc Zyngier static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 16579ed24f4bSMarc Zyngier struct kvm_vcpu_events *events) 16589ed24f4bSMarc Zyngier { 16599ed24f4bSMarc Zyngier int i; 16609ed24f4bSMarc Zyngier 16619ed24f4bSMarc Zyngier /* check whether the reserved field is zero */ 16629ed24f4bSMarc Zyngier for (i = 0; i < ARRAY_SIZE(events->reserved); i++) 16639ed24f4bSMarc Zyngier if (events->reserved[i]) 16649ed24f4bSMarc Zyngier return -EINVAL; 16659ed24f4bSMarc Zyngier 16669ed24f4bSMarc Zyngier /* check whether the pad field is zero */ 16679ed24f4bSMarc Zyngier for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) 16689ed24f4bSMarc Zyngier if (events->exception.pad[i]) 16699ed24f4bSMarc Zyngier return -EINVAL; 16709ed24f4bSMarc Zyngier 16719ed24f4bSMarc Zyngier return __kvm_arm_vcpu_set_events(vcpu, events); 16729ed24f4bSMarc Zyngier } 16739ed24f4bSMarc Zyngier 16749ed24f4bSMarc Zyngier long kvm_arch_vcpu_ioctl(struct file *filp, 16759ed24f4bSMarc Zyngier unsigned int ioctl, unsigned long arg) 16769ed24f4bSMarc Zyngier { 16779ed24f4bSMarc Zyngier struct kvm_vcpu *vcpu = filp->private_data; 16789ed24f4bSMarc Zyngier void __user *argp = (void __user *)arg; 16799ed24f4bSMarc Zyngier struct kvm_device_attr attr; 16809ed24f4bSMarc Zyngier long r; 16819ed24f4bSMarc Zyngier 16829ed24f4bSMarc Zyngier switch (ioctl) { 16839ed24f4bSMarc Zyngier case KVM_ARM_VCPU_INIT: { 16849ed24f4bSMarc Zyngier struct kvm_vcpu_init init; 16859ed24f4bSMarc Zyngier 16869ed24f4bSMarc Zyngier r = -EFAULT; 16879ed24f4bSMarc Zyngier if (copy_from_user(&init, argp, sizeof(init))) 16889ed24f4bSMarc Zyngier break; 16899ed24f4bSMarc Zyngier 16909ed24f4bSMarc Zyngier r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); 16919ed24f4bSMarc Zyngier break; 16929ed24f4bSMarc Zyngier } 16939ed24f4bSMarc Zyngier case KVM_SET_ONE_REG: 16949ed24f4bSMarc Zyngier case KVM_GET_ONE_REG: { 16959ed24f4bSMarc Zyngier struct kvm_one_reg reg; 16969ed24f4bSMarc Zyngier 16979ed24f4bSMarc Zyngier r = -ENOEXEC; 16989ed24f4bSMarc Zyngier if (unlikely(!kvm_vcpu_initialized(vcpu))) 16999ed24f4bSMarc Zyngier break; 17009ed24f4bSMarc Zyngier 17019ed24f4bSMarc Zyngier r = -EFAULT; 17029ed24f4bSMarc Zyngier if (copy_from_user(®, argp, sizeof(reg))) 17039ed24f4bSMarc Zyngier break; 17049ed24f4bSMarc Zyngier 17056826c684SOliver Upton /* 17066826c684SOliver Upton * We could owe a reset due to PSCI. Handle the pending reset 17076826c684SOliver Upton * here to ensure userspace register accesses are ordered after 17086826c684SOliver Upton * the reset. 17096826c684SOliver Upton */ 17106826c684SOliver Upton if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 17116826c684SOliver Upton kvm_reset_vcpu(vcpu); 17126826c684SOliver Upton 17139ed24f4bSMarc Zyngier if (ioctl == KVM_SET_ONE_REG) 17149ed24f4bSMarc Zyngier r = kvm_arm_set_reg(vcpu, ®); 17159ed24f4bSMarc Zyngier else 17169ed24f4bSMarc Zyngier r = kvm_arm_get_reg(vcpu, ®); 17179ed24f4bSMarc Zyngier break; 17189ed24f4bSMarc Zyngier } 17199ed24f4bSMarc Zyngier case KVM_GET_REG_LIST: { 17209ed24f4bSMarc Zyngier struct kvm_reg_list __user *user_list = argp; 17219ed24f4bSMarc Zyngier struct kvm_reg_list reg_list; 17229ed24f4bSMarc Zyngier unsigned n; 17239ed24f4bSMarc Zyngier 17249ed24f4bSMarc Zyngier r = -ENOEXEC; 17259ed24f4bSMarc Zyngier if (unlikely(!kvm_vcpu_initialized(vcpu))) 17269ed24f4bSMarc Zyngier break; 17279ed24f4bSMarc Zyngier 17289ed24f4bSMarc Zyngier r = -EPERM; 17299ed24f4bSMarc Zyngier if (!kvm_arm_vcpu_is_finalized(vcpu)) 17309ed24f4bSMarc Zyngier break; 17319ed24f4bSMarc Zyngier 17329ed24f4bSMarc Zyngier r = -EFAULT; 17339ed24f4bSMarc Zyngier if (copy_from_user(®_list, user_list, sizeof(reg_list))) 17349ed24f4bSMarc Zyngier break; 17359ed24f4bSMarc Zyngier n = reg_list.n; 17369ed24f4bSMarc Zyngier reg_list.n = kvm_arm_num_regs(vcpu); 17379ed24f4bSMarc Zyngier if (copy_to_user(user_list, ®_list, sizeof(reg_list))) 17389ed24f4bSMarc Zyngier break; 17399ed24f4bSMarc Zyngier r = -E2BIG; 17409ed24f4bSMarc Zyngier if (n < reg_list.n) 17419ed24f4bSMarc Zyngier break; 17429ed24f4bSMarc Zyngier r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); 17439ed24f4bSMarc Zyngier break; 17449ed24f4bSMarc Zyngier } 17459ed24f4bSMarc Zyngier case KVM_SET_DEVICE_ATTR: { 17469ed24f4bSMarc Zyngier r = -EFAULT; 17479ed24f4bSMarc Zyngier if (copy_from_user(&attr, argp, sizeof(attr))) 17489ed24f4bSMarc Zyngier break; 17499ed24f4bSMarc Zyngier r = kvm_arm_vcpu_set_attr(vcpu, &attr); 17509ed24f4bSMarc Zyngier break; 17519ed24f4bSMarc Zyngier } 17529ed24f4bSMarc Zyngier case KVM_GET_DEVICE_ATTR: { 17539ed24f4bSMarc Zyngier r = -EFAULT; 17549ed24f4bSMarc Zyngier if (copy_from_user(&attr, argp, sizeof(attr))) 17559ed24f4bSMarc Zyngier break; 17569ed24f4bSMarc Zyngier r = kvm_arm_vcpu_get_attr(vcpu, &attr); 17579ed24f4bSMarc Zyngier break; 17589ed24f4bSMarc Zyngier } 17599ed24f4bSMarc Zyngier case KVM_HAS_DEVICE_ATTR: { 17609ed24f4bSMarc Zyngier r = -EFAULT; 17619ed24f4bSMarc Zyngier if (copy_from_user(&attr, argp, sizeof(attr))) 17629ed24f4bSMarc Zyngier break; 17639ed24f4bSMarc Zyngier r = kvm_arm_vcpu_has_attr(vcpu, &attr); 17649ed24f4bSMarc Zyngier break; 17659ed24f4bSMarc Zyngier } 17669ed24f4bSMarc Zyngier case KVM_GET_VCPU_EVENTS: { 17679ed24f4bSMarc Zyngier struct kvm_vcpu_events events; 17689ed24f4bSMarc Zyngier 17699ed24f4bSMarc Zyngier if (kvm_arm_vcpu_get_events(vcpu, &events)) 17709ed24f4bSMarc Zyngier return -EINVAL; 17719ed24f4bSMarc Zyngier 17729ed24f4bSMarc Zyngier if (copy_to_user(argp, &events, sizeof(events))) 17739ed24f4bSMarc Zyngier return -EFAULT; 17749ed24f4bSMarc Zyngier 17759ed24f4bSMarc Zyngier return 0; 17769ed24f4bSMarc Zyngier } 17779ed24f4bSMarc Zyngier case KVM_SET_VCPU_EVENTS: { 17789ed24f4bSMarc Zyngier struct kvm_vcpu_events events; 17799ed24f4bSMarc Zyngier 17809ed24f4bSMarc Zyngier if (copy_from_user(&events, argp, sizeof(events))) 17819ed24f4bSMarc Zyngier return -EFAULT; 17829ed24f4bSMarc Zyngier 17839ed24f4bSMarc Zyngier return kvm_arm_vcpu_set_events(vcpu, &events); 17849ed24f4bSMarc Zyngier } 17859ed24f4bSMarc Zyngier case KVM_ARM_VCPU_FINALIZE: { 17869ed24f4bSMarc Zyngier int what; 17879ed24f4bSMarc Zyngier 17889ed24f4bSMarc Zyngier if (!kvm_vcpu_initialized(vcpu)) 17899ed24f4bSMarc Zyngier return -ENOEXEC; 17909ed24f4bSMarc Zyngier 17919ed24f4bSMarc Zyngier if (get_user(what, (const int __user *)argp)) 17929ed24f4bSMarc Zyngier return -EFAULT; 17939ed24f4bSMarc Zyngier 17949ed24f4bSMarc Zyngier return kvm_arm_vcpu_finalize(vcpu, what); 17959ed24f4bSMarc Zyngier } 17969ed24f4bSMarc Zyngier default: 17979ed24f4bSMarc Zyngier r = -EINVAL; 17989ed24f4bSMarc Zyngier } 17999ed24f4bSMarc Zyngier 18009ed24f4bSMarc Zyngier return r; 18019ed24f4bSMarc Zyngier } 18029ed24f4bSMarc Zyngier 18039ed24f4bSMarc Zyngier void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 18049ed24f4bSMarc Zyngier { 18059ed24f4bSMarc Zyngier 18069ed24f4bSMarc Zyngier } 18079ed24f4bSMarc Zyngier 18089ed24f4bSMarc Zyngier static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, 18099ed24f4bSMarc Zyngier struct kvm_arm_device_addr *dev_addr) 18109ed24f4bSMarc Zyngier { 18119f968c92SMarc Zyngier switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) { 18129ed24f4bSMarc Zyngier case KVM_ARM_DEVICE_VGIC_V2: 18139ed24f4bSMarc Zyngier if (!vgic_present) 18149ed24f4bSMarc Zyngier return -ENXIO; 18159f968c92SMarc Zyngier return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr); 18169ed24f4bSMarc Zyngier default: 18179ed24f4bSMarc Zyngier return -ENODEV; 18189ed24f4bSMarc Zyngier } 18199ed24f4bSMarc Zyngier } 18209ed24f4bSMarc Zyngier 1821e0fc6b21SOliver Upton static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1822e0fc6b21SOliver Upton { 1823e0fc6b21SOliver Upton switch (attr->group) { 1824821d935cSOliver Upton case KVM_ARM_VM_SMCCC_CTRL: 1825821d935cSOliver Upton return kvm_vm_smccc_has_attr(kvm, attr); 1826e0fc6b21SOliver Upton default: 1827e0fc6b21SOliver Upton return -ENXIO; 1828e0fc6b21SOliver Upton } 1829e0fc6b21SOliver Upton } 1830e0fc6b21SOliver Upton 1831e0fc6b21SOliver Upton static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1832e0fc6b21SOliver Upton { 1833e0fc6b21SOliver Upton switch (attr->group) { 1834821d935cSOliver Upton case KVM_ARM_VM_SMCCC_CTRL: 1835821d935cSOliver Upton return kvm_vm_smccc_set_attr(kvm, attr); 1836e0fc6b21SOliver Upton default: 1837e0fc6b21SOliver Upton return -ENXIO; 1838e0fc6b21SOliver Upton } 1839e0fc6b21SOliver Upton } 1840e0fc6b21SOliver Upton 1841d8708b80SThomas Huth int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) 18429ed24f4bSMarc Zyngier { 18439ed24f4bSMarc Zyngier struct kvm *kvm = filp->private_data; 18449ed24f4bSMarc Zyngier void __user *argp = (void __user *)arg; 1845e0fc6b21SOliver Upton struct kvm_device_attr attr; 18469ed24f4bSMarc Zyngier 18479ed24f4bSMarc Zyngier switch (ioctl) { 18489ed24f4bSMarc Zyngier case KVM_CREATE_IRQCHIP: { 18499ed24f4bSMarc Zyngier int ret; 18509ed24f4bSMarc Zyngier if (!vgic_present) 18519ed24f4bSMarc Zyngier return -ENXIO; 18529ed24f4bSMarc Zyngier mutex_lock(&kvm->lock); 18539ed24f4bSMarc Zyngier ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 18549ed24f4bSMarc Zyngier mutex_unlock(&kvm->lock); 18559ed24f4bSMarc Zyngier return ret; 18569ed24f4bSMarc Zyngier } 18579ed24f4bSMarc Zyngier case KVM_ARM_SET_DEVICE_ADDR: { 18589ed24f4bSMarc Zyngier struct kvm_arm_device_addr dev_addr; 18599ed24f4bSMarc Zyngier 18609ed24f4bSMarc Zyngier if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) 18619ed24f4bSMarc Zyngier return -EFAULT; 18629ed24f4bSMarc Zyngier return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); 18639ed24f4bSMarc Zyngier } 18649ed24f4bSMarc Zyngier case KVM_ARM_PREFERRED_TARGET: { 18655346f7e1SOliver Upton struct kvm_vcpu_init init = { 18665346f7e1SOliver Upton .target = KVM_ARM_TARGET_GENERIC_V8, 18675346f7e1SOliver Upton }; 18689ed24f4bSMarc Zyngier 18699ed24f4bSMarc Zyngier if (copy_to_user(argp, &init, sizeof(init))) 18709ed24f4bSMarc Zyngier return -EFAULT; 18719ed24f4bSMarc Zyngier 18729ed24f4bSMarc Zyngier return 0; 18739ed24f4bSMarc Zyngier } 1874f0376edbSSteven Price case KVM_ARM_MTE_COPY_TAGS: { 1875f0376edbSSteven Price struct kvm_arm_copy_mte_tags copy_tags; 1876f0376edbSSteven Price 1877f0376edbSSteven Price if (copy_from_user(©_tags, argp, sizeof(copy_tags))) 1878f0376edbSSteven Price return -EFAULT; 1879f0376edbSSteven Price return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags); 1880f0376edbSSteven Price } 188130ec7997SMarc Zyngier case KVM_ARM_SET_COUNTER_OFFSET: { 188230ec7997SMarc Zyngier struct kvm_arm_counter_offset offset; 188330ec7997SMarc Zyngier 188430ec7997SMarc Zyngier if (copy_from_user(&offset, argp, sizeof(offset))) 188530ec7997SMarc Zyngier return -EFAULT; 188630ec7997SMarc Zyngier return kvm_vm_ioctl_set_counter_offset(kvm, &offset); 188730ec7997SMarc Zyngier } 1888e0fc6b21SOliver Upton case KVM_HAS_DEVICE_ATTR: { 1889e0fc6b21SOliver Upton if (copy_from_user(&attr, argp, sizeof(attr))) 1890e0fc6b21SOliver Upton return -EFAULT; 1891e0fc6b21SOliver Upton 1892e0fc6b21SOliver Upton return kvm_vm_has_attr(kvm, &attr); 1893e0fc6b21SOliver Upton } 1894e0fc6b21SOliver Upton case KVM_SET_DEVICE_ATTR: { 1895e0fc6b21SOliver Upton if (copy_from_user(&attr, argp, sizeof(attr))) 1896e0fc6b21SOliver Upton return -EFAULT; 1897e0fc6b21SOliver Upton 1898e0fc6b21SOliver Upton return kvm_vm_set_attr(kvm, &attr); 1899e0fc6b21SOliver Upton } 19003f9cd0caSJing Zhang case KVM_ARM_GET_REG_WRITABLE_MASKS: { 19013f9cd0caSJing Zhang struct reg_mask_range range; 19023f9cd0caSJing Zhang 19033f9cd0caSJing Zhang if (copy_from_user(&range, argp, sizeof(range))) 19043f9cd0caSJing Zhang return -EFAULT; 19053f9cd0caSJing Zhang return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range); 19063f9cd0caSJing Zhang } 19079ed24f4bSMarc Zyngier default: 19089ed24f4bSMarc Zyngier return -EINVAL; 19099ed24f4bSMarc Zyngier } 19109ed24f4bSMarc Zyngier } 19119ed24f4bSMarc Zyngier 191296906a91SMarc Zyngier /* unlocks vcpus from @vcpu_lock_idx and smaller */ 191396906a91SMarc Zyngier static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx) 191496906a91SMarc Zyngier { 191596906a91SMarc Zyngier struct kvm_vcpu *tmp_vcpu; 191696906a91SMarc Zyngier 191796906a91SMarc Zyngier for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { 191896906a91SMarc Zyngier tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); 191996906a91SMarc Zyngier mutex_unlock(&tmp_vcpu->mutex); 192096906a91SMarc Zyngier } 192196906a91SMarc Zyngier } 192296906a91SMarc Zyngier 192396906a91SMarc Zyngier void unlock_all_vcpus(struct kvm *kvm) 192496906a91SMarc Zyngier { 192596906a91SMarc Zyngier lockdep_assert_held(&kvm->lock); 192696906a91SMarc Zyngier 192796906a91SMarc Zyngier unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1); 192896906a91SMarc Zyngier } 192996906a91SMarc Zyngier 193096906a91SMarc Zyngier /* Returns true if all vcpus were locked, false otherwise */ 193196906a91SMarc Zyngier bool lock_all_vcpus(struct kvm *kvm) 193296906a91SMarc Zyngier { 193396906a91SMarc Zyngier struct kvm_vcpu *tmp_vcpu; 193496906a91SMarc Zyngier unsigned long c; 193596906a91SMarc Zyngier 193696906a91SMarc Zyngier lockdep_assert_held(&kvm->lock); 193796906a91SMarc Zyngier 193896906a91SMarc Zyngier /* 193996906a91SMarc Zyngier * Any time a vcpu is in an ioctl (including running), the 194096906a91SMarc Zyngier * core KVM code tries to grab the vcpu->mutex. 194196906a91SMarc Zyngier * 194296906a91SMarc Zyngier * By grabbing the vcpu->mutex of all VCPUs we ensure that no 194396906a91SMarc Zyngier * other VCPUs can fiddle with the state while we access it. 194496906a91SMarc Zyngier */ 194596906a91SMarc Zyngier kvm_for_each_vcpu(c, tmp_vcpu, kvm) { 194696906a91SMarc Zyngier if (!mutex_trylock(&tmp_vcpu->mutex)) { 194796906a91SMarc Zyngier unlock_vcpus(kvm, c - 1); 194896906a91SMarc Zyngier return false; 194996906a91SMarc Zyngier } 195096906a91SMarc Zyngier } 195196906a91SMarc Zyngier 195296906a91SMarc Zyngier return true; 195396906a91SMarc Zyngier } 195496906a91SMarc Zyngier 195530c95391SDavid Brazdil static unsigned long nvhe_percpu_size(void) 195630c95391SDavid Brazdil { 195730c95391SDavid Brazdil return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) - 195830c95391SDavid Brazdil (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start); 195930c95391SDavid Brazdil } 196030c95391SDavid Brazdil 196130c95391SDavid Brazdil static unsigned long nvhe_percpu_order(void) 196230c95391SDavid Brazdil { 196330c95391SDavid Brazdil unsigned long size = nvhe_percpu_size(); 196430c95391SDavid Brazdil 196530c95391SDavid Brazdil return size ? get_order(size) : 0; 196630c95391SDavid Brazdil } 196730c95391SDavid Brazdil 196866d5b53eSFuad Tabba static size_t pkvm_host_sve_state_order(void) 196966d5b53eSFuad Tabba { 197066d5b53eSFuad Tabba return get_order(pkvm_host_sve_state_size()); 197166d5b53eSFuad Tabba } 197266d5b53eSFuad Tabba 1973b881cdceSWill Deacon /* A lookup table holding the hypervisor VA for each vector slot */ 1974b881cdceSWill Deacon static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS]; 1975de5bcdb4SWill Deacon 1976b881cdceSWill Deacon static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot) 1977b881cdceSWill Deacon { 1978bc1d2892SQuentin Perret hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot); 19799ef2b48bSWill Deacon } 19809ef2b48bSWill Deacon 1981b881cdceSWill Deacon static int kvm_init_vector_slots(void) 1982b881cdceSWill Deacon { 1983b881cdceSWill Deacon int err; 1984b881cdceSWill Deacon void *base; 1985b881cdceSWill Deacon 1986b881cdceSWill Deacon base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 1987b881cdceSWill Deacon kvm_init_vector_slot(base, HYP_VECTOR_DIRECT); 1988b881cdceSWill Deacon 1989b881cdceSWill Deacon base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); 1990b881cdceSWill Deacon kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT); 1991b881cdceSWill Deacon 19922e403167SQuentin Perret if (kvm_system_needs_idmapped_vectors() && 19932e403167SQuentin Perret !is_protected_kvm_enabled()) { 1994b881cdceSWill Deacon err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs), 1995b881cdceSWill Deacon __BP_HARDEN_HYP_VECS_SZ, &base); 1996b881cdceSWill Deacon if (err) 1997b881cdceSWill Deacon return err; 1998b881cdceSWill Deacon } 19999ef2b48bSWill Deacon 2000b881cdceSWill Deacon kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT); 2001b881cdceSWill Deacon kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT); 20029ef2b48bSWill Deacon return 0; 20039ef2b48bSWill Deacon } 20049ef2b48bSWill Deacon 200553bf620aSSean Christopherson static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits) 20069ed24f4bSMarc Zyngier { 20079cc77581SQuentin Perret struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 2008bd412e2aSRyan Roberts u64 mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 2009d3e1086cSDavid Brazdil unsigned long tcr; 20109ed24f4bSMarc Zyngier 201171b3ec5fSDavid Brazdil /* 201271b3ec5fSDavid Brazdil * Calculate the raw per-cpu offset without a translation from the 201371b3ec5fSDavid Brazdil * kernel's mapping to the linear mapping, and store it in tpidr_el2 201471b3ec5fSDavid Brazdil * so that we can use adr_l to access per-cpu variables in EL2. 2015e1663372SSteven Price * Also drop the KASAN tag which gets in the way... 201671b3ec5fSDavid Brazdil */ 20179cc77581SQuentin Perret params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) - 201830c95391SDavid Brazdil (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start)); 201971b3ec5fSDavid Brazdil 2020d3e1086cSDavid Brazdil params->mair_el2 = read_sysreg(mair_el1); 2021d3e1086cSDavid Brazdil 2022cff3b5cfSMarc Zyngier tcr = read_sysreg(tcr_el1); 2023cff3b5cfSMarc Zyngier if (cpus_have_final_cap(ARM64_KVM_HVHE)) { 2024cff3b5cfSMarc Zyngier tcr |= TCR_EPD1_MASK; 2025cff3b5cfSMarc Zyngier } else { 2026cff3b5cfSMarc Zyngier tcr &= TCR_EL2_MASK; 2027cff3b5cfSMarc Zyngier tcr |= TCR_EL2_RES1; 2028cff3b5cfSMarc Zyngier } 2029d3e1086cSDavid Brazdil tcr &= ~TCR_T0SZ_MASK; 2030579d7ebeSRyan Roberts tcr |= TCR_T0SZ(hyp_va_bits); 2031bd412e2aSRyan Roberts tcr &= ~TCR_EL2_PS_MASK; 2032bd412e2aSRyan Roberts tcr |= FIELD_PREP(TCR_EL2_PS_MASK, kvm_get_parange(mmfr0)); 2033bd412e2aSRyan Roberts if (kvm_lpa2_is_enabled()) 2034bd412e2aSRyan Roberts tcr |= TCR_EL2_DS; 2035d3e1086cSDavid Brazdil params->tcr_el2 = tcr; 2036d3e1086cSDavid Brazdil 203763fec243SDavid Brazdil params->pgd_pa = kvm_mmu_get_httbr(); 2038734864c1SQuentin Perret if (is_protected_kvm_enabled()) 2039734864c1SQuentin Perret params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; 2040734864c1SQuentin Perret else 2041734864c1SQuentin Perret params->hcr_el2 = HCR_HOST_NVHE_FLAGS; 2042d0daf5a2SMarc Zyngier if (cpus_have_final_cap(ARM64_KVM_HVHE)) 2043d0daf5a2SMarc Zyngier params->hcr_el2 |= HCR_E2H; 2044734864c1SQuentin Perret params->vttbr = params->vtcr = 0; 204563fec243SDavid Brazdil 204663fec243SDavid Brazdil /* 204763fec243SDavid Brazdil * Flush the init params from the data cache because the struct will 204863fec243SDavid Brazdil * be read while the MMU is off. 204963fec243SDavid Brazdil */ 205063fec243SDavid Brazdil kvm_flush_dcache_to_poc(params, sizeof(*params)); 20519cc77581SQuentin Perret } 20529cc77581SQuentin Perret 2053bfa79a80SQuentin Perret static void hyp_install_host_vector(void) 20549cc77581SQuentin Perret { 20559cc77581SQuentin Perret struct kvm_nvhe_init_params *params; 20569cc77581SQuentin Perret struct arm_smccc_res res; 20579cc77581SQuentin Perret 20589cc77581SQuentin Perret /* Switch from the HYP stub to our own HYP init vector */ 20599cc77581SQuentin Perret __hyp_set_vectors(kvm_get_idmap_vector()); 20609ed24f4bSMarc Zyngier 206171b3ec5fSDavid Brazdil /* 206271b3ec5fSDavid Brazdil * Call initialization code, and switch to the full blown HYP code. 206371b3ec5fSDavid Brazdil * If the cpucaps haven't been finalized yet, something has gone very 206471b3ec5fSDavid Brazdil * wrong, and hyp will crash and burn when it uses any 2065d8569fbaSMark Rutland * cpus_have_*_cap() wrapper. 206671b3ec5fSDavid Brazdil */ 206771b3ec5fSDavid Brazdil BUG_ON(!system_capabilities_finalized()); 20689cc77581SQuentin Perret params = this_cpu_ptr_nvhe_sym(kvm_init_params); 206963fec243SDavid Brazdil arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res); 207004e4caa8SAndrew Scull WARN_ON(res.a0 != SMCCC_RET_SUCCESS); 2071bfa79a80SQuentin Perret } 2072bfa79a80SQuentin Perret 2073bfa79a80SQuentin Perret static void cpu_init_hyp_mode(void) 2074bfa79a80SQuentin Perret { 2075bfa79a80SQuentin Perret hyp_install_host_vector(); 207671b3ec5fSDavid Brazdil 207771b3ec5fSDavid Brazdil /* 207871b3ec5fSDavid Brazdil * Disabling SSBD on a non-VHE system requires us to enable SSBS 207971b3ec5fSDavid Brazdil * at EL2. 208071b3ec5fSDavid Brazdil */ 208171b3ec5fSDavid Brazdil if (this_cpu_has_cap(ARM64_SSBS) && 2082d63d975aSMarc Zyngier arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) { 208313aeb9b4SDavid Brazdil kvm_call_hyp_nvhe(__kvm_enable_ssbs); 208471b3ec5fSDavid Brazdil } 20859ed24f4bSMarc Zyngier } 20869ed24f4bSMarc Zyngier 20879ed24f4bSMarc Zyngier static void cpu_hyp_reset(void) 20889ed24f4bSMarc Zyngier { 20899ed24f4bSMarc Zyngier if (!is_kernel_in_hyp_mode()) 20909ed24f4bSMarc Zyngier __hyp_reset_vectors(); 20919ed24f4bSMarc Zyngier } 20929ed24f4bSMarc Zyngier 2093042c76a9SWill Deacon /* 2094042c76a9SWill Deacon * EL2 vectors can be mapped and rerouted in a number of ways, 2095042c76a9SWill Deacon * depending on the kernel configuration and CPU present: 2096042c76a9SWill Deacon * 2097042c76a9SWill Deacon * - If the CPU is affected by Spectre-v2, the hardening sequence is 2098042c76a9SWill Deacon * placed in one of the vector slots, which is executed before jumping 2099042c76a9SWill Deacon * to the real vectors. 2100042c76a9SWill Deacon * 2101c4792b6dSWill Deacon * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot 2102042c76a9SWill Deacon * containing the hardening sequence is mapped next to the idmap page, 2103042c76a9SWill Deacon * and executed before jumping to the real vectors. 2104042c76a9SWill Deacon * 2105c4792b6dSWill Deacon * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an 2106042c76a9SWill Deacon * empty slot is selected, mapped next to the idmap page, and 2107042c76a9SWill Deacon * executed before jumping to the real vectors. 2108042c76a9SWill Deacon * 2109c4792b6dSWill Deacon * Note that ARM64_SPECTRE_V3A is somewhat incompatible with 2110042c76a9SWill Deacon * VHE, as we don't have hypervisor-specific mappings. If the system 2111042c76a9SWill Deacon * is VHE and yet selects this capability, it will be ignored. 2112042c76a9SWill Deacon */ 2113042c76a9SWill Deacon static void cpu_set_hyp_vector(void) 2114042c76a9SWill Deacon { 21156279017eSWill Deacon struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data); 2116b881cdceSWill Deacon void *vector = hyp_spectre_vector_selector[data->slot]; 2117042c76a9SWill Deacon 2118bfa79a80SQuentin Perret if (!is_protected_kvm_enabled()) 2119b881cdceSWill Deacon *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector; 2120bfa79a80SQuentin Perret else 2121bfa79a80SQuentin Perret kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot); 2122042c76a9SWill Deacon } 2123042c76a9SWill Deacon 21248579a185SWill Deacon static void cpu_hyp_init_context(void) 21259ed24f4bSMarc Zyngier { 212687f842c6SMarc Zyngier kvm_init_host_cpu_context(host_data_ptr(host_ctxt)); 21279ed24f4bSMarc Zyngier 21288579a185SWill Deacon if (!is_kernel_in_hyp_mode()) 21298579a185SWill Deacon cpu_init_hyp_mode(); 21308579a185SWill Deacon } 21318579a185SWill Deacon 21328579a185SWill Deacon static void cpu_hyp_init_features(void) 21338579a185SWill Deacon { 21348579a185SWill Deacon cpu_set_hyp_vector(); 21358579a185SWill Deacon kvm_arm_init_debug(); 2136a0e47952SAndrew Scull 21379ed24f4bSMarc Zyngier if (is_kernel_in_hyp_mode()) 21389ed24f4bSMarc Zyngier kvm_timer_init_vhe(); 21399ed24f4bSMarc Zyngier 21409ed24f4bSMarc Zyngier if (vgic_present) 21419ed24f4bSMarc Zyngier kvm_vgic_init_cpu_hardware(); 21429ed24f4bSMarc Zyngier } 21439ed24f4bSMarc Zyngier 21448579a185SWill Deacon static void cpu_hyp_reinit(void) 21458579a185SWill Deacon { 21468579a185SWill Deacon cpu_hyp_reset(); 21478579a185SWill Deacon cpu_hyp_init_context(); 21488579a185SWill Deacon cpu_hyp_init_features(); 21498579a185SWill Deacon } 21508579a185SWill Deacon 2151733c758eSOliver Upton static void cpu_hyp_init(void *discard) 21529ed24f4bSMarc Zyngier { 2153733c758eSOliver Upton if (!__this_cpu_read(kvm_hyp_initialized)) { 21549ed24f4bSMarc Zyngier cpu_hyp_reinit(); 2155733c758eSOliver Upton __this_cpu_write(kvm_hyp_initialized, 1); 2156733c758eSOliver Upton } 2157733c758eSOliver Upton } 2158733c758eSOliver Upton 2159733c758eSOliver Upton static void cpu_hyp_uninit(void *discard) 2160733c758eSOliver Upton { 2161733c758eSOliver Upton if (__this_cpu_read(kvm_hyp_initialized)) { 2162733c758eSOliver Upton cpu_hyp_reset(); 2163733c758eSOliver Upton __this_cpu_write(kvm_hyp_initialized, 0); 21649ed24f4bSMarc Zyngier } 21659ed24f4bSMarc Zyngier } 21669ed24f4bSMarc Zyngier 21679ed24f4bSMarc Zyngier int kvm_arch_hardware_enable(void) 21689ed24f4bSMarc Zyngier { 2169970dee09SMarc Zyngier /* 2170970dee09SMarc Zyngier * Most calls to this function are made with migration 2171970dee09SMarc Zyngier * disabled, but not with preemption disabled. The former is 2172970dee09SMarc Zyngier * enough to ensure correctness, but most of the helpers 2173970dee09SMarc Zyngier * expect the later and will throw a tantrum otherwise. 2174970dee09SMarc Zyngier */ 2175970dee09SMarc Zyngier preempt_disable(); 2176970dee09SMarc Zyngier 2177733c758eSOliver Upton cpu_hyp_init(NULL); 2178466d27e4SMarc Zyngier 2179466d27e4SMarc Zyngier kvm_vgic_cpu_up(); 2180466d27e4SMarc Zyngier kvm_timer_cpu_up(); 2181466d27e4SMarc Zyngier 2182970dee09SMarc Zyngier preempt_enable(); 2183970dee09SMarc Zyngier 21849ed24f4bSMarc Zyngier return 0; 21859ed24f4bSMarc Zyngier } 21869ed24f4bSMarc Zyngier 21879ed24f4bSMarc Zyngier void kvm_arch_hardware_disable(void) 21889ed24f4bSMarc Zyngier { 2189466d27e4SMarc Zyngier kvm_timer_cpu_down(); 2190466d27e4SMarc Zyngier kvm_vgic_cpu_down(); 2191466d27e4SMarc Zyngier 2192fa8c3d65SDavid Brazdil if (!is_protected_kvm_enabled()) 2193733c758eSOliver Upton cpu_hyp_uninit(NULL); 21949ed24f4bSMarc Zyngier } 21959ed24f4bSMarc Zyngier 21969ed24f4bSMarc Zyngier #ifdef CONFIG_CPU_PM 21979ed24f4bSMarc Zyngier static int hyp_init_cpu_pm_notifier(struct notifier_block *self, 21989ed24f4bSMarc Zyngier unsigned long cmd, 21999ed24f4bSMarc Zyngier void *v) 22009ed24f4bSMarc Zyngier { 22019ed24f4bSMarc Zyngier /* 2202733c758eSOliver Upton * kvm_hyp_initialized is left with its old value over 22039ed24f4bSMarc Zyngier * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should 22049ed24f4bSMarc Zyngier * re-enable hyp. 22059ed24f4bSMarc Zyngier */ 22069ed24f4bSMarc Zyngier switch (cmd) { 22079ed24f4bSMarc Zyngier case CPU_PM_ENTER: 2208733c758eSOliver Upton if (__this_cpu_read(kvm_hyp_initialized)) 22099ed24f4bSMarc Zyngier /* 2210733c758eSOliver Upton * don't update kvm_hyp_initialized here 2211733c758eSOliver Upton * so that the hyp will be re-enabled 22129ed24f4bSMarc Zyngier * when we resume. See below. 22139ed24f4bSMarc Zyngier */ 22149ed24f4bSMarc Zyngier cpu_hyp_reset(); 22159ed24f4bSMarc Zyngier 22169ed24f4bSMarc Zyngier return NOTIFY_OK; 22179ed24f4bSMarc Zyngier case CPU_PM_ENTER_FAILED: 22189ed24f4bSMarc Zyngier case CPU_PM_EXIT: 2219733c758eSOliver Upton if (__this_cpu_read(kvm_hyp_initialized)) 2220733c758eSOliver Upton /* The hyp was enabled before suspend. */ 22219ed24f4bSMarc Zyngier cpu_hyp_reinit(); 22229ed24f4bSMarc Zyngier 22239ed24f4bSMarc Zyngier return NOTIFY_OK; 22249ed24f4bSMarc Zyngier 22259ed24f4bSMarc Zyngier default: 22269ed24f4bSMarc Zyngier return NOTIFY_DONE; 22279ed24f4bSMarc Zyngier } 22289ed24f4bSMarc Zyngier } 22299ed24f4bSMarc Zyngier 22309ed24f4bSMarc Zyngier static struct notifier_block hyp_init_cpu_pm_nb = { 22319ed24f4bSMarc Zyngier .notifier_call = hyp_init_cpu_pm_notifier, 22329ed24f4bSMarc Zyngier }; 22339ed24f4bSMarc Zyngier 223453bf620aSSean Christopherson static void __init hyp_cpu_pm_init(void) 22359ed24f4bSMarc Zyngier { 2236fa8c3d65SDavid Brazdil if (!is_protected_kvm_enabled()) 22379ed24f4bSMarc Zyngier cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); 22389ed24f4bSMarc Zyngier } 223953bf620aSSean Christopherson static void __init hyp_cpu_pm_exit(void) 22409ed24f4bSMarc Zyngier { 2241fa8c3d65SDavid Brazdil if (!is_protected_kvm_enabled()) 22429ed24f4bSMarc Zyngier cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); 22439ed24f4bSMarc Zyngier } 22449ed24f4bSMarc Zyngier #else 224553bf620aSSean Christopherson static inline void __init hyp_cpu_pm_init(void) 22469ed24f4bSMarc Zyngier { 22479ed24f4bSMarc Zyngier } 224853bf620aSSean Christopherson static inline void __init hyp_cpu_pm_exit(void) 22499ed24f4bSMarc Zyngier { 22509ed24f4bSMarc Zyngier } 22519ed24f4bSMarc Zyngier #endif 22529ed24f4bSMarc Zyngier 225353bf620aSSean Christopherson static void __init init_cpu_logical_map(void) 225494f5e8a4SDavid Brazdil { 225594f5e8a4SDavid Brazdil unsigned int cpu; 225694f5e8a4SDavid Brazdil 225794f5e8a4SDavid Brazdil /* 225894f5e8a4SDavid Brazdil * Copy the MPIDR <-> logical CPU ID mapping to hyp. 225921ea4578SJulia Lawall * Only copy the set of online CPUs whose features have been checked 226094f5e8a4SDavid Brazdil * against the finalized system capabilities. The hypervisor will not 226194f5e8a4SDavid Brazdil * allow any other CPUs from the `possible` set to boot. 226294f5e8a4SDavid Brazdil */ 226394f5e8a4SDavid Brazdil for_each_online_cpu(cpu) 226461fe0c37SDavid Brazdil hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu); 226594f5e8a4SDavid Brazdil } 226694f5e8a4SDavid Brazdil 2267767c973fSMarc Zyngier #define init_psci_0_1_impl_state(config, what) \ 2268767c973fSMarc Zyngier config.psci_0_1_ ## what ## _implemented = psci_ops.what 2269767c973fSMarc Zyngier 227053bf620aSSean Christopherson static bool __init init_psci_relay(void) 2271eeeee719SDavid Brazdil { 2272eeeee719SDavid Brazdil /* 2273eeeee719SDavid Brazdil * If PSCI has not been initialized, protected KVM cannot install 2274eeeee719SDavid Brazdil * itself on newly booted CPUs. 2275eeeee719SDavid Brazdil */ 2276eeeee719SDavid Brazdil if (!psci_ops.get_version) { 2277eeeee719SDavid Brazdil kvm_err("Cannot initialize protected mode without PSCI\n"); 2278eeeee719SDavid Brazdil return false; 2279eeeee719SDavid Brazdil } 2280eeeee719SDavid Brazdil 2281ff367fe4SDavid Brazdil kvm_host_psci_config.version = psci_ops.get_version(); 228212bdce4fSWill Deacon kvm_host_psci_config.smccc_version = arm_smccc_get_version(); 2283ff367fe4SDavid Brazdil 2284ff367fe4SDavid Brazdil if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) { 2285ff367fe4SDavid Brazdil kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids(); 2286767c973fSMarc Zyngier init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend); 2287767c973fSMarc Zyngier init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on); 2288767c973fSMarc Zyngier init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off); 2289767c973fSMarc Zyngier init_psci_0_1_impl_state(kvm_host_psci_config, migrate); 2290ff367fe4SDavid Brazdil } 2291eeeee719SDavid Brazdil return true; 2292eeeee719SDavid Brazdil } 2293eeeee719SDavid Brazdil 229453bf620aSSean Christopherson static int __init init_subsystems(void) 22959ed24f4bSMarc Zyngier { 22969ed24f4bSMarc Zyngier int err = 0; 22979ed24f4bSMarc Zyngier 22989ed24f4bSMarc Zyngier /* 22999ed24f4bSMarc Zyngier * Enable hardware so that subsystem initialisation can access EL2. 23009ed24f4bSMarc Zyngier */ 2301733c758eSOliver Upton on_each_cpu(cpu_hyp_init, NULL, 1); 23029ed24f4bSMarc Zyngier 23039ed24f4bSMarc Zyngier /* 23049ed24f4bSMarc Zyngier * Register CPU lower-power notifier 23059ed24f4bSMarc Zyngier */ 23069ed24f4bSMarc Zyngier hyp_cpu_pm_init(); 23079ed24f4bSMarc Zyngier 23089ed24f4bSMarc Zyngier /* 23099ed24f4bSMarc Zyngier * Init HYP view of VGIC 23109ed24f4bSMarc Zyngier */ 23119ed24f4bSMarc Zyngier err = kvm_vgic_hyp_init(); 23129ed24f4bSMarc Zyngier switch (err) { 23139ed24f4bSMarc Zyngier case 0: 23149ed24f4bSMarc Zyngier vgic_present = true; 23159ed24f4bSMarc Zyngier break; 23169ed24f4bSMarc Zyngier case -ENODEV: 23179ed24f4bSMarc Zyngier case -ENXIO: 23189ed24f4bSMarc Zyngier vgic_present = false; 23199ed24f4bSMarc Zyngier err = 0; 23209ed24f4bSMarc Zyngier break; 23219ed24f4bSMarc Zyngier default: 23229ed24f4bSMarc Zyngier goto out; 23239ed24f4bSMarc Zyngier } 23249ed24f4bSMarc Zyngier 23259ed24f4bSMarc Zyngier /* 23269ed24f4bSMarc Zyngier * Init HYP architected timer support 23279ed24f4bSMarc Zyngier */ 23289ed24f4bSMarc Zyngier err = kvm_timer_hyp_init(vgic_present); 23299ed24f4bSMarc Zyngier if (err) 23309ed24f4bSMarc Zyngier goto out; 23319ed24f4bSMarc Zyngier 233217ed14ebSSean Christopherson kvm_register_perf_callbacks(NULL); 233317ed14ebSSean Christopherson 23349ed24f4bSMarc Zyngier out: 233578b3bf48SSean Christopherson if (err) 233678b3bf48SSean Christopherson hyp_cpu_pm_exit(); 233778b3bf48SSean Christopherson 2338fa8c3d65SDavid Brazdil if (err || !is_protected_kvm_enabled()) 2339733c758eSOliver Upton on_each_cpu(cpu_hyp_uninit, NULL, 1); 23409ed24f4bSMarc Zyngier 23419ed24f4bSMarc Zyngier return err; 23429ed24f4bSMarc Zyngier } 23439ed24f4bSMarc Zyngier 234453bf620aSSean Christopherson static void __init teardown_subsystems(void) 234578b3bf48SSean Christopherson { 234678b3bf48SSean Christopherson kvm_unregister_perf_callbacks(); 234778b3bf48SSean Christopherson hyp_cpu_pm_exit(); 234878b3bf48SSean Christopherson } 234978b3bf48SSean Christopherson 235053bf620aSSean Christopherson static void __init teardown_hyp_mode(void) 23519ed24f4bSMarc Zyngier { 235266d5b53eSFuad Tabba bool free_sve = system_supports_sve() && is_protected_kvm_enabled(); 23539ed24f4bSMarc Zyngier int cpu; 23549ed24f4bSMarc Zyngier 23559ed24f4bSMarc Zyngier free_hyp_pgds(); 235630c95391SDavid Brazdil for_each_possible_cpu(cpu) { 23579ed24f4bSMarc Zyngier free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); 2358fe41a7f8SQuentin Perret free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order()); 235966d5b53eSFuad Tabba 236066d5b53eSFuad Tabba if (free_sve) { 236166d5b53eSFuad Tabba struct cpu_sve_state *sve_state; 236266d5b53eSFuad Tabba 236366d5b53eSFuad Tabba sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state; 236466d5b53eSFuad Tabba free_pages((unsigned long) sve_state, pkvm_host_sve_state_order()); 236566d5b53eSFuad Tabba } 236630c95391SDavid Brazdil } 23679ed24f4bSMarc Zyngier } 23689ed24f4bSMarc Zyngier 236953bf620aSSean Christopherson static int __init do_pkvm_init(u32 hyp_va_bits) 2370bfa79a80SQuentin Perret { 2371fe41a7f8SQuentin Perret void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)); 2372bfa79a80SQuentin Perret int ret; 2373bfa79a80SQuentin Perret 2374bfa79a80SQuentin Perret preempt_disable(); 23758579a185SWill Deacon cpu_hyp_init_context(); 2376bfa79a80SQuentin Perret ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size, 2377bfa79a80SQuentin Perret num_possible_cpus(), kern_hyp_va(per_cpu_base), 2378bfa79a80SQuentin Perret hyp_va_bits); 23798579a185SWill Deacon cpu_hyp_init_features(); 23808579a185SWill Deacon 23818579a185SWill Deacon /* 23828579a185SWill Deacon * The stub hypercalls are now disabled, so set our local flag to 23838579a185SWill Deacon * prevent a later re-init attempt in kvm_arch_hardware_enable(). 23848579a185SWill Deacon */ 2385733c758eSOliver Upton __this_cpu_write(kvm_hyp_initialized, 1); 2386bfa79a80SQuentin Perret preempt_enable(); 2387bfa79a80SQuentin Perret 2388bfa79a80SQuentin Perret return ret; 2389bfa79a80SQuentin Perret } 2390bfa79a80SQuentin Perret 2391e8162521SFuad Tabba static u64 get_hyp_id_aa64pfr0_el1(void) 2392e8162521SFuad Tabba { 2393e8162521SFuad Tabba /* 2394e8162521SFuad Tabba * Track whether the system isn't affected by spectre/meltdown in the 2395e8162521SFuad Tabba * hypervisor's view of id_aa64pfr0_el1, used for protected VMs. 2396e8162521SFuad Tabba * Although this is per-CPU, we make it global for simplicity, e.g., not 2397e8162521SFuad Tabba * to have to worry about vcpu migration. 2398e8162521SFuad Tabba * 2399e8162521SFuad Tabba * Unlike for non-protected VMs, userspace cannot override this for 2400e8162521SFuad Tabba * protected VMs. 2401e8162521SFuad Tabba */ 2402e8162521SFuad Tabba u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 2403e8162521SFuad Tabba 2404e8162521SFuad Tabba val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) | 2405e8162521SFuad Tabba ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3)); 2406e8162521SFuad Tabba 2407e8162521SFuad Tabba val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 2408e8162521SFuad Tabba arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED); 2409e8162521SFuad Tabba val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 2410e8162521SFuad Tabba arm64_get_meltdown_state() == SPECTRE_UNAFFECTED); 2411e8162521SFuad Tabba 2412e8162521SFuad Tabba return val; 2413e8162521SFuad Tabba } 2414e8162521SFuad Tabba 24156c165223SWill Deacon static void kvm_hyp_init_symbols(void) 2416bfa79a80SQuentin Perret { 2417e8162521SFuad Tabba kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1(); 24186c30bfb1SFuad Tabba kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1); 24196c30bfb1SFuad Tabba kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1); 24206c30bfb1SFuad Tabba kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 2421def8c222SVladimir Murzin kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 24227c419937SMarc Zyngier kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 24237c419937SMarc Zyngier kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 24246c30bfb1SFuad Tabba kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1); 24258669651cSQuentin Perret kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1); 242613e248aaSWill Deacon kvm_nvhe_sym(__icache_flags) = __icache_flags; 242773f38ef2SWill Deacon kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits; 24286c165223SWill Deacon } 24296c165223SWill Deacon 243053bf620aSSean Christopherson static int __init kvm_hyp_init_protection(u32 hyp_va_bits) 24316c165223SWill Deacon { 24326c165223SWill Deacon void *addr = phys_to_virt(hyp_mem_base); 24336c165223SWill Deacon int ret; 24347c419937SMarc Zyngier 2435bfa79a80SQuentin Perret ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP); 2436bfa79a80SQuentin Perret if (ret) 2437bfa79a80SQuentin Perret return ret; 2438bfa79a80SQuentin Perret 2439bfa79a80SQuentin Perret ret = do_pkvm_init(hyp_va_bits); 2440bfa79a80SQuentin Perret if (ret) 2441bfa79a80SQuentin Perret return ret; 2442bfa79a80SQuentin Perret 2443bfa79a80SQuentin Perret free_hyp_pgds(); 2444bfa79a80SQuentin Perret 2445bfa79a80SQuentin Perret return 0; 2446bfa79a80SQuentin Perret } 2447bfa79a80SQuentin Perret 244866d5b53eSFuad Tabba static int init_pkvm_host_sve_state(void) 244966d5b53eSFuad Tabba { 245066d5b53eSFuad Tabba int cpu; 245166d5b53eSFuad Tabba 245266d5b53eSFuad Tabba if (!system_supports_sve()) 245366d5b53eSFuad Tabba return 0; 245466d5b53eSFuad Tabba 245566d5b53eSFuad Tabba /* Allocate pages for host sve state in protected mode. */ 245666d5b53eSFuad Tabba for_each_possible_cpu(cpu) { 245766d5b53eSFuad Tabba struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order()); 245866d5b53eSFuad Tabba 245966d5b53eSFuad Tabba if (!page) 246066d5b53eSFuad Tabba return -ENOMEM; 246166d5b53eSFuad Tabba 246266d5b53eSFuad Tabba per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = page_address(page); 246366d5b53eSFuad Tabba } 246466d5b53eSFuad Tabba 246566d5b53eSFuad Tabba /* 246666d5b53eSFuad Tabba * Don't map the pages in hyp since these are only used in protected 246766d5b53eSFuad Tabba * mode, which will (re)create its own mapping when initialized. 246866d5b53eSFuad Tabba */ 246966d5b53eSFuad Tabba 247066d5b53eSFuad Tabba return 0; 247166d5b53eSFuad Tabba } 247266d5b53eSFuad Tabba 247366d5b53eSFuad Tabba /* 247466d5b53eSFuad Tabba * Finalizes the initialization of hyp mode, once everything else is initialized 247566d5b53eSFuad Tabba * and the initialziation process cannot fail. 247666d5b53eSFuad Tabba */ 247766d5b53eSFuad Tabba static void finalize_init_hyp_mode(void) 247866d5b53eSFuad Tabba { 247966d5b53eSFuad Tabba int cpu; 248066d5b53eSFuad Tabba 24811696fc21SFuad Tabba if (system_supports_sve() && is_protected_kvm_enabled()) { 248266d5b53eSFuad Tabba for_each_possible_cpu(cpu) { 248366d5b53eSFuad Tabba struct cpu_sve_state *sve_state; 248466d5b53eSFuad Tabba 248566d5b53eSFuad Tabba sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state; 24861696fc21SFuad Tabba per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = 24871696fc21SFuad Tabba kern_hyp_va(sve_state); 24881696fc21SFuad Tabba } 24891696fc21SFuad Tabba } else { 24901696fc21SFuad Tabba for_each_possible_cpu(cpu) { 24911696fc21SFuad Tabba struct user_fpsimd_state *fpsimd_state; 24921696fc21SFuad Tabba 24931696fc21SFuad Tabba fpsimd_state = &per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->host_ctxt.fp_regs; 24941696fc21SFuad Tabba per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->fpsimd_state = 24951696fc21SFuad Tabba kern_hyp_va(fpsimd_state); 24961696fc21SFuad Tabba } 249766d5b53eSFuad Tabba } 249866d5b53eSFuad Tabba } 249966d5b53eSFuad Tabba 25008c15c2a0SMostafa Saleh static void pkvm_hyp_init_ptrauth(void) 25018c15c2a0SMostafa Saleh { 25028c15c2a0SMostafa Saleh struct kvm_cpu_context *hyp_ctxt; 25038c15c2a0SMostafa Saleh int cpu; 25048c15c2a0SMostafa Saleh 25058c15c2a0SMostafa Saleh for_each_possible_cpu(cpu) { 25068c15c2a0SMostafa Saleh hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu); 25078c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long(); 25088c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long(); 25098c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long(); 25108c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long(); 25118c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long(); 25128c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long(); 25138c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long(); 25148c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long(); 25158c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long(); 25168c15c2a0SMostafa Saleh hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long(); 25178c15c2a0SMostafa Saleh } 25188c15c2a0SMostafa Saleh } 25198c15c2a0SMostafa Saleh 252067d953d4SMarc Zyngier /* Inits Hyp-mode on all online CPUs */ 252153bf620aSSean Christopherson static int __init init_hyp_mode(void) 25229ed24f4bSMarc Zyngier { 2523bfa79a80SQuentin Perret u32 hyp_va_bits; 25249ed24f4bSMarc Zyngier int cpu; 2525bfa79a80SQuentin Perret int err = -ENOMEM; 2526bfa79a80SQuentin Perret 2527bfa79a80SQuentin Perret /* 2528bfa79a80SQuentin Perret * The protected Hyp-mode cannot be initialized if the memory pool 2529bfa79a80SQuentin Perret * allocation has failed. 2530bfa79a80SQuentin Perret */ 2531bfa79a80SQuentin Perret if (is_protected_kvm_enabled() && !hyp_mem_base) 2532bfa79a80SQuentin Perret goto out_err; 25339ed24f4bSMarc Zyngier 25349ed24f4bSMarc Zyngier /* 25359ed24f4bSMarc Zyngier * Allocate Hyp PGD and setup Hyp identity mapping 25369ed24f4bSMarc Zyngier */ 2537bfa79a80SQuentin Perret err = kvm_mmu_init(&hyp_va_bits); 25389ed24f4bSMarc Zyngier if (err) 25399ed24f4bSMarc Zyngier goto out_err; 25409ed24f4bSMarc Zyngier 25419ed24f4bSMarc Zyngier /* 25429ed24f4bSMarc Zyngier * Allocate stack pages for Hypervisor-mode 25439ed24f4bSMarc Zyngier */ 25449ed24f4bSMarc Zyngier for_each_possible_cpu(cpu) { 25459ed24f4bSMarc Zyngier unsigned long stack_page; 25469ed24f4bSMarc Zyngier 25479ed24f4bSMarc Zyngier stack_page = __get_free_page(GFP_KERNEL); 25489ed24f4bSMarc Zyngier if (!stack_page) { 25499ed24f4bSMarc Zyngier err = -ENOMEM; 25509ed24f4bSMarc Zyngier goto out_err; 25519ed24f4bSMarc Zyngier } 25529ed24f4bSMarc Zyngier 25539ed24f4bSMarc Zyngier per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; 25549ed24f4bSMarc Zyngier } 25559ed24f4bSMarc Zyngier 25569ed24f4bSMarc Zyngier /* 255730c95391SDavid Brazdil * Allocate and initialize pages for Hypervisor-mode percpu regions. 255830c95391SDavid Brazdil */ 255930c95391SDavid Brazdil for_each_possible_cpu(cpu) { 256030c95391SDavid Brazdil struct page *page; 256130c95391SDavid Brazdil void *page_addr; 256230c95391SDavid Brazdil 256330c95391SDavid Brazdil page = alloc_pages(GFP_KERNEL, nvhe_percpu_order()); 256430c95391SDavid Brazdil if (!page) { 256530c95391SDavid Brazdil err = -ENOMEM; 256630c95391SDavid Brazdil goto out_err; 256730c95391SDavid Brazdil } 256830c95391SDavid Brazdil 256930c95391SDavid Brazdil page_addr = page_address(page); 257030c95391SDavid Brazdil memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size()); 2571fe41a7f8SQuentin Perret kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr; 257230c95391SDavid Brazdil } 257330c95391SDavid Brazdil 257430c95391SDavid Brazdil /* 25759ed24f4bSMarc Zyngier * Map the Hyp-code called directly from the host 25769ed24f4bSMarc Zyngier */ 25779ed24f4bSMarc Zyngier err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), 25789ed24f4bSMarc Zyngier kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); 25799ed24f4bSMarc Zyngier if (err) { 25809ed24f4bSMarc Zyngier kvm_err("Cannot map world-switch code\n"); 25819ed24f4bSMarc Zyngier goto out_err; 25829ed24f4bSMarc Zyngier } 25839ed24f4bSMarc Zyngier 258416174eeaSDavid Brazdil err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), 258516174eeaSDavid Brazdil kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); 25862d7bf218SDavid Brazdil if (err) { 258716174eeaSDavid Brazdil kvm_err("Cannot map .hyp.rodata section\n"); 25882d7bf218SDavid Brazdil goto out_err; 25892d7bf218SDavid Brazdil } 25902d7bf218SDavid Brazdil 25919ed24f4bSMarc Zyngier err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), 25929ed24f4bSMarc Zyngier kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); 25939ed24f4bSMarc Zyngier if (err) { 25949ed24f4bSMarc Zyngier kvm_err("Cannot map rodata section\n"); 25959ed24f4bSMarc Zyngier goto out_err; 25969ed24f4bSMarc Zyngier } 25979ed24f4bSMarc Zyngier 2598380e18adSQuentin Perret /* 2599380e18adSQuentin Perret * .hyp.bss is guaranteed to be placed at the beginning of the .bss 2600380e18adSQuentin Perret * section thanks to an assertion in the linker script. Map it RW and 2601380e18adSQuentin Perret * the rest of .bss RO. 2602380e18adSQuentin Perret */ 2603380e18adSQuentin Perret err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start), 2604380e18adSQuentin Perret kvm_ksym_ref(__hyp_bss_end), PAGE_HYP); 2605380e18adSQuentin Perret if (err) { 2606380e18adSQuentin Perret kvm_err("Cannot map hyp bss section: %d\n", err); 2607380e18adSQuentin Perret goto out_err; 2608380e18adSQuentin Perret } 2609380e18adSQuentin Perret 2610380e18adSQuentin Perret err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end), 26119ed24f4bSMarc Zyngier kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); 26129ed24f4bSMarc Zyngier if (err) { 26139ed24f4bSMarc Zyngier kvm_err("Cannot map bss section\n"); 26149ed24f4bSMarc Zyngier goto out_err; 26159ed24f4bSMarc Zyngier } 26169ed24f4bSMarc Zyngier 26179ed24f4bSMarc Zyngier /* 26189ed24f4bSMarc Zyngier * Map the Hyp stack pages 26199ed24f4bSMarc Zyngier */ 26209ed24f4bSMarc Zyngier for_each_possible_cpu(cpu) { 2621ce335431SKalesh Singh struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 26229ed24f4bSMarc Zyngier char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); 26239ed24f4bSMarc Zyngier 2624f156a7d1SVincent Donnefort err = create_hyp_stack(__pa(stack_page), ¶ms->stack_hyp_va); 26259ed24f4bSMarc Zyngier if (err) { 26269ed24f4bSMarc Zyngier kvm_err("Cannot map hyp stack\n"); 26279ed24f4bSMarc Zyngier goto out_err; 26289ed24f4bSMarc Zyngier } 2629ce335431SKalesh Singh 2630ce335431SKalesh Singh /* 2631ce335431SKalesh Singh * Save the stack PA in nvhe_init_params. This will be needed 2632ce335431SKalesh Singh * to recreate the stack mapping in protected nVHE mode. 2633ce335431SKalesh Singh * __hyp_pa() won't do the right thing there, since the stack 2634ce335431SKalesh Singh * has been mapped in the flexible private VA space. 2635ce335431SKalesh Singh */ 2636ce335431SKalesh Singh params->stack_pa = __pa(stack_page); 26379ed24f4bSMarc Zyngier } 26389ed24f4bSMarc Zyngier 26399ed24f4bSMarc Zyngier for_each_possible_cpu(cpu) { 2640fe41a7f8SQuentin Perret char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]; 264130c95391SDavid Brazdil char *percpu_end = percpu_begin + nvhe_percpu_size(); 26429ed24f4bSMarc Zyngier 26439cc77581SQuentin Perret /* Map Hyp percpu pages */ 264430c95391SDavid Brazdil err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP); 26459ed24f4bSMarc Zyngier if (err) { 264630c95391SDavid Brazdil kvm_err("Cannot map hyp percpu region\n"); 26476e3bfbb2SAndrew Scull goto out_err; 26486e3bfbb2SAndrew Scull } 26499cc77581SQuentin Perret 26509cc77581SQuentin Perret /* Prepare the CPU initialization parameters */ 2651579d7ebeSRyan Roberts cpu_prepare_hyp_mode(cpu, hyp_va_bits); 26529ed24f4bSMarc Zyngier } 26539ed24f4bSMarc Zyngier 26546c165223SWill Deacon kvm_hyp_init_symbols(); 26556c165223SWill Deacon 2656eeeee719SDavid Brazdil if (is_protected_kvm_enabled()) { 26578c15c2a0SMostafa Saleh if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) && 2658d8569fbaSMark Rutland cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH)) 26598c15c2a0SMostafa Saleh pkvm_hyp_init_ptrauth(); 26608c15c2a0SMostafa Saleh 266194f5e8a4SDavid Brazdil init_cpu_logical_map(); 266294f5e8a4SDavid Brazdil 266352b9e265SWang Wensheng if (!init_psci_relay()) { 266452b9e265SWang Wensheng err = -ENODEV; 2665eeeee719SDavid Brazdil goto out_err; 2666eeeee719SDavid Brazdil } 2667eeeee719SDavid Brazdil 266866d5b53eSFuad Tabba err = init_pkvm_host_sve_state(); 266966d5b53eSFuad Tabba if (err) 267066d5b53eSFuad Tabba goto out_err; 267166d5b53eSFuad Tabba 2672bfa79a80SQuentin Perret err = kvm_hyp_init_protection(hyp_va_bits); 2673bfa79a80SQuentin Perret if (err) { 2674bfa79a80SQuentin Perret kvm_err("Failed to init hyp memory protection\n"); 2675bfa79a80SQuentin Perret goto out_err; 2676bfa79a80SQuentin Perret } 2677bfa79a80SQuentin Perret } 26789ed24f4bSMarc Zyngier 26799ed24f4bSMarc Zyngier return 0; 26809ed24f4bSMarc Zyngier 26819ed24f4bSMarc Zyngier out_err: 26829ed24f4bSMarc Zyngier teardown_hyp_mode(); 26839ed24f4bSMarc Zyngier kvm_err("error initializing Hyp mode: %d\n", err); 26849ed24f4bSMarc Zyngier return err; 26859ed24f4bSMarc Zyngier } 26869ed24f4bSMarc Zyngier 26879ed24f4bSMarc Zyngier struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) 26889ed24f4bSMarc Zyngier { 2689ce5d2448SOliver Upton struct kvm_vcpu *vcpu = NULL; 2690ce5d2448SOliver Upton struct kvm_mpidr_data *data; 269146808a4cSMarc Zyngier unsigned long i; 26929ed24f4bSMarc Zyngier 26939ed24f4bSMarc Zyngier mpidr &= MPIDR_HWID_BITMASK; 269454a8006dSMarc Zyngier 2695ce5d2448SOliver Upton rcu_read_lock(); 2696ce5d2448SOliver Upton data = rcu_dereference(kvm->arch.mpidr_data); 269754a8006dSMarc Zyngier 2698ce5d2448SOliver Upton if (data) { 2699ce5d2448SOliver Upton u16 idx = kvm_mpidr_index(data, mpidr); 2700ce5d2448SOliver Upton 2701ce5d2448SOliver Upton vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]); 270254a8006dSMarc Zyngier if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu)) 270354a8006dSMarc Zyngier vcpu = NULL; 270454a8006dSMarc Zyngier } 270554a8006dSMarc Zyngier 2706ce5d2448SOliver Upton rcu_read_unlock(); 2707ce5d2448SOliver Upton 2708ce5d2448SOliver Upton if (vcpu) 2709ce5d2448SOliver Upton return vcpu; 2710ce5d2448SOliver Upton 27119ed24f4bSMarc Zyngier kvm_for_each_vcpu(i, vcpu, kvm) { 27129ed24f4bSMarc Zyngier if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) 27139ed24f4bSMarc Zyngier return vcpu; 27149ed24f4bSMarc Zyngier } 27159ed24f4bSMarc Zyngier return NULL; 27169ed24f4bSMarc Zyngier } 27179ed24f4bSMarc Zyngier 2718d663b8a2SPaolo Bonzini bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 2719d663b8a2SPaolo Bonzini { 2720d663b8a2SPaolo Bonzini return irqchip_in_kernel(kvm); 2721d663b8a2SPaolo Bonzini } 2722d663b8a2SPaolo Bonzini 27239ed24f4bSMarc Zyngier bool kvm_arch_has_irq_bypass(void) 27249ed24f4bSMarc Zyngier { 27259ed24f4bSMarc Zyngier return true; 27269ed24f4bSMarc Zyngier } 27279ed24f4bSMarc Zyngier 27289ed24f4bSMarc Zyngier int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 27299ed24f4bSMarc Zyngier struct irq_bypass_producer *prod) 27309ed24f4bSMarc Zyngier { 27319ed24f4bSMarc Zyngier struct kvm_kernel_irqfd *irqfd = 27329ed24f4bSMarc Zyngier container_of(cons, struct kvm_kernel_irqfd, consumer); 27339ed24f4bSMarc Zyngier 27349ed24f4bSMarc Zyngier return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, 27359ed24f4bSMarc Zyngier &irqfd->irq_entry); 27369ed24f4bSMarc Zyngier } 27379ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 27389ed24f4bSMarc Zyngier struct irq_bypass_producer *prod) 27399ed24f4bSMarc Zyngier { 27409ed24f4bSMarc Zyngier struct kvm_kernel_irqfd *irqfd = 27419ed24f4bSMarc Zyngier container_of(cons, struct kvm_kernel_irqfd, consumer); 27429ed24f4bSMarc Zyngier 27439ed24f4bSMarc Zyngier kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq, 27449ed24f4bSMarc Zyngier &irqfd->irq_entry); 27459ed24f4bSMarc Zyngier } 27469ed24f4bSMarc Zyngier 27479ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) 27489ed24f4bSMarc Zyngier { 27499ed24f4bSMarc Zyngier struct kvm_kernel_irqfd *irqfd = 27509ed24f4bSMarc Zyngier container_of(cons, struct kvm_kernel_irqfd, consumer); 27519ed24f4bSMarc Zyngier 27529ed24f4bSMarc Zyngier kvm_arm_halt_guest(irqfd->kvm); 27539ed24f4bSMarc Zyngier } 27549ed24f4bSMarc Zyngier 27559ed24f4bSMarc Zyngier void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) 27569ed24f4bSMarc Zyngier { 27579ed24f4bSMarc Zyngier struct kvm_kernel_irqfd *irqfd = 27589ed24f4bSMarc Zyngier container_of(cons, struct kvm_kernel_irqfd, consumer); 27599ed24f4bSMarc Zyngier 27609ed24f4bSMarc Zyngier kvm_arm_resume_guest(irqfd->kvm); 27619ed24f4bSMarc Zyngier } 27629ed24f4bSMarc Zyngier 276367d953d4SMarc Zyngier /* Initialize Hyp-mode and memory mappings on all CPUs */ 276453bf620aSSean Christopherson static __init int kvm_arm_init(void) 27659ed24f4bSMarc Zyngier { 27669ed24f4bSMarc Zyngier int err; 27679ed24f4bSMarc Zyngier bool in_hyp_mode; 27689ed24f4bSMarc Zyngier 27699ed24f4bSMarc Zyngier if (!is_hyp_mode_available()) { 27709ed24f4bSMarc Zyngier kvm_info("HYP mode not available\n"); 27719ed24f4bSMarc Zyngier return -ENODEV; 27729ed24f4bSMarc Zyngier } 27739ed24f4bSMarc Zyngier 2774b6a68b97SMarc Zyngier if (kvm_get_mode() == KVM_MODE_NONE) { 2775b6a68b97SMarc Zyngier kvm_info("KVM disabled from command line\n"); 2776b6a68b97SMarc Zyngier return -ENODEV; 2777b6a68b97SMarc Zyngier } 2778b6a68b97SMarc Zyngier 2779f1f0c0cfSAlexandru Elisei err = kvm_sys_reg_table_init(); 2780f1f0c0cfSAlexandru Elisei if (err) { 2781f1f0c0cfSAlexandru Elisei kvm_info("Error initializing system register tables"); 2782f1f0c0cfSAlexandru Elisei return err; 2783f1f0c0cfSAlexandru Elisei } 2784f1f0c0cfSAlexandru Elisei 27859ed24f4bSMarc Zyngier in_hyp_mode = is_kernel_in_hyp_mode(); 27869ed24f4bSMarc Zyngier 278796d389caSRob Herring if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || 278896d389caSRob Herring cpus_have_final_cap(ARM64_WORKAROUND_1508412)) 2789abf532ccSRob Herring kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \ 2790abf532ccSRob Herring "Only trusted guests should be used on this system.\n"); 2791abf532ccSRob Herring 2792bf249d9eSAnshuman Khandual err = kvm_set_ipa_limit(); 27939ed24f4bSMarc Zyngier if (err) 27949ed24f4bSMarc Zyngier return err; 27959ed24f4bSMarc Zyngier 27969ed24f4bSMarc Zyngier err = kvm_arm_init_sve(); 27979ed24f4bSMarc Zyngier if (err) 27989ed24f4bSMarc Zyngier return err; 27999ed24f4bSMarc Zyngier 28003248136bSJulien Grall err = kvm_arm_vmid_alloc_init(); 28013248136bSJulien Grall if (err) { 28023248136bSJulien Grall kvm_err("Failed to initialize VMID allocator.\n"); 28033248136bSJulien Grall return err; 28043248136bSJulien Grall } 28053248136bSJulien Grall 28069ed24f4bSMarc Zyngier if (!in_hyp_mode) { 28079ed24f4bSMarc Zyngier err = init_hyp_mode(); 28089ed24f4bSMarc Zyngier if (err) 28099ed24f4bSMarc Zyngier goto out_err; 28109ed24f4bSMarc Zyngier } 28119ed24f4bSMarc Zyngier 2812b881cdceSWill Deacon err = kvm_init_vector_slots(); 2813b881cdceSWill Deacon if (err) { 2814b881cdceSWill Deacon kvm_err("Cannot initialise vector slots\n"); 28156baaeda8SSean Christopherson goto out_hyp; 2816b881cdceSWill Deacon } 2817b881cdceSWill Deacon 28189ed24f4bSMarc Zyngier err = init_subsystems(); 28199ed24f4bSMarc Zyngier if (err) 282078b3bf48SSean Christopherson goto out_hyp; 28219ed24f4bSMarc Zyngier 2822d96c66abSMarc Zyngier kvm_info("%s%sVHE mode initialized successfully\n", 2823d96c66abSMarc Zyngier in_hyp_mode ? "" : (is_protected_kvm_enabled() ? 2824d96c66abSMarc Zyngier "Protected " : "Hyp "), 2825d96c66abSMarc Zyngier in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ? 2826d96c66abSMarc Zyngier "h" : "n")); 28279ed24f4bSMarc Zyngier 28281dc0f02dSSean Christopherson /* 28291dc0f02dSSean Christopherson * FIXME: Do something reasonable if kvm_init() fails after pKVM 28301dc0f02dSSean Christopherson * hypervisor protection is finalized. 28311dc0f02dSSean Christopherson */ 283281a1cf9fSSean Christopherson err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE); 28331dc0f02dSSean Christopherson if (err) 28341dc0f02dSSean Christopherson goto out_subs; 28351dc0f02dSSean Christopherson 283666d5b53eSFuad Tabba /* 283766d5b53eSFuad Tabba * This should be called after initialization is done and failure isn't 283866d5b53eSFuad Tabba * possible anymore. 283966d5b53eSFuad Tabba */ 284066d5b53eSFuad Tabba if (!in_hyp_mode) 284166d5b53eSFuad Tabba finalize_init_hyp_mode(); 284266d5b53eSFuad Tabba 2843fa729bc7SSudeep Holla kvm_arm_initialised = true; 2844fa729bc7SSudeep Holla 28459ed24f4bSMarc Zyngier return 0; 28469ed24f4bSMarc Zyngier 28476baaeda8SSean Christopherson out_subs: 284878b3bf48SSean Christopherson teardown_subsystems(); 28496baaeda8SSean Christopherson out_hyp: 28509ed24f4bSMarc Zyngier if (!in_hyp_mode) 28519ed24f4bSMarc Zyngier teardown_hyp_mode(); 28529ed24f4bSMarc Zyngier out_err: 28533248136bSJulien Grall kvm_arm_vmid_alloc_free(); 28549ed24f4bSMarc Zyngier return err; 28559ed24f4bSMarc Zyngier } 28569ed24f4bSMarc Zyngier 2857d8b369c4SDavid Brazdil static int __init early_kvm_mode_cfg(char *arg) 2858d8b369c4SDavid Brazdil { 2859d8b369c4SDavid Brazdil if (!arg) 2860d8b369c4SDavid Brazdil return -EINVAL; 2861d8b369c4SDavid Brazdil 2862b2a4d007SElliot Berman if (strcmp(arg, "none") == 0) { 2863b2a4d007SElliot Berman kvm_mode = KVM_MODE_NONE; 2864b2a4d007SElliot Berman return 0; 2865b2a4d007SElliot Berman } 2866b2a4d007SElliot Berman 2867b2a4d007SElliot Berman if (!is_hyp_mode_available()) { 2868b2a4d007SElliot Berman pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n"); 2869b2a4d007SElliot Berman return 0; 2870b2a4d007SElliot Berman } 2871b2a4d007SElliot Berman 2872d8b369c4SDavid Brazdil if (strcmp(arg, "protected") == 0) { 2873cde5042aSWill Deacon if (!is_kernel_in_hyp_mode()) 2874d8b369c4SDavid Brazdil kvm_mode = KVM_MODE_PROTECTED; 2875cde5042aSWill Deacon else 2876cde5042aSWill Deacon pr_warn_once("Protected KVM not available with VHE\n"); 2877cde5042aSWill Deacon 2878d8b369c4SDavid Brazdil return 0; 2879d8b369c4SDavid Brazdil } 2880d8b369c4SDavid Brazdil 2881b6a68b97SMarc Zyngier if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) { 2882b6a68b97SMarc Zyngier kvm_mode = KVM_MODE_DEFAULT; 28831945a067SMarc Zyngier return 0; 2884b6a68b97SMarc Zyngier } 2885b6a68b97SMarc Zyngier 2886675cabc8SJintack Lim if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) { 2887675cabc8SJintack Lim kvm_mode = KVM_MODE_NV; 2888675cabc8SJintack Lim return 0; 2889675cabc8SJintack Lim } 2890675cabc8SJintack Lim 2891d8b369c4SDavid Brazdil return -EINVAL; 2892d8b369c4SDavid Brazdil } 2893d8b369c4SDavid Brazdil early_param("kvm-arm.mode", early_kvm_mode_cfg); 2894d8b369c4SDavid Brazdil 28950b5afe05SColton Lewis static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p) 28960b5afe05SColton Lewis { 28970b5afe05SColton Lewis if (!arg) 28980b5afe05SColton Lewis return -EINVAL; 28990b5afe05SColton Lewis 29000b5afe05SColton Lewis if (strcmp(arg, "trap") == 0) { 29010b5afe05SColton Lewis *p = KVM_WFX_TRAP; 29020b5afe05SColton Lewis return 0; 29030b5afe05SColton Lewis } 29040b5afe05SColton Lewis 29050b5afe05SColton Lewis if (strcmp(arg, "notrap") == 0) { 29060b5afe05SColton Lewis *p = KVM_WFX_NOTRAP; 29070b5afe05SColton Lewis return 0; 29080b5afe05SColton Lewis } 29090b5afe05SColton Lewis 29100b5afe05SColton Lewis return -EINVAL; 29110b5afe05SColton Lewis } 29120b5afe05SColton Lewis 29130b5afe05SColton Lewis static int __init early_kvm_wfi_trap_policy_cfg(char *arg) 29140b5afe05SColton Lewis { 29150b5afe05SColton Lewis return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy); 29160b5afe05SColton Lewis } 29170b5afe05SColton Lewis early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg); 29180b5afe05SColton Lewis 29190b5afe05SColton Lewis static int __init early_kvm_wfe_trap_policy_cfg(char *arg) 29200b5afe05SColton Lewis { 29210b5afe05SColton Lewis return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy); 29220b5afe05SColton Lewis } 29230b5afe05SColton Lewis early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg); 29240b5afe05SColton Lewis 29253eb681fbSDavid Brazdil enum kvm_mode kvm_get_mode(void) 29263eb681fbSDavid Brazdil { 29273eb681fbSDavid Brazdil return kvm_mode; 29283eb681fbSDavid Brazdil } 29293eb681fbSDavid Brazdil 29301dc0f02dSSean Christopherson module_init(kvm_arm_init); 2931