signal.c 12 KB

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