foobar2000 SDK  2015-01-14
Data Structures | Functions | Variables
cpu-features.c File Reference

Go to the source code of this file.

Data Structures

struct  CpuList
 

Functions

static void android_cpuInit (void)
 
static void android_cpuInitDummy (void)
 
static void android_cpuInitFamily (void)
 
int android_getCpuCount (void)
 
AndroidCpuFamily android_getCpuFamily (void)
 
uint64_t android_getCpuFeatures (void)
 
uint32_t android_getCpuIdArm (void)
 
int android_setCpu (int cpu_count, uint64_t cpu_features)
 
int android_setCpuArm (int cpu_count, uint64_t cpu_features, uint32_t cpu_id)
 
static __inline__ void cpulist_and (CpuList *list1, CpuList *list2)
 
static __inline__ int cpulist_count (CpuList *list)
 
static __inline__ void cpulist_init (CpuList *list)
 
static void cpulist_parse (CpuList *list, const char *line, int line_len)
 
static void cpulist_read_from (CpuList *list, const char *filename)
 
static __inline__ void cpulist_set (CpuList *list, int index)
 
static char * extract_cpuinfo_field (const char *buffer, int buflen, const char *field)
 
static int get_cpu_count (void)
 
static uint32_t get_elf_hwcap_from_getauxval (void)
 
static uint32_t get_elf_hwcap_from_proc_cpuinfo (const char *cpuinfo, int cpuinfo_len)
 
static uint32_t get_elf_hwcap_from_proc_self_auxv (void)
 
static int get_file_size (const char *pathname)
 
static int has_list_item (const char *list, const char *item)
 
static const char * parse_decimal (const char *input, const char *limit, int *result)
 
static const char * parse_hexadecimal (const char *input, const char *limit, int *result)
 
static const char * parse_number (const char *input, const char *limit, int base, int *result)
 
static int read_file (const char *pathname, char *buffer, size_t buffsize)
 
static __inline__ void x86_cpuid (int func, int values[4])
 

Variables

static const int android_cpufeatures_debug = 0
 
static int g_cpuCount
 
static AndroidCpuFamily g_cpuFamily
 
static uint64_t g_cpuFeatures
 
static uint32_t g_cpuIdArm
 
static int g_inited
 
static pthread_once_t g_once
 

Data Structure Documentation

struct CpuList

Definition at line 327 of file cpu-features.c.

Data Fields
uint32_t mask

Function Documentation

static void android_cpuInit ( void  )
static

Definition at line 627 of file cpu-features.c.

628 {
629  char* cpuinfo = NULL;
630  int cpuinfo_len;
631 
633 
634  g_cpuFeatures = 0;
635  g_cpuCount = 1;
636  g_inited = 1;
637 
638  cpuinfo_len = get_file_size("/proc/cpuinfo");
639  if (cpuinfo_len < 0) {
640  D("cpuinfo_len cannot be computed!");
641  return;
642  }
643  cpuinfo = (char *) malloc(cpuinfo_len);
644  if (cpuinfo == NULL) {
645  D("cpuinfo buffer could not be allocated");
646  return;
647  }
648  cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, cpuinfo_len);
649  D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len,
650  cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo);
651 
652  if (cpuinfo_len < 0) /* should not happen */ {
653  free(cpuinfo);
654  return;
655  }
656 
657  /* Count the CPU cores, the value may be 0 for single-core CPUs */
659  if (g_cpuCount == 0) {
660  g_cpuCount = 1;
661  }
662 
663  D("found cpuCount = %d\n", g_cpuCount);
664 
665 #ifdef __arm__
666  {
667  /* Extract architecture from the "CPU Architecture" field.
668  * The list is well-known, unlike the the output of
669  * the 'Processor' field which can vary greatly.
670  *
671  * See the definition of the 'proc_arch' array in
672  * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
673  * same file.
674  */
675  char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
676 
677  if (cpuArch != NULL) {
678  char* end;
679  long archNumber;
680  int hasARMv7 = 0;
681 
682  D("found cpuArch = '%s'\n", cpuArch);
683 
684  /* read the initial decimal number, ignore the rest */
685  archNumber = strtol(cpuArch, &end, 10);
686 
687  /* Note that ARMv8 is upwards compatible with ARMv7. */
688  if (end > cpuArch && archNumber >= 7) {
689  hasARMv7 = 1;
690  }
691 
692  /* Unfortunately, it seems that certain ARMv6-based CPUs
693  * report an incorrect architecture number of 7!
694  *
695  * See http://code.google.com/p/android/issues/detail?id=10812
696  *
697  * We try to correct this by looking at the 'elf_format'
698  * field reported by the 'Processor' field, which is of the
699  * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
700  * an ARMv6-one.
701  */
702  if (hasARMv7) {
703  char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len,
704  "Processor");
705  if (cpuProc != NULL) {
706  D("found cpuProc = '%s'\n", cpuProc);
707  if (has_list_item(cpuProc, "(v6l)")) {
708  D("CPU processor and architecture mismatch!!\n");
709  hasARMv7 = 0;
710  }
711  free(cpuProc);
712  }
713  }
714 
715  if (hasARMv7) {
717  }
718 
719  /* The LDREX / STREX instructions are available from ARMv6 */
720  if (archNumber >= 6) {
722  }
723 
724  free(cpuArch);
725  }
726 
727  /* Extract the list of CPU features from ELF hwcaps */
728  uint32_t hwcaps = 0;
729  hwcaps = get_elf_hwcap_from_getauxval();
730  if (!hwcaps) {
731  D("Parsing /proc/self/auxv to extract ELF hwcaps!\n");
733  }
734  if (!hwcaps) {
735  // Parsing /proc/self/auxv will fail from regular application
736  // processes on some Android platform versions, when this happens
737  // parse proc/cpuinfo instead.
738  D("Parsing /proc/cpuinfo to extract ELF hwcaps!\n");
739  hwcaps = get_elf_hwcap_from_proc_cpuinfo(cpuinfo, cpuinfo_len);
740  }
741 
742  if (hwcaps != 0) {
743  int has_vfp = (hwcaps & HWCAP_VFP);
744  int has_vfpv3 = (hwcaps & HWCAP_VFPv3);
745  int has_vfpv3d16 = (hwcaps & HWCAP_VFPv3D16);
746  int has_vfpv4 = (hwcaps & HWCAP_VFPv4);
747  int has_neon = (hwcaps & HWCAP_NEON);
748  int has_idiva = (hwcaps & HWCAP_IDIVA);
749  int has_idivt = (hwcaps & HWCAP_IDIVT);
750  int has_iwmmxt = (hwcaps & HWCAP_IWMMXT);
751 
752  // The kernel does a poor job at ensuring consistency when
753  // describing CPU features. So lots of guessing is needed.
754 
755  // 'vfpv4' implies VFPv3|VFP_FMA|FP16
756  if (has_vfpv4)
760 
761  // 'vfpv3' or 'vfpv3d16' imply VFPv3. Note that unlike GCC,
762  // a value of 'vfpv3' doesn't necessarily mean that the D32
763  // feature is present, so be conservative. All CPUs in the
764  // field that support D32 also support NEON, so this should
765  // not be a problem in practice.
766  if (has_vfpv3 || has_vfpv3d16)
768 
769  // 'vfp' is super ambiguous. Depending on the kernel, it can
770  // either mean VFPv2 or VFPv3. Make it depend on ARMv7.
771  if (has_vfp) {
774  else
776  }
777 
778  // Neon implies VFPv3|D32, and if vfpv4 is detected, NEON_FMA
779  if (has_neon) {
783  if (has_vfpv4)
785  }
786 
787  // VFPv3 implies VFPv2 and ARMv7
791 
792  if (has_idiva)
794  if (has_idivt)
796 
797  if (has_iwmmxt)
799  }
800 
801  /* Extract the cpuid value from various fields */
802  // The CPUID value is broken up in several entries in /proc/cpuinfo.
803  // This table is used to rebuild it from the entries.
804  static const struct CpuIdEntry {
805  const char* field;
806  char format;
807  char bit_lshift;
808  char bit_length;
809  } cpu_id_entries[] = {
810  { "CPU implementer", 'x', 24, 8 },
811  { "CPU variant", 'x', 20, 4 },
812  { "CPU part", 'x', 4, 12 },
813  { "CPU revision", 'd', 0, 4 },
814  };
815  size_t i;
816  D("Parsing /proc/cpuinfo to recover CPUID\n");
817  for (i = 0;
818  i < sizeof(cpu_id_entries)/sizeof(cpu_id_entries[0]);
819  ++i) {
820  const struct CpuIdEntry* entry = &cpu_id_entries[i];
821  char* value = extract_cpuinfo_field(cpuinfo,
822  cpuinfo_len,
823  entry->field);
824  if (value == NULL)
825  continue;
826 
827  D("field=%s value='%s'\n", entry->field, value);
828  char* value_end = value + strlen(value);
829  int val = 0;
830  const char* start = value;
831  const char* p;
832  if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) {
833  start += 2;
834  p = parse_hexadecimal(start, value_end, &val);
835  } else if (entry->format == 'x')
836  p = parse_hexadecimal(value, value_end, &val);
837  else
838  p = parse_decimal(value, value_end, &val);
839 
840  if (p > (const char*)start) {
841  val &= ((1 << entry->bit_length)-1);
842  val <<= entry->bit_lshift;
843  g_cpuIdArm |= (uint32_t) val;
844  }
845 
846  free(value);
847  }
848 
849  // Handle kernel configuration bugs that prevent the correct
850  // reporting of CPU features.
851  static const struct CpuFix {
852  uint32_t cpuid;
853  uint64_t or_flags;
854  } cpu_fixes[] = {
855  /* The Nexus 4 (Qualcomm Krait) kernel configuration
856  * forgets to report IDIV support. */
857  { 0x510006f2, ANDROID_CPU_ARM_FEATURE_IDIV_ARM |
859  { 0x510006f3, ANDROID_CPU_ARM_FEATURE_IDIV_ARM |
861  };
862  size_t n;
863  for (n = 0; n < sizeof(cpu_fixes)/sizeof(cpu_fixes[0]); ++n) {
864  const struct CpuFix* entry = &cpu_fixes[n];
865 
866  if (g_cpuIdArm == entry->cpuid)
867  g_cpuFeatures |= entry->or_flags;
868  }
869 
870  // Special case: The emulator-specific Android 4.2 kernel fails
871  // to report support for the 32-bit ARM IDIV instruction.
872  // Technically, this is a feature of the virtual CPU implemented
873  // by the emulator. Note that it could also support Thumb IDIV
874  // in the future, and this will have to be slightly updated.
875  char* hardware = extract_cpuinfo_field(cpuinfo,
876  cpuinfo_len,
877  "Hardware");
878  if (hardware) {
879  if (!strcmp(hardware, "Goldfish") &&
880  g_cpuIdArm == 0x4100c080 &&
883  }
884  free(hardware);
885  }
886  }
887 #endif /* __arm__ */
888 
889 #ifdef __i386__
890  int regs[4];
891 
892 /* According to http://en.wikipedia.org/wiki/CPUID */
893 #define VENDOR_INTEL_b 0x756e6547
894 #define VENDOR_INTEL_c 0x6c65746e
895 #define VENDOR_INTEL_d 0x49656e69
896 
897  x86_cpuid(0, regs);
898  int vendorIsIntel = (regs[1] == VENDOR_INTEL_b &&
899  regs[2] == VENDOR_INTEL_c &&
900  regs[3] == VENDOR_INTEL_d);
901 
902  x86_cpuid(1, regs);
903  if ((regs[2] & (1 << 9)) != 0) {
905  }
906  if ((regs[2] & (1 << 23)) != 0) {
908  }
909  if (vendorIsIntel && (regs[2] & (1 << 22)) != 0) {
911  }
912 #endif
913 
914  free(cpuinfo);
915 }
static char * extract_cpuinfo_field(const char *buffer, int buflen, const char *field)
Definition: cpu-features.c:193
static uint64_t g_cpuFeatures
Definition: cpu-features.c:82
static AndroidCpuFamily g_cpuFamily
Definition: cpu-features.c:81
static uint32_t get_elf_hwcap_from_getauxval(void)
Definition: cpu-features.c:472
static int get_file_size(const char *pathname)
Definition: cpu-features.c:124
static uint32_t get_elf_hwcap_from_proc_self_auxv(void)
Definition: cpu-features.c:500
static uint32_t get_elf_hwcap_from_proc_cpuinfo(const char *cpuinfo, int cpuinfo_len)
Definition: cpu-features.c:535
static void android_cpuInitFamily(void)
Definition: cpu-features.c:606
static __inline__ void x86_cpuid(int func, int values[4])
Definition: cpu-features.c:99
static const char * parse_hexadecimal(const char *input, const char *limit, int *result)
Definition: cpu-features.c:316
static int g_cpuCount
Definition: cpu-features.c:83
static int read_file(const char *pathname, char *buffer, size_t buffsize)
Definition: cpu-features.c:158
static int g_inited
Definition: cpu-features.c:80
static int has_list_item(const char *list, const char *item)
Definition: cpu-features.c:243
static const char * parse_decimal(const char *input, const char *limit, int *result)
Definition: cpu-features.c:310
static int get_cpu_count(void)
Definition: cpu-features.c:589
static uint32_t g_cpuIdArm
Definition: cpu-features.c:86
static void android_cpuInitDummy ( void  )
static

Definition at line 942 of file cpu-features.c.

943 {
944  g_inited = 1;
945 }
static int g_inited
Definition: cpu-features.c:80
static void android_cpuInitFamily ( void  )
static

Definition at line 606 of file cpu-features.c.

607 {
608 #if defined(__arm__)
610 #elif defined(__i386__)
612 #elif defined(__mips64)
613 /* Needs to be before __mips__ since the compiler defines both */
615 #elif defined(__mips__)
617 #elif defined(__aarch64__)
619 #elif defined(__x86_64__)
621 #else
623 #endif
624 }
static AndroidCpuFamily g_cpuFamily
Definition: cpu-features.c:81
int android_getCpuCount ( void  )

Definition at line 935 of file cpu-features.c.

936 {
937  pthread_once(&g_once, android_cpuInit);
938  return g_cpuCount;
939 }
static pthread_once_t g_once
Definition: cpu-features.c:79
static int g_cpuCount
Definition: cpu-features.c:83
static void android_cpuInit(void)
Definition: cpu-features.c:627
AndroidCpuFamily android_getCpuFamily ( void  )

Definition at line 919 of file cpu-features.c.

920 {
921  pthread_once(&g_once, android_cpuInit);
922  return g_cpuFamily;
923 }
static pthread_once_t g_once
Definition: cpu-features.c:79
static AndroidCpuFamily g_cpuFamily
Definition: cpu-features.c:81
static void android_cpuInit(void)
Definition: cpu-features.c:627
uint64_t android_getCpuFeatures ( void  )

Definition at line 927 of file cpu-features.c.

928 {
929  pthread_once(&g_once, android_cpuInit);
930  return g_cpuFeatures;
931 }
static pthread_once_t g_once
Definition: cpu-features.c:79
static uint64_t g_cpuFeatures
Definition: cpu-features.c:82
static void android_cpuInit(void)
Definition: cpu-features.c:627
uint32_t android_getCpuIdArm ( void  )

Definition at line 964 of file cpu-features.c.

965 {
966  pthread_once(&g_once, android_cpuInit);
967  return g_cpuIdArm;
968 }
static pthread_once_t g_once
Definition: cpu-features.c:79
static void android_cpuInit(void)
Definition: cpu-features.c:627
static uint32_t g_cpuIdArm
Definition: cpu-features.c:86
int android_setCpu ( int  cpu_count,
uint64_t  cpu_features 
)

Definition at line 948 of file cpu-features.c.

949 {
950  /* Fail if the library was already initialized. */
951  if (g_inited)
952  return 0;
953 
955  g_cpuCount = (cpu_count <= 0 ? 1 : cpu_count);
956  g_cpuFeatures = cpu_features;
957  pthread_once(&g_once, android_cpuInitDummy);
958 
959  return 1;
960 }
static pthread_once_t g_once
Definition: cpu-features.c:79
static uint64_t g_cpuFeatures
Definition: cpu-features.c:82
static void android_cpuInitFamily(void)
Definition: cpu-features.c:606
static int g_cpuCount
Definition: cpu-features.c:83
static int g_inited
Definition: cpu-features.c:80
static void android_cpuInitDummy(void)
Definition: cpu-features.c:942
int android_setCpuArm ( int  cpu_count,
uint64_t  cpu_features,
uint32_t  cpu_id 
)

Definition at line 971 of file cpu-features.c.

972 {
973  if (!android_setCpu(cpu_count, cpu_features))
974  return 0;
975 
976  g_cpuIdArm = cpu_id;
977  return 1;
978 }
int android_setCpu(int cpu_count, uint64_t cpu_features)
Definition: cpu-features.c:948
static uint32_t g_cpuIdArm
Definition: cpu-features.c:86
static __inline__ void cpulist_and ( CpuList list1,
CpuList list2 
)
static

Definition at line 337 of file cpu-features.c.

337  {
338  list1->mask &= list2->mask;
339 }
uint32_t mask
Definition: cpu-features.c:328
static __inline__ int cpulist_count ( CpuList list)
static

Definition at line 349 of file cpu-features.c.

349  {
350  return __builtin_popcount(list->mask);
351 }
uint32_t mask
Definition: cpu-features.c:328
static __inline__ void cpulist_init ( CpuList list)
static

Definition at line 332 of file cpu-features.c.

332  {
333  list->mask = 0;
334 }
uint32_t mask
Definition: cpu-features.c:328
static void cpulist_parse ( CpuList list,
const char *  line,
int  line_len 
)
static

Definition at line 364 of file cpu-features.c.

365 {
366  const char* p = line;
367  const char* end = p + line_len;
368  const char* q;
369 
370  /* NOTE: the input line coming from sysfs typically contains a
371  * trailing newline, so take care of it in the code below
372  */
373  while (p < end && *p != '\n')
374  {
375  int val, start_value, end_value;
376 
377  /* Find the end of current item, and put it into 'q' */
378  q = (const char *) memchr(p, ',', end-p);
379  if (q == NULL) {
380  q = end;
381  }
382 
383  /* Get first value */
384  p = parse_decimal(p, q, &start_value);
385  if (p == NULL)
386  goto BAD_FORMAT;
387 
388  end_value = start_value;
389 
390  /* If we're not at the end of the item, expect a dash and
391  * and integer; extract end value.
392  */
393  if (p < q && *p == '-') {
394  p = parse_decimal(p+1, q, &end_value);
395  if (p == NULL)
396  goto BAD_FORMAT;
397  }
398 
399  /* Set bits CPU list bits */
400  for (val = start_value; val <= end_value; val++) {
401  cpulist_set(list, val);
402  }
403 
404  /* Jump to next item */
405  p = q;
406  if (p < end)
407  p++;
408  }
409 
410 BAD_FORMAT:
411  ;
412 }
static __inline__ void cpulist_set(CpuList *list, int index)
Definition: cpu-features.c:342
static const char * parse_decimal(const char *input, const char *limit, int *result)
Definition: cpu-features.c:310
static void cpulist_read_from ( CpuList list,
const char *  filename 
)
static

Definition at line 416 of file cpu-features.c.

417 {
418  char file[64];
419  int filelen;
420 
421  cpulist_init(list);
422 
423  filelen = read_file(filename, file, sizeof file);
424  if (filelen < 0) {
425  D("Could not read %s: %s\n", filename, strerror(errno));
426  return;
427  }
428 
429  cpulist_parse(list, file, filelen);
430 }
static void cpulist_parse(CpuList *list, const char *line, int line_len)
Definition: cpu-features.c:364
static __inline__ void cpulist_init(CpuList *list)
Definition: cpu-features.c:332
static int read_file(const char *pathname, char *buffer, size_t buffsize)
Definition: cpu-features.c:158
static __inline__ void cpulist_set ( CpuList list,
int  index 
)
static

Definition at line 342 of file cpu-features.c.

342  {
343  if ((unsigned)index < 32) {
344  list->mask |= (uint32_t)(1U << index);
345  }
346 }
uint32_t mask
Definition: cpu-features.c:328
static char* extract_cpuinfo_field ( const char *  buffer,
int  buflen,
const char *  field 
)
static

Definition at line 193 of file cpu-features.c.

194 {
195  int fieldlen = strlen(field);
196  const char* bufend = buffer + buflen;
197  char* result = NULL;
198  int len, ignore;
199  const char *p, *q;
200 
201  /* Look for first field occurence, and ensures it starts the line. */
202  p = buffer;
203  for (;;) {
204  p = (const char *) memmem(p, bufend-p, field, fieldlen);
205  if (p == NULL)
206  goto EXIT;
207 
208  if (p == buffer || p[-1] == '\n')
209  break;
210 
211  p += fieldlen;
212  }
213 
214  /* Skip to the first column followed by a space */
215  p += fieldlen;
216  p = (const char *) memchr(p, ':', bufend-p);
217  if (p == NULL || p[1] != ' ')
218  goto EXIT;
219 
220  /* Find the end of the line */
221  p += 2;
222  q = (const char *) memchr(p, '\n', bufend-p);
223  if (q == NULL)
224  q = bufend;
225 
226  /* Copy the line into a heap-allocated buffer */
227  len = q-p;
228  result = (char *) malloc(len+1);
229  if (result == NULL)
230  goto EXIT;
231 
232  memcpy(result, p, len);
233  result[len] = '\0';
234 
235 EXIT:
236  return result;
237 }
static int get_cpu_count ( void  )
static

Definition at line 589 of file cpu-features.c.

590 {
591  CpuList cpus_present[1];
592  CpuList cpus_possible[1];
593 
594  cpulist_read_from(cpus_present, "/sys/devices/system/cpu/present");
595  cpulist_read_from(cpus_possible, "/sys/devices/system/cpu/possible");
596 
597  /* Compute the intersection of both sets to get the actual number of
598  * CPU cores that can be used on this device by the kernel.
599  */
600  cpulist_and(cpus_present, cpus_possible);
601 
602  return cpulist_count(cpus_present);
603 }
static __inline__ int cpulist_count(CpuList *list)
Definition: cpu-features.c:349
static __inline__ void cpulist_and(CpuList *list1, CpuList *list2)
Definition: cpu-features.c:337
static void cpulist_read_from(CpuList *list, const char *filename)
Definition: cpu-features.c:416
static uint32_t get_elf_hwcap_from_getauxval ( void  )
static

Definition at line 472 of file cpu-features.c.

472  {
473  typedef unsigned long getauxval_func_t(unsigned long);
474 
475  dlerror();
476  void* libc_handle = dlopen("libc.so", RTLD_NOW);
477  if (!libc_handle) {
478  D("Could not dlopen() C library: %s\n", dlerror());
479  return 0;
480  }
481 
482  uint32_t ret = 0;
483  getauxval_func_t* func = (getauxval_func_t*)
484  dlsym(libc_handle, "getauxval");
485  if (!func) {
486  D("Could not find getauxval() in C library\n");
487  } else {
488  // Note: getauxval() returns 0 on failure. Doesn't touch errno.
489  ret = (uint32_t)(*func)(AT_HWCAP);
490  }
491  dlclose(libc_handle);
492  return ret;
493 }
static uint32_t get_elf_hwcap_from_proc_cpuinfo ( const char *  cpuinfo,
int  cpuinfo_len 
)
static

Definition at line 535 of file cpu-features.c.

535  {
536  uint32_t hwcaps = 0;
537  long architecture = 0;
538  char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
539  if (cpuArch) {
540  architecture = strtol(cpuArch, NULL, 10);
541  free(cpuArch);
542 
543  if (architecture >= 8L) {
544  // This is a 32-bit ARM binary running on a 64-bit ARM64 kernel.
545  // The 'Features' line only lists the optional features that the
546  // device's CPU supports, compared to its reference architecture
547  // which are of no use for this process.
548  D("Faking 32-bit ARM HWCaps on ARMv%ld CPU\n", architecture);
549  return HWCAP_SET_FOR_ARMV8;
550  }
551  }
552 
553  char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features");
554  if (cpuFeatures != NULL) {
555  D("Found cpuFeatures = '%s'\n", cpuFeatures);
556 
557  if (has_list_item(cpuFeatures, "vfp"))
558  hwcaps |= HWCAP_VFP;
559  if (has_list_item(cpuFeatures, "vfpv3"))
560  hwcaps |= HWCAP_VFPv3;
561  if (has_list_item(cpuFeatures, "vfpv3d16"))
562  hwcaps |= HWCAP_VFPv3D16;
563  if (has_list_item(cpuFeatures, "vfpv4"))
564  hwcaps |= HWCAP_VFPv4;
565  if (has_list_item(cpuFeatures, "neon"))
566  hwcaps |= HWCAP_NEON;
567  if (has_list_item(cpuFeatures, "idiva"))
568  hwcaps |= HWCAP_IDIVA;
569  if (has_list_item(cpuFeatures, "idivt"))
570  hwcaps |= HWCAP_IDIVT;
571  if (has_list_item(cpuFeatures, "idiv"))
572  hwcaps |= HWCAP_IDIVA | HWCAP_IDIVT;
573  if (has_list_item(cpuFeatures, "iwmmxt"))
574  hwcaps |= HWCAP_IWMMXT;
575 
576  free(cpuFeatures);
577  }
578  return hwcaps;
579 }
static char * extract_cpuinfo_field(const char *buffer, int buflen, const char *field)
Definition: cpu-features.c:193
static int has_list_item(const char *list, const char *item)
Definition: cpu-features.c:243
static uint32_t get_elf_hwcap_from_proc_self_auxv ( void  )
static

Definition at line 500 of file cpu-features.c.

500  {
501  const char filepath[] = "/proc/self/auxv";
502  int fd = TEMP_FAILURE_RETRY(open(filepath, O_RDONLY));
503  if (fd < 0) {
504  D("Could not open %s: %s\n", filepath, strerror(errno));
505  return 0;
506  }
507 
508  struct { uint32_t tag; uint32_t value; } entry;
509 
510  uint32_t result = 0;
511  for (;;) {
512  int ret = TEMP_FAILURE_RETRY(read(fd, (char*)&entry, sizeof entry));
513  if (ret < 0) {
514  D("Error while reading %s: %s\n", filepath, strerror(errno));
515  break;
516  }
517  // Detect end of list.
518  if (ret == 0 || (entry.tag == 0 && entry.value == 0))
519  break;
520  if (entry.tag == AT_HWCAP) {
521  result = entry.value;
522  break;
523  }
524  }
525  close(fd);
526  return result;
527 }
void read(const service_ptr_t< file > &p_file, abort_callback &p_abort, pfc::string_base &p_out, bool &is_utf8)
static int get_file_size ( const char *  pathname)
static

Definition at line 124 of file cpu-features.c.

125 {
126  int fd, ret, result = 0;
127  char buffer[256];
128 
129  fd = open(pathname, O_RDONLY);
130  if (fd < 0) {
131  D("Can't open %s: %s\n", pathname, strerror(errno));
132  return -1;
133  }
134 
135  for (;;) {
136  int ret = read(fd, buffer, sizeof buffer);
137  if (ret < 0) {
138  if (errno == EINTR)
139  continue;
140  D("Error while reading %s: %s\n", pathname, strerror(errno));
141  break;
142  }
143  if (ret == 0)
144  break;
145 
146  result += ret;
147  }
148  close(fd);
149  return result;
150 }
void read(const service_ptr_t< file > &p_file, abort_callback &p_abort, pfc::string_base &p_out, bool &is_utf8)
static int has_list_item ( const char *  list,
const char *  item 
)
static

Definition at line 243 of file cpu-features.c.

244 {
245  const char* p = list;
246  int itemlen = strlen(item);
247 
248  if (list == NULL)
249  return 0;
250 
251  while (*p) {
252  const char* q;
253 
254  /* skip spaces */
255  while (*p == ' ' || *p == '\t')
256  p++;
257 
258  /* find end of current list item */
259  q = p;
260  while (*q && *q != ' ' && *q != '\t')
261  q++;
262 
263  if (itemlen == q-p && !memcmp(p, item, itemlen))
264  return 1;
265 
266  /* skip to next item */
267  p = q;
268  }
269  return 0;
270 }
static const char* parse_decimal ( const char *  input,
const char *  limit,
int *  result 
)
static

Definition at line 310 of file cpu-features.c.

311 {
312  return parse_number(input, limit, 10, result);
313 }
static const char * parse_number(const char *input, const char *limit, int base, int *result)
Definition: cpu-features.c:283
static const char* parse_hexadecimal ( const char *  input,
const char *  limit,
int *  result 
)
static

Definition at line 316 of file cpu-features.c.

317 {
318  return parse_number(input, limit, 16, result);
319 }
static const char * parse_number(const char *input, const char *limit, int base, int *result)
Definition: cpu-features.c:283
static const char* parse_number ( const char *  input,
const char *  limit,
int  base,
int *  result 
)
static

Definition at line 283 of file cpu-features.c.

284 {
285  const char* p = input;
286  int val = 0;
287  while (p < limit) {
288  int d = (*p - '0');
289  if ((unsigned)d >= 10U) {
290  d = (*p - 'a');
291  if ((unsigned)d >= 6U)
292  d = (*p - 'A');
293  if ((unsigned)d >= 6U)
294  break;
295  d += 10;
296  }
297  if (d >= base)
298  break;
299  val = val*base + d;
300  p++;
301  }
302  if (p == input)
303  return NULL;
304 
305  *result = val;
306  return p;
307 }
static int read_file ( const char *  pathname,
char *  buffer,
size_t  buffsize 
)
static

Definition at line 158 of file cpu-features.c.

159 {
160  int fd, count;
161 
162  fd = open(pathname, O_RDONLY);
163  if (fd < 0) {
164  D("Could not open %s: %s\n", pathname, strerror(errno));
165  return -1;
166  }
167  count = 0;
168  while (count < (int)buffsize) {
169  int ret = read(fd, buffer + count, buffsize - count);
170  if (ret < 0) {
171  if (errno == EINTR)
172  continue;
173  D("Error while reading from %s: %s\n", pathname, strerror(errno));
174  if (count == 0)
175  count = -1;
176  break;
177  }
178  if (ret == 0)
179  break;
180  count += ret;
181  }
182  close(fd);
183  return count;
184 }
void read(const service_ptr_t< file > &p_file, abort_callback &p_abort, pfc::string_base &p_out, bool &is_utf8)
static __inline__ void x86_cpuid ( int  func,
int  values[4] 
)
static

Definition at line 99 of file cpu-features.c.

100 {
101  int a, b, c, d;
102  /* We need to preserve ebx since we're compiling PIC code */
103  /* this means we can't use "=b" for the second output register */
104  __asm__ __volatile__ ( \
105  "push %%ebx\n"
106  "cpuid\n" \
107  "mov %%ebx, %1\n"
108  "pop %%ebx\n"
109  : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
110  : "a" (func) \
111  );
112  values[0] = a;
113  values[1] = b;
114  values[2] = c;
115  values[3] = d;
116 }

Variable Documentation

const int android_cpufeatures_debug = 0
static

Definition at line 89 of file cpu-features.c.

int g_cpuCount
static

Definition at line 83 of file cpu-features.c.

AndroidCpuFamily g_cpuFamily
static

Definition at line 81 of file cpu-features.c.

uint64_t g_cpuFeatures
static

Definition at line 82 of file cpu-features.c.

uint32_t g_cpuIdArm
static

Definition at line 86 of file cpu-features.c.

int g_inited
static

Definition at line 80 of file cpu-features.c.

pthread_once_t g_once
static

Definition at line 79 of file cpu-features.c.