xref: /netbsd-src/lib/libc/arch/alpha/gen/__longjmp14.c (revision 2a8eed4fde034413bbd2c73fbcf4983cbfdeded7)
1*2a8eed4fSthorpej /*	$NetBSD: __longjmp14.c,v 1.8 2021/05/25 00:14:41 thorpej Exp $	*/
2ca145383Smatt 
3ca145383Smatt /*-
4ca145383Smatt  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5ca145383Smatt  * All rights reserved.
6ca145383Smatt  *
7ca145383Smatt  * This code is derived from software contributed to The NetBSD Foundation
8ca145383Smatt  * by Christian Limpach and Matt Thomas.
9ca145383Smatt  *
10ca145383Smatt  * Redistribution and use in source and binary forms, with or without
11ca145383Smatt  * modification, are permitted provided that the following conditions
12ca145383Smatt  * are met:
13ca145383Smatt  * 1. Redistributions of source code must retain the above copyright
14ca145383Smatt  *    notice, this list of conditions and the following disclaimer.
15ca145383Smatt  * 2. Redistributions in binary form must reproduce the above copyright
16ca145383Smatt  *    notice, this list of conditions and the following disclaimer in the
17ca145383Smatt  *    documentation and/or other materials provided with the distribution.
18ca145383Smatt  *
19ca145383Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ca145383Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ca145383Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ca145383Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ca145383Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ca145383Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ca145383Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ca145383Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ca145383Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ca145383Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ca145383Smatt  * POSSIBILITY OF SUCH DAMAGE.
30ca145383Smatt  */
31ca145383Smatt 
32ca145383Smatt #include "namespace.h"
33ca145383Smatt #include <sys/types.h>
34ca145383Smatt #include <ucontext.h>
35ca145383Smatt #include <signal.h>
36ca145383Smatt #include <stdlib.h>
37ca145383Smatt #include <string.h>
38ca145383Smatt #include <machine/reg.h>
39ca145383Smatt #include <machine/alpha_cpu.h>
40ca145383Smatt 
41ca145383Smatt #define __LIBC12_SOURCE__
42ca145383Smatt #include <setjmp.h>
43b0e34b2bSmartin #include <compat/include/setjmp.h>
44ca145383Smatt 
45ca145383Smatt void
__longjmp14(jmp_buf env,int val)46ca145383Smatt __longjmp14(jmp_buf env, int val)
47ca145383Smatt {
48ca145383Smatt 	struct sigcontext *sc = (void *)env;
49ca145383Smatt 	ucontext_t uc;
50ca145383Smatt 
51ca145383Smatt 	/* Ensure non-zero SP */
52ca145383Smatt 	if (sc->sc_sp == 0 || sc->sc_regs[R_ZERO] != 0xacedbade)
53ca145383Smatt 		goto err;
54ca145383Smatt 
55ca145383Smatt 	/* Ensure non-zero return value */
56ca145383Smatt 	if (val == 0)
57*2a8eed4fSthorpej 		val = 1;
58ca145383Smatt 
59ca145383Smatt 	/* Set _UC_SIGMASK and _UC_CPU */
60ca145383Smatt 	uc.uc_flags = _UC_SIGMASK | _UC_CPU;
61ca145383Smatt 
62ca145383Smatt 	/* Clear uc_link */
63ca145383Smatt 	uc.uc_link = 0;
64ca145383Smatt 
65ca145383Smatt 	/* Save return value in context */
66ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_V0] = val;
67ca145383Smatt 
68ca145383Smatt 	/* Copy saved registers */
69ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[R_S0];
70ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[R_S1];
71ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[R_S2];
72ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[R_S3];
73ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[R_S4];
74ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[R_S5];
75ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[R_S6];
76ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[R_RA];
77c06bee84Smartin 	uc.uc_mcontext.__gregs[_REG_GP] = sc->sc_regs[R_GP];
78ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
79ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
80ca145383Smatt 	uc.uc_mcontext.__gregs[_REG_PS] =
81ca145383Smatt 	   (sc->sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
82ca145383Smatt 
83ca145383Smatt 	/* Copy FP state */
84ca145383Smatt 	if (sc->sc_ownedfp) {
85ca145383Smatt 		memcpy(&uc.uc_mcontext.__fpregs.__fp_fr,
86ca145383Smatt 		    &sc->sc_fpregs, 31 * sizeof(unsigned long));
87339695cfSsimonb 		uc.uc_mcontext.__fpregs.__fp_fpcr = sc->sc_fpcr;
88ca145383Smatt 		/* XXX sc_fp_control */
89ca145383Smatt 		uc.uc_flags |= _UC_FPU;
90ca145383Smatt 	}
91ca145383Smatt 
92ca145383Smatt 	/* Copy signal mask */
93ca145383Smatt 	uc.uc_sigmask = sc->sc_mask;
94ca145383Smatt 
95ca145383Smatt 	setcontext(&uc);
96ca145383Smatt  err:
97ca145383Smatt 	longjmperror();
98ca145383Smatt 	abort();
99ca145383Smatt 	/* NOTREACHED */
100ca145383Smatt }
101