Botan 3.6.1
Crypto and TLS for C&
cpuid.cpp
Go to the documentation of this file.
1/*
2* Runtime CPU detection
3* (C) 2009,2010,2013,2017,2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/cpuid.h>
9
10#include <botan/exceptn.h>
11#include <botan/types.h>
12#include <botan/internal/os_utils.h>
13#include <botan/internal/parsing.h>
14#include <ostream>
15
16namespace Botan {
17
18//static
19std::string CPUID::to_string() {
20 std::vector<std::string> flags;
21
22 auto append_fn = [&](bool flag, const char* flag_name) {
23 if(flag) {
24 flags.push_back(flag_name);
25 }
26 };
27
28 // NOLINTNEXTLINE(*-macro-usage)
29#define CPUID_PRINT(flag) append_fn(has_##flag(), #flag)
30
31#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
32 CPUID_PRINT(rdtsc);
33
34 CPUID_PRINT(sse2);
35 CPUID_PRINT(ssse3);
36 CPUID_PRINT(avx2);
37
38 CPUID_PRINT(bmi2);
39 CPUID_PRINT(adx);
40 CPUID_PRINT(gfni);
41
42 CPUID_PRINT(aes_ni);
43 CPUID_PRINT(clmul);
44 CPUID_PRINT(rdrand);
45 CPUID_PRINT(rdseed);
46 CPUID_PRINT(intel_sha);
47 CPUID_PRINT(intel_sha512);
48
49 CPUID_PRINT(avx2_vaes);
50 CPUID_PRINT(avx2_clmul);
51
52 CPUID_PRINT(avx512);
53 CPUID_PRINT(avx512_aes);
54 CPUID_PRINT(avx512_clmul);
55
56 CPUID_PRINT(intel_sm3);
57 CPUID_PRINT(intel_sm4);
58
59#elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
60 CPUID_PRINT(altivec);
61 CPUID_PRINT(power_crypto);
62 CPUID_PRINT(darn_rng);
63#elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
64 CPUID_PRINT(neon);
65 CPUID_PRINT(arm_sve);
66
67 CPUID_PRINT(arm_sha1);
68 CPUID_PRINT(arm_sha2);
69 CPUID_PRINT(arm_aes);
70 CPUID_PRINT(arm_pmull);
71 CPUID_PRINT(arm_sha2_512);
72 CPUID_PRINT(arm_sha3);
73 CPUID_PRINT(arm_sm3);
74 CPUID_PRINT(arm_sm4);
75#else
76 BOTAN_UNUSED(append_fn);
77#endif
78
79#undef CPUID_PRINT
80
81 return string_join(flags, ' ');
82}
83
84//static
86 state() = CPUID_Data();
87}
88
89namespace {
90
91// Returns true if big-endian
92bool runtime_check_if_big_endian() {
93 // Check runtime endian
94 const uint32_t endian32 = 0x01234567;
95 const uint8_t* e8 = reinterpret_cast<const uint8_t*>(&endian32);
96
97 bool is_big_endian = false;
98
99 if(e8[0] == 0x01 && e8[1] == 0x23 && e8[2] == 0x45 && e8[3] == 0x67) {
100 is_big_endian = true;
101 } else if(e8[0] == 0x67 && e8[1] == 0x45 && e8[2] == 0x23 && e8[3] == 0x01) {
102 is_big_endian = false;
103 } else {
104 throw Internal_Error("Unexpected endian at runtime, neither big nor little");
105 }
106
107 // If we were compiled with a known endian, verify it matches at runtime
108#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
109 BOTAN_ASSERT(!is_big_endian, "Build and runtime endian match");
110#elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
111 BOTAN_ASSERT(is_big_endian, "Build and runtime endian match");
112#endif
113
114 return is_big_endian;
115}
116
117#if defined(BOTAN_CPUID_HAS_DETECTION)
118uint32_t cleared_cpuid_bits() {
119 uint32_t cleared = 0;
120 std::string clear_cpuid_env;
121 if(OS::read_env_variable(clear_cpuid_env, "BOTAN_CLEAR_CPUID")) {
122 for(const auto& cpuid : split_on(clear_cpuid_env, ',')) {
123 for(auto& bit : CPUID::bit_from_string(cpuid)) {
124 cleared |= bit;
125 }
126 }
127 }
128 return cleared;
129}
130#endif
131
132} // namespace
133
134CPUID::CPUID_Data::CPUID_Data() {
135 m_processor_features = 0;
136
137#if defined(BOTAN_CPUID_HAS_DETECTION)
138 m_processor_features = detect_cpu_features(~cleared_cpuid_bits());
139#endif
140
141 m_processor_features |= CPUID::CPUID_INITIALIZED_BIT;
142
143 if(runtime_check_if_big_endian()) {
144 m_processor_features |= CPUID::CPUID_IS_BIG_ENDIAN_BIT;
145 }
146}
147
148std::vector<CPUID::CPUID_bits> CPUID::bit_from_string(std::string_view tok) {
149#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
150 if(tok == "sse2" || tok == "simd") {
151 return {CPUID::CPUID_SSE2_BIT};
152 } else if(tok == "ssse3") {
153 return {CPUID::CPUID_SSSE3_BIT};
154 } else if(tok == "aesni" || tok == "aes_ni") {
155 // aes_ni is the string printed on the console when running "botan cpuid"
156 return {CPUID::CPUID_AESNI_BIT};
157 } else if(tok == "clmul") {
158 return {CPUID::CPUID_CLMUL_BIT};
159 } else if(tok == "avx2") {
160 return {CPUID::CPUID_AVX2_BIT};
161 } else if(tok == "avx512") {
162 return {CPUID::CPUID_AVX512_BIT};
163 }
164 // there were two if statements testing "sha" and "intel_sha" separately; combined
165 else if(tok == "sha" || tok == "intel_sha") {
166 return {CPUID::CPUID_SHA_BIT};
167 } else if(tok == "rdtsc") {
168 return {CPUID::CPUID_RDTSC_BIT};
169 } else if(tok == "bmi2") {
170 return {CPUID::CPUID_BMI_BIT};
171 } else if(tok == "adx") {
172 return {CPUID::CPUID_ADX_BIT};
173 } else if(tok == "gfni") {
174 return {CPUID::CPUID_GFNI_BIT};
175 } else if(tok == "rdrand") {
176 return {CPUID::CPUID_RDRAND_BIT};
177 } else if(tok == "rdseed") {
178 return {CPUID::CPUID_RDSEED_BIT};
179 } else if(tok == "avx512_aes") {
180 return {CPUID::CPUID_AVX512_AES_BIT};
181 } else if(tok == "avx512_clmul") {
182 return {CPUID::CPUID_AVX512_CLMUL_BIT};
183 } else if(tok == "avx2_vaes") {
184 return {CPUID::CPUID_AVX2_AES_BIT};
185 } else if(tok == "avx2_clmul") {
186 return {CPUID::CPUID_AVX2_CLMUL_BIT};
187 } else if(tok == "intel_sm3") {
188 return {CPUID::CPUID_SM3_BIT};
189 } else if(tok == "intel_sm4") {
190 return {CPUID::CPUID_SM4_BIT};
191 }
192
193#elif defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
194 if(tok == "altivec" || tok == "simd") {
195 return {CPUID::CPUID_ALTIVEC_BIT};
196 } else if(tok == "power_crypto") {
197 return {CPUID::CPUID_POWER_CRYPTO_BIT};
198 } else if(tok == "darn_rng") {
199 return {CPUID::CPUID_DARN_BIT};
200 }
201
202#elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
203 if(tok == "neon" || tok == "simd") {
204 return {CPUID::CPUID_ARM_NEON_BIT};
205 } else if(tok == "arm_sve") {
206 return {CPUID::CPUID_ARM_SVE_BIT};
207 } else if(tok == "armv8sha1" || tok == "arm_sha1") {
208 return {CPUID::CPUID_ARM_SHA1_BIT};
209 } else if(tok == "armv8sha2" || tok == "arm_sha2") {
210 return {CPUID::CPUID_ARM_SHA2_BIT};
211 } else if(tok == "armv8aes" || tok == "arm_aes") {
212 return {CPUID::CPUID_ARM_AES_BIT};
213 } else if(tok == "armv8pmull" || tok == "arm_pmull") {
214 return {CPUID::CPUID_ARM_PMULL_BIT};
215 } else if(tok == "armv8sha3" || tok == "arm_sha3") {
216 return {CPUID::CPUID_ARM_SHA3_BIT};
217 } else if(tok == "armv8sha2_512" || tok == "arm_sha2_512") {
218 return {CPUID::CPUID_ARM_SHA2_512_BIT};
219 } else if(tok == "armv8sm3" || tok == "arm_sm3") {
220 return {CPUID::CPUID_ARM_SM3_BIT};
221 } else if(tok == "armv8sm4" || tok == "arm_sm4") {
222 return {CPUID::CPUID_ARM_SM4_BIT};
223 }
224
225#else
226 BOTAN_UNUSED(tok);
227#endif
228
229 return {};
230}
231
232} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
@ CPUID_INITIALIZED_BIT
Definition cpuid.h:147
@ CPUID_IS_BIG_ENDIAN_BIT
Definition cpuid.h:146
static std::string to_string()
Definition cpuid.cpp:19
static void initialize()
Definition cpuid.cpp:85
static std::vector< CPUID::CPUID_bits > bit_from_string(std::string_view tok)
Definition cpuid.cpp:148
#define CPUID_PRINT(flag)
bool read_env_variable(std::string &value_out, std::string_view var_name)
Definition os_utils.cpp:442
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:111
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition parsing.cpp:140