diff --git a/BUILD.bazel b/BUILD.bazel index 14b148a1..2097a51c 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -244,7 +244,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/cpuid_aarch64.h", + ], PLATFORM_CPU_MIPS: ["include/cpuinfo_mips.h"], PLATFORM_CPU_PPC: ["include/cpuinfo_ppc.h"], PLATFORM_CPU_RISCV32: ["include/cpuinfo_riscv.h"], @@ -288,6 +291,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", diff --git a/CMakeLists.txt b/CMakeLists.txt index a070e944..5b513493 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,7 @@ macro(add_cpu_features_headers_and_sources HDRS_LIST_NAME SRCS_LIST_NAME) list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_arm.h) elseif(PROCESSOR_IS_AARCH64) list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_aarch64.h) + list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/internal/cpuid_aarch64.h) list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/internal/windows_utils.h) elseif(PROCESSOR_IS_X86) list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_x86.h) diff --git a/include/internal/cpuid_aarch64.h b/include/internal/cpuid_aarch64.h new file mode 100644 index 00000000..ca70fb0a --- /dev/null +++ b/include/internal/cpuid_aarch64.h @@ -0,0 +1,28 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef CPU_FEATURES_INCLUDE_CPUID_AARCH64_H_ +#define CPU_FEATURES_INCLUDE_CPUID_AARCH64_H_ + +#include + +#include "cpu_features_macros.h" + +CPU_FEATURES_START_CPP_NAMESPACE + +uint64_t GetMidrEl1(); + +CPU_FEATURES_END_CPP_NAMESPACE + +#endif // CPU_FEATURES_INCLUDE_CPUID_AARCH64_H_ diff --git a/src/impl_aarch64_cpuid.c b/src/impl_aarch64_cpuid.c new file mode 100644 index 00000000..6f7c3697 --- /dev/null +++ b/src/impl_aarch64_cpuid.c @@ -0,0 +1,39 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "cpu_features_macros.h" + +#ifdef CPU_FEATURES_ARCH_AARCH64 +#ifdef CPU_FEATURES_OS_FREEBSD +#if (defined(CPU_FEATURES_COMPILER_GCC) || defined(CPU_FEATURES_COMPILER_CLANG)) + +#include "internal/cpuid_aarch64.h" + +#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)); + return midr_el1; +} +#endif // CPU_FEATURES_MOCK_CPUID_AARCH64 + +#else +#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 // CPU_FEATURES_ARCH_AARCH64 \ No newline at end of file diff --git a/src/impl_aarch64_freebsd.c b/src/impl_aarch64_freebsd.c index 7caada3e..6fb03da4 100644 --- a/src/impl_aarch64_freebsd.c +++ b/src/impl_aarch64_freebsd.c @@ -12,12 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #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" static const Aarch64Info kEmptyAarch64Info; @@ -30,6 +33,14 @@ Aarch64Info GetAarch64Info(void) { kSetters[i](&info.features, true); } } + printf("cpuid is supported: %d", info.features.cpuid); + if (info.features.cpuid) { + const uint64_t midr_el1 = GetMidrEl1(); + info.implementer = ExtractBitRange(midr_el1, 31, 24); + info.variant = ExtractBitRange(midr_el1, 23, 20); + info.part = ExtractBitRange(midr_el1, 15, 4); + info.revision = ExtractBitRange(midr_el1, 3, 0); + } return info; }