[arm64] Add the full neon regs to the context
[mono.git] / mono / mini / exceptions-arm64.c
1 /**
2  * \file
3  * exception support for ARM64
4  *
5  * Copyright 2013 Xamarin Inc
6  *
7  * Based on exceptions-arm.c:
8  *
9  * Authors:
10  *   Dietmar Maurer (dietmar@ximian.com)
11  *   Paolo Molaro (lupus@ximian.com)
12  *
13  * (C) 2001 Ximian, Inc.
14  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15  */
16
17 #include "mini.h"
18
19 #include <mono/arch/arm64/arm64-codegen.h>
20 #include <mono/metadata/abi-details.h>
21
22 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
23
24 #ifndef DISABLE_JIT
25
26 gpointer
27 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
28 {
29         guint8 *start, *code;
30         MonoJumpInfo *ji = NULL;
31         GSList *unwind_ops = NULL;
32         int i, ctx_reg, size;
33         guint8 *labels [16];
34
35         size = 256;
36         code = start = mono_global_codeman_reserve (size);
37
38         arm_movx (code, ARMREG_IP0, ARMREG_R0);
39         ctx_reg = ARMREG_IP0;
40
41         /* Restore fregs */
42         arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, has_fregs));
43         labels [0] = code;
44         arm_cbzx (code, ARMREG_IP1, 0);
45         for (i = 0; i < 32; ++i)
46                 arm_ldrfpx (code, i, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs) + (i * sizeof (MonoContextSimdReg)));
47         mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
48         /* Restore gregs */
49         // FIXME: Restore less registers
50         // FIXME: fp should be restored later
51         code = mono_arm_emit_load_regarray (code, 0xffffffff & ~(1 << ctx_reg) & ~(1 << ARMREG_SP), ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs));
52         /* ip0/ip1 doesn't need to be restored */
53         /* ip1 = pc */
54         arm_ldrx (code, ARMREG_IP1, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
55         /* ip0 = sp */
56         arm_ldrx (code, ARMREG_IP0, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_SP * 8));
57         /* Restore sp, ctx is no longer valid */
58         arm_movspx (code, ARMREG_SP, ARMREG_IP0); 
59         /* Branch to pc */
60         arm_brx (code, ARMREG_IP1);
61         /* Not reached */
62         arm_brk (code, 0);
63
64         g_assert ((code - start) < size);
65         mono_arch_flush_icache (start, code - start);
66         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
67
68         if (info)
69                 *info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
70
71         return start;
72 }
73
74 gpointer
75 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
76 {
77         guint8 *code;
78         guint8* start;
79         int i, size, offset, gregs_offset, fregs_offset, ctx_offset, num_fregs, frame_size;
80         MonoJumpInfo *ji = NULL;
81         GSList *unwind_ops = NULL;
82         guint8 *labels [16];
83
84         size = 512;
85         start = code = mono_global_codeman_reserve (size);
86
87         /* Compute stack frame size and offsets */
88         offset = 0;
89         /* frame block */
90         offset += 2 * 8;
91         /* gregs */
92         gregs_offset = offset;
93         offset += 32 * 8;
94         /* fregs */
95         num_fregs = 8;
96         fregs_offset = offset;
97         offset += num_fregs * 8;
98         ctx_offset = offset;
99         ctx_offset += 8;
100         frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
101
102         /*
103          * We are being called from C code, ctx is in r0, the address to call is in r1.
104          * We need to save state, restore ctx, make the call, then restore the previous state,
105          * returning the value returned by the call.
106          */
107
108         /* Setup a frame */
109         arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
110         arm_movspx (code, ARMREG_FP, ARMREG_SP);
111
112         /* Save ctx */
113         arm_strx (code, ARMREG_R0, ARMREG_FP, ctx_offset);
114         /* Save gregs */
115         code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS | (1 << ARMREG_FP), ARMREG_FP, gregs_offset);
116         /* Save fregs */
117         for (i = 0; i < num_fregs; ++i)
118                 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
119
120         /* Load regs from ctx */
121         code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs));
122         /* Load fregs */
123         arm_ldrx (code, ARMREG_IP0, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, has_fregs));
124         labels [0] = code;
125         arm_cbzx (code, ARMREG_IP0, 0);
126         for (i = 0; i < num_fregs; ++i)
127                 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, fregs) + ((i + 8) * sizeof (MonoContextSimdReg)));
128         mono_arm_patch (labels [0], code, MONO_R_ARM64_CBZ);
129         /* Load fp */
130         arm_ldrx (code, ARMREG_FP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_FP * 8));
131
132         /* Make the call */
133         arm_blrx (code, ARMREG_R1);
134         /* For filters, the result is in R0 */
135
136         /* Restore fp */
137         arm_ldrx (code, ARMREG_FP, ARMREG_SP, gregs_offset + (ARMREG_FP * 8));
138         /* Load ctx */
139         arm_ldrx (code, ARMREG_IP0, ARMREG_FP, ctx_offset);
140         /* Save registers back to ctx */
141         /* This isn't strictly neccessary since we don't allocate variables used in eh clauses to registers */
142         code = mono_arm_emit_store_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_IP0, MONO_STRUCT_OFFSET (MonoContext, regs));
143
144         /* Restore regs */
145         code = mono_arm_emit_load_regarray (code, MONO_ARCH_CALLEE_SAVED_REGS, ARMREG_FP, gregs_offset);
146         /* Restore fregs */
147         for (i = 0; i < num_fregs; ++i)
148                 arm_ldrfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
149         /* Destroy frame */
150         code = mono_arm_emit_destroy_frame (code, frame_size, (1 << ARMREG_IP0));
151         arm_retx (code, ARMREG_LR);
152
153         g_assert ((code - start) < size);
154         mono_arch_flush_icache (start, code - start);
155         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
156
157         if (info)
158                 *info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
159
160         return start;
161 }
162
163 static gpointer 
164 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
165 {
166         guint8 *start, *code;
167         MonoJumpInfo *ji = NULL;
168         GSList *unwind_ops = NULL;
169         int i, offset, gregs_offset, fregs_offset, frame_size, num_fregs;
170
171         code = start = mono_global_codeman_reserve (size);
172
173         /* We are being called by JITted code, the exception object/type token is in R0 */
174
175         /* Compute stack frame size and offsets */
176         offset = 0;
177         /* frame block */
178         offset += 2 * 8;
179         /* gregs */
180         gregs_offset = offset;
181         offset += 32 * 8;
182         /* fregs */
183         num_fregs = 8;
184         fregs_offset = offset;
185         offset += num_fregs * 8;
186         frame_size = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
187
188         /* Setup a frame */
189         arm_stpx_pre (code, ARMREG_FP, ARMREG_LR, ARMREG_SP, -frame_size);
190         arm_movspx (code, ARMREG_FP, ARMREG_SP);
191
192         /* Save gregs */
193         code = mono_arm_emit_store_regarray (code, 0xffffffff, ARMREG_FP, gregs_offset);
194         if (corlib && !llvm)
195                 /* The real LR is in R1 */
196                 arm_strx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
197         /* Save fp/sp */
198         arm_ldrx (code, ARMREG_IP0, ARMREG_FP, 0);
199         arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_FP * 8));
200         arm_addx_imm (code, ARMREG_IP0, ARMREG_FP, frame_size);
201         arm_strx (code, ARMREG_IP0, ARMREG_FP, gregs_offset + (ARMREG_SP * 8)); 
202         /* Save fregs */
203         for (i = 0; i < num_fregs; ++i)
204                 arm_strfpx (code, ARMREG_D8 + i, ARMREG_FP, fregs_offset + (i * 8));
205
206         /* Call the C trampoline function */
207         /* Arg1 =  exception object/type token */
208         arm_movx (code, ARMREG_R0, ARMREG_R0);
209         /* Arg2 = caller ip */
210         if (corlib) {
211                 if (llvm)
212                         arm_ldrx (code, ARMREG_R1, ARMREG_FP, gregs_offset + (ARMREG_LR * 8));
213                 else
214                         arm_movx (code, ARMREG_R1, ARMREG_R1);
215         } else {
216                 arm_ldrx (code, ARMREG_R1, ARMREG_FP, 8);
217         }
218         /* Arg 3 = gregs */
219         arm_addx_imm (code, ARMREG_R2, ARMREG_FP, gregs_offset);
220         /* Arg 4 = fregs */
221         arm_addx_imm (code, ARMREG_R3, ARMREG_FP, fregs_offset);
222         /* Arg 5 = corlib */
223         arm_movzx (code, ARMREG_R4, corlib ? 1 : 0, 0);
224         /* Arg 6 = rethrow */
225         arm_movzx (code, ARMREG_R5, rethrow ? 1 : 0, 0);
226         /* Call the function */
227         if (aot) {
228                 const char *icall_name;
229
230                 if (resume_unwind)
231                         icall_name = "mono_arm_resume_unwind";
232                 else
233                         icall_name = "mono_arm_throw_exception";
234
235                 code = mono_arm_emit_aotconst (&ji, code, start, ARMREG_LR, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
236         } else {
237                 gpointer icall_func;
238
239                 if (resume_unwind)
240                         icall_func = mono_arm_resume_unwind;
241                 else
242                         icall_func = mono_arm_throw_exception;
243
244                 code = mono_arm_emit_imm64 (code, ARMREG_LR, (guint64)icall_func);
245         }
246         arm_blrx (code, ARMREG_LR);
247         /* This shouldn't return */
248         arm_brk (code, 0x0);
249
250         g_assert ((code - start) < size);
251         mono_arch_flush_icache (start, code - start);
252         MONO_PROFILER_RAISE (jit_code_buffer, (start, code - start, MONO_PROFILER_CODE_BUFFER_EXCEPTION_HANDLING, NULL));
253
254         if (info)
255                 *info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
256
257         return start;
258 }
259
260 gpointer 
261 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
262 {
263         return get_throw_trampoline (256, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
264 }
265
266 gpointer
267 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
268 {
269         return get_throw_trampoline (256, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
270 }
271
272 gpointer 
273 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
274 {
275         return get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
276 }
277
278 GSList*
279 mono_arm_get_exception_trampolines (gboolean aot)
280 {
281         MonoTrampInfo *info;
282         GSList *tramps = NULL;
283
284         /* LLVM uses the normal trampolines, but with a different name */
285         get_throw_trampoline (256, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
286         tramps = g_slist_prepend (tramps, info);
287         
288         get_throw_trampoline (256, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
289         tramps = g_slist_prepend (tramps, info);
290
291         get_throw_trampoline (256, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
292         tramps = g_slist_prepend (tramps, info);
293
294         return tramps;
295 }
296
297 #else /* DISABLE_JIT */
298
299 gpointer
300 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
301 {
302         g_assert_not_reached ();
303         return NULL;
304 }
305
306 gpointer
307 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
308 {
309         g_assert_not_reached ();
310         return NULL;
311 }
312
313 gpointer 
314 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
315 {
316         g_assert_not_reached ();
317         return NULL;
318 }
319
320 gpointer
321 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
322 {
323         g_assert_not_reached ();
324         return NULL;
325 }
326
327 gpointer 
328 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
329 {
330         g_assert_not_reached ();
331         return NULL;
332 }       
333
334 GSList*
335 mono_arm_get_exception_trampolines (gboolean aot)
336 {
337         g_assert_not_reached ();
338         return NULL;
339 }
340
341 #endif /* !DISABLE_JIT */
342
343 void
344 mono_arch_exceptions_init (void)
345 {
346         guint8 *tramp;
347         GSList *tramps, *l;
348
349         if (mono_aot_only) {
350                 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
351                 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
352                 tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
353                 mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
354                 tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
355                 mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
356         } else {
357                 tramps = mono_arm_get_exception_trampolines (FALSE);
358                 for (l = tramps; l; l = l->next) {
359                         MonoTrampInfo *info = l->data;
360
361                         mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
362                         mono_tramp_info_register (info, NULL);
363                 }
364                 g_slist_free (tramps);
365         }
366 }
367
368 /*
369  * mono_arm_throw_exception:
370  *
371  *   This function is called by the exception trampolines.
372  * FP_REGS points to the 8 callee saved fp regs.
373  */
374 void
375 mono_arm_throw_exception (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
376 {
377         MonoError error;
378         MonoContext ctx;
379         MonoObject *exc = NULL;
380         guint32 ex_token_index, ex_token;
381
382         if (!corlib)
383                 exc = arg;
384         else {
385                 ex_token_index = (guint64)arg;
386                 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
387                 exc = (MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token);
388         }
389
390         /* Adjust pc so it points into the call instruction */
391         pc -= 4;
392
393         /* Initialize a ctx based on the arguments */
394         memset (&ctx, 0, sizeof (MonoContext));
395         memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
396         for (int i = 0; i < 8; i++)
397                 *((gdouble*)&ctx.fregs [ARMREG_D8 + i]) = fp_regs [i];
398         ctx.has_fregs = 1;
399         ctx.pc = pc;
400
401         if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
402                 MonoException *mono_ex = (MonoException*)exc;
403                 if (!rethrow) {
404                         mono_ex->stack_trace = NULL;
405                         mono_ex->trace_ips = NULL;
406                 }
407         }
408         mono_error_assert_ok (&error);
409
410         mono_handle_exception (&ctx, exc);
411
412         mono_restore_context (&ctx);
413 }
414
415 void
416 mono_arm_resume_unwind (gpointer arg, mgreg_t pc, mgreg_t *int_regs, gdouble *fp_regs, gboolean corlib, gboolean rethrow)
417 {
418         MonoContext ctx;
419
420         /* Adjust pc so it points into the call instruction */
421         pc -= 4;
422
423         /* Initialize a ctx based on the arguments */
424         memset (&ctx, 0, sizeof (MonoContext));
425         memcpy (&(ctx.regs [0]), int_regs, sizeof (mgreg_t) * 32);
426         for (int i = 0; i < 8; i++)
427                 *((gdouble*)&ctx.fregs [ARMREG_D8 + i]) = fp_regs [i];
428         ctx.has_fregs = 1;
429         ctx.pc = pc;
430
431         mono_resume_unwind (&ctx);
432 }
433
434 /* 
435  * mono_arch_unwind_frame:
436  *
437  * See exceptions-amd64.c for docs;
438  */
439 gboolean
440 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls, 
441                                                          MonoJitInfo *ji, MonoContext *ctx, 
442                                                          MonoContext *new_ctx, MonoLMF **lmf,
443                                                          mgreg_t **save_locations,
444                                                          StackFrameInfo *frame)
445 {
446         gpointer ip = MONO_CONTEXT_GET_IP (ctx);
447
448         memset (frame, 0, sizeof (StackFrameInfo));
449         frame->ji = ji;
450
451         *new_ctx = *ctx;
452
453         if (ji != NULL) {
454                 mgreg_t regs [MONO_MAX_IREGS + 8 + 1];
455                 guint8 *cfa;
456                 guint32 unwind_info_len;
457                 guint8 *unwind_info;
458
459                 frame->type = FRAME_TYPE_MANAGED;
460
461                 unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
462
463                 memcpy (regs, &new_ctx->regs, sizeof (mgreg_t) * 32);
464                 /* v8..v15 are callee saved */
465                 for (int i = 0; i < 8; i++)
466                         (regs + MONO_MAX_IREGS) [i] = *((mgreg_t*)&new_ctx->fregs [8 + i]);
467
468                 mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start, 
469                                                    (guint8*)ji->code_start + ji->code_size,
470                                                    ip, NULL, regs, MONO_MAX_IREGS + 8,
471                                                    save_locations, MONO_MAX_IREGS, &cfa);
472
473                 memcpy (&new_ctx->regs, regs, sizeof (mgreg_t) * 32);
474                 for (int i = 0; i < 8; i++)
475                         *((mgreg_t*)&new_ctx->fregs [8 + i]) = (regs + MONO_MAX_IREGS) [i];
476
477                 new_ctx->pc = regs [ARMREG_LR];
478                 new_ctx->regs [ARMREG_SP] = (mgreg_t)cfa;
479
480                 if (*lmf && (*lmf)->gregs [MONO_ARCH_LMF_REG_SP] && (MONO_CONTEXT_GET_SP (ctx) >= (gpointer)(*lmf)->gregs [MONO_ARCH_LMF_REG_SP])) {
481                         /* remove any unused lmf */
482                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
483                 }
484
485                 /* we substract 1, so that the IP points into the call instruction */
486                 new_ctx->pc--;
487
488                 return TRUE;
489         } else if (*lmf) {
490                 if (((gsize)(*lmf)->previous_lmf) & 2) {
491                         MonoLMFExt *ext = (MonoLMFExt*)(*lmf);
492
493                         if (ext->debugger_invoke) {
494                                 /*
495                                  * This LMF entry is created by the soft debug code to mark transitions to
496                                  * managed code done during invokes.
497                                  */
498                                 frame->type = FRAME_TYPE_DEBUGGER_INVOKE;
499                                 memcpy (new_ctx, &ext->ctx, sizeof (MonoContext));
500                         } else if (ext->interp_exit) {
501                                 frame->type = FRAME_TYPE_INTERP_TO_MANAGED;
502                                 frame->interp_exit_data = ext->interp_exit_data;
503                         } else {
504                                 g_assert_not_reached ();
505                         }
506
507                         *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
508
509                         return TRUE;
510                 }
511
512                 frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
513
514                 ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->pc, NULL);
515                 if (!ji)
516                         return FALSE;
517
518                 g_assert (MONO_ARCH_LMF_REGS == ((0x3ff << 19) | (1 << ARMREG_FP) | (1 << ARMREG_SP)));
519                 memcpy (&new_ctx->regs [ARMREG_R19], &(*lmf)->gregs [0], sizeof (mgreg_t) * 10);
520                 new_ctx->regs [ARMREG_FP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_FP];
521                 new_ctx->regs [ARMREG_SP] = (*lmf)->gregs [MONO_ARCH_LMF_REG_SP];
522                 new_ctx->pc = (*lmf)->pc;
523
524                 /* we substract 1, so that the IP points into the call instruction */
525                 new_ctx->pc--;
526
527                 *lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
528
529                 return TRUE;
530         }
531
532         return FALSE;
533 }
534
535 /*
536  * handle_exception:
537  *
538  *   Called by resuming from a signal handler.
539  */
540 static void
541 handle_signal_exception (gpointer obj)
542 {
543         MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
544         MonoContext ctx;
545
546         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
547
548         mono_handle_exception (&ctx, obj);
549
550         mono_restore_context (&ctx);
551 }
552
553 /*
554  * This is the function called from the signal handler
555  */
556 gboolean
557 mono_arch_handle_exception (void *ctx, gpointer obj)
558 {
559 #if defined(MONO_CROSS_COMPILE)
560         g_assert_not_reached ();
561 #else
562         MonoJitTlsData *jit_tls;
563         void *sigctx = ctx;
564
565         /*
566          * Resume into the normal stack and handle the exception there.
567          */
568         jit_tls = mono_tls_get_jit_tls ();
569
570         /* Pass the ctx parameter in TLS */
571         mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
572         /* The others in registers */
573         UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
574
575         UCONTEXT_REG_PC (sigctx) = (gsize)handle_signal_exception;
576         UCONTEXT_REG_SP (sigctx) = UCONTEXT_REG_SP (sigctx) - MONO_ARCH_REDZONE_SIZE;
577 #endif
578
579         return TRUE;
580 }
581
582 gpointer
583 mono_arch_ip_from_context (void *sigctx)
584 {
585 #ifdef MONO_CROSS_COMPILE
586         g_assert_not_reached ();
587         return NULL;
588 #else
589         return (gpointer)UCONTEXT_REG_PC (sigctx);
590 #endif
591 }
592
593 void
594 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
595 {
596         mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
597
598         // FIXME:
599         g_assert (!user_data);
600
601         /* Allocate a stack frame */
602         sp -= 32;
603         MONO_CONTEXT_SET_SP (ctx, sp);
604
605         mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
606 }
607
608 /*
609  * mono_arch_setup_resume_sighandler_ctx:
610  *
611  *   Setup CTX so execution continues at FUNC.
612  */
613 void
614 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
615 {
616         MONO_CONTEXT_SET_IP (ctx,func);
617 }