OSDN Git Service

5e81460c7c63b23f5617af59a74ec834f623b7ef
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / xtensa / setjmp.S
1 /* setjmp for Xtensa Processors.
2    Copyright (C) 2001, 2007 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
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    Lesser General Public License for more details.
14
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., 51 Franklin Street - Fifth Floor,
18    Boston, MA 02110-1301, USA.  */
19
20 /* This implementation relies heavily on the Xtensa register window
21    mechanism.  Setjmp flushes all the windows except its own to the
22    stack and then copies registers from the save areas on the stack
23    into the jmp_buf structure, along with the return address of the call
24    to setjmp.  Longjmp invalidates all the windows except its own, and
25    then sets things up so that it will return to the right place,
26    using a window underflow to automatically restore the registers.
27
28    Note that it would probably be sufficient to only copy the
29    registers from setjmp's caller into jmp_buf.  However, we also copy
30    the save area located at the stack pointer of setjmp's caller.
31    This save area will typically remain intact until the longjmp call.
32    The one exception is when there is an intervening alloca in
33    setjmp's caller.  This is certainly an unusual situation and is
34    likely to cause problems in any case (the storage allocated on the
35    stack cannot be safely accessed following the longjmp).  As bad as
36    it is, on most systems this situation would not necessarily lead to
37    a catastrophic failure.  If we did not preserve the extra save area
38    on Xtensa, however, it would.  When setjmp's caller returns after a
39    longjmp, there will be a window underflow; an invalid return
40    address or stack pointer in the save area will almost certainly
41    lead to a crash.  Keeping a copy of the extra save area in the
42    jmp_buf avoids this with only a small additional cost.  If setjmp
43    and longjmp are ever time-critical, this could be removed.  */
44
45 #include "sysdep.h"
46
47 /* int setjmp (a2 = jmp_buf env) */
48
49 ENTRY (_setjmp)
50         movi    a3, 0
51         j       1f
52 END (_setjmp)
53 libc_hidden_def (_setjmp)
54
55 ENTRY (setjmp)
56         movi    a3, 1
57         j       1f
58 END (setjmp)
59
60 /* int __sigsetjmp (a2 = jmp_buf env,
61                     a3 = int savemask)  */
62
63 ENTRY (__sigsetjmp)
64 1:      
65         /* Flush registers.  */
66         movi    a4, __window_spill
67         callx4  a4
68
69         /* Preserve the second argument (savemask) in a15.  The selection
70            of a15 is arbitrary, except it's otherwise unused.  There is no
71            risk of triggering a window overflow since we just returned
72            from __window_spill().  */
73         mov     a15, a3
74
75         /* Copy the register save area at (sp - 16).  */
76         addi    a5, a1, -16
77         l32i    a3, a5, 0
78         l32i    a4, a5, 4
79         s32i    a3, a2, 0
80         s32i    a4, a2, 4
81         l32i    a3, a5, 8
82         l32i    a4, a5, 12
83         s32i    a3, a2, 8
84         s32i    a4, a2, 12
85
86         /* Copy 0-8 words from the register overflow area.  */
87         extui   a3, a0, 30, 2
88         blti    a3, 2, .Lendsj
89         l32i    a7, a1, 4
90         slli    a4, a3, 4
91         sub     a5, a7, a4
92         addi    a6, a2, 16
93         addi    a7, a7, -16             // a7 = end of register overflow area
94 .Lsjloop:
95         l32i    a3, a5, 0
96         l32i    a4, a5, 4
97         s32i    a3, a6, 0
98         s32i    a4, a6, 4
99         l32i    a3, a5, 8
100         l32i    a4, a5, 12
101         s32i    a3, a6, 8
102         s32i    a4, a6, 12
103         addi    a5, a5, 16
104         addi    a6, a6, 16
105         blt     a5, a7, .Lsjloop
106 .Lendsj:
107
108         /* Copy the register save area at sp.  */
109         l32i    a3, a1, 0
110         l32i    a4, a1, 4
111         s32i    a3, a2, 48
112         s32i    a4, a2, 52
113         l32i    a3, a1, 8
114         l32i    a4, a1, 12
115         s32i    a3, a2, 56
116         s32i    a4, a2, 60
117
118         /* Save the return address, including the window size bits.  */
119         s32i    a0, a2, 64
120
121         /* a2 still addresses jmp_buf.  a15 contains savemask.  */
122         mov     a6, a2
123         mov     a7, a15
124         movi    a3, __sigjmp_save
125         callx4  a3
126         mov     a2, a6
127         retw
128 END(__sigsetjmp)
129
130 weak_extern(_setjmp)
131 weak_extern(setjmp)