cpu-features.c revision 3ebb5017c5a6154992928ec2409bf3cc048e585b
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 r9?: Support for 64-bit CPUs (Intel, ARM & MIPS).
32 *
33 * NDK r8d: Add android_setCpu().
34 *
35 * NDK r8c: Add new ARM CPU features: VFPv2, VFP_D32, VFP_FP16,
36 *          VFP_FMA, NEON_FMA, IDIV_ARM, IDIV_THUMB2 and iWMMXt.
37 *
38 *          Rewrite the code to parse /proc/self/auxv instead of
39 *          the "Features" field in /proc/cpuinfo.
40 *
41 *          Dynamically allocate the buffer that hold the content
42 *          of /proc/cpuinfo to deal with newer hardware.
43 *
44 * NDK r7c: Fix CPU count computation. The old method only reported the
45 *           number of _active_ CPUs when the library was initialized,
46 *           which could be less than the real total.
47 *
48 * NDK r5: Handle buggy kernels which report a CPU Architecture number of 7
49 *         for an ARMv6 CPU (see below).
50 *
51 *         Handle kernels that only report 'neon', and not 'vfpv3'
52 *         (VFPv3 is mandated by the ARM architecture is Neon is implemented)
53 *
54 *         Handle kernels that only report 'vfpv3d16', and not 'vfpv3'
55 *
56 *         Fix x86 compilation. Report ANDROID_CPU_FAMILY_X86 in
57 *         android_getCpuFamily().
58 *
59 * NDK r4: Initial release
60 */
61
62#if defined(__le32__) || defined(__le64__)
63
64// When users enter this, we should only provide interface and
65// libportable will give the implementations.
66
67#else // !__le32__ && !__le64__
68
69#include "cpu-features.h"
70
71#include <dlfcn.h>
72#include <errno.h>
73#include <fcntl.h>
74#include <pthread.h>
75#include <stdio.h>
76#include <stdlib.h>
77#include <sys/system_properties.h>
78
79static  pthread_once_t     g_once;
80static  int                g_inited;
81static  AndroidCpuFamily   g_cpuFamily;
82static  uint64_t           g_cpuFeatures;
83static  int                g_cpuCount;
84
85#ifdef __arm__
86static  uint32_t           g_cpuIdArm;
87#endif
88
89static const int android_cpufeatures_debug = 0;
90
91#define  D(...) \
92    do { \
93        if (android_cpufeatures_debug) { \
94            printf(__VA_ARGS__); fflush(stdout); \
95        } \
96    } while (0)
97
98#ifdef __i386__
99static __inline__ void x86_cpuid(int func, int values[4])
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}
117#endif
118
119/* Get the size of a file by reading it until the end. This is needed
120 * because files under /proc do not always return a valid size when
121 * using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed.
122 */
123static int
124get_file_size(const char* pathname)
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}
151
152/* Read the content of /proc/cpuinfo into a user-provided buffer.
153 * Return the length of the data, or -1 on error. Does *not*
154 * zero-terminate the content. Will not read more
155 * than 'buffsize' bytes.
156 */
157static int
158read_file(const char*  pathname, char*  buffer, size_t  buffsize)
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}
185
186/* Extract the content of a the first occurence of a given field in
187 * the content of /proc/cpuinfo and return it as a heap-allocated
188 * string that must be freed by the caller.
189 *
190 * Return NULL if not found
191 */
192static char*
193extract_cpuinfo_field(const char* buffer, int buflen, const char* field)
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 = 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  = 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 = 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 = malloc(len+1);
229    if (result == NULL)
230        goto EXIT;
231
232    memcpy(result, p, len);
233    result[len] = '\0';
234
235EXIT:
236    return result;
237}
238
239/* Checks that a space-separated list of items contains one given 'item'.
240 * Returns 1 if found, 0 otherwise.
241 */
242static int
243has_list_item(const char* list, const char* item)
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}
271
272/* Parse a number starting from 'input', but not going further
273 * than 'limit'. Return the value into '*result'.
274 *
275 * NOTE: Does not skip over leading spaces, or deal with sign characters.
276 * NOTE: Ignores overflows.
277 *
278 * The function returns NULL in case of error (bad format), or the new
279 * position after the decimal number in case of success (which will always
280 * be <= 'limit').
281 */
282static const char*
283parse_number(const char* input, const char* limit, int base, int* result)
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}
308
309static const char*
310parse_decimal(const char* input, const char* limit, int* result)
311{
312    return parse_number(input, limit, 10, result);
313}
314
315static const char*
316parse_hexadecimal(const char* input, const char* limit, int* result)
317{
318    return parse_number(input, limit, 16, result);
319}
320
321/* This small data type is used to represent a CPU list / mask, as read
322 * from sysfs on Linux. See http://www.kernel.org/doc/Documentation/cputopology.txt
323 *
324 * For now, we don't expect more than 32 cores on mobile devices, so keep
325 * everything simple.
326 */
327typedef struct {
328    uint32_t mask;
329} CpuList;
330
331static __inline__ void
332cpulist_init(CpuList* list) {
333    list->mask = 0;
334}
335
336static __inline__ void
337cpulist_and(CpuList* list1, CpuList* list2) {
338    list1->mask &= list2->mask;
339}
340
341static __inline__ void
342cpulist_set(CpuList* list, int index) {
343    if ((unsigned)index < 32) {
344        list->mask |= (uint32_t)(1U << index);
345    }
346}
347
348static __inline__ int
349cpulist_count(CpuList* list) {
350    return __builtin_popcount(list->mask);
351}
352
353/* Parse a textual list of cpus and store the result inside a CpuList object.
354 * Input format is the following:
355 * - comma-separated list of items (no spaces)
356 * - each item is either a single decimal number (cpu index), or a range made
357 *   of two numbers separated by a single dash (-). Ranges are inclusive.
358 *
359 * Examples:   0
360 *             2,4-127,128-143
361 *             0-1
362 */
363static void
364cpulist_parse(CpuList* list, const char* line, int line_len)
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 = 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
410BAD_FORMAT:
411    ;
412}
413
414/* Read a CPU list from one sysfs file */
415static void
416cpulist_read_from(CpuList* list, const char* filename)
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}
431#if defined(__aarch64__)
432// see <uapi/asm/hwcap.h> kernel header
433#define HWCAP_FP                (1 << 0)
434#define HWCAP_ASIMD             (1 << 1)
435#define HWCAP_AES               (1 << 3)
436#define HWCAP_PMULL             (1 << 4)
437#define HWCAP_SHA1              (1 << 5)
438#define HWCAP_SHA2              (1 << 6)
439#define HWCAP_CRC32             (1 << 7)
440#endif
441
442#if defined(__arm__)
443
444// See <asm/hwcap.h> kernel header.
445#define HWCAP_VFP       (1 << 6)
446#define HWCAP_IWMMXT    (1 << 9)
447#define HWCAP_NEON      (1 << 12)
448#define HWCAP_VFPv3     (1 << 13)
449#define HWCAP_VFPv3D16  (1 << 14)
450#define HWCAP_VFPv4     (1 << 16)
451#define HWCAP_IDIVA     (1 << 17)
452#define HWCAP_IDIVT     (1 << 18)
453
454// see <uapi/asm/hwcap.h> kernel header
455#define HWCAP2_AES     (1 << 0)
456#define HWCAP2_PMULL   (1 << 1)
457#define HWCAP2_SHA1    (1 << 2)
458#define HWCAP2_SHA2    (1 << 3)
459#define HWCAP2_CRC32   (1 << 4)
460
461// This is the list of 32-bit ARMv7 optional features that are _always_
462// supported by ARMv8 CPUs, as mandated by the ARM Architecture Reference
463// Manual.
464#define HWCAP_SET_FOR_ARMV8  \
465  ( HWCAP_VFP | \
466    HWCAP_NEON | \
467    HWCAP_VFPv3 | \
468    HWCAP_VFPv4 | \
469    HWCAP_IDIVA | \
470    HWCAP_IDIVT )
471#endif
472
473#if defined(__arm__) || defined(__aarch64__)
474
475#define AT_HWCAP 16
476#define AT_HWCAP2 26
477
478// Probe the system's C library for a 'getauxval' function and call it if
479// it exits, or return 0 for failure. This function is available since API
480// level 20.
481//
482// This code does *NOT* check for '__ANDROID_API__ >= 20' to support the
483// edge case where some NDK developers use headers for a platform that is
484// newer than the one really targetted by their application.
485// This is typically done to use newer native APIs only when running on more
486// recent Android versions, and requires careful symbol management.
487//
488// Note that getauxval() can't really be re-implemented here, because
489// its implementation does not parse /proc/self/auxv. Instead it depends
490// on values  that are passed by the kernel at process-init time to the
491// C runtime initialization layer.
492static uint32_t
493get_elf_hwcap_from_getauxval(int hwcap_type) {
494    typedef unsigned long getauxval_func_t(unsigned long);
495
496    dlerror();
497    void* libc_handle = dlopen("libc.so", RTLD_NOW);
498    if (!libc_handle) {
499        D("Could not dlopen() C library: %s\n", dlerror());
500        return 0;
501    }
502
503    uint32_t ret = 0;
504    getauxval_func_t* func = (getauxval_func_t*)
505            dlsym(libc_handle, "getauxval");
506    if (!func) {
507        D("Could not find getauxval() in C library\n");
508    } else {
509        // Note: getauxval() returns 0 on failure. Doesn't touch errno.
510        ret = (uint32_t)(*func)(hwcap_type);
511    }
512    dlclose(libc_handle);
513    return ret;
514}
515#endif
516
517#if defined(__arm__)
518// Parse /proc/self/auxv to extract the ELF HW capabilities bitmap for the
519// current CPU. Note that this file is not accessible from regular
520// application processes on some Android platform releases.
521// On success, return new ELF hwcaps, or 0 on failure.
522static uint32_t
523get_elf_hwcap_from_proc_self_auxv(void) {
524    const char filepath[] = "/proc/self/auxv";
525    int fd = TEMP_FAILURE_RETRY(open(filepath, O_RDONLY));
526    if (fd < 0) {
527        D("Could not open %s: %s\n", filepath, strerror(errno));
528        return 0;
529    }
530
531    struct { uint32_t tag; uint32_t value; } entry;
532
533    uint32_t result = 0;
534    for (;;) {
535        int ret = TEMP_FAILURE_RETRY(read(fd, (char*)&entry, sizeof entry));
536        if (ret < 0) {
537            D("Error while reading %s: %s\n", filepath, strerror(errno));
538            break;
539        }
540        // Detect end of list.
541        if (ret == 0 || (entry.tag == 0 && entry.value == 0))
542          break;
543        if (entry.tag == AT_HWCAP) {
544          result = entry.value;
545          break;
546        }
547    }
548    close(fd);
549    return result;
550}
551
552/* Compute the ELF HWCAP flags from the content of /proc/cpuinfo.
553 * This works by parsing the 'Features' line, which lists which optional
554 * features the device's CPU supports, on top of its reference
555 * architecture.
556 */
557static uint32_t
558get_elf_hwcap_from_proc_cpuinfo(const char* cpuinfo, int cpuinfo_len) {
559    uint32_t hwcaps = 0;
560    long architecture = 0;
561    char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
562    if (cpuArch) {
563        architecture = strtol(cpuArch, NULL, 10);
564        free(cpuArch);
565
566        if (architecture >= 8L) {
567            // This is a 32-bit ARM binary running on a 64-bit ARM64 kernel.
568            // The 'Features' line only lists the optional features that the
569            // device's CPU supports, compared to its reference architecture
570            // which are of no use for this process.
571            D("Faking 32-bit ARM HWCaps on ARMv%ld CPU\n", architecture);
572            return HWCAP_SET_FOR_ARMV8;
573        }
574    }
575
576    char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features");
577    if (cpuFeatures != NULL) {
578        D("Found cpuFeatures = '%s'\n", cpuFeatures);
579
580        if (has_list_item(cpuFeatures, "vfp"))
581            hwcaps |= HWCAP_VFP;
582        if (has_list_item(cpuFeatures, "vfpv3"))
583            hwcaps |= HWCAP_VFPv3;
584        if (has_list_item(cpuFeatures, "vfpv3d16"))
585            hwcaps |= HWCAP_VFPv3D16;
586        if (has_list_item(cpuFeatures, "vfpv4"))
587            hwcaps |= HWCAP_VFPv4;
588        if (has_list_item(cpuFeatures, "neon"))
589            hwcaps |= HWCAP_NEON;
590        if (has_list_item(cpuFeatures, "idiva"))
591            hwcaps |= HWCAP_IDIVA;
592        if (has_list_item(cpuFeatures, "idivt"))
593            hwcaps |= HWCAP_IDIVT;
594        if (has_list_item(cpuFeatures, "idiv"))
595            hwcaps |= HWCAP_IDIVA | HWCAP_IDIVT;
596        if (has_list_item(cpuFeatures, "iwmmxt"))
597            hwcaps |= HWCAP_IWMMXT;
598
599        free(cpuFeatures);
600    }
601    return hwcaps;
602}
603#endif  /* __arm__ */
604
605/* Return the number of cpus present on a given device.
606 *
607 * To handle all weird kernel configurations, we need to compute the
608 * intersection of the 'present' and 'possible' CPU lists and count
609 * the result.
610 */
611static int
612get_cpu_count(void)
613{
614    CpuList cpus_present[1];
615    CpuList cpus_possible[1];
616
617    cpulist_read_from(cpus_present, "/sys/devices/system/cpu/present");
618    cpulist_read_from(cpus_possible, "/sys/devices/system/cpu/possible");
619
620    /* Compute the intersection of both sets to get the actual number of
621     * CPU cores that can be used on this device by the kernel.
622     */
623    cpulist_and(cpus_present, cpus_possible);
624
625    return cpulist_count(cpus_present);
626}
627
628static void
629android_cpuInitFamily(void)
630{
631#if defined(__arm__)
632    g_cpuFamily = ANDROID_CPU_FAMILY_ARM;
633#elif defined(__i386__)
634    g_cpuFamily = ANDROID_CPU_FAMILY_X86;
635#elif defined(__mips64)
636/* Needs to be before __mips__ since the compiler defines both */
637    g_cpuFamily = ANDROID_CPU_FAMILY_MIPS64;
638#elif defined(__mips__)
639    g_cpuFamily = ANDROID_CPU_FAMILY_MIPS;
640#elif defined(__aarch64__)
641    g_cpuFamily = ANDROID_CPU_FAMILY_ARM64;
642#elif defined(__x86_64__)
643    g_cpuFamily = ANDROID_CPU_FAMILY_X86_64;
644#else
645    g_cpuFamily = ANDROID_CPU_FAMILY_UNKNOWN;
646#endif
647}
648
649static void
650android_cpuInit(void)
651{
652    char* cpuinfo = NULL;
653    int   cpuinfo_len;
654
655    android_cpuInitFamily();
656
657    g_cpuFeatures = 0;
658    g_cpuCount    = 1;
659    g_inited      = 1;
660
661    cpuinfo_len = get_file_size("/proc/cpuinfo");
662    if (cpuinfo_len < 0) {
663      D("cpuinfo_len cannot be computed!");
664      return;
665    }
666    cpuinfo = malloc(cpuinfo_len);
667    if (cpuinfo == NULL) {
668      D("cpuinfo buffer could not be allocated");
669      return;
670    }
671    cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, cpuinfo_len);
672    D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len,
673      cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo);
674
675    if (cpuinfo_len < 0)  /* should not happen */ {
676        free(cpuinfo);
677        return;
678    }
679
680    /* Count the CPU cores, the value may be 0 for single-core CPUs */
681    g_cpuCount = get_cpu_count();
682    if (g_cpuCount == 0) {
683        g_cpuCount = 1;
684    }
685
686    D("found cpuCount = %d\n", g_cpuCount);
687
688#ifdef __arm__
689    {
690        /* Extract architecture from the "CPU Architecture" field.
691         * The list is well-known, unlike the the output of
692         * the 'Processor' field which can vary greatly.
693         *
694         * See the definition of the 'proc_arch' array in
695         * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
696         * same file.
697         */
698        char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
699
700        if (cpuArch != NULL) {
701            char*  end;
702            long   archNumber;
703            int    hasARMv7 = 0;
704
705            D("found cpuArch = '%s'\n", cpuArch);
706
707            /* read the initial decimal number, ignore the rest */
708            archNumber = strtol(cpuArch, &end, 10);
709
710            /* Note that ARMv8 is upwards compatible with ARMv7. */
711            if (end > cpuArch && archNumber >= 7) {
712                hasARMv7 = 1;
713            }
714
715            /* Unfortunately, it seems that certain ARMv6-based CPUs
716             * report an incorrect architecture number of 7!
717             *
718             * See http://code.google.com/p/android/issues/detail?id=10812
719             *
720             * We try to correct this by looking at the 'elf_format'
721             * field reported by the 'Processor' field, which is of the
722             * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
723             * an ARMv6-one.
724             */
725            if (hasARMv7) {
726                char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len,
727                                                      "Processor");
728                if (cpuProc != NULL) {
729                    D("found cpuProc = '%s'\n", cpuProc);
730                    if (has_list_item(cpuProc, "(v6l)")) {
731                        D("CPU processor and architecture mismatch!!\n");
732                        hasARMv7 = 0;
733                    }
734                    free(cpuProc);
735                }
736            }
737
738            if (hasARMv7) {
739                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_ARMv7;
740            }
741
742            /* The LDREX / STREX instructions are available from ARMv6 */
743            if (archNumber >= 6) {
744                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_LDREX_STREX;
745            }
746
747            free(cpuArch);
748        }
749
750        /* Extract the list of CPU features from ELF hwcaps */
751        uint32_t hwcaps = 0;
752        hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
753        if (!hwcaps) {
754            D("Parsing /proc/self/auxv to extract ELF hwcaps!\n");
755            hwcaps = get_elf_hwcap_from_proc_self_auxv();
756        }
757        if (!hwcaps) {
758            // Parsing /proc/self/auxv will fail from regular application
759            // processes on some Android platform versions, when this happens
760            // parse proc/cpuinfo instead.
761            D("Parsing /proc/cpuinfo to extract ELF hwcaps!\n");
762            hwcaps = get_elf_hwcap_from_proc_cpuinfo(cpuinfo, cpuinfo_len);
763        }
764
765        if (hwcaps != 0) {
766            int has_vfp = (hwcaps & HWCAP_VFP);
767            int has_vfpv3 = (hwcaps & HWCAP_VFPv3);
768            int has_vfpv3d16 = (hwcaps & HWCAP_VFPv3D16);
769            int has_vfpv4 = (hwcaps & HWCAP_VFPv4);
770            int has_neon = (hwcaps & HWCAP_NEON);
771            int has_idiva = (hwcaps & HWCAP_IDIVA);
772            int has_idivt = (hwcaps & HWCAP_IDIVT);
773            int has_iwmmxt = (hwcaps & HWCAP_IWMMXT);
774
775            // The kernel does a poor job at ensuring consistency when
776            // describing CPU features. So lots of guessing is needed.
777
778            // 'vfpv4' implies VFPv3|VFP_FMA|FP16
779            if (has_vfpv4)
780                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3    |
781                                 ANDROID_CPU_ARM_FEATURE_VFP_FP16 |
782                                 ANDROID_CPU_ARM_FEATURE_VFP_FMA;
783
784            // 'vfpv3' or 'vfpv3d16' imply VFPv3. Note that unlike GCC,
785            // a value of 'vfpv3' doesn't necessarily mean that the D32
786            // feature is present, so be conservative. All CPUs in the
787            // field that support D32 also support NEON, so this should
788            // not be a problem in practice.
789            if (has_vfpv3 || has_vfpv3d16)
790                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
791
792            // 'vfp' is super ambiguous. Depending on the kernel, it can
793            // either mean VFPv2 or VFPv3. Make it depend on ARMv7.
794            if (has_vfp) {
795              if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_ARMv7)
796                  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
797              else
798                  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2;
799            }
800
801            // Neon implies VFPv3|D32, and if vfpv4 is detected, NEON_FMA
802            if (has_neon) {
803                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 |
804                                 ANDROID_CPU_ARM_FEATURE_NEON |
805                                 ANDROID_CPU_ARM_FEATURE_VFP_D32;
806              if (has_vfpv4)
807                  g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_NEON_FMA;
808            }
809
810            // VFPv3 implies VFPv2 and ARMv7
811            if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_VFPv3)
812                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2 |
813                                 ANDROID_CPU_ARM_FEATURE_ARMv7;
814
815            if (has_idiva)
816                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM;
817            if (has_idivt)
818                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2;
819
820            if (has_iwmmxt)
821                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_iWMMXt;
822        }
823
824        /* Extract the list of CPU features from ELF hwcaps2 */
825        uint32_t hwcaps2 = 0;
826        hwcaps2 = get_elf_hwcap_from_getauxval(AT_HWCAP2);
827        if (hwcaps2 != 0) {
828            int has_aes     = (hwcaps2 & HWCAP2_AES);
829            int has_pmull   = (hwcaps2 & HWCAP2_PMULL);
830            int has_sha1    = (hwcaps2 & HWCAP2_SHA1);
831            int has_sha2    = (hwcaps2 & HWCAP2_SHA2);
832            int has_crc32   = (hwcaps2 & HWCAP2_CRC32);
833
834            if (has_aes)
835                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_AES;
836            if (has_pmull)
837                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_PMULL;
838            if (has_sha1)
839                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_SHA1;
840            if (has_sha2)
841                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_SHA2;
842            if (has_crc32)
843                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_CRC32;
844        }
845        /* Extract the cpuid value from various fields */
846        // The CPUID value is broken up in several entries in /proc/cpuinfo.
847        // This table is used to rebuild it from the entries.
848        static const struct CpuIdEntry {
849            const char* field;
850            char        format;
851            char        bit_lshift;
852            char        bit_length;
853        } cpu_id_entries[] = {
854            { "CPU implementer", 'x', 24, 8 },
855            { "CPU variant", 'x', 20, 4 },
856            { "CPU part", 'x', 4, 12 },
857            { "CPU revision", 'd', 0, 4 },
858        };
859        size_t i;
860        D("Parsing /proc/cpuinfo to recover CPUID\n");
861        for (i = 0;
862             i < sizeof(cpu_id_entries)/sizeof(cpu_id_entries[0]);
863             ++i) {
864            const struct CpuIdEntry* entry = &cpu_id_entries[i];
865            char* value = extract_cpuinfo_field(cpuinfo,
866                                                cpuinfo_len,
867                                                entry->field);
868            if (value == NULL)
869                continue;
870
871            D("field=%s value='%s'\n", entry->field, value);
872            char* value_end = value + strlen(value);
873            int val = 0;
874            const char* start = value;
875            const char* p;
876            if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) {
877              start += 2;
878              p = parse_hexadecimal(start, value_end, &val);
879            } else if (entry->format == 'x')
880              p = parse_hexadecimal(value, value_end, &val);
881            else
882              p = parse_decimal(value, value_end, &val);
883
884            if (p > (const char*)start) {
885              val &= ((1 << entry->bit_length)-1);
886              val <<= entry->bit_lshift;
887              g_cpuIdArm |= (uint32_t) val;
888            }
889
890            free(value);
891        }
892
893        // Handle kernel configuration bugs that prevent the correct
894        // reporting of CPU features.
895        static const struct CpuFix {
896            uint32_t  cpuid;
897            uint64_t  or_flags;
898        } cpu_fixes[] = {
899            /* The Nexus 4 (Qualcomm Krait) kernel configuration
900             * forgets to report IDIV support. */
901            { 0x510006f2, ANDROID_CPU_ARM_FEATURE_IDIV_ARM |
902                          ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 },
903            };
904        size_t n;
905        for (n = 0; n < sizeof(cpu_fixes)/sizeof(cpu_fixes[0]); ++n) {
906            const struct CpuFix* entry = &cpu_fixes[n];
907
908            if (g_cpuIdArm == entry->cpuid)
909                g_cpuFeatures |= entry->or_flags;
910        }
911
912        // Special case: The emulator-specific Android 4.2 kernel fails
913        // to report support for the 32-bit ARM IDIV instruction.
914        // Technically, this is a feature of the virtual CPU implemented
915        // by the emulator. Note that it could also support Thumb IDIV
916        // in the future, and this will have to be slightly updated.
917        char* hardware = extract_cpuinfo_field(cpuinfo,
918                                               cpuinfo_len,
919                                               "Hardware");
920        if (hardware) {
921            if (!strcmp(hardware, "Goldfish") &&
922                g_cpuIdArm == 0x4100c080 &&
923                (g_cpuFamily & ANDROID_CPU_ARM_FEATURE_ARMv7) != 0) {
924                g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM;
925            }
926            free(hardware);
927        }
928    }
929#endif /* __arm__ */
930#ifdef __aarch64__
931    {
932        /* Extract the list of CPU features from ELF hwcaps */
933        uint32_t hwcaps = 0;
934        hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
935        if (hwcaps != 0) {
936            int has_fp      = (hwcaps & HWCAP_FP);
937            int has_asimd   = (hwcaps & HWCAP_ASIMD);
938            int has_aes     = (hwcaps & HWCAP_AES);
939            int has_pmull   = (hwcaps & HWCAP_PMULL);
940            int has_sha1    = (hwcaps & HWCAP_SHA1);
941            int has_sha2    = (hwcaps & HWCAP_SHA2);
942            int has_crc32   = (hwcaps & HWCAP_CRC32);
943
944            if(has_fp == 0) {
945                D("ERROR: Floating-point unit missing, but is required by Android on AArch64 CPUs\n");
946            }
947            if(has_asimd == 0) {
948                D("ERROR: ASIMD unit missing, but is required by Android on AArch64 CPUs\n");
949            }
950
951            if (has_fp)
952                g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_FP;
953            if (has_asimd)
954                g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_ASIMD;
955            if (has_aes)
956                g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_AES;
957            if (has_pmull)
958                g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_PMULL;
959            if (has_sha1)
960                g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_SHA1;
961            if (has_sha2)
962                g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_SHA2;
963            if (has_crc32)
964                g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_CRC32;
965        }
966    }
967#endif /* __aarch64__ */
968
969#ifdef __i386__
970    int regs[4];
971
972/* According to http://en.wikipedia.org/wiki/CPUID */
973#define VENDOR_INTEL_b  0x756e6547
974#define VENDOR_INTEL_c  0x6c65746e
975#define VENDOR_INTEL_d  0x49656e69
976
977    x86_cpuid(0, regs);
978    int vendorIsIntel = (regs[1] == VENDOR_INTEL_b &&
979                         regs[2] == VENDOR_INTEL_c &&
980                         regs[3] == VENDOR_INTEL_d);
981
982    x86_cpuid(1, regs);
983    if ((regs[2] & (1 << 9)) != 0) {
984        g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSSE3;
985    }
986    if ((regs[2] & (1 << 23)) != 0) {
987        g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_POPCNT;
988    }
989    if (vendorIsIntel && (regs[2] & (1 << 22)) != 0) {
990        g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_MOVBE;
991    }
992#endif
993
994    free(cpuinfo);
995}
996
997
998AndroidCpuFamily
999android_getCpuFamily(void)
1000{
1001    pthread_once(&g_once, android_cpuInit);
1002    return g_cpuFamily;
1003}
1004
1005
1006uint64_t
1007android_getCpuFeatures(void)
1008{
1009    pthread_once(&g_once, android_cpuInit);
1010    return g_cpuFeatures;
1011}
1012
1013
1014int
1015android_getCpuCount(void)
1016{
1017    pthread_once(&g_once, android_cpuInit);
1018    return g_cpuCount;
1019}
1020
1021static void
1022android_cpuInitDummy(void)
1023{
1024    g_inited = 1;
1025}
1026
1027int
1028android_setCpu(int cpu_count, uint64_t cpu_features)
1029{
1030    /* Fail if the library was already initialized. */
1031    if (g_inited)
1032        return 0;
1033
1034    android_cpuInitFamily();
1035    g_cpuCount = (cpu_count <= 0 ? 1 : cpu_count);
1036    g_cpuFeatures = cpu_features;
1037    pthread_once(&g_once, android_cpuInitDummy);
1038
1039    return 1;
1040}
1041
1042#ifdef __arm__
1043uint32_t
1044android_getCpuIdArm(void)
1045{
1046    pthread_once(&g_once, android_cpuInit);
1047    return g_cpuIdArm;
1048}
1049
1050int
1051android_setCpuArm(int cpu_count, uint64_t cpu_features, uint32_t cpu_id)
1052{
1053    if (!android_setCpu(cpu_count, cpu_features))
1054        return 0;
1055
1056    g_cpuIdArm = cpu_id;
1057    return 1;
1058}
1059#endif  /* __arm__ */
1060
1061/*
1062 * Technical note: Making sense of ARM's FPU architecture versions.
1063 *
1064 * FPA was ARM's first attempt at an FPU architecture. There is no Android
1065 * device that actually uses it since this technology was already obsolete
1066 * when the project started. If you see references to FPA instructions
1067 * somewhere, you can be sure that this doesn't apply to Android at all.
1068 *
1069 * FPA was followed by "VFP", soon renamed "VFPv1" due to the emergence of
1070 * new versions / additions to it. ARM considers this obsolete right now,
1071 * and no known Android device implements it either.
1072 *
1073 * VFPv2 added a few instructions to VFPv1, and is an *optional* extension
1074 * supported by some ARMv5TE, ARMv6 and ARMv6T2 CPUs. Note that a device
1075 * supporting the 'armeabi' ABI doesn't necessarily support these.
1076 *
1077 * VFPv3-D16 adds a few instructions on top of VFPv2 and is typically used
1078 * on ARMv7-A CPUs which implement a FPU. Note that it is also mandated
1079 * by the Android 'armeabi-v7a' ABI. The -D16 suffix in its name means
1080 * that it provides 16 double-precision FPU registers (d0-d15) and 32
1081 * single-precision ones (s0-s31) which happen to be mapped to the same
1082 * register banks.
1083 *
1084 * VFPv3-D32 is the name of an extension to VFPv3-D16 that provides 16
1085 * additional double precision registers (d16-d31). Note that there are
1086 * still only 32 single precision registers.
1087 *
1088 * VFPv3xD is a *subset* of VFPv3-D16 that only provides single-precision
1089 * registers. It is only used on ARMv7-M (i.e. on micro-controllers) which
1090 * are not supported by Android. Note that it is not compatible with VFPv2.
1091 *
1092 * NOTE: The term 'VFPv3' usually designate either VFPv3-D16 or VFPv3-D32
1093 *       depending on context. For example GCC uses it for VFPv3-D32, but
1094 *       the Linux kernel code uses it for VFPv3-D16 (especially in
1095 *       /proc/cpuinfo). Always try to use the full designation when
1096 *       possible.
1097 *
1098 * NEON, a.k.a. "ARM Advanced SIMD" is an extension that provides
1099 * instructions to perform parallel computations on vectors of 8, 16,
1100 * 32, 64 and 128 bit quantities. NEON requires VFPv32-D32 since all
1101 * NEON registers are also mapped to the same register banks.
1102 *
1103 * VFPv4-D16, adds a few instructions on top of VFPv3-D16 in order to
1104 * perform fused multiply-accumulate on VFP registers, as well as
1105 * half-precision (16-bit) conversion operations.
1106 *
1107 * VFPv4-D32 is VFPv4-D16 with 32, instead of 16, FPU double precision
1108 * registers.
1109 *
1110 * VPFv4-NEON is VFPv4-D32 with NEON instructions. It also adds fused
1111 * multiply-accumulate instructions that work on the NEON registers.
1112 *
1113 * NOTE: Similarly, "VFPv4" might either reference VFPv4-D16 or VFPv4-D32
1114 *       depending on context.
1115 *
1116 * The following information was determined by scanning the binutils-2.22
1117 * sources:
1118 *
1119 * Basic VFP instruction subsets:
1120 *
1121 * #define FPU_VFP_EXT_V1xD 0x08000000     // Base VFP instruction set.
1122 * #define FPU_VFP_EXT_V1   0x04000000     // Double-precision insns.
1123 * #define FPU_VFP_EXT_V2   0x02000000     // ARM10E VFPr1.
1124 * #define FPU_VFP_EXT_V3xD 0x01000000     // VFPv3 single-precision.
1125 * #define FPU_VFP_EXT_V3   0x00800000     // VFPv3 double-precision.
1126 * #define FPU_NEON_EXT_V1  0x00400000     // Neon (SIMD) insns.
1127 * #define FPU_VFP_EXT_D32  0x00200000     // Registers D16-D31.
1128 * #define FPU_VFP_EXT_FP16 0x00100000     // Half-precision extensions.
1129 * #define FPU_NEON_EXT_FMA 0x00080000     // Neon fused multiply-add
1130 * #define FPU_VFP_EXT_FMA  0x00040000     // VFP fused multiply-add
1131 *
1132 * FPU types (excluding NEON)
1133 *
1134 * FPU_VFP_V1xD (EXT_V1xD)
1135 *    |
1136 *    +--------------------------+
1137 *    |                          |
1138 * FPU_VFP_V1 (+EXT_V1)       FPU_VFP_V3xD (+EXT_V2+EXT_V3xD)
1139 *    |                          |
1140 *    |                          |
1141 * FPU_VFP_V2 (+EXT_V2)       FPU_VFP_V4_SP_D16 (+EXT_FP16+EXT_FMA)
1142 *    |
1143 * FPU_VFP_V3D16 (+EXT_Vx3D+EXT_V3)
1144 *    |
1145 *    +--------------------------+
1146 *    |                          |
1147 * FPU_VFP_V3 (+EXT_D32)     FPU_VFP_V4D16 (+EXT_FP16+EXT_FMA)
1148 *    |                          |
1149 *    |                      FPU_VFP_V4 (+EXT_D32)
1150 *    |
1151 * FPU_VFP_HARD (+EXT_FMA+NEON_EXT_FMA)
1152 *
1153 * VFP architectures:
1154 *
1155 * ARCH_VFP_V1xD  (EXT_V1xD)
1156 *   |
1157 *   +------------------+
1158 *   |                  |
1159 *   |             ARCH_VFP_V3xD (+EXT_V2+EXT_V3xD)
1160 *   |                  |
1161 *   |             ARCH_VFP_V3xD_FP16 (+EXT_FP16)
1162 *   |                  |
1163 *   |             ARCH_VFP_V4_SP_D16 (+EXT_FMA)
1164 *   |
1165 * ARCH_VFP_V1 (+EXT_V1)
1166 *   |
1167 * ARCH_VFP_V2 (+EXT_V2)
1168 *   |
1169 * ARCH_VFP_V3D16 (+EXT_V3xD+EXT_V3)
1170 *   |
1171 *   +-------------------+
1172 *   |                   |
1173 *   |         ARCH_VFP_V3D16_FP16  (+EXT_FP16)
1174 *   |
1175 *   +-------------------+
1176 *   |                   |
1177 *   |         ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
1178 *   |                   |
1179 *   |         ARCH_VFP_V4 (+EXT_D32)
1180 *   |                   |
1181 *   |         ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
1182 *   |
1183 * ARCH_VFP_V3 (+EXT_D32)
1184 *   |
1185 *   +-------------------+
1186 *   |                   |
1187 *   |         ARCH_VFP_V3_FP16 (+EXT_FP16)
1188 *   |
1189 * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
1190 *   |
1191 * ARCH_NEON_FP16 (+EXT_FP16)
1192 *
1193 * -fpu=<name> values and their correspondance with FPU architectures above:
1194 *
1195 *   {"vfp",               FPU_ARCH_VFP_V2},
1196 *   {"vfp9",              FPU_ARCH_VFP_V2},
1197 *   {"vfp3",              FPU_ARCH_VFP_V3}, // For backwards compatbility.
1198 *   {"vfp10",             FPU_ARCH_VFP_V2},
1199 *   {"vfp10-r0",          FPU_ARCH_VFP_V1},
1200 *   {"vfpxd",             FPU_ARCH_VFP_V1xD},
1201 *   {"vfpv2",             FPU_ARCH_VFP_V2},
1202 *   {"vfpv3",             FPU_ARCH_VFP_V3},
1203 *   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
1204 *   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
1205 *   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
1206 *   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
1207 *   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
1208 *   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
1209 *   {"neon-fp16",         FPU_ARCH_NEON_FP16},
1210 *   {"vfpv4",             FPU_ARCH_VFP_V4},
1211 *   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
1212 *   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
1213 *   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
1214 *
1215 *
1216 * Simplified diagram that only includes FPUs supported by Android:
1217 * Only ARCH_VFP_V3D16 is actually mandated by the armeabi-v7a ABI,
1218 * all others are optional and must be probed at runtime.
1219 *
1220 * ARCH_VFP_V3D16 (EXT_V1xD+EXT_V1+EXT_V2+EXT_V3xD+EXT_V3)
1221 *   |
1222 *   +-------------------+
1223 *   |                   |
1224 *   |         ARCH_VFP_V3D16_FP16  (+EXT_FP16)
1225 *   |
1226 *   +-------------------+
1227 *   |                   |
1228 *   |         ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
1229 *   |                   |
1230 *   |         ARCH_VFP_V4 (+EXT_D32)
1231 *   |                   |
1232 *   |         ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
1233 *   |
1234 * ARCH_VFP_V3 (+EXT_D32)
1235 *   |
1236 *   +-------------------+
1237 *   |                   |
1238 *   |         ARCH_VFP_V3_FP16 (+EXT_FP16)
1239 *   |
1240 * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
1241 *   |
1242 * ARCH_NEON_FP16 (+EXT_FP16)
1243 *
1244 */
1245
1246#endif // defined(__le32__) || defined(__le64__)
1247