xref: /freebsd/lib/libc/amd64/gen/signalcontext.c (revision 148a8da8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Marcel Moolenaar
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/ucontext.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 
38 typedef void (*handler_t)(uint64_t, uint64_t, uint64_t);
39 
40 /* Prototypes */
41 static void sigctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args);
42 
43 __weak_reference(__signalcontext, signalcontext);
44 
45 int
46 __signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
47 {
48 	uint64_t *args;
49 	siginfo_t *sig_si;
50 	ucontext_t *sig_uc;
51 	uint64_t sp;
52 
53 	/* Bail out if we don't have a valid ucontext pointer. */
54 	if (ucp == NULL)
55 		abort();
56 
57 	/*
58 	 * Build a signal frame and copy the arguments of signal handler
59 	 * 'func' onto the stack and do the funky stack alignment.
60 	 * This means that we need an 8-byte-odd alignment since the ABI expects
61 	 * the return address to be pushed, thus breaking the 16 byte alignment.
62 	 */
63 	sp = (ucp->uc_mcontext.mc_rsp - 128 - sizeof(ucontext_t)) & ~15UL;
64 	sig_uc = (ucontext_t *)sp;
65 	bcopy(ucp, sig_uc, sizeof(*sig_uc));
66 	sp = (sp - sizeof(siginfo_t)) & ~15UL;
67 	sig_si = (siginfo_t *)sp;
68 	bzero(sig_si, sizeof(*sig_si));
69 	sig_si->si_signo = sig;
70 	sp -= 3 * sizeof(uint64_t);
71 	args = (uint64_t *)sp;
72 	args[0] = sig;
73 	args[1] = (intptr_t)sig_si;
74 	args[2] = (intptr_t)sig_uc;
75 	sp -= 16;
76 
77 	/*
78 	 * Setup the ucontext of the signal handler.
79 	 */
80 	bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext));
81 	ucp->uc_mcontext.mc_fpformat = _MC_FPFMT_NODEV;
82 	ucp->uc_mcontext.mc_ownedfp = _MC_FPOWNED_NONE;
83 	ucp->uc_link = sig_uc;
84 	sigdelset(&ucp->uc_sigmask, sig);
85 
86 	ucp->uc_mcontext.mc_len = sizeof(mcontext_t);
87 	ucp->uc_mcontext.mc_rdi = (register_t)ucp;
88 	ucp->uc_mcontext.mc_rsi = (register_t)func;
89 	ucp->uc_mcontext.mc_rdx = (register_t)args;
90 	ucp->uc_mcontext.mc_rbp = (register_t)sp;
91 	ucp->uc_mcontext.mc_rbx = (register_t)sp;
92 	ucp->uc_mcontext.mc_rsp = (register_t)sp;
93 	ucp->uc_mcontext.mc_rip = (register_t)sigctx_wrapper;
94 	return (0);
95 }
96 
97 static void
98 sigctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args)
99 {
100 
101 	(*func)(args[0], args[1], args[2]);
102 	if (ucp->uc_link == NULL)
103 		exit(0);
104 	setcontext((const ucontext_t *)ucp->uc_link);
105 	/* should never get here */
106 	abort();
107 	/* NOTREACHED */
108 }
109