xref: /netbsd-src/lib/libc/arch/alpha/gen/__longjmp14.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: __longjmp14.c,v 1.4 2005/09/14 08:59:37 martin 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 #include <compat/include/setjmp.h>
51 
52 void
53 __longjmp14(jmp_buf env, int val)
54 {
55 	struct sigcontext *sc = (void *)env;
56 	ucontext_t uc;
57 
58 	/* Ensure non-zero SP */
59 	if (sc->sc_sp == 0 || sc->sc_regs[R_ZERO] != 0xacedbade)
60 		goto err;
61 
62 	/* Ensure non-zero return value */
63 	if (val == 0)
64 		val = -1;
65 
66 	/* Set _UC_SIGMASK and _UC_CPU */
67 	uc.uc_flags = _UC_SIGMASK | _UC_CPU;
68 
69 	/* Clear uc_link */
70 	uc.uc_link = 0;
71 
72 	/* Save return value in context */
73 	uc.uc_mcontext.__gregs[_REG_V0] = val;
74 
75 	/* Copy saved registers */
76 	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[R_S0];
77 	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[R_S1];
78 	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[R_S2];
79 	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[R_S3];
80 	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[R_S4];
81 	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[R_S5];
82 	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[R_S6];
83 	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[R_RA];
84 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
85 	uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
86 	uc.uc_mcontext.__gregs[_REG_PS] =
87 	   (sc->sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
88 
89 	/* Copy FP state */
90 	if (sc->sc_ownedfp) {
91 		memcpy(&uc.uc_mcontext.__fpregs.__fp_fr,
92 		    &sc->sc_fpregs, 31 * sizeof(unsigned long));
93 		uc.uc_mcontext.__fpregs.__fp_fpcr = sc->sc_fpcr;
94 		/* XXX sc_fp_control */
95 		uc.uc_flags |= _UC_FPU;
96 	}
97 
98 	/* Copy signal mask */
99 	uc.uc_sigmask = sc->sc_mask;
100 
101 	setcontext(&uc);
102  err:
103 	longjmperror();
104 	abort();
105 	/* NOTREACHED */
106 }
107