blob: d0b3507ec6baad84eefa327b4f18b13b3385795d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/alpha/kernel/signal.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 *
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
7 */
8
9#include <linux/sched.h>
10#include <linux/kernel.h>
11#include <linux/signal.h>
12#include <linux/errno.h>
13#include <linux/wait.h>
14#include <linux/ptrace.h>
15#include <linux/unistd.h>
16#include <linux/mm.h>
17#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/stddef.h>
19#include <linux/tty.h>
20#include <linux/binfmts.h>
21#include <linux/bitops.h>
Ivan Kokshayskye5d9a902009-01-29 14:25:18 -080022#include <linux/syscalls.h>
David Howells733e5e42009-09-09 08:30:21 +010023#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26#include <asm/sigcontext.h>
27#include <asm/ucontext.h>
28
29#include "proto.h"
30
31
32#define DEBUG_SIG 0
33
34#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
36asmlinkage void ret_from_sys_call(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * The OSF/1 sigprocmask calling sequence is different from the
40 * C sigprocmask() sequence..
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Al Viroc52c2dd2010-09-26 19:28:12 +010042SYSCALL_DEFINE2(osf_sigprocmask, int, how, unsigned long, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Al Viroc52c2dd2010-09-26 19:28:12 +010044 sigset_t oldmask;
45 sigset_t mask;
46 unsigned long res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds87400e52010-09-30 08:37:38 -070048 siginitset(&mask, newmask & _BLOCKABLE);
Linus Torvalds0f44fbd2010-09-28 13:26:57 -070049 res = sigprocmask(how, &mask, &oldmask);
Al Viroc52c2dd2010-09-26 19:28:12 +010050 if (!res) {
51 force_successful_syscall_return();
Linus Torvalds0f44fbd2010-09-28 13:26:57 -070052 res = oldmask.sig[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Al Viroc52c2dd2010-09-26 19:28:12 +010054 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Ivan Kokshayskye5d9a902009-01-29 14:25:18 -080057SYSCALL_DEFINE3(osf_sigaction, int, sig,
58 const struct osf_sigaction __user *, act,
59 struct osf_sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 struct k_sigaction new_ka, old_ka;
62 int ret;
63
64 if (act) {
65 old_sigset_t mask;
66 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
67 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
Al Viro18e6bfa2010-09-26 19:28:22 +010068 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
69 __get_user(mask, &act->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 siginitset(&new_ka.sa.sa_mask, mask);
72 new_ka.ka_restorer = NULL;
73 }
74
75 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
76
77 if (!ret && oact) {
78 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
79 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
Al Viro18e6bfa2010-09-26 19:28:22 +010080 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
81 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85 return ret;
86}
87
Ivan Kokshayskye5d9a902009-01-29 14:25:18 -080088SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act,
89 struct sigaction __user *, oact,
90 size_t, sigsetsize, void __user *, restorer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct k_sigaction new_ka, old_ka;
93 int ret;
94
95 /* XXX: Don't preclude handling different sized sigset_t's. */
96 if (sigsetsize != sizeof(sigset_t))
97 return -EINVAL;
98
99 if (act) {
100 new_ka.ka_restorer = restorer;
101 if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
102 return -EFAULT;
103 }
104
105 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
106
107 if (!ret && oact) {
108 if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
109 return -EFAULT;
110 }
111
112 return ret;
113}
114
115/*
116 * Atomically swap in the new signal mask, and wait for a signal.
117 */
Al Viro392fb6e2010-09-18 08:40:07 -0400118SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Matt Fleming2561b062012-04-05 14:25:12 -0700120 sigset_t blocked;
Matt Fleming2561b062012-04-05 14:25:12 -0700121 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -0400122 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125asmlinkage int
126sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
127{
128 return do_sigaltstack(uss, uoss, rdusp());
129}
130
131/*
132 * Do a signal return; undo the signal stack.
133 */
134
135#if _NSIG_WORDS > 1
136# error "Non SA_SIGINFO frame needs rearranging"
137#endif
138
139struct sigframe
140{
141 struct sigcontext sc;
142 unsigned int retcode[3];
143};
144
145struct rt_sigframe
146{
147 struct siginfo info;
148 struct ucontext uc;
149 unsigned int retcode[3];
150};
151
152/* If this changes, userland unwinders that Know Things about our signal
153 frame will break. Do not undertake lightly. It also implies an ABI
154 change wrt the size of siginfo_t, which may cause some pain. */
155extern char compile_time_assert
156 [offsetof(struct rt_sigframe, uc.uc_mcontext) == 176 ? 1 : -1];
157
158#define INSN_MOV_R30_R16 0x47fe0410
159#define INSN_LDI_R0 0x201f0000
160#define INSN_CALLSYS 0x00000083
161
162static long
163restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
164 struct switch_stack *sw)
165{
166 unsigned long usp;
167 long i, err = __get_user(regs->pc, &sc->sc_pc);
168
Al Viro2deba1b2010-09-18 08:38:47 -0400169 current_thread_info()->restart_block.fn = do_no_restart_syscall;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sw->r26 = (unsigned long) ret_from_sys_call;
172
173 err |= __get_user(regs->r0, sc->sc_regs+0);
174 err |= __get_user(regs->r1, sc->sc_regs+1);
175 err |= __get_user(regs->r2, sc->sc_regs+2);
176 err |= __get_user(regs->r3, sc->sc_regs+3);
177 err |= __get_user(regs->r4, sc->sc_regs+4);
178 err |= __get_user(regs->r5, sc->sc_regs+5);
179 err |= __get_user(regs->r6, sc->sc_regs+6);
180 err |= __get_user(regs->r7, sc->sc_regs+7);
181 err |= __get_user(regs->r8, sc->sc_regs+8);
182 err |= __get_user(sw->r9, sc->sc_regs+9);
183 err |= __get_user(sw->r10, sc->sc_regs+10);
184 err |= __get_user(sw->r11, sc->sc_regs+11);
185 err |= __get_user(sw->r12, sc->sc_regs+12);
186 err |= __get_user(sw->r13, sc->sc_regs+13);
187 err |= __get_user(sw->r14, sc->sc_regs+14);
188 err |= __get_user(sw->r15, sc->sc_regs+15);
189 err |= __get_user(regs->r16, sc->sc_regs+16);
190 err |= __get_user(regs->r17, sc->sc_regs+17);
191 err |= __get_user(regs->r18, sc->sc_regs+18);
192 err |= __get_user(regs->r19, sc->sc_regs+19);
193 err |= __get_user(regs->r20, sc->sc_regs+20);
194 err |= __get_user(regs->r21, sc->sc_regs+21);
195 err |= __get_user(regs->r22, sc->sc_regs+22);
196 err |= __get_user(regs->r23, sc->sc_regs+23);
197 err |= __get_user(regs->r24, sc->sc_regs+24);
198 err |= __get_user(regs->r25, sc->sc_regs+25);
199 err |= __get_user(regs->r26, sc->sc_regs+26);
200 err |= __get_user(regs->r27, sc->sc_regs+27);
201 err |= __get_user(regs->r28, sc->sc_regs+28);
202 err |= __get_user(regs->gp, sc->sc_regs+29);
203 err |= __get_user(usp, sc->sc_regs+30);
204 wrusp(usp);
205
206 for (i = 0; i < 31; i++)
207 err |= __get_user(sw->fp[i], sc->sc_fpregs+i);
208 err |= __get_user(sw->fp[31], &sc->sc_fpcr);
209
210 return err;
211}
212
213/* Note that this syscall is also used by setcontext(3) to install
214 a given sigcontext. This because it's impossible to set *all*
215 registers and transfer control from userland. */
216
217asmlinkage void
218do_sigreturn(struct sigcontext __user *sc, struct pt_regs *regs,
219 struct switch_stack *sw)
220{
221 sigset_t set;
222
223 /* Verify that it's a good sigcontext before using it */
224 if (!access_ok(VERIFY_READ, sc, sizeof(*sc)))
225 goto give_sigsegv;
226 if (__get_user(set.sig[0], &sc->sc_mask))
227 goto give_sigsegv;
228
Matt Fleming2561b062012-04-05 14:25:12 -0700229 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (restore_sigcontext(sc, regs, sw))
232 goto give_sigsegv;
233
234 /* Send SIGTRAP if we're single-stepping: */
235 if (ptrace_cancel_bpt (current)) {
236 siginfo_t info;
237
238 info.si_signo = SIGTRAP;
239 info.si_errno = 0;
240 info.si_code = TRAP_BRKPT;
241 info.si_addr = (void __user *) regs->pc;
242 info.si_trapno = 0;
243 send_sig_info(SIGTRAP, &info, current);
244 }
245 return;
246
247give_sigsegv:
248 force_sig(SIGSEGV, current);
249}
250
251asmlinkage void
252do_rt_sigreturn(struct rt_sigframe __user *frame, struct pt_regs *regs,
253 struct switch_stack *sw)
254{
255 sigset_t set;
256
257 /* Verify that it's a good ucontext_t before using it */
258 if (!access_ok(VERIFY_READ, &frame->uc, sizeof(frame->uc)))
259 goto give_sigsegv;
260 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
261 goto give_sigsegv;
262
Matt Fleming2561b062012-04-05 14:25:12 -0700263 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (restore_sigcontext(&frame->uc.uc_mcontext, regs, sw))
266 goto give_sigsegv;
267
268 /* Send SIGTRAP if we're single-stepping: */
269 if (ptrace_cancel_bpt (current)) {
270 siginfo_t info;
271
272 info.si_signo = SIGTRAP;
273 info.si_errno = 0;
274 info.si_code = TRAP_BRKPT;
275 info.si_addr = (void __user *) regs->pc;
276 info.si_trapno = 0;
277 send_sig_info(SIGTRAP, &info, current);
278 }
279 return;
280
281give_sigsegv:
282 force_sig(SIGSEGV, current);
283}
284
285
286/*
287 * Set up a signal frame.
288 */
289
290static inline void __user *
291get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
292{
Laurent MEYERd09042d2006-06-23 02:05:36 -0700293 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && ! sas_ss_flags(sp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 sp = current->sas_ss_sp + current->sas_ss_size;
295
296 return (void __user *)((sp - frame_size) & -32ul);
297}
298
299static long
300setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
Al Virod9d07382012-09-05 18:53:18 -0400301 unsigned long mask, unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Al Virod9d07382012-09-05 18:53:18 -0400303 struct switch_stack *sw = (struct switch_stack *)regs - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 long i, err = 0;
305
306 err |= __put_user(on_sig_stack((unsigned long)sc), &sc->sc_onstack);
307 err |= __put_user(mask, &sc->sc_mask);
308 err |= __put_user(regs->pc, &sc->sc_pc);
309 err |= __put_user(8, &sc->sc_ps);
310
311 err |= __put_user(regs->r0 , sc->sc_regs+0);
312 err |= __put_user(regs->r1 , sc->sc_regs+1);
313 err |= __put_user(regs->r2 , sc->sc_regs+2);
314 err |= __put_user(regs->r3 , sc->sc_regs+3);
315 err |= __put_user(regs->r4 , sc->sc_regs+4);
316 err |= __put_user(regs->r5 , sc->sc_regs+5);
317 err |= __put_user(regs->r6 , sc->sc_regs+6);
318 err |= __put_user(regs->r7 , sc->sc_regs+7);
319 err |= __put_user(regs->r8 , sc->sc_regs+8);
320 err |= __put_user(sw->r9 , sc->sc_regs+9);
321 err |= __put_user(sw->r10 , sc->sc_regs+10);
322 err |= __put_user(sw->r11 , sc->sc_regs+11);
323 err |= __put_user(sw->r12 , sc->sc_regs+12);
324 err |= __put_user(sw->r13 , sc->sc_regs+13);
325 err |= __put_user(sw->r14 , sc->sc_regs+14);
326 err |= __put_user(sw->r15 , sc->sc_regs+15);
327 err |= __put_user(regs->r16, sc->sc_regs+16);
328 err |= __put_user(regs->r17, sc->sc_regs+17);
329 err |= __put_user(regs->r18, sc->sc_regs+18);
330 err |= __put_user(regs->r19, sc->sc_regs+19);
331 err |= __put_user(regs->r20, sc->sc_regs+20);
332 err |= __put_user(regs->r21, sc->sc_regs+21);
333 err |= __put_user(regs->r22, sc->sc_regs+22);
334 err |= __put_user(regs->r23, sc->sc_regs+23);
335 err |= __put_user(regs->r24, sc->sc_regs+24);
336 err |= __put_user(regs->r25, sc->sc_regs+25);
337 err |= __put_user(regs->r26, sc->sc_regs+26);
338 err |= __put_user(regs->r27, sc->sc_regs+27);
339 err |= __put_user(regs->r28, sc->sc_regs+28);
340 err |= __put_user(regs->gp , sc->sc_regs+29);
341 err |= __put_user(sp, sc->sc_regs+30);
342 err |= __put_user(0, sc->sc_regs+31);
343
344 for (i = 0; i < 31; i++)
345 err |= __put_user(sw->fp[i], sc->sc_fpregs+i);
346 err |= __put_user(0, sc->sc_fpregs+31);
347 err |= __put_user(sw->fp[31], &sc->sc_fpcr);
348
349 err |= __put_user(regs->trap_a0, &sc->sc_traparg_a0);
350 err |= __put_user(regs->trap_a1, &sc->sc_traparg_a1);
351 err |= __put_user(regs->trap_a2, &sc->sc_traparg_a2);
352
353 return err;
354}
355
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700356static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
Al Virod9d07382012-09-05 18:53:18 -0400358 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 unsigned long oldsp, r26, err = 0;
361 struct sigframe __user *frame;
362
363 oldsp = rdusp();
364 frame = get_sigframe(ka, oldsp, sizeof(*frame));
365 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Al Virocbdfb9f2012-04-22 02:34:42 -0400366 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Al Virod9d07382012-09-05 18:53:18 -0400368 err |= setup_sigcontext(&frame->sc, regs, set->sig[0], oldsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (err)
Al Virocbdfb9f2012-04-22 02:34:42 -0400370 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Set up to return from userspace. If provided, use a stub
373 already in userspace. */
374 if (ka->ka_restorer) {
375 r26 = (unsigned long) ka->ka_restorer;
376 } else {
377 err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
378 err |= __put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1);
379 err |= __put_user(INSN_CALLSYS, frame->retcode+2);
380 imb();
381 r26 = (unsigned long) frame->retcode;
382 }
383
384 /* Check that everything was written properly. */
385 if (err)
Al Virocbdfb9f2012-04-22 02:34:42 -0400386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* "Return" to the handler */
389 regs->r26 = r26;
390 regs->r27 = regs->pc = (unsigned long) ka->sa.sa_handler;
391 regs->r16 = sig; /* a0: signal number */
392 regs->r17 = 0; /* a1: exception code */
393 regs->r18 = (unsigned long) &frame->sc; /* a2: sigcontext pointer */
394 wrusp((unsigned long) frame);
395
396#if DEBUG_SIG
397 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
398 current->comm, current->pid, frame, regs->pc, regs->r26);
399#endif
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700403static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Al Virod9d07382012-09-05 18:53:18 -0400405 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 unsigned long oldsp, r26, err = 0;
408 struct rt_sigframe __user *frame;
409
410 oldsp = rdusp();
411 frame = get_sigframe(ka, oldsp, sizeof(*frame));
412 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Al Virocbdfb9f2012-04-22 02:34:42 -0400413 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 err |= copy_siginfo_to_user(&frame->info, info);
416
417 /* Create the ucontext. */
418 err |= __put_user(0, &frame->uc.uc_flags);
419 err |= __put_user(0, &frame->uc.uc_link);
420 err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask);
421 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
422 err |= __put_user(sas_ss_flags(oldsp), &frame->uc.uc_stack.ss_flags);
423 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
Al Virod9d07382012-09-05 18:53:18 -0400424 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 set->sig[0], oldsp);
426 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
427 if (err)
Al Virocbdfb9f2012-04-22 02:34:42 -0400428 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* Set up to return from userspace. If provided, use a stub
431 already in userspace. */
432 if (ka->ka_restorer) {
433 r26 = (unsigned long) ka->ka_restorer;
434 } else {
435 err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
436 err |= __put_user(INSN_LDI_R0+__NR_rt_sigreturn,
437 frame->retcode+1);
438 err |= __put_user(INSN_CALLSYS, frame->retcode+2);
439 imb();
440 r26 = (unsigned long) frame->retcode;
441 }
442
443 if (err)
Al Virocbdfb9f2012-04-22 02:34:42 -0400444 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* "Return" to the handler */
447 regs->r26 = r26;
448 regs->r27 = regs->pc = (unsigned long) ka->sa.sa_handler;
449 regs->r16 = sig; /* a0: signal number */
450 regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */
451 regs->r18 = (unsigned long) &frame->uc; /* a2: ucontext pointer */
452 wrusp((unsigned long) frame);
453
454#if DEBUG_SIG
455 printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
456 current->comm, current->pid, frame, regs->pc, regs->r26);
457#endif
458
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462
463/*
464 * OK, we're invoking a handler.
465 */
Al Virocbdfb9f2012-04-22 02:34:42 -0400466static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467handle_signal(int sig, struct k_sigaction *ka, siginfo_t *info,
Al Virod9d07382012-09-05 18:53:18 -0400468 struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Al Virob7f9a112012-05-02 09:59:21 -0400470 sigset_t *oldset = sigmask_to_save();
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700471 int ret;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (ka->sa.sa_flags & SA_SIGINFO)
Al Virod9d07382012-09-05 18:53:18 -0400474 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 else
Al Virod9d07382012-09-05 18:53:18 -0400476 ret = setup_frame(sig, ka, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Al Virocbdfb9f2012-04-22 02:34:42 -0400478 if (ret) {
479 force_sigsegv(sig, current);
480 return;
481 }
Al Viroefee9842012-04-28 02:04:15 -0400482 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485static inline void
486syscall_restart(unsigned long r0, unsigned long r19,
487 struct pt_regs *regs, struct k_sigaction *ka)
488{
489 switch (regs->r0) {
490 case ERESTARTSYS:
491 if (!(ka->sa.sa_flags & SA_RESTART)) {
492 case ERESTARTNOHAND:
493 regs->r0 = EINTR;
494 break;
495 }
496 /* fallthrough */
497 case ERESTARTNOINTR:
498 regs->r0 = r0; /* reset v0 and a3 and replay syscall */
499 regs->r19 = r19;
500 regs->pc -= 4;
501 break;
502 case ERESTART_RESTARTBLOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 regs->r0 = EINTR;
504 break;
505 }
506}
507
508
509/*
510 * Note that 'init' is a special process: it doesn't get signals it doesn't
511 * want to handle. Thus you cannot kill init even with a SIGKILL even by
512 * mistake.
513 *
514 * Note that we go through the signals twice: once to check the signals that
515 * the kernel can handle, and then we build all the user-level signal handling
516 * stack-frames in one go after that.
517 *
518 * "r0" and "r19" are the registers we need to restore for system call
519 * restart. "r0" is also used as an indicator whether we can restart at
520 * all (if we get here from anything but a syscall return, it will be 0)
521 */
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700522static void
Al Virod9d07382012-09-05 18:53:18 -0400523do_signal(struct pt_regs *regs, unsigned long r0, unsigned long r19)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 siginfo_t info;
526 int signr;
527 unsigned long single_stepping = ptrace_cancel_bpt(current);
528 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* This lets the debugger run, ... */
531 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* ... so re-check the single stepping. */
534 single_stepping |= ptrace_cancel_bpt(current);
535
536 if (signr > 0) {
537 /* Whee! Actually deliver the signal. */
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700538 if (r0)
539 syscall_restart(r0, r19, regs, &ka);
Al Virod9d07382012-09-05 18:53:18 -0400540 handle_signal(signr, &ka, &info, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (single_stepping)
542 ptrace_set_bpt(current); /* re-set bpt */
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
546 if (r0) {
547 switch (regs->r0) {
548 case ERESTARTNOHAND:
549 case ERESTARTSYS:
550 case ERESTARTNOINTR:
551 /* Reset v0 and a3 and replay syscall. */
552 regs->r0 = r0;
553 regs->r19 = r19;
554 regs->pc -= 4;
555 break;
556 case ERESTART_RESTARTBLOCK:
557 /* Force v0 to the restart syscall and reply. */
558 regs->r0 = __NR_restart_syscall;
559 regs->pc -= 4;
560 break;
561 }
562 }
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700563
564 /* If there's no signal to deliver, we just restore the saved mask. */
Al Viro51a7b442012-05-21 23:33:55 -0400565 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (single_stepping)
567 ptrace_set_bpt(current); /* re-set breakpoint */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570void
Al Virod9d07382012-09-05 18:53:18 -0400571do_work_pending(struct pt_regs *regs, struct switch_stack *unused,
Al Viro6972d6f2012-09-05 18:30:34 -0400572 unsigned long thread_flags,
Richard Hendersonb927b3e2007-05-29 16:03:28 -0700573 unsigned long r0, unsigned long r19)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Al Viro6972d6f2012-09-05 18:30:34 -0400575 do {
576 if (thread_flags & _TIF_NEED_RESCHED) {
577 schedule();
578 } else {
579 local_irq_enable();
580 if (thread_flags & _TIF_SIGPENDING) {
Al Virod9d07382012-09-05 18:53:18 -0400581 do_signal(regs, r0, r19);
Al Viro6972d6f2012-09-05 18:30:34 -0400582 r0 = 0;
583 } else {
584 clear_thread_flag(TIF_NOTIFY_RESUME);
585 tracehook_notify_resume(regs);
586 }
587 }
588 local_irq_disable();
589 thread_flags = current_thread_info()->flags;
590 } while (thread_flags & _TIF_WORK_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}