signal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * linux/arch/cris/kernel/signal.c
  3. *
  4. * Based on arch/i386/kernel/signal.c by
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *
  7. *
  8. * Ideas also taken from arch/arm.
  9. *
  10. * Copyright (C) 2000-2007 Axis Communications AB
  11. *
  12. * Authors: Bjorn Wesen (bjornw@axis.com)
  13. *
  14. */
  15. #include <linux/sched.h>
  16. #include <linux/mm.h>
  17. #include <linux/smp.h>
  18. #include <linux/kernel.h>
  19. #include <linux/signal.h>
  20. #include <linux/errno.h>
  21. #include <linux/wait.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/unistd.h>
  24. #include <linux/stddef.h>
  25. #include <asm/processor.h>
  26. #include <asm/ucontext.h>
  27. #include <asm/uaccess.h>
  28. #define DEBUG_SIG 0
  29. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  30. /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
  31. /* manipulate regs so that upon return, it will be re-executed */
  32. /* We rely on that pc points to the instruction after "break 13", so the
  33. * library must never do strange things like putting it in a delay slot.
  34. */
  35. #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
  36. void do_signal(int canrestart, struct pt_regs *regs);
  37. /*
  38. * Atomically swap in the new signal mask, and wait for a signal. Define
  39. * dummy arguments to be able to reach the regs argument. (Note that this
  40. * arrangement relies on old_sigset_t occupying one register.)
  41. */
  42. int sys_sigsuspend(old_sigset_t mask, long r11, long r12, long r13, long mof,
  43. long srp, struct pt_regs *regs)
  44. {
  45. mask &= _BLOCKABLE;
  46. spin_lock_irq(&current->sighand->siglock);
  47. current->saved_sigmask = current->blocked;
  48. siginitset(&current->blocked, mask);
  49. recalc_sigpending();
  50. spin_unlock_irq(&current->sighand->siglock);
  51. current->state = TASK_INTERRUPTIBLE;
  52. schedule();
  53. set_thread_flag(TIF_RESTORE_SIGMASK);
  54. return -ERESTARTNOHAND;
  55. }
  56. int sys_sigaction(int sig, const struct old_sigaction __user *act,
  57. struct old_sigaction *oact)
  58. {
  59. struct k_sigaction new_ka, old_ka;
  60. int ret;
  61. if (act) {
  62. old_sigset_t mask;
  63. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  64. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  65. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  66. return -EFAULT;
  67. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  68. __get_user(mask, &act->sa_mask);
  69. siginitset(&new_ka.sa.sa_mask, mask);
  70. }
  71. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  72. if (!ret && oact) {
  73. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  74. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  75. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  76. return -EFAULT;
  77. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  78. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  79. }
  80. return ret;
  81. }
  82. int sys_sigaltstack(const stack_t *uss, stack_t __user *uoss)
  83. {
  84. return do_sigaltstack(uss, uoss, rdusp());
  85. }
  86. /*
  87. * Do a signal return; undo the signal stack.
  88. */
  89. struct sigframe {
  90. struct sigcontext sc;
  91. unsigned long extramask[_NSIG_WORDS-1];
  92. unsigned char retcode[8]; /* trampoline code */
  93. };
  94. struct rt_sigframe {
  95. struct siginfo *pinfo;
  96. void *puc;
  97. struct siginfo info;
  98. struct ucontext uc;
  99. unsigned char retcode[8]; /* trampoline code */
  100. };
  101. static int
  102. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  103. {
  104. unsigned int err = 0;
  105. unsigned long old_usp;
  106. /* Always make any pending restarted system calls return -EINTR */
  107. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  108. /* restore the regs from &sc->regs (same as sc, since regs is first)
  109. * (sc is already checked for VERIFY_READ since the sigframe was
  110. * checked in sys_sigreturn previously)
  111. */
  112. if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
  113. goto badframe;
  114. /* make sure the U-flag is set so user-mode cannot fool us */
  115. regs->dccr |= 1 << 8;
  116. /* restore the old USP as it was before we stacked the sc etc.
  117. * (we cannot just pop the sigcontext since we aligned the sp and
  118. * stuff after pushing it)
  119. */
  120. err |= __get_user(old_usp, &sc->usp);
  121. wrusp(old_usp);
  122. /* TODO: the other ports use regs->orig_XX to disable syscall checks
  123. * after this completes, but we don't use that mechanism. maybe we can
  124. * use it now ?
  125. */
  126. return err;
  127. badframe:
  128. return 1;
  129. }
  130. /* Define dummy arguments to be able to reach the regs argument. */
  131. asmlinkage int sys_sigreturn(long r10, long r11, long r12, long r13, long mof,
  132. long srp, struct pt_regs *regs)
  133. {
  134. struct sigframe __user *frame = (struct sigframe *)rdusp();
  135. sigset_t set;
  136. /*
  137. * Since we stacked the signal on a dword boundary,
  138. * then frame should be dword aligned here. If it's
  139. * not, then the user is trying to mess with us.
  140. */
  141. if (((long)frame) & 3)
  142. goto badframe;
  143. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  144. goto badframe;
  145. if (__get_user(set.sig[0], &frame->sc.oldmask)
  146. || (_NSIG_WORDS > 1
  147. && __copy_from_user(&set.sig[1], frame->extramask,
  148. sizeof(frame->extramask))))
  149. goto badframe;
  150. sigdelsetmask(&set, ~_BLOCKABLE);
  151. spin_lock_irq(&current->sighand->siglock);
  152. current->blocked = set;
  153. recalc_sigpending();
  154. spin_unlock_irq(&current->sighand->siglock);
  155. if (restore_sigcontext(regs, &frame->sc))
  156. goto badframe;
  157. /* TODO: SIGTRAP when single-stepping as in arm ? */
  158. return regs->r10;
  159. badframe:
  160. force_sig(SIGSEGV, current);
  161. return 0;
  162. }
  163. /* Define dummy arguments to be able to reach the regs argument. */
  164. asmlinkage int sys_rt_sigreturn(long r10, long r11, long r12, long r13,
  165. long mof, long srp, struct pt_regs *regs)
  166. {
  167. struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
  168. sigset_t set;
  169. /*
  170. * Since we stacked the signal on a dword boundary,
  171. * then frame should be dword aligned here. If it's
  172. * not, then the user is trying to mess with us.
  173. */
  174. if (((long)frame) & 3)
  175. goto badframe;
  176. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  177. goto badframe;
  178. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  179. goto badframe;
  180. sigdelsetmask(&set, ~_BLOCKABLE);
  181. spin_lock_irq(&current->sighand->siglock);
  182. current->blocked = set;
  183. recalc_sigpending();
  184. spin_unlock_irq(&current->sighand->siglock);
  185. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  186. goto badframe;
  187. if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
  188. goto badframe;
  189. return regs->r10;
  190. badframe:
  191. force_sig(SIGSEGV, current);
  192. return 0;
  193. }
  194. /*
  195. * Set up a signal frame.
  196. */
  197. static int setup_sigcontext(struct sigcontext __user *sc,
  198. struct pt_regs *regs, unsigned long mask)
  199. {
  200. int err = 0;
  201. unsigned long usp = rdusp();
  202. /* copy the regs. they are first in sc so we can use sc directly */
  203. err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
  204. /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
  205. the signal handler. The frametype will be restored to its previous
  206. value in restore_sigcontext. */
  207. regs->frametype = CRIS_FRAME_NORMAL;
  208. /* then some other stuff */
  209. err |= __put_user(mask, &sc->oldmask);
  210. err |= __put_user(usp, &sc->usp);
  211. return err;
  212. }
  213. /* Figure out where we want to put the new signal frame
  214. * - usually on the stack. */
  215. static inline void __user *
  216. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
  217. {
  218. unsigned long sp = rdusp();
  219. /* This is the X/Open sanctioned signal stack switching. */
  220. if (ka->sa.sa_flags & SA_ONSTACK) {
  221. if (! on_sig_stack(sp))
  222. sp = current->sas_ss_sp + current->sas_ss_size;
  223. }
  224. /* make sure the frame is dword-aligned */
  225. sp &= ~3;
  226. return (void __user*)(sp - frame_size);
  227. }
  228. /* grab and setup a signal frame.
  229. *
  230. * basically we stack a lot of state info, and arrange for the
  231. * user-mode program to return to the kernel using either a
  232. * trampoline which performs the syscall sigreturn, or a provided
  233. * user-mode trampoline.
  234. */
  235. static int setup_frame(int sig, struct k_sigaction *ka,
  236. sigset_t *set, struct pt_regs *regs)
  237. {
  238. struct sigframe __user *frame;
  239. unsigned long return_ip;
  240. int err = 0;
  241. frame = get_sigframe(ka, regs, sizeof(*frame));
  242. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  243. goto give_sigsegv;
  244. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  245. if (err)
  246. goto give_sigsegv;
  247. if (_NSIG_WORDS > 1) {
  248. err |= __copy_to_user(frame->extramask, &set->sig[1],
  249. sizeof(frame->extramask));
  250. }
  251. if (err)
  252. goto give_sigsegv;
  253. /* Set up to return from userspace. If provided, use a stub
  254. already in userspace. */
  255. if (ka->sa.sa_flags & SA_RESTORER) {
  256. return_ip = (unsigned long)ka->sa.sa_restorer;
  257. } else {
  258. /* trampoline - the desired return ip is the retcode itself */
  259. return_ip = (unsigned long)&frame->retcode;
  260. /* This is movu.w __NR_sigreturn, r9; break 13; */
  261. err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
  262. err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
  263. err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
  264. }
  265. if (err)
  266. goto give_sigsegv;
  267. /* Set up registers for signal handler */
  268. regs->irp = (unsigned long) ka->sa.sa_handler; /* what we enter NOW */
  269. regs->srp = return_ip; /* what we enter LATER */
  270. regs->r10 = sig; /* first argument is signo */
  271. /* actually move the usp to reflect the stacked frame */
  272. wrusp((unsigned long)frame);
  273. return 0;
  274. give_sigsegv:
  275. force_sigsegv(sig, current);
  276. return -EFAULT;
  277. }
  278. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  279. sigset_t *set, struct pt_regs *regs)
  280. {
  281. struct rt_sigframe __user *frame;
  282. unsigned long return_ip;
  283. int err = 0;
  284. frame = get_sigframe(ka, regs, sizeof(*frame));
  285. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  286. goto give_sigsegv;
  287. err |= __put_user(&frame->info, &frame->pinfo);
  288. err |= __put_user(&frame->uc, &frame->puc);
  289. err |= copy_siginfo_to_user(&frame->info, info);
  290. if (err)
  291. goto give_sigsegv;
  292. /* Clear all the bits of the ucontext we don't use. */
  293. err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
  294. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  295. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  296. if (err)
  297. goto give_sigsegv;
  298. /* Set up to return from userspace. If provided, use a stub
  299. already in userspace. */
  300. if (ka->sa.sa_flags & SA_RESTORER) {
  301. return_ip = (unsigned long)ka->sa.sa_restorer;
  302. } else {
  303. /* trampoline - the desired return ip is the retcode itself */
  304. return_ip = (unsigned long)&frame->retcode;
  305. /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
  306. err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
  307. err |= __put_user(__NR_rt_sigreturn,
  308. (short __user *)(frame->retcode+2));
  309. err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
  310. }
  311. if (err)
  312. goto give_sigsegv;
  313. /* TODO what is the current->exec_domain stuff and invmap ? */
  314. /* Set up registers for signal handler */
  315. /* What we enter NOW */
  316. regs->irp = (unsigned long) ka->sa.sa_handler;
  317. /* What we enter LATER */
  318. regs->srp = return_ip;
  319. /* First argument is signo */
  320. regs->r10 = sig;
  321. /* Second argument is (siginfo_t *) */
  322. regs->r11 = (unsigned long)&frame->info;
  323. /* Third argument is unused */
  324. regs->r12 = 0;
  325. /* Actually move the usp to reflect the stacked frame */
  326. wrusp((unsigned long)frame);
  327. return 0;
  328. give_sigsegv:
  329. force_sigsegv(sig, current);
  330. return -EFAULT;
  331. }
  332. /*
  333. * OK, we're invoking a handler
  334. */
  335. static inline int handle_signal(int canrestart, unsigned long sig,
  336. siginfo_t *info, struct k_sigaction *ka,
  337. sigset_t *oldset, struct pt_regs *regs)
  338. {
  339. int ret;
  340. /* Are we from a system call? */
  341. if (canrestart) {
  342. /* If so, check system call restarting.. */
  343. switch (regs->r10) {
  344. case -ERESTART_RESTARTBLOCK:
  345. case -ERESTARTNOHAND:
  346. /* ERESTARTNOHAND means that the syscall should
  347. * only be restarted if there was no handler for
  348. * the signal, and since we only get here if there
  349. * is a handler, we don't restart */
  350. regs->r10 = -EINTR;
  351. break;
  352. case -ERESTARTSYS:
  353. /* ERESTARTSYS means to restart the syscall if
  354. * there is no handler or the handler was
  355. * registered with SA_RESTART */
  356. if (!(ka->sa.sa_flags & SA_RESTART)) {
  357. regs->r10 = -EINTR;
  358. break;
  359. }
  360. /* fallthrough */
  361. case -ERESTARTNOINTR:
  362. /* ERESTARTNOINTR means that the syscall should
  363. * be called again after the signal handler returns. */
  364. RESTART_CRIS_SYS(regs);
  365. }
  366. }
  367. /* Set up the stack frame */
  368. if (ka->sa.sa_flags & SA_SIGINFO)
  369. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  370. else
  371. ret = setup_frame(sig, ka, oldset, regs);
  372. if (ret == 0) {
  373. spin_lock_irq(&current->sighand->siglock);
  374. sigorsets(&current->blocked, &current->blocked,
  375. &ka->sa.sa_mask);
  376. if (!(ka->sa.sa_flags & SA_NODEFER))
  377. sigaddset(&current->blocked, sig);
  378. recalc_sigpending();
  379. spin_unlock_irq(&current->sighand->siglock);
  380. }
  381. return ret;
  382. }
  383. /*
  384. * Note that 'init' is a special process: it doesn't get signals it doesn't
  385. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  386. * mistake.
  387. *
  388. * Also note that the regs structure given here as an argument, is the latest
  389. * pushed pt_regs. It may or may not be the same as the first pushed registers
  390. * when the initial usermode->kernelmode transition took place. Therefore
  391. * we can use user_mode(regs) to see if we came directly from kernel or user
  392. * mode below.
  393. */
  394. void do_signal(int canrestart, struct pt_regs *regs)
  395. {
  396. siginfo_t info;
  397. int signr;
  398. struct k_sigaction ka;
  399. sigset_t *oldset;
  400. /*
  401. * We want the common case to go fast, which
  402. * is why we may in certain cases get here from
  403. * kernel mode. Just return without doing anything
  404. * if so.
  405. */
  406. if (!user_mode(regs))
  407. return;
  408. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  409. oldset = &current->saved_sigmask;
  410. else
  411. oldset = &current->blocked;
  412. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  413. if (signr > 0) {
  414. /* Whee! Actually deliver the signal. */
  415. if (handle_signal(canrestart, signr, &info, &ka,
  416. oldset, regs)) {
  417. /* a signal was successfully delivered; the saved
  418. * sigmask will have been stored in the signal frame,
  419. * and will be restored by sigreturn, so we can simply
  420. * clear the TIF_RESTORE_SIGMASK flag */
  421. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  422. clear_thread_flag(TIF_RESTORE_SIGMASK);
  423. }
  424. return;
  425. }
  426. /* Did we come from a system call? */
  427. if (canrestart) {
  428. /* Restart the system call - no handlers present */
  429. if (regs->r10 == -ERESTARTNOHAND ||
  430. regs->r10 == -ERESTARTSYS ||
  431. regs->r10 == -ERESTARTNOINTR) {
  432. RESTART_CRIS_SYS(regs);
  433. }
  434. if (regs->r10 == -ERESTART_RESTARTBLOCK) {
  435. regs->r10 = __NR_restart_syscall;
  436. regs->irp -= 2;
  437. }
  438. }
  439. /* if there's no signal to deliver, we just put the saved sigmask
  440. * back */
  441. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  442. clear_thread_flag(TIF_RESTORE_SIGMASK);
  443. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  444. }
  445. }