Debian Codegraph @

crt/aws-c-common/source/arch/intel/cpuid.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 /*
7 * MSVC wants us to use the non-portable _dupenv_s instead; since we need
8 * to remain portable, tell MSVC to suppress this warning.
9 */
10 #define _CRT_SECURE_NO_WARNINGS
11
12 #include <aws/common/cpuid.h>
13 #include <stdlib.h>
14
15 extern void aws_run_cpuid(uint32_t eax, uint32_t ecx, uint32_t *abcd);
16 extern uint64_t aws_run_xgetbv(uint32_t xcr);
17
18 static bool s_cpu_features[AWS_CPU_FEATURE_COUNT];
19 static bool s_cpu_features_cached;
20
21 static void s_cache_cpu_features(void) {
22 /***************************************************************************
23 * First, find the max EAX value we can pass to CPUID without undefined behavior
24 * https://en.wikipedia.org/w/index.php?title=CPUID&oldid=1270569388#EAX=0:_Highest_Function_Parameter_and_Manufacturer_ID
25 **************************************************************************/
26 uint32_t abcd[4];
27 aws_run_cpuid(0x0, 0x0, abcd);
28 const uint32_t max_cpuid_eax_value = abcd[0]; /* max-value = EAX */
29
30 /**************************************************************************
31 * CPUID(EAX=1H, ECX=0H): Processor Info and Feature Bits
32 * https://en.wikipedia.org/w/index.php?title=CPUID&oldid=1270569388#EAX=1:_Processor_Info_and_Feature_Bits
33 **************************************************************************/
34 if (0x1 > max_cpuid_eax_value) {
35 return;
36 }
37 aws_run_cpuid(0x1, 0x0, abcd);
38 s_cpu_features[AWS_CPU_FEATURE_CLMUL] = abcd[2] & (1 << 1); /* pclmulqdq = ECX[bit 1] */
39 s_cpu_features[AWS_CPU_FEATURE_SSE_4_1] = abcd[2] & (1 << 19); /* sse4.1 = ECX[bit 19] */
40 s_cpu_features[AWS_CPU_FEATURE_SSE_4_2] = abcd[2] & (1 << 20); /* sse4.2 = ECX[bit 20] */
41
42 /* NOTE: Even if the AVX flag is set, it's not necessarily usable.
43 * We need to check that OSXSAVE is enabled, and check further capabilities via XGETBV.
44 * GCC had the same bug until 7.4: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85100 */
45 bool avx_usable = false;
46 bool avx512_usable = false;
47 bool feature_osxsave = abcd[2] & (1 << 27); /* osxsave = ECX[bit 27] */
48 if (feature_osxsave) {
49 /* Check XCR0 (Extended Control Register 0) via XGETBV
50 * https://en.wikipedia.org/w/index.php?title=Control_register&oldid=1268423710#XCR0_and_XSS */
51 uint64_t xcr0 = aws_run_xgetbv(0);
52 const uint64_t avx_mask = (1 << 1) /* SSE = XCR0[bit 1] */
53 | (1 << 2) /* AVX = XCR0[bit 2] */;
54 avx_usable = (xcr0 & avx_mask) == avx_mask;
55
56 const uint64_t avx512_mask = (1 << 5) /* OPMASK = XCR0[bit 5] */
57 | (1 << 6) /* ZMM_Hi256 = XCR0[bit 6] */
58 | (1 << 7) /* Hi16_ZMM = XCR0[bit 7] */
59 | avx_mask;
60 avx512_usable = (xcr0 & avx512_mask) == avx512_mask;
61 }
62
63 bool feature_avx = false;
64 if (avx_usable) {
65 feature_avx = abcd[2] & (1 << 28); /* avx = ECX[bit 28] */
66 }
67
68 /***************************************************************************
69 * CPUID(EAX=7H, ECX=0H): Extended Features
70 * https://en.wikipedia.org/w/index.php?title=CPUID&oldid=1270569388#EAX=7,_ECX=0:_Extended_Features
71 **************************************************************************/
72 if (0x7 > max_cpuid_eax_value) {
73 return;
74 }
75 aws_run_cpuid(0x7, 0x0, abcd);
76 s_cpu_features[AWS_CPU_FEATURE_BMI2] = abcd[1] & (1 << 8); /* bmi2 = EBX[bit 8] */
77
78 /* NOTE: It SHOULD be impossible for a CPU to support AVX2 without supporting AVX.
79 * But we've received crash reports where the AVX2 feature check passed
80 * and then an AVX instruction caused an "invalid instruction" crash.
81 *
82 * We diagnosed these machines by asking users to run the sample program from:
83 * https://docs.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?view=msvc-160
84 * and observed the following results:
85 *
86 * AVX not supported
87 * AVX2 supported
88 *
89 * We don't know for sure what was up with those machines, but this extra
90 * check should stop them from running our AVX/AVX2 code paths. */
91 if (feature_avx) {
92 if (avx_usable) {
93 s_cpu_features[AWS_CPU_FEATURE_AVX2] = abcd[1] & (1 << 5); /* AVX2 = EBX[bit 5] */
94 s_cpu_features[AWS_CPU_FEATURE_VPCLMULQDQ] = abcd[2] & (1 << 10); /* vpclmulqdq = ECX[bit 10] */
95 }
96 if (avx512_usable) {
97 s_cpu_features[AWS_CPU_FEATURE_AVX512] = abcd[1] & (1 << 16); /* AVX-512 Foundation = EBX[bit 16] */
98 }
99 }
100 }
101
102 bool aws_cpu_has_feature(enum aws_cpu_feature_name feature_name) {
103 /* Look up and cache all hardware features the first time this is called */
104 if (AWS_UNLIKELY(!s_cpu_features_cached)) {
105 s_cache_cpu_features();
106 s_cpu_features_cached = true;
107 }
108
109 AWS_ASSERT(feature_name >= 0 && feature_name < AWS_CPU_FEATURE_COUNT);
110 return s_cpu_features[feature_name];
111 }
112
113 #define CPUID_AVAILABLE 0
114 #define CPUID_UNAVAILABLE 1
115 static int cpuid_state = 2;
116
117 bool aws_common_private_has_avx2(void) {
118 if (AWS_LIKELY(cpuid_state == 0)) {
119 return true;
120 }
121 if (AWS_LIKELY(cpuid_state == 1)) {
122 return false;
123 }
124
125 /* Provide a hook for testing fallbacks and benchmarking */
126 const char *env_avx2_enabled = getenv("AWS_COMMON_AVX2");
127 if (env_avx2_enabled) {
128 int is_enabled = atoi(env_avx2_enabled);
129 cpuid_state = !is_enabled;
130 return is_enabled;
131 }
132
133 bool available = aws_cpu_has_feature(AWS_CPU_FEATURE_AVX2);
134 cpuid_state = available ? CPUID_AVAILABLE : CPUID_UNAVAILABLE;
135
136 return available;
137 }