1129202Scognet/*-
2129202Scognet * Copyright (c) 2004 Olivier Houchard
3129202Scognet * All rights reserved.
4129202Scognet *
5129202Scognet * Redistribution and use in source and binary forms, with or without
6129202Scognet * modification, are permitted provided that the following conditions
7129202Scognet * are met:
8129202Scognet * 1. Redistributions of source code must retain the above copyright
9129202Scognet *    notice, this list of conditions and the following disclaimer.
10129202Scognet * 2. Redistributions in binary form must reproduce the above copyright
11129202Scognet *    notice, this list of conditions and the following disclaimer in the
12129202Scognet *    documentation and/or other materials provided with the distribution.
13129202Scognet *
14129202Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15129202Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16129202Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17129202Scognet * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18129202Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19129202Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20129202Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21129202Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22129202Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23129202Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24129202Scognet * SUCH DAMAGE.
25129202Scognet */
26129202Scognet
27129202Scognet#include <sys/cdefs.h>
28129202Scognet__FBSDID("$FreeBSD$");
29129202Scognet
30129202Scognet#include <sys/param.h>
31129202Scognet#include <sys/signal.h>
32129202Scognet#include <sys/ucontext.h>
33129202Scognet
34129202Scognet#include <machine/frame.h>
35129202Scognet#include <machine/sigframe.h>
36129202Scognet
37129202Scognet#include <errno.h>
38129202Scognet#include <stdarg.h>
39129202Scognet#include <stdlib.h>
40129202Scognet#include <unistd.h>
41129202Scognet#include <strings.h>
42129202Scognet#include <signal.h>
43129202Scognet
44129202Scognet__weak_reference(__signalcontext, signalcontext);
45129202Scognet
46129202Scognetextern void _ctx_start(void);
47129202Scognet
48129202Scognetint
49129202Scognet__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
50129202Scognet{
51129202Scognet	struct sigframe *sfp;
52129202Scognet	__greg_t *gr = ucp->uc_mcontext.__gregs;
53129202Scognet	unsigned int *sp;
54129202Scognet
55137286Scognet	sp = (unsigned int *)gr[_REG_SP];
56129202Scognet
57129202Scognet	sfp = (struct sigframe *)sp - 1;
58129202Scognet
59129202Scognet	bzero(sfp, sizeof(*sfp));
60129202Scognet	bcopy(ucp, &sfp->sf_uc, sizeof(*ucp));
61129202Scognet	sfp->sf_si.si_signo = sig;
62129202Scognet
63137286Scognet	gr[_REG_SP] = (__greg_t)sfp;
64129202Scognet	/* Wipe out frame pointer. */
65129202Scognet	gr[_REG_FP] = 0;
66129202Scognet	/* Arrange for return via the trampoline code. */
67137286Scognet	gr[_REG_PC] = (__greg_t)_ctx_start;
68137286Scognet	gr[_REG_R4] = (__greg_t)func;
69137286Scognet	gr[_REG_R5] = (__greg_t)ucp;
70137286Scognet	gr[_REG_R0] = (__greg_t)sig;
71137286Scognet	gr[_REG_R1] = (__greg_t)&sfp->sf_si;
72137286Scognet	gr[_REG_R2] = (__greg_t)&sfp->sf_uc;
73129202Scognet
74129202Scognet	ucp->uc_link = &sfp->sf_uc;
75129202Scognet	sigdelset(&ucp->uc_sigmask, sig);
76129202Scognet
77129202Scognet	return (0);
78129202Scognet}
79