Home | History | Annotate | Line # | Download | only in gen
      1 /*	$NetBSD: longjmp.c,v 1.8 2025/04/24 01:48:21 riastradh 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 <stdio.h>
     39 #include <stddef.h>
     40 
     41 #define __LIBC12_SOURCE__
     42 #include <setjmp.h>
     43 #include <compat/include/setjmp.h>
     44 
     45 typedef struct {
     46 	__greg_t	g3;
     47 	__greg_t	g6;
     48 	__greg_t	g7;
     49 	__greg_t	dummy;
     50 	__greg_t	save_mask;
     51 } __jmp_buf_regs_t;
     52 
     53 /*
     54  * setjmp.S uses hard coded offsets into the jump_buf,
     55  * make sure any changes cause a compile failure here
     56  */
     57 #ifndef lint	/* XXX this is too much for lint */
     58 __CTASSERT(0x68 == offsetof(__jmp_buf_regs_t,save_mask) +
     59 	sizeof(struct sigcontext));
     60 __CTASSERT(sizeof(sigjmp_buf) >= sizeof(__jmp_buf_regs_t) +
     61 	sizeof(struct sigcontext));
     62 #endif
     63 
     64 void
     65 __longjmp14(jmp_buf env, int val)
     66 {
     67 	struct sigcontext *sc = (void *)env;
     68 	__jmp_buf_regs_t *r = (void *)&sc[1];
     69 	ucontext_t uc;
     70 
     71 	/* Ensure non-zero SP */
     72 	if (sc->sc_sp == 0)
     73 		goto err;
     74 
     75 	memset(&uc, 0, sizeof(uc));
     76 
     77 	/*
     78 	 * Set _UC_CPU (restore CPU registers) and _UC_SIGMASK (restore
     79 	 * the signal mask) unconditionally.
     80 	 *
     81 	 * In the distant past of SA-based libpthread with sigprocmask
     82 	 * interception, we called sigprocmask here instead of using
     83 	 * _UC_SIGMASK -- but that restored the signal mask before the
     84 	 * stack pointer (PR lib/57946: longjmp fails to restore stack
     85 	 * first before restoring signal mask on most architectures),
     86 	 * which breaks sigaltstack, and SA-based libpthread is long
     87 	 * gone.  So we use _UC_SIGMASK.
     88 	 *
     89 	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
     90 	 */
     91 	uc.uc_flags = _UC_CPU | _UC_SIGMASK;
     92 	uc.uc_flags |= (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
     93 
     94 	/* Copy signal mask */
     95 	uc.uc_sigmask = sc->sc_mask;
     96 
     97 	/* Fill other registers */
     98 	uc.uc_mcontext.__gregs[_REG_CCR] = sc->sc_tstate;
     99 	uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
    100 	uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_pc+4;
    101 	uc.uc_mcontext.__gregs[_REG_G1] = sc->sc_g1;
    102 	uc.uc_mcontext.__gregs[_REG_G2] = sc->sc_o0;
    103 	uc.uc_mcontext.__gregs[_REG_G3] = r->g3;
    104 	uc.uc_mcontext.__gregs[_REG_G4] = 0;
    105 	uc.uc_mcontext.__gregs[_REG_G5] = 0;
    106 	uc.uc_mcontext.__gregs[_REG_G6] = r->g6;
    107 	uc.uc_mcontext.__gregs[_REG_G7] = r->g7;
    108 	uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp;
    109 
    110 	/* No FPU data saved, so we can't restore that. */
    111 
    112 	/* Make return value non-zero */
    113 	if (val == 0)
    114 		val = 1;
    115 
    116 	/* Save return value in context */
    117 	uc.uc_mcontext.__gregs[_REG_O0] = val;
    118 
    119 	setcontext(&uc);
    120 
    121  err:
    122 	longjmperror();
    123 	abort();
    124 	/* NOTREACHED */
    125 }
    126