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