1 /* Initialize CPU feature data. AArch64 version.
2 This file is part of the GNU C Library.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
20 #include <cpu-features.h>
22 #include <elf/dl-hwcaps.h>
23 #include <sys/prctl.h>
24 #include <sys/utsname.h>
25 #include <dl-tunables-parse.h>
27 #define DCZID_DZP_MASK (1 << 4)
28 #define DCZID_BS_MASK (0xf)
30 /* The maximal set of permitted tags that the MTE random tag generation
31 instruction may use. We exclude tag 0 because a) we want to reserve
32 that for the libc heap structures and b) because it makes it easier
33 to see when pointer have been correctly tagged. */
34 #define MTE_ALLOWED_TAGS (0xfffe << PR_MTE_TAG_SHIFT)
43 static const struct cpu_list cpu_list
[] =
45 #define CPU_LIST_ENTRY(__str, __num) { __str, sizeof (__str) - 1, __num }
46 CPU_LIST_ENTRY ("thunderxt88", 0x430F0A10),
47 CPU_LIST_ENTRY ("thunderx2t99", 0x431F0AF0),
48 CPU_LIST_ENTRY ("thunderx2t99p1", 0x420F5160),
49 CPU_LIST_ENTRY ("ares", 0x411FD0C0),
50 CPU_LIST_ENTRY ("emag", 0x503F0001),
51 CPU_LIST_ENTRY ("kunpeng920", 0x481FD010),
52 CPU_LIST_ENTRY ("a64fx", 0x460F0010),
53 CPU_LIST_ENTRY ("generic", 0x0),
57 get_midr_from_mcpu (const struct tunable_str_t
*mcpu
)
59 for (int i
= 0; i
< array_length (cpu_list
); i
++)
60 if (tunable_strcmp (mcpu
, cpu_list
[i
].name
, cpu_list
[i
].len
))
61 return cpu_list
[i
].midr
;
66 #if __LINUX_KERNEL_VERSION < 0x060200
68 /* Return true if we prefer using SVE in string ifuncs. Old kernels disable
69 SVE after every system call which results in unnecessary traps if memcpy
70 uses SVE. This is true for kernels between 4.15.0 and before 6.2.0, except
71 for 5.14.0 which was patched. For these versions return false to avoid using
73 Parse the kernel version into a 24-bit kernel.major.minor value without
74 calling any library functions. If uname() is not supported or if the version
75 format is not recognized, assume the kernel is modern and return true. */
78 prefer_sve_ifuncs (void)
81 const char *p
= &buf
.release
[0];
85 if (__uname (&buf
) < 0)
88 for (int shift
= 16; shift
>= 0; shift
-= 8)
90 for (val
= 0; *p
>= '0' && *p
<= '9'; p
++)
91 val
= val
* 10 + *p
- '0';
92 kernel
|= (val
& 255) << shift
;
97 if (kernel
>= 0x060200 || kernel
== 0x050e00)
99 if (kernel
>= 0x040f00)
107 init_cpu_features (struct cpu_features
*cpu_features
)
109 register uint64_t midr
= UINT64_MAX
;
111 /* Get the tunable override. */
112 const struct tunable_str_t
*mcpu
= TUNABLE_GET (glibc
, cpu
, name
,
113 struct tunable_str_t
*,
116 midr
= get_midr_from_mcpu (mcpu
);
118 /* If there was no useful tunable override, query the MIDR if the kernel
120 if (midr
== UINT64_MAX
)
122 if (GLRO (dl_hwcap
) & HWCAP_CPUID
)
123 asm volatile ("mrs %0, midr_el1" : "=r"(midr
));
128 cpu_features
->midr_el1
= midr
;
130 /* Check if ZVA is enabled. */
132 asm volatile ("mrs %0, dczid_el0" : "=r"(dczid
));
134 if ((dczid
& DCZID_DZP_MASK
) == 0)
135 cpu_features
->zva_size
= 4 << (dczid
& DCZID_BS_MASK
);
137 /* Check if BTI is supported. */
138 cpu_features
->bti
= GLRO (dl_hwcap2
) & HWCAP2_BTI
;
140 /* Setup memory tagging support if the HW and kernel support it, and if
141 the user has requested it. */
142 cpu_features
->mte_state
= 0;
145 int mte_state
= TUNABLE_GET (glibc
, mem
, tagging
, unsigned, 0);
146 cpu_features
->mte_state
= (GLRO (dl_hwcap2
) & HWCAP2_MTE
) ? mte_state
: 0;
147 /* If we lack the MTE feature, disable the tunable, since it will
148 otherwise cause instructions that won't run on this CPU to be used. */
149 TUNABLE_SET (glibc
, mem
, tagging
, cpu_features
->mte_state
);
151 if (cpu_features
->mte_state
& 4)
152 /* Enable choosing system-preferred faulting mode. */
153 __prctl (PR_SET_TAGGED_ADDR_CTRL
,
154 (PR_TAGGED_ADDR_ENABLE
| PR_MTE_TCF_SYNC
| PR_MTE_TCF_ASYNC
157 else if (cpu_features
->mte_state
& 2)
158 __prctl (PR_SET_TAGGED_ADDR_CTRL
,
159 (PR_TAGGED_ADDR_ENABLE
| PR_MTE_TCF_SYNC
| MTE_ALLOWED_TAGS
),
161 else if (cpu_features
->mte_state
)
162 __prctl (PR_SET_TAGGED_ADDR_CTRL
,
163 (PR_TAGGED_ADDR_ENABLE
| PR_MTE_TCF_ASYNC
| MTE_ALLOWED_TAGS
),
167 /* Check if SVE is supported. */
168 cpu_features
->sve
= GLRO (dl_hwcap
) & HWCAP_SVE
;
170 cpu_features
->prefer_sve_ifuncs
= cpu_features
->sve
;
172 #if __LINUX_KERNEL_VERSION < 0x060200
173 if (cpu_features
->sve
)
174 cpu_features
->prefer_sve_ifuncs
= prefer_sve_ifuncs ();
177 /* Check if MOPS is supported. */
178 cpu_features
->mops
= GLRO (dl_hwcap2
) & HWCAP2_MOPS
;