Home | History | Annotate | Line # | Download | only in gen
longjmp.c revision 1.1
      1  1.1  uwe /*	$NetBSD: longjmp.c,v 1.1 2005/10/15 22:11:22 uwe Exp $	*/
      2  1.1  uwe 
      3  1.1  uwe /*-
      4  1.1  uwe  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.1  uwe  * All rights reserved.
      6  1.1  uwe  *
      7  1.1  uwe  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  uwe  * by Christian Limpach.
      9  1.1  uwe  *
     10  1.1  uwe  * Redistribution and use in source and binary forms, with or without
     11  1.1  uwe  * modification, are permitted provided that the following conditions
     12  1.1  uwe  * are met:
     13  1.1  uwe  * 1. Redistributions of source code must retain the above copyright
     14  1.1  uwe  *    notice, this list of conditions and the following disclaimer.
     15  1.1  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  uwe  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  uwe  *    documentation and/or other materials provided with the distribution.
     18  1.1  uwe  * 3. All advertising materials mentioning features or use of this software
     19  1.1  uwe  *    must display the following acknowledgement:
     20  1.1  uwe  *        This product includes software developed by the NetBSD
     21  1.1  uwe  *        Foundation, Inc. and its contributors.
     22  1.1  uwe  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  uwe  *    contributors may be used to endorse or promote products derived
     24  1.1  uwe  *    from this software without specific prior written permission.
     25  1.1  uwe  *
     26  1.1  uwe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  uwe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  uwe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  uwe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  uwe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  uwe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  uwe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  uwe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  uwe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  uwe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  uwe  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  uwe  */
     38  1.1  uwe 
     39  1.1  uwe #include "namespace.h"
     40  1.1  uwe #include <sys/types.h>
     41  1.1  uwe #include <ucontext.h>
     42  1.1  uwe #include <signal.h>
     43  1.1  uwe #include <stdlib.h>
     44  1.1  uwe #include <string.h>
     45  1.1  uwe 
     46  1.1  uwe #define __LIBC12_SOURCE__
     47  1.1  uwe #include <setjmp.h>
     48  1.1  uwe #include <compat/include/setjmp.h>
     49  1.1  uwe 
     50  1.1  uwe /*
     51  1.1  uwe  * Use setcontext to reload the stack pointer, program counter <pc,npc>, and
     52  1.1  uwe  * set the return value in %o0.  The %i and %l registers will be reloaded
     53  1.1  uwe  * from the place to which %sp points.
     54  1.1  uwe  */
     55  1.1  uwe void
     56  1.1  uwe __longjmp14(jmp_buf env, int val)
     57  1.1  uwe {
     58  1.1  uwe 	struct sigcontext *sc = (void *)env;
     59  1.1  uwe 	ucontext_t uc;
     60  1.1  uwe 
     61  1.1  uwe 	/* Ensure non-zero SP */
     62  1.1  uwe 	if (sc->sc_sp == 0)
     63  1.1  uwe 		goto err;
     64  1.1  uwe 
     65  1.1  uwe 	/* Initialise the fields we're going to use */
     66  1.1  uwe 	uc.uc_link = 0;
     67  1.1  uwe 
     68  1.1  uwe 	/*
     69  1.1  uwe 	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
     70  1.1  uwe 	 *
     71  1.1  uwe 	 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
     72  1.1  uwe 	 * since libpthread may want to interpose on signal handling.
     73  1.1  uwe 	 */
     74  1.1  uwe 	uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
     75  1.1  uwe 
     76  1.1  uwe 	sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
     77  1.1  uwe 
     78  1.1  uwe 	/* Extract PSR, PC, NPC and SP from jmp_buf */
     79  1.1  uwe 	uc.uc_mcontext.__gregs[_REG_PSR] = sc->sc_psr;
     80  1.1  uwe 	uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
     81  1.1  uwe 	uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_npc;
     82  1.1  uwe 	uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp;
     83  1.1  uwe 
     84  1.1  uwe 	/* Set the return value; make sure it's non-zero */
     85  1.1  uwe 	uc.uc_mcontext.__gregs[_REG_O0] = (val != 0 ? val : 1);
     86  1.1  uwe 
     87  1.1  uwe 	setcontext(&uc);
     88  1.1  uwe 
     89  1.1  uwe err:
     90  1.1  uwe 	longjmperror();
     91  1.1  uwe 	abort();
     92  1.1  uwe 	/* NOTREACHED */
     93  1.1  uwe }
     94