signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * arch/xtensa/kernel/signal.c
  3. *
  4. * Default platform functions.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2005, 2006 Tensilica Inc.
  11. * Copyright (C) 1991, 1992 Linus Torvalds
  12. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  13. *
  14. * Chris Zankel <[email protected]>
  15. * Joe Taylor <[email protected]>
  16. */
  17. #include <linux/signal.h>
  18. #include <linux/errno.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/personality.h>
  21. #include <linux/resume_user_mode.h>
  22. #include <linux/sched/task_stack.h>
  23. #include <asm/ucontext.h>
  24. #include <linux/uaccess.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/coprocessor.h>
  27. #include <asm/unistd.h>
  28. extern struct task_struct *coproc_owners[];
  29. struct rt_sigframe
  30. {
  31. struct siginfo info;
  32. struct ucontext uc;
  33. struct {
  34. xtregs_opt_t opt;
  35. xtregs_user_t user;
  36. #if XTENSA_HAVE_COPROCESSORS
  37. xtregs_coprocessor_t cp;
  38. #endif
  39. } xtregs;
  40. unsigned char retcode[6];
  41. unsigned int window[4];
  42. };
  43. #if defined(USER_SUPPORT_WINDOWED)
  44. /*
  45. * Flush register windows stored in pt_regs to stack.
  46. * Returns 1 for errors.
  47. */
  48. static int
  49. flush_window_regs_user(struct pt_regs *regs)
  50. {
  51. const unsigned long ws = regs->windowstart;
  52. const unsigned long wb = regs->windowbase;
  53. unsigned long sp = 0;
  54. unsigned long wm;
  55. int err = 1;
  56. int base;
  57. /* Return if no other frames. */
  58. if (regs->wmask == 1)
  59. return 0;
  60. /* Rotate windowmask and skip empty frames. */
  61. wm = (ws >> wb) | (ws << (XCHAL_NUM_AREGS / 4 - wb));
  62. base = (XCHAL_NUM_AREGS / 4) - (regs->wmask >> 4);
  63. /* For call8 or call12 frames, we need the previous stack pointer. */
  64. if ((regs->wmask & 2) == 0)
  65. if (__get_user(sp, (int*)(regs->areg[base * 4 + 1] - 12)))
  66. goto errout;
  67. /* Spill frames to stack. */
  68. while (base < XCHAL_NUM_AREGS / 4) {
  69. int m = (wm >> base);
  70. int inc = 0;
  71. /* Save registers a4..a7 (call8) or a4...a11 (call12) */
  72. if (m & 2) { /* call4 */
  73. inc = 1;
  74. } else if (m & 4) { /* call8 */
  75. if (copy_to_user(&SPILL_SLOT_CALL8(sp, 4),
  76. &regs->areg[(base + 1) * 4], 16))
  77. goto errout;
  78. inc = 2;
  79. } else if (m & 8) { /* call12 */
  80. if (copy_to_user(&SPILL_SLOT_CALL12(sp, 4),
  81. &regs->areg[(base + 1) * 4], 32))
  82. goto errout;
  83. inc = 3;
  84. }
  85. /* Save current frame a0..a3 under next SP */
  86. sp = regs->areg[((base + inc) * 4 + 1) % XCHAL_NUM_AREGS];
  87. if (copy_to_user(&SPILL_SLOT(sp, 0), &regs->areg[base * 4], 16))
  88. goto errout;
  89. /* Get current stack pointer for next loop iteration. */
  90. sp = regs->areg[base * 4 + 1];
  91. base += inc;
  92. }
  93. regs->wmask = 1;
  94. regs->windowstart = 1 << wb;
  95. return 0;
  96. errout:
  97. return err;
  98. }
  99. #else
  100. static int
  101. flush_window_regs_user(struct pt_regs *regs)
  102. {
  103. return 0;
  104. }
  105. #endif
  106. /*
  107. * Note: We don't copy double exception 'regs', we have to finish double exc.
  108. * first before we return to signal handler! This dbl.exc.handler might cause
  109. * another double exception, but I think we are fine as the situation is the
  110. * same as if we had returned to the signal handerl and got an interrupt
  111. * immediately...
  112. */
  113. static int
  114. setup_sigcontext(struct rt_sigframe __user *frame, struct pt_regs *regs)
  115. {
  116. struct sigcontext __user *sc = &frame->uc.uc_mcontext;
  117. struct thread_info *ti = current_thread_info();
  118. int err = 0;
  119. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  120. COPY(pc);
  121. COPY(ps);
  122. COPY(lbeg);
  123. COPY(lend);
  124. COPY(lcount);
  125. COPY(sar);
  126. #undef COPY
  127. err |= flush_window_regs_user(regs);
  128. err |= __copy_to_user (sc->sc_a, regs->areg, 16 * 4);
  129. err |= __put_user(0, &sc->sc_xtregs);
  130. if (err)
  131. return err;
  132. #if XTENSA_HAVE_COPROCESSORS
  133. coprocessor_flush_release_all(ti);
  134. err |= __copy_to_user(&frame->xtregs.cp, &ti->xtregs_cp,
  135. sizeof (frame->xtregs.cp));
  136. #endif
  137. err |= __copy_to_user(&frame->xtregs.opt, &regs->xtregs_opt,
  138. sizeof (xtregs_opt_t));
  139. err |= __copy_to_user(&frame->xtregs.user, &ti->xtregs_user,
  140. sizeof (xtregs_user_t));
  141. err |= __put_user(err ? NULL : &frame->xtregs, &sc->sc_xtregs);
  142. return err;
  143. }
  144. static int
  145. restore_sigcontext(struct pt_regs *regs, struct rt_sigframe __user *frame)
  146. {
  147. struct sigcontext __user *sc = &frame->uc.uc_mcontext;
  148. struct thread_info *ti = current_thread_info();
  149. unsigned int err = 0;
  150. unsigned long ps;
  151. #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
  152. COPY(pc);
  153. COPY(lbeg);
  154. COPY(lend);
  155. COPY(lcount);
  156. COPY(sar);
  157. #undef COPY
  158. /* All registers were flushed to stack. Start with a pristine frame. */
  159. regs->wmask = 1;
  160. regs->windowbase = 0;
  161. regs->windowstart = 1;
  162. regs->syscall = NO_SYSCALL; /* disable syscall checks */
  163. /* For PS, restore only PS.CALLINC.
  164. * Assume that all other bits are either the same as for the signal
  165. * handler, or the user mode value doesn't matter (e.g. PS.OWB).
  166. */
  167. err |= __get_user(ps, &sc->sc_ps);
  168. regs->ps = (regs->ps & ~PS_CALLINC_MASK) | (ps & PS_CALLINC_MASK);
  169. /* Additional corruption checks */
  170. if ((regs->lcount > 0)
  171. && ((regs->lbeg > TASK_SIZE) || (regs->lend > TASK_SIZE)) )
  172. err = 1;
  173. err |= __copy_from_user(regs->areg, sc->sc_a, 16 * 4);
  174. if (err)
  175. return err;
  176. /* The signal handler may have used coprocessors in which
  177. * case they are still enabled. We disable them to force a
  178. * reloading of the original task's CP state by the lazy
  179. * context-switching mechanisms of CP exception handling.
  180. * Also, we essentially discard any coprocessor state that the
  181. * signal handler created. */
  182. #if XTENSA_HAVE_COPROCESSORS
  183. coprocessor_release_all(ti);
  184. err |= __copy_from_user(&ti->xtregs_cp, &frame->xtregs.cp,
  185. sizeof (frame->xtregs.cp));
  186. #endif
  187. err |= __copy_from_user(&ti->xtregs_user, &frame->xtregs.user,
  188. sizeof (xtregs_user_t));
  189. err |= __copy_from_user(&regs->xtregs_opt, &frame->xtregs.opt,
  190. sizeof (xtregs_opt_t));
  191. return err;
  192. }
  193. /*
  194. * Do a signal return; undo the signal stack.
  195. */
  196. asmlinkage long xtensa_rt_sigreturn(void)
  197. {
  198. struct pt_regs *regs = current_pt_regs();
  199. struct rt_sigframe __user *frame;
  200. sigset_t set;
  201. int ret;
  202. /* Always make any pending restarted system calls return -EINTR */
  203. current->restart_block.fn = do_no_restart_syscall;
  204. if (regs->depc > 64)
  205. panic("rt_sigreturn in double exception!\n");
  206. frame = (struct rt_sigframe __user *) regs->areg[1];
  207. if (!access_ok(frame, sizeof(*frame)))
  208. goto badframe;
  209. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  210. goto badframe;
  211. set_current_blocked(&set);
  212. if (restore_sigcontext(regs, frame))
  213. goto badframe;
  214. ret = regs->areg[2];
  215. if (restore_altstack(&frame->uc.uc_stack))
  216. goto badframe;
  217. return ret;
  218. badframe:
  219. force_sig(SIGSEGV);
  220. return 0;
  221. }
  222. /*
  223. * Set up a signal frame.
  224. */
  225. static int
  226. gen_return_code(unsigned char *codemem)
  227. {
  228. int err = 0;
  229. /*
  230. * The 12-bit immediate is really split up within the 24-bit MOVI
  231. * instruction. As long as the above system call numbers fit within
  232. * 8-bits, the following code works fine. See the Xtensa ISA for
  233. * details.
  234. */
  235. #if __NR_rt_sigreturn > 255
  236. # error Generating the MOVI instruction below breaks!
  237. #endif
  238. #ifdef __XTENSA_EB__ /* Big Endian version */
  239. /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
  240. err |= __put_user(0x22, &codemem[0]);
  241. err |= __put_user(0x0a, &codemem[1]);
  242. err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
  243. /* Generate instruction: SYSCALL */
  244. err |= __put_user(0x00, &codemem[3]);
  245. err |= __put_user(0x05, &codemem[4]);
  246. err |= __put_user(0x00, &codemem[5]);
  247. #elif defined __XTENSA_EL__ /* Little Endian version */
  248. /* Generate instruction: MOVI a2, __NR_rt_sigreturn */
  249. err |= __put_user(0x22, &codemem[0]);
  250. err |= __put_user(0xa0, &codemem[1]);
  251. err |= __put_user(__NR_rt_sigreturn, &codemem[2]);
  252. /* Generate instruction: SYSCALL */
  253. err |= __put_user(0x00, &codemem[3]);
  254. err |= __put_user(0x50, &codemem[4]);
  255. err |= __put_user(0x00, &codemem[5]);
  256. #else
  257. # error Must use compiler for Xtensa processors.
  258. #endif
  259. /* Flush generated code out of the data cache */
  260. if (err == 0) {
  261. __invalidate_icache_range((unsigned long)codemem, 6UL);
  262. __flush_invalidate_dcache_range((unsigned long)codemem, 6UL);
  263. }
  264. return err;
  265. }
  266. static int setup_frame(struct ksignal *ksig, sigset_t *set,
  267. struct pt_regs *regs)
  268. {
  269. struct rt_sigframe *frame;
  270. int err = 0, sig = ksig->sig;
  271. unsigned long sp, ra, tp, ps;
  272. unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
  273. unsigned long handler_fdpic_GOT = 0;
  274. unsigned int base;
  275. bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
  276. (current->personality & FDPIC_FUNCPTRS);
  277. if (fdpic) {
  278. unsigned long __user *fdpic_func_desc =
  279. (unsigned long __user *)handler;
  280. if (__get_user(handler, &fdpic_func_desc[0]) ||
  281. __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
  282. return -EFAULT;
  283. }
  284. sp = regs->areg[1];
  285. if ((ksig->ka.sa.sa_flags & SA_ONSTACK) != 0 && sas_ss_flags(sp) == 0) {
  286. sp = current->sas_ss_sp + current->sas_ss_size;
  287. }
  288. frame = (void *)((sp - sizeof(*frame)) & -16ul);
  289. if (regs->depc > 64)
  290. panic ("Double exception sys_sigreturn\n");
  291. if (!access_ok(frame, sizeof(*frame))) {
  292. return -EFAULT;
  293. }
  294. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  295. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  296. }
  297. /* Create the user context. */
  298. err |= __put_user(0, &frame->uc.uc_flags);
  299. err |= __put_user(0, &frame->uc.uc_link);
  300. err |= __save_altstack(&frame->uc.uc_stack, regs->areg[1]);
  301. err |= setup_sigcontext(frame, regs);
  302. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  303. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  304. if (fdpic) {
  305. unsigned long __user *fdpic_func_desc =
  306. (unsigned long __user *)ksig->ka.sa.sa_restorer;
  307. err |= __get_user(ra, fdpic_func_desc);
  308. } else {
  309. ra = (unsigned long)ksig->ka.sa.sa_restorer;
  310. }
  311. } else {
  312. /* Create sys_rt_sigreturn syscall in stack frame */
  313. err |= gen_return_code(frame->retcode);
  314. ra = (unsigned long) frame->retcode;
  315. }
  316. if (err)
  317. return -EFAULT;
  318. /*
  319. * Create signal handler execution context.
  320. * Return context not modified until this point.
  321. */
  322. /* Set up registers for signal handler; preserve the threadptr */
  323. tp = regs->threadptr;
  324. ps = regs->ps;
  325. start_thread(regs, handler, (unsigned long)frame);
  326. /* Set up a stack frame for a call4 if userspace uses windowed ABI */
  327. if (ps & PS_WOE_MASK) {
  328. base = 4;
  329. regs->areg[base] =
  330. (((unsigned long) ra) & 0x3fffffff) | 0x40000000;
  331. ps = (ps & ~(PS_CALLINC_MASK | PS_OWB_MASK)) |
  332. (1 << PS_CALLINC_SHIFT);
  333. } else {
  334. base = 0;
  335. regs->areg[base] = (unsigned long) ra;
  336. }
  337. regs->areg[base + 2] = (unsigned long) sig;
  338. regs->areg[base + 3] = (unsigned long) &frame->info;
  339. regs->areg[base + 4] = (unsigned long) &frame->uc;
  340. regs->threadptr = tp;
  341. regs->ps = ps;
  342. if (fdpic)
  343. regs->areg[base + 11] = handler_fdpic_GOT;
  344. pr_debug("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08lx\n",
  345. current->comm, current->pid, sig, frame, regs->pc);
  346. return 0;
  347. }
  348. /*
  349. * Note that 'init' is a special process: it doesn't get signals it doesn't
  350. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  351. * mistake.
  352. *
  353. * Note that we go through the signals twice: once to check the signals that
  354. * the kernel can handle, and then we build all the user-level signal handling
  355. * stack-frames in one go after that.
  356. */
  357. static void do_signal(struct pt_regs *regs)
  358. {
  359. struct ksignal ksig;
  360. task_pt_regs(current)->icountlevel = 0;
  361. if (get_signal(&ksig)) {
  362. int ret;
  363. /* Are we from a system call? */
  364. if (regs->syscall != NO_SYSCALL) {
  365. /* If so, check system call restarting.. */
  366. switch (regs->areg[2]) {
  367. case -ERESTARTNOHAND:
  368. case -ERESTART_RESTARTBLOCK:
  369. regs->areg[2] = -EINTR;
  370. break;
  371. case -ERESTARTSYS:
  372. if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
  373. regs->areg[2] = -EINTR;
  374. break;
  375. }
  376. fallthrough;
  377. case -ERESTARTNOINTR:
  378. regs->areg[2] = regs->syscall;
  379. regs->pc -= 3;
  380. break;
  381. default:
  382. /* nothing to do */
  383. if (regs->areg[2] != 0)
  384. break;
  385. }
  386. }
  387. /* Whee! Actually deliver the signal. */
  388. /* Set up the stack frame */
  389. ret = setup_frame(&ksig, sigmask_to_save(), regs);
  390. signal_setup_done(ret, &ksig, 0);
  391. if (test_thread_flag(TIF_SINGLESTEP))
  392. task_pt_regs(current)->icountlevel = 1;
  393. return;
  394. }
  395. /* Did we come from a system call? */
  396. if (regs->syscall != NO_SYSCALL) {
  397. /* Restart the system call - no handlers present */
  398. switch (regs->areg[2]) {
  399. case -ERESTARTNOHAND:
  400. case -ERESTARTSYS:
  401. case -ERESTARTNOINTR:
  402. regs->areg[2] = regs->syscall;
  403. regs->pc -= 3;
  404. break;
  405. case -ERESTART_RESTARTBLOCK:
  406. regs->areg[2] = __NR_restart_syscall;
  407. regs->pc -= 3;
  408. break;
  409. }
  410. }
  411. /* If there's no signal to deliver, we just restore the saved mask. */
  412. restore_saved_sigmask();
  413. if (test_thread_flag(TIF_SINGLESTEP))
  414. task_pt_regs(current)->icountlevel = 1;
  415. return;
  416. }
  417. void do_notify_resume(struct pt_regs *regs)
  418. {
  419. if (test_thread_flag(TIF_SIGPENDING) ||
  420. test_thread_flag(TIF_NOTIFY_SIGNAL))
  421. do_signal(regs);
  422. if (test_thread_flag(TIF_NOTIFY_RESUME))
  423. resume_user_mode_work(regs);
  424. }