signal.c 13 KB

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