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