blob: 90de06da3f6fc548873d374d332d4000a8653b8d [file] [log] [blame]
Michal Simek2148daa92009-03-27 14:25:14 +01001/*
2 * Signal handling
3 *
4 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
5 * Copyright (C) 2008-2009 PetaLogix
6 * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au>
7 * Copyright (C) 2001 NEC Corporation
8 * Copyright (C) 2001 Miles Bader <miles@gnu.org>
9 * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima
10 * Copyright (C) 1991,1992 Linus Torvalds
11 *
12 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
13 *
14 * This file was was derived from the sh version, arch/sh/kernel/signal.c
15 *
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file COPYING in the main directory of this
18 * archive for more details.
19 */
20
21#include <linux/sched.h>
22#include <linux/mm.h>
23#include <linux/smp.h>
Michal Simek2148daa92009-03-27 14:25:14 +010024#include <linux/kernel.h>
25#include <linux/signal.h>
26#include <linux/errno.h>
27#include <linux/wait.h>
28#include <linux/ptrace.h>
29#include <linux/unistd.h>
30#include <linux/stddef.h>
31#include <linux/personality.h>
32#include <linux/percpu.h>
33#include <linux/linkage.h>
34#include <asm/entry.h>
35#include <asm/ucontext.h>
36#include <linux/uaccess.h>
37#include <asm/pgtable.h>
38#include <asm/pgalloc.h>
Michal Simek2148daa92009-03-27 14:25:14 +010039#include <linux/syscalls.h>
40#include <asm/cacheflush.h>
41#include <asm/syscalls.h>
42
43#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
44
45asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_sycall);
46
Arnd Bergmanne5135882009-06-18 19:55:30 +020047asmlinkage long
Michal Simek2148daa92009-03-27 14:25:14 +010048sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
49 struct pt_regs *regs)
50{
51 return do_sigaltstack(uss, uoss, regs->r1);
52}
53
54/*
55 * Do a signal return; undo the signal stack.
56 */
Michal Simek2148daa92009-03-27 14:25:14 +010057struct sigframe {
58 struct sigcontext sc;
59 unsigned long extramask[_NSIG_WORDS-1];
60 unsigned long tramp[2]; /* signal trampoline */
61};
62
63struct rt_sigframe {
64 struct siginfo info;
65 struct ucontext uc;
66 unsigned long tramp[2]; /* signal trampoline */
67};
68
Arnd Bergmann353b4312009-05-01 13:37:46 +000069static int restore_sigcontext(struct pt_regs *regs,
70 struct sigcontext __user *sc, int *rval_p)
Michal Simek2148daa92009-03-27 14:25:14 +010071{
72 unsigned int err = 0;
73
74#define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
75 COPY(r0);
76 COPY(r1);
77 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
78 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
79 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
80 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
81 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
82 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
83 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
84 COPY(r30); COPY(r31);
85 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
86#undef COPY
87
88 *rval_p = regs->r3;
89
90 return err;
91}
92
Arnd Bergmann3183e062009-06-18 19:55:29 +020093asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
Michal Simek2148daa92009-03-27 14:25:14 +010094{
Arnd Bergmann353b4312009-05-01 13:37:46 +000095 struct rt_sigframe __user *frame =
Michal Simek6e835572011-01-31 15:10:04 +010096 (struct rt_sigframe __user *)(regs->r1);
Michal Simek2921e2b2009-04-21 14:08:47 +020097
Michal Simek2148daa92009-03-27 14:25:14 +010098 sigset_t set;
Michal Simek2148daa92009-03-27 14:25:14 +010099 int rval;
100
101 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
102 goto badframe;
103
104 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
105 goto badframe;
106
107 sigdelsetmask(&set, ~_BLOCKABLE);
108 spin_lock_irq(&current->sighand->siglock);
109 current->blocked = set;
110 recalc_sigpending();
111 spin_unlock_irq(&current->sighand->siglock);
112
113 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
114 goto badframe;
115
Michal Simek2148daa92009-03-27 14:25:14 +0100116 /* It is more difficult to avoid calling this function than to
117 call it and ignore errors. */
Arnd Bergmann353b4312009-05-01 13:37:46 +0000118 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->r1))
119 goto badframe;
Michal Simek2148daa92009-03-27 14:25:14 +0100120
121 return rval;
122
123badframe:
124 force_sig(SIGSEGV, current);
125 return 0;
126}
127
128/*
129 * Set up a signal frame.
130 */
131
132static int
Arnd Bergmann353b4312009-05-01 13:37:46 +0000133setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
Michal Simek2148daa92009-03-27 14:25:14 +0100134 unsigned long mask)
135{
136 int err = 0;
137
138#define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
139 COPY(r0);
140 COPY(r1);
141 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
142 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
143 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
144 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
145 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
146 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
147 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
148 COPY(r30); COPY(r31);
149 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
150#undef COPY
151
152 err |= __put_user(mask, &sc->oldmask);
153
154 return err;
155}
156
157/*
158 * Determine which stack to use..
159 */
Arnd Bergmann353b4312009-05-01 13:37:46 +0000160static inline void __user *
Michal Simek2148daa92009-03-27 14:25:14 +0100161get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
162{
163 /* Default to using normal stack */
164 unsigned long sp = regs->r1;
165
166 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
167 sp = current->sas_ss_sp + current->sas_ss_size;
168
Arnd Bergmann353b4312009-05-01 13:37:46 +0000169 return (void __user *)((sp - frame_size) & -8UL);
Michal Simek2148daa92009-03-27 14:25:14 +0100170}
171
Michal Simek2148daa92009-03-27 14:25:14 +0100172static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
173 sigset_t *set, struct pt_regs *regs)
174{
Arnd Bergmann353b4312009-05-01 13:37:46 +0000175 struct rt_sigframe __user *frame;
Michal Simek2148daa92009-03-27 14:25:14 +0100176 int err = 0;
177 int signal;
Michal Simek2ee2ff82009-12-10 11:43:57 +0100178 unsigned long address = 0;
179#ifdef CONFIG_MMU
180 pmd_t *pmdp;
181 pte_t *ptep;
182#endif
Michal Simek2148daa92009-03-27 14:25:14 +0100183
184 frame = get_sigframe(ka, regs, sizeof(*frame));
185
186 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
187 goto give_sigsegv;
188
189 signal = current_thread_info()->exec_domain
190 && current_thread_info()->exec_domain->signal_invmap
191 && sig < 32
192 ? current_thread_info()->exec_domain->signal_invmap[sig]
193 : sig;
194
Michal Simeka6029d12009-05-12 12:10:52 +0200195 if (info)
196 err |= copy_siginfo_to_user(&frame->info, info);
Michal Simek2148daa92009-03-27 14:25:14 +0100197
198 /* Create the ucontext. */
199 err |= __put_user(0, &frame->uc.uc_flags);
Michal Simek2cfedb92011-02-07 12:25:37 +0100200 err |= __put_user(NULL, &frame->uc.uc_link);
201 err |= __put_user((void __user *)current->sas_ss_sp,
Michal Simek2148daa92009-03-27 14:25:14 +0100202 &frame->uc.uc_stack.ss_sp);
203 err |= __put_user(sas_ss_flags(regs->r1),
204 &frame->uc.uc_stack.ss_flags);
205 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
206 err |= setup_sigcontext(&frame->uc.uc_mcontext,
207 regs, set->sig[0]);
208 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
209
210 /* Set up to return from userspace. If provided, use a stub
211 already in userspace. */
212 /* minus 8 is offset to cater for "rtsd r15,8" */
Arnd Bergmann3183e062009-06-18 19:55:29 +0200213 /* addi r12, r0, __NR_sigreturn */
214 err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
215 frame->tramp + 0);
216 /* brki r14, 0x8 */
217 err |= __put_user(0xb9cc0008, frame->tramp + 1);
Michal Simek2148daa92009-03-27 14:25:14 +0100218
Arnd Bergmann3183e062009-06-18 19:55:29 +0200219 /* Return from sighandler will jump to the tramp.
220 Negative 8 offset because return is rtsd r15, 8 */
221 regs->r15 = ((unsigned long)frame->tramp)-8;
Michal Simek2148daa92009-03-27 14:25:14 +0100222
Michal Simek2ee2ff82009-12-10 11:43:57 +0100223 address = ((unsigned long)frame->tramp);
224#ifdef CONFIG_MMU
225 pmdp = pmd_offset(pud_offset(
226 pgd_offset(current->mm, address),
227 address), address);
Michal Simek2148daa92009-03-27 14:25:14 +0100228
Michal Simek2ee2ff82009-12-10 11:43:57 +0100229 preempt_disable();
230 ptep = pte_offset_map(pmdp, address);
231 if (pte_present(*ptep)) {
232 address = (unsigned long) page_address(pte_page(*ptep));
233 /* MS: I need add offset in page */
234 address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
235 /* MS address is virtual */
236 address = virt_to_phys(address);
237 invalidate_icache_range(address, address + 8);
238 flush_dcache_range(address, address + 8);
239 }
240 pte_unmap(ptep);
241 preempt_enable();
242#else
243 flush_icache_range(address, address + 8);
244 flush_dcache_range(address, address + 8);
245#endif
Michal Simek2148daa92009-03-27 14:25:14 +0100246 if (err)
247 goto give_sigsegv;
248
249 /* Set up registers for signal handler */
Michal Simek6e835572011-01-31 15:10:04 +0100250 regs->r1 = (unsigned long) frame;
Michal Simek2921e2b2009-04-21 14:08:47 +0200251
Michal Simek2148daa92009-03-27 14:25:14 +0100252 /* Signal handler args: */
253 regs->r5 = signal; /* arg 0: signum */
254 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
255 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
256 /* Offset to handle microblaze rtid r14, 0 */
257 regs->pc = (unsigned long)ka->sa.sa_handler;
258
259 set_fs(USER_DS);
260
Michal Simeka01523c2009-10-15 11:32:25 +0200261 /* the tracer may want to single-step inside the handler */
262 if (test_thread_flag(TIF_SINGLESTEP))
263 ptrace_notify(SIGTRAP);
264
Michal Simek2148daa92009-03-27 14:25:14 +0100265#ifdef DEBUG_SIG
266 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
267 current->comm, current->pid, frame, regs->pc);
268#endif
269
270 return;
271
272give_sigsegv:
Matt Flemingde8f2dd2012-05-11 10:58:03 +1000273 force_sigsegv(sig, current);
Michal Simek2148daa92009-03-27 14:25:14 +0100274}
275
276/* Handle restarting system calls */
277static inline void
278handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
279{
280 switch (regs->r3) {
281 case -ERESTART_RESTARTBLOCK:
282 case -ERESTARTNOHAND:
283 if (!has_handler)
284 goto do_restart;
285 regs->r3 = -EINTR;
286 break;
287 case -ERESTARTSYS:
288 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
289 regs->r3 = -EINTR;
290 break;
291 }
292 /* fallthrough */
293 case -ERESTARTNOINTR:
294do_restart:
295 /* offset of 4 bytes to re-execute trap (brki) instruction */
Michal Simek8b286262009-05-26 16:30:28 +0200296#ifndef CONFIG_MMU
Michal Simek2148daa92009-03-27 14:25:14 +0100297 regs->pc -= 4;
Michal Simek8b286262009-05-26 16:30:28 +0200298#else
299 /* offset of 8 bytes required = 4 for rtbd
300 offset, plus 4 for size of
301 "brki r14,8"
302 instruction. */
303 regs->pc -= 8;
304#endif
Michal Simek2148daa92009-03-27 14:25:14 +0100305 break;
306 }
307}
308
309/*
310 * OK, we're invoking a handler
311 */
312
Arnd Bergmann3183e062009-06-18 19:55:29 +0200313static int
Michal Simek2148daa92009-03-27 14:25:14 +0100314handle_signal(unsigned long sig, struct k_sigaction *ka,
315 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
316{
317 /* Set up the stack frame */
318 if (ka->sa.sa_flags & SA_SIGINFO)
319 setup_rt_frame(sig, ka, info, oldset, regs);
320 else
Michal Simeka6029d12009-05-12 12:10:52 +0200321 setup_rt_frame(sig, ka, NULL, oldset, regs);
Michal Simek2148daa92009-03-27 14:25:14 +0100322
323 if (ka->sa.sa_flags & SA_ONESHOT)
324 ka->sa.sa_handler = SIG_DFL;
325
326 if (!(ka->sa.sa_flags & SA_NODEFER)) {
327 spin_lock_irq(&current->sighand->siglock);
328 sigorsets(&current->blocked,
329 &current->blocked, &ka->sa.sa_mask);
330 sigaddset(&current->blocked, sig);
331 recalc_sigpending();
332 spin_unlock_irq(&current->sighand->siglock);
333 }
Arnd Bergmann3183e062009-06-18 19:55:29 +0200334 return 1;
Michal Simek2148daa92009-03-27 14:25:14 +0100335}
336
337/*
338 * Note that 'init' is a special process: it doesn't get signals it doesn't
339 * want to handle. Thus you cannot kill init even with a SIGKILL even by
340 * mistake.
341 *
342 * Note that we go through the signals twice: once to check the signals that
343 * the kernel can handle, and then we build all the user-level signal handling
344 * stack-frames in one go after that.
345 */
346int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_syscall)
347{
348 siginfo_t info;
349 int signr;
350 struct k_sigaction ka;
351#ifdef DEBUG_SIG
352 printk(KERN_INFO "do signal: %p %p %d\n", regs, oldset, in_syscall);
353 printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
354 regs->r12, current_thread_info()->flags);
355#endif
356 /*
357 * We want the common case to go fast, which
358 * is why we may in certain cases get here from
359 * kernel mode. Just return without doing anything
360 * if so.
361 */
362 if (kernel_mode(regs))
363 return 1;
364
Arnd Bergmann3183e062009-06-18 19:55:29 +0200365 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
366 oldset = &current->saved_sigmask;
367 else
Michal Simek2148daa92009-03-27 14:25:14 +0100368 oldset = &current->blocked;
369
370 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
371 if (signr > 0) {
372 /* Whee! Actually deliver the signal. */
373 if (in_syscall)
374 handle_restart(regs, &ka, 1);
Arnd Bergmann3183e062009-06-18 19:55:29 +0200375 if (handle_signal(signr, &ka, &info, oldset, regs)) {
376 /*
377 * A signal was successfully delivered; the saved
378 * sigmask will have been stored in the signal frame,
379 * and will be restored by sigreturn, so we can simply
380 * clear the TS_RESTORE_SIGMASK flag.
381 */
382 current_thread_info()->status &=
383 ~TS_RESTORE_SIGMASK;
384 }
Michal Simek2148daa92009-03-27 14:25:14 +0100385 return 1;
386 }
387
388 if (in_syscall)
389 handle_restart(regs, NULL, 0);
390
Arnd Bergmann3183e062009-06-18 19:55:29 +0200391 /*
392 * If there's no signal to deliver, we just put the saved sigmask
393 * back.
394 */
395 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
396 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
397 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
398 }
399
Michal Simek2148daa92009-03-27 14:25:14 +0100400 /* Did we come from a system call? */
401 return 0;
402}