blob: 57acbd67d85dbd4051cd3c534ceff2a300ce9906 [file] [log] [blame]
Jeff Dike1d3468a2006-07-10 04:45:13 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dikec5d4bb12008-02-04 22:31:14 -08006#include <linux/module.h>
7#include <linux/ptrace.h>
8#include <linux/sched.h>
9#include <asm/siginfo.h>
10#include <asm/signal.h>
11#include <asm/unistd.h>
Al Viro37185b32012-10-08 03:27:32 +010012#include <frame_kern.h>
13#include <kern_util.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15EXPORT_SYMBOL(block_signals);
16EXPORT_SYMBOL(unblock_signals);
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
19 * OK, we're invoking a handler
Jeff Dike1d3468a2006-07-10 04:45:13 -070020 */
Richard Weinberger307627e2013-10-06 21:57:10 +020021static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Al Virob7f9a112012-05-02 09:59:21 -040023 sigset_t *oldset = sigmask_to_save();
Al Virof9a38ea2012-09-06 13:39:47 -040024 int singlestep = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 unsigned long sp;
26 int err;
27
Al Virof9a38ea2012-09-06 13:39:47 -040028 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
29 singlestep = 1;
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 /* Did we come from a system call? */
Jeff Dikeba180fd2007-10-16 01:27:00 -070032 if (PT_REGS_SYSCALL_NR(regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 /* If so, check system call restarting.. */
Jeff Dikec5d4bb12008-02-04 22:31:14 -080034 switch (PT_REGS_SYSCALL_RET(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 case -ERESTART_RESTARTBLOCK:
36 case -ERESTARTNOHAND:
37 PT_REGS_SYSCALL_RET(regs) = -EINTR;
38 break;
39
40 case -ERESTARTSYS:
Richard Weinberger307627e2013-10-06 21:57:10 +020041 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 PT_REGS_SYSCALL_RET(regs) = -EINTR;
43 break;
44 }
45 /* fallthrough */
46 case -ERESTARTNOINTR:
47 PT_REGS_RESTART_SYSCALL(regs);
48 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
49 break;
50 }
51 }
52
53 sp = PT_REGS_SP(regs);
Richard Weinberger307627e2013-10-06 21:57:10 +020054 if ((ksig->ka.sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 sp = current->sas_ss_sp + current->sas_ss_size;
56
57#ifdef CONFIG_ARCH_HAS_SC_SIGNALS
Richard Weinberger307627e2013-10-06 21:57:10 +020058 if (!(ksig->ka.sa.sa_flags & SA_SIGINFO))
59 err = setup_signal_stack_sc(sp, ksig, regs, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 else
61#endif
Richard Weinberger307627e2013-10-06 21:57:10 +020062 err = setup_signal_stack_si(sp, ksig, regs, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Richard Weinberger307627e2013-10-06 21:57:10 +020064 signal_setup_done(err, ksig, singlestep);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Ingo Molnarccaee5f2015-07-03 12:44:20 -070067void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Richard Weinberger307627e2013-10-06 21:57:10 +020069 struct ksignal ksig;
70 int handled_sig = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Richard Weinberger322740e2016-01-25 23:33:30 +010072 while (get_signal(&ksig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 handled_sig = 1;
74 /* Whee! Actually deliver the signal. */
Richard Weinberger307627e2013-10-06 21:57:10 +020075 handle_signal(&ksig, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
78 /* Did we come from a system call? */
Jeff Dikeba180fd2007-10-16 01:27:00 -070079 if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* Restart the system call - no handlers present */
Jeff Dikec5d4bb12008-02-04 22:31:14 -080081 switch (PT_REGS_SYSCALL_RET(regs)) {
Jeff Dike2fc10622006-01-18 17:44:02 -080082 case -ERESTARTNOHAND:
83 case -ERESTARTSYS:
84 case -ERESTARTNOINTR:
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
86 PT_REGS_RESTART_SYSCALL(regs);
Jeff Dike2fc10622006-01-18 17:44:02 -080087 break;
88 case -ERESTART_RESTARTBLOCK:
Jeff Dike1d3468a2006-07-10 04:45:13 -070089 PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 PT_REGS_RESTART_SYSCALL(regs);
Jeff Dike2fc10622006-01-18 17:44:02 -080091 break;
Jeff Dikeba180fd2007-10-16 01:27:00 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Jeff Dikeba180fd2007-10-16 01:27:00 -070095 /*
96 * This closes a way to execute a system call on the host. If
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * you set a breakpoint on a system call instruction and singlestep
98 * from it, the tracing thread used to PTRACE_SINGLESTEP the process
99 * rather than PTRACE_SYSCALL it, allowing the system call to execute
Jeff Dike1d3468a2006-07-10 04:45:13 -0700100 * on the host. The tracing thread will check this flag and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * PTRACE_SYSCALL if necessary.
102 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103 if (current->ptrace & PT_DTRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 current->thread.singlestep_syscall =
105 is_syscall(PT_REGS_IP(&current->thread.regs));
Jeff Dike2fc10622006-01-18 17:44:02 -0800106
Jeff Dikeba180fd2007-10-16 01:27:00 -0700107 /*
108 * if there's no signal to deliver, we just put the saved sigmask
109 * back
110 */
Al Viro51a7b442012-05-21 23:33:55 -0400111 if (!handled_sig)
112 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}