makecontext.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive for
  4. * more details.
  5. *
  6. * Copyright (C) 2025 Kalray Inc.
  7. * Author(s): Julian Vetter <jvetter@kalrayinc.com>
  8. */
  9. #include <stdarg.h>
  10. #include <ucontext.h>
  11. /* Number of arguments that go in registers. */
  12. #define NREG_ARGS 12
  13. /* Take a context previously prepared via getcontext() and set to
  14. call func() with the given int only args. */
  15. void
  16. __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
  17. {
  18. extern void __startcontext (void);
  19. unsigned long *funcstack;
  20. va_list vl;
  21. unsigned long *regptr;
  22. unsigned int reg;
  23. /* Start at the top of stack. */
  24. funcstack = (unsigned long *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
  25. funcstack -= argc < NREG_ARGS ? 0 : argc - NREG_ARGS;
  26. funcstack = (unsigned long *) (((uintptr_t) funcstack & -32L));
  27. ucp->uc_mcontext.sc_regs.r12 = (unsigned long) funcstack;
  28. /* Use $r20 and $r21 to pass some infos to __startcontext */
  29. ucp->uc_mcontext.sc_regs.r20 = (unsigned long) ucp->uc_link;
  30. ucp->uc_mcontext.sc_regs.r21 = (unsigned long) func;
  31. ucp->uc_mcontext.sc_regs.ra = (unsigned long) __startcontext;
  32. va_start (vl, argc);
  33. /* The first twelve arguments go into registers. */
  34. regptr = &(ucp->uc_mcontext.sc_regs.r0);
  35. for (reg = 0; (reg < argc) && (reg < NREG_ARGS); reg++)
  36. *regptr++ = va_arg (vl, unsigned long);
  37. /* And the remainder on the stack. */
  38. for (; reg < argc; reg++)
  39. *funcstack++ = va_arg (vl, unsigned long);
  40. va_end (vl);
  41. }
  42. weak_alias (__makecontext, makecontext)