1 /* $NetBSD: longjmp.c,v 1.3 2011/04/30 23:41:12 martin Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christian Limpach.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include "namespace.h"
33 #include <sys/types.h>
40 #define __LIBC12_SOURCE__
42 #include <compat/include/setjmp.h>
44 struct __jmp_buf_regs_t
{
51 * setjmp.S uses hard coded offsets into the jump_buf,
52 * make sure any changes cause a compile failure here
54 __CTASSERT(56 == offsetof(struct __jmp_buf_regs_t
,save_mask
) +
55 sizeof(struct sigcontext
));
56 __CTASSERT(sizeof(sigjmp_buf
) >= sizeof(struct __jmp_buf_regs_t
) +
57 sizeof(struct sigcontext
));
60 * Use setcontext to reload the stack pointer, program counter <pc,npc>, and
61 * set the return value in %o0. The %i and %l registers will be reloaded
62 * from the place to which %sp points.
65 __longjmp14(jmp_buf env
, int val
)
67 struct sigcontext
*sc
= (void *)env
;
68 struct __jmp_buf_regs_t
*r
= (void*)&sc
[1];
71 /* Ensure non-zero SP */
75 /* Initialise the context */
76 memset(&uc
, 0, sizeof(uc
));
79 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
81 * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
82 * since libpthread may want to interpose on signal handling.
84 uc
.uc_flags
= _UC_CPU
| (sc
->sc_onstack
? _UC_SETSTACK
: _UC_CLRSTACK
);
86 sigprocmask(SIG_SETMASK
, &sc
->sc_mask
, NULL
);
88 /* Extract PSR, PC, NPC and SP from jmp_buf */
89 uc
.uc_mcontext
.__gregs
[_REG_PSR
] = sc
->sc_psr
;
90 uc
.uc_mcontext
.__gregs
[_REG_PC
] = sc
->sc_pc
;
91 uc
.uc_mcontext
.__gregs
[_REG_nPC
] = sc
->sc_pc
+4;
92 uc
.uc_mcontext
.__gregs
[_REG_O6
] = sc
->sc_sp
;
93 uc
.uc_mcontext
.__gregs
[_REG_G2
] = sc
->sc_g1
;
94 uc
.uc_mcontext
.__gregs
[_REG_G3
] = sc
->sc_npc
;
95 uc
.uc_mcontext
.__gregs
[_REG_G4
] = r
->g4
;
96 uc
.uc_mcontext
.__gregs
[_REG_G7
] = r
->g7
;
98 /* Set the return value; make sure it's non-zero */
99 uc
.uc_mcontext
.__gregs
[_REG_O0
] = (val
!= 0 ? val
: 1);