summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/xtensa/setjmp.S
blob: bf46912947ba1fc855af0373bb41c59a75a897ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* setjmp for Xtensa Processors.
   Copyright (C) 2001, 2007 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

/* This implementation relies heavily on the Xtensa register window
   mechanism.  Setjmp flushes all the windows except its own to the
   stack and then copies registers from the save areas on the stack
   into the jmp_buf structure, along with the return address of the call
   to setjmp.  Longjmp invalidates all the windows except its own, and
   then sets things up so that it will return to the right place,
   using a window underflow to automatically restore the registers.

   Note that we also save the area located just below the stack pointer
   of the caller. This save area could get overwritten by alloca
   following the call to setjmp. The alloca moves the stack pointer
   to allocate memory on the stack. This newly allocated memory
   includes(!) the original save area (alloca copies the save area
   before it moves that stack pointer).


   previous caller SP     -> |------------------------------| <-----+
                             | caller-2 registers a0-a3     |       | p
                             |------------------------------|       | o
                             | caller registers a4-a8/a12   |       | i
                             |------------------------------|       | n
                             | caller local stack           |       | t
   caller SP              -> |------------------------------| <-+   | s
                             | caller-1 registers a0-a3     |  -:---+
   callee (setjmp) SP     -> |==============================|   |
                             | caller registers a0-a3       | --+
                             |------------------------------|

   In case of an alloca, registers a0-a3 of the previous caller (caller-1)
   are copied (*), and the original location get likely overwritten.

   previous caller SP     -> |------------------------------| <-----+
                             | caller-2 registers a0-a3     |       | p
                             |------------------------------|       | o
                             | caller registers a4-a8/a12   |       | i
                             |------------------------------|       | n
                             | caller local stack           |       | t
   caller SP before alloca-> |------------------------------|       | s
                             | alloca area (overwrites old  |       |
                             | copy of caller-1 registers)  |       |
   caller SP after alloca -> |------------------------------| <-+   |
                             | caller-1 registers a0-a3 (*) |  -:---+
   callee (setjmp) SP     -> |==============================|   |
                             | caller registers a0-a3       | --+
                             |------------------------------|

   So, when longcall returns to the original caller SP, it also needs
   to restore the save area below the SP.

  */

#include "sysdep.h"
 
/* NOTE: The ENTRY macro must allocate exactly 16 bytes (entry a1, 16) */

/* int setjmp (a2 = jmp_buf env) */

ENTRY (_setjmp)
	movi	a3, 0
	j	1f
END (_setjmp)
libc_hidden_def (_setjmp)

ENTRY (setjmp)
	movi	a3, 1
	j	1f
END (setjmp)

/* int __sigsetjmp (a2 = jmp_buf env, a3 = int savemask)  */

ENTRY (__sigsetjmp)
1:
	/* Flush registers.  */
	movi	a4, __window_spill
	callx4	a4

	/* Copy the caller register a0-a3 at (sp - 16) to jmpbuf.  */
	addi	a7, a1, -16
	l32i	a4, a7, 0
	l32i	a5, a7, 4
	s32i	a4, a2, 0
	s32i	a5, a2, 4
	l32i	a4, a7, 8
	l32i	a5, a7, 12
	s32i	a4, a2, 8
	s32i	a5, a2, 12

	/* Copy the caller registers a4-a8/a12 from the overflow area.  */
	/* Note that entry moved the SP by 16B, so SP of caller-1 is at 4(sp) */
	extui	a7, a0, 30, 2
	blti	a7, 2, .Lendsj
	l32i	a8, a1, 4	/* a8: SP of 'caller-1' */
	slli	a4, a7, 4
	sub	a6, a8, a4
	addi	a5, a2, 16
	addi	a8, a8, -16	/* a8: end of register overflow area */
.Lsjloop:
	l32i	a7, a6, 0
	l32i	a4, a6, 4
	s32i	a7, a5, 0
	s32i	a4, a5, 4
	l32i	a7, a6, 8
	l32i	a4, a6, 12
	s32i	a7, a5, 8
	s32i	a4, a5, 12
	addi	a5, a6, 16
	addi	a6, a6, 16
	blt	a6, a8, .Lsjloop
.Lendsj:

	/* Copy previous caller registers (this is assuming 'entry a1,16') */
	l32i	a4, a1, 0
	l32i	a5, a1, 4
	s32i	a4, a2, 48
	s32i	a5, a2, 52
	l32i	a4, a1, 8
	l32i	a5, a1, 12
	s32i	a4, a2, 56
	s32i	a5, a2, 60

	/* Save the return address, including the window size bits.  */
	s32i	a0, a2, 64

	/* a2 still points to jmp_buf.  a3 contains savemask.  */
	mov	a6, a2
	mov	a7, a3
	movi	a3, __sigjmp_save
	callx4	a3
	mov	a2, a6
	retw
END(__sigsetjmp)

weak_extern(_setjmp)
weak_extern(setjmp)