1532ed618SSoby Mathew /* 24d1ccf0eSAntonio Nino Diaz * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5532ed618SSoby Mathew */ 6532ed618SSoby Mathew 7a0fee747SAntonio Nino Diaz #ifndef CONTEXT_H 8a0fee747SAntonio Nino Diaz #define CONTEXT_H 9532ed618SSoby Mathew 1009d40e0eSAntonio Nino Diaz #include <lib/utils_def.h> 1176454abfSJeenu Viswambharan 12532ed618SSoby Mathew /******************************************************************************* 13532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 'gp_regs' 14532ed618SSoby Mathew * structure at their correct offsets. 15532ed618SSoby Mathew ******************************************************************************/ 16030567e6SVarun Wadekar #define CTX_GPREGS_OFFSET U(0x0) 17030567e6SVarun Wadekar #define CTX_GPREG_X0 U(0x0) 18030567e6SVarun Wadekar #define CTX_GPREG_X1 U(0x8) 19030567e6SVarun Wadekar #define CTX_GPREG_X2 U(0x10) 20030567e6SVarun Wadekar #define CTX_GPREG_X3 U(0x18) 21030567e6SVarun Wadekar #define CTX_GPREG_X4 U(0x20) 22030567e6SVarun Wadekar #define CTX_GPREG_X5 U(0x28) 23030567e6SVarun Wadekar #define CTX_GPREG_X6 U(0x30) 24030567e6SVarun Wadekar #define CTX_GPREG_X7 U(0x38) 25030567e6SVarun Wadekar #define CTX_GPREG_X8 U(0x40) 26030567e6SVarun Wadekar #define CTX_GPREG_X9 U(0x48) 27030567e6SVarun Wadekar #define CTX_GPREG_X10 U(0x50) 28030567e6SVarun Wadekar #define CTX_GPREG_X11 U(0x58) 29030567e6SVarun Wadekar #define CTX_GPREG_X12 U(0x60) 30030567e6SVarun Wadekar #define CTX_GPREG_X13 U(0x68) 31030567e6SVarun Wadekar #define CTX_GPREG_X14 U(0x70) 32030567e6SVarun Wadekar #define CTX_GPREG_X15 U(0x78) 33030567e6SVarun Wadekar #define CTX_GPREG_X16 U(0x80) 34030567e6SVarun Wadekar #define CTX_GPREG_X17 U(0x88) 35030567e6SVarun Wadekar #define CTX_GPREG_X18 U(0x90) 36030567e6SVarun Wadekar #define CTX_GPREG_X19 U(0x98) 37030567e6SVarun Wadekar #define CTX_GPREG_X20 U(0xa0) 38030567e6SVarun Wadekar #define CTX_GPREG_X21 U(0xa8) 39030567e6SVarun Wadekar #define CTX_GPREG_X22 U(0xb0) 40030567e6SVarun Wadekar #define CTX_GPREG_X23 U(0xb8) 41030567e6SVarun Wadekar #define CTX_GPREG_X24 U(0xc0) 42030567e6SVarun Wadekar #define CTX_GPREG_X25 U(0xc8) 43030567e6SVarun Wadekar #define CTX_GPREG_X26 U(0xd0) 44030567e6SVarun Wadekar #define CTX_GPREG_X27 U(0xd8) 45030567e6SVarun Wadekar #define CTX_GPREG_X28 U(0xe0) 46030567e6SVarun Wadekar #define CTX_GPREG_X29 U(0xe8) 47030567e6SVarun Wadekar #define CTX_GPREG_LR U(0xf0) 48030567e6SVarun Wadekar #define CTX_GPREG_SP_EL0 U(0xf8) 49030567e6SVarun Wadekar #define CTX_GPREGS_END U(0x100) 50532ed618SSoby Mathew 51532ed618SSoby Mathew /******************************************************************************* 52532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 'el3_state' 53532ed618SSoby Mathew * structure at their correct offsets. Note that some of the registers are only 54532ed618SSoby Mathew * 32-bits wide but are stored as 64-bit values for convenience 55532ed618SSoby Mathew ******************************************************************************/ 56d9bd656cSDimitris Papastamos #define CTX_EL3STATE_OFFSET (CTX_GPREGS_OFFSET + CTX_GPREGS_END) 57030567e6SVarun Wadekar #define CTX_SCR_EL3 U(0x0) 5876454abfSJeenu Viswambharan #define CTX_ESR_EL3 U(0x8) 5976454abfSJeenu Viswambharan #define CTX_RUNTIME_SP U(0x10) 6076454abfSJeenu Viswambharan #define CTX_SPSR_EL3 U(0x18) 6176454abfSJeenu Viswambharan #define CTX_ELR_EL3 U(0x20) 6276454abfSJeenu Viswambharan #define CTX_UNUSED U(0x28) 6376454abfSJeenu Viswambharan #define CTX_EL3STATE_END U(0x30) 64532ed618SSoby Mathew 65532ed618SSoby Mathew /******************************************************************************* 66532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 67532ed618SSoby Mathew * 'el1_sys_regs' structure at their correct offsets. Note that some of the 68532ed618SSoby Mathew * registers are only 32-bits wide but are stored as 64-bit values for 69532ed618SSoby Mathew * convenience 70532ed618SSoby Mathew ******************************************************************************/ 71532ed618SSoby Mathew #define CTX_SYSREGS_OFFSET (CTX_EL3STATE_OFFSET + CTX_EL3STATE_END) 72030567e6SVarun Wadekar #define CTX_SPSR_EL1 U(0x0) 73030567e6SVarun Wadekar #define CTX_ELR_EL1 U(0x8) 74030567e6SVarun Wadekar #define CTX_SCTLR_EL1 U(0x10) 75030567e6SVarun Wadekar #define CTX_ACTLR_EL1 U(0x18) 76030567e6SVarun Wadekar #define CTX_CPACR_EL1 U(0x20) 77030567e6SVarun Wadekar #define CTX_CSSELR_EL1 U(0x28) 78030567e6SVarun Wadekar #define CTX_SP_EL1 U(0x30) 79030567e6SVarun Wadekar #define CTX_ESR_EL1 U(0x38) 80030567e6SVarun Wadekar #define CTX_TTBR0_EL1 U(0x40) 81030567e6SVarun Wadekar #define CTX_TTBR1_EL1 U(0x48) 82030567e6SVarun Wadekar #define CTX_MAIR_EL1 U(0x50) 83030567e6SVarun Wadekar #define CTX_AMAIR_EL1 U(0x58) 84030567e6SVarun Wadekar #define CTX_TCR_EL1 U(0x60) 85030567e6SVarun Wadekar #define CTX_TPIDR_EL1 U(0x68) 86030567e6SVarun Wadekar #define CTX_TPIDR_EL0 U(0x70) 87030567e6SVarun Wadekar #define CTX_TPIDRRO_EL0 U(0x78) 88030567e6SVarun Wadekar #define CTX_PAR_EL1 U(0x80) 89030567e6SVarun Wadekar #define CTX_FAR_EL1 U(0x88) 90030567e6SVarun Wadekar #define CTX_AFSR0_EL1 U(0x90) 91030567e6SVarun Wadekar #define CTX_AFSR1_EL1 U(0x98) 92030567e6SVarun Wadekar #define CTX_CONTEXTIDR_EL1 U(0xa0) 93030567e6SVarun Wadekar #define CTX_VBAR_EL1 U(0xa8) 943e61b2b5SDavid Cunado #define CTX_PMCR_EL0 U(0xb0) 95532ed618SSoby Mathew 96532ed618SSoby Mathew /* 97532ed618SSoby Mathew * If the platform is AArch64-only, there is no need to save and restore these 98532ed618SSoby Mathew * AArch32 registers. 99532ed618SSoby Mathew */ 100532ed618SSoby Mathew #if CTX_INCLUDE_AARCH32_REGS 1013e61b2b5SDavid Cunado #define CTX_SPSR_ABT U(0xc0) /* Align to the next 16 byte boundary */ 1023e61b2b5SDavid Cunado #define CTX_SPSR_UND U(0xc8) 1033e61b2b5SDavid Cunado #define CTX_SPSR_IRQ U(0xd0) 1043e61b2b5SDavid Cunado #define CTX_SPSR_FIQ U(0xd8) 1053e61b2b5SDavid Cunado #define CTX_DACR32_EL2 U(0xe0) 1063e61b2b5SDavid Cunado #define CTX_IFSR32_EL2 U(0xe8) 1074d1ccf0eSAntonio Nino Diaz #define CTX_AARCH32_END U(0xf0) /* Align to the next 16 byte boundary */ 108532ed618SSoby Mathew #else 1094d1ccf0eSAntonio Nino Diaz #define CTX_AARCH32_END U(0xc0) /* Align to the next 16 byte boundary */ 1104d1ccf0eSAntonio Nino Diaz #endif /* CTX_INCLUDE_AARCH32_REGS */ 111532ed618SSoby Mathew 112532ed618SSoby Mathew /* 113532ed618SSoby Mathew * If the timer registers aren't saved and restored, we don't have to reserve 114532ed618SSoby Mathew * space for them in the context 115532ed618SSoby Mathew */ 116532ed618SSoby Mathew #if NS_TIMER_SWITCH 1174d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CTL_EL0 (CTX_AARCH32_END + U(0x0)) 1184d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CVAL_EL0 (CTX_AARCH32_END + U(0x8)) 1194d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CTL_EL0 (CTX_AARCH32_END + U(0x10)) 1204d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CVAL_EL0 (CTX_AARCH32_END + U(0x18)) 1214d1ccf0eSAntonio Nino Diaz #define CTX_CNTKCTL_EL1 (CTX_AARCH32_END + U(0x20)) 1224d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END (CTX_AARCH32_END + U(0x30)) /* Align to the next 16 byte boundary */ 123532ed618SSoby Mathew #else 1244d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END CTX_AARCH32_END 1254d1ccf0eSAntonio Nino Diaz #endif /* NS_TIMER_SWITCH */ 1264d1ccf0eSAntonio Nino Diaz 1274d1ccf0eSAntonio Nino Diaz /* 1284d1ccf0eSAntonio Nino Diaz * End of system registers. 1294d1ccf0eSAntonio Nino Diaz */ 1304d1ccf0eSAntonio Nino Diaz #define CTX_SYSREGS_END CTX_TIMER_SYSREGS_END 131532ed618SSoby Mathew 132532ed618SSoby Mathew /******************************************************************************* 133532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 'fp_regs' 134532ed618SSoby Mathew * structure at their correct offsets. 135532ed618SSoby Mathew ******************************************************************************/ 136532ed618SSoby Mathew #define CTX_FPREGS_OFFSET (CTX_SYSREGS_OFFSET + CTX_SYSREGS_END) 137fe007b2eSDimitris Papastamos #if CTX_INCLUDE_FPREGS 138030567e6SVarun Wadekar #define CTX_FP_Q0 U(0x0) 139030567e6SVarun Wadekar #define CTX_FP_Q1 U(0x10) 140030567e6SVarun Wadekar #define CTX_FP_Q2 U(0x20) 141030567e6SVarun Wadekar #define CTX_FP_Q3 U(0x30) 142030567e6SVarun Wadekar #define CTX_FP_Q4 U(0x40) 143030567e6SVarun Wadekar #define CTX_FP_Q5 U(0x50) 144030567e6SVarun Wadekar #define CTX_FP_Q6 U(0x60) 145030567e6SVarun Wadekar #define CTX_FP_Q7 U(0x70) 146030567e6SVarun Wadekar #define CTX_FP_Q8 U(0x80) 147030567e6SVarun Wadekar #define CTX_FP_Q9 U(0x90) 148030567e6SVarun Wadekar #define CTX_FP_Q10 U(0xa0) 149030567e6SVarun Wadekar #define CTX_FP_Q11 U(0xb0) 150030567e6SVarun Wadekar #define CTX_FP_Q12 U(0xc0) 151030567e6SVarun Wadekar #define CTX_FP_Q13 U(0xd0) 152030567e6SVarun Wadekar #define CTX_FP_Q14 U(0xe0) 153030567e6SVarun Wadekar #define CTX_FP_Q15 U(0xf0) 154030567e6SVarun Wadekar #define CTX_FP_Q16 U(0x100) 155030567e6SVarun Wadekar #define CTX_FP_Q17 U(0x110) 156030567e6SVarun Wadekar #define CTX_FP_Q18 U(0x120) 157030567e6SVarun Wadekar #define CTX_FP_Q19 U(0x130) 158030567e6SVarun Wadekar #define CTX_FP_Q20 U(0x140) 159030567e6SVarun Wadekar #define CTX_FP_Q21 U(0x150) 160030567e6SVarun Wadekar #define CTX_FP_Q22 U(0x160) 161030567e6SVarun Wadekar #define CTX_FP_Q23 U(0x170) 162030567e6SVarun Wadekar #define CTX_FP_Q24 U(0x180) 163030567e6SVarun Wadekar #define CTX_FP_Q25 U(0x190) 164030567e6SVarun Wadekar #define CTX_FP_Q26 U(0x1a0) 165030567e6SVarun Wadekar #define CTX_FP_Q27 U(0x1b0) 166030567e6SVarun Wadekar #define CTX_FP_Q28 U(0x1c0) 167030567e6SVarun Wadekar #define CTX_FP_Q29 U(0x1d0) 168030567e6SVarun Wadekar #define CTX_FP_Q30 U(0x1e0) 169030567e6SVarun Wadekar #define CTX_FP_Q31 U(0x1f0) 170030567e6SVarun Wadekar #define CTX_FP_FPSR U(0x200) 171030567e6SVarun Wadekar #define CTX_FP_FPCR U(0x208) 17291089f36SDavid Cunado #if CTX_INCLUDE_AARCH32_REGS 17391089f36SDavid Cunado #define CTX_FP_FPEXC32_EL2 U(0x210) 17491089f36SDavid Cunado #define CTX_FPREGS_END U(0x220) /* Align to the next 16 byte boundary */ 17591089f36SDavid Cunado #else 17691089f36SDavid Cunado #define CTX_FPREGS_END U(0x210) /* Align to the next 16 byte boundary */ 17791089f36SDavid Cunado #endif 178fe007b2eSDimitris Papastamos #else 179fe007b2eSDimitris Papastamos #define CTX_FPREGS_END U(0) 180532ed618SSoby Mathew #endif 181532ed618SSoby Mathew 1824d1ccf0eSAntonio Nino Diaz /******************************************************************************* 1834d1ccf0eSAntonio Nino Diaz * Registers related to CVE-2018-3639 1844d1ccf0eSAntonio Nino Diaz ******************************************************************************/ 185fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_OFFSET (CTX_FPREGS_OFFSET + CTX_FPREGS_END) 186fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_DISABLE U(0) 187fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_END U(0x10) /* Align to the next 16 byte boundary */ 188fe007b2eSDimitris Papastamos 1895283962eSAntonio Nino Diaz /******************************************************************************* 1905283962eSAntonio Nino Diaz * Registers related to ARMv8.3-PAuth. 1915283962eSAntonio Nino Diaz ******************************************************************************/ 1925283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_OFFSET (CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_END) 1935283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 1945283962eSAntonio Nino Diaz #define CTX_PACIAKEY_LO U(0x0) 1955283962eSAntonio Nino Diaz #define CTX_PACIAKEY_HI U(0x8) 1965283962eSAntonio Nino Diaz #define CTX_PACIBKEY_LO U(0x10) 1975283962eSAntonio Nino Diaz #define CTX_PACIBKEY_HI U(0x18) 1985283962eSAntonio Nino Diaz #define CTX_PACDAKEY_LO U(0x20) 1995283962eSAntonio Nino Diaz #define CTX_PACDAKEY_HI U(0x28) 2005283962eSAntonio Nino Diaz #define CTX_PACDBKEY_LO U(0x30) 2015283962eSAntonio Nino Diaz #define CTX_PACDBKEY_HI U(0x38) 2025283962eSAntonio Nino Diaz #define CTX_PACGAKEY_LO U(0x40) 2035283962eSAntonio Nino Diaz #define CTX_PACGAKEY_HI U(0x48) 2045283962eSAntonio Nino Diaz #define CTX_PACGAKEY_END U(0x50) 2055283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_END U(0x60) /* Align to the next 16 byte boundary */ 2065283962eSAntonio Nino Diaz #else 2075283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_END U(0) 2085283962eSAntonio Nino Diaz #endif /* CTX_INCLUDE_PAUTH_REGS */ 2095283962eSAntonio Nino Diaz 210*d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__ 211532ed618SSoby Mathew 212532ed618SSoby Mathew #include <stdint.h> 213532ed618SSoby Mathew 21409d40e0eSAntonio Nino Diaz #include <lib/cassert.h> 21509d40e0eSAntonio Nino Diaz 216532ed618SSoby Mathew /* 217532ed618SSoby Mathew * Common constants to help define the 'cpu_context' structure and its 218532ed618SSoby Mathew * members below. 219532ed618SSoby Mathew */ 220030567e6SVarun Wadekar #define DWORD_SHIFT U(3) 221532ed618SSoby Mathew #define DEFINE_REG_STRUCT(name, num_regs) \ 222532ed618SSoby Mathew typedef struct name { \ 223532ed618SSoby Mathew uint64_t _regs[num_regs]; \ 224532ed618SSoby Mathew } __aligned(16) name##_t 225532ed618SSoby Mathew 226532ed618SSoby Mathew /* Constants to determine the size of individual context structures */ 227532ed618SSoby Mathew #define CTX_GPREG_ALL (CTX_GPREGS_END >> DWORD_SHIFT) 228532ed618SSoby Mathew #define CTX_SYSREG_ALL (CTX_SYSREGS_END >> DWORD_SHIFT) 229532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 230532ed618SSoby Mathew # define CTX_FPREG_ALL (CTX_FPREGS_END >> DWORD_SHIFT) 231532ed618SSoby Mathew #endif 232532ed618SSoby Mathew #define CTX_EL3STATE_ALL (CTX_EL3STATE_END >> DWORD_SHIFT) 233fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_ALL (CTX_CVE_2018_3639_END >> DWORD_SHIFT) 2345283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 2355283962eSAntonio Nino Diaz # define CTX_PAUTH_REGS_ALL (CTX_PAUTH_REGS_END >> DWORD_SHIFT) 2365283962eSAntonio Nino Diaz #endif 237532ed618SSoby Mathew 238532ed618SSoby Mathew /* 239532ed618SSoby Mathew * AArch64 general purpose register context structure. Usually x0-x18, 240532ed618SSoby Mathew * lr are saved as the compiler is expected to preserve the remaining 241532ed618SSoby Mathew * callee saved registers if used by the C runtime and the assembler 242532ed618SSoby Mathew * does not touch the remaining. But in case of world switch during 243532ed618SSoby Mathew * exception handling, we need to save the callee registers too. 244532ed618SSoby Mathew */ 245532ed618SSoby Mathew DEFINE_REG_STRUCT(gp_regs, CTX_GPREG_ALL); 246532ed618SSoby Mathew 247532ed618SSoby Mathew /* 248532ed618SSoby Mathew * AArch64 EL1 system register context structure for preserving the 249532ed618SSoby Mathew * architectural state during switches from one security state to 250532ed618SSoby Mathew * another in EL1. 251532ed618SSoby Mathew */ 252532ed618SSoby Mathew DEFINE_REG_STRUCT(el1_sys_regs, CTX_SYSREG_ALL); 253532ed618SSoby Mathew 254532ed618SSoby Mathew /* 255532ed618SSoby Mathew * AArch64 floating point register context structure for preserving 256532ed618SSoby Mathew * the floating point state during switches from one security state to 257532ed618SSoby Mathew * another. 258532ed618SSoby Mathew */ 259532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 260532ed618SSoby Mathew DEFINE_REG_STRUCT(fp_regs, CTX_FPREG_ALL); 261532ed618SSoby Mathew #endif 262532ed618SSoby Mathew 263532ed618SSoby Mathew /* 264532ed618SSoby Mathew * Miscellaneous registers used by EL3 firmware to maintain its state 265532ed618SSoby Mathew * across exception entries and exits 266532ed618SSoby Mathew */ 267532ed618SSoby Mathew DEFINE_REG_STRUCT(el3_state, CTX_EL3STATE_ALL); 268532ed618SSoby Mathew 269fe007b2eSDimitris Papastamos /* Function pointer used by CVE-2018-3639 dynamic mitigation */ 270fe007b2eSDimitris Papastamos DEFINE_REG_STRUCT(cve_2018_3639, CTX_CVE_2018_3639_ALL); 271fe007b2eSDimitris Papastamos 2725283962eSAntonio Nino Diaz /* Registers associated to ARMv8.3-PAuth */ 2735283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 2745283962eSAntonio Nino Diaz DEFINE_REG_STRUCT(pauth, CTX_PAUTH_REGS_ALL); 2755283962eSAntonio Nino Diaz #endif 2765283962eSAntonio Nino Diaz 277532ed618SSoby Mathew /* 278532ed618SSoby Mathew * Macros to access members of any of the above structures using their 279532ed618SSoby Mathew * offsets 280532ed618SSoby Mathew */ 281ba6e5ca6SJeenu Viswambharan #define read_ctx_reg(ctx, offset) ((ctx)->_regs[(offset) >> DWORD_SHIFT]) 282ba6e5ca6SJeenu Viswambharan #define write_ctx_reg(ctx, offset, val) (((ctx)->_regs[(offset) >> DWORD_SHIFT]) \ 283ba6e5ca6SJeenu Viswambharan = (uint64_t) (val)) 284532ed618SSoby Mathew 285532ed618SSoby Mathew /* 286532ed618SSoby Mathew * Top-level context structure which is used by EL3 firmware to 287532ed618SSoby Mathew * preserve the state of a core at EL1 in one of the two security 288532ed618SSoby Mathew * states and save enough EL3 meta data to be able to return to that 289532ed618SSoby Mathew * EL and security state. The context management library will be used 290532ed618SSoby Mathew * to ensure that SP_EL3 always points to an instance of this 291532ed618SSoby Mathew * structure at exception entry and exit. Each instance will 292532ed618SSoby Mathew * correspond to either the secure or the non-secure state. 293532ed618SSoby Mathew */ 294532ed618SSoby Mathew typedef struct cpu_context { 295532ed618SSoby Mathew gp_regs_t gpregs_ctx; 296532ed618SSoby Mathew el3_state_t el3state_ctx; 297532ed618SSoby Mathew el1_sys_regs_t sysregs_ctx; 298532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 299532ed618SSoby Mathew fp_regs_t fpregs_ctx; 300532ed618SSoby Mathew #endif 301fe007b2eSDimitris Papastamos cve_2018_3639_t cve_2018_3639_ctx; 3025283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 3035283962eSAntonio Nino Diaz pauth_t pauth_ctx; 3045283962eSAntonio Nino Diaz #endif 305532ed618SSoby Mathew } cpu_context_t; 306532ed618SSoby Mathew 307532ed618SSoby Mathew /* Macros to access members of the 'cpu_context_t' structure */ 308532ed618SSoby Mathew #define get_el3state_ctx(h) (&((cpu_context_t *) h)->el3state_ctx) 309532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 310532ed618SSoby Mathew # define get_fpregs_ctx(h) (&((cpu_context_t *) h)->fpregs_ctx) 311532ed618SSoby Mathew #endif 312532ed618SSoby Mathew #define get_sysregs_ctx(h) (&((cpu_context_t *) h)->sysregs_ctx) 313532ed618SSoby Mathew #define get_gpregs_ctx(h) (&((cpu_context_t *) h)->gpregs_ctx) 3146f03bc77SDimitris Papastamos #define get_cve_2018_3639_ctx(h) (&((cpu_context_t *) h)->cve_2018_3639_ctx) 3155283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 3165283962eSAntonio Nino Diaz # define get_pauth_ctx(h) (&((cpu_context_t *) h)->pauth_ctx) 3175283962eSAntonio Nino Diaz #endif 318532ed618SSoby Mathew 319532ed618SSoby Mathew /* 320532ed618SSoby Mathew * Compile time assertions related to the 'cpu_context' structure to 321532ed618SSoby Mathew * ensure that the assembler and the compiler view of the offsets of 322532ed618SSoby Mathew * the structure members is the same. 323532ed618SSoby Mathew */ 324532ed618SSoby Mathew CASSERT(CTX_GPREGS_OFFSET == __builtin_offsetof(cpu_context_t, gpregs_ctx), \ 325532ed618SSoby Mathew assert_core_context_gp_offset_mismatch); 326532ed618SSoby Mathew CASSERT(CTX_SYSREGS_OFFSET == __builtin_offsetof(cpu_context_t, sysregs_ctx), \ 327532ed618SSoby Mathew assert_core_context_sys_offset_mismatch); 328532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 329532ed618SSoby Mathew CASSERT(CTX_FPREGS_OFFSET == __builtin_offsetof(cpu_context_t, fpregs_ctx), \ 330532ed618SSoby Mathew assert_core_context_fp_offset_mismatch); 331532ed618SSoby Mathew #endif 332532ed618SSoby Mathew CASSERT(CTX_EL3STATE_OFFSET == __builtin_offsetof(cpu_context_t, el3state_ctx), \ 333532ed618SSoby Mathew assert_core_context_el3state_offset_mismatch); 334fe007b2eSDimitris Papastamos CASSERT(CTX_CVE_2018_3639_OFFSET == __builtin_offsetof(cpu_context_t, cve_2018_3639_ctx), \ 335fe007b2eSDimitris Papastamos assert_core_context_cve_2018_3639_offset_mismatch); 3365283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 3375283962eSAntonio Nino Diaz CASSERT(CTX_PAUTH_REGS_OFFSET == __builtin_offsetof(cpu_context_t, pauth_ctx), \ 3385283962eSAntonio Nino Diaz assert_core_context_pauth_offset_mismatch); 3395283962eSAntonio Nino Diaz #endif 340532ed618SSoby Mathew 341532ed618SSoby Mathew /* 342532ed618SSoby Mathew * Helper macro to set the general purpose registers that correspond to 343532ed618SSoby Mathew * parameters in an aapcs_64 call i.e. x0-x7 344532ed618SSoby Mathew */ 345532ed618SSoby Mathew #define set_aapcs_args0(ctx, x0) do { \ 346532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X0, x0); \ 347532ed618SSoby Mathew } while (0) 348532ed618SSoby Mathew #define set_aapcs_args1(ctx, x0, x1) do { \ 349532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X1, x1); \ 350532ed618SSoby Mathew set_aapcs_args0(ctx, x0); \ 351532ed618SSoby Mathew } while (0) 352532ed618SSoby Mathew #define set_aapcs_args2(ctx, x0, x1, x2) do { \ 353532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X2, x2); \ 354532ed618SSoby Mathew set_aapcs_args1(ctx, x0, x1); \ 355532ed618SSoby Mathew } while (0) 356532ed618SSoby Mathew #define set_aapcs_args3(ctx, x0, x1, x2, x3) do { \ 357532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X3, x3); \ 358532ed618SSoby Mathew set_aapcs_args2(ctx, x0, x1, x2); \ 359532ed618SSoby Mathew } while (0) 360532ed618SSoby Mathew #define set_aapcs_args4(ctx, x0, x1, x2, x3, x4) do { \ 361532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X4, x4); \ 362532ed618SSoby Mathew set_aapcs_args3(ctx, x0, x1, x2, x3); \ 363532ed618SSoby Mathew } while (0) 364532ed618SSoby Mathew #define set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5) do { \ 365532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X5, x5); \ 366532ed618SSoby Mathew set_aapcs_args4(ctx, x0, x1, x2, x3, x4); \ 367532ed618SSoby Mathew } while (0) 368532ed618SSoby Mathew #define set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6) do { \ 369532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X6, x6); \ 370532ed618SSoby Mathew set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5); \ 371532ed618SSoby Mathew } while (0) 372532ed618SSoby Mathew #define set_aapcs_args7(ctx, x0, x1, x2, x3, x4, x5, x6, x7) do { \ 373532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X7, x7); \ 374532ed618SSoby Mathew set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6); \ 375532ed618SSoby Mathew } while (0) 376532ed618SSoby Mathew 377532ed618SSoby Mathew /******************************************************************************* 378532ed618SSoby Mathew * Function prototypes 379532ed618SSoby Mathew ******************************************************************************/ 380532ed618SSoby Mathew void el1_sysregs_context_save(el1_sys_regs_t *regs); 381532ed618SSoby Mathew void el1_sysregs_context_restore(el1_sys_regs_t *regs); 382532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 383532ed618SSoby Mathew void fpregs_context_save(fp_regs_t *regs); 384532ed618SSoby Mathew void fpregs_context_restore(fp_regs_t *regs); 385532ed618SSoby Mathew #endif 386532ed618SSoby Mathew 387*d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */ 388532ed618SSoby Mathew 389a0fee747SAntonio Nino Diaz #endif /* CONTEXT_H */ 390