xref: /dflybsd-src/sys/sys/signal2.h (revision bbd7e13329c5bb82b8ee744e2c8780d994efbfbf)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)signalvar.h	8.6 (Berkeley) 2/19/95
30  * $FreeBSD: src/sys/sys/signalvar.h,v 1.34.2.1 2000/05/16 06:58:05 dillon Exp $
31  */
32 
33 #ifndef _SYS_SIGNAL2_H_
34 #define _SYS_SIGNAL2_H_
35 
36 #include <sys/param.h>
37 #include <sys/proc.h>
38 #include <sys/signalvar.h>
39 #include <sys/systm.h>
40 
41 /*
42  * Inline functions:
43  */
44 /*
45  * Determine which signals are pending for a lwp.
46  *
47  * (Does not need to be interlocked with lwp_spin.  If caller holds a
48  *  critical section races will be resolved through an AST).
49  */
50 static __inline sigset_t
lwp_sigpend(struct lwp * lp)51 lwp_sigpend(struct lwp *lp)
52 {
53 	sigset_t set;
54 
55 	set = lp->lwp_proc->p_siglist;
56 	SIGSETOR(set, lp->lwp_siglist);
57 
58 	return (set);
59 }
60 
61 /*
62  * Mark a signal as handled by the lwp.
63  *
64  * (p->p_token must be held, lp->lwp_spin must be held)
65  */
66 static __inline void
lwp_delsig(struct lwp * lp,int sig,int fromproc)67 lwp_delsig(struct lwp *lp, int sig, int fromproc)
68 {
69 	SIGDELSET(lp->lwp_siglist, sig);
70 	if (fromproc)
71 		SIGDELSET_ATOMIC(lp->lwp_proc->p_siglist, sig);
72 }
73 
74 #define	CURSIG(lp)			__cursig(lp, 1, 0, NULL)
75 #define	CURSIG_TRACE(lp)		__cursig(lp, 1, 1, NULL)
76 #define	CURSIG_LCK_TRACE(lp, ptok)	__cursig(lp, 1, 1, ptok)
77 #define CURSIG_NOBLOCK(lp)		__cursig(lp, 0, 0, NULL)
78 
79 /*
80  * This inline checks lpmap->blockallsigs, a user r/w accessible
81  * memory-mapped variable that allows a user thread to instantly
82  * mask and unmask all maskable signals without having to issue a
83  * system call.
84  *
85  * On the unmask count reaching 0, userland can check and clear
86  * bit 31 to determine if any signals arrived, then issue a dummy
87  * system call to ensure delivery.
88  */
89 static __inline
90 void
__sig_condblockallsigs(sigset_t * mask,struct lwp * lp)91 __sig_condblockallsigs(sigset_t *mask, struct lwp *lp)
92 {
93 	struct sys_lpmap *lpmap;
94 	uint32_t bas;
95 	sigset_t tmp;
96 	int trapsig;
97 
98 	if ((lpmap = lp->lwp_lpmap) == NULL)
99 		return;
100 
101 	bas = lpmap->blockallsigs;
102 	while (bas & 0x7FFFFFFFU) {
103 		tmp = *mask;			/* check maskable signals */
104 		SIG_CANTMASK(tmp);
105 		if (SIGISEMPTY(tmp))		/* no unmaskable signals */
106 			return;
107 
108 		/*
109 		 * Upon successful update to lpmap->blockallsigs remove
110 		 * all maskable signals, leaving only unmaskable signals.
111 		 *
112 		 * If lwp_sig is non-zero it represents a syncronous 'trap'
113 		 * signal which, being a synchronous trap, must be allowed.
114 		 */
115 		if (atomic_fcmpset_int(&lpmap->blockallsigs, &bas,
116 				       bas | 0x80000000U)) {
117 			trapsig = lp->lwp_sig;
118 			if (trapsig && SIGISMEMBER(*mask, trapsig)) {
119 				SIGSETAND(*mask, sigcantmask_mask);
120 				SIGADDSET(*mask, trapsig);
121 			} else {
122 				SIGSETAND(*mask, sigcantmask_mask);
123 			}
124 			break;
125 		}
126 	}
127 }
128 
129 /*
130  * Determine signal that should be delivered to process p, the current
131  * process, 0 if none.  If there is a pending stop signal with default
132  * action, the process stops in issignal().
133  *
134  * This function does not interlock pending signals.  If the caller needs
135  * to interlock the caller must acquire the per-proc token.
136  *
137  * If ptok is non-NULL this function may return with proc->p_token held,
138  * indicating that the signal came from the process structure.  This is
139  * used by postsig to avoid holding p_token when possible.  Only applicable
140  * if mayblock is non-zero.
141  */
142 static __inline
143 int
__cursig(struct lwp * lp,int mayblock,int maytrace,int * ptok)144 __cursig(struct lwp *lp, int mayblock, int maytrace, int *ptok)
145 {
146 	struct proc *p = lp->lwp_proc;
147 	sigset_t tmpset;
148 	int r;
149 
150 	tmpset = lwp_sigpend(lp);
151 	SIGSETNAND(tmpset, lp->lwp_sigmask);
152 	SIG_CONDBLOCKALLSIGS(tmpset, lp);
153 
154 	/* Nothing interesting happening? */
155 	if (SIGISEMPTY(tmpset)) {
156 		/*
157 		 * Quit here, unless
158 		 *  a) we may block and
159 		 *  b) somebody is tracing us.
160 		 */
161 		if (mayblock == 0 || (p->p_flags & P_TRACED) == 0)
162 			return (0);
163 	}
164 
165 	if (mayblock)
166 		r = issignal(lp, maytrace, ptok);
167 	else
168 		r = TRUE;	/* simply state the fact */
169 
170 	return(r);
171 }
172 
173 /*
174  * Generic (non-directed) signal processing on process is in progress
175  */
176 static __inline
177 void
sigirefs_hold(struct proc * p)178 sigirefs_hold(struct proc *p)
179 {
180 	atomic_add_int(&p->p_sigirefs, 1);
181 }
182 
183 /*
184  * Signal processing complete
185  */
186 static __inline
187 void
sigirefs_drop(struct proc * p)188 sigirefs_drop(struct proc *p)
189 {
190 	if (atomic_fetchadd_int(&p->p_sigirefs, -1) == 0x80000001U) {
191 		atomic_clear_int(&p->p_sigirefs, 0x80000000U);
192 		wakeup(&p->p_sigirefs);
193 	}
194 }
195 
196 /*
197  * Wait for generic (non directed) signal processing on process to
198  * complete to interlock against races.  Called after lwp_sigmask
199  * has been changed.
200  */
201 static __inline
202 void
sigirefs_wait(struct proc * p)203 sigirefs_wait(struct proc *p)
204 {
205 	uint32_t refs;
206 
207 	cpu_mfence();
208 	refs = *(volatile uint32_t *)&p->p_sigirefs;
209 	if (refs & 0x7FFFFFFF) {
210 		while (refs & 0x7FFFFFFF) {
211 			tsleep_interlock(&p->p_sigirefs, 0);
212 			if (atomic_fcmpset_int(&p->p_sigirefs, &refs,
213 						refs | 0x80000000U))
214 			{
215 				tsleep(&p->p_sigirefs, PINTERLOCKED,
216 				       "sirefs", 0);
217 			}
218 		}
219 	}
220 }
221 
222 #endif /* !_SYS_SIGNAL2_H_ */
223