longjmp.c revision 1.4 1 1.4 matt /* $NetBSD: longjmp.c,v 1.4 2010/09/03 17:22:51 matt Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by Christian Limpach and Matt Thomas.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui *
19 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tsutsui */
31 1.1 tsutsui
32 1.1 tsutsui #include "namespace.h"
33 1.1 tsutsui #include <sys/types.h>
34 1.1 tsutsui #include <ucontext.h>
35 1.1 tsutsui #include <signal.h>
36 1.1 tsutsui #include <stdlib.h>
37 1.1 tsutsui #include <string.h>
38 1.1 tsutsui
39 1.1 tsutsui #include <machine/regnum.h>
40 1.1 tsutsui
41 1.1 tsutsui #define __LIBC12_SOURCE__
42 1.1 tsutsui #include <setjmp.h>
43 1.1 tsutsui #include <compat/include/setjmp.h>
44 1.1 tsutsui
45 1.1 tsutsui void
46 1.1 tsutsui __longjmp14(jmp_buf env, int val)
47 1.1 tsutsui {
48 1.1 tsutsui struct sigcontext *sc = (void *)env;
49 1.1 tsutsui ucontext_t uc;
50 1.1 tsutsui
51 1.1 tsutsui /* Ensure non-zero SP and sigcontext magic number is present */
52 1.3 lukem if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != (mips_reg_t)0xACEDBADE)
53 1.1 tsutsui goto err;
54 1.1 tsutsui
55 1.1 tsutsui /* Ensure non-zero return value */
56 1.1 tsutsui if (val == 0)
57 1.1 tsutsui val = 1;
58 1.1 tsutsui
59 1.1 tsutsui /*
60 1.1 tsutsui * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
61 1.1 tsutsui *
62 1.1 tsutsui * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
63 1.1 tsutsui * since libpthread may want to interpose on signal handling.
64 1.1 tsutsui */
65 1.1 tsutsui uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
66 1.1 tsutsui
67 1.1 tsutsui sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
68 1.1 tsutsui
69 1.1 tsutsui /* Clear uc_link */
70 1.1 tsutsui uc.uc_link = 0;
71 1.1 tsutsui
72 1.1 tsutsui /* Save return value in context */
73 1.4 matt uc.uc_mcontext.__gregs[_REG_V0] = val;
74 1.1 tsutsui
75 1.1 tsutsui /* Copy saved registers */
76 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0];
77 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1];
78 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2];
79 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3];
80 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4];
81 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5];
82 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6];
83 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7];
84 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8];
85 1.4 matt #if defined(__mips_n32) || defined(__mips_n64)
86 1.4 matt uc.uc_mcontext.__gregs[_REG_GP] = sc->sc_regs[_R_GP];
87 1.4 matt #endif
88 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP];
89 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA];
90 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc;
91 1.1 tsutsui
92 1.1 tsutsui /* Copy FP state */
93 1.1 tsutsui if (sc->sc_fpused) {
94 1.1 tsutsui /* FP saved regs are $f20 .. $f31 */
95 1.1 tsutsui memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20],
96 1.1 tsutsui &sc->sc_fpregs[20], 32 - 20);
97 1.1 tsutsui uc.uc_mcontext.__fpregs.__fp_csr =
98 1.1 tsutsui sc->sc_fpregs[_R_FSR - _FPBASE];
99 1.1 tsutsui /* XXX sc_fp_control */
100 1.1 tsutsui uc.uc_flags |= _UC_FPU;
101 1.1 tsutsui }
102 1.1 tsutsui
103 1.1 tsutsui setcontext(&uc);
104 1.1 tsutsui err:
105 1.1 tsutsui longjmperror();
106 1.1 tsutsui abort();
107 1.1 tsutsui /* NOTREACHED */
108 1.1 tsutsui }
109