Home | History | Annotate | Download | only in cpufeatures
      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 r8d: Add android_setCpu().
     32  *
     33  * NDK r8c: Add new ARM CPU features: VFPv2, VFP_D32, VFP_FP16,
     34  *          VFP_FMA, NEON_FMA, IDIV_ARM, IDIV_THUMB2 and iWMMXt.
     35  *
     36  *          Rewrite the code to parse /proc/self/auxv instead of
     37  *          the "Features" field in /proc/cpuinfo.
     38  *
     39  *          Dynamically allocate the buffer that hold the content
     40  *          of /proc/cpuinfo to deal with newer hardware.
     41  *
     42  * NDK r7c: Fix CPU count computation. The old method only reported the
     43  *           number of _active_ CPUs when the library was initialized,
     44  *           which could be less than the real total.
     45  *
     46  * NDK r5: Handle buggy kernels which report a CPU Architecture number of 7
     47  *         for an ARMv6 CPU (see below).
     48  *
     49  *         Handle kernels that only report 'neon', and not 'vfpv3'
     50  *         (VFPv3 is mandated by the ARM architecture is Neon is implemented)
     51  *
     52  *         Handle kernels that only report 'vfpv3d16', and not 'vfpv3'
     53  *
     54  *         Fix x86 compilation. Report ANDROID_CPU_FAMILY_X86 in
     55  *         android_getCpuFamily().
     56  *
     57  * NDK r4: Initial release
     58  */
     59 #include <sys/system_properties.h>
     60 #ifdef __arm__
     61 #include <machine/cpu-features.h>
     62 #endif
     63 #include <pthread.h>
     64 #include "cpu-features.h"
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <fcntl.h>
     68 #include <errno.h>
     69 
     70 static  pthread_once_t     g_once;
     71 static  int                g_inited;
     72 static  AndroidCpuFamily   g_cpuFamily;
     73 static  uint64_t           g_cpuFeatures;
     74 static  int                g_cpuCount;
     75 
     76 static const int  android_cpufeatures_debug = 0;
     77 
     78 #ifdef __arm__
     79 #  define DEFAULT_CPU_FAMILY  ANDROID_CPU_FAMILY_ARM
     80 #elif defined __i386__
     81 #  define DEFAULT_CPU_FAMILY  ANDROID_CPU_FAMILY_X86
     82 #else
     83 #  define DEFAULT_CPU_FAMILY  ANDROID_CPU_FAMILY_UNKNOWN
     84 #endif
     85 
     86 #define  D(...) \
     87     do { \
     88         if (android_cpufeatures_debug) { \
     89             printf(__VA_ARGS__); fflush(stdout); \
     90         } \
     91     } while (0)
     92 
     93 #ifdef __i386__
     94 static __inline__ void x86_cpuid(int func, int values[4])
     95 {
     96     int a, b, c, d;
     97     /* We need to preserve ebx since we're compiling PIC code */
     98     /* this means we can't use "=b" for the second output register */
     99     __asm__ __volatile__ ( \
    100       "push %%ebx\n"
    101       "cpuid\n" \
    102       "mov %%ebx, %1\n"
    103       "pop %%ebx\n"
    104       : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
    105       : "a" (func) \
    106     );
    107     values[0] = a;
    108     values[1] = b;
    109     values[2] = c;
    110     values[3] = d;
    111 }
    112 #endif
    113 
    114 /* Get the size of a file by reading it until the end. This is needed
    115  * because files under /proc do not always return a valid size when
    116  * using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed.
    117  */
    118 static int
    119 get_file_size(const char* pathname)
    120 {
    121     int fd, ret, result = 0;
    122     char buffer[256];
    123 
    124     fd = open(pathname, O_RDONLY);
    125     if (fd < 0) {
    126         D("Can't open %s: %s\n", pathname, strerror(errno));
    127         return -1;
    128     }
    129 
    130     for (;;) {
    131         int ret = read(fd, buffer, sizeof buffer);
    132         if (ret < 0) {
    133             if (errno == EINTR)
    134                 continue;
    135             D("Error while reading %s: %s\n", pathname, strerror(errno));
    136             break;
    137         }
    138         if (ret == 0)
    139             break;
    140 
    141         result += ret;
    142     }
    143     close(fd);
    144     return result;
    145 }
    146 
    147 /* Read the content of /proc/cpuinfo into a user-provided buffer.
    148  * Return the length of the data, or -1 on error. Does *not*
    149  * zero-terminate the content. Will not read more
    150  * than 'buffsize' bytes.
    151  */
    152 static int
    153 read_file(const char*  pathname, char*  buffer, size_t  buffsize)
    154 {
    155     int  fd, count;
    156 
    157     fd = open(pathname, O_RDONLY);
    158     if (fd < 0) {
    159         D("Could not open %s: %s\n", pathname, strerror(errno));
    160         return -1;
    161     }
    162     count = 0;
    163     while (count < (int)buffsize) {
    164         int ret = read(fd, buffer + count, buffsize - count);
    165         if (ret < 0) {
    166             if (errno == EINTR)
    167                 continue;
    168             D("Error while reading from %s: %s\n", pathname, strerror(errno));
    169             if (count == 0)
    170                 count = -1;
    171             break;
    172         }
    173         if (ret == 0)
    174             break;
    175         count += ret;
    176     }
    177     close(fd);
    178     return count;
    179 }
    180 
    181 /* Extract the content of a the first occurence of a given field in
    182  * the content of /proc/cpuinfo and return it as a heap-allocated
    183  * string that must be freed by the caller.
    184  *
    185  * Return NULL if not found
    186  */
    187 static char*
    188 extract_cpuinfo_field(const char* buffer, int buflen, const char* field)
    189 {
    190     int  fieldlen = strlen(field);
    191     const char* bufend = buffer + buflen;
    192     char* result = NULL;
    193     int len, ignore;
    194     const char *p, *q;
    195 
    196     /* Look for first field occurence, and ensures it starts the line. */
    197     p = buffer;
    198     for (;;) {
    199         p = memmem(p, bufend-p, field, fieldlen);
    200         if (p == NULL)
    201             goto EXIT;
    202 
    203         if (p == buffer || p[-1] == '\n')
    204             break;
    205 
    206         p += fieldlen;
    207     }
    208 
    209     /* Skip to the first column followed by a space */
    210     p += fieldlen;
    211     p  = memchr(p, ':', bufend-p);
    212     if (p == NULL || p[1] != ' ')
    213         goto EXIT;
    214 
    215     /* Find the end of the line */
    216     p += 2;
    217     q = memchr(p, '\n', bufend-p);
    218     if (q == NULL)
    219         q = bufend;
    220 
    221     /* Copy the line into a heap-allocated buffer */
    222     len = q-p;
    223     result = malloc(len+1);
    224     if (result == NULL)
    225         goto EXIT;
    226 
    227     memcpy(result, p, len);
    228     result[len] = '\0';
    229 
    230 EXIT:
    231     return result;
    232 }
    233 
    234 /* Like strlen(), but for constant string literals */
    235 #define STRLEN_CONST(x)  ((sizeof(x)-1)
    236 
    237 
    238 /* Checks that a space-separated list of items contains one given 'item'.
    239  * Returns 1 if found, 0 otherwise.
    240  */
    241 static int
    242 has_list_item(const char* list, const char* item)
    243 {
    244     const char*  p = list;
    245     int itemlen = strlen(item);
    246 
    247     if (list == NULL)
    248         return 0;
    249 
    250     while (*p) {
    251         const char*  q;
    252 
    253         /* skip spaces */
    254         while (*p == ' ' || *p == '\t')
    255             p++;
    256 
    257         /* find end of current list item */
    258         q = p;
    259         while (*q && *q != ' ' && *q != '\t')
    260             q++;
    261 
    262         if (itemlen == q-p && !memcmp(p, item, itemlen))
    263             return 1;
    264 
    265         /* skip to next item */
    266         p = q;
    267     }
    268     return 0;
    269 }
    270 
    271 /* Parse an decimal integer starting from 'input', but not going further
    272  * than 'limit'. Return the value into '*result'.
    273  *
    274  * NOTE: Does not skip over leading spaces, or deal with sign characters.
    275  * NOTE: Ignores overflows.
    276  *
    277  * The function returns NULL in case of error (bad format), or the new
    278  * position after the decimal number in case of success (which will always
    279  * be <= 'limit').
    280  */
    281 static const char*
    282 parse_decimal(const char* input, const char* limit, int* result)
    283 {
    284     const char* p = input;
    285     int val = 0;
    286     while (p < limit) {
    287         int d = (*p - '0');
    288         if ((unsigned)d >= 10U)
    289             break;
    290         val = val*10 + d;
    291         p++;
    292     }
    293     if (p == input)
    294         return NULL;
    295 
    296     *result = val;
    297     return p;
    298 }
    299 
    300 /* This small data type is used to represent a CPU list / mask, as read
    301  * from sysfs on Linux. See http://www.kernel.org/doc/Documentation/cputopology.txt
    302  *
    303  * For now, we don't expect more than 32 cores on mobile devices, so keep
    304  * everything simple.
    305  */
    306 typedef struct {
    307     uint32_t mask;
    308 } CpuList;
    309 
    310 static __inline__ void
    311 cpulist_init(CpuList* list) {
    312     list->mask = 0;
    313 }
    314 
    315 static __inline__ void
    316 cpulist_and(CpuList* list1, CpuList* list2) {
    317     list1->mask &= list2->mask;
    318 }
    319 
    320 static __inline__ void
    321 cpulist_set(CpuList* list, int index) {
    322     if ((unsigned)index < 32) {
    323         list->mask |= (uint32_t)(1U << index);
    324     }
    325 }
    326 
    327 static __inline__ int
    328 cpulist_count(CpuList* list) {
    329     return __builtin_popcount(list->mask);
    330 }
    331 
    332 /* Parse a textual list of cpus and store the result inside a CpuList object.
    333  * Input format is the following:
    334  * - comma-separated list of items (no spaces)
    335  * - each item is either a single decimal number (cpu index), or a range made
    336  *   of two numbers separated by a single dash (-). Ranges are inclusive.
    337  *
    338  * Examples:   0
    339  *             2,4-127,128-143
    340  *             0-1
    341  */
    342 static void
    343 cpulist_parse(CpuList* list, const char* line, int line_len)
    344 {
    345     const char* p = line;
    346     const char* end = p + line_len;
    347     const char* q;
    348 
    349     /* NOTE: the input line coming from sysfs typically contains a
    350      * trailing newline, so take care of it in the code below
    351      */
    352     while (p < end && *p != '\n')
    353     {
    354         int val, start_value, end_value;
    355 
    356         /* Find the end of current item, and put it into 'q' */
    357         q = memchr(p, ',', end-p);
    358         if (q == NULL) {
    359             q = end;
    360         }
    361 
    362         /* Get first value */
    363         p = parse_decimal(p, q, &start_value);
    364         if (p == NULL)
    365             goto BAD_FORMAT;
    366 
    367         end_value = start_value;
    368 
    369         /* If we're not at the end of the item, expect a dash and
    370          * and integer; extract end value.
    371          */
    372         if (p < q && *p == '-') {
    373             p = parse_decimal(p+1, q, &end_value);
    374             if (p == NULL)
    375                 goto BAD_FORMAT;
    376         }
    377 
    378         /* Set bits CPU list bits */
    379         for (val = start_value; val <= end_value; val++) {
    380             cpulist_set(list, val);
    381         }
    382 
    383         /* Jump to next item */
    384         p = q;
    385         if (p < end)
    386             p++;
    387     }
    388 
    389 BAD_FORMAT:
    390     ;
    391 }
    392 
    393 /* Read a CPU list from one sysfs file */
    394 static void
    395 cpulist_read_from(CpuList* list, const char* filename)
    396 {
    397     char   file[64];
    398     int    filelen;
    399 
    400     cpulist_init(list);
    401 
    402     filelen = read_file(filename, file, sizeof file);
    403     if (filelen < 0) {
    404         D("Could not read %s: %s\n", filename, strerror(errno));
    405         return;
    406     }
    407 
    408     cpulist_parse(list, file, filelen);
    409 }
    410 
    411 // See <asm/hwcap.h> kernel header.
    412 #define HWCAP_VFP       (1 << 6)
    413 #define HWCAP_IWMMXT    (1 << 9)
    414 #define HWCAP_NEON      (1 << 12)
    415 #define HWCAP_VFPv3     (1 << 13)
    416 #define HWCAP_VFPv3D16  (1 << 14)
    417 #define HWCAP_VFPv4     (1 << 16)
    418 #define HWCAP_IDIVA     (1 << 17)
    419 #define HWCAP_IDIVT     (1 << 18)
    420 
    421 #define AT_HWCAP 16
    422 
    423 #if defined(__arm__)
    424 /* Compute the ELF HWCAP flags.
    425  */
    426 static uint32_t
    427 get_elf_hwcap(const char* cpuinfo, int cpuinfo_len)
    428 {
    429   /* IMPORTANT:
    430    *   Accessing /proc/self/auxv doesn't work anymore on all
    431    *   platform versions. More specifically, when running inside
    432    *   a regular application process, most of /proc/self/ will be
    433    *   non-readable, including /proc/self/auxv. This doesn't
    434    *   happen however if the application is debuggable, or when
    435    *   running under the "shell" UID, which is why this was not
    436    *   detected appropriately.
    437    */
    438 #if 0
    439     uint32_t result = 0;
    440     const char filepath[] = "/proc/self/auxv";
    441     int fd = open(filepath, O_RDONLY);
    442     if (fd < 0) {
    443         D("Could not open %s: %s\n", filepath, strerror(errno));
    444         return 0;
    445     }
    446 
    447     struct { uint32_t tag; uint32_t value; } entry;
    448 
    449     for (;;) {
    450         int ret = read(fd, (char*)&entry, sizeof entry);
    451         if (ret < 0) {
    452             if (errno == EINTR)
    453                 continue;
    454             D("Error while reading %s: %s\n", filepath, strerror(errno));
    455             break;
    456         }
    457         // Detect end of list.
    458         if (ret == 0 || (entry.tag == 0 && entry.value == 0))
    459           break;
    460         if (entry.tag == AT_HWCAP) {
    461           result = entry.value;
    462           break;
    463         }
    464     }
    465     close(fd);
    466     return result;
    467 #else
    468     // Recreate ELF hwcaps by parsing /proc/cpuinfo Features tag.
    469     uint32_t hwcaps = 0;
    470 
    471     char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features");
    472 
    473     if (cpuFeatures != NULL) {
    474         D("Found cpuFeatures = '%s'\n", cpuFeatures);
    475 
    476         if (has_list_item(cpuFeatures, "vfp"))
    477             hwcaps |= HWCAP_VFP;
    478         if (has_list_item(cpuFeatures, "vfpv3"))
    479             hwcaps |= HWCAP_VFPv3;
    480         if (has_list_item(cpuFeatures, "vfpv3d16"))
    481             hwcaps |= HWCAP_VFPv3D16;
    482         if (has_list_item(cpuFeatures, "vfpv4"))
    483             hwcaps |= HWCAP_VFPv4;
    484         if (has_list_item(cpuFeatures, "neon"))
    485             hwcaps |= HWCAP_NEON;
    486         if (has_list_item(cpuFeatures, "idiva"))
    487             hwcaps |= HWCAP_IDIVA;
    488         if (has_list_item(cpuFeatures, "idivt"))
    489             hwcaps |= HWCAP_IDIVT;
    490         if (has_list_item(cpuFeatures, "idiv"))
    491             hwcaps |= HWCAP_IDIVA | HWCAP_IDIVT;
    492         if (has_list_item(cpuFeatures, "iwmmxt"))
    493             hwcaps |= HWCAP_IWMMXT;
    494 
    495         free(cpuFeatures);
    496     }
    497     return hwcaps;
    498 #endif
    499 }
    500 #endif  /* __arm__ */
    501 
    502 /* Return the number of cpus present on a given device.
    503  *
    504  * To handle all weird kernel configurations, we need to compute the
    505  * intersection of the 'present' and 'possible' CPU lists and count
    506  * the result.
    507  */
    508 static int
    509 get_cpu_count(void)
    510 {
    511     CpuList cpus_present[1];
    512     CpuList cpus_possible[1];
    513 
    514     cpulist_read_from(cpus_present, "/sys/devices/system/cpu/present");
    515     cpulist_read_from(cpus_possible, "/sys/devices/system/cpu/possible");
    516 
    517     /* Compute the intersection of both sets to get the actual number of
    518      * CPU cores that can be used on this device by the kernel.
    519      */
    520     cpulist_and(cpus_present, cpus_possible);
    521 
    522     return cpulist_count(cpus_present);
    523 }
    524 
    525 static void
    526 android_cpuInitFamily(void)
    527 {
    528 #if defined(__ARM_ARCH__)
    529     g_cpuFamily = ANDROID_CPU_FAMILY_ARM;
    530 #elif defined(__i386__)
    531     g_cpuFamily = ANDROID_CPU_FAMILY_X86;
    532 #elif defined(_MIPS_ARCH)
    533     g_cpuFamily = ANDROID_CPU_FAMILY_MIPS;
    534 #else
    535     g_cpuFamily = ANDROID_CPU_FAMILY_UNKNOWN;
    536 #endif
    537 }
    538 
    539 static void
    540 android_cpuInit(void)
    541 {
    542     char* cpuinfo = NULL;
    543     int   cpuinfo_len;
    544 
    545     android_cpuInitFamily();
    546 
    547     g_cpuFeatures = 0;
    548     g_cpuCount    = 1;
    549     g_inited      = 1;
    550 
    551     cpuinfo_len = get_file_size("/proc/cpuinfo");
    552     if (cpuinfo_len < 0) {
    553       D("cpuinfo_len cannot be computed!");
    554       return;
    555     }
    556     cpuinfo = malloc(cpuinfo_len);
    557     if (cpuinfo == NULL) {
    558       D("cpuinfo buffer could not be allocated");
    559       return;
    560     }
    561     cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, cpuinfo_len);
    562     D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len,
    563       cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo);
    564 
    565     if (cpuinfo_len < 0)  /* should not happen */ {
    566         free(cpuinfo);
    567         return;
    568     }
    569 
    570     /* Count the CPU cores, the value may be 0 for single-core CPUs */
    571     g_cpuCount = get_cpu_count();
    572     if (g_cpuCount == 0) {
    573         g_cpuCount = 1;
    574     }
    575 
    576     D("found cpuCount = %d\n", g_cpuCount);
    577 
    578 #ifdef __ARM_ARCH__
    579     {
    580         char*  features = NULL;
    581         char*  architecture = NULL;
    582 
    583         /* Extract architecture from the "CPU Architecture" field.
    584          * The list is well-known, unlike the the output of
    585          * the 'Processor' field which can vary greatly.
    586          *
    587          * See the definition of the 'proc_arch' array in
    588          * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
    589          * same file.
    590          */
    591         char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
    592 
    593         if (cpuArch != NULL) {
    594             char*  end;
    595             long   archNumber;
    596             int    hasARMv7 = 0;
    597 
    598             D("found cpuArch = '%s'\n", cpuArch);
    599 
    600             /* read the initial decimal number, ignore the rest */
    601             archNumber = strtol(cpuArch, &end, 10);
    602 
    603             /* Here we assume that ARMv8 will be upwards compatible with v7
    604              * in the future. Unfortunately, there is no 'Features' field to
    605              * indicate that Thumb-2 is supported.
    606              */
    607             if (end > cpuArch && archNumber >= 7) {
    608                 hasARMv7 = 1;
    609             }
    610 
    611             /* Unfortunately, it seems that certain ARMv6-based CPUs
    612              * report an incorrect architecture number of 7!
    613              *
    614              * See http://code.google.com/p/android/issues/detail?id=10812
    615              *
    616              * We try to correct this by looking at the 'elf_format'
    617              * field reported by the 'Processor' field, which is of the
    618              * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
    619              * an ARMv6-one.
    620              */
    621             if (hasARMv7) {
    622                 char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len,
    623                                                       "Processor");
    624                 if (cpuProc != NULL) {
    625                     D("found cpuProc = '%s'\n", cpuProc);
    626                     if (has_list_item(cpuProc, "(v6l)")) {
    627                         D("CPU processor and architecture mismatch!!\n");
    628                         hasARMv7 = 0;
    629                     }
    630                     free(cpuProc);
    631                 }
    632             }
    633 
    634             if (hasARMv7) {
    635                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_ARMv7;
    636             }
    637 
    638             /* The LDREX / STREX instructions are available from ARMv6 */
    639             if (archNumber >= 6) {
    640                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_LDREX_STREX;
    641             }
    642 
    643             free(cpuArch);
    644         }
    645 
    646         /* Extract the list of CPU features from ELF hwcaps */
    647         uint32_t hwcaps = get_elf_hwcap(cpuinfo, cpuinfo_len);
    648 
    649         if (hwcaps != 0) {
    650             int has_vfp = (hwcaps & HWCAP_VFP);
    651             int has_vfpv3 = (hwcaps & HWCAP_VFPv3);
    652             int has_vfpv3d16 = (hwcaps & HWCAP_VFPv3D16);
    653             int has_vfpv4 = (hwcaps & HWCAP_VFPv4);
    654             int has_neon = (hwcaps & HWCAP_NEON);
    655             int has_idiva = (hwcaps & HWCAP_IDIVA);
    656             int has_idivt = (hwcaps & HWCAP_IDIVT);
    657             int has_iwmmxt = (hwcaps & HWCAP_IWMMXT);
    658 
    659             // The kernel does a poor job at ensuring consistency when
    660             // describing CPU features. So lots of guessing is needed.
    661 
    662             // 'vfpv4' implies VFPv3|VFP_FMA|FP16
    663             if (has_vfpv4)
    664                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3    |
    665                                  ANDROID_CPU_ARM_FEATURE_VFP_FP16 |
    666                                  ANDROID_CPU_ARM_FEATURE_VFP_FMA;
    667 
    668             // 'vfpv3' or 'vfpv3d16' imply VFPv3. Note that unlike GCC,
    669             // a value of 'vfpv3' doesn't necessarily mean that the D32
    670             // feature is present, so be conservative. All CPUs in the
    671             // field that support D32 also support NEON, so this should
    672             // not be a problem in practice.
    673             if (has_vfpv3 || has_vfpv3d16)
    674                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
    675 
    676             // 'vfp' is super ambiguous. Depending on the kernel, it can
    677             // either mean VFPv2 or VFPv3. Make it depend on ARMv7.
    678             if (has_vfp) {
    679               if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_ARMv7)
    680                   g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
    681               else
    682                   g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2;
    683             }
    684 
    685             // Neon implies VFPv3|D32, and if vfpv4 is detected, NEON_FMA
    686             if (has_neon) {
    687                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 |
    688                                  ANDROID_CPU_ARM_FEATURE_NEON |
    689                                  ANDROID_CPU_ARM_FEATURE_VFP_D32;
    690               if (has_vfpv4)
    691                   g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_NEON_FMA;
    692             }
    693 
    694             // VFPv3 implies VFPv2 and ARMv7
    695             if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_VFPv3)
    696                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2 |
    697                                  ANDROID_CPU_ARM_FEATURE_ARMv7;
    698 
    699             // Note that some buggy kernels do not report these even when
    700             // the CPU actually support the division instructions. However,
    701             // assume that if 'vfpv4' is detected, then the CPU supports
    702             // sdiv/udiv properly.
    703             if (has_idiva || has_vfpv4)
    704                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM;
    705             if (has_idivt || has_vfpv4)
    706                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2;
    707 
    708             if (has_iwmmxt)
    709                 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_iWMMXt;
    710         }
    711     }
    712 #endif /* __ARM_ARCH__ */
    713 
    714 #ifdef __i386__
    715     int regs[4];
    716 
    717 /* According to http://en.wikipedia.org/wiki/CPUID */
    718 #define VENDOR_INTEL_b  0x756e6547
    719 #define VENDOR_INTEL_c  0x6c65746e
    720 #define VENDOR_INTEL_d  0x49656e69
    721 
    722     x86_cpuid(0, regs);
    723     int vendorIsIntel = (regs[1] == VENDOR_INTEL_b &&
    724                          regs[2] == VENDOR_INTEL_c &&
    725                          regs[3] == VENDOR_INTEL_d);
    726 
    727     x86_cpuid(1, regs);
    728     if ((regs[2] & (1 << 9)) != 0) {
    729         g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSSE3;
    730     }
    731     if ((regs[2] & (1 << 23)) != 0) {
    732         g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_POPCNT;
    733     }
    734     if (vendorIsIntel && (regs[2] & (1 << 22)) != 0) {
    735         g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_MOVBE;
    736     }
    737 #endif
    738 
    739     free(cpuinfo);
    740 }
    741 
    742 
    743 AndroidCpuFamily
    744 android_getCpuFamily(void)
    745 {
    746     pthread_once(&g_once, android_cpuInit);
    747     return g_cpuFamily;
    748 }
    749 
    750 
    751 uint64_t
    752 android_getCpuFeatures(void)
    753 {
    754     pthread_once(&g_once, android_cpuInit);
    755     return g_cpuFeatures;
    756 }
    757 
    758 
    759 int
    760 android_getCpuCount(void)
    761 {
    762     pthread_once(&g_once, android_cpuInit);
    763     return g_cpuCount;
    764 }
    765 
    766 static void
    767 android_cpuInitDummy(void)
    768 {
    769     g_inited = 1;
    770 }
    771 
    772 int
    773 android_setCpu(int cpu_count, uint64_t cpu_features)
    774 {
    775     /* Fail if the library was already initialized. */
    776     if (g_inited)
    777         return 0;
    778 
    779     android_cpuInitFamily();
    780     g_cpuCount = (cpu_count <= 0 ? 1 : cpu_count);
    781     g_cpuFeatures = cpu_features;
    782     pthread_once(&g_once, android_cpuInitDummy);
    783 
    784     return 1;
    785 }
    786 
    787 /*
    788  * Technical note: Making sense of ARM's FPU architecture versions.
    789  *
    790  * FPA was ARM's first attempt at an FPU architecture. There is no Android
    791  * device that actually uses it since this technology was already obsolete
    792  * when the project started. If you see references to FPA instructions
    793  * somewhere, you can be sure that this doesn't apply to Android at all.
    794  *
    795  * FPA was followed by "VFP", soon renamed "VFPv1" due to the emergence of
    796  * new versions / additions to it. ARM considers this obsolete right now,
    797  * and no known Android device implements it either.
    798  *
    799  * VFPv2 added a few instructions to VFPv1, and is an *optional* extension
    800  * supported by some ARMv5TE, ARMv6 and ARMv6T2 CPUs. Note that a device
    801  * supporting the 'armeabi' ABI doesn't necessarily support these.
    802  *
    803  * VFPv3-D16 adds a few instructions on top of VFPv2 and is typically used
    804  * on ARMv7-A CPUs which implement a FPU. Note that it is also mandated
    805  * by the Android 'armeabi-v7a' ABI. The -D16 suffix in its name means
    806  * that it provides 16 double-precision FPU registers (d0-d15) and 32
    807  * single-precision ones (s0-s31) which happen to be mapped to the same
    808  * register banks.
    809  *
    810  * VFPv3-D32 is the name of an extension to VFPv3-D16 that provides 16
    811  * additional double precision registers (d16-d31). Note that there are
    812  * still only 32 single precision registers.
    813  *
    814  * VFPv3xD is a *subset* of VFPv3-D16 that only provides single-precision
    815  * registers. It is only used on ARMv7-M (i.e. on micro-controllers) which
    816  * are not supported by Android. Note that it is not compatible with VFPv2.
    817  *
    818  * NOTE: The term 'VFPv3' usually designate either VFPv3-D16 or VFPv3-D32
    819  *       depending on context. For example GCC uses it for VFPv3-D32, but
    820  *       the Linux kernel code uses it for VFPv3-D16 (especially in
    821  *       /proc/cpuinfo). Always try to use the full designation when
    822  *       possible.
    823  *
    824  * NEON, a.k.a. "ARM Advanced SIMD" is an extension that provides
    825  * instructions to perform parallel computations on vectors of 8, 16,
    826  * 32, 64 and 128 bit quantities. NEON requires VFPv32-D32 since all
    827  * NEON registers are also mapped to the same register banks.
    828  *
    829  * VFPv4-D16, adds a few instructions on top of VFPv3-D16 in order to
    830  * perform fused multiply-accumulate on VFP registers, as well as
    831  * half-precision (16-bit) conversion operations.
    832  *
    833  * VFPv4-D32 is VFPv4-D16 with 32, instead of 16, FPU double precision
    834  * registers.
    835  *
    836  * VPFv4-NEON is VFPv4-D32 with NEON instructions. It also adds fused
    837  * multiply-accumulate instructions that work on the NEON registers.
    838  *
    839  * NOTE: Similarly, "VFPv4" might either reference VFPv4-D16 or VFPv4-D32
    840  *       depending on context.
    841  *
    842  * The following information was determined by scanning the binutils-2.22
    843  * sources:
    844  *
    845  * Basic VFP instruction subsets:
    846  *
    847  * #define FPU_VFP_EXT_V1xD 0x08000000     // Base VFP instruction set.
    848  * #define FPU_VFP_EXT_V1   0x04000000     // Double-precision insns.
    849  * #define FPU_VFP_EXT_V2   0x02000000     // ARM10E VFPr1.
    850  * #define FPU_VFP_EXT_V3xD 0x01000000     // VFPv3 single-precision.
    851  * #define FPU_VFP_EXT_V3   0x00800000     // VFPv3 double-precision.
    852  * #define FPU_NEON_EXT_V1  0x00400000     // Neon (SIMD) insns.
    853  * #define FPU_VFP_EXT_D32  0x00200000     // Registers D16-D31.
    854  * #define FPU_VFP_EXT_FP16 0x00100000     // Half-precision extensions.
    855  * #define FPU_NEON_EXT_FMA 0x00080000     // Neon fused multiply-add
    856  * #define FPU_VFP_EXT_FMA  0x00040000     // VFP fused multiply-add
    857  *
    858  * FPU types (excluding NEON)
    859  *
    860  * FPU_VFP_V1xD (EXT_V1xD)
    861  *    |
    862  *    +--------------------------+
    863  *    |                          |
    864  * FPU_VFP_V1 (+EXT_V1)       FPU_VFP_V3xD (+EXT_V2+EXT_V3xD)
    865  *    |                          |
    866  *    |                          |
    867  * FPU_VFP_V2 (+EXT_V2)       FPU_VFP_V4_SP_D16 (+EXT_FP16+EXT_FMA)
    868  *    |
    869  * FPU_VFP_V3D16 (+EXT_Vx3D+EXT_V3)
    870  *    |
    871  *    +--------------------------+
    872  *    |                          |
    873  * FPU_VFP_V3 (+EXT_D32)     FPU_VFP_V4D16 (+EXT_FP16+EXT_FMA)
    874  *    |                          |
    875  *    |                      FPU_VFP_V4 (+EXT_D32)
    876  *    |
    877  * FPU_VFP_HARD (+EXT_FMA+NEON_EXT_FMA)
    878  *
    879  * VFP architectures:
    880  *
    881  * ARCH_VFP_V1xD  (EXT_V1xD)
    882  *   |
    883  *   +------------------+
    884  *   |                  |
    885  *   |             ARCH_VFP_V3xD (+EXT_V2+EXT_V3xD)
    886  *   |                  |
    887  *   |             ARCH_VFP_V3xD_FP16 (+EXT_FP16)
    888  *   |                  |
    889  *   |             ARCH_VFP_V4_SP_D16 (+EXT_FMA)
    890  *   |
    891  * ARCH_VFP_V1 (+EXT_V1)
    892  *   |
    893  * ARCH_VFP_V2 (+EXT_V2)
    894  *   |
    895  * ARCH_VFP_V3D16 (+EXT_V3xD+EXT_V3)
    896  *   |
    897  *   +-------------------+
    898  *   |                   |
    899  *   |         ARCH_VFP_V3D16_FP16  (+EXT_FP16)
    900  *   |
    901  *   +-------------------+
    902  *   |                   |
    903  *   |         ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
    904  *   |                   |
    905  *   |         ARCH_VFP_V4 (+EXT_D32)
    906  *   |                   |
    907  *   |         ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
    908  *   |
    909  * ARCH_VFP_V3 (+EXT_D32)
    910  *   |
    911  *   +-------------------+
    912  *   |                   |
    913  *   |         ARCH_VFP_V3_FP16 (+EXT_FP16)
    914  *   |
    915  * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
    916  *   |
    917  * ARCH_NEON_FP16 (+EXT_FP16)
    918  *
    919  * -fpu=<name> values and their correspondance with FPU architectures above:
    920  *
    921  *   {"vfp",               FPU_ARCH_VFP_V2},
    922  *   {"vfp9",              FPU_ARCH_VFP_V2},
    923  *   {"vfp3",              FPU_ARCH_VFP_V3}, // For backwards compatbility.
    924  *   {"vfp10",             FPU_ARCH_VFP_V2},
    925  *   {"vfp10-r0",          FPU_ARCH_VFP_V1},
    926  *   {"vfpxd",             FPU_ARCH_VFP_V1xD},
    927  *   {"vfpv2",             FPU_ARCH_VFP_V2},
    928  *   {"vfpv3",             FPU_ARCH_VFP_V3},
    929  *   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
    930  *   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
    931  *   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
    932  *   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
    933  *   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
    934  *   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
    935  *   {"neon-fp16",         FPU_ARCH_NEON_FP16},
    936  *   {"vfpv4",             FPU_ARCH_VFP_V4},
    937  *   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
    938  *   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
    939  *   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
    940  *
    941  *
    942  * Simplified diagram that only includes FPUs supported by Android:
    943  * Only ARCH_VFP_V3D16 is actually mandated by the armeabi-v7a ABI,
    944  * all others are optional and must be probed at runtime.
    945  *
    946  * ARCH_VFP_V3D16 (EXT_V1xD+EXT_V1+EXT_V2+EXT_V3xD+EXT_V3)
    947  *   |
    948  *   +-------------------+
    949  *   |                   |
    950  *   |         ARCH_VFP_V3D16_FP16  (+EXT_FP16)
    951  *   |
    952  *   +-------------------+
    953  *   |                   |
    954  *   |         ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
    955  *   |                   |
    956  *   |         ARCH_VFP_V4 (+EXT_D32)
    957  *   |                   |
    958  *   |         ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
    959  *   |
    960  * ARCH_VFP_V3 (+EXT_D32)
    961  *   |
    962  *   +-------------------+
    963  *   |                   |
    964  *   |         ARCH_VFP_V3_FP16 (+EXT_FP16)
    965  *   |
    966  * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
    967  *   |
    968  * ARCH_NEON_FP16 (+EXT_FP16)
    969  *
    970  */
    971