Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
signal.c
Go to the documentation of this file.
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 
40 SYSCALL_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 
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 
74 
75  return err;
76 }
77 
78 void 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. */
86 SYSCALL_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 
107 badframe:
108  signal_fault("bad sigreturn frame", regs, frame, 0);
109  return 0;
110 }
111 
112 /*
113  * Set up a signal frame.
114  */
115 
116 int 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  */
129 static 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 
161 static 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  return 0;
223 
224 give_sigsegv:
225  signal_fault("bad setup frame", regs, frame, sig);
226  return -EFAULT;
227 }
228 
229 /*
230  * OK, we're invoking a handler
231  */
232 
233 static void handle_signal(unsigned long sig, siginfo_t *info,
234  struct k_sigaction *ka,
235  struct pt_regs *regs)
236 {
237  sigset_t *oldset = sigmask_to_save();
238  int ret;
239 
240  /* Are we from a system call? */
241  if (regs->faultnum == INT_SWINT_1) {
242  /* If so, check system call restarting.. */
243  switch (regs->regs[0]) {
244  case -ERESTART_RESTARTBLOCK:
245  case -ERESTARTNOHAND:
246  regs->regs[0] = -EINTR;
247  break;
248 
249  case -ERESTARTSYS:
250  if (!(ka->sa.sa_flags & SA_RESTART)) {
251  regs->regs[0] = -EINTR;
252  break;
253  }
254  /* fallthrough */
255  case -ERESTARTNOINTR:
256  /* Reload caller-saves to restore r0..r5 and r10. */
257  regs->flags |= PT_FLAGS_CALLER_SAVES;
258  regs->regs[0] = regs->orig_r0;
259  regs->pc -= 8;
260  }
261  }
262 
263  /* Set up the stack frame */
264 #ifdef CONFIG_COMPAT
265  if (is_compat_task())
266  ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
267  else
268 #endif
269  ret = setup_rt_frame(sig, ka, info, oldset, regs);
270  if (ret)
271  return;
272  signal_delivered(sig, info, ka, regs,
273  test_thread_flag(TIF_SINGLESTEP));
274 }
275 
276 /*
277  * Note that 'init' is a special process: it doesn't get signals it doesn't
278  * want to handle. Thus you cannot kill init even with a SIGKILL even by
279  * mistake.
280  */
281 void do_signal(struct pt_regs *regs)
282 {
283  siginfo_t info;
284  int signr;
285  struct k_sigaction ka;
286 
287  /*
288  * i386 will check if we're coming from kernel mode and bail out
289  * here. In my experience this just turns weird crashes into
290  * weird spin-hangs. But if we find a case where this seems
291  * helpful, we can reinstate the check on "!user_mode(regs)".
292  */
293 
294  signr = get_signal_to_deliver(&info, &ka, regs, NULL);
295  if (signr > 0) {
296  /* Whee! Actually deliver the signal. */
297  handle_signal(signr, &info, &ka, regs);
298  goto done;
299  }
300 
301  /* Did we come from a system call? */
302  if (regs->faultnum == INT_SWINT_1) {
303  /* Restart the system call - no handlers present */
304  switch (regs->regs[0]) {
305  case -ERESTARTNOHAND:
306  case -ERESTARTSYS:
307  case -ERESTARTNOINTR:
308  regs->flags |= PT_FLAGS_CALLER_SAVES;
309  regs->regs[0] = regs->orig_r0;
310  regs->pc -= 8;
311  break;
312 
313  case -ERESTART_RESTARTBLOCK:
314  regs->flags |= PT_FLAGS_CALLER_SAVES;
316  regs->pc -= 8;
317  break;
318  }
319  }
320 
321  /* If there's no signal to deliver, just put the saved sigmask back. */
322  restore_saved_sigmask();
323 
324 done:
325  /* Avoid double syscall restart if there are nested signals. */
327 }
328 
330 
331 static int __init crashinfo(char *str)
332 {
333  unsigned long val;
334  const char *word;
335 
336  if (*str == '\0')
337  val = 2;
338  else if (*str != '=' || strict_strtoul(++str, 0, &val) != 0)
339  return 0;
340  show_unhandled_signals = val;
341  switch (show_unhandled_signals) {
342  case 0:
343  word = "No";
344  break;
345  case 1:
346  word = "One-line";
347  break;
348  default:
349  word = "Detailed";
350  break;
351  }
352  pr_info("%s crash reports will be generated on the console\n", word);
353  return 1;
354 }
355 __setup("crashinfo", crashinfo);
356 
357 static void dump_mem(void __user *address)
358 {
359  void __user *addr;
360  enum { region_size = 256, bytes_per_line = 16 };
361  int i, j, k;
362  int found_readable_mem = 0;
363 
364  pr_err("\n");
365  if (!access_ok(VERIFY_READ, address, 1)) {
366  pr_err("Not dumping at address 0x%lx (kernel address)\n",
367  (unsigned long)address);
368  return;
369  }
370 
371  addr = (void __user *)
372  (((unsigned long)address & -bytes_per_line) - region_size/2);
373  if (addr > address)
374  addr = NULL;
375  for (i = 0; i < region_size;
376  addr += bytes_per_line, i += bytes_per_line) {
377  unsigned char buf[bytes_per_line];
378  char line[100];
379  if (copy_from_user(buf, addr, bytes_per_line))
380  continue;
381  if (!found_readable_mem) {
382  pr_err("Dumping memory around address 0x%lx:\n",
383  (unsigned long)address);
384  found_readable_mem = 1;
385  }
386  j = sprintf(line, REGFMT":", (unsigned long)addr);
387  for (k = 0; k < bytes_per_line; ++k)
388  j += sprintf(&line[j], " %02x", buf[k]);
389  pr_err("%s\n", line);
390  }
391  if (!found_readable_mem)
392  pr_err("No readable memory around address 0x%lx\n",
393  (unsigned long)address);
394 }
395 
396 void trace_unhandled_signal(const char *type, struct pt_regs *regs,
397  unsigned long address, int sig)
398 {
399  struct task_struct *tsk = current;
400 
401  if (show_unhandled_signals == 0)
402  return;
403 
404  /* If the signal is handled, don't show it here. */
405  if (!is_global_init(tsk)) {
406  void __user *handler =
407  tsk->sighand->action[sig-1].sa.sa_handler;
408  if (handler != SIG_IGN && handler != SIG_DFL)
409  return;
410  }
411 
412  /* Rate-limit the one-line output, not the detailed output. */
413  if (show_unhandled_signals <= 1 && !printk_ratelimit())
414  return;
415 
416  printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
417  task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
418  tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
419 
420  print_vma_addr(KERN_CONT " in ", regs->pc);
421 
422  printk(KERN_CONT "\n");
423 
424  if (show_unhandled_signals > 1) {
425  switch (sig) {
426  case SIGILL:
427  case SIGFPE:
428  case SIGSEGV:
429  case SIGBUS:
430  pr_err("User crash: signal %d,"
431  " trap %ld, address 0x%lx\n",
432  sig, regs->faultnum, address);
433  show_regs(regs);
434  dump_mem((void __user *)address);
435  break;
436  default:
437  pr_err("User crash: signal %d, trap %ld\n",
438  sig, regs->faultnum);
439  break;
440  }
441  }
442 }