1/*
2 * Copyright (c) 1999-2010 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) 1992 NeXT Computer, Inc.  All rights reserved.
29 *
30 *	File:	libc/ppc/sys/fork.s
31 *
32 * HISTORY
33 * 18-Nov-92  Ben Fathi (benf@next.com)
34 *	Created from M88K sources
35 *
36 * 11-Jan-92  Peter King (king@next.com)
37 *	Created from M68K sources
38 */
39
40/*
41 * All of the asm stubs in this file have been adjusted so the pre/post
42 * fork handlers and dyld fixup are done in C inside Libc. As such, Libc
43 * expects the __fork asm to fix up the return code to be -1, 0 or pid
44 * and errno if needed.
45 */
46
47#include "SYS.h"
48
49#if defined(__i386__)
50
51LEAF(___fork, 0)
52	subl  $28, %esp   // Align the stack, with 16 bytes of extra padding that we'll need
53
54	movl 	$ SYS_fork,%eax; 	// code for fork -> eax
55	UNIX_SYSCALL_TRAP		// do the system call
56	jnc	L1			// jump if CF==0
57
58	CALL_EXTERN(cerror)
59	movl	$-1,%eax
60	addl	$28, %esp   // restore the stack
61	ret
62
63L1:
64	orl	%edx,%edx	// CF=OF=0,  ZF set if zero result
65	jz	L2		// parent, since r1 == 0 in parent, 1 in child
66
67	//child here...
68	xorl	%eax,%eax	// zero eax
69	REG_TO_EXTERN(%eax, __current_pid);
70L2:
71	addl	$28, %esp   // restore the stack
72	// parent ends up here skipping child portion
73	ret
74
75#elif defined(__x86_64__)
76
77LEAF(___fork, 0)
78	subq  $24, %rsp   // Align the stack, plus room for local storage
79
80	movl 	$ SYSCALL_CONSTRUCT_UNIX(SYS_fork),%eax; // code for fork -> rax
81	UNIX_SYSCALL_TRAP		// do the system call
82	jnc	L1			// jump if CF==0
83
84	CALL_EXTERN(cerror)
85	movq	$-1, %rax
86	addq	$24, %rsp   // restore the stack
87	ret
88
89L1:
90	orl	%edx,%edx	// CF=OF=0,  ZF set if zero result
91	jz	L2		// parent, since r1 == 0 in parent, 1 in child
92
93	//child here...
94	xorq	%rax, %rax
95	PICIFY(__current_pid)
96	movl	%eax,(%r11)
97L2:
98	// parent ends up here skipping child portion
99	addq	$24, %rsp   // restore the stack
100	ret
101
102#elif defined(__arm__)
103
104	.globl	cerror
105	MI_ENTRY_POINT(_fork)
106	stmfd	sp!, {r4, r7, lr}
107	add	r7, sp, #4
108	mov	r1, #1					// prime results
109	mov	r12, #SYS_fork
110	swi	#SWI_SYSCALL				// make the syscall
111	bcs	Lbotch					// error?
112	cmp	r1, #0					// parent (r1=0) or child(r1=1)
113	beq	Lparent
114
115	//child here...
116	MI_GET_ADDRESS(r3, __current_pid)
117	mov	r0, #0
118	str	r0, [r3]		// clear cached pid in child
119
120#if 0
121#if defined(__DYNAMIC__)
122// Here on the child side of the fork we need to tell the dynamic linker that
123// we have forked.  To do this we call __dyld_fork_child in the dyanmic
124// linker.  But since we can't dynamicly bind anything until this is done we
125// do this by using the private extern __dyld_func_lookup() function to get the
126// address of __dyld_fork_child (the 'C' code equivlent):
127//
128//	_dyld_func_lookup("__dyld_fork_child", &address);
129//	address();
130//
131	.cstring
132	.align 2
133LC0:
134	.ascii "__dyld_fork_child\0"
135.text
136.align 2
137	sub	sp, sp, #4				// allocate space for the address parameter
138	mov	r1, sp					// get the address of the allocated space
139	ldr	r0, LP0					// get the name of the function to look up
140L0:	add	r0, pc, r0
141	bl	__dyld_func_lookup
142	mov	lr, pc
143	ldr	pc, [sp], #4				// call __dyld_fork_child indirectly and pop
144#endif
145	mov	r0, #0
146#endif
147        ldmfd   sp!, {r4, r7, pc}
148
149Lbotch:
150	MI_CALL_EXTERNAL(cerror)			// jump here on error
151	mov	r0,#-1					// set the error
152	// fall thru
153
154Lparent:
155	ldmfd   sp!, {r4, r7, pc}			// pop and return
156
157	.align 2
158#if 0
159#if defined(__DYNAMIC__)
160LP0:
161	.long	LC0-(L0+8)
162#endif
163#endif
164
165#else
166#error Unsupported architecture
167#endif
168