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