makecontext.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Create new context.
  2. Copyright (C) 2002-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 <sysdep.h>
  16. #include <stdarg.h>
  17. #include <stdint.h>
  18. #include <ucontext.h>
  19. /* makecontext sets up a stack and the registers for the
  20. user context. The stack looks like this:
  21. +-----------------------+
  22. | padding as required |
  23. +-----------------------+
  24. sp -> | parameter 7-n |
  25. +-----------------------+
  26. The registers are set up like this:
  27. %x0 .. %x7: parameter 1 to 8
  28. %x19 : uc_link
  29. %sp : stack pointer.
  30. */
  31. void
  32. __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
  33. {
  34. extern void __startcontext (void);
  35. uint64_t *sp;
  36. va_list ap;
  37. int i;
  38. sp = (uint64_t *)
  39. ((uintptr_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
  40. /* Allocate stack arguments. */
  41. sp -= argc < 8 ? 0 : argc - 8;
  42. /* Keep the stack aligned. */
  43. sp = (uint64_t *) (((uintptr_t) sp) & -16L);
  44. ucp->uc_mcontext.regs[19] = (uintptr_t) ucp->uc_link;
  45. ucp->uc_mcontext.sp = (uintptr_t) sp;
  46. ucp->uc_mcontext.pc = (uintptr_t) func;
  47. ucp->uc_mcontext.regs[29] = (uintptr_t) 0;
  48. ucp->uc_mcontext.regs[30] = (uintptr_t) &__startcontext;
  49. va_start (ap, argc);
  50. for (i = 0; i < argc; ++i)
  51. if (i < 8)
  52. ucp->uc_mcontext.regs[i] = va_arg (ap, uint64_t);
  53. else
  54. sp[i - 8] = va_arg (ap, uint64_t);
  55. va_end (ap);
  56. }
  57. weak_alias (__makecontext, makecontext)