signal.c 11 KB

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