2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / powerpc / powerpc64 / makecontext.S
blob4a82802d9660dc907d0fb7f9043b79dd9ec4dbee
1 /* Create new context.
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 <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   cfi_offset (lr, FRAME_LR_SAVE)
45   stdu  r1,-128(r1)
46   cfi_adjust_cfa_offset (128)
48   /* Get the ucontexts stack pointer and size.  Compute the top of stack
49      and round down to a quadword boundary.  Then stack a dummy frame
50      with a null back chain.  We store the context pointer in the frames
51      "compiler double word" field so we can recover if is the function
52      returns.  Finally save the callers link register and TOC pointer
53      into this frame so the debugger can display a backtrace.
54   */
55   ld    r7,UCONTEXT_STACK_SP(r3)
56   ld    r0,UCONTEXT_STACK_SIZE(r3)
57   add   r7,r7,r0
58   clrrdi  r7,r7,4
59   li    r0,0
60   stdu  r0,-64(r7)
61   std   r3,FRAME_PARM1_SAVE(r7) /* Store context in dummy parm1.  */
62   mflr  r0
63   std   r2,FRAME_TOC_SAVE(r7)  /* Store the TOC pointer for later.  */
64   std   r0,FRAME_LR_SAVE(r7)
66   /* Now we need to stack another frame to hold the parameter save area
67      for the function.  We need to allocate a frame with the minimum 48
68      byte header and 8 parameter register.  However if there are more
69      than 8 parameters addition space is need to hold all the parameters.
70      The total size it rounded up to a quadword multiple then a frame is
71      stacked.  This address is stored in the ucontext as GPR 1.  */
73   cmpdi cr1,r5,8
74   sldi  r8,r5,3
75   bgt   cr1,L(gt8)
76   li    r8,64
77 L(gt8):
78   addi  r8,r8,FRAME_PARM_SAVE+8 /* Add header plus rounding factor.  */
79   clrrdi  r8,r8,4  /* Round down to quadword.  */
81   subf  r8,r8,r7
82   std   r7,0(r8)   /* Stack the frame.  */
83   std   r8,(SIGCONTEXT_GP_REGS+(PT_R1*8))(r3)
85   /* Now we need to copy the target functions parameters.  The functions
86      parameters are saved in the parameter save area.  We skip over the
87      first three parameters and copy up to 8 double word into the
88      SIGCONTEXT_GP_REGS starting with R3.  If there are more than 8
89      parameters then doublewords 8-N are copied into the parameter
90      save area of the context frame.  */
91   cmpdi r5,0
92   beq   L(noparms)
93   mr    r0,r5
94   ble   cr1,L(le8)
95   li    r0,8
96 L(le8):
97   mtctr r0
98   addi  r7,r6,-8
99   addi  r9,r3,(SIGCONTEXT_GP_REGS+(PT_R3*8)-8)
100 L(parmloop2):
101   ldu   r0,8(r7)
102   stdu  r0,8(r9)
103   bdnz  L(parmloop2)
105   addi  r0,r5,-8
106   ble   cr1,L(noparms)
107   mtctr r0
108   addi  r9,r8,FRAME_PARM9_SAVE-8
109 L(parmloop):
110   ldu   r0,8(r7)
111   stdu  r0,8(r9)
112   bdnz  L(parmloop)
114 L(noparms):
116   /* Load the function address and TOC from the function descriptor
117      and store them in the ucontext as NIP and r2.  Store the 3rd
118      field of the function descriptor into the ucontext as r11 in case
119      the calling language needs the "environment pointer".  */
120   ld    r0,0(r4)
121   ld    r10,8(r4);
122   ld    r9,16(r4);
123   std   r0,(SIGCONTEXT_GP_REGS+(PT_NIP*8))(r3)
124   std   r10,(SIGCONTEXT_GP_REGS+(PT_R2*8))(r3)
125   std   r9,(SIGCONTEXT_GP_REGS+(PT_R11*8))(r3)
127   /* If the target function returns we need to do some cleanup.  We use a
128      code trick to get the address of our cleanup function into the link
129      register.  Do not add any code between here and L(exitcode).  */
130   bl  L(gotexitcodeaddr);
132         /* This is the helper code which gets called if a function which
133            is registered with 'makecontext' returns.  In this case we
134            have to install the context listed in the uc_link element of
135            the context 'makecontext' manipulated at the time of the
136            'makecontext' call.  If the pointer is NULL the process must
137            terminate.  */
138 L(exitcode):
139    /* Recover the ucontext and TOC from the dummy frame.  */
140   ld    r1,FRAME_BACKCHAIN(r1)  /* Unstack the parameter save area frame.  */
141   ld    r3,FRAME_PARM1_SAVE(r1)
142   ld    r2,FRAME_TOC_SAVE(r1)
143   ld    r3,UCONTEXT_LINK(r3)  /* Load the resume context.  */
144   cmpdi r3,0
145   beq   L(BADSTATUS)
146         bl    JUMPTARGET(__setcontext)
147         nop
149 L(BADSTATUS):
150 /* If setcontext returns (which can happen if the syscall fails) we will
151    exit the program with error status (-1).  */
152   li    r3,-1
153 #ifdef SHARED
154   b     JUMPTARGET(__GI_exit);
155 #else
156   b     JUMPTARGET(exit);
157 #endif
159   /* The address of the exit code is in the link register.  Store the lr
160      in the ucontext as LNK so the target function will return to our
161      exit code.  */
162 L(gotexitcodeaddr):
163   mflr  r0
164   std   r0,(SIGCONTEXT_GP_REGS+(PT_LNK*8))(r3)
165   ld    r0,128+FRAME_LR_SAVE(r1)
166   addi  r1,r1,128
167   mtlr  r0
168   blr
169 #else
170   /* If the kernel is not at least 2.4.21 then generate a ENOSYS stub.  */
171   mflr r0
172   std  r0,FRAME_LR_SAVE(r1)
173   cfi_offset (lr, FRAME_LR_SAVE)
174   stdu r1,-128(r1)
175   cfi_adjust_cfa_offset (128)
176   li   r3,ENOSYS
177   bl   JUMPTARGET(__syscall_error)
178   nop
179   li   r3,-1
180   ld   r0,128+FRAME_LR_SAVE(r1)
181   addi r1,r1,128
182   mtlr r0
183   blr
184 #endif
185 END(__makecontext)
187 weak_alias (__makecontext, makecontext)