2 * mono-hwcap-arm.c: ARM hardware feature detection
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)
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
21 #include "mono/utils/mono-hwcap-arm.h"
23 #if defined(HAVE_SYS_AUXV_H) && !defined(PLATFORM_ANDROID)
25 #elif defined(__APPLE__)
26 #include <mach/machine.h>
27 #include <sys/sysctl.h>
28 #include <sys/types.h>
33 gboolean mono_hwcap_arm_is_v5 = FALSE;
34 gboolean mono_hwcap_arm_is_v6 = FALSE;
35 gboolean mono_hwcap_arm_is_v7 = FALSE;
36 gboolean mono_hwcap_arm_has_vfp = FALSE;
37 gboolean mono_hwcap_arm_has_vfp3 = FALSE;
38 gboolean mono_hwcap_arm_has_vfp3_d16 = FALSE;
39 gboolean mono_hwcap_arm_has_thumb = FALSE;
40 gboolean mono_hwcap_arm_has_thumb2 = FALSE;
43 mono_hwcap_arch_init (void)
45 #if defined(HAVE_SYS_AUXV_H) && !defined(PLATFORM_ANDROID)
47 unsigned long platform;
49 if ((hwcap = getauxval(AT_HWCAP))) {
51 if (hwcap & 0x00000004)
52 mono_hwcap_arm_has_thumb = TRUE;
55 if (hwcap & 0x00000040)
56 mono_hwcap_arm_has_vfp = TRUE;
59 if (hwcap & 0x00002000)
60 mono_hwcap_arm_has_vfp3 = TRUE;
62 /* HWCAP_ARM_VFPv3D16 */
63 if (hwcap & 0x00004000)
64 mono_hwcap_arm_has_vfp3_d16 = TRUE;
66 /* TODO: Find a way to detect Thumb 2. */
69 if ((platform = getauxval(AT_PLATFORM))) {
70 const char *str = (const char *) platform;
73 mono_hwcap_arm_is_v5 = TRUE;
76 mono_hwcap_arm_is_v6 = TRUE;
79 mono_hwcap_arm_is_v7 = TRUE;
81 /* TODO: Find a way to detect v7s. */
83 #elif defined(__APPLE__)
84 cpu_subtype_t sub_type;
85 size_t length = sizeof (sub_type);
87 sysctlbyname ("hw.cpusubtype", &sub_type, &length, NULL, 0);
89 if (sub_type == CPU_SUBTYPE_ARM_V5TEJ || sub_type == CPU_SUBTYPE_ARM_XSCALE) {
90 mono_hwcap_arm_is_v5 = TRUE;
91 } else if (sub_type == CPU_SUBTYPE_ARM_V6) {
92 mono_hwcap_arm_is_v5 = TRUE;
93 mono_hwcap_arm_is_v6 = TRUE;
94 } else if (sub_type == CPU_SUBTYPE_ARM_V7 || sub_type == CPU_SUBTYPE_ARM_V7F || sub_type == CPU_SUBTYPE_ARM_V7K) {
95 mono_hwcap_arm_is_v5 = TRUE;
96 mono_hwcap_arm_is_v6 = TRUE;
97 mono_hwcap_arm_is_v7 = TRUE;
100 /* TODO: Find a way to detect features like Thumb and VFP. */
102 /* We can't use the auxiliary vector on Android due to
103 * permissions, so fall back to /proc/cpuinfo. We also
104 * hit this path if the target doesn't have sys/auxv.h.
110 FILE *file = fopen ("/proc/cpuinfo", "r");
113 while ((line = fgets (buf, 512, file))) {
114 if (!strncmp (line, "Processor", 9)) {
115 char *ver = strstr (line, "(v");
119 mono_hwcap_arm_is_v5 = TRUE;
122 mono_hwcap_arm_is_v6 = TRUE;
125 mono_hwcap_arm_is_v7 = TRUE;
127 /* TODO: Find a way to detect v7s. */
133 if (!strncmp (line, "Features", 8)) {
134 if (strstr (line, "thumb"))
135 mono_hwcap_arm_has_thumb = TRUE;
137 /* TODO: Find a way to detect Thumb 2. */
139 if (strstr (line, "vfp"))
140 mono_hwcap_arm_has_vfp = TRUE;
142 if (strstr (line, "vfpv3"))
143 mono_hwcap_arm_has_vfp3 = TRUE;
145 if (strstr (line, "vfpv3-d16"))
146 mono_hwcap_arm_has_vfp3_d16 = TRUE;
158 mono_hwcap_print(FILE *f)
160 g_fprintf (f, "mono_hwcap_arm_is_v5 = %i\n", mono_hwcap_arm_is_v5);
161 g_fprintf (f, "mono_hwcap_arm_is_v6 = %i\n", mono_hwcap_arm_is_v6);
162 g_fprintf (f, "mono_hwcap_arm_is_v7 = %i\n", mono_hwcap_arm_is_v7);
163 g_fprintf (f, "mono_hwcap_arm_has_vfp = %i\n", mono_hwcap_arm_has_vfp);
164 g_fprintf (f, "mono_hwcap_arm_has_vfp3 = %i\n", mono_hwcap_arm_has_vfp3);
165 g_fprintf (f, "mono_hwcap_arm_has_vfp3_d16 = %i\n", mono_hwcap_arm_has_vfp3_d16);
166 g_fprintf (f, "mono_hwcap_arm_has_thumb = %i\n", mono_hwcap_arm_has_thumb);
167 g_fprintf (f, "mono_hwcap_arm_has_thumb2 = %i\n", mono_hwcap_arm_has_thumb2);