makecontext.c 2.3 KB

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