clone.s 1.2 KB
Newer Older
1 2
.text
.global __clone
R
Rich Felker 已提交
3
.hidden __clone
4 5 6 7 8 9 10 11 12 13 14 15 16
.type __clone, %function
__clone:
# int clone(fn, stack, flags, arg, ptid, tls, ctid)
#            a  b       c     d     e    f    g
#            3  4       5     6     7    8    9
# pseudo C code:
# tid = syscall(SYS_clone,c,b,e,f,g);
# if (!tid) syscall(SYS_exit, a(d));
# return tid;

# SYS_clone = 120
# SYS_exit = 1

R
rofl0r 已提交
17 18 19 20
# store non-volatile regs r30, r31 on stack in order to put our
# start func and its arg there
stwu 30, -16(1)
stw 31, 4(1)
21 22 23 24 25

# save r3 (func) into r30, and r6(arg) into r31
mr 30, 3
mr 31, 6

26 27 28 29 30
# create initial stack frame for new thread
clrrwi 4, 4, 4
li 0, 0
stwu 0, -16(4)

31 32 33 34 35 36 37 38 39 40 41 42 43
#move c into first arg
mr 3, 5
#mr 4, 4
mr 5, 7
mr 6, 8
mr 7, 9

# move syscall number into r0    
li 0, 120

sc

# check for syscall error
R
rofl0r 已提交
44
bns+ 1f # jump to label 1 if no summary overflow.
45
#else
R
rofl0r 已提交
46
neg 3, 3 #negate the result (errno)
47 48 49 50 51 52 53 54
1:
# compare sc result with 0
cmpwi cr7, 3, 0

# if not 0, jump to end
bne cr7, 2f

#else: we're the child
R
rofl0r 已提交
55
#call funcptr: move arg (d) into r3
56 57 58 59 60 61 62 63 64 65 66 67
mr 3, 31
#move r30 (funcptr) into CTR reg
mtctr 30
# call CTR reg
bctrl
# mov SYS_exit into r0 (the exit param is already in r3)
li 0, 1
sc

2:

# restore stack
R
rofl0r 已提交
68 69
lwz 30, 0(1)
lwz 31, 4(1)
70 71 72 73
addi 1, 1, 16

blr