]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/custom/__fork.s
xnu-4570.1.46.tar.gz
[apple/xnu.git] / libsyscall / custom / __fork.s
CommitLineData
2d21ac55 1/*
6d2010ae 2 * Copyright (c) 1999-2010 Apple Inc. All rights reserved.
2d21ac55
A
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 */
6d2010ae
A
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.
2d21ac55 45 */
2d21ac55 46
6d2010ae 47#include "SYS.h"
2d21ac55 48
6d2010ae 49#if defined(__i386__)
2d21ac55
A
50
51LEAF(___fork, 0)
52 subl $28, %esp // Align the stack, with 16 bytes of extra padding that we'll need
2d21ac55
A
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
39236c6e 58 CALL_EXTERN(tramp_cerror)
2d21ac55
A
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...
2d21ac55 68 xorl %eax,%eax // zero eax
6d2010ae 69 REG_TO_EXTERN(%eax, __current_pid);
2d21ac55 70L2:
2d21ac55 71 addl $28, %esp // restore the stack
6d2010ae
A
72 // parent ends up here skipping child portion
73 ret
2d21ac55
A
74
75#elif defined(__x86_64__)
76
77LEAF(___fork, 0)
78 subq $24, %rsp // Align the stack, plus room for local storage
2d21ac55
A
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
39236c6e
A
84 movq %rax, %rdi
85 CALL_EXTERN(_cerror)
2d21ac55
A
86 movq $-1, %rax
87 addq $24, %rsp // restore the stack
88 ret
89
90L1:
91 orl %edx,%edx // CF=OF=0, ZF set if zero result
92 jz L2 // parent, since r1 == 0 in parent, 1 in child
93
94 //child here...
2d21ac55 95 xorq %rax, %rax
6d2010ae
A
96 PICIFY(__current_pid)
97 movl %eax,(%r11)
2d21ac55 98L2:
6d2010ae 99 // parent ends up here skipping child portion
2d21ac55 100 addq $24, %rsp // restore the stack
6d2010ae 101 ret
2d21ac55 102
5ba3f43e
A
103#elif defined(__arm__)
104
105MI_ENTRY_POINT(___fork)
106 stmfd sp!, {r4, r7, lr}
107 add r7, sp, #4
108
109 mov r1, #1 // prime results
110 mov r12, #SYS_fork
111 swi #SWI_SYSCALL // make the syscall
112 bcs Lbotch // error?
113
114 cmp r1, #0 // parent (r1=0) or child(r1=1)
115 beq Lparent
116
117 //child here...
118 MI_GET_ADDRESS(r3, __current_pid)
119 mov r0, #0
120 str r0, [r3] // clear cached pid in child
121 ldmfd sp!, {r4, r7, pc}
122
123Lbotch:
124 MI_CALL_EXTERNAL(_cerror) // jump here on error
125 mov r0,#-1 // set the error
126 // fall thru
127Lparent:
128 ldmfd sp!, {r4, r7, pc} // pop and return
129
130#elif defined(__arm64__)
131
132#include <mach/arm64/asm.h>
133
134MI_ENTRY_POINT(___fork)
135 PUSH_FRAME
136 // ARM moves a 1 in to r1 here, but I can't see why.
137 mov x16, #SYS_fork // Syscall code
138 svc #SWI_SYSCALL // Trap to kernel
139 b.cs Lbotch // Carry bit indicates failure
140 cbz x1, Lparent // x1 == 0 indicates that we are the parent
141
142 // Child
143 MI_GET_ADDRESS(x9, __current_pid) // Get address of cached "current pid"
144 mov w0, #0
145 str w0, [x9] // Clear cached current pid
146 POP_FRAME // And done
147 ret
148
149Lbotch:
150 MI_CALL_EXTERNAL(_cerror) // Handle error
151 mov w0, #-1 // Return value is -1
152Lparent:
153 POP_FRAME // Return
154 ret
155
2d21ac55
A
156#else
157#error Unsupported architecture
158#endif