cpu-features.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #ifndef CPU_FEATURES_H
  29. #define CPU_FEATURES_H
  30. #include <sys/cdefs.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. __BEGIN_DECLS
  34. /* A list of valid values returned by android_getCpuFamily().
  35. * They describe the CPU Architecture of the current process.
  36. */
  37. typedef enum {
  38. ANDROID_CPU_FAMILY_UNKNOWN = 0,
  39. ANDROID_CPU_FAMILY_ARM,
  40. ANDROID_CPU_FAMILY_X86,
  41. ANDROID_CPU_FAMILY_MIPS,
  42. ANDROID_CPU_FAMILY_ARM64,
  43. ANDROID_CPU_FAMILY_X86_64,
  44. ANDROID_CPU_FAMILY_MIPS64,
  45. ANDROID_CPU_FAMILY_MAX /* do not remove */
  46. } AndroidCpuFamily;
  47. /* Return the CPU family of the current process.
  48. *
  49. * Note that this matches the bitness of the current process. I.e. when
  50. * running a 32-bit binary on a 64-bit capable CPU, this will return the
  51. * 32-bit CPU family value.
  52. */
  53. extern AndroidCpuFamily android_getCpuFamily(void);
  54. /* Return a bitmap describing a set of optional CPU features that are
  55. * supported by the current device's CPU. The exact bit-flags returned
  56. * depend on the value returned by android_getCpuFamily(). See the
  57. * documentation for the ANDROID_CPU_*_FEATURE_* flags below for details.
  58. */
  59. extern uint64_t android_getCpuFeatures(void);
  60. /* The list of feature flags for ANDROID_CPU_FAMILY_ARM that can be
  61. * recognized by the library (see note below for 64-bit ARM). Value details
  62. * are:
  63. *
  64. * VFPv2:
  65. * CPU supports the VFPv2 instruction set. Many, but not all, ARMv6 CPUs
  66. * support these instructions. VFPv2 is a subset of VFPv3 so this will
  67. * be set whenever VFPv3 is set too.
  68. *
  69. * ARMv7:
  70. * CPU supports the ARMv7-A basic instruction set.
  71. * This feature is mandated by the 'armeabi-v7a' ABI.
  72. *
  73. * VFPv3:
  74. * CPU supports the VFPv3-D16 instruction set, providing hardware FPU
  75. * support for single and double precision floating point registers.
  76. * Note that only 16 FPU registers are available by default, unless
  77. * the D32 bit is set too. This feature is also mandated by the
  78. * 'armeabi-v7a' ABI.
  79. *
  80. * VFP_D32:
  81. * CPU VFP optional extension that provides 32 FPU registers,
  82. * instead of 16. Note that ARM mandates this feature is the 'NEON'
  83. * feature is implemented by the CPU.
  84. *
  85. * NEON:
  86. * CPU FPU supports "ARM Advanced SIMD" instructions, also known as
  87. * NEON. Note that this mandates the VFP_D32 feature as well, per the
  88. * ARM Architecture specification.
  89. *
  90. * VFP_FP16:
  91. * Half-width floating precision VFP extension. If set, the CPU
  92. * supports instructions to perform floating-point operations on
  93. * 16-bit registers. This is part of the VFPv4 specification, but
  94. * not mandated by any Android ABI.
  95. *
  96. * VFP_FMA:
  97. * Fused multiply-accumulate VFP instructions extension. Also part of
  98. * the VFPv4 specification, but not mandated by any Android ABI.
  99. *
  100. * NEON_FMA:
  101. * Fused multiply-accumulate NEON instructions extension. Optional
  102. * extension from the VFPv4 specification, but not mandated by any
  103. * Android ABI.
  104. *
  105. * IDIV_ARM:
  106. * Integer division available in ARM mode. Only available
  107. * on recent CPUs (e.g. Cortex-A15).
  108. *
  109. * IDIV_THUMB2:
  110. * Integer division available in Thumb-2 mode. Only available
  111. * on recent CPUs (e.g. Cortex-A15).
  112. *
  113. * iWMMXt:
  114. * Optional extension that adds MMX registers and operations to an
  115. * ARM CPU. This is only available on a few XScale-based CPU designs
  116. * sold by Marvell. Pretty rare in practice.
  117. *
  118. * AES:
  119. * CPU supports AES instructions. These instructions are only
  120. * available for 32-bit applications running on ARMv8 CPU.
  121. *
  122. * CRC32:
  123. * CPU supports CRC32 instructions. These instructions are only
  124. * available for 32-bit applications running on ARMv8 CPU.
  125. *
  126. * SHA2:
  127. * CPU supports SHA2 instructions. These instructions are only
  128. * available for 32-bit applications running on ARMv8 CPU.
  129. *
  130. * SHA1:
  131. * CPU supports SHA1 instructions. These instructions are only
  132. * available for 32-bit applications running on ARMv8 CPU.
  133. *
  134. * PMULL:
  135. * CPU supports 64-bit PMULL and PMULL2 instructions. These
  136. * instructions are only available for 32-bit applications
  137. * running on ARMv8 CPU.
  138. *
  139. * If you want to tell the compiler to generate code that targets one of
  140. * the feature set above, you should probably use one of the following
  141. * flags (for more details, see technical note at the end of this file):
  142. *
  143. * -mfpu=vfp
  144. * -mfpu=vfpv2
  145. * These are equivalent and tell GCC to use VFPv2 instructions for
  146. * floating-point operations. Use this if you want your code to
  147. * run on *some* ARMv6 devices, and any ARMv7-A device supported
  148. * by Android.
  149. *
  150. * Generated code requires VFPv2 feature.
  151. *
  152. * -mfpu=vfpv3-d16
  153. * Tell GCC to use VFPv3 instructions (using only 16 FPU registers).
  154. * This should be generic code that runs on any CPU that supports the
  155. * 'armeabi-v7a' Android ABI. Note that no ARMv6 CPU supports this.
  156. *
  157. * Generated code requires VFPv3 feature.
  158. *
  159. * -mfpu=vfpv3
  160. * Tell GCC to use VFPv3 instructions with 32 FPU registers.
  161. * Generated code requires VFPv3|VFP_D32 features.
  162. *
  163. * -mfpu=neon
  164. * Tell GCC to use VFPv3 instructions with 32 FPU registers, and
  165. * also support NEON intrinsics (see <arm_neon.h>).
  166. * Generated code requires VFPv3|VFP_D32|NEON features.
  167. *
  168. * -mfpu=vfpv4-d16
  169. * Generated code requires VFPv3|VFP_FP16|VFP_FMA features.
  170. *
  171. * -mfpu=vfpv4
  172. * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32 features.
  173. *
  174. * -mfpu=neon-vfpv4
  175. * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|NEON|NEON_FMA
  176. * features.
  177. *
  178. * -mcpu=cortex-a7
  179. * -mcpu=cortex-a15
  180. * Generated code requires VFPv3|VFP_FP16|VFP_FMA|VFP_D32|
  181. * NEON|NEON_FMA|IDIV_ARM|IDIV_THUMB2
  182. * This flag implies -mfpu=neon-vfpv4.
  183. *
  184. * -mcpu=iwmmxt
  185. * Allows the use of iWMMXt instrinsics with GCC.
  186. *
  187. * IMPORTANT NOTE: These flags should only be tested when
  188. * android_getCpuFamily() returns ANDROID_CPU_FAMILY_ARM, i.e. this is a
  189. * 32-bit process.
  190. *
  191. * When running a 64-bit ARM process on an ARMv8 CPU,
  192. * android_getCpuFeatures() will return a different set of bitflags
  193. */
  194. enum {
  195. ANDROID_CPU_ARM_FEATURE_ARMv7 = (1 << 0),
  196. ANDROID_CPU_ARM_FEATURE_VFPv3 = (1 << 1),
  197. ANDROID_CPU_ARM_FEATURE_NEON = (1 << 2),
  198. ANDROID_CPU_ARM_FEATURE_LDREX_STREX = (1 << 3),
  199. ANDROID_CPU_ARM_FEATURE_VFPv2 = (1 << 4),
  200. ANDROID_CPU_ARM_FEATURE_VFP_D32 = (1 << 5),
  201. ANDROID_CPU_ARM_FEATURE_VFP_FP16 = (1 << 6),
  202. ANDROID_CPU_ARM_FEATURE_VFP_FMA = (1 << 7),
  203. ANDROID_CPU_ARM_FEATURE_NEON_FMA = (1 << 8),
  204. ANDROID_CPU_ARM_FEATURE_IDIV_ARM = (1 << 9),
  205. ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2 = (1 << 10),
  206. ANDROID_CPU_ARM_FEATURE_iWMMXt = (1 << 11),
  207. ANDROID_CPU_ARM_FEATURE_AES = (1 << 12),
  208. ANDROID_CPU_ARM_FEATURE_PMULL = (1 << 13),
  209. ANDROID_CPU_ARM_FEATURE_SHA1 = (1 << 14),
  210. ANDROID_CPU_ARM_FEATURE_SHA2 = (1 << 15),
  211. ANDROID_CPU_ARM_FEATURE_CRC32 = (1 << 16),
  212. };
  213. /* The bit flags corresponding to the output of android_getCpuFeatures()
  214. * when android_getCpuFamily() returns ANDROID_CPU_FAMILY_ARM64. Value details
  215. * are:
  216. *
  217. * FP:
  218. * CPU has Floating-point unit.
  219. *
  220. * ASIMD:
  221. * CPU has Advanced SIMD unit.
  222. *
  223. * AES:
  224. * CPU supports AES instructions.
  225. *
  226. * CRC32:
  227. * CPU supports CRC32 instructions.
  228. *
  229. * SHA2:
  230. * CPU supports SHA2 instructions.
  231. *
  232. * SHA1:
  233. * CPU supports SHA1 instructions.
  234. *
  235. * PMULL:
  236. * CPU supports 64-bit PMULL and PMULL2 instructions.
  237. */
  238. enum {
  239. ANDROID_CPU_ARM64_FEATURE_FP = (1 << 0),
  240. ANDROID_CPU_ARM64_FEATURE_ASIMD = (1 << 1),
  241. ANDROID_CPU_ARM64_FEATURE_AES = (1 << 2),
  242. ANDROID_CPU_ARM64_FEATURE_PMULL = (1 << 3),
  243. ANDROID_CPU_ARM64_FEATURE_SHA1 = (1 << 4),
  244. ANDROID_CPU_ARM64_FEATURE_SHA2 = (1 << 5),
  245. ANDROID_CPU_ARM64_FEATURE_CRC32 = (1 << 6),
  246. };
  247. /* The bit flags corresponding to the output of android_getCpuFeatures()
  248. * when android_getCpuFamily() returns ANDROID_CPU_FAMILY_X86 or
  249. * ANDROID_CPU_FAMILY_X86_64.
  250. */
  251. enum {
  252. ANDROID_CPU_X86_FEATURE_SSSE3 = (1 << 0),
  253. ANDROID_CPU_X86_FEATURE_POPCNT = (1 << 1),
  254. ANDROID_CPU_X86_FEATURE_MOVBE = (1 << 2),
  255. ANDROID_CPU_X86_FEATURE_SSE4_1 = (1 << 3),
  256. ANDROID_CPU_X86_FEATURE_SSE4_2 = (1 << 4),
  257. ANDROID_CPU_X86_FEATURE_AES_NI = (1 << 5),
  258. ANDROID_CPU_X86_FEATURE_AVX = (1 << 6),
  259. ANDROID_CPU_X86_FEATURE_RDRAND = (1 << 7),
  260. ANDROID_CPU_X86_FEATURE_AVX2 = (1 << 8),
  261. ANDROID_CPU_X86_FEATURE_SHA_NI = (1 << 9),
  262. };
  263. /* The bit flags corresponding to the output of android_getCpuFeatures()
  264. * when android_getCpuFamily() returns ANDROID_CPU_FAMILY_MIPS
  265. * or ANDROID_CPU_FAMILY_MIPS64. Values are:
  266. *
  267. * R6:
  268. * CPU executes MIPS Release 6 instructions natively, and
  269. * supports obsoleted R1..R5 instructions only via kernel traps.
  270. *
  271. * MSA:
  272. * CPU supports Mips SIMD Architecture instructions.
  273. */
  274. enum {
  275. ANDROID_CPU_MIPS_FEATURE_R6 = (1 << 0),
  276. ANDROID_CPU_MIPS_FEATURE_MSA = (1 << 1),
  277. };
  278. /* Return the number of CPU cores detected on this device. */
  279. extern int android_getCpuCount(void);
  280. /* The following is used to force the CPU count and features
  281. * mask in sandboxed processes. Under 4.1 and higher, these processes
  282. * cannot access /proc, which is the only way to get information from
  283. * the kernel about the current hardware (at least on ARM).
  284. *
  285. * It _must_ be called only once, and before any android_getCpuXXX
  286. * function, any other case will fail.
  287. *
  288. * This function return 1 on success, and 0 on failure.
  289. */
  290. extern int android_setCpu(int cpu_count,
  291. uint64_t cpu_features);
  292. #ifdef __arm__
  293. /* Retrieve the ARM 32-bit CPUID value from the kernel.
  294. * Note that this cannot work on sandboxed processes under 4.1 and
  295. * higher, unless you called android_setCpuArm() before.
  296. */
  297. extern uint32_t android_getCpuIdArm(void);
  298. /* An ARM-specific variant of android_setCpu() that also allows you
  299. * to set the ARM CPUID field.
  300. */
  301. extern int android_setCpuArm(int cpu_count,
  302. uint64_t cpu_features,
  303. uint32_t cpu_id);
  304. #endif
  305. __END_DECLS
  306. #endif /* CPU_FEATURES_H */