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 * 3. All advertising materials mentioning features or use of this software
19178580Simp *    must display the following acknowledgement:
20178580Simp *        This product includes software developed by the NetBSD
21178580Simp *        Foundation, Inc. and its contributors.
22178580Simp * 4. Neither the name of The NetBSD Foundation nor the names of its
23178580Simp *    contributors may be used to endorse or promote products derived
24178580Simp *    from this software without specific prior written permission.
25178580Simp *
26178580Simp * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27178580Simp * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28178580Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29178580Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30178580Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31178580Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32178580Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33178580Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34178580Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35178580Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36178580Simp * POSSIBILITY OF SUCH DAMAGE.
37178580Simp */
38178580Simp
39178580Simp#include <sys/cdefs.h>
40178580Simp__FBSDID("$FreeBSD$");
41178580Simp#include "namespace.h"
42178580Simp#include <sys/types.h>
43178580Simp#include <ucontext.h>
44178580Simp#include <signal.h>
45178580Simp#include <stdlib.h>
46178580Simp#include <string.h>
47178580Simp
48178580Simp#include <machine/regnum.h>
49178580Simp
50178580Simpvoid
51178580Simp__longjmp14(jmp_buf env, int val)
52178580Simp{
53178580Simp	struct sigcontext *sc = (void *)env;
54178580Simp	ucontext_t uc;
55178580Simp
56178580Simp	/* Ensure non-zero SP and sigcontext magic number is present */
57178580Simp	if (sc->sc_regs[_R_SP] == 0 || sc->sc_regs[_R_ZERO] != 0xACEDBADE)
58178580Simp		goto err;
59178580Simp
60178580Simp	/* Ensure non-zero return value */
61178580Simp	if (val == 0)
62178580Simp		val = 1;
63178580Simp
64178580Simp	/*
65178580Simp	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
66178580Simp	 *
67178580Simp	 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
68178580Simp	 * since libpthread may want to interpose on signal handling.
69178580Simp	 */
70178580Simp	uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
71178580Simp
72178580Simp	sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
73178580Simp
74178580Simp	/* Clear uc_link */
75178580Simp	uc.uc_link = 0;
76178580Simp
77178580Simp	/* Save return value in context */
78178580Simp	uc.uc_mcontext.__gregs[_R_V0] = val;
79178580Simp
80178580Simp	/* Copy saved registers */
81178580Simp	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[_R_S0];
82178580Simp	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[_R_S1];
83178580Simp	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[_R_S2];
84178580Simp	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[_R_S3];
85178580Simp	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[_R_S4];
86178580Simp	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[_R_S5];
87178580Simp	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[_R_S6];
88178580Simp	uc.uc_mcontext.__gregs[_REG_S7] = sc->sc_regs[_R_S7];
89178580Simp	uc.uc_mcontext.__gregs[_REG_S8] = sc->sc_regs[_R_S8];
90178580Simp	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_regs[_R_SP];
91178580Simp	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[_R_RA];
92178580Simp	uc.uc_mcontext.__gregs[_REG_EPC] = sc->sc_pc;
93178580Simp
94178580Simp	/* Copy FP state */
95178580Simp	if (sc->sc_fpused) {
96178580Simp		/* FP saved regs are $f20 .. $f31 */
97178580Simp		memcpy(&uc.uc_mcontext.__fpregs.__fp_r.__fp_regs[20],
98178580Simp		    &sc->sc_fpregs[20], 32 - 20);
99178580Simp		uc.uc_mcontext.__fpregs.__fp_csr =
100178580Simp		    sc->sc_fpregs[_R_FSR - _FPBASE];
101178580Simp		/* XXX sc_fp_control */
102178580Simp		uc.uc_flags |= _UC_FPU;
103178580Simp	}
104178580Simp
105178580Simp	setcontext(&uc);
106178580Simp err:
107178580Simp	longjmperror();
108178580Simp	abort();
109178580Simp	/* NOTREACHED */
110178580Simp}
111