xref: /netbsd-src/sys/arch/mips/mips/sig_machdep.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: sig_machdep.c,v 1.22 2011/04/29 22:09:41 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
33 
34 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.22 2011/04/29 22:09:41 matt Exp $");
35 
36 #include "opt_cputype.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/signal.h>
43 #include <sys/signalvar.h>
44 #include <sys/mount.h>
45 #include <sys/syscallargs.h>
46 
47 #include <machine/cpu.h>
48 
49 #include <mips/frame.h>
50 #include <mips/regnum.h>
51 #include <mips/locore.h>
52 
53 void *
54 getframe(struct lwp *l, int sig, int *onstack)
55 {
56 	struct proc * const p = l->l_proc;
57 	struct trapframe * const tf = l->l_md.md_utf;
58 
59 	/* Do we need to jump onto the signal stack? */
60 	*onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
61 	    && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
62 	if (*onstack)
63 		return (char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size;
64 	return (void *)(intptr_t)tf->tf_regs[_R_SP];
65 }
66 
67 struct sigframe_siginfo {
68 	siginfo_t sf_si;
69 	ucontext_t sf_uc;
70 };
71 
72 /*
73  * Send a signal to process.
74  */
75 void
76 sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
77 {
78 	struct lwp * const l = curlwp;
79 	struct proc * const p = l->l_proc;
80 	struct sigacts * const sa = p->p_sigacts;
81 	struct trapframe * const tf = l->l_md.md_utf;
82 	int onstack, error;
83 	const int signo = ksi->ksi_signo;
84 	struct sigframe_siginfo *sf = getframe(l, signo, &onstack);
85 	struct sigframe_siginfo ksf;
86 	const sig_t catcher = SIGACTION(p, signo).sa_handler;
87 
88 	sf--;
89 
90 	ksf.sf_si._info = ksi->ksi_info;
91 	ksf.sf_uc.uc_flags = _UC_SIGMASK
92 	    | (l->l_sigstk.ss_flags & SS_ONSTACK ? _UC_SETSTACK : _UC_CLRSTACK);
93 	ksf.sf_uc.uc_sigmask = *mask;
94 	ksf.sf_uc.uc_link = l->l_ctxlink;
95 	memset(&ksf.sf_uc.uc_stack, 0, sizeof(ksf.sf_uc.uc_stack));
96 	sendsig_reset(l, signo);
97 
98 	mutex_exit(p->p_lock);
99 	cpu_getmcontext(l, &ksf.sf_uc.uc_mcontext, &ksf.sf_uc.uc_flags);
100 	error = copyout(&ksf, sf, sizeof(ksf));
101 	mutex_enter(p->p_lock);
102 
103 	if (error != 0) {
104 		/*
105 		 * Process has trashed its stack; give it an illegal
106 		 * instruction to halt it in its tracks.
107 		 */
108 		sigexit(l, SIGILL);
109 		/* NOTREACHED */
110 	}
111 
112 	/*
113 	 * Set up the registers to directly invoke the signal
114 	 * handler.  The return address will be set up to point
115 	 * to the signal trampoline to bounce us back.
116 	 */
117 	tf->tf_regs[_R_A0] = signo;
118 	tf->tf_regs[_R_A1] = (intptr_t)&sf->sf_si;
119 	tf->tf_regs[_R_A2] = (intptr_t)&sf->sf_uc;
120 
121 	tf->tf_regs[_R_PC] = (intptr_t)catcher;
122 	tf->tf_regs[_R_T9] = (intptr_t)catcher;
123 	tf->tf_regs[_R_SP] = (intptr_t)sf;
124 	tf->tf_regs[_R_RA] = (intptr_t)sa->sa_sigdesc[signo].sd_tramp;
125 
126 	/* Remember that we're now on the signal stack. */
127 	if (onstack)
128 		l->l_sigstk.ss_flags |= SS_ONSTACK;
129 }
130