1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 /* compile-time and runtime tests for whether to use various ARM extensions */
6 
7 #include "arm.h"
8 
9 #if defined(MOZILLA_ARM_HAVE_CPUID_DETECTION)
10 
11 // arm.h has parallel #ifs which declare MOZILLA_ARM_HAVE_CPUID_DETECTION.
12 // We don't check it here so that we get compile errors if it's defined, but
13 // we don't compile one of these detection methods. The detection code here is
14 // based on the CPU detection in libtheora.
15 
16 #if defined(__linux__) || defined(ANDROID)
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 enum {
22   MOZILLA_HAS_EDSP_FLAG = 1,
23   MOZILLA_HAS_ARMV6_FLAG = 2,
24   MOZILLA_HAS_ARMV7_FLAG = 4,
25   MOZILLA_HAS_NEON_FLAG = 8
26 };
27 
get_arm_cpu_flags(void)28 static unsigned get_arm_cpu_flags(void) {
29   unsigned flags;
30   FILE *fin;
31   bool armv6_processor = false;
32   flags = 0;
33   /*Reading /proc/self/auxv would be easier, but that doesn't work reliably on
34     Android. This also means that detection will fail in Scratchbox, which is
35     desirable, as NEON does not work in the qemu shipped with the Maemo 5 SDK.
36     I don't know if /proc/self/auxv would do any better in that case, anyway,
37     or if it would return random flags from the host CPU.*/
38   fin = fopen("/proc/cpuinfo", "r");
39   if (fin != nullptr) {
40     /*512 should be enough for anybody (it's even enough for all the flags that
41       x86 has accumulated... so far).*/
42     char buf[512];
43     while (fgets(buf, 511, fin) != nullptr) {
44       if (memcmp(buf, "Features", 8) == 0) {
45         char *p;
46         p = strstr(buf, " edsp");
47         if (p != nullptr && (p[5] == ' ' || p[5] == '\n'))
48           flags |= MOZILLA_HAS_EDSP_FLAG;
49         p = strstr(buf, " neon");
50         if (p != nullptr && (p[5] == ' ' || p[5] == '\n'))
51           flags |= MOZILLA_HAS_NEON_FLAG;
52       }
53       if (memcmp(buf, "CPU architecture:", 17) == 0) {
54         int version;
55         version = atoi(buf + 17);
56         if (version >= 6) flags |= MOZILLA_HAS_ARMV6_FLAG;
57         if (version >= 7) flags |= MOZILLA_HAS_ARMV7_FLAG;
58       }
59       /* media/webrtc/trunk/src/system_wrappers/source/cpu_features_arm.c
60        * Unfortunately, it seems that certain ARMv6-based CPUs
61        * report an incorrect architecture number of 7!
62        *
63        * We try to correct this by looking at the 'elf_format'
64        * field reported by the 'Processor' field, which is of the
65        * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
66        * an ARMv6-one.
67        */
68       if (memcmp(buf, "Processor\t:", 11) == 0) {
69         if (strstr(buf, "(v6l)") != 0) {
70           armv6_processor = true;
71         }
72       }
73     }
74     fclose(fin);
75   }
76   if (armv6_processor) {
77     // ARMv6 pretending to be ARMv7? clear flag
78     if (flags & MOZILLA_HAS_ARMV7_FLAG) {
79       flags &= ~MOZILLA_HAS_ARMV7_FLAG;
80     }
81   }
82   return flags;
83 }
84 
85 // Cache a local copy so we only have to read /proc/cpuinfo once.
86 static unsigned arm_cpu_flags = get_arm_cpu_flags();
87 
88 #if !defined(MOZILLA_PRESUME_EDSP)
check_edsp(void)89 static bool check_edsp(void) {
90   return (arm_cpu_flags & MOZILLA_HAS_EDSP_FLAG) != 0;
91 }
92 #endif
93 
94 #if !defined(MOZILLA_PRESUME_ARMV6)
check_armv6(void)95 static bool check_armv6(void) {
96   return (arm_cpu_flags & MOZILLA_HAS_ARMV6_FLAG) != 0;
97 }
98 #endif
99 
100 #if !defined(MOZILLA_PRESUME_ARMV7)
check_armv7(void)101 static bool check_armv7(void) {
102   return (arm_cpu_flags & MOZILLA_HAS_ARMV7_FLAG) != 0;
103 }
104 #endif
105 
106 #if !defined(MOZILLA_PRESUME_NEON)
check_neon(void)107 static bool check_neon(void) {
108   return (arm_cpu_flags & MOZILLA_HAS_NEON_FLAG) != 0;
109 }
110 #endif
111 
112 #endif  // defined(__linux__) || defined(ANDROID)
113 
114 namespace mozilla {
115 namespace arm_private {
116 #if !defined(MOZILLA_PRESUME_EDSP)
117 bool edsp_enabled = check_edsp();
118 #endif
119 #if !defined(MOZILLA_PRESUME_ARMV6)
120 bool armv6_enabled = check_armv6();
121 #endif
122 #if !defined(MOZILLA_PRESUME_ARMV7)
123 bool armv7_enabled = check_armv7();
124 #endif
125 #if !defined(MOZILLA_PRESUME_NEON)
126 bool neon_enabled = check_neon();
127 #endif
128 }  // namespace arm_private
129 }  // namespace mozilla
130 
131 #endif  // MOZILLA_ARM_HAVE_CPUID_DETECTION
132