longjmp.c revision 1.2.8.2 1 1.2.8.2 martin /* $NetBSD: longjmp.c,v 1.2.8.2 2008/04/28 20:22:58 martin Exp $ */
2 1.2.8.2 martin
3 1.2.8.2 martin /*-
4 1.2.8.2 martin * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.2.8.2 martin * All rights reserved.
6 1.2.8.2 martin *
7 1.2.8.2 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.2.8.2 martin * by Christian Limpach.
9 1.2.8.2 martin *
10 1.2.8.2 martin * Redistribution and use in source and binary forms, with or without
11 1.2.8.2 martin * modification, are permitted provided that the following conditions
12 1.2.8.2 martin * are met:
13 1.2.8.2 martin * 1. Redistributions of source code must retain the above copyright
14 1.2.8.2 martin * notice, this list of conditions and the following disclaimer.
15 1.2.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.8.2 martin * notice, this list of conditions and the following disclaimer in the
17 1.2.8.2 martin * documentation and/or other materials provided with the distribution.
18 1.2.8.2 martin *
19 1.2.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.8.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.8.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.8.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.8.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.8.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.8.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.8.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.8.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.8.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.8.2 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.2.8.2 martin */
31 1.2.8.2 martin
32 1.2.8.2 martin #include "namespace.h"
33 1.2.8.2 martin #include <sys/types.h>
34 1.2.8.2 martin #include <ucontext.h>
35 1.2.8.2 martin #include <signal.h>
36 1.2.8.2 martin #include <stdlib.h>
37 1.2.8.2 martin #include <string.h>
38 1.2.8.2 martin
39 1.2.8.2 martin #define __LIBC12_SOURCE__
40 1.2.8.2 martin #include <setjmp.h>
41 1.2.8.2 martin #include <compat/include/setjmp.h>
42 1.2.8.2 martin
43 1.2.8.2 martin /*
44 1.2.8.2 martin * Use setcontext to reload the stack pointer, program counter <pc,npc>, and
45 1.2.8.2 martin * set the return value in %o0. The %i and %l registers will be reloaded
46 1.2.8.2 martin * from the place to which %sp points.
47 1.2.8.2 martin */
48 1.2.8.2 martin void
49 1.2.8.2 martin __longjmp14(jmp_buf env, int val)
50 1.2.8.2 martin {
51 1.2.8.2 martin struct sigcontext *sc = (void *)env;
52 1.2.8.2 martin ucontext_t uc;
53 1.2.8.2 martin
54 1.2.8.2 martin /* Ensure non-zero SP */
55 1.2.8.2 martin if (sc->sc_sp == 0)
56 1.2.8.2 martin goto err;
57 1.2.8.2 martin
58 1.2.8.2 martin /* Initialise the fields we're going to use */
59 1.2.8.2 martin uc.uc_link = 0;
60 1.2.8.2 martin
61 1.2.8.2 martin /*
62 1.2.8.2 martin * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
63 1.2.8.2 martin *
64 1.2.8.2 martin * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
65 1.2.8.2 martin * since libpthread may want to interpose on signal handling.
66 1.2.8.2 martin */
67 1.2.8.2 martin uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
68 1.2.8.2 martin
69 1.2.8.2 martin sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
70 1.2.8.2 martin
71 1.2.8.2 martin /* Extract PSR, PC, NPC and SP from jmp_buf */
72 1.2.8.2 martin uc.uc_mcontext.__gregs[_REG_PSR] = sc->sc_psr;
73 1.2.8.2 martin uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
74 1.2.8.2 martin uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_npc;
75 1.2.8.2 martin uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp;
76 1.2.8.2 martin
77 1.2.8.2 martin /* Set the return value; make sure it's non-zero */
78 1.2.8.2 martin uc.uc_mcontext.__gregs[_REG_O0] = (val != 0 ? val : 1);
79 1.2.8.2 martin
80 1.2.8.2 martin setcontext(&uc);
81 1.2.8.2 martin
82 1.2.8.2 martin err:
83 1.2.8.2 martin longjmperror();
84 1.2.8.2 martin abort();
85 1.2.8.2 martin /* NOTREACHED */
86 1.2.8.2 martin }
87