xref: /netbsd-src/lib/libc/arch/mips/gen/longjmp.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
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 "namespace.h"
40 #include <sys/types.h>
41 #include <ucontext.h>
42 #include <signal.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include <machine/regnum.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 and sigcontext magic number is present */
59 	if (sc->sc_regs[_R_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 	/*
67 	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
68 	 *
69 	 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
70 	 * since libpthread may want to interpose on signal handling.
71 	 */
72 	uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
73 
74 	sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
75 
76 	/* Clear uc_link */
77 	uc.uc_link = 0;
78 
79 	/* Save return value in context */
80 	uc.uc_mcontext.__gregs[_R_V0] = val;
81 
82 	/* Copy saved registers */
83 	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0];
84 	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1];
85 	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2];
86 	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3];
87 	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4];
88 	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5];
89 	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6];
90 	uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7];
91 	uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8];
92 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP];
93 	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA];
94 	uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc;
95 
96 	/* Copy FP state */
97 	if (sc->sc_fpused) {
98 		/* FP saved regs are $f20 .. $f31 */
99 		memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20],
100 		    &sc->sc_fpregs[20], 32 - 20);
101 		uc.uc_mcontext.__fpregs.__fp_csr =
102 		    sc->sc_fpregs[_R_FSR - _FPBASE];
103 		/* XXX sc_fp_control */
104 		uc.uc_flags |= _UC_FPU;
105 	}
106 
107 	setcontext(&uc);
108  err:
109 	longjmperror();
110 	abort();
111 	/* NOTREACHED */
112 }
113