blob: af9c23da1a1ee2ceee74e48b729935032338472a [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
avi9b6f42932015-12-26 22:15:147#include <limits.h>
8#include <stddef.h>
9#include <stdint.h>
[email protected]d1811bc2012-03-31 07:08:5310#include <string.h>
11
[email protected]595d1592012-10-04 21:05:2312#include <algorithm>
13
avi9b6f42932015-12-26 22:15:1414#include "base/macros.h"
[email protected]d1811bc2012-03-31 07:08:5315#include "build/build_config.h"
16
[email protected]3a23f63c2014-04-28 15:33:2617#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
[email protected]e3177dd52014-08-13 20:22:1418#include "base/files/file_util.h"
[email protected]3a23f63c2014-04-28 15:33:2619#include "base/lazy_instance.h"
20#endif
21
[email protected]7e6d42b2011-02-16 18:51:5822#if defined(ARCH_CPU_X86_FAMILY)
23#if defined(_MSC_VER)
[email protected]de8d26672008-09-25 22:08:4424#include <intrin.h>
[email protected]f3d445e2013-11-22 18:35:0325#include <immintrin.h> // For _xgetbv()
[email protected]7e6d42b2011-02-16 18:51:5826#endif
27#endif
28
[email protected]de8d26672008-09-25 22:08:4429namespace base {
30
31CPU::CPU()
[email protected]5c8f89f692013-07-18 11:13:2832 : signature_(0),
33 type_(0),
[email protected]de8d26672008-09-25 22:08:4434 family_(0),
35 model_(0),
36 stepping_(0),
37 ext_model_(0),
38 ext_family_(0),
[email protected]7e6d42b2011-02-16 18:51:5839 has_mmx_(false),
40 has_sse_(false),
41 has_sse2_(false),
42 has_sse3_(false),
43 has_ssse3_(false),
44 has_sse41_(false),
45 has_sse42_(false),
ilevyb7d2f4082016-10-30 20:46:5746 has_popcnt_(false),
[email protected]f3d445e2013-11-22 18:35:0347 has_avx_(false),
fbarchard0ce41ae2015-10-02 03:23:1948 has_avx2_(false),
[email protected]b54d16d2013-12-02 16:15:0349 has_aesni_(false),
[email protected]aa312812013-04-30 19:46:0550 has_non_stop_time_stamp_counter_(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.
avi9b6f42932015-12-26 22:15:1486uint64_t _xgetbv(uint32_t xcr) {
87 uint32_t eax, edx;
[email protected]f3d445e2013-11-22 18:35:0388
fbarchard0ce41ae2015-10-02 03:23:1989 __asm__ volatile (
90 "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
avi9b6f42932015-12-26 22:15:1491 return (static_cast<uint64_t>(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:
davidben689d7132016-03-29 15:13:20100 LazyCpuInfoValue() {
[email protected]c37c1a8c2014-08-08 08:45:24101 // 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 std::string contents;
110 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
111 DCHECK(!contents.empty());
112 if (contents.empty()) {
113 return;
114 }
115
[email protected]3a23f63c2014-04-28 15:33:26116 std::istringstream iss(contents);
117 std::string line;
118 while (std::getline(iss, line)) {
[email protected]c37c1a8c2014-08-08 08:45:24119 if (brand_.empty() &&
120 (line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0 ||
121 line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0)) {
122 brand_.assign(line.substr(strlen(kModelNamePrefix)));
[email protected]652900112014-05-06 09:31:00123 }
[email protected]3a23f63c2014-04-28 15:33:26124 }
[email protected]c37c1a8c2014-08-08 08:45:24125 }
126
127 const std::string& brand() const { return brand_; }
[email protected]3a23f63c2014-04-28 15:33:26128
129 private:
[email protected]c37c1a8c2014-08-08 08:45:24130 std::string brand_;
[email protected]3a23f63c2014-04-28 15:33:26131 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue);
132};
133
[email protected]c37c1a8c2014-08-08 08:45:24134base::LazyInstance<LazyCpuInfoValue>::Leaky g_lazy_cpuinfo =
[email protected]3a23f63c2014-04-28 15:33:26135 LAZY_INSTANCE_INITIALIZER;
136
[email protected]3a23f63c2014-04-28 15:33:26137#endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
138 // defined(OS_LINUX))
139
[email protected]f3d445e2013-11-22 18:35:03140} // anonymous namespace
141
[email protected]de8d26672008-09-25 22:08:44142void CPU::Initialize() {
[email protected]7e6d42b2011-02-16 18:51:58143#if defined(ARCH_CPU_X86_FAMILY)
[email protected]de8d26672008-09-25 22:08:44144 int cpu_info[4] = {-1};
[email protected]595d1592012-10-04 21:05:23145 char cpu_string[48];
[email protected]de8d26672008-09-25 22:08:44146
147 // __cpuid with an InfoType argument of 0 returns the number of
148 // valid Ids in CPUInfo[0] and the CPU identification string in
149 // the other three array elements. The CPU identification string is
[email protected]52a261f2009-03-03 15:01:12150 // not in linear order. The code below arranges the information
[email protected]595d1592012-10-04 21:05:23151 // in a human readable form. The human readable order is CPUInfo[1] |
152 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
153 // before using memcpy to copy these three array elements to cpu_string.
[email protected]de8d26672008-09-25 22:08:44154 __cpuid(cpu_info, 0);
155 int num_ids = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23156 std::swap(cpu_info[2], cpu_info[3]);
157 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
158 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
[email protected]de8d26672008-09-25 22:08:44159
160 // Interpret CPU feature information.
[email protected]7f0813642008-09-26 23:26:34161 if (num_ids > 0) {
fbarchard0ce41ae2015-10-02 03:23:19162 int cpu_info7[4] = {0};
[email protected]7f0813642008-09-26 23:26:34163 __cpuid(cpu_info, 1);
fbarchard0ce41ae2015-10-02 03:23:19164 if (num_ids >= 7) {
165 __cpuid(cpu_info7, 7);
166 }
[email protected]5c8f89f692013-07-18 11:13:28167 signature_ = cpu_info[0];
[email protected]7f0813642008-09-26 23:26:34168 stepping_ = cpu_info[0] & 0xf;
[email protected]7e6d42b2011-02-16 18:51:58169 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
[email protected]7f0813642008-09-26 23:26:34170 family_ = (cpu_info[0] >> 8) & 0xf;
171 type_ = (cpu_info[0] >> 12) & 0x3;
172 ext_model_ = (cpu_info[0] >> 16) & 0xf;
173 ext_family_ = (cpu_info[0] >> 20) & 0xff;
[email protected]f3d445e2013-11-22 18:35:03174 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
175 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
176 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
177 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
[email protected]7e6d42b2011-02-16 18:51:58178 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
179 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
180 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
ilevyb7d2f4082016-10-30 20:46:57181 has_popcnt_ = (cpu_info[2] & 0x00800000) != 0;
182
[email protected]f3d445e2013-11-22 18:35:03183 // AVX instructions will generate an illegal instruction exception unless
184 // a) they are supported by the CPU,
185 // b) XSAVE is supported by the CPU and
186 // c) XSAVE is enabled by the kernel.
187 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
[email protected]26ce2f62014-05-28 23:28:48188 //
189 // In addition, we have observed some crashes with the xgetbv instruction
190 // even after following Intel's example code. (See crbug.com/375968.)
191 // Because of that, we also test the XSAVE bit because its description in
192 // the CPUID documentation suggests that it signals xgetbv support.
[email protected]f3d445e2013-11-22 18:35:03193 has_avx_ =
fbarchardf813a0ed2015-10-06 17:42:10194 (cpu_info[2] & 0x10000000) != 0 &&
[email protected]26ce2f62014-05-28 23:28:48195 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
[email protected]f3d445e2013-11-22 18:35:03196 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
197 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
[email protected]b54d16d2013-12-02 16:15:03198 has_aesni_ = (cpu_info[2] & 0x02000000) != 0;
fbarchard0ce41ae2015-10-02 03:23:19199 has_avx2_ = has_avx_ && (cpu_info7[1] & 0x00000020) != 0;
[email protected]7f0813642008-09-26 23:26:34200 }
[email protected]595d1592012-10-04 21:05:23201
202 // Get the brand string of the cpu.
203 __cpuid(cpu_info, 0x80000000);
204 const int parameter_end = 0x80000004;
[email protected]aa312812013-04-30 19:46:05205 int max_parameter = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23206
207 if (cpu_info[0] >= parameter_end) {
208 char* cpu_string_ptr = cpu_string;
209
210 for (int parameter = 0x80000002; parameter <= parameter_end &&
211 cpu_string_ptr < &cpu_string[sizeof(cpu_string)]; parameter++) {
212 __cpuid(cpu_info, parameter);
213 memcpy(cpu_string_ptr, cpu_info, sizeof(cpu_info));
214 cpu_string_ptr += sizeof(cpu_info);
215 }
216 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string);
217 }
[email protected]aa312812013-04-30 19:46:05218
219 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007;
220 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) {
221 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
222 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
223 }
[email protected]3a23f63c2014-04-28 15:33:26224#elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
[email protected]c37c1a8c2014-08-08 08:45:24225 cpu_brand_.assign(g_lazy_cpuinfo.Get().brand());
[email protected]7e6d42b2011-02-16 18:51:58226#endif
[email protected]de8d26672008-09-25 22:08:44227}
228
[email protected]5016a9dd2013-02-02 01:10:02229CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const {
fbarchard0ce41ae2015-10-02 03:23:19230 if (has_avx2()) return AVX2;
[email protected]5016a9dd2013-02-02 01:10:02231 if (has_avx()) return AVX;
232 if (has_sse42()) return SSE42;
233 if (has_sse41()) return SSE41;
234 if (has_ssse3()) return SSSE3;
235 if (has_sse3()) return SSE3;
236 if (has_sse2()) return SSE2;
237 if (has_sse()) return SSE;
238 return PENTIUM;
239}
240
[email protected]de8d26672008-09-25 22:08:44241} // namespace base