xref: /openbsd-src/lib/libc/arch/powerpc/gen/setjmp.S (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1/*	$OpenBSD: setjmp.S,v 1.14 2022/06/10 01:56:02 guenther Exp $ */
2/*
3 * Copyright (c) 1996 Dale Rahn. All rights reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "SYS.h"
28
29/* int setjmp(jmp_buf env) */
30
31#define JMP_r1	0x04
32#define JMP_r14	0x08
33#define JMP_r15	0x0c
34#define JMP_r16	0x10
35#define JMP_r17	0x14
36#define JMP_r18	0x18
37#define JMP_r19	0x1c
38#define JMP_r20	0x20
39#define JMP_r21	0x24
40#define JMP_r22	0x28
41#define JMP_r23	0x2c
42#define JMP_r24	0x30
43#define JMP_r25	0x34
44#define JMP_r26	0x38
45#define JMP_r27	0x3c
46#define JMP_r28	0x40
47#define JMP_r29	0x44
48#define JMP_r30	0x48
49#define JMP_r31	0x4c
50#define JMP_lr  0x50
51#define JMP_cr  0x54
52#define JMP_ctr	0x58
53#define JMP_xer	0x5c
54#define JMP_sig	0x60
55
56	.section	.openbsd.randomdata,"aw",@progbits
57	.balign	4
58	.globl	__jmpxor
59	.hidden	__jmpxor
60__jmpxor:
61	.zero	4*2		# (r1, lr)
62	END(__jmpxor)
63	.type	__jmpxor,@object
64
65
66ENTRY(setjmp)
67	mr	5, 3			/* save jmpbuf addr in r5 */
68	li	3, 1			/* how = SIG_BLOCK */
69	li	4, 0			/* oset = empty */
70	li	0, SYS_sigprocmask
71	sc
72	stw	3, JMP_sig(5)
73	mr	3, 5
74ENTRY(_setjmp)
75	mflr	6
76	bcl	20, 31, 1f
771:	mflr	7
78	addis	7, 7, __jmpxor-1b@ha
79	addi	7, 7, __jmpxor-1b@l
80	mtlr	6
81	lwz	0, 0(7)			/* xor for r1 */
82	lwz	7, 4(7)			/* xor for lr, overwrite addr */
83
84	/* r1, r14-r31 */
85	xor  0, 0, 1			/* use and overwrite the r1 xor */
86	stw  0, JMP_r1 (3)
87	stw 14, JMP_r14(3)
88	stw 15, JMP_r15(3)
89	stw 16, JMP_r16(3)
90	stw 17, JMP_r17(3)
91	stw 18, JMP_r18(3)
92	stw 19, JMP_r19(3)
93	stw 20, JMP_r20(3)
94	stw 21, JMP_r21(3)
95	stw 22, JMP_r22(3)
96	stw 23, JMP_r23(3)
97	stw 24, JMP_r24(3)
98	stw 25, JMP_r25(3)
99	stw 26, JMP_r26(3)
100	stw 27, JMP_r27(3)
101	stw 28, JMP_r28(3)
102	stw 29, JMP_r29(3)
103	stw 30, JMP_r30(3)
104	stw 31, JMP_r31(3)
105	/* cr, lr, ctr, xer */
106	mfcr 0
107	stw 0, JMP_cr(3)
108	/* "mflr 6" done at start of _setjmp() */
109	xor  7, 6, 7			/* use and overwrite the lr xor */
110	stw 7, JMP_lr(3)
111	mfctr 0
112	stw 0, JMP_ctr(3)
113	mfxer 0
114	stw 0, JMP_xer(3)
115	/* f14-f31, fpscr */
116	li 3, 0
117	blr
118END(_setjmp)
119END(setjmp)
120
121
122ENTRY(longjmp)
123	mr	5, 3			/* save jmpbuf addr in r5 */
124	mr	6, 4			/* save val in r6 */
125	li	3, 3			/* how = SIG_SETMASK */
126	lwz	4, JMP_sig(5)		/* oset from the jmpbuf */
127	li	0, SYS_sigprocmask
128	sc
129	mr	3, 5			/* restore jmpbuf and val to r3,r4 */
130	mr	4, 6
131
132ENTRY(_longjmp)
133	bcl	20, 31, 1f
1341:	mflr	9
135	addis	9, 9, __jmpxor-1b@ha
136	addi	9, 9, __jmpxor-1b@l
137	lwz	8, 0(9)			/* xor for r1 */
138	lwz	9, 4(9)			/* xor for lr, overwrite addr */
139
140	/* r1, r14-r30 */
141	lwz  0, JMP_r1 (3)
142	xor  1, 0, 8			/* use the r1 xor */
143	lwz 14, JMP_r14(3)
144	lwz 15, JMP_r15(3)
145	lwz 16, JMP_r16(3)
146	lwz 17, JMP_r17(3)
147	lwz 18, JMP_r18(3)
148	lwz 19, JMP_r19(3)
149	lwz 20, JMP_r20(3)
150	lwz 21, JMP_r21(3)
151	lwz 22, JMP_r22(3)
152	lwz 23, JMP_r23(3)
153	lwz 24, JMP_r24(3)
154	lwz 25, JMP_r25(3)
155	lwz 26, JMP_r26(3)
156	lwz 27, JMP_r27(3)
157	lwz 28, JMP_r28(3)
158	lwz 29, JMP_r29(3)
159	lwz 30, JMP_r30(3)
160	lwz 31, JMP_r31(3)
161	/* cr, lr, ctr, xer */
162	lwz 8, JMP_cr(3)		/* overwrite the r1 xor */
163	mtcr 8
164	lwz 0, JMP_lr(3)
165	xor  0, 0, 9			/* use the lr xor */
166	mtlr 0
167	lwz 9, JMP_ctr(3)		/* overwrite the lr xor */
168	mtctr 9
169	lwz 0, JMP_xer(3)
170	mtxer 0
171	/* f14-f31, fpscr */
172
173	/* if r4 == 0, return 1, not 0 */
174	mr	3, 4
175	cmpwi	4, 0
176	bnelr
177	li	3, 1
178	blr
179END(_longjmp)
180END(longjmp)
181