cpu-features.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. /* ChangeLog for this library:
  29. *
  30. * NDK r5: Handle buggy kernels which report a CPU Architecture number of 7
  31. * for an ARMv6 CPU (see below).
  32. *
  33. * Handle kernels that only report 'neon', and not 'vfpv3'
  34. * (VFPv3 is mandated by the ARM architecture is Neon is implemented)
  35. *
  36. * Handle kernels that only report 'vfpv3d16', and not 'vfpv3'
  37. *
  38. * Fix x86 compilation. Report ANDROID_CPU_FAMILY_X86 in
  39. * android_getCpuFamily().
  40. *
  41. * NDK r4: Initial release
  42. */
  43. #include <sys/system_properties.h>
  44. #ifdef __arm__
  45. #include <machine/cpu-features.h>
  46. #endif
  47. #include <pthread.h>
  48. #include "cpu-features.h"
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <fcntl.h>
  52. #include <errno.h>
  53. static pthread_once_t g_once;
  54. static AndroidCpuFamily g_cpuFamily;
  55. static uint64_t g_cpuFeatures;
  56. static int g_cpuCount;
  57. static const int android_cpufeatures_debug = 0;
  58. #ifdef __arm__
  59. # define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_ARM
  60. #elif defined __i386__
  61. # define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_X86
  62. #else
  63. # define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_UNKNOWN
  64. #endif
  65. #define D(...) \
  66. do { \
  67. if (android_cpufeatures_debug) { \
  68. printf(__VA_ARGS__); fflush(stdout); \
  69. } \
  70. } while (0)
  71. #ifdef __i386__
  72. static __inline__ void x86_cpuid(int func, int values[4])
  73. {
  74. int a, b, c, d;
  75. /* We need to preserve ebx since we're compiling PIC code */
  76. /* this means we can't use "=b" for the second output register */
  77. __asm__ __volatile__ ( \
  78. "push %%ebx\n"
  79. "cpuid\n" \
  80. "mov %1, %%ebx\n"
  81. "pop %%ebx\n"
  82. : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
  83. : "a" (func) \
  84. );
  85. values[0] = a;
  86. values[1] = b;
  87. values[2] = c;
  88. values[3] = d;
  89. }
  90. #endif
  91. /* Read the content of /proc/cpuinfo into a user-provided buffer.
  92. * Return the length of the data, or -1 on error. Does *not*
  93. * zero-terminate the content. Will not read more
  94. * than 'buffsize' bytes.
  95. */
  96. static int
  97. read_file(const char* pathname, char* buffer, size_t buffsize)
  98. {
  99. int fd, len;
  100. fd = open(pathname, O_RDONLY);
  101. if (fd < 0)
  102. return -1;
  103. do {
  104. len = read(fd, buffer, buffsize);
  105. } while (len < 0 && errno == EINTR);
  106. close(fd);
  107. return len;
  108. }
  109. /* Extract the content of a the first occurence of a given field in
  110. * the content of /proc/cpuinfo and return it as a heap-allocated
  111. * string that must be freed by the caller.
  112. *
  113. * Return NULL if not found
  114. */
  115. static char*
  116. extract_cpuinfo_field(char* buffer, int buflen, const char* field)
  117. {
  118. int fieldlen = strlen(field);
  119. char* bufend = buffer + buflen;
  120. char* result = NULL;
  121. int len, ignore;
  122. const char *p, *q;
  123. /* Look for first field occurence, and ensures it starts the line.
  124. */
  125. p = buffer;
  126. bufend = buffer + buflen;
  127. for (;;) {
  128. p = memmem(p, bufend-p, field, fieldlen);
  129. if (p == NULL)
  130. goto EXIT;
  131. if (p == buffer || p[-1] == '\n')
  132. break;
  133. p += fieldlen;
  134. }
  135. /* Skip to the first column followed by a space */
  136. p += fieldlen;
  137. p = memchr(p, ':', bufend-p);
  138. if (p == NULL || p[1] != ' ')
  139. goto EXIT;
  140. /* Find the end of the line */
  141. p += 2;
  142. q = memchr(p, '\n', bufend-p);
  143. if (q == NULL)
  144. q = bufend;
  145. /* Copy the line into a heap-allocated buffer */
  146. len = q-p;
  147. result = malloc(len+1);
  148. if (result == NULL)
  149. goto EXIT;
  150. memcpy(result, p, len);
  151. result[len] = '\0';
  152. EXIT:
  153. return result;
  154. }
  155. /* Count the number of occurences of a given field prefix in /proc/cpuinfo.
  156. */
  157. static int
  158. count_cpuinfo_field(char* buffer, int buflen, const char* field)
  159. {
  160. int fieldlen = strlen(field);
  161. const char* p = buffer;
  162. const char* bufend = buffer + buflen;
  163. const char* q;
  164. int count = 0;
  165. for (;;) {
  166. const char* q;
  167. p = memmem(p, bufend-p, field, fieldlen);
  168. if (p == NULL)
  169. break;
  170. /* Ensure that the field is at the start of a line */
  171. if (p > buffer && p[-1] != '\n') {
  172. p += fieldlen;
  173. continue;
  174. }
  175. /* skip any whitespace */
  176. q = p + fieldlen;
  177. while (q < bufend && (*q == ' ' || *q == '\t'))
  178. q++;
  179. /* we must have a colon now */
  180. if (q < bufend && *q == ':') {
  181. count += 1;
  182. q ++;
  183. }
  184. p = q;
  185. }
  186. return count;
  187. }
  188. /* Like strlen(), but for constant string literals */
  189. #define STRLEN_CONST(x) ((sizeof(x)-1)
  190. /* Checks that a space-separated list of items contains one given 'item'.
  191. * Returns 1 if found, 0 otherwise.
  192. */
  193. static int
  194. has_list_item(const char* list, const char* item)
  195. {
  196. const char* p = list;
  197. int itemlen = strlen(item);
  198. if (list == NULL)
  199. return 0;
  200. while (*p) {
  201. const char* q;
  202. /* skip spaces */
  203. while (*p == ' ' || *p == '\t')
  204. p++;
  205. /* find end of current list item */
  206. q = p;
  207. while (*q && *q != ' ' && *q != '\t')
  208. q++;
  209. if (itemlen == q-p && !memcmp(p, item, itemlen))
  210. return 1;
  211. /* skip to next item */
  212. p = q;
  213. }
  214. return 0;
  215. }
  216. static void
  217. android_cpuInit(void)
  218. {
  219. char cpuinfo[4096];
  220. int cpuinfo_len;
  221. g_cpuFamily = DEFAULT_CPU_FAMILY;
  222. g_cpuFeatures = 0;
  223. g_cpuCount = 1;
  224. cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, sizeof cpuinfo);
  225. D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len,
  226. cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo);
  227. if (cpuinfo_len < 0) /* should not happen */ {
  228. return;
  229. }
  230. /* Count the CPU cores, the value may be 0 for single-core CPUs */
  231. g_cpuCount = count_cpuinfo_field(cpuinfo, cpuinfo_len, "processor");
  232. if (g_cpuCount == 0) {
  233. g_cpuCount = count_cpuinfo_field(cpuinfo, cpuinfo_len, "Processor");
  234. if (g_cpuCount == 0) {
  235. g_cpuCount = 1;
  236. }
  237. }
  238. D("found cpuCount = %d\n", g_cpuCount);
  239. #ifdef __ARM_ARCH__
  240. {
  241. char* features = NULL;
  242. char* architecture = NULL;
  243. /* Extract architecture from the "CPU Architecture" field.
  244. * The list is well-known, unlike the the output of
  245. * the 'Processor' field which can vary greatly.
  246. *
  247. * See the definition of the 'proc_arch' array in
  248. * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
  249. * same file.
  250. */
  251. char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
  252. if (cpuArch != NULL) {
  253. char* end;
  254. long archNumber;
  255. int hasARMv7 = 0;
  256. D("found cpuArch = '%s'\n", cpuArch);
  257. /* read the initial decimal number, ignore the rest */
  258. archNumber = strtol(cpuArch, &end, 10);
  259. /* Here we assume that ARMv8 will be upwards compatible with v7
  260. * in the future. Unfortunately, there is no 'Features' field to
  261. * indicate that Thumb-2 is supported.
  262. */
  263. if (end > cpuArch && archNumber >= 7) {
  264. hasARMv7 = 1;
  265. }
  266. /* Unfortunately, it seems that certain ARMv6-based CPUs
  267. * report an incorrect architecture number of 7!
  268. *
  269. * See http://code.google.com/p/android/issues/detail?id=10812
  270. *
  271. * We try to correct this by looking at the 'elf_format'
  272. * field reported by the 'Processor' field, which is of the
  273. * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
  274. * an ARMv6-one.
  275. */
  276. if (hasARMv7) {
  277. char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len,
  278. "Processor");
  279. if (cpuProc != NULL) {
  280. D("found cpuProc = '%s'\n", cpuProc);
  281. if (has_list_item(cpuProc, "(v6l)")) {
  282. D("CPU processor and architecture mismatch!!\n");
  283. hasARMv7 = 0;
  284. }
  285. free(cpuProc);
  286. }
  287. }
  288. if (hasARMv7) {
  289. g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_ARMv7;
  290. }
  291. /* The LDREX / STREX instructions are available from ARMv6 */
  292. if (archNumber >= 6) {
  293. g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_LDREX_STREX;
  294. }
  295. free(cpuArch);
  296. }
  297. /* Extract the list of CPU features from 'Features' field */
  298. char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features");
  299. if (cpuFeatures != NULL) {
  300. D("found cpuFeatures = '%s'\n", cpuFeatures);
  301. if (has_list_item(cpuFeatures, "vfp"))
  302. g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFP;
  303. if (has_list_item(cpuFeatures, "vfpv3"))
  304. g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
  305. else if (has_list_item(cpuFeatures, "vfpv3d16"))
  306. g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
  307. if (has_list_item(cpuFeatures, "neon")) {
  308. /* Note: Certain kernels only report neon but not vfpv3
  309. * in their features list. However, ARM mandates
  310. * that if Neon is implemented, so must be VFPv3
  311. * so always set the flag.
  312. */
  313. g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_NEON |
  314. ANDROID_CPU_ARM_FEATURE_VFPv3;
  315. }
  316. free(cpuFeatures);
  317. }
  318. }
  319. #endif /* __ARM_ARCH__ */
  320. #ifdef __i386__
  321. g_cpuFamily = ANDROID_CPU_FAMILY_X86;
  322. int regs[4];
  323. /* According to http://en.wikipedia.org/wiki/CPUID */
  324. #define VENDOR_INTEL_b 0x756e6547
  325. #define VENDOR_INTEL_c 0x6c65746e
  326. #define VENDOR_INTEL_d 0x49656e69
  327. x86_cpuid(0, regs);
  328. int vendorIsIntel = (regs[1] == VENDOR_INTEL_b &&
  329. regs[2] == VENDOR_INTEL_c &&
  330. regs[3] == VENDOR_INTEL_d);
  331. x86_cpuid(1, regs);
  332. if ((regs[2] & (1 << 9)) != 0) {
  333. g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSSE3;
  334. }
  335. if ((regs[2] & (1 << 23)) != 0) {
  336. g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_POPCNT;
  337. }
  338. if (vendorIsIntel && (regs[2] & (1 << 22)) != 0) {
  339. g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_MOVBE;
  340. }
  341. #endif
  342. }
  343. AndroidCpuFamily
  344. android_getCpuFamily(void)
  345. {
  346. pthread_once(&g_once, android_cpuInit);
  347. return g_cpuFamily;
  348. }
  349. uint64_t
  350. android_getCpuFeatures(void)
  351. {
  352. pthread_once(&g_once, android_cpuInit);
  353. return g_cpuFeatures;
  354. }
  355. int
  356. android_getCpuCount(void)
  357. {
  358. pthread_once(&g_once, android_cpuInit);
  359. return g_cpuCount;
  360. }