From eb67ab7971dd9be306e448267affa71b1da0e93d Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 28 May 2025 12:46:26 +0800 Subject: [PATCH 1/2] cpuid_ppc.cpp: fix for macOS --- src/lib/utils/cpuid/cpuid_ppc/cpuid_ppc.cpp | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git src/lib/utils/cpuid/cpuid_ppc/cpuid_ppc.cpp src/lib/utils/cpuid/cpuid_ppc/cpuid_ppc.cpp index 7853e76fe..128fe255c 100644 --- src/lib/utils/cpuid/cpuid_ppc/cpuid_ppc.cpp +++ src/lib/utils/cpuid/cpuid_ppc/cpuid_ppc.cpp @@ -13,11 +13,44 @@ #include #endif +/* +* On macOS and OpenBSD ppc, use sysctl to detect AltiVec +*/ +#if defined(BOTAN_TARGET_OS_IS_MACOS) + #include +#elif defined(BOTAN_TARGET_OS_IS_OPENBSD) + #include + #include + #include +#endif + namespace Botan { uint32_t CPUID::CPUID_Data::detect_cpu_features([[maybe_unused]] uint32_t allowed) { uint32_t feat = 0; +#if defined(BOTAN_TARGET_OS_IS_MACOS) || defined(BOTAN_TARGET_OS_IS_OPENBSD) + // On macOS and OpenBSD, use sysctl + int sels[2] = { + #if defined(BOTAN_TARGET_OS_IS_OPENBSD) + CTL_MACHDEP, CPU_ALTIVEC + #else + CTL_HW, HW_VECTORUNIT + #endif + }; + + int vector_type = 0; + size_t length = sizeof(vector_type); + int error = ::sysctl(sels, 2, &vector_type, &length, NULL, 0); + + if(error == 0 && vector_type > 0) { + feat |= CPUFeature::Bit::ALTIVEC; + } + + return feat; + +#else // defined(BOTAN_TARGET_OS_IS_MACOS) || defined(BOTAN_TARGET_OS_IS_OPENBSD) + #if defined(BOTAN_HAS_OS_UTILS) if(auto auxval = OS::get_auxval_hwcap()) { @@ -80,6 +113,8 @@ uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) { #endif return feat; + +#endif // defined(BOTAN_TARGET_OS_IS_MACOS) || defined(BOTAN_TARGET_OS_IS_OPENBSD) } } // namespace Botan