1117755Sdeischen/* 2117755Sdeischen * Copyright (c) 2003 Marcel Moolenaar 3117755Sdeischen * All rights reserved. 4117755Sdeischen * 5117755Sdeischen * Redistribution and use in source and binary forms, with or without 6117755Sdeischen * modification, are permitted provided that the following conditions 7117755Sdeischen * are met: 8117755Sdeischen * 9117755Sdeischen * 1. Redistributions of source code must retain the above copyright 10117755Sdeischen * notice, this list of conditions and the following disclaimer. 11117755Sdeischen * 2. Redistributions in binary form must reproduce the above copyright 12117755Sdeischen * notice, this list of conditions and the following disclaimer in the 13117755Sdeischen * documentation and/or other materials provided with the distribution. 14117755Sdeischen * 15117755Sdeischen * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16117755Sdeischen * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17117755Sdeischen * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18117755Sdeischen * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19117755Sdeischen * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20117755Sdeischen * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21117755Sdeischen * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22117755Sdeischen * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23117755Sdeischen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24117755Sdeischen * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25117755Sdeischen */ 26117755Sdeischen 27117755Sdeischen#include <sys/cdefs.h> 28117755Sdeischen__FBSDID("$FreeBSD: releng/10.2/lib/libc/amd64/gen/signalcontext.c 132795 2004-07-28 13:08:24Z davidxu $"); 29117755Sdeischen 30117755Sdeischen#include <sys/types.h> 31117755Sdeischen#include <sys/ucontext.h> 32117755Sdeischen#include <signal.h> 33117755Sdeischen#include <stdlib.h> 34117755Sdeischen#include <strings.h> 35117755Sdeischen 36117755Sdeischentypedef void (*handler_t)(uint64_t, uint64_t, uint64_t); 37117755Sdeischen 38117755Sdeischen/* Prototypes */ 39127658Speterstatic void sigctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args); 40117755Sdeischen 41117755Sdeischen__weak_reference(__signalcontext, signalcontext); 42117755Sdeischen 43117755Sdeischenint 44117755Sdeischen__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func) 45117755Sdeischen{ 46117755Sdeischen uint64_t *args; 47117755Sdeischen siginfo_t *sig_si; 48117755Sdeischen ucontext_t *sig_uc; 49117755Sdeischen uint64_t sp; 50117755Sdeischen 51117755Sdeischen /* Bail out if we don't have a valid ucontext pointer. */ 52117755Sdeischen if (ucp == NULL) 53117755Sdeischen abort(); 54117755Sdeischen 55117755Sdeischen /* 56117755Sdeischen * Build a signal frame and copy the arguments of signal handler 57127658Speter * 'func' onto the stack and do the funky stack alignment. 58127658Speter * This means that we need an 8-byte-odd alignment since the ABI expects 59127658Speter * the return address to be pushed, thus breaking the 16 byte alignment. 60117755Sdeischen */ 61130546Sdavidxu sp = (ucp->uc_mcontext.mc_rsp - 128 - sizeof(ucontext_t)) & ~15UL; 62117755Sdeischen sig_uc = (ucontext_t *)sp; 63117755Sdeischen bcopy(ucp, sig_uc, sizeof(*sig_uc)); 64117755Sdeischen sp = (sp - sizeof(siginfo_t)) & ~15UL; 65117755Sdeischen sig_si = (siginfo_t *)sp; 66117755Sdeischen bzero(sig_si, sizeof(*sig_si)); 67117755Sdeischen sig_si->si_signo = sig; 68127658Speter sp -= 3 * sizeof(uint64_t); 69117755Sdeischen args = (uint64_t *)sp; 70117755Sdeischen args[0] = sig; 71117755Sdeischen args[1] = (intptr_t)sig_si; 72117755Sdeischen args[2] = (intptr_t)sig_uc; 73117755Sdeischen sp -= 16; 74117755Sdeischen 75117755Sdeischen /* 76117755Sdeischen * Setup the ucontext of the signal handler. 77117755Sdeischen */ 78117755Sdeischen bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext)); 79132795Sdavidxu ucp->uc_mcontext.mc_fpformat = _MC_FPFMT_NODEV; 80132795Sdavidxu ucp->uc_mcontext.mc_ownedfp = _MC_FPOWNED_NONE; 81117755Sdeischen ucp->uc_link = sig_uc; 82117755Sdeischen sigdelset(&ucp->uc_sigmask, sig); 83117755Sdeischen 84118058Sdavidxu ucp->uc_mcontext.mc_len = sizeof(mcontext_t); 85117755Sdeischen ucp->uc_mcontext.mc_rdi = (register_t)ucp; 86117755Sdeischen ucp->uc_mcontext.mc_rsi = (register_t)func; 87117755Sdeischen ucp->uc_mcontext.mc_rdx = (register_t)args; 88117755Sdeischen ucp->uc_mcontext.mc_rbp = (register_t)sp; 89117755Sdeischen ucp->uc_mcontext.mc_rbx = (register_t)sp; 90117755Sdeischen ucp->uc_mcontext.mc_rsp = (register_t)sp; 91127658Speter ucp->uc_mcontext.mc_rip = (register_t)sigctx_wrapper; 92117755Sdeischen return (0); 93117755Sdeischen} 94117755Sdeischen 95117755Sdeischenstatic void 96127658Spetersigctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args) 97117755Sdeischen{ 98117755Sdeischen 99117755Sdeischen (*func)(args[0], args[1], args[2]); 100117755Sdeischen if (ucp->uc_link == NULL) 101117755Sdeischen exit(0); 102117755Sdeischen setcontext((const ucontext_t *)ucp->uc_link); 103117755Sdeischen /* should never get here */ 104117755Sdeischen abort(); 105117755Sdeischen /* NOTREACHED */ 106117755Sdeischen} 107