remove math.blas.syntax and merge parsing words into math.blas.vectors/matrices
[factor/jcg.git] / vm / mach_signal.c
blob57fb91d662fa768cff93acf14eb784653b845d9f
1 /* Fault handler information. MacOSX version.
2 Copyright (C) 1993-1999, 2002-2003 Bruno Haible <clisp.org at bruno>
3 Copyright (C) 2003 Paolo Bonzini <gnu.org at bonzini>
5 Used under BSD license with permission from Paolo Bonzini and Bruno Haible,
6 2005-03-10:
8 http://sourceforge.net/mailarchive/message.php?msg_name=200503102200.32002.bruno%40clisp.org
10 Modified for Factor by Slava Pestov */
12 #include "master.h"
14 /* The following sources were used as a *reference* for this exception handling
15 code:
16 1. Apple's mach/xnu documentation
17 2. Timothy J. Wood's "Mach Exception Handlers 101" post to the
18 omnigroup's macosx-dev list.
19 http://www.wodeveloper.com/omniLists/macosx-dev/2000/June/msg00137.html */
21 /* Modify a suspended thread's thread_state so that when the thread resumes
22 executing, the call frame of the current C primitive (if any) is rewound, and
23 the appropriate Factor error is thrown from the top-most Factor frame. */
24 static void call_fault_handler(exception_type_t exception,
25 MACH_EXC_STATE_TYPE *exc_state,
26 MACH_THREAD_STATE_TYPE *thread_state)
28 /* There is a race condition here, but in practice an exception
29 delivered during stack frame setup/teardown or while transitioning
30 from Factor to C is a sign of things seriously gone wrong, not just
31 a divide by zero or stack underflow in the listener */
33 /* Are we in compiled Factor code? Then use the current stack pointer */
34 if(in_code_heap_p(MACH_PROGRAM_COUNTER(thread_state)))
35 signal_callstack_top = (void *)MACH_STACK_POINTER(thread_state);
36 /* Are we in C? Then use the saved callstack top */
37 else
38 signal_callstack_top = NULL;
40 MACH_STACK_POINTER(thread_state) = fix_stack_pointer(MACH_STACK_POINTER(thread_state));
42 /* Now we point the program counter at the right handler function. */
43 if(exception == EXC_BAD_ACCESS)
45 signal_fault_addr = MACH_EXC_STATE_FAULT(exc_state);
46 MACH_PROGRAM_COUNTER(thread_state) = (CELL)memory_signal_handler_impl;
48 else
50 if(exception == EXC_ARITHMETIC)
51 signal_number = SIGFPE;
52 else
53 signal_number = SIGABRT;
54 MACH_PROGRAM_COUNTER(thread_state) = (CELL)misc_signal_handler_impl;
58 /* Handle an exception by invoking the user's fault handler and/or forwarding
59 the duty to the previously installed handlers. */
60 kern_return_t
61 catch_exception_raise (mach_port_t exception_port,
62 mach_port_t thread,
63 mach_port_t task,
64 exception_type_t exception,
65 exception_data_t code,
66 mach_msg_type_number_t code_count)
68 MACH_EXC_STATE_TYPE exc_state;
69 MACH_THREAD_STATE_TYPE thread_state;
70 mach_msg_type_number_t state_count;
72 /* Get fault information and the faulting thread's register contents..
74 See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_get_state.html. */
75 state_count = MACH_EXC_STATE_COUNT;
76 if (thread_get_state (thread, MACH_EXC_STATE_FLAVOR,
77 (void *) &exc_state, &state_count)
78 != KERN_SUCCESS)
80 /* The thread is supposed to be suspended while the exception
81 handler is called. This shouldn't fail. */
82 return KERN_FAILURE;
85 state_count = MACH_THREAD_STATE_COUNT;
86 if (thread_get_state (thread, MACH_THREAD_STATE_FLAVOR,
87 (void *) &thread_state, &state_count)
88 != KERN_SUCCESS)
90 /* The thread is supposed to be suspended while the exception
91 handler is called. This shouldn't fail. */
92 return KERN_FAILURE;
95 /* Modify registers so to have the thread resume executing the
96 fault handler */
97 call_fault_handler(exception,&exc_state,&thread_state);
99 /* Set the faulting thread's register contents..
101 See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_state.html. */
102 if (thread_set_state (thread, MACH_THREAD_STATE_FLAVOR,
103 (void *) &thread_state, state_count)
104 != KERN_SUCCESS)
106 return KERN_FAILURE;
109 return KERN_SUCCESS;
113 /* The main function of the thread listening for exceptions. */
114 static void *
115 mach_exception_thread (void *arg)
117 for (;;)
119 /* These two structures contain some private kernel data. We don't need
120 to access any of it so we don't bother defining a proper struct. The
121 correct definitions are in the xnu source code. */
122 /* Buffer for a message to be received. */
123 struct
125 mach_msg_header_t head;
126 mach_msg_body_t msgh_body;
127 char data[1024];
129 msg;
130 /* Buffer for a reply message. */
131 struct
133 mach_msg_header_t head;
134 char data[1024];
136 reply;
138 mach_msg_return_t retval;
140 /* Wait for a message on the exception port. */
141 retval = mach_msg (&msg.head, MACH_RCV_MSG | MACH_RCV_LARGE, 0,
142 sizeof (msg), our_exception_port,
143 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
144 if (retval != MACH_MSG_SUCCESS)
146 abort ();
149 /* Handle the message: Call exc_server, which will call
150 catch_exception_raise and produce a reply message. */
151 exc_server (&msg.head, &reply.head);
153 /* Send the reply. */
154 if (mach_msg (&reply.head, MACH_SEND_MSG, reply.head.msgh_size,
155 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL)
156 != MACH_MSG_SUCCESS)
158 abort ();
164 /* Initialize the Mach exception handler thread. */
165 void mach_initialize (void)
167 mach_port_t self;
168 exception_mask_t mask;
170 self = mach_task_self ();
172 /* Allocate a port on which the thread shall listen for exceptions. */
173 if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
174 != KERN_SUCCESS)
175 fatal_error("mach_port_allocate() failed",0);
177 /* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html. */
178 if (mach_port_insert_right (self, our_exception_port, our_exception_port,
179 MACH_MSG_TYPE_MAKE_SEND)
180 != KERN_SUCCESS)
181 fatal_error("mach_port_insert_right() failed",0);
183 /* The exceptions we want to catch. */
184 mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC;
186 /* Create the thread listening on the exception port. */
187 start_thread(mach_exception_thread);
189 /* Replace the exception port info for these exceptions with our own.
190 Note that we replace the exception port for the entire task, not only
191 for a particular thread. This has the effect that when our exception
192 port gets the message, the thread specific exception port has already
193 been asked, and we don't need to bother about it.
194 See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html. */
195 if (task_set_exception_ports (self, mask, our_exception_port,
196 EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
197 != KERN_SUCCESS)
198 fatal_error("task_set_exception_ports() failed",0);