signal.c 13 KB

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