summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/xtensa/swapcontext.S
blob: 40b38e98ca01cb93732ac0a486efe94afe4552b8 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Copyright (C) 2018 - 2022 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/>.  */

#include <sysdep.h>
#include "ucontext_i.h"

#if defined(__XTENSA_CALL0_ABI__)
ENTRY(__swapcontext)
	s32i	a0, a2, MCONTEXT_SC_PC
	s32i	a1, a2, MCONTEXT_SC_A_0 + 4

	/* save callee-saved registers in the context */
	s32i	a12, a2, MCONTEXT_SC_A_0 + 48
	s32i	a13, a2, MCONTEXT_SC_A_0 + 52
	s32i	a14, a2, MCONTEXT_SC_A_0 + 56
	s32i	a15, a2, MCONTEXT_SC_A_0 + 60

	mov	a12, a3
	mov	a13, a2

	addi	a3, a3, UCONTEXT_SIGMASK
	addi	a4, a2, UCONTEXT_SIGMASK
	movi	a2, SIG_SETMASK
	movi	a5, JUMPTARGET (sigprocmask)
	FDPIC_LOAD_JUMPTARGET (a5, a11, a5)
	callx0	a5
	bnez	a2, .Lerror

	mov	a2, a12
	l32i	a0, a2, MCONTEXT_SC_PC
	l32i	a1, a2, MCONTEXT_SC_A_0 + 4

	/* load callee-saved registers from the context */
	l32i	a12, a2, MCONTEXT_SC_A_0 + 48
	l32i	a13, a2, MCONTEXT_SC_A_0 + 52
	l32i	a14, a2, MCONTEXT_SC_A_0 + 56
	l32i	a15, a2, MCONTEXT_SC_A_0 + 60

	movi	a2, 0
	ret
.Lerror:
	l32i	a0, a13, MCONTEXT_SC_PC
	l32i	a12, a13, MCONTEXT_SC_A_0 + 48
	l32i	a13, a13, MCONTEXT_SC_A_0 + 52
	ret
END(__swapcontext)
#elif defined(__XTENSA_WINDOWED_ABI__)
ENTRY(__swapcontext)
	movi	a4, __window_spill
	callx4	a4
	mov	a9, a3
	s32i	a0, a2, MCONTEXT_SC_PC

	/* copy registers a0..a3 from spill area */
	addi	a3, a1, -16
	l32i	a4, a3, 0
	l32i	a5, a3, 4
	l32i	a6, a3, 8
	l32i	a7, a3, 12
	s32i	a4, a2, MCONTEXT_SC_A_0 + 0
	s32i	a5, a2, MCONTEXT_SC_A_0 + 4
	s32i	a6, a2, MCONTEXT_SC_A_0 + 8
	s32i	a7, a2, MCONTEXT_SC_A_0 + 12

	/* if it was call4 then register saving is done */
	extui	a4, a0, 30, 2
	bltui	a4, 2, 1f

	/* otherwise load spill overflow area address into a3 */
	addi	a3, a5, -16
	l32i	a3, a3, 4
	addi	a3, a3, -32
	beqi	a4, 2, 2f

	/* copy registers a8..a11 from spill overflow area */
	addi	a3, a3, -16
	l32i	a4, a3, 16
	l32i	a5, a3, 20
	l32i	a6, a3, 24
	l32i	a7, a3, 28
	s32i	a4, a2, MCONTEXT_SC_A_0 + 32
	s32i	a5, a2, MCONTEXT_SC_A_0 + 36
	s32i	a6, a2, MCONTEXT_SC_A_0 + 40
	s32i	a7, a2, MCONTEXT_SC_A_0 + 44

	/* copy registers a4..a7 from spill overflow area */
2:
	l32i	a4, a3, 0
	l32i	a5, a3, 4
	l32i	a6, a3, 8
	l32i	a7, a3, 12
	s32i	a4, a2, MCONTEXT_SC_A_0 + 16
	s32i	a5, a2, MCONTEXT_SC_A_0 + 20
	s32i	a6, a2, MCONTEXT_SC_A_0 + 24
	s32i	a7, a2, MCONTEXT_SC_A_0 + 28
1:
	movi	a6, SIG_SETMASK
	addi	a7, a9, UCONTEXT_SIGMASK
	addi	a8, a2, UCONTEXT_SIGMASK
	mov	a2, a9
	movi	a4, JUMPTARGET (sigprocmask)
	callx4	a4
	bnez	a6, .Lerror

	l32i	a0, a2, MCONTEXT_SC_PC

	/* copy registers a0..a3 to spill area */
	addi	a3, a1, -16
	l32i	a4, a2, MCONTEXT_SC_A_0 + 0
	l32i	a5, a2, MCONTEXT_SC_A_0 + 4
	l32i	a6, a2, MCONTEXT_SC_A_0 + 8
	l32i	a7, a2, MCONTEXT_SC_A_0 + 12
	s32i	a4, a3, 0
	s32i	a5, a3, 4
	s32i	a6, a3, 8
	s32i	a7, a3, 12

	/* if it was call4 then register setup is done */
	extui	a4, a0, 30, 2
	bltui	a4, 2, 1f

	/* otherwise load spill overflow area address into a3 */
	addi	a3, a5, -16
	l32i	a3, a3, 4
	addi	a3, a3, -32
	beqi	a4, 2, 2f

	/* copy registers a8..a11 to spill overflow area */
	addi	a3, a3, -16
	l32i	a4, a2, MCONTEXT_SC_A_0 + 32
	l32i	a5, a2, MCONTEXT_SC_A_0 + 36
	l32i	a6, a2, MCONTEXT_SC_A_0 + 40
	l32i	a7, a2, MCONTEXT_SC_A_0 + 44
	s32i	a4, a3, 16
	s32i	a5, a3, 20
	s32i	a6, a3, 24
	s32i	a7, a3, 28

	/* copy registers a4..a7 to spill overflow area */
2:
	l32i	a4, a2, MCONTEXT_SC_A_0 + 16
	l32i	a5, a2, MCONTEXT_SC_A_0 + 20
	l32i	a6, a2, MCONTEXT_SC_A_0 + 24
	l32i	a7, a2, MCONTEXT_SC_A_0 + 28
	s32i	a4, a3, 0
	s32i	a5, a3, 4
	s32i	a6, a3, 8
	s32i	a7, a3, 12
1:
	movi	a2, 0
	retw
.Lerror:
	mov	a2, a6
	retw
END(__swapcontext)
#else
#error Unsupported Xtensa ABI
#endif

weak_alias (__swapcontext, swapcontext)