1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/cpu.h"
6
7 #include <stdint.h>
8 #include <string.h>
9
10 #include <string>
11 #include <string_view>
12 #include <utility>
13
14 #include "base/memory/protected_memory.h"
15 #include "build/build_config.h"
16
17 #if defined(ARCH_CPU_ARM_FAMILY) && \
18 (BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
19 #include <asm/hwcap.h>
20 #include <sys/auxv.h>
21
22 #include "base/files/file_util.h"
23 #include "base/numerics/checked_math.h"
24 #include "base/ranges/algorithm.h"
25 #include "base/strings/string_number_conversions.h"
26 #include "base/strings/string_split.h"
27 #include "base/strings/string_util.h"
28
29 // Temporary definitions until a new hwcap.h is pulled in everywhere.
30 // https://crbug.com/1265965
31 #ifndef HWCAP2_MTE
32 #define HWCAP2_MTE (1 << 18)
33 #define HWCAP2_BTI (1 << 17)
34 #endif
35
36 struct ProcCpuInfo {
37 std::string brand;
38 uint8_t implementer = 0;
39 uint32_t part_number = 0;
40 };
41 #endif
42
43 #if defined(ARCH_CPU_X86_FAMILY)
44 #if defined(COMPILER_MSVC)
45 #include <intrin.h>
46 #include <immintrin.h> // For _xgetbv()
47 #endif
48 #endif
49
50 namespace base {
51
52 #if defined(ARCH_CPU_X86_FAMILY)
53 namespace internal {
54
ComputeX86FamilyAndModel(const std::string & vendor,int signature)55 X86ModelInfo ComputeX86FamilyAndModel(const std::string& vendor,
56 int signature) {
57 X86ModelInfo results;
58 results.family = (signature >> 8) & 0xf;
59 results.model = (signature >> 4) & 0xf;
60 results.ext_family = 0;
61 results.ext_model = 0;
62
63 // The "Intel 64 and IA-32 Architectures Developer's Manual: Vol. 2A"
64 // specifies the Extended Model is defined only when the Base Family is
65 // 06h or 0Fh.
66 // The "AMD CPUID Specification" specifies that the Extended Model is
67 // defined only when Base Family is 0Fh.
68 // Both manuals define the display model as
69 // {ExtendedModel[3:0],BaseModel[3:0]} in that case.
70 if (results.family == 0xf ||
71 (results.family == 0x6 && vendor == "GenuineIntel")) {
72 results.ext_model = (signature >> 16) & 0xf;
73 results.model += results.ext_model << 4;
74 }
75 // Both the "Intel 64 and IA-32 Architectures Developer's Manual: Vol. 2A"
76 // and the "AMD CPUID Specification" specify that the Extended Family is
77 // defined only when the Base Family is 0Fh.
78 // Both manuals define the display family as {0000b,BaseFamily[3:0]} +
79 // ExtendedFamily[7:0] in that case.
80 if (results.family == 0xf) {
81 results.ext_family = (signature >> 20) & 0xff;
82 results.family += results.ext_family;
83 }
84
85 return results;
86 }
87
88 } // namespace internal
89 #endif // defined(ARCH_CPU_X86_FAMILY)
90
CPU(bool require_branding)91 CPU::CPU(bool require_branding) {
92 Initialize(require_branding);
93 }
CPU()94 CPU::CPU() : CPU(true) {}
95 CPU::CPU(CPU&&) = default;
96
97 namespace {
98
99 #if defined(ARCH_CPU_X86_FAMILY)
100 #if !defined(COMPILER_MSVC)
101
102 #if defined(__pic__) && defined(__i386__)
103
__cpuid(int cpu_info[4],int info_type)104 void __cpuid(int cpu_info[4], int info_type) {
105 __asm__ volatile(
106 "mov %%ebx, %%edi\n"
107 "cpuid\n"
108 "xchg %%edi, %%ebx\n"
109 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]),
110 "=d"(cpu_info[3])
111 : "a"(info_type), "c"(0));
112 }
113
114 #else
115
116 void __cpuid(int cpu_info[4], int info_type) {
117 __asm__ volatile("cpuid\n"
118 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]),
119 "=d"(cpu_info[3])
120 : "a"(info_type), "c"(0));
121 }
122
123 #endif
124 #endif // !defined(COMPILER_MSVC)
125
126 // xgetbv returns the value of an Intel Extended Control Register (XCR).
127 // Currently only XCR0 is defined by Intel so |xcr| should always be zero.
xgetbv(uint32_t xcr)128 uint64_t xgetbv(uint32_t xcr) {
129 #if defined(COMPILER_MSVC)
130 return _xgetbv(xcr);
131 #else
132 uint32_t eax, edx;
133
134 __asm__ volatile (
135 "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
136 return (static_cast<uint64_t>(edx) << 32) | eax;
137 #endif // defined(COMPILER_MSVC)
138 }
139
140 #endif // ARCH_CPU_X86_FAMILY
141
142 #if defined(ARCH_CPU_ARM_FAMILY) && \
143 (BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
FindFirstProcCpuKey(const StringPairs & pairs,std::string_view key)144 StringPairs::const_iterator FindFirstProcCpuKey(const StringPairs& pairs,
145 std::string_view key) {
146 return ranges::find_if(pairs, [key](const StringPairs::value_type& pair) {
147 return TrimWhitespaceASCII(pair.first, base::TRIM_ALL) == key;
148 });
149 }
150
151 // Parses information about the ARM processor. Note that depending on the CPU
152 // package, processor configuration, and/or kernel version, this may only
153 // report information about the processor on which this thread is running. This
154 // can happen on heterogeneous-processor SoCs like Snapdragon 808, which has 4
155 // Cortex-A53 and 2 Cortex-A57. Unfortunately there is not a universally
156 // reliable way to examine the CPU part information for all cores.
ParseProcCpu()157 const ProcCpuInfo& ParseProcCpu() {
158 static const NoDestructor<ProcCpuInfo> info([]() {
159 // This function finds the value from /proc/cpuinfo under the key "model
160 // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7
161 // and later for arm64) and is shown once per CPU. "Processor" is used in
162 // earler versions and is shown only once at the top of /proc/cpuinfo
163 // regardless of the number CPUs.
164 const char kModelNamePrefix[] = "model name";
165 const char kProcessorPrefix[] = "Processor";
166
167 std::string cpuinfo;
168 ReadFileToString(FilePath("/proc/cpuinfo"), &cpuinfo);
169 DCHECK(!cpuinfo.empty());
170
171 ProcCpuInfo info;
172
173 StringPairs pairs;
174 if (!SplitStringIntoKeyValuePairs(cpuinfo, ':', '\n', &pairs)) {
175 NOTREACHED();
176 return info;
177 }
178
179 auto model_name = FindFirstProcCpuKey(pairs, kModelNamePrefix);
180 if (model_name == pairs.end())
181 model_name = FindFirstProcCpuKey(pairs, kProcessorPrefix);
182 if (model_name != pairs.end()) {
183 TrimWhitespaceASCII(model_name->second, TRIM_ALL, &info.brand);
184 }
185
186 auto implementer_string = FindFirstProcCpuKey(pairs, "CPU implementer");
187 if (implementer_string != pairs.end()) {
188 // HexStringToUInt() handles the leading whitespace on the value.
189 uint32_t implementer;
190 HexStringToUInt(implementer_string->second, &implementer);
191 if (!CheckedNumeric<uint32_t>(implementer)
192 .AssignIfValid(&info.implementer)) {
193 info.implementer = 0;
194 }
195 }
196
197 auto part_number_string = FindFirstProcCpuKey(pairs, "CPU part");
198 if (part_number_string != pairs.end())
199 HexStringToUInt(part_number_string->second, &info.part_number);
200
201 return info;
202 }());
203
204 return *info;
205 }
206 #endif // defined(ARCH_CPU_ARM_FAMILY) && (BUILDFLAG(IS_ANDROID) ||
207 // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
208
209 DEFINE_PROTECTED_DATA base::ProtectedMemory<CPU, true> g_cpu_instance;
210
211 } // namespace
212
Initialize(bool require_branding)213 void CPU::Initialize(bool require_branding) {
214 #if defined(ARCH_CPU_X86_FAMILY)
215 int cpu_info[4] = {-1};
216
217 // __cpuid with an InfoType argument of 0 returns the number of
218 // valid Ids in CPUInfo[0] and the CPU identification string in
219 // the other three array elements. The CPU identification string is
220 // not in linear order. The code below arranges the information
221 // in a human readable form. The human readable order is CPUInfo[1] |
222 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
223 // before using memcpy() to copy these three array elements to |cpu_string|.
224 __cpuid(cpu_info, 0);
225 int num_ids = cpu_info[0];
226 std::swap(cpu_info[2], cpu_info[3]);
227 memcpy(cpu_vendor_, &cpu_info[1], kVendorNameSize);
228 cpu_vendor_[kVendorNameSize] = '\0';
229
230 // Interpret CPU feature information.
231 if (num_ids > 0) {
232 int cpu_info7[4] = {0};
233 __cpuid(cpu_info, 1);
234 if (num_ids >= 7) {
235 __cpuid(cpu_info7, 7);
236 }
237 signature_ = cpu_info[0];
238 stepping_ = cpu_info[0] & 0xf;
239 type_ = (cpu_info[0] >> 12) & 0x3;
240 internal::X86ModelInfo results =
241 internal::ComputeX86FamilyAndModel(cpu_vendor_, signature_);
242 family_ = results.family;
243 model_ = results.model;
244 ext_family_ = results.ext_family;
245 ext_model_ = results.ext_model;
246 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
247 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
248 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
249 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
250 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
251 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
252 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
253 has_popcnt_ = (cpu_info[2] & 0x00800000) != 0;
254
255 // "Hypervisor Present Bit: Bit 31 of ECX of CPUID leaf 0x1."
256 // See https://lwn.net/Articles/301888/
257 // This is checking for any hypervisor. Hypervisors may choose not to
258 // announce themselves. Hypervisors trap CPUID and sometimes return
259 // different results to underlying hardware.
260 is_running_in_vm_ = (static_cast<uint32_t>(cpu_info[2]) & 0x80000000) != 0;
261
262 // AVX instructions will generate an illegal instruction exception unless
263 // a) they are supported by the CPU,
264 // b) XSAVE is supported by the CPU and
265 // c) XSAVE is enabled by the kernel.
266 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
267 //
268 // In addition, we have observed some crashes with the xgetbv instruction
269 // even after following Intel's example code. (See crbug.com/375968.)
270 // Because of that, we also test the XSAVE bit because its description in
271 // the CPUID documentation suggests that it signals xgetbv support.
272 has_avx_ =
273 (cpu_info[2] & 0x10000000) != 0 &&
274 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
275 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
276 (xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
277 has_aesni_ = (cpu_info[2] & 0x02000000) != 0;
278 has_fma3_ = (cpu_info[2] & 0x00001000) != 0;
279 has_avx2_ = has_avx_ && (cpu_info7[1] & 0x00000020) != 0;
280
281 has_pku_ = (cpu_info7[2] & 0x00000010) != 0;
282 }
283
284 // Get the brand string of the cpu.
285 __cpuid(cpu_info, static_cast<int>(0x80000000));
286 const uint32_t max_parameter = static_cast<uint32_t>(cpu_info[0]);
287
288 static constexpr uint32_t kParameterStart = 0x80000002;
289 static constexpr uint32_t kParameterEnd = 0x80000004;
290 static constexpr uint32_t kParameterSize =
291 kParameterEnd - kParameterStart + 1;
292 static_assert(kParameterSize * sizeof(cpu_info) == kBrandNameSize,
293 "cpu_brand_ has wrong size");
294
295 if (max_parameter >= kParameterEnd) {
296 size_t i = 0;
297 for (uint32_t parameter = kParameterStart; parameter <= kParameterEnd;
298 ++parameter) {
299 __cpuid(cpu_info, static_cast<int>(parameter));
300 memcpy(&cpu_brand_[i], cpu_info, sizeof(cpu_info));
301 i += sizeof(cpu_info);
302 }
303 cpu_brand_[i] = '\0';
304 }
305
306 static constexpr uint32_t kParameterContainingNonStopTimeStampCounter =
307 0x80000007;
308 if (max_parameter >= kParameterContainingNonStopTimeStampCounter) {
309 __cpuid(cpu_info,
310 static_cast<int>(kParameterContainingNonStopTimeStampCounter));
311 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
312 }
313
314 if (!has_non_stop_time_stamp_counter_ && is_running_in_vm_) {
315 int cpu_info_hv[4] = {};
316 __cpuid(cpu_info_hv, 0x40000000);
317 if (cpu_info_hv[1] == 0x7263694D && // Micr
318 cpu_info_hv[2] == 0x666F736F && // osof
319 cpu_info_hv[3] == 0x76482074) { // t Hv
320 // If CPUID says we have a variant TSC and a hypervisor has identified
321 // itself and the hypervisor says it is Microsoft Hyper-V, then treat
322 // TSC as invariant.
323 //
324 // Microsoft Hyper-V hypervisor reports variant TSC as there are some
325 // scenarios (eg. VM live migration) where the TSC is variant, but for
326 // our purposes we can treat it as invariant.
327 has_non_stop_time_stamp_counter_ = true;
328 }
329 }
330 #elif defined(ARCH_CPU_ARM_FAMILY)
331 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
332 if (require_branding) {
333 const ProcCpuInfo& info = ParseProcCpu();
334
335 // Ensure the brand can be stored in the internal array.
336 CHECK_LE(info.brand.size(), kBrandNameSize);
337
338 const size_t chars_copied = info.brand.copy(cpu_brand_, kBrandNameSize);
339 cpu_brand_[chars_copied] = '\0';
340
341 implementer_ = info.implementer;
342 part_number_ = info.part_number;
343 }
344
345 #if defined(ARCH_CPU_ARM64)
346 // Check for Armv8.5-A BTI/MTE support, exposed via HWCAP2
347 unsigned long hwcap2 = getauxval(AT_HWCAP2);
348 has_mte_ = hwcap2 & HWCAP2_MTE;
349 has_bti_ = hwcap2 & HWCAP2_BTI;
350 #endif
351
352 #elif BUILDFLAG(IS_WIN)
353 // Windows makes high-resolution thread timing information available in
354 // user-space.
355 has_non_stop_time_stamp_counter_ = true;
356 #endif
357 #endif
358 }
359
360 #if defined(ARCH_CPU_X86_FAMILY)
GetIntelMicroArchitecture() const361 CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const {
362 if (has_avx2()) return AVX2;
363 if (has_fma3()) return FMA3;
364 if (has_avx()) return AVX;
365 if (has_sse42()) return SSE42;
366 if (has_sse41()) return SSE41;
367 if (has_ssse3()) return SSSE3;
368 if (has_sse3()) return SSE3;
369 if (has_sse2()) return SSE2;
370 if (has_sse()) return SSE;
371 return PENTIUM;
372 }
373 #endif
374
GetInstanceNoAllocation()375 const CPU& CPU::GetInstanceNoAllocation() {
376 static ProtectedMemoryInitializer cpu_initializer(g_cpu_instance, CPU(false));
377
378 return *g_cpu_instance;
379 }
380
381 } // namespace base
382