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>
17 #include <linux/sched/debug.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>
38 #include <arch/interrupts.h>
43 * Do a signal return; undo the signal stack.
46 int restore_sigcontext(struct pt_regs *regs,
47 struct sigcontext __user *sc)
51 /* Always make any pending restarted system calls return -EINTR */
52 current->restart_block.fn = do_no_restart_syscall;
55 * Enforce that sigcontext is like pt_regs, and doesn't mess
56 * up our stack alignment rules.
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));
62 /* Ensure that the PL is always set to USER_PL. */
63 regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
65 regs->faultnum = INT_SWINT_1_SIGRETURN;
70 void signal_fault(const char *type, struct pt_regs *regs,
71 void __user *frame, int sig)
73 trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV);
74 force_sigsegv(sig, current);
77 /* The assembly shim for this function arranges to ignore the return value. */
78 SYSCALL_DEFINE0(rt_sigreturn)
80 struct pt_regs *regs = current_pt_regs();
81 struct rt_sigframe __user *frame =
82 (struct rt_sigframe __user *)(regs->sp);
85 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
87 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
90 set_current_blocked(&set);
92 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
95 if (restore_altstack(&frame->uc.uc_stack))
101 signal_fault("bad sigreturn frame", regs, frame, 0);
106 * Set up a signal frame.
109 int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
111 return __copy_to_user(sc, regs, sizeof(*regs));
115 * Determine which stack to use..
117 static inline void __user *get_sigframe(struct k_sigaction *ka,
118 struct pt_regs *regs,
123 /* Default to using normal stack */
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.
131 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
132 return (void __user __force *)-1UL;
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;
142 * Align the stack pointer according to the TILE ABI,
143 * i.e. so that on function entry (sp & 15) == 0.
146 return (void __user *) sp;
149 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
150 struct pt_regs *regs)
152 unsigned long restorer;
153 struct rt_sigframe __user *frame;
154 int err = 0, sig = ksig->sig;
156 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
158 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
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;
167 err |= __put_user(ksig->info.si_signo, &frame->info.si_signo);
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));
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;
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).
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;
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;
202 trace_unhandled_signal("bad sigreturn frame", regs,
203 (unsigned long)frame, SIGSEGV);
208 * OK, we're invoking a handler
211 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
213 sigset_t *oldset = sigmask_to_save();
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;
226 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
227 regs->regs[0] = -EINTR;
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;
239 /* Set up the stack frame */
241 if (is_compat_task())
242 ret = compat_setup_rt_frame(ksig, oldset, regs);
245 ret = setup_rt_frame(ksig, oldset, regs);
247 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
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
255 void do_signal(struct pt_regs *regs)
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)".
266 if (get_signal(&ksig)) {
267 /* Whee! Actually deliver the signal. */
268 handle_signal(&ksig, regs);
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:
278 case -ERESTARTNOINTR:
279 regs->flags |= PT_FLAGS_CALLER_SAVES;
280 regs->regs[0] = regs->orig_r0;
284 case -ERESTART_RESTARTBLOCK:
285 regs->flags |= PT_FLAGS_CALLER_SAVES;
286 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
292 /* If there's no signal to deliver, just put the saved sigmask back. */
293 restore_saved_sigmask();
296 /* Avoid double syscall restart if there are nested signals. */
297 regs->faultnum = INT_SWINT_1_SIGRETURN;
300 int show_unhandled_signals = 1;
302 static int __init crashinfo(char *str)
307 show_unhandled_signals = 2;
308 else if (*str != '=' || kstrtoint(++str, 0, &show_unhandled_signals) != 0)
311 switch (show_unhandled_signals) {
322 pr_info("%s crash reports will be generated on the console\n", word);
325 __setup("crashinfo", crashinfo);
327 static void dump_mem(void __user *address)
330 enum { region_size = 256, bytes_per_line = 16 };
332 int found_readable_mem = 0;
334 if (!access_ok(VERIFY_READ, address, 1)) {
335 pr_err("Not dumping at address 0x%lx (kernel address)\n",
336 (unsigned long)address);
340 addr = (void __user *)
341 (((unsigned long)address & -bytes_per_line) - region_size/2);
344 for (i = 0; i < region_size;
345 addr += bytes_per_line, i += bytes_per_line) {
346 unsigned char buf[bytes_per_line];
348 if (copy_from_user(buf, addr, bytes_per_line))
350 if (!found_readable_mem) {
351 pr_err("Dumping memory around address 0x%lx:\n",
352 (unsigned long)address);
353 found_readable_mem = 1;
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);
360 if (!found_readable_mem)
361 pr_err("No readable memory around address 0x%lx\n",
362 (unsigned long)address);
365 void trace_unhandled_signal(const char *type, struct pt_regs *regs,
366 unsigned long address, int sig)
368 struct task_struct *tsk = current;
370 if (show_unhandled_signals == 0)
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)
381 /* Rate-limit the one-line output, not the detailed output. */
382 if (show_unhandled_signals <= 1 && !printk_ratelimit())
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);
389 print_vma_addr(KERN_CONT " in ", regs->pc);
391 printk(KERN_CONT "\n");
393 if (show_unhandled_signals > 1) {
399 pr_err("User crash: signal %d, trap %ld, address 0x%lx\n",
400 sig, regs->faultnum, address);
402 dump_mem((void __user *)address);
405 pr_err("User crash: signal %d, trap %ld\n",
406 sig, regs->faultnum);