1/*	$OpenBSD: setjmp.S,v 1.10 2023/12/10 16:45:51 deraadt Exp $	*/
2/*	$NetBSD: __setjmp14.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $	*/
3
4/*
5 * Copyright (c) 2001 Wasabi Systems, Inc.
6 * All rights reserved.
7 *
8 * Written by Frank van der Linden for Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *      This product includes software developed for the NetBSD Project by
21 *      Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 *    or promote products derived from this software without specific prior
24 *    written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39
40#include "SYS.h"
41#include <machine/setjmp.h>
42
43	.section        .openbsd.randomdata,"aw",@progbits
44	.align 8
45	.globl	__jmpxor
46__jmpxor:
47	.zero 8*3		# (rbp, rsp, pc)
48	END(__jmpxor)
49	.type	__jmpxor,@object
50
51/*
52 * C library -- _setjmp, _longjmp
53 *
54 *	longjmp(a,v)
55 * will generate a "return(v)" from the last call to
56 *	setjmp(a)
57 * by restoring registers from the stack.
58 * The previous signal state is restored.
59 */
60
61ENTRY(setjmp)
62	RETGUARD_SETUP(setjmp, r10)
63	movq	%rdi,%r8		/* save jmpbuf addr */
64	movl	$1,%edi			/* how = SIG_BLOCK */
65	xorl	%esi,%esi		/* set = empty */
66	movl	$SYS_sigprocmask,%eax
6799:	syscall
68	PINSYSCALL(SYS_sigprocmask, 99b)
69	movq	%r8,%rdi		/* restore jmpbuf addr */
70	movq	%rax,(_JB_SIGMASK * 8)(%rdi)
71
72	movq	(%rsp),%r11
73	leaq	__jmpxor(%rip),%rcx
74	movq	%rbx,(_JB_RBX * 8)(%rdi)
75	movq	%rbp,%rax
76	xorq	(0*8)(%rcx),%rax
77	movq	%rax,(_JB_RBP * 8)(%rdi)
78	movq	%r12,(_JB_R12 * 8)(%rdi)
79	movq	%r13,(_JB_R13 * 8)(%rdi)
80	movq	%r14,(_JB_R14 * 8)(%rdi)
81	movq	%r15,(_JB_R15 * 8)(%rdi)
82	movq	%rsp,%rax
83	xorq	(1*8)(%rcx),%rax
84	movq	%rax,(_JB_RSP * 8)(%rdi)
85	movq	%r11,%rax
86	xorq	(2*8)(%rcx),%rax
87	movq	%rax,(_JB_PC  * 8)(%rdi)
88	xorq	%rcx,%rcx
89	stmxcsr	(_JB_MXCSR * 8)(%rdi)
90	fnstcw	(_JB_MXCSR * 8 + 4)(%rdi)
91
92	xorq	%rax,%rax
93	RETGUARD_CHECK(setjmp, r10)
94	ret
95END(setjmp)
96
97ENTRY(longjmp)
98	movq	%rdi,%r12
99	movl	%esi,%r8d
100
101	movq	(_JB_SIGMASK * 8)(%rdi),%rsi	/* get set from sc_mask */
102	movl	$3,%edi				/* how = SIG_SETMASK */
103	movl	$SYS_sigprocmask,%eax
10498:	syscall
105	PINSYSCALL(SYS_sigprocmask, 98b)
106	movl	%r8d,%eax
107
108	leaq	 __jmpxor(%rip),%rcx
109	movq	(_JB_RSP * 8)(%r12),%r11
110	xorq	(1*8)(%rcx),%r11
111	movq	%r11,%rsp
112	movq	(_JB_PC  * 8)(%r12),%r11
113	xorq	(2*8)(%rcx),%r11
114	movq	%r11,0(%rsp)
115	RETGUARD_SETUP(longjmp, r10)
116	ldmxcsr	(_JB_MXCSR * 8)(%r12)
117	fldcw	(_JB_MXCSR * 8 + 4)(%r12)
118	movq	(_JB_RBX * 8)(%r12),%rbx
119	movq	(_JB_RBP * 8)(%r12),%r11
120	xorq	(0*8)(%rcx),%r11
121	movq	%r11,%rbp
122	movq	(_JB_R13 * 8)(%r12),%r13
123	movq	(_JB_R14 * 8)(%r12),%r14
124	movq	(_JB_R15 * 8)(%r12),%r15
125	movq	(_JB_R12 * 8)(%r12),%r12
126	xorq	%rcx,%rcx
127
128	testl	%eax,%eax
129	jnz	1f
130	incl	%eax
1311:	RETGUARD_CHECK(longjmp, r10)
132	ret
133END(longjmp)
134