signal.c 11 KB

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