]> git.saurik.com Git - apple/libc.git/blob - i386/sys/fork.s
Libc-391.2.10.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 * 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 CALL_EXTERN(__cthread_fork_prepare)
30 #if defined(__DYNAMIC__)
31 // Just like __cthread_fork_prepare we need to prevent threads on the child's
32 // side from doing a mach call in the dynamic linker until __dyld_fork_child
33 // is run (see below). So we call __dyld_fork_prepare which takes out the dyld
34 // lock to prevent all other threads but this one from entering dyld.
35 .cstring
36 LC1:
37 .ascii "__dyld_fork_prepare\0"
38 .text
39 subl $4,%esp // allocate space for the address parameter
40 leal 0(%esp),%eax // get the address of the allocated space
41 pushl %eax // push the address of the allocated space
42 call 1f
43 1: popl %eax
44 leal LC1-1b(%eax),%eax
45 pushl %eax // push the name of the function to look up
46 call __dyld_func_lookup
47 addl $8,%esp // remove parameters to __dyld_func_lookup
48 movl 0(%esp),%eax // move the value returned in address parameter
49 addl $4,%esp // deallocate the space for the address param
50 call *%eax // call __dyld_fork_prepare indirectly
51 #endif
52
53 movl $ SYS_fork,%eax; // code for fork -> eax
54 UNIX_SYSCALL_TRAP; // do the system call
55 jnc L1 // jump if CF==0
56
57 #if defined(__DYNAMIC__)
58 // __dyld_fork_parent() is called by the parent process after a fork syscall.
59 // This releases the dyld lock acquired by __dyld_fork_prepare(). In this case
60 // we just use it to clean up after a fork error so the parent process can
61 // dyld after fork() errors without deadlocking.
62 .cstring
63 LC2:
64 .ascii "__dyld_fork_parent\0"
65 .text
66 pushl %eax // save the return value (errno)
67 subl $4,%esp // allocate space for the address parameter
68 leal 0(%esp),%eax // get the address of the allocated space
69 pushl %eax // push the address of the allocated space
70 call 1f
71 1: popl %eax
72 leal LC2-1b(%eax),%eax
73 pushl %eax // push the name of the function to look up
74 call __dyld_func_lookup
75 addl $8,%esp // remove parameters to __dyld_func_lookup
76 movl 0(%esp),%eax // move the value returned in address parameter
77 addl $4,%esp // deallocate the space for the address param
78 call *%eax // call __dyld_fork_parent indirectly
79 popl %eax // restore the return value (errno)
80 #endif
81 CALL_EXTERN(cerror)
82 CALL_EXTERN(__cthread_fork_parent)
83 movl $-1,%eax
84 ret
85
86 L1:
87 orl %edx,%edx // CF=OF=0, ZF set if zero result
88 jz L2 // parent, since r1 == 0 in parent, 1 in child
89
90 //child here...
91 #if defined(__DYNAMIC__)
92 // Here on the child side of the fork we need to tell the dynamic linker that
93 // we have forked. To do this we call __dyld_fork_child in the dyanmic
94 // linker. But since we can't dynamicly bind anything until this is done we
95 // do this by using the private extern __dyld_func_lookup() function to get the
96 // address of __dyld_fork_child (the 'C' code equivlent):
97 //
98 // _dyld_func_lookup("__dyld_fork_child", &address);
99 // address();
100 //
101 .cstring
102 LC0:
103 .ascii "__dyld_fork_child\0"
104
105 .text
106 subl $4,%esp // allocate space for the address parameter
107 leal 0(%esp),%eax // get the address of the allocated space
108 pushl %eax // push the address of the allocated space
109 call 1f
110 1: popl %eax
111 leal LC0-1b(%eax),%eax
112 pushl %eax // push the name of the function to look up
113 call __dyld_func_lookup
114 addl $8,%esp // remove parameters to __dyld_func_lookup
115 movl 0(%esp),%eax // move the value returned in address parameter
116 addl $4,%esp // deallocate the space for the address param
117 call *%eax // call __dyld_fork_child indirectly
118 #endif
119 xorl %eax, %eax
120 REG_TO_EXTERN(%eax, __current_pid)
121 CALL_EXTERN(__cthread_fork_child)
122 #if defined(__DYNAMIC__)
123 .cstring
124 LC10:
125 .ascii "__dyld_fork_child_final\0"
126
127 .text
128 subl $4,%esp // allocate space for the address parameter
129 leal 0(%esp),%eax // get the address of the allocated space
130 pushl %eax // push the address of the allocated space
131 call 1f
132 1: popl %eax
133 leal LC10-1b(%eax),%eax
134 pushl %eax // push the name of the function to look up
135 call __dyld_func_lookup
136 addl $8,%esp // remove parameters to __dyld_func_lookup
137 movl 0(%esp),%eax // move the value returned in address parameter
138 addl $4,%esp // deallocate the space for the address param
139 call *%eax // call __dyld_fork_child_final indirectly
140 #endif
141 xorl %eax,%eax // zero eax
142 ret
143
144 //parent here...
145 L2:
146 push %eax // save pid
147 #if defined(__DYNAMIC__)
148 // __dyld_fork_parent() is called by the parent process after a fork syscall.
149 // This releases the dyld lock acquired by __dyld_fork_prepare().
150 subl $4,%esp // allocate space for the address parameter
151 leal 0(%esp),%eax // get the address of the allocated space
152 pushl %eax // push the address of the allocated space
153 call 1f
154 1: popl %eax
155 leal LC2-1b(%eax),%eax
156 pushl %eax // push the name of the function to look up
157 call __dyld_func_lookup
158 addl $8,%esp // remove parameters to __dyld_func_lookup
159 movl 0(%esp),%eax // move the value returned in address parameter
160 addl $4,%esp // deallocate the space for the address param
161 call *%eax // call __dyld_fork_parent indirectly
162 #endif
163 CALL_EXTERN_AGAIN(__cthread_fork_parent)
164 pop %eax
165 ret
166