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

Packit 6c4009
/* Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <stdarg.h>
Packit 6c4009
#include <ucontext.h>
Packit 6c4009
Packit 6c4009
/* Number of arguments that go in registers.  */
Packit 6c4009
#define NREG_ARGS  4
Packit 6c4009
Packit 6c4009
/* Take a context previously prepared via getcontext() and set to
Packit 6c4009
   call func() with the given int only args.  */
Packit 6c4009
void
Packit 6c4009
__makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
Packit 6c4009
{
Packit 6c4009
  extern void __startcontext (void);
Packit 6c4009
  unsigned long *funcstack;
Packit 6c4009
  va_list vl;
Packit 6c4009
  unsigned long *regptr;
Packit 6c4009
  unsigned int reg;
Packit 6c4009
  int misaligned;
Packit 6c4009
Packit 6c4009
  /* Start at the top of stack.  */
Packit 6c4009
  funcstack = (unsigned long *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
Packit 6c4009
Packit 6c4009
  /* Ensure the stack stays eight byte aligned.  */
Packit 6c4009
  misaligned = ((unsigned long) funcstack & 4) != 0;
Packit 6c4009
Packit 6c4009
  if ((argc > NREG_ARGS) && (argc & 1) != 0)
Packit 6c4009
    misaligned = !misaligned;
Packit 6c4009
Packit 6c4009
  if (misaligned)
Packit 6c4009
    funcstack -= 1;
Packit 6c4009
Packit 6c4009
  va_start (vl, argc);
Packit 6c4009
Packit 6c4009
  /* Reserve space for the on-stack arguments.  */
Packit 6c4009
  if (argc > NREG_ARGS)
Packit 6c4009
    funcstack -= (argc - NREG_ARGS);
Packit 6c4009
Packit 6c4009
  ucp->uc_mcontext.arm_sp = (unsigned long) funcstack;
Packit 6c4009
  ucp->uc_mcontext.arm_pc = (unsigned long) func;
Packit 6c4009
Packit 6c4009
  /* Exit to startcontext() with the next context in R4 */
Packit 6c4009
  ucp->uc_mcontext.arm_r4 = (unsigned long) ucp->uc_link;
Packit 6c4009
  ucp->uc_mcontext.arm_lr = (unsigned long) __startcontext;
Packit 6c4009
Packit 6c4009
  /* The first four arguments go into registers.  */
Packit 6c4009
  regptr = &(ucp->uc_mcontext.arm_r0);
Packit 6c4009
Packit 6c4009
  for (reg = 0; (reg < argc) && (reg < NREG_ARGS); reg++)
Packit 6c4009
    *regptr++ = va_arg (vl, unsigned long);
Packit 6c4009
Packit 6c4009
  /* And the remainder on the stack.  */
Packit 6c4009
  for (; reg < argc; reg++)
Packit 6c4009
    *funcstack++ = va_arg (vl, unsigned long);
Packit 6c4009
Packit 6c4009
  va_end (vl);
Packit 6c4009
}
Packit 6c4009
weak_alias (__makecontext, makecontext)