1/* Save current context and install the given one.
2   Copyright (C) 2005-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
21#include "ucontext_i.h"
22
23/*  int __swapcontext (ucontext_t *ouc, const ucontext_t *uc);  */
24
25	.text
26	.align  5
27ENTRY(__swapcontext)
28
29	/* Return value of getcontext.  R0 is the only register whose
30	   value is not preserved.  */
31	mov	#0, r0
32	mov.l	r0, @(oR0,r4)
33	mov.l	r1, @(oR1,r4)
34	mov.l	r2, @(oR2,r4)
35	mov.l	r3, @(oR3,r4)
36	mov.l	r4, @(oR4,r4)
37	mov.l	r5, @(oR5,r4)
38	mov.l	r6, @(oR6,r4)
39	mov.l	r7, @(oR7,r4)
40	mov	r4, r0
41	add	#(oMACL+4), r0
42	sts.l	macl, @-r0
43	sts.l	mach, @-r0
44	stc.l	gbr, @-r0
45
46	/* Save T flag to SR.  */
47	movt	r1
48	mov.l	r1, @-r0
49	sts.l	pr, @-r0
50
51	/* The return address of getcontext is the restart pc.  */
52	sts.l	pr, @-r0
53
54	mov.l	r15, @-r0
55	mov.l	r14, @-r0
56	mov.l	r13, @-r0
57	mov.l	r12, @-r0
58	mov.l	r11, @-r0
59	mov.l	r10, @-r0
60	mov.l	r9, @-r0
61	mov.l	r8, @-r0
62
63	mov	r5, r8
64
65	/* sigprocmask (SIG_SETMASK, &uc->uc_sigmask, &ouc->uc_sigmask).  */
66	add	#oSIGMASK, r5
67	mov	r4, r6
68	add	#oSIGMASK, r6
69	mov	#SIG_SETMASK, r4
70	mov	#+SYS_ify(sigprocmask), r3
71	trapa	#0x13
72	mov     r0, r1
73	mov	#-12, r2
74	shad	r2, r1
75	not	r1, r1			// r1=0 means r0 = -1 to -4095
76	tst	r1, r1			// i.e. error in linux
77	bf	.Lswapcontext_restore
78.Lsyscall_error:
79	SYSCALL_ERROR_HANDLER
80.Lpseudo_end:
81	rts
82	 nop
83.Lswapcontext_restore:
84	mov	r8, r0
85	add	#(oPC), r0
86	mov.l	@r0+, r2
87	lds.l	@r0+, pr
88
89	/* Restore T frag.  */
90	mov.l	@r0+, r1
91	shlr	r1
92	/* Skip GBR which is used for thread pointer.  */
93	add	#4, r0
94
95	lds.l	@r0+, mach
96	lds.l	@r0+, macl
97
98	mov	r8, r0
99	add	#(oR9), r0
100	mov.l	@r0+, r9
101	mov.l	@r0+, r10
102	mov.l	@r0+, r11
103	mov.l	@r0+, r12
104	mov.l	@r0+, r13
105	mov.l	@r0+, r14
106	mov.l	@r0+, r15
107
108	mov	r8, r0
109	mov.l	@(oR0,r0), r1
110	mov.l	r1, @-r15
111	cfi_adjust_cfa_offset(4)
112	cfi_rel_offset (r1, 0)
113	mov.l	r2, @-r15
114	cfi_adjust_cfa_offset(4)
115	cfi_rel_offset (r2, 0)
116
117	mov.l	@(oR1,r0), r1
118	mov.l	@(oR2,r0), r2
119	mov.l	@(oR3,r0), r3
120	mov.l	@(oR4,r0), r4
121	mov.l	@(oR5,r0), r5
122	mov.l	@(oR6,r0), r6
123	mov.l	@(oR7,r0), r7
124	mov.l	@(oR8,r0), r8
125	mov.l	@r15+, r0
126	cfi_adjust_cfa_offset(-4)
127	jmp	@r0
128	 mov.l	@r15+, r0
129
130PSEUDO_END(__swapcontext)
131
132weak_alias (__swapcontext, swapcontext)
133