1/* 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 setcontext (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 a0 */
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
45A0OFF = FRAMESZ - (1 * SZREG)				/* callee-allocated */
46#else
47A0OFF = FRAMESZ + (0 * 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 (__setcontext, FRAMESZ, ra)
57	.mask	MASK, -(ARGSZ * SZREG)
58	.fmask	0x00000000, 0
59
60#ifdef __PIC__
61	SETUP_GP
62#endif
63
64	PTR_ADDIU sp, -FRAMESZ
65	cfi_adjust_cfa_offset (FRAMESZ)
66
67#ifdef __PIC__
68	SETUP_GP64_STACK (GPOFF, __setcontext)
69	SAVE_GP (GPOFF)
70#endif
71
72#ifdef PROF
73	.set	noat
74	move	AT, ra
75	jal	_mcount
76	.set	at
77#endif
78
79	REG_S	a0, A0OFF(sp)
80
81/* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, NULL, _NSIG8) */
82	li	a3, _NSIG8
83	move	a2, zero
84	PTR_ADDU a1, a0, UCONTEXT_SIGMASK
85	li	a0, SIG_SETMASK
86
87	li	v0, SYS_ify (rt_sigprocmask)
88	syscall
89	bnez	a3, 99f
90
91	REG_L	v0, A0OFF(sp)
92
93#ifdef __mips_hard_float
94# if _MIPS_SIM == _ABI64
95	l.d	fs0, (24 * SZREG + MCONTEXT_FPREGS)(v0)
96	l.d	fs1, (25 * SZREG + MCONTEXT_FPREGS)(v0)
97	l.d	fs2, (26 * SZREG + MCONTEXT_FPREGS)(v0)
98	l.d	fs3, (27 * SZREG + MCONTEXT_FPREGS)(v0)
99	l.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(v0)
100	l.d	fs5, (29 * SZREG + MCONTEXT_FPREGS)(v0)
101	l.d	fs6, (30 * SZREG + MCONTEXT_FPREGS)(v0)
102	l.d	fs7, (31 * SZREG + MCONTEXT_FPREGS)(v0)
103
104# else  /* _MIPS_SIM != _ABI64 */
105	l.d	fs0, (20 * SZREG + MCONTEXT_FPREGS)(v0)
106	l.d	fs1, (22 * SZREG + MCONTEXT_FPREGS)(v0)
107	l.d	fs2, (24 * SZREG + MCONTEXT_FPREGS)(v0)
108	l.d	fs3, (26 * SZREG + MCONTEXT_FPREGS)(v0)
109	l.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(v0)
110	l.d	fs5, (30 * SZREG + MCONTEXT_FPREGS)(v0)
111
112# endif /* _MIPS_SIM != _ABI64 */
113
114	lw	v1, MCONTEXT_FPC_CSR(v0)
115	ctc1	v1, fcr31
116#endif /* __mips_hard_float */
117
118	/* Note the contents of argument registers will be random
119	   unless makecontext() has been called.  */
120	REG_L	a0, (MCONTEXT_GREGOFF + 4 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
121	REG_L	a1, (MCONTEXT_GREGOFF + 5 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
122	REG_L	a2, (MCONTEXT_GREGOFF + 6 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
123	REG_L	a3, (MCONTEXT_GREGOFF + 7 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
124#if _MIPS_SIM != _ABIO32
125	REG_L	a4, (MCONTEXT_GREGOFF + 8 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
126	REG_L	a5, (MCONTEXT_GREGOFF + 9 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
127	REG_L	a6, (MCONTEXT_GREGOFF + 10 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
128	REG_L	a7, (MCONTEXT_GREGOFF + 11 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
129#endif
130
131	REG_L	s0, (MCONTEXT_GREGOFF + 16 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
132	REG_L	s1, (MCONTEXT_GREGOFF + 17 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
133	REG_L	s2, (MCONTEXT_GREGOFF + 18 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
134	REG_L	s3, (MCONTEXT_GREGOFF + 19 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
135	REG_L	s4, (MCONTEXT_GREGOFF + 20 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
136	REG_L	s5, (MCONTEXT_GREGOFF + 21 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
137	REG_L	s6, (MCONTEXT_GREGOFF + 22 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
138	REG_L	s7, (MCONTEXT_GREGOFF + 23 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
139#if ! defined (__PIC__) || _MIPS_SIM != _ABIO32
140	REG_L	gp, (MCONTEXT_GREGOFF + 28 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
141#endif
142	REG_L	sp, (MCONTEXT_GREGOFF + 29 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
143	REG_L	fp, (MCONTEXT_GREGOFF + 30 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
144	REG_L	ra, (MCONTEXT_GREGOFF + 31 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(v0)
145	REG_L	t9, (MCONTEXT_GREGOFF + MCONTEXT_PC)(v0)
146
147	move	v0, zero
148	jr	t9
149
15099:
151#ifdef __PIC__
152	PTR_LA	t9, JUMPTARGET (__syscall_error)
153	RESTORE_GP64_STACK
154	PTR_ADDIU sp, FRAMESZ
155	cfi_adjust_cfa_offset (-FRAMESZ)
156	jr	t9
157
158#else  /* ! __PIC__ */
159
160	j	JUMPTARGET (__syscall_error)
161#endif /* ! __PIC__ */
162PSEUDO_END (__setcontext)
163
164weak_alias (__setcontext, setcontext)
165