Home | History | Annotate | Line # | Download | only in gen
      1 /*	$NetBSD: __longjmp14.c,v 1.7 2025/04/13 23:45:10 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 
     39 #define __LIBC12_SOURCE__
     40 #include <setjmp.h>
     41 #include <compat/include/setjmp.h>
     42 
     43 #include <stdio.h>
     44 #include <unistd.h>
     45 
     46 ssize_t _sys_write(int, void *, size_t);
     47 
     48 void
     49 __longjmp14(jmp_buf env, int val)
     50 {
     51 	ucontext_t uc;
     52 	struct sigcontext *sc = (void *)env;
     53 	__register_t *regs = (void *)(sc + 1);
     54 	register __register_t dp __asm("r27");
     55 
     56 	/* Ensure non-zero SP */
     57 	if (sc->sc_sp == 0)
     58 		goto err;
     59 
     60 	/* Make return value non-zero */
     61 	if (val == 0)
     62 		val = 1;
     63 
     64 	/* Copy callee-saved regs */
     65 	regs -= 3;
     66 	uc.uc_mcontext.__gregs[3] = regs[3];
     67 	uc.uc_mcontext.__gregs[4] = regs[4];
     68 	uc.uc_mcontext.__gregs[5] = regs[5];
     69 	uc.uc_mcontext.__gregs[6] = regs[6];
     70 	uc.uc_mcontext.__gregs[7] = regs[7];
     71 	uc.uc_mcontext.__gregs[8] = regs[8];
     72 	uc.uc_mcontext.__gregs[9] = regs[9];
     73 	uc.uc_mcontext.__gregs[10] = regs[10];
     74 	uc.uc_mcontext.__gregs[11] = regs[11];
     75 	uc.uc_mcontext.__gregs[12] = regs[12];
     76 	uc.uc_mcontext.__gregs[13] = regs[13];
     77 	uc.uc_mcontext.__gregs[14] = regs[14];
     78 	uc.uc_mcontext.__gregs[15] = regs[15];
     79 	uc.uc_mcontext.__gregs[16] = regs[16];
     80 	uc.uc_mcontext.__gregs[17] = regs[17];
     81 	uc.uc_mcontext.__gregs[18] = regs[18];
     82 
     83 	/* Preserve the current value of DP */
     84 	/* LINTED dp is r27, so is "initialized" */
     85 	uc.uc_mcontext.__gregs[27] = dp;
     86 
     87 	/* Set the desired return value. */
     88 	uc.uc_mcontext.__gregs[_REG_RET0] = val;
     89 
     90 	/*
     91 	 * Set _UC_CPU (restore CPU registers) and _UC_SIGMASK (restore
     92 	 * the signal mask) unconditionally.
     93 	 *
     94 	 * In the distant past of SA-based libpthread with sigprocmask
     95 	 * interception, we called sigprocmask here instead of using
     96 	 * _UC_SIGMASK -- but that restored the signal mask before the
     97 	 * stack pointer (PR lib/57946: longjmp fails to restore stack
     98 	 * first before restoring signal mask on most architectures),
     99 	 * which breaks sigaltstack, and SA-based libpthread is long
    100 	 * gone.  So we use _UC_SIGMASK.
    101 	 *
    102 	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
    103 	 */
    104 	uc.uc_flags = _UC_CPU | _UC_SIGMASK;
    105 	uc.uc_flags |= (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
    106 
    107 	/* Clear uc_link */
    108 	uc.uc_link = 0;
    109 
    110 	/* Copy signal mask */
    111 	uc.uc_sigmask = sc->sc_mask;
    112 
    113 	/* Copy special regs */
    114 	uc.uc_mcontext.__gregs[_REG_PSW] = sc->sc_ps;
    115 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
    116 	uc.uc_mcontext.__gregs[_REG_PCSQH] = sc->sc_pcsqh;
    117 	uc.uc_mcontext.__gregs[_REG_PCOQH] = sc->sc_pcoqh;
    118 	uc.uc_mcontext.__gregs[_REG_PCSQT] = sc->sc_pcsqt;
    119 	uc.uc_mcontext.__gregs[_REG_PCOQT] = sc->sc_pcoqt;
    120 
    121 	setcontext(&uc);
    122  err:
    123 	longjmperror();
    124 	abort();
    125 	/* NOTREACHED */
    126 }
    127