2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
29 subq $24, %rsp // Align the stack, plus room for local storage
30 CALL_EXTERN(__cthread_fork_prepare)
31 #if defined(__DYNAMIC__)
32 // Just like __cthread_fork_prepare we need to prevent threads on the child's
33 // side from doing a mach call in the dynamic linker until __dyld_fork_child
34 // is run (see below). So we call __dyld_fork_prepare which takes out the dyld
35 // lock to prevent all other threads but this one from entering dyld.
38 .ascii "__dyld_fork_prepare\0"
40 // Give a pointer to 8(%rsp) to _dyld_func_lookup for it to fill in.
41 leaq 8(%rsp),%rsi // get the address where we're going to store the pointer
42 leaq LC1(%rip), %rdi // copy the name of the function to look up
43 call __dyld_func_lookup
44 movq 8(%rsp),%rax // move the value returned in address parameter
45 call *%rax // call __dyld_fork_prepare
48 movl $ SYSCALL_CONSTRUCT_UNIX(SYS_fork),%eax; // code for fork -> rax
49 UNIX_SYSCALL_TRAP // do the system call
50 jnc L1 // jump if CF==0
52 #if defined(__DYNAMIC__)
53 // __dyld_fork_parent() is called by the parent process after a fork syscall.
54 // This releases the dyld lock acquired by __dyld_fork_prepare(). In this case
55 // we just use it to clean up after a fork error so the parent process can
56 // dyld after fork() errors without deadlocking.
59 .ascii "__dyld_fork_parent\0"
61 movq %rax, 16(%rsp) // save the return value (errno)
62 leaq 8(%rsp),%rsi // get the address where we're going to store the pointer
63 leaq LC2(%rip), %rdi // copy the name of the function to look up
64 call __dyld_func_lookup
65 call *8(%rsp) // call __dyld_fork_parent indirectly
66 movq 16(%rsp), %rax // restore the return value (errno)
69 CALL_EXTERN(__cthread_fork_parent)
71 addq $24, %rsp // restore the stack
75 orl %edx,%edx // CF=OF=0, ZF set if zero result
76 jz L2 // parent, since r1 == 0 in parent, 1 in child
79 #if defined(__DYNAMIC__)
80 // Here on the child side of the fork we need to tell the dynamic linker that
81 // we have forked. To do this we call __dyld_fork_child in the dyanmic
82 // linker. But since we can't dynamically bind anything until this is done we
83 // do this by using the private extern __dyld_func_lookup() function to get the
84 // address of __dyld_fork_child (the 'C' code equivlent):
86 // _dyld_func_lookup("__dyld_fork_child", &address);
91 .ascii "__dyld_fork_child\0"
94 leaq 8(%rsp),%rsi // get the address where we're going to store the pointer
95 leaq LC0(%rip), %rdi // copy the name of the function to look up
96 call __dyld_func_lookup
97 call *8(%rsp) // call __dyld_fork_child indirectly
100 REG_TO_EXTERN(%rax, __current_pid)
101 CALL_EXTERN(__cthread_fork_child)
102 #if defined(__DYNAMIC__)
105 .ascii "__dyld_fork_child_final\0"
108 leaq 8(%rsp),%rsi // get the address where we're going to store the pointer
109 leaq LC10(%rip), %rdi // copy the name of the function to look up
110 call __dyld_func_lookup
111 call *8(%rsp) // call __dyld_fork_child_final indirectly
113 xorq %rax,%rax // zero rax
114 addq $24, %rsp // restore the stack
119 movl %eax, 16(%rsp) // save pid
120 #if defined(__DYNAMIC__)
121 // __dyld_fork_parent() is called by the parent process after a fork syscall.
122 // This releases the dyld lock acquired by __dyld_fork_prepare().
123 leaq 8(%rsp),%rsi // get the address where we're going to store the pointer
124 leaq LC2(%rip), %rdi // copy the name of the function to look up
125 call __dyld_func_lookup
126 call *8(%rsp) // call __dyld_fork_parent indirectly
128 CALL_EXTERN_AGAIN(__cthread_fork_parent)
129 movl 16(%rsp), %eax // return pid
130 addq $24, %rsp // restore the stack