blob: 6de3db04b1a49f2b1ced440f4221749ed26f2553 [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
Michal Simek2148daa92009-03-27 14:25:14 +010047
Arnd Bergmanne5135882009-06-18 19:55:30 +020048asmlinkage long
Michal Simek2148daa92009-03-27 14:25:14 +010049sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
50 struct pt_regs *regs)
51{
52 return do_sigaltstack(uss, uoss, regs->r1);
53}
54
55/*
56 * Do a signal return; undo the signal stack.
57 */
Michal Simek2148daa92009-03-27 14:25:14 +010058struct sigframe {
59 struct sigcontext sc;
60 unsigned long extramask[_NSIG_WORDS-1];
61 unsigned long tramp[2]; /* signal trampoline */
62};
63
64struct rt_sigframe {
65 struct siginfo info;
66 struct ucontext uc;
67 unsigned long tramp[2]; /* signal trampoline */
68};
69
Arnd Bergmann353b4312009-05-01 13:37:46 +000070static int restore_sigcontext(struct pt_regs *regs,
71 struct sigcontext __user *sc, int *rval_p)
Michal Simek2148daa92009-03-27 14:25:14 +010072{
73 unsigned int err = 0;
74
75#define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
76 COPY(r0);
77 COPY(r1);
78 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
79 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
80 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
81 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
82 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
83 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
84 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
85 COPY(r30); COPY(r31);
86 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
87#undef COPY
88
89 *rval_p = regs->r3;
90
91 return err;
92}
93
Arnd Bergmann3183e062009-06-18 19:55:29 +020094asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
Michal Simek2148daa92009-03-27 14:25:14 +010095{
Arnd Bergmann353b4312009-05-01 13:37:46 +000096 struct rt_sigframe __user *frame =
97 (struct rt_sigframe __user *)(regs->r1 + STATE_SAVE_ARG_SPACE);
Michal Simek2921e2b2009-04-21 14:08:47 +020098
Michal Simek2148daa92009-03-27 14:25:14 +010099 sigset_t set;
Michal Simek2148daa92009-03-27 14:25:14 +0100100 int rval;
101
102 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
103 goto badframe;
104
105 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
106 goto badframe;
107
108 sigdelsetmask(&set, ~_BLOCKABLE);
109 spin_lock_irq(&current->sighand->siglock);
110 current->blocked = set;
111 recalc_sigpending();
112 spin_unlock_irq(&current->sighand->siglock);
113
114 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
115 goto badframe;
116
Michal Simek2148daa92009-03-27 14:25:14 +0100117 /* It is more difficult to avoid calling this function than to
118 call it and ignore errors. */
Arnd Bergmann353b4312009-05-01 13:37:46 +0000119 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->r1))
120 goto badframe;
Michal Simek2148daa92009-03-27 14:25:14 +0100121
122 return rval;
123
124badframe:
125 force_sig(SIGSEGV, current);
126 return 0;
127}
128
129/*
130 * Set up a signal frame.
131 */
132
133static int
Arnd Bergmann353b4312009-05-01 13:37:46 +0000134setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
Michal Simek2148daa92009-03-27 14:25:14 +0100135 unsigned long mask)
136{
137 int err = 0;
138
139#define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
140 COPY(r0);
141 COPY(r1);
142 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
143 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
144 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
145 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
146 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
147 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
148 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
149 COPY(r30); COPY(r31);
150 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
151#undef COPY
152
153 err |= __put_user(mask, &sc->oldmask);
154
155 return err;
156}
157
158/*
159 * Determine which stack to use..
160 */
Arnd Bergmann353b4312009-05-01 13:37:46 +0000161static inline void __user *
Michal Simek2148daa92009-03-27 14:25:14 +0100162get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
163{
164 /* Default to using normal stack */
165 unsigned long sp = regs->r1;
166
167 if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
168 sp = current->sas_ss_sp + current->sas_ss_size;
169
Arnd Bergmann353b4312009-05-01 13:37:46 +0000170 return (void __user *)((sp - frame_size) & -8UL);
Michal Simek2148daa92009-03-27 14:25:14 +0100171}
172
Michal Simek2148daa92009-03-27 14:25:14 +0100173static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
174 sigset_t *set, struct pt_regs *regs)
175{
Arnd Bergmann353b4312009-05-01 13:37:46 +0000176 struct rt_sigframe __user *frame;
Michal Simek2148daa92009-03-27 14:25:14 +0100177 int err = 0;
178 int signal;
Michal Simek2ee2ff82009-12-10 11:43:57 +0100179 unsigned long address = 0;
180#ifdef CONFIG_MMU
181 pmd_t *pmdp;
182 pte_t *ptep;
183#endif
Michal Simek2148daa92009-03-27 14:25:14 +0100184
185 frame = get_sigframe(ka, regs, sizeof(*frame));
186
187 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
188 goto give_sigsegv;
189
190 signal = current_thread_info()->exec_domain
191 && current_thread_info()->exec_domain->signal_invmap
192 && sig < 32
193 ? current_thread_info()->exec_domain->signal_invmap[sig]
194 : sig;
195
Michal Simeka6029d12009-05-12 12:10:52 +0200196 if (info)
197 err |= copy_siginfo_to_user(&frame->info, info);
Michal Simek2148daa92009-03-27 14:25:14 +0100198
199 /* Create the ucontext. */
200 err |= __put_user(0, &frame->uc.uc_flags);
201 err |= __put_user(0, &frame->uc.uc_link);
202 err |= __put_user((void *)current->sas_ss_sp,
203 &frame->uc.uc_stack.ss_sp);
204 err |= __put_user(sas_ss_flags(regs->r1),
205 &frame->uc.uc_stack.ss_flags);
206 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
207 err |= setup_sigcontext(&frame->uc.uc_mcontext,
208 regs, set->sig[0]);
209 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
210
211 /* Set up to return from userspace. If provided, use a stub
212 already in userspace. */
213 /* minus 8 is offset to cater for "rtsd r15,8" */
Arnd Bergmann3183e062009-06-18 19:55:29 +0200214 /* addi r12, r0, __NR_sigreturn */
215 err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
216 frame->tramp + 0);
217 /* brki r14, 0x8 */
218 err |= __put_user(0xb9cc0008, frame->tramp + 1);
Michal Simek2148daa92009-03-27 14:25:14 +0100219
Arnd Bergmann3183e062009-06-18 19:55:29 +0200220 /* Return from sighandler will jump to the tramp.
221 Negative 8 offset because return is rtsd r15, 8 */
222 regs->r15 = ((unsigned long)frame->tramp)-8;
Michal Simek2148daa92009-03-27 14:25:14 +0100223
Michal Simek2ee2ff82009-12-10 11:43:57 +0100224 address = ((unsigned long)frame->tramp);
225#ifdef CONFIG_MMU
226 pmdp = pmd_offset(pud_offset(
227 pgd_offset(current->mm, address),
228 address), address);
Michal Simek2148daa92009-03-27 14:25:14 +0100229
Michal Simek2ee2ff82009-12-10 11:43:57 +0100230 preempt_disable();
231 ptep = pte_offset_map(pmdp, address);
232 if (pte_present(*ptep)) {
233 address = (unsigned long) page_address(pte_page(*ptep));
234 /* MS: I need add offset in page */
235 address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
236 /* MS address is virtual */
237 address = virt_to_phys(address);
238 invalidate_icache_range(address, address + 8);
239 flush_dcache_range(address, address + 8);
240 }
241 pte_unmap(ptep);
242 preempt_enable();
243#else
244 flush_icache_range(address, address + 8);
245 flush_dcache_range(address, address + 8);
246#endif
Michal Simek2148daa92009-03-27 14:25:14 +0100247 if (err)
248 goto give_sigsegv;
249
250 /* Set up registers for signal handler */
Michal Simek2921e2b2009-04-21 14:08:47 +0200251 regs->r1 = (unsigned long) frame - STATE_SAVE_ARG_SPACE;
252
Michal Simek2148daa92009-03-27 14:25:14 +0100253 /* Signal handler args: */
254 regs->r5 = signal; /* arg 0: signum */
255 regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */
256 regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */
257 /* Offset to handle microblaze rtid r14, 0 */
258 regs->pc = (unsigned long)ka->sa.sa_handler;
259
260 set_fs(USER_DS);
261
Michal Simeka01523c2009-10-15 11:32:25 +0200262 /* the tracer may want to single-step inside the handler */
263 if (test_thread_flag(TIF_SINGLESTEP))
264 ptrace_notify(SIGTRAP);
265
Michal Simek2148daa92009-03-27 14:25:14 +0100266#ifdef DEBUG_SIG
267 printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n",
268 current->comm, current->pid, frame, regs->pc);
269#endif
270
271 return;
272
273give_sigsegv:
274 if (sig == SIGSEGV)
275 ka->sa.sa_handler = SIG_DFL;
276 force_sig(SIGSEGV, current);
277}
278
279/* Handle restarting system calls */
280static inline void
281handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
282{
283 switch (regs->r3) {
284 case -ERESTART_RESTARTBLOCK:
285 case -ERESTARTNOHAND:
286 if (!has_handler)
287 goto do_restart;
288 regs->r3 = -EINTR;
289 break;
290 case -ERESTARTSYS:
291 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
292 regs->r3 = -EINTR;
293 break;
294 }
295 /* fallthrough */
296 case -ERESTARTNOINTR:
297do_restart:
298 /* offset of 4 bytes to re-execute trap (brki) instruction */
Michal Simek8b286262009-05-26 16:30:28 +0200299#ifndef CONFIG_MMU
Michal Simek2148daa92009-03-27 14:25:14 +0100300 regs->pc -= 4;
Michal Simek8b286262009-05-26 16:30:28 +0200301#else
302 /* offset of 8 bytes required = 4 for rtbd
303 offset, plus 4 for size of
304 "brki r14,8"
305 instruction. */
306 regs->pc -= 8;
307#endif
Michal Simek2148daa92009-03-27 14:25:14 +0100308 break;
309 }
310}
311
312/*
313 * OK, we're invoking a handler
314 */
315
Arnd Bergmann3183e062009-06-18 19:55:29 +0200316static int
Michal Simek2148daa92009-03-27 14:25:14 +0100317handle_signal(unsigned long sig, struct k_sigaction *ka,
318 siginfo_t *info, sigset_t *oldset, struct pt_regs *regs)
319{
320 /* Set up the stack frame */
321 if (ka->sa.sa_flags & SA_SIGINFO)
322 setup_rt_frame(sig, ka, info, oldset, regs);
323 else
Michal Simeka6029d12009-05-12 12:10:52 +0200324 setup_rt_frame(sig, ka, NULL, oldset, regs);
Michal Simek2148daa92009-03-27 14:25:14 +0100325
326 if (ka->sa.sa_flags & SA_ONESHOT)
327 ka->sa.sa_handler = SIG_DFL;
328
329 if (!(ka->sa.sa_flags & SA_NODEFER)) {
330 spin_lock_irq(&current->sighand->siglock);
331 sigorsets(&current->blocked,
332 &current->blocked, &ka->sa.sa_mask);
333 sigaddset(&current->blocked, sig);
334 recalc_sigpending();
335 spin_unlock_irq(&current->sighand->siglock);
336 }
Arnd Bergmann3183e062009-06-18 19:55:29 +0200337 return 1;
Michal Simek2148daa92009-03-27 14:25:14 +0100338}
339
340/*
341 * Note that 'init' is a special process: it doesn't get signals it doesn't
342 * want to handle. Thus you cannot kill init even with a SIGKILL even by
343 * mistake.
344 *
345 * Note that we go through the signals twice: once to check the signals that
346 * the kernel can handle, and then we build all the user-level signal handling
347 * stack-frames in one go after that.
348 */
349int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_syscall)
350{
351 siginfo_t info;
352 int signr;
353 struct k_sigaction ka;
354#ifdef DEBUG_SIG
355 printk(KERN_INFO "do signal: %p %p %d\n", regs, oldset, in_syscall);
356 printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
357 regs->r12, current_thread_info()->flags);
358#endif
359 /*
360 * We want the common case to go fast, which
361 * is why we may in certain cases get here from
362 * kernel mode. Just return without doing anything
363 * if so.
364 */
365 if (kernel_mode(regs))
366 return 1;
367
Arnd Bergmann3183e062009-06-18 19:55:29 +0200368 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
369 oldset = &current->saved_sigmask;
370 else
Michal Simek2148daa92009-03-27 14:25:14 +0100371 oldset = &current->blocked;
372
373 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
374 if (signr > 0) {
375 /* Whee! Actually deliver the signal. */
376 if (in_syscall)
377 handle_restart(regs, &ka, 1);
Arnd Bergmann3183e062009-06-18 19:55:29 +0200378 if (handle_signal(signr, &ka, &info, oldset, regs)) {
379 /*
380 * A signal was successfully delivered; the saved
381 * sigmask will have been stored in the signal frame,
382 * and will be restored by sigreturn, so we can simply
383 * clear the TS_RESTORE_SIGMASK flag.
384 */
385 current_thread_info()->status &=
386 ~TS_RESTORE_SIGMASK;
387 }
Michal Simek2148daa92009-03-27 14:25:14 +0100388 return 1;
389 }
390
391 if (in_syscall)
392 handle_restart(regs, NULL, 0);
393
Arnd Bergmann3183e062009-06-18 19:55:29 +0200394 /*
395 * If there's no signal to deliver, we just put the saved sigmask
396 * back.
397 */
398 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
399 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
400 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
401 }
402
Michal Simek2148daa92009-03-27 14:25:14 +0100403 /* Did we come from a system call? */
404 return 0;
405}