blob: edba2c21988e9a92412aecb2e9cb194de6eac25a [file] [log] [blame]
[email protected]d1811bc2012-03-31 07:08:531// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]de8d26672008-09-25 22:08:442// 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"
[email protected]7e6d42b2011-02-16 18:51:586
[email protected]c37c1a8c2014-08-08 08:45:247#include <stdlib.h>
[email protected]d1811bc2012-03-31 07:08:538#include <string.h>
9
[email protected]595d1592012-10-04 21:05:2310#include <algorithm>
11
[email protected]f3d445e2013-11-22 18:35:0312#include "base/basictypes.h"
[email protected]c37c1a8c2014-08-08 08:45:2413#include "base/strings/string_piece.h"
[email protected]d1811bc2012-03-31 07:08:5314#include "build/build_config.h"
15
[email protected]3a23f63c2014-04-28 15:33:2616#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
[email protected]e3177dd52014-08-13 20:22:1417#include "base/files/file_util.h"
[email protected]3a23f63c2014-04-28 15:33:2618#include "base/lazy_instance.h"
19#endif
20
[email protected]7e6d42b2011-02-16 18:51:5821#if defined(ARCH_CPU_X86_FAMILY)
22#if defined(_MSC_VER)
[email protected]de8d26672008-09-25 22:08:4423#include <intrin.h>
[email protected]f3d445e2013-11-22 18:35:0324#include <immintrin.h> // For _xgetbv()
[email protected]7e6d42b2011-02-16 18:51:5825#endif
26#endif
27
[email protected]de8d26672008-09-25 22:08:4428namespace base {
29
30CPU::CPU()
[email protected]5c8f89f692013-07-18 11:13:2831 : signature_(0),
32 type_(0),
[email protected]de8d26672008-09-25 22:08:4433 family_(0),
34 model_(0),
35 stepping_(0),
36 ext_model_(0),
37 ext_family_(0),
[email protected]7e6d42b2011-02-16 18:51:5838 has_mmx_(false),
39 has_sse_(false),
40 has_sse2_(false),
41 has_sse3_(false),
42 has_ssse3_(false),
43 has_sse41_(false),
44 has_sse42_(false),
[email protected]f3d445e2013-11-22 18:35:0345 has_avx_(false),
46 has_avx_hardware_(false),
fbarchard0ce41ae2015-10-02 03:23:1947 has_avx2_(false),
[email protected]b54d16d2013-12-02 16:15:0348 has_aesni_(false),
[email protected]aa312812013-04-30 19:46:0549 has_non_stop_time_stamp_counter_(false),
[email protected]c37c1a8c2014-08-08 08:45:2450 has_broken_neon_(false),
[email protected]de8d26672008-09-25 22:08:4451 cpu_vendor_("unknown") {
52 Initialize();
53}
54
[email protected]f3d445e2013-11-22 18:35:0355namespace {
56
[email protected]7e6d42b2011-02-16 18:51:5857#if defined(ARCH_CPU_X86_FAMILY)
58#ifndef _MSC_VER
59
60#if defined(__pic__) && defined(__i386__)
61
62void __cpuid(int cpu_info[4], int info_type) {
63 __asm__ volatile (
64 "mov %%ebx, %%edi\n"
65 "cpuid\n"
66 "xchg %%edi, %%ebx\n"
67 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
68 : "a"(info_type)
69 );
70}
71
[email protected]7e6d42b2011-02-16 18:51:5872#else
73
74void __cpuid(int cpu_info[4], int info_type) {
75 __asm__ volatile (
fbarchard0ce41ae2015-10-02 03:23:1976 "cpuid\n"
[email protected]7e6d42b2011-02-16 18:51:5877 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
78 : "a"(info_type)
79 );
80}
81
[email protected]f3d445e2013-11-22 18:35:0382#endif
83
84// _xgetbv returns the value of an Intel Extended Control Register (XCR).
85// Currently only XCR0 is defined by Intel so |xcr| should always be zero.
86uint64 _xgetbv(uint32 xcr) {
87 uint32 eax, edx;
88
fbarchard0ce41ae2015-10-02 03:23:1989 __asm__ volatile (
90 "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
[email protected]f3d445e2013-11-22 18:35:0391 return (static_cast<uint64>(edx) << 32) | eax;
[email protected]7e6d42b2011-02-16 18:51:5892}
93
[email protected]f3d445e2013-11-22 18:35:0394#endif // !_MSC_VER
[email protected]7e6d42b2011-02-16 18:51:5895#endif // ARCH_CPU_X86_FAMILY
96
[email protected]3a23f63c2014-04-28 15:33:2697#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
[email protected]c37c1a8c2014-08-08 08:45:2498class LazyCpuInfoValue {
99 public:
100 LazyCpuInfoValue() : has_broken_neon_(false) {
101 // This function finds the value from /proc/cpuinfo under the key "model
102 // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7
103 // and later for arm64) and is shown once per CPU. "Processor" is used in
104 // earler versions and is shown only once at the top of /proc/cpuinfo
105 // regardless of the number CPUs.
106 const char kModelNamePrefix[] = "model name\t: ";
107 const char kProcessorPrefix[] = "Processor\t: ";
[email protected]3a23f63c2014-04-28 15:33:26108
[email protected]c37c1a8c2014-08-08 08:45:24109 // This function also calculates whether we believe that this CPU has a
110 // broken NEON unit based on these fields from cpuinfo:
111 unsigned implementer = 0, architecture = 0, variant = 0, part = 0,
112 revision = 0;
113 const struct {
114 const char key[17];
fbarchard0ce41ae2015-10-02 03:23:19115 unsigned int* result;
[email protected]c37c1a8c2014-08-08 08:45:24116 } kUnsignedValues[] = {
117 {"CPU implementer", &implementer},
118 {"CPU architecture", &architecture},
119 {"CPU variant", &variant},
120 {"CPU part", &part},
121 {"CPU revision", &revision},
122 };
123
124 std::string contents;
125 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
126 DCHECK(!contents.empty());
127 if (contents.empty()) {
128 return;
129 }
130
[email protected]3a23f63c2014-04-28 15:33:26131 std::istringstream iss(contents);
132 std::string line;
133 while (std::getline(iss, line)) {
[email protected]c37c1a8c2014-08-08 08:45:24134 if (brand_.empty() &&
135 (line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0 ||
136 line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0)) {
137 brand_.assign(line.substr(strlen(kModelNamePrefix)));
[email protected]652900112014-05-06 09:31:00138 }
[email protected]c37c1a8c2014-08-08 08:45:24139
140 for (size_t i = 0; i < arraysize(kUnsignedValues); i++) {
141 const char *key = kUnsignedValues[i].key;
142 const size_t len = strlen(key);
143
144 if (line.compare(0, len, key) == 0 &&
145 line.size() >= len + 1 &&
146 (line[len] == '\t' || line[len] == ' ' || line[len] == ':')) {
147 size_t colon_pos = line.find(':', len);
148 if (colon_pos == std::string::npos) {
149 continue;
150 }
151
152 const StringPiece line_sp(line);
153 StringPiece value_sp = line_sp.substr(colon_pos + 1);
154 while (!value_sp.empty() &&
155 (value_sp[0] == ' ' || value_sp[0] == '\t')) {
156 value_sp = value_sp.substr(1);
157 }
158
159 // The string may have leading "0x" or not, so we use strtoul to
160 // handle that.
fbarchard0ce41ae2015-10-02 03:23:19161 char* endptr;
[email protected]c37c1a8c2014-08-08 08:45:24162 std::string value(value_sp.as_string());
163 unsigned long int result = strtoul(value.c_str(), &endptr, 0);
164 if (*endptr == 0 && result <= UINT_MAX) {
165 *kUnsignedValues[i].result = result;
166 }
167 }
[email protected]3a23f63c2014-04-28 15:33:26168 }
169 }
[email protected]3a23f63c2014-04-28 15:33:26170
[email protected]c37c1a8c2014-08-08 08:45:24171 has_broken_neon_ =
172 implementer == 0x51 &&
173 architecture == 7 &&
174 variant == 1 &&
175 part == 0x4d &&
176 revision == 0;
177 }
178
179 const std::string& brand() const { return brand_; }
180 bool has_broken_neon() const { return has_broken_neon_; }
[email protected]3a23f63c2014-04-28 15:33:26181
182 private:
[email protected]c37c1a8c2014-08-08 08:45:24183 std::string brand_;
184 bool has_broken_neon_;
[email protected]3a23f63c2014-04-28 15:33:26185 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue);
186};
187
[email protected]c37c1a8c2014-08-08 08:45:24188base::LazyInstance<LazyCpuInfoValue>::Leaky g_lazy_cpuinfo =
[email protected]3a23f63c2014-04-28 15:33:26189 LAZY_INSTANCE_INITIALIZER;
190
[email protected]3a23f63c2014-04-28 15:33:26191#endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
192 // defined(OS_LINUX))
193
[email protected]f3d445e2013-11-22 18:35:03194} // anonymous namespace
195
[email protected]de8d26672008-09-25 22:08:44196void CPU::Initialize() {
[email protected]7e6d42b2011-02-16 18:51:58197#if defined(ARCH_CPU_X86_FAMILY)
[email protected]de8d26672008-09-25 22:08:44198 int cpu_info[4] = {-1};
[email protected]595d1592012-10-04 21:05:23199 char cpu_string[48];
[email protected]de8d26672008-09-25 22:08:44200
201 // __cpuid with an InfoType argument of 0 returns the number of
202 // valid Ids in CPUInfo[0] and the CPU identification string in
203 // the other three array elements. The CPU identification string is
[email protected]52a261f2009-03-03 15:01:12204 // not in linear order. The code below arranges the information
[email protected]595d1592012-10-04 21:05:23205 // in a human readable form. The human readable order is CPUInfo[1] |
206 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
207 // before using memcpy to copy these three array elements to cpu_string.
[email protected]de8d26672008-09-25 22:08:44208 __cpuid(cpu_info, 0);
209 int num_ids = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23210 std::swap(cpu_info[2], cpu_info[3]);
211 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
212 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
[email protected]de8d26672008-09-25 22:08:44213
214 // Interpret CPU feature information.
[email protected]7f0813642008-09-26 23:26:34215 if (num_ids > 0) {
fbarchard0ce41ae2015-10-02 03:23:19216 int cpu_info7[4] = {0};
[email protected]7f0813642008-09-26 23:26:34217 __cpuid(cpu_info, 1);
fbarchard0ce41ae2015-10-02 03:23:19218 if (num_ids >= 7) {
219 __cpuid(cpu_info7, 7);
220 }
[email protected]5c8f89f692013-07-18 11:13:28221 signature_ = cpu_info[0];
[email protected]7f0813642008-09-26 23:26:34222 stepping_ = cpu_info[0] & 0xf;
[email protected]7e6d42b2011-02-16 18:51:58223 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
[email protected]7f0813642008-09-26 23:26:34224 family_ = (cpu_info[0] >> 8) & 0xf;
225 type_ = (cpu_info[0] >> 12) & 0x3;
226 ext_model_ = (cpu_info[0] >> 16) & 0xf;
227 ext_family_ = (cpu_info[0] >> 20) & 0xff;
[email protected]f3d445e2013-11-22 18:35:03228 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
229 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
230 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
231 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
[email protected]7e6d42b2011-02-16 18:51:58232 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
233 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
234 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
[email protected]f3d445e2013-11-22 18:35:03235 has_avx_hardware_ =
236 (cpu_info[2] & 0x10000000) != 0;
237 // AVX instructions will generate an illegal instruction exception unless
238 // a) they are supported by the CPU,
239 // b) XSAVE is supported by the CPU and
240 // c) XSAVE is enabled by the kernel.
241 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
[email protected]26ce2f62014-05-28 23:28:48242 //
243 // In addition, we have observed some crashes with the xgetbv instruction
244 // even after following Intel's example code. (See crbug.com/375968.)
245 // Because of that, we also test the XSAVE bit because its description in
246 // the CPUID documentation suggests that it signals xgetbv support.
[email protected]f3d445e2013-11-22 18:35:03247 has_avx_ =
248 has_avx_hardware_ &&
[email protected]26ce2f62014-05-28 23:28:48249 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
[email protected]f3d445e2013-11-22 18:35:03250 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
251 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
[email protected]b54d16d2013-12-02 16:15:03252 has_aesni_ = (cpu_info[2] & 0x02000000) != 0;
fbarchard0ce41ae2015-10-02 03:23:19253 has_avx2_ = has_avx_ && (cpu_info7[1] & 0x00000020) != 0;
[email protected]7f0813642008-09-26 23:26:34254 }
[email protected]595d1592012-10-04 21:05:23255
256 // Get the brand string of the cpu.
257 __cpuid(cpu_info, 0x80000000);
258 const int parameter_end = 0x80000004;
[email protected]aa312812013-04-30 19:46:05259 int max_parameter = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23260
261 if (cpu_info[0] >= parameter_end) {
262 char* cpu_string_ptr = cpu_string;
263
264 for (int parameter = 0x80000002; parameter <= parameter_end &&
265 cpu_string_ptr < &cpu_string[sizeof(cpu_string)]; parameter++) {
266 __cpuid(cpu_info, parameter);
267 memcpy(cpu_string_ptr, cpu_info, sizeof(cpu_info));
268 cpu_string_ptr += sizeof(cpu_info);
269 }
270 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string);
271 }
[email protected]aa312812013-04-30 19:46:05272
273 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007;
274 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) {
275 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
276 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
277 }
[email protected]3a23f63c2014-04-28 15:33:26278#elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
[email protected]c37c1a8c2014-08-08 08:45:24279 cpu_brand_.assign(g_lazy_cpuinfo.Get().brand());
280 has_broken_neon_ = g_lazy_cpuinfo.Get().has_broken_neon();
[email protected]7e6d42b2011-02-16 18:51:58281#endif
[email protected]de8d26672008-09-25 22:08:44282}
283
[email protected]5016a9dd2013-02-02 01:10:02284CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const {
fbarchard0ce41ae2015-10-02 03:23:19285 if (has_avx2()) return AVX2;
[email protected]5016a9dd2013-02-02 01:10:02286 if (has_avx()) return AVX;
287 if (has_sse42()) return SSE42;
288 if (has_sse41()) return SSE41;
289 if (has_ssse3()) return SSSE3;
290 if (has_sse3()) return SSE3;
291 if (has_sse2()) return SSE2;
292 if (has_sse()) return SSE;
293 return PENTIUM;
294}
295
[email protected]de8d26672008-09-25 22:08:44296} // namespace base