1 /* Set up a context to call a function.
2 Copyright (C) 2002-2024 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, see
17 <https://www.gnu.org/licenses/>. */
20 #include <shlib-compat.h>
23 #include <asm/ptrace.h>
24 #include "ucontext_i.h"
27 /* Set up the first 7 args to the function in its registers */
28 lwz r11,_UC_REGS_PTR(r3)
29 stw r6,_UC_GREGS+(PT_R3*4)(r11)
30 stw r7,_UC_GREGS+(PT_R4*4)(r11)
31 stw r8,_UC_GREGS+(PT_R5*4)(r11)
32 stw r9,_UC_GREGS+(PT_R6*4)(r11)
33 stw r10,_UC_GREGS+(PT_R7*4)(r11)
36 stw r8,_UC_GREGS+(PT_R8*4)(r11)
37 stw r9,_UC_GREGS+(PT_R9*4)(r11)
39 /* Set the NIP to the start of the function */
40 stw r4,_UC_GREGS+(PT_NIP*4)(r11)
42 /* Set the function's r31 to ucp->uc_link for the exitcode below. */
44 stw r7,_UC_GREGS+(PT_R31*4)(r11)
46 /* Set the function's LR to point to the exitcode below. */
50 /* Use this conditional form of branch and link to avoid destroying
51 the cpu link stack used to predict blr return addresses. */
54 addi r6,r6,L(exitcode)-1b
59 addi r6,r6,L(exitcode)@l
61 stw r6,_UC_GREGS+(PT_LNK*4)(r11)
64 * Set up the stack frame for the function.
65 * If we have more than 5 args to the function (8 args to makecontext),
66 * there will be some arguments on the stack which have to end up
67 * in registers. If there are more than 8 args to the function,
68 * we have to copy (argc - 8) args from our stack to the functions'
69 * stack (and allow space for them in the frame).
71 lwz r4,_UC_STACK_SP(r3)
72 lwz r8,_UC_STACK_SIZE(r3)
74 rlwinm r4,r4,0,0,27 /* round down to 16-byte boundary */
75 addi r7,r4,-16 /* stack frame for fn's caller */
77 blt 2f /* less than 8 args is easy */
79 stw r10,_UC_GREGS+(PT_R10*4)(r11)
80 beq 2f /* if exactly 8 args */
85 mtctr r5 /* copy the 9th and following args */
91 2: stw r7,_UC_GREGS+(PT_R1*4)(r11)
100 * If the function returns, it comes here. We put ucp->uc_link in
101 * r31, which is a callee-saved register. We have to continue with
102 * the context that r31 points to, or exit if it is 0.
107 bl __setcontext@local
108 4: bl HIDDEN_JUMPTARGET(exit)
114 versioned_symbol (libc, __makecontext, makecontext, GLIBC_2_3_4)
116 #if SHLIB_COMPAT (libc, GLIBC_2_3_3, GLIBC_2_3_4)
119 ENTRY(__novec_makecontext)
120 /* Set up the first 7 args to the function in its registers */
121 addi r11,r3,_UC_REG_SPACE
122 stw r11,_UC_REGS_PTR(r3)
123 stw r6,_UC_GREGS+(PT_R3*4)(r11)
124 stw r7,_UC_GREGS+(PT_R4*4)(r11)
125 stw r8,_UC_GREGS+(PT_R5*4)(r11)
126 stw r9,_UC_GREGS+(PT_R6*4)(r11)
127 stw r10,_UC_GREGS+(PT_R7*4)(r11)
130 stw r8,_UC_GREGS+(PT_R8*4)(r11)
131 stw r9,_UC_GREGS+(PT_R9*4)(r11)
133 /* Set the NIP to the start of the function */
134 stw r4,_UC_GREGS+(PT_NIP*4)(r11)
136 /* Set the function's r31 to ucp->uc_link for the exitcode below. */
138 stw r7,_UC_GREGS+(PT_R31*4)(r11)
140 /* Set the function's LR to point to the exitcode below. */
144 /* Use this conditional form of branch and link to avoid destroying
145 the cpu link stack used to predict blr return addresses. */
148 addi r6,r6,L(novec_exitcode)-1b
152 lis r6,L(novec_exitcode)@ha
153 addi r6,r6,L(novec_exitcode)@l
155 stw r6,_UC_GREGS+(PT_LNK*4)(r11)
158 * Set up the stack frame for the function.
159 * If we have more than 5 args to the function (8 args to makecontext),
160 * there will be some arguments on the stack which have to end up
161 * in registers. If there are more than 8 args to the function,
162 * we have to copy (argc - 8) args from our stack to the functions'
163 * stack (and allow space for them in the frame).
165 lwz r4,_UC_STACK_SP(r3)
166 lwz r8,_UC_STACK_SIZE(r3)
168 rlwinm r4,r4,0,0,27 /* round down to 16-byte boundary */
169 addi r7,r4,-16 /* stack frame for fn's caller */
171 blt 2f /* less than 8 args is easy */
173 stw r10,_UC_GREGS+(PT_R10*4)(r11)
174 beq 2f /* if exactly 8 args */
179 mtctr r5 /* copy the 9th and following args */
185 2: stw r7,_UC_GREGS+(PT_R1*4)(r11)
194 * If the function returns, it comes here. We put ucp->uc_link in
195 * r31, which is a callee-saved register. We have to continue with
196 * the context that r31 points to, or exit if it is 0.
201 bl __novec_setcontext@local
202 4: bl HIDDEN_JUMPTARGET(exit)
206 END(__novec_makecontext)
209 compat_symbol (libc, __novec_makecontext, makecontext, GLIBC_2_3_3)
212 #if SHLIB_COMPAT (libc, GLIBC_2_1, GLIBC_2_3_3)
215 #include <bits/errno.h>
218 ENTRY (__makecontext_stub)
220 b __syscall_error@local
221 END (__makecontext_stub)
224 compat_symbol (libc, __makecontext_stub, makecontext, GLIBC_2_1)