signal.c 11 KB

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