Path: blob/main/lib/libc/amd64/gen/signalcontext.c
39500 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2003 Marcel Moolenaar4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/types.h>29#include <sys/ucontext.h>30#include <signal.h>31#include <stdlib.h>32#include <strings.h>3334typedef void (*handler_t)(uint64_t, uint64_t, uint64_t);3536/* Prototypes */37static void sigctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args);3839__weak_reference(__signalcontext, signalcontext);4041int42__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)43{44uint64_t *args;45siginfo_t *sig_si;46ucontext_t *sig_uc;47uint64_t sp;4849/* Bail out if we don't have a valid ucontext pointer. */50if (ucp == NULL)51abort();5253/*54* Build a signal frame and copy the arguments of signal handler55* 'func' onto the stack and do the funky stack alignment.56* This means that we need an 8-byte-odd alignment since the ABI expects57* the return address to be pushed, thus breaking the 16 byte alignment.58*/59sp = (ucp->uc_mcontext.mc_rsp - 128 - sizeof(ucontext_t)) & ~15UL;60sig_uc = (ucontext_t *)sp;61bcopy(ucp, sig_uc, sizeof(*sig_uc));62sp = (sp - sizeof(siginfo_t)) & ~15UL;63sig_si = (siginfo_t *)sp;64bzero(sig_si, sizeof(*sig_si));65sig_si->si_signo = sig;66sp -= 3 * sizeof(uint64_t);67args = (uint64_t *)sp;68args[0] = sig;69args[1] = (intptr_t)sig_si;70args[2] = (intptr_t)sig_uc;71sp -= 16;7273/*74* Setup the ucontext of the signal handler.75*/76bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext));77ucp->uc_mcontext.mc_fpformat = _MC_FPFMT_NODEV;78ucp->uc_mcontext.mc_ownedfp = _MC_FPOWNED_NONE;79ucp->uc_link = sig_uc;80sigdelset(&ucp->uc_sigmask, sig);8182ucp->uc_mcontext.mc_len = sizeof(mcontext_t);83ucp->uc_mcontext.mc_rdi = (register_t)ucp;84ucp->uc_mcontext.mc_rsi = (register_t)func;85ucp->uc_mcontext.mc_rdx = (register_t)args;86ucp->uc_mcontext.mc_rbp = (register_t)sp;87ucp->uc_mcontext.mc_rbx = (register_t)sp;88ucp->uc_mcontext.mc_rsp = (register_t)sp;89ucp->uc_mcontext.mc_rip = (register_t)sigctx_wrapper;90return (0);91}9293static void94sigctx_wrapper(ucontext_t *ucp, handler_t func, uint64_t *args)95{9697(*func)(args[0], args[1], args[2]);98if (ucp->uc_link == NULL)99exit(0);100setcontext((const ucontext_t *)ucp->uc_link);101/* should never get here */102abort();103/* NOTREACHED */104}105106107