1.0.46.25: consolidate common code from x86-64-darwin-os.c and x86-darwin-os.c into...
[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
71 pthread_t
72 setup_mach_exception_handling_thread()
73 {
74     kern_return_t ret;
75     pthread_t mach_exception_handling_thread = NULL;
76     pthread_attr_t attr;
77
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);
82
83     /* create the thread that will receive the mach exceptions */
84
85     FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
86
87     pthread_attr_init(&attr);
88     pthread_create(&mach_exception_handling_thread,
89                    &attr,
90                    mach_exception_handler,
91                    (void*) mach_exception_handler_port_set);
92     pthread_attr_destroy(&attr);
93
94     return mach_exception_handling_thread;
95 }
96
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). */
100 kern_return_t
101 mach_thread_init(mach_port_t thread_exception_port)
102 {
103     kern_return_t ret;
104     /* allocate a named port for the thread */
105
106     FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
107
108     ret = mach_port_allocate_name(mach_task_self(),
109                                   MACH_PORT_RIGHT_RECEIVE,
110                                   thread_exception_port);
111     if (ret) {
112         lose("mach_port_allocate_name failed with return_code %d\n", ret);
113     }
114
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);
120     if (ret) {
121         lose("mach_port_insert_right failed with return_code %d\n", ret);
122     }
123
124     ret = thread_set_exception_ports(mach_thread_self(),
125                                      EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
126                                      thread_exception_port,
127                                      EXCEPTION_DEFAULT,
128                                      THREAD_STATE_NONE);
129     if (ret) {
130         lose("thread_set_exception_port failed with return_code %d\n", ret);
131     }
132
133     ret = mach_port_move_member(mach_task_self(),
134                                 thread_exception_port,
135                                 mach_exception_handler_port_set);
136     if (ret) {
137         lose("mach_port_ failed with return_code %d\n", ret);
138     }
139
140     return ret;
141 }
142
143 void
144 setup_mach_exceptions() {
145     setup_mach_exception_handling_thread();
146     mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
147 }
148
149 pid_t
150 mach_fork() {
151     pid_t pid = fork();
152     if (pid == 0) {
153         setup_mach_exceptions();
154         return pid;
155     } else {
156         return pid;
157     }
158 }
159
160 #endif
161