Revert "Revert "Merge branch 'master' of https://github.com/mono/mono""
[mono.git] / mono / metadata / sgen-archdep.h
1 /*
2  * sgen-archdep.h: Architecture dependent parts of SGen.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright (C) 2012 Xamarin Inc
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License 2.0 as published by the Free Software Foundation;
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License 2.0 along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 #ifndef __MONO_SGENARCHDEP_H__
22 #define __MONO_SGENARCHDEP_H__
23
24 #include <mono/utils/mono-sigcontext.h>
25
26 #if defined(MONO_CROSS_COMPILE)
27
28 #define REDZONE_SIZE    0
29
30 #define ARCH_NUM_REGS 0
31 #define ARCH_STORE_REGS(ptr)
32 #define ARCH_SIGCTX_SP(ctx) NULL
33 #define ARCH_SIGCTX_IP(ctx) NULL
34 #define ARCH_COPY_SIGCTX_REGS(a,ctx)
35
36 #elif defined(TARGET_X86)
37
38 #include <mono/utils/mono-context.h>
39
40 #define REDZONE_SIZE    0
41
42 #define ARCH_NUM_REGS 8
43
44 #ifdef MONO_ARCH_HAS_MONO_CONTEXT
45 #define USE_MONO_CTX
46 #else
47 #error 0
48 #endif
49
50 /*FIXME, move this to mono-sigcontext as this is generaly useful.*/
51 #define ARCH_SIGCTX_SP(ctx)    (UCONTEXT_REG_ESP ((ctx)))
52 #define ARCH_SIGCTX_IP(ctx)    (UCONTEXT_REG_EIP ((ctx)))
53
54 #elif defined(TARGET_AMD64)
55
56 #include <mono/utils/mono-context.h>
57
58 #define REDZONE_SIZE    128
59
60 #define ARCH_NUM_REGS 16
61 #define USE_MONO_CTX
62
63 /*FIXME, move this to mono-sigcontext as this is generaly useful.*/
64 #define ARCH_SIGCTX_SP(ctx)    (UCONTEXT_REG_RSP (ctx))
65 #define ARCH_SIGCTX_IP(ctx)    (UCONTEXT_REG_RIP (ctx))
66
67 #elif defined(TARGET_POWERPC)
68
69 #define REDZONE_SIZE    224
70
71 #define ARCH_NUM_REGS 32
72 #ifdef __APPLE__
73 #define ARCH_STORE_REGS(ptr)    \
74         __asm__ __volatile__(   \
75                 "stmw r0, 0(%0)\n"      \
76                 :                       \
77                 : "b" (ptr)             \
78         )
79 #else
80 #define ARCH_STORE_REGS(ptr)    \
81         __asm__ __volatile__(   \
82                 "stmw 0, 0(%0)\n"       \
83                 :                       \
84                 : "b" (ptr)             \
85         )
86 #endif
87 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_Rn((ctx), 1))
88 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_NIP((ctx)))
89 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
90         int __i;        \
91         for (__i = 0; __i < 32; ++__i)  \
92                 ((a)[__i]) = (gpointer) UCONTEXT_REG_Rn((ctx), __i);    \
93         } while (0)
94
95 #elif defined(TARGET_ARM)
96
97 #define REDZONE_SIZE    0
98 #define USE_MONO_CTX
99
100 /* We dont store ip, sp */
101 #define ARCH_NUM_REGS 14
102 #define ARCH_STORE_REGS(ptr)            \
103         __asm__ __volatile__(                   \
104                 "push {lr}\n"                           \
105                 "mov lr, %0\n"                          \
106                 "stmia lr!, {r0-r12}\n"         \
107                 "pop {lr}\n"                            \
108                 :                                                       \
109                 : "r" (ptr)                                     \
110         )
111
112 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_SP((ctx)))
113 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_PC((ctx)))
114 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {                       \
115         ((a)[0]) = (gpointer) (UCONTEXT_REG_R0((ctx)));         \
116         ((a)[1]) = (gpointer) (UCONTEXT_REG_R1((ctx)));         \
117         ((a)[2]) = (gpointer) (UCONTEXT_REG_R2((ctx)));         \
118         ((a)[3]) = (gpointer) (UCONTEXT_REG_R3((ctx)));         \
119         ((a)[4]) = (gpointer) (UCONTEXT_REG_R4((ctx)));         \
120         ((a)[5]) = (gpointer) (UCONTEXT_REG_R5((ctx)));         \
121         ((a)[6]) = (gpointer) (UCONTEXT_REG_R6((ctx)));         \
122         ((a)[7]) = (gpointer) (UCONTEXT_REG_R7((ctx)));         \
123         ((a)[8]) = (gpointer) (UCONTEXT_REG_R8((ctx)));         \
124         ((a)[9]) = (gpointer) (UCONTEXT_REG_R9((ctx)));         \
125         ((a)[10]) = (gpointer) (UCONTEXT_REG_R10((ctx)));       \
126         ((a)[11]) = (gpointer) (UCONTEXT_REG_R11((ctx)));       \
127         ((a)[12]) = (gpointer) (UCONTEXT_REG_R12((ctx)));       \
128         ((a)[13]) = (gpointer) (UCONTEXT_REG_LR((ctx)));        \
129         } while (0)
130
131 #elif defined(TARGET_ARM64)
132
133 #include <mono/utils/mono-sigcontext.h>
134
135 #ifdef __linux__
136 #define REDZONE_SIZE    0
137 #elif defined(__APPLE__)
138 #define REDZONE_SIZE    128
139 #else
140 #error "Not implemented."
141 #endif
142 #define USE_MONO_CTX
143 #define ARCH_NUM_REGS 31
144
145 #define ARCH_STORE_REGS(ptr) do { g_assert_not_reached (); } while (0)
146
147 #define ARCH_SIGCTX_SP(ctx)     UCONTEXT_REG_SP (ctx)
148 #define ARCH_SIGCTX_IP(ctx)     UCONTEXT_REG_PC (ctx)
149 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do { g_assert_not_reached (); } while (0)
150
151 #elif defined(__mips__)
152
153 #define REDZONE_SIZE    0
154
155 #define USE_MONO_CTX
156 #define ARCH_NUM_REGS 32
157
158 /*
159  * These casts are necessary since glibc always makes the
160  * gregs 64-bit values in userland.
161  */
162 #define ARCH_SIGCTX_SP(ctx)     ((gsize) UCONTEXT_GREGS((ctx))[29])
163 #define ARCH_SIGCTX_IP(ctx)     ((gsize) UCONTEXT_REG_PC((ctx)))
164
165 #elif defined(__s390x__)
166
167 #define REDZONE_SIZE    0
168
169 #include <mono/utils/mono-context.h>
170
171 #define USE_MONO_CTX
172 #define ARCH_NUM_REGS 16        
173 #define ARCH_SIGCTX_SP(ctx)     ((UCONTEXT_GREGS((ctx))) [15])
174 #define ARCH_SIGCTX_IP(ctx)     ((ucontext_t *) (ctx))->uc_mcontext.psw.addr
175
176 #elif defined(__sparc__)
177
178 #define REDZONE_SIZE    0
179
180 /* Don't bother with %g0 (%r0), it's always hard-coded to zero */
181 #define ARCH_NUM_REGS 15        
182 #ifdef __sparcv9
183 #define ARCH_STORE_REGS(ptr)    \
184         __asm__ __volatile__(   \
185                 "st %%g1,[%0]\n\t"      \
186                 "st %%g2,[%0+0x08]\n\t" \
187                 "st %%g3,[%0+0x10]\n\t" \
188                 "st %%g4,[%0+0x18]\n\t" \
189                 "st %%g5,[%0+0x20]\n\t" \
190                 "st %%g6,[%0+0x28]\n\t" \
191                 "st %%g7,[%0+0x30]\n\t" \
192                 "st %%o0,[%0+0x38]\n\t" \
193                 "st %%o1,[%0+0x40]\n\t" \
194                 "st %%o2,[%0+0x48]\n\t" \
195                 "st %%o3,[%0+0x50]\n\t" \
196                 "st %%o4,[%0+0x58]\n\t" \
197                 "st %%o5,[%0+0x60]\n\t" \
198                 "st %%o6,[%0+0x68]\n\t" \
199                 "st %%o7,[%0+0x70]\n\t" \
200                 :                       \
201                 : "r" (ptr)             \
202                 : "memory"                      \
203         )
204 #else
205 #define ARCH_STORE_REGS(ptr)    \
206         __asm__ __volatile__(   \
207                 "st %%g1,[%0]\n\t"      \
208                 "st %%g2,[%0+0x04]\n\t" \
209                 "st %%g3,[%0+0x08]\n\t" \
210                 "st %%g4,[%0+0x0c]\n\t" \
211                 "st %%g5,[%0+0x10]\n\t" \
212                 "st %%g6,[%0+0x14]\n\t" \
213                 "st %%g7,[%0+0x18]\n\t" \
214                 "st %%o0,[%0+0x1c]\n\t" \
215                 "st %%o1,[%0+0x20]\n\t" \
216                 "st %%o2,[%0+0x24]\n\t" \
217                 "st %%o3,[%0+0x28]\n\t" \
218                 "st %%o4,[%0+0x2c]\n\t" \
219                 "st %%o5,[%0+0x30]\n\t" \
220                 "st %%o6,[%0+0x34]\n\t" \
221                 "st %%o7,[%0+0x38]\n\t" \
222                 :                       \
223                 : "r" (ptr)             \
224                 : "memory"                      \
225         )
226 #endif
227
228 #ifndef REG_SP
229 #define REG_SP REG_O6
230 #endif
231
232 #define ARCH_SIGCTX_SP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_SP])
233 #define ARCH_SIGCTX_IP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_PC])
234 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
235         (a)[0] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G1]);        \
236         (a)[1] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G2]);        \
237         (a)[2] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G3]);        \
238         (a)[3] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G4]);        \
239         (a)[4] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G5]);        \
240         (a)[5] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G6]);        \
241         (a)[6] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G7]);        \
242         (a)[7] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O0]);        \
243         (a)[8] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O1]);        \
244         (a)[9] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O2]);        \
245         (a)[10] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O3]);       \
246         (a)[11] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O4]);       \
247         (a)[12] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O5]);       \
248         (a)[13] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O6]);       \
249         (a)[14] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O7]);       \
250         } while (0)
251
252 #endif
253
254 #endif /* __MONO_SGENARCHDEP_H__ */