signal.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Signal Handling for ARC
  3. *
  4. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * vineetg: Jan 2010 (Restarting of timer related syscalls)
  11. *
  12. * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
  13. * -do_signal() supports TIF_RESTORE_SIGMASK
  14. * -do_signal() no loner needs oldset, required by OLD sys_sigsuspend
  15. * -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen
  16. * -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed
  17. * -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves
  18. * the job to do_signal()
  19. *
  20. * vineetg: July 2009
  21. * -Modified Code to support the uClibc provided userland sigreturn stub
  22. * to avoid kernel synthesing it on user stack at runtime, costing TLB
  23. * probes and Cache line flushes.
  24. *
  25. * vineetg: July 2009
  26. * -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs
  27. * in done in block copy rather than one word at a time.
  28. * This saves around 2K of code and improves LMBench lat_sig <catch>
  29. *
  30. * rajeshwarr: Feb 2009
  31. * - Support for Realtime Signals
  32. *
  33. * vineetg: Aug 11th 2008: Bug #94183
  34. * -ViXS were still seeing crashes when using insmod to load drivers.
  35. * It turned out that the code to change Execute permssions for TLB entries
  36. * of user was not guarded for interrupts (mod_tlb_permission)
  37. * This was cauing TLB entries to be overwritten on unrelated indexes
  38. *
  39. * Vineetg: July 15th 2008: Bug #94183
  40. * -Exception happens in Delay slot of a JMP, and before user space resumes,
  41. * Signal is delivered (Ctrl + C) = >SIGINT.
  42. * setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler
  43. * to run, but doesn't clear the Delay slot bit from status32. As a result,
  44. * on resuming user mode, signal handler branches off to BTA of orig JMP
  45. * -FIX: clear the DE bit from status32 in setup_frame( )
  46. *
  47. * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
  48. */
  49. #include <linux/signal.h>
  50. #include <linux/ptrace.h>
  51. #include <linux/personality.h>
  52. #include <linux/uaccess.h>
  53. #include <linux/syscalls.h>
  54. #include <linux/tracehook.h>
  55. #include <asm/ucontext.h>
  56. struct rt_sigframe {
  57. struct siginfo info;
  58. struct ucontext uc;
  59. #define MAGIC_SIGALTSTK 0x07302004
  60. unsigned int sigret_magic;
  61. };
  62. static int
  63. stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
  64. sigset_t *set)
  65. {
  66. int err;
  67. err = __copy_to_user(&(sf->uc.uc_mcontext.regs), regs,
  68. sizeof(sf->uc.uc_mcontext.regs.scratch));
  69. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
  70. return err;
  71. }
  72. static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
  73. {
  74. sigset_t set;
  75. int err;
  76. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  77. if (!err)
  78. set_current_blocked(&set);
  79. err |= __copy_from_user(regs, &(sf->uc.uc_mcontext.regs),
  80. sizeof(sf->uc.uc_mcontext.regs.scratch));
  81. return err;
  82. }
  83. static inline int is_do_ss_needed(unsigned int magic)
  84. {
  85. if (MAGIC_SIGALTSTK == magic)
  86. return 1;
  87. else
  88. return 0;
  89. }
  90. SYSCALL_DEFINE0(rt_sigreturn)
  91. {
  92. struct rt_sigframe __user *sf;
  93. unsigned int magic;
  94. int err;
  95. struct pt_regs *regs = current_pt_regs();
  96. /* Always make any pending restarted system calls return -EINTR */
  97. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  98. /* Since we stacked the signal on a word boundary,
  99. * then 'sp' should be word aligned here. If it's
  100. * not, then the user is trying to mess with us.
  101. */
  102. if (regs->sp & 3)
  103. goto badframe;
  104. sf = (struct rt_sigframe __force __user *)(regs->sp);
  105. if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
  106. goto badframe;
  107. err = restore_usr_regs(regs, sf);
  108. err |= __get_user(magic, &sf->sigret_magic);
  109. if (err)
  110. goto badframe;
  111. if (unlikely(is_do_ss_needed(magic)))
  112. if (restore_altstack(&sf->uc.uc_stack))
  113. goto badframe;
  114. /* Don't restart from sigreturn */
  115. syscall_wont_restart(regs);
  116. return regs->r0;
  117. badframe:
  118. force_sig(SIGSEGV, current);
  119. return 0;
  120. }
  121. /*
  122. * Determine which stack to use..
  123. */
  124. static inline void __user *get_sigframe(struct k_sigaction *ka,
  125. struct pt_regs *regs,
  126. unsigned long framesize)
  127. {
  128. unsigned long sp = regs->sp;
  129. void __user *frame;
  130. /* This is the X/Open sanctioned signal stack switching */
  131. if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
  132. sp = current->sas_ss_sp + current->sas_ss_size;
  133. /* No matter what happens, 'sp' must be word
  134. * aligned otherwise nasty things could happen
  135. */
  136. /* ATPCS B01 mandates 8-byte alignment */
  137. frame = (void __user *)((sp - framesize) & ~7);
  138. /* Check that we can actually write to the signal frame */
  139. if (!access_ok(VERIFY_WRITE, frame, framesize))
  140. frame = NULL;
  141. return frame;
  142. }
  143. /*
  144. * translate the signal
  145. */
  146. static inline int map_sig(int sig)
  147. {
  148. struct thread_info *thread = current_thread_info();
  149. if (thread->exec_domain && thread->exec_domain->signal_invmap
  150. && sig < 32)
  151. sig = thread->exec_domain->signal_invmap[sig];
  152. return sig;
  153. }
  154. static int
  155. setup_rt_frame(int signo, struct k_sigaction *ka, siginfo_t *info,
  156. sigset_t *set, struct pt_regs *regs)
  157. {
  158. struct rt_sigframe __user *sf;
  159. unsigned int magic = 0;
  160. int err = 0;
  161. sf = get_sigframe(ka, regs, sizeof(struct rt_sigframe));
  162. if (!sf)
  163. return 1;
  164. /*
  165. * SA_SIGINFO requires 3 args to signal handler:
  166. * #1: sig-no (common to any handler)
  167. * #2: struct siginfo
  168. * #3: struct ucontext (completely populated)
  169. */
  170. if (unlikely(ka->sa.sa_flags & SA_SIGINFO)) {
  171. err |= copy_siginfo_to_user(&sf->info, info);
  172. err |= __put_user(0, &sf->uc.uc_flags);
  173. err |= __put_user(NULL, &sf->uc.uc_link);
  174. err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
  175. /* setup args 2 and 3 for user mode handler */
  176. regs->r1 = (unsigned long)&sf->info;
  177. regs->r2 = (unsigned long)&sf->uc;
  178. /*
  179. * small optim to avoid unconditonally calling do_sigaltstack
  180. * in sigreturn path, now that we only have rt_sigreturn
  181. */
  182. magic = MAGIC_SIGALTSTK;
  183. }
  184. /*
  185. * w/o SA_SIGINFO, struct ucontext is partially populated (only
  186. * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
  187. * during signal handler execution. This works for SA_SIGINFO as well
  188. * although the semantics are now overloaded (the same reg state can be
  189. * inspected by userland: but are they allowed to fiddle with it ?
  190. */
  191. err |= stash_usr_regs(sf, regs, set);
  192. err |= __put_user(magic, &sf->sigret_magic);
  193. if (err)
  194. return err;
  195. /* #1 arg to the user Signal handler */
  196. regs->r0 = map_sig(signo);
  197. /* setup PC of user space signal handler */
  198. regs->ret = (unsigned long)ka->sa.sa_handler;
  199. /*
  200. * handler returns using sigreturn stub provided already by userpsace
  201. */
  202. BUG_ON(!(ka->sa.sa_flags & SA_RESTORER));
  203. regs->blink = (unsigned long)ka->sa.sa_restorer;
  204. /* User Stack for signal handler will be above the frame just carved */
  205. regs->sp = (unsigned long)sf;
  206. /*
  207. * Bug 94183, Clear the DE bit, so that when signal handler
  208. * starts to run, it doesn't use BTA
  209. */
  210. regs->status32 &= ~STATUS_DE_MASK;
  211. regs->status32 |= STATUS_L_MASK;
  212. return err;
  213. }
  214. static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
  215. {
  216. switch (regs->r0) {
  217. case -ERESTART_RESTARTBLOCK:
  218. case -ERESTARTNOHAND:
  219. /*
  220. * ERESTARTNOHAND means that the syscall should
  221. * only be restarted if there was no handler for
  222. * the signal, and since we only get here if there
  223. * is a handler, we don't restart
  224. */
  225. regs->r0 = -EINTR; /* ERESTART_xxx is internal */
  226. break;
  227. case -ERESTARTSYS:
  228. /*
  229. * ERESTARTSYS means to restart the syscall if
  230. * there is no handler or the handler was
  231. * registered with SA_RESTART
  232. */
  233. if (!(ka->sa.sa_flags & SA_RESTART)) {
  234. regs->r0 = -EINTR;
  235. break;
  236. }
  237. /* fallthrough */
  238. case -ERESTARTNOINTR:
  239. /*
  240. * ERESTARTNOINTR means that the syscall should
  241. * be called again after the signal handler returns.
  242. * Setup reg state just as it was before doing the trap
  243. * r0 has been clobbered with sys call ret code thus it
  244. * needs to be reloaded with orig first arg to syscall
  245. * in orig_r0. Rest of relevant reg-file:
  246. * r8 (syscall num) and (r1 - r7) will be reset to
  247. * their orig user space value when we ret from kernel
  248. */
  249. regs->r0 = regs->orig_r0;
  250. regs->ret -= 4;
  251. break;
  252. }
  253. }
  254. /*
  255. * OK, we're invoking a handler
  256. */
  257. static void
  258. handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
  259. struct pt_regs *regs)
  260. {
  261. sigset_t *oldset = sigmask_to_save();
  262. int ret;
  263. /* Set up the stack frame */
  264. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  265. if (ret)
  266. force_sigsegv(sig, current);
  267. else
  268. signal_delivered(sig, info, ka, regs, 0);
  269. }
  270. void do_signal(struct pt_regs *regs)
  271. {
  272. struct k_sigaction ka;
  273. siginfo_t info;
  274. int signr;
  275. int restart_scall;
  276. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  277. restart_scall = in_syscall(regs) && syscall_restartable(regs);
  278. if (signr > 0) {
  279. if (restart_scall) {
  280. arc_restart_syscall(&ka, regs);
  281. syscall_wont_restart(regs); /* No more restarts */
  282. }
  283. handle_signal(signr, &ka, &info, regs);
  284. return;
  285. }
  286. if (restart_scall) {
  287. /* No handler for syscall: restart it */
  288. if (regs->r0 == -ERESTARTNOHAND ||
  289. regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
  290. regs->r0 = regs->orig_r0;
  291. regs->ret -= 4;
  292. } else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
  293. regs->r8 = __NR_restart_syscall;
  294. regs->ret -= 4;
  295. }
  296. syscall_wont_restart(regs); /* No more restarts */
  297. }
  298. /* If there's no signal to deliver, restore the saved sigmask back */
  299. restore_saved_sigmask();
  300. }
  301. void do_notify_resume(struct pt_regs *regs)
  302. {
  303. /*
  304. * ASM glue gaurantees that this is only called when returning to
  305. * user mode
  306. */
  307. if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  308. tracehook_notify_resume(regs);
  309. }