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) 1998 Apple Computer, Inc. All rights reserved.
30 * File: libc/ppc/sys/vfork.s
33 * 23-Jun-1998 Umesh Vaishampayan (umeshv@apple.com)
40 #if defined(__ppc__) || defined(__ppc64__)
42 /* We use mode-independent "g" opcodes such as "srgi", and/or
43 * mode-independent macros such as MI_GET_ADDRESS. These expand
44 * into word operations when targeting __ppc__, and into doubleword
45 * operations when targeting __ppc64__.
47 #include <architecture/ppc/mode_independent_asm.h>
49 /* In vfork(), the child runs in parent's address space. */
52 MI_ENTRY_POINT(___vfork)
53 MI_GET_ADDRESS(r5,__current_pid) // get address of __current_pid in r5
55 lwarx r6,0,r5 // don't cache pid across vfork
57 ble-- 3f // is another vfork in progress
58 li r6,0 // if not, erase the stored pid
60 addi r6,r6,-1 // count the parallel vforks in
61 stwcx. r6,0,r5 // negative cached pid values
66 b Lbotch // error return
69 beq Lparent // parent, since a1 == 0 in parent,
74 Lparent: // r3 == child's pid
75 lwarx r6,0,r5 // we're back, decrement vfork count
82 lwarx r6,0,r5 // never went, decrement vfork count
87 MI_BRANCH_EXTERNAL(cerror)
89 #elif defined(__i386__)
91 #if defined(__DYNAMIC__)
92 #define GET_CURRENT_PID PICIFY(__current_pid)
94 NON_LAZY_STUB(__current_pid)
95 #define __current_pid (%edx)
97 #define GET_CURRENT_PID
101 * If __current_pid >= 0, we want to put a -1 in there
102 * otherwise we just decrement it
107 movl __current_pid, %eax
114 cmpxchgl %ecx, __current_pid
117 movl $(SYS_vfork), %eax // code for vfork -> eax
118 UNIX_SYSCALL_TRAP // do the system call
119 jnb L1 // jump if CF==0
124 BRANCH_EXTERN(cerror)
127 testl %edx, %edx // CF=OF=0, ZF set if zero result
128 jz L2 // parent, since r1 == 0 in parent, 1 in child
129 xorl %eax, %eax // zero eax
138 #elif defined(__x86_64__)
141 * If __current_pid >= 0, we want to put a -1 in there
142 * otherwise we just decrement it
146 movq __current_pid@GOTPCREL(%rip), %rax
153 movq __current_pid@GOTPCREL(%rip), %rdx
155 cmpxchgl %ecx, (%rdx)
157 popq %rdi // return address in %rdi
158 movq $ SYSCALL_CONSTRUCT_UNIX(SYS_vfork), %rax // code for vfork -> rax
159 UNIX_SYSCALL_TRAP // do the system call
160 jnb L1 // jump if CF==0
161 movq __current_pid@GOTPCREL(%rip), %rcx
165 BRANCH_EXTERN(cerror)
168 testl %edx, %edx // CF=OF=0, ZF set if zero result
169 jz L2 // parent, since r1 == 0 in parent, 1 in child
170 xorq %rax, %rax // zero rax
174 movq __current_pid@GOTPCREL(%rip), %rdx
179 #elif defined(__arm__)
181 #include <arm/arch.h>
184 MI_ENTRY_POINT(_vfork)
186 MI_GET_ADDRESS(r3, __current_pid) // get address of __current_pid
190 subs r1, r1, #1 // if __current_pid <= 0, decrement it
191 movpl r1, #-1 // otherwise put -1 in there
196 mov r2, #0x80000000 // load "looking" value
198 swp r1, r2, [r3] // look at the value, lock others out
199 cmp r1, r2 // anyone else trying to look?
200 beq L0 // yes, so wait our turn
201 subs r1, r1, #1 // if __current_pid <= 0, decrement it
202 movpl r1, #-1 // otherwise put -1 in there
206 mov r1, #1 // prime results
208 swi #SWI_SYSCALL // make the syscall
210 cmp r1, #0 // parent (r1=0) or child(r1=1)
218 MI_CALL_EXTERNAL(cerror) // jump here on error
219 mov r0,#-1 // set the error
220 // reload values clobbered by cerror (so we can treat them as live in Lparent)
221 MI_GET_ADDRESS(r3, __current_pid) // get address of __current_pid
223 mov r2, #0x80000000 // load "looking" value
230 add r1, r1, #1 // we're back, decrement vfork count
235 swp r1, r2, [r3] // look at the value, lock others out
236 cmp r1, r2 // anyone else trying to look?
237 beq Lparent // yes, so wait our turn
238 add r1, r1, #1 // we're back, decrement vfork count
245 #error Unsupported architecture