blob: ef3309dad180b4150dd39a54cd5300f6ad155fa7 [file] [log] [blame]
jbauman@chromium.orgd07e3bd2012-03-31 16:08:53 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
maruel@google.com8178de22008-09-26 07:08:44 +09002// 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"
jiesun@chromium.orgc8420402011-02-17 03:51:58 +09006
agl@chromium.org41be0792014-08-08 17:45:24 +09007#include <stdlib.h>
jbauman@chromium.orgd07e3bd2012-03-31 16:08:53 +09008#include <string.h>
9
chunyang.dai@intel.com94d8f282012-10-05 06:05:23 +090010#include <algorithm>
11
agl@chromium.orga3f13f52013-11-23 03:35:03 +090012#include "base/basictypes.h"
agl@chromium.org41be0792014-08-08 17:45:24 +090013#include "base/strings/string_piece.h"
jbauman@chromium.orgd07e3bd2012-03-31 16:08:53 +090014#include "build/build_config.h"
15
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +090016#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
brettw@chromium.org01f3da42014-08-14 05:22:14 +090017#include "base/files/file_util.h"
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +090018#include "base/lazy_instance.h"
19#endif
20
jiesun@chromium.orgc8420402011-02-17 03:51:58 +090021#if defined(ARCH_CPU_X86_FAMILY)
22#if defined(_MSC_VER)
maruel@google.com8178de22008-09-26 07:08:44 +090023#include <intrin.h>
agl@chromium.orga3f13f52013-11-23 03:35:03 +090024#include <immintrin.h> // For _xgetbv()
jiesun@chromium.orgc8420402011-02-17 03:51:58 +090025#endif
26#endif
27
maruel@google.com8178de22008-09-26 07:08:44 +090028namespace base {
29
30CPU::CPU()
simonjam@chromium.org7bf2f7b2013-07-18 20:13:28 +090031 : signature_(0),
32 type_(0),
maruel@google.com8178de22008-09-26 07:08:44 +090033 family_(0),
34 model_(0),
35 stepping_(0),
36 ext_model_(0),
37 ext_family_(0),
jiesun@chromium.orgc8420402011-02-17 03:51:58 +090038 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),
agl@chromium.orga3f13f52013-11-23 03:35:03 +090045 has_avx_(false),
46 has_avx_hardware_(false),
agl@chromium.orgabda8512013-12-03 01:15:03 +090047 has_aesni_(false),
simonjam@chromium.orge50c7cb2013-05-01 04:46:05 +090048 has_non_stop_time_stamp_counter_(false),
agl@chromium.org41be0792014-08-08 17:45:24 +090049 has_broken_neon_(false),
maruel@google.com8178de22008-09-26 07:08:44 +090050 cpu_vendor_("unknown") {
51 Initialize();
52}
53
agl@chromium.orga3f13f52013-11-23 03:35:03 +090054namespace {
55
jiesun@chromium.orgc8420402011-02-17 03:51:58 +090056#if defined(ARCH_CPU_X86_FAMILY)
57#ifndef _MSC_VER
58
59#if defined(__pic__) && defined(__i386__)
60
61void __cpuid(int cpu_info[4], int info_type) {
62 __asm__ volatile (
63 "mov %%ebx, %%edi\n"
64 "cpuid\n"
65 "xchg %%edi, %%ebx\n"
66 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
67 : "a"(info_type)
68 );
69}
70
jiesun@chromium.orgc8420402011-02-17 03:51:58 +090071#else
72
73void __cpuid(int cpu_info[4], int info_type) {
74 __asm__ volatile (
75 "cpuid \n\t"
76 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
77 : "a"(info_type)
78 );
79}
80
agl@chromium.orga3f13f52013-11-23 03:35:03 +090081#endif
82
83// _xgetbv returns the value of an Intel Extended Control Register (XCR).
84// Currently only XCR0 is defined by Intel so |xcr| should always be zero.
85uint64 _xgetbv(uint32 xcr) {
86 uint32 eax, edx;
87
88 __asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr));
89 return (static_cast<uint64>(edx) << 32) | eax;
jiesun@chromium.orgc8420402011-02-17 03:51:58 +090090}
91
agl@chromium.orga3f13f52013-11-23 03:35:03 +090092#endif // !_MSC_VER
jiesun@chromium.orgc8420402011-02-17 03:51:58 +090093#endif // ARCH_CPU_X86_FAMILY
94
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +090095#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
agl@chromium.org41be0792014-08-08 17:45:24 +090096class LazyCpuInfoValue {
97 public:
98 LazyCpuInfoValue() : has_broken_neon_(false) {
99 // This function finds the value from /proc/cpuinfo under the key "model
100 // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7
101 // and later for arm64) and is shown once per CPU. "Processor" is used in
102 // earler versions and is shown only once at the top of /proc/cpuinfo
103 // regardless of the number CPUs.
104 const char kModelNamePrefix[] = "model name\t: ";
105 const char kProcessorPrefix[] = "Processor\t: ";
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900106
agl@chromium.org41be0792014-08-08 17:45:24 +0900107 // This function also calculates whether we believe that this CPU has a
108 // broken NEON unit based on these fields from cpuinfo:
109 unsigned implementer = 0, architecture = 0, variant = 0, part = 0,
110 revision = 0;
111 const struct {
112 const char key[17];
113 unsigned *result;
114 } kUnsignedValues[] = {
115 {"CPU implementer", &implementer},
116 {"CPU architecture", &architecture},
117 {"CPU variant", &variant},
118 {"CPU part", &part},
119 {"CPU revision", &revision},
120 };
121
122 std::string contents;
123 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
124 DCHECK(!contents.empty());
125 if (contents.empty()) {
126 return;
127 }
128
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900129 std::istringstream iss(contents);
130 std::string line;
131 while (std::getline(iss, line)) {
agl@chromium.org41be0792014-08-08 17:45:24 +0900132 if (brand_.empty() &&
133 (line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0 ||
134 line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0)) {
135 brand_.assign(line.substr(strlen(kModelNamePrefix)));
grunell@chromium.orge48fc652014-05-06 18:31:00 +0900136 }
agl@chromium.org41be0792014-08-08 17:45:24 +0900137
138 for (size_t i = 0; i < arraysize(kUnsignedValues); i++) {
139 const char *key = kUnsignedValues[i].key;
140 const size_t len = strlen(key);
141
142 if (line.compare(0, len, key) == 0 &&
143 line.size() >= len + 1 &&
144 (line[len] == '\t' || line[len] == ' ' || line[len] == ':')) {
145 size_t colon_pos = line.find(':', len);
146 if (colon_pos == std::string::npos) {
147 continue;
148 }
149
150 const StringPiece line_sp(line);
151 StringPiece value_sp = line_sp.substr(colon_pos + 1);
152 while (!value_sp.empty() &&
153 (value_sp[0] == ' ' || value_sp[0] == '\t')) {
154 value_sp = value_sp.substr(1);
155 }
156
157 // The string may have leading "0x" or not, so we use strtoul to
158 // handle that.
159 char *endptr;
160 std::string value(value_sp.as_string());
161 unsigned long int result = strtoul(value.c_str(), &endptr, 0);
162 if (*endptr == 0 && result <= UINT_MAX) {
163 *kUnsignedValues[i].result = result;
164 }
165 }
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900166 }
167 }
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900168
agl@chromium.org41be0792014-08-08 17:45:24 +0900169 has_broken_neon_ =
170 implementer == 0x51 &&
171 architecture == 7 &&
172 variant == 1 &&
173 part == 0x4d &&
174 revision == 0;
175 }
176
177 const std::string& brand() const { return brand_; }
178 bool has_broken_neon() const { return has_broken_neon_; }
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900179
180 private:
agl@chromium.org41be0792014-08-08 17:45:24 +0900181 std::string brand_;
182 bool has_broken_neon_;
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900183 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue);
184};
185
agl@chromium.org41be0792014-08-08 17:45:24 +0900186base::LazyInstance<LazyCpuInfoValue>::Leaky g_lazy_cpuinfo =
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900187 LAZY_INSTANCE_INITIALIZER;
188
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900189#endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
190 // defined(OS_LINUX))
191
agl@chromium.orga3f13f52013-11-23 03:35:03 +0900192} // anonymous namespace
193
maruel@google.com8178de22008-09-26 07:08:44 +0900194void CPU::Initialize() {
jiesun@chromium.orgc8420402011-02-17 03:51:58 +0900195#if defined(ARCH_CPU_X86_FAMILY)
maruel@google.com8178de22008-09-26 07:08:44 +0900196 int cpu_info[4] = {-1};
chunyang.dai@intel.com94d8f282012-10-05 06:05:23 +0900197 char cpu_string[48];
maruel@google.com8178de22008-09-26 07:08:44 +0900198
199 // __cpuid with an InfoType argument of 0 returns the number of
200 // valid Ids in CPUInfo[0] and the CPU identification string in
201 // the other three array elements. The CPU identification string is
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900202 // not in linear order. The code below arranges the information
chunyang.dai@intel.com94d8f282012-10-05 06:05:23 +0900203 // in a human readable form. The human readable order is CPUInfo[1] |
204 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
205 // before using memcpy to copy these three array elements to cpu_string.
maruel@google.com8178de22008-09-26 07:08:44 +0900206 __cpuid(cpu_info, 0);
207 int num_ids = cpu_info[0];
chunyang.dai@intel.com94d8f282012-10-05 06:05:23 +0900208 std::swap(cpu_info[2], cpu_info[3]);
209 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
210 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
maruel@google.com8178de22008-09-26 07:08:44 +0900211
212 // Interpret CPU feature information.
mbelshe@google.com4591e482008-09-27 08:26:34 +0900213 if (num_ids > 0) {
214 __cpuid(cpu_info, 1);
simonjam@chromium.org7bf2f7b2013-07-18 20:13:28 +0900215 signature_ = cpu_info[0];
mbelshe@google.com4591e482008-09-27 08:26:34 +0900216 stepping_ = cpu_info[0] & 0xf;
jiesun@chromium.orgc8420402011-02-17 03:51:58 +0900217 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
mbelshe@google.com4591e482008-09-27 08:26:34 +0900218 family_ = (cpu_info[0] >> 8) & 0xf;
219 type_ = (cpu_info[0] >> 12) & 0x3;
220 ext_model_ = (cpu_info[0] >> 16) & 0xf;
221 ext_family_ = (cpu_info[0] >> 20) & 0xff;
agl@chromium.orga3f13f52013-11-23 03:35:03 +0900222 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
223 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
224 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
225 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
jiesun@chromium.orgc8420402011-02-17 03:51:58 +0900226 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
227 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
228 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
agl@chromium.orga3f13f52013-11-23 03:35:03 +0900229 has_avx_hardware_ =
230 (cpu_info[2] & 0x10000000) != 0;
231 // AVX instructions will generate an illegal instruction exception unless
232 // a) they are supported by the CPU,
233 // b) XSAVE is supported by the CPU and
234 // c) XSAVE is enabled by the kernel.
235 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
agl@chromium.orga1e3a3e2014-05-29 08:28:48 +0900236 //
237 // In addition, we have observed some crashes with the xgetbv instruction
238 // even after following Intel's example code. (See crbug.com/375968.)
239 // Because of that, we also test the XSAVE bit because its description in
240 // the CPUID documentation suggests that it signals xgetbv support.
agl@chromium.orga3f13f52013-11-23 03:35:03 +0900241 has_avx_ =
242 has_avx_hardware_ &&
agl@chromium.orga1e3a3e2014-05-29 08:28:48 +0900243 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
agl@chromium.orga3f13f52013-11-23 03:35:03 +0900244 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
245 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
agl@chromium.orgabda8512013-12-03 01:15:03 +0900246 has_aesni_ = (cpu_info[2] & 0x02000000) != 0;
mbelshe@google.com4591e482008-09-27 08:26:34 +0900247 }
chunyang.dai@intel.com94d8f282012-10-05 06:05:23 +0900248
249 // Get the brand string of the cpu.
250 __cpuid(cpu_info, 0x80000000);
251 const int parameter_end = 0x80000004;
simonjam@chromium.orge50c7cb2013-05-01 04:46:05 +0900252 int max_parameter = cpu_info[0];
chunyang.dai@intel.com94d8f282012-10-05 06:05:23 +0900253
254 if (cpu_info[0] >= parameter_end) {
255 char* cpu_string_ptr = cpu_string;
256
257 for (int parameter = 0x80000002; parameter <= parameter_end &&
258 cpu_string_ptr < &cpu_string[sizeof(cpu_string)]; parameter++) {
259 __cpuid(cpu_info, parameter);
260 memcpy(cpu_string_ptr, cpu_info, sizeof(cpu_info));
261 cpu_string_ptr += sizeof(cpu_info);
262 }
263 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string);
264 }
simonjam@chromium.orge50c7cb2013-05-01 04:46:05 +0900265
266 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007;
267 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) {
268 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
269 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
270 }
puttaraju.r@samsung.com11bb1852014-04-29 00:33:26 +0900271#elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
agl@chromium.org41be0792014-08-08 17:45:24 +0900272 cpu_brand_.assign(g_lazy_cpuinfo.Get().brand());
273 has_broken_neon_ = g_lazy_cpuinfo.Get().has_broken_neon();
jiesun@chromium.orgc8420402011-02-17 03:51:58 +0900274#endif
maruel@google.com8178de22008-09-26 07:08:44 +0900275}
276
whunt@chromium.orga5dbde42013-02-02 10:10:02 +0900277CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const {
278 if (has_avx()) return AVX;
279 if (has_sse42()) return SSE42;
280 if (has_sse41()) return SSE41;
281 if (has_ssse3()) return SSSE3;
282 if (has_sse3()) return SSE3;
283 if (has_sse2()) return SSE2;
284 if (has_sse()) return SSE;
285 return PENTIUM;
286}
287
maruel@google.com8178de22008-09-26 07:08:44 +0900288} // namespace base