blob: 848208f7c1024fb9dda6d6e15e8a8b17628e538f [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#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),
ilevyb7d2f4082016-10-30 20:46:5745 has_popcnt_(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) {
mtkleinc0fe8912017-01-03 18:33:4362 __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]),
67 "=d"(cpu_info[3])
68 : "a"(info_type), "c"(0));
[email protected]7e6d42b2011-02-16 18:51:5869}
70
[email protected]7e6d42b2011-02-16 18:51:5871#else
72
73void __cpuid(int cpu_info[4], int info_type) {
mtkleinc0fe8912017-01-03 18:33:4374 __asm__ volatile("cpuid\n"
75 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]),
76 "=d"(cpu_info[3])
77 : "a"(info_type), "c"(0));
[email protected]7e6d42b2011-02-16 18:51:5878}
79
[email protected]f3d445e2013-11-22 18:35:0380#endif
81
82// _xgetbv returns the value of an Intel Extended Control Register (XCR).
83// Currently only XCR0 is defined by Intel so |xcr| should always be zero.
avi9b6f42932015-12-26 22:15:1484uint64_t _xgetbv(uint32_t xcr) {
85 uint32_t eax, edx;
[email protected]f3d445e2013-11-22 18:35:0386
fbarchard0ce41ae2015-10-02 03:23:1987 __asm__ volatile (
88 "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
avi9b6f42932015-12-26 22:15:1489 return (static_cast<uint64_t>(edx) << 32) | eax;
[email protected]7e6d42b2011-02-16 18:51:5890}
91
[email protected]f3d445e2013-11-22 18:35:0392#endif // !_MSC_VER
[email protected]7e6d42b2011-02-16 18:51:5893#endif // ARCH_CPU_X86_FAMILY
94
[email protected]3a23f63c2014-04-28 15:33:2695#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
scottmg6ece5ae2017-02-01 18:25:1996std::string* CpuInfoBrand() {
97 static std::string* brand = []() {
[email protected]c37c1a8c2014-08-08 08:45:2498 // This function finds the value from /proc/cpuinfo under the key "model
99 // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7
100 // and later for arm64) and is shown once per CPU. "Processor" is used in
101 // earler versions and is shown only once at the top of /proc/cpuinfo
102 // regardless of the number CPUs.
103 const char kModelNamePrefix[] = "model name\t: ";
104 const char kProcessorPrefix[] = "Processor\t: ";
[email protected]3a23f63c2014-04-28 15:33:26105
[email protected]c37c1a8c2014-08-08 08:45:24106 std::string contents;
107 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
108 DCHECK(!contents.empty());
109 if (contents.empty()) {
scottmg6ece5ae2017-02-01 18:25:19110 return new std::string();
[email protected]c37c1a8c2014-08-08 08:45:24111 }
112
[email protected]3a23f63c2014-04-28 15:33:26113 std::istringstream iss(contents);
114 std::string line;
115 while (std::getline(iss, line)) {
scottmg6ece5ae2017-02-01 18:25:19116 if ((line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0 ||
[email protected]c37c1a8c2014-08-08 08:45:24117 line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0)) {
scottmg6ece5ae2017-02-01 18:25:19118 return new std::string(line.substr(strlen(kModelNamePrefix)));
[email protected]652900112014-05-06 09:31:00119 }
[email protected]3a23f63c2014-04-28 15:33:26120 }
[email protected]c37c1a8c2014-08-08 08:45:24121
scottmg6ece5ae2017-02-01 18:25:19122 return new std::string();
123 }();
[email protected]3a23f63c2014-04-28 15:33:26124
scottmg6ece5ae2017-02-01 18:25:19125 return brand;
126}
[email protected]3a23f63c2014-04-28 15:33:26127#endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
128 // defined(OS_LINUX))
129
[email protected]f3d445e2013-11-22 18:35:03130} // anonymous namespace
131
[email protected]de8d26672008-09-25 22:08:44132void CPU::Initialize() {
[email protected]7e6d42b2011-02-16 18:51:58133#if defined(ARCH_CPU_X86_FAMILY)
[email protected]de8d26672008-09-25 22:08:44134 int cpu_info[4] = {-1};
[email protected]595d1592012-10-04 21:05:23135 char cpu_string[48];
[email protected]de8d26672008-09-25 22:08:44136
137 // __cpuid with an InfoType argument of 0 returns the number of
138 // valid Ids in CPUInfo[0] and the CPU identification string in
139 // the other three array elements. The CPU identification string is
[email protected]52a261f2009-03-03 15:01:12140 // not in linear order. The code below arranges the information
[email protected]595d1592012-10-04 21:05:23141 // in a human readable form. The human readable order is CPUInfo[1] |
142 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
143 // before using memcpy to copy these three array elements to cpu_string.
[email protected]de8d26672008-09-25 22:08:44144 __cpuid(cpu_info, 0);
145 int num_ids = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23146 std::swap(cpu_info[2], cpu_info[3]);
147 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
148 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
[email protected]de8d26672008-09-25 22:08:44149
150 // Interpret CPU feature information.
[email protected]7f0813642008-09-26 23:26:34151 if (num_ids > 0) {
fbarchard0ce41ae2015-10-02 03:23:19152 int cpu_info7[4] = {0};
[email protected]7f0813642008-09-26 23:26:34153 __cpuid(cpu_info, 1);
fbarchard0ce41ae2015-10-02 03:23:19154 if (num_ids >= 7) {
155 __cpuid(cpu_info7, 7);
156 }
[email protected]5c8f89f692013-07-18 11:13:28157 signature_ = cpu_info[0];
[email protected]7f0813642008-09-26 23:26:34158 stepping_ = cpu_info[0] & 0xf;
[email protected]7e6d42b2011-02-16 18:51:58159 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
[email protected]7f0813642008-09-26 23:26:34160 family_ = (cpu_info[0] >> 8) & 0xf;
161 type_ = (cpu_info[0] >> 12) & 0x3;
162 ext_model_ = (cpu_info[0] >> 16) & 0xf;
163 ext_family_ = (cpu_info[0] >> 20) & 0xff;
[email protected]f3d445e2013-11-22 18:35:03164 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
165 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
166 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
167 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
[email protected]7e6d42b2011-02-16 18:51:58168 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
169 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
170 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
ilevyb7d2f4082016-10-30 20:46:57171 has_popcnt_ = (cpu_info[2] & 0x00800000) != 0;
172
[email protected]f3d445e2013-11-22 18:35:03173 // AVX instructions will generate an illegal instruction exception unless
174 // a) they are supported by the CPU,
175 // b) XSAVE is supported by the CPU and
176 // c) XSAVE is enabled by the kernel.
177 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
[email protected]26ce2f62014-05-28 23:28:48178 //
179 // In addition, we have observed some crashes with the xgetbv instruction
180 // even after following Intel's example code. (See crbug.com/375968.)
181 // Because of that, we also test the XSAVE bit because its description in
182 // the CPUID documentation suggests that it signals xgetbv support.
[email protected]f3d445e2013-11-22 18:35:03183 has_avx_ =
fbarchardf813a0ed2015-10-06 17:42:10184 (cpu_info[2] & 0x10000000) != 0 &&
[email protected]26ce2f62014-05-28 23:28:48185 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
[email protected]f3d445e2013-11-22 18:35:03186 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
187 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
[email protected]b54d16d2013-12-02 16:15:03188 has_aesni_ = (cpu_info[2] & 0x02000000) != 0;
fbarchard0ce41ae2015-10-02 03:23:19189 has_avx2_ = has_avx_ && (cpu_info7[1] & 0x00000020) != 0;
[email protected]7f0813642008-09-26 23:26:34190 }
[email protected]595d1592012-10-04 21:05:23191
192 // Get the brand string of the cpu.
193 __cpuid(cpu_info, 0x80000000);
194 const int parameter_end = 0x80000004;
[email protected]aa312812013-04-30 19:46:05195 int max_parameter = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23196
197 if (cpu_info[0] >= parameter_end) {
198 char* cpu_string_ptr = cpu_string;
199
200 for (int parameter = 0x80000002; parameter <= parameter_end &&
201 cpu_string_ptr < &cpu_string[sizeof(cpu_string)]; parameter++) {
202 __cpuid(cpu_info, parameter);
203 memcpy(cpu_string_ptr, cpu_info, sizeof(cpu_info));
204 cpu_string_ptr += sizeof(cpu_info);
205 }
206 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string);
207 }
[email protected]aa312812013-04-30 19:46:05208
209 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007;
210 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) {
211 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
212 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
213 }
[email protected]3a23f63c2014-04-28 15:33:26214#elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
scottmg6ece5ae2017-02-01 18:25:19215 cpu_brand_.assign(*CpuInfoBrand());
[email protected]7e6d42b2011-02-16 18:51:58216#endif
[email protected]de8d26672008-09-25 22:08:44217}
218
[email protected]5016a9dd2013-02-02 01:10:02219CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const {
fbarchard0ce41ae2015-10-02 03:23:19220 if (has_avx2()) return AVX2;
[email protected]5016a9dd2013-02-02 01:10:02221 if (has_avx()) return AVX;
222 if (has_sse42()) return SSE42;
223 if (has_sse41()) return SSE41;
224 if (has_ssse3()) return SSSE3;
225 if (has_sse3()) return SSE3;
226 if (has_sse2()) return SSE2;
227 if (has_sse()) return SSE;
228 return PENTIUM;
229}
230
[email protected]de8d26672008-09-25 22:08:44231} // namespace base