OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / xtensa / __longjmp.S
1 /* longjmp for Xtensa Processors.
2
3    Copyright (C) 2001, 2007 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
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.
10
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.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
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
46 #include "sysdep.h"
47
48
49 ENTRY (__longjmp)
50
51         /* Invalidate all but the current window.  Reading and writing
52            special registers WINDOWBASE and WINDOWSTART are
53            privileged operations, so user processes must call the
54            slower __window_spill() to do the job.  */
55
56         movi    a4, __window_spill
57         callx4  a4
58
59         /* Return to the return address of the setjmp, using the
60            window size bits from the setjmp call so that the caller
61            will be able to find the return value that we put in a2.  */
62
63         l32i    a0, a2, 64
64
65         /* Copy the first 4 saved registers from jmp_buf into the save area
66            at the current sp so that the values will be restored to registers
67            when longjmp returns. */
68
69         addi    a7, a1, -16
70         l32i    a4, a2, 0
71         l32i    a5, a2, 4
72         s32i    a4, a7, 0
73         s32i    a5, a7, 4
74         l32i    a4, a2, 8
75         l32i    a5, a2, 12
76         s32i    a4, a7, 8
77         s32i    a5, a7, 12
78
79         /* Copy the remaining 0-8 saved registers.  */
80         extui   a7, a0, 30, 2
81         blti    a7, 2, .Lendlj
82         l32i    a8, a2, 52
83         slli    a4, a7, 4
84         sub     a6, a8, a4
85         addi    a5, a2, 16
86         addi    a8, a8, -16             /* a8 = end of register overflow area */
87 .Lljloop:
88         l32i    a7, a5, 0
89         l32i    a4, a5, 4
90         s32i    a7, a6, 0
91         s32i    a4, a6, 4
92         l32i    a7, a5, 8
93         l32i    a4, a5, 12
94         s32i    a7, a6, 8
95         s32i    a4, a6, 12
96         addi    a5, a5, 16
97         addi    a6, a6, 16
98         blt     a6, a8, .Lljloop
99 .Lendlj:
100
101         /* The 4 words saved from the register save area at the target's
102            sp are copied back to the target procedure's save area.  The
103            only point of this is to prevent a catastrophic failure in
104            case the contents were moved by an alloca after calling
105            setjmp.  This is a bit paranoid but it doesn't cost much.  */
106
107         l32i    a7, a2, 4               /* load the target stack pointer */
108         addi    a7, a7, -16             /* find the destination save area */
109         l32i    a4, a2, 48
110         l32i    a5, a2, 52
111         s32i    a4, a7, 0
112         s32i    a5, a7, 4
113         l32i    a4, a2, 56
114         l32i    a5, a2, 60
115         s32i    a4, a7, 8
116         s32i    a5, a7, 12
117
118         /* Return v ? v : 1.  */
119         movi    a2, 1
120         movnez  a2, a3, a3
121
122         retw
123 END (__longjmp)
124
125 libc_hidden_def (__longjmp)