1/* Copyright (C) 1996-2017 Free Software Foundation, Inc.
2
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.
7
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.
12
13   You should have received a copy of the GNU Lesser General Public
14   License along with the GNU C Library; if not, see
15   <http://www.gnu.org/licenses/>.  */
16
17/* clone() is even more special than fork() as it mucks with stacks
18   and invokes a function in the right context after its all over.  */
19
20#include <sysdep.h>
21#define _ERRNO_H	1
22#include <bits/errno.h>
23
24/* int clone(int (*fn)(void *arg),            x0
25	     void *child_stack,               x1
26	     int flags,                       x2
27	     void *arg,                       x3
28	     pid_t *ptid,                     x4
29	     struct user_desc *tls,           x5
30             pid_t *ctid);                    x6
31 */
32        .text
33ENTRY(__clone)
34	/* Save args for the child.  */
35	mov	x10, x0
36	mov	x11, x2
37	mov	x12, x3
38
39	/* Sanity check args.  */
40	mov	x0, #-EINVAL
41	cbz	x10, .Lsyscall_error
42	cbz	x1, .Lsyscall_error
43
44	/* Do the system call.  */
45	/* X0:flags, x1:newsp, x2:parenttidptr, x3:newtls, x4:childtid.  */
46	mov	x0, x2                  /* flags  */
47	/* New sp is already in x1.  */
48	mov	x2, x4			/* ptid  */
49	mov	x3, x5			/* tls  */
50	mov	x4, x6			/* ctid  */
51	mov	x8, #SYS_ify(clone)
52	svc	0x0
53
54	cmp	x0, #0
55	beq	thread_start
56	blt	.Lsyscall_error
57	RET
58PSEUDO_END (__clone)
59
60	.align 4
61	.type thread_start, %function
62thread_start:
63	cfi_startproc
64	cfi_undefined (x30)
65	mov	x29, 0
66
67	/* Pick the function arg and execute.  */
68	mov	x0, x12
69	blr	x10
70
71	/* We are done, pass the return value through x0.  */
72	mov	x8, #SYS_ify(exit)
73	svc	0x0
74	cfi_endproc
75	.size thread_start, .-thread_start
76
77libc_hidden_def (__clone)
78weak_alias (__clone, clone)
79