2 * Copyright (c) 2004 Marcel Moolenaar, Peter Grehan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ucontext.h>
36 typedef void (*handler_t)(uint32_t, uint32_t, uint32_t);
39 static void ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig,
40 uint32_t sig_si, uint32_t sig_uc);
42 __weak_reference(__signalcontext, signalcontext);
45 __signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
51 /* Bail out if we don't have a valid ucontext pointer. */
56 * Build a 16-byte-aligned signal frame
58 sp = (ucp->uc_mcontext.mc_gpr[1] - sizeof(ucontext_t)) & ~15UL;
59 sig_uc = (ucontext_t *)sp;
60 bcopy(ucp, sig_uc, sizeof(*sig_uc));
61 sp = (sp - sizeof(siginfo_t)) & ~15UL;
62 sig_si = (siginfo_t *)sp;
63 bzero(sig_si, sizeof(*sig_si));
64 sig_si->si_signo = sig;
67 * Subtract 8 bytes from stack to allow for frameptr
69 sp -= 2*sizeof(uint32_t);
73 * Setup the ucontext of the signal handler.
75 bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext));
76 ucp->uc_link = sig_uc;
77 sigdelset(&ucp->uc_sigmask, sig);
79 ucp->uc_mcontext.mc_vers = _MC_VERSION;
80 ucp->uc_mcontext.mc_len = sizeof(struct __mcontext);
81 ucp->uc_mcontext.mc_srr0 = (uint32_t) ctx_wrapper;
82 ucp->uc_mcontext.mc_gpr[1] = (uint32_t) sp;
83 ucp->uc_mcontext.mc_gpr[3] = (uint32_t) func;
84 ucp->uc_mcontext.mc_gpr[4] = (uint32_t) sig;
85 ucp->uc_mcontext.mc_gpr[5] = (uint32_t) sig_si;
86 ucp->uc_mcontext.mc_gpr[6] = (uint32_t) sig_uc;
92 ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig, uint32_t sig_si,
96 (*func)(sig, sig_si, sig_uc);
97 if (ucp->uc_link == NULL)
99 setcontext((const ucontext_t *)ucp->uc_link);
100 /* should never get here */