diff --git a/pac/Makefile b/pac/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9762ed643fe0af13250091aaade8b7fcb54abd66 --- /dev/null +++ b/pac/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2023 Huawei Device Co., Ltd. +# +# Makefile for the Linux PAC module. +# + +obj-y += arm64/ diff --git a/pac/arm64/Makefile b/pac/arm64/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9060014f521c0c7cdb1130ad783280fbde4bba94 --- /dev/null +++ b/pac/arm64/Makefile @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2023 Huawei Device Co., Ltd. +# +# Makefile for the Linux PAC module. +# + +obj-y += src/pointer_auth_key.o +obj-y += src/asm_pointer_auth_key.o diff --git a/pac/arm64/include/asm_pointer_auth_key.h b/pac/arm64/include/asm_pointer_auth_key.h new file mode 100644 index 0000000000000000000000000000000000000000..e366b794355c21f086eb2d633a1b021dae1bca51 --- /dev/null +++ b/pac/arm64/include/asm_pointer_auth_key.h @@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * + * Copyright (c) 2023 Huawei Device Co., Ltd. + * + * Pointer authentication keys initialisation. + * + */ + +#ifndef __ASM_POINTER_AUTH_KEY_H +#define __ASM_POINTER_AUTH_KEY_H + +#include +#include +#include +#include + + .macro __ptrauth_address_keys_install_kernel tmp1, tmp2, tmp3 + ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_KERNEL_KEY_APIB] + msr_s SYS_APIBKEYLO_EL1, \tmp2 + msr_s SYS_APIBKEYHI_EL1, \tmp3 + + adr_l \tmp1, kernel_common_keys + ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_KERNEL_KEY_APIA] + msr_s SYS_APIAKEYLO_EL1, \tmp2 + msr_s SYS_APIAKEYHI_EL1, \tmp3 + + ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_KERNEL_KEY_APDA] + msr_s SYS_APDAKEYLO_EL1, \tmp2 + msr_s SYS_APDAKEYHI_EL1, \tmp3 + + ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_KERNEL_KEY_APDB] + msr_s SYS_APDBKEYLO_EL1, \tmp2 + msr_s SYS_APDBKEYHI_EL1, \tmp3 + .endm + + .macro __ptrauth_generic_key_install_kernel tmp1, tmp2, tmp3 + ldp \tmp2, \tmp3, [\tmp1, #PTRAUTH_KERNEL_KEY_APGA] + msr_s SYS_APGAKEYLO_EL1, \tmp2 + msr_s SYS_APGAKEYHI_EL1, \tmp3 + .endm + + .macro ptrauth_keys_install_kernel_all tsk, tmp1, tmp2, tmp3 + mov \tmp1, #THREAD_KEYS_KERNEL + add \tmp1, \tsk, \tmp1 + +alternative_if_not ARM64_HAS_ADDRESS_AUTH + b .Lno_addr_auth\@ +alternative_else_nop_endif + __ptrauth_address_keys_install_kernel \tmp1, \tmp2, \tmp3 + +.Lno_addr_auth\@: +alternative_if ARM64_HAS_GENERIC_AUTH + __ptrauth_generic_key_install_kernel \tmp1, \tmp2, \tmp3 +alternative_else_nop_endif + .endm + + .macro __ptrauth_keys_install_kernel_all tsk, tmp1, tmp2, tmp3 + mov \tmp1, #THREAD_KEYS_KERNEL + add \tmp1, \tsk, \tmp1 + __ptrauth_address_keys_install_kernel \tmp1, \tmp2, \tmp3 + __ptrauth_generic_key_install_kernel \tmp1, \tmp2, \tmp3 + .endm + +#endif diff --git a/pac/arm64/src/asm_pointer_auth_key.S b/pac/arm64/src/asm_pointer_auth_key.S new file mode 100644 index 0000000000000000000000000000000000000000..cdde298b3055aff55115ac3f25c89aa662059d2c --- /dev/null +++ b/pac/arm64/src/asm_pointer_auth_key.S @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * + * Pointer authentication keys initialisation. + */ + +#include +#include +#include + +.pushsection ".init.text", "ax" + + .macro ptrauth_key_init tmp + mrs x25, cntpct_el0 + mrs x19, cntfrq_el0 + add x25, x25, x19, lsl #32 + add x19, \tmp, x30, lsl #32 + eor x25, x25, x19 + mrs x19, cntpct_el0 + mul x19, x19, x25 + str x19, [\tmp] + mov x19, #0x9687 + movk x19, #0xB4A5, lsl #16 + mul x25, x19, x25 + mrs x19, cntpct_el0 + mul x19, x19, x25 + str x19, [\tmp, #8] + isb + .endm + + /* init ptrauth key for kernel backward-edge CFI */ + .macro ptrauth_back_key_init + mov x6, x5 /* x5: address of init task */ + mov x7, #THREAD_KEYS_KERNEL + add x6, x6, x7 + add x6, x6, #PTRAUTH_KERNEL_KEY_APIB + ptrauth_key_init x6 + .endm + + /* init common ptrauth keys for kernel forward-edge CFI, data pointer DFI and data field DFI */ + .macro ptrauth_common_keys_init + adr_l x7, kernel_common_keys + mov x6, x7 + add x6, x6, #PTRAUTH_KERNEL_KEY_APIA + ptrauth_key_init x6 + + mov x6, x7 + add x6, x6, #PTRAUTH_KERNEL_KEY_APDA + ptrauth_key_init x6 + + mov x6, x7 + add x6, x6, #PTRAUTH_KERNEL_KEY_APDB + ptrauth_key_init x6 + + mov x6, x7 + add x6, x6, #PTRAUTH_KERNEL_KEY_APGA + ptrauth_key_init x6 + + .endm + +SYM_CODE_START(ptrauth_kernel_keys_init) + ptrauth_back_key_init + + ptrauth_common_keys_init + + ret +SYM_CODE_END(ptrauth_kernel_keys_init) diff --git a/pac/arm64/src/pointer_auth_key.c b/pac/arm64/src/pointer_auth_key.c new file mode 100644 index 0000000000000000000000000000000000000000..984e2f776858fb7732a6cc4c5ce684c5ce805add --- /dev/null +++ b/pac/arm64/src/pointer_auth_key.c @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* +* Pointer authentication keys initialisation. +* +*/ + +#include + +struct ptrauth_keys_kernel_common kernel_common_keys = {{-1, -1}};