Skip to content

Commit

Permalink
Merge pull request #4315 from randombit/jack/fix-auxval
Browse files Browse the repository at this point in the history
Fix aarch64/armv7/ppc64 feature detection for systems with AT_HWCAP != 16
  • Loading branch information
randombit authored Aug 17, 2024
2 parents d463bc0 + c0697e7 commit 56f732f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib/utils/cpuid/cpuid_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
SVE_bit = (1 << 22),
};

const uint64_t hwcap = OS::get_auxval(16); // AT_HWCAP
const uint64_t hwcap = OS::get_auxval(OS::auxval_hwcap());

feat |= if_set(hwcap, ARM_hwcap_bit::NEON_bit, CPUID::CPUID_ARM_NEON_BIT, allowed);

Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/cpuid/cpuid_arm32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
SHA2_bit = (1 << 3),
};

const uint64_t hwcap_neon = OS::get_auxval(16); // AT_HWCAP
const uint64_t hwcap_neon = OS::get_auxval(OS::auxval_hwcap());

feat |= if_set(hwcap_neon, ARM_hwcap_bit::NEON_bit, CPUID::CPUID_ARM_NEON_BIT, allowed);

if(feat & CPUID::CPUID_ARM_NEON_BIT) {
const uint64_t hwcap_crypto = OS::get_auxval(26); // AT_HWCAP2
const uint64_t hwcap_crypto = OS::get_auxval(OS::auxval_hwcap2());

feat |= if_set(hwcap_crypto, ARM_hwcap_bit::AES_bit, CPUID::CPUID_ARM_AES_BIT, allowed);

Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/cpuid/cpuid_ppc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
DARN_bit = (1 << 21),
};

const uint64_t hwcap_altivec = OS::get_auxval(16); // AT_HWCAP
const uint64_t hwcap_altivec = OS::get_auxval(OS::auxval_hwcap());

feat |= if_set(hwcap_altivec, PPC_hwcap_bit::ALTIVEC_bit, CPUID::CPUID_ALTIVEC_BIT, allowed);

#if defined(BOTAN_TARGET_ARCH_IS_PPC64)
if(feat & CPUID::CPUID_ALTIVEC_BIT) {
const uint64_t hwcap_crypto = OS::get_auxval(26); // AT_HWCAP2
const uint64_t hwcap_crypto = OS::get_auxval(OS::auxval_hwcap2());
feat |= if_set(hwcap_crypto, PPC_hwcap_bit::CRYPTO_bit, CPUID::CPUID_POWER_CRYPTO_BIT, allowed);
feat |= if_set(hwcap_crypto, PPC_hwcap_bit::DARN_bit, CPUID::CPUID_DARN_BIT, allowed);
}
Expand Down
20 changes: 20 additions & 0 deletions src/lib/utils/os_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ bool OS::has_auxval() {
#endif
}

unsigned long OS::auxval_hwcap() {
#if defined(AT_HWCAP)
return AT_HWCAP;
#else
// If the value is not defined in a header we can see,
// but auxval is supported, return the Linux/Android value
return (OS::has_auxval()) ? 16 : 0;
#endif
}

unsigned long OS::auxval_hwcap2() {
#if defined(AT_HWCAP2)
return AT_HWCAP2;
#else
// If the value is not defined in a header we can see,
// but auxval is supported, return the Linux/Android value
return (OS::has_auxval()) ? 26 : 0;
#endif
}

unsigned long OS::get_auxval(unsigned long id) {
#if defined(BOTAN_TARGET_OS_HAS_GETAUXVAL)
return ::getauxval(id);
Expand Down
14 changes: 14 additions & 0 deletions src/lib/utils/os_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ size_t BOTAN_TEST_API get_cpu_available();
*/
bool has_auxval();

/**
* If get_auxval is supported, returns the relevant value for AT_HWCAP
*
* If get_auxval is not supported on this system, arbitrarily returns 0
*/
unsigned long auxval_hwcap();

/**
* If get_auxval is supported, returns the relevant value for AT_HWCAP2
*
* If get_auxval is not supported on this system, arbitrarily returns 0
*/
unsigned long auxval_hwcap2();

/**
* Return the ELF auxiliary vector cooresponding to the given ID.
* This only makes sense on Unix-like systems and is currently
Expand Down

0 comments on commit 56f732f

Please sign in to comment.