Path: blob/main/lib/libc/powerpc64/gen/signalcontext.c
39491 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2004 Marcel Moolenaar, Peter Grehan4* 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/param.h>29#include <sys/ucontext.h>30#include <signal.h>31#include <stdlib.h>32#include <strings.h>3334typedef void (*handler_t)(uint32_t, uint32_t, uint32_t);3536/* Prototypes */37static void ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig,38uint32_t sig_si, uint32_t sig_uc);3940__weak_reference(__signalcontext, signalcontext);4142int43__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)44{45siginfo_t *sig_si;46ucontext_t *sig_uc;47uintptr_t sp;4849/* Bail out if we don't have a valid ucontext pointer. */50if (ucp == NULL)51abort();5253/*54* Build a 16-byte-aligned signal frame55*/56sp = (ucp->uc_mcontext.mc_gpr[1] - sizeof(ucontext_t)) & ~15UL;57sig_uc = (ucontext_t *)sp;58bcopy(ucp, sig_uc, sizeof(*sig_uc));59sp = (sp - sizeof(siginfo_t)) & ~15UL;60sig_si = (siginfo_t *)sp;61bzero(sig_si, sizeof(*sig_si));62sig_si->si_signo = sig;6364/*65* Subtract 48 bytes from stack to allow for frameptr66*/67sp -= 6*sizeof(uint64_t);68sp &= ~15UL;6970/*71* Setup the ucontext of the signal handler.72*/73bzero(&ucp->uc_mcontext, sizeof(ucp->uc_mcontext));74ucp->uc_link = sig_uc;75sigdelset(&ucp->uc_sigmask, sig);7677ucp->uc_mcontext.mc_vers = _MC_VERSION;78ucp->uc_mcontext.mc_len = sizeof(struct __mcontext);79ucp->uc_mcontext.mc_srr0 = (uint64_t) ctx_wrapper;80ucp->uc_mcontext.mc_gpr[1] = (uint64_t) sp;81ucp->uc_mcontext.mc_gpr[3] = (uint64_t) func;82ucp->uc_mcontext.mc_gpr[4] = (uint64_t) sig;83ucp->uc_mcontext.mc_gpr[5] = (uint64_t) sig_si;84ucp->uc_mcontext.mc_gpr[6] = (uint64_t) sig_uc;8586return (0);87}8889static void90ctx_wrapper(ucontext_t *ucp, handler_t func, uint32_t sig, uint32_t sig_si,91uint32_t sig_uc)92{9394(*func)(sig, sig_si, sig_uc);95if (ucp->uc_link == NULL)96exit(0);97setcontext((const ucontext_t *)ucp->uc_link);98/* should never get here */99abort();100/* NOTREACHED */101}102103104