]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/hppa/makecontext.c
MIPS: replace bits/socket.h with bits/socket_type.h.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / hppa / makecontext.c
CommitLineData
5bccf609 1/* Create new context.
40215fde 2 Copyright (C) 2008, 2010 Free Software Foundation, Inc.
5bccf609
CD
3 This file is part of the GNU C Library.
4 Contributed by Helge Deller <deller@gmx.de>, 2008.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21#include <libintl.h>
22#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sysdep.h>
26#include <ucontext.h>
27
40215fde 28/* POSIX only supports integer arguments. */
131fafa7
CD
29
30/* Stack must be 64-byte aligned at all times. */
40215fde 31#define STACK_ALIGN 64
131fafa7
CD
32/* Size of frame marker in unsigned long words. */
33#define FRAME_SIZE_UL 8
34/* Size of frame marker in bytes. */
35#define FRAME_SIZE_BYTES (8 * sizeof(unsigned long))
36/* Size of X arguments in bytes. */
37#define ARGS(x) (x * sizeof(unsigned long))
5bccf609
CD
38
39void
40__makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
41{
131fafa7 42 unsigned long *sp, *osp;
5bccf609
CD
43 va_list ap;
44 int i;
45
131fafa7
CD
46 /* Create a 64-byte aligned frame to store args. Use ss_sp if
47 it is available, otherwise be robust and use the currently
48 saved stack pointer. */
49 if (ucp->uc_stack.ss_sp && ucp->uc_stack.ss_size)
50 osp = (unsigned long *)ucp->uc_stack.ss_sp;
51 else
52 osp = (unsigned long *)ucp->uc_mcontext.sc_gr[30];
53
54 sp = (unsigned long *)((((unsigned long) osp)
55 + FRAME_SIZE_BYTES + ARGS(argc) + STACK_ALIGN)
56 & ~(STACK_ALIGN - 1));
57
58 /* Use new frame. */
59 ucp->uc_mcontext.sc_gr[30] = ((unsigned long) sp);
60
61 /* Finish frame setup. */
62 if (ucp->uc_link)
63 {
64 /* Returning to the next context and next frame. */
65 sp[-4/sizeof(unsigned long)] = ucp->uc_link->uc_mcontext.sc_gr[30];
66 sp[-20/sizeof(unsigned long)] = ucp->uc_link->uc_mcontext.sc_gr[2];
67 }
68 else
69 {
70 /* This is the main context. No frame marker, and no return address. */
71 sp[-4/sizeof(unsigned long)] = 0x0;
72 sp[-20/sizeof(unsigned long)] = 0x0;
73 }
5bccf609
CD
74
75 /* Store address to jump to. */
76 ucp->uc_mcontext.sc_gr[2] = (unsigned long) func;
77
131fafa7 78 /* Process arguments. */
5bccf609 79 va_start (ap, argc);
5bccf609 80 for (i = 0; i < argc; ++i)
40215fde
CD
81 {
82 if (i < 4)
83 {
84 ucp->uc_mcontext.sc_gr[26-i] = va_arg (ap, int);
85 continue;
5bccf609 86 }
5bccf609 87
40215fde
CD
88 if ((i < 8) && (sizeof(unsigned long) == 8))
89 {
90 /* 64bit: r19-r22 are arg7-arg4. */
91 ucp->uc_mcontext.sc_gr[22+4-i] = va_arg (ap, int);
92 continue;
93 }
94
95 /* All other arguments go on the stack. */
131fafa7 96 sp[-1 * (FRAME_SIZE_UL + 1 + i)] = va_arg (ap, int);
40215fde
CD
97 }
98 va_end (ap);
5bccf609 99}
5bccf609 100weak_alias(__makecontext, makecontext)
131fafa7 101