1/* Install given context.
2   Copyright (C) 2008-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 <libc-symbols.h>
21
22#include "ucontext_i.h"
23
24
25ENTRY(__setcontext)
26	/* Prologue */
27	stw	%r2, -20(%sp)
28	.cfi_offset 2, -20
29	stwm	%r3, 64(%sp)
30	.cfi_def_cfa_offset -64
31	.cfi_offset 3, 0
32#ifdef PIC
33	stw	%r19, -32(%sp)
34	.cfi_offset 19, 32
35#endif
36
37	/* Save ucp.  */
38	copy	%r26, %r3
39
40.Lagain:
41	/* Set the current signal mask.  */
42	/* sigprocmask(SIG_BLOCK, &ucp->uc_sigmask, NULL);  */
43	copy	%r0, %r24
44	ldo	oSIGMASK(%r3), %r25
45	bl	__sigprocmask, %r2
46	ldi	SIG_SETMASK, %r26
47
48	comib,<>,n 0,%ret0,.Lerror
49
50	/* Save %sp, %dp.  */
51	copy	%sp, %r4
52	copy	%dp, %r5
53	copy	%r19, %r6
54
55	/* Get the registers.  */
56	ldw	oR1(%r3), %r1
57	ldw	oR2(%r3), %r2
58	/* ldw	oR3(%r3), %r3 - used for ucp pointer.	*/
59	/* ldw	oR4(%r3), %r4 - used for original %sp.	*/
60	/* ldw	oR5(%r3), %r5 - used for %dp / %r27.	*/
61	/* ldw	oR6(%r3), %r6 - used for %r19.		*/
62	ldw	oR7(%r3), %r7
63	ldw	oR8(%r3), %r8
64	ldw	oR9(%r3), %r9
65	ldw	oR10(%r3), %r10
66	ldw	oR11(%r3), %r11
67	ldw	oR12(%r3), %r12
68	ldw	oR13(%r3), %r13
69	ldw	oR14(%r3), %r14
70	ldw	oR15(%r3), %r15
71	ldw	oR16(%r3), %r16
72	ldw	oR17(%r3), %r17
73	ldw	oR18(%r3), %r18
74	ldw	oR19(%r3), %r19
75	ldw	oR20(%r3), %r20
76	ldw	oR21(%r3), %r21
77	/* ldw	oR22(%r3), %r22 - dyncall arg.  */
78	ldw	oR23(%r3), %r23
79	ldw	oR24(%r3), %r24
80	ldw	oR25(%r3), %r25
81	ldw	oR26(%r3), %r26
82	ldw	oR27(%r3), %r27
83	ldw	oR28(%r3), %r28
84	ldw	oR29(%r3), %r29
85	ldw	oR30(%r3), %sp
86	/* ldw	oR31(%r3), %r31 - dyncall scratch register */
87
88	/* Restore floating-point registers.  */
89	ldo	 oFPREGS31(%r3), %r22
90	fldds	  0(%r22), %fr31
91	fldds,mb -8(%r22), %fr30
92	fldds,mb -8(%r22), %fr29
93	fldds,mb -8(%r22), %fr28
94	fldds,mb -8(%r22), %fr27
95	fldds,mb -8(%r22), %fr26
96	fldds,mb -8(%r22), %fr25
97	fldds,mb -8(%r22), %fr24
98	fldds,mb -8(%r22), %fr23
99	fldds,mb -8(%r22), %fr22
100	fldds,mb -8(%r22), %fr21
101	fldds,mb -8(%r22), %fr20
102	fldds,mb -8(%r22), %fr19
103	fldds,mb -8(%r22), %fr18
104	fldds,mb -8(%r22), %fr17
105	fldds,mb -8(%r22), %fr16
106	fldds,mb -8(%r22), %fr15
107	fldds,mb -8(%r22), %fr14
108	fldds,mb -8(%r22), %fr13
109	fldds,mb -8(%r22), %fr12
110	fldds,mb -8(%r22), %fr11
111	fldds,mb -8(%r22), %fr10
112	fldds,mb -8(%r22), %fr9
113	fldds,mb -8(%r22), %fr8
114	fldds,mb -8(%r22), %fr7
115	fldds,mb -8(%r22), %fr6
116	fldds,mb -8(%r22), %fr5
117	fldds,mb -8(%r22), %fr4
118	fldds,mb -8(%r22), %fr3
119	fldds,mb -8(%r22), %fr2
120	fldds,mb -8(%r22), %fr1
121	fldds,mb -8(%r22), %fr0
122
123	/* Do not load oSS_SP into %sp. The value of oSS_SP indicates
124	   the start of the user allocated stack, but not the sp that
125	   should be used by the new context. In fact makecontext
126	   will create a frame, and adjust sp as required. We do not
127	   support calling getcontext and modifying ss_sp without
128	   a call to makecontext to synchronize ss_sp into the machine
129	   context.  */
130
131	/* Call external function.  */
132	copy	%r2, %r22
133	bl	$$dyncall, %r31
134	copy	%r31, %r2
135
136	/* We return here. Get new ucp in %r3, reload %sp.  */
137	ldw	oUC_LINK(%r3), %r3
138	copy	%r4, %sp
139	copy	%r5, %dp
140	copy	%r6, %r19
141
142	/* Continue until ucp == NULL.  */
143	comib,<> 0,%r3,.Lagain
144	nop
145
146	/* No further context available. Exit now.  */
147	bl	HIDDEN_JUMPTARGET(exit), %r2
148	ldi	0, %r26
149
150
151.Lerror:
152	/* Epilogue */
153	ldw	-84(%r30), %r2
154#ifdef PIC
155	ldw	-32(%r30), %r19
156#endif
157	bv	%r0(%r2)
158	ldwm	-64(%r30), %r3
159L(pseudo_end):
160PSEUDO_END(__setcontext)
161
162weak_alias(__setcontext, setcontext)
163