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