xref: /netbsd-src/lib/libc/arch/alpha/gen/__longjmp14.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: __longjmp14.c,v 1.3 2004/03/23 01:42:53 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christian Limpach and Matt Thomas.
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 by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
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 #include "namespace.h"
40 #include <sys/types.h>
41 #include <ucontext.h>
42 #include <signal.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <machine/reg.h>
46 #include <machine/alpha_cpu.h>
47 
48 #define __LIBC12_SOURCE__
49 #include <setjmp.h>
50 
51 void
52 __longjmp14(jmp_buf env, int val)
53 {
54 	struct sigcontext *sc = (void *)env;
55 	ucontext_t uc;
56 
57 	/* Ensure non-zero SP */
58 	if (sc->sc_sp == 0 || sc->sc_regs[R_ZERO] != 0xacedbade)
59 		goto err;
60 
61 	/* Ensure non-zero return value */
62 	if (val == 0)
63 		val = -1;
64 
65 	/* Set _UC_SIGMASK and _UC_CPU */
66 	uc.uc_flags = _UC_SIGMASK | _UC_CPU;
67 
68 	/* Clear uc_link */
69 	uc.uc_link = 0;
70 
71 	/* Save return value in context */
72 	uc.uc_mcontext.__gregs[_REG_V0] = val;
73 
74 	/* Copy saved registers */
75 	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[R_S0];
76 	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[R_S1];
77 	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[R_S2];
78 	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[R_S3];
79 	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[R_S4];
80 	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[R_S5];
81 	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[R_S6];
82 	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[R_RA];
83 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
84 	uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
85 	uc.uc_mcontext.__gregs[_REG_PS] =
86 	   (sc->sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
87 
88 	/* Copy FP state */
89 	if (sc->sc_ownedfp) {
90 		memcpy(&uc.uc_mcontext.__fpregs.__fp_fr,
91 		    &sc->sc_fpregs, 31 * sizeof(unsigned long));
92 		uc.uc_mcontext.__fpregs.__fp_fpcr = sc->sc_fpcr;
93 		/* XXX sc_fp_control */
94 		uc.uc_flags |= _UC_FPU;
95 	}
96 
97 	/* Copy signal mask */
98 	uc.uc_sigmask = sc->sc_mask;
99 
100 	setcontext(&uc);
101  err:
102 	longjmperror();
103 	abort();
104 	/* NOTREACHED */
105 }
106