2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc32 / makecontext.c
blob9b48dade636e6095d0f829dea6a8115a560444b4
1 /* Create new context.
2 Copyright (C) 2008 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by David S. Miller <davem@davemloft.net>, 2008.
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.
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.
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. */
21 #include <sysdep.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24 #include <ucontext.h>
26 /* Sets up the outgoing arguments and the program counter for a user
27 context for the requested function call.
29 Returning to the correct parent context is pretty simple on
30 Sparc. We only need to link up the register windows correctly.
31 Since global registers are clobbered by calls, we need not be
32 concernred about those, and thus is all could be worked out without
33 using a trampoline.
35 Except that we must deal with the signal mask, thus a trampoline
36 is unavoidable. 32-bit stackframe layout:
37 +-----------------------------------------+
38 | 7th and further parameters |
39 +-----------------------------------------+
40 | backup storage for initial 6 parameters |
41 +-----------------------------------------+
42 | struct return pointer |
43 +-----------------------------------------+
44 | 8 incoming registers |
45 +-----------------------------------------+
46 | 8 local registers |
47 %sp --> +-----------------------------------------+
51 void
52 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
54 extern void __start_context (void);
55 unsigned long int *sp;
56 va_list ap;
57 int i;
59 sp = (unsigned long int *) (ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size);
60 sp -= 16 + 7 + argc;
61 sp = (unsigned long int *) (((uintptr_t) sp) & ~(8 - 1));
63 for (i = 0; i < 8; i++)
64 sp[i + 8] = ucp->uc_mcontext.gregs[REG_O0 + i];
66 /* The struct return pointer is essentially unused, so we can
67 place the link there. */
68 sp[16] = (unsigned long int) ucp->uc_link;
70 va_start (ap, argc);
72 /* Fill in outgoing arguments, including those which will
73 end up being passed on the stack. */
74 for (i = 0; i < argc; i++)
76 unsigned long int arg = va_arg (ap, unsigned long int);
77 if (i < 6)
78 ucp->uc_mcontext.gregs[REG_O0 + i] = arg;
79 else
80 sp[i + 23] = arg;
83 va_end (ap);
85 ucp->uc_mcontext.gregs[REG_O6] = (unsigned long int) sp;
87 ucp->uc_mcontext.gregs[REG_O7] = ((unsigned long int) __start_context) - 8;
89 ucp->uc_mcontext.gregs[REG_PC] = (unsigned long int) func;
90 ucp->uc_mcontext.gregs[REG_nPC] = ucp->uc_mcontext.gregs[REG_PC] + 4;
93 weak_alias (__makecontext, makecontext)