xref: /openbsd-src/lib/libc/arch/powerpc/gen/setjmp.S (revision 83762a71f74848f4d09174ce350838b4204957c5)
1*83762a71Sderaadt/*	$OpenBSD: setjmp.S,v 1.15 2023/12/10 16:45:51 deraadt Exp $ */
2a94eda49Sdrahn/*
3a94eda49Sdrahn * Copyright (c) 1996 Dale Rahn. All rights reserved.
4a94eda49Sdrahn *
5a94eda49Sdrahn *
6a94eda49Sdrahn * Redistribution and use in source and binary forms, with or without
7a94eda49Sdrahn * modification, are permitted provided that the following conditions
8a94eda49Sdrahn * are met:
9a94eda49Sdrahn * 1. Redistributions of source code must retain the above copyright
10a94eda49Sdrahn *    notice, this list of conditions and the following disclaimer.
11a94eda49Sdrahn * 2. Redistributions in binary form must reproduce the above copyright
12a94eda49Sdrahn *    notice, this list of conditions and the following disclaimer in the
13a94eda49Sdrahn *    documentation and/or other materials provided with the distribution.
14a94eda49Sdrahn *
15a94eda49Sdrahn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16a94eda49Sdrahn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17a94eda49Sdrahn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18a94eda49Sdrahn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19a94eda49Sdrahn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20a94eda49Sdrahn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21a94eda49Sdrahn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22a94eda49Sdrahn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23a94eda49Sdrahn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24a94eda49Sdrahn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a94eda49Sdrahn */
26a94eda49Sdrahn
27a4afd6daSrahnds#include "SYS.h"
28a4afd6daSrahnds
29a4afd6daSrahnds/* int setjmp(jmp_buf env) */
30a4afd6daSrahnds
31a4afd6daSrahnds#define JMP_r1	0x04
32a4afd6daSrahnds#define JMP_r14	0x08
33a4afd6daSrahnds#define JMP_r15	0x0c
34a4afd6daSrahnds#define JMP_r16	0x10
35a4afd6daSrahnds#define JMP_r17	0x14
36a4afd6daSrahnds#define JMP_r18	0x18
37a4afd6daSrahnds#define JMP_r19	0x1c
38a4afd6daSrahnds#define JMP_r20	0x20
39a4afd6daSrahnds#define JMP_r21	0x24
40a4afd6daSrahnds#define JMP_r22	0x28
41a4afd6daSrahnds#define JMP_r23	0x2c
42a4afd6daSrahnds#define JMP_r24	0x30
43a4afd6daSrahnds#define JMP_r25	0x34
44a4afd6daSrahnds#define JMP_r26	0x38
45a4afd6daSrahnds#define JMP_r27	0x3c
46a4afd6daSrahnds#define JMP_r28	0x40
47a4afd6daSrahnds#define JMP_r29	0x44
48a4afd6daSrahnds#define JMP_r30	0x48
49a4afd6daSrahnds#define JMP_r31	0x4c
50a4afd6daSrahnds#define JMP_lr  0x50
51a4afd6daSrahnds#define JMP_cr  0x54
52a4afd6daSrahnds#define JMP_ctr	0x58
53a4afd6daSrahnds#define JMP_xer	0x5c
54a4afd6daSrahnds#define JMP_sig	0x60
55a4afd6daSrahnds
5631f02ad1Sguenther	.section	.openbsd.randomdata,"aw",@progbits
578a57d2a8Sguenther	.balign	4
5831f02ad1Sguenther	.globl	__jmpxor
5931f02ad1Sguenther	.hidden	__jmpxor
6031f02ad1Sguenther__jmpxor:
6131f02ad1Sguenther	.zero	4*2		# (r1, lr)
6231f02ad1Sguenther	END(__jmpxor)
6331f02ad1Sguenther	.type	__jmpxor,@object
64a4afd6daSrahnds
65a4afd6daSrahnds
66a4afd6daSrahndsENTRY(setjmp)
6731f02ad1Sguenther	mr	5, 3			/* save jmpbuf addr in r5 */
6831f02ad1Sguenther	li	3, 1			/* how = SIG_BLOCK */
6931f02ad1Sguenther	li	4, 0			/* oset = empty */
7031f02ad1Sguenther	li	0, SYS_sigprocmask
71*83762a71Sderaadt99:	sc
72*83762a71Sderaadt	PINSYSCALL(SYS_sigprocmask, 99b)
73*83762a71Sderaadt
7431f02ad1Sguenther	stw	3, JMP_sig(5)
7531f02ad1Sguenther	mr	3, 5
76a4afd6daSrahndsENTRY(_setjmp)
7731f02ad1Sguenther	mflr	6
7831f02ad1Sguenther	bcl	20, 31, 1f
7931f02ad1Sguenther1:	mflr	7
8031f02ad1Sguenther	addis	7, 7, __jmpxor-1b@ha
8131f02ad1Sguenther	addi	7, 7, __jmpxor-1b@l
8231f02ad1Sguenther	mtlr	6
8331f02ad1Sguenther	lwz	0, 0(7)			/* xor for r1 */
8431f02ad1Sguenther	lwz	7, 4(7)			/* xor for lr, overwrite addr */
8531f02ad1Sguenther
8631f02ad1Sguenther	/* r1, r14-r31 */
8731f02ad1Sguenther	xor  0, 0, 1			/* use and overwrite the r1 xor */
8831f02ad1Sguenther	stw  0, JMP_r1 (3)
89a4afd6daSrahnds	stw 14, JMP_r14(3)
90a4afd6daSrahnds	stw 15, JMP_r15(3)
91a4afd6daSrahnds	stw 16, JMP_r16(3)
92a4afd6daSrahnds	stw 17, JMP_r17(3)
93a4afd6daSrahnds	stw 18, JMP_r18(3)
94a4afd6daSrahnds	stw 19, JMP_r19(3)
95a4afd6daSrahnds	stw 20, JMP_r20(3)
96a4afd6daSrahnds	stw 21, JMP_r21(3)
97a4afd6daSrahnds	stw 22, JMP_r22(3)
98a4afd6daSrahnds	stw 23, JMP_r23(3)
99a4afd6daSrahnds	stw 24, JMP_r24(3)
100a4afd6daSrahnds	stw 25, JMP_r25(3)
101a4afd6daSrahnds	stw 26, JMP_r26(3)
102a4afd6daSrahnds	stw 27, JMP_r27(3)
103a4afd6daSrahnds	stw 28, JMP_r28(3)
104a4afd6daSrahnds	stw 29, JMP_r29(3)
105a4afd6daSrahnds	stw 30, JMP_r30(3)
10631f02ad1Sguenther	stw 31, JMP_r31(3)
107a4afd6daSrahnds	/* cr, lr, ctr, xer */
108a4afd6daSrahnds	mfcr 0
109a4afd6daSrahnds	stw 0, JMP_cr(3)
11031f02ad1Sguenther	/* "mflr 6" done at start of _setjmp() */
11131f02ad1Sguenther	xor  7, 6, 7			/* use and overwrite the lr xor */
11231f02ad1Sguenther	stw 7, JMP_lr(3)
113a4afd6daSrahnds	mfctr 0
114a4afd6daSrahnds	stw 0, JMP_ctr(3)
115a4afd6daSrahnds	mfxer 0
116a4afd6daSrahnds	stw 0, JMP_xer(3)
117a4afd6daSrahnds	/* f14-f31, fpscr */
118a4afd6daSrahnds	li 3, 0
119a4afd6daSrahnds	blr
12028061906SguentherEND(_setjmp)
12128061906SguentherEND(setjmp)
122a4afd6daSrahnds
12331f02ad1Sguenther
124a4afd6daSrahndsENTRY(longjmp)
12531f02ad1Sguenther	mr	5, 3			/* save jmpbuf addr in r5 */
12631f02ad1Sguenther	mr	6, 4			/* save val in r6 */
12731f02ad1Sguenther	li	3, 3			/* how = SIG_SETMASK */
12831f02ad1Sguenther	lwz	4, JMP_sig(5)		/* oset from the jmpbuf */
12931f02ad1Sguenther	li	0, SYS_sigprocmask
130*83762a71Sderaadt98:	sc
131*83762a71Sderaadt	PINSYSCALL(SYS_sigprocmask, 98b)
13231f02ad1Sguenther	mr	3, 5			/* restore jmpbuf and val to r3,r4 */
13331f02ad1Sguenther	mr	4, 6
13431f02ad1Sguenther
135a4afd6daSrahndsENTRY(_longjmp)
13631f02ad1Sguenther	bcl	20, 31, 1f
13731f02ad1Sguenther1:	mflr	9
13831f02ad1Sguenther	addis	9, 9, __jmpxor-1b@ha
13931f02ad1Sguenther	addi	9, 9, __jmpxor-1b@l
14031f02ad1Sguenther	lwz	8, 0(9)			/* xor for r1 */
14131f02ad1Sguenther	lwz	9, 4(9)			/* xor for lr, overwrite addr */
14231f02ad1Sguenther
143a4afd6daSrahnds	/* r1, r14-r30 */
14431f02ad1Sguenther	lwz  0, JMP_r1 (3)
14531f02ad1Sguenther	xor  1, 0, 8			/* use the r1 xor */
146a4afd6daSrahnds	lwz 14, JMP_r14(3)
147a4afd6daSrahnds	lwz 15, JMP_r15(3)
148a4afd6daSrahnds	lwz 16, JMP_r16(3)
149a4afd6daSrahnds	lwz 17, JMP_r17(3)
150a4afd6daSrahnds	lwz 18, JMP_r18(3)
151a4afd6daSrahnds	lwz 19, JMP_r19(3)
152a4afd6daSrahnds	lwz 20, JMP_r20(3)
153a4afd6daSrahnds	lwz 21, JMP_r21(3)
154a4afd6daSrahnds	lwz 22, JMP_r22(3)
155a4afd6daSrahnds	lwz 23, JMP_r23(3)
156a4afd6daSrahnds	lwz 24, JMP_r24(3)
157a4afd6daSrahnds	lwz 25, JMP_r25(3)
158a4afd6daSrahnds	lwz 26, JMP_r26(3)
159a4afd6daSrahnds	lwz 27, JMP_r27(3)
160a4afd6daSrahnds	lwz 28, JMP_r28(3)
161a4afd6daSrahnds	lwz 29, JMP_r29(3)
162a4afd6daSrahnds	lwz 30, JMP_r30(3)
16331f02ad1Sguenther	lwz 31, JMP_r31(3)
164a4afd6daSrahnds	/* cr, lr, ctr, xer */
16531f02ad1Sguenther	lwz 8, JMP_cr(3)		/* overwrite the r1 xor */
16631f02ad1Sguenther	mtcr 8
167a4afd6daSrahnds	lwz 0, JMP_lr(3)
16831f02ad1Sguenther	xor  0, 0, 9			/* use the lr xor */
169a4afd6daSrahnds	mtlr 0
17031f02ad1Sguenther	lwz 9, JMP_ctr(3)		/* overwrite the lr xor */
17131f02ad1Sguenther	mtctr 9
172a4afd6daSrahnds	lwz 0, JMP_xer(3)
173a4afd6daSrahnds	mtxer 0
174a4afd6daSrahnds	/* f14-f31, fpscr */
175b5047e82Sdrahn
176b5047e82Sdrahn	/* if r4 == 0, return 1, not 0 */
177a4afd6daSrahnds	mr	3, 4
178b5047e82Sdrahn	cmpwi	4, 0
17931f02ad1Sguenther	bnelr
180b5047e82Sdrahn	li	3, 1
181a4afd6daSrahnds	blr
18228061906SguentherEND(_longjmp)
18328061906SguentherEND(longjmp)
184