makecontext.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Create new context.
  2. Copyright (C) 2008-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by David S. Miller <davem@davemloft.net>, 2008.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. 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.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <sysdep.h>
  17. #include <stdarg.h>
  18. #include <stdint.h>
  19. #include <ucontext.h>
  20. /* Sets up the outgoing arguments and the program counter for a user
  21. context for the requested function call.
  22. Returning to the correct parent context is pretty simple on
  23. Sparc. We only need to link up the register windows correctly.
  24. Since global registers are clobbered by calls, we need not be
  25. concerned about those, and thus is all could be worked out without
  26. using a trampoline.
  27. Except that we must deal with the signal mask, thus a trampoline
  28. is unavoidable. 32-bit stackframe layout:
  29. +-----------------------------------------+
  30. | 7th and further parameters |
  31. +-----------------------------------------+
  32. | backup storage for initial 6 parameters |
  33. +-----------------------------------------+
  34. | struct return pointer |
  35. +-----------------------------------------+
  36. | 8 incoming registers |
  37. +-----------------------------------------+
  38. | 8 local registers |
  39. %sp --> +-----------------------------------------+
  40. */
  41. void
  42. __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
  43. {
  44. extern void __start_context (void);
  45. unsigned long int *sp;
  46. va_list ap;
  47. int i;
  48. sp = (unsigned long int *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
  49. sp -= 16 + 7 + argc;
  50. sp = (unsigned long int *) (((uintptr_t) sp) & ~(8 - 1));
  51. for (i = 0; i < 8; i++)
  52. sp[i + 8] = ucp->uc_mcontext.gregs[REG_O0 + i];
  53. /* The struct return pointer is essentially unused, so we can
  54. place the link there. */
  55. sp[16] = (unsigned long int) ucp->uc_link;
  56. va_start (ap, argc);
  57. /* Fill in outgoing arguments, including those which will
  58. end up being passed on the stack. */
  59. for (i = 0; i < argc; i++)
  60. {
  61. unsigned long int arg = va_arg (ap, unsigned long int);
  62. if (i < 6)
  63. ucp->uc_mcontext.gregs[REG_O0 + i] = arg;
  64. else
  65. sp[i + 23 - 6] = arg;
  66. }
  67. va_end (ap);
  68. ucp->uc_mcontext.gregs[REG_O6] = (unsigned long int) sp;
  69. ucp->uc_mcontext.gregs[REG_O7] = ((unsigned long int) __start_context) - 8;
  70. ucp->uc_mcontext.gregs[REG_PC] = (unsigned long int) func;
  71. ucp->uc_mcontext.gregs[REG_nPC] = ucp->uc_mcontext.gregs[REG_PC] + 4;
  72. }
  73. weak_alias (__makecontext, makecontext)