xref: /openbsd-src/lib/libc/arch/i386/gen/sigsetjmp.S (revision 83762a71f74848f4d09174ce350838b4204957c5)
1/* $OpenBSD: sigsetjmp.S,v 1.14 2023/12/10 16:45:51 deraadt Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "SYS.h"
35#include <machine/setjmp.h>
36
37	.global	__jmpxor
38
39ENTRY(sigsetjmp)
40	movl	4(%esp),%ecx		# parameter, pointer to env
41	movl	8(%esp),%eax		# parameter, savemask
42	movl	%eax,(_JB_SIGFLAG * 4)(%ecx)
43	testl	%eax,%eax
44	jz	1f
45
46	pushl	$0			/* mask = empty */
47	pushl	$1			/* how = SIG_BLOCK */
48	subl	$4,%esp
49	movl	$(SYS_sigprocmask),%eax
5099:	int	$0x80			/* leave oset in %eax */
51	PINSYSCALL(SYS_sigprocmask, 99b)
52	addl	$12,%esp
53	movl	%eax,(_JB_SIGMASK * 4)(%ecx)
54
551:	call	2f
562:	popl	%edx
57	addl	$__jmpxor-2b,%edx	# load cookie address
58
59	movl	%ebx,(_JB_EBX * 4)(%ecx)
60	movl	%esp,%eax
61	xorl	8(%edx),%eax		# use esp cookie
62	movl	%eax,(_JB_ESP * 4)(%ecx)
63	movl	%ebp,%eax
64	xorl	0(%edx),%eax		# use ebp cookie
65	movl	%eax,(_JB_EBP * 4)(%ecx)
66	movl	%esi,(_JB_ESI * 4)(%ecx)
67	movl	%edi,(_JB_EDI * 4)(%ecx)
68	movl	4(%edx),%edx		# load eip cookie over cookie address
69	xorl	0(%esp),%edx
70	movl	%edx,(_JB_EIP * 4)(%ecx)
71	fnstcw	(_JB_FCW * 4)(%ecx)
72	xorl	%eax,%eax
73	ret
74END(sigsetjmp)
75
76ENTRY(siglongjmp)
77	movl	4(%esp),%edx		# parameter, pointer to env
78	cmpl	$0,(_JB_SIGFLAG * 4)(%edx)
79	jz	1f
80
81	pushl	(_JB_SIGMASK * 4)(%edx)	/* mask from sc_mask */
82	pushl	$3			/* how = SIG_SETMASK */
83	subl	$4,%esp
84	movl	$(SYS_sigprocmask),%eax
8598:	int	$0x80
86	PINSYSCALL(SYS_sigprocmask, 98b)
87	addl	$12,%esp
88
891:	call	2f
902:	popl	%ecx
91	addl	$__jmpxor-2b,%ecx	# load cookie address
92
93	movl	4(%esp),%edx		# reload in case sigprocmask failed
94	movl	8(%esp),%eax		# parameter, val
95	fldcw	(_JB_FCW * 4)(%edx)
96	movl	(_JB_EBX * 4)(%edx),%ebx
97	movl	(_JB_ESP * 4)(%edx),%esi
98	xorl	8(%ecx),%esi		# use esp cookie
99	movl	%esi,%esp		# un-xor'ed esp is safe to use
100	movl	(_JB_EBP * 4)(%edx),%ebp
101	xorl	0(%ecx),%ebp		# use ebp cookie
102	movl	(_JB_ESI * 4)(%edx),%esi
103	movl	(_JB_EDI * 4)(%edx),%edi
104
105	movl	4(%ecx),%ecx		# load eip cookie over cookie address
106	xorl	(_JB_EIP * 4)(%edx),%ecx
107	testl	%eax,%eax
108	jnz	2f
109	incl	%eax
1102:	movl	%ecx,0(%esp)
111	ret
112END(siglongjmp)
113