signal.c 16 KB

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