xref: /rk3399_ARM-atf/include/lib/el3_runtime/aarch64/context.h (revision d6c76e6c65429326e7572e10f521dd9108a3a1e3)
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)
66d04c04a4SManish Pandey /* Constants required in supporting nested exception in EL3 */
67ac4f6aafSArvind Ram Prakash #define CTX_SAVED_ELR_EL3	U(0x38)
68d04c04a4SManish Pandey /*
69d04c04a4SManish Pandey  * General purpose flag, to save various EL3 states
70d04c04a4SManish Pandey  * FFH mode : Used to identify if handling nested exception
71d04c04a4SManish Pandey  * KFH mode : Used as counter value
72d04c04a4SManish Pandey  */
73ac4f6aafSArvind Ram Prakash #define CTX_NESTED_EA_FLAG	U(0x40)
74f87e54f7SManish Pandey #if FFH_SUPPORT
75ac4f6aafSArvind Ram Prakash  #define CTX_SAVED_ESR_EL3	U(0x48)
76ac4f6aafSArvind Ram Prakash  #define CTX_SAVED_SPSR_EL3	U(0x50)
77ac4f6aafSArvind Ram Prakash  #define CTX_SAVED_GPREG_LR	U(0x58)
78ac4f6aafSArvind Ram Prakash  #define CTX_EL3STATE_END	U(0x60) /* Align to the next 16 byte boundary */
79d04c04a4SManish Pandey #else
80d04c04a4SManish Pandey  #define CTX_EL3STATE_END	U(0x50) /* Align to the next 16 byte boundary */
81ac4f6aafSArvind Ram Prakash #endif /* FFH_SUPPORT */
82532ed618SSoby Mathew 
83532ed618SSoby Mathew /*******************************************************************************
84532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the
85532ed618SSoby Mathew  * 'el1_sys_regs' structure at their correct offsets. Note that some of the
86532ed618SSoby Mathew  * registers are only 32-bits wide but are stored as 64-bit values for
87532ed618SSoby Mathew  * convenience
88532ed618SSoby Mathew  ******************************************************************************/
892825946eSMax Shvetsov #define CTX_EL1_SYSREGS_OFFSET	(CTX_EL3STATE_OFFSET + CTX_EL3STATE_END)
90030567e6SVarun Wadekar #define CTX_SPSR_EL1		U(0x0)
91030567e6SVarun Wadekar #define CTX_ELR_EL1		U(0x8)
92030567e6SVarun Wadekar #define CTX_SCTLR_EL1		U(0x10)
93cb55615cSManish V Badarkhe #define CTX_TCR_EL1		U(0x18)
94030567e6SVarun Wadekar #define CTX_CPACR_EL1		U(0x20)
95030567e6SVarun Wadekar #define CTX_CSSELR_EL1		U(0x28)
96030567e6SVarun Wadekar #define CTX_SP_EL1		U(0x30)
97030567e6SVarun Wadekar #define CTX_ESR_EL1		U(0x38)
98030567e6SVarun Wadekar #define CTX_TTBR0_EL1		U(0x40)
99030567e6SVarun Wadekar #define CTX_TTBR1_EL1		U(0x48)
100030567e6SVarun Wadekar #define CTX_MAIR_EL1		U(0x50)
101030567e6SVarun Wadekar #define CTX_AMAIR_EL1		U(0x58)
102cb55615cSManish V Badarkhe #define CTX_ACTLR_EL1		U(0x60)
103030567e6SVarun Wadekar #define CTX_TPIDR_EL1		U(0x68)
104030567e6SVarun Wadekar #define CTX_TPIDR_EL0		U(0x70)
105030567e6SVarun Wadekar #define CTX_TPIDRRO_EL0		U(0x78)
106030567e6SVarun Wadekar #define CTX_PAR_EL1		U(0x80)
107030567e6SVarun Wadekar #define CTX_FAR_EL1		U(0x88)
108030567e6SVarun Wadekar #define CTX_AFSR0_EL1		U(0x90)
109030567e6SVarun Wadekar #define CTX_AFSR1_EL1		U(0x98)
110030567e6SVarun Wadekar #define CTX_CONTEXTIDR_EL1	U(0xa0)
111030567e6SVarun Wadekar #define CTX_VBAR_EL1		U(0xa8)
112ed9bb824SMadhukar Pappireddy #define CTX_MDCCINT_EL1		U(0xb0)
113ed9bb824SMadhukar Pappireddy #define CTX_MDSCR_EL1		U(0xb8)
114ed9bb824SMadhukar Pappireddy 
115ed9bb824SMadhukar Pappireddy #define CTX_AARCH64_END		U(0xc0) /* Align to the next 16 byte boundary */
116532ed618SSoby Mathew 
117532ed618SSoby Mathew /*
118532ed618SSoby Mathew  * If the platform is AArch64-only, there is no need to save and restore these
119532ed618SSoby Mathew  * AArch32 registers.
120532ed618SSoby Mathew  */
121532ed618SSoby Mathew #if CTX_INCLUDE_AARCH32_REGS
122ed9bb824SMadhukar Pappireddy #define CTX_SPSR_ABT		(CTX_AARCH64_END + U(0x0))
123ed9bb824SMadhukar Pappireddy #define CTX_SPSR_UND		(CTX_AARCH64_END + U(0x8))
124ed9bb824SMadhukar Pappireddy #define CTX_SPSR_IRQ		(CTX_AARCH64_END + U(0x10))
125ed9bb824SMadhukar Pappireddy #define CTX_SPSR_FIQ		(CTX_AARCH64_END + U(0x18))
126ed9bb824SMadhukar Pappireddy #define CTX_DACR32_EL2		(CTX_AARCH64_END + U(0x20))
127ed9bb824SMadhukar Pappireddy #define CTX_IFSR32_EL2		(CTX_AARCH64_END + U(0x28))
128ed9bb824SMadhukar Pappireddy #define CTX_AARCH32_END		(CTX_AARCH64_END + U(0x30)) /* Align to the next 16 byte boundary */
129532ed618SSoby Mathew #else
130ed9bb824SMadhukar Pappireddy #define CTX_AARCH32_END		CTX_AARCH64_END
1314d1ccf0eSAntonio Nino Diaz #endif /* CTX_INCLUDE_AARCH32_REGS */
132532ed618SSoby Mathew 
133532ed618SSoby Mathew /*
134532ed618SSoby Mathew  * If the timer registers aren't saved and restored, we don't have to reserve
135532ed618SSoby Mathew  * space for them in the context
136532ed618SSoby Mathew  */
137532ed618SSoby Mathew #if NS_TIMER_SWITCH
1384d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CTL_EL0	(CTX_AARCH32_END + U(0x0))
1394d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CVAL_EL0	(CTX_AARCH32_END + U(0x8))
1404d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CTL_EL0	(CTX_AARCH32_END + U(0x10))
1414d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CVAL_EL0	(CTX_AARCH32_END + U(0x18))
1424d1ccf0eSAntonio Nino Diaz #define CTX_CNTKCTL_EL1		(CTX_AARCH32_END + U(0x20))
1434d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END	(CTX_AARCH32_END + U(0x30)) /* Align to the next 16 byte boundary */
144532ed618SSoby Mathew #else
1454d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END	CTX_AARCH32_END
1464d1ccf0eSAntonio Nino Diaz #endif /* NS_TIMER_SWITCH */
1474d1ccf0eSAntonio Nino Diaz 
148c282384dSGovindraj Raja #if ENABLE_FEAT_MTE2
1499dd94382SJustin Chadwell #define CTX_TFSRE0_EL1		(CTX_TIMER_SYSREGS_END + U(0x0))
1509dd94382SJustin Chadwell #define CTX_TFSR_EL1		(CTX_TIMER_SYSREGS_END + U(0x8))
1519dd94382SJustin Chadwell #define CTX_RGSR_EL1		(CTX_TIMER_SYSREGS_END + U(0x10))
1529dd94382SJustin Chadwell #define CTX_GCR_EL1		(CTX_TIMER_SYSREGS_END + U(0x18))
153ed9bb824SMadhukar Pappireddy #define CTX_MTE_REGS_END	(CTX_TIMER_SYSREGS_END + U(0x20)) /* Align to the next 16 byte boundary */
1549dd94382SJustin Chadwell #else
1559dd94382SJustin Chadwell #define CTX_MTE_REGS_END	CTX_TIMER_SYSREGS_END
156c282384dSGovindraj Raja #endif /* ENABLE_FEAT_MTE2 */
1579dd94382SJustin Chadwell 
158ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_RAS
159ed9bb824SMadhukar Pappireddy #define CTX_DISR_EL1		(CTX_MTE_REGS_END + U(0x0))
160ed9bb824SMadhukar Pappireddy #define CTX_RAS_REGS_END	(CTX_MTE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
161ed9bb824SMadhukar Pappireddy #else
162ed9bb824SMadhukar Pappireddy #define CTX_RAS_REGS_END        CTX_MTE_REGS_END
163ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_RAS */
164ed9bb824SMadhukar Pappireddy 
165ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S1PIE
166ed9bb824SMadhukar Pappireddy #define CTX_PIRE0_EL1		(CTX_RAS_REGS_END + U(0x0))
167ed9bb824SMadhukar Pappireddy #define CTX_PIR_EL1		(CTX_RAS_REGS_END + U(0x8))
168ed9bb824SMadhukar Pappireddy #define CTX_S1PIE_REGS_END	(CTX_RAS_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
169ed9bb824SMadhukar Pappireddy #else
170ed9bb824SMadhukar Pappireddy #define CTX_S1PIE_REGS_END	CTX_RAS_REGS_END
171ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S1PIE */
172ed9bb824SMadhukar Pappireddy 
173ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S1POE
174ed9bb824SMadhukar Pappireddy #define CTX_POR_EL1		(CTX_S1PIE_REGS_END + U(0x0))
175ed9bb824SMadhukar Pappireddy #define CTX_S1POE_REGS_END	(CTX_S1PIE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
176ed9bb824SMadhukar Pappireddy #else
177ed9bb824SMadhukar Pappireddy #define CTX_S1POE_REGS_END	CTX_S1PIE_REGS_END
178ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S1POE */
179ed9bb824SMadhukar Pappireddy 
180ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S2POE
181ed9bb824SMadhukar Pappireddy #define CTX_S2POR_EL1		(CTX_S1POE_REGS_END + U(0x0))
182ed9bb824SMadhukar Pappireddy #define CTX_S2POE_REGS_END	(CTX_S1POE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
183ed9bb824SMadhukar Pappireddy #else
184ed9bb824SMadhukar Pappireddy #define CTX_S2POE_REGS_END	CTX_S1POE_REGS_END
185ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S2POE */
186ed9bb824SMadhukar Pappireddy 
187ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_TCR2
188ed9bb824SMadhukar Pappireddy #define CTX_TCR2_EL1		(CTX_S2POE_REGS_END + U(0x0))
189ed9bb824SMadhukar Pappireddy #define CTX_TCR2_REGS_END	(CTX_S2POE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
190ed9bb824SMadhukar Pappireddy #else
191ed9bb824SMadhukar Pappireddy #define CTX_TCR2_REGS_END       CTX_S2POE_REGS_END
192ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_TCR2 */
193ed9bb824SMadhukar Pappireddy 
194*d6c76e6cSMadhukar Pappireddy #if ENABLE_TRF_FOR_NS
195*d6c76e6cSMadhukar Pappireddy #define CTX_TRFCR_EL1		(CTX_TCR2_REGS_END + U(0x0))
196*d6c76e6cSMadhukar Pappireddy #define CTX_TRF_REGS_END	(CTX_TCR2_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
197*d6c76e6cSMadhukar Pappireddy #else
198*d6c76e6cSMadhukar Pappireddy #define CTX_TRF_REGS_END	CTX_TCR2_REGS_END
199*d6c76e6cSMadhukar Pappireddy #endif /* ENABLE_TRF_FOR_NS */
200*d6c76e6cSMadhukar Pappireddy 
201*d6c76e6cSMadhukar Pappireddy #if ENABLE_FEAT_CSV2_2
202*d6c76e6cSMadhukar Pappireddy #define CTX_SCXTNUM_EL0		(CTX_TRF_REGS_END + U(0x0))
203*d6c76e6cSMadhukar Pappireddy #define CTX_SCXTNUM_EL1		(CTX_TRF_REGS_END + U(0x8))
204*d6c76e6cSMadhukar Pappireddy #define CTX_CSV2_2_REGS_END	(CTX_TRF_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
205*d6c76e6cSMadhukar Pappireddy #else
206*d6c76e6cSMadhukar Pappireddy #define CTX_CSV2_2_REGS_END	CTX_TRF_REGS_END
207*d6c76e6cSMadhukar Pappireddy #endif /* ENABLE_FEAT_CSV2_2 */
208*d6c76e6cSMadhukar Pappireddy 
209*d6c76e6cSMadhukar Pappireddy #if ENABLE_FEAT_GCS
210*d6c76e6cSMadhukar Pappireddy #define CTX_GCSCR_EL1		(CTX_CSV2_2_REGS_END + U(0x0))
211*d6c76e6cSMadhukar Pappireddy #define CTX_GCSCRE0_EL1		(CTX_CSV2_2_REGS_END + U(0x8))
212*d6c76e6cSMadhukar Pappireddy #define CTX_GCSPR_EL1		(CTX_CSV2_2_REGS_END + U(0x10))
213*d6c76e6cSMadhukar Pappireddy #define CTX_GCSPR_EL0		(CTX_CSV2_2_REGS_END + U(0x18))
214*d6c76e6cSMadhukar Pappireddy #define CTX_GCS_REGS_END	(CTX_CSV2_2_REGS_END + U(0x20)) /* Align to the next 16 byte boundary */
215*d6c76e6cSMadhukar Pappireddy #else
216*d6c76e6cSMadhukar Pappireddy #define CTX_GCS_REGS_END	CTX_CSV2_2_REGS_END
217*d6c76e6cSMadhukar Pappireddy #endif /* ENABLE_FEAT_GCS */
218*d6c76e6cSMadhukar Pappireddy 
2194d1ccf0eSAntonio Nino Diaz /*
220*d6c76e6cSMadhukar Pappireddy  * End of EL1 system registers.
2212825946eSMax Shvetsov  */
222*d6c76e6cSMadhukar Pappireddy #define CTX_EL1_SYSREGS_END	CTX_GCS_REGS_END
2232825946eSMax Shvetsov 
224532ed618SSoby Mathew /*******************************************************************************
225532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the 'fp_regs'
226532ed618SSoby Mathew  * structure at their correct offsets.
227532ed618SSoby Mathew  ******************************************************************************/
2282825946eSMax Shvetsov # define CTX_FPREGS_OFFSET	(CTX_EL1_SYSREGS_OFFSET + CTX_EL1_SYSREGS_END)
229fe007b2eSDimitris Papastamos #if CTX_INCLUDE_FPREGS
230030567e6SVarun Wadekar #define CTX_FP_Q0		U(0x0)
231030567e6SVarun Wadekar #define CTX_FP_Q1		U(0x10)
232030567e6SVarun Wadekar #define CTX_FP_Q2		U(0x20)
233030567e6SVarun Wadekar #define CTX_FP_Q3		U(0x30)
234030567e6SVarun Wadekar #define CTX_FP_Q4		U(0x40)
235030567e6SVarun Wadekar #define CTX_FP_Q5		U(0x50)
236030567e6SVarun Wadekar #define CTX_FP_Q6		U(0x60)
237030567e6SVarun Wadekar #define CTX_FP_Q7		U(0x70)
238030567e6SVarun Wadekar #define CTX_FP_Q8		U(0x80)
239030567e6SVarun Wadekar #define CTX_FP_Q9		U(0x90)
240030567e6SVarun Wadekar #define CTX_FP_Q10		U(0xa0)
241030567e6SVarun Wadekar #define CTX_FP_Q11		U(0xb0)
242030567e6SVarun Wadekar #define CTX_FP_Q12		U(0xc0)
243030567e6SVarun Wadekar #define CTX_FP_Q13		U(0xd0)
244030567e6SVarun Wadekar #define CTX_FP_Q14		U(0xe0)
245030567e6SVarun Wadekar #define CTX_FP_Q15		U(0xf0)
246030567e6SVarun Wadekar #define CTX_FP_Q16		U(0x100)
247030567e6SVarun Wadekar #define CTX_FP_Q17		U(0x110)
248030567e6SVarun Wadekar #define CTX_FP_Q18		U(0x120)
249030567e6SVarun Wadekar #define CTX_FP_Q19		U(0x130)
250030567e6SVarun Wadekar #define CTX_FP_Q20		U(0x140)
251030567e6SVarun Wadekar #define CTX_FP_Q21		U(0x150)
252030567e6SVarun Wadekar #define CTX_FP_Q22		U(0x160)
253030567e6SVarun Wadekar #define CTX_FP_Q23		U(0x170)
254030567e6SVarun Wadekar #define CTX_FP_Q24		U(0x180)
255030567e6SVarun Wadekar #define CTX_FP_Q25		U(0x190)
256030567e6SVarun Wadekar #define CTX_FP_Q26		U(0x1a0)
257030567e6SVarun Wadekar #define CTX_FP_Q27		U(0x1b0)
258030567e6SVarun Wadekar #define CTX_FP_Q28		U(0x1c0)
259030567e6SVarun Wadekar #define CTX_FP_Q29		U(0x1d0)
260030567e6SVarun Wadekar #define CTX_FP_Q30		U(0x1e0)
261030567e6SVarun Wadekar #define CTX_FP_Q31		U(0x1f0)
262030567e6SVarun Wadekar #define CTX_FP_FPSR		U(0x200)
263030567e6SVarun Wadekar #define CTX_FP_FPCR		U(0x208)
26491089f36SDavid Cunado #if CTX_INCLUDE_AARCH32_REGS
26591089f36SDavid Cunado #define CTX_FP_FPEXC32_EL2	U(0x210)
26691089f36SDavid Cunado #define CTX_FPREGS_END		U(0x220) /* Align to the next 16 byte boundary */
26791089f36SDavid Cunado #else
26891089f36SDavid Cunado #define CTX_FPREGS_END		U(0x210) /* Align to the next 16 byte boundary */
269d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_AARCH32_REGS */
270fe007b2eSDimitris Papastamos #else
271fe007b2eSDimitris Papastamos #define CTX_FPREGS_END		U(0)
272d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
273532ed618SSoby Mathew 
2744d1ccf0eSAntonio Nino Diaz /*******************************************************************************
2754d1ccf0eSAntonio Nino Diaz  * Registers related to CVE-2018-3639
2764d1ccf0eSAntonio Nino Diaz  ******************************************************************************/
277fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_OFFSET	(CTX_FPREGS_OFFSET + CTX_FPREGS_END)
278fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_DISABLE	U(0)
279fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_END		U(0x10) /* Align to the next 16 byte boundary */
280fe007b2eSDimitris Papastamos 
2815283962eSAntonio Nino Diaz /*******************************************************************************
2825283962eSAntonio Nino Diaz  * Registers related to ARMv8.3-PAuth.
2835283962eSAntonio Nino Diaz  ******************************************************************************/
2845283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_OFFSET	(CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_END)
2855283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
2865283962eSAntonio Nino Diaz #define CTX_PACIAKEY_LO		U(0x0)
2875283962eSAntonio Nino Diaz #define CTX_PACIAKEY_HI		U(0x8)
2885283962eSAntonio Nino Diaz #define CTX_PACIBKEY_LO		U(0x10)
2895283962eSAntonio Nino Diaz #define CTX_PACIBKEY_HI		U(0x18)
2905283962eSAntonio Nino Diaz #define CTX_PACDAKEY_LO		U(0x20)
2915283962eSAntonio Nino Diaz #define CTX_PACDAKEY_HI		U(0x28)
2925283962eSAntonio Nino Diaz #define CTX_PACDBKEY_LO		U(0x30)
2935283962eSAntonio Nino Diaz #define CTX_PACDBKEY_HI		U(0x38)
2945283962eSAntonio Nino Diaz #define CTX_PACGAKEY_LO		U(0x40)
2955283962eSAntonio Nino Diaz #define CTX_PACGAKEY_HI		U(0x48)
296ed108b56SAlexei Fedorov #define CTX_PAUTH_REGS_END	U(0x50) /* Align to the next 16 byte boundary */
2975283962eSAntonio Nino Diaz #else
2985283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_END	U(0)
2995283962eSAntonio Nino Diaz #endif /* CTX_INCLUDE_PAUTH_REGS */
3005283962eSAntonio Nino Diaz 
301461c0a5dSElizabeth Ho /*******************************************************************************
3029acff28aSArvind Ram Prakash  * Registers related to ARMv8.2-MPAM.
3039acff28aSArvind Ram Prakash  ******************************************************************************/
3049acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_OFFSET	(CTX_PAUTH_REGS_OFFSET + CTX_PAUTH_REGS_END)
3059acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
3069acff28aSArvind Ram Prakash #define CTX_MPAM2_EL2		U(0x0)
3079acff28aSArvind Ram Prakash #define CTX_MPAMHCR_EL2		U(0x8)
3089acff28aSArvind Ram Prakash #define CTX_MPAMVPM0_EL2	U(0x10)
3099acff28aSArvind Ram Prakash #define CTX_MPAMVPM1_EL2	U(0x18)
3109acff28aSArvind Ram Prakash #define CTX_MPAMVPM2_EL2	U(0x20)
3119acff28aSArvind Ram Prakash #define CTX_MPAMVPM3_EL2	U(0x28)
3129acff28aSArvind Ram Prakash #define CTX_MPAMVPM4_EL2	U(0x30)
3139acff28aSArvind Ram Prakash #define CTX_MPAMVPM5_EL2	U(0x38)
3149acff28aSArvind Ram Prakash #define CTX_MPAMVPM6_EL2	U(0x40)
3159acff28aSArvind Ram Prakash #define CTX_MPAMVPM7_EL2	U(0x48)
3169acff28aSArvind Ram Prakash #define CTX_MPAMVPMV_EL2	U(0x50)
3179acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END	U(0x60)
3189acff28aSArvind Ram Prakash #else
3199acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END	U(0x0)
3209acff28aSArvind Ram Prakash #endif /* CTX_INCLUDE_MPAM_REGS */
3219acff28aSArvind Ram Prakash 
3229acff28aSArvind Ram Prakash /*******************************************************************************
323461c0a5dSElizabeth Ho  * Registers initialised in a per-world context.
324461c0a5dSElizabeth Ho  ******************************************************************************/
325461c0a5dSElizabeth Ho #define CTX_CPTR_EL3			U(0x0)
326461c0a5dSElizabeth Ho #define CTX_ZCR_EL3			U(0x8)
327ac4f6aafSArvind Ram Prakash #define CTX_MPAM3_EL3			U(0x10)
328ac4f6aafSArvind Ram Prakash #define CTX_PERWORLD_EL3STATE_END	U(0x18)
329461c0a5dSElizabeth Ho 
330d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
331532ed618SSoby Mathew 
332532ed618SSoby Mathew #include <stdint.h>
333532ed618SSoby Mathew 
33409d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
33509d40e0eSAntonio Nino Diaz 
336532ed618SSoby Mathew /*
337532ed618SSoby Mathew  * Common constants to help define the 'cpu_context' structure and its
338532ed618SSoby Mathew  * members below.
339532ed618SSoby Mathew  */
340030567e6SVarun Wadekar #define DWORD_SHIFT		U(3)
341532ed618SSoby Mathew #define DEFINE_REG_STRUCT(name, num_regs)	\
342532ed618SSoby Mathew 	typedef struct name {			\
3432fe75a2dSZelalem 		uint64_t ctx_regs[num_regs];	\
344532ed618SSoby Mathew 	}  __aligned(16) name##_t
345532ed618SSoby Mathew 
346532ed618SSoby Mathew /* Constants to determine the size of individual context structures */
347532ed618SSoby Mathew #define CTX_GPREG_ALL		(CTX_GPREGS_END >> DWORD_SHIFT)
3482825946eSMax Shvetsov #define CTX_EL1_SYSREGS_ALL	(CTX_EL1_SYSREGS_END >> DWORD_SHIFT)
349d6af2344SJayanth Dodderi Chidanand 
350532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
351532ed618SSoby Mathew # define CTX_FPREG_ALL		(CTX_FPREGS_END >> DWORD_SHIFT)
352532ed618SSoby Mathew #endif
353532ed618SSoby Mathew #define CTX_EL3STATE_ALL	(CTX_EL3STATE_END >> DWORD_SHIFT)
354fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_ALL	(CTX_CVE_2018_3639_END >> DWORD_SHIFT)
3555283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3565283962eSAntonio Nino Diaz # define CTX_PAUTH_REGS_ALL	(CTX_PAUTH_REGS_END >> DWORD_SHIFT)
3575283962eSAntonio Nino Diaz #endif
3589acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
3599acff28aSArvind Ram Prakash # define CTX_MPAM_REGS_ALL	(CTX_MPAM_REGS_END >> DWORD_SHIFT)
3609acff28aSArvind Ram Prakash #endif
361532ed618SSoby Mathew 
362532ed618SSoby Mathew /*
363532ed618SSoby Mathew  * AArch64 general purpose register context structure. Usually x0-x18,
364532ed618SSoby Mathew  * lr are saved as the compiler is expected to preserve the remaining
365532ed618SSoby Mathew  * callee saved registers if used by the C runtime and the assembler
366532ed618SSoby Mathew  * does not touch the remaining. But in case of world switch during
367532ed618SSoby Mathew  * exception handling, we need to save the callee registers too.
368532ed618SSoby Mathew  */
369532ed618SSoby Mathew DEFINE_REG_STRUCT(gp_regs, CTX_GPREG_ALL);
370532ed618SSoby Mathew 
371532ed618SSoby Mathew /*
3722825946eSMax Shvetsov  * AArch64 EL1 system register context structure for preserving the
37328f39f02SMax Shvetsov  * architectural state during world switches.
374532ed618SSoby Mathew  */
3752825946eSMax Shvetsov DEFINE_REG_STRUCT(el1_sysregs, CTX_EL1_SYSREGS_ALL);
3762825946eSMax Shvetsov 
377532ed618SSoby Mathew /*
378532ed618SSoby Mathew  * AArch64 floating point register context structure for preserving
379532ed618SSoby Mathew  * the floating point state during switches from one security state to
380532ed618SSoby Mathew  * another.
381532ed618SSoby Mathew  */
382532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
383532ed618SSoby Mathew DEFINE_REG_STRUCT(fp_regs, CTX_FPREG_ALL);
384532ed618SSoby Mathew #endif
385532ed618SSoby Mathew 
386532ed618SSoby Mathew /*
387532ed618SSoby Mathew  * Miscellaneous registers used by EL3 firmware to maintain its state
388532ed618SSoby Mathew  * across exception entries and exits
389532ed618SSoby Mathew  */
390532ed618SSoby Mathew DEFINE_REG_STRUCT(el3_state, CTX_EL3STATE_ALL);
391532ed618SSoby Mathew 
392fe007b2eSDimitris Papastamos /* Function pointer used by CVE-2018-3639 dynamic mitigation */
393fe007b2eSDimitris Papastamos DEFINE_REG_STRUCT(cve_2018_3639, CTX_CVE_2018_3639_ALL);
394fe007b2eSDimitris Papastamos 
3955283962eSAntonio Nino Diaz /* Registers associated to ARMv8.3-PAuth */
3965283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3975283962eSAntonio Nino Diaz DEFINE_REG_STRUCT(pauth, CTX_PAUTH_REGS_ALL);
3985283962eSAntonio Nino Diaz #endif
3995283962eSAntonio Nino Diaz 
4009acff28aSArvind Ram Prakash /* Registers associated to ARMv8.2 MPAM */
4019acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4029acff28aSArvind Ram Prakash DEFINE_REG_STRUCT(mpam, CTX_MPAM_REGS_ALL);
4039acff28aSArvind Ram Prakash #endif
4049acff28aSArvind Ram Prakash 
405532ed618SSoby Mathew /*
406532ed618SSoby Mathew  * Macros to access members of any of the above structures using their
407532ed618SSoby Mathew  * offsets
408532ed618SSoby Mathew  */
4092fe75a2dSZelalem #define read_ctx_reg(ctx, offset)	((ctx)->ctx_regs[(offset) >> DWORD_SHIFT])
4102fe75a2dSZelalem #define write_ctx_reg(ctx, offset, val)	(((ctx)->ctx_regs[(offset) >> DWORD_SHIFT]) \
411ba6e5ca6SJeenu Viswambharan 					 = (uint64_t) (val))
412532ed618SSoby Mathew 
413532ed618SSoby Mathew /*
414c5ea4f8aSZelalem Aweke  * Top-level context structure which is used by EL3 firmware to preserve
415c5ea4f8aSZelalem Aweke  * the state of a core at the next lower EL in a given security state and
416c5ea4f8aSZelalem Aweke  * save enough EL3 meta data to be able to return to that EL and security
417c5ea4f8aSZelalem Aweke  * state. The context management library will be used to ensure that
418c5ea4f8aSZelalem Aweke  * SP_EL3 always points to an instance of this structure at exception
419c5ea4f8aSZelalem Aweke  * entry and exit.
420532ed618SSoby Mathew  */
421532ed618SSoby Mathew typedef struct cpu_context {
422532ed618SSoby Mathew 	gp_regs_t gpregs_ctx;
423532ed618SSoby Mathew 	el3_state_t el3state_ctx;
4242825946eSMax Shvetsov 	el1_sysregs_t el1_sysregs_ctx;
425d6af2344SJayanth Dodderi Chidanand 
426532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
427532ed618SSoby Mathew 	fp_regs_t fpregs_ctx;
428532ed618SSoby Mathew #endif
429fe007b2eSDimitris Papastamos 	cve_2018_3639_t cve_2018_3639_ctx;
430d6af2344SJayanth Dodderi Chidanand 
4315283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4325283962eSAntonio Nino Diaz 	pauth_t pauth_ctx;
4335283962eSAntonio Nino Diaz #endif
434d6af2344SJayanth Dodderi Chidanand 
4359acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4369acff28aSArvind Ram Prakash 	mpam_t	mpam_ctx;
4379acff28aSArvind Ram Prakash #endif
438d6af2344SJayanth Dodderi Chidanand 
439d6af2344SJayanth Dodderi Chidanand #if CTX_INCLUDE_EL2_REGS
440d6af2344SJayanth Dodderi Chidanand 	el2_sysregs_t el2_sysregs_ctx;
441d6af2344SJayanth Dodderi Chidanand #endif
442d6af2344SJayanth Dodderi Chidanand 
443532ed618SSoby Mathew } cpu_context_t;
444532ed618SSoby Mathew 
445461c0a5dSElizabeth Ho /*
446461c0a5dSElizabeth Ho  * Per-World Context.
447461c0a5dSElizabeth Ho  * It stores registers whose values can be shared across CPUs.
448461c0a5dSElizabeth Ho  */
449461c0a5dSElizabeth Ho typedef struct per_world_context {
450461c0a5dSElizabeth Ho 	uint64_t ctx_cptr_el3;
451461c0a5dSElizabeth Ho 	uint64_t ctx_zcr_el3;
452ac4f6aafSArvind Ram Prakash 	uint64_t ctx_mpam3_el3;
453461c0a5dSElizabeth Ho } per_world_context_t;
454461c0a5dSElizabeth Ho 
455461c0a5dSElizabeth Ho extern per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM];
456461c0a5dSElizabeth Ho 
457532ed618SSoby Mathew /* Macros to access members of the 'cpu_context_t' structure */
458532ed618SSoby Mathew #define get_el3state_ctx(h)	(&((cpu_context_t *) h)->el3state_ctx)
459532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
460532ed618SSoby Mathew # define get_fpregs_ctx(h)	(&((cpu_context_t *) h)->fpregs_ctx)
461532ed618SSoby Mathew #endif
4622825946eSMax Shvetsov #define get_el1_sysregs_ctx(h)	(&((cpu_context_t *) h)->el1_sysregs_ctx)
4632825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS
4642825946eSMax Shvetsov # define get_el2_sysregs_ctx(h)	(&((cpu_context_t *) h)->el2_sysregs_ctx)
4652825946eSMax Shvetsov #endif
466532ed618SSoby Mathew #define get_gpregs_ctx(h)	(&((cpu_context_t *) h)->gpregs_ctx)
4676f03bc77SDimitris Papastamos #define get_cve_2018_3639_ctx(h)	(&((cpu_context_t *) h)->cve_2018_3639_ctx)
4685283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4695283962eSAntonio Nino Diaz # define get_pauth_ctx(h)	(&((cpu_context_t *) h)->pauth_ctx)
4705283962eSAntonio Nino Diaz #endif
4719acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4729acff28aSArvind Ram Prakash # define get_mpam_ctx(h)	(&((cpu_context_t *) h)->mpam_ctx)
4739acff28aSArvind Ram Prakash #endif
474532ed618SSoby Mathew 
475532ed618SSoby Mathew /*
476532ed618SSoby Mathew  * Compile time assertions related to the 'cpu_context' structure to
477532ed618SSoby Mathew  * ensure that the assembler and the compiler view of the offsets of
478532ed618SSoby Mathew  * the structure members is the same.
479532ed618SSoby Mathew  */
4809a90d720SElyes Haouas CASSERT(CTX_GPREGS_OFFSET == __builtin_offsetof(cpu_context_t, gpregs_ctx),
481532ed618SSoby Mathew 	assert_core_context_gp_offset_mismatch);
482d6af2344SJayanth Dodderi Chidanand 
483d6af2344SJayanth Dodderi Chidanand CASSERT(CTX_EL3STATE_OFFSET == __builtin_offsetof(cpu_context_t, el3state_ctx),
484d6af2344SJayanth Dodderi Chidanand 	assert_core_context_el3state_offset_mismatch);
485d6af2344SJayanth Dodderi Chidanand 
4869a90d720SElyes Haouas CASSERT(CTX_EL1_SYSREGS_OFFSET == __builtin_offsetof(cpu_context_t, el1_sysregs_ctx),
4872825946eSMax Shvetsov 	assert_core_context_el1_sys_offset_mismatch);
488d6af2344SJayanth Dodderi Chidanand 
489532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
4909a90d720SElyes Haouas CASSERT(CTX_FPREGS_OFFSET == __builtin_offsetof(cpu_context_t, fpregs_ctx),
491532ed618SSoby Mathew 	assert_core_context_fp_offset_mismatch);
492d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
493d6af2344SJayanth Dodderi Chidanand 
4949a90d720SElyes Haouas CASSERT(CTX_CVE_2018_3639_OFFSET == __builtin_offsetof(cpu_context_t, cve_2018_3639_ctx),
495fe007b2eSDimitris Papastamos 	assert_core_context_cve_2018_3639_offset_mismatch);
496d6af2344SJayanth Dodderi Chidanand 
4975283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4989a90d720SElyes Haouas CASSERT(CTX_PAUTH_REGS_OFFSET == __builtin_offsetof(cpu_context_t, pauth_ctx),
4995283962eSAntonio Nino Diaz 	assert_core_context_pauth_offset_mismatch);
500d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_PAUTH_REGS */
501d6af2344SJayanth Dodderi Chidanand 
5029acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
5039acff28aSArvind Ram Prakash CASSERT(CTX_MPAM_REGS_OFFSET == __builtin_offsetof(cpu_context_t, mpam_ctx),
5049acff28aSArvind Ram Prakash 	assert_core_context_mpam_offset_mismatch);
505d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_MPAM_REGS */
506532ed618SSoby Mathew 
507532ed618SSoby Mathew /*
508532ed618SSoby Mathew  * Helper macro to set the general purpose registers that correspond to
509532ed618SSoby Mathew  * parameters in an aapcs_64 call i.e. x0-x7
510532ed618SSoby Mathew  */
511532ed618SSoby Mathew #define set_aapcs_args0(ctx, x0)				do {	\
512532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X0, x0);	\
513532ed618SSoby Mathew 	} while (0)
514532ed618SSoby Mathew #define set_aapcs_args1(ctx, x0, x1)				do {	\
515532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X1, x1);	\
516532ed618SSoby Mathew 		set_aapcs_args0(ctx, x0);				\
517532ed618SSoby Mathew 	} while (0)
518532ed618SSoby Mathew #define set_aapcs_args2(ctx, x0, x1, x2)			do {	\
519532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X2, x2);	\
520532ed618SSoby Mathew 		set_aapcs_args1(ctx, x0, x1);				\
521532ed618SSoby Mathew 	} while (0)
522532ed618SSoby Mathew #define set_aapcs_args3(ctx, x0, x1, x2, x3)			do {	\
523532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X3, x3);	\
524532ed618SSoby Mathew 		set_aapcs_args2(ctx, x0, x1, x2);			\
525532ed618SSoby Mathew 	} while (0)
526532ed618SSoby Mathew #define set_aapcs_args4(ctx, x0, x1, x2, x3, x4)		do {	\
527532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X4, x4);	\
528532ed618SSoby Mathew 		set_aapcs_args3(ctx, x0, x1, x2, x3);			\
529532ed618SSoby Mathew 	} while (0)
530532ed618SSoby Mathew #define set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5)		do {	\
531532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X5, x5);	\
532532ed618SSoby Mathew 		set_aapcs_args4(ctx, x0, x1, x2, x3, x4);		\
533532ed618SSoby Mathew 	} while (0)
534532ed618SSoby Mathew #define set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6)	do {	\
535532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X6, x6);	\
536532ed618SSoby Mathew 		set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5);		\
537532ed618SSoby Mathew 	} while (0)
538532ed618SSoby Mathew #define set_aapcs_args7(ctx, x0, x1, x2, x3, x4, x5, x6, x7)	do {	\
539532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X7, x7);	\
540532ed618SSoby Mathew 		set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6);	\
541532ed618SSoby Mathew 	} while (0)
542532ed618SSoby Mathew 
543532ed618SSoby Mathew /*******************************************************************************
544532ed618SSoby Mathew  * Function prototypes
545532ed618SSoby Mathew  ******************************************************************************/
546532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
547532ed618SSoby Mathew void fpregs_context_save(fp_regs_t *regs);
548532ed618SSoby Mathew void fpregs_context_restore(fp_regs_t *regs);
549532ed618SSoby Mathew #endif
550532ed618SSoby Mathew 
551d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */
552532ed618SSoby Mathew 
553a0fee747SAntonio Nino Diaz #endif /* CONTEXT_H */
554