2 * This is the Darwin incarnation of OS-dependent routines. See also
7 * This software is part of the SBCL system. See the README file for
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
23 #include <mach-o/dyld.h>
27 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
28 #include <mach/mach.h>
32 os_get_runtime_executable_path(int external)
34 char path[PATH_MAX + 1];
35 uint32_t size = sizeof(path);
37 if (_NSGetExecutablePath(path, &size) == -1)
42 return copied_string(path);
45 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
47 /* exc_server handles mach exception messages from the kernel and
48 * calls catch exception raise. We use the system-provided
49 * mach_msg_server, which, I assume, calls exc_server in a loop.
52 extern boolean_t exc_server();
55 mach_exception_handler(void *port)
57 mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
58 /* mach_msg_server should never return, but it should dispatch mach
59 * exceptions to our catch_exception_raise function
61 lose("mach_msg_server returned");
64 /* Sets up the thread that will listen for mach exceptions. note that
65 the exception handlers will be run on this thread. This is
66 different from the BSD-style signal handling situation in which the
67 signal handlers run in the relevant thread directly. */
69 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
72 setup_mach_exception_handling_thread()
75 pthread_t mach_exception_handling_thread = NULL;
78 /* allocate a mach_port for this process */
79 ret = mach_port_allocate(mach_task_self(),
80 MACH_PORT_RIGHT_PORT_SET,
81 &mach_exception_handler_port_set);
83 /* create the thread that will receive the mach exceptions */
85 FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
87 pthread_attr_init(&attr);
88 pthread_create(&mach_exception_handling_thread,
90 mach_exception_handler,
91 (void*) mach_exception_handler_port_set);
92 pthread_attr_destroy(&attr);
94 return mach_exception_handling_thread;
97 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
98 exception port (which is being listened to do by the mach
99 exception handling thread). */
101 mach_thread_init(mach_port_t thread_exception_port)
104 /* allocate a named port for the thread */
106 FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
108 ret = mach_port_allocate_name(mach_task_self(),
109 MACH_PORT_RIGHT_RECEIVE,
110 thread_exception_port);
112 lose("mach_port_allocate_name failed with return_code %d\n", ret);
115 /* establish the right for the thread_exception_port to send messages */
116 ret = mach_port_insert_right(mach_task_self(),
117 thread_exception_port,
118 thread_exception_port,
119 MACH_MSG_TYPE_MAKE_SEND);
121 lose("mach_port_insert_right failed with return_code %d\n", ret);
124 ret = thread_set_exception_ports(mach_thread_self(),
125 EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
126 thread_exception_port,
130 lose("thread_set_exception_port failed with return_code %d\n", ret);
133 ret = mach_port_move_member(mach_task_self(),
134 thread_exception_port,
135 mach_exception_handler_port_set);
137 lose("mach_port_ failed with return_code %d\n", ret);
144 setup_mach_exceptions() {
145 setup_mach_exception_handling_thread();
146 mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
153 setup_mach_exceptions();