signal.c 13 KB

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