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

Packit 6c4009
/* Create new context.  RISC-V version.
Packit 6c4009
   Copyright (C) 2001-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 <sysdep.h>
Packit 6c4009
#include <sys/asm.h>
Packit 6c4009
#include <sys/ucontext.h>
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__makecontext (ucontext_t *ucp, void (*func) (void), int argc,
Packit 6c4009
	       long int a0, long int a1, long int a2, long int a3, long int a4,
Packit 6c4009
	       ...)
Packit 6c4009
{
Packit 6c4009
  extern void __start_context (void) attribute_hidden;
Packit 6c4009
  long int i, sp;
Packit 6c4009
Packit 6c4009
  _Static_assert (REG_NARGS == 8, "__makecontext assumes 8 argument registers");
Packit 6c4009
Packit 6c4009
  /* Set up the stack.  */
Packit 6c4009
  sp = ((long int) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) & ALMASK;
Packit 6c4009
Packit 6c4009
  /* Set up the register context.
Packit 6c4009
     ra = s0 = 0, terminating the stack for backtracing purposes.
Packit 6c4009
     s1 = the function we must call.
Packit 6c4009
     s2 = the subsequent context to run.  */
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_RA] = 0;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_S0] = 0;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_S1] = (long int) func;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_S2] = (long int) ucp->uc_link;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_SP] = sp;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_PC] = (long int) &__start_context;
Packit 6c4009
Packit 6c4009
  /* Put args in a0-a7, then put any remaining args on the stack.  */
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_A0 + 0] = a0;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_A0 + 1] = a1;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_A0 + 2] = a2;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_A0 + 3] = a3;
Packit 6c4009
  ucp->uc_mcontext.__gregs[REG_A0 + 4] = a4;
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (argc > 5))
Packit 6c4009
    {
Packit 6c4009
      va_list vl;
Packit 6c4009
      va_start (vl, a4);
Packit 6c4009
Packit 6c4009
      long reg_args = argc < REG_NARGS ? argc : REG_NARGS;
Packit 6c4009
      for (i = 5; i < reg_args; i++)
Packit 6c4009
        ucp->uc_mcontext.__gregs[REG_A0 + i] = va_arg (vl, long);
Packit 6c4009
Packit 6c4009
      long int stack_args = argc - reg_args;
Packit 6c4009
      if (stack_args > 0)
Packit 6c4009
	{
Packit 6c4009
	  sp = (sp - stack_args * sizeof (long int)) & ALMASK;
Packit 6c4009
	  ucp->uc_mcontext.__gregs[REG_SP] = sp;
Packit 6c4009
	  for (i = 0; i < stack_args; i++)
Packit 6c4009
	    ((long int *) sp)[i] = va_arg (vl, long int);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      va_end (vl);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__makecontext, makecontext)