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 <libintl.h>
      19  #include <stdarg.h>
      20  #include <stdio.h>
      21  #include <stdlib.h>
      22  #include <ucontext.h>
      23  #include <sys/rse.h>
      24  #include <link.h>
      25  #include <dl-fptr.h>
      26  
      27  
      28  #define PUSH(val)				\
      29  do {						\
      30    if (ia64_rse_is_rnat_slot (rbs))		\
      31      *rbs++ = 0;					\
      32    *rbs++ = (val);				\
      33  } while (0)
      34  
      35  
      36  /* This implementation can handle an ARGC value of at most 8 and
      37     values can be passed only in integer registers (r32-r39).  */
      38  
      39  void
      40  __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
      41  {
      42    mcontext_t *sc = &ucp->uc_mcontext;
      43    extern void __start_context (ucontext_t *link, long gp, ...);
      44    unsigned long stack_start, stack_end;
      45    va_list ap;
      46    unsigned long *rbs;
      47    int i;
      48  
      49    stack_start = (long) sc->sc_stack.ss_sp;
      50    stack_end = (long) sc->sc_stack.ss_sp + sc->sc_stack.ss_size;
      51  
      52    stack_start = (stack_start + 7) & -8;
      53    stack_end = stack_end & -16;
      54  
      55    if (argc > 8)
      56      {
      57        fprintf (stderr, _("\
      58  makecontext: does not know how to handle more than 8 arguments\n"));
      59        exit (-1);
      60      }
      61  
      62    /* set the entry point and global pointer: */
      63    sc->sc_br[0] = ELF_PTR_TO_FDESC (&__start_context)->ip;
      64    sc->sc_br[1] = ELF_PTR_TO_FDESC (func)->ip;
      65    sc->sc_gr[1] = ELF_PTR_TO_FDESC (func)->gp;
      66  
      67    /* set up the call frame: */
      68    sc->sc_ar_pfs = ((sc->sc_ar_pfs & ~0x3fffffffffUL)
      69  		   | (argc + 2) | ((argc + 2) << 7));
      70    rbs = (unsigned long *) stack_start;
      71    PUSH((long) ucp->uc_link);
      72    PUSH(ELF_PTR_TO_FDESC (&__start_context)->gp);
      73    va_start (ap, argc);
      74    for (i = 0; i < argc; ++i)
      75      PUSH(va_arg (ap, long));
      76    va_end (ap);
      77  
      78    /* set the memory and register stack pointers: */
      79    sc->sc_ar_bsp = (long) rbs;
      80    sc->sc_gr[12] = stack_end - 16;
      81  
      82    /* clear the NaT bits for r1 and r12: */
      83    sc->sc_nat &= ~((1 << 1) | (1 << 12));
      84    sc->sc_ar_rnat = 0;
      85  }
      86  
      87  weak_alias (__makecontext, makecontext)