1/* Copyright (C) 1996-2021 Free Software Foundation, Inc.
2
3   This file is part of the GNU C Library.
4
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.
9
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.
14
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   <https://www.gnu.org/licenses/>.  */
18
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.  */
21
22#include <sysdep.h>
23#define _ERRNO_H	1
24#include <bits/errno.h>
25
26/* int clone(int (*fn)(void *arg),            x0
27	     void *child_stack,               x1
28	     int flags,                       x2
29	     void *arg,                       x3
30	     pid_t *ptid,                     x4
31	     struct user_desc *tls,           x5
32             pid_t *ctid);                    x6
33 */
34        .text
35ENTRY(__clone)
36	PTR_ARG (0)
37	PTR_ARG (1)
38	PTR_ARG (3)
39	PTR_ARG (4)
40	PTR_ARG (5)
41	PTR_ARG (6)
42	/* Save args for the child.  */
43	mov	x10, x0
44	mov	x11, x2
45	mov	x12, x3
46
47	/* Sanity check args.  */
48	mov	x0, #-EINVAL
49	cbz	x10, .Lsyscall_error
50	/* Align sp.  */
51	and	x1, x1, -16
52	cbz	x1, .Lsyscall_error
53
54	/* Do the system call.  */
55	/* X0:flags, x1:newsp, x2:parenttidptr, x3:newtls, x4:childtid.  */
56	mov	x0, x2                  /* flags  */
57	/* New sp is already in x1.  */
58	mov	x2, x4			/* ptid  */
59	mov	x3, x5			/* tls  */
60	mov	x4, x6			/* ctid  */
61	mov	x8, #SYS_ify(clone)
62	svc	0x0
63
64	cmp	x0, #0
65	beq	thread_start
66	blt	.Lsyscall_error
67	RET
68PSEUDO_END (__clone)
69
70	.align 4
71	.type thread_start, %function
72thread_start:
73	cfi_startproc
74	cfi_undefined (x30)
75	mov	x29, 0
76
77	/* Pick the function arg and execute.  */
78	mov	x0, x12
79	blr	x10
80
81	/* We are done, pass the return value through x0.  */
82	mov	x8, #SYS_ify(exit)
83	svc	0x0
84	cfi_endproc
85	.size thread_start, .-thread_start
86
87libc_hidden_def (__clone)
88weak_alias (__clone, clone)
89