1caab277bSThomas Gleixner/* SPDX-License-Identifier: GPL-2.0-only */ 2b97b66c1SMarc Zyngier/* 3b97b66c1SMarc Zyngier * Copyright (C) 2015 - ARM Ltd 4b97b66c1SMarc Zyngier * Author: Marc Zyngier <marc.zyngier@arm.com> 5b97b66c1SMarc Zyngier */ 6b97b66c1SMarc Zyngier 7b97b66c1SMarc Zyngier#include <linux/linkage.h> 8b97b66c1SMarc Zyngier 9b97b66c1SMarc Zyngier#include <asm/asm-offsets.h> 10b97b66c1SMarc Zyngier#include <asm/assembler.h> 11b97b66c1SMarc Zyngier#include <asm/fpsimdmacros.h> 12b97b66c1SMarc Zyngier#include <asm/kvm.h> 13b97b66c1SMarc Zyngier#include <asm/kvm_arm.h> 14b97b66c1SMarc Zyngier#include <asm/kvm_asm.h> 15b97b66c1SMarc Zyngier#include <asm/kvm_mmu.h> 16384b40caSMark Rutland#include <asm/kvm_ptrauth.h> 17b97b66c1SMarc Zyngier 18b97b66c1SMarc Zyngier#define CPU_GP_REG_OFFSET(x) (CPU_GP_REGS + x) 19b97b66c1SMarc Zyngier#define CPU_XREG_OFFSET(x) CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x) 20b97b66c1SMarc Zyngier 21b97b66c1SMarc Zyngier .text 22b97b66c1SMarc Zyngier .pushsection .hyp.text, "ax" 23b97b66c1SMarc Zyngier 24b97b66c1SMarc Zyngier.macro save_callee_saved_regs ctxt 25b97b66c1SMarc Zyngier stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 26b97b66c1SMarc Zyngier stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 27b97b66c1SMarc Zyngier stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 28b97b66c1SMarc Zyngier stp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 29b97b66c1SMarc Zyngier stp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 30b97b66c1SMarc Zyngier stp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 31b97b66c1SMarc Zyngier.endm 32b97b66c1SMarc Zyngier 33b97b66c1SMarc Zyngier.macro restore_callee_saved_regs ctxt 34b97b66c1SMarc Zyngier ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 35b97b66c1SMarc Zyngier ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 36b97b66c1SMarc Zyngier ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 37b97b66c1SMarc Zyngier ldp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 38b97b66c1SMarc Zyngier ldp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 39b97b66c1SMarc Zyngier ldp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 40b97b66c1SMarc Zyngier.endm 41b97b66c1SMarc Zyngier 42b97b66c1SMarc Zyngier/* 43b97b66c1SMarc Zyngier * u64 __guest_enter(struct kvm_vcpu *vcpu, 44b97b66c1SMarc Zyngier * struct kvm_cpu_context *host_ctxt); 45b97b66c1SMarc Zyngier */ 46b97b66c1SMarc ZyngierENTRY(__guest_enter) 47b97b66c1SMarc Zyngier // x0: vcpu 4868381b2bSShanker Donthineni // x1: host context 4968381b2bSShanker Donthineni // x2-x17: clobbered by macros 5068381b2bSShanker Donthineni // x18: guest context 51b97b66c1SMarc Zyngier 52b97b66c1SMarc Zyngier // Store the host regs 53b97b66c1SMarc Zyngier save_callee_saved_regs x1 54b97b66c1SMarc Zyngier 5568381b2bSShanker Donthineni add x18, x0, #VCPU_CONTEXT 56b97b66c1SMarc Zyngier 57384b40caSMark Rutland // Macro ptrauth_switch_to_guest format: 58384b40caSMark Rutland // ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3) 59384b40caSMark Rutland // The below macro to restore guest keys is not implemented in C code 60384b40caSMark Rutland // as it may cause Pointer Authentication key signing mismatch errors 61384b40caSMark Rutland // when this feature is enabled for kernel code. 62384b40caSMark Rutland ptrauth_switch_to_guest x18, x0, x1, x2 63384b40caSMark Rutland 6468381b2bSShanker Donthineni // Restore guest regs x0-x17 6568381b2bSShanker Donthineni ldp x0, x1, [x18, #CPU_XREG_OFFSET(0)] 6668381b2bSShanker Donthineni ldp x2, x3, [x18, #CPU_XREG_OFFSET(2)] 6768381b2bSShanker Donthineni ldp x4, x5, [x18, #CPU_XREG_OFFSET(4)] 6868381b2bSShanker Donthineni ldp x6, x7, [x18, #CPU_XREG_OFFSET(6)] 6968381b2bSShanker Donthineni ldp x8, x9, [x18, #CPU_XREG_OFFSET(8)] 7068381b2bSShanker Donthineni ldp x10, x11, [x18, #CPU_XREG_OFFSET(10)] 7168381b2bSShanker Donthineni ldp x12, x13, [x18, #CPU_XREG_OFFSET(12)] 7268381b2bSShanker Donthineni ldp x14, x15, [x18, #CPU_XREG_OFFSET(14)] 7368381b2bSShanker Donthineni ldp x16, x17, [x18, #CPU_XREG_OFFSET(16)] 74b97b66c1SMarc Zyngier 7568381b2bSShanker Donthineni // Restore guest regs x19-x29, lr 7668381b2bSShanker Donthineni restore_callee_saved_regs x18 77b97b66c1SMarc Zyngier 7868381b2bSShanker Donthineni // Restore guest reg x18 7968381b2bSShanker Donthineni ldr x18, [x18, #CPU_XREG_OFFSET(18)] 80b97b66c1SMarc Zyngier 81b97b66c1SMarc Zyngier // Do not touch any register after this! 82b97b66c1SMarc Zyngier eret 83679db708SWill Deacon sb 84b97b66c1SMarc ZyngierENDPROC(__guest_enter) 85b97b66c1SMarc Zyngier 86b97b66c1SMarc ZyngierENTRY(__guest_exit) 8768381b2bSShanker Donthineni // x0: return code 8868381b2bSShanker Donthineni // x1: vcpu 8968381b2bSShanker Donthineni // x2-x29,lr: vcpu regs 9068381b2bSShanker Donthineni // vcpu x0-x1 on the stack 91b97b66c1SMarc Zyngier 9268381b2bSShanker Donthineni add x1, x1, #VCPU_CONTEXT 93b97b66c1SMarc Zyngier 94cb96408dSVladimir Murzin ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN) 95cb96408dSVladimir Murzin 9668381b2bSShanker Donthineni // Store the guest regs x2 and x3 9768381b2bSShanker Donthineni stp x2, x3, [x1, #CPU_XREG_OFFSET(2)] 98b97b66c1SMarc Zyngier 9968381b2bSShanker Donthineni // Retrieve the guest regs x0-x1 from the stack 10068381b2bSShanker Donthineni ldp x2, x3, [sp], #16 // x0, x1 101b97b66c1SMarc Zyngier 10268381b2bSShanker Donthineni // Store the guest regs x0-x1 and x4-x18 10368381b2bSShanker Donthineni stp x2, x3, [x1, #CPU_XREG_OFFSET(0)] 10468381b2bSShanker Donthineni stp x4, x5, [x1, #CPU_XREG_OFFSET(4)] 10568381b2bSShanker Donthineni stp x6, x7, [x1, #CPU_XREG_OFFSET(6)] 10668381b2bSShanker Donthineni stp x8, x9, [x1, #CPU_XREG_OFFSET(8)] 10768381b2bSShanker Donthineni stp x10, x11, [x1, #CPU_XREG_OFFSET(10)] 10868381b2bSShanker Donthineni stp x12, x13, [x1, #CPU_XREG_OFFSET(12)] 10968381b2bSShanker Donthineni stp x14, x15, [x1, #CPU_XREG_OFFSET(14)] 11068381b2bSShanker Donthineni stp x16, x17, [x1, #CPU_XREG_OFFSET(16)] 11168381b2bSShanker Donthineni str x18, [x1, #CPU_XREG_OFFSET(18)] 112b97b66c1SMarc Zyngier 11368381b2bSShanker Donthineni // Store the guest regs x19-x29, lr 11468381b2bSShanker Donthineni save_callee_saved_regs x1 115b97b66c1SMarc Zyngier 1164464e210SChristoffer Dall get_host_ctxt x2, x3 11768381b2bSShanker Donthineni 118384b40caSMark Rutland // Macro ptrauth_switch_to_guest format: 119384b40caSMark Rutland // ptrauth_switch_to_host(guest cxt, host cxt, tmp1, tmp2, tmp3) 120384b40caSMark Rutland // The below macro to save/restore keys is not implemented in C code 121384b40caSMark Rutland // as it may cause Pointer Authentication key signing mismatch errors 122384b40caSMark Rutland // when this feature is enabled for kernel code. 123384b40caSMark Rutland ptrauth_switch_to_host x1, x2, x3, x4, x5 124384b40caSMark Rutland 125b97b66c1SMarc Zyngier // Now restore the host regs 126b97b66c1SMarc Zyngier restore_callee_saved_regs x2 127b97b66c1SMarc Zyngier 1280067df41SJames Morsealternative_if ARM64_HAS_RAS_EXTN 1290067df41SJames Morse // If we have the RAS extensions we can consume a pending error 1300067df41SJames Morse // without an unmask-SError and isb. 1310067df41SJames Morse esb 1320067df41SJames Morse mrs_s x2, SYS_DISR_EL1 1330067df41SJames Morse str x2, [x1, #(VCPU_FAULT_DISR - VCPU_CONTEXT)] 1340067df41SJames Morse cbz x2, 1f 1350067df41SJames Morse msr_s SYS_DISR_EL1, xzr 1360067df41SJames Morse orr x0, x0, #(1<<ARM_EXIT_WITH_SERROR_BIT) 1370067df41SJames Morse1: ret 1380067df41SJames Morsealternative_else 139395ea79eSMarc Zyngier // If we have a pending asynchronous abort, now is the 140395ea79eSMarc Zyngier // time to find out. From your VAXorcist book, page 666: 141395ea79eSMarc Zyngier // "Threaten me not, oh Evil one! For I speak with 142395ea79eSMarc Zyngier // the power of DEC, and I command thee to show thyself!" 143395ea79eSMarc Zyngier mrs x2, elr_el2 144395ea79eSMarc Zyngier mrs x3, esr_el2 145395ea79eSMarc Zyngier mrs x4, spsr_el2 146395ea79eSMarc Zyngier mov x5, x0 147395ea79eSMarc Zyngier 148395ea79eSMarc Zyngier dsb sy // Synchronize against in-flight ld/st 1490067df41SJames Morse nop 150395ea79eSMarc Zyngier msr daifclr, #4 // Unmask aborts 1510067df41SJames Morsealternative_endif 152395ea79eSMarc Zyngier 153395ea79eSMarc Zyngier // This is our single instruction exception window. A pending 154395ea79eSMarc Zyngier // SError is guaranteed to occur at the earliest when we unmask 155395ea79eSMarc Zyngier // it, and at the latest just after the ISB. 156395ea79eSMarc Zyngier .global abort_guest_exit_start 157395ea79eSMarc Zyngierabort_guest_exit_start: 158395ea79eSMarc Zyngier 159395ea79eSMarc Zyngier isb 160395ea79eSMarc Zyngier 161395ea79eSMarc Zyngier .global abort_guest_exit_end 162395ea79eSMarc Zyngierabort_guest_exit_end: 163395ea79eSMarc Zyngier 164395ea79eSMarc Zyngier // If the exception took place, restore the EL1 exception 165395ea79eSMarc Zyngier // context so that we can report some information. 166395ea79eSMarc Zyngier // Merge the exception code with the SError pending bit. 167395ea79eSMarc Zyngier tbz x0, #ARM_EXIT_WITH_SERROR_BIT, 1f 168395ea79eSMarc Zyngier msr elr_el2, x2 169395ea79eSMarc Zyngier msr esr_el2, x3 170395ea79eSMarc Zyngier msr spsr_el2, x4 171395ea79eSMarc Zyngier orr x0, x0, x5 172395ea79eSMarc Zyngier1: ret 173b97b66c1SMarc ZyngierENDPROC(__guest_exit) 174