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