signal.c 12 KB

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