1/* Save current context.
2   Copyright (C) 2015-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 "ucontext_i.h"
21
22/* int getcontext (ucontext_t *ucp)
23
24   Returns 0 on success -1 and errno on failure.
25 */
26	.text
27ENTRY(__getcontext)
28	stw	r16, (UCONTEXT_MCONTEXT + 16*4)(r4)
29	stw	r17, (UCONTEXT_MCONTEXT + 17*4)(r4)
30	stw	r18, (UCONTEXT_MCONTEXT + 18*4)(r4)
31	stw	r19, (UCONTEXT_MCONTEXT + 19*4)(r4)
32	stw	r20, (UCONTEXT_MCONTEXT + 20*4)(r4)
33	stw	r21, (UCONTEXT_MCONTEXT + 21*4)(r4)
34	stw	r22, (UCONTEXT_MCONTEXT + 22*4)(r4)
35	stw	ra,  (UCONTEXT_MCONTEXT + 24*4)(r4)
36	stw	fp,  (UCONTEXT_MCONTEXT + 25*4)(r4)
37	stw	gp,  (UCONTEXT_MCONTEXT + 26*4)(r4)
38	/* Store return address at place for EA.  */
39	stw	ra,  (UCONTEXT_MCONTEXT + 28*4)(r4)
40	stw	sp,  (UCONTEXT_MCONTEXT + 29*4)(r4)
41	/* Store zero for return success.  */
42	stw	zero, (UCONTEXT_MCONTEXT + 2*4)(r4)
43
44	/* Store value "1" at uc_flags to recognize as getcontext created.  */
45	movi	r2, 1
46	stw	r2, UCONTEXT_FLAGS(r4)
47
48	/* Store MCONTEXT_VERSION at first word of mcontext_t.  */
49	movi	r2, MCONTEXT_VERSION
50	stw	r2, UCONTEXT_MCONTEXT(r4)
51
52	/* Get signal mask.  */
53	/* rt_sigprocmask (SIG_BLOCK, NULL, &ucp->uc_sigmask, _NSIG8) */
54	movi	r7, _NSIG8
55	addi	r6, r4, UCONTEXT_SIGMASK
56	mov	r5, zero
57	movi	r4, SIG_BLOCK
58	movi	r2, SYS_ify (rt_sigprocmask)
59	trap
60	bne	r7, zero, SYSCALL_ERROR_LABEL
61
62	mov	r2, zero
63	ret
64
65PSEUDO_END(__getcontext)
66weak_alias(__getcontext, getcontext)
67