__clone.S revision 1.6.10.1 1 1.6.10.1 matt /* $NetBSD: __clone.S,v 1.6.10.1 2007/08/28 17:36:30 matt Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.1 chris * Copyright (c) 2001 Christopher Gilbert
5 1.1 chris * All rights reserved.
6 1.1 chris *
7 1.1 chris * 1. Redistributions of source code must retain the above copyright
8 1.1 chris * notice, this list of conditions and the following disclaimer.
9 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 chris * notice, this list of conditions and the following disclaimer in the
11 1.1 chris * documentation and/or other materials provided with the distribution.
12 1.1 chris * 3. The name of the company nor the name of the author may be used to
13 1.1 chris * endorse or promote products derived from this software without specific
14 1.1 chris * prior written permission.
15 1.1 chris *
16 1.1 chris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 1.1 chris * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 1.1 chris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 chris * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 1.1 chris * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 chris * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 chris * SUCH DAMAGE.
27 1.1 chris */
28 1.1 chris
29 1.1 chris #include <machine/asm.h>
30 1.1 chris #include <sys/errno.h>
31 1.1 chris #include "SYS.h"
32 1.1 chris
33 1.1 chris #ifdef WEAK_ALIAS
34 1.1 chris WEAK_ALIAS(clone, __clone)
35 1.1 chris #endif
36 1.1 chris
37 1.1 chris /*
38 1.1 chris * int __clone(int (*fn)(void *), void *stack, int flags, void *arg);
39 1.1 chris */
40 1.1 chris ENTRY(__clone)
41 1.1 chris /* test stack and function are not null */
42 1.6.10.1 matt #ifdef __thumb__
43 1.6.10.1 matt cmp r0, #0
44 1.6.10.1 matt beq .Leinval
45 1.6.10.1 matt cmp r1, #0
46 1.6.10.1 matt beq .Leinval
47 1.6.10.1 matt #else
48 1.1 chris teq r0, #0x00
49 1.1 chris teqne r1, #0x00
50 1.2 thorpej beq .Leinval
51 1.6.10.1 matt #endif
52 1.1 chris
53 1.6 snj /* place the func and its arg onto the child's stack */
54 1.6.10.1 matt #ifdef __thumb__
55 1.6.10.1 matt sub r1, #8
56 1.6.10.1 matt str r0, [r1, #0]
57 1.6.10.1 matt str r3, [r1, #4]
58 1.6.10.1 matt #else
59 1.1 chris stmfd r1!, {r0, r3}
60 1.6.10.1 matt #endif
61 1.1 chris
62 1.1 chris /* syscall expects (flags, stack) */
63 1.1 chris mov r0, r2
64 1.1 chris
65 1.1 chris SYSTRAP(__clone)
66 1.6.10.1 matt bcs .Lcerror
67 1.1 chris
68 1.1 chris /*
69 1.1 chris * r1 and r0 are the same as from fork:
70 1.1 chris * r1 == 0 in parent process, r1 == 1 in child process.
71 1.1 chris * r0 == pid of child in parent, r0 == pid of parent in child.
72 1.1 chris */
73 1.6.10.1 matt #ifdef __thumb__
74 1.6.10.1 matt cmp r1, #0
75 1.6.10.1 matt bne 1f
76 1.6.10.1 matt RET
77 1.6.10.1 matt 1:
78 1.6.10.1 matt #else
79 1.1 chris teq r1, #0x00
80 1.1 chris
81 1.1 chris /* if this is the parent then just return the pid */
82 1.5 rearnsha RETc(eq)
83 1.6.10.1 matt #endif
84 1.1 chris /*
85 1.1 chris * This is the child
86 1.1 chris * load the function and arg off the stack
87 1.1 chris */
88 1.6.10.1 matt XPOP {r1, r2}
89 1.1 chris
90 1.1 chris /* setup return address */
91 1.6.10.1 matt #ifdef __thumb__
92 1.6.10.1 matt add r0, pc, #.Lreturnhere - . - 8
93 1.6.10.1 matt mov lr, r0
94 1.6.10.1 matt #else
95 1.2 thorpej add lr, pc, #.Lreturnhere - . - 8
96 1.6.10.1 matt #endif
97 1.1 chris
98 1.1 chris /* place arg in r0 */
99 1.1 chris mov r0, r2
100 1.1 chris
101 1.1 chris /* call the function */
102 1.5 rearnsha #ifdef _ARM_ARCH_4T
103 1.5 rearnsha bx r1
104 1.5 rearnsha #else
105 1.1 chris mov pc, r1
106 1.5 rearnsha #endif
107 1.1 chris
108 1.2 thorpej .Lreturnhere:
109 1.1 chris /* call _exit with the returned value */
110 1.6.10.1 matt bl PLT_SYM(_C_LABEL(_exit))
111 1.1 chris
112 1.1 chris /* NOTREACHED */
113 1.1 chris
114 1.1 chris /* error handler if func or stack is NULL */
115 1.2 thorpej .Leinval:
116 1.6.10.1 matt mov r0, #EINVAL
117 1.6.10.1 matt .Lcerror:
118 1.6.10.1 matt _SYSCALL_CERROR
119 1.6.10.1 matt END(__clone)
120