blob: dcba3b6e73e5aa7884d02b59794adfafa505ca9d [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]d1811bc2012-03-31 07:08:537#include <string.h>
8
[email protected]595d1592012-10-04 21:05:239#include <algorithm>
10
[email protected]f3d445e2013-11-22 18:35:0311#include "base/basictypes.h"
[email protected]d1811bc2012-03-31 07:08:5312#include "build/build_config.h"
13
[email protected]3a23f63c2014-04-28 15:33:2614#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
15#include "base/file_util.h"
16#include "base/lazy_instance.h"
17#endif
18
[email protected]7e6d42b2011-02-16 18:51:5819#if defined(ARCH_CPU_X86_FAMILY)
20#if defined(_MSC_VER)
[email protected]de8d26672008-09-25 22:08:4421#include <intrin.h>
[email protected]f3d445e2013-11-22 18:35:0322#include <immintrin.h> // For _xgetbv()
[email protected]7e6d42b2011-02-16 18:51:5823#endif
24#endif
25
[email protected]de8d26672008-09-25 22:08:4426namespace base {
27
28CPU::CPU()
[email protected]5c8f89f692013-07-18 11:13:2829 : signature_(0),
30 type_(0),
[email protected]de8d26672008-09-25 22:08:4431 family_(0),
32 model_(0),
33 stepping_(0),
34 ext_model_(0),
35 ext_family_(0),
[email protected]7e6d42b2011-02-16 18:51:5836 has_mmx_(false),
37 has_sse_(false),
38 has_sse2_(false),
39 has_sse3_(false),
40 has_ssse3_(false),
41 has_sse41_(false),
42 has_sse42_(false),
[email protected]f3d445e2013-11-22 18:35:0343 has_avx_(false),
44 has_avx_hardware_(false),
[email protected]b54d16d2013-12-02 16:15:0345 has_aesni_(false),
[email protected]aa312812013-04-30 19:46:0546 has_non_stop_time_stamp_counter_(false),
[email protected]de8d26672008-09-25 22:08:4447 cpu_vendor_("unknown") {
48 Initialize();
49}
50
[email protected]f3d445e2013-11-22 18:35:0351namespace {
52
[email protected]7e6d42b2011-02-16 18:51:5853#if defined(ARCH_CPU_X86_FAMILY)
54#ifndef _MSC_VER
55
56#if defined(__pic__) && defined(__i386__)
57
58void __cpuid(int cpu_info[4], int info_type) {
59 __asm__ volatile (
60 "mov %%ebx, %%edi\n"
61 "cpuid\n"
62 "xchg %%edi, %%ebx\n"
63 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
64 : "a"(info_type)
65 );
66}
67
[email protected]7e6d42b2011-02-16 18:51:5868#else
69
70void __cpuid(int cpu_info[4], int info_type) {
71 __asm__ volatile (
72 "cpuid \n\t"
73 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
74 : "a"(info_type)
75 );
76}
77
[email protected]f3d445e2013-11-22 18:35:0378#endif
79
80// _xgetbv returns the value of an Intel Extended Control Register (XCR).
81// Currently only XCR0 is defined by Intel so |xcr| should always be zero.
82uint64 _xgetbv(uint32 xcr) {
83 uint32 eax, edx;
84
85 __asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr));
86 return (static_cast<uint64>(edx) << 32) | eax;
[email protected]7e6d42b2011-02-16 18:51:5887}
88
[email protected]f3d445e2013-11-22 18:35:0389#endif // !_MSC_VER
[email protected]7e6d42b2011-02-16 18:51:5890#endif // ARCH_CPU_X86_FAMILY
91
[email protected]3a23f63c2014-04-28 15:33:2692#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
93
[email protected]652900112014-05-06 09:31:0094// Returns the string found in /proc/cpuinfo under the key "model name" or
95// "Processor". "model name" is used in Linux 3.8 and later (3.7 and later for
96// arm64) and is shown once per CPU. "Processor" is used in earler versions and
97// is shown only once at the top of /proc/cpuinfo regardless of the number CPUs.
[email protected]3a23f63c2014-04-28 15:33:2698std::string ParseCpuInfo() {
[email protected]652900112014-05-06 09:31:0099 const char kModelNamePrefix[] = "model name\t: ";
100 const char kProcessorPrefix[] = "Processor\t: ";
101 std::string contents;
[email protected]3a23f63c2014-04-28 15:33:26102 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
103 DCHECK(!contents.empty());
[email protected]652900112014-05-06 09:31:00104 std::string cpu_brand;
[email protected]3a23f63c2014-04-28 15:33:26105 if (!contents.empty()) {
106 std::istringstream iss(contents);
107 std::string line;
108 while (std::getline(iss, line)) {
[email protected]652900112014-05-06 09:31:00109 if (line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0) {
110 cpu_brand.assign(line.substr(strlen(kModelNamePrefix)));
111 break;
112 }
[email protected]3a23f63c2014-04-28 15:33:26113 if (line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0) {
[email protected]652900112014-05-06 09:31:00114 cpu_brand.assign(line.substr(strlen(kProcessorPrefix)));
115 break;
[email protected]3a23f63c2014-04-28 15:33:26116 }
117 }
118 }
119 return cpu_brand;
120}
121
122class LazyCpuInfoValue {
123 public:
124 LazyCpuInfoValue() : value_(ParseCpuInfo()) {}
125 const std::string& value() { return value_; }
126
127 private:
128 const std::string value_;
129 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue);
130};
131
132base::LazyInstance<LazyCpuInfoValue> g_lazy_cpu_brand =
133 LAZY_INSTANCE_INITIALIZER;
134
135const std::string& CpuBrandInfo() {
136 return g_lazy_cpu_brand.Get().value();
137}
138
139#endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
140 // defined(OS_LINUX))
141
[email protected]f3d445e2013-11-22 18:35:03142} // anonymous namespace
143
[email protected]de8d26672008-09-25 22:08:44144void CPU::Initialize() {
[email protected]7e6d42b2011-02-16 18:51:58145#if defined(ARCH_CPU_X86_FAMILY)
[email protected]de8d26672008-09-25 22:08:44146 int cpu_info[4] = {-1};
[email protected]595d1592012-10-04 21:05:23147 char cpu_string[48];
[email protected]de8d26672008-09-25 22:08:44148
149 // __cpuid with an InfoType argument of 0 returns the number of
150 // valid Ids in CPUInfo[0] and the CPU identification string in
151 // the other three array elements. The CPU identification string is
[email protected]52a261f2009-03-03 15:01:12152 // not in linear order. The code below arranges the information
[email protected]595d1592012-10-04 21:05:23153 // in a human readable form. The human readable order is CPUInfo[1] |
154 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
155 // before using memcpy to copy these three array elements to cpu_string.
[email protected]de8d26672008-09-25 22:08:44156 __cpuid(cpu_info, 0);
157 int num_ids = cpu_info[0];
[email protected]595d1592012-10-04 21:05:23158 std::swap(cpu_info[2], cpu_info[3]);
159 memcpy(cpu_string, &cpu_info[1], 3 * sizeof(cpu_info[1]));
160 cpu_vendor_.assign(cpu_string, 3 * sizeof(cpu_info[1]));
[email protected]de8d26672008-09-25 22:08:44161
162 // Interpret CPU feature information.
[email protected]7f0813642008-09-26 23:26:34163 if (num_ids > 0) {
164 __cpuid(cpu_info, 1);
[email protected]5c8f89f692013-07-18 11:13:28165 signature_ = cpu_info[0];
[email protected]7f0813642008-09-26 23:26:34166 stepping_ = cpu_info[0] & 0xf;
[email protected]7e6d42b2011-02-16 18:51:58167 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
[email protected]7f0813642008-09-26 23:26:34168 family_ = (cpu_info[0] >> 8) & 0xf;
169 type_ = (cpu_info[0] >> 12) & 0x3;
170 ext_model_ = (cpu_info[0] >> 16) & 0xf;
171 ext_family_ = (cpu_info[0] >> 20) & 0xff;
[email protected]f3d445e2013-11-22 18:35:03172 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
173 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
174 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
175 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
[email protected]7e6d42b2011-02-16 18:51:58176 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
177 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
178 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
[email protected]f3d445e2013-11-22 18:35:03179 has_avx_hardware_ =
180 (cpu_info[2] & 0x10000000) != 0;
181 // AVX instructions will generate an illegal instruction exception unless
182 // a) they are supported by the CPU,
183 // b) XSAVE is supported by the CPU and
184 // c) XSAVE is enabled by the kernel.
185 // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled
[email protected]26ce2f62014-05-28 23:28:48186 //
187 // In addition, we have observed some crashes with the xgetbv instruction
188 // even after following Intel's example code. (See crbug.com/375968.)
189 // Because of that, we also test the XSAVE bit because its description in
190 // the CPUID documentation suggests that it signals xgetbv support.
[email protected]f3d445e2013-11-22 18:35:03191 has_avx_ =
192 has_avx_hardware_ &&
[email protected]26ce2f62014-05-28 23:28:48193 (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ &&
[email protected]f3d445e2013-11-22 18:35:03194 (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ &&
195 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
[email protected]b54d16d2013-12-02 16:15:03196 has_aesni_ = (cpu_info[2] & 0x02000000) != 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))
222 cpu_brand_.assign(CpuBrandInfo());
[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 {
227 if (has_avx()) return AVX;
228 if (has_sse42()) return SSE42;
229 if (has_sse41()) return SSE41;
230 if (has_ssse3()) return SSSE3;
231 if (has_sse3()) return SSE3;
232 if (has_sse2()) return SSE2;
233 if (has_sse()) return SSE;
234 return PENTIUM;
235}
236
[email protected]de8d26672008-09-25 22:08:44237} // namespace base