signalcontext.c revision 118058
11541Srgrimes/*
21541Srgrimes * Copyright (c) 2003 Marcel Moolenaar
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes *
91541Srgrimes * 1. Redistributions of source code must retain the above copyright
101541Srgrimes *    notice, this list of conditions and the following disclaimer.
111541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer in the
131541Srgrimes *    documentation and/or other materials provided with the distribution.
141541Srgrimes *
151541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161541Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171541Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181541Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191541Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
201541Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211541Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
221541Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
231541Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241541Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251541Srgrimes */
261541Srgrimes
271541Srgrimes#include <sys/cdefs.h>
281541Srgrimes__FBSDID("$FreeBSD: head/lib/libc/amd64/gen/signalcontext.c 118058 2003-07-26 12:58:28Z davidxu $");
291541Srgrimes
301541Srgrimes#include <sys/types.h>
311541Srgrimes#include <sys/ucontext.h>
321541Srgrimes#include <signal.h>
331541Srgrimes#include <stdlib.h>
3450477Speter#include <strings.h>
351541Srgrimes
361541Srgrimestypedef void (*handler_t)(uint64_t, uint64_t, uint64_t);
372169Spaul
382169Spaul/* Prototypes */
392169Spaulstatic void ctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args);
401541Srgrimes
411541Srgrimes__weak_reference(__signalcontext, signalcontext);
421541Srgrimes
431541Srgrimesint
441541Srgrimes__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
451541Srgrimes{
461541Srgrimes	uint64_t *args;
471541Srgrimes	siginfo_t *sig_si;
481541Srgrimes	ucontext_t *sig_uc;
491541Srgrimes	uint64_t sp;
501541Srgrimes
511541Srgrimes	/* Bail out if we don't have a valid ucontext pointer. */
521541Srgrimes	if (ucp == NULL)
5337620Sbde		abort();
5437620Sbde
5536735Sdfr	/*
5637620Sbde	 * Build a signal frame and copy the arguments of signal handler
5736735Sdfr	 * 'func' onto the stack. We only need 3 arguments, but we
581541Srgrimes	 * create room for 4 so that we are 16-byte aligned.
591541Srgrimes	 */
601541Srgrimes	sp = (ucp->uc_mcontext.mc_rsp - sizeof(ucontext_t)) & ~15UL;
612169Spaul	sig_uc = (ucontext_t *)sp;
622169Spaul	bcopy(ucp, sig_uc, sizeof(*sig_uc));
63	sp = (sp - sizeof(siginfo_t)) & ~15UL;
64	sig_si = (siginfo_t *)sp;
65	bzero(sig_si, sizeof(*sig_si));
66	sig_si->si_signo = sig;
67	sp -= 4 * sizeof(uint64_t);
68	args = (uint64_t *)sp;
69	args[0] = sig;
70	args[1] = (intptr_t)sig_si;
71	args[2] = (intptr_t)sig_uc;
72	args[3] = 0;
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_link = sig_uc;
80	sigdelset(&ucp->uc_sigmask, sig);
81
82	ucp->uc_mcontext.mc_len = sizeof(mcontext_t);
83	ucp->uc_mcontext.mc_rdi = (register_t)ucp;
84	ucp->uc_mcontext.mc_rsi = (register_t)func;
85	ucp->uc_mcontext.mc_rdx = (register_t)args;
86	ucp->uc_mcontext.mc_rbp = (register_t)sp;
87	ucp->uc_mcontext.mc_rbx = (register_t)sp;
88	ucp->uc_mcontext.mc_rsp = (register_t)sp;
89	ucp->uc_mcontext.mc_rip = (register_t)ctx_wrapper;
90	return (0);
91}
92
93static void
94ctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args)
95{
96
97	(*func)(args[0], args[1], args[2]);
98	if (ucp->uc_link == NULL)
99		exit(0);
100	setcontext((const ucontext_t *)ucp->uc_link);
101	/* should never get here */
102	abort();
103	/* NOTREACHED */
104}
105