summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/nds32/clone.S
blob: b28619bf1729925d3de6d83fcf8232c27fb483ec (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2016-2017 Andes Technology, Inc.
 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
 */

/* Copyright (C) 2010-2014 Free Software Foundation, Inc.
   Contributed by Pat Beirne <patb@corelcomputer.com>

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

/* clone() is even more special than fork() as it mucks with stacks
   and invokes a function in the right context after its all over.  */

#include <sysdep.h>
#define _ERRNO_H	1
#include <bits/errno.h>

/* int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
   _syscall2(int, clone, int, flags, void *, child_stack)  */

ENTRY(__clone)
#ifdef __NDS32_ABI_2FP_PLUS__
	lwi	$r4, [$sp]
	lwi	$r5, [$sp+4]
#endif
#ifdef PIC
	/* set GP register to parent only, cause child's $SP will be $r1. */
	pushm	$fp, $gp
	cfi_adjust_cfa_offset(8)
	cfi_rel_offset(fp, 0)
	cfi_rel_offset(gp, 4)
	mfusr	$r15, $pc
	sethi	$gp, hi20(_GLOBAL_OFFSET_TABLE_+4)
	ori	$gp, $gp, lo12(_GLOBAL_OFFSET_TABLE_+8)
	add	$gp, $gp, $r15
#endif /* PIC  */

	/* sanity check arguments.  */
	beqz	$r0, 1f
	bnez	$r1, 2f

1:
	movi	$r0, -EINVAL

5:
#ifdef PIC
	/* restore GP register, only in parent's stack  */
	la	$r15, C_SYMBOL_NAME(__syscall_error@PLT)
	push	$lp
	cfi_adjust_cfa_offset(4)
	cfi_rel_offset(lp, 0)
	addi	$sp, $sp, -4
	cfi_adjust_cfa_offset(4)
	jral	$r15
	addi	$sp, $sp, 4
	cfi_adjust_cfa_offset(-4)
	pop	$lp
	cfi_adjust_cfa_offset(-4)
	cfi_restore(lp)
	popm	$fp, $gp
	cfi_adjust_cfa_offset(-8)
	cfi_restore(fp)
	cfi_restore(gp)
	ret
#else /* ! PIC  */
	la	$r15, C_SYMBOL_NAME(__syscall_error)
	jr	$r15
#endif /* ! PIC  */

2:
	/* Child's $sp will be $r1, make $sp 8-byte alignment */
	bitci	$r1, $r1, 7
	/* push to child's stack only.  */
	addi	$r1, $r1, -4
	swi.p	$r3, [$r1], -4			! arg
	swi	$r0, [$r1]			! fn

	/* do the system call  */
	or	$r0, $r2, $r2			! move $r0, $r2

	move    $r3, $r5
	move    $r5, $r2                        ! Use $r5 to backup $r2
						! The pt_regs is placed in $r5 in kerenl (sys_clone_wrapper)
	move    $r2, $r4

#ifdef __NDS32_ABI_2FP_PLUS__
# ifdef PIC
       lwi     $r4, [$sp+#0x10]
# else
       lwi     $r4, [$sp+#0x8]
# endif
#else
# ifdef PIC
       lwi     $r4, [$sp+#0x8]
# else
       lwi     $r4, [$sp]
# endif
#endif

	__do_syscall(clone)
	beqz    $r0, 4f
	bltz    $r0, 5b


10:
#ifdef PIC
	/* restore GP register, only in parent's stack  */
	popm	$fp, $gp
	cfi_adjust_cfa_offset(-8)
	cfi_restore(gp)
	cfi_restore(fp)
#endif /* PIC  */
	ret
4:
	/* Only in child's stack.  */
	pop	$r1				! fn
	pop	$r0				! arg


#if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
	addi	$sp, $sp, -24
#endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)  */

	! use $r15 in case _exit is PIC
	bral	$r1

#if !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)
	addi	$sp, $sp, 24
#endif /* !defined(__NDS32_ABI_2__) && !defined(__NDS32_ABI_2FP_PLUS__)  */

	! use $r15 in case _exit is PIC
#ifdef PIC
	la	$r15, C_SYMBOL_NAME(_exit@PLT)
#else /* ! PIC  */
	la	$r15, C_SYMBOL_NAME(_exit)
#endif /* ! PIC  */
	jr	$r15


PSEUDO_END (__clone)
weak_alias (__clone, clone)