sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[linux-2.6-block.git] / arch / tile / kernel / signal.c
1 /*
2  * Copyright (C) 1991, 1992  Linus Torvalds
3  * Copyright 2010 Tilera Corporation. All Rights Reserved.
4  *
5  *   This program is free software; you can redistribute it and/or
6  *   modify it under the terms of the GNU General Public License
7  *   as published by the Free Software Foundation, version 2.
8  *
9  *   This program is distributed in the hope that it will be useful, but
10  *   WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12  *   NON INFRINGEMENT.  See the GNU General Public License for
13  *   more details.
14  */
15
16 #include <linux/sched.h>
17 #include <linux/sched/debug.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/unistd.h>
25 #include <linux/stddef.h>
26 #include <linux/personality.h>
27 #include <linux/suspend.h>
28 #include <linux/ptrace.h>
29 #include <linux/elf.h>
30 #include <linux/compat.h>
31 #include <linux/syscalls.h>
32 #include <linux/uaccess.h>
33 #include <asm/processor.h>
34 #include <asm/ucontext.h>
35 #include <asm/sigframe.h>
36 #include <asm/syscalls.h>
37 #include <asm/vdso.h>
38 #include <arch/interrupts.h>
39
40 #define DEBUG_SIG 0
41
42 /*
43  * Do a signal return; undo the signal stack.
44  */
45
46 int restore_sigcontext(struct pt_regs *regs,
47                        struct sigcontext __user *sc)
48 {
49         int err;
50
51         /* Always make any pending restarted system calls return -EINTR */
52         current->restart_block.fn = do_no_restart_syscall;
53
54         /*
55          * Enforce that sigcontext is like pt_regs, and doesn't mess
56          * up our stack alignment rules.
57          */
58         BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
59         BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
60         err = __copy_from_user(regs, sc, sizeof(*regs));
61
62         /* Ensure that the PL is always set to USER_PL. */
63         regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
64
65         regs->faultnum = INT_SWINT_1_SIGRETURN;
66
67         return err;
68 }
69
70 void signal_fault(const char *type, struct pt_regs *regs,
71                   void __user *frame, int sig)
72 {
73         trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV);
74         force_sigsegv(sig, current);
75 }
76
77 /* The assembly shim for this function arranges to ignore the return value. */
78 SYSCALL_DEFINE0(rt_sigreturn)
79 {
80         struct pt_regs *regs = current_pt_regs();
81         struct rt_sigframe __user *frame =
82                 (struct rt_sigframe __user *)(regs->sp);
83         sigset_t set;
84
85         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
86                 goto badframe;
87         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
88                 goto badframe;
89
90         set_current_blocked(&set);
91
92         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
93                 goto badframe;
94
95         if (restore_altstack(&frame->uc.uc_stack))
96                 goto badframe;
97
98         return 0;
99
100 badframe:
101         signal_fault("bad sigreturn frame", regs, frame, 0);
102         return 0;
103 }
104
105 /*
106  * Set up a signal frame.
107  */
108
109 int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
110 {
111         return  __copy_to_user(sc, regs, sizeof(*regs));
112 }
113
114 /*
115  * Determine which stack to use..
116  */
117 static inline void __user *get_sigframe(struct k_sigaction *ka,
118                                         struct pt_regs *regs,
119                                         size_t frame_size)
120 {
121         unsigned long sp;
122
123         /* Default to using normal stack */
124         sp = regs->sp;
125
126         /*
127          * If we are on the alternate signal stack and would overflow
128          * it, don't.  Return an always-bogus address instead so we
129          * will die with SIGSEGV.
130          */
131         if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
132                 return (void __user __force *)-1UL;
133
134         /* This is the X/Open sanctioned signal stack switching.  */
135         if (ka->sa.sa_flags & SA_ONSTACK) {
136                 if (sas_ss_flags(sp) == 0)
137                         sp = current->sas_ss_sp + current->sas_ss_size;
138         }
139
140         sp -= frame_size;
141         /*
142          * Align the stack pointer according to the TILE ABI,
143          * i.e. so that on function entry (sp & 15) == 0.
144          */
145         sp &= -16UL;
146         return (void __user *) sp;
147 }
148
149 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
150                           struct pt_regs *regs)
151 {
152         unsigned long restorer;
153         struct rt_sigframe __user *frame;
154         int err = 0, sig = ksig->sig;
155
156         frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
157
158         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
159                 goto err;
160
161         /* Always write at least the signal number for the stack backtracer. */
162         if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
163                 /* At sigreturn time, restore the callee-save registers too. */
164                 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
165                 regs->flags |= PT_FLAGS_RESTORE_REGS;
166         } else {
167                 err |= __put_user(ksig->info.si_signo, &frame->info.si_signo);
168         }
169
170         /* Create the ucontext.  */
171         err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
172         err |= __put_user(0, &frame->uc.uc_flags);
173         err |= __put_user(NULL, &frame->uc.uc_link);
174         err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
175         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
176         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
177         if (err)
178                 goto err;
179
180         restorer = VDSO_SYM(&__vdso_rt_sigreturn);
181         if (ksig->ka.sa.sa_flags & SA_RESTORER)
182                 restorer = (unsigned long) ksig->ka.sa.sa_restorer;
183
184         /*
185          * Set up registers for signal handler.
186          * Registers that we don't modify keep the value they had from
187          * user-space at the time we took the signal.
188          * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
189          * since some things rely on this (e.g. glibc's debug/segfault.c).
190          */
191         regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
192         regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
193         regs->sp = (unsigned long) frame;
194         regs->lr = restorer;
195         regs->regs[0] = (unsigned long) sig;
196         regs->regs[1] = (unsigned long) &frame->info;
197         regs->regs[2] = (unsigned long) &frame->uc;
198         regs->flags |= PT_FLAGS_CALLER_SAVES;
199         return 0;
200
201 err:
202         trace_unhandled_signal("bad sigreturn frame", regs,
203                               (unsigned long)frame, SIGSEGV);
204         return -EFAULT;
205 }
206
207 /*
208  * OK, we're invoking a handler
209  */
210
211 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
212 {
213         sigset_t *oldset = sigmask_to_save();
214         int ret;
215
216         /* Are we from a system call? */
217         if (regs->faultnum == INT_SWINT_1) {
218                 /* If so, check system call restarting.. */
219                 switch (regs->regs[0]) {
220                 case -ERESTART_RESTARTBLOCK:
221                 case -ERESTARTNOHAND:
222                         regs->regs[0] = -EINTR;
223                         break;
224
225                 case -ERESTARTSYS:
226                         if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
227                                 regs->regs[0] = -EINTR;
228                                 break;
229                         }
230                         /* fallthrough */
231                 case -ERESTARTNOINTR:
232                         /* Reload caller-saves to restore r0..r5 and r10. */
233                         regs->flags |= PT_FLAGS_CALLER_SAVES;
234                         regs->regs[0] = regs->orig_r0;
235                         regs->pc -= 8;
236                 }
237         }
238
239         /* Set up the stack frame */
240 #ifdef CONFIG_COMPAT
241         if (is_compat_task())
242                 ret = compat_setup_rt_frame(ksig, oldset, regs);
243         else
244 #endif
245                 ret = setup_rt_frame(ksig, oldset, regs);
246
247         signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
248 }
249
250 /*
251  * Note that 'init' is a special process: it doesn't get signals it doesn't
252  * want to handle. Thus you cannot kill init even with a SIGKILL even by
253  * mistake.
254  */
255 void do_signal(struct pt_regs *regs)
256 {
257         struct ksignal ksig;
258
259         /*
260          * i386 will check if we're coming from kernel mode and bail out
261          * here.  In my experience this just turns weird crashes into
262          * weird spin-hangs.  But if we find a case where this seems
263          * helpful, we can reinstate the check on "!user_mode(regs)".
264          */
265
266         if (get_signal(&ksig)) {
267                 /* Whee! Actually deliver the signal.  */
268                 handle_signal(&ksig, regs);
269                 goto done;
270         }
271
272         /* Did we come from a system call? */
273         if (regs->faultnum == INT_SWINT_1) {
274                 /* Restart the system call - no handlers present */
275                 switch (regs->regs[0]) {
276                 case -ERESTARTNOHAND:
277                 case -ERESTARTSYS:
278                 case -ERESTARTNOINTR:
279                         regs->flags |= PT_FLAGS_CALLER_SAVES;
280                         regs->regs[0] = regs->orig_r0;
281                         regs->pc -= 8;
282                         break;
283
284                 case -ERESTART_RESTARTBLOCK:
285                         regs->flags |= PT_FLAGS_CALLER_SAVES;
286                         regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
287                         regs->pc -= 8;
288                         break;
289                 }
290         }
291
292         /* If there's no signal to deliver, just put the saved sigmask back. */
293         restore_saved_sigmask();
294
295 done:
296         /* Avoid double syscall restart if there are nested signals. */
297         regs->faultnum = INT_SWINT_1_SIGRETURN;
298 }
299
300 int show_unhandled_signals = 1;
301
302 static int __init crashinfo(char *str)
303 {
304         const char *word;
305
306         if (*str == '\0')
307                 show_unhandled_signals = 2;
308         else if (*str != '=' || kstrtoint(++str, 0, &show_unhandled_signals) != 0)
309                 return 0;
310
311         switch (show_unhandled_signals) {
312         case 0:
313                 word = "No";
314                 break;
315         case 1:
316                 word = "One-line";
317                 break;
318         default:
319                 word = "Detailed";
320                 break;
321         }
322         pr_info("%s crash reports will be generated on the console\n", word);
323         return 1;
324 }
325 __setup("crashinfo", crashinfo);
326
327 static void dump_mem(void __user *address)
328 {
329         void __user *addr;
330         enum { region_size = 256, bytes_per_line = 16 };
331         int i, j, k;
332         int found_readable_mem = 0;
333
334         if (!access_ok(VERIFY_READ, address, 1)) {
335                 pr_err("Not dumping at address 0x%lx (kernel address)\n",
336                        (unsigned long)address);
337                 return;
338         }
339
340         addr = (void __user *)
341                 (((unsigned long)address & -bytes_per_line) - region_size/2);
342         if (addr > address)
343                 addr = NULL;
344         for (i = 0; i < region_size;
345              addr += bytes_per_line, i += bytes_per_line) {
346                 unsigned char buf[bytes_per_line];
347                 char line[100];
348                 if (copy_from_user(buf, addr, bytes_per_line))
349                         continue;
350                 if (!found_readable_mem) {
351                         pr_err("Dumping memory around address 0x%lx:\n",
352                                (unsigned long)address);
353                         found_readable_mem = 1;
354                 }
355                 j = sprintf(line, REGFMT ":", (unsigned long)addr);
356                 for (k = 0; k < bytes_per_line; ++k)
357                         j += sprintf(&line[j], " %02x", buf[k]);
358                 pr_err("%s\n", line);
359         }
360         if (!found_readable_mem)
361                 pr_err("No readable memory around address 0x%lx\n",
362                        (unsigned long)address);
363 }
364
365 void trace_unhandled_signal(const char *type, struct pt_regs *regs,
366                             unsigned long address, int sig)
367 {
368         struct task_struct *tsk = current;
369
370         if (show_unhandled_signals == 0)
371                 return;
372
373         /* If the signal is handled, don't show it here. */
374         if (!is_global_init(tsk)) {
375                 void __user *handler =
376                         tsk->sighand->action[sig-1].sa.sa_handler;
377                 if (handler != SIG_IGN && handler != SIG_DFL)
378                         return;
379         }
380
381         /* Rate-limit the one-line output, not the detailed output. */
382         if (show_unhandled_signals <= 1 && !printk_ratelimit())
383                 return;
384
385         printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
386                task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
387                tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
388
389         print_vma_addr(KERN_CONT " in ", regs->pc);
390
391         printk(KERN_CONT "\n");
392
393         if (show_unhandled_signals > 1) {
394                 switch (sig) {
395                 case SIGILL:
396                 case SIGFPE:
397                 case SIGSEGV:
398                 case SIGBUS:
399                         pr_err("User crash: signal %d, trap %ld, address 0x%lx\n",
400                                sig, regs->faultnum, address);
401                         show_regs(regs);
402                         dump_mem((void __user *)address);
403                         break;
404                 default:
405                         pr_err("User crash: signal %d, trap %ld\n",
406                                sig, regs->faultnum);
407                         break;
408                 }
409         }
410 }