dolphin/Source/Core/Common/ArmCPUDetect.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

305 lines
7.3 KiB
C++
Raw Normal View History

// Copyright 2013 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2013-02-26 20:49:00 +01:00
2021-12-10 03:22:16 +01:00
#include "Common/CPUDetect.h"
#include <cstring>
2014-06-03 01:27:50 +02:00
#include <fstream>
#include <sstream>
#include <string>
2019-11-26 05:31:45 +01:00
#include <thread>
#ifdef __APPLE__
#include <sys/sysctl.h>
#elif defined(_WIN32)
#include <Windows.h>
#include <arm64intr.h>
#include "Common/WindowsRegistry.h"
#else
2020-08-27 22:54:04 +02:00
#ifndef __FreeBSD__
2019-11-26 05:31:45 +01:00
#include <asm/hwcap.h>
2020-08-27 22:54:04 +02:00
#endif
#include <sys/auxv.h>
2019-11-26 05:31:45 +01:00
#endif
2014-06-03 01:27:50 +02:00
#include <fmt/format.h>
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"
#if defined(__APPLE__) || defined(__FreeBSD__)
static bool SysctlByName(std::string* value, const std::string& name)
{
size_t value_len = 0;
if (sysctlbyname(name.c_str(), nullptr, &value_len, nullptr, 0))
return false;
2013-02-26 20:49:00 +01:00
value->resize(value_len);
if (sysctlbyname(name.c_str(), value->data(), &value_len, nullptr, 0))
return false;
2019-11-26 05:31:45 +01:00
TruncateToCString(value);
return true;
}
#endif
2013-02-26 20:49:00 +01:00
#if defined(_WIN32)
static constexpr char SUBKEY_CORE0[] = R"(HARDWARE\DESCRIPTION\System\CentralProcessor\0)";
// Identifier: human-readable version of CPUID
// ProcessorNameString: marketing name of the processor
// VendorIdentifier: vendor company name
// There are some other maybe-interesting values nearby, BIOS info etc.
static bool ReadProcessorString(std::string* value, const std::string& name)
2013-02-26 20:49:00 +01:00
{
return WindowsRegistry::ReadValue(value, SUBKEY_CORE0, name);
}
// Read cached register values from the registry
static bool ReadPrivilegedCPReg(u64* value, u32 reg)
{
// Not sure if the value name is padded or not
return WindowsRegistry::ReadValue(value, SUBKEY_CORE0, fmt::format("CP {:x}", reg).c_str());
}
static bool Read_MIDR_EL1(u64* value)
{
return ReadPrivilegedCPReg(value, ARM64_SYSREG(0b11, 0, 0, 0b0000, 0));
}
static bool Read_ID_AA64ISAR0_EL1(u64* value)
{
return ReadPrivilegedCPReg(value, ARM64_SYSREG(0b11, 0, 0, 0b0110, 0));
}
static bool Read_ID_AA64MMFR1_EL1(u64* value)
{
return ReadPrivilegedCPReg(value, ARM64_SYSREG(0b11, 0, 0, 0b0111, 1));
}
#endif
#if defined(__linux__)
static bool ReadDeviceTree(std::string* value, const std::string& name)
{
const std::string path = std::string("/proc/device-tree/") + name;
std::ifstream file;
File::OpenFStream(file, path.c_str(), std::ios_base::in);
if (!file)
return false;
2014-06-03 01:27:50 +02:00
file >> *value;
return true;
}
static std::string ReadCpuinfoField(const std::string& field)
{
std::string line;
std::ifstream file;
File::OpenFStream(file, "/proc/cpuinfo", std::ios_base::in);
2014-06-03 01:27:50 +02:00
if (!file)
return {};
2014-06-03 01:27:50 +02:00
while (std::getline(file, line))
2013-02-26 20:49:00 +01:00
{
if (!line.starts_with(field))
continue;
auto non_tab = line.find_first_not_of("\t", field.length());
if (non_tab == line.npos)
continue;
if (line[non_tab] != ':')
continue;
auto value_start = line.find_first_not_of(" ", non_tab + 1);
if (value_start == line.npos)
continue;
return line.substr(value_start);
2013-02-26 20:49:00 +01:00
}
return {};
}
2014-06-03 01:27:50 +02:00
static bool Read_MIDR_EL1_Sysfs(u64* value)
{
std::ifstream file;
File::OpenFStream(file, "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1",
std::ios_base::in);
if (!file)
return false;
file >> std::hex >> *value;
return true;
}
#endif
#if defined(__linux__) || defined(__FreeBSD__)
static u32 ReadHwCap(u32 type)
{
#if defined(__linux__)
return getauxval(type);
#elif defined(__FreeBSD__)
u_long hwcap = 0;
elf_aux_info(type, &hwcap, sizeof(hwcap));
return hwcap;
#endif
}
// For "Direct" reads, value gets filled via emulation, hence:
// "there is no guarantee that the value reflects the processor that it is currently executing on"
// On big.LITTLE systems, the value may be unrelated to the core this is invoked on, and unless
// other measures are taken, executing the instruction may cause the caller to be switched onto a
// different core when it resumes (and of course, caller could be preempted at any other time as
// well).
static inline u64 Read_MIDR_EL1_Direct()
{
u64 value;
__asm__ __volatile__("mrs %0, MIDR_EL1" : "=r"(value));
return value;
}
static bool Read_MIDR_EL1(u64* value)
{
#ifdef __linux__
if (Read_MIDR_EL1_Sysfs(value))
return true;
#endif
bool id_reg_user_access = ReadHwCap(AT_HWCAP) & HWCAP_CPUID;
#ifdef __FreeBSD__
// FreeBSD kernel has support but doesn't seem to indicate it?
// see user_mrs_handler
id_reg_user_access = true;
#endif
if (!id_reg_user_access)
return false;
*value = Read_MIDR_EL1_Direct();
return true;
}
#endif
#ifndef __APPLE__
static std::string MIDRToString(u64 midr)
{
u8 implementer = (midr >> 24) & 0xff;
u8 variant = (midr >> 20) & 0xf;
u8 arch = (midr >> 16) & 0xf;
u16 part_num = (midr >> 4) & 0xfff;
u8 revision = midr & 0xf;
return fmt::format("{:02X}:{:X}:{:04b}:{:03X}:{:X}", implementer, variant, arch, part_num,
revision);
2013-02-26 20:49:00 +01:00
}
2019-11-26 05:31:45 +01:00
#endif
2013-02-26 20:49:00 +01:00
CPUInfo cpu_info;
2014-06-03 01:27:50 +02:00
CPUInfo::CPUInfo()
{
2013-02-26 20:49:00 +01:00
Detect();
}
void CPUInfo::Detect()
{
vendor = CPUVendor::ARM;
bFMA = true;
bFlushToZero = true;
num_cores = std::max(static_cast<int>(std::thread::hardware_concurrency()), 1);
#ifdef __APPLE__
SysctlByName(&model_name, "machdep.cpu.brand_string");
// M-series CPUs have all of these
// Apparently the world has accepted that these can be assumed supported "for all time".
// see https://github.com/golang/go/issues/42747
bAES = true;
bSHA1 = true;
bSHA2 = true;
bCRC32 = true;
#elif defined(_WIN32)
// NOTE All this info is from cpu core 0 only.
ReadProcessorString(&model_name, "ProcessorNameString");
u64 reg = 0;
// Attempt to be forward-compatible: perform inverted check against disabled feature states.
if (Read_ID_AA64ISAR0_EL1(&reg))
{
bAES = ((reg >> 4) & 0xf) != 0;
bSHA1 = ((reg >> 8) & 0xf) != 0;
bSHA2 = ((reg >> 12) & 0xf) != 0;
bCRC32 = ((reg >> 16) & 0xf) != 0;
}
if (Read_ID_AA64MMFR1_EL1(&reg))
{
// Introduced in Armv8.7, where AFP must be supported if AdvSIMD and FP both are.
bAFP = ((reg >> 44) & 0xf) != 0;
}
// Pre-decoded MIDR_EL1 could be read with ReadProcessorString(.., "Identifier"),
// but we want format to match across all platforms where possible.
if (Read_MIDR_EL1(&reg))
{
cpu_id = MIDRToString(reg);
}
2020-08-27 22:54:04 +02:00
#else
// Linux, Android, and FreeBSD
#if defined(__FreeBSD__)
SysctlByName(&model_name, "hw.model");
#elif defined(__linux__)
if (!ReadDeviceTree(&model_name, "model"))
{
// This doesn't seem to work on modern arm64 kernels
model_name = ReadCpuinfoField("Hardware");
}
2020-08-27 22:54:04 +02:00
#endif
const u32 hwcap = ReadHwCap(AT_HWCAP);
bAES = hwcap & HWCAP_AES;
bCRC32 = hwcap & HWCAP_CRC32;
bSHA1 = hwcap & HWCAP_SHA1;
bSHA2 = hwcap & HWCAP_SHA2;
#if defined(AT_HWCAP2) && defined(HWCAP2_AFP)
const u32 hwcap2 = ReadHwCap(AT_HWCAP2);
bAFP = hwcap2 & HWCAP2_AFP;
2019-11-26 05:31:45 +01:00
#endif
u64 midr = 0;
if (Read_MIDR_EL1(&midr))
{
cpu_id = MIDRToString(midr);
}
#endif
model_name = ReplaceAll(model_name, ",", "_");
cpu_id = ReplaceAll(cpu_id, ",", "_");
2013-02-26 20:49:00 +01:00
}
std::string CPUInfo::Summarize()
{
std::vector<std::string> sum;
sum.push_back(model_name);
sum.push_back(cpu_id);
if (bAFP)
sum.push_back("AFP");
if (bAES)
sum.push_back("AES");
if (bCRC32)
sum.push_back("CRC32");
if (bSHA1)
sum.push_back("SHA1");
if (bSHA2)
sum.push_back("SHA2");
return JoinStrings(sum, ",");
2013-02-26 20:49:00 +01:00
}