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