1/* Save 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 getcontext (ucontext_t *ucp) */
27
28	.text
29	.set	nomips16
30LOCALSZ = 0
31MASK = 0x00000000
32#ifdef __PIC__
33LOCALSZ = 1						/* save gp */
34# if _MIPS_SIM != _ABIO32
35MASK = 0x10000000
36# endif
37#endif
38FRAMESZ = ((LOCALSZ * SZREG) + ALSZ) & ALMASK
39GPOFF = FRAMESZ - (1 * SZREG)
40MCONTEXT_GREGSZ = 8
41#if _MIPS_SIM == _ABIO32 && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
42MCONTEXT_GREGOFF = 4
43#else
44MCONTEXT_GREGOFF = 0
45#endif
46
47NESTED (__getcontext, FRAMESZ, ra)
48	.mask	MASK, 0
49	.fmask	0x00000000, 0
50
51#ifdef __PIC__
52	SETUP_GP
53
54	move	a2, sp
55# define _SP a2
56
57# if _MIPS_SIM != _ABIO32
58	move	a3, gp
59#  define _GP a3
60# endif
61
62	PTR_ADDIU sp, -FRAMESZ
63	cfi_adjust_cfa_offset (FRAMESZ)
64	SETUP_GP64_STACK (GPOFF, __getcontext)
65	SAVE_GP (GPOFF)
66
67#else  /* ! __PIC__ */
68# define _SP sp
69# define _GP gp
70
71#endif /* ! __PIC__ */
72
73#ifdef PROF
74	.set	noat
75	move	AT, ra
76	jal	_mcount
77	.set	at
78#endif
79
80	REG_S	s0, (MCONTEXT_GREGOFF + 16 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
81	REG_S	s1, (MCONTEXT_GREGOFF + 17 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
82	REG_S	s2, (MCONTEXT_GREGOFF + 18 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
83	REG_S	s3, (MCONTEXT_GREGOFF + 19 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
84	REG_S	s4, (MCONTEXT_GREGOFF + 20 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
85	REG_S	s5, (MCONTEXT_GREGOFF + 21 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
86	REG_S	s6, (MCONTEXT_GREGOFF + 22 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
87	REG_S	s7, (MCONTEXT_GREGOFF + 23 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
88#if ! defined (__PIC__) || _MIPS_SIM != _ABIO32
89	REG_S	_GP, (MCONTEXT_GREGOFF + 28 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
90#endif
91	REG_S	_SP, (MCONTEXT_GREGOFF + 29 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
92	REG_S	fp, (MCONTEXT_GREGOFF + 30 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
93	REG_S	ra, (MCONTEXT_GREGOFF + 31 * MCONTEXT_GREGSZ + MCONTEXT_GREGS)(a0)
94	REG_S	ra, (MCONTEXT_GREGOFF + MCONTEXT_PC)(a0)
95
96#ifdef __mips_hard_float
97# if _MIPS_SIM == _ABI64
98	s.d	fs0, (24 * SZREG + MCONTEXT_FPREGS)(a0)
99	s.d	fs1, (25 * SZREG + MCONTEXT_FPREGS)(a0)
100	s.d	fs2, (26 * SZREG + MCONTEXT_FPREGS)(a0)
101	s.d	fs3, (27 * SZREG + MCONTEXT_FPREGS)(a0)
102	s.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(a0)
103	s.d	fs5, (29 * SZREG + MCONTEXT_FPREGS)(a0)
104	s.d	fs6, (30 * SZREG + MCONTEXT_FPREGS)(a0)
105	s.d	fs7, (31 * SZREG + MCONTEXT_FPREGS)(a0)
106
107# else  /* _MIPS_SIM != _ABI64 */
108	s.d	fs0, (20 * SZREG + MCONTEXT_FPREGS)(a0)
109	s.d	fs1, (22 * SZREG + MCONTEXT_FPREGS)(a0)
110	s.d	fs2, (24 * SZREG + MCONTEXT_FPREGS)(a0)
111	s.d	fs3, (26 * SZREG + MCONTEXT_FPREGS)(a0)
112	s.d	fs4, (28 * SZREG + MCONTEXT_FPREGS)(a0)
113	s.d	fs5, (30 * SZREG + MCONTEXT_FPREGS)(a0)
114
115# endif /* _MIPS_SIM != _ABI64 */
116
117	cfc1	v1, fcr31
118	sw	v1, MCONTEXT_FPC_CSR(a0)
119#endif /* __mips_hard_float */
120
121/* rt_sigprocmask (SIG_BLOCK, NULL, &ucp->uc_sigmask, _NSIG8) */
122	li	a3, _NSIG8
123	PTR_ADDU a2, a0, UCONTEXT_SIGMASK
124	move	a1, zero
125	li	a0, SIG_BLOCK
126
127	li	v0, SYS_ify (rt_sigprocmask)
128	syscall
129	bnez	a3, 99f
130	cfi_remember_state
131
132#ifdef __PIC__
133	RESTORE_GP64_STACK
134	PTR_ADDIU sp, FRAMESZ
135	cfi_adjust_cfa_offset (-FRAMESZ)
136#endif
137	move	v0, zero
138	jr	ra
139
14099:
141	cfi_restore_state
142#ifdef __PIC__
143	PTR_LA	t9, JUMPTARGET (__syscall_error)
144	RESTORE_GP64_STACK
145	PTR_ADDIU sp, FRAMESZ
146	cfi_adjust_cfa_offset (-FRAMESZ)
147	jr	t9
148
149#else  /* ! __PIC__ */
150
151	j	JUMPTARGET (__syscall_error)
152#endif /* ! __PIC__ */
153PSEUDO_END (__getcontext)
154
155weak_alias (__getcontext, getcontext)
156