2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1992 NeXT Computer, Inc. All rights reserved.
30 * File: libc/ppc/sys/fork.s
33 * 18-Nov-92 Ben Fathi (benf@next.com)
34 * Created from M88K sources
36 * 11-Jan-92 Peter King (king@next.com)
37 * Created from M68K sources
42 #if defined(__ppc__) || defined(__ppc64__)
44 /* We use mode-independent "g" opcodes such as "srgi". These expand
45 * into word operations when targeting __ppc__, and into doubleword
46 * operations when targeting __ppc64__.
48 #include <architecture/ppc/mode_independent_asm.h>
50 MI_ENTRY_POINT(___fork)
53 MI_CALL_EXTERNAL(__cthread_fork_prepare)
57 b Lbotch // error return
59 cmpwi r4,0 // parent (r4==0) or child (r4==1) ?
60 beq Lparent // parent, since r4==0
63 /* Here if we are the child. */
65 #if defined(__DYNAMIC__)
68 .ascii "__dyld_fork_child\0"
75 addis r3,r3,ha16(LC3-1b)
76 addi r3,r3,lo16(LC3-1b)
85 MI_GET_ADDRESS(r8,__current_pid)
86 stw r9,0(r8) // clear cached pid in child
88 MI_CALL_EXTERNAL(__cthread_fork_child)
90 li r3,0 // flag for "we are the child"
94 /* Here if we are the parent, with:
98 stg r3,SF_LOCAL2(r1) // save child pid in stack
100 b Lparent_return // clean up and return child's pid
103 /* Here if the fork() syscall failed. We're still the parent. */
107 MI_CALL_EXTERNAL(cerror)
108 li r3,-1 // get an error return code
109 stg r3,SF_LOCAL2(r1) // save return code in stack
112 * We use cthread_fork_parent() to clean up after a fork error
113 * (unlock cthreads and mailloc packages) so the parent
114 * process can Malloc() after fork() errors without
119 MI_CALL_EXTERNAL(__cthread_fork_parent)
120 lg r3,SF_LOCAL2(r1) // return -1 on error, child's pid on success
123 MI_POP_STACK_FRAME_AND_RETURN
125 #elif defined(__i386__)
128 subl $28, %esp // Align the stack, with 16 bytes of extra padding that we'll need
129 CALL_EXTERN(__cthread_fork_prepare)
131 movl $ SYS_fork,%eax; // code for fork -> eax
132 UNIX_SYSCALL_TRAP // do the system call
133 jnc L1 // jump if CF==0
136 CALL_EXTERN(__cthread_fork_parent)
138 addl $28, %esp // restore the stack
142 orl %edx,%edx // CF=OF=0, ZF set if zero result
143 jz L2 // parent, since r1 == 0 in parent, 1 in child
146 #if defined(__DYNAMIC__)
147 // Here on the child side of the fork we need to tell the dynamic linker that
148 // we have forked. To do this we call __dyld_fork_child in the dyanmic
149 // linker. But since we can't dynamically bind anything until this is done we
150 // do this by using the private extern __dyld_func_lookup() function to get the
151 // address of __dyld_fork_child (the 'C' code equivlent):
153 // _dyld_func_lookup("__dyld_fork_child", &address);
158 .ascii "__dyld_fork_child\0"
161 leal 0x8(%esp),%eax // get the address where we're going to store the pointer
162 movl %eax, 0x4(%esp) // copy the address of the pointer
165 leal LC0-1b(%eax),%eax
166 movl %eax, 0x0(%esp) // copy the name of the function to look up
167 call __dyld_func_lookup
168 movl 0x8(%esp),%eax // move the value returned in address parameter
169 call *%eax // call __dyld_fork_child indirectly
172 REG_TO_EXTERN(%eax, __current_pid)
173 CALL_EXTERN(__cthread_fork_child)
175 xorl %eax,%eax // zero eax
176 addl $28, %esp // restore the stack
181 movl %eax, 0xc(%esp) // save pid
183 CALL_EXTERN_AGAIN(__cthread_fork_parent)
184 movl 0xc(%esp), %eax // return pid
185 addl $28, %esp // restore the stack
188 #elif defined(__x86_64__)
191 subq $24, %rsp // Align the stack, plus room for local storage
192 CALL_EXTERN(__cthread_fork_prepare)
194 movl $ SYSCALL_CONSTRUCT_UNIX(SYS_fork),%eax; // code for fork -> rax
195 UNIX_SYSCALL_TRAP // do the system call
196 jnc L1 // jump if CF==0
199 CALL_EXTERN(__cthread_fork_parent)
201 addq $24, %rsp // restore the stack
205 orl %edx,%edx // CF=OF=0, ZF set if zero result
206 jz L2 // parent, since r1 == 0 in parent, 1 in child
209 #if defined(__DYNAMIC__)
210 // Here on the child side of the fork we need to tell the dynamic linker that
211 // we have forked. To do this we call __dyld_fork_child in the dyanmic
212 // linker. But since we can't dynamically bind anything until this is done we
213 // do this by using the private extern __dyld_func_lookup() function to get the
214 // address of __dyld_fork_child (the 'C' code equivlent):
216 // _dyld_func_lookup("__dyld_fork_child", &address);
221 .ascii "__dyld_fork_child\0"
224 leaq 8(%rsp),%rsi // get the address where we're going to store the pointer
225 leaq LC0(%rip), %rdi // copy the name of the function to look up
226 call __dyld_func_lookup
227 call *8(%rsp) // call __dyld_fork_child indirectly
230 REG_TO_EXTERN(%rax, __current_pid)
231 CALL_EXTERN(__cthread_fork_child)
233 xorq %rax,%rax // zero rax
234 addq $24, %rsp // restore the stack
239 movl %eax, 16(%rsp) // save pid
241 CALL_EXTERN_AGAIN(__cthread_fork_parent)
242 movl 16(%rsp), %eax // return pid
243 addq $24, %rsp // restore the stack
247 #error Unsupported architecture