Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/cpu.h" |
| 6 | |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
| 12 | #include <algorithm> |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 13 | #include <utility> |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 14 | |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 15 | #include "base/macros.h" |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 16 | #include "build/build_config.h" |
| 17 | |
| 18 | #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) |
| 19 | #include "base/files/file_util.h" |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | #if defined(ARCH_CPU_X86_FAMILY) |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 23 | #if defined(COMPILER_MSVC) |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 24 | #include <intrin.h> |
| 25 | #include <immintrin.h> // For _xgetbv() |
| 26 | #endif |
| 27 | #endif |
| 28 | |
| 29 | namespace base { |
| 30 | |
| 31 | CPU::CPU() |
| 32 | : signature_(0), |
| 33 | type_(0), |
| 34 | family_(0), |
| 35 | model_(0), |
| 36 | stepping_(0), |
| 37 | ext_model_(0), |
| 38 | ext_family_(0), |
| 39 | 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), |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 46 | has_popcnt_(false), |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 47 | has_avx_(false), |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 48 | has_avx2_(false), |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 49 | has_aesni_(false), |
| 50 | has_non_stop_time_stamp_counter_(false), |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 51 | cpu_vendor_("unknown") { |
| 52 | Initialize(); |
| 53 | } |
| 54 | |
| 55 | namespace { |
| 56 | |
| 57 | #if defined(ARCH_CPU_X86_FAMILY) |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 58 | #if !defined(COMPILER_MSVC) |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 59 | |
| 60 | #if defined(__pic__) && defined(__i386__) |
| 61 | |
| 62 | void __cpuid(int cpu_info[4], int info_type) { |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 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]), |
| 68 | "=d"(cpu_info[3]) |
| 69 | : "a"(info_type), "c"(0)); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | #else |
| 73 | |
| 74 | void __cpuid(int cpu_info[4], int info_type) { |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 75 | __asm__ volatile("cpuid\n" |
| 76 | : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), |
| 77 | "=d"(cpu_info[3]) |
| 78 | : "a"(info_type), "c"(0)); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | #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. |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 85 | uint64_t _xgetbv(uint32_t xcr) { |
| 86 | uint32_t eax, edx; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 87 | |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 88 | __asm__ volatile ( |
| 89 | "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); |
| 90 | return (static_cast<uint64_t>(edx) << 32) | eax; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 91 | } |
| 92 | |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 93 | #endif // !defined(COMPILER_MSVC) |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 94 | #endif // ARCH_CPU_X86_FAMILY |
| 95 | |
| 96 | #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 97 | std::string* CpuInfoBrand() { |
| 98 | static std::string* brand = []() { |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 99 | // This function finds the value from /proc/cpuinfo under the key "model |
| 100 | // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7 |
| 101 | // and later for arm64) and is shown once per CPU. "Processor" is used in |
| 102 | // earler versions and is shown only once at the top of /proc/cpuinfo |
| 103 | // regardless of the number CPUs. |
| 104 | const char kModelNamePrefix[] = "model name\t: "; |
| 105 | const char kProcessorPrefix[] = "Processor\t: "; |
| 106 | |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 107 | std::string contents; |
| 108 | ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
| 109 | DCHECK(!contents.empty()); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 110 | |
| 111 | std::istringstream iss(contents); |
| 112 | std::string line; |
| 113 | while (std::getline(iss, line)) { |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 114 | if (line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0) |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 115 | return new std::string(line.substr(strlen(kModelNamePrefix))); |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 116 | if (line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0) |
| 117 | return new std::string(line.substr(strlen(kProcessorPrefix))); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 118 | } |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 119 | |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 120 | return new std::string(); |
| 121 | }(); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 122 | |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 123 | return brand; |
| 124 | } |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 125 | #endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || |
| 126 | // defined(OS_LINUX)) |
| 127 | |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 128 | } // namespace |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 129 | |
| 130 | void CPU::Initialize() { |
| 131 | #if defined(ARCH_CPU_X86_FAMILY) |
| 132 | int cpu_info[4] = {-1}; |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 133 | // This array is used to temporarily hold the vendor name and then the brand |
| 134 | // name. Thus it has to be big enough for both use cases. There are |
| 135 | // static_asserts below for each of the use cases to make sure this array is |
| 136 | // big enough. |
| 137 | char cpu_string[sizeof(cpu_info) * 3 + 1]; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 138 | |
| 139 | // __cpuid with an InfoType argument of 0 returns the number of |
| 140 | // valid Ids in CPUInfo[0] and the CPU identification string in |
| 141 | // the other three array elements. The CPU identification string is |
| 142 | // not in linear order. The code below arranges the information |
| 143 | // in a human readable form. The human readable order is CPUInfo[1] | |
| 144 | // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 145 | // before using memcpy() to copy these three array elements to |cpu_string|. |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 146 | __cpuid(cpu_info, 0); |
| 147 | int num_ids = cpu_info[0]; |
| 148 | std::swap(cpu_info[2], cpu_info[3]); |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 149 | static constexpr size_t kVendorNameSize = 3 * sizeof(cpu_info[1]); |
| 150 | static_assert(kVendorNameSize < arraysize(cpu_string), |
| 151 | "cpu_string too small"); |
| 152 | memcpy(cpu_string, &cpu_info[1], kVendorNameSize); |
| 153 | cpu_string[kVendorNameSize] = '\0'; |
| 154 | cpu_vendor_ = cpu_string; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 155 | |
| 156 | // Interpret CPU feature information. |
| 157 | if (num_ids > 0) { |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 158 | int cpu_info7[4] = {0}; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 159 | __cpuid(cpu_info, 1); |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 160 | if (num_ids >= 7) { |
| 161 | __cpuid(cpu_info7, 7); |
| 162 | } |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 163 | signature_ = cpu_info[0]; |
| 164 | stepping_ = cpu_info[0] & 0xf; |
| 165 | model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0); |
| 166 | family_ = (cpu_info[0] >> 8) & 0xf; |
| 167 | type_ = (cpu_info[0] >> 12) & 0x3; |
| 168 | ext_model_ = (cpu_info[0] >> 16) & 0xf; |
| 169 | ext_family_ = (cpu_info[0] >> 20) & 0xff; |
| 170 | has_mmx_ = (cpu_info[3] & 0x00800000) != 0; |
| 171 | has_sse_ = (cpu_info[3] & 0x02000000) != 0; |
| 172 | has_sse2_ = (cpu_info[3] & 0x04000000) != 0; |
| 173 | has_sse3_ = (cpu_info[2] & 0x00000001) != 0; |
| 174 | has_ssse3_ = (cpu_info[2] & 0x00000200) != 0; |
| 175 | has_sse41_ = (cpu_info[2] & 0x00080000) != 0; |
| 176 | has_sse42_ = (cpu_info[2] & 0x00100000) != 0; |
Jay Civelli | 3a83cdd | 2017-03-22 17:31:44 -0700 | [diff] [blame] | 177 | has_popcnt_ = (cpu_info[2] & 0x00800000) != 0; |
| 178 | |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 179 | // AVX instructions will generate an illegal instruction exception unless |
| 180 | // a) they are supported by the CPU, |
| 181 | // b) XSAVE is supported by the CPU and |
| 182 | // c) XSAVE is enabled by the kernel. |
| 183 | // See http://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled |
| 184 | // |
| 185 | // In addition, we have observed some crashes with the xgetbv instruction |
| 186 | // even after following Intel's example code. (See crbug.com/375968.) |
| 187 | // Because of that, we also test the XSAVE bit because its description in |
| 188 | // the CPUID documentation suggests that it signals xgetbv support. |
| 189 | has_avx_ = |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 190 | (cpu_info[2] & 0x10000000) != 0 && |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 191 | (cpu_info[2] & 0x04000000) != 0 /* XSAVE */ && |
| 192 | (cpu_info[2] & 0x08000000) != 0 /* OSXSAVE */ && |
| 193 | (_xgetbv(0) & 6) == 6 /* XSAVE enabled by kernel */; |
| 194 | has_aesni_ = (cpu_info[2] & 0x02000000) != 0; |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 195 | has_avx2_ = has_avx_ && (cpu_info7[1] & 0x00000020) != 0; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Get the brand string of the cpu. |
| 199 | __cpuid(cpu_info, 0x80000000); |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 200 | const int max_parameter = cpu_info[0]; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 201 | |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 202 | static constexpr int kParameterStart = 0x80000002; |
| 203 | static constexpr int kParameterEnd = 0x80000004; |
| 204 | static constexpr int kParameterSize = kParameterEnd - kParameterStart + 1; |
| 205 | static_assert(kParameterSize * sizeof(cpu_info) + 1 == arraysize(cpu_string), |
| 206 | "cpu_string has wrong size"); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 207 | |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 208 | if (max_parameter >= kParameterEnd) { |
| 209 | size_t i = 0; |
| 210 | for (int parameter = kParameterStart; parameter <= kParameterEnd; |
| 211 | ++parameter) { |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 212 | __cpuid(cpu_info, parameter); |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 213 | memcpy(&cpu_string[i], cpu_info, sizeof(cpu_info)); |
| 214 | i += sizeof(cpu_info); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 215 | } |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 216 | cpu_string[i] = '\0'; |
| 217 | cpu_brand_ = cpu_string; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 218 | } |
| 219 | |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 220 | static constexpr int kParameterContainingNonStopTimeStampCounter = 0x80000007; |
| 221 | if (max_parameter >= kParameterContainingNonStopTimeStampCounter) { |
| 222 | __cpuid(cpu_info, kParameterContainingNonStopTimeStampCounter); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 223 | has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0; |
| 224 | } |
| 225 | #elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) |
Jakub Pawlowski | 86aa225 | 2017-04-05 09:22:29 -0700 | [diff] [blame^] | 226 | cpu_brand_ = *CpuInfoBrand(); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 227 | #endif |
| 228 | } |
| 229 | |
| 230 | CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const { |
Alex Vakulenko | 0d205d7 | 2016-01-15 13:02:14 -0800 | [diff] [blame] | 231 | if (has_avx2()) return AVX2; |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 232 | if (has_avx()) return AVX; |
| 233 | if (has_sse42()) return SSE42; |
| 234 | if (has_sse41()) return SSE41; |
| 235 | if (has_ssse3()) return SSSE3; |
| 236 | if (has_sse3()) return SSE3; |
| 237 | if (has_sse2()) return SSE2; |
| 238 | if (has_sse()) return SSE; |
| 239 | return PENTIUM; |
| 240 | } |
| 241 | |
| 242 | } // namespace base |