Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / base / cpu.cc
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 V8_LIBC_MSVCRT
8 #include <intrin.h>  // __cpuid()
9 #endif
10 #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()
15 #endif
16 #if V8_OS_QNX
17 #include <sys/syspage.h>  // cpuinfo
18 #endif
19 #if V8_OS_POSIX
20 #include <unistd.h>  // sysconf()
21 #endif
22
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
35 namespace v8 {
36 namespace base {
37
38 #if defined(__pnacl__)
39 // Portable host shouldn't do feature detection.
40 #elif V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
41
42 // Define __cpuid() for non-MSVC libraries.
43 #if !V8_LIBC_MSVCRT
44
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.
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
101 static uint32_t ReadELFHWCaps() {
102   uint32_t result = 0;
103 #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.
107   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   }
122 #endif
123   return result;
124 }
125
126 #endif  // V8_HOST_ARCH_ARM
127
128 #if V8_HOST_ARCH_MIPS
129 int __detect_fp64_mode(void) {
130   double result = 0;
131   // Bit representation of (double)1 is 0x3FF0000000000000.
132   __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");
144
145   return !(result == 1);
146 }
147
148
149 int __detect_mips_arch_revision(void) {
150   // TODO(dusmil): Do the specific syscall as soon as it is implemented in mips
151   // 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;
167 }
168 #endif
169
170 // Extract the information exposed by the kernel via /proc/cpuinfo.
171 class 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'.
265 static 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
294 CPU::CPU() : stepping_(0),
295              model_(0),
296              ext_model_(0),
297              family_(0),
298              ext_family_(0),
299              type_(0),
300              implementer_(0),
301              architecture_(0),
302              part_(0),
303              has_fpu_(false),
304              has_cmov_(false),
305              has_sahf_(false),
306              has_mmx_(false),
307              has_sse_(false),
308              has_sse2_(false),
309              has_sse3_(false),
310              has_ssse3_(false),
311              has_sse41_(false),
312              has_sse42_(false),
313              has_idiva_(false),
314              has_neon_(false),
315              has_thumb2_(false),
316              has_vfp_(false),
317              has_vfp3_(false),
318              has_vfp3_d32_(false),
319              is_fp64_mode_(false) {
320   memcpy(vendor_, "Unknown", 8);
321 #if V8_OS_NACL
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
326   int cpu_info[4];
327
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);
339   vendor_[12] = '\0';
340
341   // Interpret CPU feature information.
342   if (num_ids > 0) {
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;
359   }
360
361 #if V8_HOST_ARCH_IA32
362   // SAHF is always available in compat/legacy mode,
363   has_sahf_ = true;
364 #else
365   // Query extended IDs.
366   __cpuid(cpu_info, 0x80000000);
367   unsigned num_ext_ids = cpu_info[0];
368
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;
374   }
375 #endif
376
377 #elif V8_HOST_ARCH_ARM
378
379 #if V8_OS_LINUX
380
381   CPUInfo cpu_info;
382
383   // Extract implementor from the "CPU implementer" field.
384   char* implementer = cpu_info.ExtractField("CPU implementer");
385   if (implementer != NULL) {
386     char* end ;
387     implementer_ = strtol(implementer, &end, 0);
388     if (end == implementer) {
389       implementer_ = 0;
390     }
391     delete[] implementer;
392   }
393
394   // Extract part number from the "CPU part" field.
395   char* part = cpu_info.ExtractField("CPU part");
396   if (part != NULL) {
397     char* end ;
398     part_ = strtol(part, &end, 0);
399     if (end == part) {
400       part_ = 0;
401     }
402     delete[] part;
403   }
404
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
410   // same file.
411   char* architecture = cpu_info.ExtractField("CPU architecture");
412   if (architecture != NULL) {
413     char* end;
414     architecture_ = strtol(architecture, &end, 10);
415     if (end == architecture) {
416       architecture_ = 0;
417     }
418     delete[] architecture;
419
420     // Unfortunately, it seems that certain ARMv6-based CPUs
421     // report an incorrect architecture number of 7!
422     //
423     // See http://code.google.com/p/android/issues/detail?id=10812
424     //
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)")) {
433         architecture_ = 6;
434       }
435       delete[] processor;
436     }
437   }
438
439   // Try to extract the list of CPU features from ELF hwcaps.
440   uint32_t hwcaps = ReadELFHWCaps();
441   if (hwcaps != 0) {
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));
448   } else {
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")) {
456       has_vfp3_ = true;
457     } else if (HasListItem(features, "vfpv3")) {
458       has_vfp3_ = true;
459       has_vfp3_d32_ = true;
460     }
461     delete[] features;
462   }
463
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_) {
469     has_vfp3_ = true;
470   }
471
472   // VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
473   if (architecture_ < 7 && has_vfp3_) {
474     architecture_ = 7;
475   }
476
477   // ARMv7 implies Thumb2.
478   if (architecture_ >= 7) {
479     has_thumb2_ = true;
480   }
481
482   // The earliest architecture with Thumb2 is ARMv6T2.
483   if (has_thumb2_ && architecture_ < 6) {
484     architecture_ = 6;
485   }
486
487   // We don't support any FPUs other than VFP.
488   has_fpu_ = has_vfp_;
489
490 #elif V8_OS_QNX
491
492   uint32_t cpu_flags = SYSPAGE_ENTRY(cpuinfo)->flags;
493   if (cpu_flags & ARM_CPU_FLAG_V7) {
494     architecture_ = 7;
495     has_thumb2_ = true;
496   } else if (cpu_flags & ARM_CPU_FLAG_V6) {
497     architecture_ = 6;
498     // QNX doesn't say if Thumb2 is available.
499     // Assume false for the architectures older than ARMv7.
500   }
501   DCHECK(architecture_ >= 6);
502   has_fpu_ = (cpu_flags & CPU_FLAG_FPU) != 0;
503   has_vfp_ = has_fpu_;
504   if (cpu_flags & ARM_CPU_FLAG_NEON) {
505     has_neon_ = true;
506     has_vfp3_ = has_vfp_;
507 #ifdef ARM_CPU_FLAG_VFP_D32
508     has_vfp3_d32_ = (cpu_flags & ARM_CPU_FLAG_VFP_D32) != 0;
509 #endif
510   }
511   has_idiva_ = (cpu_flags & ARM_CPU_FLAG_IDIV) != 0;
512
513 #endif  // V8_OS_LINUX
514
515 #elif V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64
516
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.
522   CPUInfo cpu_info;
523   char* cpu_model = cpu_info.ExtractField("cpu model");
524   has_fpu_ = HasListItem(cpu_model, "FPU");
525   delete[] cpu_model;
526 #ifdef V8_HOST_ARCH_MIPS
527   is_fp64_mode_ = __detect_fp64_mode();
528   architecture_ = __detect_mips_arch_revision();
529 #endif
530
531 #elif V8_HOST_ARCH_ARM64
532
533   CPUInfo cpu_info;
534
535   // Extract implementor from the "CPU implementer" field.
536   char* implementer = cpu_info.ExtractField("CPU implementer");
537   if (implementer != NULL) {
538     char* end ;
539     implementer_ = strtol(implementer, &end, 0);
540     if (end == implementer) {
541       implementer_ = 0;
542     }
543     delete[] implementer;
544   }
545
546   // Extract part number from the "CPU part" field.
547   char* part = cpu_info.ExtractField("CPU part");
548   if (part != NULL) {
549     char* end ;
550     part_ = strtol(part, &end, 0);
551     if (end == part) {
552       part_ = 0;
553     }
554     delete[] part;
555   }
556
557 #endif
558 }
559
560 } }  // namespace v8::base