(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / unix / sysv / linux / s390 / s390-32 / makecontext.c
blob29c8640e92568b77f5004894e4e37b736176b83a
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <libintl.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <ucontext.h>
26 /* This implementation can handle any ARGC value but only
27 normal integer type parameters. Parameters of type float,
28 double, complex and structure with sizes 0, 2, 4 or 8
29 won't work.
30 makecontext sets up a stack and the registers for the
31 context. The stack looks like this:
32 size offset
33 %r15 -> +-----------------------+
34 4 | back chain (zero) | 0
35 4 | reserved | 4
36 88 | save area for (*func) | 8
37 +-----------------------+
38 n | overflow parameters | 96
39 +-----------------------+
40 8 | trampoline | 96+n
41 +-----------------------+
42 The registers are set up like this:
43 %r2-%r6: parameters 1 to 5
44 %r7 : (*func) pointer
45 %r8 : uc_link from ucontext structure
46 %r9 : address of setcontext
47 %r14 : return address to uc_link trampoline
48 %r15 : stack pointer.
50 The trampoline looks like this:
51 basr %r14,%r7
52 lr %r2,%r8
53 br %r9. */
55 void
56 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
58 unsigned long *sp;
59 va_list ap;
60 int i;
62 sp = (long *) (((long) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size) & -8L);
64 /* Setup the trampoline. */
65 *--sp = 0x07f90000;
66 *--sp = 0x0de71828;
68 /* Set the return address to trampoline. */
69 ucp->uc_mcontext.gregs[14] = (long) sp;
71 /* Set register parameters. */
72 va_start (ap, argc);
73 for (i = 0; (i < argc) && (i < 5); i++)
74 ucp->uc_mcontext.gregs[2+i] = va_arg (ap, long);
76 /* The remaining arguments go to the overflow area. */
77 if (argc > 5) {
78 sp -= argc - 5;
79 for (i = 5; i < argc; i++)
80 sp[i] = va_arg(ap, long);
82 va_end (ap);
84 /* Make room for the save area and set the backchain. */
85 sp -= 24;
86 *sp = 0;
88 /* Pass (*func) to __start_context in %r7. */
89 ucp->uc_mcontext.gregs[7] = (long) func;
91 /* Pass ucp->uc_link to __start_context in %r8. */
92 ucp->uc_mcontext.gregs[8] = (long) ucp->uc_link;
94 /* Pass address of setcontext in %r9. */
95 ucp->uc_mcontext.gregs[9] = (long) &setcontext;
97 /* Set stack pointer. */
98 ucp->uc_mcontext.gregs[15] = (long) sp;
101 weak_alias (__makecontext, makecontext)