__longjmp14.c revision 1.1 1 /* $NetBSD: __longjmp14.c,v 1.1 2004/07/18 22:38:33 chs 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
49 #include <stdio.h>
50 #include <unistd.h>
51
52 ssize_t _sys_write(int, void *, size_t);
53
54 void
55 __longjmp14(jmp_buf env, int val)
56 {
57 ucontext_t uc;
58 struct sigcontext *sc = (void *)env;
59 register_t *regs = (void *)(sc + 1);
60 register register_t dp __asm__("r27");
61
62 /* Ensure non-zero SP */
63 if (sc->sc_sp == 0)
64 goto err;
65
66 /* Make return value non-zero */
67 if (val == 0)
68 val = 1;
69
70 /* Copy callee-saved regs */
71 regs -= 3;
72 uc.uc_mcontext.__gregs[3] = regs[3];
73 uc.uc_mcontext.__gregs[4] = regs[4];
74 uc.uc_mcontext.__gregs[5] = regs[5];
75 uc.uc_mcontext.__gregs[6] = regs[6];
76 uc.uc_mcontext.__gregs[7] = regs[7];
77 uc.uc_mcontext.__gregs[8] = regs[8];
78 uc.uc_mcontext.__gregs[9] = regs[9];
79 uc.uc_mcontext.__gregs[10] = regs[10];
80 uc.uc_mcontext.__gregs[11] = regs[11];
81 uc.uc_mcontext.__gregs[12] = regs[12];
82 uc.uc_mcontext.__gregs[13] = regs[13];
83 uc.uc_mcontext.__gregs[14] = regs[14];
84 uc.uc_mcontext.__gregs[15] = regs[15];
85 uc.uc_mcontext.__gregs[16] = regs[16];
86 uc.uc_mcontext.__gregs[17] = regs[17];
87 uc.uc_mcontext.__gregs[18] = regs[18];
88
89 /* Preserve the current value of DP */
90 uc.uc_mcontext.__gregs[27] = dp;
91
92 /* Set the desired return value. */
93 uc.uc_mcontext.__gregs[_REG_RET0] = val;
94
95 /*
96 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
97 *
98 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
99 * since libpthread may want to interpose on signal handling.
100 */
101 uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
102
103 sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
104
105 /* Clear uc_link */
106 uc.uc_link = 0;
107
108 /* Copy signal mask */
109 uc.uc_sigmask = sc->sc_mask;
110
111 /* Copy special regs */
112 uc.uc_mcontext.__gregs[_REG_PSW] = sc->sc_ps;
113 uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
114 uc.uc_mcontext.__gregs[_REG_PCSQH] = sc->sc_pcsqh;
115 uc.uc_mcontext.__gregs[_REG_PCOQH] = sc->sc_pcoqh;
116 uc.uc_mcontext.__gregs[_REG_PCSQT] = sc->sc_pcsqt;
117 uc.uc_mcontext.__gregs[_REG_PCOQT] = sc->sc_pcoqt;
118
119 setcontext(&uc);
120 err:
121 longjmperror();
122 abort();
123 /* NOTREACHED */
124 }
125