FreeRDP
cpu-features.c
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in
12  * the documentation and/or other materials provided with the
13  * distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* ChangeLog for this library:
30  *
31  * NDK r10e?: Add MIPS MSA feature.
32  *
33  * NDK r10: Support for 64-bit CPUs (Intel, ARM & MIPS).
34  *
35  * NDK r8d: Add android_setCpu().
36  *
37  * NDK r8c: Add new ARM CPU features: VFPv2, VFP_D32, VFP_FP16,
38  * VFP_FMA, NEON_FMA, IDIV_ARM, IDIV_THUMB2 and iWMMXt.
39  *
40  * Rewrite the code to parse /proc/self/auxv instead of
41  * the "Features" field in /proc/cpuinfo.
42  *
43  * Dynamically allocate the buffer that hold the content
44  * of /proc/cpuinfo to deal with newer hardware.
45  *
46  * NDK r7c: Fix CPU count computation. The old method only reported the
47  * number of _active_ CPUs when the library was initialized,
48  * which could be less than the real total.
49  *
50  * NDK r5: Handle buggy kernels which report a CPU Architecture number of 7
51  * for an ARMv6 CPU (see below).
52  *
53  * Handle kernels that only report 'neon', and not 'vfpv3'
54  * (VFPv3 is mandated by the ARM architecture is Neon is implemented)
55  *
56  * Handle kernels that only report 'vfpv3d16', and not 'vfpv3'
57  *
58  * Fix x86 compilation. Report ANDROID_CPU_FAMILY_X86 in
59  * android_getCpuFamily().
60  *
61  * NDK r4: Initial release
62  */
63 
64 #include "cpu-features.h"
65 
66 #include <dlfcn.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <pthread.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <sys/system_properties.h>
74 #include <unistd.h>
75 #include <winpr/wtypes.h>
76 #include <winpr/debug.h>
77 
78 static pthread_once_t g_once;
79 static int g_inited;
80 static AndroidCpuFamily g_cpuFamily;
81 static uint64_t g_cpuFeatures;
82 static int g_cpuCount;
83 
84 #ifdef __arm__
85 static uint32_t g_cpuIdArm;
86 #endif
87 
88 static const int android_cpufeatures_debug = 0;
89 
90 #define D(...) \
91  do \
92  { \
93  if (android_cpufeatures_debug) \
94  { \
95  printf(__VA_ARGS__); \
96  fflush(stdout); \
97  } \
98  } while (0)
99 
100 #ifdef __i386__
101 static __inline__ void x86_cpuid(int func, int values[4])
102 {
103  int a, b, c, d;
104  /* We need to preserve ebx since we're compiling PIC code */
105  /* this means we can't use "=b" for the second output register */
106  __asm__ __volatile__("push %%ebx\n"
107  "cpuid\n"
108  "mov %%ebx, %1\n"
109  "pop %%ebx\n"
110  : "=a"(a), "=r"(b), "=c"(c), "=d"(d)
111  : "a"(func));
112  values[0] = a;
113  values[1] = b;
114  values[2] = c;
115  values[3] = d;
116 }
117 #elif defined(__x86_64__)
118 static __inline__ void x86_cpuid(int func, int values[4])
119 {
120  int64_t a, b, c, d;
121  /* We need to preserve ebx since we're compiling PIC code */
122  /* this means we can't use "=b" for the second output register */
123  __asm__ __volatile__("push %%rbx\n"
124  "cpuid\n"
125  "mov %%rbx, %1\n"
126  "pop %%rbx\n"
127  : "=a"(a), "=r"(b), "=c"(c), "=d"(d)
128  : "a"(func));
129  values[0] = a;
130  values[1] = b;
131  values[2] = c;
132  values[3] = d;
133 }
134 #endif
135 
136 /* Get the size of a file by reading it until the end. This is needed
137  * because files under /proc do not always return a valid size when
138  * using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed.
139  */
140 static int get_file_size(const char* pathname)
141 {
142  int fd, result = 0;
143  char buffer[256];
144  fd = open(pathname, O_RDONLY);
145 
146  if (fd < 0)
147  {
148  char ebuffer[256] = { 0 };
149  D("Can't open %s: %s\n", pathname, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
150  return -1;
151  }
152 
153  for (;;)
154  {
155  int ret = read(fd, buffer, sizeof buffer);
156 
157  if (ret < 0)
158  {
159  char ebuffer[256] = { 0 };
160  if (errno == EINTR)
161  continue;
162 
163  D("Error while reading %s: %s\n", pathname,
164  winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
165  break;
166  }
167 
168  if (ret == 0)
169  break;
170 
171  result += ret;
172  }
173 
174  close(fd);
175  return result;
176 }
177 
178 /* Read the content of /proc/cpuinfo into a user-provided buffer.
179  * Return the length of the data, or -1 on error. Does *not*
180  * zero-terminate the content. Will not read more
181  * than 'buffsize' bytes.
182  */
183 static int read_file(const char* pathname, char* buffer, size_t buffsize)
184 {
185  int fd, count;
186  fd = open(pathname, O_RDONLY);
187 
188  if (fd < 0)
189  {
190  char ebuffer[256] = { 0 };
191  D("Could not open %s: %s\n", pathname, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
192  return -1;
193  }
194 
195  count = 0;
196 
197  while (count < (int)buffsize)
198  {
199  int ret = read(fd, buffer + count, buffsize - count);
200 
201  if (ret < 0)
202  {
203  char ebuffer[256] = { 0 };
204  if (errno == EINTR)
205  continue;
206 
207  D("Error while reading from %s: %s\n", pathname,
208  winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
209 
210  if (count == 0)
211  count = -1;
212 
213  break;
214  }
215 
216  if (ret == 0)
217  break;
218 
219  count += ret;
220  }
221 
222  close(fd);
223  return count;
224 }
225 
226 #ifdef __arm__
227 /* Extract the content of a the first occurence of a given field in
228  * the content of /proc/cpuinfo and return it as a heap-allocated
229  * string that must be freed by the caller.
230  *
231  * Return NULL if not found
232  */
233 static char* extract_cpuinfo_field(const char* buffer, int buflen, const char* field)
234 {
235  int fieldlen = strlen(field);
236  const char* bufend = buffer + buflen;
237  char* result = NULL;
238  int len;
239  const char *p, *q;
240  /* Look for first field occurence, and ensures it starts the line. */
241  p = buffer;
242 
243  for (;;)
244  {
245  p = memmem(p, bufend - p, field, fieldlen);
246 
247  if (p == NULL)
248  goto EXIT;
249 
250  if (p == buffer || p[-1] == '\n')
251  break;
252 
253  p += fieldlen;
254  }
255 
256  /* Skip to the first column followed by a space */
257  p += fieldlen;
258  p = memchr(p, ':', bufend - p);
259 
260  if (p == NULL || p[1] != ' ')
261  goto EXIT;
262 
263  /* Find the end of the line */
264  p += 2;
265  q = memchr(p, '\n', bufend - p);
266 
267  if (q == NULL)
268  q = bufend;
269 
270  /* Copy the line into a heap-allocated buffer */
271  len = q - p;
272  result = malloc(len + 1);
273 
274  if (result == NULL)
275  goto EXIT;
276 
277  memcpy(result, p, len);
278  result[len] = '\0';
279 EXIT:
280  return result;
281 }
282 
283 /* Checks that a space-separated list of items contains one given 'item'.
284  * Returns 1 if found, 0 otherwise.
285  */
286 static int has_list_item(const char* list, const char* item)
287 {
288  const char* p = list;
289  int itemlen = strlen(item);
290 
291  if (list == NULL)
292  return 0;
293 
294  while (*p)
295  {
296  const char* q;
297 
298  /* skip spaces */
299  while (*p == ' ' || *p == '\t')
300  p++;
301 
302  /* find end of current list item */
303  q = p;
304 
305  while (*q && *q != ' ' && *q != '\t')
306  q++;
307 
308  if (itemlen == q - p && !memcmp(p, item, itemlen))
309  return 1;
310 
311  /* skip to next item */
312  p = q;
313  }
314 
315  return 0;
316 }
317 #endif /* __arm__ */
318 
319 /* Parse a number starting from 'input', but not going further
320  * than 'limit'. Return the value into '*result'.
321  *
322  * NOTE: Does not skip over leading spaces, or deal with sign characters.
323  * NOTE: Ignores overflows.
324  *
325  * The function returns NULL in case of error (bad format), or the new
326  * position after the decimal number in case of success (which will always
327  * be <= 'limit').
328  */
329 static const char* parse_number(const char* input, const char* limit, int base, int* result)
330 {
331  const char* p = input;
332  int val = 0;
333 
334  while (p < limit)
335  {
336  int d = (*p - '0');
337 
338  if ((unsigned)d >= 10U)
339  {
340  d = (*p - 'a');
341 
342  if ((unsigned)d >= 6U)
343  d = (*p - 'A');
344 
345  if ((unsigned)d >= 6U)
346  break;
347 
348  d += 10;
349  }
350 
351  if (d >= base)
352  break;
353 
354  val = val * base + d;
355  p++;
356  }
357 
358  if (p == input)
359  return NULL;
360 
361  *result = val;
362  return p;
363 }
364 
365 static const char* parse_decimal(const char* input, const char* limit, int* result)
366 {
367  return parse_number(input, limit, 10, result);
368 }
369 
370 #ifdef __arm__
371 static const char* parse_hexadecimal(const char* input, const char* limit, int* result)
372 {
373  return parse_number(input, limit, 16, result);
374 }
375 #endif /* __arm__ */
376 
377 /* This small data type is used to represent a CPU list / mask, as read
378  * from sysfs on Linux. See http://www.kernel.org/doc/Documentation/cputopology.txt
379  *
380  * For now, we don't expect more than 32 cores on mobile devices, so keep
381  * everything simple.
382  */
383 typedef struct
384 {
385  uint32_t mask;
386 } CpuList;
387 
388 static __inline__ void cpulist_init(CpuList* list)
389 {
390  list->mask = 0;
391 }
392 
393 static __inline__ void cpulist_and(CpuList* list1, CpuList* list2)
394 {
395  list1->mask &= list2->mask;
396 }
397 
398 static __inline__ void cpulist_set(CpuList* list, int index)
399 {
400  if ((unsigned)index < 32)
401  {
402  list->mask |= (uint32_t)(1U << index);
403  }
404 }
405 
406 static __inline__ int cpulist_count(CpuList* list)
407 {
408  return __builtin_popcount(list->mask);
409 }
410 
411 /* Parse a textual list of cpus and store the result inside a CpuList object.
412  * Input format is the following:
413  * - comma-separated list of items (no spaces)
414  * - each item is either a single decimal number (cpu index), or a range made
415  * of two numbers separated by a single dash (-). Ranges are inclusive.
416  *
417  * Examples: 0
418  * 2,4-127,128-143
419  * 0-1
420  */
421 static void cpulist_parse(CpuList* list, const char* line, int line_len)
422 {
423  const char* p = line;
424  const char* end = p + line_len;
425  const char* q;
426 
427  /* NOTE: the input line coming from sysfs typically contains a
428  * trailing newline, so take care of it in the code below
429  */
430  while (p < end && *p != '\n')
431  {
432  int start_value = 0;
433  int end_value = 0;
434  /* Find the end of current item, and put it into 'q' */
435  q = memchr(p, ',', end - p);
436 
437  if (q == NULL)
438  {
439  q = end;
440  }
441 
442  /* Get first value */
443  p = parse_decimal(p, q, &start_value);
444 
445  if (p == NULL)
446  goto BAD_FORMAT;
447 
448  end_value = start_value;
449 
450  /* If we're not at the end of the item, expect a dash and
451  * and integer; extract end value.
452  */
453  if (p < q && *p == '-')
454  {
455  p = parse_decimal(p + 1, q, &end_value);
456 
457  if (p == NULL)
458  goto BAD_FORMAT;
459  }
460 
461  /* Set bits CPU list bits */
462  for (int val = start_value; val <= end_value; val++)
463  {
464  cpulist_set(list, val);
465  }
466 
467  /* Jump to next item */
468  p = q;
469 
470  if (p < end)
471  p++;
472  }
473 
474 BAD_FORMAT:;
475 }
476 
477 /* Read a CPU list from one sysfs file */
478 static void cpulist_read_from(CpuList* list, const char* filename)
479 {
480  char file[64];
481  int filelen;
482  cpulist_init(list);
483  filelen = read_file(filename, file, sizeof file);
484 
485  if (filelen < 0)
486  {
487  char ebuffer[256] = { 0 };
488  D("Could not read %s: %s\n", filename, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
489  return;
490  }
491 
492  cpulist_parse(list, file, filelen);
493 }
494 #if defined(__aarch64__)
495 // see <uapi/asm/hwcap.h> kernel header
496 #define HWCAP_FP (1 << 0)
497 #define HWCAP_ASIMD (1 << 1)
498 #define HWCAP_AES (1 << 3)
499 #define HWCAP_PMULL (1 << 4)
500 #define HWCAP_SHA1 (1 << 5)
501 #define HWCAP_SHA2 (1 << 6)
502 #define HWCAP_CRC32 (1 << 7)
503 #endif
504 
505 #if defined(__arm__)
506 
507 // See <asm/hwcap.h> kernel header.
508 #define HWCAP_VFP (1 << 6)
509 #define HWCAP_IWMMXT (1 << 9)
510 #define HWCAP_NEON (1 << 12)
511 #define HWCAP_VFPv3 (1 << 13)
512 #define HWCAP_VFPv3D16 (1 << 14)
513 #define HWCAP_VFPv4 (1 << 16)
514 #define HWCAP_IDIVA (1 << 17)
515 #define HWCAP_IDIVT (1 << 18)
516 
517 // see <uapi/asm/hwcap.h> kernel header
518 #define HWCAP2_AES (1 << 0)
519 #define HWCAP2_PMULL (1 << 1)
520 #define HWCAP2_SHA1 (1 << 2)
521 #define HWCAP2_SHA2 (1 << 3)
522 #define HWCAP2_CRC32 (1 << 4)
523 
524 // This is the list of 32-bit ARMv7 optional features that are _always_
525 // supported by ARMv8 CPUs, as mandated by the ARM Architecture Reference
526 // Manual.
527 #define HWCAP_SET_FOR_ARMV8 \
528  (HWCAP_VFP | HWCAP_NEON | HWCAP_VFPv3 | HWCAP_VFPv4 | HWCAP_IDIVA | HWCAP_IDIVT)
529 #endif
530 
531 #if defined(__mips__)
532 // see <uapi/asm/hwcap.h> kernel header
533 #define HWCAP_MIPS_R6 (1 << 0)
534 #define HWCAP_MIPS_MSA (1 << 1)
535 #endif
536 
537 #if defined(__arm__) || defined(__aarch64__) || defined(__mips__)
538 
539 #define AT_HWCAP 16
540 #define AT_HWCAP2 26
541 
542 // Probe the system's C library for a 'getauxval' function and call it if
543 // it exits, or return 0 for failure. This function is available since API
544 // level 20.
545 //
546 // This code does *NOT* check for '__ANDROID_API__ >= 20' to support the
547 // edge case where some NDK developers use headers for a platform that is
548 // newer than the one really targetted by their application.
549 // This is typically done to use newer native APIs only when running on more
550 // recent Android versions, and requires careful symbol management.
551 //
552 // Note that getauxval() can't really be re-implemented here, because
553 // its implementation does not parse /proc/self/auxv. Instead it depends
554 // on values that are passed by the kernel at process-init time to the
555 // C runtime initialization layer.
556 static uint32_t get_elf_hwcap_from_getauxval(int hwcap_type)
557 {
558  typedef unsigned long getauxval_func_t(unsigned long);
559  dlerror();
560  void* libc_handle = dlopen("libc.so", RTLD_NOW);
561 
562  if (!libc_handle)
563  {
564  D("Could not dlopen() C library: %s\n", dlerror());
565  return 0;
566  }
567 
568  uint32_t ret = 0;
569  getauxval_func_t* func = (getauxval_func_t*)dlsym(libc_handle, "getauxval");
570 
571  if (!func)
572  {
573  D("Could not find getauxval() in C library\n");
574  }
575  else
576  {
577  // Note: getauxval() returns 0 on failure. Doesn't touch errno.
578  ret = (uint32_t)(*func)(hwcap_type);
579  }
580 
581  dlclose(libc_handle);
582  return ret;
583 }
584 #endif
585 
586 #if defined(__arm__)
587 // Parse /proc/self/auxv to extract the ELF HW capabilities bitmap for the
588 // current CPU. Note that this file is not accessible from regular
589 // application processes on some Android platform releases.
590 // On success, return new ELF hwcaps, or 0 on failure.
591 static uint32_t get_elf_hwcap_from_proc_self_auxv(void)
592 {
593  const char filepath[] = "/proc/self/auxv";
594  int fd = TEMP_FAILURE_RETRY(open(filepath, O_RDONLY));
595 
596  if (fd < 0)
597  {
598  char ebuffer[256] = { 0 };
599  D("Could not open %s: %s\n", filepath, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
600  return 0;
601  }
602 
603  struct
604  {
605  uint32_t tag;
606  uint32_t value;
607  } entry;
608 
609  uint32_t result = 0;
610 
611  for (;;)
612  {
613  int ret = TEMP_FAILURE_RETRY(read(fd, (char*)&entry, sizeof entry));
614 
615  if (ret < 0)
616  {
617  char ebuffer[256] = { 0 };
618  D("Error while reading %s: %s\n", filepath,
619  winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
620  break;
621  }
622 
623  // Detect end of list.
624  if (ret == 0 || (entry.tag == 0 && entry.value == 0))
625  break;
626 
627  if (entry.tag == AT_HWCAP)
628  {
629  result = entry.value;
630  break;
631  }
632  }
633 
634  close(fd);
635  return result;
636 }
637 
638 /* Compute the ELF HWCAP flags from the content of /proc/cpuinfo.
639  * This works by parsing the 'Features' line, which lists which optional
640  * features the device's CPU supports, on top of its reference
641  * architecture.
642  */
643 static uint32_t get_elf_hwcap_from_proc_cpuinfo(const char* cpuinfo, int cpuinfo_len)
644 {
645  uint32_t hwcaps = 0;
646  long architecture = 0;
647  char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
648 
649  if (cpuArch)
650  {
651  architecture = strtol(cpuArch, NULL, 10);
652  free(cpuArch);
653 
654  if (architecture >= 8L)
655  {
656  // This is a 32-bit ARM binary running on a 64-bit ARM64 kernel.
657  // The 'Features' line only lists the optional features that the
658  // device's CPU supports, compared to its reference architecture
659  // which are of no use for this process.
660  D("Faking 32-bit ARM HWCaps on ARMv%ld CPU\n", architecture);
661  return HWCAP_SET_FOR_ARMV8;
662  }
663  }
664 
665  char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features");
666 
667  if (cpuFeatures != NULL)
668  {
669  D("Found cpuFeatures = '%s'\n", cpuFeatures);
670 
671  if (has_list_item(cpuFeatures, "vfp"))
672  hwcaps |= HWCAP_VFP;
673 
674  if (has_list_item(cpuFeatures, "vfpv3"))
675  hwcaps |= HWCAP_VFPv3;
676 
677  if (has_list_item(cpuFeatures, "vfpv3d16"))
678  hwcaps |= HWCAP_VFPv3D16;
679 
680  if (has_list_item(cpuFeatures, "vfpv4"))
681  hwcaps |= HWCAP_VFPv4;
682 
683  if (has_list_item(cpuFeatures, "neon"))
684  hwcaps |= HWCAP_NEON;
685 
686  if (has_list_item(cpuFeatures, "idiva"))
687  hwcaps |= HWCAP_IDIVA;
688 
689  if (has_list_item(cpuFeatures, "idivt"))
690  hwcaps |= HWCAP_IDIVT;
691 
692  if (has_list_item(cpuFeatures, "idiv"))
693  hwcaps |= HWCAP_IDIVA | HWCAP_IDIVT;
694 
695  if (has_list_item(cpuFeatures, "iwmmxt"))
696  hwcaps |= HWCAP_IWMMXT;
697 
698  free(cpuFeatures);
699  }
700 
701  return hwcaps;
702 }
703 #endif /* __arm__ */
704 
705 /* Return the number of cpus present on a given device.
706  *
707  * To handle all weird kernel configurations, we need to compute the
708  * intersection of the 'present' and 'possible' CPU lists and count
709  * the result.
710  */
711 static int get_cpu_count(void)
712 {
713  CpuList cpus_present[1];
714  CpuList cpus_possible[1];
715  cpulist_read_from(cpus_present, "/sys/devices/system/cpu/present");
716  cpulist_read_from(cpus_possible, "/sys/devices/system/cpu/possible");
717  /* Compute the intersection of both sets to get the actual number of
718  * CPU cores that can be used on this device by the kernel.
719  */
720  cpulist_and(cpus_present, cpus_possible);
721  return cpulist_count(cpus_present);
722 }
723 
724 static void android_cpuInitFamily(void)
725 {
726 #if defined(__arm__)
727  g_cpuFamily = ANDROID_CPU_FAMILY_ARM;
728 #elif defined(__i386__)
729  g_cpuFamily = ANDROID_CPU_FAMILY_X86;
730 #elif defined(__mips64)
731  /* Needs to be before __mips__ since the compiler defines both */
732  g_cpuFamily = ANDROID_CPU_FAMILY_MIPS64;
733 #elif defined(__mips__)
734  g_cpuFamily = ANDROID_CPU_FAMILY_MIPS;
735 #elif defined(__aarch64__)
736  g_cpuFamily = ANDROID_CPU_FAMILY_ARM64;
737 #elif defined(__x86_64__)
738  g_cpuFamily = ANDROID_CPU_FAMILY_X86_64;
739 #else
740  g_cpuFamily = ANDROID_CPU_FAMILY_UNKNOWN;
741 #endif
742 }
743 
744 static void android_cpuInit(void)
745 {
746  char* cpuinfo = NULL;
747  int cpuinfo_len;
748  android_cpuInitFamily();
749  g_cpuFeatures = 0;
750  g_cpuCount = 1;
751  g_inited = 1;
752  cpuinfo_len = get_file_size("/proc/cpuinfo");
753 
754  if (cpuinfo_len < 0)
755  {
756  D("cpuinfo_len cannot be computed!");
757  return;
758  }
759 
760  cpuinfo = malloc(cpuinfo_len);
761 
762  if (cpuinfo == NULL)
763  {
764  D("cpuinfo buffer could not be allocated");
765  return;
766  }
767 
768  cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, cpuinfo_len);
769  D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len, cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo);
770 
771  if (cpuinfo_len < 0) /* should not happen */
772  {
773  free(cpuinfo);
774  return;
775  }
776 
777  /* Count the CPU cores, the value may be 0 for single-core CPUs */
778  g_cpuCount = get_cpu_count();
779 
780  if (g_cpuCount == 0)
781  {
782  g_cpuCount = 1;
783  }
784 
785  D("found cpuCount = %d\n", g_cpuCount);
786 #ifdef __arm__
787  {
788  /* Extract architecture from the "CPU Architecture" field.
789  * The list is well-known, unlike the the output of
790  * the 'Processor' field which can vary greatly.
791  *
792  * See the definition of the 'proc_arch' array in
793  * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
794  * same file.
795  */
796  char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
797 
798  if (cpuArch != NULL)
799  {
800  char* end;
801  long archNumber;
802  int hasARMv7 = 0;
803  D("found cpuArch = '%s'\n", cpuArch);
804  /* read the initial decimal number, ignore the rest */
805  archNumber = strtol(cpuArch, &end, 10);
806 
807  /* Note that ARMv8 is upwards compatible with ARMv7. */
808  if (end > cpuArch && archNumber >= 7)
809  {
810  hasARMv7 = 1;
811  }
812 
813  /* Unfortunately, it seems that certain ARMv6-based CPUs
814  * report an incorrect architecture number of 7!
815  *
816  * See http://code.google.com/p/android/issues/detail?id=10812
817  *
818  * We try to correct this by looking at the 'elf_format'
819  * field reported by the 'Processor' field, which is of the
820  * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
821  * an ARMv6-one.
822  */
823  if (hasARMv7)
824  {
825  char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Processor");
826 
827  if (cpuProc != NULL)
828  {
829  D("found cpuProc = '%s'\n", cpuProc);
830 
831  if (has_list_item(cpuProc, "(v6l)"))
832  {
833  D("CPU processor and architecture mismatch!!\n");
834  hasARMv7 = 0;
835  }
836 
837  free(cpuProc);
838  }
839  }
840 
841  if (hasARMv7)
842  {
843  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_ARMv7;
844  }
845 
846  /* The LDREX / STREX instructions are available from ARMv6 */
847  if (archNumber >= 6)
848  {
849  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_LDREX_STREX;
850  }
851 
852  free(cpuArch);
853  }
854 
855  /* Extract the list of CPU features from ELF hwcaps */
856  uint32_t hwcaps = 0;
857  hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
858 
859  if (!hwcaps)
860  {
861  D("Parsing /proc/self/auxv to extract ELF hwcaps!\n");
862  hwcaps = get_elf_hwcap_from_proc_self_auxv();
863  }
864 
865  if (!hwcaps)
866  {
867  // Parsing /proc/self/auxv will fail from regular application
868  // processes on some Android platform versions, when this happens
869  // parse proc/cpuinfo instead.
870  D("Parsing /proc/cpuinfo to extract ELF hwcaps!\n");
871  hwcaps = get_elf_hwcap_from_proc_cpuinfo(cpuinfo, cpuinfo_len);
872  }
873 
874  if (hwcaps != 0)
875  {
876  int has_vfp = (hwcaps & HWCAP_VFP);
877  int has_vfpv3 = (hwcaps & HWCAP_VFPv3);
878  int has_vfpv3d16 = (hwcaps & HWCAP_VFPv3D16);
879  int has_vfpv4 = (hwcaps & HWCAP_VFPv4);
880  int has_neon = (hwcaps & HWCAP_NEON);
881  int has_idiva = (hwcaps & HWCAP_IDIVA);
882  int has_idivt = (hwcaps & HWCAP_IDIVT);
883  int has_iwmmxt = (hwcaps & HWCAP_IWMMXT);
884 
885  // The kernel does a poor job at ensuring consistency when
886  // describing CPU features. So lots of guessing is needed.
887 
888  // 'vfpv4' implies VFPv3|VFP_FMA|FP16
889  if (has_vfpv4)
890  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 | ANDROID_CPU_ARM_FEATURE_VFP_FP16 |
891  ANDROID_CPU_ARM_FEATURE_VFP_FMA;
892 
893  // 'vfpv3' or 'vfpv3d16' imply VFPv3. Note that unlike GCC,
894  // a value of 'vfpv3' doesn't necessarily mean that the D32
895  // feature is present, so be conservative. All CPUs in the
896  // field that support D32 also support NEON, so this should
897  // not be a problem in practice.
898  if (has_vfpv3 || has_vfpv3d16)
899  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
900 
901  // 'vfp' is super ambiguous. Depending on the kernel, it can
902  // either mean VFPv2 or VFPv3. Make it depend on ARMv7.
903  if (has_vfp)
904  {
905  if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_ARMv7)
906  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
907  else
908  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2;
909  }
910 
911  // Neon implies VFPv3|D32, and if vfpv4 is detected, NEON_FMA
912  if (has_neon)
913  {
914  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 | ANDROID_CPU_ARM_FEATURE_NEON |
915  ANDROID_CPU_ARM_FEATURE_VFP_D32;
916 
917  if (has_vfpv4)
918  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_NEON_FMA;
919  }
920 
921  // VFPv3 implies VFPv2 and ARMv7
922  if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_VFPv3)
923  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2 | ANDROID_CPU_ARM_FEATURE_ARMv7;
924 
925  if (has_idiva)
926  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM;
927 
928  if (has_idivt)
929  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2;
930 
931  if (has_iwmmxt)
932  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_iWMMXt;
933  }
934 
935  /* Extract the list of CPU features from ELF hwcaps2 */
936  uint32_t hwcaps2 = 0;
937  hwcaps2 = get_elf_hwcap_from_getauxval(AT_HWCAP2);
938 
939  if (hwcaps2 != 0)
940  {
941  int has_aes = (hwcaps2 & HWCAP2_AES);
942  int has_pmull = (hwcaps2 & HWCAP2_PMULL);
943  int has_sha1 = (hwcaps2 & HWCAP2_SHA1);
944  int has_sha2 = (hwcaps2 & HWCAP2_SHA2);
945  int has_crc32 = (hwcaps2 & HWCAP2_CRC32);
946 
947  if (has_aes)
948  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_AES;
949 
950  if (has_pmull)
951  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_PMULL;
952 
953  if (has_sha1)
954  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_SHA1;
955 
956  if (has_sha2)
957  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_SHA2;
958 
959  if (has_crc32)
960  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_CRC32;
961  }
962 
963  /* Extract the cpuid value from various fields */
964  // The CPUID value is broken up in several entries in /proc/cpuinfo.
965  // This table is used to rebuild it from the entries.
966  static const struct CpuIdEntry
967  {
968  const char* field;
969  char format;
970  char bit_lshift;
971  char bit_length;
972  } cpu_id_entries[] = {
973  { "CPU implementer", 'x', 24, 8 },
974  { "CPU variant", 'x', 20, 4 },
975  { "CPU part", 'x', 4, 12 },
976  { "CPU revision", 'd', 0, 4 },
977  };
978  D("Parsing /proc/cpuinfo to recover CPUID\n");
979 
980  for (size_t i = 0; i < sizeof(cpu_id_entries) / sizeof(cpu_id_entries[0]); ++i)
981  {
982  const struct CpuIdEntry* entry = &cpu_id_entries[i];
983  char* value = extract_cpuinfo_field(cpuinfo, cpuinfo_len, entry->field);
984 
985  if (value == NULL)
986  continue;
987 
988  D("field=%s value='%s'\n", entry->field, value);
989  char* value_end = value + strlen(value);
990  int val = 0;
991  const char* start = value;
992  const char* p;
993 
994  if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X'))
995  {
996  start += 2;
997  p = parse_hexadecimal(start, value_end, &val);
998  }
999  else if (entry->format == 'x')
1000  p = parse_hexadecimal(value, value_end, &val);
1001  else
1002  p = parse_decimal(value, value_end, &val);
1003 
1004  if (p > (const char*)start)
1005  {
1006  val &= ((1 << entry->bit_length) - 1);
1007  val <<= entry->bit_lshift;
1008  g_cpuIdArm |= (uint32_t)val;
1009  }
1010 
1011  free(value);
1012  }
1013 
1014  // Handle kernel configuration bugs that prevent the correct
1015  // reporting of CPU features.
1016  static const struct CpuFix
1017  {
1018  uint32_t cpuid;
1019  uint64_t or_flags;
1020  } cpu_fixes[] = {
1021  /* The Nexus 4 (Qualcomm Krait) kernel configuration
1022  * forgets to report IDIV support. */
1023  { 0x510006f2, ANDROID_CPU_ARM_FEATURE_IDIV_ARM | ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 },
1024  { 0x510006f3, ANDROID_CPU_ARM_FEATURE_IDIV_ARM | ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 },
1025  };
1026 
1027  for (size_t n = 0; n < sizeof(cpu_fixes) / sizeof(cpu_fixes[0]); ++n)
1028  {
1029  const struct CpuFix* entry = &cpu_fixes[n];
1030 
1031  if (g_cpuIdArm == entry->cpuid)
1032  g_cpuFeatures |= entry->or_flags;
1033  }
1034 
1035  // Special case: The emulator-specific Android 4.2 kernel fails
1036  // to report support for the 32-bit ARM IDIV instruction.
1037  // Technically, this is a feature of the virtual CPU implemented
1038  // by the emulator. Note that it could also support Thumb IDIV
1039  // in the future, and this will have to be slightly updated.
1040  char* hardware = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Hardware");
1041 
1042  if (hardware)
1043  {
1044  if (!strcmp(hardware, "Goldfish") && g_cpuIdArm == 0x4100c080 &&
1045  (g_cpuFamily & ANDROID_CPU_ARM_FEATURE_ARMv7) != 0)
1046  {
1047  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM;
1048  }
1049 
1050  free(hardware);
1051  }
1052  }
1053 #endif /* __arm__ */
1054 #ifdef __aarch64__
1055  {
1056  /* Extract the list of CPU features from ELF hwcaps */
1057  uint32_t hwcaps = 0;
1058  hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
1059 
1060  if (hwcaps != 0)
1061  {
1062  int has_fp = (hwcaps & HWCAP_FP);
1063  int has_asimd = (hwcaps & HWCAP_ASIMD);
1064  int has_aes = (hwcaps & HWCAP_AES);
1065  int has_pmull = (hwcaps & HWCAP_PMULL);
1066  int has_sha1 = (hwcaps & HWCAP_SHA1);
1067  int has_sha2 = (hwcaps & HWCAP_SHA2);
1068  int has_crc32 = (hwcaps & HWCAP_CRC32);
1069 
1070  if (has_fp == 0)
1071  {
1072  D("ERROR: Floating-point unit missing, but is required by Android on AArch64 "
1073  "CPUs\n");
1074  }
1075 
1076  if (has_asimd == 0)
1077  {
1078  D("ERROR: ASIMD unit missing, but is required by Android on AArch64 CPUs\n");
1079  }
1080 
1081  if (has_fp)
1082  g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_FP;
1083 
1084  if (has_asimd)
1085  g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_ASIMD;
1086 
1087  if (has_aes)
1088  g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_AES;
1089 
1090  if (has_pmull)
1091  g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_PMULL;
1092 
1093  if (has_sha1)
1094  g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_SHA1;
1095 
1096  if (has_sha2)
1097  g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_SHA2;
1098 
1099  if (has_crc32)
1100  g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_CRC32;
1101  }
1102  }
1103 #endif /* __aarch64__ */
1104 #if defined(__i386__) || defined(__x86_64__)
1105  int regs[4];
1106  /* According to http://en.wikipedia.org/wiki/CPUID */
1107 #define VENDOR_INTEL_b 0x756e6547
1108 #define VENDOR_INTEL_c 0x6c65746e
1109 #define VENDOR_INTEL_d 0x49656e69
1110  x86_cpuid(0, regs);
1111  int vendorIsIntel =
1112  (regs[1] == VENDOR_INTEL_b && regs[2] == VENDOR_INTEL_c && regs[3] == VENDOR_INTEL_d);
1113  x86_cpuid(1, regs);
1114 
1115  if ((regs[2] & (1 << 9)) != 0)
1116  {
1117  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSSE3;
1118  }
1119 
1120  if ((regs[2] & (1 << 23)) != 0)
1121  {
1122  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_POPCNT;
1123  }
1124 
1125  if ((regs[2] & (1 << 19)) != 0)
1126  {
1127  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSE4_1;
1128  }
1129 
1130  if ((regs[2] & (1 << 20)) != 0)
1131  {
1132  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSE4_2;
1133  }
1134 
1135  if (vendorIsIntel && (regs[2] & (1 << 22)) != 0)
1136  {
1137  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_MOVBE;
1138  }
1139 
1140  if ((regs[2] & (1 << 25)) != 0)
1141  {
1142  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_AES_NI;
1143  }
1144 
1145  if ((regs[2] & (1 << 28)) != 0)
1146  {
1147  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_AVX;
1148  }
1149 
1150  if ((regs[2] & (1 << 30)) != 0)
1151  {
1152  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_RDRAND;
1153  }
1154 
1155  x86_cpuid(7, regs);
1156 
1157  if ((regs[1] & (1 << 5)) != 0)
1158  {
1159  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_AVX2;
1160  }
1161 
1162  if ((regs[1] & (1 << 29)) != 0)
1163  {
1164  g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SHA_NI;
1165  }
1166 
1167 #endif
1168 #if defined(__mips__)
1169  {
1170  /* MIPS and MIPS64 */
1171  /* Extract the list of CPU features from ELF hwcaps */
1172  uint32_t hwcaps = 0;
1173  hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
1174 
1175  if (hwcaps != 0)
1176  {
1177  int has_r6 = (hwcaps & HWCAP_MIPS_R6);
1178  int has_msa = (hwcaps & HWCAP_MIPS_MSA);
1179 
1180  if (has_r6)
1181  g_cpuFeatures |= ANDROID_CPU_MIPS_FEATURE_R6;
1182 
1183  if (has_msa)
1184  g_cpuFeatures |= ANDROID_CPU_MIPS_FEATURE_MSA;
1185  }
1186  }
1187 #endif /* __mips__ */
1188  free(cpuinfo);
1189 }
1190 
1191 AndroidCpuFamily android_getCpuFamily(void)
1192 {
1193  pthread_once(&g_once, android_cpuInit);
1194  return g_cpuFamily;
1195 }
1196 
1197 uint64_t android_getCpuFeatures(void)
1198 {
1199  pthread_once(&g_once, android_cpuInit);
1200  return g_cpuFeatures;
1201 }
1202 
1203 int android_getCpuCount(void)
1204 {
1205  pthread_once(&g_once, android_cpuInit);
1206  return g_cpuCount;
1207 }
1208 
1209 static void android_cpuInitDummy(void)
1210 {
1211  g_inited = 1;
1212 }
1213 
1214 int android_setCpu(int cpu_count, uint64_t cpu_features)
1215 {
1216  /* Fail if the library was already initialized. */
1217  if (g_inited)
1218  return 0;
1219 
1220  android_cpuInitFamily();
1221  g_cpuCount = (cpu_count <= 0 ? 1 : cpu_count);
1222  g_cpuFeatures = cpu_features;
1223  pthread_once(&g_once, android_cpuInitDummy);
1224  return 1;
1225 }
1226 
1227 #ifdef __arm__
1228 uint32_t android_getCpuIdArm(void)
1229 {
1230  pthread_once(&g_once, android_cpuInit);
1231  return g_cpuIdArm;
1232 }
1233 
1234 int android_setCpuArm(int cpu_count, uint64_t cpu_features, uint32_t cpu_id)
1235 {
1236  if (!android_setCpu(cpu_count, cpu_features))
1237  return 0;
1238 
1239  g_cpuIdArm = cpu_id;
1240  return 1;
1241 }
1242 #endif /* __arm__ */
1243 
1244 /*
1245  * Technical note: Making sense of ARM's FPU architecture versions.
1246  *
1247  * FPA was ARM's first attempt at an FPU architecture. There is no Android
1248  * device that actually uses it since this technology was already obsolete
1249  * when the project started. If you see references to FPA instructions
1250  * somewhere, you can be sure that this doesn't apply to Android at all.
1251  *
1252  * FPA was followed by "VFP", soon renamed "VFPv1" due to the emergence of
1253  * new versions / additions to it. ARM considers this obsolete right now,
1254  * and no known Android device implements it either.
1255  *
1256  * VFPv2 added a few instructions to VFPv1, and is an *optional* extension
1257  * supported by some ARMv5TE, ARMv6 and ARMv6T2 CPUs. Note that a device
1258  * supporting the 'armeabi' ABI doesn't necessarily support these.
1259  *
1260  * VFPv3-D16 adds a few instructions on top of VFPv2 and is typically used
1261  * on ARMv7-A CPUs which implement a FPU. Note that it is also mandated
1262  * by the Android 'armeabi-v7a' ABI. The -D16 suffix in its name means
1263  * that it provides 16 double-precision FPU registers (d0-d15) and 32
1264  * single-precision ones (s0-s31) which happen to be mapped to the same
1265  * register banks.
1266  *
1267  * VFPv3-D32 is the name of an extension to VFPv3-D16 that provides 16
1268  * additional double precision registers (d16-d31). Note that there are
1269  * still only 32 single precision registers.
1270  *
1271  * VFPv3xD is a *subset* of VFPv3-D16 that only provides single-precision
1272  * registers. It is only used on ARMv7-M (i.e. on micro-controllers) which
1273  * are not supported by Android. Note that it is not compatible with VFPv2.
1274  *
1275  * NOTE: The term 'VFPv3' usually designate either VFPv3-D16 or VFPv3-D32
1276  * depending on context. For example GCC uses it for VFPv3-D32, but
1277  * the Linux kernel code uses it for VFPv3-D16 (especially in
1278  * /proc/cpuinfo). Always try to use the full designation when
1279  * possible.
1280  *
1281  * NEON, a.k.a. "ARM Advanced SIMD" is an extension that provides
1282  * instructions to perform parallel computations on vectors of 8, 16,
1283  * 32, 64 and 128 bit quantities. NEON requires VFPv32-D32 since all
1284  * NEON registers are also mapped to the same register banks.
1285  *
1286  * VFPv4-D16, adds a few instructions on top of VFPv3-D16 in order to
1287  * perform fused multiply-accumulate on VFP registers, as well as
1288  * half-precision (16-bit) conversion operations.
1289  *
1290  * VFPv4-D32 is VFPv4-D16 with 32, instead of 16, FPU double precision
1291  * registers.
1292  *
1293  * VPFv4-NEON is VFPv4-D32 with NEON instructions. It also adds fused
1294  * multiply-accumulate instructions that work on the NEON registers.
1295  *
1296  * NOTE: Similarly, "VFPv4" might either reference VFPv4-D16 or VFPv4-D32
1297  * depending on context.
1298  *
1299  * The following information was determined by scanning the binutils-2.22
1300  * sources:
1301  *
1302  * Basic VFP instruction subsets:
1303  *
1304  * #define FPU_VFP_EXT_V1xD 0x08000000 // Base VFP instruction set.
1305  * #define FPU_VFP_EXT_V1 0x04000000 // Double-precision insns.
1306  * #define FPU_VFP_EXT_V2 0x02000000 // ARM10E VFPr1.
1307  * #define FPU_VFP_EXT_V3xD 0x01000000 // VFPv3 single-precision.
1308  * #define FPU_VFP_EXT_V3 0x00800000 // VFPv3 double-precision.
1309  * #define FPU_NEON_EXT_V1 0x00400000 // Neon (SIMD) insns.
1310  * #define FPU_VFP_EXT_D32 0x00200000 // Registers D16-D31.
1311  * #define FPU_VFP_EXT_FP16 0x00100000 // Half-precision extensions.
1312  * #define FPU_NEON_EXT_FMA 0x00080000 // Neon fused multiply-add
1313  * #define FPU_VFP_EXT_FMA 0x00040000 // VFP fused multiply-add
1314  *
1315  * FPU types (excluding NEON)
1316  *
1317  * FPU_VFP_V1xD (EXT_V1xD)
1318  * |
1319  * +--------------------------+
1320  * | |
1321  * FPU_VFP_V1 (+EXT_V1) FPU_VFP_V3xD (+EXT_V2+EXT_V3xD)
1322  * | |
1323  * | |
1324  * FPU_VFP_V2 (+EXT_V2) FPU_VFP_V4_SP_D16 (+EXT_FP16+EXT_FMA)
1325  * |
1326  * FPU_VFP_V3D16 (+EXT_Vx3D+EXT_V3)
1327  * |
1328  * +--------------------------+
1329  * | |
1330  * FPU_VFP_V3 (+EXT_D32) FPU_VFP_V4D16 (+EXT_FP16+EXT_FMA)
1331  * | |
1332  * | FPU_VFP_V4 (+EXT_D32)
1333  * |
1334  * FPU_VFP_HARD (+EXT_FMA+NEON_EXT_FMA)
1335  *
1336  * VFP architectures:
1337  *
1338  * ARCH_VFP_V1xD (EXT_V1xD)
1339  * |
1340  * +------------------+
1341  * | |
1342  * | ARCH_VFP_V3xD (+EXT_V2+EXT_V3xD)
1343  * | |
1344  * | ARCH_VFP_V3xD_FP16 (+EXT_FP16)
1345  * | |
1346  * | ARCH_VFP_V4_SP_D16 (+EXT_FMA)
1347  * |
1348  * ARCH_VFP_V1 (+EXT_V1)
1349  * |
1350  * ARCH_VFP_V2 (+EXT_V2)
1351  * |
1352  * ARCH_VFP_V3D16 (+EXT_V3xD+EXT_V3)
1353  * |
1354  * +-------------------+
1355  * | |
1356  * | ARCH_VFP_V3D16_FP16 (+EXT_FP16)
1357  * |
1358  * +-------------------+
1359  * | |
1360  * | ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
1361  * | |
1362  * | ARCH_VFP_V4 (+EXT_D32)
1363  * | |
1364  * | ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
1365  * |
1366  * ARCH_VFP_V3 (+EXT_D32)
1367  * |
1368  * +-------------------+
1369  * | |
1370  * | ARCH_VFP_V3_FP16 (+EXT_FP16)
1371  * |
1372  * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
1373  * |
1374  * ARCH_NEON_FP16 (+EXT_FP16)
1375  *
1376  * -fpu=<name> values and their correspondance with FPU architectures above:
1377  *
1378  * {"vfp", FPU_ARCH_VFP_V2},
1379  * {"vfp9", FPU_ARCH_VFP_V2},
1380  * {"vfp3", FPU_ARCH_VFP_V3}, // For backwards compatbility.
1381  * {"vfp10", FPU_ARCH_VFP_V2},
1382  * {"vfp10-r0", FPU_ARCH_VFP_V1},
1383  * {"vfpxd", FPU_ARCH_VFP_V1xD},
1384  * {"vfpv2", FPU_ARCH_VFP_V2},
1385  * {"vfpv3", FPU_ARCH_VFP_V3},
1386  * {"vfpv3-fp16", FPU_ARCH_VFP_V3_FP16},
1387  * {"vfpv3-d16", FPU_ARCH_VFP_V3D16},
1388  * {"vfpv3-d16-fp16", FPU_ARCH_VFP_V3D16_FP16},
1389  * {"vfpv3xd", FPU_ARCH_VFP_V3xD},
1390  * {"vfpv3xd-fp16", FPU_ARCH_VFP_V3xD_FP16},
1391  * {"neon", FPU_ARCH_VFP_V3_PLUS_NEON_V1},
1392  * {"neon-fp16", FPU_ARCH_NEON_FP16},
1393  * {"vfpv4", FPU_ARCH_VFP_V4},
1394  * {"vfpv4-d16", FPU_ARCH_VFP_V4D16},
1395  * {"fpv4-sp-d16", FPU_ARCH_VFP_V4_SP_D16},
1396  * {"neon-vfpv4", FPU_ARCH_NEON_VFP_V4},
1397  *
1398  *
1399  * Simplified diagram that only includes FPUs supported by Android:
1400  * Only ARCH_VFP_V3D16 is actually mandated by the armeabi-v7a ABI,
1401  * all others are optional and must be probed at runtime.
1402  *
1403  * ARCH_VFP_V3D16 (EXT_V1xD+EXT_V1+EXT_V2+EXT_V3xD+EXT_V3)
1404  * |
1405  * +-------------------+
1406  * | |
1407  * | ARCH_VFP_V3D16_FP16 (+EXT_FP16)
1408  * |
1409  * +-------------------+
1410  * | |
1411  * | ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
1412  * | |
1413  * | ARCH_VFP_V4 (+EXT_D32)
1414  * | |
1415  * | ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
1416  * |
1417  * ARCH_VFP_V3 (+EXT_D32)
1418  * |
1419  * +-------------------+
1420  * | |
1421  * | ARCH_VFP_V3_FP16 (+EXT_FP16)
1422  * |
1423  * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
1424  * |
1425  * ARCH_NEON_FP16 (+EXT_FP16)
1426  *
1427  */