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