cpu-features.h 11 KB

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