xref: /freebsd/lib/libc/riscv/gen/_setjmp.S (revision 2008043f386721d58158e37e0d7e50df8095942d)
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#include <machine/setjmp.h>
37
38ENTRY(_setjmp)
39	/* Store the magic value and stack pointer */
40	ld	t0, .Lmagic
41	sd	t0, (0 * 8)(a0)
42	sd	sp, (1 * 8)(a0)
43	addi	a0, a0, (2 * 8)
44
45	/* Store the general purpose registers and ra */
46	sd	s0, (0 * 8)(a0)
47	sd	s1, (1 * 8)(a0)
48	sd	s2, (2 * 8)(a0)
49	sd	s3, (3 * 8)(a0)
50	sd	s4, (4 * 8)(a0)
51	sd	s5, (5 * 8)(a0)
52	sd	s6, (6 * 8)(a0)
53	sd	s7, (7 * 8)(a0)
54	sd	s8, (8 * 8)(a0)
55	sd	s9, (9 * 8)(a0)
56	sd	s10, (10 * 8)(a0)
57	sd	s11, (11 * 8)(a0)
58	sd	ra, (12 * 8)(a0)
59	addi	a0, a0, (13 * 8)
60
61#if !defined(_STANDALONE) && defined(__riscv_float_abi_double)
62	/* Store the fpe registers */
63	fsd	fs0, (0 * 8)(a0)
64	fsd	fs1, (1 * 8)(a0)
65	fsd	fs2, (2 * 8)(a0)
66	fsd	fs3, (3 * 8)(a0)
67	fsd	fs4, (4 * 8)(a0)
68	fsd	fs5, (5 * 8)(a0)
69	fsd	fs6, (6 * 8)(a0)
70	fsd	fs7, (7 * 8)(a0)
71	fsd	fs8, (8 * 8)(a0)
72	fsd	fs9, (9 * 8)(a0)
73	fsd	fs10, (10 * 8)(a0)
74	fsd	fs11, (11 * 8)(a0)
75	addi	a0, a0, (12 * 8)
76#endif
77
78	/* Return value */
79	li	a0, 0
80	ret
81	.align	3
82.Lmagic:
83	.quad	_JB_MAGIC__SETJMP
84END(_setjmp)
85
86ENTRY(_longjmp)
87	/* Check the magic value */
88	ld	t0, 0(a0)
89	ld	t1, .Lmagic
90	bne	t0, t1, botch
91
92	/* Restore the stack pointer */
93	ld	t0, 8(a0)
94	mv	sp, t0
95	addi	a0, a0, (2 * 8)
96
97	/* Restore the general purpose registers and ra */
98	ld	s0, (0 * 8)(a0)
99	ld	s1, (1 * 8)(a0)
100	ld	s2, (2 * 8)(a0)
101	ld	s3, (3 * 8)(a0)
102	ld	s4, (4 * 8)(a0)
103	ld	s5, (5 * 8)(a0)
104	ld	s6, (6 * 8)(a0)
105	ld	s7, (7 * 8)(a0)
106	ld	s8, (8 * 8)(a0)
107	ld	s9, (9 * 8)(a0)
108	ld	s10, (10 * 8)(a0)
109	ld	s11, (11 * 8)(a0)
110	ld	ra, (12 * 8)(a0)
111	addi	a0, a0, (13 * 8)
112
113#if !defined(_STANDALONE) && defined(__riscv_float_abi_double)
114	/* Restore the fpe registers */
115	fld	fs0, (0 * 8)(a0)
116	fld	fs1, (1 * 8)(a0)
117	fld	fs2, (2 * 8)(a0)
118	fld	fs3, (3 * 8)(a0)
119	fld	fs4, (4 * 8)(a0)
120	fld	fs5, (5 * 8)(a0)
121	fld	fs6, (6 * 8)(a0)
122	fld	fs7, (7 * 8)(a0)
123	fld	fs8, (8 * 8)(a0)
124	fld	fs9, (9 * 8)(a0)
125	fld	fs10, (10 * 8)(a0)
126	fld	fs11, (11 * 8)(a0)
127	addi	a0, a0, (12 * 8)
128#endif
129
130	/* Load the return value */
131	mv	a0, a1
132	bnez	a1, 1f
133	li	a0, 1
1341:
135	ret
136
137botch:
138#ifdef _STANDALONE
139	j	botch
140#else
141	call	_C_LABEL(longjmperror)
142	call	_C_LABEL(abort)
143#endif
144END(_longjmp)
145