Skip to content

Commit

Permalink
Add aarch64 midr_el1 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
toor1245 committed Sep 15, 2023
1 parent befb237 commit 942aef1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions include/internal/cpuid_aarch64.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>

#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_
39 changes: 39 additions & 0 deletions src/impl_aarch64_cpuid.c
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions src/impl_aarch64_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +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 "cpuid_aarch64.h"
#include "impl_aarch64__base_implementation.inl"

static const Aarch64Info kEmptyAarch64Info;
Expand All @@ -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;
}

Expand Down

0 comments on commit 942aef1

Please sign in to comment.