signal.c 14 KB

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