| 1 | // Copyright 2013 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/CPUDetect.h" |
| 5 | |
| 6 | #include <cstring> |
| 7 | #include <fstream> |
| 8 | #include <sstream> |
| 9 | #include <string> |
| 10 | #include <thread> |
| 11 | |
| 12 | #ifdef __APPLE__ |
| 13 | #include <sys/sysctl.h> |
| 14 | #elif defined(_WIN32) |
| 15 | #include <Windows.h> |
| 16 | #include <arm64intr.h> |
| 17 | #include "Common/WindowsRegistry.h" |
| 18 | #elif defined(__linux__) |
| 19 | #include <asm/hwcap.h> |
| 20 | #include <sys/auxv.h> |
| 21 | #elif defined(HAVE_ELF_AUX_INFO) |
| 22 | #include <sys/auxv.h> |
| 23 | #elif defined(__OpenBSD__) |
| 24 | #include <machine/armreg.h> |
| 25 | #include <machine/cpu.h> |
| 26 | #endif |
| 27 | |
| 28 | #ifdef __OpenBSD__ |
| 29 | // clang-format off |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/sysctl.h> |
| 32 | // clang-format on |
| 33 | #endif |
| 34 | |
| 35 | #include <fmt/format.h> |
| 36 | #include <fmt/ranges.h> |
| 37 | |
| 38 | #include "Common/CommonTypes.h" |
| 39 | #include "Common/FileUtil.h" |
| 40 | #include "Common/StringUtil.h" |
| 41 | |
| 42 | #if defined(__APPLE__) || defined(__FreeBSD__) |
| 43 | |
| 44 | static bool SysctlByName(std::string* value, const std::string& name) |
| 45 | { |
| 46 | size_t value_len = 0; |
| 47 | if (sysctlbyname(name.c_str(), nullptr, &value_len, nullptr, 0)) |
| 48 | return false; |
| 49 | |
| 50 | value->resize(value_len); |
| 51 | if (sysctlbyname(name.c_str(), value->data(), &value_len, nullptr, 0)) |
| 52 | return false; |
| 53 | |
| 54 | TruncateToCString(value); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | #endif |
| 59 | |
| 60 | #if defined(_WIN32) |
| 61 | |
| 62 | static constexpr char SUBKEY_CORE0[] = R"(HARDWARE\DESCRIPTION\System\CentralProcessor\0)"; |
| 63 | |
| 64 | // Identifier: human-readable version of CPUID |
| 65 | // ProcessorNameString: marketing name of the processor |
| 66 | // VendorIdentifier: vendor company name |
| 67 | // There are some other maybe-interesting values nearby, BIOS info etc. |
| 68 | static bool ReadProcessorString(std::string* value, const std::string& name) |
| 69 | { |
| 70 | return WindowsRegistry::ReadValue(value, SUBKEY_CORE0, name); |
| 71 | } |
| 72 | |
| 73 | // Read cached register values from the registry |
| 74 | static bool ReadPrivilegedCPReg(u64* value, u32 reg) |
| 75 | { |
| 76 | // Not sure if the value name is padded or not |
| 77 | return WindowsRegistry::ReadValue(value, SUBKEY_CORE0, fmt::format("CP {:x}", reg).c_str()); |
| 78 | } |
| 79 | |
| 80 | static bool Read_MIDR_EL1(u64* value) |
| 81 | { |
| 82 | return ReadPrivilegedCPReg(value, ARM64_SYSREG(0b11, 0, 0, 0b0000, 0)); |
| 83 | } |
| 84 | |
| 85 | static bool Read_ID_AA64ISAR0_EL1(u64* value) |
| 86 | { |
| 87 | return ReadPrivilegedCPReg(value, ARM64_SYSREG(0b11, 0, 0, 0b0110, 0)); |
| 88 | } |
| 89 | |
| 90 | static bool Read_ID_AA64MMFR1_EL1(u64* value) |
| 91 | { |
| 92 | return ReadPrivilegedCPReg(value, ARM64_SYSREG(0b11, 0, 0, 0b0111, 1)); |
| 93 | } |
| 94 | |
| 95 | #endif |
| 96 | |
| 97 | #if defined(__linux__) |
| 98 | |
| 99 | static bool ReadDeviceTree(std::string* value, const std::string& name) |
| 100 | { |
| 101 | const std::string path = std::string("/proc/device-tree/") + name; |
| 102 | std::ifstream file; |
| 103 | File::OpenFStream(file, path.c_str(), std::ios_base::in); |
| 104 | if (!file) |
| 105 | return false; |
| 106 | |
| 107 | file >> *value; |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | static std::string ReadCpuinfoField(const std::string& field) |
| 112 | { |
| 113 | std::string line; |
| 114 | std::ifstream file; |
| 115 | File::OpenFStream(file, "/proc/cpuinfo", std::ios_base::in); |
| 116 | if (!file) |
| 117 | return {}; |
| 118 | |
| 119 | while (std::getline(file, line)) |
| 120 | { |
| 121 | if (!line.starts_with(field)) |
| 122 | continue; |
| 123 | auto non_tab = line.find_first_not_of("\t", field.length()); |
| 124 | if (non_tab == line.npos) |
| 125 | continue; |
| 126 | if (line[non_tab] != ':') |
| 127 | continue; |
| 128 | auto value_start = line.find_first_not_of(" ", non_tab + 1); |
| 129 | if (value_start == line.npos) |
| 130 | continue; |
| 131 | return line.substr(value_start); |
| 132 | } |
| 133 | return {}; |
| 134 | } |
| 135 | |
| 136 | static bool Read_MIDR_EL1_Sysfs(u64* value) |
| 137 | { |
| 138 | std::ifstream file; |
| 139 | File::OpenFStream(file, "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1", |
| 140 | std::ios_base::in); |
| 141 | if (!file) |
| 142 | return false; |
| 143 | |
| 144 | file >> std::hex >> *value; |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | #endif |
| 149 | |
| 150 | #if defined(__linux__) || defined(HAVE_ELF_AUX_INFO) |
| 151 | |
| 152 | static u32 ReadHwCap(u32 type) |
| 153 | { |
| 154 | #if defined(__linux__) |
| 155 | return getauxval(type); |
| 156 | #elif defined(HAVE_ELF_AUX_INFO) |
| 157 | u_long hwcap = 0; |
| 158 | elf_aux_info(type, &hwcap, sizeof(hwcap)); |
| 159 | return hwcap; |
| 160 | #endif |
| 161 | } |
| 162 | |
| 163 | // For "Direct" reads, value gets filled via emulation, hence: |
| 164 | // "there is no guarantee that the value reflects the processor that it is currently executing on" |
| 165 | // On big.LITTLE systems, the value may be unrelated to the core this is invoked on, and unless |
| 166 | // other measures are taken, executing the instruction may cause the caller to be switched onto a |
| 167 | // different core when it resumes (and of course, caller could be preempted at any other time as |
| 168 | // well). |
| 169 | static inline u64 Read_MIDR_EL1_Direct() |
| 170 | { |
| 171 | u64 value; |
| 172 | __asm__ __volatile__("mrs %0, MIDR_EL1" : "=r"(value)); |
| 173 | return value; |
| 174 | } |
| 175 | |
| 176 | static bool Read_MIDR_EL1(u64* value) |
| 177 | { |
| 178 | #ifdef __linux__ |
| 179 | if (Read_MIDR_EL1_Sysfs(value)) |
| 180 | return true; |
| 181 | #endif |
| 182 | |
| 183 | bool id_reg_user_access = ReadHwCap(AT_HWCAP) & HWCAP_CPUID; |
| 184 | #ifdef __FreeBSD__ |
| 185 | // FreeBSD kernel has support but doesn't seem to indicate it? |
| 186 | // see user_mrs_handler |
| 187 | id_reg_user_access = true; |
| 188 | #endif |
| 189 | if (!id_reg_user_access) |
| 190 | return false; |
| 191 | *value = Read_MIDR_EL1_Direct(); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | #endif |
| 196 | |
| 197 | #if defined(_WIN32) || defined(__linux__) || defined(HAVE_ELF_AUX_INFO) |
| 198 | |
| 199 | static std::string MIDRToString(u64 midr) |
| 200 | { |
| 201 | u8 implementer = (midr >> 24) & 0xff; |
| 202 | u8 variant = (midr >> 20) & 0xf; |
| 203 | u8 arch = (midr >> 16) & 0xf; |
| 204 | u16 part_num = (midr >> 4) & 0xfff; |
| 205 | u8 revision = midr & 0xf; |
| 206 | return fmt::format("{:02X}:{:X}:{:04b}:{:03X}:{:X}", implementer, variant, arch, part_num, |
| 207 | revision); |
| 208 | } |
| 209 | |
| 210 | #endif |
| 211 | |
| 212 | CPUInfo cpu_info; |
| 213 | |
| 214 | CPUInfo::CPUInfo() |
| 215 | { |
| 216 | Detect(); |
| 217 | } |
| 218 | |
| 219 | void CPUInfo::Detect() |
| 220 | { |
| 221 | vendor = CPUVendor::ARM; |
| 222 | bFMA = true; |
| 223 | bFlushToZero = true; |
| 224 | |
| 225 | num_cores = std::max(static_cast<int>(std::thread::hardware_concurrency()), 1); |
| 226 | |
| 227 | #ifdef __APPLE__ |
| 228 | SysctlByName(&model_name, "machdep.cpu.brand_string"); |
| 229 | |
| 230 | // M-series CPUs have all of these |
| 231 | // Apparently the world has accepted that these can be assumed supported "for all time". |
| 232 | // see https://github.com/golang/go/issues/42747 |
| 233 | bAES = true; |
| 234 | bSHA1 = true; |
| 235 | bSHA2 = true; |
| 236 | bCRC32 = true; |
| 237 | #elif defined(_WIN32) |
| 238 | // NOTE All this info is from cpu core 0 only. |
| 239 | |
| 240 | ReadProcessorString(&model_name, "ProcessorNameString"); |
| 241 | |
| 242 | u64 reg = 0; |
| 243 | // Attempt to be forward-compatible: perform inverted check against disabled feature states. |
| 244 | if (Read_ID_AA64ISAR0_EL1(®)) |
| 245 | { |
| 246 | bAES = ((reg >> 4) & 0xf) != 0; |
| 247 | bSHA1 = ((reg >> 8) & 0xf) != 0; |
| 248 | bSHA2 = ((reg >> 12) & 0xf) != 0; |
| 249 | bCRC32 = ((reg >> 16) & 0xf) != 0; |
| 250 | } |
| 251 | if (Read_ID_AA64MMFR1_EL1(®)) |
| 252 | { |
| 253 | // Introduced in Armv8.7, where AFP must be supported if AdvSIMD and FP both are. |
| 254 | bAFP = ((reg >> 44) & 0xf) != 0; |
| 255 | } |
| 256 | // Pre-decoded MIDR_EL1 could be read with ReadProcessorString(.., "Identifier"), |
| 257 | // but we want format to match across all platforms where possible. |
| 258 | if (Read_MIDR_EL1(®)) |
| 259 | { |
| 260 | cpu_id = MIDRToString(reg); |
| 261 | } |
| 262 | #elif defined(__linux__) || defined(HAVE_ELF_AUX_INFO) |
| 263 | // Linux, Android, FreeBSD and OpenBSD with elf_aux_info |
| 264 | |
| 265 | #if defined(__FreeBSD__) |
| 266 | SysctlByName(&model_name, "hw.model"); |
| 267 | #elif defined(__OpenBSD__) |
| 268 | int mib[2]; |
| 269 | size_t len; |
| 270 | char hwmodel[256]; |
| 271 | |
| 272 | mib[0] = CTL_HW; |
| 273 | mib[1] = HW_MODEL; |
| 274 | len = std::size(hwmodel); |
| 275 | if (sysctl(mib, 2, &hwmodel, &len, nullptr, 0) != -1) |
| 276 | model_name = std::string(hwmodel, len - 1); |
| 277 | #elif defined(__linux__) |
| 278 | if (!ReadDeviceTree(&model_name, "model")) |
| 279 | { |
| 280 | // This doesn't seem to work on modern arm64 kernels |
| 281 | model_name = ReadCpuinfoField("Hardware"); |
| 282 | } |
| 283 | #endif |
| 284 | |
| 285 | const u32 hwcap = ReadHwCap(AT_HWCAP); |
| 286 | bAES = hwcap & HWCAP_AES; |
| 287 | bCRC32 = hwcap & HWCAP_CRC32; |
| 288 | bSHA1 = hwcap & HWCAP_SHA1; |
| 289 | bSHA2 = hwcap & HWCAP_SHA2; |
| 290 | |
| 291 | #if defined(AT_HWCAP2) && defined(HWCAP2_AFP) |
| 292 | const u32 hwcap2 = ReadHwCap(AT_HWCAP2); |
| 293 | bAFP = hwcap2 & HWCAP2_AFP; |
| 294 | #endif |
| 295 | |
| 296 | u64 midr = 0; |
| 297 | if (Read_MIDR_EL1(&midr)) |
| 298 | { |
| 299 | cpu_id = MIDRToString(midr); |
| 300 | } |
| 301 | #elif defined(__OpenBSD__) |
| 302 | // OpenBSD |
| 303 | int mib[2]; |
| 304 | size_t len; |
| 305 | char hwmodel[256]; |
| 306 | uint64_t isar0; |
| 307 | |
| 308 | mib[0] = CTL_HW; |
| 309 | mib[1] = HW_MODEL; |
| 310 | len = std::size(hwmodel); |
| 311 | if (sysctl(mib, 2, &hwmodel, &len, nullptr, 0) != -1) |
| 312 | model_name = std::string(hwmodel, len - 1); |
| 313 | |
| 314 | mib[0] = CTL_MACHDEP; |
| 315 | mib[1] = CPU_ID_AA64ISAR0; |
| 316 | len = sizeof(isar0); |
| 317 | if (sysctl(mib, 2, &isar0, &len, nullptr, 0) != -1) |
| 318 | { |
| 319 | if (ID_AA64ISAR0_AES(isar0) >= ID_AA64ISAR0_AES_BASE) |
| 320 | bAES = true; |
| 321 | if (ID_AA64ISAR0_SHA1(isar0) >= ID_AA64ISAR0_SHA1_BASE) |
| 322 | bSHA1 = true; |
| 323 | if (ID_AA64ISAR0_SHA2(isar0) >= ID_AA64ISAR0_SHA2_BASE) |
| 324 | bSHA2 = true; |
| 325 | if (ID_AA64ISAR0_CRC32(isar0) >= ID_AA64ISAR0_CRC32_BASE) |
| 326 | bCRC32 = true; |
| 327 | } |
| 328 | #endif |
| 329 | |
| 330 | model_name = ReplaceAll(model_name, ",", "_"); |
| 331 | cpu_id = ReplaceAll(cpu_id, ",", "_"); |
| 332 | } |
| 333 | |
| 334 | std::string CPUInfo::Summarize() |
| 335 | { |
| 336 | std::vector<std::string> sum; |
| 337 | sum.push_back(model_name); |
| 338 | sum.push_back(cpu_id); |
| 339 | |
| 340 | if (bAFP) |
| 341 | sum.push_back("AFP"); |
| 342 | if (bAES) |
| 343 | sum.push_back("AES"); |
| 344 | if (bCRC32) |
| 345 | sum.push_back("CRC32"); |
| 346 | if (bSHA1) |
| 347 | sum.push_back("SHA1"); |
| 348 | if (bSHA2) |
| 349 | sum.push_back("SHA2"); |
| 350 | |
| 351 | return fmt::to_string(fmt::join(sum, ",")); |
| 352 | } |