Debian Codegraph @

Source/Core/Common/x64CPUDetect.cpp

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