1 /**
2  * \file
3  * exception support for ARM
4  *
5  * Authors:
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *   Paolo Molaro (lupus@ximian.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11 
12 #include <config.h>
13 #include <glib.h>
14 #include <signal.h>
15 #include <string.h>
16 
17 #ifndef MONO_CROSS_COMPILE
18 #ifdef HOST_ANDROID
19 #include <asm/sigcontext.h>
20 #endif  /* def HOST_ANDROID */
21 #endif
22 
23 #ifdef HAVE_UCONTEXT_H
24 #include <ucontext.h>
25 #endif  /* def HAVE_UCONTEXT_H */
26 
27 #include <mono/arch/arm/arm-codegen.h>
28 #include <mono/arch/arm/arm-vfp-codegen.h>
29 #include <mono/metadata/abi-details.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/threads.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/exception.h>
35 #include <mono/metadata/mono-debug.h>
36 
37 #include "mini.h"
38 #include "mini-arm.h"
39 #include "mini-runtime.h"
40 #include "aot-runtime.h"
41 #include "mono/utils/mono-sigcontext.h"
42 #include "mono/utils/mono-compiler.h"
43 
44 /*
45  * arch_get_restore_context:
46  *
47  * Returns a pointer to a method which restores a previously saved sigcontext.
48  * The first argument in r0 is the pointer to the context.
49  */
50 gpointer
mono_arch_get_restore_context(MonoTrampInfo ** info,gboolean aot)51 mono_arch_get_restore_context (MonoTrampInfo **info, gboolean aot)
52 {
53 	guint8 *code;
54 	guint8 *start;
55 	int ctx_reg;
56 	MonoJumpInfo *ji = NULL;
57 	GSList *unwind_ops = NULL;
58 
59 	start = code = mono_global_codeman_reserve (128);
60 
61 	/*
62 	 * Move things to their proper place so we can restore all the registers with
63 	 * one instruction.
64 	 */
65 
66 	ctx_reg = ARMREG_R0;
67 
68 	if (!mono_arch_is_soft_float ()) {
69 		ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, fregs));
70 		ARM_FLDMD (code, ARM_VFP_D0, 16, ARMREG_IP);
71 	}
72 
73 	/* move pc to PC */
74 	ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
75 	ARM_STR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, regs) + (ARMREG_PC * sizeof (mgreg_t)));
76 
77 	/* restore everything */
78 	ARM_ADD_REG_IMM8 (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs));
79 	ARM_LDM (code, ARMREG_IP, 0xffff);
80 
81 	/* never reached */
82 	ARM_DBRK (code);
83 
84 	g_assert ((code - start) < 128);
85 
86 	mono_arch_flush_icache (start, code - start);
87 
88 	if (info)
89 		*info = mono_tramp_info_create ("restore_context", start, code - start, ji, unwind_ops);
90 
91 	return start;
92 }
93 
94 /*
95  * arch_get_call_filter:
96  *
97  * Returns a pointer to a method which calls an exception filter. We
98  * also use this function to call finally handlers (we pass NULL as
99  * @exc object in this case).
100  */
101 gpointer
mono_arch_get_call_filter(MonoTrampInfo ** info,gboolean aot)102 mono_arch_get_call_filter (MonoTrampInfo **info, gboolean aot)
103 {
104 	guint8 *code;
105 	guint8* start;
106 	int ctx_reg;
107 	MonoJumpInfo *ji = NULL;
108 	GSList *unwind_ops = NULL;
109 
110 	/* call_filter (MonoContext *ctx, unsigned long eip, gpointer exc) */
111 	start = code = mono_global_codeman_reserve (320);
112 
113 	/* save all the regs on the stack */
114 	ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
115 	ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
116 
117 	ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
118 
119 	/* restore all the regs from ctx (in r0), but not sp, the stack pointer */
120 	ctx_reg = ARMREG_R0;
121 	ARM_LDR_IMM (code, ARMREG_IP, ctx_reg, MONO_STRUCT_OFFSET (MonoContext, pc));
122 	ARM_ADD_REG_IMM8 (code, ARMREG_LR, ctx_reg, MONO_STRUCT_OFFSET(MonoContext, regs) + (MONO_ARM_FIRST_SAVED_REG * sizeof (mgreg_t)));
123 	ARM_LDM (code, ARMREG_LR, MONO_ARM_REGSAVE_MASK);
124 	/* call handler at eip (r1) and set the first arg with the exception (r2) */
125 	ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_R2);
126 	ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
127 	ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_R1);
128 
129 	ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
130 
131 	/* epilog */
132 	ARM_POP_NWB (code, 0xff0 | ((1 << ARMREG_SP) | (1 << ARMREG_PC)));
133 
134 	g_assert ((code - start) < 320);
135 
136 	mono_arch_flush_icache (start, code - start);
137 
138 	if (info)
139 		*info = mono_tramp_info_create ("call_filter", start, code - start, ji, unwind_ops);
140 
141 	return start;
142 }
143 
144 void
mono_arm_throw_exception(MonoObject * exc,mgreg_t pc,mgreg_t sp,mgreg_t * int_regs,gdouble * fp_regs)145 mono_arm_throw_exception (MonoObject *exc, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
146 {
147 	MonoError error;
148 	MonoContext ctx;
149 	gboolean rethrow = sp & 1;
150 
151 	sp &= ~1; /* clear the optional rethrow bit */
152 	pc &= ~1; /* clear the thumb bit */
153 	/* adjust eip so that it point into the call instruction */
154 	pc -= 4;
155 
156 	/*printf ("stack in throw: %p\n", esp);*/
157 	MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
158 	MONO_CONTEXT_SET_SP (&ctx, sp);
159 	MONO_CONTEXT_SET_IP (&ctx, pc);
160 	memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
161 	memcpy (&ctx.fregs, fp_regs, sizeof (double) * 16);
162 
163 	if (mono_object_isinst_checked (exc, mono_defaults.exception_class, &error)) {
164 		MonoException *mono_ex = (MonoException*)exc;
165 		if (!rethrow) {
166 			mono_ex->stack_trace = NULL;
167 			mono_ex->trace_ips = NULL;
168 		}
169 	}
170 	mono_error_assert_ok (&error);
171 	mono_handle_exception (&ctx, exc);
172 	mono_restore_context (&ctx);
173 	g_assert_not_reached ();
174 }
175 
176 void
mono_arm_throw_exception_by_token(guint32 ex_token_index,mgreg_t pc,mgreg_t sp,mgreg_t * int_regs,gdouble * fp_regs)177 mono_arm_throw_exception_by_token (guint32 ex_token_index, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
178 {
179 	guint32 ex_token = MONO_TOKEN_TYPE_DEF | ex_token_index;
180 	/* Clear thumb bit */
181 	pc &= ~1;
182 
183 	mono_arm_throw_exception ((MonoObject*)mono_exception_from_token (mono_defaults.corlib, ex_token), pc, sp, int_regs, fp_regs);
184 }
185 
186 void
mono_arm_resume_unwind(guint32 dummy1,mgreg_t pc,mgreg_t sp,mgreg_t * int_regs,gdouble * fp_regs)187 mono_arm_resume_unwind (guint32 dummy1, mgreg_t pc, mgreg_t sp, mgreg_t *int_regs, gdouble *fp_regs)
188 {
189 	MonoContext ctx;
190 
191 	pc &= ~1; /* clear the optional rethrow bit */
192 	/* adjust eip so that it point into the call instruction */
193 	pc -= 4;
194 
195 	MONO_CONTEXT_SET_BP (&ctx, int_regs [ARMREG_FP - 4]);
196 	MONO_CONTEXT_SET_SP (&ctx, sp);
197 	MONO_CONTEXT_SET_IP (&ctx, pc);
198 	memcpy (((guint8*)&ctx.regs) + (ARMREG_R4 * sizeof (mgreg_t)), int_regs, 8 * sizeof (mgreg_t));
199 
200 	mono_resume_unwind (&ctx);
201 }
202 
203 /**
204  * get_throw_trampoline:
205  *
206  * Returns a function pointer which can be used to raise
207  * exceptions. The returned function has the following
208  * signature: void (*func) (MonoException *exc); or
209  * void (*func) (guint32 ex_token, guint8* ip);
210  *
211  */
212 static gpointer
get_throw_trampoline(int size,gboolean corlib,gboolean rethrow,gboolean llvm,gboolean resume_unwind,const char * tramp_name,MonoTrampInfo ** info,gboolean aot)213 get_throw_trampoline (int size, gboolean corlib, gboolean rethrow, gboolean llvm, gboolean resume_unwind, const char *tramp_name, MonoTrampInfo **info, gboolean aot)
214 {
215 	guint8 *start;
216 	guint8 *code;
217 	MonoJumpInfo *ji = NULL;
218 	GSList *unwind_ops = NULL;
219 	int cfa_offset;
220 
221 	code = start = mono_global_codeman_reserve (size);
222 
223 	mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, 0);
224 
225 	/* save all the regs on the stack */
226 	ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
227 	ARM_PUSH (code, MONO_ARM_REGSAVE_MASK);
228 
229 	cfa_offset = MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t);
230 	mono_add_unwind_op_def_cfa (unwind_ops, code, start, ARMREG_SP, cfa_offset);
231 	mono_add_unwind_op_offset (unwind_ops, code, start, ARMREG_LR, - sizeof (mgreg_t));
232 
233 	/* Save fp regs */
234 	if (!mono_arch_is_soft_float ()) {
235 		ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (double) * 16);
236 		cfa_offset += sizeof (double) * 16;
237 		mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
238 		ARM_FSTMD (code, ARM_VFP_D0, 16, ARMREG_SP);
239 	}
240 
241 	/* Param area */
242 	ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
243 	cfa_offset += 8;
244 	mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, cfa_offset);
245 
246 	/* call throw_exception (exc, ip, sp, int_regs, fp_regs) */
247 	/* caller sp */
248 	ARM_ADD_REG_IMM8 (code, ARMREG_R2, ARMREG_SP, cfa_offset);
249 	/* we encode rethrow in sp */
250 	if (rethrow) {
251 		g_assert (!resume_unwind);
252 		g_assert (!corlib);
253 		ARM_ORR_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, rethrow);
254 	}
255 	/* exc is already in place in r0 */
256 	if (corlib) {
257 		/* The caller ip is already in R1 */
258 		if (llvm) {
259 			/*
260 			 * The address passed by llvm might point to before the call,
261 			 * thus outside the eh range recorded by llvm. Use the return
262 			 * address instead.
263 			 * FIXME: Do this on more platforms.
264 			 */
265 			ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
266 		}
267 	} else {
268 		ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR); /* caller ip */
269 	}
270 	/* int regs */
271 	ARM_ADD_REG_IMM8 (code, ARMREG_R3, ARMREG_SP, (cfa_offset - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t))));
272 	/* fp regs */
273 	ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_SP, 8);
274 	ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, 0);
275 
276 	if (aot) {
277 		const char *icall_name;
278 
279 		if (resume_unwind)
280 			icall_name = "mono_arm_resume_unwind";
281 		else if (corlib)
282 			icall_name = "mono_arm_throw_exception_by_token";
283 		else
284 			icall_name = "mono_arm_throw_exception";
285 
286 		ji = mono_patch_info_list_prepend (ji, code - start, MONO_PATCH_INFO_JIT_ICALL_ADDR, icall_name);
287 		ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
288 		ARM_B (code, 0);
289 		*(gpointer*)(gpointer)code = NULL;
290 		code += 4;
291 		ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
292 	} else {
293 		code = mono_arm_emit_load_imm (code, ARMREG_IP, GPOINTER_TO_UINT (resume_unwind ? (gpointer)mono_arm_resume_unwind : (corlib ? (gpointer)mono_arm_throw_exception_by_token : (gpointer)mono_arm_throw_exception)));
294 	}
295 	ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
296 	ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
297 	/* we should never reach this breakpoint */
298 	ARM_DBRK (code);
299 	g_assert ((code - start) < size);
300 	mono_arch_flush_icache (start, code - start);
301 
302 	if (info)
303 		*info = mono_tramp_info_create (tramp_name, start, code - start, ji, unwind_ops);
304 
305 	return start;
306 }
307 
308 /**
309  * arch_get_throw_exception:
310  *
311  * Returns a function pointer which can be used to raise
312  * exceptions. The returned function has the following
313  * signature: void (*func) (MonoException *exc);
314  * For example to raise an arithmetic exception you can use:
315  *
316  * x86_push_imm (code, mono_get_exception_arithmetic ());
317  * x86_call_code (code, arch_get_throw_exception ());
318  *
319  */
320 gpointer
mono_arch_get_throw_exception(MonoTrampInfo ** info,gboolean aot)321 mono_arch_get_throw_exception (MonoTrampInfo **info, gboolean aot)
322 {
323 	return get_throw_trampoline (132, FALSE, FALSE, FALSE, FALSE, "throw_exception", info, aot);
324 }
325 
326 /**
327  * mono_arch_get_rethrow_exception:
328  *
329  * Returns a function pointer which can be used to rethrow
330  * exceptions. The returned function has the following
331  * signature: void (*func) (MonoException *exc);
332  *
333  */
334 gpointer
mono_arch_get_rethrow_exception(MonoTrampInfo ** info,gboolean aot)335 mono_arch_get_rethrow_exception (MonoTrampInfo **info, gboolean aot)
336 {
337 	return get_throw_trampoline (132, FALSE, TRUE, FALSE, FALSE, "rethrow_exception", info, aot);
338 }
339 
340 /**
341  * mono_arch_get_throw_corlib_exception:
342  * \returns a function pointer which can be used to raise
343  * corlib exceptions. The returned function has the following
344  * signature: void (*func) (guint32 ex_token, guint32 offset);
345  * Here, \c offset is the offset which needs to be substracted from the caller IP
346  * to get the IP of the throw. Passing the offset has the advantage that it
347  * needs no relocations in the caller.
348  * On ARM, the ip is passed instead of an offset.
349  */
350 gpointer
mono_arch_get_throw_corlib_exception(MonoTrampInfo ** info,gboolean aot)351 mono_arch_get_throw_corlib_exception (MonoTrampInfo **info, gboolean aot)
352 {
353 	return get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "throw_corlib_exception", info, aot);
354 }
355 
356 GSList*
mono_arm_get_exception_trampolines(gboolean aot)357 mono_arm_get_exception_trampolines (gboolean aot)
358 {
359 	MonoTrampInfo *info;
360 	GSList *tramps = NULL;
361 
362 	/* LLVM uses the normal trampolines, but with a different name */
363 	get_throw_trampoline (168, TRUE, FALSE, FALSE, FALSE, "llvm_throw_corlib_exception_trampoline", &info, aot);
364 	tramps = g_slist_prepend (tramps, info);
365 
366 	get_throw_trampoline (168, TRUE, FALSE, TRUE, FALSE, "llvm_throw_corlib_exception_abs_trampoline", &info, aot);
367 	tramps = g_slist_prepend (tramps, info);
368 
369 	get_throw_trampoline (168, FALSE, FALSE, FALSE, TRUE, "llvm_resume_unwind_trampoline", &info, aot);
370 	tramps = g_slist_prepend (tramps, info);
371 
372 	return tramps;
373 }
374 
375 void
mono_arch_exceptions_init(void)376 mono_arch_exceptions_init (void)
377 {
378 	guint8 *tramp;
379 	GSList *tramps, *l;
380 
381 	if (mono_aot_only) {
382 		tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_trampoline");
383 		mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_trampoline", NULL, TRUE);
384 		tramp = mono_aot_get_trampoline ("llvm_throw_corlib_exception_abs_trampoline");
385 		mono_register_jit_icall (tramp, "llvm_throw_corlib_exception_abs_trampoline", NULL, TRUE);
386 		tramp = mono_aot_get_trampoline ("llvm_resume_unwind_trampoline");
387 		mono_register_jit_icall (tramp, "llvm_resume_unwind_trampoline", NULL, TRUE);
388 	} else {
389 		tramps = mono_arm_get_exception_trampolines (FALSE);
390 		for (l = tramps; l; l = l->next) {
391 			MonoTrampInfo *info = l->data;
392 
393 			mono_register_jit_icall (info->code, g_strdup (info->name), NULL, TRUE);
394 			mono_tramp_info_register (info, NULL);
395 		}
396 		g_slist_free (tramps);
397 	}
398 }
399 
400 /*
401  * mono_arch_unwind_frame:
402  *
403  * See exceptions-amd64.c for docs;
404  */
405 gboolean
mono_arch_unwind_frame(MonoDomain * domain,MonoJitTlsData * jit_tls,MonoJitInfo * ji,MonoContext * ctx,MonoContext * new_ctx,MonoLMF ** lmf,mgreg_t ** save_locations,StackFrameInfo * frame)406 mono_arch_unwind_frame (MonoDomain *domain, MonoJitTlsData *jit_tls,
407 							 MonoJitInfo *ji, MonoContext *ctx,
408 							 MonoContext *new_ctx, MonoLMF **lmf,
409 							 mgreg_t **save_locations,
410 							 StackFrameInfo *frame)
411 {
412 	gpointer ip = MONO_CONTEXT_GET_IP (ctx);
413 
414 	memset (frame, 0, sizeof (StackFrameInfo));
415 	frame->ji = ji;
416 
417 	*new_ctx = *ctx;
418 
419 	if (ji != NULL) {
420 		int i;
421 		mono_unwind_reg_t regs [MONO_MAX_IREGS + 1 + 8];
422 		guint8 *cfa;
423 		guint32 unwind_info_len;
424 		guint8 *unwind_info;
425 
426 		if (ji->is_trampoline)
427 			frame->type = FRAME_TYPE_TRAMPOLINE;
428 		else
429 			frame->type = FRAME_TYPE_MANAGED;
430 
431 		unwind_info = mono_jinfo_get_unwind_info (ji, &unwind_info_len);
432 
433 		/*
434 		printf ("%s %p %p\n", ji->d.method->name, ji->code_start, ip);
435 		mono_print_unwind_info (unwind_info, unwind_info_len);
436 		*/
437 
438 		for (i = 0; i < 16; ++i)
439 			regs [i] = new_ctx->regs [i];
440 #ifdef TARGET_IOS
441 		/* On IOS, d8..d15 are callee saved. They are mapped to 8..15 in unwind.c */
442 		for (i = 0; i < 8; ++i)
443 			regs [MONO_MAX_IREGS + i] = *(guint64*)&(new_ctx->fregs [8 + i]);
444 #endif
445 
446 		mono_unwind_frame (unwind_info, unwind_info_len, ji->code_start,
447 						   (guint8*)ji->code_start + ji->code_size,
448 						   ip, NULL, regs, MONO_MAX_IREGS + 8,
449 						   save_locations, MONO_MAX_IREGS, &cfa);
450 
451 		for (i = 0; i < 16; ++i)
452 			new_ctx->regs [i] = regs [i];
453 		new_ctx->pc = regs [ARMREG_LR];
454 		new_ctx->regs [ARMREG_SP] = (gsize)cfa;
455 #ifdef TARGET_IOS
456 		for (i = 0; i < 8; ++i)
457 			new_ctx->fregs [8 + i] = *(double*)&(regs [MONO_MAX_IREGS + i]);
458 #endif
459 
460 		/* Clear thumb bit */
461 		new_ctx->pc &= ~1;
462 
463 		/* we substract 1, so that the IP points into the call instruction */
464 		new_ctx->pc--;
465 
466 		return TRUE;
467 	} else if (*lmf) {
468 		g_assert ((((guint64)(*lmf)->previous_lmf) & 2) == 0);
469 
470 		frame->type = FRAME_TYPE_MANAGED_TO_NATIVE;
471 
472 		if ((ji = mini_jit_info_table_find (domain, (gpointer)(*lmf)->ip, NULL))) {
473 			frame->ji = ji;
474 		} else {
475 			if (!(*lmf)->method)
476 				return FALSE;
477 			frame->method = (*lmf)->method;
478 		}
479 
480 		/*
481 		 * The LMF is saved at the start of the method using:
482 		 * ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP)
483 		 * ARM_PUSH (code, 0x5ff0);
484 		 * So it stores the register state as it existed at the caller. We need to
485 		 * produce the register state which existed at the time of the call which
486 		 * transitioned to native call, so we save the sp/fp/ip in the LMF.
487 		 */
488 		memcpy (&new_ctx->regs [0], &(*lmf)->iregs [0], sizeof (mgreg_t) * 13);
489 		new_ctx->pc = (*lmf)->ip;
490 		new_ctx->regs [ARMREG_SP] = (*lmf)->sp;
491 		new_ctx->regs [ARMREG_FP] = (*lmf)->fp;
492 
493 		/* Clear thumb bit */
494 		new_ctx->pc &= ~1;
495 
496 		/* we substract 1, so that the IP points into the call instruction */
497 		new_ctx->pc--;
498 
499 		*lmf = (gpointer)(((gsize)(*lmf)->previous_lmf) & ~3);
500 
501 		return TRUE;
502 	}
503 
504 	return FALSE;
505 }
506 
507 /*
508  * handle_exception:
509  *
510  *   Called by resuming from a signal handler.
511  */
512 static void
handle_signal_exception(gpointer obj)513 handle_signal_exception (gpointer obj)
514 {
515 	MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
516 	MonoContext ctx;
517 
518 	memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
519 
520 	mono_handle_exception (&ctx, obj);
521 
522 	mono_restore_context (&ctx);
523 }
524 
525 /*
526  * This works around a gcc 4.5 bug:
527  * https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/721531
528  */
529 static MONO_NEVER_INLINE gpointer
get_handle_signal_exception_addr(void)530 get_handle_signal_exception_addr (void)
531 {
532 	return handle_signal_exception;
533 }
534 
535 /*
536  * This is the function called from the signal handler
537  */
538 gboolean
mono_arch_handle_exception(void * ctx,gpointer obj)539 mono_arch_handle_exception (void *ctx, gpointer obj)
540 {
541 #if defined(MONO_CROSS_COMPILE) || !defined(MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX)
542 	g_assert_not_reached ();
543 #elif defined(MONO_ARCH_USE_SIGACTION)
544 	arm_ucontext *sigctx = ctx;
545 	/*
546 	 * Handling the exception in the signal handler is problematic, since the original
547 	 * signal is disabled, and we could run arbitrary code though the debugger. So
548 	 * resume into the normal stack and do most work there if possible.
549 	 */
550 	MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
551 	guint64 sp = UCONTEXT_REG_SP (sigctx);
552 
553 	/* Pass the ctx parameter in TLS */
554 	mono_sigctx_to_monoctx (sigctx, &jit_tls->ex_ctx);
555 	/* The others in registers */
556 	UCONTEXT_REG_R0 (sigctx) = (gsize)obj;
557 
558 	/* Allocate a stack frame */
559 	sp -= 16;
560 	UCONTEXT_REG_SP (sigctx) = sp;
561 
562 	UCONTEXT_REG_PC (sigctx) = (gsize)get_handle_signal_exception_addr ();
563 #ifdef UCONTEXT_REG_CPSR
564 	if ((gsize)UCONTEXT_REG_PC (sigctx) & 1)
565 		/* Transition to thumb */
566 		UCONTEXT_REG_CPSR (sigctx) |= (1 << 5);
567 	else
568 		/* Transition to ARM */
569 		UCONTEXT_REG_CPSR (sigctx) &= ~(1 << 5);
570 #endif
571 
572 	return TRUE;
573 #else
574 	MonoContext mctx;
575 	gboolean result;
576 
577 	mono_sigctx_to_monoctx (ctx, &mctx);
578 
579 	result = mono_handle_exception (&mctx, obj);
580 	/* restore the context so that returning from the signal handler will invoke
581 	 * the catch clause
582 	 */
583 	mono_monoctx_to_sigctx (&mctx, ctx);
584 	return result;
585 #endif
586 }
587 
588 gpointer
mono_arch_ip_from_context(void * sigctx)589 mono_arch_ip_from_context (void *sigctx)
590 {
591 #ifdef MONO_CROSS_COMPILE
592 	g_assert_not_reached ();
593 #else
594 	arm_ucontext *my_uc = sigctx;
595 	return (void*) UCONTEXT_REG_PC (my_uc);
596 #endif
597 }
598 
599 void
mono_arch_setup_async_callback(MonoContext * ctx,void (* async_cb)(void * fun),gpointer user_data)600 mono_arch_setup_async_callback (MonoContext *ctx, void (*async_cb)(void *fun), gpointer user_data)
601 {
602 	mgreg_t sp = (mgreg_t)MONO_CONTEXT_GET_SP (ctx);
603 
604 	// FIXME:
605 	g_assert (!user_data);
606 
607 	/* Allocate a stack frame */
608 	sp -= 16;
609 	MONO_CONTEXT_SET_SP (ctx, sp);
610 
611 	mono_arch_setup_resume_sighandler_ctx (ctx, async_cb);
612 }
613 
614 /*
615  * mono_arch_setup_resume_sighandler_ctx:
616  *
617  *   Setup CTX so execution continues at FUNC.
618  */
619 void
mono_arch_setup_resume_sighandler_ctx(MonoContext * ctx,gpointer func)620 mono_arch_setup_resume_sighandler_ctx (MonoContext *ctx, gpointer func)
621 {
622 	MONO_CONTEXT_SET_IP (ctx,func);
623 	if ((mgreg_t)MONO_CONTEXT_GET_IP (ctx) & 1)
624 		/* Transition to thumb */
625 		ctx->cpsr |= (1 << 5);
626 	else
627 		/* Transition to ARM */
628 		ctx->cpsr &= ~(1 << 5);
629 }
630