summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/nios2/clone.S
blob: ba514c40bda588e7cf76427bebaf2c2d251c301e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * libc/sysdeps/linux/nios2/clone.S -- `clone' syscall for linux/nios2
 *
 *  Copyright (C) 2004  Microtronix Datacom Ltd
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License.  See the file COPYING.LIB in the main
 * directory of this archive for more details.
 * 
 * Written by Wentao Xu <wentao@microtronix.com>
 */

#define _ERRNO_H
#include <bits/errno.h>
#include <sys/syscall.h>

#ifdef __NR_clone
/* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */

.text
.global __clone
.type __clone,%function
.align 4
__clone:
	addi    sp,sp,-8
	mov     r8,r4
	stw     ra,4(sp)
	stw     r16,0(sp)

	mov     r4,r6
	movi    r2,-EINVAL
	
	/* sanity check */
	beq     r8,zero,CLONE_ERROR_LABEL
	beq     r5,zero,CLONE_ERROR_LABEL
	
	/* system call */
	movi    r2,TRAP_ID_SYSCALL
	movi    r3,__NR_clone
	trap
	
	/* child call the function */
	mov     r4,r7
	bne     r2,zero,CLONE_ERROR_LABEL
	callr   r8
	
	/* exit if it returns */
	mov     r4,r2
	movi    r3,__NR_exit
	trap
	
CLONE_ERROR_LABEL: 
	movi    r3,-4096
	sub     r16,zero,r2
	bgeu    r3,r2,CLONE_OK
	
	/* store errno */
	call    __errno_location
	stw     r16,0(r2)
	movi    r2,-1

CLONE_OK:
	ldw     ra,4(sp)
	ldw     r16,0(sp)
	addi    sp,sp,8
	ret

.size __clone,.-__clone

.weak clone
	clone = __clone

#endif