5e7b5f8f1a3975641f76ea18f0fbafbad1659488
[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(__mips__)
132
133 #define REDZONE_SIZE    0
134
135 #define USE_MONO_CTX
136 #define ARCH_NUM_REGS 32
137
138 /*
139  * These casts are necessary since glibc always makes the
140  * gregs 64-bit values in userland.
141  */
142 #define ARCH_SIGCTX_SP(ctx)     ((gsize) UCONTEXT_GREGS((ctx))[29])
143 #define ARCH_SIGCTX_IP(ctx)     ((gsize) UCONTEXT_REG_PC((ctx)))
144
145 #elif defined(__s390x__)
146
147 #define REDZONE_SIZE    0
148
149 #include <mono/utils/mono-context.h>
150
151 #define USE_MONO_CTX
152 #define ARCH_NUM_REGS 16        
153 #define ARCH_SIGCTX_SP(ctx)     ((UCONTEXT_GREGS((ctx))) [15])
154 #define ARCH_SIGCTX_IP(ctx)     ((ucontext_t *) (ctx))->uc_mcontext.psw.addr
155
156 #elif defined(__sparc__)
157
158 #define REDZONE_SIZE    0
159
160 /* Don't bother with %g0 (%r0), it's always hard-coded to zero */
161 #define ARCH_NUM_REGS 15        
162 #ifdef __sparcv9
163 #define ARCH_STORE_REGS(ptr)    \
164         __asm__ __volatile__(   \
165                 "st %%g1,[%0]\n\t"      \
166                 "st %%g2,[%0+0x08]\n\t" \
167                 "st %%g3,[%0+0x10]\n\t" \
168                 "st %%g4,[%0+0x18]\n\t" \
169                 "st %%g5,[%0+0x20]\n\t" \
170                 "st %%g6,[%0+0x28]\n\t" \
171                 "st %%g7,[%0+0x30]\n\t" \
172                 "st %%o0,[%0+0x38]\n\t" \
173                 "st %%o1,[%0+0x40]\n\t" \
174                 "st %%o2,[%0+0x48]\n\t" \
175                 "st %%o3,[%0+0x50]\n\t" \
176                 "st %%o4,[%0+0x58]\n\t" \
177                 "st %%o5,[%0+0x60]\n\t" \
178                 "st %%o6,[%0+0x68]\n\t" \
179                 "st %%o7,[%0+0x70]\n\t" \
180                 :                       \
181                 : "r" (ptr)             \
182                 : "memory"                      \
183         )
184 #else
185 #define ARCH_STORE_REGS(ptr)    \
186         __asm__ __volatile__(   \
187                 "st %%g1,[%0]\n\t"      \
188                 "st %%g2,[%0+0x04]\n\t" \
189                 "st %%g3,[%0+0x08]\n\t" \
190                 "st %%g4,[%0+0x0c]\n\t" \
191                 "st %%g5,[%0+0x10]\n\t" \
192                 "st %%g6,[%0+0x14]\n\t" \
193                 "st %%g7,[%0+0x18]\n\t" \
194                 "st %%o0,[%0+0x1c]\n\t" \
195                 "st %%o1,[%0+0x20]\n\t" \
196                 "st %%o2,[%0+0x24]\n\t" \
197                 "st %%o3,[%0+0x28]\n\t" \
198                 "st %%o4,[%0+0x2c]\n\t" \
199                 "st %%o5,[%0+0x30]\n\t" \
200                 "st %%o6,[%0+0x34]\n\t" \
201                 "st %%o7,[%0+0x38]\n\t" \
202                 :                       \
203                 : "r" (ptr)             \
204                 : "memory"                      \
205         )
206 #endif
207
208 #ifndef REG_SP
209 #define REG_SP REG_O6
210 #endif
211
212 #define ARCH_SIGCTX_SP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_SP])
213 #define ARCH_SIGCTX_IP(ctx)     (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_PC])
214 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
215         (a)[0] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G1]);        \
216         (a)[1] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G2]);        \
217         (a)[2] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G3]);        \
218         (a)[3] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G4]);        \
219         (a)[4] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G5]);        \
220         (a)[5] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G6]);        \
221         (a)[6] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_G7]);        \
222         (a)[7] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O0]);        \
223         (a)[8] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O1]);        \
224         (a)[9] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O2]);        \
225         (a)[10] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O3]);       \
226         (a)[11] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O4]);       \
227         (a)[12] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O5]);       \
228         (a)[13] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O6]);       \
229         (a)[14] = (gpointer) (((ucontext_t *)(ctx))->uc_mcontext.gregs [REG_O7]);       \
230         } while (0)
231
232 #endif
233
234 #endif /* __MONO_SGENARCHDEP_H__ */