1 /* Copyright (C) 2001, 2005, 2007 Free Software Foundation, Inc.
3 The GNU C Library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
8 The GNU C Library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with the GNU C Library; if not, write to the Free
15 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
16 Boston, MA 02110-1301, USA. */
18 /* clone is even more special than fork as it mucks with stacks
19 and invokes a function in the right context after it's all over. */
22 #include <sys/syscall.h>
24 #include <bits/errno.h>
26 /* int clone (a2 = int (*fn)(void *arg),
27 a3 = void *child_stack,
31 a7 = struct user_desc *tls,
32 16(sp) = pid_t *ctid) */
37 /* Sanity check arguments. */
38 beqz a2, .Leinval /* no NULL function pointers */
39 beqz a3, .Leinval /* no NULL stack pointers */
41 /* a2 and a3 are candidates for destruction by system-call return
42 parameters. We don't need the stack pointer after the system
43 call. We trust that the kernel will preserve a7, a9, and a6. */
45 mov a9, a5 /* save function argument */
47 mov a7, a2 /* save function pointer */
48 mov a8, a6 /* use a8 as a temp */
51 l32i a8, a1, 16 /* child_tid */
52 movi a2, SYS_ify (clone)
54 /* syscall (a2 = NR_clone,
61 bltz a2, SYSCALL_ERROR_LABEL
62 beqz a2, .Lthread_start
64 /* Fall through for parent. */
73 /* Start child thread. */
74 movi a0, 0 /* terminate the stack frame */
77 /* Check and see if we need to reset the PID. */
78 bbsi.l a6, 16, 1f /* CLONE_THREAD = 0x00010000 */
80 bbsi.l a6, 8, 2f /* CLONE_VM = 0x00000100 */
81 movi a2, SYS_ify (getpid)
91 #endif /* RESET_PID */
93 mov a6, a9 /* load up the 'arg' parameter */
94 callx4 a7 /* call the user's function */
96 /* Call _exit. Note that any return parameter from the user's
97 function in a6 is seen as inputs to _exit. */
98 movi a2, JUMPTARGET(_exit)
103 weak_alias (__clone, clone)