/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2004 Olivier Houchard4* 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* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/signal.h>30#include <sys/ucontext.h>3132#include <machine/frame.h>33#include <machine/sigframe.h>3435#include <errno.h>36#include <stdarg.h>37#include <stdlib.h>38#include <unistd.h>39#include <strings.h>40#include <signal.h>4142__weak_reference(__signalcontext, signalcontext);4344extern void _ctx_start(void);4546int47__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)48{49struct sigframe *sfp;50__greg_t *gr = ucp->uc_mcontext.__gregs;51unsigned int *sp;5253sp = (unsigned int *)gr[_REG_SP];5455sfp = (struct sigframe *)sp - 1;5657bzero(sfp, sizeof(*sfp));58bcopy(ucp, &sfp->sf_uc, sizeof(*ucp));59sfp->sf_si.si_signo = sig;6061gr[_REG_SP] = (__greg_t)sfp;62/* Wipe out frame pointer. */63gr[_REG_FP] = 0;64/* Arrange for return via the trampoline code. */65gr[_REG_PC] = (__greg_t)_ctx_start;66gr[_REG_R4] = (__greg_t)func;67gr[_REG_R5] = (__greg_t)ucp;68gr[_REG_R0] = (__greg_t)sig;69gr[_REG_R1] = (__greg_t)&sfp->sf_si;70gr[_REG_R2] = (__greg_t)&sfp->sf_uc;7172ucp->uc_link = &sfp->sf_uc;73sigdelset(&ucp->uc_sigmask, sig);7475return (0);76}777879