1  /* Copyright (C) 2001-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       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  
       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  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <stdarg.h>
      19  #include <stdio.h>
      20  #include <stdlib.h>
      21  #include <ucontext.h>
      22  
      23  extern void __start_context (ucontext_t *ucp);
      24  
      25  void
      26  __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
      27  {
      28    extern void __makecontext_ret (void);
      29    unsigned long *sp, *topsp;
      30    va_list ap;
      31    int i;
      32  
      33    sp = (unsigned long *) ((long) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
      34    sp -= (argc > 6 ? argc : 6) + 32;
      35    sp = (unsigned long *) (((long) sp) & -16L);
      36    topsp = sp + (argc > 6 ? argc : 6) + 16;
      37  
      38    ucp->uc_mcontext.mc_gregs[MC_PC] = (long) func;
      39    ucp->uc_mcontext.mc_gregs[MC_NPC] = ((long) func) + 4;
      40    ucp->uc_mcontext.mc_gregs[MC_O6] = ((long) sp) - 0x7ff;
      41    ucp->uc_mcontext.mc_gregs[MC_O7] = ((long) __start_context) - 8;
      42    ucp->uc_mcontext.mc_fp = ((long) topsp) - 0x7ff;
      43    ucp->uc_mcontext.mc_i7 = 0;
      44    topsp[14] = 0;
      45    topsp[15] = 0;
      46    sp[8] = (long) ucp->uc_link;
      47    va_start (ap, argc);
      48    for (i = 0; i < argc; ++i)
      49      if (i < 6)
      50        ucp->uc_mcontext.mc_gregs[MC_O0 + i] = va_arg (ap, long);
      51      else
      52        sp[16 + i] = va_arg (ap, long);
      53    va_end (ap);
      54  }
      55  
      56  weak_alias (__makecontext, makecontext)