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 */
227 /* restore caller saved registers */
228 if (ji->used_regs & X86_ESI_MASK) {
229 ctx->SC_ESI = *((int *)ctx->SC_EBP + offset);
232 if (ji->used_regs & X86_EDI_MASK) {
233 ctx->SC_EDI = *((int *)ctx->SC_EBP + offset);
236 if (ji->used_regs & X86_EBX_MASK) {
237 ctx->SC_EBX = *((int *)ctx->SC_EBP + offset);
240 ctx->SC_ESP = ctx->SC_EBP;
241 ctx->SC_EIP = *((int *)ctx->SC_EBP + 1) - 5;
242 ctx->SC_EBP = *((int *)ctx->SC_EBP);
244 if (ctx->SC_EBP < (unsigned)end_of_stack)
245 arch_handle_exception (ctx, obj);
252 MonoLMF *lmf = jit_tls->lmf;
261 jit_tls->lmf = lmf->previous_lmf;
263 ctx->SC_ESI = lmf->esi;
264 ctx->SC_EDI = lmf->edi;
265 ctx->SC_EBX = lmf->ebx;
266 ctx->SC_EBP = lmf->ebp;
267 ctx->SC_EIP = lmf->eip;
268 ctx->SC_ESP = (unsigned long)&lmf->eip;
270 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
271 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
274 if (!strcmp (strace, "TODO: implement stack traces"))
275 strace = g_strdup ("");
277 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
278 m->klass->name, m->name);
282 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
286 if (ctx->SC_EBP < (unsigned)end_of_stack)
287 arch_handle_exception (ctx, obj);
294 g_assert_not_reached ();
298 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
299 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
300 unsigned long eip, unsigned long esp)
302 struct sigcontext ctx;
304 /* adjust eip so that it point to the call instruction */
317 arch_handle_exception (&ctx, exc);
319 g_assert_not_reached ();
323 * arch_get_throw_exception:
325 * Returns a function pointer which can be used to raise
326 * exceptions. The returned function has the following
327 * signature: void (*func) (MonoException *exc);
328 * For example to raise an arithmetic exception you can use:
330 * x86_push_imm (code, mono_get_exception_arithmetic ());
331 * x86_call_code (code, arch_get_throw_exception ());
335 arch_get_throw_exception (void)
337 static guint8 start [24];
338 static int inited = 0;
347 x86_push_reg (code, X86_ESP);
348 x86_push_membase (code, X86_ESP, 4); /* IP */
349 x86_push_membase (code, X86_ESP, 12); /* exception */
350 x86_push_reg (code, X86_EBP);
351 x86_push_reg (code, X86_EDI);
352 x86_push_reg (code, X86_ESI);
353 x86_push_reg (code, X86_EBX);
354 x86_push_reg (code, X86_EDX);
355 x86_push_reg (code, X86_ECX);
356 x86_push_reg (code, X86_EAX);
357 x86_call_code (code, throw_exception);
358 /* we should never reach this breakpoint */
359 x86_breakpoint (code);
361 g_assert ((code - start) < 24);
366 * arch_get_throw_exception_by_name:
368 * Returns a function pointer which can be used to raise
369 * corlib exceptions. The returned function has the following
370 * signature: void (*func) (char *exc_name);
371 * For example to raise an arithmetic exception you can use:
373 * x86_push_imm (code, "ArithmeticException");
374 * x86_call_code (code, arch_get_throw_exception ());
378 arch_get_throw_exception_by_name ()
380 static guint8 start [32];
381 static int inited = 0;
390 /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
392 x86_push_membase (code, X86_ESP, 4); /* exception name */
393 x86_push_imm (code, "System");
394 x86_push_imm (code, mono_defaults.exception_class->image);
395 x86_call_code (code, mono_exception_from_name);
396 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
397 /* save the newly create object (overwrite exception name)*/
398 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
399 x86_jump_code (code, arch_get_throw_exception ());
401 g_assert ((code - start) < 32);