2010-06-21 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions-amd64.c
1 /*
2  * exceptions-amd64.c: exception support for AMD64
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12 #include <signal.h>
13 #include <string.h>
14 #ifdef HAVE_UCONTEXT_H
15 #include <ucontext.h>
16 #endif
17
18 #include <mono/arch/amd64/amd64-codegen.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/metadata/threads.h>
22 #include <mono/metadata/threads-types.h>
23 #include <mono/metadata/debug-helpers.h>
24 #include <mono/metadata/exception.h>
25 #include <mono/metadata/gc-internal.h>
26 #include <mono/metadata/mono-debug.h>
27 #include <mono/utils/mono-mmap.h>
28
29 #include "mini.h"
30 #include "mini-amd64.h"
31 #include "tasklets.h"
32 #include "debug-mini.h"
33
34 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
35
36 #ifdef TARGET_WIN32
37 static MonoW32ExceptionHandler fpe_handler;
38 static MonoW32ExceptionHandler ill_handler;
39 static MonoW32ExceptionHandler segv_handler;
40
41 static LPTOP_LEVEL_EXCEPTION_FILTER old_handler;
42
43 #define W32_SEH_HANDLE_EX(_ex) \
44         if (_ex##_handler) _ex##_handler(0, er, sctx)
45
46 /*
47  * Unhandled Exception Filter
48  * Top-level per-process exception handler.
49  */
50 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep)
51 {
52         EXCEPTION_RECORD* er;
53         CONTEXT* ctx;
54         MonoContext* sctx;
55         LONG res;
56
57         res = EXCEPTION_CONTINUE_EXECUTION;
58
59         er = ep->ExceptionRecord;
60         ctx = ep->ContextRecord;
61         sctx = g_malloc(sizeof(MonoContext));
62
63         /* Copy Win32 context to UNIX style context */
64         sctx->rax = ctx->Rax;
65         sctx->rbx = ctx->Rbx;
66         sctx->rcx = ctx->Rcx;
67         sctx->rdx = ctx->Rdx;
68         sctx->rbp = ctx->Rbp;
69         sctx->rsp = ctx->Rsp;
70         sctx->rsi = ctx->Rsi;
71         sctx->rdi = ctx->Rdi;
72         sctx->rip = ctx->Rip;
73         sctx->r12 = ctx->R12;
74         sctx->r13 = ctx->R13;
75         sctx->r14 = ctx->R14;
76         sctx->r15 = ctx->R15;
77
78         switch (er->ExceptionCode) {
79         case EXCEPTION_ACCESS_VIOLATION:
80                 W32_SEH_HANDLE_EX(segv);
81                 break;
82         case EXCEPTION_ILLEGAL_INSTRUCTION:
83                 W32_SEH_HANDLE_EX(ill);
84                 break;
85         case EXCEPTION_INT_DIVIDE_BY_ZERO:
86         case EXCEPTION_INT_OVERFLOW:
87         case EXCEPTION_FLT_DIVIDE_BY_ZERO:
88         case EXCEPTION_FLT_OVERFLOW:
89         case EXCEPTION_FLT_UNDERFLOW:
90         case EXCEPTION_FLT_INEXACT_RESULT:
91                 W32_SEH_HANDLE_EX(fpe);
92                 break;
93         default:
94                 break;
95         }
96
97         /* Copy context back */
98         /* Nonvolatile */
99         ctx->Rsp = sctx->rsp; 
100         ctx->Rdi = sctx->rdi; 
101         ctx->Rsi = sctx->rsi; 
102         ctx->Rbx = sctx->rbx; 
103         ctx->Rbp = sctx->rbp;
104         ctx->R12 = sctx->r12; 
105         ctx->R13 = sctx->r13; 
106         ctx->R14 = sctx->r14;
107         ctx->R15 = sctx->r15;
108         ctx->Rip = sctx->rip; 
109
110         /* Volatile But should not matter?*/
111         ctx->Rax = sctx->rax; 
112         ctx->Rcx = sctx->rcx; 
113         ctx->Rdx = sctx->rdx;
114
115         g_free (sctx);
116
117         return res;
118 }
119
120 void win32_seh_init()
121 {
122         old_handler = SetUnhandledExceptionFilter(seh_handler);
123 }
124
125 void win32_seh_cleanup()
126 {
127         if (old_handler) SetUnhandledExceptionFilter(old_handler);
128 }
129
130 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler)
131 {
132         switch (type) {
133         case SIGFPE:
134                 fpe_handler = handler;
135                 break;
136         case SIGILL:
137                 ill_handler = handler;
138                 break;
139         case SIGSEGV:
140                 segv_handler = handler;
141                 break;
142         default:
143                 break;
144         }
145 }
146
147 #endif /* TARGET_WIN32 */
148
149 /*
150  * mono_arch_get_restore_context:
151  *
152  * Returns a pointer to a method which restores a previously saved sigcontext.
153  */
154 gpointer
155 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
156 {
157         guint8 *start = NULL;
158         guint8 *code;
159         MonoJumpInfo *ji = NULL;
160         GSList *unwind_ops = NULL;
161
162         /* restore_contect (MonoContext *ctx) */
163
164         start = code = mono_global_codeman_reserve (256);
165
166         amd64_mov_reg_reg (code, AMD64_R11, AMD64_ARG_REG1, 8);
167
168         /* Restore all registers except %rip and %r11 */
169         amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rax), 8);
170         amd64_mov_reg_membase (code, AMD64_RCX, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rcx), 8);
171         amd64_mov_reg_membase (code, AMD64_RDX, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rdx), 8);
172         amd64_mov_reg_membase (code, AMD64_RBX, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rbx), 8);
173         amd64_mov_reg_membase (code, AMD64_RBP, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rbp), 8);
174         amd64_mov_reg_membase (code, AMD64_RSI, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rsi), 8);
175         amd64_mov_reg_membase (code, AMD64_RDI, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rdi), 8);
176         //amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r8), 8);
177         //amd64_mov_reg_membase (code, AMD64_R9, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r9), 8);
178         //amd64_mov_reg_membase (code, AMD64_R10, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r10), 8);
179         amd64_mov_reg_membase (code, AMD64_R12, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r12), 8);
180         amd64_mov_reg_membase (code, AMD64_R13, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r13), 8);
181         amd64_mov_reg_membase (code, AMD64_R14, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r14), 8);
182         amd64_mov_reg_membase (code, AMD64_R15, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, r15), 8);
183
184         if (mono_running_on_valgrind ()) {
185                 /* Prevent 'Address 0x... is just below the stack ptr.' errors */
186                 amd64_mov_reg_membase (code, AMD64_R8, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rsp), 8);
187                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rip), 8);
188                 amd64_mov_reg_reg (code, AMD64_RSP, AMD64_R8, 8);
189         } else {
190                 amd64_mov_reg_membase (code, AMD64_RSP, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rsp), 8);
191                 /* get return address */
192                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11,  G_STRUCT_OFFSET (MonoContext, rip), 8);
193         }
194
195         /* jump to the saved IP */
196         amd64_jump_reg (code, AMD64_R11);
197
198         mono_arch_flush_icache (start, code - start);
199
200         if (info)
201                 *info = mono_tramp_info_create (g_strdup_printf ("restore_context"), start, code - start, ji, unwind_ops);
202
203         return start;
204 }
205
206 /*
207  * mono_arch_get_call_filter:
208  *
209  * Returns a pointer to a method which calls an exception filter. We
210  * also use this function to call finally handlers (we pass NULL as 
211  * @exc object in this case).
212  */
213 gpointer
214 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
215 {
216         guint8 *start;
217         int i;
218         guint8 *code;
219         guint32 pos;
220         MonoJumpInfo *ji = NULL;
221         GSList *unwind_ops = NULL;
222
223         start = code = mono_global_codeman_reserve (128);
224
225         /* call_filter (MonoContext *ctx, unsigned long eip) */
226         code = start;
227
228         /* Alloc new frame */
229         amd64_push_reg (code, AMD64_RBP);
230         amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, 8);
231
232         /* Save callee saved regs */
233         pos = 0;
234         for (i = 0; i < AMD64_NREG; ++i)
235                 if (AMD64_IS_CALLEE_SAVED_REG (i)) {
236                         amd64_push_reg (code, i);
237                         pos += 8;
238                 }
239
240         /* Save EBP */
241         pos += 8;
242         amd64_push_reg (code, AMD64_RBP);
243
244         /* Make stack misaligned, the call will make it aligned again */
245         if (! (pos & 8))
246                 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
247
248         /* set new EBP */
249         amd64_mov_reg_membase (code, AMD64_RBP, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbp), 8);
250         /* load callee saved regs */
251         amd64_mov_reg_membase (code, AMD64_RBX, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, rbx), 8);
252         amd64_mov_reg_membase (code, AMD64_R12, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r12), 8);
253         amd64_mov_reg_membase (code, AMD64_R13, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r13), 8);
254         amd64_mov_reg_membase (code, AMD64_R14, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r14), 8);
255         amd64_mov_reg_membase (code, AMD64_R15, AMD64_ARG_REG1, G_STRUCT_OFFSET (MonoContext, r15), 8);
256 #ifdef TARGET_WIN32
257         amd64_mov_reg_membase (code, AMD64_RDI, AMD64_ARG_REG1,  G_STRUCT_OFFSET (MonoContext, rdi), 8);
258         amd64_mov_reg_membase (code, AMD64_RSI, AMD64_ARG_REG1,  G_STRUCT_OFFSET (MonoContext, rsi), 8);
259 #endif
260
261         /* call the handler */
262         amd64_call_reg (code, AMD64_ARG_REG2);
263
264         if (! (pos & 8))
265                 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
266
267         /* restore RBP */
268         amd64_pop_reg (code, AMD64_RBP);
269
270         /* Restore callee saved regs */
271         for (i = AMD64_NREG; i >= 0; --i)
272                 if (AMD64_IS_CALLEE_SAVED_REG (i))
273                         amd64_pop_reg (code, i);
274
275         amd64_leave (code);
276         amd64_ret (code);
277
278         g_assert ((code - start) < 128);
279
280         mono_arch_flush_icache (start, code - start);
281
282         if (info)
283                 *info = mono_tramp_info_create (g_strdup_printf ("call_filter"), start, code - start, ji, unwind_ops);
284
285         return start;
286 }
287
288 /* 
289  * The first few arguments are dummy, to force the other arguments to be passed on
290  * the stack, this avoids overwriting the argument registers in the throw trampoline.
291  */
292 void
293 mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
294                                                         guint64 dummy5, guint64 dummy6,
295                                                         mgreg_t *regs, mgreg_t rip,
296                                                         MonoObject *exc, gboolean rethrow)
297 {
298         static void (*restore_context) (MonoContext *);
299         MonoContext ctx;
300
301         if (!restore_context)
302                 restore_context = mono_get_restore_context ();
303
304         ctx.rsp = regs [AMD64_RSP];
305         ctx.rip = rip;
306         ctx.rbx = regs [AMD64_RBX];
307         ctx.rbp = regs [AMD64_RBP];
308         ctx.r12 = regs [AMD64_R12];
309         ctx.r13 = regs [AMD64_R13];
310         ctx.r14 = regs [AMD64_R14];
311         ctx.r15 = regs [AMD64_R15];
312         ctx.rdi = regs [AMD64_RDI];
313         ctx.rsi = regs [AMD64_RSI];
314         ctx.rax = regs [AMD64_RAX];
315         ctx.rcx = regs [AMD64_RCX];
316         ctx.rdx = regs [AMD64_RDX];
317
318         if (mono_object_isinst (exc, mono_defaults.exception_class)) {
319                 MonoException *mono_ex = (MonoException*)exc;
320                 if (!rethrow)
321                         mono_ex->stack_trace = NULL;
322         }
323
324         if (mono_debug_using_mono_debugger ()) {
325                 guint8 buf [16], *code;
326
327                 mono_breakpoint_clean_code (NULL, (gpointer)rip, 8, buf, sizeof (buf));
328                 code = buf + 8;
329
330                 if (buf [3] == 0xe8) {
331                         MonoContext ctx_cp = ctx;
332                         ctx_cp.rip = rip - 5;
333
334                         if (mono_debugger_handle_exception (&ctx_cp, exc)) {
335                                 restore_context (&ctx_cp);
336                                 g_assert_not_reached ();
337                         }
338                 }
339         }
340
341         /* adjust eip so that it point into the call instruction */
342         ctx.rip -= 1;
343
344         mono_handle_exception (&ctx, exc, (gpointer)rip, FALSE);
345         restore_context (&ctx);
346
347         g_assert_not_reached ();
348 }
349
350 void
351 mono_amd64_throw_corlib_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
352                                                                    guint64 dummy5, guint64 dummy6,
353                                                                    mgreg_t *regs, mgreg_t rip,
354                                                                    guint32 ex_token_index, gint64 pc_offset)
355 {
356         guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
357         MonoException *ex;
358
359         ex = mono_exception_from_token (mono_defaults.exception_class->image, ex_token);
360
361         rip -= pc_offset;
362
363         /* Negate the ip adjustment done in mono_amd64_throw_exception () */
364         rip += 1;
365
366         mono_amd64_throw_exception (dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, regs, rip, (MonoObject*)ex, FALSE);
367 }
368
369 /*
370  * get_throw_trampoline:
371  *
372  *  Generate a call to mono_amd64_throw_exception/
373  * mono_amd64_throw_corlib_exception.
374  */
375 static gpointer
376 get_throw_trampoline (MonoTrampInfo **info, gboolean rethrow, gboolean corlib, gboolean llvm_abs, const char *tramp_name, gboolean aot)
377 {
378         guint8* start;
379         guint8 *code;
380         MonoJumpInfo *ji = NULL;
381         GSList *unwind_ops = NULL;
382         int i, buf_size, stack_size, arg_offsets [16], regs_offset;
383
384         buf_size = 256;
385         start = code = mono_global_codeman_reserve (buf_size);
386
387         stack_size = 192;
388
389         code = start;
390
391         unwind_ops = mono_arch_get_cie_program ();
392
393         /* Alloc frame */
394         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_size);
395         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, stack_size + 8);
396
397         /*
398          * To hide linux/windows calling convention differences, we pass all arguments on
399          * the stack by passing 6 dummy values in registers.
400          */
401
402         arg_offsets [0] = 0;
403         arg_offsets [1] = sizeof (gpointer);
404         arg_offsets [2] = sizeof (gpointer) * 2;
405         arg_offsets [3] = sizeof (gpointer) * 3;
406         regs_offset = sizeof (gpointer) * 4;
407
408         /* Save registers */
409         for (i = 0; i < AMD64_NREG; ++i)
410                 if (i != AMD64_RSP)
411                         amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (i * sizeof (gpointer)), i, 8);
412         /* Save RSP */
413         amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, stack_size + sizeof (gpointer));
414         amd64_mov_membase_reg (code, AMD64_RSP, regs_offset + (AMD64_RSP * sizeof (gpointer)), X86_EAX, 8);
415         /* Set arg1 == regs */
416         amd64_lea_membase (code, AMD64_RAX, AMD64_RSP, regs_offset);
417         amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [0], AMD64_RAX, 8);
418         /* Set arg2 == eip */
419         if (llvm_abs)
420                 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
421         else
422                 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, stack_size, 8);
423         amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [1], AMD64_RAX, 8);
424         /* Set arg3 == exc/ex_token_index */
425         amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [2], AMD64_ARG_REG1, 8);
426         /* Set arg4 == rethrow/pc offset */
427         if (corlib) {
428                 amd64_mov_membase_reg (code, AMD64_RSP, arg_offsets [3], AMD64_ARG_REG2, 8);
429                 if (llvm_abs)
430                         /* 
431                          * The caller is LLVM code which passes the absolute address not a pc offset,
432                          * so compensate by passing 0 as 'rip' and passing the negated abs address as
433                          * the pc offset.
434                          */
435                         amd64_neg_membase (code, AMD64_RSP, arg_offsets [3]);
436         } else {
437                 amd64_mov_membase_imm (code, AMD64_RSP, arg_offsets [3], rethrow, 8);
438         }
439
440         if (aot) {
441                 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, corlib ? (llvm_abs ? "mono_amd64_throw_corlib_exception_abs" : "mono_amd64_throw_corlib_exception") : "mono_amd64_throw_exception");
442                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
443         } else {
444                 amd64_mov_reg_imm (code, AMD64_R11, corlib ? (gpointer)mono_amd64_throw_corlib_exception : (gpointer)mono_amd64_throw_exception);
445         }
446         amd64_call_reg (code, AMD64_R11);
447         amd64_breakpoint (code);
448
449         mono_arch_flush_icache (start, code - start);
450
451         g_assert ((code - start) < buf_size);
452
453         mono_save_trampoline_xdebug_info (tramp_name, start, code - start, unwind_ops);
454
455         if (info)
456                 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
457
458         return start;
459 }
460
461 /**
462  * mono_arch_get_throw_exception:
463  *
464  * Returns a function pointer which can be used to raise 
465  * exceptions. The returned function has the following 
466  * signature: void (*func) (MonoException *exc); 
467  *
468  */
469 gpointer
470 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
471 {
472         return get_throw_trampoline (info, FALSE, FALSE, FALSE, "throw_exception", aot);
473 }
474
475 gpointer 
476 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
477 {
478         return get_throw_trampoline (info, TRUE, FALSE, FALSE, "rethrow_exception", aot);
479 }
480
481 /**
482  * mono_arch_get_throw_corlib_exception:
483  *
484  * Returns a function pointer which can be used to raise 
485  * corlib exceptions. The returned function has the following 
486  * signature: void (*func) (guint32 ex_token, guint32 offset); 
487  * Here, offset is the offset which needs to be substracted from the caller IP 
488  * to get the IP of the throw. Passing the offset has the advantage that it 
489  * needs no relocations in the caller.
490  */
491 gpointer 
492 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
493 {
494         return get_throw_trampoline (info, FALSE, TRUE, FALSE, "throw_corlib_exception", aot);
495 }
496
497 /*
498  * mono_arch_find_jit_info_ext:
499  *
500  * This function is used to gather information from @ctx, and store it in @frame_info.
501  * It unwinds one stack frame, and stores the resulting context into @new_ctx. @lmf
502  * is modified if needed.
503  * Returns TRUE on success, FALSE otherwise.
504  * This function is a version of mono_arch_find_jit_info () where all the results are
505  * returned in a StackFrameInfo structure.
506  */
507 gboolean
508 mono_arch_find_jit_info_ext (MonoDomain *domain, MonoJitTlsData *jit_tls, 
509                                                          MonoJitInfo *ji, MonoContext *ctx, 
510                                                          MonoContext *new_ctx, MonoLMF **lmf, 
511                                                          StackFrameInfo *frame)
512 {
513         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
514
515         memset (frame, 0, sizeof (StackFrameInfo));
516         frame->ji = ji;
517         frame->managed = FALSE;
518
519         *new_ctx = *ctx;
520
521         if (ji != NULL) {
522                 gssize regs [MONO_MAX_IREGS + 1];
523                 guint8 *cfa;
524                 guint32 unwind_info_len;
525                 guint8 *unwind_info;
526
527                 frame->type = FRAME_TYPE_MANAGED;
528
529                 if (!ji->method->wrapper_type || ji->method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
530                         frame->managed = TRUE;
531
532                 if (ji->from_aot)
533                         unwind_info = mono_aot_get_unwind_info (ji, &unwind_info_len);
534                 else
535                         unwind_info = mono_get_cached_unwind_info (ji->used_regs, &unwind_info_len);
536  
537                 regs [AMD64_RAX] = new_ctx->rax;
538                 regs [AMD64_RBX] = new_ctx->rbx;
539                 regs [AMD64_RCX] = new_ctx->rcx;
540                 regs [AMD64_RDX] = new_ctx->rdx;
541                 regs [AMD64_RBP] = new_ctx->rbp;
542                 regs [AMD64_RSP] = new_ctx->rsp;
543                 regs [AMD64_RSI] = new_ctx->rsi;
544                 regs [AMD64_RDI] = new_ctx->rdi;
545                 regs [AMD64_RIP] = new_ctx->rip;
546                 regs [AMD64_R12] = new_ctx->r12;
547                 regs [AMD64_R13] = new_ctx->r13;
548                 regs [AMD64_R14] = new_ctx->r14;
549                 regs [AMD64_R15] = new_ctx->r15;
550
551                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
552                                                    (guint8*)ji->code_start + ji->code_size,
553                                                    ip, regs, MONO_MAX_IREGS + 1, &cfa);
554
555                 new_ctx->rax = regs [AMD64_RAX];
556                 new_ctx->rbx = regs [AMD64_RBX];
557                 new_ctx->rcx = regs [AMD64_RCX];
558                 new_ctx->rdx = regs [AMD64_RDX];
559                 new_ctx->rbp = regs [AMD64_RBP];
560                 new_ctx->rsp = regs [AMD64_RSP];
561                 new_ctx->rsi = regs [AMD64_RSI];
562                 new_ctx->rdi = regs [AMD64_RDI];
563                 new_ctx->rip = regs [AMD64_RIP];
564                 new_ctx->r12 = regs [AMD64_R12];
565                 new_ctx->r13 = regs [AMD64_R13];
566                 new_ctx->r14 = regs [AMD64_R14];
567                 new_ctx->r15 = regs [AMD64_R15];
568  
569                 /* The CFA becomes the new SP value */
570                 new_ctx->rsp = (gssize)cfa;
571
572                 /* Adjust IP */
573                 new_ctx->rip --;
574
575                 if (*lmf && ((*lmf) != jit_tls->first_lmf) && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->rsp)) {
576                         /* remove any unused lmf */
577                         *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
578                 }
579
580 #ifndef MONO_AMD64_NO_PUSHES
581                 /* Pop arguments off the stack */
582                 {
583                         MonoJitArgumentInfo *arg_info = g_newa (MonoJitArgumentInfo, mono_method_signature (ji->method)->param_count + 1);
584
585                         guint32 stack_to_pop = mono_arch_get_argument_info (mono_method_signature (ji->method), mono_method_signature (ji->method)->param_count, arg_info);
586                         new_ctx->rsp += stack_to_pop;
587                 }
588 #endif
589
590                 return TRUE;
591         } else if (*lmf) {
592                 guint64 rip;
593
594                 if (((guint64)(*lmf)->previous_lmf) & 2) {
595                         /* 
596                          * This LMF entry is created by the soft debug code to mark transitions to
597                          * managed code done during invokes.
598                          */
599                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
600
601                         g_assert (ext->debugger_invoke);
602
603                         memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
604
605                         *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
606
607                         frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
608
609                         return TRUE;
610                 }
611
612                 if (((guint64)(*lmf)->previous_lmf) & 1) {
613                         /* This LMF has the rip field set */
614                         rip = (*lmf)->rip;
615                 } else if ((*lmf)->rsp == 0) {
616                         /* Top LMF entry */
617                         return FALSE;
618                 } else {
619                         /* 
620                          * The rsp field is set just before the call which transitioned to native 
621                          * code. Obtain the rip from the stack.
622                          */
623                         rip = *(guint64*)((*lmf)->rsp - sizeof (gpointer));
624                 }
625
626                 ji = mini_jit_info_table_find (domain, (gpointer)rip, NULL);
627                 if (!ji) {
628                         // FIXME: This can happen with multiple appdomains (bug #444383)
629                         return FALSE;
630                 }
631
632                 /* Adjust IP */
633                 rip --;
634
635                 frame->ji = ji;
636                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
637
638                 new_ctx->rip = rip;
639                 new_ctx->rbp = (*lmf)->rbp;
640                 new_ctx->rsp = (*lmf)->rsp;
641
642                 new_ctx->rbx = (*lmf)->rbx;
643                 new_ctx->r12 = (*lmf)->r12;
644                 new_ctx->r13 = (*lmf)->r13;
645                 new_ctx->r14 = (*lmf)->r14;
646                 new_ctx->r15 = (*lmf)->r15;
647 #ifdef TARGET_WIN32
648                 new_ctx->rdi = (*lmf)->rdi;
649                 new_ctx->rsi = (*lmf)->rsi;
650 #endif
651
652                 *lmf = (gpointer)(((guint64)(*lmf)->previous_lmf) & ~3);
653
654                 return TRUE;
655         }
656
657         return FALSE;
658 }
659
660 /*
661  * handle_exception:
662  *
663  *   Called by resuming from a signal handler.
664  */
665 static void
666 handle_signal_exception (gpointer obj, gboolean test_only)
667 {
668         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
669         MonoContext ctx;
670         static void (*restore_context) (MonoContext *);
671
672         if (!restore_context)
673                 restore_context = mono_get_restore_context ();
674
675         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
676
677         if (mono_debugger_handle_exception (&ctx, (MonoObject *)obj))
678                 return;
679
680         mono_handle_exception (&ctx, obj, MONO_CONTEXT_GET_IP (&ctx), test_only);
681
682         restore_context (&ctx);
683 }
684
685 /**
686  * mono_arch_handle_exception:
687  *
688  * @ctx: saved processor state
689  * @obj: the exception object
690  */
691 gboolean
692 mono_arch_handle_exception (void *sigctx, gpointer obj, gboolean test_only)
693 {
694 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_GREGS)
695         /*
696          * Handling the exception in the signal handler is problematic, since the original
697          * signal is disabled, and we could run arbitrary code though the debugger. So
698          * resume into the normal stack and do most work there if possible.
699          */
700         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
701         guint64 sp = UCONTEXT_REG_RSP (sigctx);
702
703         /* Pass the ctx parameter in TLS */
704         mono_arch_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
705         /* The others in registers */
706         UCONTEXT_REG_RDI (sigctx) = (guint64)obj;
707         UCONTEXT_REG_RSI (sigctx) = test_only;
708
709         /* Allocate a stack frame below the red zone */
710         sp -= 128;
711         /* The stack should be unaligned */
712         if (sp % 8 == 0)
713                 sp -= 8;
714         UCONTEXT_REG_RSP (sigctx) = sp;
715
716         UCONTEXT_REG_RIP (sigctx) = (guint64)handle_signal_exception;
717
718         return TRUE;
719 #else
720         MonoContext mctx;
721
722         mono_arch_sigctx_to_monoctx (sigctx, &mctx);
723
724         if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj))
725                 return TRUE;
726
727         mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), test_only);
728
729         mono_arch_monoctx_to_sigctx (&mctx, sigctx);
730
731         return TRUE;
732 #endif
733 }
734
735 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_GREGS)
736 static inline guint64*
737 gregs_from_ucontext (ucontext_t *ctx)
738 {
739         return (guint64 *) UCONTEXT_GREGS (ctx);
740 }
741 #endif
742 void
743 mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
744 {
745 #if defined(MONO_ARCH_USE_SIGACTION)
746         ucontext_t *ctx = (ucontext_t*)sigctx;
747
748         mctx->rax = UCONTEXT_REG_RAX (ctx);
749         mctx->rbx = UCONTEXT_REG_RBX (ctx);
750         mctx->rcx = UCONTEXT_REG_RCX (ctx);
751         mctx->rdx = UCONTEXT_REG_RDX (ctx);
752         mctx->rbp = UCONTEXT_REG_RBP (ctx);
753         mctx->rsp = UCONTEXT_REG_RSP (ctx);
754         mctx->rsi = UCONTEXT_REG_RSI (ctx);
755         mctx->rdi = UCONTEXT_REG_RDI (ctx);
756         mctx->rip = UCONTEXT_REG_RIP (ctx);
757         mctx->r12 = UCONTEXT_REG_R12 (ctx);
758         mctx->r13 = UCONTEXT_REG_R13 (ctx);
759         mctx->r14 = UCONTEXT_REG_R14 (ctx);
760         mctx->r15 = UCONTEXT_REG_R15 (ctx);
761 #else
762         MonoContext *ctx = (MonoContext *)sigctx;
763
764         mctx->rax = ctx->rax;
765         mctx->rbx = ctx->rbx;
766         mctx->rcx = ctx->rcx;
767         mctx->rdx = ctx->rdx;
768         mctx->rbp = ctx->rbp;
769         mctx->rsp = ctx->rsp;
770         mctx->rsi = ctx->rsi;
771         mctx->rdi = ctx->rdi;
772         mctx->rip = ctx->rip;
773         mctx->r12 = ctx->r12;
774         mctx->r13 = ctx->r13;
775         mctx->r14 = ctx->r14;
776         mctx->r15 = ctx->r15;
777 #endif
778 }
779
780 void
781 mono_arch_monoctx_to_sigctx (MonoContext *mctx, void *sigctx)
782 {
783 #if defined(MONO_ARCH_USE_SIGACTION)
784         ucontext_t *ctx = (ucontext_t*)sigctx;
785
786         UCONTEXT_REG_RAX (ctx) = mctx->rax;
787         UCONTEXT_REG_RBX (ctx) = mctx->rbx;
788         UCONTEXT_REG_RCX (ctx) = mctx->rcx;
789         UCONTEXT_REG_RDX (ctx) = mctx->rdx;
790         UCONTEXT_REG_RBP (ctx) = mctx->rbp;
791         UCONTEXT_REG_RSP (ctx) = mctx->rsp;
792         UCONTEXT_REG_RSI (ctx) = mctx->rsi;
793         UCONTEXT_REG_RDI (ctx) = mctx->rdi;
794         UCONTEXT_REG_RIP (ctx) = mctx->rip;
795         UCONTEXT_REG_R12 (ctx) = mctx->r12;
796         UCONTEXT_REG_R13 (ctx) = mctx->r13;
797         UCONTEXT_REG_R14 (ctx) = mctx->r14;
798         UCONTEXT_REG_R15 (ctx) = mctx->r15;
799 #else
800         MonoContext *ctx = (MonoContext *)sigctx;
801
802         ctx->rax = mctx->rax;
803         ctx->rbx = mctx->rbx;
804         ctx->rcx = mctx->rcx;
805         ctx->rdx = mctx->rdx;
806         ctx->rbp = mctx->rbp;
807         ctx->rsp = mctx->rsp;
808         ctx->rsi = mctx->rsi;
809         ctx->rdi = mctx->rdi;
810         ctx->rip = mctx->rip;
811         ctx->r12 = mctx->r12;
812         ctx->r13 = mctx->r13;
813         ctx->r14 = mctx->r14;
814         ctx->r15 = mctx->r15;
815 #endif
816 }
817
818 gpointer
819 mono_arch_ip_from_context (void *sigctx)
820 {
821 #if defined(MONO_ARCH_USE_SIGACTION)
822         ucontext_t *ctx = (ucontext_t*)sigctx;
823
824         return (gpointer)UCONTEXT_REG_RIP (ctx);
825 #else
826         MonoContext *ctx = sigctx;
827         return (gpointer)ctx->rip;
828 #endif  
829 }
830
831 static void
832 restore_soft_guard_pages (void)
833 {
834         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
835         if (jit_tls->stack_ovf_guard_base)
836                 mono_mprotect (jit_tls->stack_ovf_guard_base, jit_tls->stack_ovf_guard_size, MONO_MMAP_NONE);
837 }
838
839 /* 
840  * this function modifies mctx so that when it is restored, it
841  * won't execcute starting at mctx.eip, but in a function that
842  * will restore the protection on the soft-guard pages and return back to
843  * continue at mctx.eip.
844  */
845 static void
846 prepare_for_guard_pages (MonoContext *mctx)
847 {
848         gpointer *sp;
849         sp = (gpointer)(mctx->rsp);
850         sp -= 1;
851         /* the return addr */
852         sp [0] = (gpointer)(mctx->rip);
853         mctx->rip = (guint64)restore_soft_guard_pages;
854         mctx->rsp = (guint64)sp;
855 }
856
857 static void
858 altstack_handle_and_restore (void *sigctx, gpointer obj, gboolean stack_ovf)
859 {
860         void (*restore_context) (MonoContext *);
861         MonoContext mctx;
862
863         restore_context = mono_get_restore_context ();
864         mono_arch_sigctx_to_monoctx (sigctx, &mctx);
865
866         if (mono_debugger_handle_exception (&mctx, (MonoObject *)obj)) {
867                 if (stack_ovf)
868                         prepare_for_guard_pages (&mctx);
869                 restore_context (&mctx);
870         }
871
872         mono_handle_exception (&mctx, obj, MONO_CONTEXT_GET_IP (&mctx), FALSE);
873         if (stack_ovf)
874                 prepare_for_guard_pages (&mctx);
875         restore_context (&mctx);
876 }
877
878 void
879 mono_arch_handle_altstack_exception (void *sigctx, gpointer fault_addr, gboolean stack_ovf)
880 {
881 #if defined(MONO_ARCH_USE_SIGACTION) && defined(UCONTEXT_GREGS)
882         MonoException *exc = NULL;
883         ucontext_t *ctx = (ucontext_t*)sigctx;
884         guint64 *gregs = gregs_from_ucontext (ctx);
885         MonoJitInfo *ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)gregs [REG_RIP], NULL);
886         gpointer *sp;
887         int frame_size;
888
889         if (stack_ovf)
890                 exc = mono_domain_get ()->stack_overflow_ex;
891         if (!ji)
892                 mono_handle_native_sigsegv (SIGSEGV, sigctx);
893
894         /* setup a call frame on the real stack so that control is returned there
895          * and exception handling can continue.
896          * The frame looks like:
897          *   ucontext struct
898          *   ...
899          *   return ip
900          * 128 is the size of the red zone
901          */
902         frame_size = sizeof (ucontext_t) + sizeof (gpointer) * 4 + 128;
903         frame_size += 15;
904         frame_size &= ~15;
905         sp = (gpointer)(gregs [REG_RSP] & ~15);
906         sp = (gpointer)((char*)sp - frame_size);
907         /* the arguments must be aligned */
908         sp [-1] = (gpointer)gregs [REG_RIP];
909         /* may need to adjust pointers in the new struct copy, depending on the OS */
910         memcpy (sp + 4, ctx, sizeof (ucontext_t));
911         /* at the return form the signal handler execution starts in altstack_handle_and_restore() */
912         gregs [REG_RIP] = (unsigned long)altstack_handle_and_restore;
913         gregs [REG_RSP] = (unsigned long)(sp - 1);
914         gregs [REG_RDI] = (unsigned long)(sp + 4);
915         gregs [REG_RSI] = (guint64)exc;
916         gregs [REG_RDX] = stack_ovf;
917 #endif
918 }
919
920 guint64
921 mono_amd64_get_original_ip (void)
922 {
923         MonoLMF *lmf = mono_get_lmf ();
924
925         g_assert (lmf);
926
927         /* Reset the change to previous_lmf */
928         lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf & ~1);
929
930         return lmf->rip;
931 }
932
933 gpointer
934 mono_arch_get_throw_pending_exception (MonoTrampInfo **info, gboolean aot)
935 {
936         guint8 *code, *start;
937         guint8 *br[1];
938         gpointer throw_trampoline;
939         MonoJumpInfo *ji = NULL;
940         GSList *unwind_ops = NULL;
941
942         start = code = mono_global_codeman_reserve (128);
943
944         /* We are in the frame of a managed method after a call */
945         /* 
946          * We would like to throw the pending exception in such a way that it looks to
947          * be thrown from the managed method.
948          */
949
950         /* Save registers which might contain the return value of the call */
951         amd64_push_reg (code, AMD64_RAX);
952         amd64_push_reg (code, AMD64_RDX);
953
954         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
955         amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
956
957         /* Align stack */
958         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
959
960         /* Obtain the pending exception */
961         if (aot) {
962                 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_thread_get_and_clear_pending_exception");
963                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
964         } else {
965                 amd64_mov_reg_imm (code, AMD64_R11, mono_thread_get_and_clear_pending_exception);
966         }
967         amd64_call_reg (code, AMD64_R11);
968
969         /* Check if it is NULL, and branch */
970         amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
971         br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
972
973         /* exc != NULL branch */
974
975         /* Save the exc on the stack */
976         amd64_push_reg (code, AMD64_RAX);
977         /* Align stack */
978         amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
979
980         /* Obtain the original ip and clear the flag in previous_lmf */
981         if (aot) {
982                 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
983                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
984         } else {
985                 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
986         }
987         amd64_call_reg (code, AMD64_R11);       
988
989         /* Load exc */
990         amd64_mov_reg_membase (code, AMD64_R11, AMD64_RSP, 8, 8);
991
992         /* Pop saved stuff from the stack */
993         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 6 * 8);
994
995         /* Setup arguments for the throw trampoline */
996         /* Exception */
997         amd64_mov_reg_reg (code, AMD64_ARG_REG1, AMD64_R11, 8);
998         /* The trampoline expects the caller ip to be pushed on the stack */
999         amd64_push_reg (code, AMD64_RAX);
1000
1001         /* Call the throw trampoline */
1002         if (aot) {
1003                 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_throw_exception");
1004                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1005         } else {
1006                 throw_trampoline = mono_get_throw_exception ();
1007                 amd64_mov_reg_imm (code, AMD64_R11, throw_trampoline);
1008         }
1009         /* We use a jump instead of a call so we can push the original ip on the stack */
1010         amd64_jump_reg (code, AMD64_R11);
1011
1012         /* ex == NULL branch */
1013         mono_amd64_patch (br [0], code);
1014
1015         /* Obtain the original ip and clear the flag in previous_lmf */
1016         if (aot) {
1017                 ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_amd64_get_original_ip");
1018                 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
1019         } else {
1020                 amd64_mov_reg_imm (code, AMD64_R11, mono_amd64_get_original_ip);
1021         }
1022         amd64_call_reg (code, AMD64_R11);       
1023         amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
1024
1025         /* Restore registers */
1026         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1027         amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
1028         amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
1029         amd64_pop_reg (code, AMD64_RDX);
1030         amd64_pop_reg (code, AMD64_RAX);
1031
1032         /* Return to original code */
1033         amd64_jump_reg (code, AMD64_R11);
1034
1035         g_assert ((code - start) < 128);
1036
1037         if (info)
1038                 *info = mono_tramp_info_create (g_strdup_printf ("throw_pending_exception"), start, code - start, ji, unwind_ops);
1039
1040         return start;
1041 }
1042
1043 static gpointer throw_pending_exception;
1044
1045 /*
1046  * Called when a thread receives an async exception while executing unmanaged code.
1047  * Instead of checking for this exception in the managed-to-native wrapper, we hijack 
1048  * the return address on the stack to point to a helper routine which throws the
1049  * exception.
1050  */
1051 void
1052 mono_arch_notify_pending_exc (void)
1053 {
1054         MonoLMF *lmf = mono_get_lmf ();
1055
1056         if (!lmf)
1057                 /* Not yet started */
1058                 return;
1059
1060         if (lmf->rsp == 0)
1061                 /* Initial LMF */
1062                 return;
1063
1064         if ((guint64)lmf->previous_lmf & 1)
1065                 /* Already hijacked or trampoline LMF entry */
1066                 return;
1067
1068         /* lmf->rsp is set just before making the call which transitions to unmanaged code */
1069         lmf->rip = *(guint64*)(lmf->rsp - 8);
1070         /* Signal that lmf->rip is set */
1071         lmf->previous_lmf = (gpointer)((guint64)lmf->previous_lmf | 1);
1072
1073         *(gpointer*)(lmf->rsp - 8) = throw_pending_exception;
1074 }
1075
1076 void
1077 mono_arch_exceptions_init (void)
1078 {
1079         guint8 *tramp;
1080
1081         if (mono_aot_only) {
1082                 throw_pending_exception = mono_aot_get_trampoline ("throw_pending_exception");
1083         } else {
1084                 /* Call this to avoid initialization races */
1085                 throw_pending_exception = mono_arch_get_throw_pending_exception (NULL, FALSE);
1086
1087                 /* LLVM needs different throw trampolines */
1088                 tramp = get_throw_trampoline (NULL, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception", FALSE);
1089                 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception", NULL, TRUE);
1090
1091                 tramp = get_throw_trampoline (NULL, FALSE, TRUE, TRUE, "llvm_throw_corlib_exception_abs", FALSE);
1092                 mono_register_jit_icall (tramp, "mono_arch_llvm_throw_corlib_exception_abs", NULL, TRUE);
1093         }
1094 }
1095
1096 #ifdef TARGET_WIN32
1097
1098 /*
1099  * The mono_arch_unwindinfo* methods are used to build and add
1100  * function table info for each emitted method from mono.  On Winx64
1101  * the seh handler will not be called if the mono methods are not
1102  * added to the function table.  
1103  *
1104  * We should not need to add non-volatile register info to the 
1105  * table since mono stores that info elsewhere. (Except for the register 
1106  * used for the fp.)
1107  */
1108
1109 #define MONO_MAX_UNWIND_CODES 22
1110
1111 typedef union _UNWIND_CODE {
1112     struct {
1113         guchar CodeOffset;
1114         guchar UnwindOp : 4;
1115         guchar OpInfo   : 4;
1116     };
1117     gushort FrameOffset;
1118 } UNWIND_CODE, *PUNWIND_CODE;
1119
1120 typedef struct _UNWIND_INFO {
1121         guchar Version       : 3;
1122         guchar Flags         : 5;
1123         guchar SizeOfProlog;
1124         guchar CountOfCodes;
1125         guchar FrameRegister : 4;
1126         guchar FrameOffset   : 4;
1127         /* custom size for mono allowing for mono allowing for*/
1128         /*UWOP_PUSH_NONVOL ebp offset = 21*/
1129         /*UWOP_ALLOC_LARGE : requires 2 or 3 offset = 20*/
1130         /*UWOP_SET_FPREG : requires 2 offset = 17*/
1131         /*UWOP_PUSH_NONVOL offset = 15-0*/
1132         UNWIND_CODE UnwindCode[MONO_MAX_UNWIND_CODES]; 
1133
1134 /*      UNWIND_CODE MoreUnwindCode[((CountOfCodes + 1) & ~1) - 1];
1135  *      union {
1136  *          OPTIONAL ULONG ExceptionHandler;
1137  *          OPTIONAL ULONG FunctionEntry;
1138  *      };
1139  *      OPTIONAL ULONG ExceptionData[]; */
1140 } UNWIND_INFO, *PUNWIND_INFO;
1141
1142 typedef struct
1143 {
1144         RUNTIME_FUNCTION runtimeFunction;
1145         UNWIND_INFO unwindInfo;
1146 } MonoUnwindInfo, *PMonoUnwindInfo;
1147
1148 static void
1149 mono_arch_unwindinfo_create (gpointer* monoui)
1150 {
1151         PMonoUnwindInfo newunwindinfo;
1152         *monoui = newunwindinfo = g_new0 (MonoUnwindInfo, 1);
1153         newunwindinfo->unwindInfo.Version = 1;
1154 }
1155
1156 void
1157 mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1158 {
1159         PMonoUnwindInfo unwindinfo;
1160         PUNWIND_CODE unwindcode;
1161         guchar codeindex;
1162         if (!*monoui)
1163                 mono_arch_unwindinfo_create (monoui);
1164         
1165         unwindinfo = (MonoUnwindInfo*)*monoui;
1166
1167         if (unwindinfo->unwindInfo.CountOfCodes >= MONO_MAX_UNWIND_CODES)
1168                 g_error ("Larger allocation needed for the unwind information.");
1169
1170         codeindex = MONO_MAX_UNWIND_CODES - (++unwindinfo->unwindInfo.CountOfCodes);
1171         unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1172         unwindcode->UnwindOp = 0; /*UWOP_PUSH_NONVOL*/
1173         unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1174         unwindcode->OpInfo = reg;
1175
1176         if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1177                 g_error ("Adding unwind info in wrong order.");
1178         
1179         unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1180 }
1181
1182 void
1183 mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg )
1184 {
1185         PMonoUnwindInfo unwindinfo;
1186         PUNWIND_CODE unwindcode;
1187         guchar codeindex;
1188         if (!*monoui)
1189                 mono_arch_unwindinfo_create (monoui);
1190         
1191         unwindinfo = (MonoUnwindInfo*)*monoui;
1192
1193         if (unwindinfo->unwindInfo.CountOfCodes + 1 >= MONO_MAX_UNWIND_CODES)
1194                 g_error ("Larger allocation needed for the unwind information.");
1195
1196         codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += 2);
1197         unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1198         unwindcode->FrameOffset = 0; /*Assuming no frame pointer offset for mono*/
1199         unwindcode++;
1200         unwindcode->UnwindOp = 3; /*UWOP_SET_FPREG*/
1201         unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1202         unwindcode->OpInfo = reg;
1203         
1204         unwindinfo->unwindInfo.FrameRegister = reg;
1205
1206         if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1207                 g_error ("Adding unwind info in wrong order.");
1208         
1209         unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1210 }
1211
1212 void
1213 mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size )
1214 {
1215         PMonoUnwindInfo unwindinfo;
1216         PUNWIND_CODE unwindcode;
1217         guchar codeindex;
1218         guchar codesneeded;
1219         if (!*monoui)
1220                 mono_arch_unwindinfo_create (monoui);
1221         
1222         unwindinfo = (MonoUnwindInfo*)*monoui;
1223
1224         if (size < 0x8)
1225                 g_error ("Stack allocation must be equal to or greater than 0x8.");
1226         
1227         if (size <= 0x80)
1228                 codesneeded = 1;
1229         else if (size <= 0x7FFF8)
1230                 codesneeded = 2;
1231         else
1232                 codesneeded = 3;
1233         
1234         if (unwindinfo->unwindInfo.CountOfCodes + codesneeded > MONO_MAX_UNWIND_CODES)
1235                 g_error ("Larger allocation needed for the unwind information.");
1236
1237         codeindex = MONO_MAX_UNWIND_CODES - (unwindinfo->unwindInfo.CountOfCodes += codesneeded);
1238         unwindcode = &unwindinfo->unwindInfo.UnwindCode[codeindex];
1239
1240         if (codesneeded == 1) {
1241                 /*The size of the allocation is 
1242                   (the number in the OpInfo member) times 8 plus 8*/
1243                 unwindcode->OpInfo = (size - 8)/8;
1244                 unwindcode->UnwindOp = 2; /*UWOP_ALLOC_SMALL*/
1245         }
1246         else {
1247                 if (codesneeded == 3) {
1248                         /*the unscaled size of the allocation is recorded
1249                           in the next two slots in little-endian format*/
1250                         *((unsigned int*)(&unwindcode->FrameOffset)) = size;
1251                         unwindcode += 2;
1252                         unwindcode->OpInfo = 1;
1253                 }
1254                 else {
1255                         /*the size of the allocation divided by 8
1256                           is recorded in the next slot*/
1257                         unwindcode->FrameOffset = size/8; 
1258                         unwindcode++;   
1259                         unwindcode->OpInfo = 0;
1260                         
1261                 }
1262                 unwindcode->UnwindOp = 1; /*UWOP_ALLOC_LARGE*/
1263         }
1264
1265         unwindcode->CodeOffset = (((guchar*)nextip)-((guchar*)codebegin));
1266
1267         if (unwindinfo->unwindInfo.SizeOfProlog >= unwindcode->CodeOffset)
1268                 g_error ("Adding unwind info in wrong order.");
1269         
1270         unwindinfo->unwindInfo.SizeOfProlog = unwindcode->CodeOffset;
1271 }
1272
1273 guint
1274 mono_arch_unwindinfo_get_size (gpointer monoui)
1275 {
1276         PMonoUnwindInfo unwindinfo;
1277         if (!monoui)
1278                 return 0;
1279         
1280         unwindinfo = (MonoUnwindInfo*)monoui;
1281         return (8 + sizeof (MonoUnwindInfo)) - 
1282                 (sizeof (UNWIND_CODE) * (MONO_MAX_UNWIND_CODES - unwindinfo->unwindInfo.CountOfCodes));
1283 }
1284
1285 PRUNTIME_FUNCTION
1286 MONO_GET_RUNTIME_FUNCTION_CALLBACK ( DWORD64 ControlPc, IN PVOID Context )
1287 {
1288         MonoJitInfo *ji;
1289         guint64 pos;
1290         PMonoUnwindInfo targetinfo;
1291         MonoDomain *domain = mono_domain_get ();
1292
1293         ji = mini_jit_info_table_find (domain, (char*)ControlPc, NULL);
1294         if (!ji)
1295                 return 0;
1296
1297         pos = (guint64)(((char*)ji->code_start) + ji->code_size);
1298         
1299         targetinfo = (PMonoUnwindInfo)ALIGN_TO (pos, 8);
1300
1301         targetinfo->runtimeFunction.UnwindData = ((DWORD64)&targetinfo->unwindInfo) - ((DWORD64)Context);
1302
1303         return &targetinfo->runtimeFunction;
1304 }
1305
1306 void
1307 mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size)
1308 {
1309         PMonoUnwindInfo unwindinfo, targetinfo;
1310         guchar codecount;
1311         guint64 targetlocation;
1312         if (!*monoui)
1313                 return;
1314
1315         unwindinfo = (MonoUnwindInfo*)*monoui;
1316         targetlocation = (guint64)&(((guchar*)code)[code_size]);
1317         targetinfo = (PMonoUnwindInfo) ALIGN_TO(targetlocation, 8);
1318
1319         unwindinfo->runtimeFunction.EndAddress = code_size;
1320         unwindinfo->runtimeFunction.UnwindData = ((guchar*)&targetinfo->unwindInfo) - ((guchar*)code);
1321         
1322         memcpy (targetinfo, unwindinfo, sizeof (MonoUnwindInfo) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
1323         
1324         codecount = unwindinfo->unwindInfo.CountOfCodes;
1325         if (codecount) {
1326                 memcpy (&targetinfo->unwindInfo.UnwindCode[0], &unwindinfo->unwindInfo.UnwindCode[MONO_MAX_UNWIND_CODES-codecount], 
1327                         sizeof (UNWIND_CODE) * unwindinfo->unwindInfo.CountOfCodes);
1328         }
1329
1330         g_free (unwindinfo);
1331         *monoui = 0;
1332
1333         RtlInstallFunctionTableCallback (((DWORD64)code) | 0x3, (DWORD64)code, code_size, MONO_GET_RUNTIME_FUNCTION_CALLBACK, code, NULL);
1334 }
1335
1336 #endif
1337
1338 #if MONO_SUPPORT_TASKLETS
1339 MonoContinuationRestore
1340 mono_tasklets_arch_restore (void)
1341 {
1342         static guint8* saved = NULL;
1343         guint8 *code, *start;
1344         int cont_reg = AMD64_R9; /* register usable on both call conventions */
1345
1346         if (saved)
1347                 return (MonoContinuationRestore)saved;
1348         code = start = mono_global_codeman_reserve (64);
1349         /* the signature is: restore (MonoContinuation *cont, int state, MonoLMF **lmf_addr) */
1350         /* cont is in AMD64_ARG_REG1 ($rcx or $rdi)
1351          * state is in AMD64_ARG_REG2 ($rdx or $rsi)
1352          * lmf_addr is in AMD64_ARG_REG3 ($r8 or $rdx)
1353          * We move cont to cont_reg since we need both rcx and rdi for the copy
1354          * state is moved to $rax so it's setup as the return value and we can overwrite $rsi
1355          */
1356         amd64_mov_reg_reg (code, cont_reg, MONO_AMD64_ARG_REG1, 8);
1357         amd64_mov_reg_reg (code, AMD64_RAX, MONO_AMD64_ARG_REG2, 8);
1358         /* setup the copy of the stack */
1359         amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, stack_used_size), sizeof (int));
1360         amd64_shift_reg_imm (code, X86_SHR, AMD64_RCX, 3);
1361         x86_cld (code);
1362         amd64_mov_reg_membase (code, AMD64_RSI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, saved_stack), sizeof (gpointer));
1363         amd64_mov_reg_membase (code, AMD64_RDI, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_sp), sizeof (gpointer));
1364         amd64_prefix (code, X86_REP_PREFIX);
1365         amd64_movsl (code);
1366
1367         /* now restore the registers from the LMF */
1368         amd64_mov_reg_membase (code, AMD64_RCX, cont_reg, G_STRUCT_OFFSET (MonoContinuation, lmf), 8);
1369         amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbx), 8);
1370         amd64_mov_reg_membase (code, AMD64_RBP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rbp), 8);
1371         amd64_mov_reg_membase (code, AMD64_R12, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r12), 8);
1372         amd64_mov_reg_membase (code, AMD64_R13, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r13), 8);
1373         amd64_mov_reg_membase (code, AMD64_R14, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r14), 8);
1374         amd64_mov_reg_membase (code, AMD64_R15, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, r15), 8);
1375 #ifdef TARGET_WIN32
1376         amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rdi), 8);
1377         amd64_mov_reg_membase (code, AMD64_RSI, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsi), 8);
1378 #endif
1379         amd64_mov_reg_membase (code, AMD64_RSP, AMD64_RCX, G_STRUCT_OFFSET (MonoLMF, rsp), 8);
1380
1381         /* restore the lmf chain */
1382         /*x86_mov_reg_membase (code, X86_ECX, X86_ESP, 12, 4);
1383         x86_mov_membase_reg (code, X86_ECX, 0, X86_EDX, 4);*/
1384
1385         /* state is already in rax */
1386         amd64_jump_membase (code, cont_reg, G_STRUCT_OFFSET (MonoContinuation, return_ip));
1387         g_assert ((code - start) <= 64);
1388         saved = start;
1389         return (MonoContinuationRestore)saved;
1390 }
1391 #endif
1392