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>
37 #include <arch/interrupts.h>
42 * Do a signal return; undo the signal stack.
45 int restore_sigcontext(struct pt_regs
*regs
,
46 struct sigcontext __user
*sc
)
51 /* Always make any pending restarted system calls return -EINTR */
52 current_thread_info()->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);
61 for (i
= 0; i
< sizeof(struct pt_regs
)/sizeof(long); ++i
)
62 err
|= __get_user(regs
->regs
[i
], &sc
->gregs
[i
]);
64 /* Ensure that the PL is always set to USER_PL. */
65 regs
->ex1
= PL_ICS_EX1(USER_PL
, EX1_ICS(regs
->ex1
));
67 regs
->faultnum
= INT_SWINT_1_SIGRETURN
;
72 void signal_fault(const char *type
, struct pt_regs
*regs
,
73 void __user
*frame
, int sig
)
75 trace_unhandled_signal(type
, regs
, (unsigned long)frame
, SIGSEGV
);
76 force_sigsegv(sig
, current
);
79 /* The assembly shim for this function arranges to ignore the return value. */
80 SYSCALL_DEFINE0(rt_sigreturn
)
82 struct pt_regs
*regs
= current_pt_regs();
83 struct rt_sigframe __user
*frame
=
84 (struct rt_sigframe __user
*)(regs
->sp
);
87 if (!access_ok(VERIFY_READ
, frame
, sizeof(*frame
)))
89 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
92 set_current_blocked(&set
);
94 if (restore_sigcontext(regs
, &frame
->uc
.uc_mcontext
))
97 if (restore_altstack(&frame
->uc
.uc_stack
))
103 signal_fault("bad sigreturn frame", regs
, frame
, 0);
108 * Set up a signal frame.
111 int setup_sigcontext(struct sigcontext __user
*sc
, struct pt_regs
*regs
)
115 for (i
= 0; i
< sizeof(struct pt_regs
)/sizeof(long); ++i
)
116 err
|= __put_user(regs
->regs
[i
], &sc
->gregs
[i
]);
122 * Determine which stack to use..
124 static inline void __user
*get_sigframe(struct k_sigaction
*ka
,
125 struct pt_regs
*regs
,
130 /* Default to using normal stack */
134 * If we are on the alternate signal stack and would overflow
135 * it, don't. Return an always-bogus address instead so we
136 * will die with SIGSEGV.
138 if (on_sig_stack(sp
) && !likely(on_sig_stack(sp
- frame_size
)))
139 return (void __user __force
*)-1UL;
141 /* This is the X/Open sanctioned signal stack switching. */
142 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
143 if (sas_ss_flags(sp
) == 0)
144 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
149 * Align the stack pointer according to the TILE ABI,
150 * i.e. so that on function entry (sp & 15) == 0.
153 return (void __user
*) sp
;
156 static int setup_rt_frame(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
157 sigset_t
*set
, struct pt_regs
*regs
)
159 unsigned long restorer
;
160 struct rt_sigframe __user
*frame
;
164 frame
= get_sigframe(ka
, regs
, sizeof(*frame
));
166 if (!access_ok(VERIFY_WRITE
, frame
, sizeof(*frame
)))
169 usig
= current_thread_info()->exec_domain
170 && current_thread_info()->exec_domain
->signal_invmap
172 ? current_thread_info()->exec_domain
->signal_invmap
[sig
]
175 /* Always write at least the signal number for the stack backtracer. */
176 if (ka
->sa
.sa_flags
& SA_SIGINFO
) {
177 /* At sigreturn time, restore the callee-save registers too. */
178 err
|= copy_siginfo_to_user(&frame
->info
, info
);
179 regs
->flags
|= PT_FLAGS_RESTORE_REGS
;
181 err
|= __put_user(info
->si_signo
, &frame
->info
.si_signo
);
184 /* Create the ucontext. */
185 err
|= __clear_user(&frame
->save_area
, sizeof(frame
->save_area
));
186 err
|= __put_user(0, &frame
->uc
.uc_flags
);
187 err
|= __put_user(NULL
, &frame
->uc
.uc_link
);
188 err
|= __save_altstack(&frame
->uc
.uc_stack
, regs
->sp
);
189 err
|= setup_sigcontext(&frame
->uc
.uc_mcontext
, regs
);
190 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
194 restorer
= VDSO_SYM(&__vdso_rt_sigreturn
);
195 if (ka
->sa
.sa_flags
& SA_RESTORER
)
196 restorer
= (unsigned long) ka
->sa
.sa_restorer
;
199 * Set up registers for signal handler.
200 * Registers that we don't modify keep the value they had from
201 * user-space at the time we took the signal.
202 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
203 * since some things rely on this (e.g. glibc's debug/segfault.c).
205 regs
->pc
= (unsigned long) ka
->sa
.sa_handler
;
206 regs
->ex1
= PL_ICS_EX1(USER_PL
, 1); /* set crit sec in handler */
207 regs
->sp
= (unsigned long) frame
;
209 regs
->regs
[0] = (unsigned long) usig
;
210 regs
->regs
[1] = (unsigned long) &frame
->info
;
211 regs
->regs
[2] = (unsigned long) &frame
->uc
;
212 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
216 signal_fault("bad setup frame", regs
, frame
, sig
);
221 * OK, we're invoking a handler
224 static void handle_signal(unsigned long sig
, siginfo_t
*info
,
225 struct k_sigaction
*ka
,
226 struct pt_regs
*regs
)
228 sigset_t
*oldset
= sigmask_to_save();
231 /* Are we from a system call? */
232 if (regs
->faultnum
== INT_SWINT_1
) {
233 /* If so, check system call restarting.. */
234 switch (regs
->regs
[0]) {
235 case -ERESTART_RESTARTBLOCK
:
236 case -ERESTARTNOHAND
:
237 regs
->regs
[0] = -EINTR
;
241 if (!(ka
->sa
.sa_flags
& SA_RESTART
)) {
242 regs
->regs
[0] = -EINTR
;
246 case -ERESTARTNOINTR
:
247 /* Reload caller-saves to restore r0..r5 and r10. */
248 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
249 regs
->regs
[0] = regs
->orig_r0
;
254 /* Set up the stack frame */
256 if (is_compat_task())
257 ret
= compat_setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
260 ret
= setup_rt_frame(sig
, ka
, info
, oldset
, regs
);
263 signal_delivered(sig
, info
, ka
, regs
,
264 test_thread_flag(TIF_SINGLESTEP
));
268 * Note that 'init' is a special process: it doesn't get signals it doesn't
269 * want to handle. Thus you cannot kill init even with a SIGKILL even by
272 void do_signal(struct pt_regs
*regs
)
276 struct k_sigaction ka
;
279 * i386 will check if we're coming from kernel mode and bail out
280 * here. In my experience this just turns weird crashes into
281 * weird spin-hangs. But if we find a case where this seems
282 * helpful, we can reinstate the check on "!user_mode(regs)".
285 signr
= get_signal_to_deliver(&info
, &ka
, regs
, NULL
);
287 /* Whee! Actually deliver the signal. */
288 handle_signal(signr
, &info
, &ka
, regs
);
292 /* Did we come from a system call? */
293 if (regs
->faultnum
== INT_SWINT_1
) {
294 /* Restart the system call - no handlers present */
295 switch (regs
->regs
[0]) {
296 case -ERESTARTNOHAND
:
298 case -ERESTARTNOINTR
:
299 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
300 regs
->regs
[0] = regs
->orig_r0
;
304 case -ERESTART_RESTARTBLOCK
:
305 regs
->flags
|= PT_FLAGS_CALLER_SAVES
;
306 regs
->regs
[TREG_SYSCALL_NR
] = __NR_restart_syscall
;
312 /* If there's no signal to deliver, just put the saved sigmask back. */
313 restore_saved_sigmask();
316 /* Avoid double syscall restart if there are nested signals. */
317 regs
->faultnum
= INT_SWINT_1_SIGRETURN
;
320 int show_unhandled_signals
= 1;
322 static int __init
crashinfo(char *str
)
327 show_unhandled_signals
= 2;
328 else if (*str
!= '=' || kstrtoint(++str
, 0, &show_unhandled_signals
) != 0)
331 switch (show_unhandled_signals
) {
342 pr_info("%s crash reports will be generated on the console\n", word
);
345 __setup("crashinfo", crashinfo
);
347 static void dump_mem(void __user
*address
)
350 enum { region_size
= 256, bytes_per_line
= 16 };
352 int found_readable_mem
= 0;
355 if (!access_ok(VERIFY_READ
, address
, 1)) {
356 pr_err("Not dumping at address 0x%lx (kernel address)\n",
357 (unsigned long)address
);
361 addr
= (void __user
*)
362 (((unsigned long)address
& -bytes_per_line
) - region_size
/2);
365 for (i
= 0; i
< region_size
;
366 addr
+= bytes_per_line
, i
+= bytes_per_line
) {
367 unsigned char buf
[bytes_per_line
];
369 if (copy_from_user(buf
, addr
, bytes_per_line
))
371 if (!found_readable_mem
) {
372 pr_err("Dumping memory around address 0x%lx:\n",
373 (unsigned long)address
);
374 found_readable_mem
= 1;
376 j
= sprintf(line
, REGFMT
":", (unsigned long)addr
);
377 for (k
= 0; k
< bytes_per_line
; ++k
)
378 j
+= sprintf(&line
[j
], " %02x", buf
[k
]);
379 pr_err("%s\n", line
);
381 if (!found_readable_mem
)
382 pr_err("No readable memory around address 0x%lx\n",
383 (unsigned long)address
);
386 void trace_unhandled_signal(const char *type
, struct pt_regs
*regs
,
387 unsigned long address
, int sig
)
389 struct task_struct
*tsk
= current
;
391 if (show_unhandled_signals
== 0)
394 /* If the signal is handled, don't show it here. */
395 if (!is_global_init(tsk
)) {
396 void __user
*handler
=
397 tsk
->sighand
->action
[sig
-1].sa
.sa_handler
;
398 if (handler
!= SIG_IGN
&& handler
!= SIG_DFL
)
402 /* Rate-limit the one-line output, not the detailed output. */
403 if (show_unhandled_signals
<= 1 && !printk_ratelimit())
406 printk("%s%s[%d]: %s at %lx pc "REGFMT
" signal %d",
407 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
408 tsk
->comm
, task_pid_nr(tsk
), type
, address
, regs
->pc
, sig
);
410 print_vma_addr(KERN_CONT
" in ", regs
->pc
);
412 printk(KERN_CONT
"\n");
414 if (show_unhandled_signals
> 1) {
420 pr_err("User crash: signal %d,"
421 " trap %ld, address 0x%lx\n",
422 sig
, regs
->faultnum
, address
);
424 dump_mem((void __user
*)address
);
427 pr_err("User crash: signal %d, trap %ld\n",
428 sig
, regs
->faultnum
);