1
2
3
4
5
6
7
8
9
10
11
12
13
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
40#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
42SYSCALL_DEFINE3(sigaltstack, const stack_t __user *, uss,
43 stack_t __user *, uoss, struct pt_regs *, regs)
44{
45 return do_sigaltstack(uss, uoss, regs->sp);
46}
47
48
49
50
51
52
53int restore_sigcontext(struct pt_regs *regs,
54 struct sigcontext __user *sc)
55{
56 int err = 0;
57 int i;
58
59
60 current_thread_info()->restart_block.fn = do_no_restart_syscall;
61
62
63
64
65
66 BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
67 BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
68
69 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
70 err |= __get_user(regs->regs[i], &sc->gregs[i]);
71
72
73 regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
74
75 regs->faultnum = INT_SWINT_1_SIGRETURN;
76
77 return err;
78}
79
80void signal_fault(const char *type, struct pt_regs *regs,
81 void __user *frame, int sig)
82{
83 trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV);
84 force_sigsegv(sig, current);
85}
86
87
88SYSCALL_DEFINE1(rt_sigreturn, struct pt_regs *, regs)
89{
90 struct rt_sigframe __user *frame =
91 (struct rt_sigframe __user *)(regs->sp);
92 sigset_t set;
93
94 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
95 goto badframe;
96 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
97 goto badframe;
98
99 sigdelsetmask(&set, ~_BLOCKABLE);
100 set_current_blocked(&set);
101
102 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
103 goto badframe;
104
105 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
106 goto badframe;
107
108 return 0;
109
110badframe:
111 signal_fault("bad sigreturn frame", regs, frame, 0);
112 return 0;
113}
114
115
116
117
118
119int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
120{
121 int i, err = 0;
122
123 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
124 err |= __put_user(regs->regs[i], &sc->gregs[i]);
125
126 return err;
127}
128
129
130
131
132static inline void __user *get_sigframe(struct k_sigaction *ka,
133 struct pt_regs *regs,
134 size_t frame_size)
135{
136 unsigned long sp;
137
138
139 sp = regs->sp;
140
141
142
143
144
145
146 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
147 return (void __user __force *)-1UL;
148
149
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;
153 }
154
155 sp -= frame_size;
156
157
158
159
160 sp &= -16UL;
161 return (void __user *) sp;
162}
163
164static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
165 sigset_t *set, struct pt_regs *regs)
166{
167 unsigned long restorer;
168 struct rt_sigframe __user *frame;
169 int err = 0;
170 int usig;
171
172 frame = get_sigframe(ka, regs, sizeof(*frame));
173
174 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
175 goto give_sigsegv;
176
177 usig = current_thread_info()->exec_domain
178 && current_thread_info()->exec_domain->signal_invmap
179 && sig < 32
180 ? current_thread_info()->exec_domain->signal_invmap[sig]
181 : sig;
182
183
184 if (ka->sa.sa_flags & SA_SIGINFO) {
185
186 err |= copy_siginfo_to_user(&frame->info, info);
187 regs->flags |= PT_FLAGS_RESTORE_REGS;
188 } else {
189 err |= __put_user(info->si_signo, &frame->info.si_signo);
190 }
191
192
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));
203 if (err)
204 goto give_sigsegv;
205
206 restorer = VDSO_BASE;
207 if (ka->sa.sa_flags & SA_RESTORER)
208 restorer = (unsigned long) ka->sa.sa_restorer;
209
210
211
212
213
214
215
216
217 regs->pc = (unsigned long) ka->sa.sa_handler;
218 regs->ex1 = PL_ICS_EX1(USER_PL, 1);
219 regs->sp = (unsigned long) frame;
220 regs->lr = restorer;
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;
225
226
227
228
229
230
231 if (test_thread_flag(TIF_SINGLESTEP))
232 ptrace_notify(SIGTRAP);
233
234 return 0;
235
236give_sigsegv:
237 signal_fault("bad setup frame", regs, frame, sig);
238 return -EFAULT;
239}
240
241
242
243
244
245static int handle_signal(unsigned long sig, siginfo_t *info,
246 struct k_sigaction *ka, sigset_t *oldset,
247 struct pt_regs *regs)
248{
249 int ret;
250
251
252 if (regs->faultnum == INT_SWINT_1) {
253
254 switch (regs->regs[0]) {
255 case -ERESTART_RESTARTBLOCK:
256 case -ERESTARTNOHAND:
257 regs->regs[0] = -EINTR;
258 break;
259
260 case -ERESTARTSYS:
261 if (!(ka->sa.sa_flags & SA_RESTART)) {
262 regs->regs[0] = -EINTR;
263 break;
264 }
265
266 case -ERESTARTNOINTR:
267
268 regs->flags |= PT_FLAGS_CALLER_SAVES;
269 regs->regs[0] = regs->orig_r0;
270 regs->pc -= 8;
271 }
272 }
273
274
275#ifdef CONFIG_COMPAT
276 if (is_compat_task())
277 ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
278 else
279#endif
280 ret = setup_rt_frame(sig, ka, info, oldset, regs);
281 if (ret == 0) {
282
283
284
285
286 block_sigmask(ka, sig);
287 }
288
289 return ret;
290}
291
292
293
294
295
296
297void do_signal(struct pt_regs *regs)
298{
299 siginfo_t info;
300 int signr;
301 struct k_sigaction ka;
302 sigset_t *oldset;
303
304
305
306
307
308
309
310
311 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
312 oldset = ¤t->saved_sigmask;
313 else
314 oldset = ¤t->blocked;
315
316 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
317 if (signr > 0) {
318
319 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
320
321
322
323
324
325
326 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
327 }
328
329 goto done;
330 }
331
332
333 if (regs->faultnum == INT_SWINT_1) {
334
335 switch (regs->regs[0]) {
336 case -ERESTARTNOHAND:
337 case -ERESTARTSYS:
338 case -ERESTARTNOINTR:
339 regs->flags |= PT_FLAGS_CALLER_SAVES;
340 regs->regs[0] = regs->orig_r0;
341 regs->pc -= 8;
342 break;
343
344 case -ERESTART_RESTARTBLOCK:
345 regs->flags |= PT_FLAGS_CALLER_SAVES;
346 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
347 regs->pc -= 8;
348 break;
349 }
350 }
351
352
353 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
354 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
355 sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL);
356 }
357
358done:
359
360 regs->faultnum = INT_SWINT_1_SIGRETURN;
361}
362
363int show_unhandled_signals = 1;
364
365static int __init crashinfo(char *str)
366{
367 unsigned long val;
368 const char *word;
369
370 if (*str == '\0')
371 val = 2;
372 else if (*str != '=' || strict_strtoul(++str, 0, &val) != 0)
373 return 0;
374 show_unhandled_signals = val;
375 switch (show_unhandled_signals) {
376 case 0:
377 word = "No";
378 break;
379 case 1:
380 word = "One-line";
381 break;
382 default:
383 word = "Detailed";
384 break;
385 }
386 pr_info("%s crash reports will be generated on the console\n", word);
387 return 1;
388}
389__setup("crashinfo", crashinfo);
390
391static void dump_mem(void __user *address)
392{
393 void __user *addr;
394 enum { region_size = 256, bytes_per_line = 16 };
395 int i, j, k;
396 int found_readable_mem = 0;
397
398 pr_err("\n");
399 if (!access_ok(VERIFY_READ, address, 1)) {
400 pr_err("Not dumping at address 0x%lx (kernel address)\n",
401 (unsigned long)address);
402 return;
403 }
404
405 addr = (void __user *)
406 (((unsigned long)address & -bytes_per_line) - region_size/2);
407 if (addr > address)
408 addr = NULL;
409 for (i = 0; i < region_size;
410 addr += bytes_per_line, i += bytes_per_line) {
411 unsigned char buf[bytes_per_line];
412 char line[100];
413 if (copy_from_user(buf, addr, bytes_per_line))
414 continue;
415 if (!found_readable_mem) {
416 pr_err("Dumping memory around address 0x%lx:\n",
417 (unsigned long)address);
418 found_readable_mem = 1;
419 }
420 j = sprintf(line, REGFMT":", (unsigned long)addr);
421 for (k = 0; k < bytes_per_line; ++k)
422 j += sprintf(&line[j], " %02x", buf[k]);
423 pr_err("%s\n", line);
424 }
425 if (!found_readable_mem)
426 pr_err("No readable memory around address 0x%lx\n",
427 (unsigned long)address);
428}
429
430void trace_unhandled_signal(const char *type, struct pt_regs *regs,
431 unsigned long address, int sig)
432{
433 struct task_struct *tsk = current;
434
435 if (show_unhandled_signals == 0)
436 return;
437
438
439 if (!is_global_init(tsk)) {
440 void __user *handler =
441 tsk->sighand->action[sig-1].sa.sa_handler;
442 if (handler != SIG_IGN && handler != SIG_DFL)
443 return;
444 }
445
446
447 if (show_unhandled_signals <= 1 && !printk_ratelimit())
448 return;
449
450 printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
451 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
452 tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
453
454 print_vma_addr(KERN_CONT " in ", regs->pc);
455
456 printk(KERN_CONT "\n");
457
458 if (show_unhandled_signals > 1) {
459 switch (sig) {
460 case SIGILL:
461 case SIGFPE:
462 case SIGSEGV:
463 case SIGBUS:
464 pr_err("User crash: signal %d,"
465 " trap %ld, address 0x%lx\n",
466 sig, regs->faultnum, address);
467 show_regs(regs);
468 dump_mem((void __user *)address);
469 break;
470 default:
471 pr_err("User crash: signal %d, trap %ld\n",
472 sig, regs->faultnum);
473 break;
474 }
475 }
476}
477