1 // Copyright 2013 the V8 project 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.
5 #include "src/base/cpu.h"
8 #include <intrin.h> // __cpuid()
11 #include <linux/auxvec.h> // AT_HWCAP
13 #if V8_GLIBC_PREREQ(2, 16)
14 #include <sys/auxv.h> // getauxval()
17 #include <sys/syspage.h> // cpuinfo
20 #include <unistd.h> // sysconf()
30 #include "src/base/logging.h"
32 #include "src/base/win32-headers.h" // NOLINT
38 #if defined(__pnacl__)
39 // Portable host shouldn't do feature detection.
40 #elif V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
42 // Define __cpuid() for non-MSVC libraries.
45 static V8_INLINE void __cpuid(int cpu_info[4], int info_type) {
46 #if defined(__i386__) && defined(__pic__)
47 // Make sure to preserve ebx, which contains the pointer
48 // to the GOT in case we're generating PIC.
50 "mov %%ebx, %%edi\n\t"
52 "xchg %%edi, %%ebx\n\t"
53 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
59 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
62 #endif // defined(__i386__) && defined(__pic__)
65 #endif // !V8_LIBC_MSVCRT
67 #elif V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64 \
68 || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
74 // See <uapi/asm/hwcap.h> kernel header.
76 * HWCAP flags - for elf_hwcap (in kernel) and AT_HWCAP
78 #define HWCAP_SWP (1 << 0)
79 #define HWCAP_HALF (1 << 1)
80 #define HWCAP_THUMB (1 << 2)
81 #define HWCAP_26BIT (1 << 3) /* Play it safe */
82 #define HWCAP_FAST_MULT (1 << 4)
83 #define HWCAP_FPA (1 << 5)
84 #define HWCAP_VFP (1 << 6)
85 #define HWCAP_EDSP (1 << 7)
86 #define HWCAP_JAVA (1 << 8)
87 #define HWCAP_IWMMXT (1 << 9)
88 #define HWCAP_CRUNCH (1 << 10)
89 #define HWCAP_THUMBEE (1 << 11)
90 #define HWCAP_NEON (1 << 12)
91 #define HWCAP_VFPv3 (1 << 13)
92 #define HWCAP_VFPv3D16 (1 << 14) /* also set for VFPv4-D16 */
93 #define HWCAP_TLS (1 << 15)
94 #define HWCAP_VFPv4 (1 << 16)
95 #define HWCAP_IDIVA (1 << 17)
96 #define HWCAP_IDIVT (1 << 18)
97 #define HWCAP_VFPD32 (1 << 19) /* set if VFP has 32 regs (not 16) */
98 #define HWCAP_IDIV (HWCAP_IDIVA | HWCAP_IDIVT)
99 #define HWCAP_LPAE (1 << 20)
101 static uint32_t ReadELFHWCaps() {
103 #if V8_GLIBC_PREREQ(2, 16)
104 result = static_cast<uint32_t>(getauxval(AT_HWCAP));
106 // Read the ELF HWCAP flags by parsing /proc/self/auxv.
107 FILE* fp = fopen("/proc/self/auxv", "r");
109 struct { uint32_t tag; uint32_t value; } entry;
111 size_t n = fread(&entry, sizeof(entry), 1, fp);
112 if (n == 0 || (entry.tag == 0 && entry.value == 0)) {
115 if (entry.tag == AT_HWCAP) {
116 result = entry.value;
126 #endif // V8_HOST_ARCH_ARM
128 #if V8_HOST_ARCH_MIPS
129 int __detect_fp64_mode(void) {
131 // Bit representation of (double)1 is 0x3FF0000000000000.
136 "lui $t0, 0x3FF0\n\t"
143 : "t0", "$f0", "$f1", "memory");
145 return !(result == 1);
149 int __detect_mips_arch_revision(void) {
150 // TODO(dusmil): Do the specific syscall as soon as it is implemented in mips
154 "move $v0, $zero\n\t"
155 // Encoding for "addi $v0, $v0, 1" on non-r6,
156 // which is encoding for "bovc $v0, %v0, 1" on r6.
157 // Use machine code directly to avoid compilation errors with different
158 // toolchains and maintain compatibility.
159 ".word 0x20420001\n\t"
164 // Result is 0 on r6 architectures, 1 on other architecture revisions.
165 // Fall-back to the least common denominator which is mips32 revision 1.
166 return result ? 1 : 6;
170 // Extract the information exposed by the kernel via /proc/cpuinfo.
171 class CPUInfo FINAL {
173 CPUInfo() : datalen_(0) {
174 // Get the size of the cpuinfo file by reading it until the end. This is
175 // required because files under /proc do not always return a valid size
176 // when using fseek(0, SEEK_END) + ftell(). Nor can the be mmap()-ed.
177 static const char PATHNAME[] = "/proc/cpuinfo";
178 FILE* fp = fopen(PATHNAME, "r");
182 size_t n = fread(buffer, 1, sizeof(buffer), fp);
191 // Read the contents of the cpuinfo file.
192 data_ = new char[datalen_ + 1];
193 fp = fopen(PATHNAME, "r");
195 for (size_t offset = 0; offset < datalen_; ) {
196 size_t n = fread(data_ + offset, 1, datalen_ - offset, fp);
205 // Zero-terminate the data.
206 data_[datalen_] = '\0';
213 // Extract the content of a the first occurence of a given field in
214 // the content of the cpuinfo file and return it as a heap-allocated
215 // string that must be freed by the caller using delete[].
216 // Return NULL if not found.
217 char* ExtractField(const char* field) const {
218 DCHECK(field != NULL);
220 // Look for first field occurence, and ensure it starts the line.
221 size_t fieldlen = strlen(field);
224 p = strstr(p, field);
228 if (p == data_ || p[-1] == '\n') {
234 // Skip to the first colon followed by a space.
235 p = strchr(p + fieldlen, ':');
236 if (p == NULL || !isspace(p[1])) {
241 // Find the end of the line.
242 char* q = strchr(p, '\n');
244 q = data_ + datalen_;
247 // Copy the line into a heap-allocated buffer.
249 char* result = new char[len + 1];
250 if (result != NULL) {
251 memcpy(result, p, len);
262 #if V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
264 // Checks that a space-separated list of items contains one given 'item'.
265 static bool HasListItem(const char* list, const char* item) {
266 ssize_t item_len = strlen(item);
267 const char* p = list;
271 while (isspace(*p)) ++p;
273 // Find end of current list item.
275 while (*q != '\0' && !isspace(*q)) ++q;
277 if (item_len == q - p && memcmp(p, item, item_len) == 0) {
281 // Skip to next item.
288 #endif // V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
290 #endif // V8_OS_LINUX
292 #endif // V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
294 CPU::CPU() : stepping_(0),
318 has_vfp3_d32_(false),
319 is_fp64_mode_(false) {
320 memcpy(vendor_, "Unknown", 8);
322 // Portable host shouldn't do feature detection.
323 // TODO(jfb): Remove the hardcoded ARM simulator flags in the build, and
324 // hardcode them here instead.
325 #elif V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
328 // __cpuid with an InfoType argument of 0 returns the number of
329 // valid Ids in CPUInfo[0] and the CPU identification string in
330 // the other three array elements. The CPU identification string is
331 // not in linear order. The code below arranges the information
332 // in a human readable form. The human readable order is CPUInfo[1] |
333 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
334 // before using memcpy to copy these three array elements to cpu_string.
335 __cpuid(cpu_info, 0);
336 unsigned num_ids = cpu_info[0];
337 std::swap(cpu_info[2], cpu_info[3]);
338 memcpy(vendor_, cpu_info + 1, 12);
341 // Interpret CPU feature information.
343 __cpuid(cpu_info, 1);
344 stepping_ = cpu_info[0] & 0xf;
345 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
346 family_ = (cpu_info[0] >> 8) & 0xf;
347 type_ = (cpu_info[0] >> 12) & 0x3;
348 ext_model_ = (cpu_info[0] >> 16) & 0xf;
349 ext_family_ = (cpu_info[0] >> 20) & 0xff;
350 has_fpu_ = (cpu_info[3] & 0x00000001) != 0;
351 has_cmov_ = (cpu_info[3] & 0x00008000) != 0;
352 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
353 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
354 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
355 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
356 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
357 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
358 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
361 #if V8_HOST_ARCH_IA32
362 // SAHF is always available in compat/legacy mode,
365 // Query extended IDs.
366 __cpuid(cpu_info, 0x80000000);
367 unsigned num_ext_ids = cpu_info[0];
369 // Interpret extended CPU feature information.
370 if (num_ext_ids > 0x80000000) {
371 __cpuid(cpu_info, 0x80000001);
372 // SAHF must be probed in long mode.
373 has_sahf_ = (cpu_info[2] & 0x00000001) != 0;
377 #elif V8_HOST_ARCH_ARM
383 // Extract implementor from the "CPU implementer" field.
384 char* implementer = cpu_info.ExtractField("CPU implementer");
385 if (implementer != NULL) {
387 implementer_ = strtol(implementer, &end, 0);
388 if (end == implementer) {
391 delete[] implementer;
394 // Extract part number from the "CPU part" field.
395 char* part = cpu_info.ExtractField("CPU part");
398 part_ = strtol(part, &end, 0);
405 // Extract architecture from the "CPU Architecture" field.
406 // The list is well-known, unlike the the output of
407 // the 'Processor' field which can vary greatly.
408 // See the definition of the 'proc_arch' array in
409 // $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
411 char* architecture = cpu_info.ExtractField("CPU architecture");
412 if (architecture != NULL) {
414 architecture_ = strtol(architecture, &end, 10);
415 if (end == architecture) {
418 delete[] architecture;
420 // Unfortunately, it seems that certain ARMv6-based CPUs
421 // report an incorrect architecture number of 7!
423 // See http://code.google.com/p/android/issues/detail?id=10812
425 // We try to correct this by looking at the 'elf_format'
426 // field reported by the 'Processor' field, which is of the
427 // form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
428 // an ARMv6-one. For example, the Raspberry Pi is one popular
429 // ARMv6 device that reports architecture 7.
430 if (architecture_ == 7) {
431 char* processor = cpu_info.ExtractField("Processor");
432 if (HasListItem(processor, "(v6l)")) {
439 // Try to extract the list of CPU features from ELF hwcaps.
440 uint32_t hwcaps = ReadELFHWCaps();
442 has_idiva_ = (hwcaps & HWCAP_IDIVA) != 0;
443 has_neon_ = (hwcaps & HWCAP_NEON) != 0;
444 has_vfp_ = (hwcaps & HWCAP_VFP) != 0;
445 has_vfp3_ = (hwcaps & (HWCAP_VFPv3 | HWCAP_VFPv3D16 | HWCAP_VFPv4)) != 0;
446 has_vfp3_d32_ = (has_vfp3_ && ((hwcaps & HWCAP_VFPv3D16) == 0 ||
447 (hwcaps & HWCAP_VFPD32) != 0));
449 // Try to fallback to "Features" CPUInfo field.
450 char* features = cpu_info.ExtractField("Features");
451 has_idiva_ = HasListItem(features, "idiva");
452 has_neon_ = HasListItem(features, "neon");
453 has_thumb2_ = HasListItem(features, "thumb2");
454 has_vfp_ = HasListItem(features, "vfp");
455 if (HasListItem(features, "vfpv3d16")) {
457 } else if (HasListItem(features, "vfpv3")) {
459 has_vfp3_d32_ = true;
464 // Some old kernels will report vfp not vfpv3. Here we make an attempt
465 // to detect vfpv3 by checking for vfp *and* neon, since neon is only
466 // available on architectures with vfpv3. Checking neon on its own is
467 // not enough as it is possible to have neon without vfp.
468 if (has_vfp_ && has_neon_) {
472 // VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
473 if (architecture_ < 7 && has_vfp3_) {
477 // ARMv7 implies Thumb2.
478 if (architecture_ >= 7) {
482 // The earliest architecture with Thumb2 is ARMv6T2.
483 if (has_thumb2_ && architecture_ < 6) {
487 // We don't support any FPUs other than VFP.
492 uint32_t cpu_flags = SYSPAGE_ENTRY(cpuinfo)->flags;
493 if (cpu_flags & ARM_CPU_FLAG_V7) {
496 } else if (cpu_flags & ARM_CPU_FLAG_V6) {
498 // QNX doesn't say if Thumb2 is available.
499 // Assume false for the architectures older than ARMv7.
501 DCHECK(architecture_ >= 6);
502 has_fpu_ = (cpu_flags & CPU_FLAG_FPU) != 0;
504 if (cpu_flags & ARM_CPU_FLAG_NEON) {
506 has_vfp3_ = has_vfp_;
507 #ifdef ARM_CPU_FLAG_VFP_D32
508 has_vfp3_d32_ = (cpu_flags & ARM_CPU_FLAG_VFP_D32) != 0;
511 has_idiva_ = (cpu_flags & ARM_CPU_FLAG_IDIV) != 0;
513 #endif // V8_OS_LINUX
515 #elif V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
517 // Simple detection of FPU at runtime for Linux.
518 // It is based on /proc/cpuinfo, which reveals hardware configuration
519 // to user-space applications. According to MIPS (early 2010), no similar
520 // facility is universally available on the MIPS architectures,
521 // so it's up to individual OSes to provide such.
523 char* cpu_model = cpu_info.ExtractField("cpu model");
524 has_fpu_ = HasListItem(cpu_model, "FPU");
526 #ifdef V8_HOST_ARCH_MIPS
527 is_fp64_mode_ = __detect_fp64_mode();
528 architecture_ = __detect_mips_arch_revision();
531 #elif V8_HOST_ARCH_ARM64
535 // Extract implementor from the "CPU implementer" field.
536 char* implementer = cpu_info.ExtractField("CPU implementer");
537 if (implementer != NULL) {
539 implementer_ = strtol(implementer, &end, 0);
540 if (end == implementer) {
543 delete[] implementer;
546 // Extract part number from the "CPU part" field.
547 char* part = cpu_info.ExtractField("CPU part");
550 part_ = strtol(part, &end, 0);
560 } } // namespace v8::base