1178580Simp/*	$NetBSD: longjmp.c,v 1.1 2005/09/17 11:49:39 tsutsui Exp $	*/
2178580Simp
3178580Simp/*-
4178580Simp * Copyright (c) 2003 The NetBSD Foundation, Inc.
5178580Simp * All rights reserved.
6178580Simp *
7178580Simp * This code is derived from software contributed to The NetBSD Foundation
8178580Simp * by Christian Limpach and Matt Thomas.
9178580Simp *
10178580Simp * Redistribution and use in source and binary forms, with or without
11178580Simp * modification, are permitted provided that the following conditions
12178580Simp * are met:
13178580Simp * 1. Redistributions of source code must retain the above copyright
14178580Simp *    notice, this list of conditions and the following disclaimer.
15178580Simp * 2. Redistributions in binary form must reproduce the above copyright
16178580Simp *    notice, this list of conditions and the following disclaimer in the
17178580Simp *    documentation and/or other materials provided with the distribution.
18178580Simp *
19178580Simp * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20178580Simp * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21178580Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22178580Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23178580Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24178580Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25178580Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26178580Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27178580Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28178580Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29178580Simp * POSSIBILITY OF SUCH DAMAGE.
30178580Simp */
31178580Simp
32178580Simp#include <sys/cdefs.h>
33178580Simp__FBSDID("$FreeBSD: releng/10.3/lib/libc/mips/gen/longjmp.c 251091 2013-05-29 01:54:10Z emaste $");
34178580Simp#include "namespace.h"
35178580Simp#include <sys/types.h>
36178580Simp#include <ucontext.h>
37178580Simp#include <signal.h>
38178580Simp#include <stdlib.h>
39178580Simp#include <string.h>
40178580Simp
41178580Simp#include <machine/regnum.h>
42178580Simp
43178580Simpvoid
44178580Simp__longjmp14(jmp_buf env, int val)
45178580Simp{
46178580Simp	struct sigcontext *sc = (void *)env;
47178580Simp	ucontext_t uc;
48178580Simp
49178580Simp	/* Ensure non-zero SP and sigcontext magic number is present */
50178580Simp	if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != 0xACEDBADE)
51178580Simp		goto err;
52178580Simp
53178580Simp	/* Ensure non-zero return value */
54178580Simp	if (val == 0)
55178580Simp		val = 1;
56178580Simp
57178580Simp	/*
58178580Simp	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
59178580Simp	 *
60178580Simp	 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
61178580Simp	 * since libpthread may want to interpose on signal handling.
62178580Simp	 */
63178580Simp	uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
64178580Simp
65178580Simp	sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
66178580Simp
67178580Simp	/* Clear uc_link */
68178580Simp	uc.uc_link = 0;
69178580Simp
70178580Simp	/* Save return value in context */
71178580Simp	uc.uc_mcontext.__gregs[_R_V0] = val;
72178580Simp
73178580Simp	/* Copy saved registers */
74178580Simp	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0];
75178580Simp	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1];
76178580Simp	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2];
77178580Simp	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3];
78178580Simp	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4];
79178580Simp	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5];
80178580Simp	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6];
81178580Simp	uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7];
82178580Simp	uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8];
83178580Simp	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP];
84178580Simp	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA];
85178580Simp	uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc;
86178580Simp
87178580Simp	/* Copy FP state */
88178580Simp	if (sc->sc_fpused) {
89178580Simp		/* FP saved regs are $f20 .. $f31 */
90178580Simp		memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20],
91178580Simp		    &sc->sc_fpregs[20], 32 - 20);
92178580Simp		uc.uc_mcontext.__fpregs.__fp_csr =
93178580Simp		    sc->sc_fpregs[_R_FSR - _FPBASE];
94178580Simp		/* XXX sc_fp_control */
95178580Simp		uc.uc_flags |= _UC_FPU;
96178580Simp	}
97178580Simp
98178580Simp	setcontext(&uc);
99178580Simp err:
100178580Simp	longjmperror();
101178580Simp	abort();
102178580Simp	/* NOTREACHED */
103178580Simp}
104