1/* Copyright (C) 1996-2022 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), void *child_stack, int flags, void *arg,
27              pid_t *ptid, struct user_desc *tls, pid_t *ctid);
28
29   INCOMING: r5 (fn), r6 (child_stack), r7 (flags), r8 (arg), r9 (ptid)
30             r10 (tls), 28 (r1) ctid
31
32   OUTGOING:
33
34   linux: arch/microblaze/entry.S: sys_clone expects
35          r5 (flags)  r6 (child stack) r7 (stack_size) r8 (ptid)r9 (ctid)
36          r10 (tls)
37*/
38
39        .text
40ENTRY (__clone)
41	addik	r3,r0,-EINVAL
42	beqi	r5,SYSCALL_ERROR_LABEL	; // Invalid func
43	beqi	r6,SYSCALL_ERROR_LABEL	; // Invalid stack
44	addik	r6,r6,-8
45	swi	r5,r6,0			; // Push fn onto child's stack
46	swi	r8,r6,4			; // Push arg for child
47	addk	r5,r0,r7		; // flags for clone() syscall
48	addk	r7,r0,r0
49	addk	r8,r0,r9		; // parent tid ptr
50	lwi	r9,r1,28		; // child tid ptr
51	addik	r12,r0,SYS_ify(clone)
52	brki	r14,8
53	addk	r0,r0,r0
54	addik	r4,r0,-4095
55	cmpu	r4,r4,r3
56	bgei	r4,SYSCALL_ERROR_LABEL
57	beqi	r3,L(thread_start)
58	rtsd	r15,8
59	nop
60
61L(thread_start):
62	lwi	r12,r1,0		; // fn
63	lwi	r5,r1,4			; // arg
64	brald	r15,r12
65	nop
66	addk	r5,r0,r3
67	addik	r12,r0,SYS_ify(exit)
68	brki	r14,8
69	nop
70PSEUDO_END(__clone)
71
72libc_hidden_def (__clone)
73weak_alias (__clone,clone)
74