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