Home | History | Annotate | Line # | Download | only in gen
longjmp.c revision 1.5.2.1
      1  1.5.2.1  perseant /*	$NetBSD: longjmp.c,v 1.5.2.1 2025/08/02 05:54:33 perseant 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  *
     19      1.1       uwe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1       uwe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1       uwe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1       uwe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1       uwe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1       uwe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1       uwe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1       uwe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1       uwe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1       uwe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1       uwe  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1       uwe  */
     31      1.1       uwe 
     32      1.1       uwe #include "namespace.h"
     33      1.1       uwe #include <sys/types.h>
     34      1.1       uwe #include <ucontext.h>
     35      1.1       uwe #include <signal.h>
     36      1.1       uwe #include <stdlib.h>
     37      1.1       uwe #include <string.h>
     38      1.3    martin #include <stddef.h>
     39      1.1       uwe 
     40      1.1       uwe #define __LIBC12_SOURCE__
     41      1.1       uwe #include <setjmp.h>
     42      1.1       uwe #include <compat/include/setjmp.h>
     43      1.1       uwe 
     44      1.4       mrg #include "assym.h"
     45      1.4       mrg #include "sparc_longjmp.h"
     46      1.3    martin 
     47      1.3    martin /*
     48      1.4       mrg  * check that offsets in the above structures match their usage in the
     49      1.4       mrg  * setjmp() side of this setup.  a jmp_buf is the 12-word contents of
     50      1.5       uwe  * the sigcontext structure, plus 2 more words for g4 and g7.
     51      1.3    martin  */
     52      1.4       mrg __CTASSERT(_SIZEOF_SC + _JB_G4 == offsetof(struct __jmp_buf,regs.g4));
     53      1.4       mrg __CTASSERT(_SIZEOF_SC + _JB_G7 == offsetof(struct __jmp_buf,regs.g7));
     54      1.4       mrg __CTASSERT(sizeof(jmp_buf) >= sizeof(struct __jmp_buf));
     55      1.3    martin 
     56      1.1       uwe /*
     57      1.1       uwe  * Use setcontext to reload the stack pointer, program counter <pc,npc>, and
     58      1.1       uwe  * set the return value in %o0.  The %i and %l registers will be reloaded
     59      1.1       uwe  * from the place to which %sp points.
     60      1.1       uwe  */
     61      1.1       uwe void
     62      1.1       uwe __longjmp14(jmp_buf env, int val)
     63      1.1       uwe {
     64      1.4       mrg 	struct __jmp_buf *context = (void *)env;
     65      1.4       mrg 	struct sigcontext *sc = &context->sc;
     66      1.4       mrg 	struct __jmp_buf_regs_t *r = &context->regs;
     67      1.1       uwe 	ucontext_t uc;
     68      1.1       uwe 
     69      1.1       uwe 	/* Ensure non-zero SP */
     70      1.1       uwe 	if (sc->sc_sp == 0)
     71      1.1       uwe 		goto err;
     72      1.1       uwe 
     73      1.3    martin 	/* Initialise the context */
     74      1.3    martin 	memset(&uc, 0, sizeof(uc));
     75      1.1       uwe 
     76      1.1       uwe 	/*
     77  1.5.2.1  perseant 	 * Set _UC_CPU (restore CPU registers) and _UC_SIGMASK (restore
     78  1.5.2.1  perseant 	 * the signal mask) unconditionally.
     79  1.5.2.1  perseant 	 *
     80  1.5.2.1  perseant 	 * In the distant past of SA-based libpthread with sigprocmask
     81  1.5.2.1  perseant 	 * interception, we called sigprocmask here instead of using
     82  1.5.2.1  perseant 	 * _UC_SIGMASK -- but that restored the signal mask before the
     83  1.5.2.1  perseant 	 * stack pointer (PR lib/57946: longjmp fails to restore stack
     84  1.5.2.1  perseant 	 * first before restoring signal mask on most architectures),
     85  1.5.2.1  perseant 	 * which breaks sigaltstack, and SA-based libpthread is long
     86  1.5.2.1  perseant 	 * gone.  So we use _UC_SIGMASK.
     87      1.1       uwe 	 *
     88  1.5.2.1  perseant 	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
     89      1.1       uwe 	 */
     90  1.5.2.1  perseant 	uc.uc_flags = _UC_CPU | _UC_SIGMASK;
     91  1.5.2.1  perseant 	uc.uc_flags |= (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
     92      1.1       uwe 
     93  1.5.2.1  perseant 	/* Copy signal mask */
     94  1.5.2.1  perseant 	uc.uc_sigmask = sc->sc_mask;
     95      1.1       uwe 
     96      1.1       uwe 	/* Extract PSR, PC, NPC and SP from jmp_buf */
     97      1.1       uwe 	uc.uc_mcontext.__gregs[_REG_PSR] = sc->sc_psr;
     98      1.1       uwe 	uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
     99      1.3    martin 	uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_pc+4;
    100      1.1       uwe 	uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp;
    101      1.3    martin 	uc.uc_mcontext.__gregs[_REG_G2] = sc->sc_g1;
    102      1.3    martin 	uc.uc_mcontext.__gregs[_REG_G3] = sc->sc_npc;
    103      1.3    martin 	uc.uc_mcontext.__gregs[_REG_G4] = r->g4;
    104      1.3    martin 	uc.uc_mcontext.__gregs[_REG_G7] = r->g7;
    105      1.1       uwe 
    106      1.1       uwe 	/* Set the return value; make sure it's non-zero */
    107      1.1       uwe 	uc.uc_mcontext.__gregs[_REG_O0] = (val != 0 ? val : 1);
    108      1.1       uwe 
    109      1.1       uwe 	setcontext(&uc);
    110      1.1       uwe 
    111      1.1       uwe err:
    112      1.1       uwe 	longjmperror();
    113      1.1       uwe 	abort();
    114      1.1       uwe 	/* NOTREACHED */
    115      1.1       uwe }
    116