]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/mips/gen/longjmp.c
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt, libc++,
[FreeBSD/FreeBSD.git] / lib / libc / mips / gen / longjmp.c
1 /*      $NetBSD: longjmp.c,v 1.1 2005/09/17 11:49:39 tsutsui Exp $      */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2003 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Christian Limpach and Matt Thomas.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/types.h>
38 #include <ucontext.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <machine/regnum.h>
44
45 void
46 __longjmp14(jmp_buf env, int val)
47 {
48         struct sigcontext *sc = (void *)env;
49         ucontext_t uc;
50
51         /* Ensure non-zero SP and sigcontext magic number is present */
52         if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != 0xACEDBADE)
53                 goto err;
54
55         /* Ensure non-zero return value */
56         if (val == 0)
57                 val = 1;
58
59         /*
60          * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
61          *
62          * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
63          * since libpthread may want to interpose on signal handling.
64          */
65         uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
66
67         sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
68
69         /* Clear uc_link */
70         uc.uc_link = 0;
71
72         /* Save return value in context */
73         uc.uc_mcontext.__gregs[_R_V0] = val;
74
75         /* Copy saved registers */
76         uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0];
77         uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1];
78         uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2];
79         uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3];
80         uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4];
81         uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5];
82         uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6];
83         uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7];
84         uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8];
85         uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP];
86         uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA];
87         uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc;
88
89         /* Copy FP state */
90         if (sc->sc_fpused) {
91                 /* FP saved regs are $f20 .. $f31 */
92                 memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20],
93                     &sc->sc_fpregs[20], 32 - 20);
94                 uc.uc_mcontext.__fpregs.__fp_csr =
95                     sc->sc_fpregs[_R_FSR - _FPBASE];
96                 /* XXX sc_fp_control */
97                 uc.uc_flags |= _UC_FPU;
98         }
99
100         setcontext(&uc);
101  err:
102         longjmperror();
103         abort();
104         /* NOTREACHED */
105 }