1 /* setjmp for Xtensa Processors.
2 Copyright (C) 2001, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, see
17 <http://www.gnu.org/licenses/>. */
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.
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).
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 |------------------------------|
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.
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 |------------------------------|
65 So, when longcall returns to the original caller SP, it also needs
66 to restore the save area below the SP.
72 /* NOTE: The ENTRY macro must allocate exactly 16 bytes (entry a1, 16) */
74 /* int setjmp (a2 = jmp_buf env) */
80 libc_hidden_def (_setjmp)
87 /* int __sigsetjmp (a2 = jmp_buf env, a3 = int savemask) */
91 /* Flush registers. */
92 movi a4, __window_spill
95 /* Copy the caller register a0-a3 at (sp - 16) to jmpbuf. */
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) */
110 l32i a8, a1, 4 /* a8: SP of 'caller-1' */
114 addi a8, a8, -16 /* a8: end of register overflow area */
129 /* Copy previous caller registers (this is assuming 'entry a1,16') */
139 /* Save the return address, including the window size bits. */
142 /* a2 still points to jmp_buf. a3 contains savemask. */
145 movi a3, __sigjmp_save