signal.c 11 KB

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