1118655Sjake/*-
2118655Sjake * Copyright (c) 2003 Jake Burkholder.
3118655Sjake * All rights reserved.
4118655Sjake *
5118655Sjake * Redistribution and use in source and binary forms, with or without
6118655Sjake * modification, are permitted provided that the following conditions
7118655Sjake * are met:
8118655Sjake * 1. Redistributions of source code must retain the above copyright
9118655Sjake *    notice, this list of conditions and the following disclaimer.
10118655Sjake * 2. Redistributions in binary form must reproduce the above copyright
11118655Sjake *    notice, this list of conditions and the following disclaimer in the
12118655Sjake *    documentation and/or other materials provided with the distribution.
13118655Sjake *
14118655Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15118655Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16118655Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17118655Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18118655Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19118655Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20118655Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21118655Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22118655Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23118655Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24118655Sjake * SUCH DAMAGE.
25118655Sjake */
26118655Sjake
27118655Sjake#include <sys/cdefs.h>
28118655Sjake__FBSDID("$FreeBSD$");
29118655Sjake
30118655Sjake#include <sys/param.h>
31118655Sjake#include <sys/ucontext.h>
32118655Sjake
33118655Sjake#include <machine/frame.h>
34118655Sjake#include <machine/sigframe.h>
35118655Sjake
36118655Sjake#include <errno.h>
37124182Snectar#include <signal.h>
38118655Sjake#include <stdarg.h>
39118655Sjake#include <stdlib.h>
40124182Snectar#include <strings.h>
41118655Sjake#include <unistd.h>
42118655Sjake
43118655Sjake__weak_reference(__signalcontext, signalcontext);
44118655Sjake
45118655Sjakeextern void _ctx_start(void);
46118655Sjake
47118655Sjakeint
48118655Sjake__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
49118655Sjake{
50118655Sjake	struct sigframe *sfp;
51118655Sjake	struct frame *fp;
52118655Sjake	mcontext_t *mc;
53118655Sjake
54118655Sjake	mc = &ucp->uc_mcontext;
55253266Smarius	sfp = (struct sigframe *)(mc->_mc_sp + SPOFF) - 1;
56118655Sjake	fp = (struct frame *)sfp - 1;
57118655Sjake
58118655Sjake	bzero(fp, sizeof(*fp));
59118655Sjake
60118655Sjake	bzero(sfp, sizeof(*sfp));
61118655Sjake	bcopy(ucp, &sfp->sf_uc, sizeof(*ucp));
62118655Sjake	sfp->sf_si.si_signo = sig;
63118655Sjake
64118655Sjake	mc->mc_global[1] = (uint64_t)func;
65118655Sjake	mc->mc_global[2] = (uint64_t)ucp;
66118655Sjake	mc->mc_out[0] = sig;
67118655Sjake	mc->mc_out[1] = (uint64_t)&sfp->sf_si;
68118655Sjake	mc->mc_out[2] = (uint64_t)&sfp->sf_uc;
69118655Sjake	mc->mc_out[6] = (uint64_t)fp - SPOFF;
70253266Smarius	mc->_mc_tnpc = (uint64_t)_ctx_start + 4;
71253266Smarius	mc->_mc_tpc = (uint64_t)_ctx_start;
72118655Sjake
73118655Sjake	ucp->uc_link = &sfp->sf_uc;
74118655Sjake	sigdelset(&ucp->uc_sigmask, sig);
75118655Sjake
76118655Sjake	return (0);
77118655Sjake}
78