1*4c48b124Srin /* $NetBSD: __longjmp14.c,v 1.6 2024/06/29 06:52:34 rin Exp $ */
24b376dedSmatt
34b376dedSmatt /*-
44b376dedSmatt * Copyright (c) 2003 The NetBSD Foundation, Inc.
54b376dedSmatt * All rights reserved.
64b376dedSmatt *
74b376dedSmatt * This code is derived from software contributed to The NetBSD Foundation
84b376dedSmatt * by Christian Limpach and Matt Thomas.
94b376dedSmatt *
104b376dedSmatt * Redistribution and use in source and binary forms, with or without
114b376dedSmatt * modification, are permitted provided that the following conditions
124b376dedSmatt * are met:
134b376dedSmatt * 1. Redistributions of source code must retain the above copyright
144b376dedSmatt * notice, this list of conditions and the following disclaimer.
154b376dedSmatt * 2. Redistributions in binary form must reproduce the above copyright
164b376dedSmatt * notice, this list of conditions and the following disclaimer in the
174b376dedSmatt * documentation and/or other materials provided with the distribution.
184b376dedSmatt *
194b376dedSmatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204b376dedSmatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214b376dedSmatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224b376dedSmatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234b376dedSmatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244b376dedSmatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254b376dedSmatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264b376dedSmatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274b376dedSmatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284b376dedSmatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294b376dedSmatt * POSSIBILITY OF SUCH DAMAGE.
304b376dedSmatt */
314b376dedSmatt
324b376dedSmatt #include "namespace.h"
334b376dedSmatt #include <sys/types.h>
344b376dedSmatt #include <ucontext.h>
354b376dedSmatt #include <signal.h>
364b376dedSmatt #include <stdlib.h>
374b376dedSmatt #include <string.h>
384b376dedSmatt #include <machine/reg.h>
394b376dedSmatt
404b376dedSmatt #define __LIBC12_SOURCE__
414b376dedSmatt #include <setjmp.h>
42f9efc4abShe #include <compat/include/setjmp.h>
434b376dedSmatt
444b376dedSmatt struct _jmp_buf {
454b376dedSmatt struct sigcontext jb_sc;
46013af8a9Schristos __register_t jb_regs[6];
474b376dedSmatt };
484b376dedSmatt
494b376dedSmatt void
__longjmp14(jmp_buf env,int val)504b376dedSmatt __longjmp14(jmp_buf env, int val)
514b376dedSmatt {
524b376dedSmatt struct _jmp_buf *jb = (void *)env;
534b376dedSmatt ucontext_t uc;
544b376dedSmatt
554b376dedSmatt /* Ensure non-zero SP */
564b376dedSmatt if (jb->jb_sc.sc_sp == 0)
574b376dedSmatt goto err;
584b376dedSmatt
59*4c48b124Srin /* Return 1 if val is zero */
604b376dedSmatt if (val == 0)
61*4c48b124Srin val = 1;
624b376dedSmatt
634b376dedSmatt /* Set _UC_SIGMASK and _UC_CPU */
644b376dedSmatt uc.uc_flags = _UC_SIGMASK | _UC_CPU;
654b376dedSmatt
664b376dedSmatt /* Clear uc_link */
674b376dedSmatt uc.uc_link = 0;
684b376dedSmatt
694b376dedSmatt /* Save return value in context */
704b376dedSmatt uc.uc_mcontext.__gregs[_REG_R0] = val;
714b376dedSmatt
724b376dedSmatt /* Copy saved registers */
734b376dedSmatt uc.uc_mcontext.__gregs[_REG_AP] = jb->jb_sc.sc_ap;
744b376dedSmatt uc.uc_mcontext.__gregs[_REG_SP] = jb->jb_sc.sc_sp;
754b376dedSmatt uc.uc_mcontext.__gregs[_REG_FP] = jb->jb_sc.sc_fp;
764b376dedSmatt uc.uc_mcontext.__gregs[_REG_PC] = jb->jb_sc.sc_pc;
774b376dedSmatt uc.uc_mcontext.__gregs[_REG_PSL] = jb->jb_sc.sc_ps;
784b376dedSmatt
794b376dedSmatt uc.uc_mcontext.__gregs[_REG_R6] = jb->jb_regs[0];
804b376dedSmatt uc.uc_mcontext.__gregs[_REG_R7] = jb->jb_regs[1];
814b376dedSmatt uc.uc_mcontext.__gregs[_REG_R8] = jb->jb_regs[2];
824b376dedSmatt uc.uc_mcontext.__gregs[_REG_R9] = jb->jb_regs[3];
834b376dedSmatt uc.uc_mcontext.__gregs[_REG_R10] = jb->jb_regs[4];
844b376dedSmatt uc.uc_mcontext.__gregs[_REG_R11] = jb->jb_regs[5];
854b376dedSmatt
864b376dedSmatt /* Copy signal mask */
874b376dedSmatt uc.uc_sigmask = jb->jb_sc.sc_mask;
884b376dedSmatt
894b376dedSmatt setcontext(&uc);
904b376dedSmatt err:
914b376dedSmatt longjmperror();
924b376dedSmatt abort();
934b376dedSmatt /* NOTREACHED */
944b376dedSmatt }
95