blob: daf33023656028ff6d81876660446050273fa021 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
4
5#include "src/base/cpu.h"
6
7#if V8_LIBC_MSVCRT
8#include <intrin.h> // __cpuid()
9#endif
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010#if V8_OS_LINUX
11#include <linux/auxvec.h> // AT_HWCAP
12#endif
13#if V8_GLIBC_PREREQ(2, 16)
14#include <sys/auxv.h> // getauxval()
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#endif
16#if V8_OS_QNX
17#include <sys/syspage.h> // cpuinfo
18#endif
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019#if V8_OS_POSIX
20#include <unistd.h> // sysconf()
21#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022
23#include <ctype.h>
24#include <limits.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <algorithm>
29
30#include "src/base/logging.h"
31#if V8_OS_WIN
32#include "src/base/win32-headers.h" // NOLINT
33#endif
34
35namespace v8 {
36namespace base {
37
Emily Bernierd0a1eb72015-03-24 16:35:39 -040038#if defined(__pnacl__)
39// Portable host shouldn't do feature detection.
40#elif V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041
42// Define __cpuid() for non-MSVC libraries.
43#if !V8_LIBC_MSVCRT
44
45static 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.
49 __asm__ volatile (
50 "mov %%ebx, %%edi\n\t"
51 "cpuid\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])
54 : "a"(info_type)
55 );
56#else
57 __asm__ volatile (
58 "cpuid \n\t"
59 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
60 : "a"(info_type)
61 );
62#endif // defined(__i386__) && defined(__pic__)
63}
64
65#endif // !V8_LIBC_MSVCRT
66
67#elif V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64 \
68 || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
69
70#if V8_OS_LINUX
71
72#if V8_HOST_ARCH_ARM
73
74// See <uapi/asm/hwcap.h> kernel header.
75/*
76 * HWCAP flags - for elf_hwcap (in kernel) and AT_HWCAP
77 */
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)
100
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101static uint32_t ReadELFHWCaps() {
102 uint32_t result = 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400103#if V8_GLIBC_PREREQ(2, 16)
104 result = static_cast<uint32_t>(getauxval(AT_HWCAP));
105#else
106 // Read the ELF HWCAP flags by parsing /proc/self/auxv.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 FILE* fp = fopen("/proc/self/auxv", "r");
108 if (fp != NULL) {
109 struct { uint32_t tag; uint32_t value; } entry;
110 for (;;) {
111 size_t n = fread(&entry, sizeof(entry), 1, fp);
112 if (n == 0 || (entry.tag == 0 && entry.value == 0)) {
113 break;
114 }
115 if (entry.tag == AT_HWCAP) {
116 result = entry.value;
117 break;
118 }
119 }
120 fclose(fp);
121 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400122#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 return result;
124}
125
126#endif // V8_HOST_ARCH_ARM
127
128#if V8_HOST_ARCH_MIPS
129int __detect_fp64_mode(void) {
130 double result = 0;
131 // Bit representation of (double)1 is 0x3FF0000000000000.
Paul Lind18a7ebb2014-12-17 22:29:32 -0800132 __asm__ volatile(
133 ".set push\n\t"
134 ".set noreorder\n\t"
135 ".set oddspreg\n\t"
136 "lui $t0, 0x3FF0\n\t"
137 "ldc1 $f0, %0\n\t"
138 "mtc1 $t0, $f1\n\t"
139 "sdc1 $f0, %0\n\t"
140 ".set pop\n\t"
141 : "+m"(result)
142 :
143 : "t0", "$f0", "$f1", "memory");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144
145 return !(result == 1);
146}
147
148
149int __detect_mips_arch_revision(void) {
150 // TODO(dusmil): Do the specific syscall as soon as it is implemented in mips
Paul Lind18a7ebb2014-12-17 22:29:32 -0800151 // kernel.
152 uint32_t result = 0;
153 __asm__ volatile(
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"
160 "sw $v0, %0\n\t"
161 : "=m"(result)
162 :
163 : "v0", "memory");
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;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167}
168#endif
169
170// Extract the information exposed by the kernel via /proc/cpuinfo.
171class CPUInfo FINAL {
172 public:
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");
179 if (fp != NULL) {
180 for (;;) {
181 char buffer[256];
182 size_t n = fread(buffer, 1, sizeof(buffer), fp);
183 if (n == 0) {
184 break;
185 }
186 datalen_ += n;
187 }
188 fclose(fp);
189 }
190
191 // Read the contents of the cpuinfo file.
192 data_ = new char[datalen_ + 1];
193 fp = fopen(PATHNAME, "r");
194 if (fp != NULL) {
195 for (size_t offset = 0; offset < datalen_; ) {
196 size_t n = fread(data_ + offset, 1, datalen_ - offset, fp);
197 if (n == 0) {
198 break;
199 }
200 offset += n;
201 }
202 fclose(fp);
203 }
204
205 // Zero-terminate the data.
206 data_[datalen_] = '\0';
207 }
208
209 ~CPUInfo() {
210 delete[] data_;
211 }
212
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);
219
220 // Look for first field occurence, and ensure it starts the line.
221 size_t fieldlen = strlen(field);
222 char* p = data_;
223 for (;;) {
224 p = strstr(p, field);
225 if (p == NULL) {
226 return NULL;
227 }
228 if (p == data_ || p[-1] == '\n') {
229 break;
230 }
231 p += fieldlen;
232 }
233
234 // Skip to the first colon followed by a space.
235 p = strchr(p + fieldlen, ':');
236 if (p == NULL || !isspace(p[1])) {
237 return NULL;
238 }
239 p += 2;
240
241 // Find the end of the line.
242 char* q = strchr(p, '\n');
243 if (q == NULL) {
244 q = data_ + datalen_;
245 }
246
247 // Copy the line into a heap-allocated buffer.
248 size_t len = q - p;
249 char* result = new char[len + 1];
250 if (result != NULL) {
251 memcpy(result, p, len);
252 result[len] = '\0';
253 }
254 return result;
255 }
256
257 private:
258 char* data_;
259 size_t datalen_;
260};
261
262#if V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
263
264// Checks that a space-separated list of items contains one given 'item'.
265static bool HasListItem(const char* list, const char* item) {
266 ssize_t item_len = strlen(item);
267 const char* p = list;
268 if (p != NULL) {
269 while (*p != '\0') {
270 // Skip whitespace.
271 while (isspace(*p)) ++p;
272
273 // Find end of current list item.
274 const char* q = p;
275 while (*q != '\0' && !isspace(*q)) ++q;
276
277 if (item_len == q - p && memcmp(p, item, item_len) == 0) {
278 return true;
279 }
280
281 // Skip to next item.
282 p = q;
283 }
284 }
285 return false;
286}
287
288#endif // V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
289
290#endif // V8_OS_LINUX
291
292#endif // V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
293
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400294CPU::CPU()
295 : stepping_(0),
296 model_(0),
297 ext_model_(0),
298 family_(0),
299 ext_family_(0),
300 type_(0),
301 implementer_(0),
302 architecture_(0),
303 variant_(-1),
304 part_(0),
305 has_fpu_(false),
306 has_cmov_(false),
307 has_sahf_(false),
308 has_mmx_(false),
309 has_sse_(false),
310 has_sse2_(false),
311 has_sse3_(false),
312 has_ssse3_(false),
313 has_sse41_(false),
314 has_sse42_(false),
315 has_avx_(false),
316 has_fma3_(false),
317 has_idiva_(false),
318 has_neon_(false),
319 has_thumb2_(false),
320 has_vfp_(false),
321 has_vfp3_(false),
322 has_vfp3_d32_(false),
323 is_fp64_mode_(false) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 memcpy(vendor_, "Unknown", 8);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400325#if V8_OS_NACL
326// Portable host shouldn't do feature detection.
327// TODO(jfb): Remove the hardcoded ARM simulator flags in the build, and
328// hardcode them here instead.
329#elif V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 int cpu_info[4];
331
332 // __cpuid with an InfoType argument of 0 returns the number of
333 // valid Ids in CPUInfo[0] and the CPU identification string in
334 // the other three array elements. The CPU identification string is
335 // not in linear order. The code below arranges the information
336 // in a human readable form. The human readable order is CPUInfo[1] |
337 // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
338 // before using memcpy to copy these three array elements to cpu_string.
339 __cpuid(cpu_info, 0);
340 unsigned num_ids = cpu_info[0];
341 std::swap(cpu_info[2], cpu_info[3]);
342 memcpy(vendor_, cpu_info + 1, 12);
343 vendor_[12] = '\0';
344
345 // Interpret CPU feature information.
346 if (num_ids > 0) {
347 __cpuid(cpu_info, 1);
348 stepping_ = cpu_info[0] & 0xf;
349 model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
350 family_ = (cpu_info[0] >> 8) & 0xf;
351 type_ = (cpu_info[0] >> 12) & 0x3;
352 ext_model_ = (cpu_info[0] >> 16) & 0xf;
353 ext_family_ = (cpu_info[0] >> 20) & 0xff;
354 has_fpu_ = (cpu_info[3] & 0x00000001) != 0;
355 has_cmov_ = (cpu_info[3] & 0x00008000) != 0;
356 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
357 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
358 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
359 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
360 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
361 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
362 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400363 has_avx_ = (cpu_info[2] & 0x10000000) != 0;
364 if (has_avx_) has_fma3_ = (cpu_info[2] & 0x00001000) != 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 }
366
367#if V8_HOST_ARCH_IA32
368 // SAHF is always available in compat/legacy mode,
369 has_sahf_ = true;
370#else
371 // Query extended IDs.
372 __cpuid(cpu_info, 0x80000000);
373 unsigned num_ext_ids = cpu_info[0];
374
375 // Interpret extended CPU feature information.
376 if (num_ext_ids > 0x80000000) {
377 __cpuid(cpu_info, 0x80000001);
378 // SAHF must be probed in long mode.
379 has_sahf_ = (cpu_info[2] & 0x00000001) != 0;
380 }
381#endif
382
383#elif V8_HOST_ARCH_ARM
384
385#if V8_OS_LINUX
386
387 CPUInfo cpu_info;
388
389 // Extract implementor from the "CPU implementer" field.
390 char* implementer = cpu_info.ExtractField("CPU implementer");
391 if (implementer != NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400392 char* end;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 implementer_ = strtol(implementer, &end, 0);
394 if (end == implementer) {
395 implementer_ = 0;
396 }
397 delete[] implementer;
398 }
399
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400400 char* variant = cpu_info.ExtractField("CPU variant");
401 if (variant != NULL) {
402 char* end;
403 variant_ = strtol(variant, &end, 0);
404 if (end == variant) {
405 variant_ = -1;
406 }
407 delete[] variant;
408 }
409
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000410 // Extract part number from the "CPU part" field.
411 char* part = cpu_info.ExtractField("CPU part");
412 if (part != NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400413 char* end;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000414 part_ = strtol(part, &end, 0);
415 if (end == part) {
416 part_ = 0;
417 }
418 delete[] part;
419 }
420
421 // Extract architecture from the "CPU Architecture" field.
422 // The list is well-known, unlike the the output of
423 // the 'Processor' field which can vary greatly.
424 // See the definition of the 'proc_arch' array in
425 // $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
426 // same file.
427 char* architecture = cpu_info.ExtractField("CPU architecture");
428 if (architecture != NULL) {
429 char* end;
430 architecture_ = strtol(architecture, &end, 10);
431 if (end == architecture) {
432 architecture_ = 0;
433 }
434 delete[] architecture;
435
436 // Unfortunately, it seems that certain ARMv6-based CPUs
437 // report an incorrect architecture number of 7!
438 //
439 // See http://code.google.com/p/android/issues/detail?id=10812
440 //
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400441 // We try to correct this by looking at the 'elf_platform'
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 // field reported by the 'Processor' field, which is of the
443 // form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
444 // an ARMv6-one. For example, the Raspberry Pi is one popular
445 // ARMv6 device that reports architecture 7.
446 if (architecture_ == 7) {
447 char* processor = cpu_info.ExtractField("Processor");
448 if (HasListItem(processor, "(v6l)")) {
449 architecture_ = 6;
450 }
451 delete[] processor;
452 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400453
454 // elf_platform moved to the model name field in Linux v3.8.
455 if (architecture_ == 7) {
456 char* processor = cpu_info.ExtractField("model name");
457 if (HasListItem(processor, "(v6l)")) {
458 architecture_ = 6;
459 }
460 delete[] processor;
461 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 }
463
464 // Try to extract the list of CPU features from ELF hwcaps.
465 uint32_t hwcaps = ReadELFHWCaps();
466 if (hwcaps != 0) {
467 has_idiva_ = (hwcaps & HWCAP_IDIVA) != 0;
468 has_neon_ = (hwcaps & HWCAP_NEON) != 0;
469 has_vfp_ = (hwcaps & HWCAP_VFP) != 0;
470 has_vfp3_ = (hwcaps & (HWCAP_VFPv3 | HWCAP_VFPv3D16 | HWCAP_VFPv4)) != 0;
471 has_vfp3_d32_ = (has_vfp3_ && ((hwcaps & HWCAP_VFPv3D16) == 0 ||
472 (hwcaps & HWCAP_VFPD32) != 0));
473 } else {
474 // Try to fallback to "Features" CPUInfo field.
475 char* features = cpu_info.ExtractField("Features");
476 has_idiva_ = HasListItem(features, "idiva");
477 has_neon_ = HasListItem(features, "neon");
478 has_thumb2_ = HasListItem(features, "thumb2");
479 has_vfp_ = HasListItem(features, "vfp");
480 if (HasListItem(features, "vfpv3d16")) {
481 has_vfp3_ = true;
482 } else if (HasListItem(features, "vfpv3")) {
483 has_vfp3_ = true;
484 has_vfp3_d32_ = true;
485 }
486 delete[] features;
487 }
488
489 // Some old kernels will report vfp not vfpv3. Here we make an attempt
490 // to detect vfpv3 by checking for vfp *and* neon, since neon is only
491 // available on architectures with vfpv3. Checking neon on its own is
492 // not enough as it is possible to have neon without vfp.
493 if (has_vfp_ && has_neon_) {
494 has_vfp3_ = true;
495 }
496
497 // VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
498 if (architecture_ < 7 && has_vfp3_) {
499 architecture_ = 7;
500 }
501
502 // ARMv7 implies Thumb2.
503 if (architecture_ >= 7) {
504 has_thumb2_ = true;
505 }
506
507 // The earliest architecture with Thumb2 is ARMv6T2.
508 if (has_thumb2_ && architecture_ < 6) {
509 architecture_ = 6;
510 }
511
512 // We don't support any FPUs other than VFP.
513 has_fpu_ = has_vfp_;
514
515#elif V8_OS_QNX
516
517 uint32_t cpu_flags = SYSPAGE_ENTRY(cpuinfo)->flags;
518 if (cpu_flags & ARM_CPU_FLAG_V7) {
519 architecture_ = 7;
520 has_thumb2_ = true;
521 } else if (cpu_flags & ARM_CPU_FLAG_V6) {
522 architecture_ = 6;
523 // QNX doesn't say if Thumb2 is available.
524 // Assume false for the architectures older than ARMv7.
525 }
526 DCHECK(architecture_ >= 6);
527 has_fpu_ = (cpu_flags & CPU_FLAG_FPU) != 0;
528 has_vfp_ = has_fpu_;
529 if (cpu_flags & ARM_CPU_FLAG_NEON) {
530 has_neon_ = true;
531 has_vfp3_ = has_vfp_;
532#ifdef ARM_CPU_FLAG_VFP_D32
533 has_vfp3_d32_ = (cpu_flags & ARM_CPU_FLAG_VFP_D32) != 0;
534#endif
535 }
536 has_idiva_ = (cpu_flags & ARM_CPU_FLAG_IDIV) != 0;
537
538#endif // V8_OS_LINUX
539
540#elif V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
541
542 // Simple detection of FPU at runtime for Linux.
543 // It is based on /proc/cpuinfo, which reveals hardware configuration
544 // to user-space applications. According to MIPS (early 2010), no similar
545 // facility is universally available on the MIPS architectures,
546 // so it's up to individual OSes to provide such.
547 CPUInfo cpu_info;
548 char* cpu_model = cpu_info.ExtractField("cpu model");
549 has_fpu_ = HasListItem(cpu_model, "FPU");
550 delete[] cpu_model;
551#ifdef V8_HOST_ARCH_MIPS
552 is_fp64_mode_ = __detect_fp64_mode();
553 architecture_ = __detect_mips_arch_revision();
554#endif
555
556#elif V8_HOST_ARCH_ARM64
557
558 CPUInfo cpu_info;
559
560 // Extract implementor from the "CPU implementer" field.
561 char* implementer = cpu_info.ExtractField("CPU implementer");
562 if (implementer != NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400563 char* end;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000564 implementer_ = strtol(implementer, &end, 0);
565 if (end == implementer) {
566 implementer_ = 0;
567 }
568 delete[] implementer;
569 }
570
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400571 char* variant = cpu_info.ExtractField("CPU variant");
572 if (variant != NULL) {
573 char* end;
574 variant_ = strtol(variant, &end, 0);
575 if (end == variant) {
576 variant_ = -1;
577 }
578 delete[] variant;
579 }
580
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 // Extract part number from the "CPU part" field.
582 char* part = cpu_info.ExtractField("CPU part");
583 if (part != NULL) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400584 char* end;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000585 part_ = strtol(part, &end, 0);
586 if (end == part) {
587 part_ = 0;
588 }
589 delete[] part;
590 }
591
592#endif
593}
594
595} } // namespace v8::base