109cf57ebSDavid Brazdil // SPDX-License-Identifier: GPL-2.0-only 209cf57ebSDavid Brazdil /* 309cf57ebSDavid Brazdil * Copyright (C) 2015 - ARM Ltd 409cf57ebSDavid Brazdil * Author: Marc Zyngier <marc.zyngier@arm.com> 509cf57ebSDavid Brazdil */ 609cf57ebSDavid Brazdil 709cf57ebSDavid Brazdil #include <hyp/switch.h> 809cf57ebSDavid Brazdil 909cf57ebSDavid Brazdil #include <linux/arm-smccc.h> 1009cf57ebSDavid Brazdil #include <linux/kvm_host.h> 1109cf57ebSDavid Brazdil #include <linux/types.h> 1209cf57ebSDavid Brazdil #include <linux/jump_label.h> 13bd09128dSJames Morse #include <linux/percpu.h> 1409cf57ebSDavid Brazdil #include <uapi/linux/psci.h> 1509cf57ebSDavid Brazdil 1609cf57ebSDavid Brazdil #include <kvm/arm_psci.h> 1709cf57ebSDavid Brazdil 1809cf57ebSDavid Brazdil #include <asm/barrier.h> 1909cf57ebSDavid Brazdil #include <asm/cpufeature.h> 2009cf57ebSDavid Brazdil #include <asm/kprobes.h> 2109cf57ebSDavid Brazdil #include <asm/kvm_asm.h> 2209cf57ebSDavid Brazdil #include <asm/kvm_emulate.h> 2309cf57ebSDavid Brazdil #include <asm/kvm_hyp.h> 2409cf57ebSDavid Brazdil #include <asm/kvm_mmu.h> 2509cf57ebSDavid Brazdil #include <asm/fpsimd.h> 2609cf57ebSDavid Brazdil #include <asm/debug-monitors.h> 2709cf57ebSDavid Brazdil #include <asm/processor.h> 28bd09128dSJames Morse #include <asm/thread_info.h> 29bd09128dSJames Morse #include <asm/vectors.h> 3009cf57ebSDavid Brazdil 3114ef9d04SMarc Zyngier /* VHE specific context */ 3214ef9d04SMarc Zyngier DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); 3314ef9d04SMarc Zyngier DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); 3414ef9d04SMarc Zyngier DEFINE_PER_CPU(unsigned long, kvm_hyp_vector); 352a1198c9SDavid Brazdil 3609cf57ebSDavid Brazdil static void __activate_traps(struct kvm_vcpu *vcpu) 3709cf57ebSDavid Brazdil { 3809cf57ebSDavid Brazdil u64 val; 3909cf57ebSDavid Brazdil 4009cf57ebSDavid Brazdil ___activate_traps(vcpu); 4109cf57ebSDavid Brazdil 4209cf57ebSDavid Brazdil val = read_sysreg(cpacr_el1); 4309cf57ebSDavid Brazdil val |= CPACR_EL1_TTA; 44*51729fb1SMark Brown val &= ~(CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN | 45*51729fb1SMark Brown CPACR_EL1_SMEN_EL0EN | CPACR_EL1_SMEN_EL1EN); 4609cf57ebSDavid Brazdil 4709cf57ebSDavid Brazdil /* 4809cf57ebSDavid Brazdil * With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to 4909cf57ebSDavid Brazdil * CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2, 5009cf57ebSDavid Brazdil * except for some missing controls, such as TAM. 5109cf57ebSDavid Brazdil * In this case, CPTR_EL2.TAM has the same position with or without 5209cf57ebSDavid Brazdil * VHE (HCR.E2H == 1) which allows us to use here the CPTR_EL2.TAM 5309cf57ebSDavid Brazdil * shift value for trapping the AMU accesses. 5409cf57ebSDavid Brazdil */ 5509cf57ebSDavid Brazdil 5609cf57ebSDavid Brazdil val |= CPTR_EL2_TAM; 5709cf57ebSDavid Brazdil 5809cf57ebSDavid Brazdil if (update_fp_enabled(vcpu)) { 5909cf57ebSDavid Brazdil if (vcpu_has_sve(vcpu)) 603bb72d86SMark Brown val |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN; 6109cf57ebSDavid Brazdil } else { 623bb72d86SMark Brown val &= ~(CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN); 6309cf57ebSDavid Brazdil __activate_traps_fpsimd32(vcpu); 6409cf57ebSDavid Brazdil } 6509cf57ebSDavid Brazdil 66*51729fb1SMark Brown if (cpus_have_final_cap(ARM64_SME)) 67*51729fb1SMark Brown write_sysreg(read_sysreg(sctlr_el2) & ~SCTLR_ELx_ENTP2, 68*51729fb1SMark Brown sctlr_el2); 69*51729fb1SMark Brown 7009cf57ebSDavid Brazdil write_sysreg(val, cpacr_el1); 7109cf57ebSDavid Brazdil 72a0e47952SAndrew Scull write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el1); 7309cf57ebSDavid Brazdil } 7409cf57ebSDavid Brazdil NOKPROBE_SYMBOL(__activate_traps); 7509cf57ebSDavid Brazdil 7609cf57ebSDavid Brazdil static void __deactivate_traps(struct kvm_vcpu *vcpu) 7709cf57ebSDavid Brazdil { 78bd09128dSJames Morse const char *host_vectors = vectors; 7909cf57ebSDavid Brazdil 8009cf57ebSDavid Brazdil ___deactivate_traps(vcpu); 8109cf57ebSDavid Brazdil 8209cf57ebSDavid Brazdil write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2); 8309cf57ebSDavid Brazdil 8409cf57ebSDavid Brazdil /* 8509cf57ebSDavid Brazdil * ARM errata 1165522 and 1530923 require the actual execution of the 8609cf57ebSDavid Brazdil * above before we can switch to the EL2/EL0 translation regime used by 8709cf57ebSDavid Brazdil * the host. 8809cf57ebSDavid Brazdil */ 8909cf57ebSDavid Brazdil asm(ALTERNATIVE("nop", "isb", ARM64_WORKAROUND_SPECULATIVE_AT)); 9009cf57ebSDavid Brazdil 91*51729fb1SMark Brown if (cpus_have_final_cap(ARM64_SME)) 92*51729fb1SMark Brown write_sysreg(read_sysreg(sctlr_el2) | SCTLR_ELx_ENTP2, 93*51729fb1SMark Brown sctlr_el2); 94*51729fb1SMark Brown 9509cf57ebSDavid Brazdil write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1); 96bd09128dSJames Morse 97bd09128dSJames Morse if (!arm64_kernel_unmapped_at_el0()) 98bd09128dSJames Morse host_vectors = __this_cpu_read(this_cpu_vector); 99bd09128dSJames Morse write_sysreg(host_vectors, vbar_el1); 10009cf57ebSDavid Brazdil } 10109cf57ebSDavid Brazdil NOKPROBE_SYMBOL(__deactivate_traps); 10209cf57ebSDavid Brazdil 10309cf57ebSDavid Brazdil void activate_traps_vhe_load(struct kvm_vcpu *vcpu) 10409cf57ebSDavid Brazdil { 10509cf57ebSDavid Brazdil __activate_traps_common(vcpu); 10609cf57ebSDavid Brazdil } 10709cf57ebSDavid Brazdil 1081460b4b2SFuad Tabba void deactivate_traps_vhe_put(struct kvm_vcpu *vcpu) 10909cf57ebSDavid Brazdil { 1101460b4b2SFuad Tabba __deactivate_traps_common(vcpu); 11109cf57ebSDavid Brazdil } 11209cf57ebSDavid Brazdil 1138fb20461SMarc Zyngier static const exit_handler_fn hyp_exit_handlers[] = { 1148fb20461SMarc Zyngier [0 ... ESR_ELx_EC_MAX] = NULL, 1158fb20461SMarc Zyngier [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32, 1168fb20461SMarc Zyngier [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg, 1178fb20461SMarc Zyngier [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd, 1188fb20461SMarc Zyngier [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd, 1198fb20461SMarc Zyngier [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low, 1208fb20461SMarc Zyngier [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low, 1218fb20461SMarc Zyngier [ESR_ELx_EC_PAC] = kvm_hyp_handle_ptrauth, 1228fb20461SMarc Zyngier }; 1238fb20461SMarc Zyngier 1240c7639ccSMarc Zyngier static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu) 1258fb20461SMarc Zyngier { 1268fb20461SMarc Zyngier return hyp_exit_handlers; 1278fb20461SMarc Zyngier } 1288fb20461SMarc Zyngier 1297183b2b5SMarc Zyngier static void early_exit_filter(struct kvm_vcpu *vcpu, u64 *exit_code) 1307183b2b5SMarc Zyngier { 1317183b2b5SMarc Zyngier } 1327183b2b5SMarc Zyngier 13309cf57ebSDavid Brazdil /* Switch to the guest for VHE systems running in EL2 */ 13409cf57ebSDavid Brazdil static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu) 13509cf57ebSDavid Brazdil { 13609cf57ebSDavid Brazdil struct kvm_cpu_context *host_ctxt; 13709cf57ebSDavid Brazdil struct kvm_cpu_context *guest_ctxt; 13809cf57ebSDavid Brazdil u64 exit_code; 13909cf57ebSDavid Brazdil 140717cf94aSDavid Brazdil host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt; 14109cf57ebSDavid Brazdil host_ctxt->__hyp_running_vcpu = vcpu; 14209cf57ebSDavid Brazdil guest_ctxt = &vcpu->arch.ctxt; 14309cf57ebSDavid Brazdil 14409cf57ebSDavid Brazdil sysreg_save_host_state_vhe(host_ctxt); 14509cf57ebSDavid Brazdil 14609cf57ebSDavid Brazdil /* 14709cf57ebSDavid Brazdil * ARM erratum 1165522 requires us to configure both stage 1 and 14809cf57ebSDavid Brazdil * stage 2 translation for the guest context before we clear 14909cf57ebSDavid Brazdil * HCR_EL2.TGE. 15009cf57ebSDavid Brazdil * 15109cf57ebSDavid Brazdil * We have already configured the guest's stage 1 translation in 152501a67a2SAndrew Scull * kvm_vcpu_load_sysregs_vhe above. We must now call 1534efc0edeSMarc Zyngier * __load_stage2 before __activate_traps, because 1544efc0edeSMarc Zyngier * __load_stage2 configures stage 2 translation, and 155501a67a2SAndrew Scull * __activate_traps clear HCR_EL2.TGE (among other things). 15609cf57ebSDavid Brazdil */ 1574efc0edeSMarc Zyngier __load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch); 15809cf57ebSDavid Brazdil __activate_traps(vcpu); 15909cf57ebSDavid Brazdil 160f5e30680SMarc Zyngier __kvm_adjust_pc(vcpu); 161cdb5e02eSMarc Zyngier 16209cf57ebSDavid Brazdil sysreg_restore_guest_state_vhe(guest_ctxt); 16309cf57ebSDavid Brazdil __debug_switch_to_guest(vcpu); 16409cf57ebSDavid Brazdil 16509cf57ebSDavid Brazdil do { 16609cf57ebSDavid Brazdil /* Jump in the fire! */ 167b619d9aaSAndrew Scull exit_code = __guest_enter(vcpu); 16809cf57ebSDavid Brazdil 16909cf57ebSDavid Brazdil /* And we're baaack! */ 17009cf57ebSDavid Brazdil } while (fixup_guest_exit(vcpu, &exit_code)); 17109cf57ebSDavid Brazdil 17209cf57ebSDavid Brazdil sysreg_save_guest_state_vhe(guest_ctxt); 17309cf57ebSDavid Brazdil 17409cf57ebSDavid Brazdil __deactivate_traps(vcpu); 17509cf57ebSDavid Brazdil 17609cf57ebSDavid Brazdil sysreg_restore_host_state_vhe(host_ctxt); 17709cf57ebSDavid Brazdil 17809cf57ebSDavid Brazdil if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) 17909cf57ebSDavid Brazdil __fpsimd_save_fpexc32(vcpu); 18009cf57ebSDavid Brazdil 18109cf57ebSDavid Brazdil __debug_switch_to_host(vcpu); 18209cf57ebSDavid Brazdil 18309cf57ebSDavid Brazdil return exit_code; 18409cf57ebSDavid Brazdil } 18509cf57ebSDavid Brazdil NOKPROBE_SYMBOL(__kvm_vcpu_run_vhe); 18609cf57ebSDavid Brazdil 18709cf57ebSDavid Brazdil int __kvm_vcpu_run(struct kvm_vcpu *vcpu) 18809cf57ebSDavid Brazdil { 18909cf57ebSDavid Brazdil int ret; 19009cf57ebSDavid Brazdil 19109cf57ebSDavid Brazdil local_daif_mask(); 19209cf57ebSDavid Brazdil 19309cf57ebSDavid Brazdil /* 19409cf57ebSDavid Brazdil * Having IRQs masked via PMR when entering the guest means the GIC 19509cf57ebSDavid Brazdil * will not signal the CPU of interrupts of lower priority, and the 19609cf57ebSDavid Brazdil * only way to get out will be via guest exceptions. 19709cf57ebSDavid Brazdil * Naturally, we want to avoid this. 19809cf57ebSDavid Brazdil * 19909cf57ebSDavid Brazdil * local_daif_mask() already sets GIC_PRIO_PSR_I_SET, we just need a 20009cf57ebSDavid Brazdil * dsb to ensure the redistributor is forwards EL2 IRQs to the CPU. 20109cf57ebSDavid Brazdil */ 20209cf57ebSDavid Brazdil pmr_sync(); 20309cf57ebSDavid Brazdil 20409cf57ebSDavid Brazdil ret = __kvm_vcpu_run_vhe(vcpu); 20509cf57ebSDavid Brazdil 20609cf57ebSDavid Brazdil /* 20709cf57ebSDavid Brazdil * local_daif_restore() takes care to properly restore PSTATE.DAIF 20809cf57ebSDavid Brazdil * and the GIC PMR if the host is using IRQ priorities. 20909cf57ebSDavid Brazdil */ 21009cf57ebSDavid Brazdil local_daif_restore(DAIF_PROCCTX_NOIRQ); 21109cf57ebSDavid Brazdil 21209cf57ebSDavid Brazdil /* 21309cf57ebSDavid Brazdil * When we exit from the guest we change a number of CPU configuration 21409cf57ebSDavid Brazdil * parameters, such as traps. Make sure these changes take effect 21509cf57ebSDavid Brazdil * before running the host or additional guests. 21609cf57ebSDavid Brazdil */ 21709cf57ebSDavid Brazdil isb(); 21809cf57ebSDavid Brazdil 21909cf57ebSDavid Brazdil return ret; 22009cf57ebSDavid Brazdil } 22109cf57ebSDavid Brazdil 2226a0259edSAndrew Scull static void __hyp_call_panic(u64 spsr, u64 elr, u64 par) 22309cf57ebSDavid Brazdil { 2246a0259edSAndrew Scull struct kvm_cpu_context *host_ctxt; 22509cf57ebSDavid Brazdil struct kvm_vcpu *vcpu; 2266a0259edSAndrew Scull 22714ef9d04SMarc Zyngier host_ctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt; 22809cf57ebSDavid Brazdil vcpu = host_ctxt->__hyp_running_vcpu; 22909cf57ebSDavid Brazdil 23009cf57ebSDavid Brazdil __deactivate_traps(vcpu); 23109cf57ebSDavid Brazdil sysreg_restore_host_state_vhe(host_ctxt); 23209cf57ebSDavid Brazdil 233aec0fae6SAndrew Scull panic("HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n", 23409cf57ebSDavid Brazdil spsr, elr, 23509cf57ebSDavid Brazdil read_sysreg_el2(SYS_ESR), read_sysreg_el2(SYS_FAR), 23609cf57ebSDavid Brazdil read_sysreg(hpfar_el2), par, vcpu); 23709cf57ebSDavid Brazdil } 23809cf57ebSDavid Brazdil NOKPROBE_SYMBOL(__hyp_call_panic); 23909cf57ebSDavid Brazdil 2406a0259edSAndrew Scull void __noreturn hyp_panic(void) 24109cf57ebSDavid Brazdil { 24209cf57ebSDavid Brazdil u64 spsr = read_sysreg_el2(SYS_SPSR); 24309cf57ebSDavid Brazdil u64 elr = read_sysreg_el2(SYS_ELR); 24496d389caSRob Herring u64 par = read_sysreg_par(); 24509cf57ebSDavid Brazdil 2466a0259edSAndrew Scull __hyp_call_panic(spsr, elr, par); 24709cf57ebSDavid Brazdil unreachable(); 24809cf57ebSDavid Brazdil } 249e9ee186bSJames Morse 250e9ee186bSJames Morse asmlinkage void kvm_unexpected_el2_exception(void) 251e9ee186bSJames Morse { 252e9ee186bSJames Morse return __kvm_unexpected_el2_exception(); 253e9ee186bSJames Morse } 254