xref: /netbsd-src/lib/libc/compat/arch/arm/gen/compat_setjmp.S (revision 95cc57f231a501104ab0612db1ef94c945ae88d0)
1*95cc57f2Sskrll/*	$NetBSD: compat_setjmp.S,v 1.5 2020/12/05 11:18:21 skrll Exp $	*/
2b638db2cSchristos
3b638db2cSchristos/*
4b638db2cSchristos * Copyright (c) 1997 Mark Brinicombe
5b638db2cSchristos * All rights reserved.
6b638db2cSchristos *
7b638db2cSchristos * Redistribution and use in source and binary forms, with or without
8b638db2cSchristos * modification, are permitted provided that the following conditions
9b638db2cSchristos * are met:
10b638db2cSchristos * 1. Redistributions of source code must retain the above copyright
11b638db2cSchristos *    notice, this list of conditions and the following disclaimer.
12b638db2cSchristos * 2. Redistributions in binary form must reproduce the above copyright
13b638db2cSchristos *    notice, this list of conditions and the following disclaimer in the
14b638db2cSchristos *    documentation and/or other materials provided with the distribution.
15b638db2cSchristos * 3. All advertising materials mentioning features or use of this software
16b638db2cSchristos *    must display the following acknowledgement:
17b638db2cSchristos *	This product includes software developed by Mark Brinicombe
18b638db2cSchristos * 4. Neither the name of the University nor the names of its contributors
19b638db2cSchristos *    may be used to endorse or promote products derived from this software
20b638db2cSchristos *    without specific prior written permission.
21b638db2cSchristos *
22b638db2cSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23b638db2cSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b638db2cSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b638db2cSchristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26b638db2cSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b638db2cSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b638db2cSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b638db2cSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b638db2cSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b638db2cSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b638db2cSchristos * SUCH DAMAGE.
33b638db2cSchristos */
34b638db2cSchristos
35b638db2cSchristos#include <machine/asm.h>
36b638db2cSchristos#include <machine/setjmp.h>
37b638db2cSchristos
38b638db2cSchristos/*
39b638db2cSchristos * C library -- setjmp, longjmp
40b638db2cSchristos *
41b638db2cSchristos *	longjmp(a,v)
42b638db2cSchristos * will generate a "return(v)" from the last call to
43b638db2cSchristos *	setjmp(a)
44b638db2cSchristos * by restoring registers from the stack.
45b638db2cSchristos * The previous signal state is restored.
46b638db2cSchristos */
47b638db2cSchristos
48b638db2cSchristosENTRY(setjmp)
49b638db2cSchristos	/* Block all signals and retrieve the old signal mask */
502cf5e996Smatt	push	{r0, lr}
512cf5e996Smatt	movs	r0, #0x00000000
52b638db2cSchristos
53526051f6Sjoerg	bl	PLT_SYM(_C_LABEL(sigblock))
54b638db2cSchristos	mov	r1, r0
55b638db2cSchristos
56b638db2cSchristos	/* Store signal mask */
5760e85183Smatt	str	r1, [r0, #(_JB_SIGMASK * 4)]
58b638db2cSchristos
59b638db2cSchristos	ldr	r1, .Lsetjmp_magic
6060e85183Smatt	str	r1, [r0]
61b638db2cSchristos
62b638db2cSchristos	/* Store integer registers */
632cf5e996Smatt	adds	r0, r0, #(_JB_REG_R4 * 4)
642cf5e996Smatt#ifdef __thumb__
652cf5e996Smatt#ifdef _ARM_ARCH_7
662cf5e996Smatt	stmia	r0!, {r4-r12}
672cf5e996Smatt	str	sp, [r0, #0]
682cf5e996Smatt	str	lr, [r0, #4]
692cf5e996Smatt#else
702cf5e996Smatt	stmia	r0!, {r4-r7}
712cf5e996Smatt	mov	r2, r8
722cf5e996Smatt	mov	r3, r9
732cf5e996Smatt	stmia	r0!, {r2-r3}
742cf5e996Smatt	mov	r1, r10
752cf5e996Smatt	mov	r2, r11
762cf5e996Smatt	mov	r3, r12
772cf5e996Smatt	stmia	r0!, {r1-r3}
782cf5e996Smatt	mov	r2, sp
792cf5e996Smatt	mov	r3, lr
802cf5e996Smatt	stmia	r0!, {r2-r3}
812cf5e996Smatt#endif
822cf5e996Smatt#else
832cf5e996Smatt	stmia	r0, {r4-lr}
842cf5e996Smatt#endif
852cf5e996Smatt	movs	r0, #0
862cf5e996Smatt	pop	{r3, pc}
872cf5e996SmattEND(setjmp)
88b638db2cSchristos
89b638db2cSchristosENTRY(longjmp)
90b638db2cSchristos	ldr	r2, .Lsetjmp_magic
91b638db2cSchristos	ldr	r3, [r0]
922cf5e996Smatt	cmp	r2, r3
93b638db2cSchristos	bne	botch
94b638db2cSchristos
95b638db2cSchristos	/* Fetch signal mask */
9660e85183Smatt	ldr	r2, [r0, #(_JB_SIGMASK * 4)]
97b638db2cSchristos
98b638db2cSchristos	/* Set signal mask */
992cf5e996Smatt	push	{r0, r1}	/* don't care about lr */
100b638db2cSchristos
101b638db2cSchristos	mov	r0, r2
102526051f6Sjoerg	bl	PLT_SYM(_C_LABEL(sigsetmask))
103b638db2cSchristos
1042cf5e996Smatt	pop	{r0, r1}
105b638db2cSchristos
106b638db2cSchristos	/* Restore integer registers */
1072cf5e996Smatt	adds	r0, r0, #(_JB_REG_R4 * 4)
1082cf5e996Smatt#if !defined(__thumb__) || defined(_ARM_ARCH_7)
1092cf5e996Smatt	ldmia	r0!, {r4-r12}
1102cf5e996Smatt#else
1112cf5e996Smatt	ldmia	r0!, {r4-r7}
1122cf5e996Smatt	ldmia	r0!, {r2-r3}
1132cf5e996Smatt	mov	r8, r2
1142cf5e996Smatt	mov	r9, r3
1152cf5e996Smatt	ldmia	r0!, {r1-r3}
1162cf5e996Smatt	mov	r10, r2
1172cf5e996Smatt	mov	r11, r3
1182cf5e996Smatt	ldmia	r0!, {r2}
1192cf5e996Smatt	mov	r12, r2
1202cf5e996Smatt#endif
121b638db2cSchristos
1222cf5e996Smatt	ldmia	r0!, {r2-r3}	/* r2 = sp, r3 = lr */
1232cf5e996Smatt
1242cf5e996Smatt	/* Validate sp */
1252cf5e996Smatt	cmp	r2, #0
126b638db2cSchristos	beq	botch
1272cf5e996Smatt	mov	sp, r2
1282cf5e996Smatt
1292cf5e996Smatt	/* Validate lr */
1302cf5e996Smatt	cmp	r3, #0
1312cf5e996Smatt	beq	botch
1322cf5e996Smatt	mov	lr, r3
133b638db2cSchristos
134b638db2cSchristos	/* Set return value */
135b638db2cSchristos	mov	r0, r1
1362cf5e996Smatt	cmp	r0, #0
1372cf5e996Smatt#ifdef __thumb__
1382cf5e996Smatt	bne	1f
1392cf5e996Smatt	movs	r0, #1
1402cf5e996Smatt1:
1412cf5e996Smatt#else
1422cf5e996Smatt	moveq	r0, #1
1432cf5e996Smatt#endif
144b638db2cSchristos	RET
145b638db2cSchristos
146b638db2cSchristos	/* validation failed, die die die. */
147b638db2cSchristosbotch:
148526051f6Sjoerg	bl	PLT_SYM(_C_LABEL(longjmperror))
149526051f6Sjoerg	bl	PLT_SYM(_C_LABEL(abort))
1502cf5e996Smatt2:	b	2b		/* Cannot get here */
1512cf5e996Smatt
1522cf5e996Smatt	.align	0
1532cf5e996Smatt.Lsetjmp_magic:
1542cf5e996Smatt	.word	_JB_MAGIC_SETJMP
1552cf5e996SmattEND(longjmp)
156