xref: /openbsd-src/lib/libc/arch/amd64/gen/sigsetjmp.S (revision 83762a71f74848f4d09174ce350838b4204957c5)
1*83762a71Sderaadt/*	$OpenBSD: sigsetjmp.S,v 1.10 2023/12/10 16:45:51 deraadt Exp $	*/
2118f6189Smickey/*	$NetBSD: __setjmp14.S,v 1.1 2001/06/19 00:25:02 fvdl Exp $	*/
3118f6189Smickey
4118f6189Smickey/*
5118f6189Smickey * Copyright (c) 2001 Wasabi Systems, Inc.
6118f6189Smickey * All rights reserved.
7118f6189Smickey *
8118f6189Smickey * Written by Frank van der Linden for Wasabi Systems, Inc.
9118f6189Smickey *
10118f6189Smickey * Redistribution and use in source and binary forms, with or without
11118f6189Smickey * modification, are permitted provided that the following conditions
12118f6189Smickey * are met:
13118f6189Smickey * 1. Redistributions of source code must retain the above copyright
14118f6189Smickey *    notice, this list of conditions and the following disclaimer.
15118f6189Smickey * 2. Redistributions in binary form must reproduce the above copyright
16118f6189Smickey *    notice, this list of conditions and the following disclaimer in the
17118f6189Smickey *    documentation and/or other materials provided with the distribution.
18118f6189Smickey * 3. All advertising materials mentioning features or use of this software
19118f6189Smickey *    must display the following acknowledgement:
20118f6189Smickey *      This product includes software developed for the NetBSD Project by
21118f6189Smickey *      Wasabi Systems, Inc.
22118f6189Smickey * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23118f6189Smickey *    or promote products derived from this software without specific prior
24118f6189Smickey *    written permission.
25118f6189Smickey *
26118f6189Smickey * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27118f6189Smickey * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28118f6189Smickey * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29118f6189Smickey * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30118f6189Smickey * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31118f6189Smickey * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32118f6189Smickey * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33118f6189Smickey * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34118f6189Smickey * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35118f6189Smickey * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36118f6189Smickey * POSSIBILITY OF SUCH DAMAGE.
37118f6189Smickey */
38118f6189Smickey
39118f6189Smickey
40747916ccSguenther#include "SYS.h"
41118f6189Smickey#include <machine/setjmp.h>
42118f6189Smickey
439d294f30Sderaadt	.hidden __jmpxor
449d294f30Sderaadt
45118f6189Smickey/*
46118f6189Smickey * C library -- _setjmp, _longjmp
47118f6189Smickey *
48118f6189Smickey *	longjmp(a,v)
49118f6189Smickey * will generate a "return(v)" from the last call to
50118f6189Smickey *	setjmp(a)
51118f6189Smickey * by restoring registers from the stack.
52118f6189Smickey * The previous signal state is restored.
53118f6189Smickey */
54118f6189Smickey
55118f6189SmickeyENTRY(sigsetjmp)
564672739eSmortimer	RETGUARD_SETUP(sigsetjmp, r10)
570035f85fSderaadt	movl    %esi,(_JB_SIGFLAG  * 8)(%rdi)
58118f6189Smickey	testl   %esi,%esi
59118f6189Smickey	jz      2f
60118f6189Smickey
61747916ccSguenther	movq	%rdi,%r8		/* save jmpbuf addr */
62747916ccSguenther	movl	$1,%edi			/* how = SIG_BLOCK */
63747916ccSguenther	xorl	%esi,%esi		/* set = empty */
64747916ccSguenther	movl	$SYS_sigprocmask,%eax
65*83762a71Sderaadt99:	syscall
66*83762a71Sderaadt	PINSYSCALL(SYS_sigprocmask, 99b)
67747916ccSguenther	movq	%r8,%rdi		/* restore jmpbuf addr */
68118f6189Smickey	movq	%rax,(_JB_SIGMASK * 8)(%rdi)
69118f6189Smickey
700035f85fSderaadt2:	movq	(%rsp),%r11
719d294f30Sderaadt	leaq	__jmpxor(%rip),%rcx
720035f85fSderaadt	movq	%rbx,(_JB_RBX * 8)(%rdi)
739d294f30Sderaadt	movq	%rbp,%rax
749d294f30Sderaadt	xorq	(0*8)(%rcx),%rax
759d294f30Sderaadt	movq	%rax,(_JB_RBP * 8)(%rdi)
760035f85fSderaadt	movq	%r12,(_JB_R12 * 8)(%rdi)
770035f85fSderaadt	movq	%r13,(_JB_R13 * 8)(%rdi)
780035f85fSderaadt	movq	%r14,(_JB_R14 * 8)(%rdi)
790035f85fSderaadt	movq	%r15,(_JB_R15 * 8)(%rdi)
809d294f30Sderaadt	movq	%rsp,%rax
819d294f30Sderaadt	xorq	(1*8)(%rcx),%rax
829d294f30Sderaadt	movq	%rax,(_JB_RSP * 8)(%rdi)
839d294f30Sderaadt	movq	%r11,%rax
849d294f30Sderaadt	xorq	(2*8)(%rcx),%rax
859d294f30Sderaadt	movq	%rax,(_JB_PC  * 8)(%rdi)
869d294f30Sderaadt	xorq	%rcx,%rcx
87e48de7d1Skettenis	stmxcsr	(_JB_MXCSR * 8)(%rdi)
88e48de7d1Skettenis	fnstcw	(_JB_MXCSR * 8 + 4)(%rdi)
890035f85fSderaadt
909d294f30Sderaadt	xorq	%rax,%rax
914672739eSmortimer	RETGUARD_CHECK(sigsetjmp, r10)
92118f6189Smickey	ret
9353e48b83SuebayasiEND(sigsetjmp)
94118f6189Smickey
95118f6189SmickeyENTRY(siglongjmp)
96118f6189Smickey	movq	%rdi,%r12
97747916ccSguenther	movl	%esi,%r8d
98118f6189Smickey	cmpl    $0, (_JB_SIGFLAG * 8)(%rdi)
99118f6189Smickey	jz      2f
100118f6189Smickey
101747916ccSguenther	movq	(_JB_SIGMASK * 8)(%rdi),%rsi	/* get set from sc_mask */
102747916ccSguenther	movl	$3,%edi				/* how = SIG_SETMASK */
103747916ccSguenther	movl	$SYS_sigprocmask,%eax
104*83762a71Sderaadt98:	syscall
105*83762a71Sderaadt	PINSYSCALL(SYS_sigprocmask, 98b)
106747916ccSguenther2:	movl	%r8d,%eax
1079d294f30Sderaadt
1089d294f30Sderaadt	leaq	 __jmpxor(%rip),%rcx
1094672739eSmortimer	movq	(_JB_RSP * 8)(%r12),%r11
1104672739eSmortimer	xorq	(1*8)(%rcx),%r11
1114672739eSmortimer	movq	%r11,%rsp
1124672739eSmortimer	movq	(_JB_PC  * 8)(%r12),%r11
1134672739eSmortimer	xorq	(2*8)(%rcx),%r11
1144672739eSmortimer	movq	%r11,0(%rsp)
1154672739eSmortimer	RETGUARD_SETUP(siglongjmp, r10)
116e48de7d1Skettenis	ldmxcsr	(_JB_MXCSR * 8)(%r12)
117e48de7d1Skettenis	fldcw	(_JB_MXCSR * 8 + 4)(%r12)
118118f6189Smickey	movq	(_JB_RBX * 8)(%r12),%rbx
1199d294f30Sderaadt	movq	(_JB_RBP * 8)(%r12),%r11
1209d294f30Sderaadt	xorq	(0*8)(%rcx),%r11
1219d294f30Sderaadt	movq	%r11,%rbp
122118f6189Smickey	movq	(_JB_R13 * 8)(%r12),%r13
123118f6189Smickey	movq	(_JB_R14 * 8)(%r12),%r14
124118f6189Smickey	movq	(_JB_R15 * 8)(%r12),%r15
125118f6189Smickey	movq	(_JB_R12 * 8)(%r12),%r12
1269d294f30Sderaadt	xorq	%rcx,%rcx
127118f6189Smickey
128118f6189Smickey	testl	%eax,%eax
129118f6189Smickey	jnz	1f
130118f6189Smickey	incl	%eax
1314672739eSmortimer1:	RETGUARD_CHECK(siglongjmp, r10)
132118f6189Smickey	ret
13353e48b83SuebayasiEND(siglongjmp)
134