xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context.S (revision 1d6d6802dd547c8b378a9a47572ee72e68cceb3b)
1532ed618SSoby Mathew/*
2ed804406SRohit Mathew * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew */
6532ed618SSoby Mathew
7532ed618SSoby Mathew#include <arch.h>
8532ed618SSoby Mathew#include <asm_macros.S>
9bb9549baSJan Dabros#include <assert_macros.S>
10532ed618SSoby Mathew#include <context.h>
113b8456bdSManish V Badarkhe#include <el3_common_macros.S>
12532ed618SSoby Mathew
1328f39f02SMax Shvetsov#if CTX_INCLUDE_EL2_REGS
14d20052f3SZelalem Aweke	.global	el2_sysregs_context_save_common
15d20052f3SZelalem Aweke	.global	el2_sysregs_context_restore_common
16d20052f3SZelalem Aweke#if CTX_INCLUDE_MTE_REGS
17d20052f3SZelalem Aweke	.global	el2_sysregs_context_save_mte
18d20052f3SZelalem Aweke	.global	el2_sysregs_context_restore_mte
19d20052f3SZelalem Aweke#endif /* CTX_INCLUDE_MTE_REGS */
20d20052f3SZelalem Aweke#if RAS_EXTENSION
21d20052f3SZelalem Aweke	.global	el2_sysregs_context_save_ras
22d20052f3SZelalem Aweke	.global	el2_sysregs_context_restore_ras
23d20052f3SZelalem Aweke#endif /* RAS_EXTENSION */
240ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_EL2_REGS */
2528f39f02SMax Shvetsov
26532ed618SSoby Mathew	.global	el1_sysregs_context_save
27532ed618SSoby Mathew	.global	el1_sysregs_context_restore
28532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
29532ed618SSoby Mathew	.global	fpregs_context_save
30532ed618SSoby Mathew	.global	fpregs_context_restore
310ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_FPREGS */
3297215e0fSDaniel Boulby	.global	prepare_el3_entry
33ed108b56SAlexei Fedorov	.global	restore_gp_pmcr_pauth_regs
343b8456bdSManish V Badarkhe	.global save_and_update_ptw_el1_sys_regs
35532ed618SSoby Mathew	.global	el3_exit
36532ed618SSoby Mathew
3728f39f02SMax Shvetsov#if CTX_INCLUDE_EL2_REGS
3828f39f02SMax Shvetsov
3928f39f02SMax Shvetsov/* -----------------------------------------------------
40d20052f3SZelalem Aweke * The following functions strictly follow the AArch64
41a7cf2743SMax Shvetsov * PCS to use x9-x16 (temporary caller-saved registers)
42d20052f3SZelalem Aweke * to save/restore EL2 system register context.
43d20052f3SZelalem Aweke * el2_sysregs_context_save/restore_common functions
44d20052f3SZelalem Aweke * save and restore registers that are common to all
45d20052f3SZelalem Aweke * configurations. The rest of the functions save and
46d20052f3SZelalem Aweke * restore EL2 system registers that are present when a
47d20052f3SZelalem Aweke * particular feature is enabled. All functions assume
48d20052f3SZelalem Aweke * that 'x0' is pointing to a 'el2_sys_regs' structure
49d20052f3SZelalem Aweke * where the register context will be saved/restored.
502825946eSMax Shvetsov *
512825946eSMax Shvetsov * The following registers are not added.
522825946eSMax Shvetsov * AMEVCNTVOFF0<n>_EL2
532825946eSMax Shvetsov * AMEVCNTVOFF1<n>_EL2
542825946eSMax Shvetsov * ICH_AP0R<n>_EL2
552825946eSMax Shvetsov * ICH_AP1R<n>_EL2
562825946eSMax Shvetsov * ICH_LR<n>_EL2
5728f39f02SMax Shvetsov * -----------------------------------------------------
5828f39f02SMax Shvetsov */
59d20052f3SZelalem Awekefunc el2_sysregs_context_save_common
6028f39f02SMax Shvetsov	mrs	x9, actlr_el2
612825946eSMax Shvetsov	mrs	x10, afsr0_el2
622825946eSMax Shvetsov	stp	x9, x10, [x0, #CTX_ACTLR_EL2]
6328f39f02SMax Shvetsov
642825946eSMax Shvetsov	mrs	x11, afsr1_el2
652825946eSMax Shvetsov	mrs	x12, amair_el2
662825946eSMax Shvetsov	stp	x11, x12, [x0, #CTX_AFSR1_EL2]
6728f39f02SMax Shvetsov
682825946eSMax Shvetsov	mrs	x13, cnthctl_el2
69a7cf2743SMax Shvetsov	mrs	x14, cntvoff_el2
702825946eSMax Shvetsov	stp	x13, x14, [x0, #CTX_CNTHCTL_EL2]
7128f39f02SMax Shvetsov
72a7cf2743SMax Shvetsov	mrs	x15, cptr_el2
73a7cf2743SMax Shvetsov	str	x15, [x0, #CTX_CPTR_EL2]
7428f39f02SMax Shvetsov
750f777eabSArunachalam Ganapathy#if CTX_INCLUDE_AARCH32_REGS
76a7cf2743SMax Shvetsov	mrs	x16, dbgvcr32_el2
77a7cf2743SMax Shvetsov	str	x16, [x0, #CTX_DBGVCR32_EL2]
780ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */
7928f39f02SMax Shvetsov
80a7cf2743SMax Shvetsov	mrs	x9, elr_el2
81a7cf2743SMax Shvetsov	mrs	x10, esr_el2
82a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_ELR_EL2]
8328f39f02SMax Shvetsov
84a7cf2743SMax Shvetsov	mrs	x11, far_el2
85a7cf2743SMax Shvetsov	mrs	x12, hacr_el2
86a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_FAR_EL2]
8728f39f02SMax Shvetsov
88a7cf2743SMax Shvetsov	mrs	x13, hcr_el2
89a7cf2743SMax Shvetsov	mrs	x14, hpfar_el2
90a7cf2743SMax Shvetsov	stp	x13, x14, [x0, #CTX_HCR_EL2]
9128f39f02SMax Shvetsov
92a7cf2743SMax Shvetsov	mrs	x15, hstr_el2
93a7cf2743SMax Shvetsov	mrs	x16, ICC_SRE_EL2
94a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_HSTR_EL2]
9528f39f02SMax Shvetsov
96a7cf2743SMax Shvetsov	mrs	x9, ICH_HCR_EL2
97a7cf2743SMax Shvetsov	mrs	x10, ICH_VMCR_EL2
98a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_ICH_HCR_EL2]
9928f39f02SMax Shvetsov
100a7cf2743SMax Shvetsov	mrs	x11, mair_el2
101a7cf2743SMax Shvetsov	mrs	x12, mdcr_el2
102a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_MAIR_EL2]
103a7cf2743SMax Shvetsov
104a7cf2743SMax Shvetsov	mrs	x14, sctlr_el2
105a7cf2743SMax Shvetsov	str	x14, [x0, #CTX_SCTLR_EL2]
10628f39f02SMax Shvetsov
107a7cf2743SMax Shvetsov	mrs	x15, spsr_el2
108a7cf2743SMax Shvetsov	mrs	x16, sp_el2
109a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_SPSR_EL2]
11028f39f02SMax Shvetsov
111a7cf2743SMax Shvetsov	mrs	x9, tcr_el2
112a7cf2743SMax Shvetsov	mrs	x10, tpidr_el2
113a7cf2743SMax Shvetsov	stp	x9, x10, [x0, #CTX_TCR_EL2]
11428f39f02SMax Shvetsov
115a7cf2743SMax Shvetsov	mrs	x11, ttbr0_el2
116a7cf2743SMax Shvetsov	mrs	x12, vbar_el2
117a7cf2743SMax Shvetsov	stp	x11, x12, [x0, #CTX_TTBR0_EL2]
11828f39f02SMax Shvetsov
119a7cf2743SMax Shvetsov	mrs	x13, vmpidr_el2
120a7cf2743SMax Shvetsov	mrs	x14, vpidr_el2
121a7cf2743SMax Shvetsov	stp	x13, x14, [x0, #CTX_VMPIDR_EL2]
12228f39f02SMax Shvetsov
123a7cf2743SMax Shvetsov	mrs	x15, vtcr_el2
124a7cf2743SMax Shvetsov	mrs	x16, vttbr_el2
125a7cf2743SMax Shvetsov	stp	x15, x16, [x0, #CTX_VTCR_EL2]
12628f39f02SMax Shvetsov	ret
127d20052f3SZelalem Awekeendfunc el2_sysregs_context_save_common
12828f39f02SMax Shvetsov
129d20052f3SZelalem Awekefunc el2_sysregs_context_restore_common
1302825946eSMax Shvetsov	ldp	x9, x10, [x0, #CTX_ACTLR_EL2]
13128f39f02SMax Shvetsov	msr	actlr_el2, x9
1322825946eSMax Shvetsov	msr	afsr0_el2, x10
13328f39f02SMax Shvetsov
1342825946eSMax Shvetsov	ldp	x11, x12, [x0, #CTX_AFSR1_EL2]
1352825946eSMax Shvetsov	msr	afsr1_el2, x11
1362825946eSMax Shvetsov	msr	amair_el2, x12
13728f39f02SMax Shvetsov
1382825946eSMax Shvetsov	ldp	x13, x14, [x0, #CTX_CNTHCTL_EL2]
1392825946eSMax Shvetsov	msr	cnthctl_el2, x13
140a7cf2743SMax Shvetsov	msr	cntvoff_el2, x14
14128f39f02SMax Shvetsov
142a7cf2743SMax Shvetsov	ldr	x15, [x0, #CTX_CPTR_EL2]
143a7cf2743SMax Shvetsov	msr	cptr_el2, x15
14428f39f02SMax Shvetsov
1450f777eabSArunachalam Ganapathy#if CTX_INCLUDE_AARCH32_REGS
146a7cf2743SMax Shvetsov	ldr	x16, [x0, #CTX_DBGVCR32_EL2]
147a7cf2743SMax Shvetsov	msr	dbgvcr32_el2, x16
1480ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */
14928f39f02SMax Shvetsov
150a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_ELR_EL2]
151a7cf2743SMax Shvetsov	msr	elr_el2, x9
152a7cf2743SMax Shvetsov	msr	esr_el2, x10
15328f39f02SMax Shvetsov
154a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_FAR_EL2]
155a7cf2743SMax Shvetsov	msr	far_el2, x11
156a7cf2743SMax Shvetsov	msr	hacr_el2, x12
15728f39f02SMax Shvetsov
158a7cf2743SMax Shvetsov	ldp	x13, x14, [x0, #CTX_HCR_EL2]
159a7cf2743SMax Shvetsov	msr	hcr_el2, x13
160a7cf2743SMax Shvetsov	msr	hpfar_el2, x14
16128f39f02SMax Shvetsov
162a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_HSTR_EL2]
163a7cf2743SMax Shvetsov	msr	hstr_el2, x15
164a7cf2743SMax Shvetsov	msr	ICC_SRE_EL2, x16
16528f39f02SMax Shvetsov
166a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_ICH_HCR_EL2]
167a7cf2743SMax Shvetsov	msr	ICH_HCR_EL2, x9
168a7cf2743SMax Shvetsov	msr	ICH_VMCR_EL2, x10
169a7cf2743SMax Shvetsov
170a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_MAIR_EL2]
171a7cf2743SMax Shvetsov	msr	mair_el2, x11
172a7cf2743SMax Shvetsov	msr	mdcr_el2, x12
17328f39f02SMax Shvetsov
174a7cf2743SMax Shvetsov	ldr	x14, [x0, #CTX_SCTLR_EL2]
175a7cf2743SMax Shvetsov	msr	sctlr_el2, x14
17628f39f02SMax Shvetsov
177a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_SPSR_EL2]
178a7cf2743SMax Shvetsov	msr	spsr_el2, x15
179a7cf2743SMax Shvetsov	msr	sp_el2, x16
18028f39f02SMax Shvetsov
181a7cf2743SMax Shvetsov	ldp	x9, x10, [x0, #CTX_TCR_EL2]
182a7cf2743SMax Shvetsov	msr	tcr_el2, x9
183a7cf2743SMax Shvetsov	msr	tpidr_el2, x10
18428f39f02SMax Shvetsov
185a7cf2743SMax Shvetsov	ldp	x11, x12, [x0, #CTX_TTBR0_EL2]
186a7cf2743SMax Shvetsov	msr	ttbr0_el2, x11
187a7cf2743SMax Shvetsov	msr	vbar_el2, x12
18828f39f02SMax Shvetsov
189a7cf2743SMax Shvetsov	ldp	x13, x14, [x0, #CTX_VMPIDR_EL2]
190a7cf2743SMax Shvetsov	msr	vmpidr_el2, x13
191a7cf2743SMax Shvetsov	msr	vpidr_el2, x14
19228f39f02SMax Shvetsov
193a7cf2743SMax Shvetsov	ldp	x15, x16, [x0, #CTX_VTCR_EL2]
194a7cf2743SMax Shvetsov	msr	vtcr_el2, x15
195a7cf2743SMax Shvetsov	msr	vttbr_el2, x16
196d20052f3SZelalem Aweke	ret
197d20052f3SZelalem Awekeendfunc el2_sysregs_context_restore_common
198d20052f3SZelalem Aweke
1992825946eSMax Shvetsov#if CTX_INCLUDE_MTE_REGS
200d20052f3SZelalem Awekefunc el2_sysregs_context_save_mte
201d20052f3SZelalem Aweke	mrs	x9, TFSR_EL2
202d20052f3SZelalem Aweke	str	x9, [x0, #CTX_TFSR_EL2]
203d20052f3SZelalem Aweke	ret
204d20052f3SZelalem Awekeendfunc el2_sysregs_context_save_mte
205d20052f3SZelalem Aweke
206d20052f3SZelalem Awekefunc el2_sysregs_context_restore_mte
207fb2072b0SManish V Badarkhe	ldr	x9, [x0, #CTX_TFSR_EL2]
208fb2072b0SManish V Badarkhe	msr	TFSR_EL2, x9
209d20052f3SZelalem Aweke	ret
210d20052f3SZelalem Awekeendfunc el2_sysregs_context_restore_mte
2110ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_MTE_REGS */
21228f39f02SMax Shvetsov
2130ce220afSJayanth Dodderi Chidanand#if RAS_EXTENSION
214d20052f3SZelalem Awekefunc el2_sysregs_context_save_ras
215d20052f3SZelalem Aweke	/*
216d20052f3SZelalem Aweke	 * VDISR_EL2 and VSESR_EL2 registers are saved only when
217d20052f3SZelalem Aweke	 * FEAT_RAS is supported.
218d20052f3SZelalem Aweke	 */
219d20052f3SZelalem Aweke	mrs	x11, vdisr_el2
220d20052f3SZelalem Aweke	mrs	x12, vsesr_el2
221d20052f3SZelalem Aweke	stp	x11, x12, [x0, #CTX_VDISR_EL2]
222d20052f3SZelalem Aweke	ret
223d20052f3SZelalem Awekeendfunc el2_sysregs_context_save_ras
224d20052f3SZelalem Aweke
225d20052f3SZelalem Awekefunc el2_sysregs_context_restore_ras
2260ce220afSJayanth Dodderi Chidanand	/*
2270ce220afSJayanth Dodderi Chidanand	 * VDISR_EL2 and VSESR_EL2 registers are restored only when FEAT_RAS
2280ce220afSJayanth Dodderi Chidanand	 * is supported.
2290ce220afSJayanth Dodderi Chidanand	 */
2300ce220afSJayanth Dodderi Chidanand	ldp	x11, x12, [x0, #CTX_VDISR_EL2]
2310ce220afSJayanth Dodderi Chidanand	msr	vdisr_el2, x11
2320ce220afSJayanth Dodderi Chidanand	msr	vsesr_el2, x12
233d20052f3SZelalem Aweke	ret
234d20052f3SZelalem Awekeendfunc el2_sysregs_context_restore_ras
2350ce220afSJayanth Dodderi Chidanand#endif /* RAS_EXTENSION */
2360ce220afSJayanth Dodderi Chidanand
23728f39f02SMax Shvetsov#endif /* CTX_INCLUDE_EL2_REGS */
23828f39f02SMax Shvetsov
239ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
240ed108b56SAlexei Fedorov * The following function strictly follows the AArch64 PCS to use
241ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers) to save EL1 system
242ed108b56SAlexei Fedorov * register context. It assumes that 'x0' is pointing to a
243ed108b56SAlexei Fedorov * 'el1_sys_regs' structure where the register context will be saved.
244ed108b56SAlexei Fedorov * ------------------------------------------------------------------
245532ed618SSoby Mathew */
246532ed618SSoby Mathewfunc el1_sysregs_context_save
247532ed618SSoby Mathew
248532ed618SSoby Mathew	mrs	x9, spsr_el1
249532ed618SSoby Mathew	mrs	x10, elr_el1
250532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_SPSR_EL1]
251532ed618SSoby Mathew
2523b8456bdSManish V Badarkhe#if !ERRATA_SPECULATIVE_AT
253532ed618SSoby Mathew	mrs	x15, sctlr_el1
254cb55615cSManish V Badarkhe	mrs	x16, tcr_el1
255532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_SCTLR_EL1]
2560ce220afSJayanth Dodderi Chidanand#endif /* ERRATA_SPECULATIVE_AT */
257532ed618SSoby Mathew
258532ed618SSoby Mathew	mrs	x17, cpacr_el1
259532ed618SSoby Mathew	mrs	x9, csselr_el1
260532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CPACR_EL1]
261532ed618SSoby Mathew
262532ed618SSoby Mathew	mrs	x10, sp_el1
263532ed618SSoby Mathew	mrs	x11, esr_el1
264532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_SP_EL1]
265532ed618SSoby Mathew
266532ed618SSoby Mathew	mrs	x12, ttbr0_el1
267532ed618SSoby Mathew	mrs	x13, ttbr1_el1
268532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_TTBR0_EL1]
269532ed618SSoby Mathew
270532ed618SSoby Mathew	mrs	x14, mair_el1
271532ed618SSoby Mathew	mrs	x15, amair_el1
272532ed618SSoby Mathew	stp	x14, x15, [x0, #CTX_MAIR_EL1]
273532ed618SSoby Mathew
274cb55615cSManish V Badarkhe	mrs	x16, actlr_el1
275532ed618SSoby Mathew	mrs	x17, tpidr_el1
276cb55615cSManish V Badarkhe	stp	x16, x17, [x0, #CTX_ACTLR_EL1]
277532ed618SSoby Mathew
278532ed618SSoby Mathew	mrs	x9, tpidr_el0
279532ed618SSoby Mathew	mrs	x10, tpidrro_el0
280532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_TPIDR_EL0]
281532ed618SSoby Mathew
282532ed618SSoby Mathew	mrs	x13, par_el1
283532ed618SSoby Mathew	mrs	x14, far_el1
284532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_PAR_EL1]
285532ed618SSoby Mathew
286532ed618SSoby Mathew	mrs	x15, afsr0_el1
287532ed618SSoby Mathew	mrs	x16, afsr1_el1
288532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_AFSR0_EL1]
289532ed618SSoby Mathew
290532ed618SSoby Mathew	mrs	x17, contextidr_el1
291532ed618SSoby Mathew	mrs	x9, vbar_el1
292532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
293532ed618SSoby Mathew
294532ed618SSoby Mathew	/* Save AArch32 system registers if the build has instructed so */
295532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
296532ed618SSoby Mathew	mrs	x11, spsr_abt
297532ed618SSoby Mathew	mrs	x12, spsr_und
298532ed618SSoby Mathew	stp	x11, x12, [x0, #CTX_SPSR_ABT]
299532ed618SSoby Mathew
300532ed618SSoby Mathew	mrs	x13, spsr_irq
301532ed618SSoby Mathew	mrs	x14, spsr_fiq
302532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_SPSR_IRQ]
303532ed618SSoby Mathew
304532ed618SSoby Mathew	mrs	x15, dacr32_el2
305532ed618SSoby Mathew	mrs	x16, ifsr32_el2
306532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_DACR32_EL2]
3070ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */
308532ed618SSoby Mathew
309532ed618SSoby Mathew	/* Save NS timer registers if the build has instructed so */
310532ed618SSoby Mathew#if NS_TIMER_SWITCH
311532ed618SSoby Mathew	mrs	x10, cntp_ctl_el0
312532ed618SSoby Mathew	mrs	x11, cntp_cval_el0
313532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
314532ed618SSoby Mathew
315532ed618SSoby Mathew	mrs	x12, cntv_ctl_el0
316532ed618SSoby Mathew	mrs	x13, cntv_cval_el0
317532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
318532ed618SSoby Mathew
319532ed618SSoby Mathew	mrs	x14, cntkctl_el1
320532ed618SSoby Mathew	str	x14, [x0, #CTX_CNTKCTL_EL1]
3210ce220afSJayanth Dodderi Chidanand#endif /* NS_TIMER_SWITCH */
322532ed618SSoby Mathew
3239dd94382SJustin Chadwell	/* Save MTE system registers if the build has instructed so */
3249dd94382SJustin Chadwell#if CTX_INCLUDE_MTE_REGS
3259dd94382SJustin Chadwell	mrs	x15, TFSRE0_EL1
3269dd94382SJustin Chadwell	mrs	x16, TFSR_EL1
3279dd94382SJustin Chadwell	stp	x15, x16, [x0, #CTX_TFSRE0_EL1]
3289dd94382SJustin Chadwell
3299dd94382SJustin Chadwell	mrs	x9, RGSR_EL1
3309dd94382SJustin Chadwell	mrs	x10, GCR_EL1
3319dd94382SJustin Chadwell	stp	x9, x10, [x0, #CTX_RGSR_EL1]
3320ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_MTE_REGS */
3339dd94382SJustin Chadwell
334532ed618SSoby Mathew	ret
335532ed618SSoby Mathewendfunc el1_sysregs_context_save
336532ed618SSoby Mathew
337ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
338ed108b56SAlexei Fedorov * The following function strictly follows the AArch64 PCS to use
339ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers) to restore EL1 system
340ed108b56SAlexei Fedorov * register context.  It assumes that 'x0' is pointing to a
341ed108b56SAlexei Fedorov * 'el1_sys_regs' structure from where the register context will be
342ed108b56SAlexei Fedorov * restored
343ed108b56SAlexei Fedorov * ------------------------------------------------------------------
344532ed618SSoby Mathew */
345532ed618SSoby Mathewfunc el1_sysregs_context_restore
346532ed618SSoby Mathew
347532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_SPSR_EL1]
348532ed618SSoby Mathew	msr	spsr_el1, x9
349532ed618SSoby Mathew	msr	elr_el1, x10
350532ed618SSoby Mathew
3513b8456bdSManish V Badarkhe#if !ERRATA_SPECULATIVE_AT
352fb2072b0SManish V Badarkhe	ldp	x15, x16, [x0, #CTX_SCTLR_EL1]
353fb2072b0SManish V Badarkhe	msr	sctlr_el1, x15
354cb55615cSManish V Badarkhe	msr	tcr_el1, x16
3550ce220afSJayanth Dodderi Chidanand#endif /* ERRATA_SPECULATIVE_AT */
356532ed618SSoby Mathew
357532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CPACR_EL1]
358532ed618SSoby Mathew	msr	cpacr_el1, x17
359532ed618SSoby Mathew	msr	csselr_el1, x9
360532ed618SSoby Mathew
361532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_SP_EL1]
362532ed618SSoby Mathew	msr	sp_el1, x10
363532ed618SSoby Mathew	msr	esr_el1, x11
364532ed618SSoby Mathew
365532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_TTBR0_EL1]
366532ed618SSoby Mathew	msr	ttbr0_el1, x12
367532ed618SSoby Mathew	msr	ttbr1_el1, x13
368532ed618SSoby Mathew
369532ed618SSoby Mathew	ldp	x14, x15, [x0, #CTX_MAIR_EL1]
370532ed618SSoby Mathew	msr	mair_el1, x14
371532ed618SSoby Mathew	msr	amair_el1, x15
372532ed618SSoby Mathew
373cb55615cSManish V Badarkhe	ldp 	x16, x17, [x0, #CTX_ACTLR_EL1]
374cb55615cSManish V Badarkhe	msr	actlr_el1, x16
375fb2072b0SManish V Badarkhe	msr	tpidr_el1, x17
376532ed618SSoby Mathew
377532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_TPIDR_EL0]
378532ed618SSoby Mathew	msr	tpidr_el0, x9
379532ed618SSoby Mathew	msr	tpidrro_el0, x10
380532ed618SSoby Mathew
381532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_PAR_EL1]
382532ed618SSoby Mathew	msr	par_el1, x13
383532ed618SSoby Mathew	msr	far_el1, x14
384532ed618SSoby Mathew
385532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_AFSR0_EL1]
386532ed618SSoby Mathew	msr	afsr0_el1, x15
387532ed618SSoby Mathew	msr	afsr1_el1, x16
388532ed618SSoby Mathew
389532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
390532ed618SSoby Mathew	msr	contextidr_el1, x17
391532ed618SSoby Mathew	msr	vbar_el1, x9
392532ed618SSoby Mathew
393532ed618SSoby Mathew	/* Restore AArch32 system registers if the build has instructed so */
394532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
395532ed618SSoby Mathew	ldp	x11, x12, [x0, #CTX_SPSR_ABT]
396532ed618SSoby Mathew	msr	spsr_abt, x11
397532ed618SSoby Mathew	msr	spsr_und, x12
398532ed618SSoby Mathew
399532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_SPSR_IRQ]
400532ed618SSoby Mathew	msr	spsr_irq, x13
401532ed618SSoby Mathew	msr	spsr_fiq, x14
402532ed618SSoby Mathew
403532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_DACR32_EL2]
404532ed618SSoby Mathew	msr	dacr32_el2, x15
405532ed618SSoby Mathew	msr	ifsr32_el2, x16
4060ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */
4070ce220afSJayanth Dodderi Chidanand
408532ed618SSoby Mathew	/* Restore NS timer registers if the build has instructed so */
409532ed618SSoby Mathew#if NS_TIMER_SWITCH
410532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
411532ed618SSoby Mathew	msr	cntp_ctl_el0, x10
412532ed618SSoby Mathew	msr	cntp_cval_el0, x11
413532ed618SSoby Mathew
414532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
415532ed618SSoby Mathew	msr	cntv_ctl_el0, x12
416532ed618SSoby Mathew	msr	cntv_cval_el0, x13
417532ed618SSoby Mathew
418532ed618SSoby Mathew	ldr	x14, [x0, #CTX_CNTKCTL_EL1]
419532ed618SSoby Mathew	msr	cntkctl_el1, x14
4200ce220afSJayanth Dodderi Chidanand#endif /* NS_TIMER_SWITCH */
4210ce220afSJayanth Dodderi Chidanand
4229dd94382SJustin Chadwell	/* Restore MTE system registers if the build has instructed so */
4239dd94382SJustin Chadwell#if CTX_INCLUDE_MTE_REGS
4249dd94382SJustin Chadwell	ldp	x11, x12, [x0, #CTX_TFSRE0_EL1]
4259dd94382SJustin Chadwell	msr	TFSRE0_EL1, x11
4269dd94382SJustin Chadwell	msr	TFSR_EL1, x12
4279dd94382SJustin Chadwell
4289dd94382SJustin Chadwell	ldp	x13, x14, [x0, #CTX_RGSR_EL1]
4299dd94382SJustin Chadwell	msr	RGSR_EL1, x13
4309dd94382SJustin Chadwell	msr	GCR_EL1, x14
4310ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_MTE_REGS */
432532ed618SSoby Mathew
433532ed618SSoby Mathew	/* No explict ISB required here as ERET covers it */
434532ed618SSoby Mathew	ret
435532ed618SSoby Mathewendfunc el1_sysregs_context_restore
436532ed618SSoby Mathew
437ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
438ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use
439ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers according to AArch64 PCS)
440ed108b56SAlexei Fedorov * to save floating point register context. It assumes that 'x0' is
441ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure where the register context will
442532ed618SSoby Mathew * be saved.
443532ed618SSoby Mathew *
444ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set.
445ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in
446ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared.
447532ed618SSoby Mathew *
448532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
449ed108b56SAlexei Fedorov * ------------------------------------------------------------------
450532ed618SSoby Mathew */
451532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
452532ed618SSoby Mathewfunc fpregs_context_save
453532ed618SSoby Mathew	stp	q0, q1, [x0, #CTX_FP_Q0]
454532ed618SSoby Mathew	stp	q2, q3, [x0, #CTX_FP_Q2]
455532ed618SSoby Mathew	stp	q4, q5, [x0, #CTX_FP_Q4]
456532ed618SSoby Mathew	stp	q6, q7, [x0, #CTX_FP_Q6]
457532ed618SSoby Mathew	stp	q8, q9, [x0, #CTX_FP_Q8]
458532ed618SSoby Mathew	stp	q10, q11, [x0, #CTX_FP_Q10]
459532ed618SSoby Mathew	stp	q12, q13, [x0, #CTX_FP_Q12]
460532ed618SSoby Mathew	stp	q14, q15, [x0, #CTX_FP_Q14]
461532ed618SSoby Mathew	stp	q16, q17, [x0, #CTX_FP_Q16]
462532ed618SSoby Mathew	stp	q18, q19, [x0, #CTX_FP_Q18]
463532ed618SSoby Mathew	stp	q20, q21, [x0, #CTX_FP_Q20]
464532ed618SSoby Mathew	stp	q22, q23, [x0, #CTX_FP_Q22]
465532ed618SSoby Mathew	stp	q24, q25, [x0, #CTX_FP_Q24]
466532ed618SSoby Mathew	stp	q26, q27, [x0, #CTX_FP_Q26]
467532ed618SSoby Mathew	stp	q28, q29, [x0, #CTX_FP_Q28]
468532ed618SSoby Mathew	stp	q30, q31, [x0, #CTX_FP_Q30]
469532ed618SSoby Mathew
470532ed618SSoby Mathew	mrs	x9, fpsr
471532ed618SSoby Mathew	str	x9, [x0, #CTX_FP_FPSR]
472532ed618SSoby Mathew
473532ed618SSoby Mathew	mrs	x10, fpcr
474532ed618SSoby Mathew	str	x10, [x0, #CTX_FP_FPCR]
475532ed618SSoby Mathew
47691089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
47791089f36SDavid Cunado	mrs	x11, fpexc32_el2
47891089f36SDavid Cunado	str	x11, [x0, #CTX_FP_FPEXC32_EL2]
4790ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */
480532ed618SSoby Mathew	ret
481532ed618SSoby Mathewendfunc fpregs_context_save
482532ed618SSoby Mathew
483ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
484ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use x9-x17
485ed108b56SAlexei Fedorov * (temporary caller-saved registers according to AArch64 PCS) to
486ed108b56SAlexei Fedorov * restore floating point register context. It assumes that 'x0' is
487ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure from where the register context
488532ed618SSoby Mathew * will be restored.
489532ed618SSoby Mathew *
490ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set.
491ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in
492ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared.
493532ed618SSoby Mathew *
494532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
495ed108b56SAlexei Fedorov * ------------------------------------------------------------------
496532ed618SSoby Mathew */
497532ed618SSoby Mathewfunc fpregs_context_restore
498532ed618SSoby Mathew	ldp	q0, q1, [x0, #CTX_FP_Q0]
499532ed618SSoby Mathew	ldp	q2, q3, [x0, #CTX_FP_Q2]
500532ed618SSoby Mathew	ldp	q4, q5, [x0, #CTX_FP_Q4]
501532ed618SSoby Mathew	ldp	q6, q7, [x0, #CTX_FP_Q6]
502532ed618SSoby Mathew	ldp	q8, q9, [x0, #CTX_FP_Q8]
503532ed618SSoby Mathew	ldp	q10, q11, [x0, #CTX_FP_Q10]
504532ed618SSoby Mathew	ldp	q12, q13, [x0, #CTX_FP_Q12]
505532ed618SSoby Mathew	ldp	q14, q15, [x0, #CTX_FP_Q14]
506532ed618SSoby Mathew	ldp	q16, q17, [x0, #CTX_FP_Q16]
507532ed618SSoby Mathew	ldp	q18, q19, [x0, #CTX_FP_Q18]
508532ed618SSoby Mathew	ldp	q20, q21, [x0, #CTX_FP_Q20]
509532ed618SSoby Mathew	ldp	q22, q23, [x0, #CTX_FP_Q22]
510532ed618SSoby Mathew	ldp	q24, q25, [x0, #CTX_FP_Q24]
511532ed618SSoby Mathew	ldp	q26, q27, [x0, #CTX_FP_Q26]
512532ed618SSoby Mathew	ldp	q28, q29, [x0, #CTX_FP_Q28]
513532ed618SSoby Mathew	ldp	q30, q31, [x0, #CTX_FP_Q30]
514532ed618SSoby Mathew
515532ed618SSoby Mathew	ldr	x9, [x0, #CTX_FP_FPSR]
516532ed618SSoby Mathew	msr	fpsr, x9
517532ed618SSoby Mathew
518532ed618SSoby Mathew	ldr	x10, [x0, #CTX_FP_FPCR]
519532ed618SSoby Mathew	msr	fpcr, x10
520532ed618SSoby Mathew
52191089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
52291089f36SDavid Cunado	ldr	x11, [x0, #CTX_FP_FPEXC32_EL2]
52391089f36SDavid Cunado	msr	fpexc32_el2, x11
5240ce220afSJayanth Dodderi Chidanand#endif /* CTX_INCLUDE_AARCH32_REGS */
5250ce220afSJayanth Dodderi Chidanand
526532ed618SSoby Mathew	/*
527532ed618SSoby Mathew	 * No explict ISB required here as ERET to
528532ed618SSoby Mathew	 * switch to secure EL1 or non-secure world
529532ed618SSoby Mathew	 * covers it
530532ed618SSoby Mathew	 */
531532ed618SSoby Mathew
532532ed618SSoby Mathew	ret
533532ed618SSoby Mathewendfunc fpregs_context_restore
534532ed618SSoby Mathew#endif /* CTX_INCLUDE_FPREGS */
535532ed618SSoby Mathew
5367d33ffe4SDaniel Boulby	/*
5371cbe42a5SManish Pandey	 * Set SCR_EL3.EA bit to enable SErrors at EL3
5381cbe42a5SManish Pandey	 */
5391cbe42a5SManish Pandey	.macro enable_serror_at_el3
5401cbe42a5SManish Pandey	mrs     x8, scr_el3
5411cbe42a5SManish Pandey	orr     x8, x8, #SCR_EA_BIT
5421cbe42a5SManish Pandey	msr     scr_el3, x8
5431cbe42a5SManish Pandey	.endm
5441cbe42a5SManish Pandey
5451cbe42a5SManish Pandey	/*
5467d33ffe4SDaniel Boulby	 * Set the PSTATE bits not set when the exception was taken as
5477d33ffe4SDaniel Boulby	 * described in the AArch64.TakeException() pseudocode function
5487d33ffe4SDaniel Boulby	 * in ARM DDI 0487F.c page J1-7635 to a default value.
5497d33ffe4SDaniel Boulby	 */
5507d33ffe4SDaniel Boulby	.macro set_unset_pstate_bits
5517d33ffe4SDaniel Boulby	/*
5527d33ffe4SDaniel Boulby	 * If Data Independent Timing (DIT) functionality is implemented,
5537d33ffe4SDaniel Boulby	 * always enable DIT in EL3
5547d33ffe4SDaniel Boulby	 */
5557d33ffe4SDaniel Boulby#if ENABLE_FEAT_DIT
55688727fc3SAndre Przywara#if ENABLE_FEAT_DIT == 2
55788727fc3SAndre Przywara	mrs	x8, id_aa64pfr0_el1
55888727fc3SAndre Przywara	and	x8, x8, #(ID_AA64PFR0_DIT_MASK << ID_AA64PFR0_DIT_SHIFT)
55988727fc3SAndre Przywara	cbz	x8, 1f
56088727fc3SAndre Przywara#endif
5617d33ffe4SDaniel Boulby	mov     x8, #DIT_BIT
5627d33ffe4SDaniel Boulby	msr     DIT, x8
56388727fc3SAndre Przywara1:
5647d33ffe4SDaniel Boulby#endif /* ENABLE_FEAT_DIT */
5657d33ffe4SDaniel Boulby	.endm /* set_unset_pstate_bits */
5667d33ffe4SDaniel Boulby
567ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
56897215e0fSDaniel Boulby * The following macro is used to save and restore all the general
569ed108b56SAlexei Fedorov * purpose and ARMv8.3-PAuth (if enabled) registers.
570d64bfef5SJayanth Dodderi Chidanand * It also checks if the Secure Cycle Counter (PMCCNTR_EL0)
571d64bfef5SJayanth Dodderi Chidanand * is disabled in EL3/Secure (ARMv8.5-PMU), wherein PMCCNTR_EL0
572d64bfef5SJayanth Dodderi Chidanand * needs not to be saved/restored during world switch.
573ed108b56SAlexei Fedorov *
574ed108b56SAlexei Fedorov * Ideally we would only save and restore the callee saved registers
575ed108b56SAlexei Fedorov * when a world switch occurs but that type of implementation is more
576ed108b56SAlexei Fedorov * complex. So currently we will always save and restore these
577ed108b56SAlexei Fedorov * registers on entry and exit of EL3.
578532ed618SSoby Mathew * clobbers: x18
579ed108b56SAlexei Fedorov * ------------------------------------------------------------------
580532ed618SSoby Mathew */
58197215e0fSDaniel Boulby	.macro save_gp_pmcr_pauth_regs
582532ed618SSoby Mathew	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
583532ed618SSoby Mathew	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
584532ed618SSoby Mathew	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
585532ed618SSoby Mathew	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
586532ed618SSoby Mathew	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
587532ed618SSoby Mathew	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
588532ed618SSoby Mathew	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
589532ed618SSoby Mathew	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
590532ed618SSoby Mathew	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
591532ed618SSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
592532ed618SSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
593532ed618SSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
594532ed618SSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
595532ed618SSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
596532ed618SSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
597532ed618SSoby Mathew	mrs	x18, sp_el0
598532ed618SSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
599ed108b56SAlexei Fedorov	mrs	x9, pmcr_el0
600ed108b56SAlexei Fedorov	str	x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
601ed108b56SAlexei Fedorov	/* Disable cycle counter when event counting is prohibited */
602*1d6d6802SBoyan Karatotev	orr	x9, x9, #PMCR_EL0_DP_BIT
603ed108b56SAlexei Fedorov	msr	pmcr_el0, x9
604ed108b56SAlexei Fedorov	isb
605ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS
606ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
607ed108b56SAlexei Fedorov 	 * Save the ARMv8.3-PAuth keys as they are not banked
608ed108b56SAlexei Fedorov 	 * by exception level
609ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
610ed108b56SAlexei Fedorov	 */
611ed108b56SAlexei Fedorov	add	x19, sp, #CTX_PAUTH_REGS_OFFSET
612ed108b56SAlexei Fedorov
613ed108b56SAlexei Fedorov	mrs	x20, APIAKeyLo_EL1	/* x21:x20 = APIAKey */
614ed108b56SAlexei Fedorov	mrs	x21, APIAKeyHi_EL1
615ed108b56SAlexei Fedorov	mrs	x22, APIBKeyLo_EL1	/* x23:x22 = APIBKey */
616ed108b56SAlexei Fedorov	mrs	x23, APIBKeyHi_EL1
617ed108b56SAlexei Fedorov	mrs	x24, APDAKeyLo_EL1	/* x25:x24 = APDAKey */
618ed108b56SAlexei Fedorov	mrs	x25, APDAKeyHi_EL1
619ed108b56SAlexei Fedorov	mrs	x26, APDBKeyLo_EL1	/* x27:x26 = APDBKey */
620ed108b56SAlexei Fedorov	mrs	x27, APDBKeyHi_EL1
621ed108b56SAlexei Fedorov	mrs	x28, APGAKeyLo_EL1	/* x29:x28 = APGAKey */
622ed108b56SAlexei Fedorov	mrs	x29, APGAKeyHi_EL1
623ed108b56SAlexei Fedorov
624ed108b56SAlexei Fedorov	stp	x20, x21, [x19, #CTX_PACIAKEY_LO]
625ed108b56SAlexei Fedorov	stp	x22, x23, [x19, #CTX_PACIBKEY_LO]
626ed108b56SAlexei Fedorov	stp	x24, x25, [x19, #CTX_PACDAKEY_LO]
627ed108b56SAlexei Fedorov	stp	x26, x27, [x19, #CTX_PACDBKEY_LO]
628ed108b56SAlexei Fedorov	stp	x28, x29, [x19, #CTX_PACGAKEY_LO]
629ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */
63097215e0fSDaniel Boulby	.endm /* save_gp_pmcr_pauth_regs */
63197215e0fSDaniel Boulby
63297215e0fSDaniel Boulby/* -----------------------------------------------------------------
6337d33ffe4SDaniel Boulby * This function saves the context and sets the PSTATE to a known
6347d33ffe4SDaniel Boulby * state, preparing entry to el3.
63597215e0fSDaniel Boulby * Save all the general purpose and ARMv8.3-PAuth (if enabled)
63697215e0fSDaniel Boulby * registers.
6377d33ffe4SDaniel Boulby * Then set any of the PSTATE bits that are not set by hardware
6387d33ffe4SDaniel Boulby * according to the Aarch64.TakeException pseudocode in the Arm
6397d33ffe4SDaniel Boulby * Architecture Reference Manual to a default value for EL3.
6407d33ffe4SDaniel Boulby * clobbers: x17
64197215e0fSDaniel Boulby * -----------------------------------------------------------------
64297215e0fSDaniel Boulby */
64397215e0fSDaniel Boulbyfunc prepare_el3_entry
64497215e0fSDaniel Boulby	save_gp_pmcr_pauth_regs
6451cbe42a5SManish Pandey	enable_serror_at_el3
6467d33ffe4SDaniel Boulby	/*
6477d33ffe4SDaniel Boulby	 * Set the PSTATE bits not described in the Aarch64.TakeException
6487d33ffe4SDaniel Boulby	 * pseudocode to their default values.
6497d33ffe4SDaniel Boulby	 */
6507d33ffe4SDaniel Boulby	set_unset_pstate_bits
651ed108b56SAlexei Fedorov	ret
65297215e0fSDaniel Boulbyendfunc prepare_el3_entry
653ed108b56SAlexei Fedorov
654ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
655ed108b56SAlexei Fedorov * This function restores ARMv8.3-PAuth (if enabled) and all general
656ed108b56SAlexei Fedorov * purpose registers except x30 from the CPU context.
657ed108b56SAlexei Fedorov * x30 register must be explicitly restored by the caller.
658ed108b56SAlexei Fedorov * ------------------------------------------------------------------
659ed108b56SAlexei Fedorov */
660ed108b56SAlexei Fedorovfunc restore_gp_pmcr_pauth_regs
661ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS
662ed108b56SAlexei Fedorov 	/* Restore the ARMv8.3 PAuth keys */
663ed108b56SAlexei Fedorov	add	x10, sp, #CTX_PAUTH_REGS_OFFSET
664ed108b56SAlexei Fedorov
665ed108b56SAlexei Fedorov	ldp	x0, x1, [x10, #CTX_PACIAKEY_LO]	/* x1:x0 = APIAKey */
666ed108b56SAlexei Fedorov	ldp	x2, x3, [x10, #CTX_PACIBKEY_LO]	/* x3:x2 = APIBKey */
667ed108b56SAlexei Fedorov	ldp	x4, x5, [x10, #CTX_PACDAKEY_LO]	/* x5:x4 = APDAKey */
668ed108b56SAlexei Fedorov	ldp	x6, x7, [x10, #CTX_PACDBKEY_LO]	/* x7:x6 = APDBKey */
669ed108b56SAlexei Fedorov	ldp	x8, x9, [x10, #CTX_PACGAKEY_LO]	/* x9:x8 = APGAKey */
670ed108b56SAlexei Fedorov
671ed108b56SAlexei Fedorov	msr	APIAKeyLo_EL1, x0
672ed108b56SAlexei Fedorov	msr	APIAKeyHi_EL1, x1
673ed108b56SAlexei Fedorov	msr	APIBKeyLo_EL1, x2
674ed108b56SAlexei Fedorov	msr	APIBKeyHi_EL1, x3
675ed108b56SAlexei Fedorov	msr	APDAKeyLo_EL1, x4
676ed108b56SAlexei Fedorov	msr	APDAKeyHi_EL1, x5
677ed108b56SAlexei Fedorov	msr	APDBKeyLo_EL1, x6
678ed108b56SAlexei Fedorov	msr	APDBKeyHi_EL1, x7
679ed108b56SAlexei Fedorov	msr	APGAKeyLo_EL1, x8
680ed108b56SAlexei Fedorov	msr	APGAKeyHi_EL1, x9
681ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */
682ed108b56SAlexei Fedorov	ldr	x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
683ed108b56SAlexei Fedorov	msr	pmcr_el0, x0
684532ed618SSoby Mathew	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
685532ed618SSoby Mathew	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
686532ed618SSoby Mathew	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
687532ed618SSoby Mathew	ldp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
688532ed618SSoby Mathew	ldp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
689532ed618SSoby Mathew	ldp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
690532ed618SSoby Mathew	ldp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
691532ed618SSoby Mathew	ldp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
692ef653d93SJeenu Viswambharan	ldp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
693532ed618SSoby Mathew	ldp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
694532ed618SSoby Mathew	ldp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
695532ed618SSoby Mathew	ldp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
696532ed618SSoby Mathew	ldp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
697532ed618SSoby Mathew	ldp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
698ef653d93SJeenu Viswambharan	ldr	x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
699ef653d93SJeenu Viswambharan	msr	sp_el0, x28
700532ed618SSoby Mathew	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
701ef653d93SJeenu Viswambharan	ret
702ed108b56SAlexei Fedorovendfunc restore_gp_pmcr_pauth_regs
703ef653d93SJeenu Viswambharan
7043b8456bdSManish V Badarkhe/*
7053b8456bdSManish V Badarkhe * In case of ERRATA_SPECULATIVE_AT, save SCTLR_EL1 and TCR_EL1
7063b8456bdSManish V Badarkhe * registers and update EL1 registers to disable stage1 and stage2
7073b8456bdSManish V Badarkhe * page table walk
7083b8456bdSManish V Badarkhe */
7093b8456bdSManish V Badarkhefunc save_and_update_ptw_el1_sys_regs
7103b8456bdSManish V Badarkhe	/* ----------------------------------------------------------
7113b8456bdSManish V Badarkhe	 * Save only sctlr_el1 and tcr_el1 registers
7123b8456bdSManish V Badarkhe	 * ----------------------------------------------------------
7133b8456bdSManish V Badarkhe	 */
7143b8456bdSManish V Badarkhe	mrs	x29, sctlr_el1
7153b8456bdSManish V Badarkhe	str	x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_SCTLR_EL1)]
7163b8456bdSManish V Badarkhe	mrs	x29, tcr_el1
7173b8456bdSManish V Badarkhe	str	x29, [sp, #(CTX_EL1_SYSREGS_OFFSET + CTX_TCR_EL1)]
7183b8456bdSManish V Badarkhe
7193b8456bdSManish V Badarkhe	/* ------------------------------------------------------------
7203b8456bdSManish V Badarkhe	 * Must follow below order in order to disable page table
7213b8456bdSManish V Badarkhe	 * walk for lower ELs (EL1 and EL0). First step ensures that
7223b8456bdSManish V Badarkhe	 * page table walk is disabled for stage1 and second step
7233b8456bdSManish V Badarkhe	 * ensures that page table walker should use TCR_EL1.EPDx
7243b8456bdSManish V Badarkhe	 * bits to perform address translation. ISB ensures that CPU
7253b8456bdSManish V Badarkhe	 * does these 2 steps in order.
7263b8456bdSManish V Badarkhe	 *
7273b8456bdSManish V Badarkhe	 * 1. Update TCR_EL1.EPDx bits to disable page table walk by
7283b8456bdSManish V Badarkhe	 *    stage1.
7293b8456bdSManish V Badarkhe	 * 2. Enable MMU bit to avoid identity mapping via stage2
7303b8456bdSManish V Badarkhe	 *    and force TCR_EL1.EPDx to be used by the page table
7313b8456bdSManish V Badarkhe	 *    walker.
7323b8456bdSManish V Badarkhe	 * ------------------------------------------------------------
7333b8456bdSManish V Badarkhe	 */
7343b8456bdSManish V Badarkhe	orr	x29, x29, #(TCR_EPD0_BIT)
7353b8456bdSManish V Badarkhe	orr	x29, x29, #(TCR_EPD1_BIT)
7363b8456bdSManish V Badarkhe	msr	tcr_el1, x29
7373b8456bdSManish V Badarkhe	isb
7383b8456bdSManish V Badarkhe	mrs	x29, sctlr_el1
7393b8456bdSManish V Badarkhe	orr	x29, x29, #SCTLR_M_BIT
7403b8456bdSManish V Badarkhe	msr	sctlr_el1, x29
7413b8456bdSManish V Badarkhe	isb
7423b8456bdSManish V Badarkhe
7433b8456bdSManish V Badarkhe	ret
7443b8456bdSManish V Badarkheendfunc save_and_update_ptw_el1_sys_regs
7453b8456bdSManish V Badarkhe
746ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
747ed108b56SAlexei Fedorov * This routine assumes that the SP_EL3 is pointing to a valid
748ed108b56SAlexei Fedorov * context structure from where the gp regs and other special
749ed108b56SAlexei Fedorov * registers can be retrieved.
750ed108b56SAlexei Fedorov * ------------------------------------------------------------------
751532ed618SSoby Mathew */
752532ed618SSoby Mathewfunc el3_exit
753bb9549baSJan Dabros#if ENABLE_ASSERTIONS
754bb9549baSJan Dabros	/* el3_exit assumes SP_EL0 on entry */
755bb9549baSJan Dabros	mrs	x17, spsel
756bb9549baSJan Dabros	cmp	x17, #MODE_SP_EL0
757bb9549baSJan Dabros	ASM_ASSERT(eq)
7580ce220afSJayanth Dodderi Chidanand#endif /* ENABLE_ASSERTIONS */
759bb9549baSJan Dabros
760ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
761ed108b56SAlexei Fedorov	 * Save the current SP_EL0 i.e. the EL3 runtime stack which
762ed108b56SAlexei Fedorov	 * will be used for handling the next SMC.
763ed108b56SAlexei Fedorov	 * Then switch to SP_EL3.
764ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
765532ed618SSoby Mathew	 */
766532ed618SSoby Mathew	mov	x17, sp
767ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
768532ed618SSoby Mathew	str	x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
769532ed618SSoby Mathew
7700c5e7d1cSMax Shvetsov#if IMAGE_BL31
7710c5e7d1cSMax Shvetsov	/* ----------------------------------------------------------
77268ac5ed0SArunachalam Ganapathy	 * Restore CPTR_EL3.
7730c5e7d1cSMax Shvetsov	 * ZCR is only restored if SVE is supported and enabled.
7740c5e7d1cSMax Shvetsov	 * Synchronization is required before zcr_el3 is addressed.
7750c5e7d1cSMax Shvetsov	 * ----------------------------------------------------------
7760c5e7d1cSMax Shvetsov	 */
7770c5e7d1cSMax Shvetsov	ldp	x19, x20, [sp, #CTX_EL3STATE_OFFSET + CTX_CPTR_EL3]
7780c5e7d1cSMax Shvetsov	msr	cptr_el3, x19
7790c5e7d1cSMax Shvetsov
7800c5e7d1cSMax Shvetsov	ands	x19, x19, #CPTR_EZ_BIT
7810c5e7d1cSMax Shvetsov	beq	sve_not_enabled
7820c5e7d1cSMax Shvetsov
7830c5e7d1cSMax Shvetsov	isb
7840c5e7d1cSMax Shvetsov	msr	S3_6_C1_C2_0, x20 /* zcr_el3 */
7850c5e7d1cSMax Shvetsovsve_not_enabled:
7860ce220afSJayanth Dodderi Chidanand#endif /* IMAGE_BL31 */
7870c5e7d1cSMax Shvetsov
788fe007b2eSDimitris Papastamos#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
789ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
790ed108b56SAlexei Fedorov	 * Restore mitigation state as it was on entry to EL3
791ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
792ed108b56SAlexei Fedorov	 */
793fe007b2eSDimitris Papastamos	ldr	x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
794ed108b56SAlexei Fedorov	cbz	x17, 1f
795fe007b2eSDimitris Papastamos	blr	x17
7964d1ccf0eSAntonio Nino Diaz1:
7970ce220afSJayanth Dodderi Chidanand#endif /* IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639 */
7980ce220afSJayanth Dodderi Chidanand
799ed108b56SAlexei Fedorov#if IMAGE_BL31 && RAS_EXTENSION
800ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
801ed108b56SAlexei Fedorov	 * Issue Error Synchronization Barrier to synchronize SErrors
802ed108b56SAlexei Fedorov	 * before exiting EL3. We're running with EAs unmasked, so
803ed108b56SAlexei Fedorov	 * any synchronized errors would be taken immediately;
804ed108b56SAlexei Fedorov	 * therefore no need to inspect DISR_EL1 register.
805ed108b56SAlexei Fedorov 	 * ----------------------------------------------------------
806ed108b56SAlexei Fedorov	 */
807ed108b56SAlexei Fedorov	esb
808c2d32a5fSMadhukar Pappireddy#else
809c2d32a5fSMadhukar Pappireddy	dsb	sy
8100ce220afSJayanth Dodderi Chidanand#endif /* IMAGE_BL31 && RAS_EXTENSION */
8110ce220afSJayanth Dodderi Chidanand
812ff1d2ef3SManish Pandey	/* ----------------------------------------------------------
813ff1d2ef3SManish Pandey	 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
814ff1d2ef3SManish Pandey	 * ----------------------------------------------------------
815ff1d2ef3SManish Pandey	 */
816ff1d2ef3SManish Pandey	ldr	x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
817ff1d2ef3SManish Pandey	ldp	x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
818ff1d2ef3SManish Pandey	msr	scr_el3, x18
819ff1d2ef3SManish Pandey	msr	spsr_el3, x16
820ff1d2ef3SManish Pandey	msr	elr_el3, x17
821ff1d2ef3SManish Pandey
822ff1d2ef3SManish Pandey	restore_ptw_el1_sys_regs
823ff1d2ef3SManish Pandey
824ff1d2ef3SManish Pandey	/* ----------------------------------------------------------
825ff1d2ef3SManish Pandey	 * Restore general purpose (including x30), PMCR_EL0 and
826ff1d2ef3SManish Pandey	 * ARMv8.3-PAuth registers.
827ff1d2ef3SManish Pandey	 * Exit EL3 via ERET to a lower exception level.
828ff1d2ef3SManish Pandey 	 * ----------------------------------------------------------
829ff1d2ef3SManish Pandey 	 */
830ff1d2ef3SManish Pandey	bl	restore_gp_pmcr_pauth_regs
831ff1d2ef3SManish Pandey	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
832ff1d2ef3SManish Pandey
833c2d32a5fSMadhukar Pappireddy#ifdef IMAGE_BL31
834c2d32a5fSMadhukar Pappireddy	str	xzr, [sp, #CTX_EL3STATE_OFFSET + CTX_IS_IN_EL3]
8350ce220afSJayanth Dodderi Chidanand#endif /* IMAGE_BL31 */
8360ce220afSJayanth Dodderi Chidanand
837f461fe34SAnthony Steinhauser	exception_return
8385283962eSAntonio Nino Diaz
839532ed618SSoby Mathewendfunc el3_exit
840