signal.c 11 KB

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