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>
18 #include <linux/sched/task_stack.h>
20 #include <linux/smp.h>
21 #include <linux/kernel.h>
22 #include <linux/signal.h>
23 #include <linux/errno.h>
24 #include <linux/wait.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27 #include <linux/personality.h>
28 #include <linux/suspend.h>
29 #include <linux/ptrace.h>
30 #include <linux/elf.h>
31 #include <linux/compat.h>
32 #include <linux/syscalls.h>
33 #include <linux/uaccess.h>
34 #include <asm/processor.h>
35 #include <asm/ucontext.h>
36 #include <asm/sigframe.h>
37 #include <asm/syscalls.h>
39 #include <arch/interrupts.h>
44 * Do a signal return; undo the signal stack.
47 int restore_sigcontext(struct pt_regs
*regs
,
48 struct sigcontext __user
*sc
)
52 /* Always make any pending restarted system calls return -EINTR */
53 current
->restart_block
.fn
= do_no_restart_syscall
;
56 * Enforce that sigcontext is like pt_regs, and doesn't mess
57 * up our stack alignment rules.
59 BUILD_BUG_ON(sizeof(struct sigcontext
) != sizeof(struct pt_regs
));
60 BUILD_BUG_ON(sizeof(struct sigcontext
) % 8 != 0);
61 err
= __copy_from_user(regs
, sc
, sizeof(*regs
));
63 /* Ensure that the PL is always set to USER_PL. */
64 regs
->ex1
= PL_ICS_EX1(USER_PL
, EX1_ICS(regs
->ex1
));
66 regs
->faultnum
= INT_SWINT_1_SIGRETURN
;
71 void signal_fault(const char *type
, struct pt_regs
*regs
,
72 void __user
*frame
, int sig
)
74 trace_unhandled_signal(type
, regs
, (unsigned long)frame
, SIGSEGV
);
75 force_sigsegv(sig
, current
);
78 /* The assembly shim for this function arranges to ignore the return value. */
79 SYSCALL_DEFINE0(rt_sigreturn
)
81 struct pt_regs
*regs
= current_pt_regs();
82 struct rt_sigframe __user
*frame
=
83 (struct rt_sigframe __user
*)(regs
->sp
);
86 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
88 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
91 set_current_blocked(&set
);
93 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
96 if (restore_altstack(&frame
->uc
.uc_stack
))
102 signal_fault("bad sigreturn frame", regs
, frame
, 0);
107 * Set up a signal frame.
110 int setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
)
112 return __copy_to_user(sc
, regs
, sizeof(*regs
));
116 * Determine which stack to use..
118 static inline void __user
*get_sigframe(struct k_sigaction
*ka
,
119 struct pt_regs
*regs
,
124 /* Default to using normal stack */
128 * If we are on the alternate signal stack and would overflow
129 * it, don't. Return an always-bogus address instead so we
130 * will die with SIGSEGV.
132 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- frame_size
)))
133 return (void __user __force
*)-1UL;
135 /* This is the X/Open sanctioned signal stack switching. */
136 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
137 if (sas_ss_flags(sp
) == 0)
138 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
143 * Align the stack pointer according to the TILE ABI,
144 * i.e. so that on function entry (sp & 15) == 0.
147 return (void __user
*) sp
;
150 static int setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
,
151 struct pt_regs
*regs
)
153 unsigned long restorer
;
154 struct rt_sigframe __user
*frame
;
155 int err
= 0, sig
= ksig
->sig
;
157 frame
= get_sigframe(&ksig
->ka
, regs
, sizeof(*frame
));
159 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
162 /* Always write at least the signal number for the stack backtracer. */
163 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
) {
164 /* At sigreturn time, restore the callee-save registers too. */
165 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
166 regs
->flags
|= PT_FLAGS_RESTORE_REGS
;
168 err
|= __put_user(ksig
->info
.si_signo
, &frame
->info
.si_signo
);
171 /* Create the ucontext. */
172 err
|= __clear_user(&frame
->save_area
, sizeof(frame
->save_area
));
173 err
|= __put_user(0, &frame
->uc
.uc_flags
);
174 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
175 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
176 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
);
177 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
181 restorer
= VDSO_SYM(&__vdso_rt_sigreturn
);
182 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
)
183 restorer
= (unsigned long) ksig
->ka
.sa
.sa_restorer
;
186 * Set up registers for signal handler.
187 * Registers that we don't modify keep the value they had from
188 * user-space at the time we took the signal.
189 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
190 * since some things rely on this (e.g. glibc's debug/segfault.c).
192 regs
->pc
= (unsigned long) ksig
->ka
.sa
.sa_handler
;
193 regs
->ex1
= PL_ICS_EX1(USER_PL
, 1); /* set crit sec in handler */
194 regs
->sp
= (unsigned long) frame
;
196 regs
->regs
[0] = (unsigned long) sig
;
197 regs
->regs
[1] = (unsigned long) &frame
->info
;
198 regs
->regs
[2] = (unsigned long) &frame
->uc
;
199 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
203 trace_unhandled_signal("bad sigreturn frame", regs
,
204 (unsigned long)frame
, SIGSEGV
);
209 * OK, we're invoking a handler
212 static void handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
214 sigset_t
*oldset
= sigmask_to_save();
217 /* Are we from a system call? */
218 if (regs
->faultnum
== INT_SWINT_1
) {
219 /* If so, check system call restarting.. */
220 switch (regs
->regs
[0]) {
221 case -ERESTART_RESTARTBLOCK
:
222 case -ERESTARTNOHAND
:
223 regs
->regs
[0] = -EINTR
;
227 if (!(ksig
->ka
.sa
.sa_flags
& SA_RESTART
)) {
228 regs
->regs
[0] = -EINTR
;
232 case -ERESTARTNOINTR
:
233 /* Reload caller-saves to restore r0..r5 and r10. */
234 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
235 regs
->regs
[0] = regs
->orig_r0
;
240 /* Set up the stack frame */
242 if (is_compat_task())
243 ret
= compat_setup_rt_frame(ksig
, oldset
, regs
);
246 ret
= setup_rt_frame(ksig
, oldset
, regs
);
248 signal_setup_done(ret
, ksig
, test_thread_flag(TIF_SINGLESTEP
));
252 * Note that 'init' is a special process: it doesn't get signals it doesn't
253 * want to handle. Thus you cannot kill init even with a SIGKILL even by
256 void do_signal(struct pt_regs
*regs
)
261 * i386 will check if we're coming from kernel mode and bail out
262 * here. In my experience this just turns weird crashes into
263 * weird spin-hangs. But if we find a case where this seems
264 * helpful, we can reinstate the check on "!user_mode(regs)".
267 if (get_signal(&ksig
)) {
268 /* Whee! Actually deliver the signal. */
269 handle_signal(&ksig
, regs
);
273 /* Did we come from a system call? */
274 if (regs
->faultnum
== INT_SWINT_1
) {
275 /* Restart the system call - no handlers present */
276 switch (regs
->regs
[0]) {
277 case -ERESTARTNOHAND
:
279 case -ERESTARTNOINTR
:
280 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
281 regs
->regs
[0] = regs
->orig_r0
;
285 case -ERESTART_RESTARTBLOCK
:
286 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
287 regs
->regs
[TREG_SYSCALL_NR
] = __NR_restart_syscall
;
293 /* If there's no signal to deliver, just put the saved sigmask back. */
294 restore_saved_sigmask();
297 /* Avoid double syscall restart if there are nested signals. */
298 regs
->faultnum
= INT_SWINT_1_SIGRETURN
;
301 int show_unhandled_signals
= 1;
303 static int __init
crashinfo(char *str
)
308 show_unhandled_signals
= 2;
309 else if (*str
!= '=' || kstrtoint(++str
, 0, &show_unhandled_signals
) != 0)
312 switch (show_unhandled_signals
) {
323 pr_info("%s crash reports will be generated on the console\n", word
);
326 __setup("crashinfo", crashinfo
);
328 static void dump_mem(void __user
*address
)
331 enum { region_size
= 256, bytes_per_line
= 16 };
333 int found_readable_mem
= 0;
335 if (!access_ok(VERIFY_READ
, address
, 1)) {
336 pr_err("Not dumping at address 0x%lx (kernel address)\n",
337 (unsigned long)address
);
341 addr
= (void __user
*)
342 (((unsigned long)address
& -bytes_per_line
) - region_size
/2);
345 for (i
= 0; i
< region_size
;
346 addr
+= bytes_per_line
, i
+= bytes_per_line
) {
347 unsigned char buf
[bytes_per_line
];
349 if (copy_from_user(buf
, addr
, bytes_per_line
))
351 if (!found_readable_mem
) {
352 pr_err("Dumping memory around address 0x%lx:\n",
353 (unsigned long)address
);
354 found_readable_mem
= 1;
356 j
= sprintf(line
, REGFMT
":", (unsigned long)addr
);
357 for (k
= 0; k
< bytes_per_line
; ++k
)
358 j
+= sprintf(&line
[j
], " %02x", buf
[k
]);
359 pr_err("%s\n", line
);
361 if (!found_readable_mem
)
362 pr_err("No readable memory around address 0x%lx\n",
363 (unsigned long)address
);
366 void trace_unhandled_signal(const char *type
, struct pt_regs
*regs
,
367 unsigned long address
, int sig
)
369 struct task_struct
*tsk
= current
;
371 if (show_unhandled_signals
== 0)
374 /* If the signal is handled, don't show it here. */
375 if (!is_global_init(tsk
)) {
376 void __user
*handler
=
377 tsk
->sighand
->action
[sig
-1].sa
.sa_handler
;
378 if (handler
!= SIG_IGN
&& handler
!= SIG_DFL
)
382 /* Rate-limit the one-line output, not the detailed output. */
383 if (show_unhandled_signals
<= 1 && !printk_ratelimit())
386 printk("%s%s[%d]: %s at %lx pc "REGFMT
" signal %d",
387 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
388 tsk
->comm
, task_pid_nr(tsk
), type
, address
, regs
->pc
, sig
);
390 print_vma_addr(KERN_CONT
" in ", regs
->pc
);
392 printk(KERN_CONT
"\n");
394 if (show_unhandled_signals
> 1) {
400 pr_err("User crash: signal %d, trap %ld, address 0x%lx\n",
401 sig
, regs
->faultnum
, address
);
403 dump_mem((void __user
*)address
);
406 pr_err("User crash: signal %d, trap %ld\n",
407 sig
, regs
->faultnum
);