xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context.S (revision cb55615c506d6bff3f1a9223182e190abbbf6fc5)
1532ed618SSoby Mathew/*
228f39f02SMax Shvetsov * Copyright (c) 2013-2020, 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>
11532ed618SSoby Mathew
1228f39f02SMax Shvetsov#if CTX_INCLUDE_EL2_REGS
1328f39f02SMax Shvetsov	.global	el2_sysregs_context_save
1428f39f02SMax Shvetsov	.global	el2_sysregs_context_restore
1528f39f02SMax Shvetsov#endif
1628f39f02SMax Shvetsov
17532ed618SSoby Mathew	.global	el1_sysregs_context_save
18532ed618SSoby Mathew	.global	el1_sysregs_context_restore
19532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
20532ed618SSoby Mathew	.global	fpregs_context_save
21532ed618SSoby Mathew	.global	fpregs_context_restore
22532ed618SSoby Mathew#endif
23ed108b56SAlexei Fedorov	.global	save_gp_pmcr_pauth_regs
24ed108b56SAlexei Fedorov	.global	restore_gp_pmcr_pauth_regs
25532ed618SSoby Mathew	.global	el3_exit
26532ed618SSoby Mathew
2728f39f02SMax Shvetsov#if CTX_INCLUDE_EL2_REGS
2828f39f02SMax Shvetsov
2928f39f02SMax Shvetsov/* -----------------------------------------------------
3028f39f02SMax Shvetsov * The following function strictly follows the AArch64
3128f39f02SMax Shvetsov * PCS to use x9-x17 (temporary caller-saved registers)
322825946eSMax Shvetsov * to save EL2 system register context. It assumes that
332825946eSMax Shvetsov * 'x0' is pointing to a 'el2_sys_regs' structure where
3428f39f02SMax Shvetsov * the register context will be saved.
352825946eSMax Shvetsov *
362825946eSMax Shvetsov * The following registers are not added.
372825946eSMax Shvetsov * AMEVCNTVOFF0<n>_EL2
382825946eSMax Shvetsov * AMEVCNTVOFF1<n>_EL2
392825946eSMax Shvetsov * ICH_AP0R<n>_EL2
402825946eSMax Shvetsov * ICH_AP1R<n>_EL2
412825946eSMax Shvetsov * ICH_LR<n>_EL2
4228f39f02SMax Shvetsov * -----------------------------------------------------
4328f39f02SMax Shvetsov */
442825946eSMax Shvetsov
4528f39f02SMax Shvetsovfunc el2_sysregs_context_save
4628f39f02SMax Shvetsov	mrs	x9, actlr_el2
472825946eSMax Shvetsov	mrs	x10, afsr0_el2
482825946eSMax Shvetsov	stp	x9, x10, [x0, #CTX_ACTLR_EL2]
4928f39f02SMax Shvetsov
502825946eSMax Shvetsov	mrs	x11, afsr1_el2
512825946eSMax Shvetsov	mrs	x12, amair_el2
522825946eSMax Shvetsov	stp	x11, x12, [x0, #CTX_AFSR1_EL2]
5328f39f02SMax Shvetsov
542825946eSMax Shvetsov	mrs	x13, cnthctl_el2
552825946eSMax Shvetsov	mrs	x14, cnthp_ctl_el2
562825946eSMax Shvetsov	stp	x13, x14, [x0, #CTX_CNTHCTL_EL2]
5728f39f02SMax Shvetsov
582825946eSMax Shvetsov	mrs	x15, cnthp_cval_el2
592825946eSMax Shvetsov	mrs	x16, cnthp_tval_el2
602825946eSMax Shvetsov	stp	x15, x16, [x0, #CTX_CNTHP_CVAL_EL2]
6128f39f02SMax Shvetsov
622825946eSMax Shvetsov	mrs	x17, cntvoff_el2
6328f39f02SMax Shvetsov	mrs	x9, cptr_el2
642825946eSMax Shvetsov	stp	x17, x9, [x0, #CTX_CNTVOFF_EL2]
6528f39f02SMax Shvetsov
662825946eSMax Shvetsov	mrs	x10, dbgvcr32_el2
672825946eSMax Shvetsov	mrs	x11, elr_el2
682825946eSMax Shvetsov	stp	x10, x11, [x0, #CTX_DBGVCR32_EL2]
6928f39f02SMax Shvetsov
702825946eSMax Shvetsov	mrs	x14, esr_el2
712825946eSMax Shvetsov	mrs	x15, far_el2
722825946eSMax Shvetsov	stp	x14, x15, [x0, #CTX_ESR_EL2]
7328f39f02SMax Shvetsov
7430ee3755SMax Shvetsov	mrs	x16, hacr_el2
7530ee3755SMax Shvetsov	mrs	x17, hcr_el2
7630ee3755SMax Shvetsov	stp	x16, x17, [x0, #CTX_HACR_EL2]
7728f39f02SMax Shvetsov
7830ee3755SMax Shvetsov	mrs	x9, hpfar_el2
7930ee3755SMax Shvetsov	mrs	x10, hstr_el2
8030ee3755SMax Shvetsov	stp	x9, x10, [x0, #CTX_HPFAR_EL2]
8128f39f02SMax Shvetsov
8230ee3755SMax Shvetsov	mrs	x11, ICC_SRE_EL2
8330ee3755SMax Shvetsov	mrs	x12, ICH_HCR_EL2
8430ee3755SMax Shvetsov	stp	x11, x12, [x0, #CTX_ICC_SRE_EL2]
8528f39f02SMax Shvetsov
8630ee3755SMax Shvetsov	mrs	x13, ICH_VMCR_EL2
8730ee3755SMax Shvetsov	mrs	x14, mair_el2
8830ee3755SMax Shvetsov	stp	x13, x14, [x0, #CTX_ICH_VMCR_EL2]
8928f39f02SMax Shvetsov
9030ee3755SMax Shvetsov	mrs	x15, mdcr_el2
9130ee3755SMax Shvetsov	mrs	x16, PMSCR_EL2
9230ee3755SMax Shvetsov	stp	x15, x16, [x0, #CTX_MDCR_EL2]
9328f39f02SMax Shvetsov
9430ee3755SMax Shvetsov	mrs	x17, sctlr_el2
9530ee3755SMax Shvetsov	mrs	x9, spsr_el2
9630ee3755SMax Shvetsov	stp	x17, x9, [x0, #CTX_SCTLR_EL2]
9728f39f02SMax Shvetsov
9830ee3755SMax Shvetsov	mrs	x10, sp_el2
9930ee3755SMax Shvetsov	mrs	x11, tcr_el2
10030ee3755SMax Shvetsov	stp	x10, x11, [x0, #CTX_SP_EL2]
10128f39f02SMax Shvetsov
10230ee3755SMax Shvetsov	mrs	x12, tpidr_el2
10330ee3755SMax Shvetsov	mrs	x13, ttbr0_el2
10430ee3755SMax Shvetsov	stp	x12, x13, [x0, #CTX_TPIDR_EL2]
10528f39f02SMax Shvetsov
10630ee3755SMax Shvetsov	mrs	x14, vbar_el2
10730ee3755SMax Shvetsov	mrs	x15, vmpidr_el2
10830ee3755SMax Shvetsov	stp	x14, x15, [x0, #CTX_VBAR_EL2]
10928f39f02SMax Shvetsov
11030ee3755SMax Shvetsov	mrs	x16, vpidr_el2
11130ee3755SMax Shvetsov	mrs	x17, vtcr_el2
11230ee3755SMax Shvetsov	stp	x16, x17, [x0, #CTX_VPIDR_EL2]
11328f39f02SMax Shvetsov
11430ee3755SMax Shvetsov	mrs	x9, vttbr_el2
11530ee3755SMax Shvetsov	str	x9, [x0, #CTX_VTTBR_EL2]
11628f39f02SMax Shvetsov
1172825946eSMax Shvetsov#if CTX_INCLUDE_MTE_REGS
11830ee3755SMax Shvetsov	mrs	x10, TFSR_EL2
11930ee3755SMax Shvetsov	str	x10, [x0, #CTX_TFSR_EL2]
1202825946eSMax Shvetsov#endif
12128f39f02SMax Shvetsov
1222825946eSMax Shvetsov#if ENABLE_MPAM_FOR_LOWER_ELS
1232825946eSMax Shvetsov	mrs	x9, MPAM2_EL2
1242825946eSMax Shvetsov	mrs	x10, MPAMHCR_EL2
1252825946eSMax Shvetsov	stp	x9, x10, [x0, #CTX_MPAM2_EL2]
1262825946eSMax Shvetsov
1272825946eSMax Shvetsov	mrs	x11, MPAMVPM0_EL2
1282825946eSMax Shvetsov	mrs	x12, MPAMVPM1_EL2
1292825946eSMax Shvetsov	stp	x11, x12, [x0, #CTX_MPAMVPM0_EL2]
1302825946eSMax Shvetsov
1312825946eSMax Shvetsov	mrs	x13, MPAMVPM2_EL2
1322825946eSMax Shvetsov	mrs	x14, MPAMVPM3_EL2
1332825946eSMax Shvetsov	stp	x13, x14, [x0, #CTX_MPAMVPM2_EL2]
1342825946eSMax Shvetsov
1352825946eSMax Shvetsov	mrs	x15, MPAMVPM4_EL2
1362825946eSMax Shvetsov	mrs	x16, MPAMVPM5_EL2
1372825946eSMax Shvetsov	stp	x15, x16, [x0, #CTX_MPAMVPM4_EL2]
1382825946eSMax Shvetsov
1392825946eSMax Shvetsov	mrs	x17, MPAMVPM6_EL2
1402825946eSMax Shvetsov	mrs	x9, MPAMVPM7_EL2
1412825946eSMax Shvetsov	stp	x17, x9, [x0, #CTX_MPAMVPM6_EL2]
1422825946eSMax Shvetsov
1432825946eSMax Shvetsov	mrs	x10, MPAMVPMV_EL2
1442825946eSMax Shvetsov	str	x10, [x0, #CTX_MPAMVPMV_EL2]
1452825946eSMax Shvetsov#endif
1462825946eSMax Shvetsov
1472825946eSMax Shvetsov
1482825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 6)
1492825946eSMax Shvetsov	mrs	x11, HAFGRTR_EL2
1502825946eSMax Shvetsov	mrs	x12, HDFGRTR_EL2
1512825946eSMax Shvetsov	stp	x11, x12, [x0, #CTX_HAFGRTR_EL2]
1522825946eSMax Shvetsov
1532825946eSMax Shvetsov	mrs	x13, HDFGWTR_EL2
1542825946eSMax Shvetsov	mrs	x14, HFGITR_EL2
1552825946eSMax Shvetsov	stp	x13, x14, [x0, #CTX_HDFGWTR_EL2]
1562825946eSMax Shvetsov
1572825946eSMax Shvetsov	mrs	x15, HFGRTR_EL2
1582825946eSMax Shvetsov	mrs	x16, HFGWTR_EL2
1592825946eSMax Shvetsov	stp	x15, x16, [x0, #CTX_HFGRTR_EL2]
1602825946eSMax Shvetsov
1612825946eSMax Shvetsov	mrs	x17, CNTPOFF_EL2
1622825946eSMax Shvetsov	str	x17, [x0, #CTX_CNTPOFF_EL2]
1632825946eSMax Shvetsov#endif
1642825946eSMax Shvetsov
1652825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 4)
1662825946eSMax Shvetsov	mrs	x9, cnthps_ctl_el2
1672825946eSMax Shvetsov	mrs	x10, cnthps_cval_el2
1682825946eSMax Shvetsov	stp	x9, x10, [x0, #CTX_CNTHPS_CTL_EL2]
1692825946eSMax Shvetsov
1702825946eSMax Shvetsov	mrs	x11, cnthps_tval_el2
1712825946eSMax Shvetsov	mrs	x12, cnthvs_ctl_el2
1722825946eSMax Shvetsov	stp	x11, x12, [x0, #CTX_CNTHPS_TVAL_EL2]
1732825946eSMax Shvetsov
1742825946eSMax Shvetsov	mrs	x13, cnthvs_cval_el2
1752825946eSMax Shvetsov	mrs	x14, cnthvs_tval_el2
1762825946eSMax Shvetsov	stp	x13, x14, [x0, #CTX_CNTHVS_CVAL_EL2]
1772825946eSMax Shvetsov
1782825946eSMax Shvetsov	mrs	x15, cnthv_ctl_el2
1792825946eSMax Shvetsov	mrs	x16, cnthv_cval_el2
1802825946eSMax Shvetsov	stp	x15, x16, [x0, #CTX_CNTHV_CTL_EL2]
1812825946eSMax Shvetsov
1822825946eSMax Shvetsov	mrs	x17, cnthv_tval_el2
1832825946eSMax Shvetsov	mrs	x9, contextidr_el2
1842825946eSMax Shvetsov	stp	x17, x9, [x0, #CTX_CNTHV_TVAL_EL2]
1852825946eSMax Shvetsov
1862825946eSMax Shvetsov	mrs	x10, sder32_el2
1872825946eSMax Shvetsov	str	x10, [x0, #CTX_SDER32_EL2]
1882825946eSMax Shvetsov
1892825946eSMax Shvetsov	mrs	x11, ttbr1_el2
1902825946eSMax Shvetsov	str	x11, [x0, #CTX_TTBR1_EL2]
1912825946eSMax Shvetsov
1922825946eSMax Shvetsov	mrs	x12, vdisr_el2
1932825946eSMax Shvetsov	str	x12, [x0, #CTX_VDISR_EL2]
1942825946eSMax Shvetsov
1952825946eSMax Shvetsov	mrs	x13, vncr_el2
1962825946eSMax Shvetsov	str	x13, [x0, #CTX_VNCR_EL2]
1972825946eSMax Shvetsov
1982825946eSMax Shvetsov	mrs	x14, vsesr_el2
1992825946eSMax Shvetsov	str	x14, [x0, #CTX_VSESR_EL2]
2002825946eSMax Shvetsov
2012825946eSMax Shvetsov	mrs	x15, vstcr_el2
2022825946eSMax Shvetsov	str	x15, [x0, #CTX_VSTCR_EL2]
2032825946eSMax Shvetsov
2042825946eSMax Shvetsov	mrs	x16, vsttbr_el2
2052825946eSMax Shvetsov	str	x16, [x0, #CTX_VSTTBR_EL2]
2067f164a83SOlivier Deprez
2077f164a83SOlivier Deprez	mrs	x17, TRFCR_EL2
2087f164a83SOlivier Deprez	str	x17, [x0, #CTX_TRFCR_EL2]
2092825946eSMax Shvetsov#endif
2102825946eSMax Shvetsov
2112825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 5)
2127f164a83SOlivier Deprez	mrs	x9, scxtnum_el2
2137f164a83SOlivier Deprez	str	x9, [x0, #CTX_SCXTNUM_EL2]
2142825946eSMax Shvetsov#endif
21528f39f02SMax Shvetsov
21628f39f02SMax Shvetsov	ret
21728f39f02SMax Shvetsovendfunc el2_sysregs_context_save
21828f39f02SMax Shvetsov
21928f39f02SMax Shvetsov/* -----------------------------------------------------
22028f39f02SMax Shvetsov * The following function strictly follows the AArch64
22128f39f02SMax Shvetsov * PCS to use x9-x17 (temporary caller-saved registers)
2222825946eSMax Shvetsov * to restore EL2 system register context.  It assumes
2232825946eSMax Shvetsov * that 'x0' is pointing to a 'el2_sys_regs' structure
22428f39f02SMax Shvetsov * from where the register context will be restored
2252825946eSMax Shvetsov
2262825946eSMax Shvetsov * The following registers are not restored
2272825946eSMax Shvetsov * AMEVCNTVOFF0<n>_EL2
2282825946eSMax Shvetsov * AMEVCNTVOFF1<n>_EL2
2292825946eSMax Shvetsov * ICH_AP0R<n>_EL2
2302825946eSMax Shvetsov * ICH_AP1R<n>_EL2
2312825946eSMax Shvetsov * ICH_LR<n>_EL2
23228f39f02SMax Shvetsov * -----------------------------------------------------
23328f39f02SMax Shvetsov */
23428f39f02SMax Shvetsovfunc el2_sysregs_context_restore
23528f39f02SMax Shvetsov
2362825946eSMax Shvetsov	ldp	x9, x10, [x0, #CTX_ACTLR_EL2]
23728f39f02SMax Shvetsov	msr	actlr_el2, x9
2382825946eSMax Shvetsov	msr	afsr0_el2, x10
23928f39f02SMax Shvetsov
2402825946eSMax Shvetsov	ldp	x11, x12, [x0, #CTX_AFSR1_EL2]
2412825946eSMax Shvetsov	msr	afsr1_el2, x11
2422825946eSMax Shvetsov	msr	amair_el2, x12
24328f39f02SMax Shvetsov
2442825946eSMax Shvetsov	ldp	x13, x14, [x0, #CTX_CNTHCTL_EL2]
2452825946eSMax Shvetsov	msr	cnthctl_el2, x13
2462825946eSMax Shvetsov	msr	cnthp_ctl_el2, x14
24728f39f02SMax Shvetsov
2482825946eSMax Shvetsov	ldp	x15, x16, [x0, #CTX_CNTHP_CVAL_EL2]
2492825946eSMax Shvetsov	msr	cnthp_cval_el2, x15
2502825946eSMax Shvetsov	msr	cnthp_tval_el2, x16
25128f39f02SMax Shvetsov
2522825946eSMax Shvetsov	ldp	x17, x9, [x0, #CTX_CNTVOFF_EL2]
2532825946eSMax Shvetsov	msr	cntvoff_el2, x17
25428f39f02SMax Shvetsov	msr	cptr_el2, x9
25528f39f02SMax Shvetsov
2562825946eSMax Shvetsov	ldp	x10, x11, [x0, #CTX_DBGVCR32_EL2]
2572825946eSMax Shvetsov	msr	dbgvcr32_el2, x10
2582825946eSMax Shvetsov	msr	elr_el2, x11
25928f39f02SMax Shvetsov
2602825946eSMax Shvetsov	ldp	x14, x15, [x0, #CTX_ESR_EL2]
2612825946eSMax Shvetsov	msr	esr_el2, x14
2622825946eSMax Shvetsov	msr	far_el2, x15
26328f39f02SMax Shvetsov
26430ee3755SMax Shvetsov	ldp	x16, x17, [x0, #CTX_HACR_EL2]
26530ee3755SMax Shvetsov	msr	hacr_el2, x16
26630ee3755SMax Shvetsov	msr	hcr_el2, x17
26728f39f02SMax Shvetsov
26830ee3755SMax Shvetsov	ldp	x9, x10, [x0, #CTX_HPFAR_EL2]
26930ee3755SMax Shvetsov	msr	hpfar_el2, x9
27030ee3755SMax Shvetsov	msr	hstr_el2, x10
27128f39f02SMax Shvetsov
27230ee3755SMax Shvetsov	ldp	x11, x12, [x0, #CTX_ICC_SRE_EL2]
27330ee3755SMax Shvetsov	msr	ICC_SRE_EL2, x11
27430ee3755SMax Shvetsov	msr	ICH_HCR_EL2, x12
27528f39f02SMax Shvetsov
27630ee3755SMax Shvetsov	ldp	x13, x14, [x0, #CTX_ICH_VMCR_EL2]
27730ee3755SMax Shvetsov	msr	ICH_VMCR_EL2, x13
27830ee3755SMax Shvetsov	msr	mair_el2, x14
27928f39f02SMax Shvetsov
28030ee3755SMax Shvetsov	ldp	x15, x16, [x0, #CTX_MDCR_EL2]
28130ee3755SMax Shvetsov	msr	mdcr_el2, x15
28230ee3755SMax Shvetsov	msr	PMSCR_EL2, x16
28328f39f02SMax Shvetsov
284fb2072b0SManish V Badarkhe	ldp	x17, x9, [x0, #CTX_SCTLR_EL2]
285fb2072b0SManish V Badarkhe	msr	sctlr_el2, x17
286fb2072b0SManish V Badarkhe	msr	spsr_el2, x9
28728f39f02SMax Shvetsov
288fb2072b0SManish V Badarkhe	ldp	x10, x11, [x0, #CTX_SP_EL2]
289fb2072b0SManish V Badarkhe	msr	sp_el2, x10
290fb2072b0SManish V Badarkhe	msr	tcr_el2, x11
29128f39f02SMax Shvetsov
292fb2072b0SManish V Badarkhe	ldp	x12, x13, [x0, #CTX_TPIDR_EL2]
293fb2072b0SManish V Badarkhe	msr	tpidr_el2, x12
294fb2072b0SManish V Badarkhe	msr	ttbr0_el2, x13
29528f39f02SMax Shvetsov
296fb2072b0SManish V Badarkhe	ldp	x13, x14, [x0, #CTX_VBAR_EL2]
297fb2072b0SManish V Badarkhe	msr	vbar_el2, x13
298fb2072b0SManish V Badarkhe	msr	vmpidr_el2, x14
29928f39f02SMax Shvetsov
300fb2072b0SManish V Badarkhe	ldp	x15, x16, [x0, #CTX_VPIDR_EL2]
301fb2072b0SManish V Badarkhe	msr	vpidr_el2, x15
302fb2072b0SManish V Badarkhe	msr	vtcr_el2, x16
303fb2072b0SManish V Badarkhe
304fb2072b0SManish V Badarkhe	ldr	x17, [x0, #CTX_VTTBR_EL2]
305fb2072b0SManish V Badarkhe	msr	vttbr_el2, x17
30628f39f02SMax Shvetsov
3072825946eSMax Shvetsov#if CTX_INCLUDE_MTE_REGS
308fb2072b0SManish V Badarkhe	ldr	x9, [x0, #CTX_TFSR_EL2]
309fb2072b0SManish V Badarkhe	msr	TFSR_EL2, x9
3102825946eSMax Shvetsov#endif
31128f39f02SMax Shvetsov
3122825946eSMax Shvetsov#if ENABLE_MPAM_FOR_LOWER_ELS
313fb2072b0SManish V Badarkhe	ldp	x10, x11, [x0, #CTX_MPAM2_EL2]
314fb2072b0SManish V Badarkhe	msr	MPAM2_EL2, x10
315fb2072b0SManish V Badarkhe	msr	MPAMHCR_EL2, x11
3162825946eSMax Shvetsov
317fb2072b0SManish V Badarkhe	ldp	x12, x13, [x0, #CTX_MPAMVPM0_EL2]
318fb2072b0SManish V Badarkhe	msr	MPAMVPM0_EL2, x12
319fb2072b0SManish V Badarkhe	msr	MPAMVPM1_EL2, x13
3202825946eSMax Shvetsov
321fb2072b0SManish V Badarkhe	ldp	x14, x15, [x0, #CTX_MPAMVPM2_EL2]
322fb2072b0SManish V Badarkhe	msr	MPAMVPM2_EL2, x14
323fb2072b0SManish V Badarkhe	msr	MPAMVPM3_EL2, x15
3242825946eSMax Shvetsov
325fb2072b0SManish V Badarkhe	ldp	x16, x17, [x0, #CTX_MPAMVPM4_EL2]
326fb2072b0SManish V Badarkhe	msr	MPAMVPM4_EL2, x16
327fb2072b0SManish V Badarkhe	msr	MPAMVPM5_EL2, x17
3282825946eSMax Shvetsov
329fb2072b0SManish V Badarkhe	ldp	x9, x10, [x0, #CTX_MPAMVPM6_EL2]
330fb2072b0SManish V Badarkhe	msr	MPAMVPM6_EL2, x9
331fb2072b0SManish V Badarkhe	msr	MPAMVPM7_EL2, x10
3322825946eSMax Shvetsov
333fb2072b0SManish V Badarkhe	ldr	x11, [x0, #CTX_MPAMVPMV_EL2]
334fb2072b0SManish V Badarkhe	msr	MPAMVPMV_EL2, x11
3352825946eSMax Shvetsov#endif
3362825946eSMax Shvetsov
3372825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 6)
338fb2072b0SManish V Badarkhe	ldp	x12, x13, [x0, #CTX_HAFGRTR_EL2]
339fb2072b0SManish V Badarkhe	msr	HAFGRTR_EL2, x12
340fb2072b0SManish V Badarkhe	msr	HDFGRTR_EL2, x13
3412825946eSMax Shvetsov
342fb2072b0SManish V Badarkhe	ldp	x14, x15, [x0, #CTX_HDFGWTR_EL2]
343fb2072b0SManish V Badarkhe	msr	HDFGWTR_EL2, x14
344fb2072b0SManish V Badarkhe	msr	HFGITR_EL2, x15
3452825946eSMax Shvetsov
346fb2072b0SManish V Badarkhe	ldp	x16, x17, [x0, #CTX_HFGRTR_EL2]
347fb2072b0SManish V Badarkhe	msr	HFGRTR_EL2, x16
348fb2072b0SManish V Badarkhe	msr	HFGWTR_EL2, x17
3492825946eSMax Shvetsov
350fb2072b0SManish V Badarkhe	ldr	x9, [x0, #CTX_CNTPOFF_EL2]
351fb2072b0SManish V Badarkhe	msr	CNTPOFF_EL2, x9
3522825946eSMax Shvetsov#endif
3532825946eSMax Shvetsov
3542825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 4)
355fb2072b0SManish V Badarkhe	ldp	x10, x11, [x0, #CTX_CNTHPS_CTL_EL2]
356fb2072b0SManish V Badarkhe	msr	cnthps_ctl_el2, x10
357fb2072b0SManish V Badarkhe	msr	cnthps_cval_el2, x11
3582825946eSMax Shvetsov
359fb2072b0SManish V Badarkhe	ldp	x12, x13, [x0, #CTX_CNTHPS_TVAL_EL2]
360fb2072b0SManish V Badarkhe	msr	cnthps_tval_el2, x12
361fb2072b0SManish V Badarkhe	msr	cnthvs_ctl_el2, x13
3622825946eSMax Shvetsov
363fb2072b0SManish V Badarkhe	ldp	x14, x15, [x0, #CTX_CNTHVS_CVAL_EL2]
364fb2072b0SManish V Badarkhe	msr	cnthvs_cval_el2, x14
365fb2072b0SManish V Badarkhe	msr	cnthvs_tval_el2, x15
3662825946eSMax Shvetsov
367fb2072b0SManish V Badarkhe	ldp	x16, x17, [x0, #CTX_CNTHV_CTL_EL2]
368fb2072b0SManish V Badarkhe	msr	cnthv_ctl_el2, x16
369fb2072b0SManish V Badarkhe	msr	cnthv_cval_el2, x17
3702825946eSMax Shvetsov
371fb2072b0SManish V Badarkhe	ldp	x9, x10, [x0, #CTX_CNTHV_TVAL_EL2]
372fb2072b0SManish V Badarkhe	msr	cnthv_tval_el2, x9
373fb2072b0SManish V Badarkhe	msr	contextidr_el2, x10
3742825946eSMax Shvetsov
375fb2072b0SManish V Badarkhe	ldr	x11, [x0, #CTX_SDER32_EL2]
376fb2072b0SManish V Badarkhe	msr	sder32_el2, x11
3772825946eSMax Shvetsov
378fb2072b0SManish V Badarkhe	ldr	x12, [x0, #CTX_TTBR1_EL2]
379fb2072b0SManish V Badarkhe	msr	ttbr1_el2, x12
3802825946eSMax Shvetsov
381fb2072b0SManish V Badarkhe	ldr	x13, [x0, #CTX_VDISR_EL2]
382fb2072b0SManish V Badarkhe	msr	vdisr_el2, x13
3832825946eSMax Shvetsov
384fb2072b0SManish V Badarkhe	ldr	x14, [x0, #CTX_VNCR_EL2]
385fb2072b0SManish V Badarkhe	msr	vncr_el2, x14
3862825946eSMax Shvetsov
387fb2072b0SManish V Badarkhe	ldr	x15, [x0, #CTX_VSESR_EL2]
388fb2072b0SManish V Badarkhe	msr	vsesr_el2, x15
3892825946eSMax Shvetsov
390fb2072b0SManish V Badarkhe	ldr	x16, [x0, #CTX_VSTCR_EL2]
391fb2072b0SManish V Badarkhe	msr	vstcr_el2, x16
3922825946eSMax Shvetsov
393fb2072b0SManish V Badarkhe	ldr	x17, [x0, #CTX_VSTTBR_EL2]
394fb2072b0SManish V Badarkhe	msr	vsttbr_el2, x17
3957f164a83SOlivier Deprez
396fb2072b0SManish V Badarkhe	ldr	x9, [x0, #CTX_TRFCR_EL2]
397fb2072b0SManish V Badarkhe	msr	TRFCR_EL2, x9
3982825946eSMax Shvetsov#endif
3992825946eSMax Shvetsov
4002825946eSMax Shvetsov#if ARM_ARCH_AT_LEAST(8, 5)
401fb2072b0SManish V Badarkhe	ldr	x10, [x0, #CTX_SCXTNUM_EL2]
402fb2072b0SManish V Badarkhe	msr	scxtnum_el2, x10
4032825946eSMax Shvetsov#endif
40428f39f02SMax Shvetsov
40528f39f02SMax Shvetsov	ret
40628f39f02SMax Shvetsovendfunc el2_sysregs_context_restore
40728f39f02SMax Shvetsov
40828f39f02SMax Shvetsov#endif /* CTX_INCLUDE_EL2_REGS */
40928f39f02SMax Shvetsov
410ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
411ed108b56SAlexei Fedorov * The following function strictly follows the AArch64 PCS to use
412ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers) to save EL1 system
413ed108b56SAlexei Fedorov * register context. It assumes that 'x0' is pointing to a
414ed108b56SAlexei Fedorov * 'el1_sys_regs' structure where the register context will be saved.
415ed108b56SAlexei Fedorov * ------------------------------------------------------------------
416532ed618SSoby Mathew */
417532ed618SSoby Mathewfunc el1_sysregs_context_save
418532ed618SSoby Mathew
419532ed618SSoby Mathew	mrs	x9, spsr_el1
420532ed618SSoby Mathew	mrs	x10, elr_el1
421532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_SPSR_EL1]
422532ed618SSoby Mathew
423532ed618SSoby Mathew	mrs	x15, sctlr_el1
424*cb55615cSManish V Badarkhe	mrs	x16, tcr_el1
425532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_SCTLR_EL1]
426532ed618SSoby Mathew
427532ed618SSoby Mathew	mrs	x17, cpacr_el1
428532ed618SSoby Mathew	mrs	x9, csselr_el1
429532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CPACR_EL1]
430532ed618SSoby Mathew
431532ed618SSoby Mathew	mrs	x10, sp_el1
432532ed618SSoby Mathew	mrs	x11, esr_el1
433532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_SP_EL1]
434532ed618SSoby Mathew
435532ed618SSoby Mathew	mrs	x12, ttbr0_el1
436532ed618SSoby Mathew	mrs	x13, ttbr1_el1
437532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_TTBR0_EL1]
438532ed618SSoby Mathew
439532ed618SSoby Mathew	mrs	x14, mair_el1
440532ed618SSoby Mathew	mrs	x15, amair_el1
441532ed618SSoby Mathew	stp	x14, x15, [x0, #CTX_MAIR_EL1]
442532ed618SSoby Mathew
443*cb55615cSManish V Badarkhe	mrs	x16, actlr_el1
444532ed618SSoby Mathew	mrs	x17, tpidr_el1
445*cb55615cSManish V Badarkhe	stp	x16, x17, [x0, #CTX_ACTLR_EL1]
446532ed618SSoby Mathew
447532ed618SSoby Mathew	mrs	x9, tpidr_el0
448532ed618SSoby Mathew	mrs	x10, tpidrro_el0
449532ed618SSoby Mathew	stp	x9, x10, [x0, #CTX_TPIDR_EL0]
450532ed618SSoby Mathew
451532ed618SSoby Mathew	mrs	x13, par_el1
452532ed618SSoby Mathew	mrs	x14, far_el1
453532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_PAR_EL1]
454532ed618SSoby Mathew
455532ed618SSoby Mathew	mrs	x15, afsr0_el1
456532ed618SSoby Mathew	mrs	x16, afsr1_el1
457532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_AFSR0_EL1]
458532ed618SSoby Mathew
459532ed618SSoby Mathew	mrs	x17, contextidr_el1
460532ed618SSoby Mathew	mrs	x9, vbar_el1
461532ed618SSoby Mathew	stp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
462532ed618SSoby Mathew
463532ed618SSoby Mathew	/* Save AArch32 system registers if the build has instructed so */
464532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
465532ed618SSoby Mathew	mrs	x11, spsr_abt
466532ed618SSoby Mathew	mrs	x12, spsr_und
467532ed618SSoby Mathew	stp	x11, x12, [x0, #CTX_SPSR_ABT]
468532ed618SSoby Mathew
469532ed618SSoby Mathew	mrs	x13, spsr_irq
470532ed618SSoby Mathew	mrs	x14, spsr_fiq
471532ed618SSoby Mathew	stp	x13, x14, [x0, #CTX_SPSR_IRQ]
472532ed618SSoby Mathew
473532ed618SSoby Mathew	mrs	x15, dacr32_el2
474532ed618SSoby Mathew	mrs	x16, ifsr32_el2
475532ed618SSoby Mathew	stp	x15, x16, [x0, #CTX_DACR32_EL2]
476532ed618SSoby Mathew#endif
477532ed618SSoby Mathew
478532ed618SSoby Mathew	/* Save NS timer registers if the build has instructed so */
479532ed618SSoby Mathew#if NS_TIMER_SWITCH
480532ed618SSoby Mathew	mrs	x10, cntp_ctl_el0
481532ed618SSoby Mathew	mrs	x11, cntp_cval_el0
482532ed618SSoby Mathew	stp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
483532ed618SSoby Mathew
484532ed618SSoby Mathew	mrs	x12, cntv_ctl_el0
485532ed618SSoby Mathew	mrs	x13, cntv_cval_el0
486532ed618SSoby Mathew	stp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
487532ed618SSoby Mathew
488532ed618SSoby Mathew	mrs	x14, cntkctl_el1
489532ed618SSoby Mathew	str	x14, [x0, #CTX_CNTKCTL_EL1]
490532ed618SSoby Mathew#endif
491532ed618SSoby Mathew
4929dd94382SJustin Chadwell	/* Save MTE system registers if the build has instructed so */
4939dd94382SJustin Chadwell#if CTX_INCLUDE_MTE_REGS
4949dd94382SJustin Chadwell	mrs	x15, TFSRE0_EL1
4959dd94382SJustin Chadwell	mrs	x16, TFSR_EL1
4969dd94382SJustin Chadwell	stp	x15, x16, [x0, #CTX_TFSRE0_EL1]
4979dd94382SJustin Chadwell
4989dd94382SJustin Chadwell	mrs	x9, RGSR_EL1
4999dd94382SJustin Chadwell	mrs	x10, GCR_EL1
5009dd94382SJustin Chadwell	stp	x9, x10, [x0, #CTX_RGSR_EL1]
5019dd94382SJustin Chadwell#endif
5029dd94382SJustin Chadwell
503532ed618SSoby Mathew	ret
504532ed618SSoby Mathewendfunc el1_sysregs_context_save
505532ed618SSoby Mathew
506ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
507ed108b56SAlexei Fedorov * The following function strictly follows the AArch64 PCS to use
508ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers) to restore EL1 system
509ed108b56SAlexei Fedorov * register context.  It assumes that 'x0' is pointing to a
510ed108b56SAlexei Fedorov * 'el1_sys_regs' structure from where the register context will be
511ed108b56SAlexei Fedorov * restored
512ed108b56SAlexei Fedorov * ------------------------------------------------------------------
513532ed618SSoby Mathew */
514532ed618SSoby Mathewfunc el1_sysregs_context_restore
515532ed618SSoby Mathew
516532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_SPSR_EL1]
517532ed618SSoby Mathew	msr	spsr_el1, x9
518532ed618SSoby Mathew	msr	elr_el1, x10
519532ed618SSoby Mathew
520fb2072b0SManish V Badarkhe	ldp	x15, x16, [x0, #CTX_SCTLR_EL1]
521fb2072b0SManish V Badarkhe	msr	sctlr_el1, x15
522*cb55615cSManish V Badarkhe	msr	tcr_el1, x16
523532ed618SSoby Mathew
524532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CPACR_EL1]
525532ed618SSoby Mathew	msr	cpacr_el1, x17
526532ed618SSoby Mathew	msr	csselr_el1, x9
527532ed618SSoby Mathew
528532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_SP_EL1]
529532ed618SSoby Mathew	msr	sp_el1, x10
530532ed618SSoby Mathew	msr	esr_el1, x11
531532ed618SSoby Mathew
532532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_TTBR0_EL1]
533532ed618SSoby Mathew	msr	ttbr0_el1, x12
534532ed618SSoby Mathew	msr	ttbr1_el1, x13
535532ed618SSoby Mathew
536532ed618SSoby Mathew	ldp	x14, x15, [x0, #CTX_MAIR_EL1]
537532ed618SSoby Mathew	msr	mair_el1, x14
538532ed618SSoby Mathew	msr	amair_el1, x15
539532ed618SSoby Mathew
540*cb55615cSManish V Badarkhe	ldp 	x16, x17, [x0, #CTX_ACTLR_EL1]
541*cb55615cSManish V Badarkhe	msr	actlr_el1, x16
542fb2072b0SManish V Badarkhe	msr	tpidr_el1, x17
543532ed618SSoby Mathew
544532ed618SSoby Mathew	ldp	x9, x10, [x0, #CTX_TPIDR_EL0]
545532ed618SSoby Mathew	msr	tpidr_el0, x9
546532ed618SSoby Mathew	msr	tpidrro_el0, x10
547532ed618SSoby Mathew
548532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_PAR_EL1]
549532ed618SSoby Mathew	msr	par_el1, x13
550532ed618SSoby Mathew	msr	far_el1, x14
551532ed618SSoby Mathew
552532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_AFSR0_EL1]
553532ed618SSoby Mathew	msr	afsr0_el1, x15
554532ed618SSoby Mathew	msr	afsr1_el1, x16
555532ed618SSoby Mathew
556532ed618SSoby Mathew	ldp	x17, x9, [x0, #CTX_CONTEXTIDR_EL1]
557532ed618SSoby Mathew	msr	contextidr_el1, x17
558532ed618SSoby Mathew	msr	vbar_el1, x9
559532ed618SSoby Mathew
560532ed618SSoby Mathew	/* Restore AArch32 system registers if the build has instructed so */
561532ed618SSoby Mathew#if CTX_INCLUDE_AARCH32_REGS
562532ed618SSoby Mathew	ldp	x11, x12, [x0, #CTX_SPSR_ABT]
563532ed618SSoby Mathew	msr	spsr_abt, x11
564532ed618SSoby Mathew	msr	spsr_und, x12
565532ed618SSoby Mathew
566532ed618SSoby Mathew	ldp	x13, x14, [x0, #CTX_SPSR_IRQ]
567532ed618SSoby Mathew	msr	spsr_irq, x13
568532ed618SSoby Mathew	msr	spsr_fiq, x14
569532ed618SSoby Mathew
570532ed618SSoby Mathew	ldp	x15, x16, [x0, #CTX_DACR32_EL2]
571532ed618SSoby Mathew	msr	dacr32_el2, x15
572532ed618SSoby Mathew	msr	ifsr32_el2, x16
573532ed618SSoby Mathew#endif
574532ed618SSoby Mathew	/* Restore NS timer registers if the build has instructed so */
575532ed618SSoby Mathew#if NS_TIMER_SWITCH
576532ed618SSoby Mathew	ldp	x10, x11, [x0, #CTX_CNTP_CTL_EL0]
577532ed618SSoby Mathew	msr	cntp_ctl_el0, x10
578532ed618SSoby Mathew	msr	cntp_cval_el0, x11
579532ed618SSoby Mathew
580532ed618SSoby Mathew	ldp	x12, x13, [x0, #CTX_CNTV_CTL_EL0]
581532ed618SSoby Mathew	msr	cntv_ctl_el0, x12
582532ed618SSoby Mathew	msr	cntv_cval_el0, x13
583532ed618SSoby Mathew
584532ed618SSoby Mathew	ldr	x14, [x0, #CTX_CNTKCTL_EL1]
585532ed618SSoby Mathew	msr	cntkctl_el1, x14
586532ed618SSoby Mathew#endif
5879dd94382SJustin Chadwell	/* Restore MTE system registers if the build has instructed so */
5889dd94382SJustin Chadwell#if CTX_INCLUDE_MTE_REGS
5899dd94382SJustin Chadwell	ldp	x11, x12, [x0, #CTX_TFSRE0_EL1]
5909dd94382SJustin Chadwell	msr	TFSRE0_EL1, x11
5919dd94382SJustin Chadwell	msr	TFSR_EL1, x12
5929dd94382SJustin Chadwell
5939dd94382SJustin Chadwell	ldp	x13, x14, [x0, #CTX_RGSR_EL1]
5949dd94382SJustin Chadwell	msr	RGSR_EL1, x13
5959dd94382SJustin Chadwell	msr	GCR_EL1, x14
5969dd94382SJustin Chadwell#endif
597532ed618SSoby Mathew
598532ed618SSoby Mathew	/* No explict ISB required here as ERET covers it */
599532ed618SSoby Mathew	ret
600532ed618SSoby Mathewendfunc el1_sysregs_context_restore
601532ed618SSoby Mathew
602ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
603ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use
604ed108b56SAlexei Fedorov * x9-x17 (temporary caller-saved registers according to AArch64 PCS)
605ed108b56SAlexei Fedorov * to save floating point register context. It assumes that 'x0' is
606ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure where the register context will
607532ed618SSoby Mathew * be saved.
608532ed618SSoby Mathew *
609ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set.
610ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in
611ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared.
612532ed618SSoby Mathew *
613532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
614ed108b56SAlexei Fedorov * ------------------------------------------------------------------
615532ed618SSoby Mathew */
616532ed618SSoby Mathew#if CTX_INCLUDE_FPREGS
617532ed618SSoby Mathewfunc fpregs_context_save
618532ed618SSoby Mathew	stp	q0, q1, [x0, #CTX_FP_Q0]
619532ed618SSoby Mathew	stp	q2, q3, [x0, #CTX_FP_Q2]
620532ed618SSoby Mathew	stp	q4, q5, [x0, #CTX_FP_Q4]
621532ed618SSoby Mathew	stp	q6, q7, [x0, #CTX_FP_Q6]
622532ed618SSoby Mathew	stp	q8, q9, [x0, #CTX_FP_Q8]
623532ed618SSoby Mathew	stp	q10, q11, [x0, #CTX_FP_Q10]
624532ed618SSoby Mathew	stp	q12, q13, [x0, #CTX_FP_Q12]
625532ed618SSoby Mathew	stp	q14, q15, [x0, #CTX_FP_Q14]
626532ed618SSoby Mathew	stp	q16, q17, [x0, #CTX_FP_Q16]
627532ed618SSoby Mathew	stp	q18, q19, [x0, #CTX_FP_Q18]
628532ed618SSoby Mathew	stp	q20, q21, [x0, #CTX_FP_Q20]
629532ed618SSoby Mathew	stp	q22, q23, [x0, #CTX_FP_Q22]
630532ed618SSoby Mathew	stp	q24, q25, [x0, #CTX_FP_Q24]
631532ed618SSoby Mathew	stp	q26, q27, [x0, #CTX_FP_Q26]
632532ed618SSoby Mathew	stp	q28, q29, [x0, #CTX_FP_Q28]
633532ed618SSoby Mathew	stp	q30, q31, [x0, #CTX_FP_Q30]
634532ed618SSoby Mathew
635532ed618SSoby Mathew	mrs	x9, fpsr
636532ed618SSoby Mathew	str	x9, [x0, #CTX_FP_FPSR]
637532ed618SSoby Mathew
638532ed618SSoby Mathew	mrs	x10, fpcr
639532ed618SSoby Mathew	str	x10, [x0, #CTX_FP_FPCR]
640532ed618SSoby Mathew
64191089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
64291089f36SDavid Cunado	mrs	x11, fpexc32_el2
64391089f36SDavid Cunado	str	x11, [x0, #CTX_FP_FPEXC32_EL2]
64491089f36SDavid Cunado#endif
645532ed618SSoby Mathew	ret
646532ed618SSoby Mathewendfunc fpregs_context_save
647532ed618SSoby Mathew
648ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
649ed108b56SAlexei Fedorov * The following function follows the aapcs_64 strictly to use x9-x17
650ed108b56SAlexei Fedorov * (temporary caller-saved registers according to AArch64 PCS) to
651ed108b56SAlexei Fedorov * restore floating point register context. It assumes that 'x0' is
652ed108b56SAlexei Fedorov * pointing to a 'fp_regs' structure from where the register context
653532ed618SSoby Mathew * will be restored.
654532ed618SSoby Mathew *
655ed108b56SAlexei Fedorov * Access to VFP registers will trap if CPTR_EL3.TFP is set.
656ed108b56SAlexei Fedorov * However currently we don't use VFP registers nor set traps in
657ed108b56SAlexei Fedorov * Trusted Firmware, and assume it's cleared.
658532ed618SSoby Mathew *
659532ed618SSoby Mathew * TODO: Revisit when VFP is used in secure world
660ed108b56SAlexei Fedorov * ------------------------------------------------------------------
661532ed618SSoby Mathew */
662532ed618SSoby Mathewfunc fpregs_context_restore
663532ed618SSoby Mathew	ldp	q0, q1, [x0, #CTX_FP_Q0]
664532ed618SSoby Mathew	ldp	q2, q3, [x0, #CTX_FP_Q2]
665532ed618SSoby Mathew	ldp	q4, q5, [x0, #CTX_FP_Q4]
666532ed618SSoby Mathew	ldp	q6, q7, [x0, #CTX_FP_Q6]
667532ed618SSoby Mathew	ldp	q8, q9, [x0, #CTX_FP_Q8]
668532ed618SSoby Mathew	ldp	q10, q11, [x0, #CTX_FP_Q10]
669532ed618SSoby Mathew	ldp	q12, q13, [x0, #CTX_FP_Q12]
670532ed618SSoby Mathew	ldp	q14, q15, [x0, #CTX_FP_Q14]
671532ed618SSoby Mathew	ldp	q16, q17, [x0, #CTX_FP_Q16]
672532ed618SSoby Mathew	ldp	q18, q19, [x0, #CTX_FP_Q18]
673532ed618SSoby Mathew	ldp	q20, q21, [x0, #CTX_FP_Q20]
674532ed618SSoby Mathew	ldp	q22, q23, [x0, #CTX_FP_Q22]
675532ed618SSoby Mathew	ldp	q24, q25, [x0, #CTX_FP_Q24]
676532ed618SSoby Mathew	ldp	q26, q27, [x0, #CTX_FP_Q26]
677532ed618SSoby Mathew	ldp	q28, q29, [x0, #CTX_FP_Q28]
678532ed618SSoby Mathew	ldp	q30, q31, [x0, #CTX_FP_Q30]
679532ed618SSoby Mathew
680532ed618SSoby Mathew	ldr	x9, [x0, #CTX_FP_FPSR]
681532ed618SSoby Mathew	msr	fpsr, x9
682532ed618SSoby Mathew
683532ed618SSoby Mathew	ldr	x10, [x0, #CTX_FP_FPCR]
684532ed618SSoby Mathew	msr	fpcr, x10
685532ed618SSoby Mathew
68691089f36SDavid Cunado#if CTX_INCLUDE_AARCH32_REGS
68791089f36SDavid Cunado	ldr	x11, [x0, #CTX_FP_FPEXC32_EL2]
68891089f36SDavid Cunado	msr	fpexc32_el2, x11
68991089f36SDavid Cunado#endif
690532ed618SSoby Mathew	/*
691532ed618SSoby Mathew	 * No explict ISB required here as ERET to
692532ed618SSoby Mathew	 * switch to secure EL1 or non-secure world
693532ed618SSoby Mathew	 * covers it
694532ed618SSoby Mathew	 */
695532ed618SSoby Mathew
696532ed618SSoby Mathew	ret
697532ed618SSoby Mathewendfunc fpregs_context_restore
698532ed618SSoby Mathew#endif /* CTX_INCLUDE_FPREGS */
699532ed618SSoby Mathew
700ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
701ed108b56SAlexei Fedorov * The following function is used to save and restore all the general
702ed108b56SAlexei Fedorov * purpose and ARMv8.3-PAuth (if enabled) registers.
703ed108b56SAlexei Fedorov * It also checks if Secure Cycle Counter is not disabled in MDCR_EL3
704ed108b56SAlexei Fedorov * when ARMv8.5-PMU is implemented, and if called from Non-secure
705ed108b56SAlexei Fedorov * state saves PMCR_EL0 and disables Cycle Counter.
706ed108b56SAlexei Fedorov *
707ed108b56SAlexei Fedorov * Ideally we would only save and restore the callee saved registers
708ed108b56SAlexei Fedorov * when a world switch occurs but that type of implementation is more
709ed108b56SAlexei Fedorov * complex. So currently we will always save and restore these
710ed108b56SAlexei Fedorov * registers on entry and exit of EL3.
711ed108b56SAlexei Fedorov * These are not macros to ensure their invocation fits within the 32
712ed108b56SAlexei Fedorov * instructions per exception vector.
713532ed618SSoby Mathew * clobbers: x18
714ed108b56SAlexei Fedorov * ------------------------------------------------------------------
715532ed618SSoby Mathew */
716ed108b56SAlexei Fedorovfunc save_gp_pmcr_pauth_regs
717532ed618SSoby Mathew	stp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
718532ed618SSoby Mathew	stp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
719532ed618SSoby Mathew	stp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
720532ed618SSoby Mathew	stp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
721532ed618SSoby Mathew	stp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
722532ed618SSoby Mathew	stp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
723532ed618SSoby Mathew	stp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
724532ed618SSoby Mathew	stp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
725532ed618SSoby Mathew	stp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
726532ed618SSoby Mathew	stp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
727532ed618SSoby Mathew	stp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
728532ed618SSoby Mathew	stp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
729532ed618SSoby Mathew	stp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
730532ed618SSoby Mathew	stp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
731532ed618SSoby Mathew	stp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
732532ed618SSoby Mathew	mrs	x18, sp_el0
733532ed618SSoby Mathew	str	x18, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
734532ed618SSoby Mathew
735ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
736ed108b56SAlexei Fedorov	 * Check if earlier initialization MDCR_EL3.SCCD to 1 failed,
737ed108b56SAlexei Fedorov	 * meaning that ARMv8-PMU is not implemented and PMCR_EL0
738ed108b56SAlexei Fedorov	 * should be saved in non-secure context.
739ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
740ef653d93SJeenu Viswambharan	 */
741ed108b56SAlexei Fedorov	mrs	x9, mdcr_el3
742ed108b56SAlexei Fedorov	tst	x9, #MDCR_SCCD_BIT
743ed108b56SAlexei Fedorov	bne	1f
744ed108b56SAlexei Fedorov
745ed108b56SAlexei Fedorov	/* Secure Cycle Counter is not disabled */
746ed108b56SAlexei Fedorov	mrs	x9, pmcr_el0
747ed108b56SAlexei Fedorov
748ed108b56SAlexei Fedorov	/* Check caller's security state */
749ed108b56SAlexei Fedorov	mrs	x10, scr_el3
750ed108b56SAlexei Fedorov	tst	x10, #SCR_NS_BIT
751ed108b56SAlexei Fedorov	beq	2f
752ed108b56SAlexei Fedorov
753ed108b56SAlexei Fedorov	/* Save PMCR_EL0 if called from Non-secure state */
754ed108b56SAlexei Fedorov	str	x9, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
755ed108b56SAlexei Fedorov
756ed108b56SAlexei Fedorov	/* Disable cycle counter when event counting is prohibited */
757ed108b56SAlexei Fedorov2:	orr	x9, x9, #PMCR_EL0_DP_BIT
758ed108b56SAlexei Fedorov	msr	pmcr_el0, x9
759ed108b56SAlexei Fedorov	isb
760ed108b56SAlexei Fedorov1:
761ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS
762ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
763ed108b56SAlexei Fedorov 	 * Save the ARMv8.3-PAuth keys as they are not banked
764ed108b56SAlexei Fedorov 	 * by exception level
765ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
766ed108b56SAlexei Fedorov	 */
767ed108b56SAlexei Fedorov	add	x19, sp, #CTX_PAUTH_REGS_OFFSET
768ed108b56SAlexei Fedorov
769ed108b56SAlexei Fedorov	mrs	x20, APIAKeyLo_EL1	/* x21:x20 = APIAKey */
770ed108b56SAlexei Fedorov	mrs	x21, APIAKeyHi_EL1
771ed108b56SAlexei Fedorov	mrs	x22, APIBKeyLo_EL1	/* x23:x22 = APIBKey */
772ed108b56SAlexei Fedorov	mrs	x23, APIBKeyHi_EL1
773ed108b56SAlexei Fedorov	mrs	x24, APDAKeyLo_EL1	/* x25:x24 = APDAKey */
774ed108b56SAlexei Fedorov	mrs	x25, APDAKeyHi_EL1
775ed108b56SAlexei Fedorov	mrs	x26, APDBKeyLo_EL1	/* x27:x26 = APDBKey */
776ed108b56SAlexei Fedorov	mrs	x27, APDBKeyHi_EL1
777ed108b56SAlexei Fedorov	mrs	x28, APGAKeyLo_EL1	/* x29:x28 = APGAKey */
778ed108b56SAlexei Fedorov	mrs	x29, APGAKeyHi_EL1
779ed108b56SAlexei Fedorov
780ed108b56SAlexei Fedorov	stp	x20, x21, [x19, #CTX_PACIAKEY_LO]
781ed108b56SAlexei Fedorov	stp	x22, x23, [x19, #CTX_PACIBKEY_LO]
782ed108b56SAlexei Fedorov	stp	x24, x25, [x19, #CTX_PACDAKEY_LO]
783ed108b56SAlexei Fedorov	stp	x26, x27, [x19, #CTX_PACDBKEY_LO]
784ed108b56SAlexei Fedorov	stp	x28, x29, [x19, #CTX_PACGAKEY_LO]
785ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */
786ed108b56SAlexei Fedorov
787ed108b56SAlexei Fedorov	ret
788ed108b56SAlexei Fedorovendfunc save_gp_pmcr_pauth_regs
789ed108b56SAlexei Fedorov
790ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
791ed108b56SAlexei Fedorov * This function restores ARMv8.3-PAuth (if enabled) and all general
792ed108b56SAlexei Fedorov * purpose registers except x30 from the CPU context.
793ed108b56SAlexei Fedorov * x30 register must be explicitly restored by the caller.
794ed108b56SAlexei Fedorov * ------------------------------------------------------------------
795ed108b56SAlexei Fedorov */
796ed108b56SAlexei Fedorovfunc restore_gp_pmcr_pauth_regs
797ed108b56SAlexei Fedorov#if CTX_INCLUDE_PAUTH_REGS
798ed108b56SAlexei Fedorov 	/* Restore the ARMv8.3 PAuth keys */
799ed108b56SAlexei Fedorov	add	x10, sp, #CTX_PAUTH_REGS_OFFSET
800ed108b56SAlexei Fedorov
801ed108b56SAlexei Fedorov	ldp	x0, x1, [x10, #CTX_PACIAKEY_LO]	/* x1:x0 = APIAKey */
802ed108b56SAlexei Fedorov	ldp	x2, x3, [x10, #CTX_PACIBKEY_LO]	/* x3:x2 = APIBKey */
803ed108b56SAlexei Fedorov	ldp	x4, x5, [x10, #CTX_PACDAKEY_LO]	/* x5:x4 = APDAKey */
804ed108b56SAlexei Fedorov	ldp	x6, x7, [x10, #CTX_PACDBKEY_LO]	/* x7:x6 = APDBKey */
805ed108b56SAlexei Fedorov	ldp	x8, x9, [x10, #CTX_PACGAKEY_LO]	/* x9:x8 = APGAKey */
806ed108b56SAlexei Fedorov
807ed108b56SAlexei Fedorov	msr	APIAKeyLo_EL1, x0
808ed108b56SAlexei Fedorov	msr	APIAKeyHi_EL1, x1
809ed108b56SAlexei Fedorov	msr	APIBKeyLo_EL1, x2
810ed108b56SAlexei Fedorov	msr	APIBKeyHi_EL1, x3
811ed108b56SAlexei Fedorov	msr	APDAKeyLo_EL1, x4
812ed108b56SAlexei Fedorov	msr	APDAKeyHi_EL1, x5
813ed108b56SAlexei Fedorov	msr	APDBKeyLo_EL1, x6
814ed108b56SAlexei Fedorov	msr	APDBKeyHi_EL1, x7
815ed108b56SAlexei Fedorov	msr	APGAKeyLo_EL1, x8
816ed108b56SAlexei Fedorov	msr	APGAKeyHi_EL1, x9
817ed108b56SAlexei Fedorov#endif /* CTX_INCLUDE_PAUTH_REGS */
818ed108b56SAlexei Fedorov
819ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
820ed108b56SAlexei Fedorov	 * Restore PMCR_EL0 when returning to Non-secure state if
821ed108b56SAlexei Fedorov	 * Secure Cycle Counter is not disabled in MDCR_EL3 when
822ed108b56SAlexei Fedorov	 * ARMv8.5-PMU is implemented.
823ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
824ed108b56SAlexei Fedorov	 */
825ed108b56SAlexei Fedorov	mrs	x0, scr_el3
826ed108b56SAlexei Fedorov	tst	x0, #SCR_NS_BIT
827ed108b56SAlexei Fedorov	beq	2f
828ed108b56SAlexei Fedorov
829ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
830ed108b56SAlexei Fedorov	 * Back to Non-secure state.
831ed108b56SAlexei Fedorov	 * Check if earlier initialization MDCR_EL3.SCCD to 1 failed,
832ed108b56SAlexei Fedorov	 * meaning that ARMv8-PMU is not implemented and PMCR_EL0
833ed108b56SAlexei Fedorov	 * should be restored from non-secure context.
834ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
835ed108b56SAlexei Fedorov	 */
836ed108b56SAlexei Fedorov	mrs	x0, mdcr_el3
837ed108b56SAlexei Fedorov	tst	x0, #MDCR_SCCD_BIT
838ed108b56SAlexei Fedorov	bne	2f
839ed108b56SAlexei Fedorov	ldr	x0, [sp, #CTX_EL3STATE_OFFSET + CTX_PMCR_EL0]
840ed108b56SAlexei Fedorov	msr	pmcr_el0, x0
841ed108b56SAlexei Fedorov2:
842532ed618SSoby Mathew	ldp	x0, x1, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X0]
843532ed618SSoby Mathew	ldp	x2, x3, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X2]
844532ed618SSoby Mathew	ldp	x4, x5, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X4]
845532ed618SSoby Mathew	ldp	x6, x7, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X6]
846532ed618SSoby Mathew	ldp	x8, x9, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X8]
847532ed618SSoby Mathew	ldp	x10, x11, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X10]
848532ed618SSoby Mathew	ldp	x12, x13, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X12]
849532ed618SSoby Mathew	ldp	x14, x15, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X14]
850ef653d93SJeenu Viswambharan	ldp	x16, x17, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X16]
851532ed618SSoby Mathew	ldp	x18, x19, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X18]
852532ed618SSoby Mathew	ldp	x20, x21, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X20]
853532ed618SSoby Mathew	ldp	x22, x23, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X22]
854532ed618SSoby Mathew	ldp	x24, x25, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X24]
855532ed618SSoby Mathew	ldp	x26, x27, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X26]
856ef653d93SJeenu Viswambharan	ldr	x28, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_SP_EL0]
857ef653d93SJeenu Viswambharan	msr	sp_el0, x28
858532ed618SSoby Mathew	ldp	x28, x29, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_X28]
859ef653d93SJeenu Viswambharan	ret
860ed108b56SAlexei Fedorovendfunc restore_gp_pmcr_pauth_regs
861ef653d93SJeenu Viswambharan
862ed108b56SAlexei Fedorov/* ------------------------------------------------------------------
863ed108b56SAlexei Fedorov * This routine assumes that the SP_EL3 is pointing to a valid
864ed108b56SAlexei Fedorov * context structure from where the gp regs and other special
865ed108b56SAlexei Fedorov * registers can be retrieved.
866ed108b56SAlexei Fedorov * ------------------------------------------------------------------
867532ed618SSoby Mathew */
868532ed618SSoby Mathewfunc el3_exit
869bb9549baSJan Dabros#if ENABLE_ASSERTIONS
870bb9549baSJan Dabros	/* el3_exit assumes SP_EL0 on entry */
871bb9549baSJan Dabros	mrs	x17, spsel
872bb9549baSJan Dabros	cmp	x17, #MODE_SP_EL0
873bb9549baSJan Dabros	ASM_ASSERT(eq)
874bb9549baSJan Dabros#endif
875bb9549baSJan Dabros
876ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
877ed108b56SAlexei Fedorov	 * Save the current SP_EL0 i.e. the EL3 runtime stack which
878ed108b56SAlexei Fedorov	 * will be used for handling the next SMC.
879ed108b56SAlexei Fedorov	 * Then switch to SP_EL3.
880ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
881532ed618SSoby Mathew	 */
882532ed618SSoby Mathew	mov	x17, sp
883ed108b56SAlexei Fedorov	msr	spsel, #MODE_SP_ELX
884532ed618SSoby Mathew	str	x17, [sp, #CTX_EL3STATE_OFFSET + CTX_RUNTIME_SP]
885532ed618SSoby Mathew
886ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
887532ed618SSoby Mathew	 * Restore SPSR_EL3, ELR_EL3 and SCR_EL3 prior to ERET
888ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
889532ed618SSoby Mathew	 */
890532ed618SSoby Mathew	ldr	x18, [sp, #CTX_EL3STATE_OFFSET + CTX_SCR_EL3]
891532ed618SSoby Mathew	ldp	x16, x17, [sp, #CTX_EL3STATE_OFFSET + CTX_SPSR_EL3]
892532ed618SSoby Mathew	msr	scr_el3, x18
893532ed618SSoby Mathew	msr	spsr_el3, x16
894532ed618SSoby Mathew	msr	elr_el3, x17
895532ed618SSoby Mathew
896fe007b2eSDimitris Papastamos#if IMAGE_BL31 && DYNAMIC_WORKAROUND_CVE_2018_3639
897ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
898ed108b56SAlexei Fedorov	 * Restore mitigation state as it was on entry to EL3
899ed108b56SAlexei Fedorov	 * ----------------------------------------------------------
900ed108b56SAlexei Fedorov	 */
901fe007b2eSDimitris Papastamos	ldr	x17, [sp, #CTX_CVE_2018_3639_OFFSET + CTX_CVE_2018_3639_DISABLE]
902ed108b56SAlexei Fedorov	cbz	x17, 1f
903fe007b2eSDimitris Papastamos	blr	x17
9044d1ccf0eSAntonio Nino Diaz1:
905fe007b2eSDimitris Papastamos#endif
906ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
907ed108b56SAlexei Fedorov	 * Restore general purpose (including x30), PMCR_EL0 and
908ed108b56SAlexei Fedorov	 * ARMv8.3-PAuth registers.
909ed108b56SAlexei Fedorov	 * Exit EL3 via ERET to a lower exception level.
910ed108b56SAlexei Fedorov 	 * ----------------------------------------------------------
911ed108b56SAlexei Fedorov 	 */
912ed108b56SAlexei Fedorov	bl	restore_gp_pmcr_pauth_regs
913ed108b56SAlexei Fedorov	ldr	x30, [sp, #CTX_GPREGS_OFFSET + CTX_GPREG_LR]
914fe007b2eSDimitris Papastamos
915ed108b56SAlexei Fedorov#if IMAGE_BL31 && RAS_EXTENSION
916ed108b56SAlexei Fedorov	/* ----------------------------------------------------------
917ed108b56SAlexei Fedorov	 * Issue Error Synchronization Barrier to synchronize SErrors
918ed108b56SAlexei Fedorov	 * before exiting EL3. We're running with EAs unmasked, so
919ed108b56SAlexei Fedorov	 * any synchronized errors would be taken immediately;
920ed108b56SAlexei Fedorov	 * therefore no need to inspect DISR_EL1 register.
921ed108b56SAlexei Fedorov 	 * ----------------------------------------------------------
922ed108b56SAlexei Fedorov	 */
923ed108b56SAlexei Fedorov	esb
9245283962eSAntonio Nino Diaz#endif
925f461fe34SAnthony Steinhauser	exception_return
9265283962eSAntonio Nino Diaz
927532ed618SSoby Mathewendfunc el3_exit
928