* sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S (__clone): Save
[glibc.git] / sysdeps / unix / sysv / linux / powerpc / powerpc64 / clone.S
blobf6ce115077149c6ce390701eaa9a82b120620844
1 /* Wrapper around clone system call.  PowerPC64 version.
2    Copyright (C) 1997,98,99,2000,02, 2004 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 #define CLONE_VM        0x00000100
27 #define CLONE_THREAD    0x00010000
29 /* This is the only really unusual system call in PPC linux, but not
30    because of any weirdness in the system call itself; because of
31    all the freaky stuff we have to do to make the call useful.  */
33 /* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4],
34                   int flags [r5], void *arg [r6], void *parent_tid [r7],
35                   void *tls [r8], void *child_tid [r9]); */
37 ENTRY (BP_SYM (__clone))
38         CALL_MCOUNT 7
39         /* GKM FIXME: add bounds checks, where sensible.  */
40         DISCARD_BOUNDS (r4)
41         DISCARD_BOUNDS (r6)
43         /* Check for child_stack == NULL || fn == NULL.  */
44         cmpdi   cr0,r4,0
45         ld      r0,0(r3)
46         cmpdi   cr1,r0,0
47         cror    cr0*4+eq,cr1*4+eq,cr0*4+eq
48         beq-    cr0,L(badargs)
50         /* Set up stack frame for parent.  */
51         stdu    r1,-88(r1)
52         std     r29,56(r1)
53         std     r30,64(r1)
54         std     r31,72(r1)
55 #ifdef RESET_PID
56         std     r28,80(r1)
57 #endif
59         /* Set up stack frame for child.  */
60         clrrdi  r4,r4,4
61         li      r0,0
62         stdu    r0,-48(r4) /* min stack frame is 48 bytes per ABI */
64         /* Save fn, args, stack across syscall.  */
65         mr      r29,r3                  /* Function in r29.  */
66         mr      r30,r4                  /* Stack pointer in r30.  */
67 #ifdef RESET_PID
68         mr      r28,r5                  /* Flags in r28.  */
69 #endif
70         mr      r31,r6                  /* Argument in r31.  */
72         /* 'flags' argument is first parameter to clone syscall. (The other
73            argument is the stack pointer, already in r4.)  */
74         mr      r3,r5
75   /* Move the parent_tid, child_tid and tls arguments. */
76         mr      r5,r7
77         mr      r6,r8
78         mr      r7,r9
80         /* Do the call.  */
81         DO_CALL(SYS_ify(clone))
83         /* Check for child process.  */
84         cmpdi   cr1,r3,0
85         crandc  cr1*4+eq,cr1*4+eq,cr0*4+so
86         bne-    cr1,L(parent)           /* The '-' is to minimise the race.  */
88         /* On at least mklinux DR3a5, clone() doesn't actually change
89            the stack pointer.  I'm pretty sure this is a bug, because
90            it adds a race condition if a signal is sent to a thread
91            just after it is created (in the previous three instructions).  */
92         mr      r1,r30
94 #ifdef RESET_PID
95         rldicl. r0,r28,48,63    /* This is & CLONE_THREAD */
96         bne+    r0,L(oldpid)
97         rldicl. r0,r28,56,63    /* This is & CLONE_VM */
98         li      r3,-1
99         bne-    r0,L(nomoregetpid)
100         DO_CALL(SYS_ify(getpid))
101 L(nomoregetpid):
102         stw     r3,TID(r13)
103         stw     r3,PID(r13)
104 L(oldpid):
105 #endif
107         std     r2,40(r1)
108         /* Call procedure.  */
109         ld      r0,0(r29)
110         ld      r2,8(r29)
111         mtctr   r0
112         mr      r3,r31
113         bctrl
114         ld      r2,40(r1)
115         /* Call _exit with result from procedure.  */
116 #ifdef SHARED
117         b       JUMPTARGET(__GI__exit)
118 #else
119         b       JUMPTARGET(_exit)
120 #endif
122 L(parent):
123         /* Parent.  Restore registers & return.  */
124 #ifdef RESET_PID
125         ld      r28,88(r1)
126 #endif
127         ld      r31,72(r1)
128         ld      r30,64(r1)
129         ld      r29,56(r1)
130         addi    r1,r1,88
131         bnslr+
132         b       JUMPTARGET(__syscall_error)
134 L(badargs):
135         li      r3,EINVAL
136         b       JUMPTARGET(__syscall_error)
137 END (BP_SYM (__clone))
139 weak_alias (BP_SYM (__clone), BP_SYM (clone))