xref: /openbsd-src/lib/libc/arch/arm/gen/_setjmp.S (revision a8d2d9e857f3e0e5e619a6a7a1a692b947560973)
1*a8d2d9e8Sguenther/*	$OpenBSD: _setjmp.S,v 1.8 2022/05/25 17:32:36 guenther Exp $	*/
2d987040fSdrahn/*	$NetBSD: _setjmp.S,v 1.5 2003/04/05 23:08:51 bjh21 Exp $	*/
3d987040fSdrahn
4d987040fSdrahn/*
5d987040fSdrahn * Copyright (c) 1997 Mark Brinicombe
6d987040fSdrahn * All rights reserved.
7d987040fSdrahn *
8d987040fSdrahn * Redistribution and use in source and binary forms, with or without
9d987040fSdrahn * modification, are permitted provided that the following conditions
10d987040fSdrahn * are met:
11d987040fSdrahn * 1. Redistributions of source code must retain the above copyright
12d987040fSdrahn *    notice, this list of conditions and the following disclaimer.
13d987040fSdrahn * 2. Redistributions in binary form must reproduce the above copyright
14d987040fSdrahn *    notice, this list of conditions and the following disclaimer in the
15d987040fSdrahn *    documentation and/or other materials provided with the distribution.
16d987040fSdrahn * 3. All advertising materials mentioning features or use of this software
17d987040fSdrahn *    must display the following acknowledgement:
18d987040fSdrahn *	This product includes software developed by Mark Brinicombe
19d987040fSdrahn * 4. Neither the name of the University nor the names of its contributors
20d987040fSdrahn *    may be used to endorse or promote products derived from this software
21d987040fSdrahn *    without specific prior written permission.
22d987040fSdrahn *
23d987040fSdrahn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24d987040fSdrahn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25d987040fSdrahn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26d987040fSdrahn * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27d987040fSdrahn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28d987040fSdrahn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29d987040fSdrahn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30d987040fSdrahn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31d987040fSdrahn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32d987040fSdrahn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33d987040fSdrahn * SUCH DAMAGE.
34d987040fSdrahn */
35d987040fSdrahn
3638848718Sguenther#include "DEFS.h"
37d987040fSdrahn#include <machine/setjmp.h>
38d987040fSdrahn
39b29bfccfSguenther	.section	.openbsd.randomdata,"aw",%progbits
40ef6a97f9Sguenther	.balign	4
41b29bfccfSguenther	.globl	__jmpxor
42b29bfccfSguenther__jmpxor:
43b29bfccfSguenther	.zero 4*2		/* (sp, lr) */
44b29bfccfSguenther	END(__jmpxor)
45b29bfccfSguenther	.type	__jmpxor,%object
46b29bfccfSguenther
47b29bfccfSguenther
48d987040fSdrahn/*
49d987040fSdrahn * C library -- _setjmp, _longjmp
50d987040fSdrahn *
51d987040fSdrahn *	_longjmp(a,v)
52d987040fSdrahn * will generate a "return(v)" from the last call to
53d987040fSdrahn *	_setjmp(a)
54d987040fSdrahn * by restoring registers from the stack.
55d987040fSdrahn * The previous signal state is NOT restored.
56d987040fSdrahn *
57d987040fSdrahn * Note: r0 is the return value
58d987040fSdrahn *       r1-r3 are scratch registers in functions
59d987040fSdrahn */
60d987040fSdrahn
61d987040fSdrahnENTRY(_setjmp)
62d987040fSdrahn	ldr	r1, .L_setjmp_magic
63d987040fSdrahn	str	r1, [r0], #4
64b29bfccfSguenther	ldr	r2, .L_jmpxor_setjmp
65b29bfccfSguenther1:	add	r2, pc, r2			/* r2 = &__jmpxor */
66b29bfccfSguenther	ldr	r3, [r2], #4			/* r3 = __jmpxor[1] */
67b29bfccfSguenther	ldr	r2, [r2] 			/* r2 = __jmpxor[0] */
68b29bfccfSguenther	eor	r2, r13, r2			/* r2 = sp ^ __jmpxor[0] */
69b29bfccfSguenther	eor	r3, lr, r3			/* r3 = lr ^ __jmpxor[1] */
70b29bfccfSguenther
71d987040fSdrahn#ifdef SOFTFLOAT
72552f7563Skettenis	add	r0, r0, #68
73d987040fSdrahn#else
74552f7563Skettenis	/* Store fpcsr */
75552f7563Skettenis	vmrs	r1, fpscr
76552f7563Skettenis	str	r1, [r0], #4
77d987040fSdrahn	/* Store fp registers */
78552f7563Skettenis	vstmia	r0!, {d8-d15}
79d987040fSdrahn#endif	/* SOFTFLOAT */
80d987040fSdrahn	/* Store integer registers */
81b29bfccfSguenther	stmia	r0, {r2-r11}
82d987040fSdrahn
83d987040fSdrahn	mov	r0, #0x00000000
84b29bfccfSguenther	mov	r2, r0				/* overwrite __jmpxor copies */
85b29bfccfSguenther	mov	r3, r0
86b29bfccfSguenther	mov	pc, lr
87d987040fSdrahn
88*a8d2d9e8Sguenther.L_setjmp_magic:
89*a8d2d9e8Sguenther	.word	_JB_MAGIC__SETJMP
90*a8d2d9e8Sguenther
91b29bfccfSguenther.L_jmpxor_setjmp:
92b29bfccfSguenther	.word	__jmpxor - 1b
9338848718SguentherEND_STRONG(_setjmp)
94d987040fSdrahn
95d987040fSdrahnENTRY(_longjmp)
96d987040fSdrahn	ldr	r2, .L_setjmp_magic
97d987040fSdrahn	ldr	r3, [r0], #4
98d987040fSdrahn	teq	r2, r3
99ef6a97f9Sguenther	bne	.Lbotch
100d987040fSdrahn
101d987040fSdrahn#ifdef SOFTFLOAT
102552f7563Skettenis	add	r0, r0, #68
103d987040fSdrahn#else
104552f7563Skettenis	/* Restore fpcsr */
105552f7563Skettenis	ldr	r4, [r0], #4
106552f7563Skettenis	vmsr	fpscr, r4
107d987040fSdrahn	/* Restore fp registers */
108552f7563Skettenis	vldmia	r0!, {d8-d15}
109d987040fSdrahn#endif	/* SOFTFLOAT */
110d987040fSdrahn	/* Restore integer registers */
111b29bfccfSguenther	ldmia	r0, {r2-r11}
112d987040fSdrahn
113b29bfccfSguenther	ldr	r0, .L_jmpxor_longjmp
114b29bfccfSguenther1:	add	r0, pc, r0			/* r0 = &__jmpxor */
115b29bfccfSguenther	ldr	lr, [r0], #4			/* lr = __jmpxor[1] */
116b29bfccfSguenther	eor	lr, r3, lr			/* lr ^= jmpbuf[LR] */
117b29bfccfSguenther	ldr	r0, [r0] 			/* r0 = __jmpxor[0] */
118b29bfccfSguenther	eor	r13, r0, r2		/* sp = __jmpxor[0] ^ jmpbuf[SP] */
119b29bfccfSguenther	mov	r2, r1				/* overwrite __jmpxor copies */
120b29bfccfSguenther	mov	r3, r1
121b29bfccfSguenther
122b29bfccfSguenther	/* Validate sp and lr */
123d987040fSdrahn	teq	sp, #0
124b29bfccfSguenther	teqne	lr, #0
125ef6a97f9Sguenther	beq	.Lbotch
126d987040fSdrahn
127d987040fSdrahn	/* Set return value */
128d987040fSdrahn	mov	r0, r1
129d987040fSdrahn	teq	r0, #0x00000000
130d987040fSdrahn	moveq	r0, #0x00000001
131b29bfccfSguenther	mov	pc, lr
132b29bfccfSguenther
133b29bfccfSguenther.L_jmpxor_longjmp:
134b29bfccfSguenther	.word	__jmpxor - 1b
135d987040fSdrahn
136d987040fSdrahn	/* validation failed, die die die. */
137ef6a97f9Sguenther.Lbotch:
13838848718Sguenther	bl	_HIDDEN(abort)
139d987040fSdrahn	b	. - 8		/* Cannot get here */
14038848718SguentherEND_STRONG(_longjmp)
141