xref: /freebsd/lib/libc/arm/gen/signalcontext.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
12357939bSOlivier Houchard /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
42357939bSOlivier Houchard  * Copyright (c) 2004 Olivier Houchard
52357939bSOlivier Houchard  * All rights reserved.
62357939bSOlivier Houchard  *
72357939bSOlivier Houchard  * Redistribution and use in source and binary forms, with or without
82357939bSOlivier Houchard  * modification, are permitted provided that the following conditions
92357939bSOlivier Houchard  * are met:
102357939bSOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
112357939bSOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
122357939bSOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
132357939bSOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
142357939bSOlivier Houchard  *    documentation and/or other materials provided with the distribution.
152357939bSOlivier Houchard  *
162357939bSOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172357939bSOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182357939bSOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192357939bSOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202357939bSOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212357939bSOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222357939bSOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232357939bSOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242357939bSOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252357939bSOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262357939bSOlivier Houchard  * SUCH DAMAGE.
272357939bSOlivier Houchard  */
282357939bSOlivier Houchard 
292357939bSOlivier Houchard #include <sys/param.h>
302357939bSOlivier Houchard #include <sys/signal.h>
312357939bSOlivier Houchard #include <sys/ucontext.h>
322357939bSOlivier Houchard 
332357939bSOlivier Houchard #include <machine/frame.h>
342357939bSOlivier Houchard #include <machine/sigframe.h>
352357939bSOlivier Houchard 
362357939bSOlivier Houchard #include <errno.h>
372357939bSOlivier Houchard #include <stdarg.h>
382357939bSOlivier Houchard #include <stdlib.h>
392357939bSOlivier Houchard #include <unistd.h>
402357939bSOlivier Houchard #include <strings.h>
412357939bSOlivier Houchard #include <signal.h>
422357939bSOlivier Houchard 
432357939bSOlivier Houchard __weak_reference(__signalcontext, signalcontext);
442357939bSOlivier Houchard 
452357939bSOlivier Houchard extern void _ctx_start(void);
462357939bSOlivier Houchard 
472357939bSOlivier Houchard int
__signalcontext(ucontext_t * ucp,int sig,__sighandler_t * func)482357939bSOlivier Houchard __signalcontext(ucontext_t *ucp, int sig, __sighandler_t *func)
492357939bSOlivier Houchard {
502357939bSOlivier Houchard 	struct sigframe *sfp;
512357939bSOlivier Houchard 	__greg_t *gr = ucp->uc_mcontext.__gregs;
522357939bSOlivier Houchard 	unsigned int *sp;
532357939bSOlivier Houchard 
5460497154SOlivier Houchard 	sp = (unsigned int *)gr[_REG_SP];
552357939bSOlivier Houchard 
562357939bSOlivier Houchard 	sfp = (struct sigframe *)sp - 1;
572357939bSOlivier Houchard 
582357939bSOlivier Houchard 	bzero(sfp, sizeof(*sfp));
592357939bSOlivier Houchard 	bcopy(ucp, &sfp->sf_uc, sizeof(*ucp));
602357939bSOlivier Houchard 	sfp->sf_si.si_signo = sig;
612357939bSOlivier Houchard 
6260497154SOlivier Houchard 	gr[_REG_SP] = (__greg_t)sfp;
632357939bSOlivier Houchard 	/* Wipe out frame pointer. */
642357939bSOlivier Houchard 	gr[_REG_FP] = 0;
652357939bSOlivier Houchard 	/* Arrange for return via the trampoline code. */
6660497154SOlivier Houchard 	gr[_REG_PC] = (__greg_t)_ctx_start;
6760497154SOlivier Houchard 	gr[_REG_R4] = (__greg_t)func;
6860497154SOlivier Houchard 	gr[_REG_R5] = (__greg_t)ucp;
6960497154SOlivier Houchard 	gr[_REG_R0] = (__greg_t)sig;
7060497154SOlivier Houchard 	gr[_REG_R1] = (__greg_t)&sfp->sf_si;
7160497154SOlivier Houchard 	gr[_REG_R2] = (__greg_t)&sfp->sf_uc;
722357939bSOlivier Houchard 
732357939bSOlivier Houchard 	ucp->uc_link = &sfp->sf_uc;
742357939bSOlivier Houchard 	sigdelset(&ucp->uc_sigmask, sig);
752357939bSOlivier Houchard 
762357939bSOlivier Houchard 	return (0);
772357939bSOlivier Houchard }
78