Botan 3.14.1
Crypto and TLS for C&
cpuid_aarch64.cpp
Go to the documentation of this file.
1/*
2* Runtime CPU detection for Aarch64
3* (C) 2009,2010,2013,2017,2020,2024 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/assert.h>
11#include <botan/internal/target_info.h>
12#include <functional>
13#include <optional>
14
15#if defined(BOTAN_HAS_OS_UTILS)
16 #include <botan/internal/os_utils.h>
17#endif
18
19#if defined(BOTAN_TARGET_OS_HAS_SYSCTLBYNAME)
20 #include <sys/sysctl.h>
21 #include <sys/types.h>
22#endif
23
24#if defined(BOTAN_TARGET_OS_HAS_WIN32)
25 #define NOMINMAX 1
26 #define _WINSOCKAPI_ // stop windows.h including winsock.h
27 #include <windows.h>
28#endif
29
30namespace Botan {
31
32namespace {
33
34std::optional<uint32_t> aarch64_feat_via_auxval(uint32_t allowed) {
35#if defined(BOTAN_HAS_OS_UTILS)
36
37 if(auto auxval = OS::get_auxval_hwcap()) {
38 uint32_t feat = 0;
39
40 /*
41 * On systems with getauxval these bits should normally be defined
42 * in bits/auxv.h but some buggy? glibc installs seem to miss them.
43 * These following values are all fixed, for the Linux ELF format,
44 * so we just hardcode them in ARM_hwcap_bit enum.
45 */
46 enum class ARM_hwcap_bit : uint64_t /* NOLINT(*-enum-size) */ {
47 NEON_bit = (1 << 1),
48 AES_bit = (1 << 3),
49 PMULL_bit = (1 << 4),
50 SHA1_bit = (1 << 5),
51 SHA2_bit = (1 << 6),
52 SHA3_bit = (1 << 17),
53 SM3_bit = (1 << 18),
54 SM4_bit = (1 << 19),
55 SHA2_512_bit = (1 << 21),
56 SVE_bit = (1 << 22),
57 };
58 enum class ARM_hwcap2_bit : uint64_t /* NOLINT(*-enum-size) */ {
59 RNG_bit = (1 << 16),
60 };
61
62 const auto hwcap = auxval->first;
63
64 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::NEON_bit, CPUFeature::Bit::NEON, allowed);
65
66 if((feat & CPUFeature::Bit::NEON) == CPUFeature::Bit::NEON) {
67 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::AES_bit, CPUFeature::Bit::AES, allowed);
68 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::PMULL_bit, CPUFeature::Bit::PMULL, allowed);
69 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::SHA1_bit, CPUFeature::Bit::SHA1, allowed);
70 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::SHA2_bit, CPUFeature::Bit::SHA2, allowed);
71 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::SHA3_bit, CPUFeature::Bit::SHA3, allowed);
72 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::SM3_bit, CPUFeature::Bit::SM3, allowed);
73 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::SM4_bit, CPUFeature::Bit::SM4, allowed);
74 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::SHA2_512_bit, CPUFeature::Bit::SHA2_512, allowed);
75 feat |= CPUID::if_set(hwcap, ARM_hwcap_bit::SVE_bit, CPUFeature::Bit::SVE, allowed);
76 }
77
78 const auto hwcap2 = auxval->second;
79 feat |= CPUID::if_set(hwcap2, ARM_hwcap2_bit::RNG_bit, CPUFeature::Bit::RNG, allowed);
80
81 return feat;
82 }
83#else
84 BOTAN_UNUSED(allowed);
85#endif
86
87 return {};
88}
89
90std::optional<uint32_t> aarch64_feat_using_mac_api(uint32_t allowed) {
91#if defined(BOTAN_TARGET_OS_IS_IOS) || defined(BOTAN_TARGET_OS_IS_MACOS)
92 uint32_t feat = 0;
93
94 auto sysctlbyname_has_feature = [](const char* feature_name) -> bool {
95 unsigned int feature = 0;
96 size_t size = sizeof(feature);
97 if(::sysctlbyname(feature_name, &feature, &size, nullptr, 0) == 0) {
98 return (feature == 1);
99 } else {
100 return false;
101 }
102 };
103
104 // All 64-bit Apple ARM chips have NEON, AES, and SHA support
105 feat |= CPUFeature::Bit::NEON & allowed;
106 if((feat & CPUFeature::Bit::NEON) == CPUFeature::Bit::NEON) {
107 feat |= CPUFeature::Bit::AES & allowed;
108 feat |= CPUFeature::Bit::PMULL & allowed;
109 feat |= CPUFeature::Bit::SHA1 & allowed;
110 feat |= CPUFeature::Bit::SHA2 & allowed;
111
112 if(sysctlbyname_has_feature("hw.optional.armv8_2_sha3")) {
113 feat |= CPUFeature::Bit::SHA3 & allowed;
114 }
115 if(sysctlbyname_has_feature("hw.optional.armv8_2_sha512")) {
116 feat |= CPUFeature::Bit::SHA2_512 & allowed;
117 }
118 }
119
120 return feat;
121#else
122 BOTAN_UNUSED(allowed);
123 return {};
124#endif
125}
126
127std::optional<uint32_t> aarch64_feat_using_win_api(uint32_t allowed) {
128#if defined(BOTAN_TARGET_OS_HAS_WIN32)
129 uint32_t feat = 0;
130
131 // The Windows Aarch64 ABI requires NEON
132 feat |= CPUFeature::Bit::NEON & allowed;
133
134 if((feat & CPUFeature::Bit::NEON) == CPUFeature::Bit::NEON) {
135 if(::IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) != 0) {
136 feat |= CPUFeature::Bit::AES & allowed;
137 feat |= CPUFeature::Bit::PMULL & allowed;
138 feat |= CPUFeature::Bit::SHA1 & allowed;
139 feat |= CPUFeature::Bit::SHA2 & allowed;
140 }
141
142 #if defined(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE)
143 if(::IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE) != 0) {
144 feat |= CPUFeature::Bit::SVE & allowed;
145 }
146 #endif
147 }
148
149 return feat;
150#else
151 BOTAN_UNUSED(allowed);
152 return {};
153#endif
154}
155
156std::optional<uint32_t> aarch64_feat_using_instr_probe(uint32_t allowed) {
157#if defined(BOTAN_USE_GCC_INLINE_ASM) && defined(BOTAN_HAS_OS_UTILS)
158
159 // NOLINTBEGIN(*-no-assembler)
160
161 /*
162 No getauxval API available, fall back on probe functions.
163
164 The probes are written using raw encodings so they assemble even if the
165 assembler is not told about the relevant extension. Each probe clobbers
166 only a caller saved register (v0 or x0).
167 */
168
169 auto neon_probe = []() noexcept -> int {
170 asm volatile("and v0.16b, v0.16b, v0.16b" ::: "v0");
171 return 1;
172 };
173 auto aes_probe = []() noexcept -> int {
174 asm volatile(".inst 0x4e284800" ::: "v0"); // aese v0.16b, v0.16b
175 return 1;
176 };
177 auto pmull_probe = []() noexcept -> int {
178 asm volatile(".inst 0x0ee0e000" ::: "v0"); // pmull v0.1q, v0.1d, v0.1d
179 return 1;
180 };
181 auto sha1_probe = []() noexcept -> int {
182 asm volatile(".inst 0x5e280800" ::: "v0"); // sha1h s0, s0
183 return 1;
184 };
185 auto sha2_probe = []() noexcept -> int {
186 asm volatile(".inst 0x5e282800" ::: "v0"); // sha256su0 v0.4s, v0.4s
187 return 1;
188 };
189 auto sha3_probe = []() noexcept -> int {
190 asm volatile(".inst 0xce000000" ::: "v0"); // eor3 v0.16b, v0.16b, v0.16b, v0.16b
191 return 1;
192 };
193 auto sm3_probe = []() noexcept -> int {
194 asm volatile(".inst 0xce60c000" ::: "v0"); // sm3partw1 v0.4s, v0.4s, v0.4s
195 return 1;
196 };
197 auto sm4_probe = []() noexcept -> int {
198 asm volatile(".inst 0xcec08400" ::: "v0"); // sm4e v0.4s, v0.4s
199 return 1;
200 };
201 auto sha512_probe = []() noexcept -> int {
202 asm volatile(".inst 0xcec08000" ::: "v0"); // sha512su0 v0.2d, v0.2d
203 return 1;
204 };
205 auto sve_probe = []() noexcept -> int {
206 asm volatile(".inst 0x04bf5020" ::: "x0"); // rdvl x0, #1
207 return 1;
208 };
209
210 // NOLINTEND(*-no-assembler)
211
212 // Only run the probe if the feature is allowed; a cleared feature should never be executed
213 auto probe = [allowed](CPUFeature::Bit bit, const std::function<int()>& probe_fn) -> uint32_t {
214 if((allowed & bit) == bit && OS::run_cpu_instruction_probe(probe_fn) == 1) {
215 return bit;
216 }
217 return 0;
218 };
219
220 uint32_t feat = probe(CPUFeature::Bit::NEON, neon_probe);
221
222 if((feat & CPUFeature::Bit::NEON) == CPUFeature::Bit::NEON) {
223 feat |= probe(CPUFeature::Bit::AES, aes_probe);
224 feat |= probe(CPUFeature::Bit::PMULL, pmull_probe);
225 feat |= probe(CPUFeature::Bit::SHA1, sha1_probe);
226 feat |= probe(CPUFeature::Bit::SHA2, sha2_probe);
227 feat |= probe(CPUFeature::Bit::SHA3, sha3_probe);
228 feat |= probe(CPUFeature::Bit::SM3, sm3_probe);
229 feat |= probe(CPUFeature::Bit::SM4, sm4_probe);
230 feat |= probe(CPUFeature::Bit::SHA2_512, sha512_probe);
231 feat |= probe(CPUFeature::Bit::SVE, sve_probe);
232 }
233
234 return feat;
235#else
236 BOTAN_UNUSED(allowed);
237 return {};
238#endif
239}
240
241} // namespace
242
243uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
244 if(auto feat_aux = aarch64_feat_via_auxval(allowed)) {
245 return feat_aux.value();
246 } else if(auto feat_mac = aarch64_feat_using_mac_api(allowed)) {
247 return feat_mac.value();
248 } else if(auto feat_win = aarch64_feat_using_win_api(allowed)) {
249 return feat_win.value();
250 } else if(auto feat_instr = aarch64_feat_using_instr_probe(allowed)) {
251 return feat_instr.value();
252 } else {
253 return 0;
254 }
255}
256
257} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
static uint32_t if_set(uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed)
Definition cpuid.h:116
CPUID::Feature feature()
std::optional< std::pair< unsigned long, unsigned long > > get_auxval_hwcap()
Definition os_utils.cpp:137
int BOTAN_TEST_API run_cpu_instruction_probe(const std::function< int()> &probe_fn)
Definition os_utils.cpp:814