makecontext.c 2.4 KB

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