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