From 7fc1bfddad13395194c7765fd2490816789aef9c Mon Sep 17 00:00:00 2001 From: Mykola Hohsadze Date: Sun, 17 Sep 2023 15:21:31 +0300 Subject: [PATCH] Add MIDR_EL1 tests --- BUILD.bazel | 5 +++- src/impl_aarch64_cpuid.c | 7 ++--- test/CMakeLists.txt | 1 + test/cpuinfo_aarch64_test.cc | 50 ++++++++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index eb19bbc..031eb63 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -310,7 +310,10 @@ cc_library( "include/internal/windows_utils.h", ], PLATFORM_CPU_ARM: ["include/cpuinfo_arm.h"], - PLATFORM_CPU_ARM64: ["include/cpuinfo_aarch64.h"], + PLATFORM_CPU_ARM64: [ + "include/cpuinfo_aarch64.h", + "include/internal/cpuid_aarch64.h" + ], PLATFORM_CPU_MIPS: ["include/cpuinfo_mips.h"], PLATFORM_CPU_PPC: ["include/cpuinfo_ppc.h"], PLATFORM_CPU_RISCV32: ["include/cpuinfo_riscv.h"], diff --git a/src/impl_aarch64_cpuid.c b/src/impl_aarch64_cpuid.c index 85feb33..156aa41 100644 --- a/src/impl_aarch64_cpuid.c +++ b/src/impl_aarch64_cpuid.c @@ -15,7 +15,8 @@ #include "cpu_features_macros.h" #ifdef CPU_FEATURES_ARCH_AARCH64 -#ifdef CPU_FEATURES_OS_FREEBSD +#if (defined(CPU_FEATURES_OS_FREEBSD) || defined(CPU_FEATURES_OS_LINUX) || \ + defined(CPU_FEATURES_OS_ANDROID)) #if (defined(CPU_FEATURES_COMPILER_GCC) || defined(CPU_FEATURES_COMPILER_CLANG)) #include "internal/cpuid_aarch64.h" @@ -23,7 +24,6 @@ #ifdef CPU_FEATURES_MOCK_CPUID_AARCH64 // Implementation will be provided by test/cpuinfo_aarch64_test.cc. #else - uint64_t GetMidrEl1() { uint64_t midr_el1; __asm("mrs %0, MIDR_EL1" : "=r"(midr_el1)); @@ -35,5 +35,6 @@ uint64_t GetMidrEl1() { #error "Unsupported compiler, aarch64 cpuid requires either GCC or Clang." #endif // (defined(CPU_FEATURES_COMPILER_GCC) || // defined(CPU_FEATURES_COMPILER_CLANG)) -#endif // CPU_FEATURES_OS_FREEBSD +#endif // (defined(CPU_FEATURES_OS_FREEBSD) || defined(CPU_FEATURES_OS_LINUX) + // || defined(CPU_FEATURES_OS_ANDROID)) #endif // CPU_FEATURES_ARCH_AARCH64 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 12f3a21..d2177f2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -77,6 +77,7 @@ endif() if(PROCESSOR_IS_AARCH64) add_executable(cpuinfo_aarch64_test cpuinfo_aarch64_test.cc + ../src/impl_aarch64_cpuid.c ../src/impl_aarch64_linux_or_android.c ../src/impl_aarch64_windows.c ../src/impl_aarch64_macos_or_iphone.c diff --git a/test/cpuinfo_aarch64_test.cc b/test/cpuinfo_aarch64_test.cc index 0c050d6..f982e5c 100644 --- a/test/cpuinfo_aarch64_test.cc +++ b/test/cpuinfo_aarch64_test.cc @@ -22,11 +22,20 @@ #if defined(CPU_FEATURES_OS_WINDOWS) #include "internal/windows_utils.h" #endif // CPU_FEATURES_OS_WINDOWS +#if defined(CPU_FEATURES_OS_FREEBSD) || defined(CPU_FEATURES_OS_LINUX) +#include "internal/cpuid_aarch64.h" +#endif // defined(CPU_FEATURES_OS_FREEBSD) || defined(CPU_FEATURES_OS_LINUX) namespace cpu_features { class FakeCpuAarch64 { -#if defined(CPU_FEATURES_OS_LINUX) - // No particular implementation for Linux as we use /proc/cpuinfo +#if defined(CPU_FEATURES_OS_FREEBSD) || defined(CPU_FEATURES_OS_LINUX) + public: + uint64_t GetMidrEl1() const { return _midr_el1; } + + void SetMidrEl1(uint64_t midr_el1) { _midr_el1 = midr_el1; } + + private: + uint64_t _midr_el1; #elif defined(CPU_FEATURES_OS_MACOS) std::set darwin_sysctlbyname_; std::map darwin_sysctlbynamevalue_; @@ -80,8 +89,8 @@ static FakeCpuAarch64& cpu() { } // Define OS dependent mock functions -#if defined(CPU_FEATURES_OS_LINUX) -// No particular functions to implement for Linux as we use /proc/cpuinfo +#if defined(CPU_FEATURES_OS_FREEBSD) || defined(CPU_FEATURES_OS_LINUX) +extern "C" uint64_t GetMidrEl1() { return cpu().GetMidrEl1(); } #elif defined(CPU_FEATURES_OS_MACOS) extern "C" bool GetDarwinSysCtlByName(const char* name) { return cpu().GetDarwinSysCtlByName(name); @@ -386,6 +395,37 @@ TEST_F(CpuidAarch64Test, WINDOWS_AARCH64_RPI4) { EXPECT_FALSE(info.features.jscvt); EXPECT_FALSE(info.features.lrcpc); } -#endif // CPU_FEATURES_OS_WINDOWS +#elif defined(CPU_FEATURES_OS_FREEBSD) +TEST_F(CpuidAarch64Test, MrsMidrEl1_RPI4) { + ResetHwcaps(); + SetHardwareCapabilities(AARCH64_HWCAP_FP | AARCH64_HWCAP_CPUID, 0); + cpu().SetMidrEl1(0x410FD083); + const auto info = GetAarch64Info(); + + EXPECT_EQ(info.implementer, 0x41); + EXPECT_EQ(info.variant, 0); + EXPECT_EQ(info.part, 0xD08); + EXPECT_EQ(info.revision, 0x3); + + EXPECT_TRUE(info.features.fp); + + EXPECT_FALSE(info.features.dcpodp); + EXPECT_FALSE(info.features.sveaes); + EXPECT_FALSE(info.features.svepmull); + EXPECT_FALSE(info.features.svebitperm); + EXPECT_FALSE(info.features.svesha3); + EXPECT_FALSE(info.features.svesm4); + EXPECT_FALSE(info.features.flagm2); + EXPECT_FALSE(info.features.frint); + EXPECT_FALSE(info.features.svei8mm); + EXPECT_FALSE(info.features.svef32mm); + EXPECT_FALSE(info.features.svef64mm); + EXPECT_FALSE(info.features.svebf16); + EXPECT_FALSE(info.features.i8mm); + EXPECT_FALSE(info.features.bf16); + EXPECT_FALSE(info.features.dgh); + EXPECT_FALSE(info.features.rng); +} +#endif // CPU_FEATURES_OS_FREEBSD } // namespace } // namespace cpu_features