1/* Save and set current context.
2   Copyright (C) 2009-2022 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   <https://www.gnu.org/licenses/>.  */
18
19#include <sysdep.h>
20#include <sys/asm.h>
21#include <sys/fpregdef.h>
22#include <sys/regdef.h>
23
24#include "ucontext_i.h"
25
26/* int swapcontext (ucontext_t *oucp, const ucontext_t *ucp) */
27
28	.text
29	.set	nomips16
30LOCALSZ = 0
31ARGSZ = 0
32MASK = 0x00000000
33#ifdef __PIC__
34LOCALSZ = 1						/* save gp */
35#endif
36#if _MIPS_SIM != _ABIO32
37ARGSZ = 1						/* save a1 */
38# ifdef __PIC__
39MASK = 0x10000000
40# endif
41#endif
42FRAMESZ = (((ARGSZ + LOCALSZ) * SZREG) + ALSZ) & ALMASK
43GPOFF = FRAMESZ - ((ARGSZ + 1) * SZREG)
44#if _MIPS_SIM != _ABIO32
45A1OFF = FRAMESZ - (1 * SZREG)				/* callee-allocated */
46#else
47A1OFF = FRAMESZ + (1 * SZREG)				/* caller-allocated */
48#endif
49MCONTEXT_GREGSZ = 8
50#if _MIPS_SIM == _ABIO32 && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
51MCONTEXT_GREGOFF = 4
52#else
53MCONTEXT_GREGOFF = 0
54#endif
55
56NESTED (__swapcontext, FRAMESZ, ra)
57	.mask	MASK, -(ARGSZ * SZREG)
58	.fmask	0x00000000, 0
59
60#ifdef __PIC__
61	SETUP_GP
62
63	move	a2, sp
64# define _SP a2
65
66# if _MIPS_SIM != _ABIO32
67	move	a3, gp
68#  define _GP a3
69# endif
70
71	PTR_ADDIU sp, -FRAMESZ
72	cfi_adjust_cfa_offset (FRAMESZ)
73	SETUP_GP64_STACK (GPOFF, __swapcontext)
74	SAVE_GP (GPOFF)
75
76#else  /* ! __PIC__ */
77# define _SP sp
78# define _GP gp
79
80#endif /* ! __PIC__ */
81
82#ifdef PROF
83	.set	noat
84	move	AT, ra
85	jal	_mcount
86	.set	at
87#endif
88
89	REG_S	s0, (MCONTEXT_GREGOFF + 16 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
90	REG_S	s1, (MCONTEXT_GREGOFF + 17 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
91	REG_S	s2, (MCONTEXT_GREGOFF + 18 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
92	REG_S	s3, (MCONTEXT_GREGOFF + 19 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
93	REG_S	s4, (MCONTEXT_GREGOFF + 20 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
94	REG_S	s5, (MCONTEXT_GREGOFF + 21 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
95	REG_S	s6, (MCONTEXT_GREGOFF + 22 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
96	REG_S	s7, (MCONTEXT_GREGOFF + 23 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
97#if ! defined (__PIC__) || _MIPS_SIM != _ABIO32
98	REG_S	_GP, (MCONTEXT_GREGOFF + 28 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
99#endif
100	REG_S	_SP, (MCONTEXT_GREGOFF + 29 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
101	REG_S	fp, (MCONTEXT_GREGOFF + 30 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
102	REG_S	ra, (MCONTEXT_GREGOFF + 31 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
103	REG_S	ra, (MCONTEXT_GREGOFF + MCONTEXT_PC)(a0)
104
105#ifdef __mips_hard_float
106# if _MIPS_SIM == _ABI64
107	s.d	fs0, (24 * SZREG + MCONTEXT_FPREGS)(a0)
108	s.d	fs1, (25 * SZREG + MCONTEXT_FPREGS)(a0)
109	s.d	fs2, (26 * SZREG + MCONTEXT_FPREGS)(a0)
110	s.d	fs3, (27 * SZREG + MCONTEXT_FPREGS)(a0)
111	s.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(a0)
112	s.d	fs5, (29 * SZREG + MCONTEXT_FPREGS)(a0)
113	s.d	fs6, (30 * SZREG + MCONTEXT_FPREGS)(a0)
114	s.d	fs7, (31 * SZREG + MCONTEXT_FPREGS)(a0)
115
116# else  /* _MIPS_SIM != _ABI64 */
117	s.d	fs0, (20 * SZREG + MCONTEXT_FPREGS)(a0)
118	s.d	fs1, (22 * SZREG + MCONTEXT_FPREGS)(a0)
119	s.d	fs2, (24 * SZREG + MCONTEXT_FPREGS)(a0)
120	s.d	fs3, (26 * SZREG + MCONTEXT_FPREGS)(a0)
121	s.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(a0)
122	s.d	fs5, (30 * SZREG + MCONTEXT_FPREGS)(a0)
123
124# endif /* _MIPS_SIM != _ABI64 */
125
126	cfc1	v1, fcr31
127	sw	v1, MCONTEXT_FPC_CSR(a0)
128#endif /* __mips_hard_float */
129
130	REG_S	a1, A1OFF(sp)
131
132/* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, &oucp->uc_sigmask, _NSIG8) */
133	li	a3, _NSIG8
134	PTR_ADDU a2, a0, UCONTEXT_SIGMASK
135	PTR_ADDU a1, a1, UCONTEXT_SIGMASK
136	li	a0, SIG_SETMASK
137
138	li	v0, SYS_ify (rt_sigprocmask)
139	syscall
140	bnez	a3, 99f
141
142	REG_L	v0, A1OFF(sp)
143
144#ifdef __mips_hard_float
145# if _MIPS_SIM == _ABI64
146	l.d	fs0, (24 * SZREG + MCONTEXT_FPREGS)(v0)
147	l.d	fs1, (25 * SZREG + MCONTEXT_FPREGS)(v0)
148	l.d	fs2, (26 * SZREG + MCONTEXT_FPREGS)(v0)
149	l.d	fs3, (27 * SZREG + MCONTEXT_FPREGS)(v0)
150	l.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(v0)
151	l.d	fs5, (29 * SZREG + MCONTEXT_FPREGS)(v0)
152	l.d	fs6, (30 * SZREG + MCONTEXT_FPREGS)(v0)
153	l.d	fs7, (31 * SZREG + MCONTEXT_FPREGS)(v0)
154
155# else  /* _MIPS_SIM != _ABI64 */
156	l.d	fs0, (20 * SZREG + MCONTEXT_FPREGS)(v0)
157	l.d	fs1, (22 * SZREG + MCONTEXT_FPREGS)(v0)
158	l.d	fs2, (24 * SZREG + MCONTEXT_FPREGS)(v0)
159	l.d	fs3, (26 * SZREG + MCONTEXT_FPREGS)(v0)
160	l.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(v0)
161	l.d	fs5, (30 * SZREG + MCONTEXT_FPREGS)(v0)
162
163# endif /* _MIPS_SIM != _ABI64 */
164
165	lw	v1, MCONTEXT_FPC_CSR(v0)
166	ctc1	v1, fcr31
167#endif /* __mips_hard_float */
168
169	/* Note the contents of argument registers will be random
170	   unless makecontext() has been called.  */
171	REG_L	a0, (MCONTEXT_GREGOFF + 4 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
172	REG_L	a1, (MCONTEXT_GREGOFF + 5 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
173	REG_L	a2, (MCONTEXT_GREGOFF + 6 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
174	REG_L	a3, (MCONTEXT_GREGOFF + 7 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
175#if _MIPS_SIM != _ABIO32
176	REG_L	a4, (MCONTEXT_GREGOFF + 8 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
177	REG_L	a5, (MCONTEXT_GREGOFF + 9 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
178	REG_L	a6, (MCONTEXT_GREGOFF + 10 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
179	REG_L	a7, (MCONTEXT_GREGOFF + 11 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
180#endif
181
182	REG_L	s0, (MCONTEXT_GREGOFF + 16 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
183	REG_L	s1, (MCONTEXT_GREGOFF + 17 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
184	REG_L	s2, (MCONTEXT_GREGOFF + 18 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
185	REG_L	s3, (MCONTEXT_GREGOFF + 19 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
186	REG_L	s4, (MCONTEXT_GREGOFF + 20 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
187	REG_L	s5, (MCONTEXT_GREGOFF + 21 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
188	REG_L	s6, (MCONTEXT_GREGOFF + 22 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
189	REG_L	s7, (MCONTEXT_GREGOFF + 23 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
190#if ! defined (__PIC__) || _MIPS_SIM != _ABIO32
191	REG_L	gp, (MCONTEXT_GREGOFF + 28 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
192#endif
193	REG_L	sp, (MCONTEXT_GREGOFF + 29 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
194	REG_L	fp, (MCONTEXT_GREGOFF + 30 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
195	REG_L	ra, (MCONTEXT_GREGOFF + 31 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
196	REG_L	t9, (MCONTEXT_GREGOFF + MCONTEXT_PC)(v0)
197
198	move	v0, zero
199	jr	t9
200
20199:
202#ifdef __PIC__
203	PTR_LA	t9, JUMPTARGET (__syscall_error)
204	RESTORE_GP64_STACK
205	PTR_ADDIU sp, FRAMESZ
206	cfi_adjust_cfa_offset (-FRAMESZ)
207	jr	t9
208
209#else  /* ! __PIC__ */
210
211	j	JUMPTARGET (__syscall_error)
212#endif /* ! __PIC__ */
213PSEUDO_END (__swapcontext)
214
215weak_alias (__swapcontext, swapcontext)
216