(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / unix / sysv / linux / powerpc / powerpc64 / makecontext.S
blob8034559200ccd657eedd4ca76c6a73cc0c2049e8
1 /* Create new context.
2    Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #include <sysdep.h>
21 #include "kernel-features.h"
23 #define __ASSEMBLY__
24 #include <asm/ptrace.h>
25 #include "ucontext_i.h"
26 #include <asm/errno.h>
28 ENTRY(__makecontext)
29         CALL_MCOUNT 3
30 #ifdef __ASSUME_NEW_RT_SIGRETURN_SYSCALL
31   /* Save parameters into the parameter save area of callers frame.  */
32   std  r3,FRAME_PARM1_SAVE(r1) /* ucontext_t *ucp  */
33   std  r4,FRAME_PARM2_SAVE(r1) /* void (*func)(void)  */
34   std  r5,FRAME_PARM3_SAVE(r1) /* int argc  */
35   std  r6,FRAME_PARM4_SAVE(r1) /* ... */
36   std  r7,FRAME_PARM5_SAVE(r1)
37   std  r8,FRAME_PARM6_SAVE(r1)
38   std  r9,FRAME_PARM7_SAVE(r1)
39   std  r10,FRAME_PARM8_SAVE(r1)
40   mflr  r0
41   /* Get the address of the target functions first parameter.  */
42   addi  r6,r1,FRAME_PARM4_SAVE
43   std   r0,FRAME_LR_SAVE(r1)
44   stdu  r1,-128(r1)
46   /* Get the ucontexts stack pointer and size.  Compute the top of stack
47      and round down to a quadword boundary.  Then stack a dummy frame
48      with a null back chain.  We store the context pointer in the frames
49      "compiler double word" field so we can recover if is the function
50      returns.  Finally save the callers link register and TOC pointer
51      into this frame so the debugger can display a backtrace.
52   */
53   ld    r7,UCONTEXT_STACK_SP(r3)
54   ld    r0,UCONTEXT_STACK_SIZE(r3)
55   add   r7,r7,r0
56   clrrdi  r7,r7,4
57   li    r0,0
58   stdu  r0,-64(r7)
59   std   r3,FRAME_PARM1_SAVE(r7) /* Store context in dummy parm1.  */
60   mflr  r0
61   std   r2,FRAME_TOC_SAVE(r7)  /* Store the TOC pointer for later.  */
62   std   r0,FRAME_LR_SAVE(r7)
64   /* Now we need to stack another frame to hold the parameter save area
65      for the function.  We need to allocate a frame with the minimum 48
66      byte header and 8 parameter register.  However if there are more
67      than 8 parameters addition space is need to hold all the parameters.
68      The total size it rounded up to a quadword multiple then a frame is
69      stacked.  This address is stored in the ucontext as GPR 1.  */
71   cmpdi cr1,r5,8
72   sldi  r8,r5,3
73   bgt   cr1,L(gt8)
74   li    r8,64
75 L(gt8):
76   addi  r8,r8,FRAME_PARM_SAVE+8 /* Add header plus rounding factor.  */
77   clrrdi  r8,r8,4  /* Round down to quadword.  */
79   subf  r8,r8,r7
80   std   r7,0(r8)   /* Stack the frame.  */
81   std   r8,(SIGCONTEXT_GP_REGS+(PT_R1*8))(r3)
83   /* Now we need to copy the target functions parameters.  The functions
84      parameters are saved in the parameter save area.  We skip over the
85      first three parameters and copy up to 8 double word into the
86      SIGCONTEXT_GP_REGS starting with R3.  If there are more than 8
87      parameters then doublewords 8-N are copied into the parameter
88      save area of the context frame.  */
89   cmpdi r5,0
90   beq   L(noparms)
91   mr    r0,r5
92   ble   cr1,L(le8)
93   li    r0,8
94 L(le8):
95   mtctr r0
96   addi  r7,r6,-8
97   addi  r9,r3,(SIGCONTEXT_GP_REGS+(PT_R3*8)-8)
98 L(parmloop2):
99   ldu   r0,8(r7)
100   stdu  r0,8(r9)
101   bdnz  L(parmloop2)
103   addi  r0,r5,-8
104   ble   cr1,L(noparms)
105   mtctr r0
106   addi  r9,r8,FRAME_PARM9_SAVE-8
107 L(parmloop):
108   ldu   r0,8(r7)
109   stdu  r0,8(r9)
110   bdnz  L(parmloop)
112 L(noparms):
114   /* Load the function address and TOC from the function descriptor
115      and store them in the ucontext as NIP and r2.  Store the 3rd
116      field of the function descriptor into the ucontext as r11 in case
117      the calling language needs the "environment pointer".  */
118   ld    r0,0(r4)
119   ld    r10,8(r4);
120   ld    r9,16(r4);
121   std   r0,(SIGCONTEXT_GP_REGS+(PT_NIP*8))(r3)
122   std   r10,(SIGCONTEXT_GP_REGS+(PT_R2*8))(r3)
123   std   r9,(SIGCONTEXT_GP_REGS+(PT_R11*8))(r3)
125   /* If the target function returns we need to do some cleanup.  We use a
126      code trick to get the address of our cleanup function into the link
127      register.  Do not add any code between here and L(exitcode).  */
128   bl  L(gotexitcodeaddr);
130         /* This is the helper code which gets called if a function which
131            is registered with 'makecontext' returns.  In this case we
132            have to install the context listed in the uc_link element of
133            the context 'makecontext' manipulated at the time of the
134            'makecontext' call.  If the pointer is NULL the process must
135            terminate.  */
136 L(exitcode):
137    /* Recover the ucontext and TOC from the dummy frame.  */
138   ld    r1,FRAME_BACKCHAIN(r1)  /* Unstack the parameter save area frame.  */
139   ld    r3,FRAME_PARM1_SAVE(r1)
140   ld    r2,FRAME_TOC_SAVE(r1)
141   ld    r3,UCONTEXT_LINK(r3)  /* Load the resume context.  */
142   cmpdi r3,0
143   beq   L(BADSTATUS)
144         bl    JUMPTARGET(__setcontext)
145         nop
147 L(BADSTATUS):
148 /* If setcontext returns (which can happen if the syscall fails) we will
149    exit the program with error status (-1).  */
150   li    r3,-1
151 #ifdef SHARED
152   b     JUMPTARGET(__GI_exit);
153 #else
154   b     JUMPTARGET(exit);
155 #endif
157   /* The address of the exit code is in the link register.  Store the lr
158      in the ucontext as LNK so the target function will return to our
159      exit code.  */
160 L(gotexitcodeaddr):
161   mflr  r0
162   std   r0,(SIGCONTEXT_GP_REGS+(PT_LNK*8))(r3)
163   ld    r0,128+FRAME_LR_SAVE(r1)
164   addi  r1,r1,128
165   mtlr  r0
166   blr
167 #else
168   /* If the kernel is not at least 2.4.21 then generate a ENOSYS stub.  */
169   mflr r0
170   std  r0,FRAME_LR_SAVE(r1)
171   stdu r1,-128(r1)
172   li   r3,ENOSYS
173   bl   JUMPTARGET(__syscall_error)
174   nop
175   li   r3,-1
176   ld   r0,128+FRAME_LR_SAVE(r1)
177   addi r1,r1,128
178   mtlr r0
179   blr
180 #endif
181 END(__makecontext)
183 weak_alias(__makecontext, makecontext)