1 /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 #include <stdarg.h>
19 #include <ucontext.h>
20
21 /* Number of arguments that go in registers. */
22 #define NREG_ARGS 4
23
24 /* Take a context previously prepared via getcontext() and set to
25 call func() with the given int only args. */
26 void
__makecontext(ucontext_t * ucp,void (* func)(void),int argc,...)27 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
28 {
29 extern void __startcontext (void);
30 unsigned long *funcstack;
31 va_list vl;
32 unsigned long *regptr;
33 unsigned int reg;
34 int misaligned;
35
36 /* Start at the top of stack. */
37 funcstack = (unsigned long *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
38
39 /* Ensure the stack stays eight byte aligned. */
40 misaligned = ((unsigned long) funcstack & 4) != 0;
41
42 if ((argc > NREG_ARGS) && (argc & 1) != 0)
43 misaligned = !misaligned;
44
45 if (misaligned)
46 funcstack -= 1;
47
48 va_start (vl, argc);
49
50 /* Reserve space for the on-stack arguments. */
51 if (argc > NREG_ARGS)
52 funcstack -= (argc - NREG_ARGS);
53
54 ucp->uc_mcontext.arm_sp = (unsigned long) funcstack;
55 ucp->uc_mcontext.arm_pc = (unsigned long) func;
56
57 /* Exit to startcontext() with the next context in R4 */
58 ucp->uc_mcontext.arm_r4 = (unsigned long) ucp->uc_link;
59 ucp->uc_mcontext.arm_lr = (unsigned long) __startcontext;
60
61 /* The first four arguments go into registers. */
62 regptr = &(ucp->uc_mcontext.arm_r0);
63
64 for (reg = 0; (reg < argc) && (reg < NREG_ARGS); reg++)
65 *regptr++ = va_arg (vl, unsigned long);
66
67 /* And the remainder on the stack. */
68 for (; reg < argc; reg++)
69 *funcstack++ = va_arg (vl, unsigned long);
70
71 va_end (vl);
72 }
73 weak_alias (__makecontext, makecontext)
74