First set of licensing changes
[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 #ifdef HAVE_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 #ifdef HAVE_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 } while (0);
257
258 #else
259
260 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
261         __asm__ __volatile__(                   \
262                 "push {r0}\n"                           \
263                 "push {r1}\n"                           \
264                 "mov r0, %0\n"                          \
265                 "ldr r1, [sp, #4]\n"                    \
266                 "str r1, [r0]!\n"                       \
267                 "ldr r1, [sp, #0]\n"                    \
268                 "str r1, [r0]!\n"                       \
269                 "stmia r0!, {r2-r12}\n"         \
270                 "str sp, [r0]!\n"                       \
271                 "str lr, [r0]!\n"                       \
272                 "mov r1, pc\n"                          \
273                 "str r1, [r0]!\n"                       \
274                 "pop {r1}\n"                            \
275                 "pop {r0}\n"                            \
276                 :                                                       \
277                 : "r" (&ctx.regs)                       \
278                 : "memory"                                      \
279         );                                                              \
280         ctx.pc = ctx.regs [15];                 \
281 } while (0)
282
283 #endif
284
285 #define MONO_ARCH_HAS_MONO_CONTEXT 1
286
287 #elif (defined(__aarch64__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_ARM64))
288
289 #include <mono/arch/arm64/arm64-codegen.h>
290
291 typedef struct {
292         mgreg_t regs [32];
293         double fregs [32];
294         mgreg_t pc;
295 } MonoContext;
296
297 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->pc = (mgreg_t)ip; } while (0)
298 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (mgreg_t)bp; } while (0);
299 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->regs [ARMREG_SP] = (mgreg_t)bp; } while (0);
300
301 #define MONO_CONTEXT_GET_IP(ctx) (gpointer)((ctx)->pc)
302 #define MONO_CONTEXT_GET_BP(ctx) (gpointer)((ctx)->regs [ARMREG_FP])
303 #define MONO_CONTEXT_GET_SP(ctx) (gpointer)((ctx)->regs [ARMREG_SP])
304
305 #if defined (HOST_APPLETVOS)
306
307 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
308         arm_unified_thread_state_t thread_state;        \
309         int state_flavor = ARM_UNIFIED_THREAD_STATE;    \
310         unsigned state_count = ARM_UNIFIED_THREAD_STATE_COUNT;  \
311         thread_port_t self = mach_thread_self ();       \
312         kern_return_t ret = thread_get_state (self, state_flavor, (thread_state_t) &thread_state, &state_count);        \
313         g_assert (ret == 0);    \
314         mono_mach_arch_thread_state_to_mono_context ((thread_state_t)&thread_state, &ctx); \
315         mach_port_deallocate (current_task (), self);   \
316 } while (0);
317
318 #else
319
320 #define MONO_CONTEXT_GET_CURRENT(ctx)   do {    \
321         __asm__ __volatile__(                   \
322                 "mov x16, %0\n" \
323                 "stp x0, x1, [x16], #16\n"      \
324                 "stp x2, x3, [x16], #16\n"      \
325                 "stp x4, x5, [x16], #16\n"      \
326                 "stp x6, x7, [x16], #16\n"      \
327                 "stp x8, x9, [x16], #16\n"      \
328                 "stp x10, x11, [x16], #16\n"    \
329                 "stp x12, x13, [x16], #16\n"    \
330                 "stp x14, x15, [x16], #16\n"    \
331                 "stp xzr, x17, [x16], #16\n"    \
332                 "stp x18, x19, [x16], #16\n"    \
333                 "stp x20, x21, [x16], #16\n"    \
334                 "stp x22, x23, [x16], #16\n"    \
335                 "stp x24, x25, [x16], #16\n"    \
336                 "stp x26, x27, [x16], #16\n"    \
337                 "stp x28, x29, [x16], #16\n"    \
338                 "stp x30, xzr, [x16]\n" \
339                 "mov x30, sp\n"                         \
340                 "str x30, [x16, #8]\n"          \
341                 :                                                       \
342                 : "r" (&ctx.regs)                       \
343                 : "x30", "memory"                       \
344         );                                                              \
345         __asm__ __volatile__( \
346                 "adr %0, L0%=\n" \
347                 "L0%=:\n"       \
348                 : "=r" (ctx.pc)         \
349                 :                                       \
350                 : "memory"                       \
351         ); \
352 } while (0)
353
354 #endif
355
356 #define MONO_ARCH_HAS_MONO_CONTEXT 1
357
358 #elif defined(__mono_ppc__) /* defined(__arm__) */
359
360 /* we define our own structure and we'll copy the data
361  * from sigcontext/ucontext/mach when we need it.
362  * This also makes us save stack space and time when copying
363  * We might also want to add an additional field to propagate
364  * the original context from the signal handler.
365  */
366 #ifdef __mono_ppc64__
367
368 typedef struct {
369         gulong sc_ir;          // pc 
370         gulong sc_sp;          // r1
371         mgreg_t regs [32];
372         double fregs [32];
373 } MonoContext;
374
375 /* we have the stack pointer, not the base pointer in sigcontext */
376 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (gulong)ip; } while (0);
377 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
378 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)sp; } while (0);
379
380 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
381 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
382 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
383
384 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
385         __asm__ __volatile__(   \
386                 "std 0, 0(%0)\n"        \
387                 "std 1, 8(%0)\n"        \
388                 "std 0, 8*0+16(%0)\n"   \
389                 "std 1, 8*1+16(%0)\n"   \
390                 "std 2, 8*2+16(%0)\n"   \
391                 "std 3, 8*3+16(%0)\n"   \
392                 "std 4, 8*4+16(%0)\n"   \
393                 "std 5, 8*5+16(%0)\n"   \
394                 "std 6, 8*6+16(%0)\n"   \
395                 "std 7, 8*7+16(%0)\n"   \
396                 "std 8, 8*8+16(%0)\n"   \
397                 "std 9, 8*9+16(%0)\n"   \
398                 "std 10, 8*10+16(%0)\n" \
399                 "std 11, 8*11+16(%0)\n" \
400                 "std 12, 8*12+16(%0)\n" \
401                 "std 13, 8*13+16(%0)\n" \
402                 "std 14, 8*14+16(%0)\n" \
403                 "std 15, 8*15+16(%0)\n" \
404                 "std 16, 8*16+16(%0)\n" \
405                 "std 17, 8*17+16(%0)\n" \
406                 "std 18, 8*18+16(%0)\n" \
407                 "std 19, 8*19+16(%0)\n" \
408                 "std 20, 8*20+16(%0)\n" \
409                 "std 21, 8*21+16(%0)\n" \
410                 "std 22, 8*22+16(%0)\n" \
411                 "std 23, 8*23+16(%0)\n" \
412                 "std 24, 8*24+16(%0)\n" \
413                 "std 25, 8*25+16(%0)\n" \
414                 "std 26, 8*26+16(%0)\n" \
415                 "std 27, 8*27+16(%0)\n" \
416                 "std 28, 8*28+16(%0)\n" \
417                 "std 29, 8*29+16(%0)\n" \
418                 "std 30, 8*30+16(%0)\n" \
419                 "std 31, 8*31+16(%0)\n" \
420                 "stfd 0, 8*0+8*32+16(%0)\n"     \
421                 "stfd 1, 8*1+8*32+16(%0)\n"     \
422                 "stfd 2, 8*2+8*32+16(%0)\n"     \
423                 "stfd 3, 8*3+8*32+16(%0)\n"     \
424                 "stfd 4, 8*4+8*32+16(%0)\n"     \
425                 "stfd 5, 8*5+8*32+16(%0)\n"     \
426                 "stfd 6, 8*6+8*32+16(%0)\n"     \
427                 "stfd 7, 8*7+8*32+16(%0)\n"     \
428                 "stfd 8, 8*8+8*32+16(%0)\n"     \
429                 "stfd 9, 8*9+8*32+16(%0)\n"     \
430                 "stfd 10, 8*10+8*32+16(%0)\n"   \
431                 "stfd 11, 8*11+8*32+16(%0)\n"   \
432                 "stfd 12, 8*12+8*32+16(%0)\n"   \
433                 "stfd 13, 8*13+8*32+16(%0)\n"   \
434                 "stfd 14, 8*14+8*32+16(%0)\n"   \
435                 "stfd 15, 8*15+8*32+16(%0)\n"   \
436                 "stfd 16, 8*16+8*32+16(%0)\n"   \
437                 "stfd 17, 8*17+8*32+16(%0)\n"   \
438                 "stfd 18, 8*18+8*32+16(%0)\n"   \
439                 "stfd 19, 8*19+8*32+16(%0)\n"   \
440                 "stfd 20, 8*20+8*32+16(%0)\n"   \
441                 "stfd 21, 8*21+8*32+16(%0)\n"   \
442                 "stfd 22, 8*22+8*32+16(%0)\n"   \
443                 "stfd 23, 8*23+8*32+16(%0)\n"   \
444                 "stfd 24, 8*24+8*32+16(%0)\n"   \
445                 "stfd 25, 8*25+8*32+16(%0)\n"   \
446                 "stfd 26, 8*26+8*32+16(%0)\n"   \
447                 "stfd 27, 8*27+8*32+16(%0)\n"   \
448                 "stfd 28, 8*28+8*32+16(%0)\n"   \
449                 "stfd 29, 8*29+8*32+16(%0)\n"   \
450                 "stfd 30, 8*30+8*32+16(%0)\n"   \
451                 "stfd 31, 8*31+8*32+16(%0)\n"   \
452                 : : "r" (&(ctx))        \
453                 : "memory"                      \
454         )
455
456 #define MONO_ARCH_HAS_MONO_CONTEXT 1
457
458 #else 
459
460 typedef struct {
461         gulong sc_ir;          // pc 
462         gulong sc_sp;          // r1
463         mgreg_t regs [19]; /*FIXME, this must be changed to 32 for sgen*/
464         double fregs [18];
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 = (gulong)ip; } while (0);
469 /* FIXME: should be called SET_SP */
470 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
471 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)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 #endif
477
478 #elif defined(__sparc__) || defined(sparc) /* defined(__mono_ppc__) */
479
480 typedef struct MonoContext {
481         guint8 *ip;
482         gpointer *sp;
483         gpointer *fp;
484 } MonoContext;
485
486 #define MONO_CONTEXT_SET_IP(ctx,eip) do { (ctx)->ip = (gpointer)(eip); } while (0); 
487 #define MONO_CONTEXT_SET_BP(ctx,ebp) do { (ctx)->fp = (gpointer*)(ebp); } while (0); 
488 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->sp = (gpointer*)(esp); } while (0); 
489
490 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->ip))
491 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->fp))
492 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
493
494 #elif defined(__ia64__) /*defined(__sparc__) || defined(sparc) */
495
496 #ifndef UNW_LOCAL_ONLY
497
498 #define UNW_LOCAL_ONLY
499 #include <libunwind.h>
500
501 #endif
502
503 typedef struct MonoContext {
504         unw_cursor_t cursor;
505         /* Whenever the ip in 'cursor' points to the ip where the exception happened */
506         /* This is true for the initial context for exceptions thrown from signal handlers */
507         gboolean precise_ip;
508 } MonoContext;
509
510 /*XXX SET_BP is missing*/
511 #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)
512 #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)
513
514 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)(mono_ia64_context_get_ip ((ctx))))
515 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)(mono_ia64_context_get_fp ((ctx))))
516 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)(mono_ia64_context_get_sp ((ctx))))
517
518 static inline unw_word_t
519 mono_ia64_context_get_ip (MonoContext *ctx)
520 {
521         unw_word_t ip;
522         int err;
523
524         err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
525         g_assert (err == 0);
526
527         if (ctx->precise_ip) {
528                 return ip;
529         } else {
530                 /* Subtrack 1 so ip points into the actual instruction */
531                 return ip - 1;
532         }
533 }
534
535 static inline unw_word_t
536 mono_ia64_context_get_sp (MonoContext *ctx)
537 {
538         unw_word_t sp;
539         int err;
540
541         err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
542         g_assert (err == 0);
543
544         return sp;
545 }
546
547 static inline unw_word_t
548 mono_ia64_context_get_fp (MonoContext *ctx)
549 {
550         unw_cursor_t new_cursor;
551         unw_word_t fp;
552         int err;
553
554         {
555                 unw_word_t ip, sp;
556
557                 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
558                 g_assert (err == 0);
559
560                 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
561                 g_assert (err == 0);
562         }
563
564         /* fp is the SP of the parent frame */
565         new_cursor = ctx->cursor;
566
567         err = unw_step (&new_cursor);
568         g_assert (err >= 0);
569
570         err = unw_get_reg (&new_cursor, UNW_IA64_SP, &fp);
571         g_assert (err == 0);
572
573         return fp;
574 }
575
576 #elif ((defined(__mips__) && !defined(MONO_CROSS_COMPILE)) || (defined(TARGET_MIPS))) && SIZEOF_REGISTER == 4 /* defined(__ia64__) */
577
578 #include <mono/arch/mips/mips-codegen.h>
579
580 typedef struct {
581         mgreg_t     sc_pc;
582         mgreg_t         sc_regs [32];
583         gfloat          sc_fpregs [32];
584 } MonoContext;
585
586 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_pc = (mgreg_t)(ip); } while (0);
587 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_regs[mips_fp] = (mgreg_t)(bp); } while (0);
588 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_regs[mips_sp] = (mgreg_t)(sp); } while (0);
589
590 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_pc))
591 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_regs[mips_fp]))
592 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_regs[mips_sp]))
593
594 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
595         __asm__ __volatile__(   \
596                 "sw $0,0(%0)\n\t"       \
597                 "sw $1,4(%0)\n\t"       \
598                 "sw $2,8(%0)\n\t"       \
599                 "sw $3,12(%0)\n\t"      \
600                 "sw $4,16(%0)\n\t"      \
601                 "sw $5,20(%0)\n\t"      \
602                 "sw $6,24(%0)\n\t"      \
603                 "sw $7,28(%0)\n\t"      \
604                 "sw $8,32(%0)\n\t"      \
605                 "sw $9,36(%0)\n\t"      \
606                 "sw $10,40(%0)\n\t"     \
607                 "sw $11,44(%0)\n\t"     \
608                 "sw $12,48(%0)\n\t"     \
609                 "sw $13,52(%0)\n\t"     \
610                 "sw $14,56(%0)\n\t"     \
611                 "sw $15,60(%0)\n\t"     \
612                 "sw $16,64(%0)\n\t"     \
613                 "sw $17,68(%0)\n\t"     \
614                 "sw $18,72(%0)\n\t"     \
615                 "sw $19,76(%0)\n\t"     \
616                 "sw $20,80(%0)\n\t"     \
617                 "sw $21,84(%0)\n\t"     \
618                 "sw $22,88(%0)\n\t"     \
619                 "sw $23,92(%0)\n\t"     \
620                 "sw $24,96(%0)\n\t"     \
621                 "sw $25,100(%0)\n\t"    \
622                 "sw $26,104(%0)\n\t"    \
623                 "sw $27,108(%0)\n\t"    \
624                 "sw $28,112(%0)\n\t"    \
625                 "sw $29,116(%0)\n\t"    \
626                 "sw $30,120(%0)\n\t"    \
627                 "sw $31,124(%0)\n\t"    \
628                 : : "r" (&(ctx).sc_regs [0])    \
629                 : "memory"                      \
630         )
631
632 #elif defined(__s390x__)
633
634 #define MONO_ARCH_HAS_MONO_CONTEXT 1
635
636 typedef struct ucontext MonoContext;
637
638 #define MONO_CONTEXT_SET_IP(ctx,ip)                                     \
639         do {                                                            \
640                 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip;       \
641                 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip;        \
642         } while (0); 
643
644 #define MONO_CONTEXT_SET_SP(ctx,bp) MONO_CONTEXT_SET_BP((ctx),(bp))
645 #define MONO_CONTEXT_SET_BP(ctx,bp)                                     \
646         do {                                                            \
647                 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp;       \
648                 (ctx)->uc_stack.ss_sp        = (void*)bp;               \
649         } while (0) 
650
651 #define MONO_CONTEXT_GET_IP(ctx) (gpointer) (ctx)->uc_mcontext.psw.addr
652 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
653 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[11]))
654
655 #define MONO_CONTEXT_GET_CURRENT(ctx)   \
656         __asm__ __volatile__(   \
657                 "stmg   %%r0,%%r15,0(%0)\n"     \
658                 : : "r" (&(ctx).uc_mcontext.gregs[0])   \
659                 : "memory"                      \
660         )
661
662 #else
663
664 #error "Implement mono-context for the current arch"
665
666 #endif
667
668 /*
669  * The naming is misleading, the SIGCTX argument should be the platform's context
670  * structure (ucontext_c on posix, CONTEXT on windows).
671  */
672 void mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx);
673
674 /*
675  * This will not completely initialize SIGCTX since MonoContext contains less
676  * information that the system context. The caller should obtain a SIGCTX from
677  * the system, and use this function to override the parts of it which are
678  * also in MonoContext.
679  */
680 void mono_monoctx_to_sigctx (MonoContext *mctx, void *sigctx);
681
682 #endif /* __MONO_MONO_CONTEXT_H__ */