1/* Wrapper around clone system call.
2   Copyright (C) 1997-2021 Free Software Foundation, Inc.
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#include <sysdep.h>
20#define _ERRNO_H	1
21#include <bits/errno.h>
22
23/* This is the only really unusual system call in PPC linux, but not
24   because of any weirdness in the system call itself; because of
25   all the freaky stuff we have to do to make the call useful.  */
26
27/* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4],
28		  int flags [r5], void *arg [r6], void *parent_tid [r7],
29		  void *tls [r8], void *child_tid [r9]); */
30
31ENTRY (__clone)
32
33	/* Check for child_stack == NULL || fn == NULL.  */
34	cmpwi	cr0,r4,0
35	cmpwi	cr1,r3,0
36	cror	cr0*4+eq,cr1*4+eq,cr0*4+eq
37	beq-	cr0,L(badargs)
38
39	/* Set up stack frame for parent.  */
40	stwu	r1,-32(r1)
41	cfi_adjust_cfa_offset (32)
42	stmw	r28,16(r1)
43
44	/* Set up stack frame for child.  */
45	clrrwi	r4,r4,4
46	li	r0,0
47	stwu	r0,-16(r4)
48
49	/* Save fn, args, stack across syscall.  */
50	mr	r30,r3			/* Function in r30.  */
51	mr	r28,r5
52	mr	r31,r6			/* Argument in r31.  */
53
54	/* 'flags' argument is first parameter to clone syscall. (The other
55	   argument is the stack pointer, already in r4.)  */
56	mr	r3,r5
57
58	/* Move the parent_tid, child_tid and tls arguments. */
59	mr	r5,r7
60	mr	r6,r8
61	mr	r7,r9
62
63	/* End FDE now, because in the child the unwind info will be
64	   wrong.  */
65	cfi_endproc
66
67	/* Do the call.  */
68	DO_CALL(SYS_ify(clone))
69
70	/* Check for child process.  */
71	cmpwi	cr1,r3,0
72	crandc	cr1*4+eq,cr1*4+eq,cr0*4+so
73	bne-	cr1,L(parent)		/* The '-' is to minimise the race.  */
74
75	/* Call procedure.  */
76	mtctr	r30
77	mr	r3,r31
78	bctrl
79	DO_CALL(SYS_ify(exit))
80
81L(parent):
82	/* Parent.  Restore registers & return.  */
83	lmw	r28,16(r1)
84	addi	r1,r1,32
85	bnslr+
86	b	__syscall_error@local
87
88L(badargs):
89	li	r3,EINVAL
90	b	__syscall_error@local
91
92	cfi_startproc
93END (__clone)
94
95libc_hidden_def (__clone)
96weak_alias (__clone, clone)
97