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