]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/hppa/makecontext.c
5b6f5db1011e9e591639767611423bd7768031d1
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / hppa / makecontext.c
1 /* Create new context.
2 Copyright (C) 2008-2020 Free Software Foundation, Inc.
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, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include <libintl.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sysdep.h>
25 #include <ucontext.h>
26
27 /* POSIX only supports integer arguments. */
28
29 /* Stack must be 64-byte aligned at all times. */
30 #define STACK_ALIGN 64
31 /* Size of frame marker in unsigned long words. */
32 #define FRAME_SIZE_UL 8
33 /* Size of frame marker in bytes. */
34 #define FRAME_SIZE_BYTES (8 * sizeof (unsigned long))
35 /* Size of X arguments in bytes. */
36 #define ARGS(x) (x * sizeof (unsigned long))
37
38 void
39 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
40 {
41 unsigned long *sp, *osp;
42 va_list ap;
43 int i;
44
45 /* Create a 64-byte aligned frame to store args. Use ss_sp if
46 it is available, otherwise be robust and use the currently
47 saved stack pointer. */
48 if (ucp->uc_stack.ss_sp && ucp->uc_stack.ss_size)
49 osp = (unsigned long *)ucp->uc_stack.ss_sp;
50 else
51 osp = (unsigned long *)ucp->uc_mcontext.sc_gr[30];
52
53 sp = (unsigned long *)((((unsigned long) osp)
54 + FRAME_SIZE_BYTES + ARGS(argc) + STACK_ALIGN)
55 & ~(STACK_ALIGN - 1));
56
57 /* Use new frame. */
58 ucp->uc_mcontext.sc_gr[30] = ((unsigned long) sp);
59
60 /* Finish frame setup. */
61 if (ucp->uc_link)
62 {
63 /* Returning to the next context and next frame. */
64 sp[-4 / sizeof (unsigned long)] = ucp->uc_link->uc_mcontext.sc_gr[30];
65 sp[-20 / sizeof (unsigned long)] = ucp->uc_link->uc_mcontext.sc_gr[2];
66 }
67 else
68 {
69 /* This is the main context. No frame marker, and no return address. */
70 sp[-4 / sizeof (unsigned long)] = 0x0;
71 sp[-20 / sizeof (unsigned long)] = 0x0;
72 }
73
74 /* Store address to jump to. */
75 ucp->uc_mcontext.sc_gr[2] = (unsigned long) func;
76
77 /* Process arguments. */
78 va_start (ap, argc);
79 for (i = 0; i < argc; ++i)
80 {
81 if (i < 4)
82 {
83 ucp->uc_mcontext.sc_gr[26-i] = va_arg (ap, int);
84 continue;
85 }
86
87 if ((i < 8) && (sizeof (unsigned long) == 8))
88 {
89 /* 64bit: r19-r22 are arg7-arg4. */
90 ucp->uc_mcontext.sc_gr[22+4-i] = va_arg (ap, int);
91 continue;
92 }
93
94 /* All other arguments go on the stack. */
95 sp[-1 * (FRAME_SIZE_UL + 1 + i)] = va_arg (ap, int);
96 }
97 va_end (ap);
98 }
99 weak_alias(__makecontext, makecontext)