1 /* Fault handler information. MacOSX version.
2 Copyright (C) 1993-1999, 2002-2003 Bruno Haible <clisp.org at bruno>
4 Copyright (C) 2003 Paolo Bonzini <gnu.org at bonzini>
6 Used under BSD license with permission from Paolo Bonzini and Bruno Haible,
9 http://sourceforge.net/mailarchive/message.php?msg_name=200503102200.32002.bruno%40clisp.org
11 Modified for Factor by Slava Pestov */
18 /* The exception port on which our thread listens. */
19 mach_port_t our_exception_port;
21 /* The following sources were used as a *reference* for this exception handling
23 1. Apple's mach/xnu documentation
24 2. Timothy J. Wood's "Mach Exception Handlers 101" post to the
25 omnigroup's macosx-dev list.
26 http://www.wodeveloper.com/omniLists/macosx-dev/2000/June/msg00137.html */
28 /* Modify a suspended thread's thread_state so that when the thread resumes
29 executing, the call frame of the current C primitive (if any) is rewound, and
30 the appropriate Factor error is thrown from the top-most Factor frame. */
31 static void call_fault_handler(
32 exception_type_t exception,
33 exception_data_type_t code,
34 MACH_EXC_STATE_TYPE *exc_state,
35 MACH_THREAD_STATE_TYPE *thread_state,
36 MACH_FLOAT_STATE_TYPE *float_state)
38 /* There is a race condition here, but in practice an exception
39 delivered during stack frame setup/teardown or while transitioning
40 from Factor to C is a sign of things seriously gone wrong, not just
41 a divide by zero or stack underflow in the listener */
43 /* Are we in compiled Factor code? Then use the current stack pointer */
44 if(vm->in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state)))
45 vm->signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state);
46 /* Are we in C? Then use the saved callstack top */
48 vm->signal_callstack_top = NULL;
50 MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state));
52 /* Now we point the program counter at the right handler function. */
53 if(exception == EXC_BAD_ACCESS)
55 vm->signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state);
56 MACH_PROGRAM_COUNTER(thread_state) = (cell)memory_signal_handler_impl;
58 else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV)
60 signal_fpu_status = fpu_status(mach_fpu_status(float_state));
61 mach_clear_fpu_status(float_state);
62 MACH_PROGRAM_COUNTER(thread_state) = (cell)fp_signal_handler_impl;
66 vm->signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT);
67 MACH_PROGRAM_COUNTER(thread_state) = (cell)misc_signal_handler_impl;
71 /* Handle an exception by invoking the user's fault handler and/or forwarding
72 the duty to the previously installed handlers. */
75 catch_exception_raise (mach_port_t exception_port,
78 exception_type_t exception,
79 exception_data_t code,
80 mach_msg_type_number_t code_count)
82 MACH_EXC_STATE_TYPE exc_state;
83 MACH_THREAD_STATE_TYPE thread_state;
84 MACH_FLOAT_STATE_TYPE float_state;
85 mach_msg_type_number_t exc_state_count, thread_state_count, float_state_count;
87 /* Get fault information and the faulting thread's register contents..
89 See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_get_state.html. */
90 exc_state_count = MACH_EXC_STATE_COUNT;
91 if (thread_get_state (thread, MACH_EXC_STATE_FLAVOR,
92 (natural_t *)&exc_state, &exc_state_count)
95 /* The thread is supposed to be suspended while the exception
96 handler is called. This shouldn't fail. */
100 thread_state_count = MACH_THREAD_STATE_COUNT;
101 if (thread_get_state (thread, MACH_THREAD_STATE_FLAVOR,
102 (natural_t *)&thread_state, &thread_state_count)
105 /* The thread is supposed to be suspended while the exception
106 handler is called. This shouldn't fail. */
110 float_state_count = MACH_FLOAT_STATE_COUNT;
111 if (thread_get_state (thread, MACH_FLOAT_STATE_FLAVOR,
112 (natural_t *)&float_state, &float_state_count)
115 /* The thread is supposed to be suspended while the exception
116 handler is called. This shouldn't fail. */
120 /* Modify registers so to have the thread resume executing the
122 call_fault_handler(exception,code[0],&exc_state,&thread_state,&float_state);
124 /* Set the faulting thread's register contents..
126 See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_state.html. */
127 if (thread_set_state (thread, MACH_FLOAT_STATE_FLAVOR,
128 (natural_t *)&float_state, float_state_count)
134 if (thread_set_state (thread, MACH_THREAD_STATE_FLAVOR,
135 (natural_t *)&thread_state, thread_state_count)
145 /* The main function of the thread listening for exceptions. */
147 mach_exception_thread (void *arg)
151 /* These two structures contain some private kernel data. We don't need
152 to access any of it so we don't bother defining a proper struct. The
153 correct definitions are in the xnu source code. */
154 /* Buffer for a message to be received. */
157 mach_msg_header_t head;
158 mach_msg_body_t msgh_body;
162 /* Buffer for a reply message. */
165 mach_msg_header_t head;
170 mach_msg_return_t retval;
172 /* Wait for a message on the exception port. */
173 retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0,
174 sizeof (msg), our_exception_port,
175 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
176 if (retval != MACH_MSG_SUCCESS)
181 /* Handle the message: Call exc_server, which will call
182 catch_exception_raise and produce a reply message. */
183 exc_server (&msg.head, &reply.head);
185 /* Send the reply. */
186 if (mach_msg (&reply.head, MACH_SEND_MSG, reply.head.msgh_size,
187 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)
195 /* Initialize the Mach exception handler thread. */
196 void mach_initialize ()
199 exception_mask_t mask;
201 self = mach_task_self ();
203 /* Allocate a port on which the thread shall listen for exceptions. */
204 if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
206 vm->fatal_error("mach_port_allocate() failed",0);
208 /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html. */
209 if (mach_port_insert_right (self, our_exception_port, our_exception_port,
210 MACH_MSG_TYPE_MAKE_SEND)
212 vm->fatal_error("mach_port_insert_right() failed",0);
214 /* The exceptions we want to catch. */
215 mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC;
217 /* Create the thread listening on the exception port. */
218 start_thread(mach_exception_thread,NULL);
220 /* Replace the exception port info for these exceptions with our own.
221 Note that we replace the exception port for the entire task, not only
222 for a particular thread. This has the effect that when our exception
223 port gets the message, the thread specific exception port has already
224 been asked, and we don't need to bother about it.
225 See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html. */
226 if (task_set_exception_ports (self, mask, our_exception_port,
227 EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
229 vm->fatal_error("task_set_exception_ports() failed",0);