2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / powerpc / powerpc64 / clone.S
blobf1a55e64dbb7f0d08cb86add450b5af186141b64
1 /* Wrapper around clone system call.  PowerPC64 version.
2    Copyright (C) 1997,98,99,2000,02,04, 2006 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., 51 Franklin Street, Fifth Floor, Boston MA
18    02110-1301 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         cmpdi   cr1,r3,0
46         cror    cr0*4+eq,cr1*4+eq,cr0*4+eq
47         beq-    cr0,L(badargs)
49         /* Set up stack frame for parent.  */
50         stdu    r1,-80(r1)
51         cfi_adjust_cfa_offset (80)
52         std     r29,56(r1)
53         std     r30,64(r1)
54         std     r31,72(r1)
55         cfi_offset(r29,-56)
56         cfi_offset(r30,-64)
57         cfi_offset(r31,-72)
58 #ifdef RESET_PID
59         std     r28,48(r1)
60         cfi_offset(r28,-48)
61 #endif
63         /* Set up stack frame for child.  */
64         clrrdi  r4,r4,4
65         li      r0,0
66         stdu    r0,-48(r4) /* min stack frame is 48 bytes per ABI */
68         /* Save fn, args, stack across syscall.  */
69         mr      r29,r3                  /* Function in r29.  */
70         mr      r30,r4                  /* Stack pointer in r30.  */
71 #ifdef RESET_PID
72         mr      r28,r5                  /* Flags in r28.  */
73 #endif
74         mr      r31,r6                  /* Argument in r31.  */
76         /* 'flags' argument is first parameter to clone syscall. (The other
77            argument is the stack pointer, already in r4.)  */
78         mr      r3,r5
79   /* Move the parent_tid, child_tid and tls arguments. */
80         mr      r5,r7
81         mr      r6,r8
82         mr      r7,r9
84         /* End FDE now, because in the child the unwind info will be
85            wrong.  */
86         cfi_endproc
88         /* Do the call.  */
89         DO_CALL(SYS_ify(clone))
91         /* Check for child process.  */
92         cmpdi   cr1,r3,0
93         crandc  cr1*4+eq,cr1*4+eq,cr0*4+so
94         bne-    cr1,L(parent)           /* The '-' is to minimise the race.  */
96 #ifdef RESET_PID
97         andis.  r0,r28,CLONE_THREAD>>16
98         bne+    cr0,L(oldpid)
99         andi.   r0,r28,CLONE_VM
100         li      r3,-1
101         bne-    cr0,L(nomoregetpid)
102         DO_CALL(SYS_ify(getpid))
103 L(nomoregetpid):
104         stw     r3,TID(r13)
105         stw     r3,PID(r13)
106 L(oldpid):
107 #endif
109         std     r2,40(r1)
110         /* Call procedure.  */
111         ld      r0,0(r29)
112         ld      r2,8(r29)
113         mtctr   r0
114         mr      r3,r31
115         bctrl
116         ld      r2,40(r1)
117         /* Call _exit with result from procedure.  */
118 #ifdef SHARED
119         b       JUMPTARGET(__GI__exit)
120 #else
121         b       JUMPTARGET(_exit)
122 #endif
124 L(parent):
125         /* Parent.  Restore registers & return.  */
126 #ifdef RESET_PID
127         ld      r28,48(r1)
128 #endif
129         ld      r31,72(r1)
130         ld      r30,64(r1)
131         ld      r29,56(r1)
132         addi    r1,r1,80
133         bnslr+
134         b       JUMPTARGET(__syscall_error)
136 L(badargs):
137         li      r3,EINVAL
138         b       JUMPTARGET(__syscall_error)
140         cfi_startproc
141 END (BP_SYM (__clone))
143 weak_alias (BP_SYM (__clone), BP_SYM (clone))