1/* Copyright (C) 1996-2022 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3
4   The GNU C Library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8
9   The GNU C Library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with the GNU C Library; if not, see
16   <https://www.gnu.org/licenses/>.  */
17
18/* clone() is even more special than fork() as it mucks with stacks
19   and invokes a function in the right context after its all over.  */
20
21#include <asm/errno.h>
22#include <tcb-offsets.h>
23#include <sysdep.h>
24
25/* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
26	     pid_t *ptid, void *tls, pid_t *ctid); */
27
28	.text
29ENTRY (__clone)
30	save	%sp,-96,%sp
31	cfi_def_cfa_register(%fp)
32	cfi_window_save
33	cfi_register(%o7, %i7)
34
35	/* sanity check arguments */
36	orcc	%i0,%g0,%g2
37	be	.Leinval
38	 orcc	%i1,%g0,%o1
39	be	.Leinval
40	 mov	%i2,%o0
41
42	/* The child_stack is the top of the stack, allocate one
43	   whole stack frame from that as this is what the kernel
44	   expects.  */
45	sub	%o1, 96, %o1
46	mov	%i3, %g3
47
48	/* ptid */
49	mov	%i4,%o2
50	/* tls */
51	mov	%i5,%o3
52	/* ctid */
53	ld	[%fp+92],%o4
54
55	/* Do the system call */
56	set	__NR_clone,%g1
57	ta	0x10
58	bcs	.Lerror
59	 tst	%o1
60	bne	__thread_start
61	 nop
62	jmpl	%i7 + 8, %g0
63	 restore %o0,%g0,%o0
64
65.Leinval:
66	mov	EINVAL, %o0
67.Lerror:
68	call	HIDDEN_JUMPTARGET(__errno_location)
69	 mov	%o0, %i0
70	st	%i0,[%o0]
71	jmpl	%i7 + 8, %g0
72	 restore %g0,-1,%o0
73END(__clone)
74
75	.type	__thread_start,@function
76__thread_start:
77	mov	%g0, %fp	/* terminate backtrace */
78	call	%g2
79	 mov	%g3,%o0
80	set	__NR_exit, %g1
81	ta	0x10
82	 nop
83
84	.size	__thread_start, .-__thread_start
85
86libc_hidden_def (__clone)
87weak_alias (__clone, clone)
88