xref: /minix3/lib/libc/arch/alpha/gen/__longjmp14.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: __longjmp14.c,v 1.7 2013/03/13 08:05:46 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 and Matt Thomas.
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 <machine/reg.h>
392fe8fb19SBen Gras #include <machine/alpha_cpu.h>
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #define __LIBC12_SOURCE__
422fe8fb19SBen Gras #include <setjmp.h>
432fe8fb19SBen Gras #include <compat/include/setjmp.h>
442fe8fb19SBen Gras 
452fe8fb19SBen Gras void
__longjmp14(jmp_buf env,int val)462fe8fb19SBen Gras __longjmp14(jmp_buf env, int val)
472fe8fb19SBen Gras {
482fe8fb19SBen Gras 	struct sigcontext *sc = (void *)env;
492fe8fb19SBen Gras 	ucontext_t uc;
502fe8fb19SBen Gras 
512fe8fb19SBen Gras 	/* Ensure non-zero SP */
522fe8fb19SBen Gras 	if (sc->sc_sp == 0 || sc->sc_regs[R_ZERO] != 0xacedbade)
532fe8fb19SBen Gras 		goto err;
542fe8fb19SBen Gras 
552fe8fb19SBen Gras 	/* Ensure non-zero return value */
562fe8fb19SBen Gras 	if (val == 0)
572fe8fb19SBen Gras 		val = -1;
582fe8fb19SBen Gras 
592fe8fb19SBen Gras 	/* Set _UC_SIGMASK and _UC_CPU */
602fe8fb19SBen Gras 	uc.uc_flags = _UC_SIGMASK | _UC_CPU;
612fe8fb19SBen Gras 
622fe8fb19SBen Gras 	/* Clear uc_link */
632fe8fb19SBen Gras 	uc.uc_link = 0;
642fe8fb19SBen Gras 
652fe8fb19SBen Gras 	/* Save return value in context */
662fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_V0] = val;
672fe8fb19SBen Gras 
682fe8fb19SBen Gras 	/* Copy saved registers */
692fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_S0] = sc->sc_regs[R_S0];
702fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_S1] = sc->sc_regs[R_S1];
712fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_S2] = sc->sc_regs[R_S2];
722fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_S3] = sc->sc_regs[R_S3];
732fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_S4] = sc->sc_regs[R_S4];
742fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_S5] = sc->sc_regs[R_S5];
752fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_S6] = sc->sc_regs[R_S6];
762fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_RA] = sc->sc_regs[R_RA];
77*84d9c625SLionel Sambuc 	uc.uc_mcontext.__gregs[_REG_GP] = sc->sc_regs[R_GP];
782fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_SP] = sc->sc_sp;
792fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
802fe8fb19SBen Gras 	uc.uc_mcontext.__gregs[_REG_PS] =
812fe8fb19SBen Gras 	   (sc->sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
822fe8fb19SBen Gras 
832fe8fb19SBen Gras 	/* Copy FP state */
842fe8fb19SBen Gras 	if (sc->sc_ownedfp) {
852fe8fb19SBen Gras 		memcpy(&uc.uc_mcontext.__fpregs.__fp_fr,
862fe8fb19SBen Gras 		    &sc->sc_fpregs, 31 * sizeof(unsigned long));
872fe8fb19SBen Gras 		uc.uc_mcontext.__fpregs.__fp_fpcr = sc->sc_fpcr;
882fe8fb19SBen Gras 		/* XXX sc_fp_control */
892fe8fb19SBen Gras 		uc.uc_flags |= _UC_FPU;
902fe8fb19SBen Gras 	}
912fe8fb19SBen Gras 
922fe8fb19SBen Gras 	/* Copy signal mask */
932fe8fb19SBen Gras 	uc.uc_sigmask = sc->sc_mask;
942fe8fb19SBen Gras 
952fe8fb19SBen Gras 	setcontext(&uc);
962fe8fb19SBen Gras  err:
972fe8fb19SBen Gras 	longjmperror();
982fe8fb19SBen Gras 	abort();
992fe8fb19SBen Gras 	/* NOTREACHED */
1002fe8fb19SBen Gras }
101