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
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
39 struct rbs_flush_values
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__
));
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
;
60 dst_slot
= ia64_rse_slot_num (dst
);
68 /* read source value, including NaT bit: */
69 src_slot
= ia64_rse_slot_num (src
);
72 /* skip src RNaT slot */
76 if (first_time
|| src_slot
== 0)
79 src_rnat_addr
= ia64_rse_rnat_addr (src
);
80 if (src_rnat_addr
< src_end
)
81 src_rnat
= *src_rnat_addr
;
83 src_rnat
= current_rnat
;
85 nat_bit
= (src_rnat
>> src_slot
) & 1;
87 assert (src
< src_end
);
91 dst_rnat
|= (1UL << dst_slot
);
93 dst_rnat
&= ~(1UL << dst_slot
);
96 dst_slot
= ia64_rse_slot_num (dst
);
98 *ia64_rse_rnat_addr (dst
) = dst_rnat
;
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
;
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
);
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
);
164 rnat
= *jb_rnat_addr
;
167 __ia64_longjmp (buf
, val
, rnat
, c
.rsc
);