x64CPUDetect.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "Common/CPUDetect.h"
  4. #ifdef _WIN32
  5. #include <windows.h>
  6. #include <processthreadsapi.h>
  7. #endif
  8. #include <algorithm>
  9. #include <cstring>
  10. #include <string>
  11. #include <thread>
  12. #include <fmt/format.h>
  13. #include <fmt/ranges.h>
  14. #include "Common/CommonTypes.h"
  15. #include "Common/Intrinsics.h"
  16. #include "Common/MsgHandler.h"
  17. #include "Common/StringUtil.h"
  18. #ifndef _WIN32
  19. #ifdef __FreeBSD__
  20. #include <unistd.h>
  21. #include <machine/cpufunc.h>
  22. #include <sys/types.h>
  23. #endif
  24. static inline void __cpuidex(int info[4], int function_id, int subfunction_id)
  25. {
  26. #ifdef __FreeBSD__
  27. // Despite the name, this is just do_cpuid() with ECX as second input.
  28. cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
  29. #else
  30. info[0] = function_id; // eax
  31. info[2] = subfunction_id; // ecx
  32. __asm__("cpuid"
  33. : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3])
  34. : "a"(function_id), "c"(subfunction_id));
  35. #endif
  36. }
  37. constexpr u32 XCR_XFEATURE_ENABLED_MASK = 0;
  38. static u64 xgetbv(u32 index)
  39. {
  40. u32 eax, edx;
  41. __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
  42. return ((u64)edx << 32) | eax;
  43. }
  44. #else
  45. constexpr u32 XCR_XFEATURE_ENABLED_MASK = _XCR_XFEATURE_ENABLED_MASK;
  46. static u64 xgetbv(u32 index)
  47. {
  48. return _xgetbv(index);
  49. }
  50. static void WarnIfRunningUnderEmulation()
  51. {
  52. // Starting with win11, arm64 windows can run x64 processes under emulation.
  53. // This detects such a scenario and informs the user they probably want to run a native build.
  54. PROCESS_MACHINE_INFORMATION info{};
  55. if (!GetProcessInformation(GetCurrentProcess(), ProcessMachineTypeInfo, &info, sizeof(info)))
  56. {
  57. // Possibly we are running on version of windows which doesn't support ProcessMachineTypeInfo.
  58. return;
  59. }
  60. if (info.MachineAttributes & MACHINE_ATTRIBUTES::KernelEnabled)
  61. {
  62. // KernelEnabled will be set if process arch matches the kernel arch - how we want people to run
  63. // dolphin.
  64. return;
  65. }
  66. // The process is not native; could use IsWow64Process2 to get native machine type, but for now
  67. // we can assume it is arm64.
  68. PanicAlertFmtT("This build of Dolphin is not natively compiled for your CPU.\n"
  69. "Please run the ARM64 build of Dolphin for a better experience.");
  70. }
  71. #endif // ifdef _WIN32
  72. struct CPUIDResult
  73. {
  74. u32 eax{}, ebx{}, ecx{}, edx{};
  75. };
  76. static_assert(sizeof(CPUIDResult) == sizeof(u32) * 4);
  77. static inline CPUIDResult cpuid(int function_id, int subfunction_id = 0)
  78. {
  79. CPUIDResult info;
  80. __cpuidex((int*)&info, function_id, subfunction_id);
  81. return info;
  82. }
  83. CPUInfo cpu_info;
  84. CPUInfo::CPUInfo()
  85. {
  86. Detect();
  87. }
  88. void CPUInfo::Detect()
  89. {
  90. #ifdef _WIN32
  91. WarnIfRunningUnderEmulation();
  92. #endif
  93. // This should be much more reliable and easier than trying to get the number of cores out of the
  94. // CPUID data ourselves.
  95. num_cores = std::max(static_cast<int>(std::thread::hardware_concurrency()), 1);
  96. // Assume CPU supports the CPUID instruction. Those that don't can barely
  97. // boot modern OS anyway.
  98. // Detect CPU's CPUID capabilities and grab vendor string.
  99. auto info = cpuid(0);
  100. const u32 func_id_max = info.eax;
  101. std::string vendor_id;
  102. vendor_id.resize(sizeof(u32) * 3);
  103. std::memcpy(&vendor_id[0], &info.ebx, sizeof(u32));
  104. std::memcpy(&vendor_id[4], &info.edx, sizeof(u32));
  105. std::memcpy(&vendor_id[8], &info.ecx, sizeof(u32));
  106. TruncateToCString(&vendor_id);
  107. if (vendor_id == "GenuineIntel")
  108. vendor = CPUVendor::Intel;
  109. else if (vendor_id == "AuthenticAMD")
  110. vendor = CPUVendor::AMD;
  111. else
  112. vendor = CPUVendor::Other;
  113. // Detect family and other misc stuff.
  114. bool is_amd_family_17 = false;
  115. bool has_sse = false;
  116. if (func_id_max >= 1)
  117. {
  118. info = cpuid(1);
  119. const u32 version = info.eax;
  120. const u32 family = ((version >> 8) & 0xf) + ((version >> 20) & 0xff);
  121. const u32 model = ((version >> 4) & 0xf) + ((version >> 12) & 0xf0);
  122. const u32 stepping = version & 0xf;
  123. cpu_id = fmt::format("{:02X}:{:02X}:{:X}", family, model, stepping);
  124. // Detect people unfortunate enough to be running Dolphin on an Atom
  125. if (vendor == CPUVendor::Intel && family == 6 &&
  126. (model == 0x1C || model == 0x26 || model == 0x27 || model == 0x35 || model == 0x36 ||
  127. model == 0x37 || model == 0x4A || model == 0x4D || model == 0x5A || model == 0x5D))
  128. bAtom = true;
  129. // Detect AMD Zen1, Zen1+ and Zen2
  130. if (vendor == CPUVendor::AMD && family == 0x17)
  131. is_amd_family_17 = true;
  132. // AMD CPUs before Zen faked this flag and didn't actually
  133. // implement simultaneous multithreading (SMT; Intel calls it HTT)
  134. // but rather some weird middle-ground between 1-2 cores
  135. const bool ht = (info.edx >> 28) & 1;
  136. HTT = ht && (vendor == CPUVendor::Intel || (vendor == CPUVendor::AMD && family >= 0x17));
  137. if ((info.edx >> 25) & 1)
  138. has_sse = true;
  139. if (info.ecx & 1)
  140. bSSE3 = true;
  141. if ((info.ecx >> 9) & 1)
  142. bSSSE3 = true;
  143. if ((info.ecx >> 19) & 1)
  144. bSSE4_1 = true;
  145. if ((info.ecx >> 20) & 1)
  146. bSSE4_2 = true;
  147. if ((info.ecx >> 22) & 1)
  148. bMOVBE = true;
  149. if ((info.ecx >> 25) & 1)
  150. bAES = true;
  151. // AVX support requires 3 separate checks:
  152. // - Is the AVX bit set in CPUID?
  153. // - Is the XSAVE bit set in CPUID?
  154. // - XGETBV result has the XCR bit set.
  155. if (((info.ecx >> 28) & 1) && ((info.ecx >> 27) & 1))
  156. {
  157. // Check that XSAVE can be used for SSE and AVX
  158. if ((xgetbv(XCR_XFEATURE_ENABLED_MASK) & 0b110) == 0b110)
  159. {
  160. bAVX = true;
  161. if ((info.ecx >> 12) & 1)
  162. bFMA = true;
  163. }
  164. }
  165. if (func_id_max >= 7)
  166. {
  167. info = cpuid(7);
  168. if ((info.ebx >> 3) & 1)
  169. bBMI1 = true;
  170. if ((info.ebx >> 8) & 1)
  171. bBMI2 = true;
  172. if ((info.ebx >> 29) & 1)
  173. bSHA1 = bSHA2 = true;
  174. }
  175. }
  176. info = cpuid(0x80000000);
  177. const u32 ext_func_id_max = info.eax;
  178. if (ext_func_id_max >= 0x80000004)
  179. {
  180. // Extract CPU model string
  181. model_name.resize(sizeof(info) * 3);
  182. for (u32 i = 0; i < 3; i++)
  183. {
  184. info = cpuid(0x80000002 + i);
  185. memcpy(&model_name[sizeof(info) * i], &info, sizeof(info));
  186. }
  187. TruncateToCString(&model_name);
  188. model_name = StripSpaces(model_name);
  189. }
  190. if (ext_func_id_max >= 0x80000001)
  191. {
  192. // Check for more features.
  193. info = cpuid(0x80000001);
  194. if ((info.ecx >> 5) & 1)
  195. bLZCNT = true;
  196. if ((info.ecx >> 16) & 1)
  197. bFMA4 = true;
  198. }
  199. // Computed flags
  200. bFlushToZero = has_sse;
  201. bBMI2FastParallelBitOps = bBMI2 && !is_amd_family_17;
  202. bCRC32 = bSSE4_2;
  203. model_name = ReplaceAll(model_name, ",", "_");
  204. cpu_id = ReplaceAll(cpu_id, ",", "_");
  205. }
  206. std::string CPUInfo::Summarize()
  207. {
  208. std::vector<std::string> sum;
  209. sum.push_back(model_name);
  210. sum.push_back(cpu_id);
  211. if (bSSE3)
  212. sum.push_back("SSE3");
  213. if (bSSSE3)
  214. sum.push_back("SSSE3");
  215. if (bSSE4_1)
  216. sum.push_back("SSE4.1");
  217. if (bSSE4_2)
  218. sum.push_back("SSE4.2");
  219. if (HTT)
  220. sum.push_back("HTT");
  221. if (bAVX)
  222. sum.push_back("AVX");
  223. if (bBMI1)
  224. sum.push_back("BMI1");
  225. if (bBMI2)
  226. sum.push_back("BMI2");
  227. if (bFMA)
  228. sum.push_back("FMA");
  229. if (bMOVBE)
  230. sum.push_back("MOVBE");
  231. if (bAES)
  232. sum.push_back("AES");
  233. if (bCRC32)
  234. sum.push_back("CRC32");
  235. if (bSHA1)
  236. sum.push_back("SHA1");
  237. if (bSHA2)
  238. sum.push_back("SHA2");
  239. return fmt::to_string(fmt::join(sum, ","));
  240. }