Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / unix / sysv / linux / arm / makecontext.c
blob73fbe4a329fbcb17da2e01853e722a895dc54bb2
1 /* Copyright (C) 2012-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
18 #include <stdarg.h>
19 #include <ucontext.h>
21 /* Number of arguments that go in registers. */
22 #define NREG_ARGS 4
24 /* Take a context previously prepared via getcontext() and set to
25 call func() with the given int only args. */
26 void
27 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
29 extern void __startcontext (void);
30 unsigned long *funcstack;
31 va_list vl;
32 unsigned long *regptr;
33 unsigned int reg;
34 int misaligned;
36 /* Start at the top of stack. */
37 funcstack = (unsigned long *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
39 /* Ensure the stack stays eight byte aligned. */
40 misaligned = ((unsigned long) funcstack & 4) != 0;
42 if ((argc > NREG_ARGS) && (argc & 1) != 0)
43 misaligned = !misaligned;
45 if (misaligned)
46 funcstack -= 1;
48 va_start (vl, argc);
50 /* Reserve space for the on-stack arguments. */
51 if (argc > NREG_ARGS)
52 funcstack -= (argc - NREG_ARGS);
54 ucp->uc_mcontext.arm_sp = (unsigned long) funcstack;
55 ucp->uc_mcontext.arm_pc = (unsigned long) func;
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;
61 /* The first four arguments go into registers. */
62 regptr = &(ucp->uc_mcontext.arm_r0);
64 for (reg = 0; (reg < argc) && (reg < NREG_ARGS); reg++)
65 *regptr++ = va_arg (vl, unsigned long);
67 /* And the remainder on the stack. */
68 for (; reg < argc; reg++)
69 *funcstack++ = va_arg (vl, unsigned long);
71 va_end (vl);
73 weak_alias (__makecontext, makecontext)