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