1/* Set current 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 setcontext (const ucontext_t *ucp) */
23	.text
24ENTRY(__setcontext)
25	ldw	r5, UCONTEXT_FLAGS(r4)
26	movi	r6, 1
27	bne	r5, r6, .Lsigreturn
28
29	mov	r10, r4
30
31	/* Restore signal mask.  */
32	/* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, NULL, _NSIG8) */
33	movi	r7, _NSIG8
34	addi	r5, r4, UCONTEXT_SIGMASK
35	mov	r6, zero
36	movi	r4, SIG_SETMASK
37	movi	r2, SYS_ify (rt_sigprocmask)
38	trap
39	bne	r7, zero, SYSCALL_ERROR_LABEL
40
41	/* Restore argument registers, for the makecontext() case.  */
42	ldw	r4,   (UCONTEXT_MCONTEXT + 4*4)(r10)
43	ldw	r5,   (UCONTEXT_MCONTEXT + 5*4)(r10)
44	ldw	r6,   (UCONTEXT_MCONTEXT + 6*4)(r10)
45	ldw	r7,   (UCONTEXT_MCONTEXT + 7*4)(r10)
46
47	ldw	r16, (UCONTEXT_MCONTEXT + 16*4)(r10)
48	ldw	r17, (UCONTEXT_MCONTEXT + 17*4)(r10)
49	ldw	r18, (UCONTEXT_MCONTEXT + 18*4)(r10)
50	ldw	r19, (UCONTEXT_MCONTEXT + 19*4)(r10)
51	ldw	r20, (UCONTEXT_MCONTEXT + 20*4)(r10)
52	ldw	r21, (UCONTEXT_MCONTEXT + 21*4)(r10)
53	ldw	r22, (UCONTEXT_MCONTEXT + 22*4)(r10)
54	ldw	ra,  (UCONTEXT_MCONTEXT + 24*4)(r10)
55	ldw	fp,  (UCONTEXT_MCONTEXT + 25*4)(r10)
56	ldw	gp,  (UCONTEXT_MCONTEXT + 26*4)(r10)
57	/* Load address to continue execution.  */
58	ldw	r3,  (UCONTEXT_MCONTEXT + 28*4)(r10)
59	ldw	sp,  (UCONTEXT_MCONTEXT + 29*4)(r10)
60
61	mov	r2, zero
62	jmp	r3
63
64.Lsigreturn:
65	addi	sp, sp, -RT_SIGFRAME_SIZE
66	cfi_adjust_cfa_offset (RT_SIGFRAME_SIZE)
67
68	addi	r2, sp, RT_SIGFRAME_UCONTEXT
69	movi	r3, UCONTEXT_SIZE-4
701:
71	add	r6, r4, r3
72	ldw	r5, 0(r6)
73	add	r7, r2, r3
74	addi	r3, r3, -4
75	stw	r5, 0(r7)
76	bgt	r3, zero, 1b
77
78	movi	r2, SYS_ify (rt_sigreturn)
79	trap
80
81	addi	sp, sp, RT_SIGFRAME_SIZE
82	cfi_adjust_cfa_offset (-RT_SIGFRAME_SIZE)
83	br	SYSCALL_ERROR_LABEL
84
85PSEUDO_END (__setcontext)
86weak_alias (__setcontext, setcontext)
87
88	/* We add an NOP here to separate between __setcontext/__startcontext.
89	   The wanted behavior that happens is: when unwinding from a function
90	   called inside a makecontext() context, FDE lookup will use
91	   '&__startcontext - 1', then returns NULL for no FDE found,
92	   and immediately ends the unwind, in a normal fashion.
93
94	   If this NOP word does not exist, FDE lookup just repeatedly finds
95	   __setcontext's FDE in an infinite loop, due to the convention of
96	   using 'address - 1' for FDE lookup. Modifiying/deleting the below
97	   __startcontext's FDE has no help on this.  */
98	nop
99
100ENTRY(__startcontext)
101	mov	r4, r16
102	bne	r4, zero, __setcontext
103
104	/* If uc_link == zero, call exit.  */
105#ifdef PIC
106	nextpc	r22
1071:	movhi	r8, %hiadj(_gp_got - 1b)
108	addi	r8, r8, %lo(_gp_got - 1b)
109	add	r22, r22, r8
110	ldw	r8, %call(HIDDEN_JUMPTARGET(exit))(r22)
111	jmp	r8
112#else
113	jmpi	exit
114#endif
115END(__startcontext)
116