signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 causing 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 <linux/sched/task_stack.h>
  56. #include <asm/ucontext.h>
  57. struct rt_sigframe {
  58. struct siginfo info;
  59. struct ucontext uc;
  60. #define MAGIC_SIGALTSTK 0x07302004
  61. unsigned int sigret_magic;
  62. };
  63. static int
  64. stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
  65. sigset_t *set)
  66. {
  67. int err;
  68. struct user_regs_struct uregs;
  69. uregs.scratch.bta = regs->bta;
  70. uregs.scratch.lp_start = regs->lp_start;
  71. uregs.scratch.lp_end = regs->lp_end;
  72. uregs.scratch.lp_count = regs->lp_count;
  73. uregs.scratch.status32 = regs->status32;
  74. uregs.scratch.ret = regs->ret;
  75. uregs.scratch.blink = regs->blink;
  76. uregs.scratch.fp = regs->fp;
  77. uregs.scratch.gp = regs->r26;
  78. uregs.scratch.r12 = regs->r12;
  79. uregs.scratch.r11 = regs->r11;
  80. uregs.scratch.r10 = regs->r10;
  81. uregs.scratch.r9 = regs->r9;
  82. uregs.scratch.r8 = regs->r8;
  83. uregs.scratch.r7 = regs->r7;
  84. uregs.scratch.r6 = regs->r6;
  85. uregs.scratch.r5 = regs->r5;
  86. uregs.scratch.r4 = regs->r4;
  87. uregs.scratch.r3 = regs->r3;
  88. uregs.scratch.r2 = regs->r2;
  89. uregs.scratch.r1 = regs->r1;
  90. uregs.scratch.r0 = regs->r0;
  91. uregs.scratch.sp = regs->sp;
  92. err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), &uregs.scratch,
  93. sizeof(sf->uc.uc_mcontext.regs.scratch));
  94. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
  95. return err;
  96. }
  97. static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
  98. {
  99. sigset_t set;
  100. int err;
  101. struct user_regs_struct uregs;
  102. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  103. err |= __copy_from_user(&uregs.scratch,
  104. &(sf->uc.uc_mcontext.regs.scratch),
  105. sizeof(sf->uc.uc_mcontext.regs.scratch));
  106. if (err)
  107. return err;
  108. set_current_blocked(&set);
  109. regs->bta = uregs.scratch.bta;
  110. regs->lp_start = uregs.scratch.lp_start;
  111. regs->lp_end = uregs.scratch.lp_end;
  112. regs->lp_count = uregs.scratch.lp_count;
  113. regs->status32 = uregs.scratch.status32;
  114. regs->ret = uregs.scratch.ret;
  115. regs->blink = uregs.scratch.blink;
  116. regs->fp = uregs.scratch.fp;
  117. regs->r26 = uregs.scratch.gp;
  118. regs->r12 = uregs.scratch.r12;
  119. regs->r11 = uregs.scratch.r11;
  120. regs->r10 = uregs.scratch.r10;
  121. regs->r9 = uregs.scratch.r9;
  122. regs->r8 = uregs.scratch.r8;
  123. regs->r7 = uregs.scratch.r7;
  124. regs->r6 = uregs.scratch.r6;
  125. regs->r5 = uregs.scratch.r5;
  126. regs->r4 = uregs.scratch.r4;
  127. regs->r3 = uregs.scratch.r3;
  128. regs->r2 = uregs.scratch.r2;
  129. regs->r1 = uregs.scratch.r1;
  130. regs->r0 = uregs.scratch.r0;
  131. regs->sp = uregs.scratch.sp;
  132. return 0;
  133. }
  134. static inline int is_do_ss_needed(unsigned int magic)
  135. {
  136. if (MAGIC_SIGALTSTK == magic)
  137. return 1;
  138. else
  139. return 0;
  140. }
  141. SYSCALL_DEFINE0(rt_sigreturn)
  142. {
  143. struct rt_sigframe __user *sf;
  144. unsigned int magic;
  145. struct pt_regs *regs = current_pt_regs();
  146. /* Always make any pending restarted system calls return -EINTR */
  147. current->restart_block.fn = do_no_restart_syscall;
  148. /* Since we stacked the signal on a word boundary,
  149. * then 'sp' should be word aligned here. If it's
  150. * not, then the user is trying to mess with us.
  151. */
  152. if (regs->sp & 3)
  153. goto badframe;
  154. sf = (struct rt_sigframe __force __user *)(regs->sp);
  155. if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
  156. goto badframe;
  157. if (__get_user(magic, &sf->sigret_magic))
  158. goto badframe;
  159. if (unlikely(is_do_ss_needed(magic)))
  160. if (restore_altstack(&sf->uc.uc_stack))
  161. goto badframe;
  162. if (restore_usr_regs(regs, sf))
  163. goto badframe;
  164. /* Don't restart from sigreturn */
  165. syscall_wont_restart(regs);
  166. /*
  167. * Ensure that sigreturn always returns to user mode (in case the
  168. * regs saved on user stack got fudged between save and sigreturn)
  169. * Otherwise it is easy to panic the kernel with a custom
  170. * signal handler and/or restorer which clobberes the status32/ret
  171. * to return to a bogus location in kernel mode.
  172. */
  173. regs->status32 |= STATUS_U_MASK;
  174. return regs->r0;
  175. badframe:
  176. force_sig(SIGSEGV, current);
  177. return 0;
  178. }
  179. /*
  180. * Determine which stack to use..
  181. */
  182. static inline void __user *get_sigframe(struct ksignal *ksig,
  183. struct pt_regs *regs,
  184. unsigned long framesize)
  185. {
  186. unsigned long sp = sigsp(regs->sp, ksig);
  187. void __user *frame;
  188. /* No matter what happens, 'sp' must be word
  189. * aligned otherwise nasty things could happen
  190. */
  191. /* ATPCS B01 mandates 8-byte alignment */
  192. frame = (void __user *)((sp - framesize) & ~7);
  193. /* Check that we can actually write to the signal frame */
  194. if (!access_ok(VERIFY_WRITE, frame, framesize))
  195. frame = NULL;
  196. return frame;
  197. }
  198. static int
  199. setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
  200. {
  201. struct rt_sigframe __user *sf;
  202. unsigned int magic = 0;
  203. int err = 0;
  204. sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
  205. if (!sf)
  206. return 1;
  207. /*
  208. * w/o SA_SIGINFO, struct ucontext is partially populated (only
  209. * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
  210. * during signal handler execution. This works for SA_SIGINFO as well
  211. * although the semantics are now overloaded (the same reg state can be
  212. * inspected by userland: but are they allowed to fiddle with it ?
  213. */
  214. err |= stash_usr_regs(sf, regs, set);
  215. /*
  216. * SA_SIGINFO requires 3 args to signal handler:
  217. * #1: sig-no (common to any handler)
  218. * #2: struct siginfo
  219. * #3: struct ucontext (completely populated)
  220. */
  221. if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) {
  222. err |= copy_siginfo_to_user(&sf->info, &ksig->info);
  223. err |= __put_user(0, &sf->uc.uc_flags);
  224. err |= __put_user(NULL, &sf->uc.uc_link);
  225. err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
  226. /* setup args 2 and 3 for user mode handler */
  227. regs->r1 = (unsigned long)&sf->info;
  228. regs->r2 = (unsigned long)&sf->uc;
  229. /*
  230. * small optim to avoid unconditonally calling do_sigaltstack
  231. * in sigreturn path, now that we only have rt_sigreturn
  232. */
  233. magic = MAGIC_SIGALTSTK;
  234. }
  235. err |= __put_user(magic, &sf->sigret_magic);
  236. if (err)
  237. return err;
  238. /* #1 arg to the user Signal handler */
  239. regs->r0 = ksig->sig;
  240. /* setup PC of user space signal handler */
  241. regs->ret = (unsigned long)ksig->ka.sa.sa_handler;
  242. /*
  243. * handler returns using sigreturn stub provided already by userpsace
  244. * If not, nuke the process right away
  245. */
  246. if(!(ksig->ka.sa.sa_flags & SA_RESTORER))
  247. return 1;
  248. regs->blink = (unsigned long)ksig->ka.sa.sa_restorer;
  249. /* User Stack for signal handler will be above the frame just carved */
  250. regs->sp = (unsigned long)sf;
  251. /*
  252. * Bug 94183, Clear the DE bit, so that when signal handler
  253. * starts to run, it doesn't use BTA
  254. */
  255. regs->status32 &= ~STATUS_DE_MASK;
  256. regs->status32 |= STATUS_L_MASK;
  257. return err;
  258. }
  259. static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
  260. {
  261. switch (regs->r0) {
  262. case -ERESTART_RESTARTBLOCK:
  263. case -ERESTARTNOHAND:
  264. /*
  265. * ERESTARTNOHAND means that the syscall should
  266. * only be restarted if there was no handler for
  267. * the signal, and since we only get here if there
  268. * is a handler, we don't restart
  269. */
  270. regs->r0 = -EINTR; /* ERESTART_xxx is internal */
  271. break;
  272. case -ERESTARTSYS:
  273. /*
  274. * ERESTARTSYS means to restart the syscall if
  275. * there is no handler or the handler was
  276. * registered with SA_RESTART
  277. */
  278. if (!(ka->sa.sa_flags & SA_RESTART)) {
  279. regs->r0 = -EINTR;
  280. break;
  281. }
  282. /* fallthrough */
  283. case -ERESTARTNOINTR:
  284. /*
  285. * ERESTARTNOINTR means that the syscall should
  286. * be called again after the signal handler returns.
  287. * Setup reg state just as it was before doing the trap
  288. * r0 has been clobbered with sys call ret code thus it
  289. * needs to be reloaded with orig first arg to syscall
  290. * in orig_r0. Rest of relevant reg-file:
  291. * r8 (syscall num) and (r1 - r7) will be reset to
  292. * their orig user space value when we ret from kernel
  293. */
  294. regs->r0 = regs->orig_r0;
  295. regs->ret -= is_isa_arcv2() ? 2 : 4;
  296. break;
  297. }
  298. }
  299. /*
  300. * OK, we're invoking a handler
  301. */
  302. static void
  303. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  304. {
  305. sigset_t *oldset = sigmask_to_save();
  306. int failed;
  307. /* Set up the stack frame */
  308. failed = setup_rt_frame(ksig, oldset, regs);
  309. signal_setup_done(failed, ksig, 0);
  310. }
  311. void do_signal(struct pt_regs *regs)
  312. {
  313. struct ksignal ksig;
  314. int restart_scall;
  315. restart_scall = in_syscall(regs) && syscall_restartable(regs);
  316. if (get_signal(&ksig)) {
  317. if (restart_scall) {
  318. arc_restart_syscall(&ksig.ka, regs);
  319. syscall_wont_restart(regs); /* No more restarts */
  320. }
  321. handle_signal(&ksig, regs);
  322. return;
  323. }
  324. if (restart_scall) {
  325. /* No handler for syscall: restart it */
  326. if (regs->r0 == -ERESTARTNOHAND ||
  327. regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
  328. regs->r0 = regs->orig_r0;
  329. regs->ret -= is_isa_arcv2() ? 2 : 4;
  330. } else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
  331. regs->r8 = __NR_restart_syscall;
  332. regs->ret -= is_isa_arcv2() ? 2 : 4;
  333. }
  334. syscall_wont_restart(regs); /* No more restarts */
  335. }
  336. /* If there's no signal to deliver, restore the saved sigmask back */
  337. restore_saved_sigmask();
  338. }
  339. void do_notify_resume(struct pt_regs *regs)
  340. {
  341. /*
  342. * ASM glue gaurantees that this is only called when returning to
  343. * user mode
  344. */
  345. if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
  346. tracehook_notify_resume(regs);
  347. }