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