xref: /rk3399_ARM-atf/include/lib/el3_runtime/aarch64/context.h (revision ed9bb824e4a3815e60acaa69ed66796279f4afbf)
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)
112*ed9bb824SMadhukar Pappireddy #define CTX_MDCCINT_EL1		U(0xb0)
113*ed9bb824SMadhukar Pappireddy #define CTX_MDSCR_EL1		U(0xb8)
114*ed9bb824SMadhukar Pappireddy 
115*ed9bb824SMadhukar 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
122*ed9bb824SMadhukar Pappireddy #define CTX_SPSR_ABT		(CTX_AARCH64_END + U(0x0))
123*ed9bb824SMadhukar Pappireddy #define CTX_SPSR_UND		(CTX_AARCH64_END + U(0x8))
124*ed9bb824SMadhukar Pappireddy #define CTX_SPSR_IRQ		(CTX_AARCH64_END + U(0x10))
125*ed9bb824SMadhukar Pappireddy #define CTX_SPSR_FIQ		(CTX_AARCH64_END + U(0x18))
126*ed9bb824SMadhukar Pappireddy #define CTX_DACR32_EL2		(CTX_AARCH64_END + U(0x20))
127*ed9bb824SMadhukar Pappireddy #define CTX_IFSR32_EL2		(CTX_AARCH64_END + U(0x28))
128*ed9bb824SMadhukar Pappireddy #define CTX_AARCH32_END		(CTX_AARCH64_END + U(0x30)) /* Align to the next 16 byte boundary */
129532ed618SSoby Mathew #else
130*ed9bb824SMadhukar 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))
153*ed9bb824SMadhukar 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 
158*ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_RAS
159*ed9bb824SMadhukar Pappireddy #define CTX_DISR_EL1		(CTX_MTE_REGS_END + U(0x0))
160*ed9bb824SMadhukar Pappireddy #define CTX_RAS_REGS_END	(CTX_MTE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
161*ed9bb824SMadhukar Pappireddy #else
162*ed9bb824SMadhukar Pappireddy #define CTX_RAS_REGS_END        CTX_MTE_REGS_END
163*ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_RAS */
164*ed9bb824SMadhukar Pappireddy 
165*ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S1PIE
166*ed9bb824SMadhukar Pappireddy #define CTX_PIRE0_EL1		(CTX_RAS_REGS_END + U(0x0))
167*ed9bb824SMadhukar Pappireddy #define CTX_PIR_EL1		(CTX_RAS_REGS_END + U(0x8))
168*ed9bb824SMadhukar Pappireddy #define CTX_S1PIE_REGS_END	(CTX_RAS_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
169*ed9bb824SMadhukar Pappireddy #else
170*ed9bb824SMadhukar Pappireddy #define CTX_S1PIE_REGS_END	CTX_RAS_REGS_END
171*ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S1PIE */
172*ed9bb824SMadhukar Pappireddy 
173*ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S1POE
174*ed9bb824SMadhukar Pappireddy #define CTX_POR_EL1		(CTX_S1PIE_REGS_END + U(0x0))
175*ed9bb824SMadhukar Pappireddy #define CTX_S1POE_REGS_END	(CTX_S1PIE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
176*ed9bb824SMadhukar Pappireddy #else
177*ed9bb824SMadhukar Pappireddy #define CTX_S1POE_REGS_END	CTX_S1PIE_REGS_END
178*ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S1POE */
179*ed9bb824SMadhukar Pappireddy 
180*ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_S2POE
181*ed9bb824SMadhukar Pappireddy #define CTX_S2POR_EL1		(CTX_S1POE_REGS_END + U(0x0))
182*ed9bb824SMadhukar Pappireddy #define CTX_S2POE_REGS_END	(CTX_S1POE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
183*ed9bb824SMadhukar Pappireddy #else
184*ed9bb824SMadhukar Pappireddy #define CTX_S2POE_REGS_END	CTX_S1POE_REGS_END
185*ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_S2POE */
186*ed9bb824SMadhukar Pappireddy 
187*ed9bb824SMadhukar Pappireddy #if ENABLE_FEAT_TCR2
188*ed9bb824SMadhukar Pappireddy #define CTX_TCR2_EL1		(CTX_S2POE_REGS_END + U(0x0))
189*ed9bb824SMadhukar Pappireddy #define CTX_TCR2_REGS_END	(CTX_S2POE_REGS_END + U(0x10)) /* Align to the next 16 byte boundary */
190*ed9bb824SMadhukar Pappireddy #else
191*ed9bb824SMadhukar Pappireddy #define CTX_TCR2_REGS_END       CTX_S2POE_REGS_END
192*ed9bb824SMadhukar Pappireddy #endif /* ENABLE_FEAT_TCR2 */
193*ed9bb824SMadhukar Pappireddy 
1944d1ccf0eSAntonio Nino Diaz /*
1952825946eSMax Shvetsov  * End of system registers.
1962825946eSMax Shvetsov  */
197*ed9bb824SMadhukar Pappireddy #define CTX_EL1_SYSREGS_END	CTX_TCR2_REGS_END
1982825946eSMax Shvetsov 
199532ed618SSoby Mathew /*******************************************************************************
200532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the 'fp_regs'
201532ed618SSoby Mathew  * structure at their correct offsets.
202532ed618SSoby Mathew  ******************************************************************************/
2032825946eSMax Shvetsov # define CTX_FPREGS_OFFSET	(CTX_EL1_SYSREGS_OFFSET + CTX_EL1_SYSREGS_END)
204fe007b2eSDimitris Papastamos #if CTX_INCLUDE_FPREGS
205030567e6SVarun Wadekar #define CTX_FP_Q0		U(0x0)
206030567e6SVarun Wadekar #define CTX_FP_Q1		U(0x10)
207030567e6SVarun Wadekar #define CTX_FP_Q2		U(0x20)
208030567e6SVarun Wadekar #define CTX_FP_Q3		U(0x30)
209030567e6SVarun Wadekar #define CTX_FP_Q4		U(0x40)
210030567e6SVarun Wadekar #define CTX_FP_Q5		U(0x50)
211030567e6SVarun Wadekar #define CTX_FP_Q6		U(0x60)
212030567e6SVarun Wadekar #define CTX_FP_Q7		U(0x70)
213030567e6SVarun Wadekar #define CTX_FP_Q8		U(0x80)
214030567e6SVarun Wadekar #define CTX_FP_Q9		U(0x90)
215030567e6SVarun Wadekar #define CTX_FP_Q10		U(0xa0)
216030567e6SVarun Wadekar #define CTX_FP_Q11		U(0xb0)
217030567e6SVarun Wadekar #define CTX_FP_Q12		U(0xc0)
218030567e6SVarun Wadekar #define CTX_FP_Q13		U(0xd0)
219030567e6SVarun Wadekar #define CTX_FP_Q14		U(0xe0)
220030567e6SVarun Wadekar #define CTX_FP_Q15		U(0xf0)
221030567e6SVarun Wadekar #define CTX_FP_Q16		U(0x100)
222030567e6SVarun Wadekar #define CTX_FP_Q17		U(0x110)
223030567e6SVarun Wadekar #define CTX_FP_Q18		U(0x120)
224030567e6SVarun Wadekar #define CTX_FP_Q19		U(0x130)
225030567e6SVarun Wadekar #define CTX_FP_Q20		U(0x140)
226030567e6SVarun Wadekar #define CTX_FP_Q21		U(0x150)
227030567e6SVarun Wadekar #define CTX_FP_Q22		U(0x160)
228030567e6SVarun Wadekar #define CTX_FP_Q23		U(0x170)
229030567e6SVarun Wadekar #define CTX_FP_Q24		U(0x180)
230030567e6SVarun Wadekar #define CTX_FP_Q25		U(0x190)
231030567e6SVarun Wadekar #define CTX_FP_Q26		U(0x1a0)
232030567e6SVarun Wadekar #define CTX_FP_Q27		U(0x1b0)
233030567e6SVarun Wadekar #define CTX_FP_Q28		U(0x1c0)
234030567e6SVarun Wadekar #define CTX_FP_Q29		U(0x1d0)
235030567e6SVarun Wadekar #define CTX_FP_Q30		U(0x1e0)
236030567e6SVarun Wadekar #define CTX_FP_Q31		U(0x1f0)
237030567e6SVarun Wadekar #define CTX_FP_FPSR		U(0x200)
238030567e6SVarun Wadekar #define CTX_FP_FPCR		U(0x208)
23991089f36SDavid Cunado #if CTX_INCLUDE_AARCH32_REGS
24091089f36SDavid Cunado #define CTX_FP_FPEXC32_EL2	U(0x210)
24191089f36SDavid Cunado #define CTX_FPREGS_END		U(0x220) /* Align to the next 16 byte boundary */
24291089f36SDavid Cunado #else
24391089f36SDavid Cunado #define CTX_FPREGS_END		U(0x210) /* Align to the next 16 byte boundary */
244d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_AARCH32_REGS */
245fe007b2eSDimitris Papastamos #else
246fe007b2eSDimitris Papastamos #define CTX_FPREGS_END		U(0)
247d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
248532ed618SSoby Mathew 
2494d1ccf0eSAntonio Nino Diaz /*******************************************************************************
2504d1ccf0eSAntonio Nino Diaz  * Registers related to CVE-2018-3639
2514d1ccf0eSAntonio Nino Diaz  ******************************************************************************/
252fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_OFFSET	(CTX_FPREGS_OFFSET + CTX_FPREGS_END)
253fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_DISABLE	U(0)
254fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_END		U(0x10) /* Align to the next 16 byte boundary */
255fe007b2eSDimitris Papastamos 
2565283962eSAntonio Nino Diaz /*******************************************************************************
2575283962eSAntonio Nino Diaz  * Registers related to ARMv8.3-PAuth.
2585283962eSAntonio Nino Diaz  ******************************************************************************/
2595283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_OFFSET	(CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_END)
2605283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
2615283962eSAntonio Nino Diaz #define CTX_PACIAKEY_LO		U(0x0)
2625283962eSAntonio Nino Diaz #define CTX_PACIAKEY_HI		U(0x8)
2635283962eSAntonio Nino Diaz #define CTX_PACIBKEY_LO		U(0x10)
2645283962eSAntonio Nino Diaz #define CTX_PACIBKEY_HI		U(0x18)
2655283962eSAntonio Nino Diaz #define CTX_PACDAKEY_LO		U(0x20)
2665283962eSAntonio Nino Diaz #define CTX_PACDAKEY_HI		U(0x28)
2675283962eSAntonio Nino Diaz #define CTX_PACDBKEY_LO		U(0x30)
2685283962eSAntonio Nino Diaz #define CTX_PACDBKEY_HI		U(0x38)
2695283962eSAntonio Nino Diaz #define CTX_PACGAKEY_LO		U(0x40)
2705283962eSAntonio Nino Diaz #define CTX_PACGAKEY_HI		U(0x48)
271ed108b56SAlexei Fedorov #define CTX_PAUTH_REGS_END	U(0x50) /* Align to the next 16 byte boundary */
2725283962eSAntonio Nino Diaz #else
2735283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_END	U(0)
2745283962eSAntonio Nino Diaz #endif /* CTX_INCLUDE_PAUTH_REGS */
2755283962eSAntonio Nino Diaz 
276461c0a5dSElizabeth Ho /*******************************************************************************
2779acff28aSArvind Ram Prakash  * Registers related to ARMv8.2-MPAM.
2789acff28aSArvind Ram Prakash  ******************************************************************************/
2799acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_OFFSET	(CTX_PAUTH_REGS_OFFSET + CTX_PAUTH_REGS_END)
2809acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
2819acff28aSArvind Ram Prakash #define CTX_MPAM2_EL2		U(0x0)
2829acff28aSArvind Ram Prakash #define CTX_MPAMHCR_EL2		U(0x8)
2839acff28aSArvind Ram Prakash #define CTX_MPAMVPM0_EL2	U(0x10)
2849acff28aSArvind Ram Prakash #define CTX_MPAMVPM1_EL2	U(0x18)
2859acff28aSArvind Ram Prakash #define CTX_MPAMVPM2_EL2	U(0x20)
2869acff28aSArvind Ram Prakash #define CTX_MPAMVPM3_EL2	U(0x28)
2879acff28aSArvind Ram Prakash #define CTX_MPAMVPM4_EL2	U(0x30)
2889acff28aSArvind Ram Prakash #define CTX_MPAMVPM5_EL2	U(0x38)
2899acff28aSArvind Ram Prakash #define CTX_MPAMVPM6_EL2	U(0x40)
2909acff28aSArvind Ram Prakash #define CTX_MPAMVPM7_EL2	U(0x48)
2919acff28aSArvind Ram Prakash #define CTX_MPAMVPMV_EL2	U(0x50)
2929acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END	U(0x60)
2939acff28aSArvind Ram Prakash #else
2949acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END	U(0x0)
2959acff28aSArvind Ram Prakash #endif /* CTX_INCLUDE_MPAM_REGS */
2969acff28aSArvind Ram Prakash 
2979acff28aSArvind Ram Prakash /*******************************************************************************
298461c0a5dSElizabeth Ho  * Registers initialised in a per-world context.
299461c0a5dSElizabeth Ho  ******************************************************************************/
300461c0a5dSElizabeth Ho #define CTX_CPTR_EL3			U(0x0)
301461c0a5dSElizabeth Ho #define CTX_ZCR_EL3			U(0x8)
302ac4f6aafSArvind Ram Prakash #define CTX_MPAM3_EL3			U(0x10)
303ac4f6aafSArvind Ram Prakash #define CTX_PERWORLD_EL3STATE_END	U(0x18)
304461c0a5dSElizabeth Ho 
305d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
306532ed618SSoby Mathew 
307532ed618SSoby Mathew #include <stdint.h>
308532ed618SSoby Mathew 
30909d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
31009d40e0eSAntonio Nino Diaz 
311532ed618SSoby Mathew /*
312532ed618SSoby Mathew  * Common constants to help define the 'cpu_context' structure and its
313532ed618SSoby Mathew  * members below.
314532ed618SSoby Mathew  */
315030567e6SVarun Wadekar #define DWORD_SHIFT		U(3)
316532ed618SSoby Mathew #define DEFINE_REG_STRUCT(name, num_regs)	\
317532ed618SSoby Mathew 	typedef struct name {			\
3182fe75a2dSZelalem 		uint64_t ctx_regs[num_regs];	\
319532ed618SSoby Mathew 	}  __aligned(16) name##_t
320532ed618SSoby Mathew 
321532ed618SSoby Mathew /* Constants to determine the size of individual context structures */
322532ed618SSoby Mathew #define CTX_GPREG_ALL		(CTX_GPREGS_END >> DWORD_SHIFT)
3232825946eSMax Shvetsov #define CTX_EL1_SYSREGS_ALL	(CTX_EL1_SYSREGS_END >> DWORD_SHIFT)
324d6af2344SJayanth Dodderi Chidanand 
325532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
326532ed618SSoby Mathew # define CTX_FPREG_ALL		(CTX_FPREGS_END >> DWORD_SHIFT)
327532ed618SSoby Mathew #endif
328532ed618SSoby Mathew #define CTX_EL3STATE_ALL	(CTX_EL3STATE_END >> DWORD_SHIFT)
329fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_ALL	(CTX_CVE_2018_3639_END >> DWORD_SHIFT)
3305283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3315283962eSAntonio Nino Diaz # define CTX_PAUTH_REGS_ALL	(CTX_PAUTH_REGS_END >> DWORD_SHIFT)
3325283962eSAntonio Nino Diaz #endif
3339acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
3349acff28aSArvind Ram Prakash # define CTX_MPAM_REGS_ALL	(CTX_MPAM_REGS_END >> DWORD_SHIFT)
3359acff28aSArvind Ram Prakash #endif
336532ed618SSoby Mathew 
337532ed618SSoby Mathew /*
338532ed618SSoby Mathew  * AArch64 general purpose register context structure. Usually x0-x18,
339532ed618SSoby Mathew  * lr are saved as the compiler is expected to preserve the remaining
340532ed618SSoby Mathew  * callee saved registers if used by the C runtime and the assembler
341532ed618SSoby Mathew  * does not touch the remaining. But in case of world switch during
342532ed618SSoby Mathew  * exception handling, we need to save the callee registers too.
343532ed618SSoby Mathew  */
344532ed618SSoby Mathew DEFINE_REG_STRUCT(gp_regs, CTX_GPREG_ALL);
345532ed618SSoby Mathew 
346532ed618SSoby Mathew /*
3472825946eSMax Shvetsov  * AArch64 EL1 system register context structure for preserving the
34828f39f02SMax Shvetsov  * architectural state during world switches.
349532ed618SSoby Mathew  */
3502825946eSMax Shvetsov DEFINE_REG_STRUCT(el1_sysregs, CTX_EL1_SYSREGS_ALL);
3512825946eSMax Shvetsov 
352532ed618SSoby Mathew /*
353532ed618SSoby Mathew  * AArch64 floating point register context structure for preserving
354532ed618SSoby Mathew  * the floating point state during switches from one security state to
355532ed618SSoby Mathew  * another.
356532ed618SSoby Mathew  */
357532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
358532ed618SSoby Mathew DEFINE_REG_STRUCT(fp_regs, CTX_FPREG_ALL);
359532ed618SSoby Mathew #endif
360532ed618SSoby Mathew 
361532ed618SSoby Mathew /*
362532ed618SSoby Mathew  * Miscellaneous registers used by EL3 firmware to maintain its state
363532ed618SSoby Mathew  * across exception entries and exits
364532ed618SSoby Mathew  */
365532ed618SSoby Mathew DEFINE_REG_STRUCT(el3_state, CTX_EL3STATE_ALL);
366532ed618SSoby Mathew 
367fe007b2eSDimitris Papastamos /* Function pointer used by CVE-2018-3639 dynamic mitigation */
368fe007b2eSDimitris Papastamos DEFINE_REG_STRUCT(cve_2018_3639, CTX_CVE_2018_3639_ALL);
369fe007b2eSDimitris Papastamos 
3705283962eSAntonio Nino Diaz /* Registers associated to ARMv8.3-PAuth */
3715283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3725283962eSAntonio Nino Diaz DEFINE_REG_STRUCT(pauth, CTX_PAUTH_REGS_ALL);
3735283962eSAntonio Nino Diaz #endif
3745283962eSAntonio Nino Diaz 
3759acff28aSArvind Ram Prakash /* Registers associated to ARMv8.2 MPAM */
3769acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
3779acff28aSArvind Ram Prakash DEFINE_REG_STRUCT(mpam, CTX_MPAM_REGS_ALL);
3789acff28aSArvind Ram Prakash #endif
3799acff28aSArvind Ram Prakash 
380532ed618SSoby Mathew /*
381532ed618SSoby Mathew  * Macros to access members of any of the above structures using their
382532ed618SSoby Mathew  * offsets
383532ed618SSoby Mathew  */
3842fe75a2dSZelalem #define read_ctx_reg(ctx, offset)	((ctx)->ctx_regs[(offset) >> DWORD_SHIFT])
3852fe75a2dSZelalem #define write_ctx_reg(ctx, offset, val)	(((ctx)->ctx_regs[(offset) >> DWORD_SHIFT]) \
386ba6e5ca6SJeenu Viswambharan 					 = (uint64_t) (val))
387532ed618SSoby Mathew 
388532ed618SSoby Mathew /*
389c5ea4f8aSZelalem Aweke  * Top-level context structure which is used by EL3 firmware to preserve
390c5ea4f8aSZelalem Aweke  * the state of a core at the next lower EL in a given security state and
391c5ea4f8aSZelalem Aweke  * save enough EL3 meta data to be able to return to that EL and security
392c5ea4f8aSZelalem Aweke  * state. The context management library will be used to ensure that
393c5ea4f8aSZelalem Aweke  * SP_EL3 always points to an instance of this structure at exception
394c5ea4f8aSZelalem Aweke  * entry and exit.
395532ed618SSoby Mathew  */
396532ed618SSoby Mathew typedef struct cpu_context {
397532ed618SSoby Mathew 	gp_regs_t gpregs_ctx;
398532ed618SSoby Mathew 	el3_state_t el3state_ctx;
3992825946eSMax Shvetsov 	el1_sysregs_t el1_sysregs_ctx;
400d6af2344SJayanth Dodderi Chidanand 
401532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
402532ed618SSoby Mathew 	fp_regs_t fpregs_ctx;
403532ed618SSoby Mathew #endif
404fe007b2eSDimitris Papastamos 	cve_2018_3639_t cve_2018_3639_ctx;
405d6af2344SJayanth Dodderi Chidanand 
4065283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4075283962eSAntonio Nino Diaz 	pauth_t pauth_ctx;
4085283962eSAntonio Nino Diaz #endif
409d6af2344SJayanth Dodderi Chidanand 
4109acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4119acff28aSArvind Ram Prakash 	mpam_t	mpam_ctx;
4129acff28aSArvind Ram Prakash #endif
413d6af2344SJayanth Dodderi Chidanand 
414d6af2344SJayanth Dodderi Chidanand #if CTX_INCLUDE_EL2_REGS
415d6af2344SJayanth Dodderi Chidanand 	el2_sysregs_t el2_sysregs_ctx;
416d6af2344SJayanth Dodderi Chidanand #endif
417d6af2344SJayanth Dodderi Chidanand 
418532ed618SSoby Mathew } cpu_context_t;
419532ed618SSoby Mathew 
420461c0a5dSElizabeth Ho /*
421461c0a5dSElizabeth Ho  * Per-World Context.
422461c0a5dSElizabeth Ho  * It stores registers whose values can be shared across CPUs.
423461c0a5dSElizabeth Ho  */
424461c0a5dSElizabeth Ho typedef struct per_world_context {
425461c0a5dSElizabeth Ho 	uint64_t ctx_cptr_el3;
426461c0a5dSElizabeth Ho 	uint64_t ctx_zcr_el3;
427ac4f6aafSArvind Ram Prakash 	uint64_t ctx_mpam3_el3;
428461c0a5dSElizabeth Ho } per_world_context_t;
429461c0a5dSElizabeth Ho 
430461c0a5dSElizabeth Ho extern per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM];
431461c0a5dSElizabeth Ho 
432532ed618SSoby Mathew /* Macros to access members of the 'cpu_context_t' structure */
433532ed618SSoby Mathew #define get_el3state_ctx(h)	(&((cpu_context_t *) h)->el3state_ctx)
434532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
435532ed618SSoby Mathew # define get_fpregs_ctx(h)	(&((cpu_context_t *) h)->fpregs_ctx)
436532ed618SSoby Mathew #endif
4372825946eSMax Shvetsov #define get_el1_sysregs_ctx(h)	(&((cpu_context_t *) h)->el1_sysregs_ctx)
4382825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS
4392825946eSMax Shvetsov # define get_el2_sysregs_ctx(h)	(&((cpu_context_t *) h)->el2_sysregs_ctx)
4402825946eSMax Shvetsov #endif
441532ed618SSoby Mathew #define get_gpregs_ctx(h)	(&((cpu_context_t *) h)->gpregs_ctx)
4426f03bc77SDimitris Papastamos #define get_cve_2018_3639_ctx(h)	(&((cpu_context_t *) h)->cve_2018_3639_ctx)
4435283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4445283962eSAntonio Nino Diaz # define get_pauth_ctx(h)	(&((cpu_context_t *) h)->pauth_ctx)
4455283962eSAntonio Nino Diaz #endif
4469acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4479acff28aSArvind Ram Prakash # define get_mpam_ctx(h)	(&((cpu_context_t *) h)->mpam_ctx)
4489acff28aSArvind Ram Prakash #endif
449532ed618SSoby Mathew 
450532ed618SSoby Mathew /*
451532ed618SSoby Mathew  * Compile time assertions related to the 'cpu_context' structure to
452532ed618SSoby Mathew  * ensure that the assembler and the compiler view of the offsets of
453532ed618SSoby Mathew  * the structure members is the same.
454532ed618SSoby Mathew  */
4559a90d720SElyes Haouas CASSERT(CTX_GPREGS_OFFSET == __builtin_offsetof(cpu_context_t, gpregs_ctx),
456532ed618SSoby Mathew 	assert_core_context_gp_offset_mismatch);
457d6af2344SJayanth Dodderi Chidanand 
458d6af2344SJayanth Dodderi Chidanand CASSERT(CTX_EL3STATE_OFFSET == __builtin_offsetof(cpu_context_t, el3state_ctx),
459d6af2344SJayanth Dodderi Chidanand 	assert_core_context_el3state_offset_mismatch);
460d6af2344SJayanth Dodderi Chidanand 
4619a90d720SElyes Haouas CASSERT(CTX_EL1_SYSREGS_OFFSET == __builtin_offsetof(cpu_context_t, el1_sysregs_ctx),
4622825946eSMax Shvetsov 	assert_core_context_el1_sys_offset_mismatch);
463d6af2344SJayanth Dodderi Chidanand 
464532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
4659a90d720SElyes Haouas CASSERT(CTX_FPREGS_OFFSET == __builtin_offsetof(cpu_context_t, fpregs_ctx),
466532ed618SSoby Mathew 	assert_core_context_fp_offset_mismatch);
467d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
468d6af2344SJayanth Dodderi Chidanand 
4699a90d720SElyes Haouas CASSERT(CTX_CVE_2018_3639_OFFSET == __builtin_offsetof(cpu_context_t, cve_2018_3639_ctx),
470fe007b2eSDimitris Papastamos 	assert_core_context_cve_2018_3639_offset_mismatch);
471d6af2344SJayanth Dodderi Chidanand 
4725283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4739a90d720SElyes Haouas CASSERT(CTX_PAUTH_REGS_OFFSET == __builtin_offsetof(cpu_context_t, pauth_ctx),
4745283962eSAntonio Nino Diaz 	assert_core_context_pauth_offset_mismatch);
475d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_PAUTH_REGS */
476d6af2344SJayanth Dodderi Chidanand 
4779acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4789acff28aSArvind Ram Prakash CASSERT(CTX_MPAM_REGS_OFFSET == __builtin_offsetof(cpu_context_t, mpam_ctx),
4799acff28aSArvind Ram Prakash 	assert_core_context_mpam_offset_mismatch);
480d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_MPAM_REGS */
481532ed618SSoby Mathew 
482532ed618SSoby Mathew /*
483532ed618SSoby Mathew  * Helper macro to set the general purpose registers that correspond to
484532ed618SSoby Mathew  * parameters in an aapcs_64 call i.e. x0-x7
485532ed618SSoby Mathew  */
486532ed618SSoby Mathew #define set_aapcs_args0(ctx, x0)				do {	\
487532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X0, x0);	\
488532ed618SSoby Mathew 	} while (0)
489532ed618SSoby Mathew #define set_aapcs_args1(ctx, x0, x1)				do {	\
490532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X1, x1);	\
491532ed618SSoby Mathew 		set_aapcs_args0(ctx, x0);				\
492532ed618SSoby Mathew 	} while (0)
493532ed618SSoby Mathew #define set_aapcs_args2(ctx, x0, x1, x2)			do {	\
494532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X2, x2);	\
495532ed618SSoby Mathew 		set_aapcs_args1(ctx, x0, x1);				\
496532ed618SSoby Mathew 	} while (0)
497532ed618SSoby Mathew #define set_aapcs_args3(ctx, x0, x1, x2, x3)			do {	\
498532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X3, x3);	\
499532ed618SSoby Mathew 		set_aapcs_args2(ctx, x0, x1, x2);			\
500532ed618SSoby Mathew 	} while (0)
501532ed618SSoby Mathew #define set_aapcs_args4(ctx, x0, x1, x2, x3, x4)		do {	\
502532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X4, x4);	\
503532ed618SSoby Mathew 		set_aapcs_args3(ctx, x0, x1, x2, x3);			\
504532ed618SSoby Mathew 	} while (0)
505532ed618SSoby Mathew #define set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5)		do {	\
506532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X5, x5);	\
507532ed618SSoby Mathew 		set_aapcs_args4(ctx, x0, x1, x2, x3, x4);		\
508532ed618SSoby Mathew 	} while (0)
509532ed618SSoby Mathew #define set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6)	do {	\
510532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X6, x6);	\
511532ed618SSoby Mathew 		set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5);		\
512532ed618SSoby Mathew 	} while (0)
513532ed618SSoby Mathew #define set_aapcs_args7(ctx, x0, x1, x2, x3, x4, x5, x6, x7)	do {	\
514532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X7, x7);	\
515532ed618SSoby Mathew 		set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6);	\
516532ed618SSoby Mathew 	} while (0)
517532ed618SSoby Mathew 
518532ed618SSoby Mathew /*******************************************************************************
519532ed618SSoby Mathew  * Function prototypes
520532ed618SSoby Mathew  ******************************************************************************/
521532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
522532ed618SSoby Mathew void fpregs_context_save(fp_regs_t *regs);
523532ed618SSoby Mathew void fpregs_context_restore(fp_regs_t *regs);
524532ed618SSoby Mathew #endif
525532ed618SSoby Mathew 
526d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */
527532ed618SSoby Mathew 
528a0fee747SAntonio Nino Diaz #endif /* CONTEXT_H */
529