1*e415d488SLionel Sambuc /* $NetBSD: longjmp.c,v 1.3 2011/04/30 23:41:12 martin Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 2003 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Christian Limpach.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include "namespace.h"
332fe8fb19SBen Gras #include <sys/types.h>
342fe8fb19SBen Gras #include <ucontext.h>
352fe8fb19SBen Gras #include <signal.h>
362fe8fb19SBen Gras #include <stdlib.h>
372fe8fb19SBen Gras #include <string.h>
38*e415d488SLionel Sambuc #include <stddef.h>
392fe8fb19SBen Gras
402fe8fb19SBen Gras #define __LIBC12_SOURCE__
412fe8fb19SBen Gras #include <setjmp.h>
422fe8fb19SBen Gras #include <compat/include/setjmp.h>
432fe8fb19SBen Gras
44*e415d488SLionel Sambuc struct __jmp_buf_regs_t {
45*e415d488SLionel Sambuc __greg_t g4;
46*e415d488SLionel Sambuc __greg_t g7;
47*e415d488SLionel Sambuc __greg_t save_mask;
48*e415d488SLionel Sambuc };
49*e415d488SLionel Sambuc
50*e415d488SLionel Sambuc /*
51*e415d488SLionel Sambuc * setjmp.S uses hard coded offsets into the jump_buf,
52*e415d488SLionel Sambuc * make sure any changes cause a compile failure here
53*e415d488SLionel Sambuc */
54*e415d488SLionel Sambuc __CTASSERT(56 == offsetof(struct __jmp_buf_regs_t,save_mask) +
55*e415d488SLionel Sambuc sizeof(struct sigcontext));
56*e415d488SLionel Sambuc __CTASSERT(sizeof(sigjmp_buf) >= sizeof(struct __jmp_buf_regs_t) +
57*e415d488SLionel Sambuc sizeof(struct sigcontext));
58*e415d488SLionel Sambuc
592fe8fb19SBen Gras /*
602fe8fb19SBen Gras * Use setcontext to reload the stack pointer, program counter <pc,npc>, and
612fe8fb19SBen Gras * set the return value in %o0. The %i and %l registers will be reloaded
622fe8fb19SBen Gras * from the place to which %sp points.
632fe8fb19SBen Gras */
642fe8fb19SBen Gras void
__longjmp14(jmp_buf env,int val)652fe8fb19SBen Gras __longjmp14(jmp_buf env, int val)
662fe8fb19SBen Gras {
672fe8fb19SBen Gras struct sigcontext *sc = (void *)env;
68*e415d488SLionel Sambuc struct __jmp_buf_regs_t *r = (void*)&sc[1];
692fe8fb19SBen Gras ucontext_t uc;
702fe8fb19SBen Gras
712fe8fb19SBen Gras /* Ensure non-zero SP */
722fe8fb19SBen Gras if (sc->sc_sp == 0)
732fe8fb19SBen Gras goto err;
742fe8fb19SBen Gras
75*e415d488SLionel Sambuc /* Initialise the context */
76*e415d488SLionel Sambuc memset(&uc, 0, sizeof(uc));
772fe8fb19SBen Gras
782fe8fb19SBen Gras /*
792fe8fb19SBen Gras * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
802fe8fb19SBen Gras *
812fe8fb19SBen Gras * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
822fe8fb19SBen Gras * since libpthread may want to interpose on signal handling.
832fe8fb19SBen Gras */
842fe8fb19SBen Gras uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
852fe8fb19SBen Gras
862fe8fb19SBen Gras sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
872fe8fb19SBen Gras
882fe8fb19SBen Gras /* Extract PSR, PC, NPC and SP from jmp_buf */
892fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_PSR] = sc->sc_psr;
902fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
91*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_pc+4;
922fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp;
93*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G2] = sc->sc_g1;
94*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G3] = sc->sc_npc;
95*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G4] = r->g4;
96*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G7] = r->g7;
972fe8fb19SBen Gras
982fe8fb19SBen Gras /* Set the return value; make sure it's non-zero */
992fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_O0] = (val != 0 ? val : 1);
1002fe8fb19SBen Gras
1012fe8fb19SBen Gras setcontext(&uc);
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras err:
1042fe8fb19SBen Gras longjmperror();
1052fe8fb19SBen Gras abort();
1062fe8fb19SBen Gras /* NOTREACHED */
1072fe8fb19SBen Gras }
108