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