xref: /xnu-10063.101.15/libsyscall/custom/__fork.s (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions/*
2*94d3b452SApple OSS Distributions * Copyright (c) 1999-2010 Apple Inc. All rights reserved.
3*94d3b452SApple OSS Distributions *
4*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*94d3b452SApple OSS Distributions *
6*94d3b452SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*94d3b452SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*94d3b452SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*94d3b452SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*94d3b452SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*94d3b452SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*94d3b452SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*94d3b452SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*94d3b452SApple OSS Distributions *
15*94d3b452SApple OSS Distributions * Please obtain a copy of the License at
16*94d3b452SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*94d3b452SApple OSS Distributions *
18*94d3b452SApple OSS Distributions * The Original Code and all software distributed under the License are
19*94d3b452SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*94d3b452SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*94d3b452SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*94d3b452SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*94d3b452SApple OSS Distributions * Please see the License for the specific language governing rights and
24*94d3b452SApple OSS Distributions * limitations under the License.
25*94d3b452SApple OSS Distributions *
26*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*94d3b452SApple OSS Distributions */
28*94d3b452SApple OSS Distributions/* Copyright (c) 1992 NeXT Computer, Inc.  All rights reserved.
29*94d3b452SApple OSS Distributions *
30*94d3b452SApple OSS Distributions *	File:	libc/ppc/sys/fork.s
31*94d3b452SApple OSS Distributions *
32*94d3b452SApple OSS Distributions * HISTORY
33*94d3b452SApple OSS Distributions * 18-Nov-92  Ben Fathi ([email protected])
34*94d3b452SApple OSS Distributions *	Created from M88K sources
35*94d3b452SApple OSS Distributions *
36*94d3b452SApple OSS Distributions * 11-Jan-92  Peter King ([email protected])
37*94d3b452SApple OSS Distributions *	Created from M68K sources
38*94d3b452SApple OSS Distributions */
39*94d3b452SApple OSS Distributions
40*94d3b452SApple OSS Distributions/*
41*94d3b452SApple OSS Distributions * All of the asm stubs in this file have been adjusted so the pre/post
42*94d3b452SApple OSS Distributions * fork handlers and dyld fixup are done in C inside Libc. As such, Libc
43*94d3b452SApple OSS Distributions * expects the __fork asm to fix up the return code to be -1, 0 or pid
44*94d3b452SApple OSS Distributions * and errno if needed.
45*94d3b452SApple OSS Distributions */
46*94d3b452SApple OSS Distributions
47*94d3b452SApple OSS Distributions#include "SYS.h"
48*94d3b452SApple OSS Distributions
49*94d3b452SApple OSS Distributions#if defined(__i386__)
50*94d3b452SApple OSS Distributions
51*94d3b452SApple OSS DistributionsLEAF(___fork, 0)
52*94d3b452SApple OSS Distributions	subl  $28, %esp   // Align the stack, with 16 bytes of extra padding that we'll need
53*94d3b452SApple OSS Distributions
54*94d3b452SApple OSS Distributions	movl 	$ SYS_fork,%eax; 	// code for fork -> eax
55*94d3b452SApple OSS Distributions	UNIX_SYSCALL_TRAP		// do the system call
56*94d3b452SApple OSS Distributions	jnc	L1			// jump if CF==0
57*94d3b452SApple OSS Distributions
58*94d3b452SApple OSS Distributions	CALL_EXTERN(tramp_cerror)
59*94d3b452SApple OSS Distributions	movl	$-1,%eax
60*94d3b452SApple OSS Distributions	addl	$28, %esp   // restore the stack
61*94d3b452SApple OSS Distributions	ret
62*94d3b452SApple OSS Distributions
63*94d3b452SApple OSS DistributionsL1:
64*94d3b452SApple OSS Distributions	orl	%edx,%edx	// CF=OF=0,  ZF set if zero result
65*94d3b452SApple OSS Distributions	jz	L2		// parent, since r1 == 0 in parent, 1 in child
66*94d3b452SApple OSS Distributions
67*94d3b452SApple OSS Distributions	//child here...
68*94d3b452SApple OSS Distributions	xorl	%eax,%eax	// zero eax
69*94d3b452SApple OSS Distributions	REG_TO_EXTERN(%eax, __current_pid);
70*94d3b452SApple OSS DistributionsL2:
71*94d3b452SApple OSS Distributions	addl	$28, %esp   // restore the stack
72*94d3b452SApple OSS Distributions	// parent ends up here skipping child portion
73*94d3b452SApple OSS Distributions	ret
74*94d3b452SApple OSS Distributions
75*94d3b452SApple OSS Distributions#elif defined(__x86_64__)
76*94d3b452SApple OSS Distributions
77*94d3b452SApple OSS DistributionsLEAF(___fork, 0)
78*94d3b452SApple OSS Distributions	subq  $24, %rsp   // Align the stack, plus room for local storage
79*94d3b452SApple OSS Distributions
80*94d3b452SApple OSS Distributions	movl 	$ SYSCALL_CONSTRUCT_UNIX(SYS_fork),%eax; // code for fork -> rax
81*94d3b452SApple OSS Distributions	UNIX_SYSCALL_TRAP		// do the system call
82*94d3b452SApple OSS Distributions	jnc	L1			// jump if CF==0
83*94d3b452SApple OSS Distributions
84*94d3b452SApple OSS Distributions	movq	%rax, %rdi
85*94d3b452SApple OSS Distributions	CALL_EXTERN(_cerror)
86*94d3b452SApple OSS Distributions	movq	$-1, %rax
87*94d3b452SApple OSS Distributions	addq	$24, %rsp   // restore the stack
88*94d3b452SApple OSS Distributions	ret
89*94d3b452SApple OSS Distributions
90*94d3b452SApple OSS DistributionsL1:
91*94d3b452SApple OSS Distributions	orl	%edx,%edx	// CF=OF=0,  ZF set if zero result
92*94d3b452SApple OSS Distributions	jz	L2		// parent, since r1 == 0 in parent, 1 in child
93*94d3b452SApple OSS Distributions
94*94d3b452SApple OSS Distributions	//child here...
95*94d3b452SApple OSS Distributions	xorq	%rax, %rax
96*94d3b452SApple OSS Distributions	PICIFY(__current_pid)
97*94d3b452SApple OSS Distributions	movl	%eax,(%r11)
98*94d3b452SApple OSS DistributionsL2:
99*94d3b452SApple OSS Distributions	// parent ends up here skipping child portion
100*94d3b452SApple OSS Distributions	addq	$24, %rsp   // restore the stack
101*94d3b452SApple OSS Distributions	ret
102*94d3b452SApple OSS Distributions	UNWIND_EPILOGUE
103*94d3b452SApple OSS Distributions
104*94d3b452SApple OSS Distributions#elif defined(__arm__)
105*94d3b452SApple OSS Distributions
106*94d3b452SApple OSS DistributionsMI_ENTRY_POINT(___fork)
107*94d3b452SApple OSS Distributions	stmfd	sp!, {r4, r7, lr}
108*94d3b452SApple OSS Distributions	add	r7, sp, #4
109*94d3b452SApple OSS Distributions
110*94d3b452SApple OSS Distributions	mov	r1, #1					// prime results
111*94d3b452SApple OSS Distributions	mov	r12, #SYS_fork
112*94d3b452SApple OSS Distributions	swi	#SWI_SYSCALL				// make the syscall
113*94d3b452SApple OSS Distributions	bcs	Lbotch					// error?
114*94d3b452SApple OSS Distributions
115*94d3b452SApple OSS Distributions	cmp	r1, #0					// parent (r1=0) or child(r1=1)
116*94d3b452SApple OSS Distributions	beq	Lparent
117*94d3b452SApple OSS Distributions
118*94d3b452SApple OSS Distributions	//child here...
119*94d3b452SApple OSS Distributions	MI_GET_ADDRESS(r3, __current_pid)
120*94d3b452SApple OSS Distributions	mov	r0, #0
121*94d3b452SApple OSS Distributions	str	r0, [r3]		// clear cached pid in child
122*94d3b452SApple OSS Distributions	ldmfd   sp!, {r4, r7, pc}
123*94d3b452SApple OSS Distributions
124*94d3b452SApple OSS DistributionsLbotch:
125*94d3b452SApple OSS Distributions	MI_CALL_EXTERNAL(_cerror)			// jump here on error
126*94d3b452SApple OSS Distributions	mov	r0,#-1					// set the error
127*94d3b452SApple OSS Distributions	// fall thru
128*94d3b452SApple OSS DistributionsLparent:
129*94d3b452SApple OSS Distributions	ldmfd   sp!, {r4, r7, pc}			// pop and return
130*94d3b452SApple OSS Distributions
131*94d3b452SApple OSS Distributions#elif defined(__arm64__)
132*94d3b452SApple OSS Distributions
133*94d3b452SApple OSS Distributions#include <mach/arm64/asm.h>
134*94d3b452SApple OSS Distributions
135*94d3b452SApple OSS DistributionsMI_ENTRY_POINT(___fork)
136*94d3b452SApple OSS Distributions	ARM64_STACK_PROLOG
137*94d3b452SApple OSS Distributions	PUSH_FRAME
138*94d3b452SApple OSS Distributions	// ARM moves a 1 in to r1 here, but I can't see why.
139*94d3b452SApple OSS Distributions	mov		x16, #SYS_fork				// Syscall code
140*94d3b452SApple OSS Distributions	svc		#SWI_SYSCALL				// Trap to kernel
141*94d3b452SApple OSS Distributions	b.cs	Lbotch						// Carry bit indicates failure
142*94d3b452SApple OSS Distributions	cbz		x1, Lparent					// x1 == 0 indicates that we are the parent
143*94d3b452SApple OSS Distributions
144*94d3b452SApple OSS Distributions	// Child
145*94d3b452SApple OSS Distributions	MI_GET_ADDRESS(x9, __current_pid)	// Get address of cached "current pid"
146*94d3b452SApple OSS Distributions	mov		w0, #0
147*94d3b452SApple OSS Distributions	str		w0, [x9]					// Clear cached current pid
148*94d3b452SApple OSS Distributions	POP_FRAME							// And done
149*94d3b452SApple OSS Distributions	ARM64_STACK_EPILOG
150*94d3b452SApple OSS Distributions
151*94d3b452SApple OSS DistributionsLbotch:
152*94d3b452SApple OSS Distributions	MI_CALL_EXTERNAL(_cerror)			// Handle error
153*94d3b452SApple OSS Distributions	mov		w0, #-1						// Return value is -1
154*94d3b452SApple OSS DistributionsLparent:
155*94d3b452SApple OSS Distributions	POP_FRAME							// Return
156*94d3b452SApple OSS Distributions	ARM64_STACK_EPILOG
157*94d3b452SApple OSS Distributions
158*94d3b452SApple OSS Distributions#else
159*94d3b452SApple OSS Distributions#error Unsupported architecture
160*94d3b452SApple OSS Distributions#endif
161