Move the mono_ia64_context_get_... functions to mono-context.h to fix the ia64 build.
[mono-project/dkf.git] / mono / utils / mono-context.h
blob273a5f3cc8d6b59b245a8b7beffdf2ed7297cddd
1 /*
2 * mono-context.h: plat independent machine state definitions
5 * Copyright (c) 2011 Novell, Inc (http://www.novell.com)
6 */
9 #ifndef __MONO_MONO_CONTEXT_H__
10 #define __MONO_MONO_CONTEXT_H__
12 #include "mono-compiler.h"
13 #include "mono-sigcontext.h"
14 #include "mono-machine.h"
16 #ifdef HAVE_SIGNAL_H
17 #include <signal.h>
18 #endif
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.
28 #if defined(__i386__)
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
36 #if defined(__native_client__)
37 #undef MONO_SIGNAL_USE_SIGACTION
38 #endif
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;
53 #endif
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
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;
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);
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))
107 /*We set EAX to zero since we are clobering it anyway*/
108 #define MONO_CONTEXT_GET_CURRENT(ctx) \
109 __asm__ __volatile__( \
110 "movl $0x0, 0x00(%0)\n" \
111 "mov %%ebx, 0x04(%0)\n" \
112 "mov %%ecx, 0x08(%0)\n" \
113 "mov %%edx, 0x0c(%0)\n" \
114 "mov %%ebp, 0x10(%0)\n" \
115 "mov %%esp, 0x14(%0)\n" \
116 "mov %%esi, 0x18(%0)\n" \
117 "mov %%edi, 0x1c(%0)\n" \
118 "call 1f\n" \
119 "1: pop 0x20(%0)\n" \
121 : "a" (&(ctx)) \
122 : "memory")
125 #elif defined(__x86_64__) /* defined(__i386__) */
128 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
130 #define MONO_SIGNAL_USE_SIGACTION 1
132 #endif
134 typedef struct {
135 mgreg_t rax;
136 mgreg_t rbx;
137 mgreg_t rcx;
138 mgreg_t rdx;
139 mgreg_t rbp;
140 mgreg_t rsp;
141 mgreg_t rsi;
142 mgreg_t rdi;
143 mgreg_t r8;
144 mgreg_t r9;
145 mgreg_t r10;
146 mgreg_t r11;
147 mgreg_t r12;
148 mgreg_t r13;
149 mgreg_t r14;
150 mgreg_t r15;
151 mgreg_t rip;
152 } MonoContext;
154 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->rip = (mgreg_t)(ip); } while (0);
155 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->rbp = (mgreg_t)(bp); } while (0);
156 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->rsp = (mgreg_t)(esp); } while (0);
158 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->rip))
159 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->rbp))
160 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->rsp))
162 #define MONO_CONTEXT_GET_CURRENT(ctx) \
163 __asm__ __volatile__( \
164 "movq $0x0, 0x00(%0)\n" \
165 "movq %%rbx, 0x08(%0)\n" \
166 "movq %%rcx, 0x10(%0)\n" \
167 "movq %%rdx, 0x18(%0)\n" \
168 "movq %%rbp, 0x20(%0)\n" \
169 "movq %%rsp, 0x28(%0)\n" \
170 "movq %%rsi, 0x30(%0)\n" \
171 "movq %%rdi, 0x38(%0)\n" \
172 "movq %%r8, 0x40(%0)\n" \
173 "movq %%r9, 0x48(%0)\n" \
174 "movq %%r10, 0x50(%0)\n" \
175 "movq %%r11, 0x58(%0)\n" \
176 "movq %%r12, 0x60(%0)\n" \
177 "movq %%r13, 0x68(%0)\n" \
178 "movq %%r14, 0x70(%0)\n" \
179 "movq %%r15, 0x78(%0)\n" \
180 "leaq (%%rip), %%rdx\n" \
181 "movq %%rdx, 0x80(%0)\n" \
183 : "a" (&(ctx)) \
184 : "rdx", "memory")
186 #elif defined(__arm__) /* defined(__x86_64__) */
188 typedef struct {
189 gulong eip; // pc
190 gulong esp; // sp
191 gulong regs [16];
192 double fregs [8];
193 } MonoContext;
195 /* we have the stack pointer, not the base pointer in sigcontext */
196 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (int)ip; } while (0);
197 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->regs [ARMREG_FP] = (int)bp; } while (0);
198 #define MONO_CONTEXT_SET_SP(ctx,bp) do { (ctx)->esp = (int)bp; } while (0);
200 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
201 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ARMREG_FP]))
202 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
204 #elif defined(__mono_ppc__) /* defined(__arm__) */
206 /* we define our own structure and we'll copy the data
207 * from sigcontext/ucontext/mach when we need it.
208 * This also makes us save stack space and time when copying
209 * We might also want to add an additional field to propagate
210 * the original context from the signal handler.
212 typedef struct {
213 gulong sc_ir; // pc
214 gulong sc_sp; // r1
215 mgreg_t regs [19]; /*FIXME, this must be changed to 32 for sgen*/
216 double fregs [18];
217 } MonoContext;
219 /* we have the stack pointer, not the base pointer in sigcontext */
220 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_ir = (gulong)ip; } while (0);
221 /* FIXME: should be called SET_SP */
222 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_sp = (gulong)bp; } while (0);
223 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_sp = (gulong)sp; } while (0);
225 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_ir))
226 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->regs [ppc_r31-13]))
227 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_sp))
229 #elif defined(__sparc__) || defined(sparc) /* defined(__mono_ppc__) */
231 typedef struct MonoContext {
232 guint8 *ip;
233 gpointer *sp;
234 gpointer *fp;
235 } MonoContext;
237 #define MONO_CONTEXT_SET_IP(ctx,eip) do { (ctx)->ip = (gpointer)(eip); } while (0);
238 #define MONO_CONTEXT_SET_BP(ctx,ebp) do { (ctx)->fp = (gpointer*)(ebp); } while (0);
239 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->sp = (gpointer*)(esp); } while (0);
241 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->ip))
242 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->fp))
243 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sp))
245 #elif defined(__ia64__) /*defined(__sparc__) || defined(sparc) */
247 #ifndef UNW_LOCAL_ONLY
249 #define UNW_LOCAL_ONLY
250 #include <libunwind.h>
252 #endif
254 typedef struct MonoContext {
255 unw_cursor_t cursor;
256 /* Whenever the ip in 'cursor' points to the ip where the exception happened */
257 /* This is true for the initial context for exceptions thrown from signal handlers */
258 gboolean precise_ip;
259 } MonoContext;
261 /*XXX SET_BP is missing*/
262 #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)
263 #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)
265 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)(mono_ia64_context_get_ip ((ctx))))
266 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)(mono_ia64_context_get_fp ((ctx))))
267 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)(mono_ia64_context_get_sp ((ctx))))
269 static inline unw_word_t
270 mono_ia64_context_get_ip (MonoContext *ctx)
272 unw_word_t ip;
273 int err;
275 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
276 g_assert (err == 0);
278 if (ctx->precise_ip) {
279 return ip;
280 } else {
281 /* Subtrack 1 so ip points into the actual instruction */
282 return ip - 1;
286 static inline unw_word_t
287 mono_ia64_context_get_sp (MonoContext *ctx)
289 unw_word_t sp;
290 int err;
292 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
293 g_assert (err == 0);
295 return sp;
298 static inline unw_word_t
299 mono_ia64_context_get_fp (MonoContext *ctx)
301 unw_cursor_t new_cursor;
302 unw_word_t fp;
303 int err;
306 unw_word_t ip, sp;
308 err = unw_get_reg (&ctx->cursor, UNW_IA64_SP, &sp);
309 g_assert (err == 0);
311 err = unw_get_reg (&ctx->cursor, UNW_IA64_IP, &ip);
312 g_assert (err == 0);
315 /* fp is the SP of the parent frame */
316 new_cursor = ctx->cursor;
318 err = unw_step (&new_cursor);
319 g_assert (err >= 0);
321 err = unw_get_reg (&new_cursor, UNW_IA64_SP, &fp);
322 g_assert (err == 0);
324 return fp;
327 #elif defined(__mips__) && SIZEOF_REGISTER == 4 /* defined(__ia64__) */
329 /* we define our own structure and we'll copy the data
330 * from sigcontext/ucontext/mach when we need it.
331 * This also makes us save stack space and time when copying
332 * We might also want to add an additional field to propagate
333 * the original context from the signal handler.
335 typedef struct {
336 gpointer sc_pc;
337 guint32 sc_regs [32];
338 gfloat sc_fpregs [32];
339 } MonoContext;
341 /* we have the stack pointer, not the base pointer in sigcontext */
342 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->sc_pc = (int)(ip); } while (0);
343 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->sc_regs[mips_fp] = (int)(bp); } while (0);
344 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->sc_regs[mips_sp] = (int)(sp); } while (0);
346 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->sc_pc))
347 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->sc_regs[mips_fp]))
348 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->sc_regs[mips_sp]))
350 #elif defined(__s390x__)
352 typedef struct ucontext MonoContext;
354 #define MONO_CONTEXT_SET_IP(ctx,ip) \
355 do { \
356 (ctx)->uc_mcontext.gregs[14] = (unsigned long)ip; \
357 (ctx)->uc_mcontext.psw.addr = (unsigned long)ip; \
358 } while (0);
360 #define MONO_CONTEXT_SET_SP(ctx,bp) MONO_CONTEXT_SET_BP((ctx),(bp))
361 #define MONO_CONTEXT_SET_BP(ctx,bp) \
362 do { \
363 (ctx)->uc_mcontext.gregs[15] = (unsigned long)bp; \
364 (ctx)->uc_stack.ss_sp = (void*)bp; \
365 } while (0)
367 #define MONO_CONTEXT_GET_IP(ctx) (gpointer) (ctx)->uc_mcontext.psw.addr
368 #define MONO_CONTEXT_GET_BP(ctx) MONO_CONTEXT_GET_SP((ctx))
369 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->uc_mcontext.gregs[15]))
371 #define MONO_CONTEXT_GET_CURRENT(ctx) \
372 __asm__ __volatile__( \
373 "stg %%r0,0x00(%0)\n\t" \
374 "stg %%r1,0x08(%0)\n\t" \
375 "stg %%r2,0x10(%0)\n\t" \
376 "stg %%r3,0x18(%0)\n\t" \
377 "stg %%r4,0x20(%0)\n\t" \
378 "stg %%r5,0x28(%0)\n\t" \
379 "stg %%r6,0x30(%0)\n\t" \
380 "stg %%r7,0x38(%0)\n\t" \
381 "stg %%r8,0x40(%0)\n\t" \
382 "stg %%r9,0x48(%0)\n\t" \
383 "stg %%r10,0x50(%0)\n\t" \
384 "stg %%r11,0x58(%0)\n\t" \
385 "stg %%r12,0x60(%0)\n\t" \
386 "stg %%r13,0x68(%0)\n\t" \
387 "stg %%r14,0x70(%0)\n\t" \
388 "stg %%r15,0x78(%0)\n" \
389 : "=&a" (ptr) \
390 : "0" (cur_thread_regs) \
391 : "memory" \
394 #else
396 #error "Implement mono-context for the current arch"
398 #endif
400 void mono_sigctx_to_monoctx (void *sigctx, MonoContext *mctx) MONO_INTERNAL;
401 void mono_monoctx_to_sigctx (MonoContext *mctx, void *sigctx) MONO_INTERNAL;
403 #endif /* __MONO_MONO_CONTEXT_H__ */