1 1.7 riastrad /* $NetBSD: longjmp.c,v 1.7 2025/04/24 01:47:55 riastradh Exp $ */ 2 1.1 tsutsui 3 1.1 tsutsui /*- 4 1.1 tsutsui * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 1.1 tsutsui * All rights reserved. 6 1.1 tsutsui * 7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation 8 1.1 tsutsui * by Christian Limpach and Matt Thomas. 9 1.1 tsutsui * 10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 11 1.1 tsutsui * modification, are permitted provided that the following conditions 12 1.1 tsutsui * are met: 13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 14 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 17 1.1 tsutsui * documentation and/or other materials provided with the distribution. 18 1.1 tsutsui * 19 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE. 30 1.1 tsutsui */ 31 1.1 tsutsui 32 1.1 tsutsui #include "namespace.h" 33 1.1 tsutsui #include <sys/types.h> 34 1.1 tsutsui #include <ucontext.h> 35 1.1 tsutsui #include <signal.h> 36 1.1 tsutsui #include <stdlib.h> 37 1.1 tsutsui #include <string.h> 38 1.1 tsutsui 39 1.1 tsutsui #include <machine/regnum.h> 40 1.1 tsutsui 41 1.1 tsutsui #define __LIBC12_SOURCE__ 42 1.1 tsutsui #include <setjmp.h> 43 1.1 tsutsui #include <compat/include/setjmp.h> 44 1.1 tsutsui 45 1.1 tsutsui void 46 1.1 tsutsui __longjmp14(jmp_buf env, int val) 47 1.1 tsutsui { 48 1.1 tsutsui struct sigcontext *sc = (void *)env; 49 1.1 tsutsui ucontext_t uc; 50 1.1 tsutsui 51 1.1 tsutsui /* Ensure non-zero SP and sigcontext magic number is present */ 52 1.6 christos if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != (__register_t)0xACEDBADEU) 53 1.1 tsutsui goto err; 54 1.1 tsutsui 55 1.1 tsutsui /* Ensure non-zero return value */ 56 1.1 tsutsui if (val == 0) 57 1.1 tsutsui val = 1; 58 1.1 tsutsui 59 1.1 tsutsui /* 60 1.7 riastrad * Set _UC_CPU (restore CPU registers) and _UC_SIGMASK (restore 61 1.7 riastrad * the signal mask) unconditionally. 62 1.7 riastrad * 63 1.7 riastrad * In the distant past of SA-based libpthread with sigprocmask 64 1.7 riastrad * interception, we called sigprocmask here instead of using 65 1.7 riastrad * _UC_SIGMASK -- but that restored the signal mask before the 66 1.7 riastrad * stack pointer (PR lib/57946: longjmp fails to restore stack 67 1.7 riastrad * first before restoring signal mask on most architectures), 68 1.7 riastrad * which breaks sigaltstack, and SA-based libpthread is long 69 1.7 riastrad * gone. So we use _UC_SIGMASK. 70 1.7 riastrad * 71 1.1 tsutsui * Set _UC_{SET,CLR}STACK according to SS_ONSTACK. 72 1.1 tsutsui */ 73 1.7 riastrad uc.uc_flags = _UC_CPU | _UC_SIGMASK; 74 1.7 riastrad uc.uc_flags |= (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK); 75 1.1 tsutsui 76 1.1 tsutsui /* Clear uc_link */ 77 1.1 tsutsui uc.uc_link = 0; 78 1.1 tsutsui 79 1.7 riastrad /* Copy signal mask */ 80 1.7 riastrad uc.uc_sigmask = sc->sc_mask; 81 1.7 riastrad 82 1.1 tsutsui /* Save return value in context */ 83 1.4 matt uc.uc_mcontext.__gregs[_REG_V0] = val; 84 1.1 tsutsui 85 1.1 tsutsui /* Copy saved registers */ 86 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0]; 87 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1]; 88 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2]; 89 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3]; 90 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4]; 91 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5]; 92 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6]; 93 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7]; 94 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8]; 95 1.4 matt #if defined(__mips_n32) || defined(__mips_n64) 96 1.4 matt uc.uc_mcontext.__gregs[_REG_GP] = sc->sc_regs[_R_GP]; 97 1.4 matt #endif 98 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP]; 99 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA]; 100 1.1 tsutsui uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc; 101 1.1 tsutsui 102 1.1 tsutsui /* Copy FP state */ 103 1.1 tsutsui if (sc->sc_fpused) { 104 1.1 tsutsui /* FP saved regs are $f20 .. $f31 */ 105 1.1 tsutsui memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20], 106 1.1 tsutsui &sc->sc_fpregs[20], 32 - 20); 107 1.1 tsutsui uc.uc_mcontext.__fpregs.__fp_csr = 108 1.1 tsutsui sc->sc_fpregs[_R_FSR - _FPBASE]; 109 1.1 tsutsui /* XXX sc_fp_control */ 110 1.1 tsutsui uc.uc_flags |= _UC_FPU; 111 1.1 tsutsui } 112 1.1 tsutsui 113 1.1 tsutsui setcontext(&uc); 114 1.1 tsutsui err: 115 1.1 tsutsui longjmperror(); 116 1.1 tsutsui abort(); 117 1.1 tsutsui /* NOTREACHED */ 118 1.1 tsutsui } 119