Merge pull request #1190 from esdrubal/encoder-convert
[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 #ifdef _MSC_VER
109 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
110         void *_ptr = &(ctx);                                                                                            \
111         __asm {                                                                                                                         \
112          __asm mov eax, _ptr                                                                                            \
113          __asm mov [eax+0x00], eax                                                                                      \
114          __asm mov [eax+0x04], ebx                                                                                      \
115          __asm mov [eax+0x08], ecx                                                                                      \
116          __asm mov [eax+0x0c], edx                                                                                      \
117          __asm mov [eax+0x10], ebp                                                                                      \
118          __asm mov [eax+0x14], esp                                                                                      \
119          __asm mov [eax+0x18], esi                                                                                      \
120          __asm mov [eax+0x1c], edi                                                                                      \
121          __asm call __mono_context_get_ip                                                                       \
122          __asm __mono_context_get_ip:                                                                           \
123          __asm pop dword ptr [eax+0x20]                                                                         \
124                  }                                                                                                                              \
125         } while (0)
126 #else
127 #define MONO_CONTEXT_GET_CURRENT(ctx) \
128         __asm__ __volatile__(   \
129         "movl $0x0, 0x00(%0)\n" \
130         "mov %%ebx, 0x04(%0)\n" \
131         "mov %%ecx, 0x08(%0)\n" \
132         "mov %%edx, 0x0c(%0)\n" \
133         "mov %%ebp, 0x10(%0)\n" \
134         "mov %%esp, 0x14(%0)\n" \
135         "mov %%esi, 0x18(%0)\n" \
136         "mov %%edi, 0x1c(%0)\n" \
137         "call 1f\n"     \
138         "1: pop 0x20(%0)\n"     \
139         :       \
140         : "a" (&(ctx))  \
141         : "memory")
142 #endif
143
144 #define MONO_ARCH_HAS_MONO_CONTEXT 1
145
146 #elif (defined(__x86_64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_AMD64)) /* defined(__i386__) */
147
148
149 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
150
151 #define MONO_SIGNAL_USE_SIGACTION 1
152
153 #endif
154
155 typedef struct {
156         mgreg_t rax;
157         mgreg_t rbx;
158         mgreg_t rcx;
159         mgreg_t rdx;
160         mgreg_t rbp;
161         mgreg_t rsp;
162     mgreg_t rsi;
163         mgreg_t rdi;
164         mgreg_t r8;
165         mgreg_t r9;
166         mgreg_t r10;
167         mgreg_t r11;
168         mgreg_t r12;
169         mgreg_t r13;
170         mgreg_t r14;
171         mgreg_t r15;
172         mgreg_t rip;
173 } MonoContext;
174
175 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->rip = (mgreg_t)(ip); } while (0); 
176 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->rbp = (mgreg_t)(bp); } while (0); 
177 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->rsp = (mgreg_t)(esp); } while (0); 
178
179 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->rip))
180 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->rbp))
181 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->rsp))
182
183 #if defined (HOST_WIN32) && !defined(__GNUC__)
184 /* msvc doesn't support inline assembly, so have to use a separate .asm file */
185 extern void mono_context_get_current (void *);
186 #define MONO_CONTEXT_GET_CURRENT(ctx) do { mono_context_get_current((void*)&(ctx)); } while (0)
187
188 #elif defined(__native_client__)
189 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
190         __asm__ __volatile__(   \
191                 "movq $0x0,  %%nacl:0x00(%%r15, %0, 1)\n"       \
192                 "movq %%rbx, %%nacl:0x08(%%r15, %0, 1)\n"       \
193                 "movq %%rcx, %%nacl:0x10(%%r15, %0, 1)\n"       \
194                 "movq %%rdx, %%nacl:0x18(%%r15, %0, 1)\n"       \
195                 "movq %%rbp, %%nacl:0x20(%%r15, %0, 1)\n"       \
196                 "movq %%rsp, %%nacl:0x28(%%r15, %0, 1)\n"       \
197                 "movq %%rsi, %%nacl:0x30(%%r15, %0, 1)\n"       \
198                 "movq %%rdi, %%nacl:0x38(%%r15, %0, 1)\n"       \
199                 "movq %%r8,  %%nacl:0x40(%%r15, %0, 1)\n"       \
200                 "movq %%r9,  %%nacl:0x48(%%r15, %0, 1)\n"       \
201                 "movq %%r10, %%nacl:0x50(%%r15, %0, 1)\n"       \
202                 "movq %%r11, %%nacl:0x58(%%r15, %0, 1)\n"       \
203                 "movq %%r12, %%nacl:0x60(%%r15, %0, 1)\n"       \
204                 "movq %%r13, %%nacl:0x68(%%r15, %0, 1)\n"       \
205                 "movq %%r14, %%nacl:0x70(%%r15, %0, 1)\n"       \
206                 "movq %%r15, %%nacl:0x78(%%r15, %0, 1)\n"       \
207                 "leaq (%%rip), %%rdx\n" \
208                 "movq %%rdx, %%nacl:0x80(%%r15, %0, 1)\n"       \
209                 :       \
210                 : "a" ((int64_t)&(ctx)) \
211                 : "rdx", "memory")
212 #else
213 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
214         __asm__ __volatile__(   \
215                 "movq $0x0,  0x00(%0)\n"        \
216                 "movq %%rbx, 0x08(%0)\n"        \
217                 "movq %%rcx, 0x10(%0)\n"        \
218                 "movq %%rdx, 0x18(%0)\n"        \
219                 "movq %%rbp, 0x20(%0)\n"        \
220                 "movq %%rsp, 0x28(%0)\n"        \
221                 "movq %%rsi, 0x30(%0)\n"        \
222                 "movq %%rdi, 0x38(%0)\n"        \
223                 "movq %%r8,  0x40(%0)\n"        \
224                 "movq %%r9,  0x48(%0)\n"        \
225                 "movq %%r10, 0x50(%0)\n"        \
226                 "movq %%r11, 0x58(%0)\n"        \
227                 "movq %%r12, 0x60(%0)\n"        \
228                 "movq %%r13, 0x68(%0)\n"        \
229                 "movq %%r14, 0x70(%0)\n"        \
230                 "movq %%r15, 0x78(%0)\n"        \
231                 /* "leaq (%%rip), %%rdx\n" is not understood by icc */  \
232                 ".byte 0x48, 0x8d, 0x15, 0x00, 0x00, 0x00, 0x00\n" \
233                 "movq %%rdx, 0x80(%0)\n"        \
234                 :       \
235                 : "a" (&(ctx))  \
236                 : "rdx", "memory")
237 #endif
238
239 #if !defined(HOST_WIN32)
240 #define MONO_ARCH_HAS_MONO_CONTEXT 1
241 #endif
242
243 #elif (defined(__arm__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM)) /* defined(__x86_64__) */
244
245 typedef struct {
246         mgreg_t pc;
247         mgreg_t regs [16];
248         double fregs [16];
249         mgreg_t cpsr;
250 } MonoContext;
251
252 /* we have the stack pointer, not the base pointer in sigcontext */
253 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0); 
254 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0); 
255 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0); 
256
257 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->pc))
258 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ARMREG_FP]))
259 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->regs [ARMREG_SP]))
260
261 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
262         __asm__ __volatile__(                   \
263                 "push {r0}\n"                           \
264                 "push {r1}\n"                           \
265                 "mov r0, %0\n"                          \
266                 "ldr r1, [sp, #4]\n"                    \
267                 "str r1, [r0]!\n"                       \
268                 "ldr r1, [sp, #0]\n"                    \
269                 "str r1, [r0]!\n"                       \
270                 "stmia r0!, {r2-r12}\n"         \
271                 "str sp, [r0]!\n"                       \
272                 "str lr, [r0]!\n"                       \
273                 "mov r1, pc\n"                          \
274                 "str r1, [r0]!\n"                       \
275                 "pop {r1}\n"                            \
276                 "pop {r0}\n"                            \
277                 :                                                       \
278                 : "r" (&ctx.regs)                       \
279                 : "memory"                                      \
280         );                                                              \
281         ctx.pc = ctx.regs [15];                 \
282 } while (0)
283
284 #elif (defined(__aarch64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM64))
285
286 #include <mono/arch/arm64/arm64-codegen.h>
287
288 typedef struct {
289         mgreg_t regs [32];
290         double fregs [32];
291         mgreg_t pc;
292 } MonoContext;
293
294 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0)
295 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0);
296 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0);
297
298 #define MONO_CONTEXT_GET_IP(ctx) (gpointer)((ctx)->pc)
299 #define MONO_CONTEXT_GET_BP(ctx) (gpointer)((ctx)->regs [ARMREG_FP])
300 #define MONO_CONTEXT_GET_SP(ctx) (gpointer)((ctx)->regs [ARMREG_SP])
301
302 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
303         __asm__ __volatile__(                   \
304                 "mov x16, %0\n" \
305                 "stp x0, x1, [x16], #16\n"      \
306                 "stp x2, x3, [x16], #16\n"      \
307                 "stp x4, x5, [x16], #16\n"      \
308                 "stp x6, x7, [x16], #16\n"      \
309                 "stp x8, x9, [x16], #16\n"      \
310                 "stp x10, x11, [x16], #16\n"    \
311                 "stp x12, x13, [x16], #16\n"    \
312                 "stp x14, x15, [x16], #16\n"    \
313                 "stp xzr, x17, [x16], #16\n"    \
314                 "stp x18, x19, [x16], #16\n"    \
315                 "stp x20, x21, [x16], #16\n"    \
316                 "stp x22, x23, [x16], #16\n"    \
317                 "stp x24, x25, [x16], #16\n"    \
318                 "stp x26, x27, [x16], #16\n"    \
319                 "stp x28, x29, [x16], #16\n"    \
320                 "stp x30, xzr, [x16]\n" \
321                 "mov x30, sp\n"                         \
322                 "str x30, [x16, #8]\n"          \
323                 :                                                       \
324                 : "r" (&ctx.regs)                       \
325                 : "x30", "memory"                       \
326         );                                                              \
327         __asm__ __volatile__( \
328                 "adr %0, L0\n" \
329                 "L0:\n" \
330                 : "=r" (ctx.pc)         \
331                 :                                       \
332                 : "memory"                       \
333         ); \
334 } while (0)
335
336 #elif defined(__mono_ppc__) /* defined(__arm__) */
337
338 /* we define our own structure and we'll copy the data
339  * from sigcontext/ucontext/mach when we need it.
340  * This also makes us save stack space and time when copying
341  * We might also want to add an additional field to propagate
342  * the original context from the signal handler.
343  */
344 typedef struct {
345         gulong sc_ir;          // pc 
346         gulong sc_sp;          // r1
347         mgreg_t regs [19]; /*FIXME, this must be changed to 32 for sgen*/
348         double fregs [18];
349 } MonoContext;
350
351 /* we have the stack pointer, not the base pointer in sigcontext */
352 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (gulong)ip; } while (0);
353 /* FIXME: should be called SET_SP */
354 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
355 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)sp; } while (0);
356
357 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
358 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
359 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
360
361 #elif defined(__sparc__) || defined(sparc) /* defined(__mono_ppc__) */
362
363 typedef struct MonoContext {
364         guint8 *ip;
365         gpointer *sp;
366         gpointer *fp;
367 } MonoContext;
368
369 #define MONO_CONTEXT_SET_IP(ctx,eip) do { (ctx)->ip = (gpointer)(eip); } while (0); 
370 #define MONO_CONTEXT_SET_BP(ctx,ebp) do { (ctx)->fp = (gpointer*)(ebp); } while (0); 
371 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->sp = (gpointer*)(esp); } while (0); 
372
373 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->ip))
374 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->fp))
375 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
376
377 #elif defined(__ia64__) /*defined(__sparc__) || defined(sparc) */
378
379 #ifndef UNW_LOCAL_ONLY
380
381 #define UNW_LOCAL_ONLY
382 #include <libunwind.h>
383
384 #endif
385
386 typedef struct MonoContext {
387         unw_cursor_t cursor;
388         /* Whenever the ip in 'cursor' points to the ip where the exception happened */
389         /* This is true for the initial context for exceptions thrown from signal handlers */
390         gboolean precise_ip;
391 } MonoContext;
392
393 /*XXX SET_BP is missing*/
394 #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)
395 #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)
396
397 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)(mono_ia64_context_get_ip ((ctx))))
398 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)(mono_ia64_context_get_fp ((ctx))))
399 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)(mono_ia64_context_get_sp ((ctx))))
400
401 static inline unw_word_t
402 mono_ia64_context_get_ip (MonoContext *ctx)
403 {
404         unw_word_t ip;
405         int err;
406
407         err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
408         g_assert (err == 0);
409
410         if (ctx->precise_ip) {
411                 return ip;
412         } else {
413                 /* Subtrack 1 so ip points into the actual instruction */
414                 return ip - 1;
415         }
416 }
417
418 static inline unw_word_t
419 mono_ia64_context_get_sp (MonoContext *ctx)
420 {
421         unw_word_t sp;
422         int err;
423
424         err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
425         g_assert (err == 0);
426
427         return sp;
428 }
429
430 static inline unw_word_t
431 mono_ia64_context_get_fp (MonoContext *ctx)
432 {
433         unw_cursor_t new_cursor;
434         unw_word_t fp;
435         int err;
436
437         {
438                 unw_word_t ip, sp;
439
440                 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
441                 g_assert (err == 0);
442
443                 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
444                 g_assert (err == 0);
445         }
446
447         /* fp is the SP of the parent frame */
448         new_cursor = ctx->cursor;
449
450         err = unw_step (&new_cursor);
451         g_assert (err >= 0);
452
453         err = unw_get_reg (&new_cursor, UNW_IA64_SP, &fp);
454         g_assert (err == 0);
455
456         return fp;
457 }
458
459 #elif ((defined(__mips__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_MIPS))) && SIZEOF_REGISTER == 4 /* defined(__ia64__) */
460
461 typedef struct {
462         mgreg_t     sc_pc;
463         mgreg_t         sc_regs [32];
464         gfloat          sc_fpregs [32];
465 } MonoContext;
466
467 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_pc = (mgreg_t)(ip); } while (0);
468 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_regs[mips_fp] = (mgreg_t)(bp); } while (0);
469 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_regs[mips_sp] = (mgreg_t)(sp); } while (0);
470
471 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_pc))
472 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_regs[mips_fp]))
473 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_regs[mips_sp]))
474
475 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
476         __asm__ __volatile__(   \
477                 "sw $0,0(%0)\n\t"       \
478                 "sw $1,4(%0)\n\t"       \
479                 "sw $2,8(%0)\n\t"       \
480                 "sw $3,12(%0)\n\t"      \
481                 "sw $4,16(%0)\n\t"      \
482                 "sw $5,20(%0)\n\t"      \
483                 "sw $6,24(%0)\n\t"      \
484                 "sw $7,28(%0)\n\t"      \
485                 "sw $8,32(%0)\n\t"      \
486                 "sw $9,36(%0)\n\t"      \
487                 "sw $10,40(%0)\n\t"     \
488                 "sw $11,44(%0)\n\t"     \
489                 "sw $12,48(%0)\n\t"     \
490                 "sw $13,52(%0)\n\t"     \
491                 "sw $14,56(%0)\n\t"     \
492                 "sw $15,60(%0)\n\t"     \
493                 "sw $16,64(%0)\n\t"     \
494                 "sw $17,68(%0)\n\t"     \
495                 "sw $18,72(%0)\n\t"     \
496                 "sw $19,76(%0)\n\t"     \
497                 "sw $20,80(%0)\n\t"     \
498                 "sw $21,84(%0)\n\t"     \
499                 "sw $22,88(%0)\n\t"     \
500                 "sw $23,92(%0)\n\t"     \
501                 "sw $24,96(%0)\n\t"     \
502                 "sw $25,100(%0)\n\t"    \
503                 "sw $26,104(%0)\n\t"    \
504                 "sw $27,108(%0)\n\t"    \
505                 "sw $28,112(%0)\n\t"    \
506                 "sw $29,116(%0)\n\t"    \
507                 "sw $30,120(%0)\n\t"    \
508                 "sw $31,124(%0)\n\t"    \
509                 : : "r" (&(ctx).sc_regs [0])    \
510                 : "memory"                      \
511         )
512
513 #elif defined(__s390x__)
514
515 #define MONO_ARCH_HAS_MONO_CONTEXT 1
516
517 typedef struct ucontext MonoContext;
518
519 #define MONO_CONTEXT_SET_IP(ctx,ip)                                     \
520         do {                                                            \
521                 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip;       \
522                 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip;        \
523         } while (0); 
524
525 #define MONO_CONTEXT_SET_SP(ctx,bp) MONO_CONTEXT_SET_BP((ctx),(bp))
526 #define MONO_CONTEXT_SET_BP(ctx,bp)                                     \
527         do {                                                            \
528                 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp;       \
529                 (ctx)->uc_stack.ss_sp        = (void*)bp;               \
530         } while (0) 
531
532 #define MONO_CONTEXT_GET_IP(ctx) (gpointer) (ctx)->uc_mcontext.psw.addr
533 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
534 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[11]))
535
536 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
537         __asm__ __volatile__(   \
538                 "stmg   %%r0,%%r15,0(%0)\n"     \
539                 : : "r" (&(ctx).uc_mcontext.gregs[0])   \
540                 : "memory"                      \
541         )
542
543 #else
544
545 #error "Implement mono-context for the current arch"
546
547 #endif
548
549 void mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx) MONO_INTERNAL;
550 void mono_monoctx_to_sigctx (MonoContext *mctx, void *sigctx) MONO_INTERNAL;
551
552 #endif /* __MONO_MONO_CONTEXT_H__ */