Merge remote-tracking branch 'ppc-port/ppc-squashed'
[musl] / src / thread / powerpc / clone.s
1 .text
2 .global __clone
3 .type __clone, %function
4 __clone:
5 # int clone(fn, stack, flags, arg, ptid, tls, ctid)
6 #            a  b       c     d     e    f    g
7 #            3  4       5     6     7    8    9
8 # pseudo C code:
9 # tid = syscall(SYS_clone,c,b,e,f,g);
10 # if (!tid) syscall(SYS_exit, a(d));
11 # return tid;
12
13 # SYS_clone = 120
14 # SYS_exit = 1
15
16 # in order that the child can find the start func and its arg, we need to store it into
17 # non-volative regs. to do so, we have to store those 2 regs into our stackframe, so
18 # we can restore them later.
19 stw 30, -4(1)
20 stw 31, -8(1)
21 subi 1, 1, 16 
22
23 # save r3 (func) into r30, and r6(arg) into r31
24 mr 30, 3
25 mr 31, 6
26
27 #move c into first arg
28 mr 3, 5
29 #mr 4, 4
30 mr 5, 7
31 mr 6, 8
32 mr 7, 9
33
34 # move syscall number into r0    
35 li 0, 120
36
37 sc
38
39 # check for syscall error
40 #this code should be more efficient, but it borks
41 #bns+ 1f # jump to label 1 if no summary overflow.
42 #else
43 #neg 3, 3 #negate the result (errno)
44 #b 2f # jump to epilogue
45
46 # this error check code at least does not spoil the clone call.
47 #mfcr    0                      # Check for an error
48 #rlwinm  4, 0, 0, 3, 3        # by checking for bit 28.
49 #cmplwi  0, 4, 0               # It is an error if non-zero.
50 #beq     0, 1f                  # Jump if not an error.
51 #neg     3, 3                  # Negate the error number.
52 #b       2f # jump to epilogue
53 1:
54 # compare sc result with 0
55 cmpwi cr7, 3, 0
56
57 # if not 0, jump to end
58 bne cr7, 2f
59
60 #else: we're the child
61 #call funcptr
62 # move arg (d) into r3
63 mr 3, 31
64 #move r30 (funcptr) into CTR reg
65 mtctr 30
66 # call CTR reg
67 bctrl
68 # mov SYS_exit into r0 (the exit param is already in r3)
69 li 0, 1
70 sc
71
72 2:
73
74 # restore stack
75 addi 1, 1, 16
76 lwz 30, -4(1)
77 lwz 31, -8(1)
78
79 blr
80
81
82
83