Skip to content

Commit

Permalink
Add detection hwcap cpuid to hwcaps.h
Browse files Browse the repository at this point in the history
  • Loading branch information
toor1245 committed Sep 15, 2023
1 parent d773ad7 commit 9d6f18a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ cc_library(
],
PLATFORM_CPU_ARM: ["src/impl_arm_linux_or_android.c"],
PLATFORM_CPU_ARM64: [
"src/impl_aarch64_cpuid.c",
"src/impl_aarch64_linux_or_android.c",
"src/impl_aarch64_macos_or_iphone.c",
"src/impl_aarch64_windows.c",
Expand Down
4 changes: 4 additions & 0 deletions include/internal/hwcaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ const char* CpuFeatures_GetPlatformPointer(void);
// Get pointer for the AT_BASE_PLATFORM type.
const char* CpuFeatures_GetBasePlatformPointer(void);

// Checks whether privilege system registers are supported in user mode for
// AArch64.
bool CpuFeatures_IsHwCapCpuidSupported();

CPU_FEATURES_END_CPP_NAMESPACE

#endif // CPU_FEATURES_INCLUDE_INTERNAL_HWCAPS_H_
7 changes: 5 additions & 2 deletions src/hwcaps_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "cpu_features_macros.h"

#ifdef CPU_FEATURES_ARCH_AARCH64
#ifdef CPU_FEATURES_OS_FREEBSD

#include "internal/hwcaps.h"
Expand All @@ -27,6 +26,7 @@ const char* CpuFeatures_GetBasePlatformPointer(void);
#else

#ifdef HAVE_STRONG_ELF_AUX_INFO
#include <stdbool.h>
#include <stddef.h>
#include <sys/auxv.h>

Expand All @@ -46,10 +46,13 @@ HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) {
const char *CpuFeatures_GetPlatformPointer(void) { return NULL; }

const char *CpuFeatures_GetBasePlatformPointer(void) { return NULL; }

bool CpuFeatures_IsHwCapCpuidSupported() {
return GetElfHwcapFromElfAuxInfo(AARCH64_HWCAP_CPUID);
}
#else
#error "FreeBSD needs support for elf_aux_info"
#endif // HAVE_STRONG_ELF_AUX_INFO

#endif // CPU_FEATURES_TEST
#endif // CPU_FEATURES_OS_FREEBSD
#endif // CPU_FEATURES_ARCH_AARCH64
4 changes: 4 additions & 0 deletions src/hwcaps_linux_or_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,9 @@ const char *CpuFeatures_GetBasePlatformPointer(void) {
return (const char *)GetHardwareCapabilitiesFor(AT_BASE_PLATFORM);
}

bool CpuFeatures_IsHwCapCpuidSupported() {
return GetElfHwcapFromGetauxval(AARCH64_HWCAP_CPUID);
}

#endif // CPU_FEATURES_TEST
#endif // defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
2 changes: 1 addition & 1 deletion src/impl_aarch64_cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ uint64_t GetMidrEl1() {
#endif // (defined(CPU_FEATURES_COMPILER_GCC) ||
// defined(CPU_FEATURES_COMPILER_CLANG))
#endif // CPU_FEATURES_OS_FREEBSD
#endif // CPU_FEATURES_ARCH_AARCH64
#endif // CPU_FEATURES_ARCH_AARCH64
22 changes: 8 additions & 14 deletions src/impl_aarch64_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <printf.h>

#include "cpu_features_macros.h"

#ifdef CPU_FEATURES_ARCH_AARCH64
#ifdef CPU_FEATURES_OS_FREEBSD

#include "cpuinfo_aarch64.h"
#include "internal/cpuid_aarch64.h"
#include "impl_aarch64__base_implementation.inl"
#include "sys/auxv.h"
#include "internal/cpuid_aarch64.h"
#include "internal/hwcaps.h"

static const Aarch64Info kEmptyAarch64Info;

Expand All @@ -34,17 +32,13 @@ Aarch64Info GetAarch64Info(void) {
kSetters[i](&info.features, true);
}
}

unsigned long hwcap_cpuid;
elf_aux_info(HWCAP_CPUID, &hwcap_cpuid, sizeof(hwcap_cpuid));

if (hwcap_cpuid) {
info.features.cpuid = true;
info.features.cpuid = CpuFeatures_IsHwCapCpuidSupported();
if (info.features.cpuid) {
const uint64_t midr_el1 = GetMidrEl1();
info.implementer = (int) ExtractBitRange(midr_el1, 31, 24);
info.variant = (int) ExtractBitRange(midr_el1, 23, 20);
info.part = (int) ExtractBitRange(midr_el1, 15, 4);
info.revision = (int) ExtractBitRange(midr_el1, 3, 0);
info.implementer = (int)ExtractBitRange(midr_el1, 31, 24);
info.variant = (int)ExtractBitRange(midr_el1, 23, 20);
info.part = (int)ExtractBitRange(midr_el1, 15, 4);
info.revision = (int)ExtractBitRange(midr_el1, 3, 0);
}
return info;
}
Expand Down

0 comments on commit 9d6f18a

Please sign in to comment.