1 /*
2  * Copyright (c) 2023, Google, Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #define TLOG_TAG "pac"
25 
26 #include <arch/ops.h>
27 #include <stdbool.h>
28 #include <stdint.h>
29 
30 /*
31  * FEAT_PAuth is mandatory at ARM-A v8.3.
32  * FEAT_FPAC is mandatory at ARM-A v8.6.
33  * Assume present at v9 (and above), otherwise probe ID registers.
34  */
35 #if __ARM_ARCH >= 9
arch_pac_address_supported(void)36 bool arch_pac_address_supported(void) {
37     return true;
38 }
39 
arch_pac_exception_supported(void)40 bool arch_pac_exception_supported(void) {
41     return true;
42 }
43 #else
44 
45 #include <arch/arm64/sregs.h>
46 #include <arch/arm64.h>
47 
get_nibble(uint64_t reg,uint8_t shift)48 static uint8_t get_nibble(uint64_t reg, uint8_t shift) {
49     return (reg >> shift) & 0xf;
50 }
51 
arch_pac_address_supported(void)52 bool arch_pac_address_supported(void) {
53     const uint64_t isar1 = ARM64_READ_SYSREG(id_aa64isar1_el1);
54     const uint64_t isar2 = ARM64_READ_SYSREG(id_aa64isar2_el1);
55 
56     /* Address & Data authentication (APIxKey_EL1, APDxKey_EL1)*/
57     return get_nibble(isar1, ID_AA64ISAR1_EL1_APA_SHIFT) != 0 ||
58            get_nibble(isar1, ID_AA64ISAR1_EL1_API_SHIFT) != 0 ||
59            get_nibble(isar2, ID_AA64ISAR2_EL1_APA3_SHIFT) != 0;
60 }
61 
arch_pac_exception_supported(void)62 bool arch_pac_exception_supported(void) {
63 
64     /*
65      * Check each of the sysregs.
66      * Only one register should be populated, and gives which algorithm is
67      * implemented.  We check bit 0x4 in the field for FPAC or FPACCOMBINE.
68      */
69     const uint64_t isar1 = ARM64_READ_SYSREG(id_aa64isar1_el1);
70     const uint8_t apa = get_nibble(isar1, ID_AA64ISAR1_EL1_APA_SHIFT);
71     if (apa) {
72         return (apa & 0x4) != 0;
73     }
74 
75     const uint8_t api = get_nibble(isar1, ID_AA64ISAR1_EL1_API_SHIFT);
76     if (api) {
77         return (api & 0x4) != 0;
78     }
79 
80     const uint64_t isar2 = ARM64_READ_SYSREG(id_aa64isar2_el1);
81     const uint8_t apa3 = get_nibble(isar2, ID_AA64ISAR2_EL1_APA3_SHIFT);
82     if (apa3) {
83         return (apa3 & 0x4) != 0;
84     }
85 
86     return false;
87 }
88 #endif
89