makecontext.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (C) 2016-2017 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* Copyright (C) 2011-2013 Free Software Foundation, Inc.
  6. Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <ucontext.h>
  23. /* makecontext sets up a stack and the registers for the
  24. user context. The stack looks like this:
  25. +-----------------------+
  26. | padding as required |
  27. +-----------------------+
  28. sp -> | parameter 7-n |
  29. +-----------------------+
  30. The registers are set up like this:
  31. $r0 .. $r5: parameter 1 to 6
  32. $r6 : uc_link
  33. $sp : stack pointer.
  34. */
  35. void
  36. __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
  37. {
  38. extern void __startcontext (void);
  39. unsigned long int *sp;
  40. unsigned long *regptr;
  41. va_list ap;
  42. int i;
  43. sp = (unsigned long int *)
  44. ((uintptr_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
  45. /* Allocate stack arguments. */
  46. sp -= argc < 6 ? 0 : argc - 6;
  47. /* Keep the stack aligned. */
  48. sp = (unsigned long int *) (((uintptr_t) sp) & -8L);
  49. ucp->uc_mcontext.nds32_r6 = (uintptr_t) ucp->uc_link;
  50. ucp->uc_mcontext.nds32_sp = (uintptr_t) sp;
  51. ucp->uc_mcontext.nds32_ipc = (uintptr_t) func;
  52. ucp->uc_mcontext.nds32_lp = (uintptr_t) &__startcontext;
  53. va_start (ap, argc);
  54. regptr = &ucp->uc_mcontext.nds32_r0;
  55. for (i = 0; i < argc; ++i)
  56. if (i < 6)
  57. *regptr++ = va_arg (ap, unsigned long int);
  58. else
  59. sp[i - 6] = va_arg (ap, unsigned long int);
  60. va_end (ap);
  61. }
  62. weak_alias (__makecontext, makecontext)