]> gitweb.factorcode.org Git - factor.git/blob - vm/mach_signal.cpp
Merge branch 'vm_cleanup' of git://github.com/phildawes/factor
[factor.git] / vm / mach_signal.cpp
1 /* Fault handler information.  MacOSX version.
2 Copyright (C) 1993-1999, 2002-2003  Bruno Haible <clisp.org at bruno>
3
4 Copyright (C) 2003  Paolo Bonzini <gnu.org at bonzini>
5
6 Used under BSD license with permission from Paolo Bonzini and Bruno Haible,
7 2005-03-10:
8
9 http://sourceforge.net/mailarchive/message.php?msg_name=200503102200.32002.bruno%40clisp.org
10
11 Modified for Factor by Slava Pestov */
12
13 #include "master.hpp"
14
15 namespace factor
16 {
17
18 /* The exception port on which our thread listens. */
19 mach_port_t our_exception_port;
20
21 /* The following sources were used as a *reference* for this exception handling
22 code:
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 */
27
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 void factor_vm::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)
37 {
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 */
42
43         /* Are we in compiled Factor code? Then use the current stack pointer */
44         if(in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state)))
45                 signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state);
46         /* Are we in C? Then use the saved callstack top */
47         else
48                 signal_callstack_top = NULL;
49
50         MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state));
51
52         /* Now we point the program counter at the right handler function. */
53         if(exception == EXC_BAD_ACCESS)
54         {
55                 signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state);
56                 MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::memory_signal_handler_impl;
57         }
58         else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV)
59         {
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)factor::fp_signal_handler_impl;
63         }
64         else
65         {
66                 signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT);
67                 MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::misc_signal_handler_impl;
68         }
69 }
70
71 static void call_fault_handler(exception_type_t exception,
72                                                            exception_data_type_t code,
73                                                            MACH_EXC_STATE_TYPE *exc_state,
74                                                            MACH_THREAD_STATE_TYPE *thread_state,
75                                                            MACH_FLOAT_STATE_TYPE *float_state)
76 {
77         SIGNAL_VM_PTR()->call_fault_handler(exception,code,exc_state,thread_state,float_state);
78 }
79
80 /* Handle an exception by invoking the user's fault handler and/or forwarding
81 the duty to the previously installed handlers.  */
82 extern "C"
83 kern_return_t
84 catch_exception_raise (mach_port_t exception_port,
85         mach_port_t thread,
86         mach_port_t task,
87         exception_type_t exception,
88         exception_data_t code,
89         mach_msg_type_number_t code_count)
90 {
91         MACH_EXC_STATE_TYPE exc_state;
92         MACH_THREAD_STATE_TYPE thread_state;
93         MACH_FLOAT_STATE_TYPE float_state;
94         mach_msg_type_number_t exc_state_count, thread_state_count, float_state_count;
95
96         /* Get fault information and the faulting thread's register contents..
97         
98         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_get_state.html.  */
99         exc_state_count = MACH_EXC_STATE_COUNT;
100         if (thread_get_state (thread, MACH_EXC_STATE_FLAVOR,
101                               (natural_t *)&exc_state, &exc_state_count)
102                 != KERN_SUCCESS)
103         {
104                 /* The thread is supposed to be suspended while the exception
105                 handler is called. This shouldn't fail. */
106                 return KERN_FAILURE;
107         }
108
109         thread_state_count = MACH_THREAD_STATE_COUNT;
110         if (thread_get_state (thread, MACH_THREAD_STATE_FLAVOR,
111                               (natural_t *)&thread_state, &thread_state_count)
112                 != KERN_SUCCESS)
113         {
114                 /* The thread is supposed to be suspended while the exception
115                 handler is called. This shouldn't fail. */
116                 return KERN_FAILURE;
117         }
118
119         float_state_count = MACH_FLOAT_STATE_COUNT;
120         if (thread_get_state (thread, MACH_FLOAT_STATE_FLAVOR,
121                               (natural_t *)&float_state, &float_state_count)
122                 != KERN_SUCCESS)
123         {
124                 /* The thread is supposed to be suspended while the exception
125                 handler is called. This shouldn't fail. */
126                 return KERN_FAILURE;
127         }
128
129         /* Modify registers so to have the thread resume executing the
130         fault handler */
131         call_fault_handler(exception,code[0],&exc_state,&thread_state,&float_state);
132
133         /* Set the faulting thread's register contents..
134         
135         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_state.html.  */
136         if (thread_set_state (thread, MACH_FLOAT_STATE_FLAVOR,
137                               (natural_t *)&float_state, float_state_count)
138                 != KERN_SUCCESS)
139         {
140                 return KERN_FAILURE;
141         }
142
143         if (thread_set_state (thread, MACH_THREAD_STATE_FLAVOR,
144                               (natural_t *)&thread_state, thread_state_count)
145                 != KERN_SUCCESS)
146         {
147                 return KERN_FAILURE;
148         }
149
150         return KERN_SUCCESS;
151 }
152
153 /* The main function of the thread listening for exceptions.  */
154 static void *
155 mach_exception_thread (void *arg)
156 {
157         for (;;)
158         {
159                 /* These two structures contain some private kernel data. We don't need
160                 to access any of it so we don't bother defining a proper struct. The
161                 correct definitions are in the xnu source code. */
162                 /* Buffer for a message to be received.  */
163                 struct
164                 {
165                         mach_msg_header_t head;
166                         mach_msg_body_t msgh_body;
167                         char data[1024];
168                 }
169                 msg;
170                 /* Buffer for a reply message.  */
171                 struct
172                 {
173                         mach_msg_header_t head;
174                         char data[1024];
175                 }
176                 reply;
177
178                 mach_msg_return_t retval;
179
180                 /* Wait for a message on the exception port.  */
181                 retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0,
182                         sizeof (msg), our_exception_port,
183                         MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
184                 if (retval != MACH_MSG_SUCCESS)
185                 {
186                         abort ();
187                 }
188
189                 /* Handle the message: Call exc_server, which will call
190                 catch_exception_raise and produce a reply message.  */
191                 exc_server (&msg.head, &reply.head);
192
193                 /* Send the reply.  */
194                 if (mach_msg (&reply.head, MACH_SEND_MSG, reply.head.msgh_size,
195                         0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)
196                         != MACH_MSG_SUCCESS)
197                 {
198                         abort ();
199                 }
200         }
201 }
202
203 /* Initialize the Mach exception handler thread. */
204 void mach_initialize ()
205 {
206         mach_port_t self;
207         exception_mask_t mask;
208
209         self = mach_task_self ();
210
211         /* Allocate a port on which the thread shall listen for exceptions.  */
212         if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
213                 != KERN_SUCCESS)
214                 fatal_error("mach_port_allocate() failed",0);
215
216         /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html.  */
217         if (mach_port_insert_right (self, our_exception_port, our_exception_port,
218                 MACH_MSG_TYPE_MAKE_SEND)
219                 != KERN_SUCCESS)
220                 fatal_error("mach_port_insert_right() failed",0);
221
222         /* The exceptions we want to catch. */
223         mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC;
224
225         /* Create the thread listening on the exception port.  */
226         start_thread(mach_exception_thread,NULL);
227
228         /* Replace the exception port info for these exceptions with our own.
229         Note that we replace the exception port for the entire task, not only
230         for a particular thread.  This has the effect that when our exception
231         port gets the message, the thread specific exception port has already
232         been asked, and we don't need to bother about it.
233         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html.  */
234         if (task_set_exception_ports (self, mask, our_exception_port,
235                 EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
236                 != KERN_SUCCESS)
237                 fatal_error("task_set_exception_ports() failed",0);
238 }
239
240 }