xref: /rk3399_ARM-atf/include/lib/el3_runtime/aarch64/context.h (revision c282384dbb45b6185b4aba14efebbad110d18e49)
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)
112532ed618SSoby Mathew 
113532ed618SSoby Mathew /*
114532ed618SSoby Mathew  * If the platform is AArch64-only, there is no need to save and restore these
115532ed618SSoby Mathew  * AArch32 registers.
116532ed618SSoby Mathew  */
117532ed618SSoby Mathew #if CTX_INCLUDE_AARCH32_REGS
118e290a8fcSAlexei Fedorov #define CTX_SPSR_ABT		U(0xb0)	/* Align to the next 16 byte boundary */
119e290a8fcSAlexei Fedorov #define CTX_SPSR_UND		U(0xb8)
120e290a8fcSAlexei Fedorov #define CTX_SPSR_IRQ		U(0xc0)
121e290a8fcSAlexei Fedorov #define CTX_SPSR_FIQ		U(0xc8)
122e290a8fcSAlexei Fedorov #define CTX_DACR32_EL2		U(0xd0)
123e290a8fcSAlexei Fedorov #define CTX_IFSR32_EL2		U(0xd8)
124e290a8fcSAlexei Fedorov #define CTX_AARCH32_END		U(0xe0) /* Align to the next 16 byte boundary */
125532ed618SSoby Mathew #else
126e290a8fcSAlexei Fedorov #define CTX_AARCH32_END		U(0xb0)	/* Align to the next 16 byte boundary */
1274d1ccf0eSAntonio Nino Diaz #endif /* CTX_INCLUDE_AARCH32_REGS */
128532ed618SSoby Mathew 
129532ed618SSoby Mathew /*
130532ed618SSoby Mathew  * If the timer registers aren't saved and restored, we don't have to reserve
131532ed618SSoby Mathew  * space for them in the context
132532ed618SSoby Mathew  */
133532ed618SSoby Mathew #if NS_TIMER_SWITCH
1344d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CTL_EL0	(CTX_AARCH32_END + U(0x0))
1354d1ccf0eSAntonio Nino Diaz #define CTX_CNTP_CVAL_EL0	(CTX_AARCH32_END + U(0x8))
1364d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CTL_EL0	(CTX_AARCH32_END + U(0x10))
1374d1ccf0eSAntonio Nino Diaz #define CTX_CNTV_CVAL_EL0	(CTX_AARCH32_END + U(0x18))
1384d1ccf0eSAntonio Nino Diaz #define CTX_CNTKCTL_EL1		(CTX_AARCH32_END + U(0x20))
1394d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END	(CTX_AARCH32_END + U(0x30)) /* Align to the next 16 byte boundary */
140532ed618SSoby Mathew #else
1414d1ccf0eSAntonio Nino Diaz #define CTX_TIMER_SYSREGS_END	CTX_AARCH32_END
1424d1ccf0eSAntonio Nino Diaz #endif /* NS_TIMER_SWITCH */
1434d1ccf0eSAntonio Nino Diaz 
144*c282384dSGovindraj Raja #if ENABLE_FEAT_MTE2
1459dd94382SJustin Chadwell #define CTX_TFSRE0_EL1		(CTX_TIMER_SYSREGS_END + U(0x0))
1469dd94382SJustin Chadwell #define CTX_TFSR_EL1		(CTX_TIMER_SYSREGS_END + U(0x8))
1479dd94382SJustin Chadwell #define CTX_RGSR_EL1		(CTX_TIMER_SYSREGS_END + U(0x10))
1489dd94382SJustin Chadwell #define CTX_GCR_EL1		(CTX_TIMER_SYSREGS_END + U(0x18))
1499dd94382SJustin Chadwell 
1509dd94382SJustin Chadwell /* Align to the next 16 byte boundary */
1519dd94382SJustin Chadwell #define CTX_MTE_REGS_END	(CTX_TIMER_SYSREGS_END + U(0x20))
1529dd94382SJustin Chadwell #else
1539dd94382SJustin Chadwell #define CTX_MTE_REGS_END	CTX_TIMER_SYSREGS_END
154*c282384dSGovindraj Raja #endif /* ENABLE_FEAT_MTE2 */
1559dd94382SJustin Chadwell 
1564d1ccf0eSAntonio Nino Diaz /*
1572825946eSMax Shvetsov  * End of system registers.
1582825946eSMax Shvetsov  */
1592825946eSMax Shvetsov #define CTX_EL1_SYSREGS_END		CTX_MTE_REGS_END
1602825946eSMax Shvetsov 
161532ed618SSoby Mathew /*******************************************************************************
162532ed618SSoby Mathew  * Constants that allow assembler code to access members of and the 'fp_regs'
163532ed618SSoby Mathew  * structure at their correct offsets.
164532ed618SSoby Mathew  ******************************************************************************/
1652825946eSMax Shvetsov # define CTX_FPREGS_OFFSET	(CTX_EL1_SYSREGS_OFFSET + CTX_EL1_SYSREGS_END)
166fe007b2eSDimitris Papastamos #if CTX_INCLUDE_FPREGS
167030567e6SVarun Wadekar #define CTX_FP_Q0		U(0x0)
168030567e6SVarun Wadekar #define CTX_FP_Q1		U(0x10)
169030567e6SVarun Wadekar #define CTX_FP_Q2		U(0x20)
170030567e6SVarun Wadekar #define CTX_FP_Q3		U(0x30)
171030567e6SVarun Wadekar #define CTX_FP_Q4		U(0x40)
172030567e6SVarun Wadekar #define CTX_FP_Q5		U(0x50)
173030567e6SVarun Wadekar #define CTX_FP_Q6		U(0x60)
174030567e6SVarun Wadekar #define CTX_FP_Q7		U(0x70)
175030567e6SVarun Wadekar #define CTX_FP_Q8		U(0x80)
176030567e6SVarun Wadekar #define CTX_FP_Q9		U(0x90)
177030567e6SVarun Wadekar #define CTX_FP_Q10		U(0xa0)
178030567e6SVarun Wadekar #define CTX_FP_Q11		U(0xb0)
179030567e6SVarun Wadekar #define CTX_FP_Q12		U(0xc0)
180030567e6SVarun Wadekar #define CTX_FP_Q13		U(0xd0)
181030567e6SVarun Wadekar #define CTX_FP_Q14		U(0xe0)
182030567e6SVarun Wadekar #define CTX_FP_Q15		U(0xf0)
183030567e6SVarun Wadekar #define CTX_FP_Q16		U(0x100)
184030567e6SVarun Wadekar #define CTX_FP_Q17		U(0x110)
185030567e6SVarun Wadekar #define CTX_FP_Q18		U(0x120)
186030567e6SVarun Wadekar #define CTX_FP_Q19		U(0x130)
187030567e6SVarun Wadekar #define CTX_FP_Q20		U(0x140)
188030567e6SVarun Wadekar #define CTX_FP_Q21		U(0x150)
189030567e6SVarun Wadekar #define CTX_FP_Q22		U(0x160)
190030567e6SVarun Wadekar #define CTX_FP_Q23		U(0x170)
191030567e6SVarun Wadekar #define CTX_FP_Q24		U(0x180)
192030567e6SVarun Wadekar #define CTX_FP_Q25		U(0x190)
193030567e6SVarun Wadekar #define CTX_FP_Q26		U(0x1a0)
194030567e6SVarun Wadekar #define CTX_FP_Q27		U(0x1b0)
195030567e6SVarun Wadekar #define CTX_FP_Q28		U(0x1c0)
196030567e6SVarun Wadekar #define CTX_FP_Q29		U(0x1d0)
197030567e6SVarun Wadekar #define CTX_FP_Q30		U(0x1e0)
198030567e6SVarun Wadekar #define CTX_FP_Q31		U(0x1f0)
199030567e6SVarun Wadekar #define CTX_FP_FPSR		U(0x200)
200030567e6SVarun Wadekar #define CTX_FP_FPCR		U(0x208)
20191089f36SDavid Cunado #if CTX_INCLUDE_AARCH32_REGS
20291089f36SDavid Cunado #define CTX_FP_FPEXC32_EL2	U(0x210)
20391089f36SDavid Cunado #define CTX_FPREGS_END		U(0x220) /* Align to the next 16 byte boundary */
20491089f36SDavid Cunado #else
20591089f36SDavid Cunado #define CTX_FPREGS_END		U(0x210) /* Align to the next 16 byte boundary */
206d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_AARCH32_REGS */
207fe007b2eSDimitris Papastamos #else
208fe007b2eSDimitris Papastamos #define CTX_FPREGS_END		U(0)
209d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
210532ed618SSoby Mathew 
2114d1ccf0eSAntonio Nino Diaz /*******************************************************************************
2124d1ccf0eSAntonio Nino Diaz  * Registers related to CVE-2018-3639
2134d1ccf0eSAntonio Nino Diaz  ******************************************************************************/
214fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_OFFSET	(CTX_FPREGS_OFFSET + CTX_FPREGS_END)
215fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_DISABLE	U(0)
216fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_END		U(0x10) /* Align to the next 16 byte boundary */
217fe007b2eSDimitris Papastamos 
2185283962eSAntonio Nino Diaz /*******************************************************************************
2195283962eSAntonio Nino Diaz  * Registers related to ARMv8.3-PAuth.
2205283962eSAntonio Nino Diaz  ******************************************************************************/
2215283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_OFFSET	(CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_END)
2225283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
2235283962eSAntonio Nino Diaz #define CTX_PACIAKEY_LO		U(0x0)
2245283962eSAntonio Nino Diaz #define CTX_PACIAKEY_HI		U(0x8)
2255283962eSAntonio Nino Diaz #define CTX_PACIBKEY_LO		U(0x10)
2265283962eSAntonio Nino Diaz #define CTX_PACIBKEY_HI		U(0x18)
2275283962eSAntonio Nino Diaz #define CTX_PACDAKEY_LO		U(0x20)
2285283962eSAntonio Nino Diaz #define CTX_PACDAKEY_HI		U(0x28)
2295283962eSAntonio Nino Diaz #define CTX_PACDBKEY_LO		U(0x30)
2305283962eSAntonio Nino Diaz #define CTX_PACDBKEY_HI		U(0x38)
2315283962eSAntonio Nino Diaz #define CTX_PACGAKEY_LO		U(0x40)
2325283962eSAntonio Nino Diaz #define CTX_PACGAKEY_HI		U(0x48)
233ed108b56SAlexei Fedorov #define CTX_PAUTH_REGS_END	U(0x50) /* Align to the next 16 byte boundary */
2345283962eSAntonio Nino Diaz #else
2355283962eSAntonio Nino Diaz #define CTX_PAUTH_REGS_END	U(0)
2365283962eSAntonio Nino Diaz #endif /* CTX_INCLUDE_PAUTH_REGS */
2375283962eSAntonio Nino Diaz 
238461c0a5dSElizabeth Ho /*******************************************************************************
2399acff28aSArvind Ram Prakash  * Registers related to ARMv8.2-MPAM.
2409acff28aSArvind Ram Prakash  ******************************************************************************/
2419acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_OFFSET	(CTX_PAUTH_REGS_OFFSET + CTX_PAUTH_REGS_END)
2429acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
2439acff28aSArvind Ram Prakash #define CTX_MPAM2_EL2		U(0x0)
2449acff28aSArvind Ram Prakash #define CTX_MPAMHCR_EL2		U(0x8)
2459acff28aSArvind Ram Prakash #define CTX_MPAMVPM0_EL2	U(0x10)
2469acff28aSArvind Ram Prakash #define CTX_MPAMVPM1_EL2	U(0x18)
2479acff28aSArvind Ram Prakash #define CTX_MPAMVPM2_EL2	U(0x20)
2489acff28aSArvind Ram Prakash #define CTX_MPAMVPM3_EL2	U(0x28)
2499acff28aSArvind Ram Prakash #define CTX_MPAMVPM4_EL2	U(0x30)
2509acff28aSArvind Ram Prakash #define CTX_MPAMVPM5_EL2	U(0x38)
2519acff28aSArvind Ram Prakash #define CTX_MPAMVPM6_EL2	U(0x40)
2529acff28aSArvind Ram Prakash #define CTX_MPAMVPM7_EL2	U(0x48)
2539acff28aSArvind Ram Prakash #define CTX_MPAMVPMV_EL2	U(0x50)
2549acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END	U(0x60)
2559acff28aSArvind Ram Prakash #else
2569acff28aSArvind Ram Prakash #define CTX_MPAM_REGS_END	U(0x0)
2579acff28aSArvind Ram Prakash #endif /* CTX_INCLUDE_MPAM_REGS */
2589acff28aSArvind Ram Prakash 
2599acff28aSArvind Ram Prakash /*******************************************************************************
260461c0a5dSElizabeth Ho  * Registers initialised in a per-world context.
261461c0a5dSElizabeth Ho  ******************************************************************************/
262461c0a5dSElizabeth Ho #define CTX_CPTR_EL3			U(0x0)
263461c0a5dSElizabeth Ho #define CTX_ZCR_EL3			U(0x8)
264ac4f6aafSArvind Ram Prakash #define CTX_MPAM3_EL3			U(0x10)
265ac4f6aafSArvind Ram Prakash #define CTX_PERWORLD_EL3STATE_END	U(0x18)
266461c0a5dSElizabeth Ho 
267d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__
268532ed618SSoby Mathew 
269532ed618SSoby Mathew #include <stdint.h>
270532ed618SSoby Mathew 
27109d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
27209d40e0eSAntonio Nino Diaz 
273532ed618SSoby Mathew /*
274532ed618SSoby Mathew  * Common constants to help define the 'cpu_context' structure and its
275532ed618SSoby Mathew  * members below.
276532ed618SSoby Mathew  */
277030567e6SVarun Wadekar #define DWORD_SHIFT		U(3)
278532ed618SSoby Mathew #define DEFINE_REG_STRUCT(name, num_regs)	\
279532ed618SSoby Mathew 	typedef struct name {			\
2802fe75a2dSZelalem 		uint64_t ctx_regs[num_regs];	\
281532ed618SSoby Mathew 	}  __aligned(16) name##_t
282532ed618SSoby Mathew 
283532ed618SSoby Mathew /* Constants to determine the size of individual context structures */
284532ed618SSoby Mathew #define CTX_GPREG_ALL		(CTX_GPREGS_END >> DWORD_SHIFT)
2852825946eSMax Shvetsov #define CTX_EL1_SYSREGS_ALL	(CTX_EL1_SYSREGS_END >> DWORD_SHIFT)
286d6af2344SJayanth Dodderi Chidanand 
287532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
288532ed618SSoby Mathew # define CTX_FPREG_ALL		(CTX_FPREGS_END >> DWORD_SHIFT)
289532ed618SSoby Mathew #endif
290532ed618SSoby Mathew #define CTX_EL3STATE_ALL	(CTX_EL3STATE_END >> DWORD_SHIFT)
291fe007b2eSDimitris Papastamos #define CTX_CVE_2018_3639_ALL	(CTX_CVE_2018_3639_END >> DWORD_SHIFT)
2925283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
2935283962eSAntonio Nino Diaz # define CTX_PAUTH_REGS_ALL	(CTX_PAUTH_REGS_END >> DWORD_SHIFT)
2945283962eSAntonio Nino Diaz #endif
2959acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
2969acff28aSArvind Ram Prakash # define CTX_MPAM_REGS_ALL	(CTX_MPAM_REGS_END >> DWORD_SHIFT)
2979acff28aSArvind Ram Prakash #endif
298532ed618SSoby Mathew 
299532ed618SSoby Mathew /*
300532ed618SSoby Mathew  * AArch64 general purpose register context structure. Usually x0-x18,
301532ed618SSoby Mathew  * lr are saved as the compiler is expected to preserve the remaining
302532ed618SSoby Mathew  * callee saved registers if used by the C runtime and the assembler
303532ed618SSoby Mathew  * does not touch the remaining. But in case of world switch during
304532ed618SSoby Mathew  * exception handling, we need to save the callee registers too.
305532ed618SSoby Mathew  */
306532ed618SSoby Mathew DEFINE_REG_STRUCT(gp_regs, CTX_GPREG_ALL);
307532ed618SSoby Mathew 
308532ed618SSoby Mathew /*
3092825946eSMax Shvetsov  * AArch64 EL1 system register context structure for preserving the
31028f39f02SMax Shvetsov  * architectural state during world switches.
311532ed618SSoby Mathew  */
3122825946eSMax Shvetsov DEFINE_REG_STRUCT(el1_sysregs, CTX_EL1_SYSREGS_ALL);
3132825946eSMax Shvetsov 
314532ed618SSoby Mathew /*
315532ed618SSoby Mathew  * AArch64 floating point register context structure for preserving
316532ed618SSoby Mathew  * the floating point state during switches from one security state to
317532ed618SSoby Mathew  * another.
318532ed618SSoby Mathew  */
319532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
320532ed618SSoby Mathew DEFINE_REG_STRUCT(fp_regs, CTX_FPREG_ALL);
321532ed618SSoby Mathew #endif
322532ed618SSoby Mathew 
323532ed618SSoby Mathew /*
324532ed618SSoby Mathew  * Miscellaneous registers used by EL3 firmware to maintain its state
325532ed618SSoby Mathew  * across exception entries and exits
326532ed618SSoby Mathew  */
327532ed618SSoby Mathew DEFINE_REG_STRUCT(el3_state, CTX_EL3STATE_ALL);
328532ed618SSoby Mathew 
329fe007b2eSDimitris Papastamos /* Function pointer used by CVE-2018-3639 dynamic mitigation */
330fe007b2eSDimitris Papastamos DEFINE_REG_STRUCT(cve_2018_3639, CTX_CVE_2018_3639_ALL);
331fe007b2eSDimitris Papastamos 
3325283962eSAntonio Nino Diaz /* Registers associated to ARMv8.3-PAuth */
3335283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3345283962eSAntonio Nino Diaz DEFINE_REG_STRUCT(pauth, CTX_PAUTH_REGS_ALL);
3355283962eSAntonio Nino Diaz #endif
3365283962eSAntonio Nino Diaz 
3379acff28aSArvind Ram Prakash /* Registers associated to ARMv8.2 MPAM */
3389acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
3399acff28aSArvind Ram Prakash DEFINE_REG_STRUCT(mpam, CTX_MPAM_REGS_ALL);
3409acff28aSArvind Ram Prakash #endif
3419acff28aSArvind Ram Prakash 
342532ed618SSoby Mathew /*
343532ed618SSoby Mathew  * Macros to access members of any of the above structures using their
344532ed618SSoby Mathew  * offsets
345532ed618SSoby Mathew  */
3462fe75a2dSZelalem #define read_ctx_reg(ctx, offset)	((ctx)->ctx_regs[(offset) >> DWORD_SHIFT])
3472fe75a2dSZelalem #define write_ctx_reg(ctx, offset, val)	(((ctx)->ctx_regs[(offset) >> DWORD_SHIFT]) \
348ba6e5ca6SJeenu Viswambharan 					 = (uint64_t) (val))
349532ed618SSoby Mathew 
350532ed618SSoby Mathew /*
351c5ea4f8aSZelalem Aweke  * Top-level context structure which is used by EL3 firmware to preserve
352c5ea4f8aSZelalem Aweke  * the state of a core at the next lower EL in a given security state and
353c5ea4f8aSZelalem Aweke  * save enough EL3 meta data to be able to return to that EL and security
354c5ea4f8aSZelalem Aweke  * state. The context management library will be used to ensure that
355c5ea4f8aSZelalem Aweke  * SP_EL3 always points to an instance of this structure at exception
356c5ea4f8aSZelalem Aweke  * entry and exit.
357532ed618SSoby Mathew  */
358532ed618SSoby Mathew typedef struct cpu_context {
359532ed618SSoby Mathew 	gp_regs_t gpregs_ctx;
360532ed618SSoby Mathew 	el3_state_t el3state_ctx;
3612825946eSMax Shvetsov 	el1_sysregs_t el1_sysregs_ctx;
362d6af2344SJayanth Dodderi Chidanand 
363532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
364532ed618SSoby Mathew 	fp_regs_t fpregs_ctx;
365532ed618SSoby Mathew #endif
366fe007b2eSDimitris Papastamos 	cve_2018_3639_t cve_2018_3639_ctx;
367d6af2344SJayanth Dodderi Chidanand 
3685283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
3695283962eSAntonio Nino Diaz 	pauth_t pauth_ctx;
3705283962eSAntonio Nino Diaz #endif
371d6af2344SJayanth Dodderi Chidanand 
3729acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
3739acff28aSArvind Ram Prakash 	mpam_t	mpam_ctx;
3749acff28aSArvind Ram Prakash #endif
375d6af2344SJayanth Dodderi Chidanand 
376d6af2344SJayanth Dodderi Chidanand #if CTX_INCLUDE_EL2_REGS
377d6af2344SJayanth Dodderi Chidanand 	el2_sysregs_t el2_sysregs_ctx;
378d6af2344SJayanth Dodderi Chidanand #endif
379d6af2344SJayanth Dodderi Chidanand 
380532ed618SSoby Mathew } cpu_context_t;
381532ed618SSoby Mathew 
382461c0a5dSElizabeth Ho /*
383461c0a5dSElizabeth Ho  * Per-World Context.
384461c0a5dSElizabeth Ho  * It stores registers whose values can be shared across CPUs.
385461c0a5dSElizabeth Ho  */
386461c0a5dSElizabeth Ho typedef struct per_world_context {
387461c0a5dSElizabeth Ho 	uint64_t ctx_cptr_el3;
388461c0a5dSElizabeth Ho 	uint64_t ctx_zcr_el3;
389ac4f6aafSArvind Ram Prakash 	uint64_t ctx_mpam3_el3;
390461c0a5dSElizabeth Ho } per_world_context_t;
391461c0a5dSElizabeth Ho 
392461c0a5dSElizabeth Ho extern per_world_context_t per_world_context[CPU_DATA_CONTEXT_NUM];
393461c0a5dSElizabeth Ho 
394532ed618SSoby Mathew /* Macros to access members of the 'cpu_context_t' structure */
395532ed618SSoby Mathew #define get_el3state_ctx(h)	(&((cpu_context_t *) h)->el3state_ctx)
396532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
397532ed618SSoby Mathew # define get_fpregs_ctx(h)	(&((cpu_context_t *) h)->fpregs_ctx)
398532ed618SSoby Mathew #endif
3992825946eSMax Shvetsov #define get_el1_sysregs_ctx(h)	(&((cpu_context_t *) h)->el1_sysregs_ctx)
4002825946eSMax Shvetsov #if CTX_INCLUDE_EL2_REGS
4012825946eSMax Shvetsov # define get_el2_sysregs_ctx(h)	(&((cpu_context_t *) h)->el2_sysregs_ctx)
4022825946eSMax Shvetsov #endif
403532ed618SSoby Mathew #define get_gpregs_ctx(h)	(&((cpu_context_t *) h)->gpregs_ctx)
4046f03bc77SDimitris Papastamos #define get_cve_2018_3639_ctx(h)	(&((cpu_context_t *) h)->cve_2018_3639_ctx)
4055283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4065283962eSAntonio Nino Diaz # define get_pauth_ctx(h)	(&((cpu_context_t *) h)->pauth_ctx)
4075283962eSAntonio Nino Diaz #endif
4089acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4099acff28aSArvind Ram Prakash # define get_mpam_ctx(h)	(&((cpu_context_t *) h)->mpam_ctx)
4109acff28aSArvind Ram Prakash #endif
411532ed618SSoby Mathew 
412532ed618SSoby Mathew /*
413532ed618SSoby Mathew  * Compile time assertions related to the 'cpu_context' structure to
414532ed618SSoby Mathew  * ensure that the assembler and the compiler view of the offsets of
415532ed618SSoby Mathew  * the structure members is the same.
416532ed618SSoby Mathew  */
4179a90d720SElyes Haouas CASSERT(CTX_GPREGS_OFFSET == __builtin_offsetof(cpu_context_t, gpregs_ctx),
418532ed618SSoby Mathew 	assert_core_context_gp_offset_mismatch);
419d6af2344SJayanth Dodderi Chidanand 
420d6af2344SJayanth Dodderi Chidanand CASSERT(CTX_EL3STATE_OFFSET == __builtin_offsetof(cpu_context_t, el3state_ctx),
421d6af2344SJayanth Dodderi Chidanand 	assert_core_context_el3state_offset_mismatch);
422d6af2344SJayanth Dodderi Chidanand 
4239a90d720SElyes Haouas CASSERT(CTX_EL1_SYSREGS_OFFSET == __builtin_offsetof(cpu_context_t, el1_sysregs_ctx),
4242825946eSMax Shvetsov 	assert_core_context_el1_sys_offset_mismatch);
425d6af2344SJayanth Dodderi Chidanand 
426532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
4279a90d720SElyes Haouas CASSERT(CTX_FPREGS_OFFSET == __builtin_offsetof(cpu_context_t, fpregs_ctx),
428532ed618SSoby Mathew 	assert_core_context_fp_offset_mismatch);
429d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_FPREGS */
430d6af2344SJayanth Dodderi Chidanand 
4319a90d720SElyes Haouas CASSERT(CTX_CVE_2018_3639_OFFSET == __builtin_offsetof(cpu_context_t, cve_2018_3639_ctx),
432fe007b2eSDimitris Papastamos 	assert_core_context_cve_2018_3639_offset_mismatch);
433d6af2344SJayanth Dodderi Chidanand 
4345283962eSAntonio Nino Diaz #if CTX_INCLUDE_PAUTH_REGS
4359a90d720SElyes Haouas CASSERT(CTX_PAUTH_REGS_OFFSET == __builtin_offsetof(cpu_context_t, pauth_ctx),
4365283962eSAntonio Nino Diaz 	assert_core_context_pauth_offset_mismatch);
437d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_PAUTH_REGS */
438d6af2344SJayanth Dodderi Chidanand 
4399acff28aSArvind Ram Prakash #if CTX_INCLUDE_MPAM_REGS
4409acff28aSArvind Ram Prakash CASSERT(CTX_MPAM_REGS_OFFSET == __builtin_offsetof(cpu_context_t, mpam_ctx),
4419acff28aSArvind Ram Prakash 	assert_core_context_mpam_offset_mismatch);
442d6af2344SJayanth Dodderi Chidanand #endif /* CTX_INCLUDE_MPAM_REGS */
443532ed618SSoby Mathew 
444532ed618SSoby Mathew /*
445532ed618SSoby Mathew  * Helper macro to set the general purpose registers that correspond to
446532ed618SSoby Mathew  * parameters in an aapcs_64 call i.e. x0-x7
447532ed618SSoby Mathew  */
448532ed618SSoby Mathew #define set_aapcs_args0(ctx, x0)				do {	\
449532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X0, x0);	\
450532ed618SSoby Mathew 	} while (0)
451532ed618SSoby Mathew #define set_aapcs_args1(ctx, x0, x1)				do {	\
452532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X1, x1);	\
453532ed618SSoby Mathew 		set_aapcs_args0(ctx, x0);				\
454532ed618SSoby Mathew 	} while (0)
455532ed618SSoby Mathew #define set_aapcs_args2(ctx, x0, x1, x2)			do {	\
456532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X2, x2);	\
457532ed618SSoby Mathew 		set_aapcs_args1(ctx, x0, x1);				\
458532ed618SSoby Mathew 	} while (0)
459532ed618SSoby Mathew #define set_aapcs_args3(ctx, x0, x1, x2, x3)			do {	\
460532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X3, x3);	\
461532ed618SSoby Mathew 		set_aapcs_args2(ctx, x0, x1, x2);			\
462532ed618SSoby Mathew 	} while (0)
463532ed618SSoby Mathew #define set_aapcs_args4(ctx, x0, x1, x2, x3, x4)		do {	\
464532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X4, x4);	\
465532ed618SSoby Mathew 		set_aapcs_args3(ctx, x0, x1, x2, x3);			\
466532ed618SSoby Mathew 	} while (0)
467532ed618SSoby Mathew #define set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5)		do {	\
468532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X5, x5);	\
469532ed618SSoby Mathew 		set_aapcs_args4(ctx, x0, x1, x2, x3, x4);		\
470532ed618SSoby Mathew 	} while (0)
471532ed618SSoby Mathew #define set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6)	do {	\
472532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X6, x6);	\
473532ed618SSoby Mathew 		set_aapcs_args5(ctx, x0, x1, x2, x3, x4, x5);		\
474532ed618SSoby Mathew 	} while (0)
475532ed618SSoby Mathew #define set_aapcs_args7(ctx, x0, x1, x2, x3, x4, x5, x6, x7)	do {	\
476532ed618SSoby Mathew 		write_ctx_reg(get_gpregs_ctx(ctx), CTX_GPREG_X7, x7);	\
477532ed618SSoby Mathew 		set_aapcs_args6(ctx, x0, x1, x2, x3, x4, x5, x6);	\
478532ed618SSoby Mathew 	} while (0)
479532ed618SSoby Mathew 
480532ed618SSoby Mathew /*******************************************************************************
481532ed618SSoby Mathew  * Function prototypes
482532ed618SSoby Mathew  ******************************************************************************/
483532ed618SSoby Mathew #if CTX_INCLUDE_FPREGS
484532ed618SSoby Mathew void fpregs_context_save(fp_regs_t *regs);
485532ed618SSoby Mathew void fpregs_context_restore(fp_regs_t *regs);
486532ed618SSoby Mathew #endif
487532ed618SSoby Mathew 
488d5dfdeb6SJulius Werner #endif /* __ASSEMBLER__ */
489532ed618SSoby Mathew 
490a0fee747SAntonio Nino Diaz #endif /* CONTEXT_H */
491