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
180 #error Unsupported architecture