Update.
[glibc.git] / sysdeps / unix / sysv / linux / powerpc / powerpc32 / clone.S
blob97849a5b26532bf88a7f2983f4bd7e6f947b9983
1 /* Wrapper around clone system call.
2    Copyright (C) 1997,98,99,2000,02 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
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.
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.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #include <sysdep.h>
21 #define _ERRNO_H        1
22 #include <bits/errno.h>
23 #include <bp-sym.h>
24 #include <bp-asm.h>
26 /* This is the only really unusual system call in PPC linux, but not
27    because of any weirdness in the system call itself; because of
28    all the freaky stuff we have to do to make the call useful.  */
30 /* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4],
31                   int flags [r5], void *arg [r6]); */
33 ENTRY (BP_SYM (__clone))
34         /* GKM FIXME: add bounds checks, where sensible.  */
35         DISCARD_BOUNDS (r4)
36         DISCARD_BOUNDS (r6)
38         /* Check for child_stack == NULL || fn == NULL.  */
39         cmpwi   cr0,r4,0
40         cmpwi   cr1,r3,0
41         cror    cr0*4+eq,cr1*4+eq,cr0*4+eq
42         beq-    cr0,L(badargs)
44         /* Set up stack frame for parent.  */
45         stwu    r1,-32(r1)
46         stmw    r29,16(r1)
48         /* Set up stack frame for child.  */
49         clrrwi  r4,r4,4
50         li      r0,0
51         stwu    r0,-16(r4)
53         /* Save fn, args, stack across syscall.  */
54         mr      r29,r3                  /* Function in r29.  */
55         mr      r30,r4                  /* Stack pointer in r30.  */
56         mr      r31,r6                  /* Argument in r31.  */
58         /* 'flags' argument is first parameter to clone syscall. (The other
59            argument is the stack pointer, already in r4.)  */
60         mr      r3,r5
62         /* Do the call.  */
63         DO_CALL(SYS_ify(clone))
65         /* Check for child process.  */
66         cmpwi   cr1,r3,0
67         crandc  cr1*4+eq,cr1*4+eq,cr0*4+so
68         bne-    cr1,L(parent)           /* The '-' is to minimise the race.  */
70         /* On at least mklinux DR3a5, clone() doesn't actually change
71            the stack pointer.  I'm pretty sure this is a bug, because
72            it adds a race condition if a signal is sent to a thread
73            just after it is created (in the previous three instructions).  */
74         mr      r1,r30
75         /* Call procedure.  */
76         mtctr   r29
77         mr      r3,r31
78         bctrl
79         /* Call _exit with result from procedure.  */
80         b       HIDDEN_JUMPTARGET(_exit)
82 L(parent):
83         /* Parent.  Restore registers & return.  */
84         lmw     r29,16(r1)
85         addi    r1,r1,32
86         bnslr+
87         b       JUMPTARGET(__syscall_error)
89 L(badargs):
90         li      r3,EINVAL
91         b       JUMPTARGET(__syscall_error)
92 END (BP_SYM (__clone))
94 weak_alias (BP_SYM (__clone), BP_SYM (clone))