|
Packit |
6c4009 |
/* Create new context.
|
|
Packit |
6c4009 |
Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
|
|
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 <stdarg.h>
|
|
Packit |
6c4009 |
#include <stdint.h>
|
|
Packit |
6c4009 |
#include <ucontext.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* makecontext sets up a stack and the registers for the
|
|
Packit |
6c4009 |
user context. The stack looks like this:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
+-----------------------+
|
|
Packit |
6c4009 |
| padding as required |
|
|
Packit |
6c4009 |
+-----------------------+
|
|
Packit |
6c4009 |
sp -> | parameter 7-n |
|
|
Packit |
6c4009 |
+-----------------------+
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The registers are set up like this:
|
|
Packit |
6c4009 |
%x0 .. %x7: parameter 1 to 8
|
|
Packit |
6c4009 |
%x19 : uc_link
|
|
Packit |
6c4009 |
%sp : stack pointer.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
__makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
extern void __startcontext (void);
|
|
Packit |
6c4009 |
uint64_t *sp;
|
|
Packit |
6c4009 |
va_list ap;
|
|
Packit |
6c4009 |
int i;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
sp = (uint64_t *)
|
|
Packit |
6c4009 |
((uintptr_t) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Allocate stack arguments. */
|
|
Packit |
6c4009 |
sp -= argc < 8 ? 0 : argc - 8;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Keep the stack aligned. */
|
|
Packit |
6c4009 |
sp = (uint64_t *) (((uintptr_t) sp) & -16L);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ucp->uc_mcontext.regs[19] = (uintptr_t) ucp->uc_link;
|
|
Packit |
6c4009 |
ucp->uc_mcontext.sp = (uintptr_t) sp;
|
|
Packit |
6c4009 |
ucp->uc_mcontext.pc = (uintptr_t) func;
|
|
Packit |
6c4009 |
ucp->uc_mcontext.regs[29] = (uintptr_t) 0;
|
|
Packit |
6c4009 |
ucp->uc_mcontext.regs[30] = (uintptr_t) &__startcontext;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
va_start (ap, argc);
|
|
Packit |
6c4009 |
for (i = 0; i < argc; ++i)
|
|
Packit |
6c4009 |
if (i < 8)
|
|
Packit |
6c4009 |
ucp->uc_mcontext.regs[i] = va_arg (ap, uint64_t);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
sp[i - 8] = va_arg (ap, uint64_t);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
va_end (ap);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
weak_alias (__makecontext, makecontext)
|