blob: de4a001f7f71210b0924688154431de45ee69b5f [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),
[email protected]f3d445e2013-11-22 18:35:0346 has_avx_(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]de8d26672008-09-25 22:08:4450 cpu_vendor_("unknown") {
51 Initialize();
52}
53
[email protected]f3d445e2013-11-22 18:35:0354namespace {
55
[email protected]7e6d42b2011-02-16 18:51:5856#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
[email protected]7e6d42b2011-02-16 18:51:5871#else
72
73void __cpuid(int cpu_info[4], int info_type) {
74 __asm__ volatile (
fbarchard0ce41ae2015-10-02 03:23:1975 "cpuid\n"
[email protected]7e6d42b2011-02-16 18:51:5876 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
77 : "a"(info_type)
78 );
79}
80
[email protected]f3d445e2013-11-22 18:35:0381#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.
avi9b6f42932015-12-26 22:15:1485uint64_t _xgetbv(uint32_t xcr) {
86 uint32_t eax, edx;
[email protected]f3d445e2013-11-22 18:35:0387
fbarchard0ce41ae2015-10-02 03:23:1988 __asm__ volatile (
89 "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
avi9b6f42932015-12-26 22:15:1490 return (static_cast<uint64_t>(edx) << 32) | eax;
[email protected]7e6d42b2011-02-16 18:51:5891}
92
[email protected]f3d445e2013-11-22 18:35:0393#endif // !_MSC_VER
[email protected]7e6d42b2011-02-16 18:51:5894#endif // ARCH_CPU_X86_FAMILY
95
[email protected]3a23f63c2014-04-28 15:33:2696#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
[email protected]c37c1a8c2014-08-08 08:45:2497class LazyCpuInfoValue {
98 public:
davidben689d7132016-03-29 15:13:2099 LazyCpuInfoValue() {
[email protected]c37c1a8c2014-08-08 08:45:24100 // This function finds the value from /proc/cpuinfo under the key "model
101 // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7
102 // and later for arm64) and is shown once per CPU. "Processor" is used in
103 // earler versions and is shown only once at the top of /proc/cpuinfo
104 // regardless of the number CPUs.
105 const char kModelNamePrefix[] = "model name\t: ";
106 const char kProcessorPrefix[] = "Processor\t: ";
[email protected]3a23f63c2014-04-28 15:33:26107
[email protected]c37c1a8c2014-08-08 08:45:24108 std::string contents;
109 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
110 DCHECK(!contents.empty());
111 if (contents.empty()) {
112 return;
113 }
114
[email protected]3a23f63c2014-04-28 15:33:26115 std::istringstream iss(contents);
116 std::string line;
117 while (std::getline(iss, line)) {
[email protected]c37c1a8c2014-08-08 08:45:24118 if (brand_.empty() &&
119 (line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0 ||
120 line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0)) {
121 brand_.assign(line.substr(strlen(kModelNamePrefix)));
[email protected]652900112014-05-06 09:31:00122 }
[email protected]3a23f63c2014-04-28 15:33:26123 }
[email protected]c37c1a8c2014-08-08 08:45:24124 }
125
126 const std::string& brand() const { return brand_; }
[email protected]3a23f63c2014-04-28 15:33:26127
128 private:
[email protected]c37c1a8c2014-08-08 08:45:24129 std::string brand_;
[email protected]3a23f63c2014-04-28 15:33:26130 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue);
131};
132
[email protected]c37c1a8c2014-08-08 08:45:24133base::LazyInstance<LazyCpuInfoValue>::Leaky g_lazy_cpuinfo =
[email protected]3a23f63c2014-04-28 15:33:26134 LAZY_INSTANCE_INITIALIZER;
135
[email protected]3a23f63c2014-04-28 15:33:26136#endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
137 // defined(OS_LINUX))
138
[email protected]f3d445e2013-11-22 18:35:03139} // anonymous namespace
140
[email protected]de8d26672008-09-25 22:08:44141void CPU::Initialize() {
[email protected]7e6d42b2011-02-16 18:51:58142#if defined(ARCH_CPU_X86_FAMILY)
[email protected]de8d26672008-09-25 22:08:44143 int cpu_info[4] = {-1};
[email protected]595d1592012-10-04 21:05:23144 char cpu_string[48];
[email protected]de8d26672008-09-25 22:08:44145
146 // __cpuid with an InfoType argument of 0 returns the number of
147 // valid Ids in CPUInfo[0] and the CPU identification string in
148 // the other three array elements. The CPU identification string is
[email protected]52a261f2009-03-03 15:01:12149 // not in linear order. The code below arranges the information
[email protected]595d1592012-10-04 21:05:23150 // in a human readable form. The human readable order is CPUInfo[1] |
151 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
152 // before using memcpy to copy these three array elements to cpu_string.
[email protected]de8d26672008-09-25 22:08:44153 __cpuid(cpu_info, 0);
154 int num_ids = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23155 std::swap(cpu_info[2], cpu_info[3]);
156 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
157 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
[email protected]de8d26672008-09-25 22:08:44158
159 // Interpret CPU feature information.
[email protected]7f0813642008-09-26 23:26:34160 if (num_ids > 0) {
fbarchard0ce41ae2015-10-02 03:23:19161 int cpu_info7[4] = {0};
[email protected]7f0813642008-09-26 23:26:34162 __cpuid(cpu_info, 1);
fbarchard0ce41ae2015-10-02 03:23:19163 if (num_ids >= 7) {
164 __cpuid(cpu_info7, 7);
165 }
[email protected]5c8f89f692013-07-18 11:13:28166 signature_ = cpu_info[0];
[email protected]7f0813642008-09-26 23:26:34167 stepping_ = cpu_info[0] & 0xf;
[email protected]7e6d42b2011-02-16 18:51:58168 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
[email protected]7f0813642008-09-26 23:26:34169 family_ = (cpu_info[0] >> 8) & 0xf;
170 type_ = (cpu_info[0] >> 12) & 0x3;
171 ext_model_ = (cpu_info[0] >> 16) & 0xf;
172 ext_family_ = (cpu_info[0] >> 20) & 0xff;
[email protected]f3d445e2013-11-22 18:35:03173 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
174 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
175 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
176 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
[email protected]7e6d42b2011-02-16 18:51:58177 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
178 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
179 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
[email protected]f3d445e2013-11-22 18:35:03180 // AVX instructions will generate an illegal instruction exception unless
181 // a) they are supported by the CPU,
182 // b) XSAVE is supported by the CPU and
183 // c) XSAVE is enabled by the kernel.
184 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
[email protected]26ce2f62014-05-28 23:28:48185 //
186 // In addition, we have observed some crashes with the xgetbv instruction
187 // even after following Intel's example code. (See crbug.com/375968.)
188 // Because of that, we also test the XSAVE bit because its description in
189 // the CPUID documentation suggests that it signals xgetbv support.
[email protected]f3d445e2013-11-22 18:35:03190 has_avx_ =
fbarchardf813a0ed2015-10-06 17:42:10191 (cpu_info[2] & 0x10000000) != 0 &&
[email protected]26ce2f62014-05-28 23:28:48192 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
[email protected]f3d445e2013-11-22 18:35:03193 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
194 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
[email protected]b54d16d2013-12-02 16:15:03195 has_aesni_ = (cpu_info[2] & 0x02000000) != 0;
fbarchard0ce41ae2015-10-02 03:23:19196 has_avx2_ = has_avx_ && (cpu_info7[1] & 0x00000020) != 0;
[email protected]7f0813642008-09-26 23:26:34197 }
[email protected]595d1592012-10-04 21:05:23198
199 // Get the brand string of the cpu.
200 __cpuid(cpu_info, 0x80000000);
201 const int parameter_end = 0x80000004;
[email protected]aa312812013-04-30 19:46:05202 int max_parameter = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23203
204 if (cpu_info[0] >= parameter_end) {
205 char* cpu_string_ptr = cpu_string;
206
207 for (int parameter = 0x80000002; parameter <= parameter_end &&
208 cpu_string_ptr < &cpu_string[sizeof(cpu_string)]; parameter++) {
209 __cpuid(cpu_info, parameter);
210 memcpy(cpu_string_ptr, cpu_info, sizeof(cpu_info));
211 cpu_string_ptr += sizeof(cpu_info);
212 }
213 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string);
214 }
[email protected]aa312812013-04-30 19:46:05215
216 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007;
217 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) {
218 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
219 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
220 }
[email protected]3a23f63c2014-04-28 15:33:26221#elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
[email protected]c37c1a8c2014-08-08 08:45:24222 cpu_brand_.assign(g_lazy_cpuinfo.Get().brand());
[email protected]7e6d42b2011-02-16 18:51:58223#endif
[email protected]de8d26672008-09-25 22:08:44224}
225
[email protected]5016a9dd2013-02-02 01:10:02226CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const {
fbarchard0ce41ae2015-10-02 03:23:19227 if (has_avx2()) return AVX2;
[email protected]5016a9dd2013-02-02 01:10:02228 if (has_avx()) return AVX;
229 if (has_sse42()) return SSE42;
230 if (has_sse41()) return SSE41;
231 if (has_ssse3()) return SSSE3;
232 if (has_sse3()) return SSE3;
233 if (has_sse2()) return SSE2;
234 if (has_sse()) return SSE;
235 return PENTIUM;
236}
237
[email protected]de8d26672008-09-25 22:08:44238} // namespace base