1/* Save current context and install the given one.
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 <sys/trap.h>
21
22#include "ucontext_i.h"
23
24
25/* int __swapcontext (ucontext_t *oucp, const ucontext_t *ucp);
26
27  Saves the machine context in oucp such that when it is activated,
28  it appears as if __swapcontext() returned again, restores the
29  machine context in ucp and thereby resumes execution in that
30  context.
31
32  This implementation is intended to be used for *synchronous* context
33  switches only.  Therefore, it does not have to save anything
34  other than the PRESERVED state.  */
35
36ENTRY(__swapcontext)
37	save	%sp, -112, %sp
38	ta	ST_FLUSH_WINDOWS
39	st	%g0, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_PSR]
40	add	%i7, 8, %o0
41	st	%o0, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_PC]
42	add	%o0, 4, %o0
43	st	%o0, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_NPC]
44	rd	%y, %o1
45	st	%o1, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_Y]
46	st	%g1, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_G1]
47	st	%g2, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_G2]
48	st	%g3, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_G3]
49	st	%g4, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_G4]
50	st	%g5, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_G5]
51	st	%g6, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_G6]
52	st	%g7, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_G7]
53	st	%g0, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O0]
54	st	%i1, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O1]
55	st	%i2, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O2]
56	st	%i3, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O3]
57	st	%i4, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O4]
58	st	%i5, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O5]
59	st	%i6, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O6]
60	st	%i7, [%i0 + UC_MCONTEXT + MC_GREGS + GREG_O7]
61	st	%g0, [%i0 + UC_MCONTEXT + MC_GWINS]
62	stb	%g0, [%i0 + UC_MCONTEXT + MC_FPREGS + FPU_EN]
63	st	%g0, [%i0 + UC_MCONTEXT + MC_XRS + XRS_ID]
64	st	%g0, [%i0 + UC_MCONTEXT + MC_XRS + XRS_PTR]
65
66	mov	SIG_SETMASK, %o0
67	add	%i1, UC_SIGMASK, %o1
68	add	%i0, UC_SIGMASK, %o2
69	mov	8, %o3
70	mov	__NR_rt_sigprocmask, %g1
71	ta	0x10
72
73	mov	%i1, %i0
74	ldub	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_EN], %g1
75	cmp	%g1, 0
76	be	1f
77	 nop
78	ld	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_FSR], %fsr
79	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D0], %f0
80	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D2], %f2
81	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D4], %f4
82	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D6], %f6
83	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D8], %f8
84	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D10], %f10
85	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D12], %f12
86	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D14], %f14
87	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D16], %f16
88	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D18], %f18
89	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D20], %f20
90	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D22], %f22
91	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D24], %f24
92	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D26], %f26
93	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D28], %f28
94	ldd	[%i0 + UC_MCONTEXT + MC_FPREGS + FPU_D30], %f30
951:
96	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_Y], %g1
97	wr	%g1, 0x0, %y
98	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_G2], %g2
99	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_G3], %g3
100	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_G4], %g4
101	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_G5], %g5
102	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_G6], %g6
103	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_G7], %g7
104	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_O1], %i1
105	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_O2], %i2
106	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_O3], %i3
107	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_O4], %i4
108	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_O5], %i5
109	ld	[%i0 + UC_MCONTEXT + MC_GREGS + GREG_O6], %i6
110	restore
111	ld	[%o0 + UC_MCONTEXT + MC_GREGS + GREG_O7], %o7
112	ld	[%o0 + UC_MCONTEXT + MC_GREGS + GREG_PC], %g1
113	jmpl	%g1, %g0
114	 ld	[%o0 + UC_MCONTEXT + MC_GREGS + GREG_O0], %o0
115END(__swapcontext)
116
117weak_alias (__swapcontext, swapcontext)
118