xref: /rk3399_ARM-atf/include/lib/el3_runtime/aarch64/context.h (revision 123002f9171384d976d95935b7f566740d69cc68)
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 
10d6af2344SJayanth Dodderi Chidanand #include <lib/el3_runtime/context_el2.h>
11461c0a5dSElizabeth Ho #include <lib/el3_runtime/cpu_data.h>
1209d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
1376454abfSJeenu Viswambharan 
14532ed618SSoby Mathew /*******************************************************************************
15532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the 'gp_regs'
16532ed618SSoby Mathew  * structure at their correct offsets.
17532ed618SSoby Mathew  ******************************************************************************/
18030567e6SVarun Wadekar #define CTX_GPREGS_OFFSET	U(0x0)
19030567e6SVarun Wadekar #define CTX_GPREG_X0		U(0x0)
20030567e6SVarun Wadekar #define CTX_GPREG_X1		U(0x8)
21030567e6SVarun Wadekar #define CTX_GPREG_X2		U(0x10)
22030567e6SVarun Wadekar #define CTX_GPREG_X3		U(0x18)
23030567e6SVarun Wadekar #define CTX_GPREG_X4		U(0x20)
24030567e6SVarun Wadekar #define CTX_GPREG_X5		U(0x28)
25030567e6SVarun Wadekar #define CTX_GPREG_X6		U(0x30)
26030567e6SVarun Wadekar #define CTX_GPREG_X7		U(0x38)
27030567e6SVarun Wadekar #define CTX_GPREG_X8		U(0x40)
28030567e6SVarun Wadekar #define CTX_GPREG_X9		U(0x48)
29030567e6SVarun Wadekar #define CTX_GPREG_X10		U(0x50)
30030567e6SVarun Wadekar #define CTX_GPREG_X11		U(0x58)
31030567e6SVarun Wadekar #define CTX_GPREG_X12		U(0x60)
32030567e6SVarun Wadekar #define CTX_GPREG_X13		U(0x68)
33030567e6SVarun Wadekar #define CTX_GPREG_X14		U(0x70)
34030567e6SVarun Wadekar #define CTX_GPREG_X15		U(0x78)
35030567e6SVarun Wadekar #define CTX_GPREG_X16		U(0x80)
36030567e6SVarun Wadekar #define CTX_GPREG_X17		U(0x88)
37030567e6SVarun Wadekar #define CTX_GPREG_X18		U(0x90)
38030567e6SVarun Wadekar #define CTX_GPREG_X19		U(0x98)
39030567e6SVarun Wadekar #define CTX_GPREG_X20		U(0xa0)
40030567e6SVarun Wadekar #define CTX_GPREG_X21		U(0xa8)
41030567e6SVarun Wadekar #define CTX_GPREG_X22		U(0xb0)
42030567e6SVarun Wadekar #define CTX_GPREG_X23		U(0xb8)
43030567e6SVarun Wadekar #define CTX_GPREG_X24		U(0xc0)
44030567e6SVarun Wadekar #define CTX_GPREG_X25		U(0xc8)
45030567e6SVarun Wadekar #define CTX_GPREG_X26		U(0xd0)
46030567e6SVarun Wadekar #define CTX_GPREG_X27		U(0xd8)
47030567e6SVarun Wadekar #define CTX_GPREG_X28		U(0xe0)
48030567e6SVarun Wadekar #define CTX_GPREG_X29		U(0xe8)
49030567e6SVarun Wadekar #define CTX_GPREG_LR		U(0xf0)
50030567e6SVarun Wadekar #define CTX_GPREG_SP_EL0	U(0xf8)
51030567e6SVarun Wadekar #define CTX_GPREGS_END		U(0x100)
52532ed618SSoby Mathew 
53532ed618SSoby Mathew /*******************************************************************************
54532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the 'el3_state'
55532ed618SSoby Mathew  * structure at their correct offsets. Note that some of the registers are only
56532ed618SSoby Mathew  * 32-bits wide but are stored as 64-bit values for convenience
57532ed618SSoby Mathew  ******************************************************************************/
58d9bd656cSDimitris Papastamos #define CTX_EL3STATE_OFFSET	(CTX_GPREGS_OFFSET + CTX_GPREGS_END)
59030567e6SVarun Wadekar #define CTX_SCR_EL3		U(0x0)
6076454abfSJeenu Viswambharan #define CTX_ESR_EL3		U(0x8)
6176454abfSJeenu Viswambharan #define CTX_RUNTIME_SP		U(0x10)
6276454abfSJeenu Viswambharan #define CTX_SPSR_EL3		U(0x18)
6376454abfSJeenu Viswambharan #define CTX_ELR_EL3		U(0x20)
64e290a8fcSAlexei Fedorov #define CTX_PMCR_EL0		U(0x28)
65c2d32a5fSMadhukar Pappireddy #define CTX_IS_IN_EL3		U(0x30)
66*123002f9SJayanth Dodderi Chidanand #define CTX_MDCR_EL3		U(0x38)
67d04c04a4SManish Pandey /* Constants required in supporting nested exception in EL3 */
68*123002f9SJayanth Dodderi Chidanand #define CTX_SAVED_ELR_EL3	U(0x40)
69d04c04a4SManish Pandey /*
70d04c04a4SManish Pandey  * General purpose flag, to save various EL3 states
71d04c04a4SManish Pandey  * FFH mode : Used to identify if handling nested exception
72d04c04a4SManish Pandey  * KFH mode : Used as counter value
73d04c04a4SManish Pandey  */
74*123002f9SJayanth Dodderi Chidanand #define CTX_NESTED_EA_FLAG	U(0x48)
75f87e54f7SManish Pandey #if FFH_SUPPORT
76*123002f9SJayanth Dodderi Chidanand  #define CTX_SAVED_ESR_EL3	U(0x50)
77*123002f9SJayanth Dodderi Chidanand  #define CTX_SAVED_SPSR_EL3	U(0x58)
78*123002f9SJayanth Dodderi Chidanand  #define CTX_SAVED_GPREG_LR	U(0x60)
79*123002f9SJayanth Dodderi Chidanand  #define CTX_EL3STATE_END	U(0x70) /* Align to the next 16 byte boundary */
80d04c04a4SManish Pandey #else
81d04c04a4SManish Pandey  #define CTX_EL3STATE_END	U(0x50) /* Align to the next 16 byte boundary */
82ac4f6aafSArvind Ram Prakash #endif /* FFH_SUPPORT */
83532ed618SSoby Mathew 
84532ed618SSoby Mathew /*******************************************************************************
85532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the
86532ed618SSoby Mathew  * 'el1_sys_regs' structure at their correct offsets. Note that some of the
87532ed618SSoby Mathew  * registers are only 32-bits wide but are stored as 64-bit values for
88532ed618SSoby Mathew  * convenience
89532ed618SSoby Mathew  ******************************************************************************/
902825946eSMax Shvetsov #define CTX_EL1_SYSREGS_OFFSET	(CTX_EL3STATE_OFFSET + CTX_EL3STATE_END)
91030567e6SVarun Wadekar #define CTX_SPSR_EL1		U(0x0)
92030567e6SVarun Wadekar #define CTX_ELR_EL1		U(0x8)
93030567e6SVarun Wadekar #define CTX_SCTLR_EL1		U(0x10)
94cb55615cSManish V Badarkhe #define CTX_TCR_EL1		U(0x18)
95030567e6SVarun Wadekar #define CTX_CPACR_EL1		U(0x20)
96030567e6SVarun Wadekar #define CTX_CSSELR_EL1		U(0x28)
97030567e6SVarun Wadekar #define CTX_SP_EL1		U(0x30)
98030567e6SVarun Wadekar #define CTX_ESR_EL1		U(0x38)
99030567e6SVarun Wadekar #define CTX_TTBR0_EL1		U(0x40)
100030567e6SVarun Wadekar #define CTX_TTBR1_EL1		U(0x48)
101030567e6SVarun Wadekar #define CTX_MAIR_EL1		U(0x50)
102030567e6SVarun Wadekar #define CTX_AMAIR_EL1		U(0x58)
103cb55615cSManish V Badarkhe #define CTX_ACTLR_EL1		U(0x60)
104030567e6SVarun Wadekar #define CTX_TPIDR_EL1		U(0x68)
105030567e6SVarun Wadekar #define CTX_TPIDR_EL0		U(0x70)
106030567e6SVarun Wadekar #define CTX_TPIDRRO_EL0		U(0x78)
107030567e6SVarun Wadekar #define CTX_PAR_EL1		U(0x80)
108030567e6SVarun Wadekar #define CTX_FAR_EL1		U(0x88)
109030567e6SVarun Wadekar #define CTX_AFSR0_EL1		U(0x90)
110030567e6SVarun Wadekar #define CTX_AFSR1_EL1		U(0x98)
111030567e6SVarun Wadekar #define CTX_CONTEXTIDR_EL1	U(0xa0)
112030567e6SVarun Wadekar #define CTX_VBAR_EL1		U(0xa8)
113ed9bb824SMadhukar Pappireddy #define CTX_MDCCINT_EL1		U(0xb0)
114ed9bb824SMadhukar Pappireddy #define CTX_MDSCR_EL1		U(0xb8)
115ed9bb824SMadhukar Pappireddy 
116ed9bb824SMadhukar Pappireddy #define CTX_AARCH64_END		U(0xc0) /* Align to the next 16 byte boundary */
117532ed618SSoby Mathew 
118532ed618SSoby Mathew /*
119532ed618SSoby Mathew  * If the platform is AArch64-only, there is no need to save and restore these
120532ed618SSoby Mathew  * AArch32 registers.
121532ed618SSoby Mathew  */
122532ed618SSoby Mathew #if CTX_INCLUDE_AARCH32_REGS
123ed9bb824SMadhukar Pappireddy #define CTX_SPSR_ABT		(CTX_AARCH64_END + U(0x0))
124ed9bb824SMadhukar Pappireddy #define CTX_SPSR_UND		(CTX_AARCH64_END + U(0x8))
125ed9bb824SMadhukar Pappireddy #define CTX_SPSR_IRQ		(CTX_AARCH64_END + U(0x10))
126ed9bb824SMadhukar Pappireddy #define CTX_SPSR_FIQ		(CTX_AARCH64_END + U(0x18))
127ed9bb824SMadhukar Pappireddy #define CTX_DACR32_EL2		(CTX_AARCH64_END + U(0x20))
128ed9bb824SMadhukar Pappireddy #define CTX_IFSR32_EL2		(CTX_AARCH64_END + U(0x28))
129ed9bb824SMadhukar Pappireddy #define CTX_AARCH32_END		(CTX_AARCH64_END + U(0x30)) /* Align to the next 16 byte boundary */
130532ed618SSoby Mathew #else
131ed9bb824SMadhukar Pappireddy #define CTX_AARCH32_END		CTX_AARCH64_END
1324d1ccf0eSAntonio Nino Diaz #endif /* CTX_INCLUDE_AARCH32_REGS */
133532ed618SSoby Mathew 
134532ed618SSoby Mathew /*
135532ed618SSoby Mathew  * If the timer registers aren't saved and restored, we don't have to reserve
136532ed618SSoby Mathew  * space for them in the context
137532ed618SSoby Mathew  */
138532ed618SSoby Mathew #if NS_TIMER_SWITCH
1394d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CTL_EL0	(CTX_AARCH32_END + U(0x0))
1404d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CVAL_EL0	(CTX_AARCH32_END + U(0x8))
1414d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CTL_EL0	(CTX_AARCH32_END + U(0x10))
1424d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CVAL_EL0	(CTX_AARCH32_END + U(0x18))
1434d1ccf0eSAntonio Nino Diaz #define CTX_CNTKCTL_EL1		(CTX_AARCH32_END + U(0x20))
1444d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END	(CTX_AARCH32_END + U(0x30)) /* Align to the next 16 byte boundary */
145532ed618SSoby Mathew #else
1464d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END	CTX_AARCH32_END
1474d1ccf0eSAntonio Nino Diaz #endif /* NS_TIMER_SWITCH */
1484d1ccf0eSAntonio Nino Diaz 
149c282384dSGovindraj Raja #if ENABLE_FEAT_MTE2
1509dd94382SJustin Chadwell #define CTX_TFSRE0_EL1		(CTX_TIMER_SYSREGS_END + U(0x0))
1519dd94382SJustin Chadwell #define CTX_TFSR_EL1		(CTX_TIMER_SYSREGS_END + U(0x8))
1529dd94382SJustin Chadwell #define CTX_RGSR_EL1		(CTX_TIMER_SYSREGS_END + U(0x10))
1539dd94382SJustin Chadwell #define CTX_GCR_EL1		(CTX_TIMER_SYSREGS_END + U(0x18))
154ed9bb824SMadhukar Pappireddy #define CTX_MTE_REGS_END	(CTX_TIMER_SYSREGS_END + U(0x20)) /* Align to the next 16 byte boundary */
1559dd94382SJustin Chadwell #else
1569dd94382SJustin Chadwell #define CTX_MTE_REGS_END	CTX_TIMER_SYSREGS_END
157c282384dSGovindraj Raja #endif /* ENABLE_FEAT_MTE2 */
1589dd94382SJustin Chadwell 
159ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_RAS
160ed9bb824SMadhukar Pappireddy #define CTX_DISR_EL1		(CTX_MTE_REGS_END + U(0x0))
161ed9bb824SMadhukar Pappireddy #define CTX_RAS_REGS_END	(CTX_MTE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
162ed9bb824SMadhukar Pappireddy #else
163ed9bb824SMadhukar Pappireddy #define CTX_RAS_REGS_END        CTX_MTE_REGS_END
164ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_RAS */
165ed9bb824SMadhukar Pappireddy 
166ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S1PIE
167ed9bb824SMadhukar Pappireddy #define CTX_PIRE0_EL1		(CTX_RAS_REGS_END + U(0x0))
168ed9bb824SMadhukar Pappireddy #define CTX_PIR_EL1		(CTX_RAS_REGS_END + U(0x8))
169ed9bb824SMadhukar Pappireddy #define CTX_S1PIE_REGS_END	(CTX_RAS_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
170ed9bb824SMadhukar Pappireddy #else
171ed9bb824SMadhukar Pappireddy #define CTX_S1PIE_REGS_END	CTX_RAS_REGS_END
172ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S1PIE */
173ed9bb824SMadhukar Pappireddy 
174ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S1POE
175ed9bb824SMadhukar Pappireddy #define CTX_POR_EL1		(CTX_S1PIE_REGS_END + U(0x0))
176ed9bb824SMadhukar Pappireddy #define CTX_S1POE_REGS_END	(CTX_S1PIE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
177ed9bb824SMadhukar Pappireddy #else
178ed9bb824SMadhukar Pappireddy #define CTX_S1POE_REGS_END	CTX_S1PIE_REGS_END
179ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S1POE */
180ed9bb824SMadhukar Pappireddy 
181ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S2POE
182ed9bb824SMadhukar Pappireddy #define CTX_S2POR_EL1		(CTX_S1POE_REGS_END + U(0x0))
183ed9bb824SMadhukar Pappireddy #define CTX_S2POE_REGS_END	(CTX_S1POE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
184ed9bb824SMadhukar Pappireddy #else
185ed9bb824SMadhukar Pappireddy #define CTX_S2POE_REGS_END	CTX_S1POE_REGS_END
186ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S2POE */
187ed9bb824SMadhukar Pappireddy 
188ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_TCR2
189ed9bb824SMadhukar Pappireddy #define CTX_TCR2_EL1		(CTX_S2POE_REGS_END + U(0x0))
190ed9bb824SMadhukar Pappireddy #define CTX_TCR2_REGS_END	(CTX_S2POE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
191ed9bb824SMadhukar Pappireddy #else
192ed9bb824SMadhukar Pappireddy #define CTX_TCR2_REGS_END       CTX_S2POE_REGS_END
193ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_TCR2 */
194ed9bb824SMadhukar Pappireddy 
195d6c76e6cSMadhukar Pappireddy #if ENABLE_TRF_FOR_NS
196d6c76e6cSMadhukar Pappireddy #define CTX_TRFCR_EL1		(CTX_TCR2_REGS_END + U(0x0))
197d6c76e6cSMadhukar Pappireddy #define CTX_TRF_REGS_END	(CTX_TCR2_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
198d6c76e6cSMadhukar Pappireddy #else
199d6c76e6cSMadhukar Pappireddy #define CTX_TRF_REGS_END	CTX_TCR2_REGS_END
200d6c76e6cSMadhukar Pappireddy #endif /* ENABLE_TRF_FOR_NS */
201d6c76e6cSMadhukar Pappireddy 
202d6c76e6cSMadhukar Pappireddy #if ENABLE_FEAT_CSV2_2
203d6c76e6cSMadhukar Pappireddy #define CTX_SCXTNUM_EL0		(CTX_TRF_REGS_END + U(0x0))
204d6c76e6cSMadhukar Pappireddy #define CTX_SCXTNUM_EL1		(CTX_TRF_REGS_END + U(0x8))
205d6c76e6cSMadhukar Pappireddy #define CTX_CSV2_2_REGS_END	(CTX_TRF_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
206d6c76e6cSMadhukar Pappireddy #else
207d6c76e6cSMadhukar Pappireddy #define CTX_CSV2_2_REGS_END	CTX_TRF_REGS_END
208d6c76e6cSMadhukar Pappireddy #endif /* ENABLE_FEAT_CSV2_2 */
209d6c76e6cSMadhukar Pappireddy 
210d6c76e6cSMadhukar Pappireddy #if ENABLE_FEAT_GCS
211d6c76e6cSMadhukar Pappireddy #define CTX_GCSCR_EL1		(CTX_CSV2_2_REGS_END + U(0x0))
212d6c76e6cSMadhukar Pappireddy #define CTX_GCSCRE0_EL1		(CTX_CSV2_2_REGS_END + U(0x8))
213d6c76e6cSMadhukar Pappireddy #define CTX_GCSPR_EL1		(CTX_CSV2_2_REGS_END + U(0x10))
214d6c76e6cSMadhukar Pappireddy #define CTX_GCSPR_EL0		(CTX_CSV2_2_REGS_END + U(0x18))
215d6c76e6cSMadhukar Pappireddy #define CTX_GCS_REGS_END	(CTX_CSV2_2_REGS_END + U(0x20)) /* Align to the next 16 byte boundary */
216d6c76e6cSMadhukar Pappireddy #else
217d6c76e6cSMadhukar Pappireddy #define CTX_GCS_REGS_END	CTX_CSV2_2_REGS_END
218d6c76e6cSMadhukar Pappireddy #endif /* ENABLE_FEAT_GCS */
219d6c76e6cSMadhukar Pappireddy 
2204d1ccf0eSAntonio Nino Diaz /*
221d6c76e6cSMadhukar Pappireddy  * End of EL1 system registers.
2222825946eSMax Shvetsov  */
223d6c76e6cSMadhukar Pappireddy #define CTX_EL1_SYSREGS_END	CTX_GCS_REGS_END
2242825946eSMax Shvetsov 
225532ed618SSoby Mathew /*******************************************************************************
226532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the 'fp_regs'
227532ed618SSoby Mathew  * structure at their correct offsets.
228532ed618SSoby Mathew  ******************************************************************************/
2292825946eSMax Shvetsov # define CTX_FPREGS_OFFSET	(CTX_EL1_SYSREGS_OFFSET + CTX_EL1_SYSREGS_END)
230fe007b2eSDimitris Papastamos #if CTX_INCLUDE_FPREGS
231030567e6SVarun Wadekar #define CTX_FP_Q0		U(0x0)
232030567e6SVarun Wadekar #define CTX_FP_Q1		U(0x10)
233030567e6SVarun Wadekar #define CTX_FP_Q2		U(0x20)
234030567e6SVarun Wadekar #define CTX_FP_Q3		U(0x30)
235030567e6SVarun Wadekar #define CTX_FP_Q4		U(0x40)
236030567e6SVarun Wadekar #define CTX_FP_Q5		U(0x50)
237030567e6SVarun Wadekar #define CTX_FP_Q6		U(0x60)
238030567e6SVarun Wadekar #define CTX_FP_Q7		U(0x70)
239030567e6SVarun Wadekar #define CTX_FP_Q8		U(0x80)
240030567e6SVarun Wadekar #define CTX_FP_Q9		U(0x90)
241030567e6SVarun Wadekar #define CTX_FP_Q10		U(0xa0)
242030567e6SVarun Wadekar #define CTX_FP_Q11		U(0xb0)
243030567e6SVarun Wadekar #define CTX_FP_Q12		U(0xc0)
244030567e6SVarun Wadekar #define CTX_FP_Q13		U(0xd0)
245030567e6SVarun Wadekar #define CTX_FP_Q14		U(0xe0)
246030567e6SVarun Wadekar #define CTX_FP_Q15		U(0xf0)
247030567e6SVarun Wadekar #define CTX_FP_Q16		U(0x100)
248030567e6SVarun Wadekar #define CTX_FP_Q17		U(0x110)
249030567e6SVarun Wadekar #define CTX_FP_Q18		U(0x120)
250030567e6SVarun Wadekar #define CTX_FP_Q19		U(0x130)
251030567e6SVarun Wadekar #define CTX_FP_Q20		U(0x140)
252030567e6SVarun Wadekar #define CTX_FP_Q21		U(0x150)
253030567e6SVarun Wadekar #define CTX_FP_Q22		U(0x160)
254030567e6SVarun Wadekar #define CTX_FP_Q23		U(0x170)
255030567e6SVarun Wadekar #define CTX_FP_Q24		U(0x180)
256030567e6SVarun Wadekar #define CTX_FP_Q25		U(0x190)
257030567e6SVarun Wadekar #define CTX_FP_Q26		U(0x1a0)
258030567e6SVarun Wadekar #define CTX_FP_Q27		U(0x1b0)
259030567e6SVarun Wadekar #define CTX_FP_Q28		U(0x1c0)
260030567e6SVarun Wadekar #define CTX_FP_Q29		U(0x1d0)
261030567e6SVarun Wadekar #define CTX_FP_Q30		U(0x1e0)
262030567e6SVarun Wadekar #define CTX_FP_Q31		U(0x1f0)
263030567e6SVarun Wadekar #define CTX_FP_FPSR		U(0x200)
264030567e6SVarun Wadekar #define CTX_FP_FPCR		U(0x208)
26591089f36SDavid Cunado #if CTX_INCLUDE_AARCH32_REGS
26691089f36SDavid Cunado #define CTX_FP_FPEXC32_EL2	U(0x210)
26791089f36SDavid Cunado #define CTX_FPREGS_END		U(0x220) /* Align to the next 16 byte boundary */
26891089f36SDavid Cunado #else
26991089f36SDavid Cunado #define CTX_FPREGS_END		U(0x210) /* Align to the next 16 byte boundary */
270d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_AARCH32_REGS */
271fe007b2eSDimitris Papastamos #else
272fe007b2eSDimitris Papastamos #define CTX_FPREGS_END		U(0)
273d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
274532ed618SSoby Mathew 
2754d1ccf0eSAntonio Nino Diaz /*******************************************************************************
2764d1ccf0eSAntonio Nino Diaz  * Registers related to CVE-2018-3639
2774d1ccf0eSAntonio Nino Diaz  ******************************************************************************/
278fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_OFFSET	(CTX_FPREGS_OFFSET + CTX_FPREGS_END)
279fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_DISABLE	U(0)
280fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_END		U(0x10) /* Align to the next 16 byte boundary */
281fe007b2eSDimitris Papastamos 
2825283962eSAntonio Nino Diaz /*******************************************************************************
2835283962eSAntonio Nino Diaz  * Registers related to ARMv8.3-PAuth.
2845283962eSAntonio Nino Diaz  ******************************************************************************/
2855283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_OFFSET	(CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_END)
2865283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
2875283962eSAntonio Nino Diaz #define CTX_PACIAKEY_LO		U(0x0)
2885283962eSAntonio Nino Diaz #define CTX_PACIAKEY_HI		U(0x8)
2895283962eSAntonio Nino Diaz #define CTX_PACIBKEY_LO		U(0x10)
2905283962eSAntonio Nino Diaz #define CTX_PACIBKEY_HI		U(0x18)
2915283962eSAntonio Nino Diaz #define CTX_PACDAKEY_LO		U(0x20)
2925283962eSAntonio Nino Diaz #define CTX_PACDAKEY_HI		U(0x28)
2935283962eSAntonio Nino Diaz #define CTX_PACDBKEY_LO		U(0x30)
2945283962eSAntonio Nino Diaz #define CTX_PACDBKEY_HI		U(0x38)
2955283962eSAntonio Nino Diaz #define CTX_PACGAKEY_LO		U(0x40)
2965283962eSAntonio Nino Diaz #define CTX_PACGAKEY_HI		U(0x48)
297ed108b56SAlexei Fedorov #define CTX_PAUTH_REGS_END	U(0x50) /* Align to the next 16 byte boundary */
2985283962eSAntonio Nino Diaz #else
2995283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_END	U(0)
3005283962eSAntonio Nino Diaz #endif /* CTX_INCLUDE_PAUTH_REGS */
3015283962eSAntonio Nino Diaz 
302461c0a5dSElizabeth Ho /*******************************************************************************
303461c0a5dSElizabeth Ho  * Registers initialised in a per-world context.
304461c0a5dSElizabeth Ho  ******************************************************************************/
305461c0a5dSElizabeth Ho #define CTX_CPTR_EL3			U(0x0)
306461c0a5dSElizabeth Ho #define CTX_ZCR_EL3			U(0x8)
307ac4f6aafSArvind Ram Prakash #define CTX_MPAM3_EL3			U(0x10)
308ac4f6aafSArvind Ram Prakash #define CTX_PERWORLD_EL3STATE_END	U(0x18)
309461c0a5dSElizabeth Ho 
310d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
311532ed618SSoby Mathew 
312532ed618SSoby Mathew #include <stdint.h>
313532ed618SSoby Mathew 
31409d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
31509d40e0eSAntonio Nino Diaz 
316532ed618SSoby Mathew /*
317532ed618SSoby Mathew  * Common constants to help define the 'cpu_context' structure and its
318532ed618SSoby Mathew  * members below.
319532ed618SSoby Mathew  */
320030567e6SVarun Wadekar #define DWORD_SHIFT		U(3)
321532ed618SSoby Mathew #define DEFINE_REG_STRUCT(name, num_regs)	\
322532ed618SSoby Mathew 	typedef struct name {			\
3232fe75a2dSZelalem 		uint64_t ctx_regs[num_regs];	\
324532ed618SSoby Mathew 	}  __aligned(16) name##_t
325532ed618SSoby Mathew 
326532ed618SSoby Mathew /* Constants to determine the size of individual context structures */
327532ed618SSoby Mathew #define CTX_GPREG_ALL		(CTX_GPREGS_END >> DWORD_SHIFT)
3282825946eSMax Shvetsov #define CTX_EL1_SYSREGS_ALL	(CTX_EL1_SYSREGS_END >> DWORD_SHIFT)
329d6af2344SJayanth Dodderi Chidanand 
330532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
331532ed618SSoby Mathew # define CTX_FPREG_ALL		(CTX_FPREGS_END >> DWORD_SHIFT)
332532ed618SSoby Mathew #endif
333532ed618SSoby Mathew #define CTX_EL3STATE_ALL	(CTX_EL3STATE_END >> DWORD_SHIFT)
334fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_ALL	(CTX_CVE_2018_3639_END >> DWORD_SHIFT)
3355283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3365283962eSAntonio Nino Diaz # define CTX_PAUTH_REGS_ALL	(CTX_PAUTH_REGS_END >> DWORD_SHIFT)
3375283962eSAntonio Nino Diaz #endif
338532ed618SSoby Mathew 
339532ed618SSoby Mathew /*
340532ed618SSoby Mathew  * AArch64 general purpose register context structure. Usually x0-x18,
341532ed618SSoby Mathew  * lr are saved as the compiler is expected to preserve the remaining
342532ed618SSoby Mathew  * callee saved registers if used by the C runtime and the assembler
343532ed618SSoby Mathew  * does not touch the remaining. But in case of world switch during
344532ed618SSoby Mathew  * exception handling, we need to save the callee registers too.
345532ed618SSoby Mathew  */
346532ed618SSoby Mathew DEFINE_REG_STRUCT(gp_regs, CTX_GPREG_ALL);
347532ed618SSoby Mathew 
348532ed618SSoby Mathew /*
3492825946eSMax Shvetsov  * AArch64 EL1 system register context structure for preserving the
35028f39f02SMax Shvetsov  * architectural state during world switches.
351532ed618SSoby Mathew  */
3522825946eSMax Shvetsov DEFINE_REG_STRUCT(el1_sysregs, CTX_EL1_SYSREGS_ALL);
3532825946eSMax Shvetsov 
354532ed618SSoby Mathew /*
355532ed618SSoby Mathew  * AArch64 floating point register context structure for preserving
356532ed618SSoby Mathew  * the floating point state during switches from one security state to
357532ed618SSoby Mathew  * another.
358532ed618SSoby Mathew  */
359532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
360532ed618SSoby Mathew DEFINE_REG_STRUCT(fp_regs, CTX_FPREG_ALL);
361532ed618SSoby Mathew #endif
362532ed618SSoby Mathew 
363532ed618SSoby Mathew /*
364532ed618SSoby Mathew  * Miscellaneous registers used by EL3 firmware to maintain its state
365532ed618SSoby Mathew  * across exception entries and exits
366532ed618SSoby Mathew  */
367532ed618SSoby Mathew DEFINE_REG_STRUCT(el3_state, CTX_EL3STATE_ALL);
368532ed618SSoby Mathew 
369fe007b2eSDimitris Papastamos /* Function pointer used by CVE-2018-3639 dynamic mitigation */
370fe007b2eSDimitris Papastamos DEFINE_REG_STRUCT(cve_2018_3639, CTX_CVE_2018_3639_ALL);
371fe007b2eSDimitris Papastamos 
3725283962eSAntonio Nino Diaz /* Registers associated to ARMv8.3-PAuth */
3735283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3745283962eSAntonio Nino Diaz DEFINE_REG_STRUCT(pauth, CTX_PAUTH_REGS_ALL);
3755283962eSAntonio Nino Diaz #endif
3765283962eSAntonio Nino Diaz 
377532ed618SSoby Mathew /*
378532ed618SSoby Mathew  * Macros to access members of any of the above structures using their
379532ed618SSoby Mathew  * offsets
380532ed618SSoby Mathew  */
3812fe75a2dSZelalem #define read_ctx_reg(ctx, offset)	((ctx)->ctx_regs[(offset) >> DWORD_SHIFT])
3822fe75a2dSZelalem #define write_ctx_reg(ctx, offset, val)	(((ctx)->ctx_regs[(offset) >> DWORD_SHIFT]) \
383ba6e5ca6SJeenu Viswambharan 					 = (uint64_t) (val))
384532ed618SSoby Mathew 
385532ed618SSoby Mathew /*
386c5ea4f8aSZelalem Aweke  * Top-level context structure which is used by EL3 firmware to preserve
387c5ea4f8aSZelalem Aweke  * the state of a core at the next lower EL in a given security state and
388c5ea4f8aSZelalem Aweke  * save enough EL3 meta data to be able to return to that EL and security
389c5ea4f8aSZelalem Aweke  * state. The context management library will be used to ensure that
390c5ea4f8aSZelalem Aweke  * SP_EL3 always points to an instance of this structure at exception
391c5ea4f8aSZelalem Aweke  * entry and exit.
392532ed618SSoby Mathew  */
393532ed618SSoby Mathew typedef struct cpu_context {
394532ed618SSoby Mathew 	gp_regs_t gpregs_ctx;
395532ed618SSoby Mathew 	el3_state_t el3state_ctx;
3962825946eSMax Shvetsov 	el1_sysregs_t el1_sysregs_ctx;
397d6af2344SJayanth Dodderi Chidanand 
398532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
399532ed618SSoby Mathew 	fp_regs_t fpregs_ctx;
400532ed618SSoby Mathew #endif
401fe007b2eSDimitris Papastamos 	cve_2018_3639_t cve_2018_3639_ctx;
402d6af2344SJayanth Dodderi Chidanand 
4035283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4045283962eSAntonio Nino Diaz 	pauth_t pauth_ctx;
4055283962eSAntonio Nino Diaz #endif
406d6af2344SJayanth Dodderi Chidanand 
407d6af2344SJayanth Dodderi Chidanand #if CTX_INCLUDE_EL2_REGS
408d6af2344SJayanth Dodderi Chidanand 	el2_sysregs_t el2_sysregs_ctx;
409d6af2344SJayanth Dodderi Chidanand #endif
410d6af2344SJayanth Dodderi Chidanand 
411532ed618SSoby Mathew } cpu_context_t;
412532ed618SSoby Mathew 
413461c0a5dSElizabeth Ho /*
414461c0a5dSElizabeth Ho  * Per-World Context.
415461c0a5dSElizabeth Ho  * It stores registers whose values can be shared across CPUs.
416461c0a5dSElizabeth Ho  */
417461c0a5dSElizabeth Ho typedef struct per_world_context {
418461c0a5dSElizabeth Ho 	uint64_t ctx_cptr_el3;
419461c0a5dSElizabeth Ho 	uint64_t ctx_zcr_el3;
420ac4f6aafSArvind Ram Prakash 	uint64_t ctx_mpam3_el3;
421461c0a5dSElizabeth Ho } per_world_context_t;
422461c0a5dSElizabeth Ho 
423461c0a5dSElizabeth Ho extern per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM];
424461c0a5dSElizabeth Ho 
425532ed618SSoby Mathew /* Macros to access members of the 'cpu_context_t' structure */
426532ed618SSoby Mathew #define get_el3state_ctx(h)	(&((cpu_context_t *) h)->el3state_ctx)
427532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
428532ed618SSoby Mathew # define get_fpregs_ctx(h)	(&((cpu_context_t *) h)->fpregs_ctx)
429532ed618SSoby Mathew #endif
4302825946eSMax Shvetsov #define get_el1_sysregs_ctx(h)	(&((cpu_context_t *) h)->el1_sysregs_ctx)
4312825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS
4322825946eSMax Shvetsov # define get_el2_sysregs_ctx(h)	(&((cpu_context_t *) h)->el2_sysregs_ctx)
4332825946eSMax Shvetsov #endif
434532ed618SSoby Mathew #define get_gpregs_ctx(h)	(&((cpu_context_t *) h)->gpregs_ctx)
4356f03bc77SDimitris Papastamos #define get_cve_2018_3639_ctx(h)	(&((cpu_context_t *) h)->cve_2018_3639_ctx)
4365283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4375283962eSAntonio Nino Diaz # define get_pauth_ctx(h)	(&((cpu_context_t *) h)->pauth_ctx)
4385283962eSAntonio Nino Diaz #endif
439532ed618SSoby Mathew 
440532ed618SSoby Mathew /*
441532ed618SSoby Mathew  * Compile time assertions related to the 'cpu_context' structure to
442532ed618SSoby Mathew  * ensure that the assembler and the compiler view of the offsets of
443532ed618SSoby Mathew  * the structure members is the same.
444532ed618SSoby Mathew  */
4459a90d720SElyes Haouas CASSERT(CTX_GPREGS_OFFSET == __builtin_offsetof(cpu_context_t, gpregs_ctx),
446532ed618SSoby Mathew 	assert_core_context_gp_offset_mismatch);
447d6af2344SJayanth Dodderi Chidanand 
448d6af2344SJayanth Dodderi Chidanand CASSERT(CTX_EL3STATE_OFFSET == __builtin_offsetof(cpu_context_t, el3state_ctx),
449d6af2344SJayanth Dodderi Chidanand 	assert_core_context_el3state_offset_mismatch);
450d6af2344SJayanth Dodderi Chidanand 
4519a90d720SElyes Haouas CASSERT(CTX_EL1_SYSREGS_OFFSET == __builtin_offsetof(cpu_context_t, el1_sysregs_ctx),
4522825946eSMax Shvetsov 	assert_core_context_el1_sys_offset_mismatch);
453d6af2344SJayanth Dodderi Chidanand 
454532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
4559a90d720SElyes Haouas CASSERT(CTX_FPREGS_OFFSET == __builtin_offsetof(cpu_context_t, fpregs_ctx),
456532ed618SSoby Mathew 	assert_core_context_fp_offset_mismatch);
457d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
458d6af2344SJayanth Dodderi Chidanand 
4599a90d720SElyes Haouas CASSERT(CTX_CVE_2018_3639_OFFSET == __builtin_offsetof(cpu_context_t, cve_2018_3639_ctx),
460fe007b2eSDimitris Papastamos 	assert_core_context_cve_2018_3639_offset_mismatch);
461d6af2344SJayanth Dodderi Chidanand 
4625283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4639a90d720SElyes Haouas CASSERT(CTX_PAUTH_REGS_OFFSET == __builtin_offsetof(cpu_context_t, pauth_ctx),
4645283962eSAntonio Nino Diaz 	assert_core_context_pauth_offset_mismatch);
465d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_PAUTH_REGS */
466d6af2344SJayanth Dodderi Chidanand 
467532ed618SSoby Mathew /*
468532ed618SSoby Mathew  * Helper macro to set the general purpose registers that correspond to
469532ed618SSoby Mathew  * parameters in an aapcs_64 call i.e. x0-x7
470532ed618SSoby Mathew  */
471532ed618SSoby Mathew #define set_aapcs_args0(ctx, x0)				do {	\
472532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X0, x0);	\
473532ed618SSoby Mathew 	} while (0)
474532ed618SSoby Mathew #define set_aapcs_args1(ctx, x0, x1)				do {	\
475532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X1, x1);	\
476532ed618SSoby Mathew 		set_aapcs_args0(ctx, x0);				\
477532ed618SSoby Mathew 	} while (0)
478532ed618SSoby Mathew #define set_aapcs_args2(ctx, x0, x1, x2)			do {	\
479532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X2, x2);	\
480532ed618SSoby Mathew 		set_aapcs_args1(ctx, x0, x1);				\
481532ed618SSoby Mathew 	} while (0)
482532ed618SSoby Mathew #define set_aapcs_args3(ctx, x0, x1, x2, x3)			do {	\
483532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X3, x3);	\
484532ed618SSoby Mathew 		set_aapcs_args2(ctx, x0, x1, x2);			\
485532ed618SSoby Mathew 	} while (0)
486532ed618SSoby Mathew #define set_aapcs_args4(ctx, x0, x1, x2, x3, x4)		do {	\
487532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X4, x4);	\
488532ed618SSoby Mathew 		set_aapcs_args3(ctx, x0, x1, x2, x3);			\
489532ed618SSoby Mathew 	} while (0)
490532ed618SSoby Mathew #define set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5)		do {	\
491532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X5, x5);	\
492532ed618SSoby Mathew 		set_aapcs_args4(ctx, x0, x1, x2, x3, x4);		\
493532ed618SSoby Mathew 	} while (0)
494532ed618SSoby Mathew #define set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6)	do {	\
495532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X6, x6);	\
496532ed618SSoby Mathew 		set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5);		\
497532ed618SSoby Mathew 	} while (0)
498532ed618SSoby Mathew #define set_aapcs_args7(ctx, x0, x1, x2, x3, x4, x5, x6, x7)	do {	\
499532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X7, x7);	\
500532ed618SSoby Mathew 		set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6);	\
501532ed618SSoby Mathew 	} while (0)
502532ed618SSoby Mathew 
503532ed618SSoby Mathew /*******************************************************************************
504532ed618SSoby Mathew  * Function prototypes
505532ed618SSoby Mathew  ******************************************************************************/
506532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
507532ed618SSoby Mathew void fpregs_context_save(fp_regs_t *regs);
508532ed618SSoby Mathew void fpregs_context_restore(fp_regs_t *regs);
509532ed618SSoby Mathew #endif
510532ed618SSoby Mathew 
511d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */
512532ed618SSoby Mathew 
513a0fee747SAntonio Nino Diaz #endif /* CONTEXT_H */
514