[msvc] Update csproj files (#4315)
[mono.git] / mono / utils / mono-hwcap-arm.c
1 /*
2  * mono-hwcap-arm.c: ARM hardware feature detection
3  *
4  * Authors:
5  *    Alex Rønne Petersen (alexrp@xamarin.com)
6  *    Elijah Taylor (elijahtaylor@google.com)
7  *    Miguel de Icaza (miguel@xamarin.com)
8  *    Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com)
9  *    Paolo Molaro (lupus@xamarin.com)
10  *    Rodrigo Kumpera (kumpera@gmail.com)
11  *    Sebastien Pouliot (sebastien@xamarin.com)
12  *    Zoltan Varga (vargaz@xamarin.com)
13  *
14  * Copyright 2003 Ximian, Inc.
15  * Copyright 2003-2011 Novell, Inc
16  * Copyright 2006 Broadcom
17  * Copyright 2007-2008 Andreas Faerber
18  * Copyright 2011-2013 Xamarin Inc
19  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
20  */
21
22 #include "mono/utils/mono-hwcap.h"
23
24 #if defined(HAVE_SYS_AUXV_H) && !defined(PLATFORM_ANDROID)
25 #include <sys/auxv.h>
26 #elif defined(__APPLE__)
27 #include <mach/machine.h>
28 #include <sys/sysctl.h>
29 #include <sys/types.h>
30 #else
31 #if defined (HAVE_SYS_UTSNAME_H)
32 #include <sys/utsname.h>
33 #endif
34 #include <stdio.h>
35 #endif
36
37 void
38 mono_hwcap_arch_init (void)
39 {
40 #if defined(HAVE_SYS_AUXV_H) && !defined(PLATFORM_ANDROID)
41         unsigned long hwcap;
42         unsigned long platform;
43
44         if ((hwcap = getauxval(AT_HWCAP))) {
45                 /* HWCAP_ARM_THUMB */
46                 if (hwcap & 0x00000004)
47                         mono_hwcap_arm_has_thumb = TRUE;
48
49                 /* HWCAP_ARM_VFP */
50                 if (hwcap & 0x00000040)
51                         mono_hwcap_arm_has_vfp = TRUE;
52
53                 /* HWCAP_ARM_VFPv3 */
54                 if (hwcap & 0x00002000)
55                         mono_hwcap_arm_has_vfp3 = TRUE;
56
57                 /* HWCAP_ARM_VFPv3D16 */
58                 if (hwcap & 0x00004000)
59                         mono_hwcap_arm_has_vfp3_d16 = TRUE;
60
61                 /* TODO: Find a way to detect Thumb 2. */
62         }
63
64         if ((platform = getauxval(AT_PLATFORM))) {
65                 const char *str = (const char *) platform;
66
67                 if (str [1] >= '5')
68                         mono_hwcap_arm_is_v5 = TRUE;
69
70                 if (str [1] >= '6')
71                         mono_hwcap_arm_is_v6 = TRUE;
72
73                 if (str [1] >= '7')
74                         mono_hwcap_arm_is_v7 = TRUE;
75
76                 /* TODO: Find a way to detect v7s. */
77         }
78 #elif defined(__APPLE__)
79         cpu_subtype_t sub_type;
80         size_t length = sizeof (sub_type);
81
82         sysctlbyname ("hw.cpusubtype", &sub_type, &length, NULL, 0);
83
84         if (sub_type == CPU_SUBTYPE_ARM_V5TEJ || sub_type == CPU_SUBTYPE_ARM_XSCALE) {
85                 mono_hwcap_arm_is_v5 = TRUE;
86         } else if (sub_type == CPU_SUBTYPE_ARM_V6) {
87                 mono_hwcap_arm_is_v5 = TRUE;
88                 mono_hwcap_arm_is_v6 = TRUE;
89         } else if (sub_type == CPU_SUBTYPE_ARM_V7 || sub_type == CPU_SUBTYPE_ARM_V7F || sub_type == CPU_SUBTYPE_ARM_V7K) {
90                 mono_hwcap_arm_is_v5 = TRUE;
91                 mono_hwcap_arm_is_v6 = TRUE;
92                 mono_hwcap_arm_is_v7 = TRUE;
93         }
94
95         /* TODO: Find a way to detect features like Thumb and VFP. */
96 #else
97         /* We can't use the auxiliary vector on Android due to
98          * permissions, so fall back to /proc/cpuinfo. We also
99          * hit this path if the target doesn't have sys/auxv.h.
100          */
101
102 #if defined (HAVE_SYS_UTSNAME_H)
103         struct utsname name;
104
105         /* Only fails if `name` is invalid (it isn't). */
106         g_assert (!uname (&name));
107
108         if (!strncmp (name.machine, "aarch64", 7) || !strncmp (name.machine, "armv8", 5)) {
109                 /*
110                  * We're a 32-bit program running on an ARMv8 system.
111                  * Whether the system is actually 32-bit or 64-bit
112                  * doesn't matter to us. The important thing is that
113                  * all 3 of ARMv8's execution states (A64, A32, T32)
114                  * are guaranteed to have all of the features that
115                  * we want to detect and use.
116                  *
117                  * We do this ARMv8 detection via uname () because
118                  * in the early days of ARMv8 on Linux, the
119                  * /proc/cpuinfo format was a disaster and there
120                  * were multiple (merged into mainline) attempts at
121                  * cleaning it up (read: breaking applications that
122                  * tried to rely on it). So now multiple ARMv8
123                  * systems in the wild have different /proc/cpuinfo
124                  * output, some of which are downright useless.
125                  *
126                  * So, when it comes to detecting ARMv8 in a 32-bit
127                  * program, it's better to just avoid /proc/cpuinfo
128                  * entirely. Maybe in a decade or two, we won't
129                  * have to worry about this mess that the Linux ARM
130                  * maintainers created. One can hope.
131                  */
132
133                 mono_hwcap_arm_is_v5 = TRUE;
134                 mono_hwcap_arm_is_v6 = TRUE;
135                 mono_hwcap_arm_is_v7 = TRUE;
136
137                 mono_hwcap_arm_has_vfp = TRUE;
138                 mono_hwcap_arm_has_vfp3 = TRUE;
139                 mono_hwcap_arm_has_vfp3_d16 = TRUE;
140
141                 mono_hwcap_arm_has_thumb = TRUE;
142                 mono_hwcap_arm_has_thumb2 = TRUE;
143         }
144 #endif
145
146         char buf [512];
147         char *line;
148
149         FILE *file = fopen ("/proc/cpuinfo", "r");
150
151         if (file) {
152                 while ((line = fgets (buf, 512, file))) {
153                         if (!strncmp (line, "Processor", 9) ||
154                             !strncmp (line, "model name", 10)) {
155                                 char *ver = strstr (line, "(v");
156
157                                 if (ver) {
158                                         if (ver [2] >= '5')
159                                                 mono_hwcap_arm_is_v5 = TRUE;
160
161                                         if (ver [2] >= '6')
162                                                 mono_hwcap_arm_is_v6 = TRUE;
163
164                                         if (ver [2] >= '7')
165                                                 mono_hwcap_arm_is_v7 = TRUE;
166
167                                         /* TODO: Find a way to detect v7s. */
168                                 }
169
170                                 continue;
171                         }
172
173                         if (!strncmp (line, "Features", 8)) {
174                                 if (strstr (line, "thumb"))
175                                         mono_hwcap_arm_has_thumb = TRUE;
176
177                                 /* TODO: Find a way to detect Thumb 2. */
178
179                                 if (strstr (line, "vfp"))
180                                         mono_hwcap_arm_has_vfp = TRUE;
181
182                                 if (strstr (line, "vfpv3"))
183                                         mono_hwcap_arm_has_vfp3 = TRUE;
184
185                                 if (strstr (line, "vfpv3-d16"))
186                                         mono_hwcap_arm_has_vfp3_d16 = TRUE;
187
188                                 continue;
189                         }
190                 }
191
192                 fclose (file);
193         }
194 #endif
195 }