Merge branch 'sgen-android'
[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 #ifdef __i386__
32
33 #define REDZONE_SIZE    0
34
35 #define ARCH_NUM_REGS 7         /* we're never storing ESP */
36 #define ARCH_STORE_REGS(ptr)    \
37         __asm__ __volatile__(   \
38                 "mov %%ecx, 0x00(%0)\n" \
39                 "mov %%edx, 0x04(%0)\n" \
40                 "mov %%ebx, 0x08(%0)\n" \
41                 "mov %%edi, 0x0c(%0)\n" \
42                 "mov %%esi, 0x10(%0)\n" \
43                 "mov %%ebp, 0x14(%0)\n" \
44                 : "=&a" (ptr)   \
45                 : "0" (cur_thread_regs) \
46                 : "memory"      \
47         )
48 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_ESP ((ctx)))
49 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_EIP ((ctx)))
50 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {                       \
51         (a)[0] = (gpointer) UCONTEXT_REG_EAX ((ctx));           \
52         (a)[1] = (gpointer) UCONTEXT_REG_EBX ((ctx));           \
53         (a)[2] = (gpointer) UCONTEXT_REG_ECX ((ctx));           \
54         (a)[3] = (gpointer) UCONTEXT_REG_EDX ((ctx));           \
55         (a)[4] = (gpointer) UCONTEXT_REG_ESI ((ctx));           \
56         (a)[5] = (gpointer) UCONTEXT_REG_EDI ((ctx));           \
57         (a)[6] = (gpointer) UCONTEXT_REG_EBP ((ctx));           \
58         } while (0)
59
60 #elif defined(__x86_64__)
61
62 #define REDZONE_SIZE    128
63
64 #define ARCH_NUM_REGS 15        /* we're never storing RSP */
65 #define ARCH_STORE_REGS(ptr)    \
66         __asm__ __volatile__(   \
67                 "movq %%rcx, 0x00(%0)\n"        \
68                 "movq %%rdx, 0x08(%0)\n"        \
69                 "movq %%rbx, 0x10(%0)\n"        \
70                 "movq %%rdi, 0x18(%0)\n"        \
71                 "movq %%rsi, 0x20(%0)\n"        \
72                 "movq %%rbp, 0x28(%0)\n"        \
73                 "movq %%r8, 0x30(%0)\n" \
74                 "movq %%r9, 0x38(%0)\n" \
75                 "movq %%r10, 0x40(%0)\n"        \
76                 "movq %%r11, 0x48(%0)\n"        \
77                 "movq %%r12, 0x50(%0)\n"        \
78                 "movq %%r13, 0x58(%0)\n"        \
79                 "movq %%r14, 0x60(%0)\n"        \
80                 "movq %%r15, 0x68(%0)\n"        \
81                 : "=&a" (ptr)   \
82                 : "0" (cur_thread_regs) \
83                 : "memory"      \
84         )
85 #define ARCH_SIGCTX_SP(ctx)    (UCONTEXT_REG_RSP (ctx))
86 #define ARCH_SIGCTX_IP(ctx)    (UCONTEXT_REG_RIP (ctx))
87 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
88         ((a)[0] = (gpointer) (UCONTEXT_REG_RAX (ctx))); \
89         ((a)[1] = (gpointer) (UCONTEXT_REG_RBX (ctx))); \
90         ((a)[2] = (gpointer) (UCONTEXT_REG_RCX (ctx))); \
91         ((a)[3] = (gpointer) (UCONTEXT_REG_RDX (ctx))); \
92         ((a)[4] = (gpointer) (UCONTEXT_REG_RSI (ctx))); \
93         ((a)[5] = (gpointer) (UCONTEXT_REG_RDI (ctx))); \
94         ((a)[6] = (gpointer) (UCONTEXT_REG_RBP (ctx))); \
95         ((a)[7] = (gpointer) (UCONTEXT_REG_R8 (ctx)));  \
96         ((a)[8] = (gpointer) (UCONTEXT_REG_R9 (ctx)));  \
97         ((a)[9] = (gpointer) (UCONTEXT_REG_R10 (ctx))); \
98         ((a)[10] = (gpointer) (UCONTEXT_REG_R11 (ctx)));        \
99         ((a)[11] = (gpointer) (UCONTEXT_REG_R12 (ctx)));        \
100         ((a)[12] = (gpointer) (UCONTEXT_REG_R13 (ctx)));        \
101         ((a)[13] = (gpointer) (UCONTEXT_REG_R14 (ctx)));        \
102         ((a)[14] = (gpointer) (UCONTEXT_REG_R15 (ctx)));        \
103         } while (0)
104
105 #elif defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
106
107 #define REDZONE_SIZE    224
108
109 #define ARCH_NUM_REGS 32
110 #define ARCH_STORE_REGS(ptr)    \
111         __asm__ __volatile__(   \
112                 "stmw r0, 0(%0)\n"      \
113                 :                       \
114                 : "b" (ptr)             \
115         )
116 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_Rn((ctx), 1))
117 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_NIP((ctx)))
118 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {       \
119         int __i;        \
120         for (__i = 0; __i < 32; ++__i)  \
121                 ((a)[__i]) = UCONTEXT_REG_Rn((ctx), __i);       \
122         } while (0)
123
124 #elif defined(__arm__)
125
126 #define REDZONE_SIZE    0
127
128 /* We dont store ip, sp */
129 #define ARCH_NUM_REGS 14
130 #define ARCH_STORE_REGS(ptr)                            \
131         __asm__ __volatile__(                           \
132                 "ldr r12, %0\n"                         \
133                 "push {r0}\n"                           \
134                 "push {r12}\n"                          \
135                 "stmia r12!, {r0-r11}\n"                \
136                 "pop {r0}\n"                            \
137                 "stmia r12!, {r0, lr}\n"                \
138                 "mov r12, r0\n"                         \
139                 "pop {r0}\n"                            \
140                 :                                       \
141                 : "m" (ptr)                             \
142         )
143
144 #define ARCH_SIGCTX_SP(ctx)     (UCONTEXT_REG_SP((ctx)))
145 #define ARCH_SIGCTX_IP(ctx)     (UCONTEXT_REG_PC((ctx)))
146 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {                       \
147         ((a)[0]) = (gpointer) (UCONTEXT_REG_R0((ctx)));         \
148         ((a)[1]) = (gpointer) (UCONTEXT_REG_R1((ctx)));         \
149         ((a)[2]) = (gpointer) (UCONTEXT_REG_R2((ctx)));         \
150         ((a)[3]) = (gpointer) (UCONTEXT_REG_R3((ctx)));         \
151         ((a)[4]) = (gpointer) (UCONTEXT_REG_R4((ctx)));         \
152         ((a)[5]) = (gpointer) (UCONTEXT_REG_R5((ctx)));         \
153         ((a)[6]) = (gpointer) (UCONTEXT_REG_R6((ctx)));         \
154         ((a)[7]) = (gpointer) (UCONTEXT_REG_R7((ctx)));         \
155         ((a)[8]) = (gpointer) (UCONTEXT_REG_R8((ctx)));         \
156         ((a)[9]) = (gpointer) (UCONTEXT_REG_R9((ctx)));         \
157         ((a)[10]) = (gpointer) (UCONTEXT_REG_R10((ctx)));       \
158         ((a)[11]) = (gpointer) (UCONTEXT_REG_R11((ctx)));       \
159         ((a)[12]) = (gpointer) (UCONTEXT_REG_R12((ctx)));       \
160         ((a)[13]) = (gpointer) (UCONTEXT_REG_LR((ctx)));        \
161         } while (0)
162
163 #elif defined(__s390x__)
164
165 #define REDZONE_SIZE    0
166
167 #define ARCH_NUM_REGS 16        
168 #define ARCH_STORE_REGS(ptr)    \
169         __asm__ __volatile__(   \
170                 "stg    %%r0,0x00(%0)\n\t"      \
171                 "stg    %%r1,0x08(%0)\n\t"      \
172                 "stg    %%r2,0x10(%0)\n\t"      \
173                 "stg    %%r3,0x18(%0)\n\t"      \
174                 "stg    %%r4,0x20(%0)\n\t"      \
175                 "stg    %%r5,0x28(%0)\n\t"      \
176                 "stg    %%r6,0x30(%0)\n\t"      \
177                 "stg    %%r7,0x38(%0)\n\t"      \
178                 "stg    %%r8,0x40(%0)\n\t"      \
179                 "stg    %%r9,0x48(%0)\n\t"      \
180                 "stg    %%r10,0x50(%0)\n\t"     \
181                 "stg    %%r11,0x58(%0)\n\t"     \
182                 "stg    %%r12,0x60(%0)\n\t"     \
183                 "stg    %%r13,0x68(%0)\n\t"     \
184                 "stg    %%r14,0x70(%0)\n\t"     \
185                 "stg    %%r15,0x78(%0)\n"       \
186                 : "=&a" (ptr)                   \
187                 : "0" (cur_thread_regs)         \
188                 : "memory"                      \
189         )
190 #define ARCH_SIGCTX_SP(ctx)     ((UCONTEXT_GREGS((ctx))) [15])
191 #define ARCH_SIGCTX_IP(ctx)     ((ucontext_t *) (ctx))->uc_mcontext.psw.addr
192 #define ARCH_COPY_SIGCTX_REGS(a,ctx) do {               \
193         ((a)[0] = (gpointer) (UCONTEXT_GREGS((ctx))) [0]);              \
194         ((a)[1] = (gpointer) (UCONTEXT_GREGS((ctx))) [1]);              \
195         ((a)[2] = (gpointer) (UCONTEXT_GREGS((ctx))) [2]);              \
196         ((a)[3] = (gpointer) (UCONTEXT_GREGS((ctx))) [3]);              \
197         ((a)[4] = (gpointer) (UCONTEXT_GREGS((ctx))) [4]);              \
198         ((a)[5] = (gpointer) (UCONTEXT_GREGS((ctx))) [5]);              \
199         ((a)[6] = (gpointer) (UCONTEXT_GREGS((ctx))) [6]);              \
200         ((a)[7] = (gpointer) (UCONTEXT_GREGS((ctx))) [7]);              \
201         ((a)[8] = (gpointer) (UCONTEXT_GREGS((ctx))) [8]);              \
202         ((a)[9] = (gpointer) (UCONTEXT_GREGS((ctx))) [9]);              \
203         ((a)[10] = (gpointer) (UCONTEXT_GREGS((ctx))) [10]);            \
204         ((a)[11] = (gpointer) (UCONTEXT_GREGS((ctx))) [11]);            \
205         ((a)[12] = (gpointer) (UCONTEXT_GREGS((ctx))) [12]);            \
206         ((a)[13] = (gpointer) (UCONTEXT_GREGS((ctx))) [13]);            \
207         ((a)[14] = (gpointer) (UCONTEXT_GREGS((ctx))) [14]);            \
208         ((a)[15] = (gpointer) (UCONTEXT_GREGS((ctx))) [15]);            \
209         } while (0)
210
211 #endif
212
213 #endif /* __MONO_SGENARCHDEP_H__ */