xref: /freebsd/lib/libc/riscv/gen/setjmp.S (revision ce6a89e27cd190313be39bb479880aeda4778436)
1/*-
2 * Copyright (c) 2015-2016 Ruslan Bukin <br@bsdpad.com>
3 * All rights reserved.
4 *
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
8 *
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <machine/asm.h>
36__FBSDID("$FreeBSD$");
37
38#include <machine/setjmp.h>
39
40ENTRY(setjmp)
41	addi	sp, sp, -(2 * 8)
42	sd	a0, 0(sp)
43	sd	ra, 8(sp)
44
45	/* Store the signal mask */
46	addi	a2, a0, (_JB_SIGMASK * 8)	/* oset */
47	li	a1, 0				/* set */
48	li	a0, 1				/* SIG_BLOCK */
49	call	_C_LABEL(sigprocmask)
50
51	ld	a0, 0(sp)
52	ld	ra, 8(sp)
53	addi	sp, sp, (2 * 8)
54
55	/* Store the magic value and stack pointer */
56	ld	t0, .Lmagic
57	sd	t0, (0 * 8)(a0)
58	sd	sp, (1 * 8)(a0)
59	addi	a0, a0, (2 * 8)
60
61	/* Store the general purpose registers and ra */
62	sd	s0, (0 * 8)(a0)
63	sd	s1, (1 * 8)(a0)
64	sd	s2, (2 * 8)(a0)
65	sd	s3, (3 * 8)(a0)
66	sd	s4, (4 * 8)(a0)
67	sd	s5, (5 * 8)(a0)
68	sd	s6, (6 * 8)(a0)
69	sd	s7, (7 * 8)(a0)
70	sd	s8, (8 * 8)(a0)
71	sd	s9, (9 * 8)(a0)
72	sd	s10, (10 * 8)(a0)
73	sd	s11, (11 * 8)(a0)
74	sd	ra, (12 * 8)(a0)
75	addi	a0, a0, (13 * 8)
76
77#ifdef __riscv_float_abi_double
78	/* Store the fpe registers */
79	fsd	fs0, (0 * 8)(a0)
80	fsd	fs1, (1 * 8)(a0)
81	fsd	fs2, (2 * 8)(a0)
82	fsd	fs3, (3 * 8)(a0)
83	fsd	fs4, (4 * 8)(a0)
84	fsd	fs5, (5 * 8)(a0)
85	fsd	fs6, (6 * 8)(a0)
86	fsd	fs7, (7 * 8)(a0)
87	fsd	fs8, (8 * 8)(a0)
88	fsd	fs9, (9 * 8)(a0)
89	fsd	fs10, (10 * 8)(a0)
90	fsd	fs11, (11 * 8)(a0)
91	addi	a0, a0, (12 * 8)
92#endif
93
94	/* Return value */
95	li	a0, 0
96	ret
97	.align	3
98.Lmagic:
99	.quad	_JB_MAGIC_SETJMP
100END(setjmp)
101
102ENTRY(longjmp)
103	addi	sp, sp, -(4 * 8)
104	sd	a0, (0 * 8)(sp)
105	sd	ra, (1 * 8)(sp)
106	sd	a1, (2 * 8)(sp)
107
108	/* Restore the signal mask */
109	li	a2, 0				/* oset */
110	addi	a1, a0, (_JB_SIGMASK * 8)	/* set */
111	li	a0, 3				/* SIG_BLOCK */
112	call	_C_LABEL(sigprocmask)
113
114	ld	a1, (2 * 8)(sp)
115	ld	ra, (1 * 8)(sp)
116	ld	a0, (0 * 8)(sp)
117	addi	sp, sp, (4 * 8)
118
119	/* Check the magic value */
120	ld	t0, 0(a0)
121	ld	t1, .Lmagic
122	bne	t0, t1, botch
123
124	/* Restore the stack pointer */
125	ld	t0, 8(a0)
126	mv	sp, t0
127	addi	a0, a0, (2 * 8)
128
129	/* Restore the general purpose registers and ra */
130	ld	s0, (0 * 8)(a0)
131	ld	s1, (1 * 8)(a0)
132	ld	s2, (2 * 8)(a0)
133	ld	s3, (3 * 8)(a0)
134	ld	s4, (4 * 8)(a0)
135	ld	s5, (5 * 8)(a0)
136	ld	s6, (6 * 8)(a0)
137	ld	s7, (7 * 8)(a0)
138	ld	s8, (8 * 8)(a0)
139	ld	s9, (9 * 8)(a0)
140	ld	s10, (10 * 8)(a0)
141	ld	s11, (11 * 8)(a0)
142	ld	ra, (12 * 8)(a0)
143	addi	a0, a0, (13 * 8)
144
145#ifdef __riscv_float_abi_double
146	/* Restore the fpe registers */
147	fld	fs0, (0 * 8)(a0)
148	fld	fs1, (1 * 8)(a0)
149	fld	fs2, (2 * 8)(a0)
150	fld	fs3, (3 * 8)(a0)
151	fld	fs4, (4 * 8)(a0)
152	fld	fs5, (5 * 8)(a0)
153	fld	fs6, (6 * 8)(a0)
154	fld	fs7, (7 * 8)(a0)
155	fld	fs8, (8 * 8)(a0)
156	fld	fs9, (9 * 8)(a0)
157	fld	fs10, (10 * 8)(a0)
158	fld	fs11, (11 * 8)(a0)
159	addi	a0, a0, (12 * 8)
160#endif
161
162	/* Load the return value */
163	mv	a0, a1
164	ret
165
166botch:
167	call	_C_LABEL(longjmperror)
168	call	_C_LABEL(abort)
169END(longjmp)
170