signal.c 13 KB

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