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