1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
4cc73b0f0SPeter Grehan * Copyright (c) 2004 Marcel Moolenaar, Peter Grehan
5cc73b0f0SPeter Grehan * All rights reserved.
6cc73b0f0SPeter Grehan *
7cc73b0f0SPeter Grehan * Redistribution and use in source and binary forms, with or without
8cc73b0f0SPeter Grehan * modification, are permitted provided that the following conditions
9cc73b0f0SPeter Grehan * are met:
10cc73b0f0SPeter Grehan *
11cc73b0f0SPeter Grehan * 1. Redistributions of source code must retain the above copyright
12cc73b0f0SPeter Grehan * notice, this list of conditions and the following disclaimer.
13cc73b0f0SPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright
14cc73b0f0SPeter Grehan * notice, this list of conditions and the following disclaimer in the
15cc73b0f0SPeter Grehan * documentation and/or other materials provided with the distribution.
16cc73b0f0SPeter Grehan *
17cc73b0f0SPeter Grehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18cc73b0f0SPeter Grehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19cc73b0f0SPeter Grehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20cc73b0f0SPeter Grehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21cc73b0f0SPeter Grehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22cc73b0f0SPeter Grehan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23cc73b0f0SPeter Grehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24cc73b0f0SPeter Grehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25cc73b0f0SPeter Grehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26cc73b0f0SPeter Grehan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27cc73b0f0SPeter Grehan */
28cc73b0f0SPeter Grehan
29cc73b0f0SPeter Grehan #include <sys/param.h>
30cc73b0f0SPeter Grehan #include <sys/ucontext.h>
31cc73b0f0SPeter Grehan #include <signal.h>
32cc73b0f0SPeter Grehan #include <stdlib.h>
33cc73b0f0SPeter Grehan #include <strings.h>
34cc73b0f0SPeter Grehan
35cc73b0f0SPeter Grehan typedef void (*handler_t)(uint32_t, uint32_t, uint32_t);
36cc73b0f0SPeter Grehan
37cc73b0f0SPeter Grehan /* Prototypes */
38cc73b0f0SPeter Grehan static void ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig,
39cc73b0f0SPeter Grehan uint32_t sig_si, uint32_t sig_uc);
40cc73b0f0SPeter Grehan
41cc73b0f0SPeter Grehan __weak_reference(__signalcontext, signalcontext);
42cc73b0f0SPeter Grehan
43cc73b0f0SPeter Grehan int
__signalcontext(ucontext_t * ucp,int sig,__sighandler_t * func)44cc73b0f0SPeter Grehan __signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
45cc73b0f0SPeter Grehan {
46cc73b0f0SPeter Grehan siginfo_t *sig_si;
47cc73b0f0SPeter Grehan ucontext_t *sig_uc;
48cc73b0f0SPeter Grehan uint32_t sp;
49cc73b0f0SPeter Grehan
50cc73b0f0SPeter Grehan /* Bail out if we don't have a valid ucontext pointer. */
51cc73b0f0SPeter Grehan if (ucp == NULL)
52cc73b0f0SPeter Grehan abort();
53cc73b0f0SPeter Grehan
54cc73b0f0SPeter Grehan /*
55cc73b0f0SPeter Grehan * Build a 16-byte-aligned signal frame
56cc73b0f0SPeter Grehan */
57cc73b0f0SPeter Grehan sp = (ucp->uc_mcontext.mc_gpr[1] - sizeof(ucontext_t)) & ~15UL;
58cc73b0f0SPeter Grehan sig_uc = (ucontext_t *)sp;
59cc73b0f0SPeter Grehan bcopy(ucp, sig_uc, sizeof(*sig_uc));
60cc73b0f0SPeter Grehan sp = (sp - sizeof(siginfo_t)) & ~15UL;
61cc73b0f0SPeter Grehan sig_si = (siginfo_t *)sp;
62cc73b0f0SPeter Grehan bzero(sig_si, sizeof(*sig_si));
63cc73b0f0SPeter Grehan sig_si->si_signo = sig;
64cc73b0f0SPeter Grehan
65cc73b0f0SPeter Grehan /*
66cc73b0f0SPeter Grehan * Subtract 8 bytes from stack to allow for frameptr
67cc73b0f0SPeter Grehan */
68cc73b0f0SPeter Grehan sp -= 2*sizeof(uint32_t);
69cc73b0f0SPeter Grehan sp &= ~15UL;
70cc73b0f0SPeter Grehan
71cc73b0f0SPeter Grehan /*
72cc73b0f0SPeter Grehan * Setup the ucontext of the signal handler.
73cc73b0f0SPeter Grehan */
74cc73b0f0SPeter Grehan bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext));
75cc73b0f0SPeter Grehan ucp->uc_link = sig_uc;
76cc73b0f0SPeter Grehan sigdelset(&ucp->uc_sigmask, sig);
77cc73b0f0SPeter Grehan
78cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_vers = _MC_VERSION;
79cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_len = sizeof(struct __mcontext);
80cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_srr0 = (uint32_t) ctx_wrapper;
81cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_gpr[1] = (uint32_t) sp;
82cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_gpr[3] = (uint32_t) func;
83cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_gpr[4] = (uint32_t) sig;
84cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_gpr[5] = (uint32_t) sig_si;
85cc73b0f0SPeter Grehan ucp->uc_mcontext.mc_gpr[6] = (uint32_t) sig_uc;
86cc73b0f0SPeter Grehan
87cc73b0f0SPeter Grehan return (0);
88cc73b0f0SPeter Grehan }
89cc73b0f0SPeter Grehan
90cc73b0f0SPeter Grehan static void
ctx_wrapper(ucontext_t * ucp,handler_t func,uint32_t sig,uint32_t sig_si,uint32_t sig_uc)91cc73b0f0SPeter Grehan ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig, uint32_t sig_si,
92cc73b0f0SPeter Grehan uint32_t sig_uc)
93cc73b0f0SPeter Grehan {
94cc73b0f0SPeter Grehan
95cc73b0f0SPeter Grehan (*func)(sig, sig_si, sig_uc);
96cc73b0f0SPeter Grehan if (ucp->uc_link == NULL)
97cc73b0f0SPeter Grehan exit(0);
98cc73b0f0SPeter Grehan setcontext((const ucontext_t *)ucp->uc_link);
99cc73b0f0SPeter Grehan /* should never get here */
100cc73b0f0SPeter Grehan abort();
101cc73b0f0SPeter Grehan /* NOTREACHED */
102cc73b0f0SPeter Grehan }
103