xref: /xnu-10063.121.3/libsyscall/custom/__vfork.s (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions/*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions/* Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
29*2c2f96dcSApple OSS Distributions *
30*2c2f96dcSApple OSS Distributions *	File:	libc/ppc/sys/vfork.s
31*2c2f96dcSApple OSS Distributions *
32*2c2f96dcSApple OSS Distributions * HISTORY
33*2c2f96dcSApple OSS Distributions * 23-Jun-1998	Umesh Vaishampayan ([email protected])
34*2c2f96dcSApple OSS Distributions *	Created from fork.s
35*2c2f96dcSApple OSS Distributions *
36*2c2f96dcSApple OSS Distributions */
37*2c2f96dcSApple OSS Distributions
38*2c2f96dcSApple OSS Distributions#include "SYS.h"
39*2c2f96dcSApple OSS Distributions
40*2c2f96dcSApple OSS Distributions#if defined(__i386__)
41*2c2f96dcSApple OSS Distributions
42*2c2f96dcSApple OSS Distributions#if defined(__DYNAMIC__)
43*2c2f96dcSApple OSS Distributions#define GET_CURRENT_PID	PICIFY(__current_pid)
44*2c2f96dcSApple OSS Distributions
45*2c2f96dcSApple OSS Distributions        NON_LAZY_STUB(__current_pid)
46*2c2f96dcSApple OSS Distributions#define __current_pid	(%edx)
47*2c2f96dcSApple OSS Distributions#else
48*2c2f96dcSApple OSS Distributions#define GET_CURRENT_PID
49*2c2f96dcSApple OSS Distributions#endif
50*2c2f96dcSApple OSS Distributions
51*2c2f96dcSApple OSS Distributions/*
52*2c2f96dcSApple OSS Distributions * If __current_pid >= 0, we want to put a -1 in there
53*2c2f96dcSApple OSS Distributions * otherwise we just decrement it
54*2c2f96dcSApple OSS Distributions */
55*2c2f96dcSApple OSS Distributions
56*2c2f96dcSApple OSS DistributionsLEAF(___vfork, 0)
57*2c2f96dcSApple OSS Distributions	GET_CURRENT_PID
58*2c2f96dcSApple OSS Distributions	movl		__current_pid, %eax
59*2c2f96dcSApple OSS Distributions0:
60*2c2f96dcSApple OSS Distributions	xorl		%ecx, %ecx
61*2c2f96dcSApple OSS Distributions	testl		%eax, %eax
62*2c2f96dcSApple OSS Distributions	cmovs		%eax, %ecx
63*2c2f96dcSApple OSS Distributions	decl		%ecx
64*2c2f96dcSApple OSS Distributions	lock
65*2c2f96dcSApple OSS Distributions	cmpxchgl	%ecx, __current_pid
66*2c2f96dcSApple OSS Distributions	jne		0b
67*2c2f96dcSApple OSS Distributions	popl		%ecx
68*2c2f96dcSApple OSS Distributions	movl		$(SYS_vfork), %eax	// code for vfork -> eax
69*2c2f96dcSApple OSS Distributions	UNIX_SYSCALL_TRAP			// do the system call
70*2c2f96dcSApple OSS Distributions	jnb		L1                     	// jump if CF==0
71*2c2f96dcSApple OSS Distributions	GET_CURRENT_PID
72*2c2f96dcSApple OSS Distributions	lock
73*2c2f96dcSApple OSS Distributions	incl		__current_pid
74*2c2f96dcSApple OSS Distributions	pushl		%ecx
75*2c2f96dcSApple OSS Distributions	BRANCH_EXTERN(tramp_cerror)
76*2c2f96dcSApple OSS Distributions
77*2c2f96dcSApple OSS DistributionsL1:
78*2c2f96dcSApple OSS Distributions	testl		%edx, %edx		// CF=OF=0,  ZF set if zero result
79*2c2f96dcSApple OSS Distributions	jz		L2			// parent, since r1 == 0 in parent, 1 in child
80*2c2f96dcSApple OSS Distributions	xorl		%eax, %eax		// zero eax
81*2c2f96dcSApple OSS Distributions	jmp		*%ecx
82*2c2f96dcSApple OSS Distributions
83*2c2f96dcSApple OSS DistributionsL2:
84*2c2f96dcSApple OSS Distributions	GET_CURRENT_PID
85*2c2f96dcSApple OSS Distributions	lock
86*2c2f96dcSApple OSS Distributions	incl		__current_pid
87*2c2f96dcSApple OSS Distributions	jmp		*%ecx
88*2c2f96dcSApple OSS Distributions
89*2c2f96dcSApple OSS Distributions#elif defined(__x86_64__)
90*2c2f96dcSApple OSS Distributions
91*2c2f96dcSApple OSS Distributions/*
92*2c2f96dcSApple OSS Distributions * If __current_pid >= 0, we want to put a -1 in there
93*2c2f96dcSApple OSS Distributions * otherwise we just decrement it
94*2c2f96dcSApple OSS Distributions */
95*2c2f96dcSApple OSS Distributions
96*2c2f96dcSApple OSS DistributionsLEAF(___vfork, 0)
97*2c2f96dcSApple OSS Distributions	movq		__current_pid@GOTPCREL(%rip), %rax
98*2c2f96dcSApple OSS Distributions	movl		(%rax), %eax
99*2c2f96dcSApple OSS Distributions0:
100*2c2f96dcSApple OSS Distributions	xorl		%ecx, %ecx
101*2c2f96dcSApple OSS Distributions	testl		%eax, %eax
102*2c2f96dcSApple OSS Distributions	cmovs		%eax, %ecx
103*2c2f96dcSApple OSS Distributions	subl		$1, %ecx
104*2c2f96dcSApple OSS Distributions	movq		__current_pid@GOTPCREL(%rip), %rdx
105*2c2f96dcSApple OSS Distributions	lock
106*2c2f96dcSApple OSS Distributions	cmpxchgl	%ecx, (%rdx)
107*2c2f96dcSApple OSS Distributions	jne		0b
108*2c2f96dcSApple OSS Distributions	popq		%rdi			// return address in %rdi
109*2c2f96dcSApple OSS Distributions	movq		$ SYSCALL_CONSTRUCT_UNIX(SYS_vfork), %rax	// code for vfork -> rax
110*2c2f96dcSApple OSS Distributions	UNIX_SYSCALL_TRAP			// do the system call
111*2c2f96dcSApple OSS Distributions	jnb		L1			// jump if CF==0
112*2c2f96dcSApple OSS Distributions	pushq		%rdi			// put return address back on stack for cerror
113*2c2f96dcSApple OSS Distributions	movq		__current_pid@GOTPCREL(%rip), %rcx
114*2c2f96dcSApple OSS Distributions	lock
115*2c2f96dcSApple OSS Distributions	addl		$1, (%rcx)
116*2c2f96dcSApple OSS Distributions	movq		%rax, %rdi
117*2c2f96dcSApple OSS Distributions	BRANCH_EXTERN(_cerror)
118*2c2f96dcSApple OSS Distributions
119*2c2f96dcSApple OSS DistributionsL1:
120*2c2f96dcSApple OSS Distributions	testl		%edx, %edx		// CF=OF=0,  ZF set if zero result
121*2c2f96dcSApple OSS Distributions	jz		L2			// parent, since r1 == 0 in parent, 1 in child
122*2c2f96dcSApple OSS Distributions	xorq		%rax, %rax		// zero rax
123*2c2f96dcSApple OSS Distributions	jmp		*%rdi
124*2c2f96dcSApple OSS Distributions
125*2c2f96dcSApple OSS DistributionsL2:
126*2c2f96dcSApple OSS Distributions	movq		__current_pid@GOTPCREL(%rip), %rdx
127*2c2f96dcSApple OSS Distributions	lock
128*2c2f96dcSApple OSS Distributions	addl		$1, (%rdx)
129*2c2f96dcSApple OSS Distributions	jmp		*%rdi
130*2c2f96dcSApple OSS Distributions	UNWIND_EPILOGUE
131*2c2f96dcSApple OSS Distributions
132*2c2f96dcSApple OSS Distributions#elif defined(__arm__)
133*2c2f96dcSApple OSS Distributions
134*2c2f96dcSApple OSS Distributions#include <arm/arch.h>
135*2c2f96dcSApple OSS Distributions
136*2c2f96dcSApple OSS Distributions	.globl	cerror
137*2c2f96dcSApple OSS Distributions	MI_ENTRY_POINT(___vfork)
138*2c2f96dcSApple OSS Distributions
139*2c2f96dcSApple OSS Distributions	MI_GET_ADDRESS(r3, __current_pid)	// get address of __current_pid
140*2c2f96dcSApple OSS Distributions#ifdef _ARM_ARCH_6
141*2c2f96dcSApple OSS DistributionsL0:
142*2c2f96dcSApple OSS Distributions	ldrex	r1, [r3]
143*2c2f96dcSApple OSS Distributions	subs	r1, r1, #1			// if __current_pid <= 0, decrement it
144*2c2f96dcSApple OSS Distributions	movpl	r1, #-1				// otherwise put -1 in there
145*2c2f96dcSApple OSS Distributions	strex	r2, r1, [r3]
146*2c2f96dcSApple OSS Distributions	cmp	r2, #0
147*2c2f96dcSApple OSS Distributions	bne	L0
148*2c2f96dcSApple OSS Distributions#else
149*2c2f96dcSApple OSS Distributions	mov	r2, #0x80000000			// load "looking" value
150*2c2f96dcSApple OSS DistributionsL0:
151*2c2f96dcSApple OSS Distributions	swp	r1, r2, [r3]			// look at the value, lock others out
152*2c2f96dcSApple OSS Distributions	cmp	r1, r2				// anyone else trying to look?
153*2c2f96dcSApple OSS Distributions	beq	L0				// yes, so wait our turn
154*2c2f96dcSApple OSS Distributions        subs    r1, r1, #1                      // if __current_pid <= 0, decrement it
155*2c2f96dcSApple OSS Distributions	movpl   r1, #-1                         // otherwise put -1 in there
156*2c2f96dcSApple OSS Distributions	str	r1, [r3]
157*2c2f96dcSApple OSS Distributions#endif
158*2c2f96dcSApple OSS Distributions
159*2c2f96dcSApple OSS Distributions	mov	r1, #1					// prime results
160*2c2f96dcSApple OSS Distributions	mov	r12, #SYS_vfork
161*2c2f96dcSApple OSS Distributions	swi	#SWI_SYSCALL				// make the syscall
162*2c2f96dcSApple OSS Distributions	bcs	Lbotch					// error?
163*2c2f96dcSApple OSS Distributions	cmp	r1, #0					// parent (r1=0) or child(r1=1)
164*2c2f96dcSApple OSS Distributions	beq	Lparent
165*2c2f96dcSApple OSS Distributions
166*2c2f96dcSApple OSS Distributions	//child here...
167*2c2f96dcSApple OSS Distributions	mov	r0, #0
168*2c2f96dcSApple OSS Distributions	bx	lr					// return
169*2c2f96dcSApple OSS Distributions
170*2c2f96dcSApple OSS DistributionsLbotch:
171*2c2f96dcSApple OSS Distributions	stmfd	sp!, {lr}
172*2c2f96dcSApple OSS Distributions	MI_CALL_EXTERNAL(_cerror)			// jump here on error
173*2c2f96dcSApple OSS Distributions	mov	r0,#-1					// set the error
174*2c2f96dcSApple OSS Distributions	// reload values clobbered by cerror (so we can treat them as live in Lparent)
175*2c2f96dcSApple OSS Distributions	MI_GET_ADDRESS(r3, __current_pid)		// get address of __current_pid
176*2c2f96dcSApple OSS Distributions	ldmfd   sp!, {lr}
177*2c2f96dcSApple OSS Distributions#ifndef _ARM_ARCH_6
178*2c2f96dcSApple OSS Distributions	mov	r2, #0x80000000			// load "looking" value
179*2c2f96dcSApple OSS Distributions#endif
180*2c2f96dcSApple OSS Distributions	// fall thru
181*2c2f96dcSApple OSS Distributions
182*2c2f96dcSApple OSS DistributionsLparent:
183*2c2f96dcSApple OSS Distributions#ifdef _ARM_ARCH_6
184*2c2f96dcSApple OSS Distributions	ldrex	r1, [r3]
185*2c2f96dcSApple OSS Distributions	add	r1, r1, #1			// we're back, decrement vfork count
186*2c2f96dcSApple OSS Distributions	strex	r2, r1, [r3]
187*2c2f96dcSApple OSS Distributions	cmp	r2, #0
188*2c2f96dcSApple OSS Distributions	bne	Lparent
189*2c2f96dcSApple OSS Distributions#else
190*2c2f96dcSApple OSS Distributions	swp	r1, r2, [r3]			// look at the value, lock others out
191*2c2f96dcSApple OSS Distributions	cmp	r1, r2				// anyone else trying to look?
192*2c2f96dcSApple OSS Distributions	beq	Lparent				// yes, so wait our turn
193*2c2f96dcSApple OSS Distributions	add	r1, r1, #1			// we're back, decrement vfork count
194*2c2f96dcSApple OSS Distributions	str	r1, [r3]
195*2c2f96dcSApple OSS Distributions#endif
196*2c2f96dcSApple OSS Distributions
197*2c2f96dcSApple OSS Distributions	bx	lr					// return
198*2c2f96dcSApple OSS Distributions
199*2c2f96dcSApple OSS Distributions#elif defined(__arm64__)
200*2c2f96dcSApple OSS Distributions
201*2c2f96dcSApple OSS Distributions	MI_ENTRY_POINT(___vfork)
202*2c2f96dcSApple OSS Distributions	ARM64_STACK_PROLOG
203*2c2f96dcSApple OSS Distributions
204*2c2f96dcSApple OSS Distributions	MI_GET_ADDRESS(x9, __current_pid)
205*2c2f96dcSApple OSS DistributionsLtry_set_vfork:
206*2c2f96dcSApple OSS Distributions	ldxr	w10, [x9]			// Get old current pid value (exclusive)
207*2c2f96dcSApple OSS Distributions	mov		w11, #-1			// Will be -1 if current value is positive
208*2c2f96dcSApple OSS Distributions	subs	w10, w10, #1		// Subtract one
209*2c2f96dcSApple OSS Distributions	csel	w12, w11, w10, pl	// If >= 0, set to -1, else set to (current - 1)
210*2c2f96dcSApple OSS Distributions	stxr	w13, w12, [x9]		// Attempt exclusive store to current pid
211*2c2f96dcSApple OSS Distributions	cbnz	w13, Ltry_set_vfork	// If store failed, retry
212*2c2f96dcSApple OSS Distributions
213*2c2f96dcSApple OSS Distributions	// ARM sets r1 to 1 here.  I don't see why.
214*2c2f96dcSApple OSS Distributions	mov		w16, #SYS_vfork		// Set syscall code
215*2c2f96dcSApple OSS Distributions	svc		#SWI_SYSCALL
216*2c2f96dcSApple OSS Distributions	b.cs 	Lbotch
217*2c2f96dcSApple OSS Distributions	cbz		w1, Lparent
218*2c2f96dcSApple OSS Distributions
219*2c2f96dcSApple OSS Distributions	// Child
220*2c2f96dcSApple OSS Distributions	mov		w0, #0
221*2c2f96dcSApple OSS Distributions	ARM64_STACK_EPILOG
222*2c2f96dcSApple OSS Distributions
223*2c2f96dcSApple OSS Distributions	// Error case
224*2c2f96dcSApple OSS DistributionsLbotch:
225*2c2f96dcSApple OSS Distributions	PUSH_FRAME
226*2c2f96dcSApple OSS Distributions	bl 		_cerror				// Update errno
227*2c2f96dcSApple OSS Distributions	mov		w0, #-1				// Set return value
228*2c2f96dcSApple OSS Distributions	MI_GET_ADDRESS(x9, __current_pid) // Reload current pid address
229*2c2f96dcSApple OSS Distributions	POP_FRAME
230*2c2f96dcSApple OSS Distributions	// Fall through
231*2c2f96dcSApple OSS DistributionsLparent:
232*2c2f96dcSApple OSS Distributions	ldxr	w10, [x9]			// Exclusive load current pid value
233*2c2f96dcSApple OSS Distributions	add		w10, w10, #1		// Increment (i.e. decrement vfork count)
234*2c2f96dcSApple OSS Distributions	stxr	w11, w10, [x9]		// Attempt exclusive store of updated vfork count
235*2c2f96dcSApple OSS Distributions	cbnz	w11, Lparent		// If exclusive store failed, retry
236*2c2f96dcSApple OSS Distributions	ARM64_STACK_EPILOG		// Done, return
237*2c2f96dcSApple OSS Distributions
238*2c2f96dcSApple OSS Distributions#else
239*2c2f96dcSApple OSS Distributions#error Unsupported architecture
240*2c2f96dcSApple OSS Distributions#endif
241