]> gitweb.factorcode.org Git - factor.git/blob - vm/mach_signal.cpp
vm: clean up signal handler dispatch
[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         cell offset = MACH_STACK_POINTER(thread_state) % 16;
39         if (offset != 0)
40                 fatal_error("fault in unaligned frame with offset", offset);
41         MACH_STACK_POINTER(thread_state) -= sizeof(cell);
42         *(cell*)MACH_STACK_POINTER(thread_state) = MACH_PROGRAM_COUNTER(thread_state);
43
44         ctx->callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state);
45
46         /* Now we point the program counter at the right handler function. */
47         if(exception == EXC_BAD_ACCESS)
48         {
49                 signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state);
50                 MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::memory_signal_handler_impl;
51         }
52         else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV)
53         {
54                 signal_fpu_status = fpu_status(mach_fpu_status(float_state));
55                 mach_clear_fpu_status(float_state);
56                 MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::fp_signal_handler_impl;
57         }
58         else
59         {
60                 switch(exception)
61                 {
62                 case EXC_ARITHMETIC: signal_number = SIGFPE; break;
63                 case EXC_BAD_INSTRUCTION: signal_number = SIGILL; break;
64                 default: signal_number = SIGABRT; break;
65                 }
66
67                 MACH_PROGRAM_COUNTER(thread_state) = (cell)factor::synchronous_signal_handler_impl;
68         }
69 }
70
71 static void call_fault_handler(
72         mach_port_t thread,
73         exception_type_t exception,
74         exception_data_type_t code,
75         MACH_EXC_STATE_TYPE *exc_state,
76         MACH_THREAD_STATE_TYPE *thread_state,
77         MACH_FLOAT_STATE_TYPE *float_state)
78 {
79         /* Look up the VM instance involved */
80         THREADHANDLE thread_id = pthread_from_mach_thread_np(thread);
81         assert(thread_id);
82         std::map<THREADHANDLE, factor_vm*>::const_iterator vm = thread_vms.find(thread_id);
83
84         /* Handle the exception */
85         if (vm != thread_vms.end())
86                 vm->second->call_fault_handler(exception,code,exc_state,thread_state,float_state);
87 }
88
89 /* Handle an exception by invoking the user's fault handler and/or forwarding
90 the duty to the previously installed handlers.  */
91 extern "C"
92 kern_return_t
93 catch_exception_raise (mach_port_t exception_port,
94         mach_port_t thread,
95         mach_port_t task,
96         exception_type_t exception,
97         exception_data_t code,
98         mach_msg_type_number_t code_count)
99 {
100         /* 10.6 likes to report exceptions from child processes too. Ignore those */
101         if(task != mach_task_self()) return KERN_FAILURE;
102
103         /* Get fault information and the faulting thread's register contents..
104         
105         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_get_state.html.  */
106         MACH_EXC_STATE_TYPE exc_state;
107         mach_msg_type_number_t exc_state_count = MACH_EXC_STATE_COUNT;
108         if (thread_get_state (thread, MACH_EXC_STATE_FLAVOR,
109                               (natural_t *)&exc_state, &exc_state_count)
110                 != KERN_SUCCESS)
111         {
112                 /* The thread is supposed to be suspended while the exception
113                 handler is called. This shouldn't fail. */
114                 return KERN_FAILURE;
115         }
116
117         MACH_THREAD_STATE_TYPE thread_state;
118         mach_msg_type_number_t thread_state_count = MACH_THREAD_STATE_COUNT;
119         if (thread_get_state (thread, MACH_THREAD_STATE_FLAVOR,
120                               (natural_t *)&thread_state, &thread_state_count)
121                 != KERN_SUCCESS)
122         {
123                 /* The thread is supposed to be suspended while the exception
124                 handler is called. This shouldn't fail. */
125                 return KERN_FAILURE;
126         }
127
128         MACH_FLOAT_STATE_TYPE float_state;
129         mach_msg_type_number_t float_state_count = MACH_FLOAT_STATE_COUNT;
130         if (thread_get_state (thread, MACH_FLOAT_STATE_FLAVOR,
131                               (natural_t *)&float_state, &float_state_count)
132                 != KERN_SUCCESS)
133         {
134                 /* The thread is supposed to be suspended while the exception
135                 handler is called. This shouldn't fail. */
136                 return KERN_FAILURE;
137         }
138
139         /* Modify registers so to have the thread resume executing the
140         fault handler */
141         call_fault_handler(thread,exception,code[0],&exc_state,&thread_state,&float_state);
142
143         /* Set the faulting thread's register contents..
144         
145         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_state.html.  */
146         if (thread_set_state (thread, MACH_FLOAT_STATE_FLAVOR,
147                               (natural_t *)&float_state, float_state_count)
148                 != KERN_SUCCESS)
149         {
150                 return KERN_FAILURE;
151         }
152
153         if (thread_set_state (thread, MACH_THREAD_STATE_FLAVOR,
154                               (natural_t *)&thread_state, thread_state_count)
155                 != KERN_SUCCESS)
156         {
157                 return KERN_FAILURE;
158         }
159
160         return KERN_SUCCESS;
161 }
162
163 /* The main function of the thread listening for exceptions.  */
164 static void *
165 mach_exception_thread (void *arg)
166 {
167         for (;;)
168         {
169                 /* These two structures contain some private kernel data. We don't need
170                 to access any of it so we don't bother defining a proper struct. The
171                 correct definitions are in the xnu source code. */
172                 /* Buffer for a message to be received.  */
173                 struct
174                 {
175                         mach_msg_header_t head;
176                         mach_msg_body_t msgh_body;
177                         char data[1024];
178                 }
179                 msg;
180                 /* Buffer for a reply message.  */
181                 struct
182                 {
183                         mach_msg_header_t head;
184                         char data[1024];
185                 }
186                 reply;
187
188                 mach_msg_return_t retval;
189
190                 /* Wait for a message on the exception port.  */
191                 retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0,
192                         sizeof (msg), our_exception_port,
193                         MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
194                 if (retval != MACH_MSG_SUCCESS)
195                 {
196                         abort ();
197                 }
198
199                 /* Handle the message: Call exc_server, which will call
200                 catch_exception_raise and produce a reply message.  */
201                 exc_server (&msg.head, &reply.head);
202
203                 /* Send the reply.  */
204                 if (mach_msg (&reply.head, MACH_SEND_MSG, reply.head.msgh_size,
205                         0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)
206                         != MACH_MSG_SUCCESS)
207                 {
208                         abort ();
209                 }
210         }
211 }
212
213 /* Initialize the Mach exception handler thread. */
214 void mach_initialize ()
215 {
216         mach_port_t self;
217         exception_mask_t mask;
218
219         self = mach_task_self ();
220
221         /* Allocate a port on which the thread shall listen for exceptions.  */
222         if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
223                 != KERN_SUCCESS)
224                 fatal_error("mach_port_allocate() failed",0);
225
226         /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html.  */
227         if (mach_port_insert_right (self, our_exception_port, our_exception_port,
228                 MACH_MSG_TYPE_MAKE_SEND)
229                 != KERN_SUCCESS)
230                 fatal_error("mach_port_insert_right() failed",0);
231
232         /* The exceptions we want to catch. */
233         mask = EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | EXC_MASK_ARITHMETIC;
234
235         /* Create the thread listening on the exception port.  */
236         start_thread(mach_exception_thread,NULL);
237
238         /* Replace the exception port info for these exceptions with our own.
239         Note that we replace the exception port for the entire task, not only
240         for a particular thread.  This has the effect that when our exception
241         port gets the message, the thread specific exception port has already
242         been asked, and we don't need to bother about it.
243         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html.  */
244         if (task_set_exception_ports (self, mask, our_exception_port,
245                 EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
246                 != KERN_SUCCESS)
247                 fatal_error("task_set_exception_ports() failed",0);
248 }
249
250 }