PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/xtensa/kernel/signal.c

https://github.com/kvaneesh/linux
C | 507 lines | 311 code | 107 blank | 89 comment | 53 complexity | 9884c1779a1fa5d966a1f88c25236099 MD5 | raw file
  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. 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(&SPILL_SLOT_CALL8(sp, 4),
  75. &regs->areg[(base + 1) * 4], 16))
  76. goto errout;
  77. inc = 2;
  78. } else if (m & 8) { /* call12 */
  79. if (copy_to_user(&SPILL_SLOT_CALL12(sp, 4),
  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(&SPILL_SLOT(sp, 0), &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 pristine frame. */
  152. regs->wmask = 1;
  153. regs->windowbase = 0;
  154. regs->windowstart = 1;
  155. regs->syscall = NO_SYSCALL; /* 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(void)
  190. {
  191. struct pt_regs *regs = current_pt_regs();
  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(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);
  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, ps;
  265. unsigned int base;
  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(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. ps = regs->ps;
  302. start_thread(regs, (unsigned long) ksig->ka.sa.sa_handler,
  303. (unsigned long) frame);
  304. /* Set up a stack frame for a call4 if userspace uses windowed ABI */
  305. if (ps & PS_WOE_MASK) {
  306. base = 4;
  307. regs->areg[base] =
  308. (((unsigned long) ra) & 0x3fffffff) | 0x40000000;
  309. ps = (ps & ~(PS_CALLINC_MASK | PS_OWB_MASK)) |
  310. (1 << PS_CALLINC_SHIFT);
  311. } else {
  312. base = 0;
  313. regs->areg[base] = (unsigned long) ra;
  314. }
  315. regs->areg[base + 2] = (unsigned long) sig;
  316. regs->areg[base + 3] = (unsigned long) &frame->info;
  317. regs->areg[base + 4] = (unsigned long) &frame->uc;
  318. regs->threadptr = tp;
  319. regs->ps = ps;
  320. pr_debug("SIG rt deliver (%s:%d): signal=%d sp=%p pc=%08lx\n",
  321. current->comm, current->pid, sig, frame, regs->pc);
  322. return 0;
  323. }
  324. /*
  325. * Note that 'init' is a special process: it doesn't get signals it doesn't
  326. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  327. * mistake.
  328. *
  329. * Note that we go through the signals twice: once to check the signals that
  330. * the kernel can handle, and then we build all the user-level signal handling
  331. * stack-frames in one go after that.
  332. */
  333. static void do_signal(struct pt_regs *regs)
  334. {
  335. struct ksignal ksig;
  336. task_pt_regs(current)->icountlevel = 0;
  337. if (get_signal(&ksig)) {
  338. int ret;
  339. /* Are we from a system call? */
  340. if (regs->syscall != NO_SYSCALL) {
  341. /* If so, check system call restarting.. */
  342. switch (regs->areg[2]) {
  343. case -ERESTARTNOHAND:
  344. case -ERESTART_RESTARTBLOCK:
  345. regs->areg[2] = -EINTR;
  346. break;
  347. case -ERESTARTSYS:
  348. if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
  349. regs->areg[2] = -EINTR;
  350. break;
  351. }
  352. fallthrough;
  353. case -ERESTARTNOINTR:
  354. regs->areg[2] = regs->syscall;
  355. regs->pc -= 3;
  356. break;
  357. default:
  358. /* nothing to do */
  359. if (regs->areg[2] != 0)
  360. break;
  361. }
  362. }
  363. /* Whee! Actually deliver the signal. */
  364. /* Set up the stack frame */
  365. ret = setup_frame(&ksig, sigmask_to_save(), regs);
  366. signal_setup_done(ret, &ksig, 0);
  367. if (current->ptrace & PT_SINGLESTEP)
  368. task_pt_regs(current)->icountlevel = 1;
  369. return;
  370. }
  371. /* Did we come from a system call? */
  372. if (regs->syscall != NO_SYSCALL) {
  373. /* Restart the system call - no handlers present */
  374. switch (regs->areg[2]) {
  375. case -ERESTARTNOHAND:
  376. case -ERESTARTSYS:
  377. case -ERESTARTNOINTR:
  378. regs->areg[2] = regs->syscall;
  379. regs->pc -= 3;
  380. break;
  381. case -ERESTART_RESTARTBLOCK:
  382. regs->areg[2] = __NR_restart_syscall;
  383. regs->pc -= 3;
  384. break;
  385. }
  386. }
  387. /* If there's no signal to deliver, we just restore the saved mask. */
  388. restore_saved_sigmask();
  389. if (current->ptrace & PT_SINGLESTEP)
  390. task_pt_regs(current)->icountlevel = 1;
  391. return;
  392. }
  393. void do_notify_resume(struct pt_regs *regs)
  394. {
  395. if (test_thread_flag(TIF_SIGPENDING) ||
  396. test_thread_flag(TIF_NOTIFY_SIGNAL))
  397. do_signal(regs);
  398. if (test_thread_flag(TIF_NOTIFY_RESUME))
  399. tracehook_notify_resume(regs);
  400. }