1532ed618SSoby Mathew /* 28c56a788SJayanth Dodderi Chidanand * Copyright (c) 2013-2024, 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 10461c0a5dSElizabeth Ho #include <lib/el3_runtime/cpu_data.h> 1109d40e0eSAntonio Nino Diaz #include <lib/utils_def.h> 1276454abfSJeenu Viswambharan 13532ed618SSoby Mathew /******************************************************************************* 14532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 'gp_regs' 15532ed618SSoby Mathew * structure at their correct offsets. 16532ed618SSoby Mathew ******************************************************************************/ 17030567e6SVarun Wadekar #define CTX_GPREGS_OFFSET U(0x0) 18030567e6SVarun Wadekar #define CTX_GPREG_X0 U(0x0) 19030567e6SVarun Wadekar #define CTX_GPREG_X1 U(0x8) 20030567e6SVarun Wadekar #define CTX_GPREG_X2 U(0x10) 21030567e6SVarun Wadekar #define CTX_GPREG_X3 U(0x18) 22030567e6SVarun Wadekar #define CTX_GPREG_X4 U(0x20) 23030567e6SVarun Wadekar #define CTX_GPREG_X5 U(0x28) 24030567e6SVarun Wadekar #define CTX_GPREG_X6 U(0x30) 25030567e6SVarun Wadekar #define CTX_GPREG_X7 U(0x38) 26030567e6SVarun Wadekar #define CTX_GPREG_X8 U(0x40) 27030567e6SVarun Wadekar #define CTX_GPREG_X9 U(0x48) 28030567e6SVarun Wadekar #define CTX_GPREG_X10 U(0x50) 29030567e6SVarun Wadekar #define CTX_GPREG_X11 U(0x58) 30030567e6SVarun Wadekar #define CTX_GPREG_X12 U(0x60) 31030567e6SVarun Wadekar #define CTX_GPREG_X13 U(0x68) 32030567e6SVarun Wadekar #define CTX_GPREG_X14 U(0x70) 33030567e6SVarun Wadekar #define CTX_GPREG_X15 U(0x78) 34030567e6SVarun Wadekar #define CTX_GPREG_X16 U(0x80) 35030567e6SVarun Wadekar #define CTX_GPREG_X17 U(0x88) 36030567e6SVarun Wadekar #define CTX_GPREG_X18 U(0x90) 37030567e6SVarun Wadekar #define CTX_GPREG_X19 U(0x98) 38030567e6SVarun Wadekar #define CTX_GPREG_X20 U(0xa0) 39030567e6SVarun Wadekar #define CTX_GPREG_X21 U(0xa8) 40030567e6SVarun Wadekar #define CTX_GPREG_X22 U(0xb0) 41030567e6SVarun Wadekar #define CTX_GPREG_X23 U(0xb8) 42030567e6SVarun Wadekar #define CTX_GPREG_X24 U(0xc0) 43030567e6SVarun Wadekar #define CTX_GPREG_X25 U(0xc8) 44030567e6SVarun Wadekar #define CTX_GPREG_X26 U(0xd0) 45030567e6SVarun Wadekar #define CTX_GPREG_X27 U(0xd8) 46030567e6SVarun Wadekar #define CTX_GPREG_X28 U(0xe0) 47030567e6SVarun Wadekar #define CTX_GPREG_X29 U(0xe8) 48030567e6SVarun Wadekar #define CTX_GPREG_LR U(0xf0) 49030567e6SVarun Wadekar #define CTX_GPREG_SP_EL0 U(0xf8) 50030567e6SVarun Wadekar #define CTX_GPREGS_END U(0x100) 51532ed618SSoby Mathew 52532ed618SSoby Mathew /******************************************************************************* 53532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 'el3_state' 54532ed618SSoby Mathew * structure at their correct offsets. Note that some of the registers are only 55532ed618SSoby Mathew * 32-bits wide but are stored as 64-bit values for convenience 56532ed618SSoby Mathew ******************************************************************************/ 57d9bd656cSDimitris Papastamos #define CTX_EL3STATE_OFFSET (CTX_GPREGS_OFFSET + CTX_GPREGS_END) 58030567e6SVarun Wadekar #define CTX_SCR_EL3 U(0x0) 5976454abfSJeenu Viswambharan #define CTX_ESR_EL3 U(0x8) 6076454abfSJeenu Viswambharan #define CTX_RUNTIME_SP U(0x10) 6176454abfSJeenu Viswambharan #define CTX_SPSR_EL3 U(0x18) 6276454abfSJeenu Viswambharan #define CTX_ELR_EL3 U(0x20) 63e290a8fcSAlexei Fedorov #define CTX_PMCR_EL0 U(0x28) 64c2d32a5fSMadhukar Pappireddy #define CTX_IS_IN_EL3 U(0x30) 65d04c04a4SManish Pandey /* Constants required in supporting nested exception in EL3 */ 66ac4f6aafSArvind Ram Prakash #define CTX_SAVED_ELR_EL3 U(0x38) 67d04c04a4SManish Pandey /* 68d04c04a4SManish Pandey * General purpose flag, to save various EL3 states 69d04c04a4SManish Pandey * FFH mode : Used to identify if handling nested exception 70d04c04a4SManish Pandey * KFH mode : Used as counter value 71d04c04a4SManish Pandey */ 72ac4f6aafSArvind Ram Prakash #define CTX_NESTED_EA_FLAG U(0x40) 73f87e54f7SManish Pandey #if FFH_SUPPORT 74ac4f6aafSArvind Ram Prakash #define CTX_SAVED_ESR_EL3 U(0x48) 75ac4f6aafSArvind Ram Prakash #define CTX_SAVED_SPSR_EL3 U(0x50) 76ac4f6aafSArvind Ram Prakash #define CTX_SAVED_GPREG_LR U(0x58) 77ac4f6aafSArvind Ram Prakash #define CTX_EL3STATE_END U(0x60) /* Align to the next 16 byte boundary */ 78d04c04a4SManish Pandey #else 79d04c04a4SManish Pandey #define CTX_EL3STATE_END U(0x50) /* Align to the next 16 byte boundary */ 80ac4f6aafSArvind Ram Prakash #endif /* FFH_SUPPORT */ 81532ed618SSoby Mathew 82532ed618SSoby Mathew /******************************************************************************* 83532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 84532ed618SSoby Mathew * 'el1_sys_regs' structure at their correct offsets. Note that some of the 85532ed618SSoby Mathew * registers are only 32-bits wide but are stored as 64-bit values for 86532ed618SSoby Mathew * convenience 87532ed618SSoby Mathew ******************************************************************************/ 882825946eSMax Shvetsov #define CTX_EL1_SYSREGS_OFFSET (CTX_EL3STATE_OFFSET + CTX_EL3STATE_END) 89030567e6SVarun Wadekar #define CTX_SPSR_EL1 U(0x0) 90030567e6SVarun Wadekar #define CTX_ELR_EL1 U(0x8) 91030567e6SVarun Wadekar #define CTX_SCTLR_EL1 U(0x10) 92cb55615cSManish V Badarkhe #define CTX_TCR_EL1 U(0x18) 93030567e6SVarun Wadekar #define CTX_CPACR_EL1 U(0x20) 94030567e6SVarun Wadekar #define CTX_CSSELR_EL1 U(0x28) 95030567e6SVarun Wadekar #define CTX_SP_EL1 U(0x30) 96030567e6SVarun Wadekar #define CTX_ESR_EL1 U(0x38) 97030567e6SVarun Wadekar #define CTX_TTBR0_EL1 U(0x40) 98030567e6SVarun Wadekar #define CTX_TTBR1_EL1 U(0x48) 99030567e6SVarun Wadekar #define CTX_MAIR_EL1 U(0x50) 100030567e6SVarun Wadekar #define CTX_AMAIR_EL1 U(0x58) 101cb55615cSManish V Badarkhe #define CTX_ACTLR_EL1 U(0x60) 102030567e6SVarun Wadekar #define CTX_TPIDR_EL1 U(0x68) 103030567e6SVarun Wadekar #define CTX_TPIDR_EL0 U(0x70) 104030567e6SVarun Wadekar #define CTX_TPIDRRO_EL0 U(0x78) 105030567e6SVarun Wadekar #define CTX_PAR_EL1 U(0x80) 106030567e6SVarun Wadekar #define CTX_FAR_EL1 U(0x88) 107030567e6SVarun Wadekar #define CTX_AFSR0_EL1 U(0x90) 108030567e6SVarun Wadekar #define CTX_AFSR1_EL1 U(0x98) 109030567e6SVarun Wadekar #define CTX_CONTEXTIDR_EL1 U(0xa0) 110030567e6SVarun Wadekar #define CTX_VBAR_EL1 U(0xa8) 111532ed618SSoby Mathew 112532ed618SSoby Mathew /* 113532ed618SSoby Mathew * If the platform is AArch64-only, there is no need to save and restore these 114532ed618SSoby Mathew * AArch32 registers. 115532ed618SSoby Mathew */ 116532ed618SSoby Mathew #if CTX_INCLUDE_AARCH32_REGS 117e290a8fcSAlexei Fedorov #define CTX_SPSR_ABT U(0xb0) /* Align to the next 16 byte boundary */ 118e290a8fcSAlexei Fedorov #define CTX_SPSR_UND U(0xb8) 119e290a8fcSAlexei Fedorov #define CTX_SPSR_IRQ U(0xc0) 120e290a8fcSAlexei Fedorov #define CTX_SPSR_FIQ U(0xc8) 121e290a8fcSAlexei Fedorov #define CTX_DACR32_EL2 U(0xd0) 122e290a8fcSAlexei Fedorov #define CTX_IFSR32_EL2 U(0xd8) 123e290a8fcSAlexei Fedorov #define CTX_AARCH32_END U(0xe0) /* Align to the next 16 byte boundary */ 124532ed618SSoby Mathew #else 125e290a8fcSAlexei Fedorov #define CTX_AARCH32_END U(0xb0) /* Align to the next 16 byte boundary */ 1264d1ccf0eSAntonio Nino Diaz #endif /* CTX_INCLUDE_AARCH32_REGS */ 127532ed618SSoby Mathew 128532ed618SSoby Mathew /* 129532ed618SSoby Mathew * If the timer registers aren't saved and restored, we don't have to reserve 130532ed618SSoby Mathew * space for them in the context 131532ed618SSoby Mathew */ 132532ed618SSoby Mathew #if NS_TIMER_SWITCH 1334d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CTL_EL0 (CTX_AARCH32_END + U(0x0)) 1344d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CVAL_EL0 (CTX_AARCH32_END + U(0x8)) 1354d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CTL_EL0 (CTX_AARCH32_END + U(0x10)) 1364d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CVAL_EL0 (CTX_AARCH32_END + U(0x18)) 1374d1ccf0eSAntonio Nino Diaz #define CTX_CNTKCTL_EL1 (CTX_AARCH32_END + U(0x20)) 1384d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END (CTX_AARCH32_END + U(0x30)) /* Align to the next 16 byte boundary */ 139532ed618SSoby Mathew #else 1404d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END CTX_AARCH32_END 1414d1ccf0eSAntonio Nino Diaz #endif /* NS_TIMER_SWITCH */ 1424d1ccf0eSAntonio Nino Diaz 143*0a33adc0SGovindraj Raja #if ENABLE_FEAT_MTE 1449dd94382SJustin Chadwell #define CTX_TFSRE0_EL1 (CTX_TIMER_SYSREGS_END + U(0x0)) 1459dd94382SJustin Chadwell #define CTX_TFSR_EL1 (CTX_TIMER_SYSREGS_END + U(0x8)) 1469dd94382SJustin Chadwell #define CTX_RGSR_EL1 (CTX_TIMER_SYSREGS_END + U(0x10)) 1479dd94382SJustin Chadwell #define CTX_GCR_EL1 (CTX_TIMER_SYSREGS_END + U(0x18)) 1489dd94382SJustin Chadwell 1499dd94382SJustin Chadwell /* Align to the next 16 byte boundary */ 1509dd94382SJustin Chadwell #define CTX_MTE_REGS_END (CTX_TIMER_SYSREGS_END + U(0x20)) 1519dd94382SJustin Chadwell #else 1529dd94382SJustin Chadwell #define CTX_MTE_REGS_END CTX_TIMER_SYSREGS_END 153*0a33adc0SGovindraj Raja #endif /* ENABLE_FEAT_MTE */ 1549dd94382SJustin Chadwell 1554d1ccf0eSAntonio Nino Diaz /* 1562825946eSMax Shvetsov * End of system registers. 1572825946eSMax Shvetsov */ 1582825946eSMax Shvetsov #define CTX_EL1_SYSREGS_END CTX_MTE_REGS_END 1592825946eSMax Shvetsov 1602825946eSMax Shvetsov /* 1612825946eSMax Shvetsov * EL2 register set 16228f39f02SMax Shvetsov */ 16328f39f02SMax Shvetsov 16428f39f02SMax Shvetsov #if CTX_INCLUDE_EL2_REGS 16528f39f02SMax Shvetsov /* For later discussion 16628f39f02SMax Shvetsov * ICH_AP0R<n>_EL2 16728f39f02SMax Shvetsov * ICH_AP1R<n>_EL2 16828f39f02SMax Shvetsov * AMEVCNTVOFF0<n>_EL2 16928f39f02SMax Shvetsov * AMEVCNTVOFF1<n>_EL2 17028f39f02SMax Shvetsov * ICH_LR<n>_EL2 17128f39f02SMax Shvetsov */ 1722825946eSMax Shvetsov #define CTX_EL2_SYSREGS_OFFSET (CTX_EL1_SYSREGS_OFFSET + CTX_EL1_SYSREGS_END) 17328f39f02SMax Shvetsov 1742825946eSMax Shvetsov #define CTX_ACTLR_EL2 U(0x0) 1752825946eSMax Shvetsov #define CTX_AFSR0_EL2 U(0x8) 1762825946eSMax Shvetsov #define CTX_AFSR1_EL2 U(0x10) 1772825946eSMax Shvetsov #define CTX_AMAIR_EL2 U(0x18) 1782825946eSMax Shvetsov #define CTX_CNTHCTL_EL2 U(0x20) 179a7cf2743SMax Shvetsov #define CTX_CNTVOFF_EL2 U(0x28) 180a7cf2743SMax Shvetsov #define CTX_CPTR_EL2 U(0x30) 181a7cf2743SMax Shvetsov #define CTX_DBGVCR32_EL2 U(0x38) 182a7cf2743SMax Shvetsov #define CTX_ELR_EL2 U(0x40) 183a7cf2743SMax Shvetsov #define CTX_ESR_EL2 U(0x48) 184a7cf2743SMax Shvetsov #define CTX_FAR_EL2 U(0x50) 185a7cf2743SMax Shvetsov #define CTX_HACR_EL2 U(0x58) 186a7cf2743SMax Shvetsov #define CTX_HCR_EL2 U(0x60) 187a7cf2743SMax Shvetsov #define CTX_HPFAR_EL2 U(0x68) 188a7cf2743SMax Shvetsov #define CTX_HSTR_EL2 U(0x70) 189a7cf2743SMax Shvetsov #define CTX_ICC_SRE_EL2 U(0x78) 190a7cf2743SMax Shvetsov #define CTX_ICH_HCR_EL2 U(0x80) 191a7cf2743SMax Shvetsov #define CTX_ICH_VMCR_EL2 U(0x88) 192a7cf2743SMax Shvetsov #define CTX_MAIR_EL2 U(0x90) 193a7cf2743SMax Shvetsov #define CTX_MDCR_EL2 U(0x98) 194a7cf2743SMax Shvetsov #define CTX_PMSCR_EL2 U(0xa0) 195a7cf2743SMax Shvetsov #define CTX_SCTLR_EL2 U(0xa8) 196a7cf2743SMax Shvetsov #define CTX_SPSR_EL2 U(0xb0) 197a7cf2743SMax Shvetsov #define CTX_SP_EL2 U(0xb8) 198a7cf2743SMax Shvetsov #define CTX_TCR_EL2 U(0xc0) 199a7cf2743SMax Shvetsov #define CTX_TPIDR_EL2 U(0xc8) 200a7cf2743SMax Shvetsov #define CTX_TTBR0_EL2 U(0xd0) 201a7cf2743SMax Shvetsov #define CTX_VBAR_EL2 U(0xd8) 202a7cf2743SMax Shvetsov #define CTX_VMPIDR_EL2 U(0xe0) 203a7cf2743SMax Shvetsov #define CTX_VPIDR_EL2 U(0xe8) 204a7cf2743SMax Shvetsov #define CTX_VTCR_EL2 U(0xf0) 205a7cf2743SMax Shvetsov #define CTX_VTTBR_EL2 U(0xf8) 2062825946eSMax Shvetsov 2072825946eSMax Shvetsov // Only if MTE registers in use 208a7cf2743SMax Shvetsov #define CTX_TFSR_EL2 U(0x100) 2092825946eSMax Shvetsov 2102825946eSMax Shvetsov // Starting with Armv8.6 2118c56a788SJayanth Dodderi Chidanand #define CTX_HDFGRTR_EL2 U(0x108) 2128c56a788SJayanth Dodderi Chidanand #define CTX_HAFGRTR_EL2 U(0x110) 2138c56a788SJayanth Dodderi Chidanand #define CTX_HDFGWTR_EL2 U(0x118) 2148c56a788SJayanth Dodderi Chidanand #define CTX_HFGITR_EL2 U(0x120) 2158c56a788SJayanth Dodderi Chidanand #define CTX_HFGRTR_EL2 U(0x128) 2168c56a788SJayanth Dodderi Chidanand #define CTX_HFGWTR_EL2 U(0x130) 2178c56a788SJayanth Dodderi Chidanand #define CTX_CNTPOFF_EL2 U(0x138) 2182825946eSMax Shvetsov 2192825946eSMax Shvetsov // Starting with Armv8.4 2208c56a788SJayanth Dodderi Chidanand #define CTX_CONTEXTIDR_EL2 U(0x140) 2218c56a788SJayanth Dodderi Chidanand #define CTX_TTBR1_EL2 U(0x148) 2228c56a788SJayanth Dodderi Chidanand #define CTX_VDISR_EL2 U(0x150) 2238c56a788SJayanth Dodderi Chidanand #define CTX_VSESR_EL2 U(0x158) 2248c56a788SJayanth Dodderi Chidanand #define CTX_VNCR_EL2 U(0x160) 2258c56a788SJayanth Dodderi Chidanand #define CTX_TRFCR_EL2 U(0x168) 2262825946eSMax Shvetsov 2272825946eSMax Shvetsov // Starting with Armv8.5 2288c56a788SJayanth Dodderi Chidanand #define CTX_SCXTNUM_EL2 U(0x170) 229cb4ec47bSjohpow01 230cb4ec47bSjohpow01 // Register for FEAT_HCX 2318c56a788SJayanth Dodderi Chidanand #define CTX_HCRX_EL2 U(0x178) 232cb4ec47bSjohpow01 233d3331603SMark Brown // Starting with Armv8.9 2348c56a788SJayanth Dodderi Chidanand #define CTX_TCR2_EL2 U(0x180) 2358c56a788SJayanth Dodderi Chidanand #define CTX_POR_EL2 U(0x188) 2368c56a788SJayanth Dodderi Chidanand #define CTX_PIRE0_EL2 U(0x190) 2378c56a788SJayanth Dodderi Chidanand #define CTX_PIR_EL2 U(0x198) 2388c56a788SJayanth Dodderi Chidanand #define CTX_S2PIR_EL2 U(0x1a0) 2398c56a788SJayanth Dodderi Chidanand #define CTX_GCSCR_EL2 U(0x1a8) 2408c56a788SJayanth Dodderi Chidanand #define CTX_GCSPR_EL2 U(0x1b0) 241d3331603SMark Brown 24228f39f02SMax Shvetsov /* Align to the next 16 byte boundary */ 2438c56a788SJayanth Dodderi Chidanand #define CTX_EL2_SYSREGS_END U(0x1c0) 2447f164a83SOlivier Deprez 24528f39f02SMax Shvetsov #endif /* CTX_INCLUDE_EL2_REGS */ 24628f39f02SMax Shvetsov 247532ed618SSoby Mathew /******************************************************************************* 248532ed618SSoby Mathew * Constants that allow assembler code to access members of and the 'fp_regs' 249532ed618SSoby Mathew * structure at their correct offsets. 250532ed618SSoby Mathew ******************************************************************************/ 2512825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS 2522825946eSMax Shvetsov # define CTX_FPREGS_OFFSET (CTX_EL2_SYSREGS_OFFSET + CTX_EL2_SYSREGS_END) 2532825946eSMax Shvetsov #else 2542825946eSMax Shvetsov # define CTX_FPREGS_OFFSET (CTX_EL1_SYSREGS_OFFSET + CTX_EL1_SYSREGS_END) 2552825946eSMax Shvetsov #endif 256fe007b2eSDimitris Papastamos #if CTX_INCLUDE_FPREGS 257030567e6SVarun Wadekar #define CTX_FP_Q0 U(0x0) 258030567e6SVarun Wadekar #define CTX_FP_Q1 U(0x10) 259030567e6SVarun Wadekar #define CTX_FP_Q2 U(0x20) 260030567e6SVarun Wadekar #define CTX_FP_Q3 U(0x30) 261030567e6SVarun Wadekar #define CTX_FP_Q4 U(0x40) 262030567e6SVarun Wadekar #define CTX_FP_Q5 U(0x50) 263030567e6SVarun Wadekar #define CTX_FP_Q6 U(0x60) 264030567e6SVarun Wadekar #define CTX_FP_Q7 U(0x70) 265030567e6SVarun Wadekar #define CTX_FP_Q8 U(0x80) 266030567e6SVarun Wadekar #define CTX_FP_Q9 U(0x90) 267030567e6SVarun Wadekar #define CTX_FP_Q10 U(0xa0) 268030567e6SVarun Wadekar #define CTX_FP_Q11 U(0xb0) 269030567e6SVarun Wadekar #define CTX_FP_Q12 U(0xc0) 270030567e6SVarun Wadekar #define CTX_FP_Q13 U(0xd0) 271030567e6SVarun Wadekar #define CTX_FP_Q14 U(0xe0) 272030567e6SVarun Wadekar #define CTX_FP_Q15 U(0xf0) 273030567e6SVarun Wadekar #define CTX_FP_Q16 U(0x100) 274030567e6SVarun Wadekar #define CTX_FP_Q17 U(0x110) 275030567e6SVarun Wadekar #define CTX_FP_Q18 U(0x120) 276030567e6SVarun Wadekar #define CTX_FP_Q19 U(0x130) 277030567e6SVarun Wadekar #define CTX_FP_Q20 U(0x140) 278030567e6SVarun Wadekar #define CTX_FP_Q21 U(0x150) 279030567e6SVarun Wadekar #define CTX_FP_Q22 U(0x160) 280030567e6SVarun Wadekar #define CTX_FP_Q23 U(0x170) 281030567e6SVarun Wadekar #define CTX_FP_Q24 U(0x180) 282030567e6SVarun Wadekar #define CTX_FP_Q25 U(0x190) 283030567e6SVarun Wadekar #define CTX_FP_Q26 U(0x1a0) 284030567e6SVarun Wadekar #define CTX_FP_Q27 U(0x1b0) 285030567e6SVarun Wadekar #define CTX_FP_Q28 U(0x1c0) 286030567e6SVarun Wadekar #define CTX_FP_Q29 U(0x1d0) 287030567e6SVarun Wadekar #define CTX_FP_Q30 U(0x1e0) 288030567e6SVarun Wadekar #define CTX_FP_Q31 U(0x1f0) 289030567e6SVarun Wadekar #define CTX_FP_FPSR U(0x200) 290030567e6SVarun Wadekar #define CTX_FP_FPCR U(0x208) 29191089f36SDavid Cunado #if CTX_INCLUDE_AARCH32_REGS 29291089f36SDavid Cunado #define CTX_FP_FPEXC32_EL2 U(0x210) 29391089f36SDavid Cunado #define CTX_FPREGS_END U(0x220) /* Align to the next 16 byte boundary */ 29491089f36SDavid Cunado #else 29591089f36SDavid Cunado #define CTX_FPREGS_END U(0x210) /* Align to the next 16 byte boundary */ 29691089f36SDavid Cunado #endif 297fe007b2eSDimitris Papastamos #else 298fe007b2eSDimitris Papastamos #define CTX_FPREGS_END U(0) 299532ed618SSoby Mathew #endif 300532ed618SSoby Mathew 3014d1ccf0eSAntonio Nino Diaz /******************************************************************************* 3024d1ccf0eSAntonio Nino Diaz * Registers related to CVE-2018-3639 3034d1ccf0eSAntonio Nino Diaz ******************************************************************************/ 304fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_OFFSET (CTX_FPREGS_OFFSET + CTX_FPREGS_END) 305fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_DISABLE U(0) 306fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_END U(0x10) /* Align to the next 16 byte boundary */ 307fe007b2eSDimitris Papastamos 3085283962eSAntonio Nino Diaz /******************************************************************************* 3095283962eSAntonio Nino Diaz * Registers related to ARMv8.3-PAuth. 3105283962eSAntonio Nino Diaz ******************************************************************************/ 3115283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_OFFSET (CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_END) 3125283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 3135283962eSAntonio Nino Diaz #define CTX_PACIAKEY_LO U(0x0) 3145283962eSAntonio Nino Diaz #define CTX_PACIAKEY_HI U(0x8) 3155283962eSAntonio Nino Diaz #define CTX_PACIBKEY_LO U(0x10) 3165283962eSAntonio Nino Diaz #define CTX_PACIBKEY_HI U(0x18) 3175283962eSAntonio Nino Diaz #define CTX_PACDAKEY_LO U(0x20) 3185283962eSAntonio Nino Diaz #define CTX_PACDAKEY_HI U(0x28) 3195283962eSAntonio Nino Diaz #define CTX_PACDBKEY_LO U(0x30) 3205283962eSAntonio Nino Diaz #define CTX_PACDBKEY_HI U(0x38) 3215283962eSAntonio Nino Diaz #define CTX_PACGAKEY_LO U(0x40) 3225283962eSAntonio Nino Diaz #define CTX_PACGAKEY_HI U(0x48) 323ed108b56SAlexei Fedorov #define CTX_PAUTH_REGS_END U(0x50) /* Align to the next 16 byte boundary */ 3245283962eSAntonio Nino Diaz #else 3255283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_END U(0) 3265283962eSAntonio Nino Diaz #endif /* CTX_INCLUDE_PAUTH_REGS */ 3275283962eSAntonio Nino Diaz 328461c0a5dSElizabeth Ho /******************************************************************************* 3299acff28aSArvind Ram Prakash * Registers related to ARMv8.2-MPAM. 3309acff28aSArvind Ram Prakash ******************************************************************************/ 3319acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_OFFSET (CTX_PAUTH_REGS_OFFSET + CTX_PAUTH_REGS_END) 3329acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS 3339acff28aSArvind Ram Prakash #define CTX_MPAM2_EL2 U(0x0) 3349acff28aSArvind Ram Prakash #define CTX_MPAMHCR_EL2 U(0x8) 3359acff28aSArvind Ram Prakash #define CTX_MPAMVPM0_EL2 U(0x10) 3369acff28aSArvind Ram Prakash #define CTX_MPAMVPM1_EL2 U(0x18) 3379acff28aSArvind Ram Prakash #define CTX_MPAMVPM2_EL2 U(0x20) 3389acff28aSArvind Ram Prakash #define CTX_MPAMVPM3_EL2 U(0x28) 3399acff28aSArvind Ram Prakash #define CTX_MPAMVPM4_EL2 U(0x30) 3409acff28aSArvind Ram Prakash #define CTX_MPAMVPM5_EL2 U(0x38) 3419acff28aSArvind Ram Prakash #define CTX_MPAMVPM6_EL2 U(0x40) 3429acff28aSArvind Ram Prakash #define CTX_MPAMVPM7_EL2 U(0x48) 3439acff28aSArvind Ram Prakash #define CTX_MPAMVPMV_EL2 U(0x50) 3449acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END U(0x60) 3459acff28aSArvind Ram Prakash #else 3469acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END U(0x0) 3479acff28aSArvind Ram Prakash #endif /* CTX_INCLUDE_MPAM_REGS */ 3489acff28aSArvind Ram Prakash 3499acff28aSArvind Ram Prakash /******************************************************************************* 350461c0a5dSElizabeth Ho * Registers initialised in a per-world context. 351461c0a5dSElizabeth Ho ******************************************************************************/ 352461c0a5dSElizabeth Ho #define CTX_CPTR_EL3 U(0x0) 353461c0a5dSElizabeth Ho #define CTX_ZCR_EL3 U(0x8) 354ac4f6aafSArvind Ram Prakash #define CTX_MPAM3_EL3 U(0x10) 355ac4f6aafSArvind Ram Prakash #define CTX_PERWORLD_EL3STATE_END U(0x18) 356461c0a5dSElizabeth Ho 357d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__ 358532ed618SSoby Mathew 359532ed618SSoby Mathew #include <stdint.h> 360532ed618SSoby Mathew 36109d40e0eSAntonio Nino Diaz #include <lib/cassert.h> 36209d40e0eSAntonio Nino Diaz 363532ed618SSoby Mathew /* 364532ed618SSoby Mathew * Common constants to help define the 'cpu_context' structure and its 365532ed618SSoby Mathew * members below. 366532ed618SSoby Mathew */ 367030567e6SVarun Wadekar #define DWORD_SHIFT U(3) 368532ed618SSoby Mathew #define DEFINE_REG_STRUCT(name, num_regs) \ 369532ed618SSoby Mathew typedef struct name { \ 3702fe75a2dSZelalem uint64_t ctx_regs[num_regs]; \ 371532ed618SSoby Mathew } __aligned(16) name##_t 372532ed618SSoby Mathew 373532ed618SSoby Mathew /* Constants to determine the size of individual context structures */ 374532ed618SSoby Mathew #define CTX_GPREG_ALL (CTX_GPREGS_END >> DWORD_SHIFT) 3752825946eSMax Shvetsov #define CTX_EL1_SYSREGS_ALL (CTX_EL1_SYSREGS_END >> DWORD_SHIFT) 3762825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS 3772825946eSMax Shvetsov # define CTX_EL2_SYSREGS_ALL (CTX_EL2_SYSREGS_END >> DWORD_SHIFT) 3782825946eSMax Shvetsov #endif 379532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 380532ed618SSoby Mathew # define CTX_FPREG_ALL (CTX_FPREGS_END >> DWORD_SHIFT) 381532ed618SSoby Mathew #endif 382532ed618SSoby Mathew #define CTX_EL3STATE_ALL (CTX_EL3STATE_END >> DWORD_SHIFT) 383fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_ALL (CTX_CVE_2018_3639_END >> DWORD_SHIFT) 3845283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 3855283962eSAntonio Nino Diaz # define CTX_PAUTH_REGS_ALL (CTX_PAUTH_REGS_END >> DWORD_SHIFT) 3865283962eSAntonio Nino Diaz #endif 3879acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS 3889acff28aSArvind Ram Prakash # define CTX_MPAM_REGS_ALL (CTX_MPAM_REGS_END >> DWORD_SHIFT) 3899acff28aSArvind Ram Prakash #endif 390532ed618SSoby Mathew 391532ed618SSoby Mathew /* 392532ed618SSoby Mathew * AArch64 general purpose register context structure. Usually x0-x18, 393532ed618SSoby Mathew * lr are saved as the compiler is expected to preserve the remaining 394532ed618SSoby Mathew * callee saved registers if used by the C runtime and the assembler 395532ed618SSoby Mathew * does not touch the remaining. But in case of world switch during 396532ed618SSoby Mathew * exception handling, we need to save the callee registers too. 397532ed618SSoby Mathew */ 398532ed618SSoby Mathew DEFINE_REG_STRUCT(gp_regs, CTX_GPREG_ALL); 399532ed618SSoby Mathew 400532ed618SSoby Mathew /* 4012825946eSMax Shvetsov * AArch64 EL1 system register context structure for preserving the 40228f39f02SMax Shvetsov * architectural state during world switches. 403532ed618SSoby Mathew */ 4042825946eSMax Shvetsov DEFINE_REG_STRUCT(el1_sysregs, CTX_EL1_SYSREGS_ALL); 4052825946eSMax Shvetsov 4062825946eSMax Shvetsov 4072825946eSMax Shvetsov /* 4082825946eSMax Shvetsov * AArch64 EL2 system register context structure for preserving the 4092825946eSMax Shvetsov * architectural state during world switches. 4102825946eSMax Shvetsov */ 4112825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS 4122825946eSMax Shvetsov DEFINE_REG_STRUCT(el2_sysregs, CTX_EL2_SYSREGS_ALL); 4132825946eSMax Shvetsov #endif 414532ed618SSoby Mathew 415532ed618SSoby Mathew /* 416532ed618SSoby Mathew * AArch64 floating point register context structure for preserving 417532ed618SSoby Mathew * the floating point state during switches from one security state to 418532ed618SSoby Mathew * another. 419532ed618SSoby Mathew */ 420532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 421532ed618SSoby Mathew DEFINE_REG_STRUCT(fp_regs, CTX_FPREG_ALL); 422532ed618SSoby Mathew #endif 423532ed618SSoby Mathew 424532ed618SSoby Mathew /* 425532ed618SSoby Mathew * Miscellaneous registers used by EL3 firmware to maintain its state 426532ed618SSoby Mathew * across exception entries and exits 427532ed618SSoby Mathew */ 428532ed618SSoby Mathew DEFINE_REG_STRUCT(el3_state, CTX_EL3STATE_ALL); 429532ed618SSoby Mathew 430fe007b2eSDimitris Papastamos /* Function pointer used by CVE-2018-3639 dynamic mitigation */ 431fe007b2eSDimitris Papastamos DEFINE_REG_STRUCT(cve_2018_3639, CTX_CVE_2018_3639_ALL); 432fe007b2eSDimitris Papastamos 4335283962eSAntonio Nino Diaz /* Registers associated to ARMv8.3-PAuth */ 4345283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 4355283962eSAntonio Nino Diaz DEFINE_REG_STRUCT(pauth, CTX_PAUTH_REGS_ALL); 4365283962eSAntonio Nino Diaz #endif 4375283962eSAntonio Nino Diaz 4389acff28aSArvind Ram Prakash /* Registers associated to ARMv8.2 MPAM */ 4399acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS 4409acff28aSArvind Ram Prakash DEFINE_REG_STRUCT(mpam, CTX_MPAM_REGS_ALL); 4419acff28aSArvind Ram Prakash #endif 4429acff28aSArvind Ram Prakash 443532ed618SSoby Mathew /* 444532ed618SSoby Mathew * Macros to access members of any of the above structures using their 445532ed618SSoby Mathew * offsets 446532ed618SSoby Mathew */ 4472fe75a2dSZelalem #define read_ctx_reg(ctx, offset) ((ctx)->ctx_regs[(offset) >> DWORD_SHIFT]) 4482fe75a2dSZelalem #define write_ctx_reg(ctx, offset, val) (((ctx)->ctx_regs[(offset) >> DWORD_SHIFT]) \ 449ba6e5ca6SJeenu Viswambharan = (uint64_t) (val)) 450532ed618SSoby Mathew 451532ed618SSoby Mathew /* 452c5ea4f8aSZelalem Aweke * Top-level context structure which is used by EL3 firmware to preserve 453c5ea4f8aSZelalem Aweke * the state of a core at the next lower EL in a given security state and 454c5ea4f8aSZelalem Aweke * save enough EL3 meta data to be able to return to that EL and security 455c5ea4f8aSZelalem Aweke * state. The context management library will be used to ensure that 456c5ea4f8aSZelalem Aweke * SP_EL3 always points to an instance of this structure at exception 457c5ea4f8aSZelalem Aweke * entry and exit. 458532ed618SSoby Mathew */ 459532ed618SSoby Mathew typedef struct cpu_context { 460532ed618SSoby Mathew gp_regs_t gpregs_ctx; 461532ed618SSoby Mathew el3_state_t el3state_ctx; 4622825946eSMax Shvetsov el1_sysregs_t el1_sysregs_ctx; 4632825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS 4642825946eSMax Shvetsov el2_sysregs_t el2_sysregs_ctx; 4652825946eSMax Shvetsov #endif 466532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 467532ed618SSoby Mathew fp_regs_t fpregs_ctx; 468532ed618SSoby Mathew #endif 469fe007b2eSDimitris Papastamos cve_2018_3639_t cve_2018_3639_ctx; 4705283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 4715283962eSAntonio Nino Diaz pauth_t pauth_ctx; 4725283962eSAntonio Nino Diaz #endif 4739acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS 4749acff28aSArvind Ram Prakash mpam_t mpam_ctx; 4759acff28aSArvind Ram Prakash #endif 476532ed618SSoby Mathew } cpu_context_t; 477532ed618SSoby Mathew 478461c0a5dSElizabeth Ho /* 479461c0a5dSElizabeth Ho * Per-World Context. 480461c0a5dSElizabeth Ho * It stores registers whose values can be shared across CPUs. 481461c0a5dSElizabeth Ho */ 482461c0a5dSElizabeth Ho typedef struct per_world_context { 483461c0a5dSElizabeth Ho uint64_t ctx_cptr_el3; 484461c0a5dSElizabeth Ho uint64_t ctx_zcr_el3; 485ac4f6aafSArvind Ram Prakash uint64_t ctx_mpam3_el3; 486461c0a5dSElizabeth Ho } per_world_context_t; 487461c0a5dSElizabeth Ho 488461c0a5dSElizabeth Ho extern per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM]; 489461c0a5dSElizabeth Ho 490532ed618SSoby Mathew /* Macros to access members of the 'cpu_context_t' structure */ 491532ed618SSoby Mathew #define get_el3state_ctx(h) (&((cpu_context_t *) h)->el3state_ctx) 492532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 493532ed618SSoby Mathew # define get_fpregs_ctx(h) (&((cpu_context_t *) h)->fpregs_ctx) 494532ed618SSoby Mathew #endif 4952825946eSMax Shvetsov #define get_el1_sysregs_ctx(h) (&((cpu_context_t *) h)->el1_sysregs_ctx) 4962825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS 4972825946eSMax Shvetsov # define get_el2_sysregs_ctx(h) (&((cpu_context_t *) h)->el2_sysregs_ctx) 4982825946eSMax Shvetsov #endif 499532ed618SSoby Mathew #define get_gpregs_ctx(h) (&((cpu_context_t *) h)->gpregs_ctx) 5006f03bc77SDimitris Papastamos #define get_cve_2018_3639_ctx(h) (&((cpu_context_t *) h)->cve_2018_3639_ctx) 5015283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 5025283962eSAntonio Nino Diaz # define get_pauth_ctx(h) (&((cpu_context_t *) h)->pauth_ctx) 5035283962eSAntonio Nino Diaz #endif 5049acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS 5059acff28aSArvind Ram Prakash # define get_mpam_ctx(h) (&((cpu_context_t *) h)->mpam_ctx) 5069acff28aSArvind Ram Prakash #endif 507532ed618SSoby Mathew 508532ed618SSoby Mathew /* 509532ed618SSoby Mathew * Compile time assertions related to the 'cpu_context' structure to 510532ed618SSoby Mathew * ensure that the assembler and the compiler view of the offsets of 511532ed618SSoby Mathew * the structure members is the same. 512532ed618SSoby Mathew */ 5139a90d720SElyes Haouas CASSERT(CTX_GPREGS_OFFSET == __builtin_offsetof(cpu_context_t, gpregs_ctx), 514532ed618SSoby Mathew assert_core_context_gp_offset_mismatch); 5159a90d720SElyes Haouas CASSERT(CTX_EL1_SYSREGS_OFFSET == __builtin_offsetof(cpu_context_t, el1_sysregs_ctx), 5162825946eSMax Shvetsov assert_core_context_el1_sys_offset_mismatch); 5172825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS 5189a90d720SElyes Haouas CASSERT(CTX_EL2_SYSREGS_OFFSET == __builtin_offsetof(cpu_context_t, el2_sysregs_ctx), 5192825946eSMax Shvetsov assert_core_context_el2_sys_offset_mismatch); 5202825946eSMax Shvetsov #endif 521532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 5229a90d720SElyes Haouas CASSERT(CTX_FPREGS_OFFSET == __builtin_offsetof(cpu_context_t, fpregs_ctx), 523532ed618SSoby Mathew assert_core_context_fp_offset_mismatch); 524532ed618SSoby Mathew #endif 5259a90d720SElyes Haouas CASSERT(CTX_EL3STATE_OFFSET == __builtin_offsetof(cpu_context_t, el3state_ctx), 526532ed618SSoby Mathew assert_core_context_el3state_offset_mismatch); 5279a90d720SElyes Haouas CASSERT(CTX_CVE_2018_3639_OFFSET == __builtin_offsetof(cpu_context_t, cve_2018_3639_ctx), 528fe007b2eSDimitris Papastamos assert_core_context_cve_2018_3639_offset_mismatch); 5295283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS 5309a90d720SElyes Haouas CASSERT(CTX_PAUTH_REGS_OFFSET == __builtin_offsetof(cpu_context_t, pauth_ctx), 5315283962eSAntonio Nino Diaz assert_core_context_pauth_offset_mismatch); 5325283962eSAntonio Nino Diaz #endif 5339acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS 5349acff28aSArvind Ram Prakash CASSERT(CTX_MPAM_REGS_OFFSET == __builtin_offsetof(cpu_context_t, mpam_ctx), 5359acff28aSArvind Ram Prakash assert_core_context_mpam_offset_mismatch); 5369acff28aSArvind Ram Prakash #endif 537532ed618SSoby Mathew 538532ed618SSoby Mathew /* 539532ed618SSoby Mathew * Helper macro to set the general purpose registers that correspond to 540532ed618SSoby Mathew * parameters in an aapcs_64 call i.e. x0-x7 541532ed618SSoby Mathew */ 542532ed618SSoby Mathew #define set_aapcs_args0(ctx, x0) do { \ 543532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X0, x0); \ 544532ed618SSoby Mathew } while (0) 545532ed618SSoby Mathew #define set_aapcs_args1(ctx, x0, x1) do { \ 546532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X1, x1); \ 547532ed618SSoby Mathew set_aapcs_args0(ctx, x0); \ 548532ed618SSoby Mathew } while (0) 549532ed618SSoby Mathew #define set_aapcs_args2(ctx, x0, x1, x2) do { \ 550532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X2, x2); \ 551532ed618SSoby Mathew set_aapcs_args1(ctx, x0, x1); \ 552532ed618SSoby Mathew } while (0) 553532ed618SSoby Mathew #define set_aapcs_args3(ctx, x0, x1, x2, x3) do { \ 554532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X3, x3); \ 555532ed618SSoby Mathew set_aapcs_args2(ctx, x0, x1, x2); \ 556532ed618SSoby Mathew } while (0) 557532ed618SSoby Mathew #define set_aapcs_args4(ctx, x0, x1, x2, x3, x4) do { \ 558532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X4, x4); \ 559532ed618SSoby Mathew set_aapcs_args3(ctx, x0, x1, x2, x3); \ 560532ed618SSoby Mathew } while (0) 561532ed618SSoby Mathew #define set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5) do { \ 562532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X5, x5); \ 563532ed618SSoby Mathew set_aapcs_args4(ctx, x0, x1, x2, x3, x4); \ 564532ed618SSoby Mathew } while (0) 565532ed618SSoby Mathew #define set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6) do { \ 566532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X6, x6); \ 567532ed618SSoby Mathew set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5); \ 568532ed618SSoby Mathew } while (0) 569532ed618SSoby Mathew #define set_aapcs_args7(ctx, x0, x1, x2, x3, x4, x5, x6, x7) do { \ 570532ed618SSoby Mathew write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X7, x7); \ 571532ed618SSoby Mathew set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6); \ 572532ed618SSoby Mathew } while (0) 573532ed618SSoby Mathew 574532ed618SSoby Mathew /******************************************************************************* 575532ed618SSoby Mathew * Function prototypes 576532ed618SSoby Mathew ******************************************************************************/ 5772825946eSMax Shvetsov void el1_sysregs_context_save(el1_sysregs_t *regs); 5782825946eSMax Shvetsov void el1_sysregs_context_restore(el1_sysregs_t *regs); 57928f39f02SMax Shvetsov 580532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS 581532ed618SSoby Mathew void fpregs_context_save(fp_regs_t *regs); 582532ed618SSoby Mathew void fpregs_context_restore(fp_regs_t *regs); 583532ed618SSoby Mathew #endif 584532ed618SSoby Mathew 585d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */ 586532ed618SSoby Mathew 587a0fee747SAntonio Nino Diaz #endif /* CONTEXT_H */ 588