/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2002 Jonathan Mini <[email protected]>4* 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/ucontext.h>30#include <machine/psl.h>31#include <machine/sigframe.h>32#include <signal.h>33#include <strings.h>3435__weak_reference(__signalcontext, signalcontext);3637extern void _ctx_start(ucontext_t *, int argc, ...);3839int40__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)41{42register_t *p;43struct sigframe *sfp;4445/*-46* Set up stack.47* (n = sizeof(int))48* 2n+sizeof(struct sigframe) ucp49* 2n struct sigframe50* 1n &func51* 0n &_ctx_start52*/53p = (register_t *)(void *)(intptr_t)ucp->uc_mcontext.mc_esp;54*--p = (register_t)(intptr_t)ucp;55p = (register_t *)((u_register_t)p & ~0xF); /* Align to 16 bytes. */56p = (register_t *)((u_register_t)p - sizeof(struct sigframe));57sfp = (struct sigframe *)p;58bzero(sfp, sizeof(struct sigframe));59sfp->sf_signum = sig;60sfp->sf_siginfo = (register_t)(intptr_t)&sfp->sf_si;61sfp->sf_ucontext = (register_t)(intptr_t)&sfp->sf_uc;62sfp->sf_ahu.sf_action = (__siginfohandler_t *)func;63bcopy(ucp, &sfp->sf_uc, sizeof(ucontext_t));64sfp->sf_si.si_signo = sig;65*--p = (register_t)(intptr_t)func;6667/*68* Set up ucontext_t.69*/70ucp->uc_mcontext.mc_esi = ucp->uc_mcontext.mc_esp - sizeof(int);71ucp->uc_mcontext.mc_esp = (register_t)(intptr_t)p;72ucp->uc_mcontext.mc_eip = (register_t)(intptr_t)_ctx_start;73ucp->uc_mcontext.mc_eflags &= ~PSL_T;74ucp->uc_link = &sfp->sf_uc;75sigdelset(&ucp->uc_sigmask, sig);76return (0);77}787980