]> wimlib.net Git - wimlib/blob - src/cpu_features.c
9aae638c6267d7551efbb12a70553e9cb498b3f1
[wimlib] / src / cpu_features.c
1 /*
2  * cpu_features.c - runtime CPU feature detection
3  *
4  * Copyright 2022 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 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31
32 #include "wimlib/cpu_features.h"
33
34 #if CPU_FEATURES_ENABLED
35
36 #include "wimlib/util.h"
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #if defined(__i386__) || defined(__x86_64__)
42
43 /*
44  * With old GCC versions we have to manually save and restore the x86_32 PIC
45  * register (ebx).  See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47602
46  */
47 #if defined(__i386__) && defined(__PIC__)
48 #  define EBX_CONSTRAINT "=&r"
49 #else
50 #  define EBX_CONSTRAINT "=b"
51 #endif
52
53 /* Execute the CPUID instruction. */
54 static inline void
55 cpuid(u32 leaf, u32 subleaf, u32 *a, u32 *b, u32 *c, u32 *d)
56 {
57         asm(".ifnc %%ebx, %1; mov  %%ebx, %1; .endif\n"
58             "cpuid                                  \n"
59             ".ifnc %%ebx, %1; xchg %%ebx, %1; .endif\n"
60             : "=a" (*a), EBX_CONSTRAINT (*b), "=c" (*c), "=d" (*d)
61             : "a" (leaf), "c" (subleaf));
62 }
63
64 /* Read an extended control register. */
65 static inline u64
66 read_xcr(u32 index)
67 {
68         u32 d, a;
69
70         /*
71          * Execute the "xgetbv" instruction.  Old versions of binutils do not
72          * recognize this instruction, so list the raw bytes instead.
73          */
74         asm(".byte 0x0f, 0x01, 0xd0" : "=d" (d), "=a" (a) : "c" (index));
75
76         return ((u64)d << 32) | a;
77 }
78
79 static u32
80 get_cpu_features(void)
81 {
82         u32 max_leaf, a, b, c, d;
83         u64 xcr0 = 0;
84         u32 features = 0;
85
86         /* EAX=0: Highest Function Parameter and Manufacturer ID */
87         cpuid(0, 0, &max_leaf, &b, &c, &d);
88         if (max_leaf < 1)
89                 return features;
90
91         /* EAX=1: Processor Info and Feature Bits */
92         cpuid(1, 0, &a, &b, &c, &d);
93         if (c & (1 << 9))
94                 features |= X86_CPU_FEATURE_SSSE3;
95         if (c & (1 << 19))
96                 features |= X86_CPU_FEATURE_SSE4_1;
97         if (c & (1 << 20))
98                 features |= X86_CPU_FEATURE_SSE4_2;
99         if (c & (1 << 27))
100                 xcr0 = read_xcr(0);
101         if ((c & (1 << 28)) && ((xcr0 & 0x6) == 0x6))
102                 features |= X86_CPU_FEATURE_AVX;
103
104         if (max_leaf < 7)
105                 return features;
106
107         /* EAX=7, ECX=0: Extended Features */
108         cpuid(7, 0, &a, &b, &c, &d);
109         if (b & (1 << 8))
110                 features |= X86_CPU_FEATURE_BMI2;
111         if (b & (1 << 29))
112                 features |= X86_CPU_FEATURE_SHA;
113
114         return features;
115 }
116
117 #elif defined(__aarch64__) && defined(__linux__)
118
119 /*
120  * On Linux, arm32 and arm64 CPU features can be detected by reading the
121  * AT_HWCAP and AT_HWCAP2 values from /proc/self/auxv.
122  *
123  * Ideally we'd use the C library function getauxval(), but it's not guaranteed
124  * to be available: it was only added to glibc in 2.16, and in Android it was
125  * added to API level 18 for arm32 and level 21 for arm64.
126  */
127
128 #include <errno.h>
129 #include <fcntl.h>
130 #include <string.h>
131 #include <unistd.h>
132
133 #define AT_HWCAP        16
134 #define AT_HWCAP2       26
135
136 static void scan_auxv(unsigned long *hwcap, unsigned long *hwcap2)
137 {
138         int fd;
139         unsigned long auxbuf[32];
140         int filled = 0;
141         int i;
142
143         fd = open("/proc/self/auxv", O_RDONLY);
144         if (fd < 0)
145                 return;
146
147         for (;;) {
148                 do {
149                         int ret = read(fd, &((char *)auxbuf)[filled],
150                                        sizeof(auxbuf) - filled);
151                         if (ret <= 0) {
152                                 if (ret < 0 && errno == EINTR)
153                                         continue;
154                                 goto out;
155                         }
156                         filled += ret;
157                 } while (filled < 2 * sizeof(long));
158
159                 i = 0;
160                 do {
161                         unsigned long type = auxbuf[i];
162                         unsigned long value = auxbuf[i + 1];
163
164                         if (type == AT_HWCAP)
165                                 *hwcap = value;
166                         else if (type == AT_HWCAP2)
167                                 *hwcap2 = value;
168                         i += 2;
169                         filled -= 2 * sizeof(long);
170                 } while (filled >= 2 * sizeof(long));
171
172                 memmove(auxbuf, &auxbuf[i], filled);
173         }
174 out:
175         close(fd);
176 }
177
178 static u32
179 get_cpu_features(void)
180 {
181         unsigned long hwcap = 0;
182         unsigned long hwcap2 = 0;
183         u32 features = 0;
184
185         scan_auxv(&hwcap, &hwcap2);
186
187         if (hwcap & (1 << 5))   /* HWCAP_SHA1 */
188                 features |= ARM_CPU_FEATURE_SHA1;
189
190         return features;
191 }
192
193 #elif defined(__aarch64__) && defined(__APPLE__)
194
195 /* On Apple platforms, arm64 CPU features can be detected via sysctlbyname(). */
196
197 #include <sys/types.h>
198 #include <sys/sysctl.h>
199
200 static const struct {
201         const char *name;
202         u32 feature;
203 } feature_sysctls[] = {
204         { "hw.optional.arm.FEAT_SHA1",  ARM_CPU_FEATURE_SHA1 },
205 };
206
207 static u32
208 get_cpu_features(void)
209 {
210         u32 features = 0;
211
212         for (size_t i = 0; i < ARRAY_LEN(feature_sysctls); i++) {
213                 const char *name = feature_sysctls[i].name;
214                 u32 val = 0;
215                 size_t valsize = sizeof(val);
216
217                 if (sysctlbyname(name, &val, &valsize, NULL, 0) == 0 &&
218                     valsize == sizeof(val) && val == 1)
219                         features |= feature_sysctls[i].feature;
220         }
221         return features;
222 }
223
224 #elif defined(__aarch64__) && defined(_WIN32)
225
226 #include <windows.h>
227
228 static u32
229 get_cpu_features(void)
230 {
231         u32 features = 0;
232
233         if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
234                 features |= ARM_CPU_FEATURE_SHA1;
235
236         return features;
237 }
238
239 #else
240 #  error "CPU_FEATURES_ENABLED was set but no implementation is available!"
241 #endif
242
243 static const struct {
244         const char *name;
245         u32 feature;
246 } feature_table[] = {
247 #if defined(__i386__) || defined(__x86_64__)
248         {"ssse3",       X86_CPU_FEATURE_SSSE3},
249         {"sse4.1",      X86_CPU_FEATURE_SSE4_1},
250         {"sse4.2",      X86_CPU_FEATURE_SSE4_2},
251         {"avx",         X86_CPU_FEATURE_AVX},
252         {"bmi2",        X86_CPU_FEATURE_BMI2},
253         {"sha",         X86_CPU_FEATURE_SHA},
254         {"sha1",        X86_CPU_FEATURE_SHA},
255 #elif defined(__aarch64__)
256         {"sha1",        ARM_CPU_FEATURE_SHA1},
257 #else
258 #  error "CPU_FEATURES_ENABLED was set but no features are defined!"
259 #endif
260         {"*",           0xFFFFFFFF},
261 };
262
263 static u32
264 find_cpu_feature(const char *name, size_t namelen)
265 {
266         for (size_t i = 0; i < ARRAY_LEN(feature_table); i++) {
267                 if (namelen == strlen(feature_table[i].name) &&
268                     memcmp(name, feature_table[i].name, namelen) == 0)
269                         return feature_table[i].feature;
270         }
271         return 0;
272 }
273
274 u32 cpu_features;
275
276 void init_cpu_features(void)
277 {
278         char *p, *sep;
279
280         cpu_features = get_cpu_features();
281
282         /*
283          * Allow disabling CPU features via an environmental variable for
284          * testing purposes.  Syntax is comma-separated list of feature names.
285          */
286         p = getenv("WIMLIB_DISABLE_CPU_FEATURES");
287         if (likely(p == NULL))
288                 return;
289         for (; (sep = strchr(p, ',')) != NULL; p = sep + 1)
290                 cpu_features &= ~find_cpu_feature(p, sep - p);
291         cpu_features &= ~find_cpu_feature(p, strlen(p));
292 }
293
294 #endif /* CPU_FEATURES_ENABLED */