1/*
2 * Copyright (c) 2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <asm_macros.S>
9#include <lib/el3_runtime/cpu_data.h>
10
11	.global	pauth_init_enable_el1
12	.global	pauth_disable_el1
13	.global	pauth_init_enable_el3
14	.global	pauth_disable_el3
15	.globl	pauth_load_bl31_apiakey
16	.globl	pauth_load_bl1_apiakey_enable
17
18/* -------------------------------------------------------------
19 * Program APIAKey_EL1 and enable pointer authentication in EL1
20 * -------------------------------------------------------------
21 */
22func pauth_init_enable_el1
23	stp	x29, x30, [sp, #-16]!
24
25	/* Initialize platform key */
26	bl	plat_init_apkey
27
28	/* Program instruction key A used by the Trusted Firmware */
29	msr	APIAKeyLo_EL1, x0
30	msr	APIAKeyHi_EL1, x1
31
32	/* Enable pointer authentication */
33	mrs	x0, sctlr_el1
34	orr	x0, x0, #SCTLR_EnIA_BIT
35
36#if ENABLE_BTI
37	 /* Enable PAC branch type compatibility */
38	bic	x0, x0, #(SCTLR_BT0_BIT | SCTLR_BT1_BIT)
39#endif
40	msr	sctlr_el1, x0
41	isb
42
43	ldp	x29, x30, [sp], #16
44	ret
45endfunc pauth_init_enable_el1
46
47/* -------------------------------------------------------------
48 * Disable pointer authentication in EL1
49 * -------------------------------------------------------------
50 */
51func pauth_disable_el1
52	mrs	x0, sctlr_el1
53	bic	x0, x0, #SCTLR_EnIA_BIT
54	msr	sctlr_el1, x0
55	isb
56	ret
57endfunc pauth_disable_el1
58
59/* -------------------------------------------------------------
60 * Program APIAKey_EL1 and enable pointer authentication in EL3
61 * -------------------------------------------------------------
62 */
63func pauth_init_enable_el3
64	stp	x29, x30, [sp, #-16]!
65
66	/* Initialize platform key */
67	bl	plat_init_apkey
68
69	/* Program instruction key A used by the Trusted Firmware */
70	msr	APIAKeyLo_EL1, x0
71	msr	APIAKeyHi_EL1, x1
72
73	/* Enable pointer authentication */
74	mrs	x0, sctlr_el3
75	orr	x0, x0, #SCTLR_EnIA_BIT
76
77#if ENABLE_BTI
78	 /* Enable PAC branch type compatibility */
79	bic	x0, x0, #SCTLR_BT_BIT
80#endif
81	msr	sctlr_el3, x0
82	isb
83
84	ldp	x29, x30, [sp], #16
85	ret
86endfunc pauth_init_enable_el3
87
88/* -------------------------------------------------------------
89 * Disable pointer authentication in EL3
90 * -------------------------------------------------------------
91 */
92func pauth_disable_el3
93	mrs	x0, sctlr_el3
94	bic	x0, x0, #SCTLR_EnIA_BIT
95	msr	sctlr_el3, x0
96	isb
97	ret
98endfunc pauth_disable_el3
99
100/* -------------------------------------------------------------
101 * The following functions strictly follow the AArch64 PCS
102 * to use x9-x17 (temporary caller-saved registers) to load
103 * the APIAKey_EL1 and enable pointer authentication.
104 * -------------------------------------------------------------
105 */
106func pauth_load_bl31_apiakey
107	/* tpidr_el3 contains the address of cpu_data structure */
108	mrs	x9, tpidr_el3
109
110	/* Load apiakey from cpu_data */
111	ldp	x10, x11, [x9, #CPU_DATA_APIAKEY_OFFSET]
112
113	/* Program instruction key A */
114	msr	APIAKeyLo_EL1, x10
115	msr	APIAKeyHi_EL1, x11
116	isb
117	ret
118endfunc pauth_load_bl31_apiakey
119
120func pauth_load_bl1_apiakey_enable
121	/* Load instruction key A used by the Trusted Firmware */
122	adrp	x9, bl1_apiakey
123	add	x9, x9, :lo12:bl1_apiakey
124	ldp	x10, x11, [x9]
125
126	/* Program instruction key A */
127	msr	APIAKeyLo_EL1, x10
128	msr	APIAKeyHi_EL1, x11
129
130	/* Enable pointer authentication */
131	mrs	x9, sctlr_el3
132	orr	x9, x9, #SCTLR_EnIA_BIT
133
134#if ENABLE_BTI
135	 /* Enable PAC branch type compatibility */
136	bic	x9, x9, #SCTLR_BT_BIT
137#endif
138	msr	sctlr_el3, x9
139	isb
140	ret
141endfunc pauth_load_bl1_apiakey_enable
142