Blame sysdeps/unix/sysv/linux/ia64/makecontext.c

Packit 6c4009
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <ucontext.h>
Packit 6c4009
#include <sys/rse.h>
Packit 6c4009
#include <link.h>
Packit 6c4009
#include <dl-fptr.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define PUSH(val)				\
Packit 6c4009
do {						\
Packit 6c4009
  if (ia64_rse_is_rnat_slot (rbs))		\
Packit 6c4009
    *rbs++ = 0;					\
Packit 6c4009
  *rbs++ = (val);				\
Packit 6c4009
} while (0)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This implementation can handle an ARGC value of at most 8 and
Packit 6c4009
   values can be passed only in integer registers (r32-r39).  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
Packit 6c4009
{
Packit 6c4009
  mcontext_t *sc = &ucp->uc_mcontext;
Packit 6c4009
  extern void __start_context (ucontext_t *link, long gp, ...);
Packit 6c4009
  unsigned long stack_start, stack_end;
Packit 6c4009
  va_list ap;
Packit 6c4009
  unsigned long *rbs;
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  stack_start = (long) sc->sc_stack.ss_sp;
Packit 6c4009
  stack_end = (long) sc->sc_stack.ss_sp + sc->sc_stack.ss_size;
Packit 6c4009
Packit 6c4009
  stack_start = (stack_start + 7) & -8;
Packit 6c4009
  stack_end = stack_end & -16;
Packit 6c4009
Packit 6c4009
  if (argc > 8)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, _("\
Packit 6c4009
makecontext: does not know how to handle more than 8 arguments\n"));
Packit 6c4009
      exit (-1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* set the entry point and global pointer: */
Packit 6c4009
  sc->sc_br[0] = ELF_PTR_TO_FDESC (&__start_context)->ip;
Packit 6c4009
  sc->sc_br[1] = ELF_PTR_TO_FDESC (func)->ip;
Packit 6c4009
  sc->sc_gr[1] = ELF_PTR_TO_FDESC (func)->gp;
Packit 6c4009
Packit 6c4009
  /* set up the call frame: */
Packit 6c4009
  sc->sc_ar_pfs = ((sc->sc_ar_pfs & ~0x3fffffffffUL)
Packit 6c4009
		   | (argc + 2) | ((argc + 2) << 7));
Packit 6c4009
  rbs = (unsigned long *) stack_start;
Packit 6c4009
  PUSH((long) ucp->uc_link);
Packit 6c4009
  PUSH(ELF_PTR_TO_FDESC (&__start_context)->gp);
Packit 6c4009
  va_start (ap, argc);
Packit 6c4009
  for (i = 0; i < argc; ++i)
Packit 6c4009
    PUSH(va_arg (ap, long));
Packit 6c4009
  va_end (ap);
Packit 6c4009
Packit 6c4009
  /* set the memory and register stack pointers: */
Packit 6c4009
  sc->sc_ar_bsp = (long) rbs;
Packit 6c4009
  sc->sc_gr[12] = stack_end - 16;
Packit 6c4009
Packit 6c4009
  /* clear the NaT bits for r1 and r12: */
Packit 6c4009
  sc->sc_nat &= ~((1 << 1) | (1 << 12));
Packit 6c4009
  sc->sc_ar_rnat = 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__makecontext, makecontext)