1*bcbf0090Suwe /* $NetBSD: longjmp.c,v 1.5 2024/02/20 00:09:31 uwe Exp $ */
2eb9395f8Suwe
3eb9395f8Suwe /*-
4eb9395f8Suwe * Copyright (c) 2003 The NetBSD Foundation, Inc.
5eb9395f8Suwe * All rights reserved.
6eb9395f8Suwe *
7eb9395f8Suwe * This code is derived from software contributed to The NetBSD Foundation
8eb9395f8Suwe * by Christian Limpach.
9eb9395f8Suwe *
10eb9395f8Suwe * Redistribution and use in source and binary forms, with or without
11eb9395f8Suwe * modification, are permitted provided that the following conditions
12eb9395f8Suwe * are met:
13eb9395f8Suwe * 1. Redistributions of source code must retain the above copyright
14eb9395f8Suwe * notice, this list of conditions and the following disclaimer.
15eb9395f8Suwe * 2. Redistributions in binary form must reproduce the above copyright
16eb9395f8Suwe * notice, this list of conditions and the following disclaimer in the
17eb9395f8Suwe * documentation and/or other materials provided with the distribution.
18eb9395f8Suwe *
19eb9395f8Suwe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20eb9395f8Suwe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21eb9395f8Suwe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22eb9395f8Suwe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23eb9395f8Suwe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24eb9395f8Suwe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25eb9395f8Suwe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26eb9395f8Suwe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27eb9395f8Suwe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28eb9395f8Suwe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29eb9395f8Suwe * POSSIBILITY OF SUCH DAMAGE.
30eb9395f8Suwe */
31eb9395f8Suwe
32eb9395f8Suwe #include "namespace.h"
33eb9395f8Suwe #include <sys/types.h>
34eb9395f8Suwe #include <ucontext.h>
35eb9395f8Suwe #include <signal.h>
36eb9395f8Suwe #include <stdlib.h>
37eb9395f8Suwe #include <string.h>
38251de2deSmartin #include <stddef.h>
39eb9395f8Suwe
40eb9395f8Suwe #define __LIBC12_SOURCE__
41eb9395f8Suwe #include <setjmp.h>
42eb9395f8Suwe #include <compat/include/setjmp.h>
43eb9395f8Suwe
44377714d8Smrg #include "assym.h"
45377714d8Smrg #include "sparc_longjmp.h"
46251de2deSmartin
47251de2deSmartin /*
48377714d8Smrg * check that offsets in the above structures match their usage in the
49377714d8Smrg * setjmp() side of this setup. a jmp_buf is the 12-word contents of
50*bcbf0090Suwe * the sigcontext structure, plus 2 more words for g4 and g7.
51251de2deSmartin */
52377714d8Smrg __CTASSERT(_SIZEOF_SC + _JB_G4 == offsetof(struct __jmp_buf,regs.g4));
53377714d8Smrg __CTASSERT(_SIZEOF_SC + _JB_G7 == offsetof(struct __jmp_buf,regs.g7));
54377714d8Smrg __CTASSERT(sizeof(jmp_buf) >= sizeof(struct __jmp_buf));
55251de2deSmartin
56eb9395f8Suwe /*
57eb9395f8Suwe * Use setcontext to reload the stack pointer, program counter <pc,npc>, and
58eb9395f8Suwe * set the return value in %o0. The %i and %l registers will be reloaded
59eb9395f8Suwe * from the place to which %sp points.
60eb9395f8Suwe */
61eb9395f8Suwe void
__longjmp14(jmp_buf env,int val)62eb9395f8Suwe __longjmp14(jmp_buf env, int val)
63eb9395f8Suwe {
64377714d8Smrg struct __jmp_buf *context = (void *)env;
65377714d8Smrg struct sigcontext *sc = &context->sc;
66377714d8Smrg struct __jmp_buf_regs_t *r = &context->regs;
67eb9395f8Suwe ucontext_t uc;
68eb9395f8Suwe
69eb9395f8Suwe /* Ensure non-zero SP */
70eb9395f8Suwe if (sc->sc_sp == 0)
71eb9395f8Suwe goto err;
72eb9395f8Suwe
73251de2deSmartin /* Initialise the context */
74251de2deSmartin memset(&uc, 0, sizeof(uc));
75eb9395f8Suwe
76eb9395f8Suwe /*
77eb9395f8Suwe * Set _UC_{SET,CLR}STACK according to SS_ONSTACK.
78eb9395f8Suwe *
79eb9395f8Suwe * Restore the signal mask with sigprocmask() instead of _UC_SIGMASK,
80eb9395f8Suwe * since libpthread may want to interpose on signal handling.
81eb9395f8Suwe */
82eb9395f8Suwe uc.uc_flags = _UC_CPU | (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
83eb9395f8Suwe
84eb9395f8Suwe sigprocmask(SIG_SETMASK, &sc->sc_mask, NULL);
85eb9395f8Suwe
86eb9395f8Suwe /* Extract PSR, PC, NPC and SP from jmp_buf */
87eb9395f8Suwe uc.uc_mcontext.__gregs[_REG_PSR] = sc->sc_psr;
88eb9395f8Suwe uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
89251de2deSmartin uc.uc_mcontext.__gregs[_REG_nPC] = sc->sc_pc+4;
90eb9395f8Suwe uc.uc_mcontext.__gregs[_REG_O6] = sc->sc_sp;
91251de2deSmartin uc.uc_mcontext.__gregs[_REG_G2] = sc->sc_g1;
92251de2deSmartin uc.uc_mcontext.__gregs[_REG_G3] = sc->sc_npc;
93251de2deSmartin uc.uc_mcontext.__gregs[_REG_G4] = r->g4;
94251de2deSmartin uc.uc_mcontext.__gregs[_REG_G7] = r->g7;
95eb9395f8Suwe
96eb9395f8Suwe /* Set the return value; make sure it's non-zero */
97eb9395f8Suwe uc.uc_mcontext.__gregs[_REG_O0] = (val != 0 ? val : 1);
98eb9395f8Suwe
99eb9395f8Suwe setcontext(&uc);
100eb9395f8Suwe
101eb9395f8Suwe err:
102eb9395f8Suwe longjmperror();
103eb9395f8Suwe abort();
104eb9395f8Suwe /* NOTREACHED */
105eb9395f8Suwe }
106