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