]> git.saurik.com Git - apple/libc.git/blob - i386/sys/fork.s
Libc-320.tar.gz
[apple/libc.git] / i386 / sys / fork.s
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
27 */
28 #include "SYS.h"
29
30 LEAF(_fork, 0)
31 CALL_EXTERN(__cthread_fork_prepare)
32 #if defined(__DYNAMIC__)
33 // Just like __cthread_fork_prepare we need to prevent threads on the child's
34 // side from doing a mach call in the dynamic linker until __dyld_fork_child
35 // is run (see below). So we call __dyld_fork_prepare which takes out the dyld
36 // lock to prevent all other threads but this one from entering dyld.
37 .cstring
38 LC1:
39 .ascii "__dyld_fork_prepare\0"
40 .text
41 subl $4,%esp // allocate space for the address parameter
42 leal 0(%esp),%eax // get the address of the allocated space
43 pushl %eax // push the address of the allocated space
44 call 1f
45 1: popl %eax
46 leal LC1-1b(%eax),%eax
47 pushl %eax // push the name of the function to look up
48 call __dyld_func_lookup
49 addl $8,%esp // remove parameters to __dyld_func_lookup
50 movl 0(%esp),%eax // move the value returned in address parameter
51 addl $4,%esp // deallocate the space for the address param
52 call *%eax // call __dyld_fork_prepare indirectly
53 #endif
54
55 movl $ SYS_fork,%eax; // code for fork -> eax
56 UNIX_SYSCALL_TRAP; // do the system call
57 jnc L1 // jump if CF==0
58
59 #if defined(__DYNAMIC__)
60 // __dyld_fork_parent() is called by the parent process after a fork syscall.
61 // This releases the dyld lock acquired by __dyld_fork_prepare(). In this case
62 // we just use it to clean up after a fork error so the parent process can
63 // dyld after fork() errors without deadlocking.
64 .cstring
65 LC2:
66 .ascii "__dyld_fork_parent\0"
67 .text
68 pushl %eax // save the return value (errno)
69 subl $4,%esp // allocate space for the address parameter
70 leal 0(%esp),%eax // get the address of the allocated space
71 pushl %eax // push the address of the allocated space
72 call 1f
73 1: popl %eax
74 leal LC2-1b(%eax),%eax
75 pushl %eax // push the name of the function to look up
76 call __dyld_func_lookup
77 addl $8,%esp // remove parameters to __dyld_func_lookup
78 movl 0(%esp),%eax // move the value returned in address parameter
79 addl $4,%esp // deallocate the space for the address param
80 call *%eax // call __dyld_fork_parent indirectly
81 popl %eax // restore the return value (errno)
82 #endif
83 CALL_EXTERN(cerror)
84 CALL_EXTERN(__cthread_fork_parent)
85 movl $-1,%eax
86 ret
87
88 L1:
89 orl %edx,%edx // CF=OF=0, ZF set if zero result
90 jz L2 // parent, since r1 == 0 in parent, 1 in child
91
92 //child here...
93 #if defined(__DYNAMIC__)
94 // Here on the child side of the fork we need to tell the dynamic linker that
95 // we have forked. To do this we call __dyld_fork_child in the dyanmic
96 // linker. But since we can't dynamicly bind anything until this is done we
97 // do this by using the private extern __dyld_func_lookup() function to get the
98 // address of __dyld_fork_child (the 'C' code equivlent):
99 //
100 // _dyld_func_lookup("__dyld_fork_child", &address);
101 // address();
102 //
103 .cstring
104 LC0:
105 .ascii "__dyld_fork_child\0"
106
107 .text
108 subl $4,%esp // allocate space for the address parameter
109 leal 0(%esp),%eax // get the address of the allocated space
110 pushl %eax // push the address of the allocated space
111 call 1f
112 1: popl %eax
113 leal LC0-1b(%eax),%eax
114 pushl %eax // push the name of the function to look up
115 call __dyld_func_lookup
116 addl $8,%esp // remove parameters to __dyld_func_lookup
117 movl 0(%esp),%eax // move the value returned in address parameter
118 addl $4,%esp // deallocate the space for the address param
119 call *%eax // call __dyld_fork_child indirectly
120 #endif
121 xorl %eax, %eax
122 REG_TO_EXTERN(%eax, __current_pid)
123 CALL_EXTERN(__cthread_fork_child)
124 #if defined(__DYNAMIC__)
125 .cstring
126 LC10:
127 .ascii "__dyld_fork_child_final\0"
128
129 .text
130 subl $4,%esp // allocate space for the address parameter
131 leal 0(%esp),%eax // get the address of the allocated space
132 pushl %eax // push the address of the allocated space
133 call 1f
134 1: popl %eax
135 leal LC10-1b(%eax),%eax
136 pushl %eax // push the name of the function to look up
137 call __dyld_func_lookup
138 addl $8,%esp // remove parameters to __dyld_func_lookup
139 movl 0(%esp),%eax // move the value returned in address parameter
140 addl $4,%esp // deallocate the space for the address param
141 call *%eax // call __dyld_fork_child_final indirectly
142 #endif
143 xorl %eax,%eax // zero eax
144 ret
145
146 //parent here...
147 L2:
148 push %eax // save pid
149 #if defined(__DYNAMIC__)
150 // __dyld_fork_parent() is called by the parent process after a fork syscall.
151 // This releases the dyld lock acquired by __dyld_fork_prepare().
152 subl $4,%esp // allocate space for the address parameter
153 leal 0(%esp),%eax // get the address of the allocated space
154 pushl %eax // push the address of the allocated space
155 call 1f
156 1: popl %eax
157 leal LC2-1b(%eax),%eax
158 pushl %eax // push the name of the function to look up
159 call __dyld_func_lookup
160 addl $8,%esp // remove parameters to __dyld_func_lookup
161 movl 0(%esp),%eax // move the value returned in address parameter
162 addl $4,%esp // deallocate the space for the address param
163 call *%eax // call __dyld_fork_parent indirectly
164 #endif
165 CALL_EXTERN_AGAIN(__cthread_fork_parent)
166 pop %eax
167 ret
168