xref: /openbmc/linux/arch/um/kernel/signal.c (revision 9cdb81c7)
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5 
6 #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>
12 #include "frame_kern.h"
13 #include "kern_util.h"
14 
15 EXPORT_SYMBOL(block_signals);
16 EXPORT_SYMBOL(unblock_signals);
17 
18 #define _S(nr) (1<<((nr)-1))
19 
20 #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
21 
22 /*
23  * OK, we're invoking a handler
24  */
25 static int handle_signal(struct pt_regs *regs, unsigned long signr,
26 			 struct k_sigaction *ka, siginfo_t *info,
27 			 sigset_t *oldset)
28 {
29 	unsigned long sp;
30 	int err;
31 
32 	/* Always make any pending restarted system calls return -EINTR */
33 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
34 
35 	/* Did we come from a system call? */
36 	if (PT_REGS_SYSCALL_NR(regs) >= 0) {
37 		/* If so, check system call restarting.. */
38 		switch (PT_REGS_SYSCALL_RET(regs)) {
39 		case -ERESTART_RESTARTBLOCK:
40 		case -ERESTARTNOHAND:
41 			PT_REGS_SYSCALL_RET(regs) = -EINTR;
42 			break;
43 
44 		case -ERESTARTSYS:
45 			if (!(ka->sa.sa_flags & SA_RESTART)) {
46 				PT_REGS_SYSCALL_RET(regs) = -EINTR;
47 				break;
48 			}
49 		/* fallthrough */
50 		case -ERESTARTNOINTR:
51 			PT_REGS_RESTART_SYSCALL(regs);
52 			PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
53 			break;
54 		}
55 	}
56 
57 	sp = PT_REGS_SP(regs);
58 	if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
59 		sp = current->sas_ss_sp + current->sas_ss_size;
60 
61 #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
62 	if (!(ka->sa.sa_flags & SA_SIGINFO))
63 		err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
64 	else
65 #endif
66 		err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
67 
68 	if (err)
69 		force_sigsegv(signr, current);
70 	else
71 		block_sigmask(ka, signr);
72 
73 	return err;
74 }
75 
76 static int kern_do_signal(struct pt_regs *regs)
77 {
78 	struct k_sigaction ka_copy;
79 	siginfo_t info;
80 	sigset_t *oldset;
81 	int sig, handled_sig = 0;
82 
83 	if (test_thread_flag(TIF_RESTORE_SIGMASK))
84 		oldset = &current->saved_sigmask;
85 	else
86 		oldset = &current->blocked;
87 
88 	while ((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0) {
89 		handled_sig = 1;
90 		/* Whee!  Actually deliver the signal.  */
91 		if (!handle_signal(regs, sig, &ka_copy, &info, oldset)) {
92 			/*
93 			 * a signal was successfully delivered; the saved
94 			 * sigmask will have been stored in the signal frame,
95 			 * and will be restored by sigreturn, so we can simply
96 			 * clear the TIF_RESTORE_SIGMASK flag
97 			 */
98 			if (test_thread_flag(TIF_RESTORE_SIGMASK))
99 				clear_thread_flag(TIF_RESTORE_SIGMASK);
100 			break;
101 		}
102 	}
103 
104 	/* Did we come from a system call? */
105 	if (!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)) {
106 		/* Restart the system call - no handlers present */
107 		switch (PT_REGS_SYSCALL_RET(regs)) {
108 		case -ERESTARTNOHAND:
109 		case -ERESTARTSYS:
110 		case -ERESTARTNOINTR:
111 			PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
112 			PT_REGS_RESTART_SYSCALL(regs);
113 			break;
114 		case -ERESTART_RESTARTBLOCK:
115 			PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
116 			PT_REGS_RESTART_SYSCALL(regs);
117 			break;
118 		}
119 	}
120 
121 	/*
122 	 * This closes a way to execute a system call on the host.  If
123 	 * you set a breakpoint on a system call instruction and singlestep
124 	 * from it, the tracing thread used to PTRACE_SINGLESTEP the process
125 	 * rather than PTRACE_SYSCALL it, allowing the system call to execute
126 	 * on the host.  The tracing thread will check this flag and
127 	 * PTRACE_SYSCALL if necessary.
128 	 */
129 	if (current->ptrace & PT_DTRACE)
130 		current->thread.singlestep_syscall =
131 			is_syscall(PT_REGS_IP(&current->thread.regs));
132 
133 	/*
134 	 * if there's no signal to deliver, we just put the saved sigmask
135 	 * back
136 	 */
137 	if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) {
138 		clear_thread_flag(TIF_RESTORE_SIGMASK);
139 		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
140 	}
141 	return handled_sig;
142 }
143 
144 int do_signal(void)
145 {
146 	return kern_do_signal(&current->thread.regs);
147 }
148 
149 /*
150  * Atomically swap in the new signal mask, and wait for a signal.
151  */
152 long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
153 {
154 	sigset_t blocked;
155 
156 	mask &= _BLOCKABLE;
157 	siginitset(&blocked, mask);
158 	set_current_blocked(&blocked);
159 
160 	current->state = TASK_INTERRUPTIBLE;
161 	schedule();
162 	set_thread_flag(TIF_RESTORE_SIGMASK);
163 	return -ERESTARTNOHAND;
164 }
165 
166 long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
167 {
168 	return do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs));
169 }
170