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

Packit Service 82fcde
/* Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <stdarg.h>
Packit Service 82fcde
#include <ucontext.h>
Packit Service 82fcde
Packit Service 82fcde
/* Number of arguments that go in registers.  */
Packit Service 82fcde
#define NREG_ARGS  4
Packit Service 82fcde
Packit Service 82fcde
/* Take a context previously prepared via getcontext() and set to
Packit Service 82fcde
   call func() with the given int only args.  */
Packit Service 82fcde
void
Packit Service 82fcde
__makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
Packit Service 82fcde
{
Packit Service 82fcde
  extern void __startcontext (void);
Packit Service 82fcde
  unsigned long *funcstack;
Packit Service 82fcde
  va_list vl;
Packit Service 82fcde
  unsigned long *regptr;
Packit Service 82fcde
  unsigned int reg;
Packit Service 82fcde
  int misaligned;
Packit Service 82fcde
Packit Service 82fcde
  /* Start at the top of stack.  */
Packit Service 82fcde
  funcstack = (unsigned long *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
Packit Service 82fcde
Packit Service 82fcde
  /* Ensure the stack stays eight byte aligned.  */
Packit Service 82fcde
  misaligned = ((unsigned long) funcstack & 4) != 0;
Packit Service 82fcde
Packit Service 82fcde
  if ((argc > NREG_ARGS) && (argc & 1) != 0)
Packit Service 82fcde
    misaligned = !misaligned;
Packit Service 82fcde
Packit Service 82fcde
  if (misaligned)
Packit Service 82fcde
    funcstack -= 1;
Packit Service 82fcde
Packit Service 82fcde
  va_start (vl, argc);
Packit Service 82fcde
Packit Service 82fcde
  /* Reserve space for the on-stack arguments.  */
Packit Service 82fcde
  if (argc > NREG_ARGS)
Packit Service 82fcde
    funcstack -= (argc - NREG_ARGS);
Packit Service 82fcde
Packit Service 82fcde
  ucp->uc_mcontext.arm_sp = (unsigned long) funcstack;
Packit Service 82fcde
  ucp->uc_mcontext.arm_pc = (unsigned long) func;
Packit Service 82fcde
Packit Service 82fcde
  /* Exit to startcontext() with the next context in R4 */
Packit Service 82fcde
  ucp->uc_mcontext.arm_r4 = (unsigned long) ucp->uc_link;
Packit Service 82fcde
  ucp->uc_mcontext.arm_lr = (unsigned long) __startcontext;
Packit Service 82fcde
Packit Service 82fcde
  /* The first four arguments go into registers.  */
Packit Service 82fcde
  regptr = &(ucp->uc_mcontext.arm_r0);
Packit Service 82fcde
Packit Service 82fcde
  for (reg = 0; (reg < argc) && (reg < NREG_ARGS); reg++)
Packit Service 82fcde
    *regptr++ = va_arg (vl, unsigned long);
Packit Service 82fcde
Packit Service 82fcde
  /* And the remainder on the stack.  */
Packit Service 82fcde
  for (; reg < argc; reg++)
Packit Service 82fcde
    *funcstack++ = va_arg (vl, unsigned long);
Packit Service 82fcde
Packit Service 82fcde
  va_end (vl);
Packit Service 82fcde
}
Packit Service 82fcde
weak_alias (__makecontext, makecontext)