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
40#if defined(__ppc__) || defined(__ppc64__)
41
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__.
46 */
47#include <architecture/ppc/mode_independent_asm.h>
48
49/* In vfork(), the child runs in parent's address space.  */
50
51
52MI_ENTRY_POINT(___vfork)
53    MI_GET_ADDRESS(r5,__current_pid)  // get address of __current_pid in r5
542:
55	lwarx	r6,0,r5			// don't cache pid across vfork
56	cmpwi	r6,0
57	ble--	3f              // is another vfork in progress
58	li      r6,0			// if not, erase the stored pid
593:
60	addi	r6,r6,-1		// count the parallel vforks in
61	stwcx.	r6,0,r5			// negative cached pid values
62	bne--	2b
63
64	li      r0,SYS_vfork
65	sc
66	b       Lbotch			// error return
67
68	cmpwi	r4,0
69	beq     Lparent			// parent, since a1 == 0 in parent,
70
71	li      r3,0			// child
72	blr
73
74Lparent:                    // r3 == child's pid
75	lwarx	r6,0,r5			// we're back, decrement vfork count
76	addi	r6,r6,1
77	stwcx.	r6,0,r5
78	bne--	Lparent
79	blr                     // return pid
80
81Lbotch:
82	lwarx	r6,0,r5			// never went, decrement vfork count
83	addi	r6,r6,1
84	stwcx.	r6,0,r5
85	bne--	Lbotch
86
87	MI_BRANCH_EXTERNAL(cerror)
88
89#elif defined(__i386__)
90
91#if defined(__DYNAMIC__)
92#define GET_CURRENT_PID	PICIFY(__current_pid)
93
94        NON_LAZY_STUB(__current_pid)
95#define __current_pid	(%edx)
96#else
97#define GET_CURRENT_PID
98#endif
99
100/*
101 * If __current_pid >= 0, we want to put a -1 in there
102 * otherwise we just decrement it
103 */
104
105LEAF(___vfork, 0)
106	GET_CURRENT_PID
107	movl		__current_pid, %eax
1080:
109	xorl		%ecx, %ecx
110	testl		%eax, %eax
111	cmovs		%eax, %ecx
112	decl		%ecx
113	lock
114	cmpxchgl	%ecx, __current_pid
115	jne		0b
116	popl		%ecx
117	movl		$(SYS_vfork), %eax	// code for vfork -> eax
118	UNIX_SYSCALL_TRAP			// do the system call
119	jnb		L1                     	// jump if CF==0
120	GET_CURRENT_PID
121	lock
122	incl		__current_pid
123	pushl		%ecx
124	BRANCH_EXTERN(cerror)
125
126L1:
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
130	jmp		*%ecx
131
132L2:
133	GET_CURRENT_PID
134	lock
135	incl		__current_pid
136	jmp		*%ecx
137
138#elif defined(__x86_64__)
139
140/*
141 * If __current_pid >= 0, we want to put a -1 in there
142 * otherwise we just decrement it
143 */
144
145LEAF(___vfork, 0)
146	movq		__current_pid@GOTPCREL(%rip), %rax
147	movl		(%rax), %eax
1480:
149	xorl		%ecx, %ecx
150	testl		%eax, %eax
151	cmovs		%eax, %ecx
152	subl		$1, %ecx
153	movq		__current_pid@GOTPCREL(%rip), %rdx
154	lock
155	cmpxchgl	%ecx, (%rdx)
156	jne		0b
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
162	lock
163	addq		$1, (%rcx)
164	movq		(%rcx), %rdi
165	BRANCH_EXTERN(cerror)
166
167L1:
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
171	jmp		*%rdi
172
173L2:
174	movq		__current_pid@GOTPCREL(%rip), %rdx
175	lock
176	addq		$1, (%rdx)
177	jmp		*%rdi
178
179#else
180#error Unsupported architecture
181#endif
182