2 Copyright (C) 2015-2023 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/>. */
24 /* makecontext sets up a stack and the registers for the
25 user context. The stack looks like this:
27 +-----------------------+
28 | padding as required |
29 +-----------------------+
30 sp -> | parameters 5 to n |
31 +-----------------------+
33 The registers are set up like this:
34 r4--r7 : parameter 1 to 4
40 __makecontext (ucontext_t
*ucp
, void (*func
) (void), int argc
, ...)
42 extern void __startcontext (void);
47 sp
= (unsigned long *)
48 ((uintptr_t) ucp
->uc_stack
.ss_sp
+ ucp
->uc_stack
.ss_size
);
50 /* Allocate stack arguments. */
51 sp
-= argc
< 4 ? 0 : argc
- 4;
53 /* Keep the stack aligned. */
54 sp
= (unsigned long*) (((uintptr_t) sp
) & -4L);
56 /* Init version field. */
57 ucp
->uc_mcontext
.version
= 2;
58 /* Keep uc_link in r16. */
59 ucp
->uc_mcontext
.regs
[15] = (uintptr_t) ucp
->uc_link
;
60 /* Return address points to __startcontext(). */
61 ucp
->uc_mcontext
.regs
[23] = (uintptr_t) &__startcontext
;
62 /* Frame pointer is null. */
63 ucp
->uc_mcontext
.regs
[24] = (uintptr_t) 0;
64 /* Restart in user-space starting at 'func'. */
65 ucp
->uc_mcontext
.regs
[27] = (uintptr_t) func
;
66 /* Set stack pointer. */
67 ucp
->uc_mcontext
.regs
[28] = (uintptr_t) sp
;
70 for (i
= 0; i
< argc
; ++i
)
72 ucp
->uc_mcontext
.regs
[i
+ 3] = va_arg (ap
, unsigned long);
74 sp
[i
- 4] = va_arg (ap
, unsigned long);
79 weak_alias (__makecontext
, makecontext
)