2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
16 #include <linux/sched.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>
40 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
42 SYSCALL_DEFINE3(sigaltstack, const stack_t __user *, uss,
43 stack_t __user *, uoss, struct pt_regs *, regs)
45 return do_sigaltstack(uss, uoss, regs->sp);
50 * Do a signal return; undo the signal stack.
53 int restore_sigcontext(struct pt_regs *regs,
54 struct sigcontext __user *sc)
59 /* Always make any pending restarted system calls return -EINTR */
60 current_thread_info()->restart_block.fn = do_no_restart_syscall;
63 * Enforce that sigcontext is like pt_regs, and doesn't mess
64 * up our stack alignment rules.
66 BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
67 BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
69 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
70 err |= __get_user(regs->regs[i], &sc->gregs[i]);
72 /* Ensure that the PL is always set to USER_PL. */
73 regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
75 regs->faultnum = INT_SWINT_1_SIGRETURN;
80 void signal_fault(const char *type, struct pt_regs *regs,
81 void __user *frame, int sig)
83 trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV);
84 force_sigsegv(sig, current);
87 /* The assembly shim for this function arranges to ignore the return value. */
88 SYSCALL_DEFINE1(rt_sigreturn, struct pt_regs *, regs)
90 struct rt_sigframe __user *frame =
91 (struct rt_sigframe __user *)(regs->sp);
94 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
96 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
99 sigdelsetmask(&set, ~_BLOCKABLE);
100 set_current_blocked(&set);
102 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
105 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
111 signal_fault("bad sigreturn frame", regs, frame, 0);
116 * Set up a signal frame.
119 int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
123 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
124 err |= __put_user(regs->regs[i], &sc->gregs[i]);
130 * Determine which stack to use..
132 static inline void __user *get_sigframe(struct k_sigaction *ka,
133 struct pt_regs *regs,
138 /* Default to using normal stack */
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.
146 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
147 return (void __user __force *)-1UL;
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;
157 * Align the stack pointer according to the TILE ABI,
158 * i.e. so that on function entry (sp & 15) == 0.
161 return (void __user *) sp;
164 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
165 sigset_t *set, struct pt_regs *regs)
167 unsigned long restorer;
168 struct rt_sigframe __user *frame;
172 frame = get_sigframe(ka, regs, sizeof(*frame));
174 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
177 usig = current_thread_info()->exec_domain
178 && current_thread_info()->exec_domain->signal_invmap
180 ? current_thread_info()->exec_domain->signal_invmap[sig]
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;
189 err |= __put_user(info->si_signo, &frame->info.si_signo);
192 /* Create the ucontext. */
193 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
194 err |= __put_user(0, &frame->uc.uc_flags);
195 err |= __put_user(NULL, &frame->uc.uc_link);
196 err |= __put_user((void __user *)(current->sas_ss_sp),
197 &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));
206 restorer = VDSO_BASE;
207 if (ka->sa.sa_flags & SA_RESTORER)
208 restorer = (unsigned long) ka->sa.sa_restorer;
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.
214 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
215 * since some things rely on this (e.g. glibc's debug/segfault.c).
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;
221 regs->regs[0] = (unsigned long) usig;
222 regs->regs[1] = (unsigned long) &frame->info;
223 regs->regs[2] = (unsigned long) &frame->uc;
224 regs->flags |= PT_FLAGS_CALLER_SAVES;
227 * Notify any tracer that was single-stepping it.
228 * The tracer may want to single-step inside the
231 if (test_thread_flag(TIF_SINGLESTEP))
232 ptrace_notify(SIGTRAP);
237 signal_fault("bad setup frame", regs, frame, sig);
242 * OK, we're invoking a handler
245 static int handle_signal(unsigned long sig, siginfo_t *info,
246 struct k_sigaction *ka,
247 struct pt_regs *regs)
249 sigset_t *oldset = sigmask_to_save();
252 /* 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;
262 if (!(ka->sa.sa_flags & SA_RESTART)) {
263 regs->regs[0] = -EINTR;
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;
275 /* Set up the stack frame */
277 if (is_compat_task())
278 ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
281 ret = setup_rt_frame(sig, ka, info, oldset, regs);
283 /* This code is only called from system calls or from
284 * the work_pending path in the return-to-user code, and
285 * either way we can re-enable interrupts unconditionally.
287 block_sigmask(ka, sig);
294 * Note that 'init' is a special process: it doesn't get signals it doesn't
295 * want to handle. Thus you cannot kill init even with a SIGKILL even by
298 void do_signal(struct pt_regs *regs)
302 struct k_sigaction ka;
305 * i386 will check if we're coming from kernel mode and bail out
306 * here. In my experience this just turns weird crashes into
307 * weird spin-hangs. But if we find a case where this seems
308 * helpful, we can reinstate the check on "!user_mode(regs)".
311 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
313 /* Whee! Actually deliver the signal. */
314 if (handle_signal(signr, &info, &ka, regs) == 0) {
316 * A signal was successfully delivered; the saved
317 * sigmask will have been stored in the signal frame,
318 * and will be restored by sigreturn, so we can simply
319 * clear the TS_RESTORE_SIGMASK flag.
321 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
327 /* Did we come from a system call? */
328 if (regs->faultnum == INT_SWINT_1) {
329 /* Restart the system call - no handlers present */
330 switch (regs->regs[0]) {
331 case -ERESTARTNOHAND:
333 case -ERESTARTNOINTR:
334 regs->flags |= PT_FLAGS_CALLER_SAVES;
335 regs->regs[0] = regs->orig_r0;
339 case -ERESTART_RESTARTBLOCK:
340 regs->flags |= PT_FLAGS_CALLER_SAVES;
341 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
347 /* If there's no signal to deliver, just put the saved sigmask back. */
348 restore_saved_sigmask();
351 /* Avoid double syscall restart if there are nested signals. */
352 regs->faultnum = INT_SWINT_1_SIGRETURN;
355 int show_unhandled_signals = 1;
357 static int __init crashinfo(char *str)
364 else if (*str != '=' || strict_strtoul(++str, 0, &val) != 0)
366 show_unhandled_signals = val;
367 switch (show_unhandled_signals) {
378 pr_info("%s crash reports will be generated on the console\n", word);
381 __setup("crashinfo", crashinfo);
383 static void dump_mem(void __user *address)
386 enum { region_size = 256, bytes_per_line = 16 };
388 int found_readable_mem = 0;
391 if (!access_ok(VERIFY_READ, address, 1)) {
392 pr_err("Not dumping at address 0x%lx (kernel address)\n",
393 (unsigned long)address);
397 addr = (void __user *)
398 (((unsigned long)address & -bytes_per_line) - region_size/2);
401 for (i = 0; i < region_size;
402 addr += bytes_per_line, i += bytes_per_line) {
403 unsigned char buf[bytes_per_line];
405 if (copy_from_user(buf, addr, bytes_per_line))
407 if (!found_readable_mem) {
408 pr_err("Dumping memory around address 0x%lx:\n",
409 (unsigned long)address);
410 found_readable_mem = 1;
412 j = sprintf(line, REGFMT":", (unsigned long)addr);
413 for (k = 0; k < bytes_per_line; ++k)
414 j += sprintf(&line[j], " %02x", buf[k]);
415 pr_err("%s\n", line);
417 if (!found_readable_mem)
418 pr_err("No readable memory around address 0x%lx\n",
419 (unsigned long)address);
422 void trace_unhandled_signal(const char *type, struct pt_regs *regs,
423 unsigned long address, int sig)
425 struct task_struct *tsk = current;
427 if (show_unhandled_signals == 0)
430 /* If the signal is handled, don't show it here. */
431 if (!is_global_init(tsk)) {
432 void __user *handler =
433 tsk->sighand->action[sig-1].sa.sa_handler;
434 if (handler != SIG_IGN && handler != SIG_DFL)
438 /* Rate-limit the one-line output, not the detailed output. */
439 if (show_unhandled_signals <= 1 && !printk_ratelimit())
442 printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
443 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
444 tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
446 print_vma_addr(KERN_CONT " in ", regs->pc);
448 printk(KERN_CONT "\n");
450 if (show_unhandled_signals > 1) {
456 pr_err("User crash: signal %d,"
457 " trap %ld, address 0x%lx\n",
458 sig, regs->faultnum, address);
460 dump_mem((void __user *)address);
463 pr_err("User crash: signal %d, trap %ld\n",
464 sig, regs->faultnum);