1*e415d488SLionel Sambuc /* $NetBSD: longjmp.c,v 1.7 2012/03/17 21:35:06 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>
382fe8fb19SBen Gras #include <stdio.h>
39*e415d488SLionel Sambuc #include <stddef.h>
402fe8fb19SBen Gras
412fe8fb19SBen Gras #define __LIBC12_SOURCE__
422fe8fb19SBen Gras #include <setjmp.h>
432fe8fb19SBen Gras #include <compat/include/setjmp.h>
442fe8fb19SBen Gras
452fe8fb19SBen Gras typedef struct {
46*e415d488SLionel Sambuc __greg_t g3;
47*e415d488SLionel Sambuc __greg_t g6;
48*e415d488SLionel Sambuc __greg_t g7;
49*e415d488SLionel Sambuc __greg_t dummy;
50*e415d488SLionel Sambuc __greg_t save_mask;
512fe8fb19SBen Gras } __jmp_buf_regs_t;
522fe8fb19SBen Gras
53*e415d488SLionel Sambuc /*
54*e415d488SLionel Sambuc * setjmp.S uses hard coded offsets into the jump_buf,
55*e415d488SLionel Sambuc * make sure any changes cause a compile failure here
56*e415d488SLionel Sambuc */
57*e415d488SLionel Sambuc #ifndef lint /* XXX this is too much for lint */
58*e415d488SLionel Sambuc __CTASSERT(0x68 == offsetof(__jmp_buf_regs_t,save_mask) +
59*e415d488SLionel Sambuc sizeof(struct sigcontext));
60*e415d488SLionel Sambuc __CTASSERT(sizeof(sigjmp_buf) >= sizeof(__jmp_buf_regs_t) +
61*e415d488SLionel Sambuc sizeof(struct sigcontext));
62*e415d488SLionel Sambuc #endif
63*e415d488SLionel Sambuc
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;
682fe8fb19SBen Gras __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
752fe8fb19SBen Gras /*
762fe8fb19SBen Gras * Set _UC_CPU. No FPU data saved, so we can't restore
772fe8fb19SBen Gras * that. Set _UC_{SET,CLR}STACK according to SS_ONSTACK
782fe8fb19SBen Gras */
792fe8fb19SBen Gras memset(&uc, 0, sizeof(uc));
802fe8fb19SBen Gras uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
812fe8fb19SBen Gras
822fe8fb19SBen Gras /*
832fe8fb19SBen Gras * Set the signal mask - this is a weak symbol, so don't use
842fe8fb19SBen Gras * _UC_SIGMASK in the mcontext, libpthread might override sigprocmask.
852fe8fb19SBen Gras */
862fe8fb19SBen Gras sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
872fe8fb19SBen Gras
882fe8fb19SBen Gras /* Fill other registers */
892fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_CCR] = sc->sc_tstate;
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_G1] = sc->sc_g1;
932fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_G2] = sc->sc_o0;
94*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G3] = r->g3;
95*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G4] = 0;
96*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G5] = 0;
97*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G6] = r->g6;
98*e415d488SLionel Sambuc uc.uc_mcontext.__gregs[_REG_G7] = r->g7;
992fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp;
1002fe8fb19SBen Gras
101*e415d488SLionel Sambuc
1022fe8fb19SBen Gras /* Make return value non-zero */
1032fe8fb19SBen Gras if (val == 0)
1042fe8fb19SBen Gras val = 1;
1052fe8fb19SBen Gras
1062fe8fb19SBen Gras /* Save return value in context */
1072fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_O0] = val;
1082fe8fb19SBen Gras
1092fe8fb19SBen Gras setcontext(&uc);
1102fe8fb19SBen Gras
1112fe8fb19SBen Gras err:
1122fe8fb19SBen Gras longjmperror();
1132fe8fb19SBen Gras abort();
1142fe8fb19SBen Gras /* NOTREACHED */
1152fe8fb19SBen Gras }
116