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