18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2015 - ARM Ltd 48c2ecf20Sopenharmony_ci * Author: Marc Zyngier <marc.zyngier@arm.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/linkage.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <asm/alternative.h> 108c2ecf20Sopenharmony_ci#include <asm/assembler.h> 118c2ecf20Sopenharmony_ci#include <asm/fpsimdmacros.h> 128c2ecf20Sopenharmony_ci#include <asm/kvm.h> 138c2ecf20Sopenharmony_ci#include <asm/kvm_arm.h> 148c2ecf20Sopenharmony_ci#include <asm/kvm_asm.h> 158c2ecf20Sopenharmony_ci#include <asm/kvm_mmu.h> 168c2ecf20Sopenharmony_ci#include <asm/kvm_ptrauth.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci .text 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* 218c2ecf20Sopenharmony_ci * u64 __guest_enter(struct kvm_vcpu *vcpu); 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_ciSYM_FUNC_START(__guest_enter) 248c2ecf20Sopenharmony_ci // x0: vcpu 258c2ecf20Sopenharmony_ci // x1-x17: clobbered by macros 268c2ecf20Sopenharmony_ci // x29: guest context 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci adr_this_cpu x1, kvm_hyp_ctxt, x2 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci // Store the hyp regs 318c2ecf20Sopenharmony_ci save_callee_saved_regs x1 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci // Save hyp's sp_el0 348c2ecf20Sopenharmony_ci save_sp_el0 x1, x2 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci // Now the hyp state is stored if we have a pending RAS SError it must 378c2ecf20Sopenharmony_ci // affect the host or hyp. If any asynchronous exception is pending we 388c2ecf20Sopenharmony_ci // defer the guest entry. The DSB isn't necessary before v8.2 as any 398c2ecf20Sopenharmony_ci // SError would be fatal. 408c2ecf20Sopenharmony_cialternative_if ARM64_HAS_RAS_EXTN 418c2ecf20Sopenharmony_ci dsb nshst 428c2ecf20Sopenharmony_ci isb 438c2ecf20Sopenharmony_cialternative_else_nop_endif 448c2ecf20Sopenharmony_ci mrs x1, isr_el1 458c2ecf20Sopenharmony_ci cbz x1, 1f 468c2ecf20Sopenharmony_ci mov x0, #ARM_EXCEPTION_IRQ 478c2ecf20Sopenharmony_ci ret 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci1: 508c2ecf20Sopenharmony_ci set_loaded_vcpu x0, x1, x2 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci add x29, x0, #VCPU_CONTEXT 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci // Macro ptrauth_switch_to_guest format: 558c2ecf20Sopenharmony_ci // ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3) 568c2ecf20Sopenharmony_ci // The below macro to restore guest keys is not implemented in C code 578c2ecf20Sopenharmony_ci // as it may cause Pointer Authentication key signing mismatch errors 588c2ecf20Sopenharmony_ci // when this feature is enabled for kernel code. 598c2ecf20Sopenharmony_ci ptrauth_switch_to_guest x29, x0, x1, x2 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci // Restore the guest's sp_el0 628c2ecf20Sopenharmony_ci restore_sp_el0 x29, x0 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci // Restore guest regs x0-x17 658c2ecf20Sopenharmony_ci ldp x0, x1, [x29, #CPU_XREG_OFFSET(0)] 668c2ecf20Sopenharmony_ci ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)] 678c2ecf20Sopenharmony_ci ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)] 688c2ecf20Sopenharmony_ci ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)] 698c2ecf20Sopenharmony_ci ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)] 708c2ecf20Sopenharmony_ci ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)] 718c2ecf20Sopenharmony_ci ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)] 728c2ecf20Sopenharmony_ci ldp x14, x15, [x29, #CPU_XREG_OFFSET(14)] 738c2ecf20Sopenharmony_ci ldp x16, x17, [x29, #CPU_XREG_OFFSET(16)] 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci // Restore guest regs x18-x29, lr 768c2ecf20Sopenharmony_ci restore_callee_saved_regs x29 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci // Do not touch any register after this! 798c2ecf20Sopenharmony_ci eret 808c2ecf20Sopenharmony_ci sb 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ciSYM_INNER_LABEL(__guest_exit_panic, SYM_L_GLOBAL) 838c2ecf20Sopenharmony_ci // x2-x29,lr: vcpu regs 848c2ecf20Sopenharmony_ci // vcpu x0-x1 on the stack 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci // If the hyp context is loaded, go straight to hyp_panic 878c2ecf20Sopenharmony_ci get_loaded_vcpu x0, x1 888c2ecf20Sopenharmony_ci cbz x0, hyp_panic 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci // The hyp context is saved so make sure it is restored to allow 918c2ecf20Sopenharmony_ci // hyp_panic to run at hyp and, subsequently, panic to run in the host. 928c2ecf20Sopenharmony_ci // This makes use of __guest_exit to avoid duplication but sets the 938c2ecf20Sopenharmony_ci // return address to tail call into hyp_panic. As a side effect, the 948c2ecf20Sopenharmony_ci // current state is saved to the guest context but it will only be 958c2ecf20Sopenharmony_ci // accurate if the guest had been completely restored. 968c2ecf20Sopenharmony_ci adr_this_cpu x0, kvm_hyp_ctxt, x1 978c2ecf20Sopenharmony_ci adr x1, hyp_panic 988c2ecf20Sopenharmony_ci str x1, [x0, #CPU_XREG_OFFSET(30)] 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci get_vcpu_ptr x1, x0 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciSYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL) 1038c2ecf20Sopenharmony_ci // x0: return code 1048c2ecf20Sopenharmony_ci // x1: vcpu 1058c2ecf20Sopenharmony_ci // x2-x29,lr: vcpu regs 1068c2ecf20Sopenharmony_ci // vcpu x0-x1 on the stack 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci add x1, x1, #VCPU_CONTEXT 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN) 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci // Store the guest regs x2 and x3 1138c2ecf20Sopenharmony_ci stp x2, x3, [x1, #CPU_XREG_OFFSET(2)] 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci // Retrieve the guest regs x0-x1 from the stack 1168c2ecf20Sopenharmony_ci ldp x2, x3, [sp], #16 // x0, x1 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci // Store the guest regs x0-x1 and x4-x17 1198c2ecf20Sopenharmony_ci stp x2, x3, [x1, #CPU_XREG_OFFSET(0)] 1208c2ecf20Sopenharmony_ci stp x4, x5, [x1, #CPU_XREG_OFFSET(4)] 1218c2ecf20Sopenharmony_ci stp x6, x7, [x1, #CPU_XREG_OFFSET(6)] 1228c2ecf20Sopenharmony_ci stp x8, x9, [x1, #CPU_XREG_OFFSET(8)] 1238c2ecf20Sopenharmony_ci stp x10, x11, [x1, #CPU_XREG_OFFSET(10)] 1248c2ecf20Sopenharmony_ci stp x12, x13, [x1, #CPU_XREG_OFFSET(12)] 1258c2ecf20Sopenharmony_ci stp x14, x15, [x1, #CPU_XREG_OFFSET(14)] 1268c2ecf20Sopenharmony_ci stp x16, x17, [x1, #CPU_XREG_OFFSET(16)] 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci // Store the guest regs x18-x29, lr 1298c2ecf20Sopenharmony_ci save_callee_saved_regs x1 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci // Store the guest's sp_el0 1328c2ecf20Sopenharmony_ci save_sp_el0 x1, x2 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci adr_this_cpu x2, kvm_hyp_ctxt, x3 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci // Macro ptrauth_switch_to_hyp format: 1378c2ecf20Sopenharmony_ci // ptrauth_switch_to_hyp(guest cxt, host cxt, tmp1, tmp2, tmp3) 1388c2ecf20Sopenharmony_ci // The below macro to save/restore keys is not implemented in C code 1398c2ecf20Sopenharmony_ci // as it may cause Pointer Authentication key signing mismatch errors 1408c2ecf20Sopenharmony_ci // when this feature is enabled for kernel code. 1418c2ecf20Sopenharmony_ci ptrauth_switch_to_hyp x1, x2, x3, x4, x5 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci // Restore hyp's sp_el0 1448c2ecf20Sopenharmony_ci restore_sp_el0 x2, x3 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci // Now restore the hyp regs 1478c2ecf20Sopenharmony_ci restore_callee_saved_regs x2 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci set_loaded_vcpu xzr, x2, x3 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cialternative_if ARM64_HAS_RAS_EXTN 1528c2ecf20Sopenharmony_ci // If we have the RAS extensions we can consume a pending error 1538c2ecf20Sopenharmony_ci // without an unmask-SError and isb. The ESB-instruction consumed any 1548c2ecf20Sopenharmony_ci // pending guest error when we took the exception from the guest. 1558c2ecf20Sopenharmony_ci mrs_s x2, SYS_DISR_EL1 1568c2ecf20Sopenharmony_ci str x2, [x1, #(VCPU_FAULT_DISR - VCPU_CONTEXT)] 1578c2ecf20Sopenharmony_ci cbz x2, 1f 1588c2ecf20Sopenharmony_ci msr_s SYS_DISR_EL1, xzr 1598c2ecf20Sopenharmony_ci orr x0, x0, #(1<<ARM_EXIT_WITH_SERROR_BIT) 1608c2ecf20Sopenharmony_ci1: ret 1618c2ecf20Sopenharmony_cialternative_else 1628c2ecf20Sopenharmony_ci dsb sy // Synchronize against in-flight ld/st 1638c2ecf20Sopenharmony_ci isb // Prevent an early read of side-effect free ISR 1648c2ecf20Sopenharmony_ci mrs x2, isr_el1 1658c2ecf20Sopenharmony_ci tbnz x2, #8, 2f // ISR_EL1.A 1668c2ecf20Sopenharmony_ci ret 1678c2ecf20Sopenharmony_ci nop 1688c2ecf20Sopenharmony_ci2: 1698c2ecf20Sopenharmony_cialternative_endif 1708c2ecf20Sopenharmony_ci // We know we have a pending asynchronous abort, now is the 1718c2ecf20Sopenharmony_ci // time to flush it out. From your VAXorcist book, page 666: 1728c2ecf20Sopenharmony_ci // "Threaten me not, oh Evil one! For I speak with 1738c2ecf20Sopenharmony_ci // the power of DEC, and I command thee to show thyself!" 1748c2ecf20Sopenharmony_ci mrs x2, elr_el2 1758c2ecf20Sopenharmony_ci mrs x3, esr_el2 1768c2ecf20Sopenharmony_ci mrs x4, spsr_el2 1778c2ecf20Sopenharmony_ci mov x5, x0 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci msr daifclr, #4 // Unmask aborts 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci // This is our single instruction exception window. A pending 1828c2ecf20Sopenharmony_ci // SError is guaranteed to occur at the earliest when we unmask 1838c2ecf20Sopenharmony_ci // it, and at the latest just after the ISB. 1848c2ecf20Sopenharmony_ciabort_guest_exit_start: 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci isb 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ciabort_guest_exit_end: 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci msr daifset, #4 // Mask aborts 1918c2ecf20Sopenharmony_ci ret 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci _kvm_extable abort_guest_exit_start, 9997f 1948c2ecf20Sopenharmony_ci _kvm_extable abort_guest_exit_end, 9997f 1958c2ecf20Sopenharmony_ci9997: 1968c2ecf20Sopenharmony_ci msr daifset, #4 // Mask aborts 1978c2ecf20Sopenharmony_ci mov x0, #(1 << ARM_EXIT_WITH_SERROR_BIT) 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci // restore the EL1 exception context so that we can report some 2008c2ecf20Sopenharmony_ci // information. Merge the exception code with the SError pending bit. 2018c2ecf20Sopenharmony_ci msr elr_el2, x2 2028c2ecf20Sopenharmony_ci msr esr_el2, x3 2038c2ecf20Sopenharmony_ci msr spsr_el2, x4 2048c2ecf20Sopenharmony_ci orr x0, x0, x5 2058c2ecf20Sopenharmony_ci1: ret 2068c2ecf20Sopenharmony_ciSYM_FUNC_END(__guest_enter) 207