Debian Codegraph @

lib/arm/cpu_features.c

raw
1 /*
2 * arm/cpu_features.c - feature detection for ARM CPUs
3 *
4 * Copyright 2018 Eric Biggers
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 /*
29 * ARM CPUs don't have a standard way for unprivileged programs to detect CPU
30 * features. But an OS-specific way can be used when available.
31 */
32
33 #ifdef __APPLE__
34 # undef _ANSI_SOURCE
35 # undef _DARWIN_C_SOURCE
36 # define _DARWIN_C_SOURCE /* for sysctlbyname() */
37 #endif
38
39 #include "../cpu_features_common.h" /* must be included first */
40 #include "cpu_features.h"
41
42 #ifdef ARM_CPU_FEATURES_KNOWN
43 /* Runtime ARM CPU feature detection is supported. */
44
45 #ifdef __linux__
46 /*
47 * On Linux, arm32 and arm64 CPU features can be detected by reading the
48 * AT_HWCAP and AT_HWCAP2 values from /proc/self/auxv.
49 *
50 * Ideally we'd use the C library function getauxval(), but it's not guaranteed
51 * to be available: it was only added to glibc in 2.16, and in Android it was
52 * added to API level 18 for arm32 and level 21 for arm64.
53 */
54
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #define AT_HWCAP 16
61 #define AT_HWCAP2 26
62
63 static void scan_auxv(unsigned long *hwcap, unsigned long *hwcap2)
64 {
65 int fd;
66 unsigned long auxbuf[32];
67 int filled = 0;
68 int i;
69
70 fd = open("/proc/self/auxv", O_RDONLY);
71 if (fd < 0)
72 return;
73
74 for (;;) {
75 do {
76 int ret = read(fd, &((char *)auxbuf)[filled],
77 sizeof(auxbuf) - filled);
78 if (ret <= 0) {
79 if (ret < 0 && errno == EINTR)
80 continue;
81 goto out;
82 }
83 filled += ret;
84 } while (filled < 2 * sizeof(long));
85
86 i = 0;
87 do {
88 unsigned long type = auxbuf[i];
89 unsigned long value = auxbuf[i + 1];
90
91 if (type == AT_HWCAP)
92 *hwcap = value;
93 else if (type == AT_HWCAP2)
94 *hwcap2 = value;
95 i += 2;
96 filled -= 2 * sizeof(long);
97 } while (filled >= 2 * sizeof(long));
98
99 memmove(auxbuf, &auxbuf[i], filled);
100 }
101 out:
102 close(fd);
103 }
104
105 static u32 query_arm_cpu_features(void)
106 {
107 u32 features = 0;
108 unsigned long hwcap = 0;
109 unsigned long hwcap2 = 0;
110
111 scan_auxv(&hwcap, &hwcap2);
112
113 #ifdef ARCH_ARM32
114 STATIC_ASSERT(sizeof(long) == 4);
115 if (hwcap & (1 << 12)) /* HWCAP_NEON */
116 features |= ARM_CPU_FEATURE_NEON;
117 #else
118 STATIC_ASSERT(sizeof(long) == 8);
119 if (hwcap & (1 << 1)) /* HWCAP_ASIMD */
120 features |= ARM_CPU_FEATURE_NEON;
121 if (hwcap & (1 << 4)) /* HWCAP_PMULL */
122 features |= ARM_CPU_FEATURE_PMULL;
123 if (hwcap & (1 << 7)) /* HWCAP_CRC32 */
124 features |= ARM_CPU_FEATURE_CRC32;
125 if (hwcap & (1 << 17)) /* HWCAP_SHA3 */
126 features |= ARM_CPU_FEATURE_SHA3;
127 if (hwcap & (1 << 20)) /* HWCAP_ASIMDDP */
128 features |= ARM_CPU_FEATURE_DOTPROD;
129 #endif
130 return features;
131 }
132
133 #elif defined(__APPLE__)
134 /* On Apple platforms, arm64 CPU features can be detected via sysctlbyname(). */
135
136 #include <sys/types.h>
137 #include <sys/sysctl.h>
138 #include <TargetConditionals.h>
139
140 static const struct {
141 const char *name;
142 u32 feature;
143 } feature_sysctls[] = {
144 { "hw.optional.neon", ARM_CPU_FEATURE_NEON },
145 { "hw.optional.AdvSIMD", ARM_CPU_FEATURE_NEON },
146 { "hw.optional.arm.FEAT_PMULL", ARM_CPU_FEATURE_PMULL },
147 { "hw.optional.armv8_crc32", ARM_CPU_FEATURE_CRC32 },
148 { "hw.optional.armv8_2_sha3", ARM_CPU_FEATURE_SHA3 },
149 { "hw.optional.arm.FEAT_SHA3", ARM_CPU_FEATURE_SHA3 },
150 { "hw.optional.arm.FEAT_DotProd", ARM_CPU_FEATURE_DOTPROD },
151 };
152
153 static u32 query_arm_cpu_features(void)
154 {
155 u32 features = 0;
156 size_t i;
157
158 for (i = 0; i < ARRAY_LEN(feature_sysctls); i++) {
159 const char *name = feature_sysctls[i].name;
160 u32 val = 0;
161 size_t valsize = sizeof(val);
162
163 if (sysctlbyname(name, &val, &valsize, NULL, 0) == 0 &&
164 valsize == sizeof(val) && val == 1)
165 features |= feature_sysctls[i].feature;
166 }
167 return features;
168 }
169 #elif defined(_WIN32)
170
171 #include <windows.h>
172
173 #ifndef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE /* added in Windows SDK 20348 */
174 # define PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE 43
175 #endif
176
177 static u32 query_arm_cpu_features(void)
178 {
179 u32 features = ARM_CPU_FEATURE_NEON;
180
181 if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
182 features |= ARM_CPU_FEATURE_PMULL;
183 if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
184 features |= ARM_CPU_FEATURE_CRC32;
185 if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
186 features |= ARM_CPU_FEATURE_DOTPROD;
187
188 /* FIXME: detect SHA3 support too. */
189
190 return features;
191 }
192 #else
193 #error "unhandled case"
194 #endif
195
196 static const struct cpu_feature arm_cpu_feature_table[] = {
197 {ARM_CPU_FEATURE_NEON, "neon"},
198 {ARM_CPU_FEATURE_PMULL, "pmull"},
199 {ARM_CPU_FEATURE_PREFER_PMULL, "prefer_pmull"},
200 {ARM_CPU_FEATURE_CRC32, "crc32"},
201 {ARM_CPU_FEATURE_SHA3, "sha3"},
202 {ARM_CPU_FEATURE_DOTPROD, "dotprod"},
203 };
204
205 volatile u32 libdeflate_arm_cpu_features = 0;
206
207 void libdeflate_init_arm_cpu_features(void)
208 {
209 u32 features = query_arm_cpu_features();
210
211 /*
212 * On the Apple M1 processor, crc32 instructions max out at about 25.5
213 * GB/s in the best case of using a 3-way or greater interleaved chunked
214 * implementation, whereas a pmull-based implementation achieves 68 GB/s
215 * provided that the stride length is large enough (about 10+ vectors
216 * with eor3, or 12+ without).
217 *
218 * Assume that crc32 instructions are preferable in other cases.
219 */
220 #if (defined(__APPLE__) && TARGET_OS_OSX) || defined(TEST_SUPPORT__DO_NOT_USE)
221 features |= ARM_CPU_FEATURE_PREFER_PMULL;
222 #endif
223
224 disable_cpu_features_for_testing(&features, arm_cpu_feature_table,
225 ARRAY_LEN(arm_cpu_feature_table));
226
227 libdeflate_arm_cpu_features = features | ARM_CPU_FEATURES_KNOWN;
228 }
229
230 #endif /* ARM_CPU_FEATURES_KNOWN */