1/* Swap two contexts.  OpenRISC version.
2   Copyright (C) 2022 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	/* Store r1, the stack pointer.  */
28	l.sw	(UCONTEXT_MCONTEXT +  1*4)(r3), r1
29	/* Store r2, the frame pointer.  */
30	l.sw	(UCONTEXT_MCONTEXT +  2*4)(r3), r2
31	/* Store r9, the link register.  */
32	l.sw	(UCONTEXT_MCONTEXT +  9*4)(r3), r9
33	/* Store r9 to reg[11] too, as we need two links for makecontext.  */
34	l.sw	(UCONTEXT_MCONTEXT + 11*4)(r3), r9
35	/* Store r10, the TLS register.  */
36	l.sw	(UCONTEXT_MCONTEXT + 10*4)(r3), r10
37	/* Store r14-r30 even, callee saved registers.  */
38	l.sw	(UCONTEXT_MCONTEXT + 14*4)(r3), r14
39	l.sw	(UCONTEXT_MCONTEXT + 16*4)(r3), r16
40	l.sw	(UCONTEXT_MCONTEXT + 18*4)(r3), r18
41	l.sw	(UCONTEXT_MCONTEXT + 20*4)(r3), r20
42	l.sw	(UCONTEXT_MCONTEXT + 22*4)(r3), r22
43	l.sw	(UCONTEXT_MCONTEXT + 24*4)(r3), r24
44	l.sw	(UCONTEXT_MCONTEXT + 26*4)(r3), r26
45	l.sw	(UCONTEXT_MCONTEXT + 28*4)(r3), r28
46	l.sw	(UCONTEXT_MCONTEXT + 30*4)(r3), r30
47
48	/* Store ucp to non-argument syscall preserved register.  */
49	l.ori	r30, r4, 0
50
51	/* Get signal mask.  */
52	/* rt_sigprocmask (SIG_BLOCK, NULL, &ucp->uc_sigmask, _NSIG8) */
53	l.ori	r6, r0, _NSIG8
54	l.addi	r5, r3, UCONTEXT_SIGMASK
55	l.ori	r4, r0, 0
56	l.ori	r3, r0, SIG_BLOCK
57	l.ori	r11, r0, SYS_ify (rt_sigprocmask)
58	/* Do the syscall.  */
59	l.sys	1
60	 l.nop
61
62	/* if -4096 < ret < 0 holds, it's an error */
63	l.sfgeui r11, 0xf001
64	l.bf	1f
65	 l.nop
66
67	/* Same as setcontext.  */
68
69	/* Restore signal mask.  */
70	/* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, NULL, _NSIG8) */
71	l.ori	r6, r0, _NSIG8
72	l.ori	r5, r0, 0
73	l.addi	r4, r30, UCONTEXT_SIGMASK
74	l.ori	r3, r0, SIG_SETMASK
75	l.ori	r11, r0, SYS_ify (rt_sigprocmask)
76	/* Do the syscall.  */
77	l.sys 1
78	 l.nop
79
80	/* if -4096 < ret < 0 holds, it's an error */
81	l.sfgeui r11, 0xf001
82	l.bf	1f
83	 l.nop
84
85	/* Restore argument registers, for the makecontext case.  */
86	l.lwz	r3, (UCONTEXT_MCONTEXT +  3*4)(r30)
87	l.lwz	r4, (UCONTEXT_MCONTEXT +  4*4)(r30)
88	l.lwz	r5, (UCONTEXT_MCONTEXT +  5*4)(r30)
89	l.lwz	r6, (UCONTEXT_MCONTEXT +  6*4)(r30)
90	l.lwz	r7, (UCONTEXT_MCONTEXT +  7*4)(r30)
91	l.lwz	r8, (UCONTEXT_MCONTEXT +  8*4)(r30)
92
93	/* Restore registers stored in getcontext.  */
94	l.lwz	r1,  (UCONTEXT_MCONTEXT +  1*4)(r30)
95	l.lwz	r2,  (UCONTEXT_MCONTEXT +  2*4)(r30)
96	l.lwz	r9,  (UCONTEXT_MCONTEXT +  9*4)(r30)
97	l.lwz	r10, (UCONTEXT_MCONTEXT + 10*4)(r30)
98	l.lwz	r11, (UCONTEXT_MCONTEXT + 11*4)(r30)
99	l.lwz	r14, (UCONTEXT_MCONTEXT + 14*4)(r30)
100	l.lwz	r16, (UCONTEXT_MCONTEXT + 16*4)(r30)
101	l.lwz	r18, (UCONTEXT_MCONTEXT + 18*4)(r30)
102	l.lwz	r20, (UCONTEXT_MCONTEXT + 20*4)(r30)
103	l.lwz	r22, (UCONTEXT_MCONTEXT + 22*4)(r30)
104	l.lwz	r24, (UCONTEXT_MCONTEXT + 24*4)(r30)
105	l.lwz	r26, (UCONTEXT_MCONTEXT + 26*4)(r30)
106	l.lwz	r28, (UCONTEXT_MCONTEXT + 28*4)(r30)
107	l.lwz	r30, (UCONTEXT_MCONTEXT + 30*4)(r30)
108
109	l.jr	r11
110	 l.ori	r11, r0, 0
111
1121:	l.j	__syscall_error
113	 l.ori	r3, r11, 0
114
115END (__swapcontext)
116weak_alias (__swapcontext, swapcontext)
117