1/* $OpenBSD: _setjmp.S,v 1.9 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 <machine/asm.h>
35#include <machine/setjmp.h>
36
37	.global	__jmpxor
38
39/*
40 * C library -- _setjmp, _longjmp
41 *
42 *	_longjmp(a,v)
43 * will generate a "return(v)" from the last call to
44 *	_setjmp(a)
45 * by restoring registers from the stack.
46 * The previous signal state is NOT restored.
47 */
48
49ENTRY(_setjmp)
50	call	1f
511:	popl	%ecx
52	addl	$__jmpxor-1b,%ecx	# load cookie address
53	movl	4(%esp),%eax		# parameter, pointer to env
54	movl	0(%esp),%edx		# caller address
55	xorl	0(%ecx),%edx		# use eip cookie
56	movl	%edx,(_JB_EIP * 4)(%eax)
57	movl	%ebx,(_JB_EBX * 4)(%eax)
58	movl	%esp,%edx
59	xorl	4(%ecx),%edx		# use esp cookie
60	movl	%edx,(_JB_ESP * 4)(%eax)
61	movl	8(%ecx),%ecx		# load ebp cookie over cookie address
62	xorl	%ebp,%ecx
63	movl	%ecx,(_JB_EBP * 4)(%eax)
64	movl	%esi,(_JB_ESI * 4)(%eax)
65	movl	%edi,(_JB_EDI * 4)(%eax)
66	fnstcw	(_JB_FCW * 4)(%eax)
67	xorl	%eax,%eax
68	ret
69END(_setjmp)
70
71ENTRY(_longjmp)
72	call	1f
731:	popl	%ecx
74	addl	$__jmpxor-1b,%ecx	# load cookie address
75	movl	4(%esp),%edx		# parameter, pointer to env
76	movl	8(%esp),%eax		# parameter, val
77	fldcw	(_JB_FCW * 4)(%edx)
78	movl	(_JB_EBX * 4)(%edx),%ebx
79	movl	(_JB_ESP * 4)(%edx),%esi
80	xorl	4(%ecx),%esi		# use esp cookie
81	movl	%esi,%esp		# un-xor'ed esp is safe to use
82	movl	(_JB_EBP * 4)(%edx),%ebp
83	xorl	8(%ecx),%ebp		# use ebp cookie
84	movl	(_JB_ESI * 4)(%edx),%esi
85	movl	(_JB_EDI * 4)(%edx),%edi
86	movl	0(%ecx),%ecx		# load eip cookie over cookie address
87	xorl	(_JB_EIP * 4)(%edx),%ecx
88	testl	%eax,%eax
89	jnz	1f
90	incl	%eax
911:	movl	%ecx,0(%esp)
92	ret
93END(_longjmp)
94