1 /*	$NetBSD: longjmp.c,v 1.1 2005/09/17 11:49:39 tsutsui 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 <sys/cdefs.h>
40 __FBSDID("$FreeBSD: stable/9/lib/libc/mips/gen/longjmp.c 178580 2008-04-26 12:08:02Z imp $");
41 #include "namespace.h"
42 #include <sys/types.h>
43 #include <ucontext.h>
44 #include <signal.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include <machine/regnum.h>
49 
50 void
__longjmp14(jmp_buf env,int val)51 __longjmp14(jmp_buf env, int val)
52 {
53 	struct sigcontext *sc = (void *)env;
54 	ucontext_t uc;
55 
56 	/* Ensure non-zero SP and sigcontext magic number is present */
57 	if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != 0xACEDBADE)
58 		goto err;
59 
60 	/* Ensure non-zero return value */
61 	if (val == 0)
62 		val = 1;
63 
64 	/*
65 	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
66 	 *
67 	 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
68 	 * since libpthread may want to interpose on signal handling.
69 	 */
70 	uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
71 
72 	sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
73 
74 	/* Clear uc_link */
75 	uc.uc_link = 0;
76 
77 	/* Save return value in context */
78 	uc.uc_mcontext.__gregs[_R_V0] = val;
79 
80 	/* Copy saved registers */
81 	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0];
82 	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1];
83 	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2];
84 	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3];
85 	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4];
86 	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5];
87 	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6];
88 	uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7];
89 	uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8];
90 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP];
91 	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA];
92 	uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc;
93 
94 	/* Copy FP state */
95 	if (sc->sc_fpused) {
96 		/* FP saved regs are $f20 .. $f31 */
97 		memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20],
98 		    &sc->sc_fpregs[20], 32 - 20);
99 		uc.uc_mcontext.__fpregs.__fp_csr =
100 		    sc->sc_fpregs[_R_FSR - _FPBASE];
101 		/* XXX sc_fp_control */
102 		uc.uc_flags |= _UC_FPU;
103 	}
104 
105 	setcontext(&uc);
106  err:
107 	longjmperror();
108 	abort();
109 	/* NOTREACHED */
110 }
111