1/* Set current context.  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 setcontext (const ucontext_t *ucp) */
23	.text
24ENTRY(__setcontext)
25	l.ori	r30, r3, 0
26
27	/* Restore signal mask.  */
28	/* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, NULL, _NSIG8) */
29	l.ori	r6, r0, _NSIG8
30	l.ori	r5, r0, 0
31	l.addi	r4, r3, UCONTEXT_SIGMASK
32	l.ori	r3, r0, SIG_SETMASK
33	l.ori	r11, r0, SYS_ify (rt_sigprocmask)
34	/* Do the syscall.  */
35	l.sys 1
36	 l.nop
37
38	/* if -4096 < ret < 0 holds, it's an error */
39	l.sfgeui r11, 0xf001
40	l.bf	1f
41	 l.nop
42
43	/* Restore argument registers, for the makecontext case.  */
44	l.lwz	r3, (UCONTEXT_MCONTEXT +  3*4)(r30)
45	l.lwz	r4, (UCONTEXT_MCONTEXT +  4*4)(r30)
46	l.lwz	r5, (UCONTEXT_MCONTEXT +  5*4)(r30)
47	l.lwz	r6, (UCONTEXT_MCONTEXT +  6*4)(r30)
48	l.lwz	r7, (UCONTEXT_MCONTEXT +  7*4)(r30)
49	l.lwz	r8, (UCONTEXT_MCONTEXT +  8*4)(r30)
50
51	/* Restore registers stored in getcontext.  */
52	l.lwz	r1,  (UCONTEXT_MCONTEXT +  1*4)(r30)
53	l.lwz	r2,  (UCONTEXT_MCONTEXT +  2*4)(r30)
54	l.lwz	r9,  (UCONTEXT_MCONTEXT +  9*4)(r30)
55	l.lwz	r10, (UCONTEXT_MCONTEXT + 10*4)(r30)
56	l.lwz	r11, (UCONTEXT_MCONTEXT + 11*4)(r30)
57	/* Restore r14-r30 even, callee saved registers.  */
58	l.lwz	r14, (UCONTEXT_MCONTEXT + 14*4)(r30)
59	l.lwz	r16, (UCONTEXT_MCONTEXT + 16*4)(r30)
60	l.lwz	r18, (UCONTEXT_MCONTEXT + 18*4)(r30)
61	l.lwz	r20, (UCONTEXT_MCONTEXT + 20*4)(r30)
62	l.lwz	r22, (UCONTEXT_MCONTEXT + 22*4)(r30)
63	l.lwz	r24, (UCONTEXT_MCONTEXT + 24*4)(r30)
64	l.lwz	r26, (UCONTEXT_MCONTEXT + 26*4)(r30)
65	l.lwz	r28, (UCONTEXT_MCONTEXT + 28*4)(r30)
66	l.lwz	r30, (UCONTEXT_MCONTEXT + 30*4)(r30)
67
68	l.jr	r11
69	 l.ori	r11, r0, 0
70
711:	l.j	__syscall_error
72	 l.ori	r3, r11, 0
73
74END (__setcontext)
75weak_alias (__setcontext, setcontext)
76
77	/* We add a NOP here because when the unwinder is looking for the
78	   enclosing function of the link register (r9) address FDE lookup will
79	   use '$r9 - 1' finding setcontext which is wrong.  This is because in
80	   makecontext we have set r9 to the start of &__startcontext.
81
82	   If this NOP did not exist the unwinder would repeatedly find
83	   __setcontext's FDE in an infinite loop.  Modifiying/deleting the below
84	   __startcontext's FDE has no help on this.  */
85	l.nop
86
87ENTRY(__startcontext)
88
89	l.ori	r3, r14, 0
90	l.sfeq	r3, r0
91	/* If uc_link is not 0 resume there, otherwise exit.  */
92	l.bnf	__setcontext
93	 l.nop
94
95#ifdef SHARED
96	/* Obtain a pointer to .got in r16 */
97	l.jal	0x8
98	 l.movhi r16, gotpchi(_GLOBAL_OFFSET_TABLE_-4)
99	l.ori	r16, r16, gotpclo(_GLOBAL_OFFSET_TABLE_+0)
100	l.add	r16, r16, r9
101	l.lwz	r16, got(exit)(r16)
102	l.jr	r16
103#else
104	l.j	exit
105#endif
106	 l.nop
107
108END(__startcontext)
109