1/* Modify saved context.
2   Copyright (C) 2015-2021 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 swapcontext (ucontext_t *oucp, const ucontext_t *ucp) */
23	.text
24ENTRY(__swapcontext)
25
26	/* Same as getcontext().  */
27	stw	r16, (UCONTEXT_MCONTEXT + 16*4)(r4)
28	stw	r17, (UCONTEXT_MCONTEXT + 17*4)(r4)
29	stw	r18, (UCONTEXT_MCONTEXT + 18*4)(r4)
30	stw	r19, (UCONTEXT_MCONTEXT + 19*4)(r4)
31	stw	r20, (UCONTEXT_MCONTEXT + 20*4)(r4)
32	stw	r21, (UCONTEXT_MCONTEXT + 21*4)(r4)
33	stw	r22, (UCONTEXT_MCONTEXT + 22*4)(r4)
34	stw	ra,  (UCONTEXT_MCONTEXT + 24*4)(r4)
35	stw	fp,  (UCONTEXT_MCONTEXT + 25*4)(r4)
36	stw	gp,  (UCONTEXT_MCONTEXT + 26*4)(r4)
37	/* Store return address at place for EA.  */
38	stw	ra,  (UCONTEXT_MCONTEXT + 28*4)(r4)
39	stw	sp,  (UCONTEXT_MCONTEXT + 29*4)(r4)
40	/* Store zero for return success.  */
41	stw	zero, (UCONTEXT_MCONTEXT + 2*4)(r4)
42
43	/* Store value "1" at uc_flags to recognize as getcontext created.  */
44	movi	r2, 1
45	stw	r2, UCONTEXT_FLAGS(r4)
46
47	/* Store MCONTEXT_VERSION at first word of mcontext_t.  */
48	movi	r2, MCONTEXT_VERSION
49	stw	r2, UCONTEXT_MCONTEXT(r4)
50
51	/* Save ucp to non-argument syscall preserved register.  */
52	mov	r10, r5
53
54	/* Get signal mask.  */
55	/* rt_sigprocmask (SIG_BLOCK, NULL, &ucp->uc_sigmask, _NSIG8) */
56	movi	r7, _NSIG8
57	addi	r6, r4, UCONTEXT_SIGMASK
58	mov	r5, zero
59	movi	r4, SIG_BLOCK
60	movi	r2, SYS_ify (rt_sigprocmask)
61	trap
62	bne	r7, zero, SYSCALL_ERROR_LABEL
63
64
65	/* Same as setcontext().  */
66	ldw	r5, UCONTEXT_FLAGS(r10)
67	movi	r6, 1
68	bne	r5, r6, .Lsigreturn
69
70	/* Restore signal mask.  */
71	/* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, NULL, _NSIG8) */
72	movi	r7, _NSIG8
73	addi	r5, r10, UCONTEXT_SIGMASK
74	mov	r6, zero
75	movi	r4, SIG_SETMASK
76	movi	r2, SYS_ify (rt_sigprocmask)
77	trap
78	bne	r7, zero, SYSCALL_ERROR_LABEL
79
80	/* Restore argument registers, for the makecontext() case.  */
81	ldw	r4,   (UCONTEXT_MCONTEXT + 4*4)(r10)
82	ldw	r5,   (UCONTEXT_MCONTEXT + 5*4)(r10)
83	ldw	r6,   (UCONTEXT_MCONTEXT + 6*4)(r10)
84	ldw	r7,   (UCONTEXT_MCONTEXT + 7*4)(r10)
85
86	ldw	r16, (UCONTEXT_MCONTEXT + 16*4)(r10)
87	ldw	r17, (UCONTEXT_MCONTEXT + 17*4)(r10)
88	ldw	r18, (UCONTEXT_MCONTEXT + 18*4)(r10)
89	ldw	r19, (UCONTEXT_MCONTEXT + 19*4)(r10)
90	ldw	r20, (UCONTEXT_MCONTEXT + 20*4)(r10)
91	ldw	r21, (UCONTEXT_MCONTEXT + 21*4)(r10)
92	ldw	r22, (UCONTEXT_MCONTEXT + 22*4)(r10)
93	ldw	ra,  (UCONTEXT_MCONTEXT + 24*4)(r10)
94	ldw	fp,  (UCONTEXT_MCONTEXT + 25*4)(r10)
95	ldw	gp,  (UCONTEXT_MCONTEXT + 26*4)(r10)
96	/* Load address to continue execution.  */
97	ldw	r3,  (UCONTEXT_MCONTEXT + 28*4)(r10)
98	ldw	sp,  (UCONTEXT_MCONTEXT + 29*4)(r10)
99
100	mov	r2, zero
101	jmp	r3
102
103.Lsigreturn:
104	addi	sp, sp, -RT_SIGFRAME_SIZE
105	cfi_adjust_cfa_offset (RT_SIGFRAME_SIZE)
106
107	addi	r2, sp, RT_SIGFRAME_UCONTEXT
108	movi	r3, UCONTEXT_SIZE-4
1091:
110	add	r6, r4, r3
111	ldw	r5, 0(r6)
112	add	r7, r2, r3
113	addi	r3, r3, -4
114	stw	r5, 0(r7)
115	bgt	r3, zero, 1b
116
117	movi	r2, SYS_ify (rt_sigreturn)
118	trap
119
120	addi	sp, sp, RT_SIGFRAME_SIZE
121	cfi_adjust_cfa_offset (-RT_SIGFRAME_SIZE)
122	br	SYSCALL_ERROR_LABEL
123
124PSEUDO_END (__swapcontext)
125weak_alias (__swapcontext, swapcontext)
126