• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "src/base/cpu.h"
6 
7 #if defined(STARBOARD)
8 #include "starboard/cpu_features.h"
9 #endif
10 
11 #if V8_LIBC_MSVCRT
12 #include <intrin.h>  // __cpuid()
13 #endif
14 #if V8_OS_LINUX
15 #include <linux/auxvec.h>  // AT_HWCAP
16 #endif
17 #if V8_GLIBC_PREREQ(2, 16)
18 #include <sys/auxv.h>  // getauxval()
19 #endif
20 #if V8_OS_QNX
21 #include <sys/syspage.h>  // cpuinfo
22 #endif
23 #if V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_PPC64)
24 #include <elf.h>
25 #endif
26 #if V8_OS_AIX
27 #include <sys/systemcfg.h>  // _system_configuration
28 #ifndef POWER_8
29 #define POWER_8 0x10000
30 #endif
31 #ifndef POWER_9
32 #define POWER_9 0x20000
33 #endif
34 #endif
35 #if V8_OS_POSIX
36 #include <unistd.h>  // sysconf()
37 #endif
38 
39 #include <ctype.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <algorithm>
45 
46 #include "src/base/logging.h"
47 #if V8_OS_WIN
48 #include "src/base/win32-headers.h"  // NOLINT
49 #endif
50 
51 namespace v8 {
52 namespace base {
53 
54 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
55 
56 // Define __cpuid() for non-MSVC libraries.
57 #if !V8_LIBC_MSVCRT
58 
__cpuid(int cpu_info[4],int info_type)59 static V8_INLINE void __cpuid(int cpu_info[4], int info_type) {
60 // Clear ecx to align with __cpuid() of MSVC:
61 // https://msdn.microsoft.com/en-us/library/hskdteyh.aspx
62 #if defined(__i386__) && defined(__pic__)
63   // Make sure to preserve ebx, which contains the pointer
64   // to the GOT in case we're generating PIC.
65   __asm__ volatile(
66       "mov %%ebx, %%edi\n\t"
67       "cpuid\n\t"
68       "xchg %%edi, %%ebx\n\t"
69       : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]),
70         "=d"(cpu_info[3])
71       : "a"(info_type), "c"(0));
72 #else
73   __asm__ volatile("cpuid \n\t"
74                    : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]),
75                      "=d"(cpu_info[3])
76                    : "a"(info_type), "c"(0));
77 #endif  // defined(__i386__) && defined(__pic__)
78 }
79 
80 #endif  // !V8_LIBC_MSVCRT
81 
82 #elif V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64 || V8_HOST_ARCH_MIPS || \
83     V8_HOST_ARCH_MIPS64
84 
85 #if V8_OS_LINUX
86 
87 #if V8_HOST_ARCH_ARM
88 
89 // See <uapi/asm/hwcap.h> kernel header.
90 /*
91  * HWCAP flags - for elf_hwcap (in kernel) and AT_HWCAP
92  */
93 #define HWCAP_SWP (1 << 0)
94 #define HWCAP_HALF  (1 << 1)
95 #define HWCAP_THUMB (1 << 2)
96 #define HWCAP_26BIT (1 << 3)  /* Play it safe */
97 #define HWCAP_FAST_MULT (1 << 4)
98 #define HWCAP_FPA (1 << 5)
99 #define HWCAP_VFP (1 << 6)
100 #define HWCAP_EDSP  (1 << 7)
101 #define HWCAP_JAVA  (1 << 8)
102 #define HWCAP_IWMMXT  (1 << 9)
103 #define HWCAP_CRUNCH  (1 << 10)
104 #define HWCAP_THUMBEE (1 << 11)
105 #define HWCAP_NEON  (1 << 12)
106 #define HWCAP_VFPv3 (1 << 13)
107 #define HWCAP_VFPv3D16  (1 << 14) /* also set for VFPv4-D16 */
108 #define HWCAP_TLS (1 << 15)
109 #define HWCAP_VFPv4 (1 << 16)
110 #define HWCAP_IDIVA (1 << 17)
111 #define HWCAP_IDIVT (1 << 18)
112 #define HWCAP_VFPD32  (1 << 19) /* set if VFP has 32 regs (not 16) */
113 #define HWCAP_IDIV  (HWCAP_IDIVA | HWCAP_IDIVT)
114 #define HWCAP_LPAE  (1 << 20)
115 
116 #endif  // V8_HOST_ARCH_ARM
117 
118 #if V8_HOST_ARCH_ARM64
119 
120 // See <uapi/asm/hwcap.h> kernel header.
121 /*
122  * HWCAP flags - for elf_hwcap (in kernel) and AT_HWCAP
123  */
124 #define HWCAP_FP (1 << 0)
125 #define HWCAP_ASIMD (1 << 1)
126 #define HWCAP_EVTSTRM (1 << 2)
127 #define HWCAP_AES (1 << 3)
128 #define HWCAP_PMULL (1 << 4)
129 #define HWCAP_SHA1 (1 << 5)
130 #define HWCAP_SHA2 (1 << 6)
131 #define HWCAP_CRC32 (1 << 7)
132 #define HWCAP_ATOMICS (1 << 8)
133 #define HWCAP_FPHP (1 << 9)
134 #define HWCAP_ASIMDHP (1 << 10)
135 #define HWCAP_CPUID (1 << 11)
136 #define HWCAP_ASIMDRDM (1 << 12)
137 #define HWCAP_JSCVT (1 << 13)
138 #define HWCAP_FCMA (1 << 14)
139 #define HWCAP_LRCPC (1 << 15)
140 #define HWCAP_DCPOP (1 << 16)
141 #define HWCAP_SHA3 (1 << 17)
142 #define HWCAP_SM3 (1 << 18)
143 #define HWCAP_SM4 (1 << 19)
144 #define HWCAP_ASIMDDP (1 << 20)
145 #define HWCAP_SHA512 (1 << 21)
146 #define HWCAP_SVE (1 << 22)
147 #define HWCAP_ASIMDFHM (1 << 23)
148 #define HWCAP_DIT (1 << 24)
149 #define HWCAP_USCAT (1 << 25)
150 #define HWCAP_ILRCPC (1 << 26)
151 #define HWCAP_FLAGM (1 << 27)
152 #define HWCAP_SSBS (1 << 28)
153 #define HWCAP_SB (1 << 29)
154 #define HWCAP_PACA (1 << 30)
155 #define HWCAP_PACG (1UL << 31)
156 
157 #endif  // V8_HOST_ARCH_ARM64
158 
159 #if V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64
160 
161 static uint32_t ReadELFHWCaps() {
162   uint32_t result = 0;
163 #if V8_GLIBC_PREREQ(2, 16)
164   result = static_cast<uint32_t>(getauxval(AT_HWCAP));
165 #else
166   // Read the ELF HWCAP flags by parsing /proc/self/auxv.
167   FILE* fp = fopen("/proc/self/auxv", "r");
168   if (fp != nullptr) {
169     struct {
170       uint32_t tag;
171       uint32_t value;
172     } entry;
173     for (;;) {
174       size_t n = fread(&entry, sizeof(entry), 1, fp);
175       if (n == 0 || (entry.tag == 0 && entry.value == 0)) {
176         break;
177       }
178       if (entry.tag == AT_HWCAP) {
179         result = entry.value;
180         break;
181       }
182     }
183     fclose(fp);
184   }
185 #endif
186   return result;
187 }
188 
189 #endif  // V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64
190 
191 #if V8_HOST_ARCH_MIPS
192 int __detect_fp64_mode(void) {
193   double result = 0;
194   // Bit representation of (double)1 is 0x3FF0000000000000.
195   __asm__ volatile(
196       ".set push\n\t"
197       ".set noreorder\n\t"
198       ".set oddspreg\n\t"
199       "lui $t0, 0x3FF0\n\t"
200       "ldc1 $f0, %0\n\t"
201       "mtc1 $t0, $f1\n\t"
202       "sdc1 $f0, %0\n\t"
203       ".set pop\n\t"
204       : "+m"(result)
205       :
206       : "t0", "$f0", "$f1", "memory");
207 
208   return !(result == 1);
209 }
210 
211 
212 int __detect_mips_arch_revision(void) {
213   // TODO(dusmil): Do the specific syscall as soon as it is implemented in mips
214   // kernel.
215   uint32_t result = 0;
216   __asm__ volatile(
217       "move $v0, $zero\n\t"
218       // Encoding for "addi $v0, $v0, 1" on non-r6,
219       // which is encoding for "bovc $v0, %v0, 1" on r6.
220       // Use machine code directly to avoid compilation errors with different
221       // toolchains and maintain compatibility.
222       ".word 0x20420001\n\t"
223       "sw $v0, %0\n\t"
224       : "=m"(result)
225       :
226       : "v0", "memory");
227   // Result is 0 on r6 architectures, 1 on other architecture revisions.
228   // Fall-back to the least common denominator which is mips32 revision 1.
229   return result ? 1 : 6;
230 }
231 #endif  // V8_HOST_ARCH_MIPS
232 
233 // Extract the information exposed by the kernel via /proc/cpuinfo.
234 class CPUInfo final {
235  public:
236   CPUInfo() : datalen_(0) {
237     // Get the size of the cpuinfo file by reading it until the end. This is
238     // required because files under /proc do not always return a valid size
239     // when using fseek(0, SEEK_END) + ftell(). Nor can the be mmap()-ed.
240     static const char PATHNAME[] = "/proc/cpuinfo";
241     FILE* fp = fopen(PATHNAME, "r");
242     if (fp != nullptr) {
243       for (;;) {
244         char buffer[256];
245         size_t n = fread(buffer, 1, sizeof(buffer), fp);
246         if (n == 0) {
247           break;
248         }
249         datalen_ += n;
250       }
251       fclose(fp);
252     }
253 
254     // Read the contents of the cpuinfo file.
255     data_ = new char[datalen_ + 1];
256     fp = fopen(PATHNAME, "r");
257     if (fp != nullptr) {
258       for (size_t offset = 0; offset < datalen_; ) {
259         size_t n = fread(data_ + offset, 1, datalen_ - offset, fp);
260         if (n == 0) {
261           break;
262         }
263         offset += n;
264       }
265       fclose(fp);
266     }
267 
268     // Zero-terminate the data.
269     data_[datalen_] = '\0';
270   }
271 
272   ~CPUInfo() {
273     delete[] data_;
274   }
275 
276   // Extract the content of a the first occurrence of a given field in
277   // the content of the cpuinfo file and return it as a heap-allocated
278   // string that must be freed by the caller using delete[].
279   // Return nullptr if not found.
280   char* ExtractField(const char* field) const {
281     DCHECK_NOT_NULL(field);
282 
283     // Look for first field occurrence, and ensure it starts the line.
284     size_t fieldlen = strlen(field);
285     char* p = data_;
286     for (;;) {
287       p = strstr(p, field);
288       if (p == nullptr) {
289         return nullptr;
290       }
291       if (p == data_ || p[-1] == '\n') {
292         break;
293       }
294       p += fieldlen;
295     }
296 
297     // Skip to the first colon followed by a space.
298     p = strchr(p + fieldlen, ':');
299     if (p == nullptr || !isspace(p[1])) {
300       return nullptr;
301     }
302     p += 2;
303 
304     // Find the end of the line.
305     char* q = strchr(p, '\n');
306     if (q == nullptr) {
307       q = data_ + datalen_;
308     }
309 
310     // Copy the line into a heap-allocated buffer.
311     size_t len = q - p;
312     char* result = new char[len + 1];
313     if (result != nullptr) {
314       memcpy(result, p, len);
315       result[len] = '\0';
316     }
317     return result;
318   }
319 
320  private:
321   char* data_;
322   size_t datalen_;
323 };
324 
325 // Checks that a space-separated list of items contains one given 'item'.
326 static bool HasListItem(const char* list, const char* item) {
327   ssize_t item_len = strlen(item);
328   const char* p = list;
329   if (p != nullptr) {
330     while (*p != '\0') {
331       // Skip whitespace.
332       while (isspace(*p)) ++p;
333 
334       // Find end of current list item.
335       const char* q = p;
336       while (*q != '\0' && !isspace(*q)) ++q;
337 
338       if (item_len == q - p && memcmp(p, item, item_len) == 0) {
339         return true;
340       }
341 
342       // Skip to next item.
343       p = q;
344     }
345   }
346   return false;
347 }
348 
349 #endif  // V8_OS_LINUX
350 
351 #endif  // V8_HOST_ARCH_ARM || V8_HOST_ARCH_ARM64 ||
352         // V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
353 
354 #if defined(STARBOARD)
355 
StarboardDetectCPU()356 bool CPU::StarboardDetectCPU() {
357 #if (SB_API_VERSION >= 11)
358   SbCPUFeatures features;
359   if (!SbCPUFeaturesGet(&features)) {
360     return false;
361   }
362   architecture_ = features.arm.architecture_generation;
363   switch (features.architecture) {
364     case kSbCPUFeaturesArchitectureArm:
365     case kSbCPUFeaturesArchitectureArm64:
366       has_neon_ = features.arm.has_neon;
367       has_thumb2_ = features.arm.has_thumb2;
368       has_vfp_ = features.arm.has_vfp;
369       has_vfp3_ = features.arm.has_vfp3;
370       has_vfp3_d32_ = features.arm.has_vfp3_d32;
371       has_idiva_ = features.arm.has_idiva;
372       break;
373     case kSbCPUFeaturesArchitectureX86:
374     case kSbCPUFeaturesArchitectureX86_64:
375       // Following flags are mandatory for V8
376       has_cmov_ = features.x86.has_cmov;
377       has_sse2_ = features.x86.has_sse2;
378       // These flags are optional
379       has_sse3_ = features.x86.has_sse3;
380       has_ssse3_ = features.x86.has_ssse3;
381       has_sse41_ = features.x86.has_sse41;
382       has_sahf_ = features.x86.has_sahf;
383       has_avx_ = features.x86.has_avx;
384       has_fma3_ = features.x86.has_fma3;
385       has_bmi1_ = features.x86.has_bmi1;
386       has_bmi2_ = features.x86.has_bmi2;
387       has_lzcnt_ = features.x86.has_lzcnt;
388       has_popcnt_ = features.x86.has_popcnt;
389       break;
390     default:
391       return false;
392   }
393 
394   return true;
395 #else  // SB_API_VERSION >= 11
396   return false;
397 #endif
398 }
399 
400 #endif
401 
CPU()402 CPU::CPU()
403     : stepping_(0),
404       model_(0),
405       ext_model_(0),
406       family_(0),
407       ext_family_(0),
408       type_(0),
409       implementer_(0),
410       architecture_(0),
411       variant_(-1),
412       part_(0),
413       icache_line_size_(UNKNOWN_CACHE_LINE_SIZE),
414       dcache_line_size_(UNKNOWN_CACHE_LINE_SIZE),
415       has_fpu_(false),
416       has_cmov_(false),
417       has_sahf_(false),
418       has_mmx_(false),
419       has_sse_(false),
420       has_sse2_(false),
421       has_sse3_(false),
422       has_ssse3_(false),
423       has_sse41_(false),
424       has_sse42_(false),
425       is_atom_(false),
426       has_osxsave_(false),
427       has_avx_(false),
428       has_fma3_(false),
429       has_bmi1_(false),
430       has_bmi2_(false),
431       has_lzcnt_(false),
432       has_popcnt_(false),
433       has_idiva_(false),
434       has_neon_(false),
435       has_thumb2_(false),
436       has_vfp_(false),
437       has_vfp3_(false),
438       has_vfp3_d32_(false),
439       has_jscvt_(false),
440       is_fp64_mode_(false),
441       has_non_stop_time_stamp_counter_(false),
442       has_msa_(false) {
443   memcpy(vendor_, "Unknown", 8);
444 
445 #if defined(STARBOARD)
446   if (StarboardDetectCPU()) {
447     return;
448   }
449 #endif
450 
451 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
452   int cpu_info[4];
453 
454   // __cpuid with an InfoType argument of 0 returns the number of
455   // valid Ids in CPUInfo[0] and the CPU identification string in
456   // the other three array elements. The CPU identification string is
457   // not in linear order. The code below arranges the information
458   // in a human readable form. The human readable order is CPUInfo[1] |
459   // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
460   // before using memcpy to copy these three array elements to cpu_string.
461   __cpuid(cpu_info, 0);
462   unsigned num_ids = cpu_info[0];
463   std::swap(cpu_info[2], cpu_info[3]);
464   memcpy(vendor_, cpu_info + 1, 12);
465   vendor_[12] = '\0';
466 
467   // Interpret CPU feature information.
468   if (num_ids > 0) {
469     __cpuid(cpu_info, 1);
470     stepping_ = cpu_info[0] & 0xF;
471     model_ = ((cpu_info[0] >> 4) & 0xF) + ((cpu_info[0] >> 12) & 0xF0);
472     family_ = (cpu_info[0] >> 8) & 0xF;
473     type_ = (cpu_info[0] >> 12) & 0x3;
474     ext_model_ = (cpu_info[0] >> 16) & 0xF;
475     ext_family_ = (cpu_info[0] >> 20) & 0xFF;
476     has_fpu_ = (cpu_info[3] & 0x00000001) != 0;
477     has_cmov_ = (cpu_info[3] & 0x00008000) != 0;
478     has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
479     has_sse_ = (cpu_info[3] & 0x02000000) != 0;
480     has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
481     has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
482     has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
483     has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
484     has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
485     has_popcnt_ = (cpu_info[2] & 0x00800000) != 0;
486     has_osxsave_ = (cpu_info[2] & 0x08000000) != 0;
487     has_avx_ = (cpu_info[2] & 0x10000000) != 0;
488     has_fma3_ = (cpu_info[2] & 0x00001000) != 0;
489 
490     if (family_ == 0x6) {
491       switch (model_) {
492         case 0x1C:  // SLT
493         case 0x26:
494         case 0x36:
495         case 0x27:
496         case 0x35:
497         case 0x37:  // SLM
498         case 0x4A:
499         case 0x4D:
500         case 0x4C:  // AMT
501         case 0x6E:
502           is_atom_ = true;
503       }
504     }
505   }
506 
507   // There are separate feature flags for VEX-encoded GPR instructions.
508   if (num_ids >= 7) {
509     __cpuid(cpu_info, 7);
510     has_bmi1_ = (cpu_info[1] & 0x00000008) != 0;
511     has_bmi2_ = (cpu_info[1] & 0x00000100) != 0;
512   }
513 
514   // Query extended IDs.
515   __cpuid(cpu_info, 0x80000000);
516   unsigned num_ext_ids = cpu_info[0];
517 
518   // Interpret extended CPU feature information.
519   if (num_ext_ids > 0x80000000) {
520     __cpuid(cpu_info, 0x80000001);
521     has_lzcnt_ = (cpu_info[2] & 0x00000020) != 0;
522     // SAHF must be probed in long mode.
523     has_sahf_ = (cpu_info[2] & 0x00000001) != 0;
524   }
525 
526   // Check if CPU has non stoppable time stamp counter.
527   const unsigned parameter_containing_non_stop_time_stamp_counter = 0x80000007;
528   if (num_ext_ids >= parameter_containing_non_stop_time_stamp_counter) {
529     __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
530     has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
531   }
532 
533 #elif V8_HOST_ARCH_ARM
534 
535 #if V8_OS_LINUX
536 
537   CPUInfo cpu_info;
538 
539   // Extract implementor from the "CPU implementer" field.
540   char* implementer = cpu_info.ExtractField("CPU implementer");
541   if (implementer != nullptr) {
542     char* end;
543     implementer_ = strtol(implementer, &end, 0);
544     if (end == implementer) {
545       implementer_ = 0;
546     }
547     delete[] implementer;
548   }
549 
550   char* variant = cpu_info.ExtractField("CPU variant");
551   if (variant != nullptr) {
552     char* end;
553     variant_ = strtol(variant, &end, 0);
554     if (end == variant) {
555       variant_ = -1;
556     }
557     delete[] variant;
558   }
559 
560   // Extract part number from the "CPU part" field.
561   char* part = cpu_info.ExtractField("CPU part");
562   if (part != nullptr) {
563     char* end;
564     part_ = strtol(part, &end, 0);
565     if (end == part) {
566       part_ = 0;
567     }
568     delete[] part;
569   }
570 
571   // Extract architecture from the "CPU Architecture" field.
572   // The list is well-known, unlike the the output of
573   // the 'Processor' field which can vary greatly.
574   // See the definition of the 'proc_arch' array in
575   // $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
576   // same file.
577   char* architecture = cpu_info.ExtractField("CPU architecture");
578   if (architecture != nullptr) {
579     char* end;
580     architecture_ = strtol(architecture, &end, 10);
581     if (end == architecture) {
582       // Kernels older than 3.18 report "CPU architecture: AArch64" on ARMv8.
583       if (strcmp(architecture, "AArch64") == 0) {
584         architecture_ = 8;
585       } else {
586         architecture_ = 0;
587       }
588     }
589     delete[] architecture;
590 
591     // Unfortunately, it seems that certain ARMv6-based CPUs
592     // report an incorrect architecture number of 7!
593     //
594     // See http://code.google.com/p/android/issues/detail?id=10812
595     //
596     // We try to correct this by looking at the 'elf_platform'
597     // field reported by the 'Processor' field, which is of the
598     // form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
599     // an ARMv6-one. For example, the Raspberry Pi is one popular
600     // ARMv6 device that reports architecture 7.
601     if (architecture_ == 7) {
602       char* processor = cpu_info.ExtractField("Processor");
603       if (HasListItem(processor, "(v6l)")) {
604         architecture_ = 6;
605       }
606       delete[] processor;
607     }
608 
609     // elf_platform moved to the model name field in Linux v3.8.
610     if (architecture_ == 7) {
611       char* processor = cpu_info.ExtractField("model name");
612       if (HasListItem(processor, "(v6l)")) {
613         architecture_ = 6;
614       }
615       delete[] processor;
616     }
617   }
618 
619   // Try to extract the list of CPU features from ELF hwcaps.
620   uint32_t hwcaps = ReadELFHWCaps();
621   if (hwcaps != 0) {
622     has_idiva_ = (hwcaps & HWCAP_IDIVA) != 0;
623     has_neon_ = (hwcaps & HWCAP_NEON) != 0;
624     has_vfp_ = (hwcaps & HWCAP_VFP) != 0;
625     has_vfp3_ = (hwcaps & (HWCAP_VFPv3 | HWCAP_VFPv3D16 | HWCAP_VFPv4)) != 0;
626     has_vfp3_d32_ = (has_vfp3_ && ((hwcaps & HWCAP_VFPv3D16) == 0 ||
627                                    (hwcaps & HWCAP_VFPD32) != 0));
628   } else {
629     // Try to fallback to "Features" CPUInfo field.
630     char* features = cpu_info.ExtractField("Features");
631     has_idiva_ = HasListItem(features, "idiva");
632     has_neon_ = HasListItem(features, "neon");
633     has_thumb2_ = HasListItem(features, "thumb2");
634     has_vfp_ = HasListItem(features, "vfp");
635     if (HasListItem(features, "vfpv3d16")) {
636       has_vfp3_ = true;
637     } else if (HasListItem(features, "vfpv3")) {
638       has_vfp3_ = true;
639       has_vfp3_d32_ = true;
640     }
641     delete[] features;
642   }
643 
644   // Some old kernels will report vfp not vfpv3. Here we make an attempt
645   // to detect vfpv3 by checking for vfp *and* neon, since neon is only
646   // available on architectures with vfpv3. Checking neon on its own is
647   // not enough as it is possible to have neon without vfp.
648   if (has_vfp_ && has_neon_) {
649     has_vfp3_ = true;
650   }
651 
652   // VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
653   if (architecture_ < 7 && has_vfp3_) {
654     architecture_ = 7;
655   }
656 
657   // ARMv7 implies Thumb2.
658   if (architecture_ >= 7) {
659     has_thumb2_ = true;
660   }
661 
662   // The earliest architecture with Thumb2 is ARMv6T2.
663   if (has_thumb2_ && architecture_ < 6) {
664     architecture_ = 6;
665   }
666 
667   // We don't support any FPUs other than VFP.
668   has_fpu_ = has_vfp_;
669 
670 #elif V8_OS_QNX
671 
672   uint32_t cpu_flags = SYSPAGE_ENTRY(cpuinfo)->flags;
673   if (cpu_flags & ARM_CPU_FLAG_V7) {
674     architecture_ = 7;
675     has_thumb2_ = true;
676   } else if (cpu_flags & ARM_CPU_FLAG_V6) {
677     architecture_ = 6;
678     // QNX doesn't say if Thumb2 is available.
679     // Assume false for the architectures older than ARMv7.
680   }
681   DCHECK_GE(architecture_, 6);
682   has_fpu_ = (cpu_flags & CPU_FLAG_FPU) != 0;
683   has_vfp_ = has_fpu_;
684   if (cpu_flags & ARM_CPU_FLAG_NEON) {
685     has_neon_ = true;
686     has_vfp3_ = has_vfp_;
687 #ifdef ARM_CPU_FLAG_VFP_D32
688     has_vfp3_d32_ = (cpu_flags & ARM_CPU_FLAG_VFP_D32) != 0;
689 #endif
690   }
691   has_idiva_ = (cpu_flags & ARM_CPU_FLAG_IDIV) != 0;
692 
693 #endif  // V8_OS_LINUX
694 
695 #elif V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
696 
697   // Simple detection of FPU at runtime for Linux.
698   // It is based on /proc/cpuinfo, which reveals hardware configuration
699   // to user-space applications.  According to MIPS (early 2010), no similar
700   // facility is universally available on the MIPS architectures,
701   // so it's up to individual OSes to provide such.
702   CPUInfo cpu_info;
703   char* cpu_model = cpu_info.ExtractField("cpu model");
704   has_fpu_ = HasListItem(cpu_model, "FPU");
705   char* ASEs = cpu_info.ExtractField("ASEs implemented");
706   has_msa_ = HasListItem(ASEs, "msa");
707   delete[] cpu_model;
708   delete[] ASEs;
709 #ifdef V8_HOST_ARCH_MIPS
710   is_fp64_mode_ = __detect_fp64_mode();
711   architecture_ = __detect_mips_arch_revision();
712 #endif
713 
714 #elif V8_HOST_ARCH_ARM64
715 #ifdef V8_OS_WIN
716   // Windows makes high-resolution thread timing information available in
717   // user-space.
718   has_non_stop_time_stamp_counter_ = true;
719 
720 #elif V8_OS_LINUX
721   // Try to extract the list of CPU features from ELF hwcaps.
722   uint32_t hwcaps = ReadELFHWCaps();
723   if (hwcaps != 0) {
724     has_jscvt_ = (hwcaps & HWCAP_JSCVT) != 0;
725   } else {
726     // Try to fallback to "Features" CPUInfo field
727     CPUInfo cpu_info;
728     char* features = cpu_info.ExtractField("Features");
729     has_jscvt_ = HasListItem(features, "jscvt");
730     delete[] features;
731   }
732 #endif  // V8_OS_WIN
733 
734 #elif V8_HOST_ARCH_PPC || V8_HOST_ARCH_PPC64
735 
736 #ifndef USE_SIMULATOR
737 #if V8_OS_LINUX
738   // Read processor info from /proc/self/auxv.
739   char* auxv_cpu_type = nullptr;
740   FILE* fp = fopen("/proc/self/auxv", "r");
741   if (fp != nullptr) {
742 #if V8_TARGET_ARCH_PPC64
743     Elf64_auxv_t entry;
744 #else
745     Elf32_auxv_t entry;
746 #endif
747     for (;;) {
748       size_t n = fread(&entry, sizeof(entry), 1, fp);
749       if (n == 0 || entry.a_type == AT_NULL) {
750         break;
751       }
752       switch (entry.a_type) {
753         case AT_PLATFORM:
754           auxv_cpu_type = reinterpret_cast<char*>(entry.a_un.a_val);
755           break;
756         case AT_ICACHEBSIZE:
757           icache_line_size_ = entry.a_un.a_val;
758           break;
759         case AT_DCACHEBSIZE:
760           dcache_line_size_ = entry.a_un.a_val;
761           break;
762       }
763     }
764     fclose(fp);
765   }
766 
767   part_ = -1;
768   if (auxv_cpu_type) {
769     if (strcmp(auxv_cpu_type, "power9") == 0) {
770       part_ = PPC_POWER9;
771     } else if (strcmp(auxv_cpu_type, "power8") == 0) {
772       part_ = PPC_POWER8;
773     } else if (strcmp(auxv_cpu_type, "power7") == 0) {
774       part_ = PPC_POWER7;
775     } else if (strcmp(auxv_cpu_type, "power6") == 0) {
776       part_ = PPC_POWER6;
777     } else if (strcmp(auxv_cpu_type, "power5") == 0) {
778       part_ = PPC_POWER5;
779     } else if (strcmp(auxv_cpu_type, "ppc970") == 0) {
780       part_ = PPC_G5;
781     } else if (strcmp(auxv_cpu_type, "ppc7450") == 0) {
782       part_ = PPC_G4;
783     } else if (strcmp(auxv_cpu_type, "pa6t") == 0) {
784       part_ = PPC_PA6T;
785     }
786   }
787 
788 #elif V8_OS_AIX
789   switch (_system_configuration.implementation) {
790     case POWER_9:
791       part_ = PPC_POWER9;
792       break;
793     case POWER_8:
794       part_ = PPC_POWER8;
795       break;
796     case POWER_7:
797       part_ = PPC_POWER7;
798       break;
799     case POWER_6:
800       part_ = PPC_POWER6;
801       break;
802     case POWER_5:
803       part_ = PPC_POWER5;
804       break;
805   }
806 #endif  // V8_OS_AIX
807 #endif  // !USE_SIMULATOR
808 #endif  // V8_HOST_ARCH_PPC || V8_HOST_ARCH_PPC64
809 }
810 
811 }  // namespace base
812 }  // namespace v8
813