]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/powerpc/clone.S
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / sysdeps / linux / powerpc / clone.S
1 /* Wrapper around clone system call.
2    Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <features.h>
20 #define _ERRNO_H        1
21 #include <bits/errno.h>
22 #include <sysdep.h>
23
24 #define CLONE_VM        0x00000100
25 #define CLONE_THREAD    0x00010000
26
27 /* This is the only really unusual system call in PPC linux, but not
28    because of any weirdness in the system call itself; because of
29    all the freaky stuff we have to do to make the call useful.  */
30
31 /* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4],
32                   int flags [r5], void *arg [r6], void *parent_tid [r7],
33                   void *tls [r8], void *child_tid [r9]); */
34
35 #ifdef __NR_clone
36         .globl __clone
37         .type __clone,@function
38         .align 2
39
40 __clone:
41         /* Check for child_stack == NULL || fn == NULL.  */
42         cmpwi   cr0,r4,0
43         cmpwi   cr1,r3,0
44         cror    cr0*4+eq,cr1*4+eq,cr0*4+eq
45         beq-    cr0,.Lbadargs
46
47         /* Set up stack frame for parent.  */
48         stwu    r1,-32(r1)
49         cfi_adjust_cfa_offset (32)
50 #ifdef RESET_PID
51         stmw    r28,16(r1)
52 #else
53 # ifndef __ASSUME_FIXED_CLONE_SYSCALL
54         stmw    r29,16(r1)
55 # else
56         stmw    r30,16(r1)
57 # endif
58 #endif
59
60         /* Set up stack frame for child.  */
61         clrrwi  r4,r4,4
62         li      r0,0
63         stwu    r0,-16(r4)
64
65         /* Save fn, args, stack across syscall.  */
66         mr      r30,r3                  /* Function in r30.  */
67 #ifndef __ASSUME_FIXED_CLONE_SYSCALL
68         mr      r29,r4                  /* Stack pointer in r29.  */
69 #endif
70 #ifdef RESET_PID
71         mr      r28,r5
72 #endif
73         mr      r31,r6                  /* Argument in r31.  */
74
75         /* 'flags' argument is first parameter to clone syscall. (The other
76            argument is the stack pointer, already in r4.)  */
77         mr      r3,r5
78
79         /* Move the parent_tid, child_tid and tls arguments. */
80         mr      r5,r7
81         mr      r6,r8
82         mr      r7,r9
83
84         /* End FDE now, because in the child the unwind info will be wrong.  */
85         cfi_endproc
86
87         /* Do the call.  */
88         li 0, __NR_clone
89         sc
90
91         /* Check for child process.  */
92         cmpwi   cr1,r3,0
93         crandc  cr1*4+eq,cr1*4+eq,cr0*4+so
94         bne-    cr1,.Lparent            /* The '-' is to minimise the race.  */
95
96 #ifndef __ASSUME_FIXED_CLONE_SYSCALL
97         /* On at least mklinux DR3a5, clone() doesn't actually change
98            the stack pointer.  I'm pretty sure this is a bug, because
99            it adds a race condition if a signal is sent to a thread
100            just after it is created (in the previous three instructions).  */
101         mr      r1,r29
102 #endif
103
104 #ifdef RESET_PID
105         andis.  r0,r28,CLONE_THREAD>>16
106         bne+    r0,.Loldpid
107         andi.   r0,r28,CLONE_VM
108         li      r3,-1
109         bne-    r0,.Lnomoregetpid
110 .Lnomoregetpid:
111         stw     r3,TID(r2)
112         stw     r3,PID(r2)
113 .Loldpid:
114 #endif
115         /* Call procedure.  */
116         mtctr   r30
117         mr      r3,r31
118         bctrl
119         /* Call _exit with result from procedure.  */
120         b       HIDDEN_JUMPTARGET(_exit)
121
122 .Lparent:
123         /* Parent.  Restore registers & return.  */
124 #ifdef RESET_PID
125         lmw     r28,16(r1)
126 #else
127 # ifndef __ASSUME_FIXED_CLONE_SYSCALL
128         lmw     r29,16(r1)
129 # else
130         lmw     r30,16(r1)
131 # endif
132 #endif
133         addi    r1,r1,32
134         bnslr+
135         b       __syscall_error
136
137 .Lbadargs:
138         li      r3,EINVAL
139         b       __syscall_error
140
141         cfi_startproc
142         .size __clone,.-__clone
143 weak_alias(__clone, clone)
144 #endif