]> git.saurik.com Git - apple/libc.git/blame - i386/sys/fork.s
Libc-262.2.12.tar.gz
[apple/libc.git] / i386 / sys / fork.s
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
e9ce8d39 7 *
734aad71
A
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
e9ce8d39
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
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.
e9ce8d39
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
27 */
28#include "SYS.h"
29
30LEAF(_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
38LC1:
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
451: 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
5b2abdfb 55 movl $ SYS_fork,%eax; // code for fork -> eax
e9ce8d39
A
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
65LC2:
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
731: 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
88L1:
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
104LC0:
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
1121: 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 CALL_EXTERN(_fork_mach_init)
122 CALL_EXTERN(__cthread_fork_child)
123#if defined(__DYNAMIC__)
124.cstring
125LC10:
126 .ascii "__dyld_fork_child_final\0"
127
128.text
129 subl $4,%esp // allocate space for the address parameter
130 leal 0(%esp),%eax // get the address of the allocated space
131 pushl %eax // push the address of the allocated space
132 call 1f
1331: popl %eax
134 leal LC10-1b(%eax),%eax
135 pushl %eax // push the name of the function to look up
136 call __dyld_func_lookup
137 addl $8,%esp // remove parameters to __dyld_func_lookup
138 movl 0(%esp),%eax // move the value returned in address parameter
139 addl $4,%esp // deallocate the space for the address param
140 call *%eax // call __dyld_fork_child_final indirectly
141#endif
142 xorl %eax,%eax // zero eax
143 ret
144
145 //parent here...
146L2:
147 push %eax // save pid
148#if defined(__DYNAMIC__)
149// __dyld_fork_parent() is called by the parent process after a fork syscall.
150// This releases the dyld lock acquired by __dyld_fork_prepare().
151 subl $4,%esp // allocate space for the address parameter
152 leal 0(%esp),%eax // get the address of the allocated space
153 pushl %eax // push the address of the allocated space
154 call 1f
1551: popl %eax
156 leal LC2-1b(%eax),%eax
157 pushl %eax // push the name of the function to look up
158 call __dyld_func_lookup
159 addl $8,%esp // remove parameters to __dyld_func_lookup
160 movl 0(%esp),%eax // move the value returned in address parameter
161 addl $4,%esp // deallocate the space for the address param
162 call *%eax // call __dyld_fork_parent indirectly
163#endif
164 CALL_EXTERN_AGAIN(__cthread_fork_parent)
165 pop %eax
166 ret
167