1 /* Copyright (C) 1996-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ralf Baechle <ralf@linux-mips.org>, 1996.
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, see
17 <http://www.gnu.org/licenses/>. */
19 /* clone() is even more special than fork() as it mucks with stacks
20 and invokes a function in the right context after its all over. */
25 #include <bits/errno.h>
28 #define CLONE_VM 0x00000100
29 #define CLONE_THREAD 0x00010000
31 /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
32 void *parent_tidptr, void *tls, void *child_tidptr) */
36 #if _MIPS_SIM == _ABIO32
37 # define EXTRA_LOCALS 1
39 # define EXTRA_LOCALS 0
42 FRAMESZ= (((NARGSAVE+LOCALSZ)*SZREG)+ALSZ)&ALMASK
43 GPOFF= FRAMESZ-(1*SZREG)
44 NESTED(__clone,4*SZREG,sp)
49 cfi_adjust_cfa_offset (FRAMESZ)
50 SETUP_GP64_STACK (GPOFF, __clone)
62 /* Sanity check arguments. */
64 beqz a0,L(error) /* No NULL function pointers. */
65 beqz a1,L(error) /* No NULL stack pointers. */
67 PTR_SUBU a1,32 /* Reserve argument save space. */
68 PTR_S a0,0(a1) /* Save function pointer. */
69 PTR_S a3,PTRSIZE(a1) /* Save argument pointer. */
70 LONG_S a2,(PTRSIZE*2)(a1) /* Save clone flags. */
74 /* Shuffle in the last three arguments - arguments 5, 6, and 7 to
75 this function, but arguments 3, 4, and 5 to the syscall. */
76 #if _MIPS_SIM == _ABIO32
77 PTR_L a2,(FRAMESZ+PTRSIZE+PTRSIZE+16)(sp)
79 PTR_L a2,(FRAMESZ+16)(sp)
80 PTR_L a3,(FRAMESZ+PTRSIZE+16)(sp)
87 /* Do the system call */
93 beqz v0,L(thread_start)
95 /* Successful return from the parent */
97 cfi_adjust_cfa_offset (FRAMESZ)
98 SETUP_GP64_STACK_CFI (GPOFF)
102 cfi_adjust_cfa_offset (-FRAMESZ)
105 /* Something bad happened -- no child created */
109 PTR_LA t9,__syscall_error
112 cfi_adjust_cfa_offset (-FRAMESZ)
117 cfi_adjust_cfa_offset (-FRAMESZ)
122 /* Load up the arguments to the function. Put this block of code in
123 its own function so that we can terminate the stack trace with our
126 ENTRY(__thread_start)
129 /* cp is already loaded. */
131 /* The stackframe has been created on entry of clone(). */
133 /* Restore the arg for user's function. */
134 PTR_L t9,0(sp) /* Function pointer. */
135 PTR_L a0,PTRSIZE(sp) /* Argument pointer. */
137 /* Call the user's function. */
140 /* Call _exit rather than doing it inline for breakpoint purposes. */
151 libc_hidden_def (__clone)
152 weak_alias (__clone, clone)