55d75bda44d0f86406a776f5127c9c856d5054ec
[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 #ifdef HOST_WATCHOS
21 #include <ucontext.h>
22 #endif
23
24 /*
25  * General notes about mono-context.
26  * Each arch defines a MonoContext struct with all GPR regs + IP/PC.
27  * IP/PC should be the last element of the struct (this is a mild sgen constraint we could drop if needed)
28  * Macros to get/set BP, SP and IP are defined too.
29  * MONO_CONTEXT_GET_CURRENT captures the current context as close as possible. One reg might be clobbered
30  *  to hold the address of the target MonoContext. It will be a caller save one, so should not be a problem.
31  */
32 #if (defined(__i386__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_X86))
33
34 /*HACK, move this to an eventual mono-signal.c*/
35 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
36        defined(__FreeBSD__) || defined(__OpenBSD__)
37 #ifdef HAVE_SIGACTION
38 #define MONO_SIGNAL_USE_SIGACTION 1
39 #endif
40 #endif
41
42 #if defined(__native_client__)
43 #undef MONO_SIGNAL_USE_SIGACTION
44 #endif
45
46 #ifdef HOST_WIN32
47 /* sigcontext surrogate */
48 struct sigcontext {
49         unsigned int eax;
50         unsigned int ebx;
51         unsigned int ecx;
52         unsigned int edx;
53         unsigned int ebp;
54         unsigned int esp;
55         unsigned int esi;
56         unsigned int edi;
57         unsigned int eip;
58 };
59 #endif
60
61 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
62 # define SC_EAX sc_eax
63 # define SC_EBX sc_ebx
64 # define SC_ECX sc_ecx
65 # define SC_EDX sc_edx
66 # define SC_EBP sc_ebp
67 # define SC_EIP sc_eip
68 # define SC_ESP sc_esp
69 # define SC_EDI sc_edi
70 # define SC_ESI sc_esi
71 #elif defined(__HAIKU__)
72 # define SC_EAX regs.eax
73 # define SC_EBX regs._reserved_2[2]
74 # define SC_ECX regs.ecx
75 # define SC_EDX regs.edx
76 # define SC_EBP regs.ebp
77 # define SC_EIP regs.eip
78 # define SC_ESP regs.esp
79 # define SC_EDI regs._reserved_2[0]
80 # define SC_ESI regs._reserved_2[1]
81 #else
82 # define SC_EAX eax
83 # define SC_EBX ebx
84 # define SC_ECX ecx
85 # define SC_EDX edx
86 # define SC_EBP ebp
87 # define SC_EIP eip
88 # define SC_ESP esp
89 # define SC_EDI edi
90 # define SC_ESI esi
91 #endif
92
93 typedef struct {
94         mgreg_t eax;
95         mgreg_t ebx;
96         mgreg_t ecx;
97         mgreg_t edx;
98         mgreg_t ebp;
99         mgreg_t esp;
100         mgreg_t esi;
101         mgreg_t edi;
102         mgreg_t eip;
103 } MonoContext;
104
105 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (mgreg_t)(ip); } while (0); 
106 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (mgreg_t)(bp); } while (0); 
107 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (mgreg_t)(sp); } while (0); 
108
109 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
110 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
111 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
112
113 /*We set EAX to zero since we are clobering it anyway*/
114 #ifdef _MSC_VER
115 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
116         void *_ptr = &(ctx);                                                                                            \
117         __asm {                                                                                                                         \
118          __asm mov eax, _ptr                                                                                            \
119          __asm mov [eax+0x00], eax                                                                                      \
120          __asm mov [eax+0x04], ebx                                                                                      \
121          __asm mov [eax+0x08], ecx                                                                                      \
122          __asm mov [eax+0x0c], edx                                                                                      \
123          __asm mov [eax+0x10], ebp                                                                                      \
124          __asm mov [eax+0x14], esp                                                                                      \
125          __asm mov [eax+0x18], esi                                                                                      \
126          __asm mov [eax+0x1c], edi                                                                                      \
127          __asm call __mono_context_get_ip                                                                       \
128          __asm __mono_context_get_ip:                                                                           \
129          __asm pop dword ptr [eax+0x20]                                                                         \
130                  }                                                                                                                              \
131         } while (0)
132 #else
133 #define MONO_CONTEXT_GET_CURRENT(ctx) \
134         __asm__ __volatile__(   \
135         "movl $0x0, 0x00(%0)\n" \
136         "mov %%ebx, 0x04(%0)\n" \
137         "mov %%ecx, 0x08(%0)\n" \
138         "mov %%edx, 0x0c(%0)\n" \
139         "mov %%ebp, 0x10(%0)\n" \
140         "mov %%esp, 0x14(%0)\n" \
141         "mov %%esi, 0x18(%0)\n" \
142         "mov %%edi, 0x1c(%0)\n" \
143         "call 1f\n"     \
144         "1: pop 0x20(%0)\n"     \
145         :       \
146         : "a" (&(ctx))  \
147         : "memory")
148 #endif
149
150 #define MONO_ARCH_HAS_MONO_CONTEXT 1
151
152 #elif (defined(__x86_64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_AMD64)) /* defined(__i386__) */
153
154 #include <mono/arch/amd64/amd64-codegen.h>
155
156 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
157
158 #ifdef HAVE_SIGACTION
159 #define MONO_SIGNAL_USE_SIGACTION 1
160 #endif
161
162 #endif
163
164 typedef struct {
165         mgreg_t gregs [AMD64_NREG];
166         double fregs [AMD64_XMM_NREG];
167 } MonoContext;
168
169 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->gregs [AMD64_RIP] = (mgreg_t)(ip); } while (0);
170 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->gregs [AMD64_RBP] = (mgreg_t)(bp); } while (0);
171 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->gregs [AMD64_RSP] = (mgreg_t)(esp); } while (0);
172
173 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->gregs [AMD64_RIP]))
174 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->gregs [AMD64_RBP]))
175 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->gregs [AMD64_RSP]))
176
177 #if defined (HOST_WIN32) && !defined(__GNUC__)
178 /* msvc doesn't support inline assembly, so have to use a separate .asm file */
179 extern void mono_context_get_current (void *);
180 #define MONO_CONTEXT_GET_CURRENT(ctx) do { mono_context_get_current((void*)&(ctx)); } while (0)
181
182 #elif defined(__native_client__)
183 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
184         __asm__ __volatile__(   \
185                 "movq $0x0,  %%nacl:0x00(%%r15, %0, 1)\n"       \
186                 "movq %%rbx, %%nacl:0x08(%%r15, %0, 1)\n"       \
187                 "movq %%rcx, %%nacl:0x10(%%r15, %0, 1)\n"       \
188                 "movq %%rdx, %%nacl:0x18(%%r15, %0, 1)\n"       \
189                 "movq %%rbp, %%nacl:0x20(%%r15, %0, 1)\n"       \
190                 "movq %%rsp, %%nacl:0x28(%%r15, %0, 1)\n"       \
191                 "movq %%rsi, %%nacl:0x30(%%r15, %0, 1)\n"       \
192                 "movq %%rdi, %%nacl:0x38(%%r15, %0, 1)\n"       \
193                 "movq %%r8,  %%nacl:0x40(%%r15, %0, 1)\n"       \
194                 "movq %%r9,  %%nacl:0x48(%%r15, %0, 1)\n"       \
195                 "movq %%r10, %%nacl:0x50(%%r15, %0, 1)\n"       \
196                 "movq %%r11, %%nacl:0x58(%%r15, %0, 1)\n"       \
197                 "movq %%r12, %%nacl:0x60(%%r15, %0, 1)\n"       \
198                 "movq %%r13, %%nacl:0x68(%%r15, %0, 1)\n"       \
199                 "movq %%r14, %%nacl:0x70(%%r15, %0, 1)\n"       \
200                 "movq %%r15, %%nacl:0x78(%%r15, %0, 1)\n"       \
201                 "leaq (%%rip), %%rdx\n" \
202                 "movq %%rdx, %%nacl:0x80(%%r15, %0, 1)\n"       \
203                 :       \
204                 : "a" ((int64_t)&(ctx)) \
205                 : "rdx", "memory")
206 #else
207 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
208         __asm__ __volatile__(   \
209                 "movq $0x0,  0x00(%0)\n"        \
210                 "movq %%rbx, 0x08(%0)\n"        \
211                 "movq %%rcx, 0x10(%0)\n"        \
212                 "movq %%rdx, 0x18(%0)\n"        \
213                 "movq %%rbp, 0x20(%0)\n"        \
214                 "movq %%rsp, 0x28(%0)\n"        \
215                 "movq %%rsi, 0x30(%0)\n"        \
216                 "movq %%rdi, 0x38(%0)\n"        \
217                 "movq %%r8,  0x40(%0)\n"        \
218                 "movq %%r9,  0x48(%0)\n"        \
219                 "movq %%r10, 0x50(%0)\n"        \
220                 "movq %%r11, 0x58(%0)\n"        \
221                 "movq %%r12, 0x60(%0)\n"        \
222                 "movq %%r13, 0x68(%0)\n"        \
223                 "movq %%r14, 0x70(%0)\n"        \
224                 "movq %%r15, 0x78(%0)\n"        \
225                 /* "leaq (%%rip), %%rdx\n" is not understood by icc */  \
226                 ".byte 0x48, 0x8d, 0x15, 0x00, 0x00, 0x00, 0x00\n" \
227                 "movq %%rdx, 0x80(%0)\n"        \
228                 :       \
229                 : "a" (&(ctx))  \
230                 : "rdx", "memory")
231 #endif
232
233 #define MONO_ARCH_HAS_MONO_CONTEXT 1
234
235 #elif (defined(__arm__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM)) /* defined(__x86_64__) */
236
237 #include <mono/arch/arm/arm-codegen.h>
238
239 typedef struct {
240         mgreg_t pc;
241         mgreg_t regs [16];
242         double fregs [16];
243         mgreg_t cpsr;
244 } MonoContext;
245
246 /* we have the stack pointer, not the base pointer in sigcontext */
247 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0); 
248 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0); 
249 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0); 
250
251 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->pc))
252 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ARMREG_FP]))
253 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->regs [ARMREG_SP]))
254
255 #if defined(HOST_WATCHOS)
256
257 #define MONO_CONTEXT_GET_CURRENT(ctx) do {                                                      \
258         ucontext_t uctx;                                                                                                \
259         struct __darwin_mcontext32 *mctx;                                                               \
260         getcontext(&uctx);                                                                                              \
261         mctx = uctx.uc_mcontext;                                                                                \
262         memcpy(ctx.regs, mctx->__ss.__r, 13 * sizeof(__uint32_t));              \
263         ctx.regs[ARMREG_SP] = mctx->__ss.__sp;                                                  \
264         ctx.regs[ARMREG_LR] = mctx->__ss.__lr;                                                  \
265         ctx.regs[ARMREG_PC] = mctx->__ss.__pc;                                                  \
266         ctx.pc = mctx->__ss.__pc;                                                                               \
267         memcpy(ctx.fregs, mctx->__fs.__r, 16 * sizeof(__uint32_t));             \
268         ctx.cpsr = mctx->__ss.__cpsr;                                                                   \
269 } while (0);
270
271 #else
272
273 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
274         __asm__ __volatile__(                   \
275                 "push {r0}\n"                           \
276                 "push {r1}\n"                           \
277                 "mov r0, %0\n"                          \
278                 "ldr r1, [sp, #4]\n"                    \
279                 "str r1, [r0]!\n"                       \
280                 "ldr r1, [sp, #0]\n"                    \
281                 "str r1, [r0]!\n"                       \
282                 "stmia r0!, {r2-r12}\n"         \
283                 "str sp, [r0]!\n"                       \
284                 "str lr, [r0]!\n"                       \
285                 "mov r1, pc\n"                          \
286                 "str r1, [r0]!\n"                       \
287                 "pop {r1}\n"                            \
288                 "pop {r0}\n"                            \
289                 :                                                       \
290                 : "r" (&ctx.regs)                       \
291                 : "memory"                                      \
292         );                                                              \
293         ctx.pc = ctx.regs [15];                 \
294 } while (0)
295
296 #endif
297
298 #define MONO_ARCH_HAS_MONO_CONTEXT 1
299
300 #elif (defined(__aarch64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM64))
301
302 #include <mono/arch/arm64/arm64-codegen.h>
303
304 typedef struct {
305         mgreg_t regs [32];
306         double fregs [32];
307         mgreg_t pc;
308 } MonoContext;
309
310 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0)
311 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0);
312 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0);
313
314 #define MONO_CONTEXT_GET_IP(ctx) (gpointer)((ctx)->pc)
315 #define MONO_CONTEXT_GET_BP(ctx) (gpointer)((ctx)->regs [ARMREG_FP])
316 #define MONO_CONTEXT_GET_SP(ctx) (gpointer)((ctx)->regs [ARMREG_SP])
317
318 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
319         __asm__ __volatile__(                   \
320                 "mov x16, %0\n" \
321                 "stp x0, x1, [x16], #16\n"      \
322                 "stp x2, x3, [x16], #16\n"      \
323                 "stp x4, x5, [x16], #16\n"      \
324                 "stp x6, x7, [x16], #16\n"      \
325                 "stp x8, x9, [x16], #16\n"      \
326                 "stp x10, x11, [x16], #16\n"    \
327                 "stp x12, x13, [x16], #16\n"    \
328                 "stp x14, x15, [x16], #16\n"    \
329                 "stp xzr, x17, [x16], #16\n"    \
330                 "stp x18, x19, [x16], #16\n"    \
331                 "stp x20, x21, [x16], #16\n"    \
332                 "stp x22, x23, [x16], #16\n"    \
333                 "stp x24, x25, [x16], #16\n"    \
334                 "stp x26, x27, [x16], #16\n"    \
335                 "stp x28, x29, [x16], #16\n"    \
336                 "stp x30, xzr, [x16]\n" \
337                 "mov x30, sp\n"                         \
338                 "str x30, [x16, #8]\n"          \
339                 :                                                       \
340                 : "r" (&ctx.regs)                       \
341                 : "x30", "memory"                       \
342         );                                                              \
343         __asm__ __volatile__( \
344                 "adr %0, L0\n" \
345                 "L0:\n" \
346                 : "=r" (ctx.pc)         \
347                 :                                       \
348                 : "memory"                       \
349         ); \
350 } while (0)
351
352 #define MONO_ARCH_HAS_MONO_CONTEXT 1
353
354 #elif defined(__mono_ppc__) /* defined(__arm__) */
355
356 /* we define our own structure and we'll copy the data
357  * from sigcontext/ucontext/mach when we need it.
358  * This also makes us save stack space and time when copying
359  * We might also want to add an additional field to propagate
360  * the original context from the signal handler.
361  */
362 typedef struct {
363         gulong sc_ir;          // pc 
364         gulong sc_sp;          // r1
365         mgreg_t regs [19]; /*FIXME, this must be changed to 32 for sgen*/
366         double fregs [18];
367 } MonoContext;
368
369 /* we have the stack pointer, not the base pointer in sigcontext */
370 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (gulong)ip; } while (0);
371 /* FIXME: should be called SET_SP */
372 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
373 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)sp; } while (0);
374
375 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
376 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
377 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
378
379 #elif defined(__sparc__) || defined(sparc) /* defined(__mono_ppc__) */
380
381 typedef struct MonoContext {
382         guint8 *ip;
383         gpointer *sp;
384         gpointer *fp;
385 } MonoContext;
386
387 #define MONO_CONTEXT_SET_IP(ctx,eip) do { (ctx)->ip = (gpointer)(eip); } while (0); 
388 #define MONO_CONTEXT_SET_BP(ctx,ebp) do { (ctx)->fp = (gpointer*)(ebp); } while (0); 
389 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->sp = (gpointer*)(esp); } while (0); 
390
391 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->ip))
392 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->fp))
393 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
394
395 #elif defined(__ia64__) /*defined(__sparc__) || defined(sparc) */
396
397 #ifndef UNW_LOCAL_ONLY
398
399 #define UNW_LOCAL_ONLY
400 #include <libunwind.h>
401
402 #endif
403
404 typedef struct MonoContext {
405         unw_cursor_t cursor;
406         /* Whenever the ip in 'cursor' points to the ip where the exception happened */
407         /* This is true for the initial context for exceptions thrown from signal handlers */
408         gboolean precise_ip;
409 } MonoContext;
410
411 /*XXX SET_BP is missing*/
412 #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)
413 #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)
414
415 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)(mono_ia64_context_get_ip ((ctx))))
416 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)(mono_ia64_context_get_fp ((ctx))))
417 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)(mono_ia64_context_get_sp ((ctx))))
418
419 static inline unw_word_t
420 mono_ia64_context_get_ip (MonoContext *ctx)
421 {
422         unw_word_t ip;
423         int err;
424
425         err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
426         g_assert (err == 0);
427
428         if (ctx->precise_ip) {
429                 return ip;
430         } else {
431                 /* Subtrack 1 so ip points into the actual instruction */
432                 return ip - 1;
433         }
434 }
435
436 static inline unw_word_t
437 mono_ia64_context_get_sp (MonoContext *ctx)
438 {
439         unw_word_t sp;
440         int err;
441
442         err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
443         g_assert (err == 0);
444
445         return sp;
446 }
447
448 static inline unw_word_t
449 mono_ia64_context_get_fp (MonoContext *ctx)
450 {
451         unw_cursor_t new_cursor;
452         unw_word_t fp;
453         int err;
454
455         {
456                 unw_word_t ip, sp;
457
458                 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
459                 g_assert (err == 0);
460
461                 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
462                 g_assert (err == 0);
463         }
464
465         /* fp is the SP of the parent frame */
466         new_cursor = ctx->cursor;
467
468         err = unw_step (&new_cursor);
469         g_assert (err >= 0);
470
471         err = unw_get_reg (&new_cursor, UNW_IA64_SP, &fp);
472         g_assert (err == 0);
473
474         return fp;
475 }
476
477 #elif ((defined(__mips__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_MIPS))) && SIZEOF_REGISTER == 4 /* defined(__ia64__) */
478
479 #include <mono/arch/mips/mips-codegen.h>
480
481 typedef struct {
482         mgreg_t     sc_pc;
483         mgreg_t         sc_regs [32];
484         gfloat          sc_fpregs [32];
485 } MonoContext;
486
487 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_pc = (mgreg_t)(ip); } while (0);
488 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_regs[mips_fp] = (mgreg_t)(bp); } while (0);
489 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_regs[mips_sp] = (mgreg_t)(sp); } while (0);
490
491 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_pc))
492 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_regs[mips_fp]))
493 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_regs[mips_sp]))
494
495 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
496         __asm__ __volatile__(   \
497                 "sw $0,0(%0)\n\t"       \
498                 "sw $1,4(%0)\n\t"       \
499                 "sw $2,8(%0)\n\t"       \
500                 "sw $3,12(%0)\n\t"      \
501                 "sw $4,16(%0)\n\t"      \
502                 "sw $5,20(%0)\n\t"      \
503                 "sw $6,24(%0)\n\t"      \
504                 "sw $7,28(%0)\n\t"      \
505                 "sw $8,32(%0)\n\t"      \
506                 "sw $9,36(%0)\n\t"      \
507                 "sw $10,40(%0)\n\t"     \
508                 "sw $11,44(%0)\n\t"     \
509                 "sw $12,48(%0)\n\t"     \
510                 "sw $13,52(%0)\n\t"     \
511                 "sw $14,56(%0)\n\t"     \
512                 "sw $15,60(%0)\n\t"     \
513                 "sw $16,64(%0)\n\t"     \
514                 "sw $17,68(%0)\n\t"     \
515                 "sw $18,72(%0)\n\t"     \
516                 "sw $19,76(%0)\n\t"     \
517                 "sw $20,80(%0)\n\t"     \
518                 "sw $21,84(%0)\n\t"     \
519                 "sw $22,88(%0)\n\t"     \
520                 "sw $23,92(%0)\n\t"     \
521                 "sw $24,96(%0)\n\t"     \
522                 "sw $25,100(%0)\n\t"    \
523                 "sw $26,104(%0)\n\t"    \
524                 "sw $27,108(%0)\n\t"    \
525                 "sw $28,112(%0)\n\t"    \
526                 "sw $29,116(%0)\n\t"    \
527                 "sw $30,120(%0)\n\t"    \
528                 "sw $31,124(%0)\n\t"    \
529                 : : "r" (&(ctx).sc_regs [0])    \
530                 : "memory"                      \
531         )
532
533 #elif defined(__s390x__)
534
535 #define MONO_ARCH_HAS_MONO_CONTEXT 1
536
537 typedef struct ucontext MonoContext;
538
539 #define MONO_CONTEXT_SET_IP(ctx,ip)                                     \
540         do {                                                            \
541                 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip;       \
542                 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip;        \
543         } while (0); 
544
545 #define MONO_CONTEXT_SET_SP(ctx,bp) MONO_CONTEXT_SET_BP((ctx),(bp))
546 #define MONO_CONTEXT_SET_BP(ctx,bp)                                     \
547         do {                                                            \
548                 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp;       \
549                 (ctx)->uc_stack.ss_sp        = (void*)bp;               \
550         } while (0) 
551
552 #define MONO_CONTEXT_GET_IP(ctx) (gpointer) (ctx)->uc_mcontext.psw.addr
553 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
554 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[11]))
555
556 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
557         __asm__ __volatile__(   \
558                 "stmg   %%r0,%%r15,0(%0)\n"     \
559                 : : "r" (&(ctx).uc_mcontext.gregs[0])   \
560                 : "memory"                      \
561         )
562
563 #else
564
565 #error "Implement mono-context for the current arch"
566
567 #endif
568
569 /*
570  * The naming is misleading, the SIGCTX argument should be the platform's context
571  * structure (ucontext_c on posix, CONTEXT on windows).
572  */
573 void mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx);
574
575 /*
576  * This will not completely initialize SIGCTX since MonoContext contains less
577  * information that the system context. The caller should obtain a SIGCTX from
578  * the system, and use this function to override the parts of it which are
579  * also in MonoContext.
580  */
581 void mono_monoctx_to_sigctx (MonoContext *mctx, void *sigctx);
582
583 #endif /* __MONO_MONO_CONTEXT_H__ */