ArmCPUDetect.cpp 8.6 KB

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