Merge pull request #555 from jack-pappas/sigaltstack-patch
[mono.git] / mono / utils / mono-context.h
1 /*
2  * mono-context.h: plat independent machine state definitions
3  *
4  *
5  * Copyright (c) 2011 Novell, Inc (http://www.novell.com)
6  */
7
8
9 #ifndef __MONO_MONO_CONTEXT_H__
10 #define __MONO_MONO_CONTEXT_H__
11
12 #include "mono-compiler.h"
13 #include "mono-sigcontext.h"
14 #include "mono-machine.h"
15
16 #ifdef HAVE_SIGNAL_H
17 #include <signal.h>
18 #endif
19
20 /*
21  * General notes about mono-context.
22  * Each arch defines a MonoContext struct with all GPR regs + IP/PC.
23  * IP/PC should be the last element of the struct (this is a mild sgen constraint we could drop if needed)
24  * Macros to get/set BP, SP and IP are defined too.
25  * MONO_CONTEXT_GET_CURRENT captures the current context as close as possible. One reg might be clobbered
26  *  to hold the address of the target MonoContext. It will be a caller save one, so should not be a problem.
27  */
28 #if (defined(__i386__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_X86))
29
30 /*HACK, move this to an eventual mono-signal.c*/
31 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
32        defined(__FreeBSD__) || defined(__OpenBSD__)
33 #define MONO_SIGNAL_USE_SIGACTION
34 #endif
35
36 #if defined(__native_client__)
37 #undef MONO_SIGNAL_USE_SIGACTION
38 #endif
39
40 #ifdef HOST_WIN32
41 /* sigcontext surrogate */
42 struct sigcontext {
43         unsigned int eax;
44         unsigned int ebx;
45         unsigned int ecx;
46         unsigned int edx;
47         unsigned int ebp;
48         unsigned int esp;
49         unsigned int esi;
50         unsigned int edi;
51         unsigned int eip;
52 };
53 #endif
54
55 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
56 # define SC_EAX sc_eax
57 # define SC_EBX sc_ebx
58 # define SC_ECX sc_ecx
59 # define SC_EDX sc_edx
60 # define SC_EBP sc_ebp
61 # define SC_EIP sc_eip
62 # define SC_ESP sc_esp
63 # define SC_EDI sc_edi
64 # define SC_ESI sc_esi
65 #elif defined(__HAIKU__)
66 # define SC_EAX regs.eax
67 # define SC_EBX regs._reserved_2[2]
68 # define SC_ECX regs.ecx
69 # define SC_EDX regs.edx
70 # define SC_EBP regs.ebp
71 # define SC_EIP regs.eip
72 # define SC_ESP regs.esp
73 # define SC_EDI regs._reserved_2[0]
74 # define SC_ESI regs._reserved_2[1]
75 #else
76 # define SC_EAX eax
77 # define SC_EBX ebx
78 # define SC_ECX ecx
79 # define SC_EDX edx
80 # define SC_EBP ebp
81 # define SC_EIP eip
82 # define SC_ESP esp
83 # define SC_EDI edi
84 # define SC_ESI esi
85 #endif
86
87 typedef struct {
88         mgreg_t eax;
89         mgreg_t ebx;
90         mgreg_t ecx;
91         mgreg_t edx;
92         mgreg_t ebp;
93         mgreg_t esp;
94         mgreg_t esi;
95         mgreg_t edi;
96         mgreg_t eip;
97 } MonoContext;
98
99 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (mgreg_t)(ip); } while (0); 
100 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (mgreg_t)(bp); } while (0); 
101 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (mgreg_t)(sp); } while (0); 
102
103 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
104 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
105 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
106
107 /*We set EAX to zero since we are clobering it anyway*/
108 #define MONO_CONTEXT_GET_CURRENT(ctx) \
109         __asm__ __volatile__(   \
110         "movl $0x0, 0x00(%0)\n" \
111         "mov %%ebx, 0x04(%0)\n" \
112         "mov %%ecx, 0x08(%0)\n" \
113         "mov %%edx, 0x0c(%0)\n" \
114         "mov %%ebp, 0x10(%0)\n" \
115         "mov %%esp, 0x14(%0)\n" \
116         "mov %%esi, 0x18(%0)\n" \
117         "mov %%edi, 0x1c(%0)\n" \
118         "call 1f\n"     \
119         "1: pop 0x20(%0)\n"     \
120         :       \
121         : "a" (&(ctx))  \
122         : "memory")
123
124 #if !defined(HOST_WIN32)
125 #define MONO_ARCH_HAS_MONO_CONTEXT 1
126 #endif
127
128 #elif (defined(__x86_64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_AMD64)) /* defined(__i386__) */
129
130
131 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
132
133 #define MONO_SIGNAL_USE_SIGACTION 1
134
135 #endif
136
137 typedef struct {
138         mgreg_t rax;
139         mgreg_t rbx;
140         mgreg_t rcx;
141         mgreg_t rdx;
142         mgreg_t rbp;
143         mgreg_t rsp;
144     mgreg_t rsi;
145         mgreg_t rdi;
146         mgreg_t r8;
147         mgreg_t r9;
148         mgreg_t r10;
149         mgreg_t r11;
150         mgreg_t r12;
151         mgreg_t r13;
152         mgreg_t r14;
153         mgreg_t r15;
154         mgreg_t rip;
155 } MonoContext;
156
157 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->rip = (mgreg_t)(ip); } while (0); 
158 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->rbp = (mgreg_t)(bp); } while (0); 
159 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->rsp = (mgreg_t)(esp); } while (0); 
160
161 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->rip))
162 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->rbp))
163 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->rsp))
164
165 #if defined(__native_client__)
166 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
167         __asm__ __volatile__(   \
168                 "movq $0x0,  %%nacl:0x00(%%r15, %0, 1)\n"       \
169                 "movq %%rbx, %%nacl:0x08(%%r15, %0, 1)\n"       \
170                 "movq %%rcx, %%nacl:0x10(%%r15, %0, 1)\n"       \
171                 "movq %%rdx, %%nacl:0x18(%%r15, %0, 1)\n"       \
172                 "movq %%rbp, %%nacl:0x20(%%r15, %0, 1)\n"       \
173                 "movq %%rsp, %%nacl:0x28(%%r15, %0, 1)\n"       \
174                 "movq %%rsi, %%nacl:0x30(%%r15, %0, 1)\n"       \
175                 "movq %%rdi, %%nacl:0x38(%%r15, %0, 1)\n"       \
176                 "movq %%r8,  %%nacl:0x40(%%r15, %0, 1)\n"       \
177                 "movq %%r9,  %%nacl:0x48(%%r15, %0, 1)\n"       \
178                 "movq %%r10, %%nacl:0x50(%%r15, %0, 1)\n"       \
179                 "movq %%r11, %%nacl:0x58(%%r15, %0, 1)\n"       \
180                 "movq %%r12, %%nacl:0x60(%%r15, %0, 1)\n"       \
181                 "movq %%r13, %%nacl:0x68(%%r15, %0, 1)\n"       \
182                 "movq %%r14, %%nacl:0x70(%%r15, %0, 1)\n"       \
183                 "movq %%r15, %%nacl:0x78(%%r15, %0, 1)\n"       \
184                 "leaq (%%rip), %%rdx\n" \
185                 "movq %%rdx, %%nacl:0x80(%%r15, %0, 1)\n"       \
186                 :       \
187                 : "a" ((int64_t)&(ctx)) \
188                 : "rdx", "memory")
189 #else
190 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
191         __asm__ __volatile__(   \
192                 "movq $0x0,  0x00(%0)\n"        \
193                 "movq %%rbx, 0x08(%0)\n"        \
194                 "movq %%rcx, 0x10(%0)\n"        \
195                 "movq %%rdx, 0x18(%0)\n"        \
196                 "movq %%rbp, 0x20(%0)\n"        \
197                 "movq %%rsp, 0x28(%0)\n"        \
198                 "movq %%rsi, 0x30(%0)\n"        \
199                 "movq %%rdi, 0x38(%0)\n"        \
200                 "movq %%r8,  0x40(%0)\n"        \
201                 "movq %%r9,  0x48(%0)\n"        \
202                 "movq %%r10, 0x50(%0)\n"        \
203                 "movq %%r11, 0x58(%0)\n"        \
204                 "movq %%r12, 0x60(%0)\n"        \
205                 "movq %%r13, 0x68(%0)\n"        \
206                 "movq %%r14, 0x70(%0)\n"        \
207                 "movq %%r15, 0x78(%0)\n"        \
208                 "leaq (%%rip), %%rdx\n" \
209                 "movq %%rdx, 0x80(%0)\n"        \
210                 :       \
211                 : "a" (&(ctx))  \
212                 : "rdx", "memory")
213 #endif
214
215 #if !defined(HOST_WIN32)
216 #define MONO_ARCH_HAS_MONO_CONTEXT 1
217 #endif
218
219 #elif (defined(__arm__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM)) /* defined(__x86_64__) */
220
221 typedef struct {
222         mgreg_t pc;
223         mgreg_t regs [16];
224         double fregs [16];
225         mgreg_t cpsr;
226 } MonoContext;
227
228 /* we have the stack pointer, not the base pointer in sigcontext */
229 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0); 
230 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0); 
231 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0); 
232
233 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->pc))
234 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ARMREG_FP]))
235 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->regs [ARMREG_SP]))
236
237 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
238         __asm__ __volatile__(                   \
239                 "push {r0}\n"                           \
240                 "push {r1}\n"                           \
241                 "mov r0, %0\n"                          \
242                 "ldr r1, [sp, #4]\n"                    \
243                 "str r1, [r0]!\n"                       \
244                 "ldr r1, [sp, #0]\n"                    \
245                 "str r1, [r0]!\n"                       \
246                 "stmia r0!, {r2-r12}\n"         \
247                 "str sp, [r0]!\n"                       \
248                 "str lr, [r0]!\n"                       \
249                 "mov r1, pc\n"                          \
250                 "str r1, [r0]!\n"                       \
251                 "pop {r1}\n"                            \
252                 "pop {r0}\n"                            \
253                 :                                                       \
254                 : "r" (&ctx.regs)                       \
255                 : "memory"                                      \
256         );                                                              \
257         ctx.pc = ctx.regs [15];                 \
258 } while (0)
259
260
261 #elif defined(__mono_ppc__) /* defined(__arm__) */
262
263 /* we define our own structure and we'll copy the data
264  * from sigcontext/ucontext/mach when we need it.
265  * This also makes us save stack space and time when copying
266  * We might also want to add an additional field to propagate
267  * the original context from the signal handler.
268  */
269 typedef struct {
270         gulong sc_ir;          // pc 
271         gulong sc_sp;          // r1
272         mgreg_t regs [19]; /*FIXME, this must be changed to 32 for sgen*/
273         double fregs [18];
274 } MonoContext;
275
276 /* we have the stack pointer, not the base pointer in sigcontext */
277 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (gulong)ip; } while (0);
278 /* FIXME: should be called SET_SP */
279 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
280 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)sp; } while (0);
281
282 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
283 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
284 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
285
286 #elif defined(__sparc__) || defined(sparc) /* defined(__mono_ppc__) */
287
288 typedef struct MonoContext {
289         guint8 *ip;
290         gpointer *sp;
291         gpointer *fp;
292 } MonoContext;
293
294 #define MONO_CONTEXT_SET_IP(ctx,eip) do { (ctx)->ip = (gpointer)(eip); } while (0); 
295 #define MONO_CONTEXT_SET_BP(ctx,ebp) do { (ctx)->fp = (gpointer*)(ebp); } while (0); 
296 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->sp = (gpointer*)(esp); } while (0); 
297
298 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->ip))
299 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->fp))
300 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
301
302 #elif defined(__ia64__) /*defined(__sparc__) || defined(sparc) */
303
304 #ifndef UNW_LOCAL_ONLY
305
306 #define UNW_LOCAL_ONLY
307 #include <libunwind.h>
308
309 #endif
310
311 typedef struct MonoContext {
312         unw_cursor_t cursor;
313         /* Whenever the ip in 'cursor' points to the ip where the exception happened */
314         /* This is true for the initial context for exceptions thrown from signal handlers */
315         gboolean precise_ip;
316 } MonoContext;
317
318 /*XXX SET_BP is missing*/
319 #define MONO_CONTEXT_SET_IP(ctx,eip) do { int err = unw_set_reg (&(ctx)->cursor, UNW_IA64_IP, (unw_word_t)(eip)); g_assert (err == 0); } while (0)
320 #define MONO_CONTEXT_SET_SP(ctx,esp) do { int err = unw_set_reg (&(ctx)->cursor, UNW_IA64_SP, (unw_word_t)(esp)); g_assert (err == 0); } while (0)
321
322 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)(mono_ia64_context_get_ip ((ctx))))
323 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)(mono_ia64_context_get_fp ((ctx))))
324 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)(mono_ia64_context_get_sp ((ctx))))
325
326 static inline unw_word_t
327 mono_ia64_context_get_ip (MonoContext *ctx)
328 {
329         unw_word_t ip;
330         int err;
331
332         err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
333         g_assert (err == 0);
334
335         if (ctx->precise_ip) {
336                 return ip;
337         } else {
338                 /* Subtrack 1 so ip points into the actual instruction */
339                 return ip - 1;
340         }
341 }
342
343 static inline unw_word_t
344 mono_ia64_context_get_sp (MonoContext *ctx)
345 {
346         unw_word_t sp;
347         int err;
348
349         err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
350         g_assert (err == 0);
351
352         return sp;
353 }
354
355 static inline unw_word_t
356 mono_ia64_context_get_fp (MonoContext *ctx)
357 {
358         unw_cursor_t new_cursor;
359         unw_word_t fp;
360         int err;
361
362         {
363                 unw_word_t ip, sp;
364
365                 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
366                 g_assert (err == 0);
367
368                 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
369                 g_assert (err == 0);
370         }
371
372         /* fp is the SP of the parent frame */
373         new_cursor = ctx->cursor;
374
375         err = unw_step (&new_cursor);
376         g_assert (err >= 0);
377
378         err = unw_get_reg (&new_cursor, UNW_IA64_SP, &fp);
379         g_assert (err == 0);
380
381         return fp;
382 }
383
384 #elif ((defined(__mips__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_MIPS))) && SIZEOF_REGISTER == 4 /* defined(__ia64__) */
385
386 typedef struct {
387         mgreg_t     sc_pc;
388         mgreg_t         sc_regs [32];
389         gfloat          sc_fpregs [32];
390 } MonoContext;
391
392 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_pc = (mgreg_t)(ip); } while (0);
393 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_regs[mips_fp] = (mgreg_t)(bp); } while (0);
394 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_regs[mips_sp] = (mgreg_t)(sp); } while (0);
395
396 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_pc))
397 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_regs[mips_fp]))
398 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_regs[mips_sp]))
399
400 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
401         __asm__ __volatile__(   \
402                 "sw $0,0(%0)\n\t"       \
403                 "sw $1,4(%0)\n\t"       \
404                 "sw $2,8(%0)\n\t"       \
405                 "sw $3,12(%0)\n\t"      \
406                 "sw $4,16(%0)\n\t"      \
407                 "sw $5,20(%0)\n\t"      \
408                 "sw $6,24(%0)\n\t"      \
409                 "sw $7,28(%0)\n\t"      \
410                 "sw $8,32(%0)\n\t"      \
411                 "sw $9,36(%0)\n\t"      \
412                 "sw $10,40(%0)\n\t"     \
413                 "sw $11,44(%0)\n\t"     \
414                 "sw $12,48(%0)\n\t"     \
415                 "sw $13,52(%0)\n\t"     \
416                 "sw $14,56(%0)\n\t"     \
417                 "sw $15,60(%0)\n\t"     \
418                 "sw $16,64(%0)\n\t"     \
419                 "sw $17,68(%0)\n\t"     \
420                 "sw $18,72(%0)\n\t"     \
421                 "sw $19,76(%0)\n\t"     \
422                 "sw $20,80(%0)\n\t"     \
423                 "sw $21,84(%0)\n\t"     \
424                 "sw $22,88(%0)\n\t"     \
425                 "sw $23,92(%0)\n\t"     \
426                 "sw $24,96(%0)\n\t"     \
427                 "sw $25,100(%0)\n\t"    \
428                 "sw $26,104(%0)\n\t"    \
429                 "sw $27,108(%0)\n\t"    \
430                 "sw $28,112(%0)\n\t"    \
431                 "sw $29,116(%0)\n\t"    \
432                 "sw $30,120(%0)\n\t"    \
433                 "sw $31,124(%0)\n\t"    \
434                 : : "r" (&(ctx).sc_regs [0])    \
435                 : "memory"                      \
436         )
437
438 #elif defined(__s390x__)
439
440 #define MONO_ARCH_HAS_MONO_CONTEXT 1
441
442 typedef struct ucontext MonoContext;
443
444 #define MONO_CONTEXT_SET_IP(ctx,ip)                                     \
445         do {                                                            \
446                 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip;       \
447                 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip;        \
448         } while (0); 
449
450 #define MONO_CONTEXT_SET_SP(ctx,bp) MONO_CONTEXT_SET_BP((ctx),(bp))
451 #define MONO_CONTEXT_SET_BP(ctx,bp)                                     \
452         do {                                                            \
453                 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp;       \
454                 (ctx)->uc_stack.ss_sp        = (void*)bp;               \
455         } while (0) 
456
457 #define MONO_CONTEXT_GET_IP(ctx) (gpointer) (ctx)->uc_mcontext.psw.addr
458 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
459 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[11]))
460
461 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
462         __asm__ __volatile__(   \
463                 "stmg   %%r0,%%r15,0(%0)\n"     \
464                 : : "r" (&(ctx).uc_mcontext.gregs[0])   \
465                 : "memory"                      \
466         )
467
468 #else
469
470 #error "Implement mono-context for the current arch"
471
472 #endif
473
474 void mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx) MONO_INTERNAL;
475 void mono_monoctx_to_sigctx (MonoContext *mctx, void *sigctx) MONO_INTERNAL;
476
477 #endif /* __MONO_MONO_CONTEXT_H__ */