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