1/*	$OpenBSD: sigsetjmp.S,v 1.9 2023/12/10 16:45:51 deraadt 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#define JMP_sigflag	0x00
30#define JMP_r1		0x04
31#define JMP_r14		0x08
32#define JMP_r15		0x0c
33#define JMP_r16		0x10
34#define JMP_r17		0x14
35#define JMP_r18		0x18
36#define JMP_r19		0x1c
37#define JMP_r20		0x20
38#define JMP_r21		0x24
39#define JMP_r22		0x28
40#define JMP_r23		0x2c
41#define JMP_r24		0x30
42#define JMP_r25		0x34
43#define JMP_r26		0x38
44#define JMP_r27		0x3c
45#define JMP_r28		0x40
46#define JMP_r29		0x44
47#define JMP_r30		0x48
48#define JMP_r31		0x4c
49#define JMP_lr		0x50
50#define JMP_cr		0x54
51#define JMP_ctr		0x58
52#define JMP_xer		0x5c
53#define JMP_sigmask	0x60
54
55	.extern	__jmpxor
56
57/* int sigsetjmp(sigjmp_buf env, int savemask) */
58ENTRY(sigsetjmp)
59	mr	5, 3			/* save jmpbuf addr in r5 */
60	stw	4, JMP_sigflag(5)
61	or.	4, 4, 4
62	beq	1f
63	li	3, 1			/* how = SIG_BLOCK */
64	li	4, 0			/* oset = empty */
65	li	0, SYS_sigprocmask
6699:	sc
67	PINSYSCALL(SYS_sigprocmask, 99b)
68	stw	3, JMP_sigmask(5)
691:	mflr	6
70	bcl	20, 31, 2f
712:	mflr	7
72	addis	7, 7, __jmpxor-2b@ha
73	addi	7, 7, __jmpxor-2b@l
74	mtlr	6
75	lwz	0, 0(7)			/* xor for r1 */
76	lwz	7, 4(7)			/* xor for lr, overwrite addr */
77
78	/* r1, r14-r31 */
79	xor  0, 0, 1			/* use and overwrite the r1 xor */
80	/* "mflr 6" done at 1: above */
81	xor  7, 6, 7			/* use and overwrite the lr xor */
82	stw 7, JMP_lr(5)
83	stw  0, JMP_r1 (5)
84	stw 14, JMP_r14(5)
85	stw 15, JMP_r15(5)
86	stw 16, JMP_r16(5)
87	stw 17, JMP_r17(5)
88	stw 18, JMP_r18(5)
89	stw 19, JMP_r19(5)
90	stw 20, JMP_r20(5)
91	stw 21, JMP_r21(5)
92	stw 22, JMP_r22(5)
93	stw 23, JMP_r23(5)
94	stw 24, JMP_r24(5)
95	stw 25, JMP_r25(5)
96	stw 26, JMP_r26(5)
97	stw 27, JMP_r27(5)
98	stw 28, JMP_r28(5)
99	stw 29, JMP_r29(5)
100	stw 30, JMP_r30(5)
101	stw 31, JMP_r31(5)
102	/* cr, lr, ctr, xer */
103	mfcr 0
104	stw 0, JMP_cr(5)
105	mfctr 0
106	stw 0, JMP_ctr(5)
107	mfxer 0
108	stw 0, JMP_xer(5)
109	/* f14-f31, fpscr */
110	li 3, 0
111	blr
112END(sigsetjmp)
113
114
115/* int siglongjmp(sigjmp_buf env, int val) */
116ENTRY(siglongjmp)
117	mr	5, 3			/* save jmpbuf addr in r5 */
118	mr	6, 4			/* save val in r6 */
119	lwz	4, JMP_sigflag(5)	/* do we need to restore sigmask? */
120	or.	4, 4, 4
121	beq	1f
122
123	li	3, 3			/* how = SIG_SETMASK */
124	lwz	4, JMP_sigmask(5)	/* oset from the jmpbuf */
125	li	0, SYS_sigprocmask
12698:	sc
127	PINSYSCALL(SYS_sigprocmask, 98b)
128
1291:	bcl	20, 31, 2f
1302:	mflr	9
131	addis	9, 9, __jmpxor-2b@ha
132	addi	9, 9, __jmpxor-2b@l
133	lwz	8, 0(9)			/* xor for r1 */
134	lwz	9, 4(9)			/* xor for lr, overwrite addr */
135
136	/* r1, r14-r31 */
137	lwz  0, JMP_r1(5)
138	xor  1, 0, 8			/* use the r1 xor */
139	lwz 14, JMP_r14(5)
140	lwz 15, JMP_r15(5)
141	lwz 16, JMP_r16(5)
142	lwz 17, JMP_r17(5)
143	lwz 18, JMP_r18(5)
144	lwz 19, JMP_r19(5)
145	lwz 20, JMP_r20(5)
146	lwz 21, JMP_r21(5)
147	lwz 22, JMP_r22(5)
148	lwz 23, JMP_r23(5)
149	lwz 24, JMP_r24(5)
150	lwz 25, JMP_r25(5)
151	lwz 26, JMP_r26(5)
152	lwz 27, JMP_r27(5)
153	lwz 28, JMP_r28(5)
154	lwz 29, JMP_r29(5)
155	lwz 30, JMP_r30(5)
156	lwz 31, JMP_r31(5)
157	/* cr, lr, ctr, xer */
158	lwz 8, JMP_cr(5)		/* overwrite the r1 xor */
159	mtcr 8
160	lwz 0, JMP_lr(5)
161	xor  0, 0, 9			/* use the lr xor */
162	mtlr 0
163	lwz 9, JMP_ctr(5)		/* overwrite the lr xor */
164	mtctr 9
165	lwz 0, JMP_xer(5)
166	mtxer 0
167	/* f14-f31, fpscr */
168
169	/* if r6 == 0, return 1, not 0 */
170	mr	3, 6
171	cmpwi	6, 0
172	bnelr
173	li	3, 1
174	blr
175END(siglongjmp)
176