summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/metag/clone.S
blob: 8fff567109604be3654f16d5f5cf8f7e3fc2f122 (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
!    Copyright (C) 2013 Imagination Technologies Ltd.

!    Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.

!    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 <asm/errno.h>
#include <asm/unistd.h>

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

	.text
	.global	__clone
	.type	__clone,function
__clone:
	! sanity check args
	MOV	D0Re0, #-EINVAL
	CMP	D1Ar1, #0
	BEQ	___error
	CMP	D0Ar2, #0
	BEQ     ___error

	! save function pointer
	MOV D0FrT, D1Ar1

	! do the system call
	! get flags
	MOV	D1Ar1, D1Ar3
	! new sp is already in D0Ar2
	MOV	D1Re0, #__NR_clone
	SWITCH	#0x440001
	CMP	D0Re0,#0
	! Error on -1
	BLT	___error
	! If non-zero we are the parent
	MOVNE	PC, D1RtP
	! BRKPNT

	! We are the child
	! pick the function arg and call address off the stack and execute
	MOV     D0Ar2, D0FrT
	MOV     D1Ar1, D0Ar4
	MOV     D1RtP, PC
	ADD	D1RtP, D1RtP, #8
	MOV	PC, D0Ar2

	! and we are done, passing the return value through D0Re0
#ifdef __PIC__
	B	_exit@PLT
#else
	B	_exit
#endif

___error:
	MOV	D1Ar1, D0Re0
#ifdef __PIC__
	B	___syscall_error@PLT
#else
	B	___syscall_error
#endif
	.size	__clone, .-__clone

.weak    _clone
_clone    =   __clone