1*e415d488SLionel Sambuc /* $NetBSD: __longjmp14.c,v 1.5 2012/03/22 12:31:32 skrll 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
392fe8fb19SBen Gras #define __LIBC12_SOURCE__
402fe8fb19SBen Gras #include <setjmp.h>
412fe8fb19SBen Gras #include <compat/include/setjmp.h>
422fe8fb19SBen Gras
432fe8fb19SBen Gras #include <stdio.h>
442fe8fb19SBen Gras #include <unistd.h>
452fe8fb19SBen Gras
462fe8fb19SBen Gras ssize_t _sys_write(int, void *, size_t);
472fe8fb19SBen Gras
482fe8fb19SBen Gras void
__longjmp14(jmp_buf env,int val)492fe8fb19SBen Gras __longjmp14(jmp_buf env, int val)
502fe8fb19SBen Gras {
512fe8fb19SBen Gras ucontext_t uc;
522fe8fb19SBen Gras struct sigcontext *sc = (void *)env;
532fe8fb19SBen Gras register_t *regs = (void *)(sc + 1);
542fe8fb19SBen Gras register register_t dp __asm("r27");
552fe8fb19SBen Gras
562fe8fb19SBen Gras /* Ensure non-zero SP */
572fe8fb19SBen Gras if (sc->sc_sp == 0)
582fe8fb19SBen Gras goto err;
592fe8fb19SBen Gras
602fe8fb19SBen Gras /* Make return value non-zero */
612fe8fb19SBen Gras if (val == 0)
622fe8fb19SBen Gras val = 1;
632fe8fb19SBen Gras
642fe8fb19SBen Gras /* Copy callee-saved regs */
652fe8fb19SBen Gras regs -= 3;
662fe8fb19SBen Gras uc.uc_mcontext.__gregs[3] = regs[3];
672fe8fb19SBen Gras uc.uc_mcontext.__gregs[4] = regs[4];
682fe8fb19SBen Gras uc.uc_mcontext.__gregs[5] = regs[5];
692fe8fb19SBen Gras uc.uc_mcontext.__gregs[6] = regs[6];
702fe8fb19SBen Gras uc.uc_mcontext.__gregs[7] = regs[7];
712fe8fb19SBen Gras uc.uc_mcontext.__gregs[8] = regs[8];
722fe8fb19SBen Gras uc.uc_mcontext.__gregs[9] = regs[9];
732fe8fb19SBen Gras uc.uc_mcontext.__gregs[10] = regs[10];
742fe8fb19SBen Gras uc.uc_mcontext.__gregs[11] = regs[11];
752fe8fb19SBen Gras uc.uc_mcontext.__gregs[12] = regs[12];
762fe8fb19SBen Gras uc.uc_mcontext.__gregs[13] = regs[13];
772fe8fb19SBen Gras uc.uc_mcontext.__gregs[14] = regs[14];
782fe8fb19SBen Gras uc.uc_mcontext.__gregs[15] = regs[15];
792fe8fb19SBen Gras uc.uc_mcontext.__gregs[16] = regs[16];
802fe8fb19SBen Gras uc.uc_mcontext.__gregs[17] = regs[17];
812fe8fb19SBen Gras uc.uc_mcontext.__gregs[18] = regs[18];
822fe8fb19SBen Gras
832fe8fb19SBen Gras /* Preserve the current value of DP */
84*e415d488SLionel Sambuc /* LINTED dp is r27, so is "initialized" */
852fe8fb19SBen Gras uc.uc_mcontext.__gregs[27] = dp;
862fe8fb19SBen Gras
872fe8fb19SBen Gras /* Set the desired return value. */
882fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_RET0] = val;
892fe8fb19SBen Gras
902fe8fb19SBen Gras /*
912fe8fb19SBen Gras * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
922fe8fb19SBen Gras *
932fe8fb19SBen Gras * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
942fe8fb19SBen Gras * since libpthread may want to interpose on signal handling.
952fe8fb19SBen Gras */
962fe8fb19SBen Gras uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
972fe8fb19SBen Gras
982fe8fb19SBen Gras sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
992fe8fb19SBen Gras
1002fe8fb19SBen Gras /* Clear uc_link */
1012fe8fb19SBen Gras uc.uc_link = 0;
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras /* Copy signal mask */
1042fe8fb19SBen Gras uc.uc_sigmask = sc->sc_mask;
1052fe8fb19SBen Gras
1062fe8fb19SBen Gras /* Copy special regs */
1072fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_PSW] = sc->sc_ps;
1082fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
1092fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_PCSQH] = sc->sc_pcsqh;
1102fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_PCOQH] = sc->sc_pcoqh;
1112fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_PCSQT] = sc->sc_pcsqt;
1122fe8fb19SBen Gras uc.uc_mcontext.__gregs[_REG_PCOQT] = sc->sc_pcoqt;
1132fe8fb19SBen Gras
1142fe8fb19SBen Gras setcontext(&uc);
1152fe8fb19SBen Gras err:
1162fe8fb19SBen Gras longjmperror();
1172fe8fb19SBen Gras abort();
1182fe8fb19SBen Gras /* NOTREACHED */
1192fe8fb19SBen Gras }
120