Merge pull request #487 from mayerwin/patch-1
[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 #ifdef _MSC_VER
48 #define ARCH_STORE_REGS(ptr) __asm {    \
49                 __asm mov [ptr], edi \
50                 __asm mov [ptr+4], esi \
51                 __asm mov [ptr+8], ebx \
52                 __asm mov [ptr+12], edx \
53                 __asm mov [ptr+16], ecx \
54                 __asm mov [ptr+20], eax \
55                 __asm mov [ptr+24], ebp \
56                 __asm mov [ptr+28], esp \
57         }
58 #else
59 #define ARCH_STORE_REGS(ptr)    \
60         __asm__ __volatile__(   \
61                 "mov %%edi,0(%0)\n"     \
62                 "mov %%esi,4(%0)\n"     \
63                 "mov %%ebx,8(%0)\n"     \
64                 "mov %%edx,12(%0)\n"    \
65                 "mov %%ecx,16(%0)\n"    \
66                 "mov %%eax,20(%0)\n"    \
67                 "mov %%ebp,24(%0)\n"    \
68                 "mov %%esp,28(%0)\n"    \
69                 :                       \
70                 : "r" (ptr)     \
71         )
72 #endif
73 #endif
74
75 /*FIXME, move this to mono-sigcontext as this is generaly useful.*/
76 #define ARCH_SIGCTX_SP(ctx)    (UCONTEXT_REG_ESP ((ctx)))
77 #define ARCH_SIGCTX_IP(ctx)    (UCONTEXT_REG_EIP ((ctx)))
78
79 #elif defined(TARGET_AMD64)
80
81 #include <mono/utils/mono-context.h>
82
83 #define REDZONE_SIZE    128
84
85 #define ARCH_NUM_REGS 16
86 #define USE_MONO_CTX
87
88 /*FIXME, move this to mono-sigcontext as this is generaly useful.*/
89 #define ARCH_SIGCTX_SP(ctx)    (UCONTEXT_REG_RSP (ctx))
90 #define ARCH_SIGCTX_IP(ctx)    (UCONTEXT_REG_RIP (ctx))
91
92 #elif defined(TARGET_PPC)
93
94 #define REDZONE_SIZE    224
95
96 #define ARCH_NUM_REGS 32
97 #ifdef __APPLE__
98 #define ARCH_STORE_REGS(ptr)    \
99         __asm__ __volatile__(   \
100                 "stmw r0, 0(%0)\n"      \
101                 :                       \
102                 : "b" (ptr)             \
103         )
104 #else
105 #define ARCH_STORE_REGS(ptr)    \
106         __asm__ __volatile__(   \
107                 "stmw 0, 0(%0)\n"       \
108                 :                       \
109                 : "b" (ptr)             \
110         )
111 #endif
112 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_Rn((ctx), 1))
113 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_NIP((ctx)))
114 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
115         int __i;        \
116         for (__i = 0; __i < 32; ++__i)  \
117                 ((a)[__i]) = UCONTEXT_REG_Rn((ctx), __i);       \
118         } while (0)
119
120 #elif defined(TARGET_ARM)
121
122 #define REDZONE_SIZE    0
123 #define USE_MONO_CTX
124
125 /* We dont store ip, sp */
126 #define ARCH_NUM_REGS 14
127 #define ARCH_STORE_REGS(ptr)            \
128         __asm__ __volatile__(                   \
129                 "push {lr}\n"                           \
130                 "mov lr, %0\n"                          \
131                 "stmia lr!, {r0-r12}\n"         \
132                 "pop {lr}\n"                            \
133                 :                                                       \
134                 : "r" (ptr)                                     \
135         )
136
137 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_SP((ctx)))
138 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_PC((ctx)))
139 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {                       \
140         ((a)[0]) = (gpointer) (UCONTEXT_REG_R0((ctx)));         \
141         ((a)[1]) = (gpointer) (UCONTEXT_REG_R1((ctx)));         \
142         ((a)[2]) = (gpointer) (UCONTEXT_REG_R2((ctx)));         \
143         ((a)[3]) = (gpointer) (UCONTEXT_REG_R3((ctx)));         \
144         ((a)[4]) = (gpointer) (UCONTEXT_REG_R4((ctx)));         \
145         ((a)[5]) = (gpointer) (UCONTEXT_REG_R5((ctx)));         \
146         ((a)[6]) = (gpointer) (UCONTEXT_REG_R6((ctx)));         \
147         ((a)[7]) = (gpointer) (UCONTEXT_REG_R7((ctx)));         \
148         ((a)[8]) = (gpointer) (UCONTEXT_REG_R8((ctx)));         \
149         ((a)[9]) = (gpointer) (UCONTEXT_REG_R9((ctx)));         \
150         ((a)[10]) = (gpointer) (UCONTEXT_REG_R10((ctx)));       \
151         ((a)[11]) = (gpointer) (UCONTEXT_REG_R11((ctx)));       \
152         ((a)[12]) = (gpointer) (UCONTEXT_REG_R12((ctx)));       \
153         ((a)[13]) = (gpointer) (UCONTEXT_REG_LR((ctx)));        \
154         } while (0)
155
156 #elif defined(__mips__)
157
158 #define REDZONE_SIZE    0
159
160 #define USE_MONO_CTX
161 #define ARCH_NUM_REGS 32
162
163 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_GREGS((ctx))[29])
164 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_PC((ctx)))
165
166 #elif defined(__s390x__)
167
168 #define REDZONE_SIZE    0
169
170 #include <mono/utils/mono-context.h>
171
172 #define USE_MONO_CTX
173 #define ARCH_NUM_REGS 16        
174 #define ARCH_SIGCTX_SP(ctx)     ((UCONTEXT_GREGS((ctx))) [15])
175 #define ARCH_SIGCTX_IP(ctx)     ((ucontext_t *) (ctx))->uc_mcontext.psw.addr
176
177 #elif defined(__sparc__)
178
179 #define REDZONE_SIZE    0
180
181 /* Don't bother with %g0 (%r0), it's always hard-coded to zero */
182 #define ARCH_NUM_REGS 15        
183 #ifdef __sparcv9
184 #define ARCH_STORE_REGS(ptr)    \
185         __asm__ __volatile__(   \
186                 "st %%g1,[%0]\n\t"      \
187                 "st %%g2,[%0+0x08]\n\t" \
188                 "st %%g3,[%0+0x10]\n\t" \
189                 "st %%g4,[%0+0x18]\n\t" \
190                 "st %%g5,[%0+0x20]\n\t" \
191                 "st %%g6,[%0+0x28]\n\t" \
192                 "st %%g7,[%0+0x30]\n\t" \
193                 "st %%o0,[%0+0x38]\n\t" \
194                 "st %%o1,[%0+0x40]\n\t" \
195                 "st %%o2,[%0+0x48]\n\t" \
196                 "st %%o3,[%0+0x50]\n\t" \
197                 "st %%o4,[%0+0x58]\n\t" \
198                 "st %%o5,[%0+0x60]\n\t" \
199                 "st %%o6,[%0+0x68]\n\t" \
200                 "st %%o7,[%0+0x70]\n\t" \
201                 :                       \
202                 : "r" (ptr)             \
203                 : "memory"                      \
204         )
205 #else
206 #define ARCH_STORE_REGS(ptr)    \
207         __asm__ __volatile__(   \
208                 "st %%g1,[%0]\n\t"      \
209                 "st %%g2,[%0+0x04]\n\t" \
210                 "st %%g3,[%0+0x08]\n\t" \
211                 "st %%g4,[%0+0x0c]\n\t" \
212                 "st %%g5,[%0+0x10]\n\t" \
213                 "st %%g6,[%0+0x14]\n\t" \
214                 "st %%g7,[%0+0x18]\n\t" \
215                 "st %%o0,[%0+0x1c]\n\t" \
216                 "st %%o1,[%0+0x20]\n\t" \
217                 "st %%o2,[%0+0x24]\n\t" \
218                 "st %%o3,[%0+0x28]\n\t" \
219                 "st %%o4,[%0+0x2c]\n\t" \
220                 "st %%o5,[%0+0x30]\n\t" \
221                 "st %%o6,[%0+0x34]\n\t" \
222                 "st %%o7,[%0+0x38]\n\t" \
223                 :                       \
224                 : "r" (ptr)             \
225                 : "memory"                      \
226         )
227 #endif
228
229 #define ARCH_SIGCTX_SP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_SP])
230 #define ARCH_SIGCTX_IP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_PC])
231 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
232         (a)[0] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G1]);        \
233         (a)[1] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G2]);        \
234         (a)[2] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G3]);        \
235         (a)[3] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G4]);        \
236         (a)[4] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G5]);        \
237         (a)[5] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G6]);        \
238         (a)[6] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G7]);        \
239         (a)[7] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O0]);        \
240         (a)[8] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O1]);        \
241         (a)[9] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O2]);        \
242         (a)[10] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O3]);       \
243         (a)[11] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O4]);       \
244         (a)[12] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O5]);       \
245         (a)[13] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O6]);       \
246         (a)[14] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O7]);       \
247         } while (0)
248
249 #endif
250
251 #endif /* __MONO_SGENARCHDEP_H__ */