2d79d0664c639a0e13a60a7400fd3e7b03adb543
[sbcl.git] / src / runtime / darwin-os.c
1 /*
2  * This is the Darwin incarnation of OS-dependent routines. See also
3  * "bsd-os.c".
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
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.
15  */
16
17 #include "thread.h"
18 #include "sbcl.h"
19 #include "globals.h"
20 #include "runtime.h"
21 #include <signal.h>
22 #include <limits.h>
23 #include <mach-o/dyld.h>
24 #include <errno.h>
25 #include <dlfcn.h>
26
27 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
28 #include <mach/mach.h>
29 #endif
30
31 char *
32 os_get_runtime_executable_path(int external)
33 {
34     char path[PATH_MAX + 1];
35     uint32_t size = sizeof(path);
36
37     if (_NSGetExecutablePath(path, &size) == -1)
38         return NULL;
39     else
40         path[size] = '\0';
41
42     return copied_string(path);
43 }
44
45 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
46
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.
50  *
51  */
52 extern boolean_t exc_server();
53
54 void *
55 mach_exception_handler(void *port)
56 {
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
60    */
61   lose("mach_msg_server returned");
62 }
63
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. */
68
69 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
70 mach_port_t current_mach_task = MACH_PORT_NULL;
71
72 pthread_t
73 setup_mach_exception_handling_thread()
74 {
75     kern_return_t ret;
76     pthread_t mach_exception_handling_thread = NULL;
77     pthread_attr_t attr;
78
79     current_mach_task = mach_task_self();
80
81     /* allocate a mach_port for this process */
82     ret = mach_port_allocate(current_mach_task,
83                              MACH_PORT_RIGHT_PORT_SET,
84                              &mach_exception_handler_port_set);
85
86     /* create the thread that will receive the mach exceptions */
87
88     FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
89
90     pthread_attr_init(&attr);
91     pthread_create(&mach_exception_handling_thread,
92                    &attr,
93                    mach_exception_handler,
94                    (void*) mach_exception_handler_port_set);
95     pthread_attr_destroy(&attr);
96
97     return mach_exception_handling_thread;
98 }
99
100 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
101    exception port (which is being listened to do by the mach
102    exception handling thread). */
103 kern_return_t
104 mach_thread_init(mach_port_t thread_exception_port)
105 {
106     kern_return_t ret;
107     mach_port_t current_mach_thread;
108
109     /* allocate a named port for the thread */
110     FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
111     ret = mach_port_allocate_name(current_mach_task,
112                                   MACH_PORT_RIGHT_RECEIVE,
113                                   thread_exception_port);
114     if (ret) {
115         lose("mach_port_allocate_name failed with return_code %d\n", ret);
116     }
117
118     /* establish the right for the thread_exception_port to send messages */
119     ret = mach_port_insert_right(current_mach_task,
120                                  thread_exception_port,
121                                  thread_exception_port,
122                                  MACH_MSG_TYPE_MAKE_SEND);
123     if (ret) {
124         lose("mach_port_insert_right failed with return_code %d\n", ret);
125     }
126
127     current_mach_thread = mach_thread_self();
128     ret = thread_set_exception_ports(current_mach_thread,
129                                      EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
130                                      thread_exception_port,
131                                      EXCEPTION_DEFAULT,
132                                      THREAD_STATE_NONE);
133     if (ret) {
134         lose("thread_set_exception_ports failed with return_code %d\n", ret);
135     }
136
137     ret = mach_port_deallocate (current_mach_task, current_mach_thread);
138     if (ret) {
139         lose("mach_port_deallocate failed with return_code %d\n", ret);
140     }
141
142     ret = mach_port_move_member(current_mach_task,
143                                 thread_exception_port,
144                                 mach_exception_handler_port_set);
145     if (ret) {
146         lose("mach_port_move_member failed with return_code %d\n", ret);
147     }
148
149     return ret;
150 }
151
152 void
153 setup_mach_exceptions() {
154     setup_mach_exception_handling_thread();
155     mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
156 }
157
158 pid_t
159 mach_fork() {
160     pid_t pid = fork();
161     if (pid == 0) {
162         setup_mach_exceptions();
163         return pid;
164     } else {
165         return pid;
166     }
167 }
168
169 #endif
170