]> git.saurik.com Git - apple/libc.git/blob - x86_64/sys/fork.s
Libc-391.5.22.tar.gz
[apple/libc.git] / x86_64 / sys / fork.s
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
25 */
26 #include <SYS.h>
27
28 LEAF(_fork, 0)
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.
36 .cstring
37 LC1:
38 .ascii "__dyld_fork_prepare\0"
39 .text
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
46 #endif
47
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
51
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.
57 .cstring
58 LC2:
59 .ascii "__dyld_fork_parent\0"
60 .text
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)
67 #endif
68 CALL_EXTERN(cerror)
69 CALL_EXTERN(__cthread_fork_parent)
70 movq $-1, %rax
71 addq $24, %rsp // restore the stack
72 ret
73
74 L1:
75 orl %edx,%edx // CF=OF=0, ZF set if zero result
76 jz L2 // parent, since r1 == 0 in parent, 1 in child
77
78 //child here...
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):
85 //
86 // _dyld_func_lookup("__dyld_fork_child", &address);
87 // address();
88 //
89 .cstring
90 LC0:
91 .ascii "__dyld_fork_child\0"
92
93 .text
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
98 #endif
99 xorq %rax, %rax
100 REG_TO_EXTERN(%rax, __current_pid)
101 CALL_EXTERN(__cthread_fork_child)
102 #if defined(__DYNAMIC__)
103 .cstring
104 LC10:
105 .ascii "__dyld_fork_child_final\0"
106
107 .text
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
112 #endif
113 xorq %rax,%rax // zero rax
114 addq $24, %rsp // restore the stack
115 ret
116
117 //parent here...
118 L2:
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
127 #endif
128 CALL_EXTERN_AGAIN(__cthread_fork_parent)
129 movl 16(%rsp), %eax // return pid
130 addq $24, %rsp // restore the stack
131 ret