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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD$"); 34#include "namespace.h" 35#include <sys/types.h> 36#include <ucontext.h> 37#include <signal.h> 38#include <stdlib.h> 39#include <string.h> 40 41#include <machine/regnum.h> 42 43void 44__longjmp14(jmp_buf env, int val) 45{ 46 struct sigcontext *sc = (void *)env; 47 ucontext_t uc; 48 49 /* Ensure non-zero SP and sigcontext magic number is present */ 50 if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != 0xACEDBADE) 51 goto err; 52 53 /* Ensure non-zero return value */ 54 if (val == 0) 55 val = 1; 56 57 /* 58 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK. 59 * 60 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK, 61 * since libpthread may want to interpose on signal handling. 62 */ 63 uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK); 64 65 sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL); 66 67 /* Clear uc_link */ 68 uc.uc_link = 0; 69 70 /* Save return value in context */ 71 uc.uc_mcontext.__gregs[_R_V0] = val; 72 73 /* Copy saved registers */ 74 uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0]; 75 uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1]; 76 uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2]; 77 uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3]; 78 uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4]; 79 uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5]; 80 uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6]; 81 uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7]; 82 uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8]; 83 uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP]; 84 uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA]; 85 uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc; 86 87 /* Copy FP state */ 88 if (sc->sc_fpused) { 89 /* FP saved regs are $f20 .. $f31 */ 90 memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20], 91 &sc->sc_fpregs[20], 32 - 20); 92 uc.uc_mcontext.__fpregs.__fp_csr = 93 sc->sc_fpregs[_R_FSR - _FPBASE]; 94 /* XXX sc_fp_control */ 95 uc.uc_flags |= _UC_FPU; 96 } 97 98 setcontext(&uc); 99 err: 100 longjmperror(); 101 abort(); 102 /* NOTREACHED */ 103} 104