2 * mono-context.h: plat independent machine state definitions
5 * Copyright (c) 2011 Novell, Inc (http://www.novell.com)
9 #ifndef __MONO_MONO_CONTEXT_H__
10 #define __MONO_MONO_CONTEXT_H__
12 #include "mono-compiler.h"
13 #include "mono-sigcontext.h"
14 #include "mono-machine.h"
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.
28 #if (defined(__i386__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_X86))
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
36 #if defined(__native_client__)
37 #undef MONO_SIGNAL_USE_SIGACTION
41 /* sigcontext surrogate */
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]
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);
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))
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" \
119 "1: pop 0x20(%0)\n" \
124 #if !defined(HOST_WIN32)
125 #define MONO_ARCH_HAS_MONO_CONTEXT 1
128 #elif (defined(__x86_64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_AMD64)) /* defined(__i386__) */
131 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
133 #define MONO_SIGNAL_USE_SIGACTION 1
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);
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))
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" \
187 : "a" ((int64_t)&(ctx)) \
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" \
215 #if !defined(HOST_WIN32)
216 #define MONO_ARCH_HAS_MONO_CONTEXT 1
219 #elif (defined(__arm__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM)) /* defined(__x86_64__) */
229 /* we have the stack pointer, not the base pointer in sigcontext */
230 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0);
231 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0);
232 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->sp = (mgreg_t)bp; } while (0);
234 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->pc))
235 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ARMREG_FP]))
236 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
239 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
240 g_assert_not_reached (); \
243 #elif defined(__mono_ppc__) /* defined(__arm__) */
245 /* we define our own structure and we'll copy the data
246 * from sigcontext/ucontext/mach when we need it.
247 * This also makes us save stack space and time when copying
248 * We might also want to add an additional field to propagate
249 * the original context from the signal handler.
254 mgreg_t regs [19]; /*FIXME, this must be changed to 32 for sgen*/
258 /* we have the stack pointer, not the base pointer in sigcontext */
259 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (gulong)ip; } while (0);
260 /* FIXME: should be called SET_SP */
261 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
262 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)sp; } while (0);
264 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
265 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
266 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
268 #elif defined(__sparc__) || defined(sparc) /* defined(__mono_ppc__) */
270 typedef struct MonoContext {
276 #define MONO_CONTEXT_SET_IP(ctx,eip) do { (ctx)->ip = (gpointer)(eip); } while (0);
277 #define MONO_CONTEXT_SET_BP(ctx,ebp) do { (ctx)->fp = (gpointer*)(ebp); } while (0);
278 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->sp = (gpointer*)(esp); } while (0);
280 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->ip))
281 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->fp))
282 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
284 #elif defined(__ia64__) /*defined(__sparc__) || defined(sparc) */
286 #ifndef UNW_LOCAL_ONLY
288 #define UNW_LOCAL_ONLY
289 #include <libunwind.h>
293 typedef struct MonoContext {
295 /* Whenever the ip in 'cursor' points to the ip where the exception happened */
296 /* This is true for the initial context for exceptions thrown from signal handlers */
300 /*XXX SET_BP is missing*/
301 #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)
302 #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)
304 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)(mono_ia64_context_get_ip ((ctx))))
305 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)(mono_ia64_context_get_fp ((ctx))))
306 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)(mono_ia64_context_get_sp ((ctx))))
308 static inline unw_word_t
309 mono_ia64_context_get_ip (MonoContext *ctx)
314 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
317 if (ctx->precise_ip) {
320 /* Subtrack 1 so ip points into the actual instruction */
325 static inline unw_word_t
326 mono_ia64_context_get_sp (MonoContext *ctx)
331 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
337 static inline unw_word_t
338 mono_ia64_context_get_fp (MonoContext *ctx)
340 unw_cursor_t new_cursor;
347 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
350 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
354 /* fp is the SP of the parent frame */
355 new_cursor = ctx->cursor;
357 err = unw_step (&new_cursor);
360 err = unw_get_reg (&new_cursor, UNW_IA64_SP, &fp);
366 #elif defined(__mips__) && SIZEOF_REGISTER == 4 /* defined(__ia64__) */
368 /* we define our own structure and we'll copy the data
369 * from sigcontext/ucontext/mach when we need it.
370 * This also makes us save stack space and time when copying
371 * We might also want to add an additional field to propagate
372 * the original context from the signal handler.
376 guint32 sc_regs [32];
377 gfloat sc_fpregs [32];
380 /* we have the stack pointer, not the base pointer in sigcontext */
381 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_pc = (int)(ip); } while (0);
382 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_regs[mips_fp] = (int)(bp); } while (0);
383 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_regs[mips_sp] = (int)(sp); } while (0);
385 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_pc))
386 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_regs[mips_fp]))
387 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_regs[mips_sp]))
389 #elif defined(__s390x__)
391 #define MONO_ARCH_HAS_MONO_CONTEXT 1
393 typedef struct ucontext MonoContext;
395 #define MONO_CONTEXT_SET_IP(ctx,ip) \
397 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip; \
398 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip; \
401 #define MONO_CONTEXT_SET_SP(ctx,bp) MONO_CONTEXT_SET_BP((ctx),(bp))
402 #define MONO_CONTEXT_SET_BP(ctx,bp) \
404 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp; \
405 (ctx)->uc_stack.ss_sp = (void*)bp; \
408 #define MONO_CONTEXT_GET_IP(ctx) (gpointer) (ctx)->uc_mcontext.psw.addr
409 #define MONO_CONTEXT_GET_BP(ctx) MONO_CONTEXT_GET_SP((ctx))
410 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
412 #define MONO_CONTEXT_GET_CURRENT(ctx) \
413 __asm__ __volatile__( \
414 "stmg %%r0,%%r15,0(%0)\n" \
415 : : "r" (&(ctx).uc_mcontext.gregs[0]) \
421 #error "Implement mono-context for the current arch"
425 void mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx) MONO_INTERNAL;
426 void mono_monoctx_to_sigctx (MonoContext *mctx, void *sigctx) MONO_INTERNAL;
428 #endif /* __MONO_MONO_CONTEXT_H__ */