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