1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * Copyright (C) 2015 - ARM Ltd 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 5 */ 6 7#include <linux/linkage.h> 8 9#include <asm/alternative.h> 10#include <asm/asm-offsets.h> 11#include <asm/assembler.h> 12#include <asm/fpsimdmacros.h> 13#include <asm/kvm.h> 14#include <asm/kvm_arm.h> 15#include <asm/kvm_asm.h> 16#include <asm/kvm_mmu.h> 17#include <asm/kvm_ptrauth.h> 18 19#define CPU_GP_REG_OFFSET(x) (CPU_GP_REGS + x) 20#define CPU_XREG_OFFSET(x) CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x) 21 22 .text 23 .pushsection .hyp.text, "ax" 24 25/* 26 * We treat x18 as callee-saved as the host may use it as a platform 27 * register (e.g. for shadow call stack). 28 */ 29.macro save_callee_saved_regs ctxt 30 str x18, [\ctxt, #CPU_XREG_OFFSET(18)] 31 stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 32 stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 33 stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 34 stp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 35 stp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 36 stp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 37.endm 38 39.macro restore_callee_saved_regs ctxt 40 // We require \ctxt is not x18-x28 41 ldr x18, [\ctxt, #CPU_XREG_OFFSET(18)] 42 ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)] 43 ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)] 44 ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)] 45 ldp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)] 46 ldp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)] 47 ldp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)] 48.endm 49 50/* 51 * u64 __guest_enter(struct kvm_vcpu *vcpu, 52 * struct kvm_cpu_context *host_ctxt); 53 */ 54ENTRY(__guest_enter) 55 // x0: vcpu 56 // x1: host context 57 // x2-x17: clobbered by macros 58 // x29: guest context 59 60 // Store the host regs 61 save_callee_saved_regs x1 62 63 // Now the host state is stored if we have a pending RAS SError it must 64 // affect the host. If any asynchronous exception is pending we defer 65 // the guest entry. The DSB isn't necessary before v8.2 as any SError 66 // would be fatal. 67alternative_if ARM64_HAS_RAS_EXTN 68 dsb nshst 69 isb 70alternative_else_nop_endif 71 mrs x1, isr_el1 72 cbz x1, 1f 73 mov x0, #ARM_EXCEPTION_IRQ 74 ret 75 761: 77 add x29, x0, #VCPU_CONTEXT 78 79 // Macro ptrauth_switch_to_guest format: 80 // ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3) 81 // The below macro to restore guest keys is not implemented in C code 82 // as it may cause Pointer Authentication key signing mismatch errors 83 // when this feature is enabled for kernel code. 84 ptrauth_switch_to_guest x29, x0, x1, x2 85 86 // Restore guest regs x0-x17 87 ldp x0, x1, [x29, #CPU_XREG_OFFSET(0)] 88 ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)] 89 ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)] 90 ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)] 91 ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)] 92 ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)] 93 ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)] 94 ldp x14, x15, [x29, #CPU_XREG_OFFSET(14)] 95 ldp x16, x17, [x29, #CPU_XREG_OFFSET(16)] 96 97 // Restore guest regs x18-x29, lr 98 restore_callee_saved_regs x29 99 100 // Do not touch any register after this! 101 eret 102 sb 103ENDPROC(__guest_enter) 104 105ENTRY(__guest_exit) 106 // x0: return code 107 // x1: vcpu 108 // x2-x29,lr: vcpu regs 109 // vcpu x0-x1 on the stack 110 111 add x1, x1, #VCPU_CONTEXT 112 113 ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN) 114 115 // Store the guest regs x2 and x3 116 stp x2, x3, [x1, #CPU_XREG_OFFSET(2)] 117 118 // Retrieve the guest regs x0-x1 from the stack 119 ldp x2, x3, [sp], #16 // x0, x1 120 121 // Store the guest regs x0-x1 and x4-x17 122 stp x2, x3, [x1, #CPU_XREG_OFFSET(0)] 123 stp x4, x5, [x1, #CPU_XREG_OFFSET(4)] 124 stp x6, x7, [x1, #CPU_XREG_OFFSET(6)] 125 stp x8, x9, [x1, #CPU_XREG_OFFSET(8)] 126 stp x10, x11, [x1, #CPU_XREG_OFFSET(10)] 127 stp x12, x13, [x1, #CPU_XREG_OFFSET(12)] 128 stp x14, x15, [x1, #CPU_XREG_OFFSET(14)] 129 stp x16, x17, [x1, #CPU_XREG_OFFSET(16)] 130 131 // Store the guest regs x18-x29, lr 132 save_callee_saved_regs x1 133 134 get_host_ctxt x2, x3 135 136 // Macro ptrauth_switch_to_guest format: 137 // ptrauth_switch_to_host(guest cxt, host cxt, tmp1, tmp2, tmp3) 138 // The below macro to save/restore keys is not implemented in C code 139 // as it may cause Pointer Authentication key signing mismatch errors 140 // when this feature is enabled for kernel code. 141 ptrauth_switch_to_host x1, x2, x3, x4, x5 142 143 // Now restore the host regs 144 restore_callee_saved_regs x2 145 146alternative_if ARM64_HAS_RAS_EXTN 147 // If we have the RAS extensions we can consume a pending error 148 // without an unmask-SError and isb. The ESB-instruction consumed any 149 // pending guest error when we took the exception from the guest. 150 mrs_s x2, SYS_DISR_EL1 151 str x2, [x1, #(VCPU_FAULT_DISR - VCPU_CONTEXT)] 152 cbz x2, 1f 153 msr_s SYS_DISR_EL1, xzr 154 orr x0, x0, #(1<<ARM_EXIT_WITH_SERROR_BIT) 1551: ret 156alternative_else 157 dsb sy // Synchronize against in-flight ld/st 158 isb // Prevent an early read of side-effect free ISR 159 mrs x2, isr_el1 160 tbnz x2, #8, 2f // ISR_EL1.A 161 ret 162 nop 1632: 164alternative_endif 165 // We know we have a pending asynchronous abort, now is the 166 // time to flush it out. From your VAXorcist book, page 666: 167 // "Threaten me not, oh Evil one! For I speak with 168 // the power of DEC, and I command thee to show thyself!" 169 mrs x2, elr_el2 170 mrs x3, esr_el2 171 mrs x4, spsr_el2 172 mov x5, x0 173 174 msr daifclr, #4 // Unmask aborts 175 176 // This is our single instruction exception window. A pending 177 // SError is guaranteed to occur at the earliest when we unmask 178 // it, and at the latest just after the ISB. 179abort_guest_exit_start: 180 181 isb 182 183abort_guest_exit_end: 184 185 msr daifset, #4 // Mask aborts 186 ret 187 188 _kvm_extable abort_guest_exit_start, 9997f 189 _kvm_extable abort_guest_exit_end, 9997f 1909997: 191 msr daifset, #4 // Mask aborts 192 mov x0, #(1 << ARM_EXIT_WITH_SERROR_BIT) 193 194 // restore the EL1 exception context so that we can report some 195 // information. Merge the exception code with the SError pending bit. 196 msr elr_el2, x2 197 msr esr_el2, x3 198 msr spsr_el2, x4 199 orr x0, x0, x5 2001: ret 201ENDPROC(__guest_exit) 202