1 /* Copyright (C) 2011-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
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 it's all over. */
24 #include <bits/errno.h>
26 #include <asm/unistd.h>
29 #include <linux/sched.h>
31 /* What we save where in the stack frame; must include all callee-saves. */
32 #define FRAME_NEXT_LR (0 * REGSIZE) /* reserved by ABI; not used here */
33 #define FRAME_SP (1 * REGSIZE)
34 #define FRAME_R30 (2 * REGSIZE)
35 #define FRAME_R31 (3 * REGSIZE)
36 #define FRAME_R32 (4 * REGSIZE)
37 #define FRAME_SIZE (5 * REGSIZE)
39 /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
40 pid_t *ptid, struct user_desc *tls, pid_t *ctid); */
44 /* Create a stack frame so we can pass callee-saves to new task. */
48 ADDI_PTR sp, sp, -FRAME_SIZE
51 cfi_def_cfa_offset (FRAME_SIZE)
52 ADDI_PTR r11, sp, FRAME_SP
55 ADDI_PTR r11, sp, FRAME_R30
59 ADDI_PTR r11, sp, FRAME_R31
61 cfi_offset (r30, FRAME_R30 - FRAME_SIZE)
64 ADDI_PTR r11, sp, FRAME_R32
66 cfi_offset (r31, FRAME_R31 - FRAME_SIZE)
68 cfi_offset (r32, FRAME_R32 - FRAME_SIZE)
70 /* sanity check arguments */
74 /* Make sure child stack is properly aligned, and set up the
75 top frame so that we can call out of it immediately in the
76 child. Setting it up here means we fault in the parent if
77 it's bogus, which is probably cleaner than faulting first
78 thing in the child. */
79 ADDI_PTR r1, r1, -C_ABI_SAVE_AREA_SIZE
80 andi r1, r1, -C_ABI_SAVE_AREA_SIZE
81 ADDI_PTR r9, r1, REGSIZE /* sp of this frame on entry, i.e. zero */
84 /* We need to switch the argument convention around from
98 r1 child_stack [same as libc]
103 Plus the callee-saves as described at .Lthread_start, below. */
118 moveli TREG_SYSCALL_NR_NAME, __NR_clone
121 beqz r0, .Lthread_start /* If in child task. */
124 /* Restore the callee-saved registers and return. */
125 ADDLI_PTR lr, sp, FRAME_SIZE
128 ADDLI_PTR r30, sp, FRAME_R30
132 ADDLI_PTR r31, sp, FRAME_R31
136 ADDLI_PTR r32, sp, FRAME_R32
140 ADDI_PTR sp, sp, FRAME_SIZE
142 cfi_def_cfa_offset (0)
156 /* This function expects to receive:
158 sp: the top of a valid stack area
160 r31: the argument to pass to the user function
161 r32: the user function pointer */
164 cfi_def_cfa_offset (FRAME_SIZE)
167 /* Invoke user function with specified argument. */
171 moveli TREG_SYSCALL_NR_NAME, __NR_exit
175 libc_hidden_def (__clone)
176 weak_alias (__clone, clone)