makecontext.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Create new context. RISC-V version.
  2. Copyright (C) 2001-2018 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 <sys/asm.h>
  17. #include <sys/ucontext.h>
  18. #include <stdarg.h>
  19. #include <assert.h>
  20. void
  21. __makecontext (ucontext_t *ucp, void (*func) (void), int argc,
  22. long int a0, long int a1, long int a2, long int a3, long int a4,
  23. ...)
  24. {
  25. extern void __start_context (void) attribute_hidden;
  26. long int i, sp;
  27. _Static_assert (REG_NARGS == 8, "__makecontext assumes 8 argument registers");
  28. /* Set up the stack. */
  29. sp = ((long int) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) & ALMASK;
  30. /* Set up the register context.
  31. ra = s0 = 0, terminating the stack for backtracing purposes.
  32. s1 = the function we must call.
  33. s2 = the subsequent context to run. */
  34. ucp->uc_mcontext.__gregs[REG_RA] = 0;
  35. ucp->uc_mcontext.__gregs[REG_S0] = 0;
  36. ucp->uc_mcontext.__gregs[REG_S1] = (long int) func;
  37. ucp->uc_mcontext.__gregs[REG_S2] = (long int) ucp->uc_link;
  38. ucp->uc_mcontext.__gregs[REG_SP] = sp;
  39. ucp->uc_mcontext.__gregs[REG_PC] = (long int) &__start_context;
  40. /* Put args in a0-a7, then put any remaining args on the stack. */
  41. ucp->uc_mcontext.__gregs[REG_A0 + 0] = a0;
  42. ucp->uc_mcontext.__gregs[REG_A0 + 1] = a1;
  43. ucp->uc_mcontext.__gregs[REG_A0 + 2] = a2;
  44. ucp->uc_mcontext.__gregs[REG_A0 + 3] = a3;
  45. ucp->uc_mcontext.__gregs[REG_A0 + 4] = a4;
  46. if (unlikely(argc > 5))
  47. {
  48. va_list vl;
  49. va_start (vl, a4);
  50. long reg_args = argc < REG_NARGS ? argc : REG_NARGS;
  51. for (i = 5; i < reg_args; i++)
  52. ucp->uc_mcontext.__gregs[REG_A0 + i] = va_arg (vl, long);
  53. long int stack_args = argc - reg_args;
  54. if (stack_args > 0)
  55. {
  56. sp = (sp - stack_args * sizeof (long int)) & ALMASK;
  57. ucp->uc_mcontext.__gregs[REG_SP] = sp;
  58. for (i = 0; i < stack_args; i++)
  59. ((long int *) sp)[i] = va_arg (vl, long int);
  60. }
  61. va_end (vl);
  62. }
  63. }
  64. weak_alias (__makecontext, makecontext)