2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / powerpc / powerpc32 / makecontext.S
blob9451f9eb36feb80ed6cfad8f3e8f29ad5e81dba2
1 /* Set up a context to call a function.
2    Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA
18    02110-1301 USA.  */
20 #include <sysdep.h>
21 #include <shlib-compat.h>
23 #define __ASSEMBLY__
24 #include <asm/ptrace.h>
25 #include "ucontext_i.h"
27 ENTRY(__makecontext)
28         /* Set up the first 7 args to the function in its registers */
29         lwz     r11,_UC_REGS_PTR(r3)
30         stw     r6,_UC_GREGS+(PT_R3*4)(r11)
31         stw     r7,_UC_GREGS+(PT_R4*4)(r11)
32         stw     r8,_UC_GREGS+(PT_R5*4)(r11)
33         stw     r9,_UC_GREGS+(PT_R6*4)(r11)
34         stw     r10,_UC_GREGS+(PT_R7*4)(r11)
35         lwz     r8,8(r1)
36         lwz     r9,12(r1)
37         stw     r8,_UC_GREGS+(PT_R8*4)(r11)
38         stw     r9,_UC_GREGS+(PT_R9*4)(r11)
40         /* Set the NIP to the start of the function */
41         stw     r4,_UC_GREGS+(PT_NIP*4)(r11)
43         /* Set the function's r31 to ucp->uc_link for the exitcode below. */
44         lwz     r7,_UC_LINK(r3)
45         stw     r7,_UC_GREGS+(PT_R31*4)(r11)
47         /* Set the function's LR to point to the exitcode below. */
48 #ifdef PIC
49         mflr    r0
50         cfi_register(lr,r0)
51         bl      1f
52 1:      mflr    r6
53         addi    r6,r6,L(exitcode)-1b
54         mtlr    r0
55         cfi_same_value (lr)
56 #else
57         lis     r6,L(exitcode)@ha
58         addi    r6,r6,L(exitcode)@l
59 #endif
60         stw     r6,_UC_GREGS+(PT_LNK*4)(r11)
62         /*
63          * Set up the stack frame for the function.
64          * If we have more than 5 args to the function (8 args to makecontext),
65          * there will be some arguments on the stack which have to end up
66          * in registers.  If there are more than 8 args to the function,
67          * we have to copy (argc - 8) args from our stack to the functions'
68          * stack (and allow space for them in the frame).
69          */
70         lwz     r4,_UC_STACK_SP(r3)
71         lwz     r8,_UC_STACK_SIZE(r3)
72         add     r4,r4,r8
73         rlwinm  r4,r4,0,0,27    /* round down to 16-byte boundary */
74         addi    r7,r4,-16       /* stack frame for fn's caller */
75         cmpwi   r5,8
76         blt     2f              /* less than 8 args is easy */
77         lwz     r10,16(r1)
78         stw     r10,_UC_GREGS+(PT_R10*4)(r11)
79         beq     2f              /* if exactly 8 args */
80         subi    r9,r5,3
81         subi    r5,r5,8
82         rlwinm  r9,r9,2,0,27
83         subf    r7,r9,r4
84         mtctr   r5              /* copy the 9th and following args */
85         addi    r6,r1,16
86         addi    r8,r7,4
87 3:      lwzu    r10,4(r6)
88         stwu    r10,4(r8)
89         bdnz    3b
90 2:      stw     r7,_UC_GREGS+(PT_R1*4)(r11)
91         li      r6,0
92         stw     r6,0(r7)
94         blr
97  * If the function returns, it comes here.  We put ucp->uc_link in
98  * r31, which is a callee-saved register.  We have to continue with
99  * the context that r31 points to, or exit if it is 0.
100  */
101 L(exitcode):
102         mr.     r3,r31
103         beq     4f
104         bl      __setcontext@local
105 4:      bl      HIDDEN_JUMPTARGET(exit)
106         b       4b
108 END(__makecontext)
110 versioned_symbol (libc, __makecontext, makecontext, GLIBC_2_3_4)
112 #if SHLIB_COMPAT (libc, GLIBC_2_3_3, GLIBC_2_3_4)
114         compat_text_section
115 ENTRY(__novec_makecontext)
116         /* Set up the first 7 args to the function in its registers */
117         addi    r11,r3,_UC_REG_SPACE
118         stw     r11,_UC_REGS_PTR(r3)
119         stw     r6,_UC_GREGS+(PT_R3*4)(r11)
120         stw     r7,_UC_GREGS+(PT_R4*4)(r11)
121         stw     r8,_UC_GREGS+(PT_R5*4)(r11)
122         stw     r9,_UC_GREGS+(PT_R6*4)(r11)
123         stw     r10,_UC_GREGS+(PT_R7*4)(r11)
124         lwz     r8,8(r1)
125         lwz     r9,12(r1)
126         stw     r8,_UC_GREGS+(PT_R8*4)(r11)
127         stw     r9,_UC_GREGS+(PT_R9*4)(r11)
129         /* Set the NIP to the start of the function */
130         stw     r4,_UC_GREGS+(PT_NIP*4)(r11)
132         /* Set the function's r31 to ucp->uc_link for the exitcode below. */
133         lwz     r7,_UC_LINK(r3)
134         stw     r7,_UC_GREGS+(PT_R31*4)(r11)
136         /* Set the function's LR to point to the exitcode below. */
137 #ifdef PIC
138         mflr    r0
139         cfi_register(lr,r0)
140         bl      1f
141 1:      mflr    r6
142         addi    r6,r6,L(novec_exitcode)-1b
143         mtlr    r0
144         cfi_same_value (lr)
145 #else
146         lis     r6,L(novec_exitcode)@ha
147         addi    r6,r6,L(novec_exitcode)@l
148 #endif
149         stw     r6,_UC_GREGS+(PT_LNK*4)(r11)
151         /*
152          * Set up the stack frame for the function.
153          * If we have more than 5 args to the function (8 args to makecontext),
154          * there will be some arguments on the stack which have to end up
155          * in registers.  If there are more than 8 args to the function,
156          * we have to copy (argc - 8) args from our stack to the functions'
157          * stack (and allow space for them in the frame).
158          */
159         lwz     r4,_UC_STACK_SP(r3)
160         lwz     r8,_UC_STACK_SIZE(r3)
161         add     r4,r4,r8
162         rlwinm  r4,r4,0,0,27    /* round down to 16-byte boundary */
163         addi    r7,r4,-16       /* stack frame for fn's caller */
164         cmpwi   r5,8
165         blt     2f              /* less than 8 args is easy */
166         lwz     r10,16(r1)
167         stw     r10,_UC_GREGS+(PT_R10*4)(r11)
168         beq     2f              /* if exactly 8 args */
169         subi    r9,r5,3
170         subi    r5,r5,8
171         rlwinm  r9,r9,2,0,27
172         subf    r7,r9,r4
173         mtctr   r5              /* copy the 9th and following args */
174         addi    r6,r1,16
175         addi    r8,r7,4
176 3:      lwzu    r10,4(r6)
177         stwu    r10,4(r8)
178         bdnz    3b
179 2:      stw     r7,_UC_GREGS+(PT_R1*4)(r11)
180         li      r6,0
181         stw     r6,0(r7)
183         blr
186  * If the function returns, it comes here.  We put ucp->uc_link in
187  * r31, which is a callee-saved register.  We have to continue with
188  * the context that r31 points to, or exit if it is 0.
189  */
190 L(novec_exitcode):
191         mr.     r3,r31
192         beq     4f
193         bl      __novec_setcontext@local
194 4:      bl      HIDDEN_JUMPTARGET(exit)
195         b       4b
197 END(__novec_makecontext)
198         .previous
200 compat_symbol (libc, __novec_makecontext, makecontext, GLIBC_2_3_3)
201 #endif
203 #if SHLIB_COMPAT (libc, GLIBC_2_1, GLIBC_2_3_3)
205 #define _ERRNO_H        1
206 #include <bits/errno.h>
208         compat_text_section
209 ENTRY (__makecontext_stub)
210         li      r3,ENOSYS
211         b       __syscall_error@local
212 END (__makecontext_stub)
213         .previous
215 compat_symbol (libc, __makecontext_stub, makecontext, GLIBC_2_1)
217 #endif