1.0.25.50: detect binding and alien stack exhaustion
[sbcl.git] / src / runtime / x86-64-darwin-os.c
1
2 #ifdef LISP_FEATURE_SB_THREAD
3 #include <architecture/i386/table.h>
4 #include <i386/user_ldt.h>
5 #include <mach/mach_init.h>
6 #endif
7
8 #include "thread.h"
9 #include "validate.h"
10 #include "runtime.h"
11 #include "interrupt.h"
12 #include "x86-64-darwin-os.h"
13 #include "genesis/fdefn.h"
14
15 #include <mach/mach.h>
16 #include <mach/mach_error.h>
17 #include <mach/mach_types.h>
18 #include <mach/sync_policy.h>
19 #include <mach/machine/thread_state.h>
20 #include <mach/machine/thread_status.h>
21 #include <sys/_types.h>
22 #include <sys/ucontext.h>
23 #include <pthread.h>
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #if __DARWIN_UNIX03
29 #include <sys/_structs.h>
30 #endif
31
32 #if __DARWIN_UNIX03
33
34 typedef struct __darwin_ucontext darwin_ucontext;
35 typedef struct __darwin_mcontext64 darwin_mcontext;
36
37 #define rip __rip
38 #define rsp __rsp
39 #define rbp __rbp
40 #define rax __rax
41 #define rbx __rbx
42 #define rcx __rcx
43 #define rdx __rdx
44 #define rsi __rsi
45 #define rdi __rdi
46 #define r8 __r8
47 #define r9 __r9
48 #define faultvaddr __faultvaddr
49 #define ss __ss
50 #define es __es
51 #define fs __fs
52
53 #else
54
55 typedef struct ucontext darwin_ucontext;
56 typedef struct mcontext darwin_mcontext;
57
58 #endif
59
60 #ifdef LISP_FEATURE_SB_THREAD
61 pthread_mutex_t mach_exception_lock = PTHREAD_MUTEX_INITIALIZER;
62 #endif
63
64 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
65
66 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
67
68 void sigill_handler(int signal, siginfo_t *siginfo, void *void_context);
69 void sigtrap_handler(int signal, siginfo_t *siginfo, void *void_context);
70 void memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context);
71
72 /* exc_server handles mach exception messages from the kernel and
73  * calls catch exception raise. We use the system-provided
74  * mach_msg_server, which, I assume, calls exc_server in a loop.
75  *
76  */
77 extern boolean_t exc_server();
78
79 /* This executes in the faulting thread as part of the signal
80  * emulation.  It is passed a context with the uc_mcontext field
81  * pointing to a valid block of memory. */
82 void build_fake_signal_context(darwin_ucontext *context,
83                                x86_thread_state64_t *thread_state,
84                                x86_float_state64_t *float_state) {
85     pthread_sigmask(0, NULL, &context->uc_sigmask);
86     context->uc_mcontext->ss = *thread_state;
87     context->uc_mcontext->fs = *float_state;
88 }
89
90 /* This executes in the faulting thread as part of the signal
91  * emulation.  It is effectively the inverse operation from above. */
92 void update_thread_state_from_context(x86_thread_state64_t *thread_state,
93                                       x86_float_state64_t *float_state,
94                                       darwin_ucontext  *context) {
95     *thread_state = context->uc_mcontext->ss;
96     *float_state = context->uc_mcontext->fs;
97     pthread_sigmask(SIG_SETMASK, &context->uc_sigmask, NULL);
98 }
99
100 /* Modify a context to push new data on its stack. */
101 void push_context(u64 data, x86_thread_state64_t *context)
102 {
103     u64 *stack_pointer;
104
105     stack_pointer = (u64*) context->rsp;
106     *(--stack_pointer) = data;
107     context->rsp = (u64) stack_pointer;
108 }
109
110 void align_context_stack(x86_thread_state64_t *context)
111 {
112     /* 16byte align the stack (provided that the stack is, as it
113      * should be, 8byte aligned. */
114     while (context->rsp & 15) push_context(0, context);
115 }
116
117 /* Stack allocation starts with a context that has a mod-4 ESP value
118  * and needs to leave a context with a mod-16 ESP that will restore
119  * the old ESP value and other register state when activated.  The
120  * first part of this is the recovery trampoline, which loads ESP from
121  * EBP, pops EBP, and returns. */
122 asm(".globl _stack_allocation_recover; .align 4; _stack_allocation_recover: mov %rbp, %rsp; pop %rsi; pop %rdi; pop \
123 %rdx; pop %rcx; pop %r8; pop %r9; pop %rbp; ret;");
124
125 void open_stack_allocation(x86_thread_state64_t *context)
126 {
127     void stack_allocation_recover(void);
128
129     push_context(context->rip, context);
130     push_context(context->rbp, context);
131
132     push_context(context->r9, context);
133     push_context(context->r8, context);
134     push_context(context->rcx, context);
135     push_context(context->rdx, context);
136     push_context(context->rsi, context);
137     push_context(context->rdi, context);
138
139     context->rbp = context->rsp;
140     context->rip = (u64) stack_allocation_recover;
141
142     align_context_stack(context);
143 }
144
145 /* Stack allocation of data starts with a context with a mod-16 ESP
146  * value and reserves some space on it by manipulating the ESP
147  * register. */
148 void *stack_allocate(x86_thread_state64_t *context, size_t size)
149 {
150     /* round up size to 16byte multiple */
151     size = (size + 15) & -16;
152
153     context->rsp = ((u64)context->rsp) - size;
154
155     return (void *)context->rsp;
156 }
157
158 /* Arranging to invoke a C function is tricky, as we have to assume
159  * cdecl calling conventions (caller removes args) and x86/darwin
160  * alignment requirements.  The simplest way to arrange this,
161  * actually, is to open a new stack allocation.
162  * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
163 void call_c_function_in_context(x86_thread_state64_t *context,
164                                 void *function,
165                                 int nargs,
166                                 ...)
167 {
168     va_list ap;
169     int i;
170     u64 *stack_pointer;
171
172     /* Set up to restore stack on exit. */
173     open_stack_allocation(context);
174
175     /* Have to keep stack 16byte aligned on x86/darwin. */
176     for (i = (1 & -nargs); i; i--) {
177         push_context(0, context);
178     }
179
180     context->rsp = ((u64)context->rsp) - nargs * 8;
181     stack_pointer = (u64 *)context->rsp;
182
183     va_start(ap, nargs);
184     if (nargs > 0) context->rdi = va_arg(ap, u64);
185     if (nargs > 1) context->rsi = va_arg(ap, u64);
186     if (nargs > 2) context->rdx = va_arg(ap, u64);
187     if (nargs > 3) context->rcx = va_arg(ap, u64);
188     if (nargs > 4) context->r8 = va_arg(ap, u64);
189     if (nargs > 5) context->r9 = va_arg(ap, u64);
190     for (i = 6; i < nargs; i++) {
191         stack_pointer[i] = va_arg(ap, u64);
192     }
193     va_end(ap);
194
195     push_context(context->rip, context);
196     context->rip = (u64) function;
197 }
198
199 void signal_emulation_wrapper(x86_thread_state64_t *thread_state,
200                               x86_float_state64_t *float_state,
201                               int signal,
202                               siginfo_t *siginfo,
203                               void (*handler)(int, siginfo_t *, void *))
204 {
205
206     /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
207      * context and regs on the stack as local variables, but this
208      * causes problems for the lisp debugger. When it walks the stack
209      * for a back trace, it sees the 1) address of the local variable
210      * on the stack and thinks that is a frame pointer to a lisp
211      * frame, and, 2) the address of the sap that we alloc'ed in
212      * dynamic space and thinks that is a return address, so it,
213      * heuristicly (and wrongly), chooses that this should be
214      * interpreted as a lisp frame instead of as a C frame.
215      * We can work around this in this case by os_validating the
216      * context (and regs just for symmetry).
217      */
218
219     darwin_ucontext  *context;
220     darwin_mcontext *regs;
221
222     context = (darwin_ucontext *) os_validate(0, sizeof(darwin_ucontext));
223     regs = (darwin_mcontext*) os_validate(0, sizeof(darwin_mcontext));
224     context->uc_mcontext = regs;
225
226     /* when BSD signals are fired, they mask they signals in sa_mask
227        which always seem to be the blockable_sigset, for us, so we
228        need to:
229        1) save the current sigmask
230        2) block blockable signals
231        3) call the signal handler
232        4) restore the sigmask */
233
234     build_fake_signal_context(context, thread_state, float_state);
235
236     block_blockable_signals();
237
238     handler(signal, siginfo, context);
239
240     update_thread_state_from_context(thread_state, float_state, context);
241
242     os_invalidate((os_vm_address_t)context, sizeof(darwin_ucontext));
243     os_invalidate((os_vm_address_t)regs, sizeof(darwin_mcontext));
244
245     /* Trap to restore the signal context. */
246     asm volatile ("mov %0, %%rax; mov %1, %%rbx; .quad 0xffffffffffff0b0f"
247                   : : "r" (thread_state), "r" (float_state));
248 }
249
250 #if defined DUMP_CONTEXT
251 void dump_context(x86_thread_state64_t *context)
252 {
253     int i;
254     u64 *stack_pointer;
255
256     printf("rax: %08lx  rcx: %08lx  rdx: %08lx  rbx: %08lx\n",
257            context->rax, context->rcx, context->rdx, context->rbx);
258     printf("rsp: %08lx  rbp: %08lx  rsi: %08lx  rdi: %08lx\n",
259            context->rsp, context->rbp, context->rsi, context->rdi);
260     printf("rip: %08lx  eflags: %08lx\n",
261            context->rip, context->rflags);
262     printf("cs: %04hx  ds: %04hx  es: %04hx  "
263            "ss: %04hx  fs: %04hx  gs: %04hx\n",
264            context->cs, context->ds, context->rs,
265            context->ss, context->fs, context->gs);
266
267     stack_pointer = (u64 *)context->rsp;
268     for (i = 0; i < 48; i+=4) {
269         printf("%08x:  %08x %08x %08x %08x\n",
270                context->rsp + (i * 4),
271                stack_pointer[i],
272                stack_pointer[i+1],
273                stack_pointer[i+2],
274                stack_pointer[i+3]);
275     }
276 }
277 #endif
278
279 void
280 control_stack_exhausted_handler(int signal, siginfo_t *siginfo, void *void_context) {
281     os_context_t *context = arch_os_get_context(&void_context);
282
283     unblock_signals_in_context_and_maybe_warn(context);
284     arrange_return_to_lisp_function
285         (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
286 }
287
288 void
289 undefined_alien_handler(int signal, siginfo_t *siginfo, void *void_context) {
290     os_context_t *context = arch_os_get_context(&void_context);
291
292     arrange_return_to_lisp_function
293         (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
294 }
295
296 kern_return_t
297 catch_exception_raise(mach_port_t exception_port,
298                       mach_port_t thread,
299                       mach_port_t task,
300                       exception_type_t exception,
301                       exception_data_t code_vector,
302                       mach_msg_type_number_t code_count)
303 {
304     kern_return_t ret;
305     int signal;
306     siginfo_t* siginfo;
307
308 #ifdef LISP_FEATURE_SB_THREAD
309     thread_mutex_lock(&mach_exception_lock);
310 #endif
311
312     x86_thread_state64_t thread_state;
313     mach_msg_type_number_t thread_state_count = x86_THREAD_STATE64_COUNT;
314
315     x86_float_state64_t float_state;
316     mach_msg_type_number_t float_state_count = x86_FLOAT_STATE64_COUNT;
317
318     x86_exception_state64_t exception_state;
319     mach_msg_type_number_t exception_state_count = x86_EXCEPTION_STATE64_COUNT;
320
321     x86_thread_state64_t backup_thread_state;
322     x86_thread_state64_t *target_thread_state;
323     x86_float_state64_t *target_float_state;
324
325     os_vm_address_t addr;
326
327     struct thread *th = (struct thread*) exception_port;
328
329     FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
330
331     switch (exception) {
332
333     case EXC_BAD_ACCESS:
334         signal = SIGBUS;
335         ret = thread_get_state(thread,
336                                x86_THREAD_STATE64,
337                                (thread_state_t)&thread_state,
338                                &thread_state_count);
339         ret = thread_get_state(thread,
340                                x86_FLOAT_STATE64,
341                                (thread_state_t)&float_state,
342                                &float_state_count);
343         ret = thread_get_state(thread,
344                                x86_EXCEPTION_STATE64,
345                                (thread_state_t)&exception_state,
346                                &exception_state_count);
347         addr = (void*)exception_state.faultvaddr;
348
349
350         /* note the os_context hackery here.  When the signal handler returns,
351          * it won't go back to what it was doing ... */
352         if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
353            addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
354             /* We hit the end of the control stack: disable guard page
355              * protection so the error handler has some headroom, protect the
356              * previous page so that we can catch returns from the guard page
357              * and restore it. */
358             protect_control_stack_guard_page(0, th);
359             protect_control_stack_return_guard_page(1, th);
360
361             backup_thread_state = thread_state;
362             open_stack_allocation(&thread_state);
363             /* Reserve a 256 byte zone for signal handlers
364              * to use on the interrupted thread stack.
365              */
366             stack_allocate(&thread_state, 256);
367
368             /* Save thread state */
369             target_thread_state =
370                 stack_allocate(&thread_state, sizeof(*target_thread_state));
371             (*target_thread_state) = backup_thread_state;
372
373             /* Save float state */
374             target_float_state =
375                 stack_allocate(&thread_state, sizeof(*target_float_state));
376             (*target_float_state) = float_state;
377
378             /* Set up siginfo */
379             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
380             /* what do we need to put in our fake siginfo?  It looks like
381              * the x86 code only uses si_signo and si_adrr. */
382             siginfo->si_signo = signal;
383             siginfo->si_addr = (void*)exception_state.faultvaddr;
384
385             call_c_function_in_context(&thread_state,
386                                        signal_emulation_wrapper,
387                                        5,
388                                        target_thread_state,
389                                        target_float_state,
390                                        signal,
391                                        siginfo,
392                                        control_stack_exhausted_handler);
393         }
394         else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
395                 addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
396             /* We're returning from the guard page: reprotect it, and
397              * unprotect this one. This works even if we somehow missed
398              * the return-guard-page, and hit it on our way to new
399              * exhaustion instead. */
400             protect_control_stack_guard_page(1, th);
401             protect_control_stack_return_guard_page(0, th);
402         }
403         else if (addr >= undefined_alien_address &&
404                  addr < undefined_alien_address + os_vm_page_size) {
405             backup_thread_state = thread_state;
406             open_stack_allocation(&thread_state);
407             stack_allocate(&thread_state, 256);
408
409             /* Save thread state */
410             target_thread_state =
411                 stack_allocate(&thread_state, sizeof(*target_thread_state));
412             (*target_thread_state) = backup_thread_state;
413
414             target_float_state =
415                 stack_allocate(&thread_state, sizeof(*target_float_state));
416             (*target_float_state) = float_state;
417
418             /* Set up siginfo */
419             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
420             /* what do we need to put in our fake siginfo?  It looks like
421              * the x86 code only uses si_signo and si_adrr. */
422             siginfo->si_signo = signal;
423             siginfo->si_addr = (void*)exception_state.faultvaddr;
424
425             call_c_function_in_context(&thread_state,
426                                        signal_emulation_wrapper,
427                                        5,
428                                        target_thread_state,
429                                        target_float_state,
430                                        signal,
431                                        siginfo,
432                                        undefined_alien_handler);
433         } else {
434
435             backup_thread_state = thread_state;
436             open_stack_allocation(&thread_state);
437             stack_allocate(&thread_state, 256);
438
439             /* Save thread state */
440             target_thread_state =
441                 stack_allocate(&thread_state, sizeof(*target_thread_state));
442             (*target_thread_state) = backup_thread_state;
443
444             target_float_state =
445                 stack_allocate(&thread_state, sizeof(*target_float_state));
446             (*target_float_state) = float_state;
447
448             /* Set up siginfo */
449             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
450             /* what do we need to put in our fake siginfo?  It looks like
451              * the x86 code only uses si_signo and si_adrr. */
452             siginfo->si_signo = signal;
453             siginfo->si_addr = (void*)exception_state.faultvaddr;
454
455             call_c_function_in_context(&thread_state,
456                                        signal_emulation_wrapper,
457                                        5,
458                                        target_thread_state,
459                                        target_float_state,
460                                        signal,
461                                        siginfo,
462                                        memory_fault_handler);
463         }
464         ret = thread_set_state(thread,
465                                x86_THREAD_STATE64,
466                                (thread_state_t)&thread_state,
467                                thread_state_count);
468
469         ret = thread_set_state(thread,
470                                x86_FLOAT_STATE64,
471                                (thread_state_t)&float_state,
472                                float_state_count);
473 #ifdef LISP_FEATURE_SB_THREAD
474         thread_mutex_unlock(&mach_exception_lock);
475 #endif
476         return KERN_SUCCESS;
477
478     case EXC_BAD_INSTRUCTION:
479
480         ret = thread_get_state(thread,
481                                x86_THREAD_STATE64,
482                                (thread_state_t)&thread_state,
483                                &thread_state_count);
484         ret = thread_get_state(thread,
485                                x86_FLOAT_STATE64,
486                                (thread_state_t)&float_state,
487                                &float_state_count);
488         ret = thread_get_state(thread,
489                                x86_EXCEPTION_STATE64,
490                                (thread_state_t)&exception_state,
491                                &exception_state_count);
492         if (0xffffffffffff0b0f == *((u64 *)thread_state.rip)) {
493             /* fake sigreturn. */
494
495             /* When we get here, thread_state.rax is a pointer to a
496              * thread_state to restore. */
497             /* thread_state = *((thread_state_t *)thread_state.rax); */
498
499             ret = thread_set_state(thread,
500                                    x86_THREAD_STATE64,
501                                    (thread_state_t) thread_state.rax,
502                                    /* &thread_state, */
503                                    thread_state_count);
504
505             ret = thread_set_state(thread,
506                                    x86_FLOAT_STATE64,
507                                    (thread_state_t) thread_state.rbx,
508                                    /* &thread_state, */
509                                    float_state_count);
510         } else {
511
512             backup_thread_state = thread_state;
513             open_stack_allocation(&thread_state);
514             stack_allocate(&thread_state, 256);
515
516             /* Save thread state */
517             target_thread_state =
518                 stack_allocate(&thread_state, sizeof(*target_thread_state));
519             (*target_thread_state) = backup_thread_state;
520
521             target_float_state =
522                 stack_allocate(&thread_state, sizeof(*target_float_state));
523             (*target_float_state) = float_state;
524
525             /* Set up siginfo */
526             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
527             /* what do we need to put in our fake siginfo?  It looks like
528              * the x86 code only uses si_signo and si_adrr. */
529             if (*((unsigned short *)target_thread_state->rip) == 0x0b0f) {
530                 signal = SIGTRAP;
531                 siginfo->si_signo = signal;
532                 siginfo->si_addr = (void*)exception_state.faultvaddr;
533                 target_thread_state->rip += 2;
534                 call_c_function_in_context(&thread_state,
535                                            signal_emulation_wrapper,
536                                            5,
537                                            target_thread_state,
538                                            target_float_state,
539                                            signal,
540                                            siginfo,
541                                            sigtrap_handler);
542             } else {
543                 signal = SIGILL;
544                 siginfo->si_signo = signal;
545                 siginfo->si_addr = (void*)exception_state.faultvaddr;
546
547                 call_c_function_in_context(&thread_state,
548                                            signal_emulation_wrapper,
549                                            5,
550                                            target_thread_state,
551                                            target_float_state,
552                                            signal,
553                                            siginfo,
554                                            sigill_handler);
555             }
556             ret = thread_set_state(thread,
557                                    x86_THREAD_STATE64,
558                                    (thread_state_t)&thread_state,
559                                    thread_state_count);
560             ret = thread_set_state(thread,
561                                    x86_FLOAT_STATE64,
562                                    (thread_state_t)&float_state,
563                                    float_state_count);
564         }
565 #ifdef LISP_FEATURE_SB_THREAD
566         thread_mutex_unlock(&mach_exception_lock);
567 #endif
568         return KERN_SUCCESS;
569
570     default:
571 #ifdef LISP_FEATURE_SB_THREAD
572         thread_mutex_unlock(&mach_exception_lock);
573 #endif
574         return KERN_INVALID_RIGHT;
575     }
576 }
577
578 void *
579 mach_exception_handler(void *port)
580 {
581   mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
582   /* mach_msg_server should never return, but it should dispatch mach
583    * exceptions to our catch_exception_raise function
584    */
585   abort();
586 }
587
588 /* Sets up the thread that will listen for mach exceptions. note that
589    the exception handlers will be run on this thread. This is
590    different from the BSD-style signal handling situation in which the
591    signal handlers run in the relevant thread directly. */
592
593 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
594
595 pthread_t
596 setup_mach_exception_handling_thread()
597 {
598     kern_return_t ret;
599     pthread_t mach_exception_handling_thread = NULL;
600     pthread_attr_t attr;
601
602     /* allocate a mach_port for this process */
603     ret = mach_port_allocate(mach_task_self(),
604                              MACH_PORT_RIGHT_PORT_SET,
605                              &mach_exception_handler_port_set);
606
607     /* create the thread that will receive the mach exceptions */
608
609     FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
610
611     pthread_attr_init(&attr);
612     pthread_create(&mach_exception_handling_thread,
613                    &attr,
614                    mach_exception_handler,
615                    (void*) mach_exception_handler_port_set);
616     pthread_attr_destroy(&attr);
617
618     return mach_exception_handling_thread;
619 }
620
621 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
622    exception port (which is being listened to do by the mach
623    exception handling thread). */
624 kern_return_t
625 mach_thread_init(mach_port_t thread_exception_port)
626 {
627     kern_return_t ret;
628     /* allocate a named port for the thread */
629
630     FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
631
632     ret = mach_port_allocate_name(mach_task_self(),
633                                   MACH_PORT_RIGHT_RECEIVE,
634                                   thread_exception_port);
635     if (ret) {
636         lose("mach_port_allocate_name failed with return_code %d\n", ret);
637     }
638
639     /* establish the right for the thread_exception_port to send messages */
640     ret = mach_port_insert_right(mach_task_self(),
641                                  thread_exception_port,
642                                  thread_exception_port,
643                                  MACH_MSG_TYPE_MAKE_SEND);
644     if (ret) {
645         lose("mach_port_insert_right failed with return_code %d\n", ret);
646     }
647
648     ret = thread_set_exception_ports(mach_thread_self(),
649                                      EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
650                                      thread_exception_port,
651                                      EXCEPTION_DEFAULT,
652                                      THREAD_STATE_NONE);
653     if (ret) {
654         lose("thread_set_exception_port failed with return_code %d\n", ret);
655     }
656
657     ret = mach_port_move_member(mach_task_self(),
658                                 thread_exception_port,
659                                 mach_exception_handler_port_set);
660     if (ret) {
661         lose("mach_port_ failed with return_code %d\n", ret);
662     }
663
664     return ret;
665 }
666
667 void
668 setup_mach_exceptions() {
669     setup_mach_exception_handling_thread();
670     mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
671 }
672
673 pid_t
674 mach_fork() {
675     pid_t pid = fork();
676     if (pid == 0) {
677         setup_mach_exceptions();
678         return pid;
679     } else {
680         return pid;
681     }
682 }
683
684 #endif