Merge pull request #409 from Alkarex/patch-1
[mono.git] / mono / metadata / sgen-archdep.h
1 /*
2  * SGen is licensed under the terms of the MIT X11 license
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * 
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  * 
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 #ifndef __MONO_SGENARCHDEP_H__
27 #define __MONO_SGENARCHDEP_H__
28
29 #include <mono/utils/mono-sigcontext.h>
30
31 #if defined(MONO_CROSS_COMPILE)
32
33 #define REDZONE_SIZE    0
34
35 #define ARCH_NUM_REGS 0
36 #define ARCH_STORE_REGS(ptr)
37 #define ARCH_SIGCTX_SP(ctx) NULL
38 #define ARCH_SIGCTX_IP(ctx) NULL
39 #define ARCH_COPY_SIGCTX_REGS(a,ctx)
40
41 #elif defined(TARGET_X86)
42
43 #include <mono/utils/mono-context.h>
44
45 #define REDZONE_SIZE    0
46
47 #define ARCH_NUM_REGS 8
48
49 #ifdef MONO_ARCH_HAS_MONO_CONTEXT
50 #define USE_MONO_CTX
51 #else
52 #ifdef _MSC_VER
53 #define ARCH_STORE_REGS(ptr) __asm {    \
54                 __asm mov [ptr], edi \
55                 __asm mov [ptr+4], esi \
56                 __asm mov [ptr+8], ebx \
57                 __asm mov [ptr+12], edx \
58                 __asm mov [ptr+16], ecx \
59                 __asm mov [ptr+20], eax \
60                 __asm mov [ptr+24], ebp \
61                 __asm mov [ptr+28], esp \
62         }
63 #else
64 #define ARCH_STORE_REGS(ptr)    \
65         __asm__ __volatile__(   \
66                 "mov %%edi,0(%0)\n"     \
67                 "mov %%esi,4(%0)\n"     \
68                 "mov %%ebx,8(%0)\n"     \
69                 "mov %%edx,12(%0)\n"    \
70                 "mov %%ecx,16(%0)\n"    \
71                 "mov %%eax,20(%0)\n"    \
72                 "mov %%ebp,24(%0)\n"    \
73                 "mov %%esp,28(%0)\n"    \
74                 :                       \
75                 : "r" (ptr)     \
76         )
77 #endif
78 #endif
79
80 /*FIXME, move this to mono-sigcontext as this is generaly useful.*/
81 #define ARCH_SIGCTX_SP(ctx)    (UCONTEXT_REG_ESP ((ctx)))
82 #define ARCH_SIGCTX_IP(ctx)    (UCONTEXT_REG_EIP ((ctx)))
83
84 #elif defined(TARGET_AMD64)
85
86 #include <mono/utils/mono-context.h>
87
88 #define REDZONE_SIZE    128
89
90 #define ARCH_NUM_REGS 16
91 #define USE_MONO_CTX
92
93 /*FIXME, move this to mono-sigcontext as this is generaly useful.*/
94 #define ARCH_SIGCTX_SP(ctx)    (UCONTEXT_REG_RSP (ctx))
95 #define ARCH_SIGCTX_IP(ctx)    (UCONTEXT_REG_RIP (ctx))
96
97 #elif defined(TARGET_PPC)
98
99 #define REDZONE_SIZE    224
100
101 #define ARCH_NUM_REGS 32
102 #ifdef __APPLE__
103 #define ARCH_STORE_REGS(ptr)    \
104         __asm__ __volatile__(   \
105                 "stmw r0, 0(%0)\n"      \
106                 :                       \
107                 : "b" (ptr)             \
108         )
109 #else
110 #define ARCH_STORE_REGS(ptr)    \
111         __asm__ __volatile__(   \
112                 "stmw 0, 0(%0)\n"       \
113                 :                       \
114                 : "b" (ptr)             \
115         )
116 #endif
117 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_Rn((ctx), 1))
118 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_NIP((ctx)))
119 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
120         int __i;        \
121         for (__i = 0; __i < 32; ++__i)  \
122                 ((a)[__i]) = UCONTEXT_REG_Rn((ctx), __i);       \
123         } while (0)
124
125 #elif defined(TARGET_ARM)
126
127 #define REDZONE_SIZE    0
128 #define USE_MONO_CTX
129
130 /* We dont store ip, sp */
131 #define ARCH_NUM_REGS 14
132 #define ARCH_STORE_REGS(ptr)            \
133         __asm__ __volatile__(                   \
134                 "push {lr}\n"                           \
135                 "mov lr, %0\n"                          \
136                 "stmia lr!, {r0-r12}\n"         \
137                 "pop {lr}\n"                            \
138                 :                                                       \
139                 : "r" (ptr)                                     \
140         )
141
142 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_SP((ctx)))
143 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_PC((ctx)))
144 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {                       \
145         ((a)[0]) = (gpointer) (UCONTEXT_REG_R0((ctx)));         \
146         ((a)[1]) = (gpointer) (UCONTEXT_REG_R1((ctx)));         \
147         ((a)[2]) = (gpointer) (UCONTEXT_REG_R2((ctx)));         \
148         ((a)[3]) = (gpointer) (UCONTEXT_REG_R3((ctx)));         \
149         ((a)[4]) = (gpointer) (UCONTEXT_REG_R4((ctx)));         \
150         ((a)[5]) = (gpointer) (UCONTEXT_REG_R5((ctx)));         \
151         ((a)[6]) = (gpointer) (UCONTEXT_REG_R6((ctx)));         \
152         ((a)[7]) = (gpointer) (UCONTEXT_REG_R7((ctx)));         \
153         ((a)[8]) = (gpointer) (UCONTEXT_REG_R8((ctx)));         \
154         ((a)[9]) = (gpointer) (UCONTEXT_REG_R9((ctx)));         \
155         ((a)[10]) = (gpointer) (UCONTEXT_REG_R10((ctx)));       \
156         ((a)[11]) = (gpointer) (UCONTEXT_REG_R11((ctx)));       \
157         ((a)[12]) = (gpointer) (UCONTEXT_REG_R12((ctx)));       \
158         ((a)[13]) = (gpointer) (UCONTEXT_REG_LR((ctx)));        \
159         } while (0)
160
161 #elif defined(__mips__)
162
163 #define REDZONE_SIZE    0
164
165 #define USE_MONO_CTX
166 #define ARCH_NUM_REGS 32
167
168 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_GREGS((ctx))[29])
169 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_PC((ctx)))
170
171 #elif defined(__s390x__)
172
173 #define REDZONE_SIZE    0
174
175 #include <mono/utils/mono-context.h>
176
177 #define USE_MONO_CTX
178 #define ARCH_NUM_REGS 16        
179 #define ARCH_SIGCTX_SP(ctx)     ((UCONTEXT_GREGS((ctx))) [15])
180 #define ARCH_SIGCTX_IP(ctx)     ((ucontext_t *) (ctx))->uc_mcontext.psw.addr
181
182 #elif defined(__sparc__)
183
184 #define REDZONE_SIZE    0
185
186 /* Don't bother with %g0 (%r0), it's always hard-coded to zero */
187 #define ARCH_NUM_REGS 15        
188 #ifdef __sparcv9
189 #define ARCH_STORE_REGS(ptr)    \
190         __asm__ __volatile__(   \
191                 "st %%g1,[%0]\n\t"      \
192                 "st %%g2,[%0+0x08]\n\t" \
193                 "st %%g3,[%0+0x10]\n\t" \
194                 "st %%g4,[%0+0x18]\n\t" \
195                 "st %%g5,[%0+0x20]\n\t" \
196                 "st %%g6,[%0+0x28]\n\t" \
197                 "st %%g7,[%0+0x30]\n\t" \
198                 "st %%o0,[%0+0x38]\n\t" \
199                 "st %%o1,[%0+0x40]\n\t" \
200                 "st %%o2,[%0+0x48]\n\t" \
201                 "st %%o3,[%0+0x50]\n\t" \
202                 "st %%o4,[%0+0x58]\n\t" \
203                 "st %%o5,[%0+0x60]\n\t" \
204                 "st %%o6,[%0+0x68]\n\t" \
205                 "st %%o7,[%0+0x70]\n\t" \
206                 :                       \
207                 : "r" (ptr)             \
208                 : "memory"                      \
209         )
210 #else
211 #define ARCH_STORE_REGS(ptr)    \
212         __asm__ __volatile__(   \
213                 "st %%g1,[%0]\n\t"      \
214                 "st %%g2,[%0+0x04]\n\t" \
215                 "st %%g3,[%0+0x08]\n\t" \
216                 "st %%g4,[%0+0x0c]\n\t" \
217                 "st %%g5,[%0+0x10]\n\t" \
218                 "st %%g6,[%0+0x14]\n\t" \
219                 "st %%g7,[%0+0x18]\n\t" \
220                 "st %%o0,[%0+0x1c]\n\t" \
221                 "st %%o1,[%0+0x20]\n\t" \
222                 "st %%o2,[%0+0x24]\n\t" \
223                 "st %%o3,[%0+0x28]\n\t" \
224                 "st %%o4,[%0+0x2c]\n\t" \
225                 "st %%o5,[%0+0x30]\n\t" \
226                 "st %%o6,[%0+0x34]\n\t" \
227                 "st %%o7,[%0+0x38]\n\t" \
228                 :                       \
229                 : "r" (ptr)             \
230                 : "memory"                      \
231         )
232 #endif
233
234 #define ARCH_SIGCTX_SP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_SP])
235 #define ARCH_SIGCTX_IP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_PC])
236 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
237         (a)[0] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G1]);        \
238         (a)[1] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G2]);        \
239         (a)[2] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G3]);        \
240         (a)[3] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G4]);        \
241         (a)[4] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G5]);        \
242         (a)[5] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G6]);        \
243         (a)[6] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G7]);        \
244         (a)[7] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O0]);        \
245         (a)[8] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O1]);        \
246         (a)[9] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O2]);        \
247         (a)[10] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O3]);       \
248         (a)[11] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O4]);       \
249         (a)[12] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O5]);       \
250         (a)[13] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O6]);       \
251         (a)[14] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O7]);       \
252         } while (0)
253
254 #endif
255
256 #endif /* __MONO_SGENARCHDEP_H__ */