1/* Save and set current context.  C-SKY ABIV2 version.
2   Copyright (C) 2018-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
21#include "ucontext_i.h"
22
23/* int swapcontext (ucontext_t *oucp, const ucontext_t *ucp) */
24
25ENTRY (swapcontext)
26	/* Save params and lr.  */
27	subi	sp, 16
28	stw	a0, (sp, 0)
29	stw	a1, (sp, 4)
30	stw	r15, (sp, 8)
31
32	/* Call getcontext.  */
33#ifdef __PIC__
34	grs	t1, .Lgetpc1
35.Lgetpc1:
36	lrw	t0, .Lgetpc1@GOTPC
37	addu	t1, t1, t0
38	lrw	t0, __getcontext@GOT
39	ldr.w	t0, (t1, t0 << 0)
40	jsr	t0
41#else /* !__PIC__ */
42	jsri	__getcontext
43#endif
44
45	mov	a3, a0	/* Save return value.  */
46
47	/* Restore params and lr.  */
48	ldw	a0, (sp, 0)
49	ldw	a1, (sp, 4)
50	ldw	r15, (sp, 8)
51	addi	sp, 16
52
53	cmpnei	a3, 0
54	bt	error_exit
55
56	/* Fix up LR and the PC.  */
57	stw	sp, (a0, MCONTEXT_CSKY_SP)
58	stw	r15, (a0, MCONTEXT_CSKY_LR)
59	stw	r15, (a0, MCONTEXT_CSKY_PC)
60
61	/* Set setcontext's arg.  */
62	mov	a0, a1
63
64#ifdef __PIC__
65	grs	t1, .Lgetpc2
66.Lgetpc2:
67	lrw	a3, .Lgetpc2@GOTPC
68	addu	t1, t1, a3
69	lrw	a3, __setcontext@GOT
70	ldr.w	a3, (t1, a3 << 0)
71#else /* __PIC__ */
72	lrw	a3, __setcontext
73#endif /* __PIC__ */
74	jmp	a3
75
76error_exit:
77	jmp	r15
78END (swapcontext)
79