2 * exception.c: exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/arch/x86/x86-codegen.h>
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/threads.h>
17 #include <mono/metadata/debug-helpers.h>
23 # define SC_EAX sc_eax
24 # define SC_EBX sc_ebx
25 # define SC_ECX sc_ecx
26 # define SC_EDX sc_edx
27 # define SC_EBP sc_ebp
28 # define SC_EIP sc_eip
29 # define SC_ESP sc_esp
30 # define SC_EDI sc_edi
31 # define SC_ESI sc_esi
45 * arch_get_restore_context:
47 * Returns a pointer to a method which restores a previously saved sigcontext.
50 arch_get_restore_context (void)
52 static guint8 *start = NULL;
58 /* restore_contect (struct sigcontext *ctx) */
59 /* we do not restore X86_EAX, X86_EDX */
61 start = code = g_malloc (1024);
64 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
66 /* get return address, stored in EDX */
67 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EIP), 4);
69 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBX), 4);
71 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EDI), 4);
73 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESI), 4);
75 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ESP), 4);
77 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
78 /* restore ECX. the exception object is passed here to the catch handler */
79 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_ECX), 4);
81 /* jump to the saved IP */
82 x86_jump_reg (code, X86_EDX);
88 * arch_get_call_finally:
90 * Returns a pointer to a method which calls a finally handler.
93 arch_get_call_finally (void)
95 static guint8 start [28];
96 static int inited = 0;
103 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
106 x86_push_reg (code, X86_EBP);
107 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
108 x86_push_reg (code, X86_EBX);
109 x86_push_reg (code, X86_EDI);
110 x86_push_reg (code, X86_ESI);
113 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
115 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
117 x86_push_reg (code, X86_EBP);
119 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, SC_EBP), 4);
120 /* call the handler */
121 x86_call_reg (code, X86_ECX);
123 x86_pop_reg (code, X86_EBP);
124 /* restore saved regs */
125 x86_pop_reg (code, X86_ESI);
126 x86_pop_reg (code, X86_EDI);
127 x86_pop_reg (code, X86_EBX);
131 g_assert ((code - start) < 28);
137 * arch_handle_exception:
138 * @ctx: saved processor state
142 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
144 MonoDomain *domain = mono_domain_get ();
146 gpointer ip = (gpointer)ctx->SC_EIP;
147 static void (*restore_context) (struct sigcontext *);
148 static void (*call_finally) (struct sigcontext *, unsigned long);
149 void (*cleanup) (MonoObject *exc);
150 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
151 gpointer end_of_stack;
153 g_assert (ctx != NULL);
154 g_assert (obj != NULL);
156 ji = mono_jit_info_table_find (domain, ip);
158 end_of_stack = jit_tls->end_of_stack;
159 g_assert (end_of_stack);
161 if (!restore_context)
162 restore_context = arch_get_restore_context ();
165 call_finally = arch_get_call_finally ();
167 cleanup = jit_tls->abort_func;
169 if (ji) { /* we are inside managed code */
170 MonoMethod *m = ji->method;
173 if (ji->num_clauses) {
176 g_assert (ji->clauses);
178 for (i = 0; i < ji->num_clauses; i++) {
179 MonoJitExceptionInfo *ei = &ji->clauses [i];
181 if (ei->try_start <= ip && ip <= (ei->try_end)) {
183 if (ei->flags == 0 && mono_object_isinst (obj,
184 mono_class_get (m->klass->image, ei->token_or_filter))) {
186 ctx->SC_EIP = (unsigned long)ei->handler_start;
187 ctx->SC_ECX = (unsigned long)obj;
188 restore_context (ctx);
189 g_assert_not_reached ();
194 /* no handler found - we need to call all finally handlers */
195 for (i = 0; i < ji->num_clauses; i++) {
196 MonoJitExceptionInfo *ei = &ji->clauses [i];
198 if (ei->try_start <= ip && ip < (ei->try_end) &&
199 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
200 call_finally (ctx, (unsigned long)ei->handler_start);
205 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
206 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
209 if (!strcmp (strace, "TODO: implement stack traces")){
211 strace = g_strdup ("");
214 tmpsig = mono_signature_get_desc(m->signature, TRUE);
215 tmp = g_strdup_printf ("%sin %04x %s.%s:%s (%s)\n", strace,
216 (char *)ip - (char *)ji->code_start,
217 m->klass->name_space, m->klass->name, m->name, tmpsig);
221 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
225 /* continue unwinding */
228 /* restore caller saved registers */
229 if (ji->used_regs & X86_ESI_MASK) {
230 ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
233 if (ji->used_regs & X86_EDI_MASK) {
234 ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
237 if (ji->used_regs & X86_EBX_MASK) {
238 ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
241 ctx->SC_ESP = ctx->SC_EBP;
242 ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 5;
243 ctx->SC_EBP = *((int *)ctx->SC_EBP);
245 if (ctx->SC_EBP < (unsigned)end_of_stack)
246 arch_handle_exception (ctx, obj);
253 MonoLMF *lmf = jit_tls->lmf;
262 jit_tls->lmf = lmf->previous_lmf;
264 ctx->SC_ESI = lmf->esi;
265 ctx->SC_EDI = lmf->edi;
266 ctx->SC_EBX = lmf->ebx;
267 ctx->SC_EBP = lmf->ebp;
268 ctx->SC_EIP = lmf->eip;
269 ctx->SC_ESP = (unsigned long)&lmf->eip;
271 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
272 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
275 if (!strcmp (strace, "TODO: implement stack traces"))
276 strace = g_strdup ("");
278 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
279 m->klass->name, m->name);
283 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
287 if (ctx->SC_EBP < (unsigned)end_of_stack)
288 arch_handle_exception (ctx, obj);
295 g_assert_not_reached ();
299 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
300 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
301 unsigned long eip, unsigned long esp)
303 struct sigcontext ctx;
305 /* adjust eip so that it point to the call instruction */
318 arch_handle_exception (&ctx, exc);
320 g_assert_not_reached ();
324 * arch_get_throw_exception:
326 * Returns a function pointer which can be used to raise
327 * exceptions. The returned function has the following
328 * signature: void (*func) (MonoException *exc);
329 * For example to raise an arithmetic exception you can use:
331 * x86_push_imm (code, mono_get_exception_arithmetic ());
332 * x86_call_code (code, arch_get_throw_exception ());
336 arch_get_throw_exception (void)
338 static guint8 start [24];
339 static int inited = 0;
348 x86_push_reg (code, X86_ESP);
349 x86_push_membase (code, X86_ESP, 4); /* IP */
350 x86_push_membase (code, X86_ESP, 12); /* exception */
351 x86_push_reg (code, X86_EBP);
352 x86_push_reg (code, X86_EDI);
353 x86_push_reg (code, X86_ESI);
354 x86_push_reg (code, X86_EBX);
355 x86_push_reg (code, X86_EDX);
356 x86_push_reg (code, X86_ECX);
357 x86_push_reg (code, X86_EAX);
358 x86_call_code (code, throw_exception);
359 /* we should never reach this breakpoint */
360 x86_breakpoint (code);
362 g_assert ((code - start) < 24);
367 * arch_get_throw_exception_by_name:
369 * Returns a function pointer which can be used to raise
370 * corlib exceptions. The returned function has the following
371 * signature: void (*func) (char *exc_name);
372 * For example to raise an arithmetic exception you can use:
374 * x86_push_imm (code, "ArithmeticException");
375 * x86_call_code (code, arch_get_throw_exception ());
379 arch_get_throw_exception_by_name ()
381 static guint8 start [32];
382 static int inited = 0;
391 /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
393 x86_push_membase (code, X86_ESP, 4); /* exception name */
394 x86_push_imm (code, "System");
395 x86_push_imm (code, mono_defaults.exception_class->image);
396 x86_call_code (code, mono_exception_from_name);
397 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
398 /* save the newly create object (overwrite exception name)*/
399 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
400 x86_jump_code (code, arch_get_throw_exception ());
402 g_assert ((code - start) < 32);