1/* The clone3 syscall wrapper.
2   Copyright (C) 2022 Free Software Foundation, Inc.
3
4   This file is part of the GNU C Library.
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10
11   The GNU C Library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with the GNU C Library.  If not, see
18   <https://www.gnu.org/licenses/>.  */
19
20/* clone3() is even more special than fork() as it mucks with stacks
21   and invokes a function in the right context after its all over.  */
22
23#include <sys/asm.h>
24#include <sysdep.h>
25#define _ERRNO_H  1
26#include <bits/errno.h>
27#include <tls.h>
28#include "tcb-offsets.h"
29
30/* int clone3(struct clone_args *cl_args, size_t size,
31   int (*func)(void *arg), void *arg); */
32
33ENTRY (__clone3)
34
35	/* Sanity check arguments.  */
36	beqz		a0, L (invalid)	/* No NULL cl_args pointer.  */
37	beqz		a2, L (invalid)	/* No NULL function pointer.  */
38
39	/* Do the system call.  */
40	LI		a7, __NR_clone3
41	syscall		0
42
43	blt		a0, zero ,L (error)
44	beqz		a0, L (thread_start3)
45
46	/* Successful return from the parent.  */
47	ret
48
49L (invalid):
50	LI		a0, -EINVAL
51
52	/* Something bad happened -- no child created.  */
53L (error):
54	b		__syscall_error
55
56END (__clone3)
57
58/* Load up the arguments to the function.  Put this block of code in
59   its own function so that we can terminate the stack trace with our
60   debug info.  */
61ENTRY (__thread_start3)
62L (thread_start3):
63
64/* Terminate call stack by noting ra is undefined.  Use a dummy
65   .cfi_label to force starting the FDE.  */
66	.cfi_label .Ldummy
67	cfi_undefined (1)
68
69	/* Align stack to 16.  */
70	BSTRINS		sp, zero, 3, 0
71
72	/* Set up arguments for the function call.  */
73	move		a0, a3		/* Argument.  */
74	jirl		ra, a2, 0	/* Call function.  */
75
76	/* Call exit with the function's return value.  */
77	LI		a7, __NR_exit
78	syscall		0
79
80	END (__thread_start3)
81
82libc_hidden_def (__clone3)
83weak_alias (__clone3, clone3)
84