makecontext.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Create new context.
  2. Copyright (C) 2002, 2004, 2005, 2008 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Andreas Jaeger <aj@suse.de>, 2002.
  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. #include "ucontext_i.h"
  21. /* This implementation can handle any ARGC value but only
  22. normal integer parameters.
  23. makecontext sets up a stack and the registers for the
  24. user context. The stack looks like this:
  25. +-----------------------+
  26. | next context |
  27. +-----------------------+
  28. | parameter 7-n |
  29. +-----------------------+
  30. | trampoline address |
  31. %rsp -> +-----------------------+
  32. The registers are set up like this:
  33. %rdi,%rsi,%rdx,%rcx,%r8,%r9: parameter 1 to 6
  34. %rbx : address of next context
  35. %rsp : stack pointer.
  36. */
  37. /* XXX: This implementation currently only handles integer arguments.
  38. To handle long int and pointer arguments the va_arg arguments needs
  39. to be changed to long and also the stdlib/tst-setcontext.c file needs
  40. to be changed to pass long arguments to makecontext. */
  41. void
  42. __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
  43. {
  44. extern void __start_context (void);
  45. greg_t *sp;
  46. unsigned int idx_uc_link;
  47. va_list ap;
  48. int i;
  49. /* Generate room on stack for parameter if needed and uc_link. */
  50. sp = (greg_t *) ((uintptr_t) ucp->uc_stack.ss_sp
  51. + ucp->uc_stack.ss_size);
  52. sp -= (argc > 6 ? argc - 6 : 0) + 1;
  53. /* Align stack and make space for trampoline address. */
  54. sp = (greg_t *) ((((uintptr_t) sp) & -16L) - 8);
  55. idx_uc_link = (argc > 6 ? argc - 6 : 0) + 1;
  56. /* Setup context ucp. */
  57. /* Address to jump to. */
  58. ucp->uc_mcontext.gregs[REG_RIP] = (uintptr_t) func;
  59. /* Setup rbx.*/
  60. ucp->uc_mcontext.gregs[REG_RBX] = (uintptr_t) &sp[idx_uc_link];
  61. ucp->uc_mcontext.gregs[REG_RSP] = (uintptr_t) sp;
  62. /* Setup stack. */
  63. sp[0] = (uintptr_t) &__start_context;
  64. sp[idx_uc_link] = (uintptr_t) ucp->uc_link;
  65. va_start (ap, argc);
  66. /* Handle arguments.
  67. The standard says the parameters must all be int values. This is
  68. an historic accident and would be done differently today. For
  69. x86-64 all integer values are passed as 64-bit values and
  70. therefore extending the API to copy 64-bit values instead of
  71. 32-bit ints makes sense. It does not break existing
  72. functionality and it does not violate the standard which says
  73. that passing non-int values means undefined behavior. */
  74. for (i = 0; i < argc; ++i)
  75. switch (i)
  76. {
  77. case 0:
  78. ucp->uc_mcontext.gregs[REG_RDI] = va_arg (ap, greg_t);
  79. break;
  80. case 1:
  81. ucp->uc_mcontext.gregs[REG_RSI] = va_arg (ap, greg_t);
  82. break;
  83. case 2:
  84. ucp->uc_mcontext.gregs[REG_RDX] = va_arg (ap, greg_t);
  85. break;
  86. case 3:
  87. ucp->uc_mcontext.gregs[REG_RCX] = va_arg (ap, greg_t);
  88. break;
  89. case 4:
  90. ucp->uc_mcontext.gregs[REG_R8] = va_arg (ap, greg_t);
  91. break;
  92. case 5:
  93. ucp->uc_mcontext.gregs[REG_R9] = va_arg (ap, greg_t);
  94. break;
  95. default:
  96. /* Put value on stack. */
  97. sp[i - 5] = va_arg (ap, greg_t);
  98. break;
  99. }
  100. va_end (ap);
  101. }
  102. weak_alias (__makecontext, makecontext)