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