1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 #include "base/basictypes.h"
12 #include "build/build_config.h"
14 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
15 #include "base/file_util.h"
16 #include "base/lazy_instance.h"
19 #if defined(ARCH_CPU_X86_FAMILY)
22 #include <immintrin.h> // For _xgetbv()
44 has_avx_hardware_(false),
46 has_non_stop_time_stamp_counter_(false),
47 cpu_vendor_("unknown") {
53 #if defined(ARCH_CPU_X86_FAMILY)
56 #if defined(__pic__) && defined(__i386__)
58 void __cpuid(int cpu_info
[4], int info_type
) {
63 : "=a"(cpu_info
[0]), "=D"(cpu_info
[1]), "=c"(cpu_info
[2]), "=d"(cpu_info
[3])
70 void __cpuid(int cpu_info
[4], int info_type
) {
73 : "=a"(cpu_info
[0]), "=b"(cpu_info
[1]), "=c"(cpu_info
[2]), "=d"(cpu_info
[3])
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.
82 uint64
_xgetbv(uint32 xcr
) {
85 __asm__
volatile ("xgetbv" : "=a" (eax
), "=d" (edx
) : "c" (xcr
));
86 return (static_cast<uint64
>(edx
) << 32) | eax
;
90 #endif // ARCH_CPU_X86_FAMILY
92 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
94 // 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.
98 std::string
ParseCpuInfo() {
99 const char kModelNamePrefix
[] = "model name\t: ";
100 const char kProcessorPrefix
[] = "Processor\t: ";
101 std::string contents
;
102 ReadFileToString(FilePath("/proc/cpuinfo"), &contents
);
103 DCHECK(!contents
.empty());
104 std::string cpu_brand
;
105 if (!contents
.empty()) {
106 std::istringstream
iss(contents
);
108 while (std::getline(iss
, line
)) {
109 if (line
.compare(0, strlen(kModelNamePrefix
), kModelNamePrefix
) == 0) {
110 cpu_brand
.assign(line
.substr(strlen(kModelNamePrefix
)));
113 if (line
.compare(0, strlen(kProcessorPrefix
), kProcessorPrefix
) == 0) {
114 cpu_brand
.assign(line
.substr(strlen(kProcessorPrefix
)));
122 class LazyCpuInfoValue
{
124 LazyCpuInfoValue() : value_(ParseCpuInfo()) {}
125 const std::string
& value() { return value_
; }
128 const std::string value_
;
129 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue
);
132 base::LazyInstance
<LazyCpuInfoValue
> g_lazy_cpu_brand
=
133 LAZY_INSTANCE_INITIALIZER
;
135 const std::string
& CpuBrandInfo() {
136 return g_lazy_cpu_brand
.Get().value();
139 #endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
140 // defined(OS_LINUX))
142 } // anonymous namespace
144 void CPU::Initialize() {
145 #if defined(ARCH_CPU_X86_FAMILY)
146 int cpu_info
[4] = {-1};
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
152 // not in linear order. The code below arranges the information
153 // 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.
156 __cpuid(cpu_info
, 0);
157 int num_ids
= cpu_info
[0];
158 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]));
162 // Interpret CPU feature information.
164 __cpuid(cpu_info
, 1);
165 signature_
= cpu_info
[0];
166 stepping_
= cpu_info
[0] & 0xf;
167 model_
= ((cpu_info
[0] >> 4) & 0xf) + ((cpu_info
[0] >> 12) & 0xf0);
168 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;
172 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;
176 has_ssse3_
= (cpu_info
[2] & 0x00000200) != 0;
177 has_sse41_
= (cpu_info
[2] & 0x00080000) != 0;
178 has_sse42_
= (cpu_info
[2] & 0x00100000) != 0;
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
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.
193 (cpu_info
[2] & 0x04000000) != 0 /* XSAVE */ &&
194 (cpu_info
[2] & 0x08000000) != 0 /* OSXSAVE */ &&
195 (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */;
196 has_aesni_
= (cpu_info
[2] & 0x02000000) != 0;
199 // Get the brand string of the cpu.
200 __cpuid(cpu_info
, 0x80000000);
201 const int parameter_end
= 0x80000004;
202 int max_parameter
= cpu_info
[0];
204 if (cpu_info
[0] >= parameter_end
) {
205 char* cpu_string_ptr
= cpu_string
;
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
);
213 cpu_brand_
.assign(cpu_string
, cpu_string_ptr
- cpu_string
);
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;
221 #elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
222 cpu_brand_
.assign(CpuBrandInfo());
226 CPU::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
;