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