]> git.saurik.com Git - apple/libc.git/blob - i386/sys/fork.s
Libc-391.4.3.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 subl $28, %esp // Align the stack, with 16 bytes of extra padding that we'll need
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 // Put a pointer to 8(%esp) in 4(%esp) for _dyld_func_lookup to fill in.
41 leal 0x8(%esp),%eax // get the address where we're going to store the pointer
42 movl %eax, 0x4(%esp) // copy the address of the pointer
43 call 1f
44 1: popl %eax
45 leal LC1-1b(%eax),%eax
46 movl %eax, 0x0(%esp) // copy the name of the function to look up
47 call __dyld_func_lookup
48 movl 0x8(%esp),%eax // move the value returned in address parameter
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
62 LC2:
63 .ascii "__dyld_fork_parent\0"
64 .text
65 movl %eax, 0xc(%esp) // save the return value (errno)
66 leal 0x8(%esp),%eax // get the address where we're going to store the pointer
67 movl %eax, 0x4(%esp) // copy the address of the pointer
68 call 1f
69 1: popl %eax
70 leal LC2-1b(%eax),%eax
71 movl %eax, 0x0(%esp) // copy the name of the function to look up
72 call __dyld_func_lookup
73 movl 0x8(%esp),%eax // move the value returned in address parameter
74 call *%eax // call __dyld_fork_parent indirectly
75 movl 0xc(%esp), %eax // restore the return value (errno)
76 #endif
77 CALL_EXTERN(cerror)
78 CALL_EXTERN(__cthread_fork_parent)
79 movl $-1,%eax
80 addl $28, %esp // restore the stack
81 ret
82
83 L1:
84 orl %edx,%edx // CF=OF=0, ZF set if zero result
85 jz L2 // parent, since r1 == 0 in parent, 1 in child
86
87 //child here...
88 #if defined(__DYNAMIC__)
89 // Here on the child side of the fork we need to tell the dynamic linker that
90 // we have forked. To do this we call __dyld_fork_child in the dyanmic
91 // linker. But since we can't dynamically bind anything until this is done we
92 // do this by using the private extern __dyld_func_lookup() function to get the
93 // address of __dyld_fork_child (the 'C' code equivlent):
94 //
95 // _dyld_func_lookup("__dyld_fork_child", &address);
96 // address();
97 //
98 .cstring
99 LC0:
100 .ascii "__dyld_fork_child\0"
101
102 .text
103 leal 0x8(%esp),%eax // get the address where we're going to store the pointer
104 movl %eax, 0x4(%esp) // copy the address of the pointer
105 call 1f
106 1: popl %eax
107 leal LC0-1b(%eax),%eax
108 movl %eax, 0x0(%esp) // copy the name of the function to look up
109 call __dyld_func_lookup
110 movl 0x8(%esp),%eax // move the value returned in address parameter
111 call *%eax // call __dyld_fork_child indirectly
112 #endif
113 xorl %eax, %eax
114 REG_TO_EXTERN(%eax, __current_pid)
115 CALL_EXTERN(__cthread_fork_child)
116 #if defined(__DYNAMIC__)
117 .cstring
118 LC10:
119 .ascii "__dyld_fork_child_final\0"
120
121 .text
122 leal 0x8(%esp),%eax // get the address where we're going to store the pointer
123 movl %eax, 0x4(%esp) // copy the address of the pointer
124 call 1f
125 1: popl %eax
126 leal LC10-1b(%eax),%eax
127 movl %eax, 0x0(%esp) // copy the name of the function to look up
128 call __dyld_func_lookup
129 movl 0x8(%esp),%eax // move the value returned in address parameter
130 call *%eax // call __dyld_fork_child_final indirectly
131 #endif
132 xorl %eax,%eax // zero eax
133 addl $28, %esp // restore the stack
134 ret
135
136 //parent here...
137 L2:
138 movl %eax, 0xc(%esp) // save pid
139 #if defined(__DYNAMIC__)
140 // __dyld_fork_parent() is called by the parent process after a fork syscall.
141 // This releases the dyld lock acquired by __dyld_fork_prepare().
142 leal 0x8(%esp),%eax // get the address where we're going to store the pointer
143 movl %eax, 0x4(%esp) // copy the address of the allocated space
144 call 1f
145 1: popl %eax
146 leal LC2-1b(%eax),%eax
147 movl %eax, 0x0(%esp) // copy the name of the function to look up
148 call __dyld_func_lookup
149 movl 0x8(%esp),%eax // move the value returned in address parameter
150 call *%eax // call __dyld_fork_parent indirectly
151 #endif
152 CALL_EXTERN_AGAIN(__cthread_fork_parent)
153 movl 0xc(%esp), %eax // return pid
154 addl $28, %esp // restore the stack
155 ret
156