5 #include "src/base/cpu.h" 11 #include <linux/auxvec.h> 13 #if V8_GLIBC_PREREQ(2, 16) 17 #include <sys/syspage.h> 19 #if V8_OS_LINUX && V8_HOST_ARCH_PPC 23 #include <sys/systemcfg.h> 25 #define POWER_8 0x10000 28 #define POWER_9 0x20000 42 #include "src/base/logging.h" 44 #include "src/base/win32-headers.h" 50 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64 55 static V8_INLINE
void __cpuid(
int cpu_info[4],
int info_type) {
58 #if defined(__i386__) && defined(__pic__) 62 "mov %%ebx, %%edi\n\t" 64 "xchg %%edi, %%ebx\n\t" 65 :
"=a"(cpu_info[0]),
"=D"(cpu_info[1]),
"=c"(cpu_info[2]),
67 :
"a"(info_type),
"c"(0));
69 __asm__
volatile(
"cpuid \n\t" 70 :
"=a"(cpu_info[0]),
"=b"(cpu_info[1]),
"=c"(cpu_info[2]),
72 :
"a"(info_type),
"c"(0));
73 #endif // defined(__i386__) && defined(__pic__) 76 #endif // !V8_LIBC_MSVCRT 78 #elif V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64 88 #define HWCAP_SWP (1 << 0) 89 #define HWCAP_HALF (1 << 1) 90 #define HWCAP_THUMB (1 << 2) 91 #define HWCAP_26BIT (1 << 3) 92 #define HWCAP_FAST_MULT (1 << 4) 93 #define HWCAP_FPA (1 << 5) 94 #define HWCAP_VFP (1 << 6) 95 #define HWCAP_EDSP (1 << 7) 96 #define HWCAP_JAVA (1 << 8) 97 #define HWCAP_IWMMXT (1 << 9) 98 #define HWCAP_CRUNCH (1 << 10) 99 #define HWCAP_THUMBEE (1 << 11) 100 #define HWCAP_NEON (1 << 12) 101 #define HWCAP_VFPv3 (1 << 13) 102 #define HWCAP_VFPv3D16 (1 << 14) 103 #define HWCAP_TLS (1 << 15) 104 #define HWCAP_VFPv4 (1 << 16) 105 #define HWCAP_IDIVA (1 << 17) 106 #define HWCAP_IDIVT (1 << 18) 107 #define HWCAP_VFPD32 (1 << 19) 108 #define HWCAP_IDIV (HWCAP_IDIVA | HWCAP_IDIVT) 109 #define HWCAP_LPAE (1 << 20) 113 #if V8_GLIBC_PREREQ(2, 16) 114 result =
static_cast<uint32_t>(getauxval(AT_HWCAP));
117 FILE* fp = fopen(
"/proc/self/auxv",
"r");
121 size_t n = fread(&entry,
sizeof(entry), 1, fp);
122 if (n == 0 || (entry.tag == 0 && entry.value == 0)) {
125 if (entry.tag == AT_HWCAP) {
126 result = entry.value;
136 #endif // V8_HOST_ARCH_ARM 138 #if V8_HOST_ARCH_MIPS 139 int __detect_fp64_mode(
void) {
146 "lui $t0, 0x3FF0\n\t" 153 :
"t0",
"$f0",
"$f1",
"memory");
155 return !(result == 1);
159 int __detect_mips_arch_revision(
void) {
164 "move $v0, $zero\n\t" 169 ".word 0x20420001\n\t" 176 return result ? 1 : 6;
178 #endif // V8_HOST_ARCH_MIPS 181 class CPUInfo final {
183 CPUInfo() : datalen_(0) {
187 static const char PATHNAME[] =
"/proc/cpuinfo";
188 FILE* fp = fopen(PATHNAME,
"r");
192 size_t n = fread(buffer, 1,
sizeof(buffer), fp);
202 data_ =
new char[datalen_ + 1];
203 fp = fopen(PATHNAME,
"r");
205 for (
size_t offset = 0; offset < datalen_; ) {
206 size_t n = fread(data_ + offset, 1, datalen_ - offset, fp);
216 data_[datalen_] =
'\0';
227 char* ExtractField(
const char* field)
const {
228 DCHECK_NOT_NULL(field);
231 size_t fieldlen = strlen(field);
234 p = strstr(p, field);
238 if (p == data_ || p[-1] ==
'\n') {
245 p = strchr(p + fieldlen,
':');
246 if (p ==
nullptr || !isspace(p[1])) {
252 char* q = strchr(p,
'\n');
254 q = data_ + datalen_;
259 char* result =
new char[len + 1];
260 if (result !=
nullptr) {
261 memcpy(result, p, len);
273 static bool HasListItem(
const char* list,
const char* item) {
274 ssize_t item_len = strlen(item);
275 const char* p = list;
279 while (isspace(*p)) ++p;
283 while (*q !=
'\0' && !isspace(*q)) ++q;
285 if (item_len == q - p && memcmp(p, item, item_len) == 0) {
296 #endif // V8_OS_LINUX 298 #endif // V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64 311 icache_line_size_(UNKNOWN_CACHE_LINE_SIZE),
312 dcache_line_size_(UNKNOWN_CACHE_LINE_SIZE),
336 has_vfp3_d32_(false),
337 is_fp64_mode_(false),
338 has_non_stop_time_stamp_counter_(false),
340 memcpy(vendor_,
"Unknown", 8);
341 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64 351 __cpuid(cpu_info, 0);
352 unsigned num_ids = cpu_info[0];
353 std::swap(cpu_info[2], cpu_info[3]);
354 memcpy(vendor_, cpu_info + 1, 12);
359 __cpuid(cpu_info, 1);
360 stepping_ = cpu_info[0] & 0xF;
361 model_ = ((cpu_info[0] >> 4) & 0xF) + ((cpu_info[0] >> 12) & 0xF0);
362 family_ = (cpu_info[0] >> 8) & 0xF;
363 type_ = (cpu_info[0] >> 12) & 0x3;
364 ext_model_ = (cpu_info[0] >> 16) & 0xF;
365 ext_family_ = (cpu_info[0] >> 20) & 0xFF;
366 has_fpu_ = (cpu_info[3] & 0x00000001) != 0;
367 has_cmov_ = (cpu_info[3] & 0x00008000) != 0;
368 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
369 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
370 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
371 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
372 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
373 has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
374 has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
375 has_popcnt_ = (cpu_info[2] & 0x00800000) != 0;
376 has_osxsave_ = (cpu_info[2] & 0x08000000) != 0;
377 has_avx_ = (cpu_info[2] & 0x10000000) != 0;
378 has_fma3_ = (cpu_info[2] & 0x00001000) != 0;
380 if (family_ == 0x6) {
399 __cpuid(cpu_info, 7);
400 has_bmi1_ = (cpu_info[1] & 0x00000008) != 0;
401 has_bmi2_ = (cpu_info[1] & 0x00000100) != 0;
405 __cpuid(cpu_info, 0x80000000);
406 unsigned num_ext_ids = cpu_info[0];
409 if (num_ext_ids > 0x80000000) {
410 __cpuid(cpu_info, 0x80000001);
411 has_lzcnt_ = (cpu_info[2] & 0x00000020) != 0;
413 has_sahf_ = (cpu_info[2] & 0x00000001) != 0;
417 const unsigned parameter_containing_non_stop_time_stamp_counter = 0x80000007;
418 if (num_ext_ids >= parameter_containing_non_stop_time_stamp_counter) {
419 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
420 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
423 #elif V8_HOST_ARCH_ARM 430 char* implementer = cpu_info.ExtractField(
"CPU implementer");
431 if (implementer !=
nullptr) {
433 implementer_ = strtol(implementer, &end, 0);
434 if (end == implementer) {
437 delete[] implementer;
440 char* variant = cpu_info.ExtractField(
"CPU variant");
441 if (variant !=
nullptr) {
443 variant_ = strtol(variant, &end, 0);
444 if (end == variant) {
451 char* part = cpu_info.ExtractField(
"CPU part");
452 if (part !=
nullptr) {
454 part_ = strtol(part, &end, 0);
467 char* architecture = cpu_info.ExtractField(
"CPU architecture");
468 if (architecture !=
nullptr) {
470 architecture_ = strtol(architecture, &end, 10);
471 if (end == architecture) {
473 if (strcmp(architecture,
"AArch64") == 0) {
479 delete[] architecture;
491 if (architecture_ == 7) {
492 char* processor = cpu_info.ExtractField(
"Processor");
493 if (HasListItem(processor,
"(v6l)")) {
500 if (architecture_ == 7) {
501 char* processor = cpu_info.ExtractField(
"model name");
502 if (HasListItem(processor,
"(v6l)")) {
512 has_idiva_ = (hwcaps & HWCAP_IDIVA) != 0;
513 has_neon_ = (hwcaps & HWCAP_NEON) != 0;
514 has_vfp_ = (hwcaps & HWCAP_VFP) != 0;
515 has_vfp3_ = (hwcaps & (HWCAP_VFPv3 | HWCAP_VFPv3D16 | HWCAP_VFPv4)) != 0;
516 has_vfp3_d32_ = (has_vfp3_ && ((hwcaps & HWCAP_VFPv3D16) == 0 ||
517 (hwcaps & HWCAP_VFPD32) != 0));
520 char* features = cpu_info.ExtractField(
"Features");
521 has_idiva_ = HasListItem(features,
"idiva");
522 has_neon_ = HasListItem(features,
"neon");
523 has_thumb2_ = HasListItem(features,
"thumb2");
524 has_vfp_ = HasListItem(features,
"vfp");
525 if (HasListItem(features,
"vfpv3d16")) {
527 }
else if (HasListItem(features,
"vfpv3")) {
529 has_vfp3_d32_ =
true;
538 if (has_vfp_ && has_neon_) {
543 if (architecture_ < 7 && has_vfp3_) {
548 if (architecture_ >= 7) {
553 if (has_thumb2_ && architecture_ < 6) {
562 uint32_t cpu_flags = SYSPAGE_ENTRY(cpuinfo)->flags;
563 if (cpu_flags & ARM_CPU_FLAG_V7) {
566 }
else if (cpu_flags & ARM_CPU_FLAG_V6) {
571 DCHECK_GE(architecture_, 6);
572 has_fpu_ = (cpu_flags & CPU_FLAG_FPU) != 0;
574 if (cpu_flags & ARM_CPU_FLAG_NEON) {
576 has_vfp3_ = has_vfp_;
577 #ifdef ARM_CPU_FLAG_VFP_D32 578 has_vfp3_d32_ = (cpu_flags & ARM_CPU_FLAG_VFP_D32) != 0;
581 has_idiva_ = (cpu_flags & ARM_CPU_FLAG_IDIV) != 0;
583 #endif // V8_OS_LINUX 585 #elif V8_HOST_ARCH_MIPS || V8_HOST_ARCH_MIPS64 593 char* cpu_model = cpu_info.ExtractField(
"cpu model");
594 has_fpu_ = HasListItem(cpu_model,
"FPU");
595 char* ASEs = cpu_info.ExtractField(
"ASEs implemented");
596 has_msa_ = HasListItem(ASEs,
"msa");
599 #ifdef V8_HOST_ARCH_MIPS 600 is_fp64_mode_ = __detect_fp64_mode();
601 architecture_ = __detect_mips_arch_revision();
604 #elif V8_HOST_ARCH_ARM64 607 #elif V8_HOST_ARCH_PPC 609 #ifndef USE_SIMULATOR 612 char* auxv_cpu_type =
nullptr;
613 FILE* fp = fopen(
"/proc/self/auxv",
"r");
615 #if V8_TARGET_ARCH_PPC64 621 size_t n = fread(&entry,
sizeof(entry), 1, fp);
622 if (n == 0 || entry.a_type == AT_NULL) {
625 switch (entry.a_type) {
627 auxv_cpu_type =
reinterpret_cast<char*
>(entry.a_un.a_val);
630 icache_line_size_ = entry.a_un.a_val;
633 dcache_line_size_ = entry.a_un.a_val;
642 if (strcmp(auxv_cpu_type,
"power9") == 0) {
644 }
else if (strcmp(auxv_cpu_type,
"power8") == 0) {
646 }
else if (strcmp(auxv_cpu_type,
"power7") == 0) {
648 }
else if (strcmp(auxv_cpu_type,
"power6") == 0) {
650 }
else if (strcmp(auxv_cpu_type,
"power5") == 0) {
652 }
else if (strcmp(auxv_cpu_type,
"ppc970") == 0) {
654 }
else if (strcmp(auxv_cpu_type,
"ppc7450") == 0) {
656 }
else if (strcmp(auxv_cpu_type,
"pa6t") == 0) {
662 switch (_system_configuration.implementation) {
680 #endif // !USE_SIMULATOR 681 #endif // V8_HOST_ARCH_PPC