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