setjmp.S revision 1.14
1/* $OpenBSD: setjmp.S,v 1.14 2020/12/13 21:21:32 bluhm 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	.section	.openbsd.randomdata,"aw",@progbits
38	.balign	4
39	.globl	__jmpxor
40	.hidden	__jmpxor
41__jmpxor:
42	.zero	4*3		# (eip, esp, ebp)
43	END(__jmpxor)
44	.type	__jmpxor,@object
45
46
47/*
48 * C library -- setjmp, longjmp
49 *
50 *	longjmp(a,v)
51 * will generate a "return(v)" from the last call to
52 *	setjmp(a)
53 * by restoring registers from the stack.
54 * The previous signal state is restored.
55 */
56
57ENTRY(setjmp)
58	pushl	$0			/* mask = empty */
59	pushl	$1			/* how = SIG_BLOCK */
60	call	1f
611:	movl	$(SYS_sigprocmask),%eax
62	int	$0x80			/* leave oset in %eax */
63	popl	%edx
64	addl	$8,%esp
65	addl	$__jmpxor-1b,%edx	# load cookie address
66
67	movl	4(%esp),%ecx		# parameter, pointer to env
68	movl	%eax,(_JB_SIGMASK * 4)(%ecx)
69	movl	%ebx,(_JB_EBX * 4)(%ecx)
70	movl	%esp,%eax
71	xorl	0(%edx),%eax		# use esp cookie
72	movl	%eax,(_JB_ESP * 4)(%ecx)
73	movl	%ebp,%eax
74	xorl	4(%edx),%eax		# use ebp cookie
75	movl	%eax,(_JB_EBP * 4)(%ecx)
76	movl	%esi,(_JB_ESI * 4)(%ecx)
77	movl	%edi,(_JB_EDI * 4)(%ecx)
78	movl	8(%edx),%edx		# load eip cookie over cookie address
79	xorl	0(%esp),%edx		# caller address
80	movl	%edx,(_JB_EIP * 4)(%ecx)
81	fnstcw	(_JB_FCW * 4)(%ecx)
82	xorl	%eax,%eax
83	ret
84END(setjmp)
85
86ENTRY(longjmp)
87	movl	4(%esp),%edx		# parameter, pointer to env
88	pushl	(_JB_SIGMASK * 4)(%edx)	/* mask from sc_mask */
89	pushl	$3			/* how = SIG_SETMASK */
90	call	1f			/* get our eip */
911:	movl	$(SYS_sigprocmask),%eax
92	int	$0x80
93	popl	%ecx
94	addl	$8,%esp
95	addl	$__jmpxor-1b,%ecx	# load cookie address
96
97	movl	4(%esp),%edx		# parameter, pointer to env
98	movl	8(%esp),%eax		# parameter, val
99	fldcw	(_JB_FCW * 4)(%edx)
100	movl	(_JB_EBX * 4)(%edx),%ebx
101	movl	(_JB_ESP * 4)(%edx),%esi
102	xorl	0(%ecx),%esi		# use esp cookie
103	movl	%esi,%esp		# un-xor'ed esp is safe to use
104	movl	(_JB_EBP * 4)(%edx),%ebp
105	xorl	4(%ecx),%ebp		# use ebp cookie
106	movl	(_JB_ESI * 4)(%edx),%esi
107	movl	(_JB_EDI * 4)(%edx),%edi
108
109	movl	8(%ecx),%ecx		# load eip cookie over cookie address
110	xorl	(_JB_EIP * 4)(%edx),%ecx
111	testl	%eax,%eax
112	jnz	1f
113	incl	%eax
1141:	movl	%ecx,0(%esp)
115	ret
116END(longjmp)
117