Merge pull request #681 from tritao/dll-api
[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  */
20
21 #include "mono/utils/mono-hwcap-arm.h"
22
23 #if defined(HAVE_SYS_AUXV_H) && !defined(PLATFORM_ANDROID)
24 #include <sys/auxv.h>
25 #elif defined(__APPLE__)
26 #include <mach/machine.h>
27 #include <sys/sysctl.h>
28 #include <sys/types.h>
29 #else
30 #include <stdio.h>
31 #endif
32
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_is_v7s = FALSE;
37 gboolean mono_hwcap_arm_has_vfp = FALSE;
38 gboolean mono_hwcap_arm_has_thumb = FALSE;
39 gboolean mono_hwcap_arm_has_thumb2 = FALSE;
40
41 #if defined(MONO_CROSS_COMPILE)
42 void
43 mono_hwcap_arch_init (void)
44 {
45 }
46 #else
47 void
48 mono_hwcap_arch_init (void)
49 {
50 #if defined(HAVE_SYS_AUXV_H) && !defined(PLATFORM_ANDROID)
51         unsigned long hwcap;
52         unsigned long platform;
53
54         if ((hwcap = getauxval(AT_HWCAP))) {
55                 /* HWCAP_ARM_THUMB */
56                 if (hwcap & 0x00000004)
57                         mono_hwcap_arm_has_thumb = TRUE;
58
59                 /* HWCAP_ARM_VFP */
60                 if (hwcap & 0x00000064)
61                         mono_hwcap_arm_has_vfp = TRUE;
62
63                 /* TODO: Find a way to detect Thumb 2. */
64         }
65
66         if ((platform = getauxval(AT_PLATFORM))) {
67                 const char *str = (const char *) platform;
68
69                 if (str [1] >= '5')
70                         mono_hwcap_arm_is_v5 = TRUE;
71
72                 if (str [1] >= '6')
73                         mono_hwcap_arm_is_v6 = TRUE;
74
75                 if (str [1] >= '7')
76                         mono_hwcap_arm_is_v7 = TRUE;
77
78                 /* TODO: Find a way to detect v7s. */
79         }
80 #elif defined(__APPLE__)
81         cpu_subtype_t sub_type;
82         size_t length = sizeof (sub_type);
83
84         sysctlbyname ("hw.cpusubtype", &sub_type, &length, NULL, 0);
85
86         if (sub_type == CPU_SUBTYPE_ARM_V5TEJ || sub_type == CPU_SUBTYPE_ARM_XSCALE) {
87                 mono_hwcap_arm_is_v5 = TRUE;
88         } else if (sub_type == CPU_SUBTYPE_ARM_V6) {
89                 mono_hwcap_arm_is_v5 = TRUE;
90                 mono_hwcap_arm_is_v6 = TRUE;
91         } else if (sub_type == CPU_SUBTYPE_ARM_V7 || sub_type == CPU_SUBTYPE_ARM_V7F || sub_type == CPU_SUBTYPE_ARM_V7K) {
92                 mono_hwcap_arm_is_v5 = TRUE;
93                 mono_hwcap_arm_is_v6 = TRUE;
94                 mono_hwcap_arm_is_v7 = TRUE;
95         }
96
97         /* TODO: Find a way to detect features like Thumb and VFP. */
98 #else
99         /* We can't use the auxiliary vector on Android due to
100          * permissions, so fall back to /proc/cpuinfo. We also
101          * hit this path if the target doesn't have sys/auxv.h.
102          */
103
104         char buf [512];
105         char *line;
106
107         FILE *file = fopen ("/proc/cpuinfo", "r");
108
109         if (file) {
110                 while ((line = fgets (buf, 512, file))) {
111                         if (!strncmp (line, "Processor", 9)) {
112                                 char *ver = strstr (line, "(v");
113
114                                 if (ver) {
115                                         if (ver [2] >= '5')
116                                                 mono_hwcap_arm_is_v5 = TRUE;
117
118                                         if (ver [2] >= '6')
119                                                 mono_hwcap_arm_is_v6 = TRUE;
120
121                                         if (ver [2] >= '7')
122                                                 mono_hwcap_arm_is_v7 = TRUE;
123
124                                         /* TODO: Find a way to detect v7s. */
125                                 }
126
127                                 continue;
128                         }
129
130                         if (!strncmp (line, "Features", 8)) {
131                                 if (strstr (line, "thumb"))
132                                         mono_hwcap_arm_has_thumb = TRUE;
133
134                                 /* TODO: Find a way to detect Thumb 2. */
135
136                                 if (strstr (line, "vfp"))
137                                         mono_hwcap_arm_has_vfp = TRUE;
138
139                                 continue;
140                         }
141                 }
142
143                 fclose (file);
144         }
145 #endif
146 }
147 #endif
148
149 void
150 mono_hwcap_print(FILE *f)
151 {
152         g_fprintf (f, "mono_hwcap_arm_is_v5 = %i\n", mono_hwcap_arm_is_v5);
153         g_fprintf (f, "mono_hwcap_arm_is_v6 = %i\n", mono_hwcap_arm_is_v6);
154         g_fprintf (f, "mono_hwcap_arm_is_v7 = %i\n", mono_hwcap_arm_is_v7);
155         g_fprintf (f, "mono_hwcap_arm_is_v7s = %i\n", mono_hwcap_arm_is_v7s);
156         g_fprintf (f, "mono_hwcap_arm_has_vfp = %i\n", mono_hwcap_arm_has_vfp);
157         g_fprintf (f, "mono_hwcap_arm_has_thumb = %i\n", mono_hwcap_arm_has_thumb);
158         g_fprintf (f, "mono_hwcap_arm_has_thumb2 = %i\n", mono_hwcap_arm_has_thumb2);
159 }