makecontext.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Copyright (C) 2001-2017 Free Software Foundation, Inc.
  2. Contributed by Jakub Jelinek <jakub@redhat.com>.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <ucontext.h>
  18. extern void __start_context (struct ucontext *ucp);
  19. void
  20. __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
  21. {
  22. extern void __makecontext_ret (void);
  23. unsigned long *sp, *topsp;
  24. va_list ap;
  25. int i;
  26. sp = (unsigned long *) ((long) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
  27. sp -= (argc > 6 ? argc : 6) + 32;
  28. sp = (unsigned long *) (((long) sp) & -16L);
  29. topsp = sp + (argc > 6 ? argc : 6) + 16;
  30. ucp->uc_mcontext.mc_gregs[MC_PC] = (long) func;
  31. ucp->uc_mcontext.mc_gregs[MC_NPC] = ((long) func) + 4;
  32. ucp->uc_mcontext.mc_gregs[MC_O6] = ((long) sp) - 0x7ff;
  33. ucp->uc_mcontext.mc_gregs[MC_O7] = ((long) __start_context) - 8;
  34. ucp->uc_mcontext.mc_fp = ((long) topsp) - 0x7ff;
  35. ucp->uc_mcontext.mc_i7 = 0;
  36. topsp[14] = 0;
  37. topsp[15] = 0;
  38. sp[8] = (long) ucp->uc_link;
  39. va_start (ap, argc);
  40. for (i = 0; i < argc; ++i)
  41. if (i < 6)
  42. ucp->uc_mcontext.mc_gregs[MC_O0 + i] = va_arg (ap, long);
  43. else
  44. sp[16 + i] = va_arg (ap, long);
  45. va_end (ap);
  46. }
  47. weak_alias (__makecontext, makecontext)