-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathcpufeatures.c
More file actions
651 lines (524 loc) · 21.9 KB
/
cpufeatures.c
File metadata and controls
651 lines (524 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "cpufeatures.h"
#include "cpuid.h"
#if HOST_WINDOWS
#include <Windows.h>
#ifndef PF_ARM_SVE_INSTRUCTIONS_AVAILABLE
#define PF_ARM_SVE_INSTRUCTIONS_AVAILABLE (46)
#endif
#ifndef PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE
#define PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE (47)
#endif
#else // HOST_WINDOWS
#include "minipalconfig.h"
#if HAVE_AUXV_HWCAP_H
#include <sys/auxv.h>
#include <asm/hwcap.h>
// Light-up for hardware capabilities that are not present in older headers used by the portable build.
#ifndef HWCAP_ASIMDRDM
#define HWCAP_ASIMDRDM (1 << 12)
#endif
#ifndef HWCAP_LRCPC
#define HWCAP_LRCPC (1 << 15)
#endif
#ifndef HWCAP_ILRCPC
#define HWCAP_ILRCPC (1 << 26)
#endif
#ifndef HWCAP_ASIMDDP
#define HWCAP_ASIMDDP (1 << 20)
#endif
#ifndef HWCAP_SVE
#define HWCAP_SVE (1 << 22)
#endif
#ifndef HWCAP2_SVE2
#define HWCAP2_SVE2 (1 << 1)
#endif
#endif
#if HAVE_SYSCTLBYNAME
#include <sys/sysctl.h>
#endif
#if HAVE_HWPROBE_H
#include <asm/hwprobe.h>
#include <asm/unistd.h>
#include <unistd.h>
#endif // HAVE_HWPROBE_H
#endif // !HOST_WINDOWS
#if defined(HOST_UNIX)
#if defined(HOST_X86) || defined(HOST_AMD64)
static uint32_t xmmYmmStateSupport()
{
#if defined(HOST_X86)
// We don't support saving XState context on linux-x86 platforms yet, so we
// need to disable any AVX support that uses the extended registers.
return 0;
#else
uint32_t eax;
__asm(" xgetbv\n" \
: "=a"(eax) /*output in eax*/\
: "c"(0) /*inputs - 0 in ecx*/\
: "edx" /* registers that are clobbered*/
);
// check OS has enabled both XMM and YMM state support
return ((eax & 0x06) == 0x06) ? 1 : 0;
#endif // HOST_X86
}
#ifndef XSTATE_MASK_AVX512
#define XSTATE_MASK_AVX512 (0xE0) /* 0b1110_0000 */
#endif // XSTATE_MASK_AVX512
#ifndef XSTATE_MASK_APX
#define XSTATE_MASK_APX (0x80000)
#endif // XSTATE_MASK_APX
static uint32_t avx512StateSupport()
{
#if defined(HOST_APPLE)
// MacOS has specialized behavior where it reports AVX512 support but doesnt
// actually enable AVX512 until the first instruction is executed and does so
// on a per thread basis. It does this by catching the faulting instruction and
// checking for the EVEX encoding. The kmov instructions, despite being part
// of the AVX512 instruction set are VEX encoded and dont trigger the enablement
//
// See https://github.com/apple/darwin-xnu/blob/main/osfmk/i386/fpu.c#L174
// TODO-AVX512: Enabling this for OSX requires ensuring threads explicitly trigger
// the AVX-512 enablement so that arbitrary usage doesn't cause downstream problems
return false;
#else
uint32_t eax;
__asm(" xgetbv\n" \
: "=a"(eax) /*output in eax*/\
: "c"(0) /*inputs - 0 in ecx*/\
: "edx" /* registers that are clobbered*/
);
// check OS has enabled XMM, YMM and ZMM state support
return ((eax & 0xE6) == 0x0E6) ? 1 : 0;
#endif
}
static uint32_t apxStateSupport()
{
#if defined(HOST_APPLE)
return 0;
#elif defined(TARGET_X86)
return 0;
#else
uint32_t eax;
__asm(" xgetbv\n" \
: "=a"(eax) /*output in eax*/\
: "c"(0) /*inputs - 0 in ecx*/\
: "edx" /* registers that are clobbered*/
);
return ((eax & 0x80000) == 0x80000) ? 1 : 0;
#endif // TARGET_AMD64
}
static bool IsAvxEnabled()
{
return true;
}
static bool IsAvx512Enabled()
{
return true;
}
static bool IsApxEnabled()
{
#if defined(TARGET_X86)
return false;
#else
return true;
#endif // TARGET_AMD64
}
#endif // defined(HOST_X86) || defined(HOST_AMD64)
#endif // HOST_UNIX
#if defined(HOST_WINDOWS)
#if defined(HOST_X86) || defined(HOST_AMD64)
static uint32_t xmmYmmStateSupport()
{
// check OS has enabled both XMM and YMM state support
return ((_xgetbv(0) & 0x06) == 0x06) ? 1 : 0;
}
static uint32_t avx512StateSupport()
{
// check OS has enabled XMM, YMM and ZMM state support
return ((_xgetbv(0) & 0xE6) == 0x0E6) ? 1 : 0;
}
static uint32_t apxStateSupport()
{
#if defined(TARGET_X86)
return 0;
#else
return ((_xgetbv(0) & 0x80000) == 0x80000) ? 1 : 0;
#endif
}
static bool IsAvxEnabled()
{
DWORD64 FeatureMask = GetEnabledXStateFeatures();
return ((FeatureMask & XSTATE_MASK_AVX) != 0);
}
static bool IsAvx512Enabled()
{
DWORD64 FeatureMask = GetEnabledXStateFeatures();
return ((FeatureMask & XSTATE_MASK_AVX512) != 0);
}
// TODO-XArch-APX:
// we will eventually need to remove this macro when windows officially supports APX.
#ifndef XSTATE_MASK_APX
#define XSTATE_MASK_APX (0x80000)
#endif // XSTATE_MASK_APX
static bool IsApxEnabled()
{
#ifdef TARGET_X86
return false;
#else
DWORD64 FeatureMask = GetEnabledXStateFeatures();
return ((FeatureMask & XSTATE_MASK_APX) != 0);
#endif
}
#endif // defined(HOST_X86) || defined(HOST_AMD64)
#endif // HOST_WINDOWS
int minipal_getcpufeatures(void)
{
int result = 0;
#if defined(HOST_X86) || defined(HOST_AMD64)
int cpuidInfo[4];
const int CPUID_EAX = 0;
const int CPUID_EBX = 1;
const int CPUID_ECX = 2;
const int CPUID_EDX = 3;
__cpuid(cpuidInfo, 0x00000000);
uint32_t maxCpuId = (uint32_t)cpuidInfo[CPUID_EAX];
assert(maxCpuId >= 1);
__cpuid(cpuidInfo, 0x00000001);
bool hasAvx2Dependencies = false;
bool hasAvx10v1Dependencies = false;
assert((cpuidInfo[CPUID_EDX] & (1 << 25)) != 0); // SSE
assert((cpuidInfo[CPUID_EDX] & (1 << 26)) != 0); // SSE2
if (((cpuidInfo[CPUID_ECX] & (1 << 25)) != 0) && // AESNI
((cpuidInfo[CPUID_ECX] & (1 << 1)) != 0)) // PCLMULQDQ
{
result |= XArchIntrinsicConstants_Aes;
}
if (((cpuidInfo[CPUID_ECX] & (1 << 0)) != 0) && // SSE3
((cpuidInfo[CPUID_ECX] & (1 << 9)) != 0) && // SSSE3
((cpuidInfo[CPUID_ECX] & (1 << 19)) != 0) && // SSE4.1
((cpuidInfo[CPUID_ECX] & (1 << 20)) != 0) && // SSE4.2
((cpuidInfo[CPUID_ECX] & (1 << 23)) != 0)) // POPCNT
{
result |= XArchIntrinsicConstants_Sse42;
if (((cpuidInfo[CPUID_ECX] & (1 << 27)) != 0) && // OSXSAVE
((cpuidInfo[CPUID_ECX] & (1 << 28)) != 0)) // AVX
{
if (IsAvxEnabled() && (xmmYmmStateSupport() == 1)) // XGETBV == 11
{
result |= XArchIntrinsicConstants_Avx;
if (((cpuidInfo[CPUID_ECX] & (1 << 29)) != 0) && // F16C
((cpuidInfo[CPUID_ECX] & (1 << 12)) != 0) && // FMA
((cpuidInfo[CPUID_ECX] & (1 << 22)) != 0)) // MOVBE
{
hasAvx2Dependencies = true;
}
}
}
}
__cpuid(cpuidInfo, 0x80000000);
uint32_t maxCpuIdEx = (uint32_t)cpuidInfo[CPUID_EAX];
if (maxCpuIdEx >= 0x80000001)
{
__cpuid(cpuidInfo, 0x80000001);
if (hasAvx2Dependencies)
{
if ((cpuidInfo[CPUID_ECX] & (1 << 5)) == 0) // LZCNT
{
hasAvx2Dependencies = false;
}
}
}
else
{
hasAvx2Dependencies = false;
}
if (maxCpuId >= 0x07)
{
__cpuidex(cpuidInfo, 0x00000007, 0x00000000);
if ((result & XArchIntrinsicConstants_Avx) != 0)
{
if (((cpuidInfo[CPUID_EBX] & (1 << 5)) != 0) && // AVX2
((cpuidInfo[CPUID_EBX] & (1 << 3)) != 0) && // BMI1
((cpuidInfo[CPUID_EBX] & (1 << 8)) != 0) && // BMI2
hasAvx2Dependencies) // F16C, FMA, LZCNT, MOVBE
{
result |= XArchIntrinsicConstants_Avx2;
if (((cpuidInfo[CPUID_EBX] & (1 << 16)) != 0) && // AVX512F
((cpuidInfo[CPUID_EBX] & (1 << 30)) != 0) && // AVX512BW
((cpuidInfo[CPUID_EBX] & (1 << 28)) != 0) && // AVX512CD
((cpuidInfo[CPUID_EBX] & (1 << 17)) != 0) && // AVX512DQ
((cpuidInfo[CPUID_EBX] & (1 << 31)) != 0)) // AVX512VL
{
if (IsAvx512Enabled() && (avx512StateSupport() == 1)) // XGETBV XRC0[7:5] == 111
{
result |= XArchIntrinsicConstants_Avx512;
if (((cpuidInfo[CPUID_EBX] & (1 << 21)) != 0) && // AVX512-IFMA
((cpuidInfo[CPUID_ECX] & (1 << 1)) != 0)) // AVX512-VBMI
{
result |= XArchIntrinsicConstants_Avx512v2;
if (((cpuidInfo[CPUID_ECX] & (1 << 12)) != 0) && // AVX512-BITALG
((cpuidInfo[CPUID_ECX] & (1 << 6)) != 0) && // AVX512-VBMI2
((cpuidInfo[CPUID_ECX] & (1 << 11)) != 0) && // AVX512-VNNI
((cpuidInfo[CPUID_ECX] & (1 << 14)) != 0)) // AVX512-VPOPCNTDQ
{
result |= XArchIntrinsicConstants_Avx512v3;
if ((cpuidInfo[CPUID_EDX] & (1 << 23)) != 0) // AVX512-FP16
{
hasAvx10v1Dependencies = true;
}
}
}
if ((cpuidInfo[CPUID_EDX] & (1 << 8)) != 0) // AVX512-VP2INTERSECT
{
result |= XArchIntrinsicConstants_Avx512Vp2intersect;
}
}
}
}
else
{
hasAvx2Dependencies = false;
}
if ((result & XArchIntrinsicConstants_Aes) != 0)
{
if (((cpuidInfo[CPUID_ECX] & (1 << 9)) != 0) && // VAES
((cpuidInfo[CPUID_ECX] & (1 << 10)) != 0)) // VPCLMULQDQ
{
result |= XArchIntrinsicConstants_Vaes;
}
}
}
if ((cpuidInfo[CPUID_ECX] & (1 << 8)) != 0) // GFNI
{
result |= XArchIntrinsicConstants_Gfni;
}
if ((cpuidInfo[CPUID_EBX] & (1 << 29)) != 0) // SHA
{
result |= XArchIntrinsicConstants_Sha;
}
if ((cpuidInfo[CPUID_ECX] & (1 << 5)) != 0) // WAITPKG
{
result |= XArchIntrinsicConstants_WaitPkg;
}
if ((cpuidInfo[CPUID_EDX] & (1 << 14)) != 0) // SERIALIZE
{
result |= XArchIntrinsicConstants_X86Serialize;
}
__cpuidex(cpuidInfo, 0x00000007, 0x00000001);
if ((result & XArchIntrinsicConstants_Avx2) != 0)
{
if ((cpuidInfo[CPUID_EAX] & (1 << 4)) != 0) // AVX-VNNI
{
result |= XArchIntrinsicConstants_AvxVnni;
}
if (((cpuidInfo[CPUID_EDX] & (1 << 4)) != 0) && // AVX-VNNI-INT8
((cpuidInfo[CPUID_EDX] & (1 << 10)) != 0)) // AVX-VNNI-INT16
{
result |= XArchIntrinsicConstants_AvxVnniInt;
}
if ((cpuidInfo[CPUID_EAX] & (1 << 23)) != 0) // AVX-IFMA
{
result |= XArchIntrinsicConstants_AvxIfma;
}
if (hasAvx10v1Dependencies)
{
if ((cpuidInfo[CPUID_EAX] & (1 << 5)) == 0) // AVX512-BF16
{
hasAvx10v1Dependencies = false;
}
}
if (IsApxEnabled() && apxStateSupport())
{
if ((cpuidInfo[CPUID_EDX] & (1 << 21)) != 0) // Apx
{
result |= XArchIntrinsicConstants_Apx;
}
}
}
if (maxCpuId >= 0x24)
{
if ((cpuidInfo[CPUID_EDX] & (1 << 19)) != 0) // Avx10
{
// While AVX10 was originally spec'd to allow no V512 support
// this was later changed and all implementations must provide
// V512 support
__cpuidex(cpuidInfo, 0x00000024, 0x00000000);
if (((cpuidInfo[CPUID_EBX] & (1 << 16)) != 0) && // Avx10/V128
((cpuidInfo[CPUID_EBX] & (1 << 17)) != 0) && // Avx10/V256
((cpuidInfo[CPUID_EBX] & (1 << 18)) != 0) && // Avx10/V512
hasAvx10v1Dependencies) // AVX512-BF16, AVX512-FP16
{
uint8_t avx10Version = (uint8_t)(cpuidInfo[CPUID_EBX] & 0xFF);
if (avx10Version >= 1) // Avx10.1
{
result |= XArchIntrinsicConstants_Avx10v1;
}
if (avx10Version >= 2) // Avx10.2
{
result |= XArchIntrinsicConstants_Avx10v2;
}
}
else
{
hasAvx10v1Dependencies = false;
}
}
}
}
#endif // HOST_X86 || HOST_AMD64
#if defined(HOST_ARM64)
#if defined(HOST_UNIX)
#if HAVE_AUXV_HWCAP_H
unsigned long hwCap = getauxval(AT_HWCAP);
assert(hwCap & HWCAP_ASIMD);
if (hwCap & HWCAP_AES)
result |= ARM64IntrinsicConstants_Aes;
if (hwCap & HWCAP_ATOMICS)
result |= ARM64IntrinsicConstants_Atomics;
if (hwCap & HWCAP_CRC32)
result |= ARM64IntrinsicConstants_Crc32;
if (hwCap & HWCAP_ASIMDDP)
result |= ARM64IntrinsicConstants_Dp;
if (hwCap & HWCAP_LRCPC)
result |= ARM64IntrinsicConstants_Rcpc;
if (hwCap & HWCAP_ILRCPC)
result |= ARM64IntrinsicConstants_Rcpc2;
if (hwCap & HWCAP_SHA1)
result |= ARM64IntrinsicConstants_Sha1;
if (hwCap & HWCAP_SHA2)
result |= ARM64IntrinsicConstants_Sha256;
if (hwCap & HWCAP_ASIMDRDM)
result |= ARM64IntrinsicConstants_Rdm;
if (hwCap & HWCAP_SVE)
result |= ARM64IntrinsicConstants_Sve;
unsigned long hwCap2 = getauxval(AT_HWCAP2);
if (hwCap2 & HWCAP2_SVE2)
result |= ARM64IntrinsicConstants_Sve2;
#else // !HAVE_AUXV_HWCAP_H
#if HAVE_SYSCTLBYNAME
int64_t valueFromSysctl = 0;
size_t sz = sizeof(valueFromSysctl);
if ((sysctlbyname("hw.optional.arm.FEAT_AES", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Aes;
if ((sysctlbyname("hw.optional.armv8_crc32", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Crc32;
if ((sysctlbyname("hw.optional.arm.FEAT_DotProd", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Dp;
if ((sysctlbyname("hw.optional.arm.FEAT_RDM", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Rdm;
if ((sysctlbyname("hw.optional.arm.FEAT_SHA1", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Sha1;
if ((sysctlbyname("hw.optional.arm.FEAT_SHA256", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Sha256;
if ((sysctlbyname("hw.optional.armv8_1_atomics", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Atomics;
if ((sysctlbyname("hw.optional.arm.FEAT_LRCPC", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Rcpc;
if ((sysctlbyname("hw.optional.arm.FEAT_LRCPC2", &valueFromSysctl, &sz, NULL, 0) == 0) && (valueFromSysctl != 0))
result |= ARM64IntrinsicConstants_Rcpc2;
#endif // HAVE_SYSCTLBYNAME
#endif // HAVE_AUXV_HWCAP_H
#endif // HOST_UNIX
#if defined(HOST_WINDOWS)
if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
{
result |= ARM64IntrinsicConstants_Aes;
result |= ARM64IntrinsicConstants_Sha1;
result |= ARM64IntrinsicConstants_Sha256;
}
if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
{
result |= ARM64IntrinsicConstants_Crc32;
}
if (IsProcessorFeaturePresent(PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE))
{
result |= ARM64IntrinsicConstants_Atomics;
}
if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
{
result |= ARM64IntrinsicConstants_Dp;
// IsProcessorFeaturePresent does not have a dedicated flag for RDM, so we enable it by implication.
// 1) DP is an optional instruction set for Armv8.2, which may be included only in processors implementing at least Armv8.1.
// 2) Armv8.1 requires RDM when AdvSIMD is implemented, and AdvSIMD is a baseline requirement of .NET.
//
// Therefore, by documented standard, DP cannot exist here without RDM. In practice, there is only one CPU supported
// by Windows that includes RDM without DP, so this implication also has little practical chance of a false negative.
//
// See: https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Understanding%20the%20Armv8.x%20extensions.pdf
// https://developer.arm.com/documentation/109697/2024_09/Feature-descriptions/The-Armv8-1-architecture-extension
result |= ARM64IntrinsicConstants_Rdm;
}
if (IsProcessorFeaturePresent(PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE))
{
result |= ARM64IntrinsicConstants_Rcpc;
}
// TODO: IsProcessorFeaturePresent doesn't support LRCPC2 yet.
if (IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE))
{
result |= ARM64IntrinsicConstants_Sve;
}
if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE))
{
result |= ARM64IntrinsicConstants_Sve2;
}
#endif // HOST_WINDOWS
#endif // HOST_ARM64
#if defined(HOST_RISCV64)
#if defined(HOST_UNIX)
#if HAVE_HWPROBE_H
struct riscv_hwprobe pairs[1] = {{RISCV_HWPROBE_KEY_IMA_EXT_0, 0}};
if (syscall(__NR_riscv_hwprobe, pairs, 1, 0, NULL, 0) == 0)
{
// Our baseline support is for RV64GC (see #73437)
assert(pairs[0].value & RISCV_HWPROBE_IMA_FD);
assert(pairs[0].value & RISCV_HWPROBE_IMA_C);
if (pairs[0].value & RISCV_HWPROBE_EXT_ZBA)
{
result |= RiscV64IntrinsicConstants_Zba;
}
if (pairs[0].value & RISCV_HWPROBE_EXT_ZBB)
{
result |= RiscV64IntrinsicConstants_Zbb;
}
}
#endif // HAVE_HWPROBE_H
#endif // HOST_UNIX
#endif // HOST_RISCV64
return result;
}
// Detect if the current process is running under the Apple Rosetta x64 emulator
bool minipal_detect_rosetta(void)
{
#if defined(HOST_AMD64) || defined(HOST_X86)
// Check for CPU brand indicating emulation
int regs[4];
char brand[49];
// Get the maximum value for extended function CPUID info
__cpuid(regs, (int)0x80000000);
if ((unsigned int)regs[0] < 0x80000004)
{
return false; // Extended CPUID not supported
}
// Retrieve the CPU brand string
for (unsigned int i = 0x80000002; i <= 0x80000004; ++i)
{
__cpuid(regs, (int)i);
memcpy(brand + (i - 0x80000002) * sizeof(regs), regs, sizeof(regs));
}
brand[sizeof(brand) - 1] = '\0';
// Check if CPU brand indicates emulation
if (strstr(brand, "VirtualApple") != NULL)
{
return true;
}
#endif // HOST_AMD64 || HOST_X86
return false;
}