signal.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * OpenRISC signal.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/kernel.h>
  21. #include <linux/signal.h>
  22. #include <linux/errno.h>
  23. #include <linux/wait.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/unistd.h>
  26. #include <linux/stddef.h>
  27. #include <linux/tracehook.h>
  28. #include <asm/processor.h>
  29. #include <asm/ucontext.h>
  30. #include <asm/uaccess.h>
  31. #define DEBUG_SIG 0
  32. asmlinkage long
  33. _sys_sigaltstack(const stack_t *uss, stack_t *uoss, struct pt_regs *regs)
  34. {
  35. return do_sigaltstack(uss, uoss, regs->sp);
  36. }
  37. struct rt_sigframe {
  38. struct siginfo *pinfo;
  39. void *puc;
  40. struct siginfo info;
  41. struct ucontext uc;
  42. unsigned char retcode[16]; /* trampoline code */
  43. };
  44. static int restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
  45. {
  46. unsigned int err = 0;
  47. /* Alwys make any pending restarted system call return -EINTR */
  48. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  49. /*
  50. * Restore the regs from &sc->regs.
  51. * (sc is already checked for VERIFY_READ since the sigframe was
  52. * checked in sys_sigreturn previously)
  53. */
  54. if (__copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long)))
  55. goto badframe;
  56. if (__copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long)))
  57. goto badframe;
  58. if (__copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long)))
  59. goto badframe;
  60. /* make sure the SM-bit is cleared so user-mode cannot fool us */
  61. regs->sr &= ~SPR_SR_SM;
  62. /* TODO: the other ports use regs->orig_XX to disable syscall checks
  63. * after this completes, but we don't use that mechanism. maybe we can
  64. * use it now ?
  65. */
  66. return err;
  67. badframe:
  68. return 1;
  69. }
  70. asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs)
  71. {
  72. struct rt_sigframe *frame = (struct rt_sigframe __user *)regs->sp;
  73. sigset_t set;
  74. stack_t st;
  75. /*
  76. * Since we stacked the signal on a dword boundary,
  77. * then frame should be dword aligned here. If it's
  78. * not, then the user is trying to mess with us.
  79. */
  80. if (((long)frame) & 3)
  81. goto badframe;
  82. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  83. goto badframe;
  84. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  85. goto badframe;
  86. set_current_blocked(&set);
  87. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  88. goto badframe;
  89. if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
  90. goto badframe;
  91. /* It is more difficult to avoid calling this function than to
  92. call it and ignore errors. */
  93. do_sigaltstack(&st, NULL, regs->sp);
  94. return regs->gpr[11];
  95. badframe:
  96. force_sig(SIGSEGV, current);
  97. return 0;
  98. }
  99. /*
  100. * Set up a signal frame.
  101. */
  102. static int setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs,
  103. unsigned long mask)
  104. {
  105. int err = 0;
  106. /* copy the regs */
  107. err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
  108. err |= __copy_to_user(&sc->regs.pc, &regs->pc, sizeof(unsigned long));
  109. err |= __copy_to_user(&sc->regs.sr, &regs->sr, sizeof(unsigned long));
  110. /* then some other stuff */
  111. err |= __put_user(mask, &sc->oldmask);
  112. return err;
  113. }
  114. static inline unsigned long align_sigframe(unsigned long sp)
  115. {
  116. return sp & ~3UL;
  117. }
  118. /*
  119. * Work out where the signal frame should go. It's either on the user stack
  120. * or the alternate stack.
  121. */
  122. static inline void __user *get_sigframe(struct k_sigaction *ka,
  123. struct pt_regs *regs, size_t frame_size)
  124. {
  125. unsigned long sp = regs->sp;
  126. int onsigstack = on_sig_stack(sp);
  127. /* redzone */
  128. sp -= STACK_FRAME_OVERHEAD;
  129. /* This is the X/Open sanctioned signal stack switching. */
  130. if ((ka->sa.sa_flags & SA_ONSTACK) && !onsigstack) {
  131. if (current->sas_ss_size)
  132. sp = current->sas_ss_sp + current->sas_ss_size;
  133. }
  134. sp = align_sigframe(sp - frame_size);
  135. /*
  136. * If we are on the alternate signal stack and would overflow it, don't.
  137. * Return an always-bogus address instead so we will die with SIGSEGV.
  138. */
  139. if (onsigstack && !likely(on_sig_stack(sp)))
  140. return (void __user *)-1L;
  141. return (void __user *)sp;
  142. }
  143. /* grab and setup a signal frame.
  144. *
  145. * basically we stack a lot of state info, and arrange for the
  146. * user-mode program to return to the kernel using either a
  147. * trampoline which performs the syscall sigreturn, or a provided
  148. * user-mode trampoline.
  149. */
  150. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  151. sigset_t *set, struct pt_regs *regs)
  152. {
  153. struct rt_sigframe *frame;
  154. unsigned long return_ip;
  155. int err = 0;
  156. frame = get_sigframe(ka, regs, sizeof(*frame));
  157. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  158. goto give_sigsegv;
  159. err |= __put_user(&frame->info, &frame->pinfo);
  160. err |= __put_user(&frame->uc, &frame->puc);
  161. if (ka->sa.sa_flags & SA_SIGINFO)
  162. err |= copy_siginfo_to_user(&frame->info, info);
  163. if (err)
  164. goto give_sigsegv;
  165. /* Clear all the bits of the ucontext we don't use. */
  166. err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
  167. err |= __put_user(0, &frame->uc.uc_flags);
  168. err |= __put_user(NULL, &frame->uc.uc_link);
  169. err |= __put_user((void *)current->sas_ss_sp,
  170. &frame->uc.uc_stack.ss_sp);
  171. err |= __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags);
  172. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  173. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  174. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  175. if (err)
  176. goto give_sigsegv;
  177. /* trampoline - the desired return ip is the retcode itself */
  178. return_ip = (unsigned long)&frame->retcode;
  179. /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
  180. err |= __put_user(0xa960, (short *)(frame->retcode + 0));
  181. err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode + 2));
  182. err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
  183. err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
  184. if (err)
  185. goto give_sigsegv;
  186. /* TODO what is the current->exec_domain stuff and invmap ? */
  187. /* Set up registers for signal handler */
  188. regs->pc = (unsigned long)ka->sa.sa_handler; /* what we enter NOW */
  189. regs->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
  190. regs->gpr[3] = (unsigned long)sig; /* arg 1: signo */
  191. regs->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
  192. regs->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
  193. /* actually move the usp to reflect the stacked frame */
  194. regs->sp = (unsigned long)frame;
  195. return 0;
  196. give_sigsegv:
  197. force_sigsegv(sig, current);
  198. return -EFAULT;
  199. }
  200. static inline void
  201. handle_signal(unsigned long sig,
  202. siginfo_t *info, struct k_sigaction *ka,
  203. struct pt_regs *regs)
  204. {
  205. int ret;
  206. ret = setup_rt_frame(sig, ka, info, sigmask_to_save(), regs);
  207. if (ret)
  208. return;
  209. signal_delivered(sig, info, ka, regs,
  210. test_thread_flag(TIF_SINGLESTEP));
  211. }
  212. /*
  213. * Note that 'init' is a special process: it doesn't get signals it doesn't
  214. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  215. * mistake.
  216. *
  217. * Also note that the regs structure given here as an argument, is the latest
  218. * pushed pt_regs. It may or may not be the same as the first pushed registers
  219. * when the initial usermode->kernelmode transition took place. Therefore
  220. * we can use user_mode(regs) to see if we came directly from kernel or user
  221. * mode below.
  222. */
  223. void do_signal(struct pt_regs *regs)
  224. {
  225. siginfo_t info;
  226. int signr;
  227. struct k_sigaction ka;
  228. /*
  229. * We want the common case to go fast, which
  230. * is why we may in certain cases get here from
  231. * kernel mode. Just return without doing anything
  232. * if so.
  233. */
  234. if (!user_mode(regs))
  235. return;
  236. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  237. /* If we are coming out of a syscall then we need
  238. * to check if the syscall was interrupted and wants to be
  239. * restarted after handling the signal. If so, the original
  240. * syscall number is put back into r11 and the PC rewound to
  241. * point at the l.sys instruction that resulted in the
  242. * original syscall. Syscall results other than the four
  243. * below mean that the syscall executed to completion and no
  244. * restart is necessary.
  245. */
  246. if (regs->orig_gpr11) {
  247. int restart = 0;
  248. switch (regs->gpr[11]) {
  249. case -ERESTART_RESTARTBLOCK:
  250. case -ERESTARTNOHAND:
  251. /* Restart if there is no signal handler */
  252. restart = (signr <= 0);
  253. break;
  254. case -ERESTARTSYS:
  255. /* Restart if there no signal handler or
  256. * SA_RESTART flag is set */
  257. restart = (signr <= 0 || (ka.sa.sa_flags & SA_RESTART));
  258. break;
  259. case -ERESTARTNOINTR:
  260. /* Always restart */
  261. restart = 1;
  262. break;
  263. }
  264. if (restart) {
  265. if (regs->gpr[11] == -ERESTART_RESTARTBLOCK)
  266. regs->gpr[11] = __NR_restart_syscall;
  267. else
  268. regs->gpr[11] = regs->orig_gpr11;
  269. regs->pc -= 4;
  270. } else {
  271. regs->gpr[11] = -EINTR;
  272. }
  273. }
  274. if (signr <= 0) {
  275. /* no signal to deliver so we just put the saved sigmask
  276. * back */
  277. restore_saved_sigmask();
  278. } else { /* signr > 0 */
  279. /* Whee! Actually deliver the signal. */
  280. handle_signal(signr, &info, &ka, regs);
  281. }
  282. return;
  283. }
  284. asmlinkage void do_notify_resume(struct pt_regs *regs)
  285. {
  286. if (current_thread_info()->flags & _TIF_SIGPENDING)
  287. do_signal(regs);
  288. if (current_thread_info()->flags & _TIF_NOTIFY_RESUME) {
  289. clear_thread_flag(TIF_NOTIFY_RESUME);
  290. tracehook_notify_resume(regs);
  291. }
  292. }