]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/custom/__vfork.s
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libsyscall / custom / __vfork.s
CommitLineData
2d21ac55
A
1/*
2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
29 *
30 * File: libc/ppc/sys/vfork.s
31 *
32 * HISTORY
33 * 23-Jun-1998 Umesh Vaishampayan (umeshv@apple.com)
34 * Created from fork.s
35 *
36 */
37
38#include "SYS.h"
39
6d2010ae 40#if defined(__i386__)
2d21ac55
A
41
42#if defined(__DYNAMIC__)
43#define GET_CURRENT_PID PICIFY(__current_pid)
44
45 NON_LAZY_STUB(__current_pid)
46#define __current_pid (%edx)
47#else
48#define GET_CURRENT_PID
49#endif
50
51/*
52 * If __current_pid >= 0, we want to put a -1 in there
53 * otherwise we just decrement it
54 */
55
56LEAF(___vfork, 0)
57 GET_CURRENT_PID
58 movl __current_pid, %eax
590:
60 xorl %ecx, %ecx
61 testl %eax, %eax
62 cmovs %eax, %ecx
63 decl %ecx
64 lock
65 cmpxchgl %ecx, __current_pid
66 jne 0b
67 popl %ecx
68 movl $(SYS_vfork), %eax // code for vfork -> eax
69 UNIX_SYSCALL_TRAP // do the system call
70 jnb L1 // jump if CF==0
71 GET_CURRENT_PID
72 lock
73 incl __current_pid
74 pushl %ecx
39236c6e 75 BRANCH_EXTERN(tramp_cerror)
2d21ac55
A
76
77L1:
78 testl %edx, %edx // CF=OF=0, ZF set if zero result
79 jz L2 // parent, since r1 == 0 in parent, 1 in child
80 xorl %eax, %eax // zero eax
81 jmp *%ecx
82
83L2:
84 GET_CURRENT_PID
85 lock
86 incl __current_pid
87 jmp *%ecx
88
89#elif defined(__x86_64__)
90
91/*
92 * If __current_pid >= 0, we want to put a -1 in there
93 * otherwise we just decrement it
94 */
95
96LEAF(___vfork, 0)
97 movq __current_pid@GOTPCREL(%rip), %rax
98 movl (%rax), %eax
990:
100 xorl %ecx, %ecx
101 testl %eax, %eax
102 cmovs %eax, %ecx
103 subl $1, %ecx
104 movq __current_pid@GOTPCREL(%rip), %rdx
105 lock
106 cmpxchgl %ecx, (%rdx)
107 jne 0b
108 popq %rdi // return address in %rdi
109 movq $ SYSCALL_CONSTRUCT_UNIX(SYS_vfork), %rax // code for vfork -> rax
110 UNIX_SYSCALL_TRAP // do the system call
39236c6e 111 jnb L1 // jump if CF==0
b0d623f7 112 pushq %rdi // put return address back on stack for cerror
2d21ac55
A
113 movq __current_pid@GOTPCREL(%rip), %rcx
114 lock
39037602 115 addl $1, (%rcx)
39236c6e
A
116 movq %rax, %rdi
117 BRANCH_EXTERN(_cerror)
2d21ac55
A
118
119L1:
120 testl %edx, %edx // CF=OF=0, ZF set if zero result
121 jz L2 // parent, since r1 == 0 in parent, 1 in child
122 xorq %rax, %rax // zero rax
123 jmp *%rdi
124
125L2:
126 movq __current_pid@GOTPCREL(%rip), %rdx
127 lock
39037602 128 addl $1, (%rdx)
2d21ac55
A
129 jmp *%rdi
130
5ba3f43e
A
131#elif defined(__arm__)
132
133#include <arm/arch.h>
134
135 .globl cerror
136 MI_ENTRY_POINT(___vfork)
137
138 MI_GET_ADDRESS(r3, __current_pid) // get address of __current_pid
139#ifdef _ARM_ARCH_6
140L0:
141 ldrex r1, [r3]
142 subs r1, r1, #1 // if __current_pid <= 0, decrement it
143 movpl r1, #-1 // otherwise put -1 in there
144 strex r2, r1, [r3]
145 cmp r2, #0
146 bne L0
147#else
148 mov r2, #0x80000000 // load "looking" value
149L0:
150 swp r1, r2, [r3] // look at the value, lock others out
151 cmp r1, r2 // anyone else trying to look?
152 beq L0 // yes, so wait our turn
153 subs r1, r1, #1 // if __current_pid <= 0, decrement it
154 movpl r1, #-1 // otherwise put -1 in there
155 str r1, [r3]
156#endif
157
158 mov r1, #1 // prime results
159 mov r12, #SYS_vfork
160 swi #SWI_SYSCALL // make the syscall
161 bcs Lbotch // error?
162 cmp r1, #0 // parent (r1=0) or child(r1=1)
163 beq Lparent
164
165 //child here...
166 mov r0, #0
167 bx lr // return
168
169Lbotch:
f427ee49 170 stmfd sp!, {lr}
5ba3f43e
A
171 MI_CALL_EXTERNAL(_cerror) // jump here on error
172 mov r0,#-1 // set the error
173 // reload values clobbered by cerror (so we can treat them as live in Lparent)
174 MI_GET_ADDRESS(r3, __current_pid) // get address of __current_pid
f427ee49 175 ldmfd sp!, {lr}
5ba3f43e
A
176#ifndef _ARM_ARCH_6
177 mov r2, #0x80000000 // load "looking" value
178#endif
179 // fall thru
180
181Lparent:
182#ifdef _ARM_ARCH_6
183 ldrex r1, [r3]
184 add r1, r1, #1 // we're back, decrement vfork count
185 strex r2, r1, [r3]
186 cmp r2, #0
187 bne Lparent
188#else
189 swp r1, r2, [r3] // look at the value, lock others out
190 cmp r1, r2 // anyone else trying to look?
191 beq Lparent // yes, so wait our turn
192 add r1, r1, #1 // we're back, decrement vfork count
193 str r1, [r3]
194#endif
195
196 bx lr // return
197
198#elif defined(__arm64__)
199
200 MI_ENTRY_POINT(___vfork)
f427ee49 201 ARM64_STACK_PROLOG
5ba3f43e
A
202
203 MI_GET_ADDRESS(x9, __current_pid)
204Ltry_set_vfork:
205 ldxr w10, [x9] // Get old current pid value (exclusive)
206 mov w11, #-1 // Will be -1 if current value is positive
207 subs w10, w10, #1 // Subtract one
208 csel w12, w11, w10, pl // If >= 0, set to -1, else set to (current - 1)
209 stxr w13, w12, [x9] // Attempt exclusive store to current pid
210 cbnz w13, Ltry_set_vfork // If store failed, retry
211
212 // ARM sets r1 to 1 here. I don't see why.
213 mov w16, #SYS_vfork // Set syscall code
214 svc #SWI_SYSCALL
215 b.cs Lbotch
216 cbz w1, Lparent
217
218 // Child
219 mov w0, #0
f427ee49 220 ARM64_STACK_EPILOG
5ba3f43e
A
221
222 // Error case
223Lbotch:
f427ee49 224 PUSH_FRAME
5ba3f43e
A
225 bl _cerror // Update errno
226 mov w0, #-1 // Set return value
227 MI_GET_ADDRESS(x9, __current_pid) // Reload current pid address
f427ee49 228 POP_FRAME
5ba3f43e
A
229 // Fall through
230Lparent:
231 ldxr w10, [x9] // Exclusive load current pid value
232 add w10, w10, #1 // Increment (i.e. decrement vfork count)
233 stxr w11, w10, [x9] // Attempt exclusive store of updated vfork count
234 cbnz w11, Lparent // If exclusive store failed, retry
f427ee49 235 ARM64_STACK_EPILOG // Done, return
5ba3f43e 236
2d21ac55
A
237#else
238#error Unsupported architecture
239#endif