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