Update.
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / ia64 / makecontext.c
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3      Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ucontext.h>
24
25 #include <ia64/rse.h>
26
27 struct fdesc
28   {
29     unsigned long ip;
30     unsigned long gp;
31   };
32
33 #define PUSH(val)                               \
34 do {                                            \
35   if (ia64_rse_is_rnat_slot (rbs))              \
36     *rbs++ = 0;                                 \
37   *rbs++ = (val);                               \
38 } while (0)
39
40
41 /* This implementation can handle an ARGC value of at most 8 and
42    values can be passed only in integer registers (r32-r39).  */
43
44 void
45 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
46 {
47   struct sigcontext *sc = &ucp->uc_mcontext;
48   extern void __start_context (ucontext_t *link, long gp, ...);
49   unsigned long stack_start, stack_end;
50   va_list ap;
51   long *rbs;
52   int i;
53
54   stack_start = (long) sc->sc_stack.ss_sp;
55   stack_end = (long) sc->sc_stack.ss_sp + sc->sc_stack.ss_size;
56
57   stack_start = (stack_start + 7) & -8;
58   stack_end = (stack_start + 15) & -16;
59
60   if (argc > 8)
61     {
62       fprintf (stderr, "__makecontext: does not know how to handle more "
63                "than 8 arguments\n");
64       exit (-1);
65     }
66
67   /* set the entry point and global pointer: */
68   sc->sc_br[0] = ((struct fdesc *) &__start_context)->ip;
69   sc->sc_br[1] = ((struct fdesc *) func)->ip;
70   sc->sc_gr[1] = ((struct fdesc *) func)->gp;
71
72   /* set up the call frame: */
73   sc->sc_ar_pfs = ((sc->sc_ar_pfs & ~0x3fffffffffUL)
74                    | (argc + 2) | ((argc + 2) << 7));
75   rbs = (long *) stack_start;
76   PUSH((long) ucp->uc_link);
77   PUSH(((struct fdesc *) &__start_context)->gp);
78   va_start (ap, argc);
79   for (i = 0; i < argc; ++i)
80     PUSH(va_arg (ap, long));
81   va_end (ap);
82
83   /* set the memory and register stack pointers: */
84   sc->sc_ar_bsp = (long) rbs;
85   sc->sc_gr[12] = stack_end - 16;
86
87   /* clear the NaT bits for r1 and r12: */
88   sc->sc_nat &= ~((1 << 1) | (1 << 12));
89   sc->sc_ar_rnat = 0;
90 }
91
92 weak_alias (__makecontext, makecontext)