Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 1 | /* |
Boyan Karatotev | 7a5246e | 2025-03-26 15:54:55 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2019-2025, Arm Limited. All rights reserved. |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 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 |
Alexei Fedorov | 3dd9f2b | 2019-10-01 13:58:23 +0100 | [diff] [blame] | 16 | .globl pauth_load_bl1_apiakey_enable |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 17 | |
| 18 | /* ------------------------------------------------------------- |
| 19 | * Program APIAKey_EL1 and enable pointer authentication in EL1 |
| 20 | * ------------------------------------------------------------- |
| 21 | */ |
| 22 | func 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 |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 35 | msr sctlr_el1, x0 |
| 36 | isb |
| 37 | |
| 38 | ldp x29, x30, [sp], #16 |
| 39 | ret |
| 40 | endfunc pauth_init_enable_el1 |
| 41 | |
| 42 | /* ------------------------------------------------------------- |
Boyan Karatotev | e7792e8 | 2023-03-06 14:45:15 +0000 | [diff] [blame] | 43 | * Disable pointer authentication in EL1 |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 44 | * ------------------------------------------------------------- |
| 45 | */ |
| 46 | func pauth_disable_el1 |
| 47 | mrs x0, sctlr_el1 |
| 48 | bic x0, x0, #SCTLR_EnIA_BIT |
| 49 | msr sctlr_el1, x0 |
| 50 | isb |
| 51 | ret |
| 52 | endfunc pauth_disable_el1 |
| 53 | |
| 54 | /* ------------------------------------------------------------- |
| 55 | * Program APIAKey_EL1 and enable pointer authentication in EL3 |
| 56 | * ------------------------------------------------------------- |
| 57 | */ |
| 58 | func pauth_init_enable_el3 |
| 59 | stp x29, x30, [sp, #-16]! |
| 60 | |
| 61 | /* Initialize platform key */ |
| 62 | bl plat_init_apkey |
| 63 | |
| 64 | /* Program instruction key A used by the Trusted Firmware */ |
| 65 | msr APIAKeyLo_EL1, x0 |
| 66 | msr APIAKeyHi_EL1, x1 |
| 67 | |
| 68 | /* Enable pointer authentication */ |
| 69 | mrs x0, sctlr_el3 |
| 70 | orr x0, x0, #SCTLR_EnIA_BIT |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 71 | msr sctlr_el3, x0 |
| 72 | isb |
| 73 | |
| 74 | ldp x29, x30, [sp], #16 |
| 75 | ret |
| 76 | endfunc pauth_init_enable_el3 |
| 77 | |
| 78 | /* ------------------------------------------------------------- |
| 79 | * Disable pointer authentication in EL3 |
| 80 | * ------------------------------------------------------------- |
| 81 | */ |
| 82 | func pauth_disable_el3 |
| 83 | mrs x0, sctlr_el3 |
| 84 | bic x0, x0, #SCTLR_EnIA_BIT |
| 85 | msr sctlr_el3, x0 |
| 86 | isb |
| 87 | ret |
| 88 | endfunc pauth_disable_el3 |
| 89 | |
| 90 | /* ------------------------------------------------------------- |
Alexei Fedorov | 3dd9f2b | 2019-10-01 13:58:23 +0100 | [diff] [blame] | 91 | * The following functions strictly follow the AArch64 PCS |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 92 | * to use x9-x17 (temporary caller-saved registers) to load |
Alexei Fedorov | 3dd9f2b | 2019-10-01 13:58:23 +0100 | [diff] [blame] | 93 | * the APIAKey_EL1 and enable pointer authentication. |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 94 | * ------------------------------------------------------------- |
| 95 | */ |
| 96 | func pauth_load_bl31_apiakey |
| 97 | /* tpidr_el3 contains the address of cpu_data structure */ |
| 98 | mrs x9, tpidr_el3 |
| 99 | |
| 100 | /* Load apiakey from cpu_data */ |
| 101 | ldp x10, x11, [x9, #CPU_DATA_APIAKEY_OFFSET] |
| 102 | |
| 103 | /* Program instruction key A */ |
| 104 | msr APIAKeyLo_EL1, x10 |
| 105 | msr APIAKeyHi_EL1, x11 |
| 106 | isb |
| 107 | ret |
| 108 | endfunc pauth_load_bl31_apiakey |
Alexei Fedorov | 3dd9f2b | 2019-10-01 13:58:23 +0100 | [diff] [blame] | 109 | |
| 110 | func pauth_load_bl1_apiakey_enable |
| 111 | /* Load instruction key A used by the Trusted Firmware */ |
| 112 | adrp x9, bl1_apiakey |
| 113 | add x9, x9, :lo12:bl1_apiakey |
| 114 | ldp x10, x11, [x9] |
| 115 | |
| 116 | /* Program instruction key A */ |
| 117 | msr APIAKeyLo_EL1, x10 |
| 118 | msr APIAKeyHi_EL1, x11 |
| 119 | |
| 120 | /* Enable pointer authentication */ |
| 121 | mrs x9, sctlr_el3 |
| 122 | orr x9, x9, #SCTLR_EnIA_BIT |
Alexei Fedorov | 3dd9f2b | 2019-10-01 13:58:23 +0100 | [diff] [blame] | 123 | msr sctlr_el3, x9 |
| 124 | isb |
| 125 | ret |
| 126 | endfunc pauth_load_bl1_apiakey_enable |