xref: /openbsd/lib/libc/arch/powerpc/gen/setjmp.S (revision 76d0caae)
1/*	$OpenBSD: setjmp.S,v 1.13 2016/05/29 07:59:36 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#include <machine/asm.h>
29
30/* int setjmp(jmp_buf env) */
31
32#define JMP_r1	0x04
33#define JMP_r14	0x08
34#define JMP_r15	0x0c
35#define JMP_r16	0x10
36#define JMP_r17	0x14
37#define JMP_r18	0x18
38#define JMP_r19	0x1c
39#define JMP_r20	0x20
40#define JMP_r21	0x24
41#define JMP_r22	0x28
42#define JMP_r23	0x2c
43#define JMP_r24	0x30
44#define JMP_r25	0x34
45#define JMP_r26	0x38
46#define JMP_r27	0x3c
47#define JMP_r28	0x40
48#define JMP_r29	0x44
49#define JMP_r30	0x48
50#define JMP_r31	0x4c
51#define JMP_lr  0x50
52#define JMP_cr  0x54
53#define JMP_ctr	0x58
54#define JMP_xer	0x5c
55#define JMP_sig	0x60
56
57	.section	.openbsd.randomdata,"aw",@progbits
58	.balign	4
59	.globl	__jmpxor
60	.hidden	__jmpxor
61__jmpxor:
62	.zero	4*2		# (r1, lr)
63	END(__jmpxor)
64	.type	__jmpxor,@object
65
66
67ENTRY(setjmp)
68	mr	5, 3			/* save jmpbuf addr in r5 */
69	li	3, 1			/* how = SIG_BLOCK */
70	li	4, 0			/* oset = empty */
71	li	0, SYS_sigprocmask
72	sc
73	stw	3, JMP_sig(5)
74	mr	3, 5
75ENTRY(_setjmp)
76	mflr	6
77	bcl	20, 31, 1f
781:	mflr	7
79	addis	7, 7, __jmpxor-1b@ha
80	addi	7, 7, __jmpxor-1b@l
81	mtlr	6
82	lwz	0, 0(7)			/* xor for r1 */
83	lwz	7, 4(7)			/* xor for lr, overwrite addr */
84
85	/* r1, r14-r31 */
86	xor  0, 0, 1			/* use and overwrite the r1 xor */
87	stw  0, JMP_r1 (3)
88	stw 14, JMP_r14(3)
89	stw 15, JMP_r15(3)
90	stw 16, JMP_r16(3)
91	stw 17, JMP_r17(3)
92	stw 18, JMP_r18(3)
93	stw 19, JMP_r19(3)
94	stw 20, JMP_r20(3)
95	stw 21, JMP_r21(3)
96	stw 22, JMP_r22(3)
97	stw 23, JMP_r23(3)
98	stw 24, JMP_r24(3)
99	stw 25, JMP_r25(3)
100	stw 26, JMP_r26(3)
101	stw 27, JMP_r27(3)
102	stw 28, JMP_r28(3)
103	stw 29, JMP_r29(3)
104	stw 30, JMP_r30(3)
105	stw 31, JMP_r31(3)
106	/* cr, lr, ctr, xer */
107	mfcr 0
108	stw 0, JMP_cr(3)
109	/* "mflr 6" done at start of _setjmp() */
110	xor  7, 6, 7			/* use and overwrite the lr xor */
111	stw 7, JMP_lr(3)
112	mfctr 0
113	stw 0, JMP_ctr(3)
114	mfxer 0
115	stw 0, JMP_xer(3)
116	/* f14-f31, fpscr */
117	li 3, 0
118	blr
119END(_setjmp)
120END(setjmp)
121
122
123ENTRY(longjmp)
124	mr	5, 3			/* save jmpbuf addr in r5 */
125	mr	6, 4			/* save val in r6 */
126	li	3, 3			/* how = SIG_SETMASK */
127	lwz	4, JMP_sig(5)		/* oset from the jmpbuf */
128	li	0, SYS_sigprocmask
129	sc
130	mr	3, 5			/* restore jmpbuf and val to r3,r4 */
131	mr	4, 6
132
133ENTRY(_longjmp)
134	bcl	20, 31, 1f
1351:	mflr	9
136	addis	9, 9, __jmpxor-1b@ha
137	addi	9, 9, __jmpxor-1b@l
138	lwz	8, 0(9)			/* xor for r1 */
139	lwz	9, 4(9)			/* xor for lr, overwrite addr */
140
141	/* r1, r14-r30 */
142	lwz  0, JMP_r1 (3)
143	xor  1, 0, 8			/* use the r1 xor */
144	lwz 14, JMP_r14(3)
145	lwz 15, JMP_r15(3)
146	lwz 16, JMP_r16(3)
147	lwz 17, JMP_r17(3)
148	lwz 18, JMP_r18(3)
149	lwz 19, JMP_r19(3)
150	lwz 20, JMP_r20(3)
151	lwz 21, JMP_r21(3)
152	lwz 22, JMP_r22(3)
153	lwz 23, JMP_r23(3)
154	lwz 24, JMP_r24(3)
155	lwz 25, JMP_r25(3)
156	lwz 26, JMP_r26(3)
157	lwz 27, JMP_r27(3)
158	lwz 28, JMP_r28(3)
159	lwz 29, JMP_r29(3)
160	lwz 30, JMP_r30(3)
161	lwz 31, JMP_r31(3)
162	/* cr, lr, ctr, xer */
163	lwz 8, JMP_cr(3)		/* overwrite the r1 xor */
164	mtcr 8
165	lwz 0, JMP_lr(3)
166	xor  0, 0, 9			/* use the lr xor */
167	mtlr 0
168	lwz 9, JMP_ctr(3)		/* overwrite the lr xor */
169	mtctr 9
170	lwz 0, JMP_xer(3)
171	mtxer 0
172	/* f14-f31, fpscr */
173
174	/* if r4 == 0, return 1, not 0 */
175	mr	3, 4
176	cmpwi	4, 0
177	bnelr
178	li	3, 1
179	blr
180END(_longjmp)
181END(longjmp)
182