1.text
2.global __clone
3.hidden __clone
4.type __clone, %function
5__clone:
6# int clone(fn, stack, flags, arg, ptid, tls, ctid)
7# a b c d e f g
8# 3 4 5 6 7 8 9
9# pseudo C code:
10# tid = syscall(SYS_clone,c,b,e,f,g);
11# if (!tid) syscall(SYS_exit, a(d));
12# return tid;
13
14# SYS_clone = 120
15# SYS_exit = 1
16
17# store non-volatile regs r30, r31 on stack in order to put our
18# start func and its arg there
19stwu 1, -16(1)
20stw 30, 8(1)
21stw 31, 12(1)
22
23# save r3 (func) into r30, and r6(arg) into r31
24mr 30, 3
25mr 31, 6
26
27# create initial stack frame for new thread
28clrrwi 4, 4, 4
29li 0, 0
30stwu 0, -16(4)
31
32#move c into first arg
33mr 3, 5
34#mr 4, 4
35mr 5, 7
36mr 6, 8
37mr 7, 9
38
39# move syscall number into r0
40li 0, 120
41
42sc
43
44# check for syscall error
45bns+ 1f # jump to label 1 if no summary overflow.
46#else
47neg 3, 3 #negate the result (errno)
481:
49# compare sc result with 0
50cmpwi cr7, 3, 0
51
52# if not 0, restore stack and return
53beq cr7, 2f
54
55lwz 30, 8(1)
56lwz 31, 12(1)
57addi 1, 1, 16
58
59blr
60
612:
62#else: we're the child
63#call funcptr: move arg (d) into r3
64mr 3, 31
65#move r30 (funcptr) into CTR reg
66mtctr 30
67# call CTR reg
68bctrl
69# mov SYS_exit into r0 (the exit param is already in r3)
70li 0, 1
71sc