blob: 9b71bfd4913d28d090c99c6fd755f6f3db23998c [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
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>
Chris Metcalf867e3592010-05-28 23:09:12 -040019#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>
Chris Metcalf0707ad32010-06-25 17:04:17 -040035#include <asm/syscalls.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040036#include <arch/interrupts.h>
37
38#define DEBUG_SIG 0
39
40#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
Chris Metcalfd929b6a2010-10-14 14:34:33 -040042SYSCALL_DEFINE3(sigaltstack, const stack_t __user *, uss,
43 stack_t __user *, uoss, struct pt_regs *, regs)
Chris Metcalf867e3592010-05-28 23:09:12 -040044{
45 return do_sigaltstack(uss, uoss, regs->sp);
46}
47
48
49/*
50 * Do a signal return; undo the signal stack.
51 */
52
53int restore_sigcontext(struct pt_regs *regs,
Chris Metcalf81711ce2010-12-14 16:07:25 -050054 struct sigcontext __user *sc)
Chris Metcalf867e3592010-05-28 23:09:12 -040055{
56 int err = 0;
57 int i;
58
59 /* Always make any pending restarted system calls return -EINTR */
60 current_thread_info()->restart_block.fn = do_no_restart_syscall;
61
Chris Metcalf74fca9d2010-09-15 11:16:08 -040062 /*
63 * Enforce that sigcontext is like pt_regs, and doesn't mess
64 * up our stack alignment rules.
65 */
66 BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
67 BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
68
Chris Metcalf867e3592010-05-28 23:09:12 -040069 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
Chris Metcalf74fca9d2010-09-15 11:16:08 -040070 err |= __get_user(regs->regs[i], &sc->gregs[i]);
Chris Metcalf867e3592010-05-28 23:09:12 -040071
Chris Metcalf1deb9c52010-10-28 15:47:06 -040072 /* Ensure that the PL is always set to USER_PL. */
73 regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
74
Chris Metcalf867e3592010-05-28 23:09:12 -040075 regs->faultnum = INT_SWINT_1_SIGRETURN;
76
Chris Metcalf867e3592010-05-28 23:09:12 -040077 return err;
78}
79
Chris Metcalf571d76a2011-05-16 14:23:44 -040080void signal_fault(const char *type, struct pt_regs *regs,
81 void __user *frame, int sig)
82{
83 trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV);
84 force_sigsegv(sig, current);
85}
86
Chris Metcalf81711ce2010-12-14 16:07:25 -050087/* The assembly shim for this function arranges to ignore the return value. */
Chris Metcalfd929b6a2010-10-14 14:34:33 -040088SYSCALL_DEFINE1(rt_sigreturn, struct pt_regs *, regs)
Chris Metcalf867e3592010-05-28 23:09:12 -040089{
90 struct rt_sigframe __user *frame =
91 (struct rt_sigframe __user *)(regs->sp);
92 sigset_t set;
Chris Metcalf867e3592010-05-28 23:09:12 -040093
94 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
95 goto badframe;
96 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
97 goto badframe;
98
99 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingad092332012-02-14 11:41:06 +0000100 set_current_blocked(&set);
Chris Metcalf867e3592010-05-28 23:09:12 -0400101
Chris Metcalf81711ce2010-12-14 16:07:25 -0500102 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
Chris Metcalf867e3592010-05-28 23:09:12 -0400103 goto badframe;
104
105 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
106 goto badframe;
107
Chris Metcalf81711ce2010-12-14 16:07:25 -0500108 return 0;
Chris Metcalf867e3592010-05-28 23:09:12 -0400109
110badframe:
Chris Metcalf571d76a2011-05-16 14:23:44 -0400111 signal_fault("bad sigreturn frame", regs, frame, 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400112 return 0;
113}
114
115/*
116 * Set up a signal frame.
117 */
118
119int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
120{
121 int i, err = 0;
122
123 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400124 err |= __put_user(regs->regs[i], &sc->gregs[i]);
Chris Metcalf867e3592010-05-28 23:09:12 -0400125
126 return err;
127}
128
129/*
130 * Determine which stack to use..
131 */
132static inline void __user *get_sigframe(struct k_sigaction *ka,
133 struct pt_regs *regs,
134 size_t frame_size)
135{
136 unsigned long sp;
137
138 /* Default to using normal stack */
139 sp = regs->sp;
140
141 /*
142 * If we are on the alternate signal stack and would overflow
143 * it, don't. Return an always-bogus address instead so we
144 * will die with SIGSEGV.
145 */
146 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Chris Metcalf0707ad32010-06-25 17:04:17 -0400147 return (void __user __force *)-1UL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400148
149 /* This is the X/Open sanctioned signal stack switching. */
150 if (ka->sa.sa_flags & SA_ONSTACK) {
151 if (sas_ss_flags(sp) == 0)
152 sp = current->sas_ss_sp + current->sas_ss_size;
153 }
154
155 sp -= frame_size;
156 /*
157 * Align the stack pointer according to the TILE ABI,
158 * i.e. so that on function entry (sp & 15) == 0.
159 */
160 sp &= -16UL;
161 return (void __user *) sp;
162}
163
164static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
165 sigset_t *set, struct pt_regs *regs)
166{
167 unsigned long restorer;
168 struct rt_sigframe __user *frame;
169 int err = 0;
170 int usig;
171
172 frame = get_sigframe(ka, regs, sizeof(*frame));
173
174 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
175 goto give_sigsegv;
176
177 usig = current_thread_info()->exec_domain
178 && current_thread_info()->exec_domain->signal_invmap
179 && sig < 32
180 ? current_thread_info()->exec_domain->signal_invmap[sig]
181 : sig;
182
183 /* Always write at least the signal number for the stack backtracer. */
184 if (ka->sa.sa_flags & SA_SIGINFO) {
185 /* At sigreturn time, restore the callee-save registers too. */
186 err |= copy_siginfo_to_user(&frame->info, info);
187 regs->flags |= PT_FLAGS_RESTORE_REGS;
188 } else {
189 err |= __put_user(info->si_signo, &frame->info.si_signo);
190 }
191
192 /* Create the ucontext. */
193 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
194 err |= __put_user(0, &frame->uc.uc_flags);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400195 err |= __put_user(NULL, &frame->uc.uc_link);
196 err |= __put_user((void __user *)(current->sas_ss_sp),
Chris Metcalf867e3592010-05-28 23:09:12 -0400197 &frame->uc.uc_stack.ss_sp);
198 err |= __put_user(sas_ss_flags(regs->sp),
199 &frame->uc.uc_stack.ss_flags);
200 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
201 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
202 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
203 if (err)
204 goto give_sigsegv;
205
206 restorer = VDSO_BASE;
207 if (ka->sa.sa_flags & SA_RESTORER)
208 restorer = (unsigned long) ka->sa.sa_restorer;
209
210 /*
211 * Set up registers for signal handler.
212 * Registers that we don't modify keep the value they had from
213 * user-space at the time we took the signal.
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400214 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
215 * since some things rely on this (e.g. glibc's debug/segfault.c).
Chris Metcalf867e3592010-05-28 23:09:12 -0400216 */
217 regs->pc = (unsigned long) ka->sa.sa_handler;
218 regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
219 regs->sp = (unsigned long) frame;
220 regs->lr = restorer;
221 regs->regs[0] = (unsigned long) usig;
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400222 regs->regs[1] = (unsigned long) &frame->info;
223 regs->regs[2] = (unsigned long) &frame->uc;
224 regs->flags |= PT_FLAGS_CALLER_SAVES;
Chris Metcalf867e3592010-05-28 23:09:12 -0400225
226 /*
227 * Notify any tracer that was single-stepping it.
228 * The tracer may want to single-step inside the
229 * handler too.
230 */
231 if (test_thread_flag(TIF_SINGLESTEP))
232 ptrace_notify(SIGTRAP);
233
234 return 0;
235
236give_sigsegv:
Chris Metcalf571d76a2011-05-16 14:23:44 -0400237 signal_fault("bad setup frame", regs, frame, sig);
Chris Metcalf867e3592010-05-28 23:09:12 -0400238 return -EFAULT;
239}
240
241/*
242 * OK, we're invoking a handler
243 */
244
Al Viroa610d6e2012-05-21 23:42:15 -0400245static void handle_signal(unsigned long sig, siginfo_t *info,
Al Virob7f9a112012-05-02 09:59:21 -0400246 struct k_sigaction *ka,
Chris Metcalf867e3592010-05-28 23:09:12 -0400247 struct pt_regs *regs)
248{
Al Virob7f9a112012-05-02 09:59:21 -0400249 sigset_t *oldset = sigmask_to_save();
Chris Metcalf867e3592010-05-28 23:09:12 -0400250 int ret;
251
Chris Metcalf867e3592010-05-28 23:09:12 -0400252 /* Are we from a system call? */
253 if (regs->faultnum == INT_SWINT_1) {
254 /* If so, check system call restarting.. */
255 switch (regs->regs[0]) {
256 case -ERESTART_RESTARTBLOCK:
257 case -ERESTARTNOHAND:
258 regs->regs[0] = -EINTR;
259 break;
260
261 case -ERESTARTSYS:
262 if (!(ka->sa.sa_flags & SA_RESTART)) {
263 regs->regs[0] = -EINTR;
264 break;
265 }
266 /* fallthrough */
267 case -ERESTARTNOINTR:
268 /* Reload caller-saves to restore r0..r5 and r10. */
269 regs->flags |= PT_FLAGS_CALLER_SAVES;
270 regs->regs[0] = regs->orig_r0;
271 regs->pc -= 8;
272 }
273 }
274
275 /* Set up the stack frame */
276#ifdef CONFIG_COMPAT
277 if (is_compat_task())
278 ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
279 else
280#endif
281 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Al Viroa610d6e2012-05-21 23:42:15 -0400282 if (ret)
283 return;
284 block_sigmask(ka, sig);
Chris Metcalf867e3592010-05-28 23:09:12 -0400285}
286
287/*
288 * Note that 'init' is a special process: it doesn't get signals it doesn't
289 * want to handle. Thus you cannot kill init even with a SIGKILL even by
290 * mistake.
291 */
292void do_signal(struct pt_regs *regs)
293{
294 siginfo_t info;
295 int signr;
296 struct k_sigaction ka;
Chris Metcalf867e3592010-05-28 23:09:12 -0400297
298 /*
299 * i386 will check if we're coming from kernel mode and bail out
300 * here. In my experience this just turns weird crashes into
301 * weird spin-hangs. But if we find a case where this seems
302 * helpful, we can reinstate the check on "!user_mode(regs)".
303 */
304
Chris Metcalf867e3592010-05-28 23:09:12 -0400305 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
306 if (signr > 0) {
307 /* Whee! Actually deliver the signal. */
Al Viroa610d6e2012-05-21 23:42:15 -0400308 handle_signal(signr, &info, &ka, regs);
Chris Metcalf34a89d22010-10-28 15:03:30 -0400309 goto done;
Chris Metcalf867e3592010-05-28 23:09:12 -0400310 }
311
312 /* Did we come from a system call? */
313 if (regs->faultnum == INT_SWINT_1) {
314 /* Restart the system call - no handlers present */
315 switch (regs->regs[0]) {
316 case -ERESTARTNOHAND:
317 case -ERESTARTSYS:
318 case -ERESTARTNOINTR:
319 regs->flags |= PT_FLAGS_CALLER_SAVES;
320 regs->regs[0] = regs->orig_r0;
321 regs->pc -= 8;
322 break;
323
324 case -ERESTART_RESTARTBLOCK:
325 regs->flags |= PT_FLAGS_CALLER_SAVES;
326 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
327 regs->pc -= 8;
328 break;
329 }
330 }
331
332 /* If there's no signal to deliver, just put the saved sigmask back. */
Al Viro51a7b442012-05-21 23:33:55 -0400333 restore_saved_sigmask();
Chris Metcalf34a89d22010-10-28 15:03:30 -0400334
335done:
336 /* Avoid double syscall restart if there are nested signals. */
337 regs->faultnum = INT_SWINT_1_SIGRETURN;
Chris Metcalf867e3592010-05-28 23:09:12 -0400338}
Chris Metcalf571d76a2011-05-16 14:23:44 -0400339
340int show_unhandled_signals = 1;
341
342static int __init crashinfo(char *str)
343{
344 unsigned long val;
345 const char *word;
346
347 if (*str == '\0')
348 val = 2;
349 else if (*str != '=' || strict_strtoul(++str, 0, &val) != 0)
350 return 0;
351 show_unhandled_signals = val;
352 switch (show_unhandled_signals) {
353 case 0:
354 word = "No";
355 break;
356 case 1:
357 word = "One-line";
358 break;
359 default:
360 word = "Detailed";
361 break;
362 }
363 pr_info("%s crash reports will be generated on the console\n", word);
364 return 1;
365}
366__setup("crashinfo", crashinfo);
367
368static void dump_mem(void __user *address)
369{
370 void __user *addr;
371 enum { region_size = 256, bytes_per_line = 16 };
372 int i, j, k;
373 int found_readable_mem = 0;
374
375 pr_err("\n");
376 if (!access_ok(VERIFY_READ, address, 1)) {
377 pr_err("Not dumping at address 0x%lx (kernel address)\n",
378 (unsigned long)address);
379 return;
380 }
381
382 addr = (void __user *)
383 (((unsigned long)address & -bytes_per_line) - region_size/2);
384 if (addr > address)
385 addr = NULL;
386 for (i = 0; i < region_size;
387 addr += bytes_per_line, i += bytes_per_line) {
388 unsigned char buf[bytes_per_line];
389 char line[100];
390 if (copy_from_user(buf, addr, bytes_per_line))
391 continue;
392 if (!found_readable_mem) {
393 pr_err("Dumping memory around address 0x%lx:\n",
394 (unsigned long)address);
395 found_readable_mem = 1;
396 }
397 j = sprintf(line, REGFMT":", (unsigned long)addr);
398 for (k = 0; k < bytes_per_line; ++k)
399 j += sprintf(&line[j], " %02x", buf[k]);
400 pr_err("%s\n", line);
401 }
402 if (!found_readable_mem)
403 pr_err("No readable memory around address 0x%lx\n",
404 (unsigned long)address);
405}
406
407void trace_unhandled_signal(const char *type, struct pt_regs *regs,
408 unsigned long address, int sig)
409{
410 struct task_struct *tsk = current;
411
412 if (show_unhandled_signals == 0)
413 return;
414
415 /* If the signal is handled, don't show it here. */
416 if (!is_global_init(tsk)) {
417 void __user *handler =
418 tsk->sighand->action[sig-1].sa.sa_handler;
419 if (handler != SIG_IGN && handler != SIG_DFL)
420 return;
421 }
422
423 /* Rate-limit the one-line output, not the detailed output. */
424 if (show_unhandled_signals <= 1 && !printk_ratelimit())
425 return;
426
427 printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
428 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
429 tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
430
431 print_vma_addr(KERN_CONT " in ", regs->pc);
432
433 printk(KERN_CONT "\n");
434
435 if (show_unhandled_signals > 1) {
436 switch (sig) {
437 case SIGILL:
438 case SIGFPE:
439 case SIGSEGV:
440 case SIGBUS:
441 pr_err("User crash: signal %d,"
442 " trap %ld, address 0x%lx\n",
443 sig, regs->faultnum, address);
444 show_regs(regs);
445 dump_mem((void __user *)address);
446 break;
447 default:
448 pr_err("User crash: signal %d, trap %ld\n",
449 sig, regs->faultnum);
450 break;
451 }
452 }
453}