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