1251881Speter/*-
2251881Speter * Copyright (c) 2004 Olivier Houchard
3251881Speter * All rights reserved.
4251881Speter *
5251881Speter * Redistribution and use in source and binary forms, with or without
6251881Speter * modification, are permitted provided that the following conditions
7251881Speter * are met:
8251881Speter * 1. Redistributions of source code must retain the above copyright
9251881Speter *    notice, this list of conditions and the following disclaimer.
10251881Speter * 2. Redistributions in binary form must reproduce the above copyright
11251881Speter *    notice, this list of conditions and the following disclaimer in the
12251881Speter *    documentation and/or other materials provided with the distribution.
13251881Speter *
14251881Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15251881Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16251881Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17251881Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18251881Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19251881Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20251881Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21251881Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22251881Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23251881Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24251881Speter * SUCH DAMAGE.
25251881Speter */
26251881Speter
27251881Speter#include <sys/cdefs.h>
28251881Speter__FBSDID("$FreeBSD$");
29251881Speter
30251881Speter#include <sys/param.h>
31251881Speter#include <sys/signal.h>
32251881Speter#include <sys/ucontext.h>
33251881Speter
34251881Speter#include <machine/frame.h>
35251881Speter#include <machine/sigframe.h>
36251881Speter
37251881Speter#include <errno.h>
38251881Speter#include <stdarg.h>
39251881Speter#include <stdlib.h>
40251881Speter#include <unistd.h>
41251881Speter#include <strings.h>
42251881Speter#include <signal.h>
43251881Speter
44251881Speter__weak_reference(__signalcontext, signalcontext);
45251881Speter
46251881Speterextern void _ctx_start(void);
47251881Speter
48251881Speterint
49251881Speter__signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
50251881Speter{
51251881Speter	struct sigframe *sfp;
52251881Speter	__greg_t *gr = ucp->uc_mcontext.__gregs;
53251881Speter	unsigned int *sp;
54251881Speter
55251881Speter	sp = (unsigned int *)gr[_REG_SP];
56251881Speter
57251881Speter	sfp = (struct sigframe *)sp - 1;
58251881Speter
59251881Speter	bzero(sfp, sizeof(*sfp));
60251881Speter	bcopy(ucp, &sfp->sf_uc, sizeof(*ucp));
61251881Speter	sfp->sf_si.si_signo = sig;
62251881Speter
63251881Speter	gr[_REG_SP] = (__greg_t)sfp;
64251881Speter	/* Wipe out frame pointer. */
65251881Speter	gr[_REG_FP] = 0;
66251881Speter	/* Arrange for return via the trampoline code. */
67251881Speter	gr[_REG_PC] = (__greg_t)_ctx_start;
68251881Speter	gr[_REG_R4] = (__greg_t)func;
69251881Speter	gr[_REG_R5] = (__greg_t)ucp;
70251881Speter	gr[_REG_R0] = (__greg_t)sig;
71251881Speter	gr[_REG_R1] = (__greg_t)&sfp->sf_si;
72251881Speter	gr[_REG_R2] = (__greg_t)&sfp->sf_uc;
73251881Speter
74251881Speter	ucp->uc_link = &sfp->sf_uc;
75251881Speter	sigdelset(&ucp->uc_sigmask, sig);
76251881Speter
77251881Speter	return (0);
78251881Speter}
79251881Speter