8#include <botan/internal/cpuid.h>
10#include <botan/assert.h>
11#include <botan/internal/target_info.h>
15#if defined(BOTAN_HAS_OS_UTILS)
16 #include <botan/internal/os_utils.h>
19#if defined(BOTAN_TARGET_OS_HAS_SYSCTLBYNAME)
20 #include <sys/sysctl.h>
21 #include <sys/types.h>
24#if defined(BOTAN_TARGET_OS_HAS_WIN32)
34std::optional<uint32_t> aarch64_feat_via_auxval(uint32_t allowed) {
35#if defined(BOTAN_HAS_OS_UTILS)
46 enum class ARM_hwcap_bit : uint64_t {
55 SHA2_512_bit = (1 << 21),
58 enum class ARM_hwcap2_bit : uint64_t {
62 const auto hwcap = auxval->first;
64 feat |=
CPUID::if_set(hwcap, ARM_hwcap_bit::NEON_bit, CPUFeature::Bit::NEON, allowed);
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);
78 const auto hwcap2 = auxval->second;
79 feat |=
CPUID::if_set(hwcap2, ARM_hwcap2_bit::RNG_bit, CPUFeature::Bit::RNG, allowed);
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)
94 auto sysctlbyname_has_feature = [](
const char* feature_name) ->
bool {
97 if(::sysctlbyname(feature_name, &feature, &size,
nullptr, 0) == 0) {
98 return (feature == 1);
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;
112 if(sysctlbyname_has_feature(
"hw.optional.armv8_2_sha3")) {
113 feat |= CPUFeature::Bit::SHA3 & allowed;
115 if(sysctlbyname_has_feature(
"hw.optional.armv8_2_sha512")) {
116 feat |= CPUFeature::Bit::SHA2_512 & allowed;
127std::optional<uint32_t> aarch64_feat_using_win_api(uint32_t allowed) {
128#if defined(BOTAN_TARGET_OS_HAS_WIN32)
132 feat |= CPUFeature::Bit::NEON & allowed;
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;
142 #if defined(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE)
143 if(::IsProcessorFeaturePresent(PF_ARM_SVE_INSTRUCTIONS_AVAILABLE) != 0) {
144 feat |= CPUFeature::Bit::SVE & allowed;
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)
169 auto neon_probe = []()
noexcept ->
int {
170 asm volatile(
"and v0.16b, v0.16b, v0.16b" :::
"v0");
173 auto aes_probe = []()
noexcept ->
int {
174 asm volatile(
".inst 0x4e284800" :::
"v0");
177 auto pmull_probe = []()
noexcept ->
int {
178 asm volatile(
".inst 0x0ee0e000" :::
"v0");
181 auto sha1_probe = []()
noexcept ->
int {
182 asm volatile(
".inst 0x5e280800" :::
"v0");
185 auto sha2_probe = []()
noexcept ->
int {
186 asm volatile(
".inst 0x5e282800" :::
"v0");
189 auto sha3_probe = []()
noexcept ->
int {
190 asm volatile(
".inst 0xce000000" :::
"v0");
193 auto sm3_probe = []()
noexcept ->
int {
194 asm volatile(
".inst 0xce60c000" :::
"v0");
197 auto sm4_probe = []()
noexcept ->
int {
198 asm volatile(
".inst 0xcec08400" :::
"v0");
201 auto sha512_probe = []()
noexcept ->
int {
202 asm volatile(
".inst 0xcec08000" :::
"v0");
205 auto sve_probe = []()
noexcept ->
int {
206 asm volatile(
".inst 0x04bf5020" :::
"x0");
213 auto probe = [allowed](
CPUFeature::Bit bit,
const std::function<int()>& probe_fn) -> uint32_t {
220 uint32_t feat = probe(CPUFeature::Bit::NEON, neon_probe);
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);
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();
static uint32_t if_set(uint64_t cpuid, T flag, CPUID::Feature bit, uint32_t allowed)
std::optional< std::pair< unsigned long, unsigned long > > get_auxval_hwcap()
int BOTAN_TEST_API run_cpu_instruction_probe(const std::function< int()> &probe_fn)