]> gitweb.factorcode.org Git - factor.git/blob - vm/mach_signal.cpp
Merge branch 'master' of git://factorcode.org/git/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 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 {
37         /* There is a race condition here, but in practice an exception
38         delivered during stack frame setup/teardown or while transitioning
39         from Factor to C is a sign of things seriously gone wrong, not just
40         a divide by zero or stack underflow in the listener */
41
42         /* Are we in compiled Factor code? Then use the current stack pointer */
43         if(in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state)))
44                 signal_callstack_top = (stack_frame *)MACH_STACK_POINTER(thread_state);
45         /* Are we in C? Then use the saved callstack top */
46         else
47                 signal_callstack_top = NULL;
48
49         MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state));
50
51         /* Now we point the program counter at the right handler function. */
52         if(exception == EXC_BAD_ACCESS)
53         {
54                 signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state);
55                 MACH_PROGRAM_COUNTER(thread_state) = (cell)memory_signal_handler_impl;
56         }
57         else if(exception == EXC_ARITHMETIC && code != MACH_EXC_INTEGER_DIV)
58         {
59                 MACH_PROGRAM_COUNTER(thread_state) = (cell)fp_signal_handler_impl;
60         }
61         else
62         {
63                 signal_number = (exception == EXC_ARITHMETIC ? SIGFPE : SIGABRT);
64                 MACH_PROGRAM_COUNTER(thread_state) = (cell)misc_signal_handler_impl;
65         }
66 }
67
68 /* Handle an exception by invoking the user's fault handler and/or forwarding
69 the duty to the previously installed handlers.  */
70 extern "C"
71 kern_return_t
72 catch_exception_raise (mach_port_t exception_port,
73         mach_port_t thread,
74         mach_port_t task,
75         exception_type_t exception,
76         exception_data_t code,
77         mach_msg_type_number_t code_count)
78 {
79         MACH_EXC_STATE_TYPE exc_state;
80         MACH_THREAD_STATE_TYPE thread_state;
81         mach_msg_type_number_t state_count;
82
83         /* Get fault information and the faulting thread's register contents..
84         
85         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_get_state.html.  */
86         state_count = MACH_EXC_STATE_COUNT;
87         if (thread_get_state (thread, MACH_EXC_STATE_FLAVOR,
88                               (natural_t *)&exc_state, &state_count)
89                 != KERN_SUCCESS)
90         {
91                 /* The thread is supposed to be suspended while the exception
92                 handler is called. This shouldn't fail. */
93                 return KERN_FAILURE;
94         }
95
96         state_count = MACH_THREAD_STATE_COUNT;
97         if (thread_get_state (thread, MACH_THREAD_STATE_FLAVOR,
98                               (natural_t *)&thread_state, &state_count)
99                 != KERN_SUCCESS)
100         {
101                 /* The thread is supposed to be suspended while the exception
102                 handler is called. This shouldn't fail. */
103                 return KERN_FAILURE;
104         }
105
106         /* Modify registers so to have the thread resume executing the
107         fault handler */
108         call_fault_handler(exception,code[0],&exc_state,&thread_state);
109
110         /* Set the faulting thread's register contents..
111         
112         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_state.html.  */
113         if (thread_set_state (thread, MACH_THREAD_STATE_FLAVOR,
114                               (natural_t *)&thread_state, state_count)
115                 != KERN_SUCCESS)
116         {
117                 return KERN_FAILURE;
118         }
119
120         return KERN_SUCCESS;
121 }
122
123
124 /* The main function of the thread listening for exceptions.  */
125 static void *
126 mach_exception_thread (void *arg)
127 {
128         for (;;)
129         {
130                 /* These two structures contain some private kernel data. We don't need
131                 to access any of it so we don't bother defining a proper struct. The
132                 correct definitions are in the xnu source code. */
133                 /* Buffer for a message to be received.  */
134                 struct
135                 {
136                         mach_msg_header_t head;
137                         mach_msg_body_t msgh_body;
138                         char data[1024];
139                 }
140                 msg;
141                 /* Buffer for a reply message.  */
142                 struct
143                 {
144                         mach_msg_header_t head;
145                         char data[1024];
146                 }
147                 reply;
148
149                 mach_msg_return_t retval;
150
151                 /* Wait for a message on the exception port.  */
152                 retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0,
153                         sizeof (msg), our_exception_port,
154                         MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
155                 if (retval != MACH_MSG_SUCCESS)
156                 {
157                         abort ();
158                 }
159
160                 /* Handle the message: Call exc_server, which will call
161                 catch_exception_raise and produce a reply message.  */
162                 exc_server (&msg.head, &reply.head);
163
164                 /* Send the reply.  */
165                 if (mach_msg (&reply.head, MACH_SEND_MSG, reply.head.msgh_size,
166                         0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)
167                         != MACH_MSG_SUCCESS)
168                 {
169                         abort ();
170                 }
171         }
172 }
173
174 /* Initialize the Mach exception handler thread. */
175 void mach_initialize ()
176 {
177         mach_port_t self;
178         exception_mask_t mask;
179
180         self = mach_task_self ();
181
182         /* Allocate a port on which the thread shall listen for exceptions.  */
183         if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
184                 != KERN_SUCCESS)
185                 fatal_error("mach_port_allocate() failed",0);
186
187         /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html.  */
188         if (mach_port_insert_right (self, our_exception_port, our_exception_port,
189                 MACH_MSG_TYPE_MAKE_SEND)
190                 != KERN_SUCCESS)
191                 fatal_error("mach_port_insert_right() failed",0);
192
193         /* The exceptions we want to catch. */
194         mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC;
195
196         /* Create the thread listening on the exception port.  */
197         start_thread(mach_exception_thread);
198
199         /* Replace the exception port info for these exceptions with our own.
200         Note that we replace the exception port for the entire task, not only
201         for a particular thread.  This has the effect that when our exception
202         port gets the message, the thread specific exception port has already
203         been asked, and we don't need to bother about it.
204         See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html.  */
205         if (task_set_exception_ports (self, mask, our_exception_port,
206                 EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
207                 != KERN_SUCCESS)
208                 fatal_error("task_set_exception_ports() failed",0);
209 }
210
211 }