|
Packit |
6c4009 |
/* Create new context.
|
|
Packit |
6c4009 |
Copyright (C) 2008-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Helge Deller <deller@gmx.de>, 2008.
|
|
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 <sysdep.h>
|
|
Packit |
6c4009 |
#include <ucontext.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* POSIX only supports integer arguments. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Stack must be 64-byte aligned at all times. */
|
|
Packit |
6c4009 |
#define STACK_ALIGN 64
|
|
Packit |
6c4009 |
/* Size of frame marker in unsigned long words. */
|
|
Packit |
6c4009 |
#define FRAME_SIZE_UL 8
|
|
Packit |
6c4009 |
/* Size of frame marker in bytes. */
|
|
Packit |
6c4009 |
#define FRAME_SIZE_BYTES (8 * sizeof(unsigned long))
|
|
Packit |
6c4009 |
/* Size of X arguments in bytes. */
|
|
Packit |
6c4009 |
#define ARGS(x) (x * sizeof(unsigned long))
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
__makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
unsigned long *sp, *osp;
|
|
Packit |
6c4009 |
va_list ap;
|
|
Packit |
6c4009 |
int i;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Create a 64-byte aligned frame to store args. Use ss_sp if
|
|
Packit |
6c4009 |
it is available, otherwise be robust and use the currently
|
|
Packit |
6c4009 |
saved stack pointer. */
|
|
Packit |
6c4009 |
if (ucp->uc_stack.ss_sp && ucp->uc_stack.ss_size)
|
|
Packit |
6c4009 |
osp = (unsigned long *)ucp->uc_stack.ss_sp;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
osp = (unsigned long *)ucp->uc_mcontext.sc_gr[30];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
sp = (unsigned long *)((((unsigned long) osp)
|
|
Packit |
6c4009 |
+ FRAME_SIZE_BYTES + ARGS(argc) + STACK_ALIGN)
|
|
Packit |
6c4009 |
& ~(STACK_ALIGN - 1));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Use new frame. */
|
|
Packit |
6c4009 |
ucp->uc_mcontext.sc_gr[30] = ((unsigned long) sp);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Finish frame setup. */
|
|
Packit |
6c4009 |
if (ucp->uc_link)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Returning to the next context and next frame. */
|
|
Packit |
6c4009 |
sp[-4/sizeof(unsigned long)] = ucp->uc_link->uc_mcontext.sc_gr[30];
|
|
Packit |
6c4009 |
sp[-20/sizeof(unsigned long)] = ucp->uc_link->uc_mcontext.sc_gr[2];
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* This is the main context. No frame marker, and no return address. */
|
|
Packit |
6c4009 |
sp[-4/sizeof(unsigned long)] = 0x0;
|
|
Packit |
6c4009 |
sp[-20/sizeof(unsigned long)] = 0x0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Store address to jump to. */
|
|
Packit |
6c4009 |
ucp->uc_mcontext.sc_gr[2] = (unsigned long) func;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Process arguments. */
|
|
Packit |
6c4009 |
va_start (ap, argc);
|
|
Packit |
6c4009 |
for (i = 0; i < argc; ++i)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (i < 4)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ucp->uc_mcontext.sc_gr[26-i] = va_arg (ap, int);
|
|
Packit |
6c4009 |
continue;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if ((i < 8) && (sizeof(unsigned long) == 8))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* 64bit: r19-r22 are arg7-arg4. */
|
|
Packit |
6c4009 |
ucp->uc_mcontext.sc_gr[22+4-i] = va_arg (ap, int);
|
|
Packit |
6c4009 |
continue;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* All other arguments go on the stack. */
|
|
Packit |
6c4009 |
sp[-1 * (FRAME_SIZE_UL + 1 + i)] = va_arg (ap, int);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
va_end (ap);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
weak_alias(__makecontext, makecontext)
|