2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / unix / sysv / linux / ia64 / __sigstack_longjmp.c
blob8f552eaad42659aa6c5bcbe4a163b36fe782b831
1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.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 /* The public __longjmp() implementation is limited to jumping within
21 the same stack. That is, in general it is not possible to use this
22 __longjmp() implementation to cross from one stack to another.
23 In constrast, the __sigstack_longjmp() implemented here allows
24 crossing from the alternate signal stack to the normal stack
25 as a special case. */
27 #include <assert.h>
28 #include <setjmp.h>
29 #include <signal.h>
30 #include <stdint.h>
31 #include <stdlib.h>
33 #include <sysdep.h>
34 #include <sys/rse.h>
36 #define JB_SP 0
37 #define JB_BSP 17
39 struct rbs_flush_values
41 unsigned long bsp;
42 unsigned long rsc;
43 unsigned long rnat;
46 extern struct rbs_flush_values __ia64_flush_rbs (void);
47 extern void __ia64_longjmp (__jmp_buf buf, int val, long rnat, long rsc)
48 __attribute__ ((__noreturn__));
50 static void
51 copy_rbs (unsigned long *dst, unsigned long *dst_end, unsigned long dst_rnat,
52 unsigned long *src, unsigned long *src_end,
53 unsigned long current_rnat)
55 unsigned long dst_slot, src_rnat = 0, src_slot, *src_rnat_addr, nat_bit;
56 int first_time = 1;
58 while (dst < dst_end)
60 dst_slot = ia64_rse_slot_num (dst);
61 if (dst_slot == 63)
63 *dst++ = dst_rnat;
64 dst_rnat = 0;
66 else
68 /* read source value, including NaT bit: */
69 src_slot = ia64_rse_slot_num (src);
70 if (src_slot == 63)
72 /* skip src RNaT slot */
73 ++src;
74 src_slot = 0;
76 if (first_time || src_slot == 0)
78 first_time = 0;
79 src_rnat_addr = ia64_rse_rnat_addr (src);
80 if (src_rnat_addr < src_end)
81 src_rnat = *src_rnat_addr;
82 else
83 src_rnat = current_rnat;
85 nat_bit = (src_rnat >> src_slot) & 1;
87 assert (src < src_end);
89 *dst++ = *src++;
90 if (nat_bit)
91 dst_rnat |= (1UL << dst_slot);
92 else
93 dst_rnat &= ~(1UL << dst_slot);
96 dst_slot = ia64_rse_slot_num (dst);
97 if (dst_slot > 0)
98 *ia64_rse_rnat_addr (dst) = dst_rnat;
101 void
102 __sigstack_longjmp (__jmp_buf buf, int val)
104 unsigned long *rbs_base, *bsp, *bspstore, *jb_bsp, jb_sp, ss_sp;
105 unsigned long ndirty, rnat, load_rnat, *jb_rnat_addr;
106 struct sigcontext *sc;
107 stack_t stk;
108 struct rbs_flush_values c;
110 /* put RSE into enforced-lazy mode and return current bsp/rsc/rnat: */
111 c = __ia64_flush_rbs ();
113 jb_sp = ((unsigned long *) buf)[JB_SP];
114 jb_bsp = ((unsigned long **) buf)[JB_BSP];
116 INTERNAL_SYSCALL_DECL (err);
117 (void) INTERNAL_SYSCALL (sigaltstack, err, 2, NULL, &stk);
119 ss_sp = (unsigned long) stk.ss_sp;
120 jb_rnat_addr = ia64_rse_rnat_addr (jb_bsp);
122 if ((stk.ss_flags & SS_ONSTACK) == 0 || jb_sp - ss_sp < stk.ss_size)
123 /* Normal non-stack-crossing longjmp; if the RNaT slot for the bsp
124 saved in the jump-buffer is the same as the one for the current
125 BSP, use the current AR.RNAT value, otherwise, load it from the
126 jump-buffer's RNaT-slot. */
127 load_rnat = (ia64_rse_rnat_addr ((unsigned long *) c.bsp) != jb_rnat_addr);
128 else
130 /* If we are on the alternate signal-stack and the jump-buffer
131 lies outside the signal-stack, we may need to copy back the
132 dirty partition which was torn off and saved on the
133 signal-stack when the signal was delivered.
135 Caveat: we assume that the top of the alternate signal-stack
136 stores the sigcontext structure of the signal that
137 caused the switch to the signal-stack. This should
138 be a fairly safe assumption but the kernel _could_
139 do things differently.. */
140 sc = ((struct sigcontext *) ((ss_sp + stk.ss_size) & -16) - 1);
142 /* As a sanity-check, verify that the register-backing-store base
143 of the alternate signal-stack is where we expect it. */
144 rbs_base = (unsigned long *)
145 ((ss_sp + sizeof (long) - 1) & -sizeof (long));
147 assert ((unsigned long) rbs_base == sc->sc_rbs_base);
149 ndirty = ia64_rse_num_regs (rbs_base, rbs_base + (sc->sc_loadrs >> 19));
150 bsp = (unsigned long *) sc->sc_ar_bsp;
151 bspstore = ia64_rse_skip_regs (bsp, -ndirty);
153 if (bspstore < jb_bsp)
154 /* AR.BSPSTORE at the time of the signal was below the value
155 of AR.BSP saved in the jump-buffer => copy the missing
156 portion from the torn off dirty partition which got saved
157 on the alternate signal-stack. */
158 copy_rbs (bspstore, jb_bsp, sc->sc_ar_rnat,
159 rbs_base, (unsigned long *) c.bsp, c.rnat);
161 load_rnat = 1;
163 if (load_rnat)
164 rnat = *jb_rnat_addr;
165 else
166 rnat = c.rnat;
167 __ia64_longjmp (buf, val, rnat, c.rsc);