1/*-
2 * Copyright (c) 2003 Jake Burkholder.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/ucontext.h>
32
33#include <machine/frame.h>
34#include <machine/sigframe.h>
35
36#include <errno.h>
37#include <signal.h>
38#include <stdarg.h>
39#include <stdlib.h>
40#include <strings.h>
41#include <unistd.h>
42
43__weak_reference(__signalcontext, signalcontext);
44
45extern void _ctx_start(void);
46
47int
48__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
49{
50	struct sigframe *sfp;
51	struct frame *fp;
52	mcontext_t *mc;
53
54	mc = &ucp->uc_mcontext;
55	sfp = (struct sigframe *)(mc->_mc_sp + SPOFF) - 1;
56	fp = (struct frame *)sfp - 1;
57
58	bzero(fp, sizeof(*fp));
59
60	bzero(sfp, sizeof(*sfp));
61	bcopy(ucp, &sfp->sf_uc, sizeof(*ucp));
62	sfp->sf_si.si_signo = sig;
63
64	mc->mc_global[1] = (uint64_t)func;
65	mc->mc_global[2] = (uint64_t)ucp;
66	mc->mc_out[0] = sig;
67	mc->mc_out[1] = (uint64_t)&sfp->sf_si;
68	mc->mc_out[2] = (uint64_t)&sfp->sf_uc;
69	mc->mc_out[6] = (uint64_t)fp - SPOFF;
70	mc->_mc_tnpc = (uint64_t)_ctx_start + 4;
71	mc->_mc_tpc = (uint64_t)_ctx_start;
72
73	ucp->uc_link = &sfp->sf_uc;
74	sigdelset(&ucp->uc_sigmask, sig);
75
76	return (0);
77}
78