Home | History | Annotate | Line # | Download | only in gen
longjmp.c revision 1.2.10.1
      1  1.2.10.1      jym /*	$NetBSD: longjmp.c,v 1.2.10.1 2009/05/13 19:18:21 jym 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.2.10.1      jym 	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.1  tsutsui 	uc.uc_mcontext.__gregs[_R_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.1  tsutsui 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP];
     86       1.1  tsutsui 	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA];
     87       1.1  tsutsui 	uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc;
     88       1.1  tsutsui 
     89       1.1  tsutsui 	/* Copy FP state */
     90       1.1  tsutsui 	if (sc->sc_fpused) {
     91       1.1  tsutsui 		/* FP saved regs are $f20 .. $f31 */
     92       1.1  tsutsui 		memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20],
     93       1.1  tsutsui 		    &sc->sc_fpregs[20], 32 - 20);
     94       1.1  tsutsui 		uc.uc_mcontext.__fpregs.__fp_csr =
     95       1.1  tsutsui 		    sc->sc_fpregs[_R_FSR - _FPBASE];
     96       1.1  tsutsui 		/* XXX sc_fp_control */
     97       1.1  tsutsui 		uc.uc_flags |= _UC_FPU;
     98       1.1  tsutsui 	}
     99       1.1  tsutsui 
    100       1.1  tsutsui 	setcontext(&uc);
    101       1.1  tsutsui  err:
    102       1.1  tsutsui 	longjmperror();
    103       1.1  tsutsui 	abort();
    104       1.1  tsutsui 	/* NOTREACHED */
    105       1.1  tsutsui }
    106