summaryrefslogtreecommitdiff
path: root/lib/extensions/pauth/pauth_helpers.S
blob: d483c7df787c2cda28004c69d064497253ace930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (c) 2019, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <arch.h>
#include <asm_macros.S>
#include <lib/el3_runtime/cpu_data.h>

	.global	pauth_init_enable_el1
	.global	pauth_disable_el1
	.global	pauth_init_enable_el3
	.global	pauth_disable_el3
	.globl	pauth_load_bl31_apiakey
	.globl	pauth_load_bl1_apiakey_enable

/* -------------------------------------------------------------
 * Program APIAKey_EL1 and enable pointer authentication in EL1
 * -------------------------------------------------------------
 */
func pauth_init_enable_el1
	stp	x29, x30, [sp, #-16]!

	/* Initialize platform key */
	bl	plat_init_apkey

	/* Program instruction key A used by the Trusted Firmware */
	msr	APIAKeyLo_EL1, x0
	msr	APIAKeyHi_EL1, x1

	/* Enable pointer authentication */
	mrs	x0, sctlr_el1
	orr	x0, x0, #SCTLR_EnIA_BIT

#if ENABLE_BTI
	 /* Enable PAC branch type compatibility */
	bic	x0, x0, #(SCTLR_BT0_BIT | SCTLR_BT1_BIT)
#endif
	msr	sctlr_el1, x0
	isb

	ldp	x29, x30, [sp], #16
	ret
endfunc pauth_init_enable_el1

/* -------------------------------------------------------------
 * Disable pointer authentication in EL3
 * -------------------------------------------------------------
 */
func pauth_disable_el1
	mrs	x0, sctlr_el1
	bic	x0, x0, #SCTLR_EnIA_BIT
	msr	sctlr_el1, x0
	isb
	ret
endfunc pauth_disable_el1

/* -------------------------------------------------------------
 * Program APIAKey_EL1 and enable pointer authentication in EL3
 * -------------------------------------------------------------
 */
func pauth_init_enable_el3
	stp	x29, x30, [sp, #-16]!

	/* Initialize platform key */
	bl	plat_init_apkey

	/* Program instruction key A used by the Trusted Firmware */
	msr	APIAKeyLo_EL1, x0
	msr	APIAKeyHi_EL1, x1

	/* Enable pointer authentication */
	mrs	x0, sctlr_el3
	orr	x0, x0, #SCTLR_EnIA_BIT

#if ENABLE_BTI
	 /* Enable PAC branch type compatibility */
	bic	x0, x0, #SCTLR_BT_BIT
#endif
	msr	sctlr_el3, x0
	isb

	ldp	x29, x30, [sp], #16
	ret
endfunc pauth_init_enable_el3

/* -------------------------------------------------------------
 * Disable pointer authentication in EL3
 * -------------------------------------------------------------
 */
func pauth_disable_el3
	mrs	x0, sctlr_el3
	bic	x0, x0, #SCTLR_EnIA_BIT
	msr	sctlr_el3, x0
	isb
	ret
endfunc pauth_disable_el3

/* -------------------------------------------------------------
 * The following functions strictly follow the AArch64 PCS
 * to use x9-x17 (temporary caller-saved registers) to load
 * the APIAKey_EL1 and enable pointer authentication.
 * -------------------------------------------------------------
 */
func pauth_load_bl31_apiakey
	/* tpidr_el3 contains the address of cpu_data structure */
	mrs	x9, tpidr_el3

	/* Load apiakey from cpu_data */
	ldp	x10, x11, [x9, #CPU_DATA_APIAKEY_OFFSET]

	/* Program instruction key A */
	msr	APIAKeyLo_EL1, x10
	msr	APIAKeyHi_EL1, x11
	isb
	ret
endfunc pauth_load_bl31_apiakey

func pauth_load_bl1_apiakey_enable
	/* Load instruction key A used by the Trusted Firmware */
	adrp	x9, bl1_apiakey
	add	x9, x9, :lo12:bl1_apiakey
	ldp	x10, x11, [x9]

	/* Program instruction key A */
	msr	APIAKeyLo_EL1, x10
	msr	APIAKeyHi_EL1, x11

	/* Enable pointer authentication */
	mrs	x9, sctlr_el3
	orr	x9, x9, #SCTLR_EnIA_BIT

#if ENABLE_BTI
	 /* Enable PAC branch type compatibility */
	bic	x9, x9, #SCTLR_BT_BIT
#endif
	msr	sctlr_el3, x9
	isb
	ret
endfunc pauth_load_bl1_apiakey_enable