xref: /netbsd-src/lib/libc/compat/arch/arm/gen/compat_setjmp.S (revision 95cc57f231a501104ab0612db1ef94c945ae88d0)
1/*	$NetBSD: compat_setjmp.S,v 1.5 2020/12/05 11:18:21 skrll Exp $	*/
2
3/*
4 * Copyright (c) 1997 Mark Brinicombe
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Mark Brinicombe
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
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
38/*
39 * C library -- setjmp, longjmp
40 *
41 *	longjmp(a,v)
42 * will generate a "return(v)" from the last call to
43 *	setjmp(a)
44 * by restoring registers from the stack.
45 * The previous signal state is restored.
46 */
47
48ENTRY(setjmp)
49	/* Block all signals and retrieve the old signal mask */
50	push	{r0, lr}
51	movs	r0, #0x00000000
52
53	bl	PLT_SYM(_C_LABEL(sigblock))
54	mov	r1, r0
55
56	/* Store signal mask */
57	str	r1, [r0, #(_JB_SIGMASK * 4)]
58
59	ldr	r1, .Lsetjmp_magic
60	str	r1, [r0]
61
62	/* Store integer registers */
63	adds	r0, r0, #(_JB_REG_R4 * 4)
64#ifdef __thumb__
65#ifdef _ARM_ARCH_7
66	stmia	r0!, {r4-r12}
67	str	sp, [r0, #0]
68	str	lr, [r0, #4]
69#else
70	stmia	r0!, {r4-r7}
71	mov	r2, r8
72	mov	r3, r9
73	stmia	r0!, {r2-r3}
74	mov	r1, r10
75	mov	r2, r11
76	mov	r3, r12
77	stmia	r0!, {r1-r3}
78	mov	r2, sp
79	mov	r3, lr
80	stmia	r0!, {r2-r3}
81#endif
82#else
83	stmia	r0, {r4-lr}
84#endif
85	movs	r0, #0
86	pop	{r3, pc}
87END(setjmp)
88
89ENTRY(longjmp)
90	ldr	r2, .Lsetjmp_magic
91	ldr	r3, [r0]
92	cmp	r2, r3
93	bne	botch
94
95	/* Fetch signal mask */
96	ldr	r2, [r0, #(_JB_SIGMASK * 4)]
97
98	/* Set signal mask */
99	push	{r0, r1}	/* don't care about lr */
100
101	mov	r0, r2
102	bl	PLT_SYM(_C_LABEL(sigsetmask))
103
104	pop	{r0, r1}
105
106	/* Restore integer registers */
107	adds	r0, r0, #(_JB_REG_R4 * 4)
108#if !defined(__thumb__) || defined(_ARM_ARCH_7)
109	ldmia	r0!, {r4-r12}
110#else
111	ldmia	r0!, {r4-r7}
112	ldmia	r0!, {r2-r3}
113	mov	r8, r2
114	mov	r9, r3
115	ldmia	r0!, {r1-r3}
116	mov	r10, r2
117	mov	r11, r3
118	ldmia	r0!, {r2}
119	mov	r12, r2
120#endif
121
122	ldmia	r0!, {r2-r3}	/* r2 = sp, r3 = lr */
123
124	/* Validate sp */
125	cmp	r2, #0
126	beq	botch
127	mov	sp, r2
128
129	/* Validate lr */
130	cmp	r3, #0
131	beq	botch
132	mov	lr, r3
133
134	/* Set return value */
135	mov	r0, r1
136	cmp	r0, #0
137#ifdef __thumb__
138	bne	1f
139	movs	r0, #1
1401:
141#else
142	moveq	r0, #1
143#endif
144	RET
145
146	/* validation failed, die die die. */
147botch:
148	bl	PLT_SYM(_C_LABEL(longjmperror))
149	bl	PLT_SYM(_C_LABEL(abort))
1502:	b	2b		/* Cannot get here */
151
152	.align	0
153.Lsetjmp_magic:
154	.word	_JB_MAGIC_SETJMP
155END(longjmp)
156