162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2015 - ARM Ltd 462306a36Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include <linux/linkage.h> 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <asm/alternative.h> 1062306a36Sopenharmony_ci#include <asm/assembler.h> 1162306a36Sopenharmony_ci#include <asm/fpsimdmacros.h> 1262306a36Sopenharmony_ci#include <asm/kvm.h> 1362306a36Sopenharmony_ci#include <asm/kvm_arm.h> 1462306a36Sopenharmony_ci#include <asm/kvm_asm.h> 1562306a36Sopenharmony_ci#include <asm/kvm_mmu.h> 1662306a36Sopenharmony_ci#include <asm/kvm_mte.h> 1762306a36Sopenharmony_ci#include <asm/kvm_ptrauth.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci .text 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci/* 2262306a36Sopenharmony_ci * u64 __guest_enter(struct kvm_vcpu *vcpu); 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_ciSYM_FUNC_START(__guest_enter) 2562306a36Sopenharmony_ci // x0: vcpu 2662306a36Sopenharmony_ci // x1-x17: clobbered by macros 2762306a36Sopenharmony_ci // x29: guest context 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci adr_this_cpu x1, kvm_hyp_ctxt, x2 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci // Store the hyp regs 3262306a36Sopenharmony_ci save_callee_saved_regs x1 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci // Save hyp's sp_el0 3562306a36Sopenharmony_ci save_sp_el0 x1, x2 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci // Now the hyp state is stored if we have a pending RAS SError it must 3862306a36Sopenharmony_ci // affect the host or hyp. If any asynchronous exception is pending we 3962306a36Sopenharmony_ci // defer the guest entry. The DSB isn't necessary before v8.2 as any 4062306a36Sopenharmony_ci // SError would be fatal. 4162306a36Sopenharmony_cialternative_if ARM64_HAS_RAS_EXTN 4262306a36Sopenharmony_ci dsb nshst 4362306a36Sopenharmony_ci isb 4462306a36Sopenharmony_cialternative_else_nop_endif 4562306a36Sopenharmony_ci mrs x1, isr_el1 4662306a36Sopenharmony_ci cbz x1, 1f 4762306a36Sopenharmony_ci mov x0, #ARM_EXCEPTION_IRQ 4862306a36Sopenharmony_ci ret 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci1: 5162306a36Sopenharmony_ci set_loaded_vcpu x0, x1, x2 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci add x29, x0, #VCPU_CONTEXT 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci // mte_switch_to_guest(g_ctxt, h_ctxt, tmp1) 5662306a36Sopenharmony_ci mte_switch_to_guest x29, x1, x2 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci // Macro ptrauth_switch_to_guest format: 5962306a36Sopenharmony_ci // ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3) 6062306a36Sopenharmony_ci // The below macro to restore guest keys is not implemented in C code 6162306a36Sopenharmony_ci // as it may cause Pointer Authentication key signing mismatch errors 6262306a36Sopenharmony_ci // when this feature is enabled for kernel code. 6362306a36Sopenharmony_ci ptrauth_switch_to_guest x29, x0, x1, x2 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci // Restore the guest's sp_el0 6662306a36Sopenharmony_ci restore_sp_el0 x29, x0 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci // Restore guest regs x0-x17 6962306a36Sopenharmony_ci ldp x0, x1, [x29, #CPU_XREG_OFFSET(0)] 7062306a36Sopenharmony_ci ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)] 7162306a36Sopenharmony_ci ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)] 7262306a36Sopenharmony_ci ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)] 7362306a36Sopenharmony_ci ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)] 7462306a36Sopenharmony_ci ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)] 7562306a36Sopenharmony_ci ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)] 7662306a36Sopenharmony_ci ldp x14, x15, [x29, #CPU_XREG_OFFSET(14)] 7762306a36Sopenharmony_ci ldp x16, x17, [x29, #CPU_XREG_OFFSET(16)] 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci // Restore guest regs x18-x29, lr 8062306a36Sopenharmony_ci restore_callee_saved_regs x29 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci // Do not touch any register after this! 8362306a36Sopenharmony_ci eret 8462306a36Sopenharmony_ci sb 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ciSYM_INNER_LABEL(__guest_exit_panic, SYM_L_GLOBAL) 8762306a36Sopenharmony_ci // x2-x29,lr: vcpu regs 8862306a36Sopenharmony_ci // vcpu x0-x1 on the stack 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci // If the hyp context is loaded, go straight to hyp_panic 9162306a36Sopenharmony_ci get_loaded_vcpu x0, x1 9262306a36Sopenharmony_ci cbnz x0, 1f 9362306a36Sopenharmony_ci b hyp_panic 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci1: 9662306a36Sopenharmony_ci // The hyp context is saved so make sure it is restored to allow 9762306a36Sopenharmony_ci // hyp_panic to run at hyp and, subsequently, panic to run in the host. 9862306a36Sopenharmony_ci // This makes use of __guest_exit to avoid duplication but sets the 9962306a36Sopenharmony_ci // return address to tail call into hyp_panic. As a side effect, the 10062306a36Sopenharmony_ci // current state is saved to the guest context but it will only be 10162306a36Sopenharmony_ci // accurate if the guest had been completely restored. 10262306a36Sopenharmony_ci adr_this_cpu x0, kvm_hyp_ctxt, x1 10362306a36Sopenharmony_ci adr_l x1, hyp_panic 10462306a36Sopenharmony_ci str x1, [x0, #CPU_XREG_OFFSET(30)] 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci get_vcpu_ptr x1, x0 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ciSYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL) 10962306a36Sopenharmony_ci // x0: return code 11062306a36Sopenharmony_ci // x1: vcpu 11162306a36Sopenharmony_ci // x2-x29,lr: vcpu regs 11262306a36Sopenharmony_ci // vcpu x0-x1 on the stack 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci add x1, x1, #VCPU_CONTEXT 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN) 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci // Store the guest regs x2 and x3 11962306a36Sopenharmony_ci stp x2, x3, [x1, #CPU_XREG_OFFSET(2)] 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci // Retrieve the guest regs x0-x1 from the stack 12262306a36Sopenharmony_ci ldp x2, x3, [sp], #16 // x0, x1 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci // Store the guest regs x0-x1 and x4-x17 12562306a36Sopenharmony_ci stp x2, x3, [x1, #CPU_XREG_OFFSET(0)] 12662306a36Sopenharmony_ci stp x4, x5, [x1, #CPU_XREG_OFFSET(4)] 12762306a36Sopenharmony_ci stp x6, x7, [x1, #CPU_XREG_OFFSET(6)] 12862306a36Sopenharmony_ci stp x8, x9, [x1, #CPU_XREG_OFFSET(8)] 12962306a36Sopenharmony_ci stp x10, x11, [x1, #CPU_XREG_OFFSET(10)] 13062306a36Sopenharmony_ci stp x12, x13, [x1, #CPU_XREG_OFFSET(12)] 13162306a36Sopenharmony_ci stp x14, x15, [x1, #CPU_XREG_OFFSET(14)] 13262306a36Sopenharmony_ci stp x16, x17, [x1, #CPU_XREG_OFFSET(16)] 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci // Store the guest regs x18-x29, lr 13562306a36Sopenharmony_ci save_callee_saved_regs x1 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci // Store the guest's sp_el0 13862306a36Sopenharmony_ci save_sp_el0 x1, x2 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci adr_this_cpu x2, kvm_hyp_ctxt, x3 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci // Macro ptrauth_switch_to_hyp format: 14362306a36Sopenharmony_ci // ptrauth_switch_to_hyp(guest cxt, host cxt, tmp1, tmp2, tmp3) 14462306a36Sopenharmony_ci // The below macro to save/restore keys is not implemented in C code 14562306a36Sopenharmony_ci // as it may cause Pointer Authentication key signing mismatch errors 14662306a36Sopenharmony_ci // when this feature is enabled for kernel code. 14762306a36Sopenharmony_ci ptrauth_switch_to_hyp x1, x2, x3, x4, x5 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci // mte_switch_to_hyp(g_ctxt, h_ctxt, reg1) 15062306a36Sopenharmony_ci mte_switch_to_hyp x1, x2, x3 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci // Restore hyp's sp_el0 15362306a36Sopenharmony_ci restore_sp_el0 x2, x3 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci // Now restore the hyp regs 15662306a36Sopenharmony_ci restore_callee_saved_regs x2 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci set_loaded_vcpu xzr, x2, x3 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_cialternative_if ARM64_HAS_RAS_EXTN 16162306a36Sopenharmony_ci // If we have the RAS extensions we can consume a pending error 16262306a36Sopenharmony_ci // without an unmask-SError and isb. The ESB-instruction consumed any 16362306a36Sopenharmony_ci // pending guest error when we took the exception from the guest. 16462306a36Sopenharmony_ci mrs_s x2, SYS_DISR_EL1 16562306a36Sopenharmony_ci str x2, [x1, #(VCPU_FAULT_DISR - VCPU_CONTEXT)] 16662306a36Sopenharmony_ci cbz x2, 1f 16762306a36Sopenharmony_ci msr_s SYS_DISR_EL1, xzr 16862306a36Sopenharmony_ci orr x0, x0, #(1<<ARM_EXIT_WITH_SERROR_BIT) 16962306a36Sopenharmony_ci1: ret 17062306a36Sopenharmony_cialternative_else 17162306a36Sopenharmony_ci dsb sy // Synchronize against in-flight ld/st 17262306a36Sopenharmony_ci isb // Prevent an early read of side-effect free ISR 17362306a36Sopenharmony_ci mrs x2, isr_el1 17462306a36Sopenharmony_ci tbnz x2, #ISR_EL1_A_SHIFT, 2f 17562306a36Sopenharmony_ci ret 17662306a36Sopenharmony_ci nop 17762306a36Sopenharmony_ci2: 17862306a36Sopenharmony_cialternative_endif 17962306a36Sopenharmony_ci // We know we have a pending asynchronous abort, now is the 18062306a36Sopenharmony_ci // time to flush it out. From your VAXorcist book, page 666: 18162306a36Sopenharmony_ci // "Threaten me not, oh Evil one! For I speak with 18262306a36Sopenharmony_ci // the power of DEC, and I command thee to show thyself!" 18362306a36Sopenharmony_ci mrs x2, elr_el2 18462306a36Sopenharmony_ci mrs x3, esr_el2 18562306a36Sopenharmony_ci mrs x4, spsr_el2 18662306a36Sopenharmony_ci mov x5, x0 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci msr daifclr, #4 // Unmask aborts 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci // This is our single instruction exception window. A pending 19162306a36Sopenharmony_ci // SError is guaranteed to occur at the earliest when we unmask 19262306a36Sopenharmony_ci // it, and at the latest just after the ISB. 19362306a36Sopenharmony_ciabort_guest_exit_start: 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci isb 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ciabort_guest_exit_end: 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci msr daifset, #4 // Mask aborts 20062306a36Sopenharmony_ci ret 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci _kvm_extable abort_guest_exit_start, 9997f 20362306a36Sopenharmony_ci _kvm_extable abort_guest_exit_end, 9997f 20462306a36Sopenharmony_ci9997: 20562306a36Sopenharmony_ci msr daifset, #4 // Mask aborts 20662306a36Sopenharmony_ci mov x0, #(1 << ARM_EXIT_WITH_SERROR_BIT) 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci // restore the EL1 exception context so that we can report some 20962306a36Sopenharmony_ci // information. Merge the exception code with the SError pending bit. 21062306a36Sopenharmony_ci msr elr_el2, x2 21162306a36Sopenharmony_ci msr esr_el2, x3 21262306a36Sopenharmony_ci msr spsr_el2, x4 21362306a36Sopenharmony_ci orr x0, x0, x5 21462306a36Sopenharmony_ci1: ret 21562306a36Sopenharmony_ciSYM_FUNC_END(__guest_enter) 216