]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/xtensa/setjmp.S
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / 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, see
17    <http://www.gnu.org/licenses/>.  */
18
19 /* This implementation relies heavily on the Xtensa register window
20    mechanism.  Setjmp flushes all the windows except its own to the
21    stack and then copies registers from the save areas on the stack
22    into the jmp_buf structure, along with the return address of the call
23    to setjmp.  Longjmp invalidates all the windows except its own, and
24    then sets things up so that it will return to the right place,
25    using a window underflow to automatically restore the registers.
26
27    Note that we also save the area located just below the stack pointer
28    of the caller. This save area could get overwritten by alloca
29    following the call to setjmp. The alloca moves the stack pointer
30    to allocate memory on the stack. This newly allocated memory
31    includes(!) the original save area (alloca copies the save area
32    before it moves that stack pointer).
33
34
35    previous caller SP     -> |------------------------------| <-----+
36                              | caller-2 registers a0-a3     |       | p
37                              |------------------------------|       | o
38                              | caller registers a4-a8/a12   |       | i
39                              |------------------------------|       | n
40                              | caller local stack           |       | t
41    caller SP              -> |------------------------------| <-+   | s
42                              | caller-1 registers a0-a3     |  -:---+
43    callee (setjmp) SP     -> |==============================|   |
44                              | caller registers a0-a3       | --+
45                              |------------------------------|
46
47    In case of an alloca, registers a0-a3 of the previous caller (caller-1)
48    are copied (*), and the original location get likely overwritten.
49
50    previous caller SP     -> |------------------------------| <-----+
51                              | caller-2 registers a0-a3     |       | p
52                              |------------------------------|       | o
53                              | caller registers a4-a8/a12   |       | i
54                              |------------------------------|       | n
55                              | caller local stack           |       | t
56    caller SP before alloca-> |------------------------------|       | s
57                              | alloca area (overwrites old  |       |
58                              | copy of caller-1 registers)  |       |
59    caller SP after alloca -> |------------------------------| <-+   |
60                              | caller-1 registers a0-a3 (*) |  -:---+
61    callee (setjmp) SP     -> |==============================|   |
62                              | caller registers a0-a3       | --+
63                              |------------------------------|
64
65    So, when longcall returns to the original caller SP, it also needs
66    to restore the save area below the SP.
67
68   */
69
70 #include "sysdep.h"
71  
72 /* NOTE: The ENTRY macro must allocate exactly 16 bytes (entry a1, 16) */
73
74 /* int setjmp (a2 = jmp_buf env) */
75
76 ENTRY (_setjmp)
77         movi    a3, 0
78         j       1f
79 END (_setjmp)
80 libc_hidden_def (_setjmp)
81
82 ENTRY (setjmp)
83         movi    a3, 1
84         j       1f
85 END (setjmp)
86
87 /* int __sigsetjmp (a2 = jmp_buf env, a3 = int savemask)  */
88
89 ENTRY (__sigsetjmp)
90 1:
91         /* Flush registers.  */
92         movi    a4, __window_spill
93         callx4  a4
94
95         /* Copy the caller register a0-a3 at (sp - 16) to jmpbuf.  */
96         addi    a7, a1, -16
97         l32i    a4, a7, 0
98         l32i    a5, a7, 4
99         s32i    a4, a2, 0
100         s32i    a5, a2, 4
101         l32i    a4, a7, 8
102         l32i    a5, a7, 12
103         s32i    a4, a2, 8
104         s32i    a5, a2, 12
105
106         /* Copy the caller registers a4-a8/a12 from the overflow area.  */
107         /* Note that entry moved the SP by 16B, so SP of caller-1 is at 4(sp) */
108         extui   a7, a0, 30, 2
109         blti    a7, 2, .Lendsj
110         l32i    a8, a1, 4       /* a8: SP of 'caller-1' */
111         slli    a4, a7, 4
112         sub     a6, a8, a4
113         addi    a5, a2, 16
114         addi    a8, a8, -16     /* a8: end of register overflow area */
115 .Lsjloop:
116         l32i    a7, a6, 0
117         l32i    a4, a6, 4
118         s32i    a7, a5, 0
119         s32i    a4, a5, 4
120         l32i    a7, a6, 8
121         l32i    a4, a6, 12
122         s32i    a7, a5, 8
123         s32i    a4, a5, 12
124         addi    a5, a6, 16
125         addi    a6, a6, 16
126         blt     a6, a8, .Lsjloop
127 .Lendsj:
128
129         /* Copy previous caller registers (this is assuming 'entry a1,16') */
130         l32i    a4, a1, 0
131         l32i    a5, a1, 4
132         s32i    a4, a2, 48
133         s32i    a5, a2, 52
134         l32i    a4, a1, 8
135         l32i    a5, a1, 12
136         s32i    a4, a2, 56
137         s32i    a5, a2, 60
138
139         /* Save the return address, including the window size bits.  */
140         s32i    a0, a2, 64
141
142         /* a2 still points to jmp_buf.  a3 contains savemask.  */
143         mov     a6, a2
144         mov     a7, a3
145         movi    a3, __sigjmp_save
146         callx4  a3
147         mov     a2, a6
148         retw
149 END(__sigsetjmp)
150
151 weak_extern(_setjmp)
152 weak_extern(setjmp)