Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / include / linux / signal.h
1 #ifndef _LINUX_SIGNAL_H
2 #define _LINUX_SIGNAL_H
3
4 #include <linux/list.h>
5 #include <linux/bug.h>
6 #include <uapi/linux/signal.h>
7
8 struct task_struct;
9
10 /* for sysctl */
11 extern int print_fatal_signals;
12 /*
13  * Real Time signals may be queued.
14  */
15
16 struct sigqueue {
17         struct list_head list;
18         int flags;
19         siginfo_t info;
20         struct user_struct *user;
21 };
22
23 /* flags values. */
24 #define SIGQUEUE_PREALLOC       1
25
26 struct sigpending {
27         struct list_head list;
28         sigset_t signal;
29 };
30
31 /*
32  * Define some primitives to manipulate sigset_t.
33  */
34
35 #ifndef __HAVE_ARCH_SIG_BITOPS
36 #include <linux/bitops.h>
37
38 /* We don't use <linux/bitops.h> for these because there is no need to
39    be atomic.  */
40 static inline void sigaddset(sigset_t *set, int _sig)
41 {
42         unsigned long sig = _sig - 1;
43         if (_NSIG_WORDS == 1)
44                 set->sig[0] |= 1UL << sig;
45         else
46                 set->sig[sig / _NSIG_BPW] |= 1UL << (sig % _NSIG_BPW);
47 }
48
49 static inline void sigdelset(sigset_t *set, int _sig)
50 {
51         unsigned long sig = _sig - 1;
52         if (_NSIG_WORDS == 1)
53                 set->sig[0] &= ~(1UL << sig);
54         else
55                 set->sig[sig / _NSIG_BPW] &= ~(1UL << (sig % _NSIG_BPW));
56 }
57
58 static inline int sigismember(sigset_t *set, int _sig)
59 {
60         unsigned long sig = _sig - 1;
61         if (_NSIG_WORDS == 1)
62                 return 1 & (set->sig[0] >> sig);
63         else
64                 return 1 & (set->sig[sig / _NSIG_BPW] >> (sig % _NSIG_BPW));
65 }
66
67 #endif /* __HAVE_ARCH_SIG_BITOPS */
68
69 static inline int sigisemptyset(sigset_t *set)
70 {
71         switch (_NSIG_WORDS) {
72         case 4:
73                 return (set->sig[3] | set->sig[2] |
74                         set->sig[1] | set->sig[0]) == 0;
75         case 2:
76                 return (set->sig[1] | set->sig[0]) == 0;
77         case 1:
78                 return set->sig[0] == 0;
79         default:
80                 BUILD_BUG();
81                 return 0;
82         }
83 }
84
85 #define sigmask(sig)    (1UL << ((sig) - 1))
86
87 #ifndef __HAVE_ARCH_SIG_SETOPS
88 #include <linux/string.h>
89
90 #define _SIG_SET_BINOP(name, op)                                        \
91 static inline void name(sigset_t *r, const sigset_t *a, const sigset_t *b) \
92 {                                                                       \
93         unsigned long a0, a1, a2, a3, b0, b1, b2, b3;                   \
94                                                                         \
95         switch (_NSIG_WORDS) {                                          \
96         case 4:                                                         \
97                 a3 = a->sig[3]; a2 = a->sig[2];                         \
98                 b3 = b->sig[3]; b2 = b->sig[2];                         \
99                 r->sig[3] = op(a3, b3);                                 \
100                 r->sig[2] = op(a2, b2);                                 \
101         case 2:                                                         \
102                 a1 = a->sig[1]; b1 = b->sig[1];                         \
103                 r->sig[1] = op(a1, b1);                                 \
104         case 1:                                                         \
105                 a0 = a->sig[0]; b0 = b->sig[0];                         \
106                 r->sig[0] = op(a0, b0);                                 \
107                 break;                                                  \
108         default:                                                        \
109                 BUILD_BUG();                                            \
110         }                                                               \
111 }
112
113 #define _sig_or(x,y)    ((x) | (y))
114 _SIG_SET_BINOP(sigorsets, _sig_or)
115
116 #define _sig_and(x,y)   ((x) & (y))
117 _SIG_SET_BINOP(sigandsets, _sig_and)
118
119 #define _sig_andn(x,y)  ((x) & ~(y))
120 _SIG_SET_BINOP(sigandnsets, _sig_andn)
121
122 #undef _SIG_SET_BINOP
123 #undef _sig_or
124 #undef _sig_and
125 #undef _sig_andn
126
127 #define _SIG_SET_OP(name, op)                                           \
128 static inline void name(sigset_t *set)                                  \
129 {                                                                       \
130         switch (_NSIG_WORDS) {                                          \
131         case 4: set->sig[3] = op(set->sig[3]);                          \
132                 set->sig[2] = op(set->sig[2]);                          \
133         case 2: set->sig[1] = op(set->sig[1]);                          \
134         case 1: set->sig[0] = op(set->sig[0]);                          \
135                     break;                                              \
136         default:                                                        \
137                 BUILD_BUG();                                            \
138         }                                                               \
139 }
140
141 #define _sig_not(x)     (~(x))
142 _SIG_SET_OP(signotset, _sig_not)
143
144 #undef _SIG_SET_OP
145 #undef _sig_not
146
147 static inline void sigemptyset(sigset_t *set)
148 {
149         switch (_NSIG_WORDS) {
150         default:
151                 memset(set, 0, sizeof(sigset_t));
152                 break;
153         case 2: set->sig[1] = 0;
154         case 1: set->sig[0] = 0;
155                 break;
156         }
157 }
158
159 static inline void sigfillset(sigset_t *set)
160 {
161         switch (_NSIG_WORDS) {
162         default:
163                 memset(set, -1, sizeof(sigset_t));
164                 break;
165         case 2: set->sig[1] = -1;
166         case 1: set->sig[0] = -1;
167                 break;
168         }
169 }
170
171 /* Some extensions for manipulating the low 32 signals in particular.  */
172
173 static inline void sigaddsetmask(sigset_t *set, unsigned long mask)
174 {
175         set->sig[0] |= mask;
176 }
177
178 static inline void sigdelsetmask(sigset_t *set, unsigned long mask)
179 {
180         set->sig[0] &= ~mask;
181 }
182
183 static inline int sigtestsetmask(sigset_t *set, unsigned long mask)
184 {
185         return (set->sig[0] & mask) != 0;
186 }
187
188 static inline void siginitset(sigset_t *set, unsigned long mask)
189 {
190         set->sig[0] = mask;
191         switch (_NSIG_WORDS) {
192         default:
193                 memset(&set->sig[1], 0, sizeof(long)*(_NSIG_WORDS-1));
194                 break;
195         case 2: set->sig[1] = 0;
196         case 1: ;
197         }
198 }
199
200 static inline void siginitsetinv(sigset_t *set, unsigned long mask)
201 {
202         set->sig[0] = ~mask;
203         switch (_NSIG_WORDS) {
204         default:
205                 memset(&set->sig[1], -1, sizeof(long)*(_NSIG_WORDS-1));
206                 break;
207         case 2: set->sig[1] = -1;
208         case 1: ;
209         }
210 }
211
212 #endif /* __HAVE_ARCH_SIG_SETOPS */
213
214 static inline void init_sigpending(struct sigpending *sig)
215 {
216         sigemptyset(&sig->signal);
217         INIT_LIST_HEAD(&sig->list);
218 }
219
220 extern void flush_sigqueue(struct sigpending *queue);
221 extern void flush_task_sigqueue(struct task_struct *tsk);
222
223 /* Test if 'sig' is valid signal. Use this instead of testing _NSIG directly */
224 static inline int valid_signal(unsigned long sig)
225 {
226         return sig <= _NSIG ? 1 : 0;
227 }
228
229 struct timespec;
230 struct pt_regs;
231
232 extern int next_signal(struct sigpending *pending, sigset_t *mask);
233 extern int do_send_sig_info(int sig, struct siginfo *info,
234                                 struct task_struct *p, bool group);
235 extern int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p);
236 extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
237 extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
238                                 const struct timespec *);
239 extern int sigprocmask(int, sigset_t *, sigset_t *);
240 extern void set_current_blocked(sigset_t *);
241 extern void __set_current_blocked(const sigset_t *);
242 extern int show_unhandled_signals;
243 extern int sigsuspend(sigset_t *);
244
245 struct sigaction {
246 #ifndef __ARCH_HAS_IRIX_SIGACTION
247         __sighandler_t  sa_handler;
248         unsigned long   sa_flags;
249 #else
250         unsigned int    sa_flags;
251         __sighandler_t  sa_handler;
252 #endif
253 #ifdef __ARCH_HAS_SA_RESTORER
254         __sigrestore_t sa_restorer;
255 #endif
256         sigset_t        sa_mask;        /* mask last for extensibility */
257 };
258
259 struct k_sigaction {
260         struct sigaction sa;
261 #ifdef __ARCH_HAS_KA_RESTORER
262         __sigrestore_t ka_restorer;
263 #endif
264 };
265  
266 #ifdef CONFIG_OLD_SIGACTION
267 struct old_sigaction {
268         __sighandler_t sa_handler;
269         old_sigset_t sa_mask;
270         unsigned long sa_flags;
271         __sigrestore_t sa_restorer;
272 };
273 #endif
274
275 struct ksignal {
276         struct k_sigaction ka;
277         siginfo_t info;
278         int sig;
279 };
280
281 extern int get_signal(struct ksignal *ksig);
282 extern void signal_setup_done(int failed, struct ksignal *ksig, int stepping);
283 extern void exit_signals(struct task_struct *tsk);
284 extern void kernel_sigaction(int, __sighandler_t);
285
286 static inline void allow_signal(int sig)
287 {
288         /*
289          * Kernel threads handle their own signals. Let the signal code
290          * know it'll be handled, so that they don't get converted to
291          * SIGKILL or just silently dropped.
292          */
293         kernel_sigaction(sig, (__force __sighandler_t)2);
294 }
295
296 static inline void disallow_signal(int sig)
297 {
298         kernel_sigaction(sig, SIG_IGN);
299 }
300
301 extern struct kmem_cache *sighand_cachep;
302
303 int unhandled_signal(struct task_struct *tsk, int sig);
304
305 /*
306  * In POSIX a signal is sent either to a specific thread (Linux task)
307  * or to the process as a whole (Linux thread group).  How the signal
308  * is sent determines whether it's to one thread or the whole group,
309  * which determines which signal mask(s) are involved in blocking it
310  * from being delivered until later.  When the signal is delivered,
311  * either it's caught or ignored by a user handler or it has a default
312  * effect that applies to the whole thread group (POSIX process).
313  *
314  * The possible effects an unblocked signal set to SIG_DFL can have are:
315  *   ignore     - Nothing Happens
316  *   terminate  - kill the process, i.e. all threads in the group,
317  *                similar to exit_group.  The group leader (only) reports
318  *                WIFSIGNALED status to its parent.
319  *   coredump   - write a core dump file describing all threads using
320  *                the same mm and then kill all those threads
321  *   stop       - stop all the threads in the group, i.e. TASK_STOPPED state
322  *
323  * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
324  * Other signals when not blocked and set to SIG_DFL behaves as follows.
325  * The job control signals also have other special effects.
326  *
327  *      +--------------------+------------------+
328  *      |  POSIX signal      |  default action  |
329  *      +--------------------+------------------+
330  *      |  SIGHUP            |  terminate       |
331  *      |  SIGINT            |  terminate       |
332  *      |  SIGQUIT           |  coredump        |
333  *      |  SIGILL            |  coredump        |
334  *      |  SIGTRAP           |  coredump        |
335  *      |  SIGABRT/SIGIOT    |  coredump        |
336  *      |  SIGBUS            |  coredump        |
337  *      |  SIGFPE            |  coredump        |
338  *      |  SIGKILL           |  terminate(+)    |
339  *      |  SIGUSR1           |  terminate       |
340  *      |  SIGSEGV           |  coredump        |
341  *      |  SIGUSR2           |  terminate       |
342  *      |  SIGPIPE           |  terminate       |
343  *      |  SIGALRM           |  terminate       |
344  *      |  SIGTERM           |  terminate       |
345  *      |  SIGCHLD           |  ignore          |
346  *      |  SIGCONT           |  ignore(*)       |
347  *      |  SIGSTOP           |  stop(*)(+)      |
348  *      |  SIGTSTP           |  stop(*)         |
349  *      |  SIGTTIN           |  stop(*)         |
350  *      |  SIGTTOU           |  stop(*)         |
351  *      |  SIGURG            |  ignore          |
352  *      |  SIGXCPU           |  coredump        |
353  *      |  SIGXFSZ           |  coredump        |
354  *      |  SIGVTALRM         |  terminate       |
355  *      |  SIGPROF           |  terminate       |
356  *      |  SIGPOLL/SIGIO     |  terminate       |
357  *      |  SIGSYS/SIGUNUSED  |  coredump        |
358  *      |  SIGSTKFLT         |  terminate       |
359  *      |  SIGWINCH          |  ignore          |
360  *      |  SIGPWR            |  terminate       |
361  *      |  SIGRTMIN-SIGRTMAX |  terminate       |
362  *      +--------------------+------------------+
363  *      |  non-POSIX signal  |  default action  |
364  *      +--------------------+------------------+
365  *      |  SIGEMT            |  coredump        |
366  *      +--------------------+------------------+
367  *
368  * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
369  * (*) Special job control effects:
370  * When SIGCONT is sent, it resumes the process (all threads in the group)
371  * from TASK_STOPPED state and also clears any pending/queued stop signals
372  * (any of those marked with "stop(*)").  This happens regardless of blocking,
373  * catching, or ignoring SIGCONT.  When any stop signal is sent, it clears
374  * any pending/queued SIGCONT signals; this happens regardless of blocking,
375  * catching, or ignored the stop signal, though (except for SIGSTOP) the
376  * default action of stopping the process may happen later or never.
377  */
378
379 #ifdef SIGEMT
380 #define SIGEMT_MASK     rt_sigmask(SIGEMT)
381 #else
382 #define SIGEMT_MASK     0
383 #endif
384
385 #if SIGRTMIN > BITS_PER_LONG
386 #define rt_sigmask(sig) (1ULL << ((sig)-1))
387 #else
388 #define rt_sigmask(sig) sigmask(sig)
389 #endif
390 #define siginmask(sig, mask) (rt_sigmask(sig) & (mask))
391
392 #define SIG_KERNEL_ONLY_MASK (\
393         rt_sigmask(SIGKILL)   |  rt_sigmask(SIGSTOP))
394
395 #define SIG_KERNEL_STOP_MASK (\
396         rt_sigmask(SIGSTOP)   |  rt_sigmask(SIGTSTP)   | \
397         rt_sigmask(SIGTTIN)   |  rt_sigmask(SIGTTOU)   )
398
399 #define SIG_KERNEL_COREDUMP_MASK (\
400         rt_sigmask(SIGQUIT)   |  rt_sigmask(SIGILL)    | \
401         rt_sigmask(SIGTRAP)   |  rt_sigmask(SIGABRT)   | \
402         rt_sigmask(SIGFPE)    |  rt_sigmask(SIGSEGV)   | \
403         rt_sigmask(SIGBUS)    |  rt_sigmask(SIGSYS)    | \
404         rt_sigmask(SIGXCPU)   |  rt_sigmask(SIGXFSZ)   | \
405         SIGEMT_MASK                                    )
406
407 #define SIG_KERNEL_IGNORE_MASK (\
408         rt_sigmask(SIGCONT)   |  rt_sigmask(SIGCHLD)   | \
409         rt_sigmask(SIGWINCH)  |  rt_sigmask(SIGURG)    )
410
411 #define sig_kernel_only(sig) \
412         (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_ONLY_MASK))
413 #define sig_kernel_coredump(sig) \
414         (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_COREDUMP_MASK))
415 #define sig_kernel_ignore(sig) \
416         (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_IGNORE_MASK))
417 #define sig_kernel_stop(sig) \
418         (((sig) < SIGRTMIN) && siginmask(sig, SIG_KERNEL_STOP_MASK))
419
420 #define sig_user_defined(t, signr) \
421         (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) &&  \
422          ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
423
424 #define sig_fatal(t, signr) \
425         (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
426          (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
427
428 void signals_init(void);
429
430 int restore_altstack(const stack_t __user *);
431 int __save_altstack(stack_t __user *, unsigned long);
432
433 #define save_altstack_ex(uss, sp) do { \
434         stack_t __user *__uss = uss; \
435         struct task_struct *t = current; \
436         put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
437         put_user_ex(sas_ss_flags(sp), &__uss->ss_flags); \
438         put_user_ex(t->sas_ss_size, &__uss->ss_size); \
439 } while (0);
440
441 #ifdef CONFIG_PROC_FS
442 struct seq_file;
443 extern void render_sigset_t(struct seq_file *, const char *, sigset_t *);
444 #endif
445
446 #endif /* _LINUX_SIGNAL_H */