1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (c) 2023 Huawei Device Co., Ltd.
4 *
5 * Pointer authentication keys initialisation.
6 */
7
8#include <linux/linkage.h>
9#include <linux/init.h>
10#include <asm/assembler.h>
11
12.pushsection ".init.text", "ax"
13
14	.macro ptrauth_key_init type, tmp
15	mrs	x25, ap\type\()keylo_el1
16	str	x25, [\tmp]
17	mrs	x25, ap\type\()keyhi_el1
18	str	x25, [\tmp, #8]
19	.endm
20
21	/* init ptrauth key for kernel backward-edge CFI */
22	.macro ptrauth_back_key_init
23	mov	x6, x5				/* x5: address of init task */
24	mov	x7, #THREAD_KEYS_KERNEL
25	add	x6, x6, x7
26	add	x6, x6, #PTRAUTH_KERNEL_KEY_APIB
27	ptrauth_key_init ib, x6
28	.endm
29
30	/* init common ptrauth keys for kernel forward-edge CFI, data pointer DFI and data field DFI */
31	.macro ptrauth_common_keys_init
32	adr_l	x7, kernel_common_keys
33	mov	x6, x7
34	add	x6, x6, #PTRAUTH_KERNEL_KEY_APIA
35	ptrauth_key_init ia, x6
36
37	mov	x6, x7
38	add	x6, x6, #PTRAUTH_KERNEL_KEY_APDA
39	ptrauth_key_init da, x6
40
41	mov	x6, x7
42	add	x6, x6, #PTRAUTH_KERNEL_KEY_APDB
43	ptrauth_key_init db, x6
44
45	mov	x6, x7
46	add	x6, x6, #PTRAUTH_KERNEL_KEY_APGA
47	ptrauth_key_init ga, x6
48
49	.endm
50
51SYM_CODE_START(ptrauth_kernel_keys_init)
52	ptrauth_back_key_init
53	ptrauth_common_keys_init
54	isb
55	ret
56SYM_CODE_END(ptrauth_kernel_keys_init)
57