Skip to content

Commit

Permalink
ArmCPUDetect: Add OpenBSD support for elf_aux_info
Browse files Browse the repository at this point in the history
  • Loading branch information
brad0 committed Dec 30, 2024
1 parent 05cad38 commit 7b7ae81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ endif()
add_definitions(-D__STDC_LIMIT_MACROS)
add_definitions(-D__STDC_CONSTANT_MACROS)

check_symbol_exists(elf_aux_info sys/auxv.h HAVE_ELF_AUX_INFO)
if(HAVE_ELF_AUX_INFO)
add_definitions(-DHAVE_ELF_AUX_INFO)
endif()

add_subdirectory(Core)
if (ANDROID)
add_subdirectory(Android/jni)
Expand Down
31 changes: 24 additions & 7 deletions Source/Core/Common/ArmCPUDetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
#elif defined(__linux__)
#include <asm/hwcap.h>
#include <sys/auxv.h>
#elif defined(__FreeBSD__)
#elif ((defined(__FreeBSD__) || defined(__OpenBSD__)) && \
defined(HAVE_ELF_AUX_INFO))
#include <sys/auxv.h>
#elif defined(__OpenBSD__)
#include <machine/armreg.h>
#include <machine/cpu.h>
#include <sys/sysctl.h>
#endif

#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

#include <fmt/format.h>
Expand Down Expand Up @@ -142,13 +146,14 @@ static bool Read_MIDR_EL1_Sysfs(u64* value)

#endif

#if defined(__linux__) || defined(__FreeBSD__)
#if defined(__linux__) || ((defined(__FreeBSD__) || defined(__OpenBSD__)) && \
defined(HAVE_ELF_AUX_INFO))

static u32 ReadHwCap(u32 type)
{
#if defined(__linux__)
return getauxval(type);
#elif defined(__FreeBSD__)
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
u_long hwcap = 0;
elf_aux_info(type, &hwcap, sizeof(hwcap));
return hwcap;
Expand Down Expand Up @@ -189,7 +194,8 @@ static bool Read_MIDR_EL1(u64* value)

#endif

#if defined(_WIN32) || defined(__linux__) || defined(__FreeBSD__)
#if defined(_WIN32) || defined(__linux__) || ((defined(__FreeBSD__) || \
defined(__OpenBSD__)) && defined(HAVE_ELF_AUX_INFO))

static std::string MIDRToString(u64 midr)
{
Expand Down Expand Up @@ -254,11 +260,22 @@ void CPUInfo::Detect()
{
cpu_id = MIDRToString(reg);
}
#elif defined(__linux__) || defined(__FreeBSD__)
// Linux, Android, and FreeBSD
#elif defined(__linux__) || ((defined(__FreeBSD__) || defined(__OpenBSD__)) && \
defined(HAVE_ELF_AUX_INFO))
// Linux, Android, FreeBSD and OpenBSD with elf_aux_info

#if defined(__FreeBSD__)
SysctlByName(&model_name, "hw.model");
#elif defined(__OpenBSD__)
int mib[2];
size_t len;
char hwmodel[256];

mib[0] = CTL_HW;
mib[1] = HW_MODEL;
len = std::size(hwmodel);
if (sysctl(mib, 2, &hwmodel, &len, nullptr, 0) != -1)
model_name = std::string(hwmodel, len - 1);
#elif defined(__linux__)
if (!ReadDeviceTree(&model_name, "model"))
{
Expand Down

0 comments on commit 7b7ae81

Please sign in to comment.