signal.c 12 KB

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