xref: /xnu-10063.121.3/libsyscall/custom/__fork.s (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions/*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 1999-2010 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) 1992 NeXT Computer, Inc.  All rights reserved.
29*2c2f96dcSApple OSS Distributions *
30*2c2f96dcSApple OSS Distributions *	File:	libc/ppc/sys/fork.s
31*2c2f96dcSApple OSS Distributions *
32*2c2f96dcSApple OSS Distributions * HISTORY
33*2c2f96dcSApple OSS Distributions * 18-Nov-92  Ben Fathi ([email protected])
34*2c2f96dcSApple OSS Distributions *	Created from M88K sources
35*2c2f96dcSApple OSS Distributions *
36*2c2f96dcSApple OSS Distributions * 11-Jan-92  Peter King ([email protected])
37*2c2f96dcSApple OSS Distributions *	Created from M68K sources
38*2c2f96dcSApple OSS Distributions */
39*2c2f96dcSApple OSS Distributions
40*2c2f96dcSApple OSS Distributions/*
41*2c2f96dcSApple OSS Distributions * All of the asm stubs in this file have been adjusted so the pre/post
42*2c2f96dcSApple OSS Distributions * fork handlers and dyld fixup are done in C inside Libc. As such, Libc
43*2c2f96dcSApple OSS Distributions * expects the __fork asm to fix up the return code to be -1, 0 or pid
44*2c2f96dcSApple OSS Distributions * and errno if needed.
45*2c2f96dcSApple OSS Distributions */
46*2c2f96dcSApple OSS Distributions
47*2c2f96dcSApple OSS Distributions#include "SYS.h"
48*2c2f96dcSApple OSS Distributions
49*2c2f96dcSApple OSS Distributions#if defined(__i386__)
50*2c2f96dcSApple OSS Distributions
51*2c2f96dcSApple OSS DistributionsLEAF(___fork, 0)
52*2c2f96dcSApple OSS Distributions	subl  $28, %esp   // Align the stack, with 16 bytes of extra padding that we'll need
53*2c2f96dcSApple OSS Distributions
54*2c2f96dcSApple OSS Distributions	movl 	$ SYS_fork,%eax; 	// code for fork -> eax
55*2c2f96dcSApple OSS Distributions	UNIX_SYSCALL_TRAP		// do the system call
56*2c2f96dcSApple OSS Distributions	jnc	L1			// jump if CF==0
57*2c2f96dcSApple OSS Distributions
58*2c2f96dcSApple OSS Distributions	CALL_EXTERN(tramp_cerror)
59*2c2f96dcSApple OSS Distributions	movl	$-1,%eax
60*2c2f96dcSApple OSS Distributions	addl	$28, %esp   // restore the stack
61*2c2f96dcSApple OSS Distributions	ret
62*2c2f96dcSApple OSS Distributions
63*2c2f96dcSApple OSS DistributionsL1:
64*2c2f96dcSApple OSS Distributions	orl	%edx,%edx	// CF=OF=0,  ZF set if zero result
65*2c2f96dcSApple OSS Distributions	jz	L2		// parent, since r1 == 0 in parent, 1 in child
66*2c2f96dcSApple OSS Distributions
67*2c2f96dcSApple OSS Distributions	//child here...
68*2c2f96dcSApple OSS Distributions	xorl	%eax,%eax	// zero eax
69*2c2f96dcSApple OSS Distributions	REG_TO_EXTERN(%eax, __current_pid);
70*2c2f96dcSApple OSS DistributionsL2:
71*2c2f96dcSApple OSS Distributions	addl	$28, %esp   // restore the stack
72*2c2f96dcSApple OSS Distributions	// parent ends up here skipping child portion
73*2c2f96dcSApple OSS Distributions	ret
74*2c2f96dcSApple OSS Distributions
75*2c2f96dcSApple OSS Distributions#elif defined(__x86_64__)
76*2c2f96dcSApple OSS Distributions
77*2c2f96dcSApple OSS DistributionsLEAF(___fork, 0)
78*2c2f96dcSApple OSS Distributions	subq  $24, %rsp   // Align the stack, plus room for local storage
79*2c2f96dcSApple OSS Distributions
80*2c2f96dcSApple OSS Distributions	movl 	$ SYSCALL_CONSTRUCT_UNIX(SYS_fork),%eax; // code for fork -> rax
81*2c2f96dcSApple OSS Distributions	UNIX_SYSCALL_TRAP		// do the system call
82*2c2f96dcSApple OSS Distributions	jnc	L1			// jump if CF==0
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions	movq	%rax, %rdi
85*2c2f96dcSApple OSS Distributions	CALL_EXTERN(_cerror)
86*2c2f96dcSApple OSS Distributions	movq	$-1, %rax
87*2c2f96dcSApple OSS Distributions	addq	$24, %rsp   // restore the stack
88*2c2f96dcSApple OSS Distributions	ret
89*2c2f96dcSApple OSS Distributions
90*2c2f96dcSApple OSS DistributionsL1:
91*2c2f96dcSApple OSS Distributions	orl	%edx,%edx	// CF=OF=0,  ZF set if zero result
92*2c2f96dcSApple OSS Distributions	jz	L2		// parent, since r1 == 0 in parent, 1 in child
93*2c2f96dcSApple OSS Distributions
94*2c2f96dcSApple OSS Distributions	//child here...
95*2c2f96dcSApple OSS Distributions	xorq	%rax, %rax
96*2c2f96dcSApple OSS Distributions	PICIFY(__current_pid)
97*2c2f96dcSApple OSS Distributions	movl	%eax,(%r11)
98*2c2f96dcSApple OSS DistributionsL2:
99*2c2f96dcSApple OSS Distributions	// parent ends up here skipping child portion
100*2c2f96dcSApple OSS Distributions	addq	$24, %rsp   // restore the stack
101*2c2f96dcSApple OSS Distributions	ret
102*2c2f96dcSApple OSS Distributions	UNWIND_EPILOGUE
103*2c2f96dcSApple OSS Distributions
104*2c2f96dcSApple OSS Distributions#elif defined(__arm__)
105*2c2f96dcSApple OSS Distributions
106*2c2f96dcSApple OSS DistributionsMI_ENTRY_POINT(___fork)
107*2c2f96dcSApple OSS Distributions	stmfd	sp!, {r4, r7, lr}
108*2c2f96dcSApple OSS Distributions	add	r7, sp, #4
109*2c2f96dcSApple OSS Distributions
110*2c2f96dcSApple OSS Distributions	mov	r1, #1					// prime results
111*2c2f96dcSApple OSS Distributions	mov	r12, #SYS_fork
112*2c2f96dcSApple OSS Distributions	swi	#SWI_SYSCALL				// make the syscall
113*2c2f96dcSApple OSS Distributions	bcs	Lbotch					// error?
114*2c2f96dcSApple OSS Distributions
115*2c2f96dcSApple OSS Distributions	cmp	r1, #0					// parent (r1=0) or child(r1=1)
116*2c2f96dcSApple OSS Distributions	beq	Lparent
117*2c2f96dcSApple OSS Distributions
118*2c2f96dcSApple OSS Distributions	//child here...
119*2c2f96dcSApple OSS Distributions	MI_GET_ADDRESS(r3, __current_pid)
120*2c2f96dcSApple OSS Distributions	mov	r0, #0
121*2c2f96dcSApple OSS Distributions	str	r0, [r3]		// clear cached pid in child
122*2c2f96dcSApple OSS Distributions	ldmfd   sp!, {r4, r7, pc}
123*2c2f96dcSApple OSS Distributions
124*2c2f96dcSApple OSS DistributionsLbotch:
125*2c2f96dcSApple OSS Distributions	MI_CALL_EXTERNAL(_cerror)			// jump here on error
126*2c2f96dcSApple OSS Distributions	mov	r0,#-1					// set the error
127*2c2f96dcSApple OSS Distributions	// fall thru
128*2c2f96dcSApple OSS DistributionsLparent:
129*2c2f96dcSApple OSS Distributions	ldmfd   sp!, {r4, r7, pc}			// pop and return
130*2c2f96dcSApple OSS Distributions
131*2c2f96dcSApple OSS Distributions#elif defined(__arm64__)
132*2c2f96dcSApple OSS Distributions
133*2c2f96dcSApple OSS Distributions#include <mach/arm64/asm.h>
134*2c2f96dcSApple OSS Distributions
135*2c2f96dcSApple OSS DistributionsMI_ENTRY_POINT(___fork)
136*2c2f96dcSApple OSS Distributions	ARM64_STACK_PROLOG
137*2c2f96dcSApple OSS Distributions	PUSH_FRAME
138*2c2f96dcSApple OSS Distributions	// ARM moves a 1 in to r1 here, but I can't see why.
139*2c2f96dcSApple OSS Distributions	mov		x16, #SYS_fork				// Syscall code
140*2c2f96dcSApple OSS Distributions	svc		#SWI_SYSCALL				// Trap to kernel
141*2c2f96dcSApple OSS Distributions	b.cs	Lbotch						// Carry bit indicates failure
142*2c2f96dcSApple OSS Distributions	cbz		x1, Lparent					// x1 == 0 indicates that we are the parent
143*2c2f96dcSApple OSS Distributions
144*2c2f96dcSApple OSS Distributions	// Child
145*2c2f96dcSApple OSS Distributions	MI_GET_ADDRESS(x9, __current_pid)	// Get address of cached "current pid"
146*2c2f96dcSApple OSS Distributions	mov		w0, #0
147*2c2f96dcSApple OSS Distributions	str		w0, [x9]					// Clear cached current pid
148*2c2f96dcSApple OSS Distributions	POP_FRAME							// And done
149*2c2f96dcSApple OSS Distributions	ARM64_STACK_EPILOG
150*2c2f96dcSApple OSS Distributions
151*2c2f96dcSApple OSS DistributionsLbotch:
152*2c2f96dcSApple OSS Distributions	MI_CALL_EXTERNAL(_cerror)			// Handle error
153*2c2f96dcSApple OSS Distributions	mov		w0, #-1						// Return value is -1
154*2c2f96dcSApple OSS DistributionsLparent:
155*2c2f96dcSApple OSS Distributions	POP_FRAME							// Return
156*2c2f96dcSApple OSS Distributions	ARM64_STACK_EPILOG
157*2c2f96dcSApple OSS Distributions
158*2c2f96dcSApple OSS Distributions#else
159*2c2f96dcSApple OSS Distributions#error Unsupported architecture
160*2c2f96dcSApple OSS Distributions#endif
161