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