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