Merge pull request #2377 from joelmartinez/docs-multiassembly-extension-fix
[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  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7  */
8
9
10 #ifndef __MONO_MONO_CONTEXT_H__
11 #define __MONO_MONO_CONTEXT_H__
12
13 #include "mono-compiler.h"
14 #include "mono-sigcontext.h"
15 #include "mono-machine.h"
16
17 #ifdef HAVE_SIGNAL_H
18 #include <signal.h>
19 #endif
20
21 /*
22  * General notes about mono-context.
23  * Each arch defines a MonoContext struct with all GPR regs + IP/PC.
24  * IP/PC should be the last element of the struct (this is a mild sgen constraint we could drop if needed)
25  * Macros to get/set BP, SP and IP are defined too.
26  * MONO_CONTEXT_GET_CURRENT captures the current context as close as possible. One reg might be clobbered
27  *  to hold the address of the target MonoContext. It will be a caller save one, so should not be a problem.
28  */
29 #if (defined(__i386__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_X86))
30
31 /*HACK, move this to an eventual mono-signal.c*/
32 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
33        defined(__FreeBSD__) || defined(__OpenBSD__)
34 #if defined(HAVE_SIGACTION) || defined(__APPLE__)  // the __APPLE__ check is required for the tvos simulator, which has ucontext_t but not sigaction
35 #define MONO_SIGNAL_USE_UCONTEXT_T 1
36 #endif
37 #endif
38
39 #if defined(__native_client__)
40 #undef MONO_SIGNAL_USE_UCONTEXT_T
41 #endif
42
43 #ifdef HOST_WIN32
44 /* sigcontext surrogate */
45 struct sigcontext {
46         unsigned int eax;
47         unsigned int ebx;
48         unsigned int ecx;
49         unsigned int edx;
50         unsigned int ebp;
51         unsigned int esp;
52         unsigned int esi;
53         unsigned int edi;
54         unsigned int eip;
55 };
56 #endif
57
58 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
59 # define SC_EAX sc_eax
60 # define SC_EBX sc_ebx
61 # define SC_ECX sc_ecx
62 # define SC_EDX sc_edx
63 # define SC_EBP sc_ebp
64 # define SC_EIP sc_eip
65 # define SC_ESP sc_esp
66 # define SC_EDI sc_edi
67 # define SC_ESI sc_esi
68 #elif defined(__HAIKU__)
69 # define SC_EAX regs.eax
70 # define SC_EBX regs._reserved_2[2]
71 # define SC_ECX regs.ecx
72 # define SC_EDX regs.edx
73 # define SC_EBP regs.ebp
74 # define SC_EIP regs.eip
75 # define SC_ESP regs.esp
76 # define SC_EDI regs._reserved_2[0]
77 # define SC_ESI regs._reserved_2[1]
78 #else
79 # define SC_EAX eax
80 # define SC_EBX ebx
81 # define SC_ECX ecx
82 # define SC_EDX edx
83 # define SC_EBP ebp
84 # define SC_EIP eip
85 # define SC_ESP esp
86 # define SC_EDI edi
87 # define SC_ESI esi
88 #endif
89
90 typedef struct {
91         mgreg_t eax;
92         mgreg_t ebx;
93         mgreg_t ecx;
94         mgreg_t edx;
95         mgreg_t ebp;
96         mgreg_t esp;
97         mgreg_t esi;
98         mgreg_t edi;
99         mgreg_t eip;
100 } MonoContext;
101
102 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (mgreg_t)(ip); } while (0); 
103 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (mgreg_t)(bp); } while (0); 
104 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (mgreg_t)(sp); } while (0); 
105
106 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
107 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
108 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
109
110 /*We set EAX to zero since we are clobering it anyway*/
111 #ifdef _MSC_VER
112 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
113         void *_ptr = &(ctx);                                                                                            \
114         __asm {                                                                                                                         \
115          __asm mov eax, _ptr                                                                                            \
116          __asm mov [eax+0x00], eax                                                                                      \
117          __asm mov [eax+0x04], ebx                                                                                      \
118          __asm mov [eax+0x08], ecx                                                                                      \
119          __asm mov [eax+0x0c], edx                                                                                      \
120          __asm mov [eax+0x10], ebp                                                                                      \
121          __asm mov [eax+0x14], esp                                                                                      \
122          __asm mov [eax+0x18], esi                                                                                      \
123          __asm mov [eax+0x1c], edi                                                                                      \
124          __asm call __mono_context_get_ip                                                                       \
125          __asm __mono_context_get_ip:                                                                           \
126          __asm pop dword ptr [eax+0x20]                                                                         \
127                  }                                                                                                                              \
128         } while (0)
129 #else
130 #define MONO_CONTEXT_GET_CURRENT(ctx) \
131         __asm__ __volatile__(   \
132         "movl $0x0, 0x00(%0)\n" \
133         "mov %%ebx, 0x04(%0)\n" \
134         "mov %%ecx, 0x08(%0)\n" \
135         "mov %%edx, 0x0c(%0)\n" \
136         "mov %%ebp, 0x10(%0)\n" \
137         "mov %%esp, 0x14(%0)\n" \
138         "mov %%esi, 0x18(%0)\n" \
139         "mov %%edi, 0x1c(%0)\n" \
140         "call 1f\n"     \
141         "1: pop 0x20(%0)\n"     \
142         :       \
143         : "a" (&(ctx))  \
144         : "memory")
145 #endif
146
147 #define MONO_ARCH_HAS_MONO_CONTEXT 1
148
149 #elif (defined(__x86_64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_AMD64)) /* defined(__i386__) */
150
151 #include <mono/arch/amd64/amd64-codegen.h>
152
153 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
154
155 #if defined(HAVE_SIGACTION) || defined(__APPLE__)  // the __APPLE__ check is required for the tvos simulator, which has ucontext_t but not sigaction
156 #define MONO_SIGNAL_USE_UCONTEXT_T 1
157 #endif
158
159 #endif
160
161 typedef struct {
162         mgreg_t gregs [AMD64_NREG];
163         double fregs [AMD64_XMM_NREG];
164 } MonoContext;
165
166 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->gregs [AMD64_RIP] = (mgreg_t)(ip); } while (0);
167 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->gregs [AMD64_RBP] = (mgreg_t)(bp); } while (0);
168 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->gregs [AMD64_RSP] = (mgreg_t)(esp); } while (0);
169
170 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->gregs [AMD64_RIP]))
171 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->gregs [AMD64_RBP]))
172 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->gregs [AMD64_RSP]))
173
174 #if defined (HOST_WIN32) && !defined(__GNUC__)
175 /* msvc doesn't support inline assembly, so have to use a separate .asm file */
176 extern void mono_context_get_current (void *);
177 #define MONO_CONTEXT_GET_CURRENT(ctx) do { mono_context_get_current((void*)&(ctx)); } while (0)
178
179 #elif defined(__native_client__)
180 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
181         __asm__ __volatile__(   \
182                 "movq $0x0,  %%nacl:0x00(%%r15, %0, 1)\n"       \
183                 "movq %%rcx, %%nacl:0x08(%%r15, %0, 1)\n"       \
184                 "movq %%rdx, %%nacl:0x10(%%r15, %0, 1)\n"       \
185                 "movq %%rbx, %%nacl:0x18(%%r15, %0, 1)\n"       \
186                 "movq %%rsp, %%nacl:0x20(%%r15, %0, 1)\n"       \
187                 "movq %%rbp, %%nacl:0x28(%%r15, %0, 1)\n"       \
188                 "movq %%rsi, %%nacl:0x30(%%r15, %0, 1)\n"       \
189                 "movq %%rdi, %%nacl:0x38(%%r15, %0, 1)\n"       \
190                 "movq %%r8,  %%nacl:0x40(%%r15, %0, 1)\n"       \
191                 "movq %%r9,  %%nacl:0x48(%%r15, %0, 1)\n"       \
192                 "movq %%r10, %%nacl:0x50(%%r15, %0, 1)\n"       \
193                 "movq %%r11, %%nacl:0x58(%%r15, %0, 1)\n"       \
194                 "movq %%r12, %%nacl:0x60(%%r15, %0, 1)\n"       \
195                 "movq %%r13, %%nacl:0x68(%%r15, %0, 1)\n"       \
196                 "movq %%r14, %%nacl:0x70(%%r15, %0, 1)\n"       \
197                 "movq %%r15, %%nacl:0x78(%%r15, %0, 1)\n"       \
198                 "leaq (%%rip), %%rdx\n" \
199                 "movq %%rdx, %%nacl:0x80(%%r15, %0, 1)\n"       \
200                 :       \
201                 : "a" ((int64_t)&(ctx)) \
202                 : "rdx", "memory")
203 #else
204
205 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
206         __asm__ __volatile__(   \
207                 "movq $0x0,  0x00(%0)\n"        \
208                 "movq %%rcx, 0x08(%0)\n"        \
209                 "movq %%rdx, 0x10(%0)\n"        \
210                 "movq %%rbx, 0x18(%0)\n"        \
211                 "movq %%rsp, 0x20(%0)\n"        \
212                 "movq %%rbp, 0x28(%0)\n"        \
213                 "movq %%rsi, 0x30(%0)\n"        \
214                 "movq %%rdi, 0x38(%0)\n"        \
215                 "movq %%r8,  0x40(%0)\n"        \
216                 "movq %%r9,  0x48(%0)\n"        \
217                 "movq %%r10, 0x50(%0)\n"        \
218                 "movq %%r11, 0x58(%0)\n"        \
219                 "movq %%r12, 0x60(%0)\n"        \
220                 "movq %%r13, 0x68(%0)\n"        \
221                 "movq %%r14, 0x70(%0)\n"        \
222                 "movq %%r15, 0x78(%0)\n"        \
223                 /* "leaq (%%rip), %%rdx\n" is not understood by icc */  \
224                 ".byte 0x48, 0x8d, 0x15, 0x00, 0x00, 0x00, 0x00\n" \
225                 "movq %%rdx, 0x80(%0)\n"        \
226                 :       \
227                 : "a" (&(ctx))  \
228                 : "rdx", "memory")
229 #endif
230
231 #define MONO_ARCH_HAS_MONO_CONTEXT 1
232
233 #elif (defined(__arm__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM)) /* defined(__x86_64__) */
234
235 #include <mono/arch/arm/arm-codegen.h>
236
237 typedef struct {
238         mgreg_t pc;
239         mgreg_t regs [16];
240         double fregs [16];
241         mgreg_t cpsr;
242 } MonoContext;
243
244 /* we have the stack pointer, not the base pointer in sigcontext */
245 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0); 
246 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0); 
247 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0); 
248
249 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->pc))
250 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ARMREG_FP]))
251 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->regs [ARMREG_SP]))
252
253 #if defined(HOST_WATCHOS)
254
255 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
256         gpointer _dummy; \
257     ctx.regs [ARMREG_SP] = &_dummy; \
258 } while (0);
259
260 #else
261
262 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
263         __asm__ __volatile__(                   \
264                 "push {r0}\n"                           \
265                 "push {r1}\n"                           \
266                 "mov r0, %0\n"                          \
267                 "ldr r1, [sp, #4]\n"                    \
268                 "str r1, [r0]!\n"                       \
269                 "ldr r1, [sp, #0]\n"                    \
270                 "str r1, [r0]!\n"                       \
271                 "stmia r0!, {r2-r12}\n"         \
272                 "str sp, [r0]!\n"                       \
273                 "str lr, [r0]!\n"                       \
274                 "mov r1, pc\n"                          \
275                 "str r1, [r0]!\n"                       \
276                 "pop {r1}\n"                            \
277                 "pop {r0}\n"                            \
278                 :                                                       \
279                 : "r" (&ctx.regs)                       \
280                 : "memory"                                      \
281         );                                                              \
282         ctx.pc = ctx.regs [15];                 \
283 } while (0)
284
285 #endif
286
287 #define MONO_ARCH_HAS_MONO_CONTEXT 1
288
289 #elif (defined(__aarch64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM64))
290
291 #include <mono/arch/arm64/arm64-codegen.h>
292
293 typedef struct {
294         mgreg_t regs [32];
295         double fregs [32];
296         mgreg_t pc;
297 } MonoContext;
298
299 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0)
300 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0);
301 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0);
302
303 #define MONO_CONTEXT_GET_IP(ctx) (gpointer)((ctx)->pc)
304 #define MONO_CONTEXT_GET_BP(ctx) (gpointer)((ctx)->regs [ARMREG_FP])
305 #define MONO_CONTEXT_GET_SP(ctx) (gpointer)((ctx)->regs [ARMREG_SP])
306
307 #if defined (HOST_APPLETVOS)
308
309 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
310         arm_unified_thread_state_t thread_state;        \
311         int state_flavor = ARM_UNIFIED_THREAD_STATE;    \
312         unsigned state_count = ARM_UNIFIED_THREAD_STATE_COUNT;  \
313         thread_port_t self = mach_thread_self ();       \
314         kern_return_t ret = thread_get_state (self, state_flavor, (thread_state_t) &thread_state, &state_count);        \
315         g_assert (ret == 0);    \
316         mono_mach_arch_thread_state_to_mono_context ((thread_state_t)&thread_state, &ctx); \
317         mach_port_deallocate (current_task (), self);   \
318 } while (0);
319
320 #else
321
322 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
323         __asm__ __volatile__(                   \
324                 "mov x16, %0\n" \
325                 "stp x0, x1, [x16], #16\n"      \
326                 "stp x2, x3, [x16], #16\n"      \
327                 "stp x4, x5, [x16], #16\n"      \
328                 "stp x6, x7, [x16], #16\n"      \
329                 "stp x8, x9, [x16], #16\n"      \
330                 "stp x10, x11, [x16], #16\n"    \
331                 "stp x12, x13, [x16], #16\n"    \
332                 "stp x14, x15, [x16], #16\n"    \
333                 "stp xzr, x17, [x16], #16\n"    \
334                 "stp x18, x19, [x16], #16\n"    \
335                 "stp x20, x21, [x16], #16\n"    \
336                 "stp x22, x23, [x16], #16\n"    \
337                 "stp x24, x25, [x16], #16\n"    \
338                 "stp x26, x27, [x16], #16\n"    \
339                 "stp x28, x29, [x16], #16\n"    \
340                 "stp x30, xzr, [x16]\n" \
341                 "mov x30, sp\n"                         \
342                 "str x30, [x16, #8]\n"          \
343                 :                                                       \
344                 : "r" (&ctx.regs)                       \
345                 : "x30", "memory"                       \
346         );                                                              \
347         __asm__ __volatile__( \
348                 "adr %0, L0%=\n" \
349                 "L0%=:\n"       \
350                 : "=r" (ctx.pc)         \
351                 :                                       \
352                 : "memory"                       \
353         ); \
354 } while (0)
355
356 #endif
357
358 #define MONO_ARCH_HAS_MONO_CONTEXT 1
359
360 #elif defined(__mono_ppc__) /* defined(__arm__) */
361
362 /* we define our own structure and we'll copy the data
363  * from sigcontext/ucontext/mach when we need it.
364  * This also makes us save stack space and time when copying
365  * We might also want to add an additional field to propagate
366  * the original context from the signal handler.
367  */
368 #ifdef __mono_ppc64__
369
370 typedef struct {
371         gulong sc_ir;          // pc 
372         gulong sc_sp;          // r1
373         mgreg_t regs [32];
374         double fregs [32];
375 } MonoContext;
376
377 /* we have the stack pointer, not the base pointer in sigcontext */
378 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (gulong)ip; } while (0);
379 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
380 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)sp; } while (0);
381
382 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
383 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
384 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
385
386 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
387         __asm__ __volatile__(   \
388                 "std 0, 0(%0)\n"        \
389                 "std 1, 8(%0)\n"        \
390                 "std 0, 8*0+16(%0)\n"   \
391                 "std 1, 8*1+16(%0)\n"   \
392                 "std 2, 8*2+16(%0)\n"   \
393                 "std 3, 8*3+16(%0)\n"   \
394                 "std 4, 8*4+16(%0)\n"   \
395                 "std 5, 8*5+16(%0)\n"   \
396                 "std 6, 8*6+16(%0)\n"   \
397                 "std 7, 8*7+16(%0)\n"   \
398                 "std 8, 8*8+16(%0)\n"   \
399                 "std 9, 8*9+16(%0)\n"   \
400                 "std 10, 8*10+16(%0)\n" \
401                 "std 11, 8*11+16(%0)\n" \
402                 "std 12, 8*12+16(%0)\n" \
403                 "std 13, 8*13+16(%0)\n" \
404                 "std 14, 8*14+16(%0)\n" \
405                 "std 15, 8*15+16(%0)\n" \
406                 "std 16, 8*16+16(%0)\n" \
407                 "std 17, 8*17+16(%0)\n" \
408                 "std 18, 8*18+16(%0)\n" \
409                 "std 19, 8*19+16(%0)\n" \
410                 "std 20, 8*20+16(%0)\n" \
411                 "std 21, 8*21+16(%0)\n" \
412                 "std 22, 8*22+16(%0)\n" \
413                 "std 23, 8*23+16(%0)\n" \
414                 "std 24, 8*24+16(%0)\n" \
415                 "std 25, 8*25+16(%0)\n" \
416                 "std 26, 8*26+16(%0)\n" \
417                 "std 27, 8*27+16(%0)\n" \
418                 "std 28, 8*28+16(%0)\n" \
419                 "std 29, 8*29+16(%0)\n" \
420                 "std 30, 8*30+16(%0)\n" \
421                 "std 31, 8*31+16(%0)\n" \
422                 "stfd 0, 8*0+8*32+16(%0)\n"     \
423                 "stfd 1, 8*1+8*32+16(%0)\n"     \
424                 "stfd 2, 8*2+8*32+16(%0)\n"     \
425                 "stfd 3, 8*3+8*32+16(%0)\n"     \
426                 "stfd 4, 8*4+8*32+16(%0)\n"     \
427                 "stfd 5, 8*5+8*32+16(%0)\n"     \
428                 "stfd 6, 8*6+8*32+16(%0)\n"     \
429                 "stfd 7, 8*7+8*32+16(%0)\n"     \
430                 "stfd 8, 8*8+8*32+16(%0)\n"     \
431                 "stfd 9, 8*9+8*32+16(%0)\n"     \
432                 "stfd 10, 8*10+8*32+16(%0)\n"   \
433                 "stfd 11, 8*11+8*32+16(%0)\n"   \
434                 "stfd 12, 8*12+8*32+16(%0)\n"   \
435                 "stfd 13, 8*13+8*32+16(%0)\n"   \
436                 "stfd 14, 8*14+8*32+16(%0)\n"   \
437                 "stfd 15, 8*15+8*32+16(%0)\n"   \
438                 "stfd 16, 8*16+8*32+16(%0)\n"   \
439                 "stfd 17, 8*17+8*32+16(%0)\n"   \
440                 "stfd 18, 8*18+8*32+16(%0)\n"   \
441                 "stfd 19, 8*19+8*32+16(%0)\n"   \
442                 "stfd 20, 8*20+8*32+16(%0)\n"   \
443                 "stfd 21, 8*21+8*32+16(%0)\n"   \
444                 "stfd 22, 8*22+8*32+16(%0)\n"   \
445                 "stfd 23, 8*23+8*32+16(%0)\n"   \
446                 "stfd 24, 8*24+8*32+16(%0)\n"   \
447                 "stfd 25, 8*25+8*32+16(%0)\n"   \
448                 "stfd 26, 8*26+8*32+16(%0)\n"   \
449                 "stfd 27, 8*27+8*32+16(%0)\n"   \
450                 "stfd 28, 8*28+8*32+16(%0)\n"   \
451                 "stfd 29, 8*29+8*32+16(%0)\n"   \
452                 "stfd 30, 8*30+8*32+16(%0)\n"   \
453                 "stfd 31, 8*31+8*32+16(%0)\n"   \
454                 : : "r" (&(ctx))        \
455                 : "memory"                      \
456         )
457
458 #else /* !defined(__mono_ppc64__) */
459
460 typedef struct {
461         mgreg_t sc_ir;          // pc
462         mgreg_t sc_sp;          // r1
463         mgreg_t regs [32];
464         double fregs [32];
465 } MonoContext;
466
467 /* we have the stack pointer, not the base pointer in sigcontext */
468 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (mgreg_t)ip; } while (0);
469 /* FIXME: should be called SET_SP */
470 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (mgreg_t)bp; } while (0);
471 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (mgreg_t)sp; } while (0);
472
473 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
474 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
475 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
476
477 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
478         __asm__ __volatile__(   \
479                 "std 0, 0(%0)\n"        \
480                 "std 1, 4(%0)\n"        \
481                 "std 0, 4*0+8(%0)\n"    \
482                 "std 1, 4*1+8(%0)\n"    \
483                 "std 2, 4*2+8(%0)\n"    \
484                 "std 3, 4*3+8(%0)\n"    \
485                 "std 4, 4*4+8(%0)\n"    \
486                 "std 5, 4*5+8(%0)\n"    \
487                 "std 6, 4*6+8(%0)\n"    \
488                 "std 7, 4*7+8(%0)\n"    \
489                 "std 8, 4*8+8(%0)\n"    \
490                 "std 9, 4*9+8(%0)\n"    \
491                 "std 10, 4*10+8(%0)\n"  \
492                 "std 11, 4*11+8(%0)\n"  \
493                 "std 12, 4*12+8(%0)\n"  \
494                 "std 13, 4*13+8(%0)\n"  \
495                 "std 14, 4*14+8(%0)\n"  \
496                 "std 15, 4*15+8(%0)\n"  \
497                 "std 16, 4*16+8(%0)\n"  \
498                 "std 17, 4*17+8(%0)\n"  \
499                 "std 18, 4*18+8(%0)\n"  \
500                 "std 19, 4*19+8(%0)\n"  \
501                 "std 20, 4*20+8(%0)\n"  \
502                 "std 21, 4*21+8(%0)\n"  \
503                 "std 22, 4*22+8(%0)\n"  \
504                 "std 23, 4*23+8(%0)\n"  \
505                 "std 24, 4*24+8(%0)\n"  \
506                 "std 25, 4*25+8(%0)\n"  \
507                 "std 26, 4*26+8(%0)\n"  \
508                 "std 27, 4*27+8(%0)\n"  \
509                 "std 28, 4*28+8(%0)\n"  \
510                 "std 29, 4*29+8(%0)\n"  \
511                 "std 30, 4*30+8(%0)\n"  \
512                 "std 31, 4*31+8(%0)\n"  \
513                 "stfd 0, 8*0+4*32+8(%0)\n"      \
514                 "stfd 1, 8*1+4*32+8(%0)\n"      \
515                 "stfd 2, 8*2+4*32+8(%0)\n"      \
516                 "stfd 3, 8*3+4*32+8(%0)\n"      \
517                 "stfd 4, 8*4+4*32+8(%0)\n"      \
518                 "stfd 5, 8*5+4*32+8(%0)\n"      \
519                 "stfd 6, 8*6+4*32+8(%0)\n"      \
520                 "stfd 7, 8*7+4*32+8(%0)\n"      \
521                 "stfd 8, 8*8+4*32+8(%0)\n"      \
522                 "stfd 9, 8*9+4*32+8(%0)\n"      \
523                 "stfd 10, 8*10+4*32+8(%0)\n"    \
524                 "stfd 11, 8*11+4*32+8(%0)\n"    \
525                 "stfd 12, 8*12+4*32+8(%0)\n"    \
526                 "stfd 13, 8*13+4*32+8(%0)\n"    \
527                 "stfd 14, 8*14+4*32+8(%0)\n"    \
528                 "stfd 15, 8*15+4*32+8(%0)\n"    \
529                 "stfd 16, 8*16+4*32+8(%0)\n"    \
530                 "stfd 17, 8*17+4*32+8(%0)\n"    \
531                 "stfd 18, 8*18+4*32+8(%0)\n"    \
532                 "stfd 19, 8*19+4*32+8(%0)\n"    \
533                 "stfd 20, 8*20+4*32+8(%0)\n"    \
534                 "stfd 21, 8*21+4*32+8(%0)\n"    \
535                 "stfd 22, 8*22+4*32+8(%0)\n"    \
536                 "stfd 23, 8*23+4*32+8(%0)\n"    \
537                 "stfd 24, 8*24+4*32+8(%0)\n"    \
538                 "stfd 25, 8*25+4*32+8(%0)\n"    \
539                 "stfd 26, 8*26+4*32+8(%0)\n"    \
540                 "stfd 27, 8*27+4*32+8(%0)\n"    \
541                 "stfd 28, 8*28+4*32+8(%0)\n"    \
542                 "stfd 29, 8*29+4*32+8(%0)\n"    \
543                 "stfd 30, 8*30+4*32+8(%0)\n"    \
544                 "stfd 31, 8*31+4*32+8(%0)\n"    \
545                 : : "r" (&(ctx))        \
546                 : "memory"                      \
547         )
548
549 #endif
550
551 #define MONO_ARCH_HAS_MONO_CONTEXT 1
552
553 #elif defined(__sparc__) || defined(sparc) /* defined(__mono_ppc__) */
554
555 typedef struct MonoContext {
556         mgreg_t regs [15];
557         guint8 *ip;
558         gpointer *sp;
559         gpointer *fp;
560 } MonoContext;
561
562 #define MONO_CONTEXT_SET_IP(ctx,eip) do { (ctx)->ip = (gpointer)(eip); } while (0); 
563 #define MONO_CONTEXT_SET_BP(ctx,ebp) do { (ctx)->fp = (gpointer*)(ebp); } while (0); 
564 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->sp = (gpointer*)(esp); } while (0); 
565
566 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->ip))
567 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->fp))
568 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
569
570 #ifdef __sparcv9
571 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
572         __asm__ __volatile__(   \
573                 "st %%g1,[%0]\n"        \
574                 "st %%g2,[%0+0x08]\n"   \
575                 "st %%g3,[%0+0x10]\n"   \
576                 "st %%g4,[%0+0x18]\n"   \
577                 "st %%g5,[%0+0x20]\n"   \
578                 "st %%g6,[%0+0x28]\n"   \
579                 "st %%g7,[%0+0x30]\n"   \
580                 "st %%o0,[%0+0x38]\n"   \
581                 "st %%o1,[%0+0x40]\n"   \
582                 "st %%o2,[%0+0x48]\n"   \
583                 "st %%o3,[%0+0x50]\n"   \
584                 "st %%o4,[%0+0x58]\n"   \
585                 "st %%o5,[%0+0x60]\n"   \
586                 "st %%o6,[%0+0x68]\n"   \
587                 "st %%o7,[%0+0x70]\n"   \
588                 :                       \
589                 : "r" (&(ctx))          \
590                 : "memory"                      \
591         )
592 #else
593 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
594         __asm__ __volatile__(   \
595                 "st %%g1,[%0]\n"        \
596                 "st %%g2,[%0+0x04]\n"   \
597                 "st %%g3,[%0+0x08]\n"   \
598                 "st %%g4,[%0+0x0c]\n"   \
599                 "st %%g5,[%0+0x10]\n"   \
600                 "st %%g6,[%0+0x14]\n"   \
601                 "st %%g7,[%0+0x18]\n"   \
602                 "st %%o0,[%0+0x1c]\n"   \
603                 "st %%o1,[%0+0x20]\n"   \
604                 "st %%o2,[%0+0x24]\n"   \
605                 "st %%o3,[%0+0x28]\n"   \
606                 "st %%o4,[%0+0x2c]\n"   \
607                 "st %%o5,[%0+0x30]\n"   \
608                 "st %%o6,[%0+0x34]\n"   \
609                 "st %%o7,[%0+0x38]\n"   \
610                 :                       \
611                 : "r" (&(ctx))          \
612                 : "memory"                      \
613         )
614 #endif
615
616 #define MONO_ARCH_HAS_MONO_CONTEXT 1
617
618 #elif defined(__ia64__) /*defined(__sparc__) || defined(sparc) */
619
620 #ifndef UNW_LOCAL_ONLY
621
622 #define UNW_LOCAL_ONLY
623 #include <libunwind.h>
624
625 #endif
626
627 typedef struct MonoContext {
628         unw_cursor_t cursor;
629         /* Whenever the ip in 'cursor' points to the ip where the exception happened */
630         /* This is true for the initial context for exceptions thrown from signal handlers */
631         gboolean precise_ip;
632 } MonoContext;
633
634 /*XXX SET_BP is missing*/
635 #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)
636 #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)
637
638 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)(mono_ia64_context_get_ip ((ctx))))
639 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)(mono_ia64_context_get_fp ((ctx))))
640 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)(mono_ia64_context_get_sp ((ctx))))
641
642 static inline unw_word_t
643 mono_ia64_context_get_ip (MonoContext *ctx)
644 {
645         unw_word_t ip;
646         int err;
647
648         err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
649         g_assert (err == 0);
650
651         if (ctx->precise_ip) {
652                 return ip;
653         } else {
654                 /* Subtrack 1 so ip points into the actual instruction */
655                 return ip - 1;
656         }
657 }
658
659 static inline unw_word_t
660 mono_ia64_context_get_sp (MonoContext *ctx)
661 {
662         unw_word_t sp;
663         int err;
664
665         err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
666         g_assert (err == 0);
667
668         return sp;
669 }
670
671 static inline unw_word_t
672 mono_ia64_context_get_fp (MonoContext *ctx)
673 {
674         unw_cursor_t new_cursor;
675         unw_word_t fp;
676         int err;
677
678         {
679                 unw_word_t ip, sp;
680
681                 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
682                 g_assert (err == 0);
683
684                 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
685                 g_assert (err == 0);
686         }
687
688         /* fp is the SP of the parent frame */
689         new_cursor = ctx->cursor;
690
691         err = unw_step (&new_cursor);
692         g_assert (err >= 0);
693
694         err = unw_get_reg (&new_cursor, UNW_IA64_SP, &fp);
695         g_assert (err == 0);
696
697         return fp;
698 }
699
700 #elif ((defined(__mips__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_MIPS))) && SIZEOF_REGISTER == 4 /* defined(__ia64__) */
701
702 #include <mono/arch/mips/mips-codegen.h>
703
704 typedef struct {
705         mgreg_t     sc_pc;
706         mgreg_t         sc_regs [32];
707         gfloat          sc_fpregs [32];
708 } MonoContext;
709
710 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_pc = (mgreg_t)(ip); } while (0);
711 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_regs[mips_fp] = (mgreg_t)(bp); } while (0);
712 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_regs[mips_sp] = (mgreg_t)(sp); } while (0);
713
714 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_pc))
715 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_regs[mips_fp]))
716 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_regs[mips_sp]))
717
718 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
719         __asm__ __volatile__(   \
720                 "sw $0,0(%0)\n\t"       \
721                 "sw $1,4(%0)\n\t"       \
722                 "sw $2,8(%0)\n\t"       \
723                 "sw $3,12(%0)\n\t"      \
724                 "sw $4,16(%0)\n\t"      \
725                 "sw $5,20(%0)\n\t"      \
726                 "sw $6,24(%0)\n\t"      \
727                 "sw $7,28(%0)\n\t"      \
728                 "sw $8,32(%0)\n\t"      \
729                 "sw $9,36(%0)\n\t"      \
730                 "sw $10,40(%0)\n\t"     \
731                 "sw $11,44(%0)\n\t"     \
732                 "sw $12,48(%0)\n\t"     \
733                 "sw $13,52(%0)\n\t"     \
734                 "sw $14,56(%0)\n\t"     \
735                 "sw $15,60(%0)\n\t"     \
736                 "sw $16,64(%0)\n\t"     \
737                 "sw $17,68(%0)\n\t"     \
738                 "sw $18,72(%0)\n\t"     \
739                 "sw $19,76(%0)\n\t"     \
740                 "sw $20,80(%0)\n\t"     \
741                 "sw $21,84(%0)\n\t"     \
742                 "sw $22,88(%0)\n\t"     \
743                 "sw $23,92(%0)\n\t"     \
744                 "sw $24,96(%0)\n\t"     \
745                 "sw $25,100(%0)\n\t"    \
746                 "sw $26,104(%0)\n\t"    \
747                 "sw $27,108(%0)\n\t"    \
748                 "sw $28,112(%0)\n\t"    \
749                 "sw $29,116(%0)\n\t"    \
750                 "sw $30,120(%0)\n\t"    \
751                 "sw $31,124(%0)\n\t"    \
752                 : : "r" (&(ctx).sc_regs [0])    \
753                 : "memory"                      \
754         )
755
756 #elif defined(__s390x__)
757
758 #define MONO_ARCH_HAS_MONO_CONTEXT 1
759
760 typedef struct ucontext MonoContext;
761
762 #define MONO_CONTEXT_SET_IP(ctx,ip)                                     \
763         do {                                                            \
764                 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip;       \
765                 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip;        \
766         } while (0); 
767
768 #define MONO_CONTEXT_SET_SP(ctx,bp) MONO_CONTEXT_SET_BP((ctx),(bp))
769 #define MONO_CONTEXT_SET_BP(ctx,bp)                                     \
770         do {                                                            \
771                 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp;       \
772                 (ctx)->uc_stack.ss_sp        = (void*)bp;               \
773         } while (0) 
774
775 #define MONO_CONTEXT_GET_IP(ctx) (gpointer) (ctx)->uc_mcontext.psw.addr
776 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
777 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[11]))
778
779 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
780         __asm__ __volatile__(   \
781                 "stmg   %%r0,%%r15,0(%0)\n"     \
782                 : : "r" (&(ctx).uc_mcontext.gregs[0])   \
783                 : "memory"                      \
784         )
785
786 #else
787
788 #error "Implement mono-context for the current arch"
789
790 #endif
791
792 /*
793  * The naming is misleading, the SIGCTX argument should be the platform's context
794  * structure (ucontext_c on posix, CONTEXT on windows).
795  */
796 void mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx);
797
798 /*
799  * This will not completely initialize SIGCTX since MonoContext contains less
800  * information that the system context. The caller should obtain a SIGCTX from
801  * the system, and use this function to override the parts of it which are
802  * also in MonoContext.
803  */
804 void mono_monoctx_to_sigctx (MonoContext *mctx, void *sigctx);
805
806 #endif /* __MONO_MONO_CONTEXT_H__ */