Blame winpr/libwinpr/sysinfo/cpufeatures/cpu-features.c

Packit 1fb8d4
/*
Packit 1fb8d4
 * Copyright (C) 2010 The Android Open Source Project
Packit 1fb8d4
 * All rights reserved.
Packit 1fb8d4
 *
Packit 1fb8d4
 * Redistribution and use in source and binary forms, with or without
Packit 1fb8d4
 * modification, are permitted provided that the following conditions
Packit 1fb8d4
 * are met:
Packit 1fb8d4
 *  * Redistributions of source code must retain the above copyright
Packit 1fb8d4
 *    notice, this list of conditions and the following disclaimer.
Packit 1fb8d4
 *  * Redistributions in binary form must reproduce the above copyright
Packit 1fb8d4
 *    notice, this list of conditions and the following disclaimer in
Packit 1fb8d4
 *    the documentation and/or other materials provided with the
Packit 1fb8d4
 *    distribution.
Packit 1fb8d4
 *
Packit 1fb8d4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 1fb8d4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 1fb8d4
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit 1fb8d4
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit 1fb8d4
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 1fb8d4
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
Packit 1fb8d4
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Packit 1fb8d4
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
Packit 1fb8d4
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 1fb8d4
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
Packit 1fb8d4
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 1fb8d4
 * SUCH DAMAGE.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
/* ChangeLog for this library:
Packit 1fb8d4
 *
Packit 1fb8d4
 * NDK r10e?: Add MIPS MSA feature.
Packit 1fb8d4
 *
Packit 1fb8d4
 * NDK r10: Support for 64-bit CPUs (Intel, ARM & MIPS).
Packit 1fb8d4
 *
Packit 1fb8d4
 * NDK r8d: Add android_setCpu().
Packit 1fb8d4
 *
Packit 1fb8d4
 * NDK r8c: Add new ARM CPU features: VFPv2, VFP_D32, VFP_FP16,
Packit 1fb8d4
 *          VFP_FMA, NEON_FMA, IDIV_ARM, IDIV_THUMB2 and iWMMXt.
Packit 1fb8d4
 *
Packit 1fb8d4
 *          Rewrite the code to parse /proc/self/auxv instead of
Packit 1fb8d4
 *          the "Features" field in /proc/cpuinfo.
Packit 1fb8d4
 *
Packit 1fb8d4
 *          Dynamically allocate the buffer that hold the content
Packit 1fb8d4
 *          of /proc/cpuinfo to deal with newer hardware.
Packit 1fb8d4
 *
Packit 1fb8d4
 * NDK r7c: Fix CPU count computation. The old method only reported the
Packit 1fb8d4
 *           number of _active_ CPUs when the library was initialized,
Packit 1fb8d4
 *           which could be less than the real total.
Packit 1fb8d4
 *
Packit 1fb8d4
 * NDK r5: Handle buggy kernels which report a CPU Architecture number of 7
Packit 1fb8d4
 *         for an ARMv6 CPU (see below).
Packit 1fb8d4
 *
Packit 1fb8d4
 *         Handle kernels that only report 'neon', and not 'vfpv3'
Packit 1fb8d4
 *         (VFPv3 is mandated by the ARM architecture is Neon is implemented)
Packit 1fb8d4
 *
Packit 1fb8d4
 *         Handle kernels that only report 'vfpv3d16', and not 'vfpv3'
Packit 1fb8d4
 *
Packit 1fb8d4
 *         Fix x86 compilation. Report ANDROID_CPU_FAMILY_X86 in
Packit 1fb8d4
 *         android_getCpuFamily().
Packit 1fb8d4
 *
Packit 1fb8d4
 * NDK r4: Initial release
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#include "cpu-features.h"
Packit 1fb8d4
Packit 1fb8d4
#include <dlfcn.h>
Packit 1fb8d4
#include <errno.h>
Packit 1fb8d4
#include <fcntl.h>
Packit 1fb8d4
#include <pthread.h>
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
#include <sys/system_properties.h>
Packit 1fb8d4
#include <unistd.h>
Packit 1fb8d4
#include <winpr/wtypes.h>
Packit 1fb8d4
Packit Service 5a9772
static pthread_once_t g_once;
Packit Service 5a9772
static int g_inited;
Packit Service 5a9772
static AndroidCpuFamily g_cpuFamily;
Packit Service 5a9772
static uint64_t g_cpuFeatures;
Packit Service 5a9772
static int g_cpuCount;
Packit 1fb8d4
Packit 1fb8d4
#ifdef __arm__
Packit Service 5a9772
static uint32_t g_cpuIdArm;
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
static const int android_cpufeatures_debug = 0;
Packit 1fb8d4
Packit Service 5a9772
#define D(...)                         \
Packit Service 5a9772
	do                                 \
Packit Service 5a9772
	{                                  \
Packit Service 5a9772
		if (android_cpufeatures_debug) \
Packit Service 5a9772
		{                              \
Packit Service 5a9772
			printf(__VA_ARGS__);       \
Packit Service 5a9772
			fflush(stdout);            \
Packit Service 5a9772
		}                              \
Packit 1fb8d4
	} while (0)
Packit 1fb8d4
Packit 1fb8d4
#ifdef __i386__
Packit 1fb8d4
static __inline__ void x86_cpuid(int func, int values[4])
Packit 1fb8d4
{
Packit 1fb8d4
	int a, b, c, d;
Packit 1fb8d4
	/* We need to preserve ebx since we're compiling PIC code */
Packit 1fb8d4
	/* this means we can't use "=b" for the second output register */
Packit Service 5a9772
	__asm__ __volatile__("push %%ebx\n"
Packit Service 5a9772
	                     "cpuid\n"
Packit 1fb8d4
	                     "mov %%ebx, %1\n"
Packit 1fb8d4
	                     "pop %%ebx\n"
Packit Service 5a9772
	                     : "=a"(a), "=r"(b), "=c"(c), "=d"(d)
Packit Service 5a9772
	                     : "a"(func));
Packit 1fb8d4
	values[0] = a;
Packit 1fb8d4
	values[1] = b;
Packit 1fb8d4
	values[2] = c;
Packit 1fb8d4
	values[3] = d;
Packit 1fb8d4
}
Packit 1fb8d4
#elif defined(__x86_64__)
Packit 1fb8d4
static __inline__ void x86_cpuid(int func, int values[4])
Packit 1fb8d4
{
Packit 1fb8d4
	int64_t a, b, c, d;
Packit 1fb8d4
	/* We need to preserve ebx since we're compiling PIC code */
Packit 1fb8d4
	/* this means we can't use "=b" for the second output register */
Packit Service 5a9772
	__asm__ __volatile__("push %%rbx\n"
Packit Service 5a9772
	                     "cpuid\n"
Packit 1fb8d4
	                     "mov %%rbx, %1\n"
Packit 1fb8d4
	                     "pop %%rbx\n"
Packit Service 5a9772
	                     : "=a"(a), "=r"(b), "=c"(c), "=d"(d)
Packit Service 5a9772
	                     : "a"(func));
Packit 1fb8d4
	values[0] = a;
Packit 1fb8d4
	values[1] = b;
Packit 1fb8d4
	values[2] = c;
Packit 1fb8d4
	values[3] = d;
Packit 1fb8d4
}
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
/* Get the size of a file by reading it until the end. This is needed
Packit 1fb8d4
 * because files under /proc do not always return a valid size when
Packit 1fb8d4
 * using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed.
Packit 1fb8d4
 */
Packit Service 5a9772
static int get_file_size(const char* pathname)
Packit 1fb8d4
{
Packit 1fb8d4
	int fd, result = 0;
Packit 1fb8d4
	char buffer[256];
Packit 1fb8d4
	fd = open(pathname, O_RDONLY);
Packit 1fb8d4
Packit 1fb8d4
	if (fd < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("Can't open %s: %s\n", pathname, strerror(errno));
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (;;)
Packit 1fb8d4
	{
Packit 1fb8d4
		int ret = read(fd, buffer, sizeof buffer);
Packit 1fb8d4
Packit 1fb8d4
		if (ret < 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (errno == EINTR)
Packit 1fb8d4
				continue;
Packit 1fb8d4
Packit 1fb8d4
			D("Error while reading %s: %s\n", pathname, strerror(errno));
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (ret == 0)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		result += ret;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	close(fd);
Packit 1fb8d4
	return result;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* Read the content of /proc/cpuinfo into a user-provided buffer.
Packit 1fb8d4
 * Return the length of the data, or -1 on error. Does *not*
Packit 1fb8d4
 * zero-terminate the content. Will not read more
Packit 1fb8d4
 * than 'buffsize' bytes.
Packit 1fb8d4
 */
Packit Service 5a9772
static int read_file(const char* pathname, char* buffer, size_t buffsize)
Packit 1fb8d4
{
Packit Service 5a9772
	int fd, count;
Packit 1fb8d4
	fd = open(pathname, O_RDONLY);
Packit 1fb8d4
Packit 1fb8d4
	if (fd < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("Could not open %s: %s\n", pathname, strerror(errno));
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	count = 0;
Packit 1fb8d4
Packit 1fb8d4
	while (count < (int)buffsize)
Packit 1fb8d4
	{
Packit 1fb8d4
		int ret = read(fd, buffer + count, buffsize - count);
Packit 1fb8d4
Packit 1fb8d4
		if (ret < 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (errno == EINTR)
Packit 1fb8d4
				continue;
Packit 1fb8d4
Packit 1fb8d4
			D("Error while reading from %s: %s\n", pathname, strerror(errno));
Packit 1fb8d4
Packit 1fb8d4
			if (count == 0)
Packit 1fb8d4
				count = -1;
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (ret == 0)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		count += ret;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	close(fd);
Packit 1fb8d4
	return count;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#ifdef __arm__
Packit 1fb8d4
/* Extract the content of a the first occurence of a given field in
Packit 1fb8d4
 * the content of /proc/cpuinfo and return it as a heap-allocated
Packit 1fb8d4
 * string that must be freed by the caller.
Packit 1fb8d4
 *
Packit 1fb8d4
 * Return NULL if not found
Packit 1fb8d4
 */
Packit Service 5a9772
static char* extract_cpuinfo_field(const char* buffer, int buflen, const char* field)
Packit 1fb8d4
{
Packit Service 5a9772
	int fieldlen = strlen(field);
Packit 1fb8d4
	const char* bufend = buffer + buflen;
Packit 1fb8d4
	char* result = NULL;
Packit 1fb8d4
	int len;
Packit Service 5a9772
	const char *p, *q;
Packit 1fb8d4
	/* Look for first field occurence, and ensures it starts the line. */
Packit 1fb8d4
	p = buffer;
Packit 1fb8d4
Packit 1fb8d4
	for (;;)
Packit 1fb8d4
	{
Packit 1fb8d4
		p = memmem(p, bufend - p, field, fieldlen);
Packit 1fb8d4
Packit 1fb8d4
		if (p == NULL)
Packit 1fb8d4
			goto EXIT;
Packit 1fb8d4
Packit 1fb8d4
		if (p == buffer || p[-1] == '\n')
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		p += fieldlen;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Skip to the first column followed by a space */
Packit 1fb8d4
	p += fieldlen;
Packit Service 5a9772
	p = memchr(p, ':', bufend - p);
Packit 1fb8d4
Packit 1fb8d4
	if (p == NULL || p[1] != ' ')
Packit 1fb8d4
		goto EXIT;
Packit 1fb8d4
Packit 1fb8d4
	/* Find the end of the line */
Packit 1fb8d4
	p += 2;
Packit 1fb8d4
	q = memchr(p, '\n', bufend - p);
Packit 1fb8d4
Packit 1fb8d4
	if (q == NULL)
Packit 1fb8d4
		q = bufend;
Packit 1fb8d4
Packit 1fb8d4
	/* Copy the line into a heap-allocated buffer */
Packit 1fb8d4
	len = q - p;
Packit 1fb8d4
	result = malloc(len + 1);
Packit 1fb8d4
Packit 1fb8d4
	if (result == NULL)
Packit 1fb8d4
		goto EXIT;
Packit 1fb8d4
Packit 1fb8d4
	memcpy(result, p, len);
Packit 1fb8d4
	result[len] = '\0';
Packit 1fb8d4
EXIT:
Packit 1fb8d4
	return result;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* Checks that a space-separated list of items contains one given 'item'.
Packit 1fb8d4
 * Returns 1 if found, 0 otherwise.
Packit 1fb8d4
 */
Packit Service 5a9772
static int has_list_item(const char* list, const char* item)
Packit 1fb8d4
{
Packit Service 5a9772
	const char* p = list;
Packit 1fb8d4
	int itemlen = strlen(item);
Packit 1fb8d4
Packit 1fb8d4
	if (list == NULL)
Packit 1fb8d4
		return 0;
Packit 1fb8d4
Packit 1fb8d4
	while (*p)
Packit 1fb8d4
	{
Packit Service 5a9772
		const char* q;
Packit 1fb8d4
Packit 1fb8d4
		/* skip spaces */
Packit 1fb8d4
		while (*p == ' ' || *p == '\t')
Packit 1fb8d4
			p++;
Packit 1fb8d4
Packit 1fb8d4
		/* find end of current list item */
Packit 1fb8d4
		q = p;
Packit 1fb8d4
Packit 1fb8d4
		while (*q && *q != ' ' && *q != '\t')
Packit 1fb8d4
			q++;
Packit 1fb8d4
Packit 1fb8d4
		if (itemlen == q - p && !memcmp(p, item, itemlen))
Packit 1fb8d4
			return 1;
Packit 1fb8d4
Packit 1fb8d4
		/* skip to next item */
Packit 1fb8d4
		p = q;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
#endif /* __arm__ */
Packit 1fb8d4
Packit 1fb8d4
/* Parse a number starting from 'input', but not going further
Packit 1fb8d4
 * than 'limit'. Return the value into '*result'.
Packit 1fb8d4
 *
Packit 1fb8d4
 * NOTE: Does not skip over leading spaces, or deal with sign characters.
Packit 1fb8d4
 * NOTE: Ignores overflows.
Packit 1fb8d4
 *
Packit 1fb8d4
 * The function returns NULL in case of error (bad format), or the new
Packit 1fb8d4
 * position after the decimal number in case of success (which will always
Packit 1fb8d4
 * be <= 'limit').
Packit 1fb8d4
 */
Packit Service 5a9772
static const char* parse_number(const char* input, const char* limit, int base, int* result)
Packit 1fb8d4
{
Packit 1fb8d4
	const char* p = input;
Packit 1fb8d4
	int val = 0;
Packit 1fb8d4
Packit 1fb8d4
	while (p < limit)
Packit 1fb8d4
	{
Packit 1fb8d4
		int d = (*p - '0');
Packit 1fb8d4
Packit 1fb8d4
		if ((unsigned)d >= 10U)
Packit 1fb8d4
		{
Packit 1fb8d4
			d = (*p - 'a');
Packit 1fb8d4
Packit 1fb8d4
			if ((unsigned)d >= 6U)
Packit 1fb8d4
				d = (*p - 'A');
Packit 1fb8d4
Packit 1fb8d4
			if ((unsigned)d >= 6U)
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			d += 10;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (d >= base)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		val = val * base + d;
Packit 1fb8d4
		p++;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (p == input)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	*result = val;
Packit 1fb8d4
	return p;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static const char* parse_decimal(const char* input, const char* limit, int* result)
Packit 1fb8d4
{
Packit 1fb8d4
	return parse_number(input, limit, 10, result);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#ifdef __arm__
Packit Service 5a9772
static const char* parse_hexadecimal(const char* input, const char* limit, int* result)
Packit 1fb8d4
{
Packit 1fb8d4
	return parse_number(input, limit, 16, result);
Packit 1fb8d4
}
Packit 1fb8d4
#endif /* __arm__ */
Packit 1fb8d4
Packit 1fb8d4
/* This small data type is used to represent a CPU list / mask, as read
Packit 1fb8d4
 * from sysfs on Linux. See http://www.kernel.org/doc/Documentation/cputopology.txt
Packit 1fb8d4
 *
Packit 1fb8d4
 * For now, we don't expect more than 32 cores on mobile devices, so keep
Packit 1fb8d4
 * everything simple.
Packit 1fb8d4
 */
Packit 1fb8d4
typedef struct
Packit 1fb8d4
{
Packit 1fb8d4
	uint32_t mask;
Packit 1fb8d4
} CpuList;
Packit 1fb8d4
Packit Service 5a9772
static __inline__ void cpulist_init(CpuList* list)
Packit 1fb8d4
{
Packit 1fb8d4
	list->mask = 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static __inline__ void cpulist_and(CpuList* list1, CpuList* list2)
Packit 1fb8d4
{
Packit 1fb8d4
	list1->mask &= list2->mask;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static __inline__ void cpulist_set(CpuList* list, int index)
Packit 1fb8d4
{
Packit 1fb8d4
	if ((unsigned)index < 32)
Packit 1fb8d4
	{
Packit 1fb8d4
		list->mask |= (uint32_t)(1U << index);
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static __inline__ int cpulist_count(CpuList* list)
Packit 1fb8d4
{
Packit 1fb8d4
	return __builtin_popcount(list->mask);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* Parse a textual list of cpus and store the result inside a CpuList object.
Packit 1fb8d4
 * Input format is the following:
Packit 1fb8d4
 * - comma-separated list of items (no spaces)
Packit 1fb8d4
 * - each item is either a single decimal number (cpu index), or a range made
Packit 1fb8d4
 *   of two numbers separated by a single dash (-). Ranges are inclusive.
Packit 1fb8d4
 *
Packit 1fb8d4
 * Examples:   0
Packit 1fb8d4
 *             2,4-127,128-143
Packit 1fb8d4
 *             0-1
Packit 1fb8d4
 */
Packit Service 5a9772
static void cpulist_parse(CpuList* list, const char* line, int line_len)
Packit 1fb8d4
{
Packit 1fb8d4
	const char* p = line;
Packit 1fb8d4
	const char* end = p + line_len;
Packit 1fb8d4
	const char* q;
Packit 1fb8d4
Packit 1fb8d4
	/* NOTE: the input line coming from sysfs typically contains a
Packit 1fb8d4
	 * trailing newline, so take care of it in the code below
Packit 1fb8d4
	 */
Packit 1fb8d4
	while (p < end && *p != '\n')
Packit 1fb8d4
	{
Packit 1fb8d4
		int val, start_value, end_value;
Packit 1fb8d4
		/* Find the end of current item, and put it into 'q' */
Packit 1fb8d4
		q = memchr(p, ',', end - p);
Packit 1fb8d4
Packit 1fb8d4
		if (q == NULL)
Packit 1fb8d4
		{
Packit 1fb8d4
			q = end;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Get first value */
Packit 1fb8d4
		p = parse_decimal(p, q, &start_value);
Packit 1fb8d4
Packit 1fb8d4
		if (p == NULL)
Packit 1fb8d4
			goto BAD_FORMAT;
Packit 1fb8d4
Packit 1fb8d4
		end_value = start_value;
Packit 1fb8d4
Packit 1fb8d4
		/* If we're not at the end of the item, expect a dash and
Packit 1fb8d4
		 * and integer; extract end value.
Packit 1fb8d4
		 */
Packit 1fb8d4
		if (p < q && *p == '-')
Packit 1fb8d4
		{
Packit 1fb8d4
			p = parse_decimal(p + 1, q, &end_value);
Packit 1fb8d4
Packit 1fb8d4
			if (p == NULL)
Packit 1fb8d4
				goto BAD_FORMAT;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Set bits CPU list bits */
Packit 1fb8d4
		for (val = start_value; val <= end_value; val++)
Packit 1fb8d4
		{
Packit 1fb8d4
			cpulist_set(list, val);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Jump to next item */
Packit 1fb8d4
		p = q;
Packit 1fb8d4
Packit 1fb8d4
		if (p < end)
Packit 1fb8d4
			p++;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
BAD_FORMAT:;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* Read a CPU list from one sysfs file */
Packit Service 5a9772
static void cpulist_read_from(CpuList* list, const char* filename)
Packit 1fb8d4
{
Packit Service 5a9772
	char file[64];
Packit Service 5a9772
	int filelen;
Packit 1fb8d4
	cpulist_init(list);
Packit 1fb8d4
	filelen = read_file(filename, file, sizeof file);
Packit 1fb8d4
Packit 1fb8d4
	if (filelen < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("Could not read %s: %s\n", filename, strerror(errno));
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cpulist_parse(list, file, filelen);
Packit 1fb8d4
}
Packit 1fb8d4
#if defined(__aarch64__)
Packit 1fb8d4
// see <uapi/asm/hwcap.h> kernel header
Packit Service 5a9772
#define HWCAP_FP (1 << 0)
Packit Service 5a9772
#define HWCAP_ASIMD (1 << 1)
Packit Service 5a9772
#define HWCAP_AES (1 << 3)
Packit Service 5a9772
#define HWCAP_PMULL (1 << 4)
Packit Service 5a9772
#define HWCAP_SHA1 (1 << 5)
Packit Service 5a9772
#define HWCAP_SHA2 (1 << 6)
Packit Service 5a9772
#define HWCAP_CRC32 (1 << 7)
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#if defined(__arm__)
Packit 1fb8d4
Packit 1fb8d4
// See <asm/hwcap.h> kernel header.
Packit Service 5a9772
#define HWCAP_VFP (1 << 6)
Packit Service 5a9772
#define HWCAP_IWMMXT (1 << 9)
Packit Service 5a9772
#define HWCAP_NEON (1 << 12)
Packit Service 5a9772
#define HWCAP_VFPv3 (1 << 13)
Packit Service 5a9772
#define HWCAP_VFPv3D16 (1 << 14)
Packit Service 5a9772
#define HWCAP_VFPv4 (1 << 16)
Packit Service 5a9772
#define HWCAP_IDIVA (1 << 17)
Packit Service 5a9772
#define HWCAP_IDIVT (1 << 18)
Packit 1fb8d4
Packit 1fb8d4
// see <uapi/asm/hwcap.h> kernel header
Packit Service 5a9772
#define HWCAP2_AES (1 << 0)
Packit Service 5a9772
#define HWCAP2_PMULL (1 << 1)
Packit Service 5a9772
#define HWCAP2_SHA1 (1 << 2)
Packit Service 5a9772
#define HWCAP2_SHA2 (1 << 3)
Packit Service 5a9772
#define HWCAP2_CRC32 (1 << 4)
Packit 1fb8d4
Packit 1fb8d4
// This is the list of 32-bit ARMv7 optional features that are _always_
Packit 1fb8d4
// supported by ARMv8 CPUs, as mandated by the ARM Architecture Reference
Packit 1fb8d4
// Manual.
Packit Service 5a9772
#define HWCAP_SET_FOR_ARMV8 \
Packit Service 5a9772
	(HWCAP_VFP | HWCAP_NEON | HWCAP_VFPv3 | HWCAP_VFPv4 | HWCAP_IDIVA | HWCAP_IDIVT)
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#if defined(__mips__)
Packit 1fb8d4
// see <uapi/asm/hwcap.h> kernel header
Packit Service 5a9772
#define HWCAP_MIPS_R6 (1 << 0)
Packit Service 5a9772
#define HWCAP_MIPS_MSA (1 << 1)
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#if defined(__arm__) || defined(__aarch64__) || defined(__mips__)
Packit 1fb8d4
Packit 1fb8d4
#define AT_HWCAP 16
Packit 1fb8d4
#define AT_HWCAP2 26
Packit 1fb8d4
Packit 1fb8d4
// Probe the system's C library for a 'getauxval' function and call it if
Packit 1fb8d4
// it exits, or return 0 for failure. This function is available since API
Packit 1fb8d4
// level 20.
Packit 1fb8d4
//
Packit 1fb8d4
// This code does *NOT* check for '__ANDROID_API__ >= 20' to support the
Packit 1fb8d4
// edge case where some NDK developers use headers for a platform that is
Packit 1fb8d4
// newer than the one really targetted by their application.
Packit 1fb8d4
// This is typically done to use newer native APIs only when running on more
Packit 1fb8d4
// recent Android versions, and requires careful symbol management.
Packit 1fb8d4
//
Packit 1fb8d4
// Note that getauxval() can't really be re-implemented here, because
Packit 1fb8d4
// its implementation does not parse /proc/self/auxv. Instead it depends
Packit 1fb8d4
// on values  that are passed by the kernel at process-init time to the
Packit 1fb8d4
// C runtime initialization layer.
Packit Service 5a9772
static uint32_t get_elf_hwcap_from_getauxval(int hwcap_type)
Packit 1fb8d4
{
Packit 1fb8d4
	typedef unsigned long getauxval_func_t(unsigned long);
Packit 1fb8d4
	dlerror();
Packit 1fb8d4
	void* libc_handle = dlopen("libc.so", RTLD_NOW);
Packit 1fb8d4
Packit 1fb8d4
	if (!libc_handle)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("Could not dlopen() C library: %s\n", dlerror());
Packit 1fb8d4
		return 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	uint32_t ret = 0;
Packit Service 5a9772
	getauxval_func_t* func = (getauxval_func_t*)dlsym(libc_handle, "getauxval");
Packit 1fb8d4
Packit 1fb8d4
	if (!func)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("Could not find getauxval() in C library\n");
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		// Note: getauxval() returns 0 on failure. Doesn't touch errno.
Packit 1fb8d4
		ret = (uint32_t)(*func)(hwcap_type);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	dlclose(libc_handle);
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#if defined(__arm__)
Packit 1fb8d4
// Parse /proc/self/auxv to extract the ELF HW capabilities bitmap for the
Packit 1fb8d4
// current CPU. Note that this file is not accessible from regular
Packit 1fb8d4
// application processes on some Android platform releases.
Packit 1fb8d4
// On success, return new ELF hwcaps, or 0 on failure.
Packit Service 5a9772
static uint32_t get_elf_hwcap_from_proc_self_auxv(void)
Packit 1fb8d4
{
Packit 1fb8d4
	const char filepath[] = "/proc/self/auxv";
Packit 1fb8d4
	int fd = TEMP_FAILURE_RETRY(open(filepath, O_RDONLY));
Packit 1fb8d4
Packit 1fb8d4
	if (fd < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("Could not open %s: %s\n", filepath, strerror(errno));
Packit 1fb8d4
		return 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	struct
Packit 1fb8d4
	{
Packit 1fb8d4
		uint32_t tag;
Packit 1fb8d4
		uint32_t value;
Packit 1fb8d4
	} entry;
Packit 1fb8d4
Packit 1fb8d4
	uint32_t result = 0;
Packit 1fb8d4
Packit 1fb8d4
	for (;;)
Packit 1fb8d4
	{
Packit 1fb8d4
		int ret = TEMP_FAILURE_RETRY(read(fd, (char*)&entry, sizeof entry));
Packit 1fb8d4
Packit 1fb8d4
		if (ret < 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			D("Error while reading %s: %s\n", filepath, strerror(errno));
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		// Detect end of list.
Packit 1fb8d4
		if (ret == 0 || (entry.tag == 0 && entry.value == 0))
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (entry.tag == AT_HWCAP)
Packit 1fb8d4
		{
Packit 1fb8d4
			result = entry.value;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	close(fd);
Packit 1fb8d4
	return result;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* Compute the ELF HWCAP flags from the content of /proc/cpuinfo.
Packit 1fb8d4
 * This works by parsing the 'Features' line, which lists which optional
Packit 1fb8d4
 * features the device's CPU supports, on top of its reference
Packit 1fb8d4
 * architecture.
Packit 1fb8d4
 */
Packit Service 5a9772
static uint32_t get_elf_hwcap_from_proc_cpuinfo(const char* cpuinfo, int cpuinfo_len)
Packit 1fb8d4
{
Packit 1fb8d4
	uint32_t hwcaps = 0;
Packit 1fb8d4
	long architecture = 0;
Packit 1fb8d4
	char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
Packit 1fb8d4
Packit 1fb8d4
	if (cpuArch)
Packit 1fb8d4
	{
Packit 1fb8d4
		architecture = strtol(cpuArch, NULL, 10);
Packit 1fb8d4
		free(cpuArch);
Packit 1fb8d4
Packit 1fb8d4
		if (architecture >= 8L)
Packit 1fb8d4
		{
Packit 1fb8d4
			// This is a 32-bit ARM binary running on a 64-bit ARM64 kernel.
Packit 1fb8d4
			// The 'Features' line only lists the optional features that the
Packit 1fb8d4
			// device's CPU supports, compared to its reference architecture
Packit 1fb8d4
			// which are of no use for this process.
Packit 1fb8d4
			D("Faking 32-bit ARM HWCaps on ARMv%ld CPU\n", architecture);
Packit 1fb8d4
			return HWCAP_SET_FOR_ARMV8;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features");
Packit 1fb8d4
Packit 1fb8d4
	if (cpuFeatures != NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("Found cpuFeatures = '%s'\n", cpuFeatures);
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "vfp"))
Packit 1fb8d4
			hwcaps |= HWCAP_VFP;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "vfpv3"))
Packit 1fb8d4
			hwcaps |= HWCAP_VFPv3;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "vfpv3d16"))
Packit 1fb8d4
			hwcaps |= HWCAP_VFPv3D16;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "vfpv4"))
Packit 1fb8d4
			hwcaps |= HWCAP_VFPv4;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "neon"))
Packit 1fb8d4
			hwcaps |= HWCAP_NEON;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "idiva"))
Packit 1fb8d4
			hwcaps |= HWCAP_IDIVA;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "idivt"))
Packit 1fb8d4
			hwcaps |= HWCAP_IDIVT;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "idiv"))
Packit 1fb8d4
			hwcaps |= HWCAP_IDIVA | HWCAP_IDIVT;
Packit 1fb8d4
Packit 1fb8d4
		if (has_list_item(cpuFeatures, "iwmmxt"))
Packit 1fb8d4
			hwcaps |= HWCAP_IWMMXT;
Packit 1fb8d4
Packit 1fb8d4
		free(cpuFeatures);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return hwcaps;
Packit 1fb8d4
}
Packit Service 5a9772
#endif /* __arm__ */
Packit 1fb8d4
Packit 1fb8d4
/* Return the number of cpus present on a given device.
Packit 1fb8d4
 *
Packit 1fb8d4
 * To handle all weird kernel configurations, we need to compute the
Packit 1fb8d4
 * intersection of the 'present' and 'possible' CPU lists and count
Packit 1fb8d4
 * the result.
Packit 1fb8d4
 */
Packit Service 5a9772
static int get_cpu_count(void)
Packit 1fb8d4
{
Packit 1fb8d4
	CpuList cpus_present[1];
Packit 1fb8d4
	CpuList cpus_possible[1];
Packit 1fb8d4
	cpulist_read_from(cpus_present, "/sys/devices/system/cpu/present");
Packit 1fb8d4
	cpulist_read_from(cpus_possible, "/sys/devices/system/cpu/possible");
Packit 1fb8d4
	/* Compute the intersection of both sets to get the actual number of
Packit 1fb8d4
	 * CPU cores that can be used on this device by the kernel.
Packit 1fb8d4
	 */
Packit 1fb8d4
	cpulist_and(cpus_present, cpus_possible);
Packit 1fb8d4
	return cpulist_count(cpus_present);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void android_cpuInitFamily(void)
Packit 1fb8d4
{
Packit 1fb8d4
#if defined(__arm__)
Packit 1fb8d4
	g_cpuFamily = ANDROID_CPU_FAMILY_ARM;
Packit 1fb8d4
#elif defined(__i386__)
Packit 1fb8d4
	g_cpuFamily = ANDROID_CPU_FAMILY_X86;
Packit 1fb8d4
#elif defined(__mips64)
Packit 1fb8d4
	/* Needs to be before __mips__ since the compiler defines both */
Packit 1fb8d4
	g_cpuFamily = ANDROID_CPU_FAMILY_MIPS64;
Packit 1fb8d4
#elif defined(__mips__)
Packit 1fb8d4
	g_cpuFamily = ANDROID_CPU_FAMILY_MIPS;
Packit 1fb8d4
#elif defined(__aarch64__)
Packit 1fb8d4
	g_cpuFamily = ANDROID_CPU_FAMILY_ARM64;
Packit 1fb8d4
#elif defined(__x86_64__)
Packit 1fb8d4
	g_cpuFamily = ANDROID_CPU_FAMILY_X86_64;
Packit 1fb8d4
#else
Packit 1fb8d4
	g_cpuFamily = ANDROID_CPU_FAMILY_UNKNOWN;
Packit 1fb8d4
#endif
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void android_cpuInit(void)
Packit 1fb8d4
{
Packit 1fb8d4
	char* cpuinfo = NULL;
Packit Service 5a9772
	int cpuinfo_len;
Packit 1fb8d4
	android_cpuInitFamily();
Packit 1fb8d4
	g_cpuFeatures = 0;
Packit Service 5a9772
	g_cpuCount = 1;
Packit Service 5a9772
	g_inited = 1;
Packit 1fb8d4
	cpuinfo_len = get_file_size("/proc/cpuinfo");
Packit 1fb8d4
Packit 1fb8d4
	if (cpuinfo_len < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("cpuinfo_len cannot be computed!");
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cpuinfo = malloc(cpuinfo_len);
Packit 1fb8d4
Packit 1fb8d4
	if (cpuinfo == NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		D("cpuinfo buffer could not be allocated");
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, cpuinfo_len);
Packit Service 5a9772
	D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len, cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo);
Packit 1fb8d4
Packit Service 5a9772
	if (cpuinfo_len < 0) /* should not happen */
Packit 1fb8d4
	{
Packit 1fb8d4
		free(cpuinfo);
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Count the CPU cores, the value may be 0 for single-core CPUs */
Packit 1fb8d4
	g_cpuCount = get_cpu_count();
Packit 1fb8d4
Packit 1fb8d4
	if (g_cpuCount == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuCount = 1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	D("found cpuCount = %d\n", g_cpuCount);
Packit 1fb8d4
#ifdef __arm__
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Extract architecture from the "CPU Architecture" field.
Packit 1fb8d4
		 * The list is well-known, unlike the the output of
Packit 1fb8d4
		 * the 'Processor' field which can vary greatly.
Packit 1fb8d4
		 *
Packit 1fb8d4
		 * See the definition of the 'proc_arch' array in
Packit 1fb8d4
		 * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
Packit 1fb8d4
		 * same file.
Packit 1fb8d4
		 */
Packit 1fb8d4
		char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
Packit 1fb8d4
Packit 1fb8d4
		if (cpuArch != NULL)
Packit 1fb8d4
		{
Packit Service 5a9772
			char* end;
Packit Service 5a9772
			long archNumber;
Packit Service 5a9772
			int hasARMv7 = 0;
Packit 1fb8d4
			D("found cpuArch = '%s'\n", cpuArch);
Packit 1fb8d4
			/* read the initial decimal number, ignore the rest */
Packit 1fb8d4
			archNumber = strtol(cpuArch, &end, 10);
Packit 1fb8d4
Packit 1fb8d4
			/* Note that ARMv8 is upwards compatible with ARMv7. */
Packit 1fb8d4
			if (end > cpuArch && archNumber >= 7)
Packit 1fb8d4
			{
Packit 1fb8d4
				hasARMv7 = 1;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			/* Unfortunately, it seems that certain ARMv6-based CPUs
Packit 1fb8d4
			 * report an incorrect architecture number of 7!
Packit 1fb8d4
			 *
Packit 1fb8d4
			 * See http://code.google.com/p/android/issues/detail?id=10812
Packit 1fb8d4
			 *
Packit 1fb8d4
			 * We try to correct this by looking at the 'elf_format'
Packit 1fb8d4
			 * field reported by the 'Processor' field, which is of the
Packit 1fb8d4
			 * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
Packit 1fb8d4
			 * an ARMv6-one.
Packit 1fb8d4
			 */
Packit 1fb8d4
			if (hasARMv7)
Packit 1fb8d4
			{
Packit Service 5a9772
				char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Processor");
Packit 1fb8d4
Packit 1fb8d4
				if (cpuProc != NULL)
Packit 1fb8d4
				{
Packit 1fb8d4
					D("found cpuProc = '%s'\n", cpuProc);
Packit 1fb8d4
Packit 1fb8d4
					if (has_list_item(cpuProc, "(v6l)"))
Packit 1fb8d4
					{
Packit 1fb8d4
						D("CPU processor and architecture mismatch!!\n");
Packit 1fb8d4
						hasARMv7 = 0;
Packit 1fb8d4
					}
Packit 1fb8d4
Packit 1fb8d4
					free(cpuProc);
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			if (hasARMv7)
Packit 1fb8d4
			{
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_ARMv7;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			/* The LDREX / STREX instructions are available from ARMv6 */
Packit 1fb8d4
			if (archNumber >= 6)
Packit 1fb8d4
			{
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_LDREX_STREX;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			free(cpuArch);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Extract the list of CPU features from ELF hwcaps */
Packit 1fb8d4
		uint32_t hwcaps = 0;
Packit 1fb8d4
		hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
Packit 1fb8d4
Packit 1fb8d4
		if (!hwcaps)
Packit 1fb8d4
		{
Packit 1fb8d4
			D("Parsing /proc/self/auxv to extract ELF hwcaps!\n");
Packit 1fb8d4
			hwcaps = get_elf_hwcap_from_proc_self_auxv();
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!hwcaps)
Packit 1fb8d4
		{
Packit 1fb8d4
			// Parsing /proc/self/auxv will fail from regular application
Packit 1fb8d4
			// processes on some Android platform versions, when this happens
Packit 1fb8d4
			// parse proc/cpuinfo instead.
Packit 1fb8d4
			D("Parsing /proc/cpuinfo to extract ELF hwcaps!\n");
Packit 1fb8d4
			hwcaps = get_elf_hwcap_from_proc_cpuinfo(cpuinfo, cpuinfo_len);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (hwcaps != 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			int has_vfp = (hwcaps & HWCAP_VFP);
Packit 1fb8d4
			int has_vfpv3 = (hwcaps & HWCAP_VFPv3);
Packit 1fb8d4
			int has_vfpv3d16 = (hwcaps & HWCAP_VFPv3D16);
Packit 1fb8d4
			int has_vfpv4 = (hwcaps & HWCAP_VFPv4);
Packit 1fb8d4
			int has_neon = (hwcaps & HWCAP_NEON);
Packit 1fb8d4
			int has_idiva = (hwcaps & HWCAP_IDIVA);
Packit 1fb8d4
			int has_idivt = (hwcaps & HWCAP_IDIVT);
Packit 1fb8d4
			int has_iwmmxt = (hwcaps & HWCAP_IWMMXT);
Packit 1fb8d4
Packit 1fb8d4
			// The kernel does a poor job at ensuring consistency when
Packit 1fb8d4
			// describing CPU features. So lots of guessing is needed.
Packit 1fb8d4
Packit 1fb8d4
			// 'vfpv4' implies VFPv3|VFP_FMA|FP16
Packit 1fb8d4
			if (has_vfpv4)
Packit Service 5a9772
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 | ANDROID_CPU_ARM_FEATURE_VFP_FP16 |
Packit 1fb8d4
				                 ANDROID_CPU_ARM_FEATURE_VFP_FMA;
Packit 1fb8d4
Packit 1fb8d4
			// 'vfpv3' or 'vfpv3d16' imply VFPv3. Note that unlike GCC,
Packit 1fb8d4
			// a value of 'vfpv3' doesn't necessarily mean that the D32
Packit 1fb8d4
			// feature is present, so be conservative. All CPUs in the
Packit 1fb8d4
			// field that support D32 also support NEON, so this should
Packit 1fb8d4
			// not be a problem in practice.
Packit 1fb8d4
			if (has_vfpv3 || has_vfpv3d16)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
Packit 1fb8d4
Packit 1fb8d4
			// 'vfp' is super ambiguous. Depending on the kernel, it can
Packit 1fb8d4
			// either mean VFPv2 or VFPv3. Make it depend on ARMv7.
Packit 1fb8d4
			if (has_vfp)
Packit 1fb8d4
			{
Packit 1fb8d4
				if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_ARMv7)
Packit 1fb8d4
					g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
Packit 1fb8d4
				else
Packit 1fb8d4
					g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			// Neon implies VFPv3|D32, and if vfpv4 is detected, NEON_FMA
Packit 1fb8d4
			if (has_neon)
Packit 1fb8d4
			{
Packit Service 5a9772
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3 | ANDROID_CPU_ARM_FEATURE_NEON |
Packit 1fb8d4
				                 ANDROID_CPU_ARM_FEATURE_VFP_D32;
Packit 1fb8d4
Packit 1fb8d4
				if (has_vfpv4)
Packit 1fb8d4
					g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_NEON_FMA;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			// VFPv3 implies VFPv2 and ARMv7
Packit 1fb8d4
			if (g_cpuFeatures & ANDROID_CPU_ARM_FEATURE_VFPv3)
Packit Service 5a9772
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv2 | ANDROID_CPU_ARM_FEATURE_ARMv7;
Packit 1fb8d4
Packit 1fb8d4
			if (has_idiva)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM;
Packit 1fb8d4
Packit 1fb8d4
			if (has_idivt)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2;
Packit 1fb8d4
Packit 1fb8d4
			if (has_iwmmxt)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_iWMMXt;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Extract the list of CPU features from ELF hwcaps2 */
Packit 1fb8d4
		uint32_t hwcaps2 = 0;
Packit 1fb8d4
		hwcaps2 = get_elf_hwcap_from_getauxval(AT_HWCAP2);
Packit 1fb8d4
Packit 1fb8d4
		if (hwcaps2 != 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			int has_aes = (hwcaps2 & HWCAP2_AES);
Packit Service 5a9772
			int has_pmull = (hwcaps2 & HWCAP2_PMULL);
Packit Service 5a9772
			int has_sha1 = (hwcaps2 & HWCAP2_SHA1);
Packit Service 5a9772
			int has_sha2 = (hwcaps2 & HWCAP2_SHA2);
Packit Service 5a9772
			int has_crc32 = (hwcaps2 & HWCAP2_CRC32);
Packit 1fb8d4
Packit 1fb8d4
			if (has_aes)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_AES;
Packit 1fb8d4
Packit 1fb8d4
			if (has_pmull)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_PMULL;
Packit 1fb8d4
Packit 1fb8d4
			if (has_sha1)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_SHA1;
Packit 1fb8d4
Packit 1fb8d4
			if (has_sha2)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_SHA2;
Packit 1fb8d4
Packit 1fb8d4
			if (has_crc32)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_CRC32;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		/* Extract the cpuid value from various fields */
Packit 1fb8d4
		// The CPUID value is broken up in several entries in /proc/cpuinfo.
Packit 1fb8d4
		// This table is used to rebuild it from the entries.
Packit 1fb8d4
		static const struct CpuIdEntry
Packit 1fb8d4
		{
Packit 1fb8d4
			const char* field;
Packit Service 5a9772
			char format;
Packit Service 5a9772
			char bit_lshift;
Packit Service 5a9772
			char bit_length;
Packit Service 5a9772
		} cpu_id_entries[] = {
Packit 1fb8d4
			{ "CPU implementer", 'x', 24, 8 },
Packit 1fb8d4
			{ "CPU variant", 'x', 20, 4 },
Packit 1fb8d4
			{ "CPU part", 'x', 4, 12 },
Packit 1fb8d4
			{ "CPU revision", 'd', 0, 4 },
Packit 1fb8d4
		};
Packit 1fb8d4
		size_t i;
Packit 1fb8d4
		D("Parsing /proc/cpuinfo to recover CPUID\n");
Packit 1fb8d4
Packit Service 5a9772
		for (i = 0; i < sizeof(cpu_id_entries) / sizeof(cpu_id_entries[0]); ++i)
Packit 1fb8d4
		{
Packit 1fb8d4
			const struct CpuIdEntry* entry = &cpu_id_entries[i];
Packit Service 5a9772
			char* value = extract_cpuinfo_field(cpuinfo, cpuinfo_len, entry->field);
Packit 1fb8d4
Packit 1fb8d4
			if (value == NULL)
Packit 1fb8d4
				continue;
Packit 1fb8d4
Packit 1fb8d4
			D("field=%s value='%s'\n", entry->field, value);
Packit 1fb8d4
			char* value_end = value + strlen(value);
Packit 1fb8d4
			int val = 0;
Packit 1fb8d4
			const char* start = value;
Packit 1fb8d4
			const char* p;
Packit 1fb8d4
Packit 1fb8d4
			if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X'))
Packit 1fb8d4
			{
Packit 1fb8d4
				start += 2;
Packit 1fb8d4
				p = parse_hexadecimal(start, value_end, &val;;
Packit 1fb8d4
			}
Packit 1fb8d4
			else if (entry->format == 'x')
Packit 1fb8d4
				p = parse_hexadecimal(value, value_end, &val;;
Packit 1fb8d4
			else
Packit 1fb8d4
				p = parse_decimal(value, value_end, &val;;
Packit 1fb8d4
Packit 1fb8d4
			if (p > (const char*)start)
Packit 1fb8d4
			{
Packit 1fb8d4
				val &= ((1 << entry->bit_length) - 1);
Packit 1fb8d4
				val <<= entry->bit_lshift;
Packit Service 5a9772
				g_cpuIdArm |= (uint32_t)val;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			free(value);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		// Handle kernel configuration bugs that prevent the correct
Packit 1fb8d4
		// reporting of CPU features.
Packit 1fb8d4
		static const struct CpuFix
Packit 1fb8d4
		{
Packit Service 5a9772
			uint32_t cpuid;
Packit Service 5a9772
			uint64_t or_flags;
Packit Service 5a9772
		} cpu_fixes[] = {
Packit 1fb8d4
			/* The Nexus 4 (Qualcomm Krait) kernel configuration
Packit 1fb8d4
			 * forgets to report IDIV support. */
Packit Service 5a9772
			{ 0x510006f2, ANDROID_CPU_ARM_FEATURE_IDIV_ARM | ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 },
Packit Service 5a9772
			{ 0x510006f3, ANDROID_CPU_ARM_FEATURE_IDIV_ARM | ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 },
Packit 1fb8d4
		};
Packit 1fb8d4
		size_t n;
Packit 1fb8d4
Packit 1fb8d4
		for (n = 0; n < sizeof(cpu_fixes) / sizeof(cpu_fixes[0]); ++n)
Packit 1fb8d4
		{
Packit 1fb8d4
			const struct CpuFix* entry = &cpu_fixes[n];
Packit 1fb8d4
Packit 1fb8d4
			if (g_cpuIdArm == entry->cpuid)
Packit 1fb8d4
				g_cpuFeatures |= entry->or_flags;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		// Special case: The emulator-specific Android 4.2 kernel fails
Packit 1fb8d4
		// to report support for the 32-bit ARM IDIV instruction.
Packit 1fb8d4
		// Technically, this is a feature of the virtual CPU implemented
Packit 1fb8d4
		// by the emulator. Note that it could also support Thumb IDIV
Packit 1fb8d4
		// in the future, and this will have to be slightly updated.
Packit Service 5a9772
		char* hardware = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Hardware");
Packit 1fb8d4
Packit 1fb8d4
		if (hardware)
Packit 1fb8d4
		{
Packit Service 5a9772
			if (!strcmp(hardware, "Goldfish") && g_cpuIdArm == 0x4100c080 &&
Packit 1fb8d4
			    (g_cpuFamily & ANDROID_CPU_ARM_FEATURE_ARMv7) != 0)
Packit 1fb8d4
			{
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_IDIV_ARM;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			free(hardware);
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
#endif /* __arm__ */
Packit 1fb8d4
#ifdef __aarch64__
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Extract the list of CPU features from ELF hwcaps */
Packit 1fb8d4
		uint32_t hwcaps = 0;
Packit 1fb8d4
		hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
Packit 1fb8d4
Packit 1fb8d4
		if (hwcaps != 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			int has_fp = (hwcaps & HWCAP_FP);
Packit Service 5a9772
			int has_asimd = (hwcaps & HWCAP_ASIMD);
Packit Service 5a9772
			int has_aes = (hwcaps & HWCAP_AES);
Packit Service 5a9772
			int has_pmull = (hwcaps & HWCAP_PMULL);
Packit Service 5a9772
			int has_sha1 = (hwcaps & HWCAP_SHA1);
Packit Service 5a9772
			int has_sha2 = (hwcaps & HWCAP_SHA2);
Packit Service 5a9772
			int has_crc32 = (hwcaps & HWCAP_CRC32);
Packit 1fb8d4
Packit 1fb8d4
			if (has_fp == 0)
Packit 1fb8d4
			{
Packit Service 5a9772
				D("ERROR: Floating-point unit missing, but is required by Android on AArch64 "
Packit Service 5a9772
				  "CPUs\n");
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			if (has_asimd == 0)
Packit 1fb8d4
			{
Packit 1fb8d4
				D("ERROR: ASIMD unit missing, but is required by Android on AArch64 CPUs\n");
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			if (has_fp)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_FP;
Packit 1fb8d4
Packit 1fb8d4
			if (has_asimd)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_ASIMD;
Packit 1fb8d4
Packit 1fb8d4
			if (has_aes)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_AES;
Packit 1fb8d4
Packit 1fb8d4
			if (has_pmull)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_PMULL;
Packit 1fb8d4
Packit 1fb8d4
			if (has_sha1)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_SHA1;
Packit 1fb8d4
Packit 1fb8d4
			if (has_sha2)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_SHA2;
Packit 1fb8d4
Packit 1fb8d4
			if (has_crc32)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_ARM64_FEATURE_CRC32;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
#endif /* __aarch64__ */
Packit 1fb8d4
#if defined(__i386__) || defined(__x86_64__)
Packit 1fb8d4
	int regs[4];
Packit 1fb8d4
	/* According to http://en.wikipedia.org/wiki/CPUID */
Packit Service 5a9772
#define VENDOR_INTEL_b 0x756e6547
Packit Service 5a9772
#define VENDOR_INTEL_c 0x6c65746e
Packit Service 5a9772
#define VENDOR_INTEL_d 0x49656e69
Packit 1fb8d4
	x86_cpuid(0, regs);
Packit Service 5a9772
	int vendorIsIntel =
Packit Service 5a9772
	    (regs[1] == VENDOR_INTEL_b && regs[2] == VENDOR_INTEL_c && regs[3] == VENDOR_INTEL_d);
Packit 1fb8d4
	x86_cpuid(1, regs);
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[2] & (1 << 9)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSSE3;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[2] & (1 << 23)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_POPCNT;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[2] & (1 << 19)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSE4_1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[2] & (1 << 20)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSE4_2;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (vendorIsIntel && (regs[2] & (1 << 22)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_MOVBE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[2] & (1 << 25)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_AES_NI;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[2] & (1 << 28)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_AVX;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[2] & (1 << 30)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_RDRAND;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	x86_cpuid(7, regs);
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[1] & (1 << 5)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_AVX2;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((regs[1] & (1 << 29)) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SHA_NI;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
#endif
Packit Service 5a9772
#if defined(__mips__)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* MIPS and MIPS64 */
Packit 1fb8d4
		/* Extract the list of CPU features from ELF hwcaps */
Packit 1fb8d4
		uint32_t hwcaps = 0;
Packit 1fb8d4
		hwcaps = get_elf_hwcap_from_getauxval(AT_HWCAP);
Packit 1fb8d4
Packit 1fb8d4
		if (hwcaps != 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			int has_r6 = (hwcaps & HWCAP_MIPS_R6);
Packit Service 5a9772
			int has_msa = (hwcaps & HWCAP_MIPS_MSA);
Packit 1fb8d4
Packit 1fb8d4
			if (has_r6)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_MIPS_FEATURE_R6;
Packit 1fb8d4
Packit 1fb8d4
			if (has_msa)
Packit 1fb8d4
				g_cpuFeatures |= ANDROID_CPU_MIPS_FEATURE_MSA;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
#endif /* __mips__ */
Packit 1fb8d4
	free(cpuinfo);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
AndroidCpuFamily android_getCpuFamily(void)
Packit 1fb8d4
{
Packit 1fb8d4
	pthread_once(&g_once, android_cpuInit);
Packit 1fb8d4
	return g_cpuFamily;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
uint64_t android_getCpuFeatures(void)
Packit 1fb8d4
{
Packit 1fb8d4
	pthread_once(&g_once, android_cpuInit);
Packit 1fb8d4
	return g_cpuFeatures;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
int android_getCpuCount(void)
Packit 1fb8d4
{
Packit 1fb8d4
	pthread_once(&g_once, android_cpuInit);
Packit 1fb8d4
	return g_cpuCount;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void android_cpuInitDummy(void)
Packit 1fb8d4
{
Packit 1fb8d4
	g_inited = 1;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
int android_setCpu(int cpu_count, uint64_t cpu_features)
Packit 1fb8d4
{
Packit 1fb8d4
	/* Fail if the library was already initialized. */
Packit 1fb8d4
	if (g_inited)
Packit 1fb8d4
		return 0;
Packit 1fb8d4
Packit 1fb8d4
	android_cpuInitFamily();
Packit 1fb8d4
	g_cpuCount = (cpu_count <= 0 ? 1 : cpu_count);
Packit 1fb8d4
	g_cpuFeatures = cpu_features;
Packit 1fb8d4
	pthread_once(&g_once, android_cpuInitDummy);
Packit 1fb8d4
	return 1;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#ifdef __arm__
Packit Service 5a9772
uint32_t android_getCpuIdArm(void)
Packit 1fb8d4
{
Packit 1fb8d4
	pthread_once(&g_once, android_cpuInit);
Packit 1fb8d4
	return g_cpuIdArm;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
int android_setCpuArm(int cpu_count, uint64_t cpu_features, uint32_t cpu_id)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!android_setCpu(cpu_count, cpu_features))
Packit 1fb8d4
		return 0;
Packit 1fb8d4
Packit 1fb8d4
	g_cpuIdArm = cpu_id;
Packit 1fb8d4
	return 1;
Packit 1fb8d4
}
Packit Service 5a9772
#endif /* __arm__ */
Packit 1fb8d4
Packit 1fb8d4
/*
Packit 1fb8d4
 * Technical note: Making sense of ARM's FPU architecture versions.
Packit 1fb8d4
 *
Packit 1fb8d4
 * FPA was ARM's first attempt at an FPU architecture. There is no Android
Packit 1fb8d4
 * device that actually uses it since this technology was already obsolete
Packit 1fb8d4
 * when the project started. If you see references to FPA instructions
Packit 1fb8d4
 * somewhere, you can be sure that this doesn't apply to Android at all.
Packit 1fb8d4
 *
Packit 1fb8d4
 * FPA was followed by "VFP", soon renamed "VFPv1" due to the emergence of
Packit 1fb8d4
 * new versions / additions to it. ARM considers this obsolete right now,
Packit 1fb8d4
 * and no known Android device implements it either.
Packit 1fb8d4
 *
Packit 1fb8d4
 * VFPv2 added a few instructions to VFPv1, and is an *optional* extension
Packit 1fb8d4
 * supported by some ARMv5TE, ARMv6 and ARMv6T2 CPUs. Note that a device
Packit 1fb8d4
 * supporting the 'armeabi' ABI doesn't necessarily support these.
Packit 1fb8d4
 *
Packit 1fb8d4
 * VFPv3-D16 adds a few instructions on top of VFPv2 and is typically used
Packit 1fb8d4
 * on ARMv7-A CPUs which implement a FPU. Note that it is also mandated
Packit 1fb8d4
 * by the Android 'armeabi-v7a' ABI. The -D16 suffix in its name means
Packit 1fb8d4
 * that it provides 16 double-precision FPU registers (d0-d15) and 32
Packit 1fb8d4
 * single-precision ones (s0-s31) which happen to be mapped to the same
Packit 1fb8d4
 * register banks.
Packit 1fb8d4
 *
Packit 1fb8d4
 * VFPv3-D32 is the name of an extension to VFPv3-D16 that provides 16
Packit 1fb8d4
 * additional double precision registers (d16-d31). Note that there are
Packit 1fb8d4
 * still only 32 single precision registers.
Packit 1fb8d4
 *
Packit 1fb8d4
 * VFPv3xD is a *subset* of VFPv3-D16 that only provides single-precision
Packit 1fb8d4
 * registers. It is only used on ARMv7-M (i.e. on micro-controllers) which
Packit 1fb8d4
 * are not supported by Android. Note that it is not compatible with VFPv2.
Packit 1fb8d4
 *
Packit 1fb8d4
 * NOTE: The term 'VFPv3' usually designate either VFPv3-D16 or VFPv3-D32
Packit 1fb8d4
 *       depending on context. For example GCC uses it for VFPv3-D32, but
Packit 1fb8d4
 *       the Linux kernel code uses it for VFPv3-D16 (especially in
Packit 1fb8d4
 *       /proc/cpuinfo). Always try to use the full designation when
Packit 1fb8d4
 *       possible.
Packit 1fb8d4
 *
Packit 1fb8d4
 * NEON, a.k.a. "ARM Advanced SIMD" is an extension that provides
Packit 1fb8d4
 * instructions to perform parallel computations on vectors of 8, 16,
Packit 1fb8d4
 * 32, 64 and 128 bit quantities. NEON requires VFPv32-D32 since all
Packit 1fb8d4
 * NEON registers are also mapped to the same register banks.
Packit 1fb8d4
 *
Packit 1fb8d4
 * VFPv4-D16, adds a few instructions on top of VFPv3-D16 in order to
Packit 1fb8d4
 * perform fused multiply-accumulate on VFP registers, as well as
Packit 1fb8d4
 * half-precision (16-bit) conversion operations.
Packit 1fb8d4
 *
Packit 1fb8d4
 * VFPv4-D32 is VFPv4-D16 with 32, instead of 16, FPU double precision
Packit 1fb8d4
 * registers.
Packit 1fb8d4
 *
Packit 1fb8d4
 * VPFv4-NEON is VFPv4-D32 with NEON instructions. It also adds fused
Packit 1fb8d4
 * multiply-accumulate instructions that work on the NEON registers.
Packit 1fb8d4
 *
Packit 1fb8d4
 * NOTE: Similarly, "VFPv4" might either reference VFPv4-D16 or VFPv4-D32
Packit 1fb8d4
 *       depending on context.
Packit 1fb8d4
 *
Packit 1fb8d4
 * The following information was determined by scanning the binutils-2.22
Packit 1fb8d4
 * sources:
Packit 1fb8d4
 *
Packit 1fb8d4
 * Basic VFP instruction subsets:
Packit 1fb8d4
 *
Packit 1fb8d4
 * #define FPU_VFP_EXT_V1xD 0x08000000     // Base VFP instruction set.
Packit 1fb8d4
 * #define FPU_VFP_EXT_V1   0x04000000     // Double-precision insns.
Packit 1fb8d4
 * #define FPU_VFP_EXT_V2   0x02000000     // ARM10E VFPr1.
Packit 1fb8d4
 * #define FPU_VFP_EXT_V3xD 0x01000000     // VFPv3 single-precision.
Packit 1fb8d4
 * #define FPU_VFP_EXT_V3   0x00800000     // VFPv3 double-precision.
Packit 1fb8d4
 * #define FPU_NEON_EXT_V1  0x00400000     // Neon (SIMD) insns.
Packit 1fb8d4
 * #define FPU_VFP_EXT_D32  0x00200000     // Registers D16-D31.
Packit 1fb8d4
 * #define FPU_VFP_EXT_FP16 0x00100000     // Half-precision extensions.
Packit 1fb8d4
 * #define FPU_NEON_EXT_FMA 0x00080000     // Neon fused multiply-add
Packit 1fb8d4
 * #define FPU_VFP_EXT_FMA  0x00040000     // VFP fused multiply-add
Packit 1fb8d4
 *
Packit 1fb8d4
 * FPU types (excluding NEON)
Packit 1fb8d4
 *
Packit 1fb8d4
 * FPU_VFP_V1xD (EXT_V1xD)
Packit 1fb8d4
 *    |
Packit 1fb8d4
 *    +--------------------------+
Packit 1fb8d4
 *    |                          |
Packit 1fb8d4
 * FPU_VFP_V1 (+EXT_V1)       FPU_VFP_V3xD (+EXT_V2+EXT_V3xD)
Packit 1fb8d4
 *    |                          |
Packit 1fb8d4
 *    |                          |
Packit 1fb8d4
 * FPU_VFP_V2 (+EXT_V2)       FPU_VFP_V4_SP_D16 (+EXT_FP16+EXT_FMA)
Packit 1fb8d4
 *    |
Packit 1fb8d4
 * FPU_VFP_V3D16 (+EXT_Vx3D+EXT_V3)
Packit 1fb8d4
 *    |
Packit 1fb8d4
 *    +--------------------------+
Packit 1fb8d4
 *    |                          |
Packit 1fb8d4
 * FPU_VFP_V3 (+EXT_D32)     FPU_VFP_V4D16 (+EXT_FP16+EXT_FMA)
Packit 1fb8d4
 *    |                          |
Packit 1fb8d4
 *    |                      FPU_VFP_V4 (+EXT_D32)
Packit 1fb8d4
 *    |
Packit 1fb8d4
 * FPU_VFP_HARD (+EXT_FMA+NEON_EXT_FMA)
Packit 1fb8d4
 *
Packit 1fb8d4
 * VFP architectures:
Packit 1fb8d4
 *
Packit 1fb8d4
 * ARCH_VFP_V1xD  (EXT_V1xD)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 *   +------------------+
Packit 1fb8d4
 *   |                  |
Packit 1fb8d4
 *   |             ARCH_VFP_V3xD (+EXT_V2+EXT_V3xD)
Packit 1fb8d4
 *   |                  |
Packit 1fb8d4
 *   |             ARCH_VFP_V3xD_FP16 (+EXT_FP16)
Packit 1fb8d4
 *   |                  |
Packit 1fb8d4
 *   |             ARCH_VFP_V4_SP_D16 (+EXT_FMA)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_VFP_V1 (+EXT_V1)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_VFP_V2 (+EXT_V2)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_VFP_V3D16 (+EXT_V3xD+EXT_V3)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 *   +-------------------+
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V3D16_FP16  (+EXT_FP16)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 *   +-------------------+
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V4 (+EXT_D32)
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_VFP_V3 (+EXT_D32)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 *   +-------------------+
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V3_FP16 (+EXT_FP16)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_NEON_FP16 (+EXT_FP16)
Packit 1fb8d4
 *
Packit 1fb8d4
 * -fpu=<name> values and their correspondance with FPU architectures above:
Packit 1fb8d4
 *
Packit 1fb8d4
 *   {"vfp",               FPU_ARCH_VFP_V2},
Packit 1fb8d4
 *   {"vfp9",              FPU_ARCH_VFP_V2},
Packit 1fb8d4
 *   {"vfp3",              FPU_ARCH_VFP_V3}, // For backwards compatbility.
Packit 1fb8d4
 *   {"vfp10",             FPU_ARCH_VFP_V2},
Packit 1fb8d4
 *   {"vfp10-r0",          FPU_ARCH_VFP_V1},
Packit 1fb8d4
 *   {"vfpxd",             FPU_ARCH_VFP_V1xD},
Packit 1fb8d4
 *   {"vfpv2",             FPU_ARCH_VFP_V2},
Packit 1fb8d4
 *   {"vfpv3",             FPU_ARCH_VFP_V3},
Packit 1fb8d4
 *   {"vfpv3-fp16",        FPU_ARCH_VFP_V3_FP16},
Packit 1fb8d4
 *   {"vfpv3-d16",         FPU_ARCH_VFP_V3D16},
Packit 1fb8d4
 *   {"vfpv3-d16-fp16",    FPU_ARCH_VFP_V3D16_FP16},
Packit 1fb8d4
 *   {"vfpv3xd",           FPU_ARCH_VFP_V3xD},
Packit 1fb8d4
 *   {"vfpv3xd-fp16",      FPU_ARCH_VFP_V3xD_FP16},
Packit 1fb8d4
 *   {"neon",              FPU_ARCH_VFP_V3_PLUS_NEON_V1},
Packit 1fb8d4
 *   {"neon-fp16",         FPU_ARCH_NEON_FP16},
Packit 1fb8d4
 *   {"vfpv4",             FPU_ARCH_VFP_V4},
Packit 1fb8d4
 *   {"vfpv4-d16",         FPU_ARCH_VFP_V4D16},
Packit 1fb8d4
 *   {"fpv4-sp-d16",       FPU_ARCH_VFP_V4_SP_D16},
Packit 1fb8d4
 *   {"neon-vfpv4",        FPU_ARCH_NEON_VFP_V4},
Packit 1fb8d4
 *
Packit 1fb8d4
 *
Packit 1fb8d4
 * Simplified diagram that only includes FPUs supported by Android:
Packit 1fb8d4
 * Only ARCH_VFP_V3D16 is actually mandated by the armeabi-v7a ABI,
Packit 1fb8d4
 * all others are optional and must be probed at runtime.
Packit 1fb8d4
 *
Packit 1fb8d4
 * ARCH_VFP_V3D16 (EXT_V1xD+EXT_V1+EXT_V2+EXT_V3xD+EXT_V3)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 *   +-------------------+
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V3D16_FP16  (+EXT_FP16)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 *   +-------------------+
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V4_D16 (+EXT_FP16+EXT_FMA)
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V4 (+EXT_D32)
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_NEON_VFP_V4 (+EXT_NEON+EXT_NEON_FMA)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_VFP_V3 (+EXT_D32)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 *   +-------------------+
Packit 1fb8d4
 *   |                   |
Packit 1fb8d4
 *   |         ARCH_VFP_V3_FP16 (+EXT_FP16)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_VFP_V3_PLUS_NEON_V1 (+EXT_NEON)
Packit 1fb8d4
 *   |
Packit 1fb8d4
 * ARCH_NEON_FP16 (+EXT_FP16)
Packit 1fb8d4
 *
Packit 1fb8d4
 */