xref: /minix3/lib/libc/arch/hppa/gen/__longjmp14.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*	$NetBSD: __longjmp14.c,v 1.4 2008/04/28 20:22:55 martin Exp $	*/
2*2fe8fb19SBen Gras 
3*2fe8fb19SBen Gras /*-
4*2fe8fb19SBen Gras  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5*2fe8fb19SBen Gras  * All rights reserved.
6*2fe8fb19SBen Gras  *
7*2fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
8*2fe8fb19SBen Gras  * by Christian Limpach.
9*2fe8fb19SBen Gras  *
10*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
11*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
12*2fe8fb19SBen Gras  * are met:
13*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
14*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
15*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
17*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
18*2fe8fb19SBen Gras  *
19*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*2fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*2fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*2fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*2fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*2fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*2fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*2fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*2fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*2fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*2fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
30*2fe8fb19SBen Gras  */
31*2fe8fb19SBen Gras 
32*2fe8fb19SBen Gras #include "namespace.h"
33*2fe8fb19SBen Gras #include <sys/types.h>
34*2fe8fb19SBen Gras #include <ucontext.h>
35*2fe8fb19SBen Gras #include <signal.h>
36*2fe8fb19SBen Gras #include <stdlib.h>
37*2fe8fb19SBen Gras #include <string.h>
38*2fe8fb19SBen Gras 
39*2fe8fb19SBen Gras #define __LIBC12_SOURCE__
40*2fe8fb19SBen Gras #include <setjmp.h>
41*2fe8fb19SBen Gras #include <compat/include/setjmp.h>
42*2fe8fb19SBen Gras 
43*2fe8fb19SBen Gras #include <stdio.h>
44*2fe8fb19SBen Gras #include <unistd.h>
45*2fe8fb19SBen Gras 
46*2fe8fb19SBen Gras ssize_t _sys_write(int, void *, size_t);
47*2fe8fb19SBen Gras 
48*2fe8fb19SBen Gras void
49*2fe8fb19SBen Gras __longjmp14(jmp_buf env, int val)
50*2fe8fb19SBen Gras {
51*2fe8fb19SBen Gras 	ucontext_t uc;
52*2fe8fb19SBen Gras 	struct sigcontext *sc = (void *)env;
53*2fe8fb19SBen Gras 	register_t *regs = (void *)(sc + 1);
54*2fe8fb19SBen Gras 	register register_t dp __asm("r27");
55*2fe8fb19SBen Gras 
56*2fe8fb19SBen Gras 	/* Ensure non-zero SP */
57*2fe8fb19SBen Gras 	if (sc->sc_sp == 0)
58*2fe8fb19SBen Gras 		goto err;
59*2fe8fb19SBen Gras 
60*2fe8fb19SBen Gras 	/* Make return value non-zero */
61*2fe8fb19SBen Gras 	if (val == 0)
62*2fe8fb19SBen Gras 		val = 1;
63*2fe8fb19SBen Gras 
64*2fe8fb19SBen Gras 	/* Copy callee-saved regs */
65*2fe8fb19SBen Gras 	regs -= 3;
66*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[3] = regs[3];
67*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[4] = regs[4];
68*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[5] = regs[5];
69*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[6] = regs[6];
70*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[7] = regs[7];
71*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[8] = regs[8];
72*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[9] = regs[9];
73*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[10] = regs[10];
74*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[11] = regs[11];
75*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[12] = regs[12];
76*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[13] = regs[13];
77*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[14] = regs[14];
78*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[15] = regs[15];
79*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[16] = regs[16];
80*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[17] = regs[17];
81*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[18] = regs[18];
82*2fe8fb19SBen Gras 
83*2fe8fb19SBen Gras 	/* Preserve the current value of DP */
84*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[27] = dp;
85*2fe8fb19SBen Gras 
86*2fe8fb19SBen Gras 	/* Set the desired return value. */
87*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_RET0] = val;
88*2fe8fb19SBen Gras 
89*2fe8fb19SBen Gras 	/*
90*2fe8fb19SBen Gras 	 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
91*2fe8fb19SBen Gras 	 *
92*2fe8fb19SBen Gras 	 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
93*2fe8fb19SBen Gras 	 * since libpthread may want to interpose on signal handling.
94*2fe8fb19SBen Gras 	 */
95*2fe8fb19SBen Gras 	uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
96*2fe8fb19SBen Gras 
97*2fe8fb19SBen Gras 	sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
98*2fe8fb19SBen Gras 
99*2fe8fb19SBen Gras 	/* Clear uc_link */
100*2fe8fb19SBen Gras 	uc.uc_link = 0;
101*2fe8fb19SBen Gras 
102*2fe8fb19SBen Gras 	/* Copy signal mask */
103*2fe8fb19SBen Gras 	uc.uc_sigmask = sc->sc_mask;
104*2fe8fb19SBen Gras 
105*2fe8fb19SBen Gras 	/* Copy special regs */
106*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_PSW] = sc->sc_ps;
107*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
108*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_PCSQH] = sc->sc_pcsqh;
109*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_PCOQH] = sc->sc_pcoqh;
110*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_PCSQT] = sc->sc_pcsqt;
111*2fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_PCOQT] = sc->sc_pcoqt;
112*2fe8fb19SBen Gras 
113*2fe8fb19SBen Gras 	setcontext(&uc);
114*2fe8fb19SBen Gras  err:
115*2fe8fb19SBen Gras 	longjmperror();
116*2fe8fb19SBen Gras 	abort();
117*2fe8fb19SBen Gras 	/* NOTREACHED */
118*2fe8fb19SBen Gras }
119