signal.c 11 KB

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