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