signal.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/syscall.h>
  30. #include <asm/ucontext.h>
  31. #include <asm/uaccess.h>
  32. #define DEBUG_SIG 0
  33. struct rt_sigframe {
  34. struct siginfo info;
  35. struct ucontext uc;
  36. unsigned char retcode[16]; /* trampoline code */
  37. };
  38. static int restore_sigcontext(struct pt_regs *regs,
  39. struct sigcontext __user *sc)
  40. {
  41. int err = 0;
  42. /* Always make any pending restarted system calls return -EINTR */
  43. current->restart_block.fn = do_no_restart_syscall;
  44. /*
  45. * Restore the regs from &sc->regs.
  46. * (sc is already checked for VERIFY_READ since the sigframe was
  47. * checked in sys_sigreturn previously)
  48. */
  49. err |= __copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long));
  50. err |= __copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long));
  51. err |= __copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long));
  52. /* make sure the SM-bit is cleared so user-mode cannot fool us */
  53. regs->sr &= ~SPR_SR_SM;
  54. regs->orig_gpr11 = -1; /* Avoid syscall restart checks */
  55. /* TODO: the other ports use regs->orig_XX to disable syscall checks
  56. * after this completes, but we don't use that mechanism. maybe we can
  57. * use it now ?
  58. */
  59. return err;
  60. }
  61. asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs)
  62. {
  63. struct rt_sigframe *frame = (struct rt_sigframe __user *)regs->sp;
  64. sigset_t set;
  65. /*
  66. * Since we stacked the signal on a dword boundary,
  67. * then frame should be dword aligned here. If it's
  68. * not, then the user is trying to mess with us.
  69. */
  70. if (((long)frame) & 3)
  71. goto badframe;
  72. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  73. goto badframe;
  74. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  75. goto badframe;
  76. set_current_blocked(&set);
  77. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  78. goto badframe;
  79. if (restore_altstack(&frame->uc.uc_stack))
  80. goto badframe;
  81. return regs->gpr[11];
  82. badframe:
  83. force_sig(SIGSEGV, current);
  84. return 0;
  85. }
  86. /*
  87. * Set up a signal frame.
  88. */
  89. static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  90. {
  91. int err = 0;
  92. /* copy the regs */
  93. /* There should be no need to save callee-saved registers here...
  94. * ...but we save them anyway. Revisit this
  95. */
  96. err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
  97. err |= __copy_to_user(&sc->regs.pc, &regs->pc, sizeof(unsigned long));
  98. err |= __copy_to_user(&sc->regs.sr, &regs->sr, sizeof(unsigned long));
  99. return err;
  100. }
  101. static inline unsigned long align_sigframe(unsigned long sp)
  102. {
  103. return sp & ~3UL;
  104. }
  105. /*
  106. * Work out where the signal frame should go. It's either on the user stack
  107. * or the alternate stack.
  108. */
  109. static inline void __user *get_sigframe(struct ksignal *ksig,
  110. struct pt_regs *regs, size_t frame_size)
  111. {
  112. unsigned long sp = regs->sp;
  113. /* redzone */
  114. sp -= STACK_FRAME_OVERHEAD;
  115. sp = sigsp(sp, ksig);
  116. sp = align_sigframe(sp - frame_size);
  117. return (void __user *)sp;
  118. }
  119. /* grab and setup a signal frame.
  120. *
  121. * basically we stack a lot of state info, and arrange for the
  122. * user-mode program to return to the kernel using either a
  123. * trampoline which performs the syscall sigreturn, or a provided
  124. * user-mode trampoline.
  125. */
  126. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  127. struct pt_regs *regs)
  128. {
  129. struct rt_sigframe *frame;
  130. unsigned long return_ip;
  131. int err = 0;
  132. frame = get_sigframe(ksig, regs, sizeof(*frame));
  133. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  134. return -EFAULT;
  135. /* Create siginfo. */
  136. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  137. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  138. /* Create the ucontext. */
  139. err |= __put_user(0, &frame->uc.uc_flags);
  140. err |= __put_user(NULL, &frame->uc.uc_link);
  141. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  142. err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
  143. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  144. if (err)
  145. return -EFAULT;
  146. /* trampoline - the desired return ip is the retcode itself */
  147. return_ip = (unsigned long)&frame->retcode;
  148. /* This is:
  149. l.ori r11,r0,__NR_sigreturn
  150. l.sys 1
  151. */
  152. err |= __put_user(0xa960, (short *)(frame->retcode + 0));
  153. err |= __put_user(__NR_rt_sigreturn, (short *)(frame->retcode + 2));
  154. err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
  155. err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
  156. if (err)
  157. return -EFAULT;
  158. /* Set up registers for signal handler */
  159. regs->pc = (unsigned long)ksig->ka.sa.sa_handler; /* what we enter NOW */
  160. regs->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
  161. regs->gpr[3] = (unsigned long)ksig->sig; /* arg 1: signo */
  162. regs->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
  163. regs->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
  164. /* actually move the usp to reflect the stacked frame */
  165. regs->sp = (unsigned long)frame;
  166. return 0;
  167. }
  168. static inline void
  169. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  170. {
  171. int ret;
  172. ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
  173. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  174. }
  175. /*
  176. * Note that 'init' is a special process: it doesn't get signals it doesn't
  177. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  178. * mistake.
  179. *
  180. * Also note that the regs structure given here as an argument, is the latest
  181. * pushed pt_regs. It may or may not be the same as the first pushed registers
  182. * when the initial usermode->kernelmode transition took place. Therefore
  183. * we can use user_mode(regs) to see if we came directly from kernel or user
  184. * mode below.
  185. */
  186. int do_signal(struct pt_regs *regs, int syscall)
  187. {
  188. struct ksignal ksig;
  189. unsigned long continue_addr = 0;
  190. unsigned long restart_addr = 0;
  191. unsigned long retval = 0;
  192. int restart = 0;
  193. if (syscall) {
  194. continue_addr = regs->pc;
  195. restart_addr = continue_addr - 4;
  196. retval = regs->gpr[11];
  197. /*
  198. * Setup syscall restart here so that a debugger will
  199. * see the already changed PC.
  200. */
  201. switch (retval) {
  202. case -ERESTART_RESTARTBLOCK:
  203. restart = -2;
  204. /* Fall through */
  205. case -ERESTARTNOHAND:
  206. case -ERESTARTSYS:
  207. case -ERESTARTNOINTR:
  208. restart++;
  209. regs->gpr[11] = regs->orig_gpr11;
  210. regs->pc = restart_addr;
  211. break;
  212. }
  213. }
  214. /*
  215. * Get the signal to deliver. During the call to get_signal the
  216. * debugger may change all our registers so we may need to revert
  217. * the decision to restart the syscall; specifically, if the PC is
  218. * changed, don't restart the syscall.
  219. */
  220. if (get_signal(&ksig)) {
  221. if (unlikely(restart) && regs->pc == restart_addr) {
  222. if (retval == -ERESTARTNOHAND ||
  223. retval == -ERESTART_RESTARTBLOCK
  224. || (retval == -ERESTARTSYS
  225. && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
  226. /* No automatic restart */
  227. regs->gpr[11] = -EINTR;
  228. regs->pc = continue_addr;
  229. }
  230. }
  231. handle_signal(&ksig, regs);
  232. } else {
  233. /* no handler */
  234. restore_saved_sigmask();
  235. /*
  236. * Restore pt_regs PC as syscall restart will be handled by
  237. * kernel without return to userspace
  238. */
  239. if (unlikely(restart) && regs->pc == restart_addr) {
  240. regs->pc = continue_addr;
  241. return restart;
  242. }
  243. }
  244. return 0;
  245. }
  246. asmlinkage int
  247. do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  248. {
  249. do {
  250. if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  251. schedule();
  252. } else {
  253. if (unlikely(!user_mode(regs)))
  254. return 0;
  255. local_irq_enable();
  256. if (thread_flags & _TIF_SIGPENDING) {
  257. int restart = do_signal(regs, syscall);
  258. if (unlikely(restart)) {
  259. /*
  260. * Restart without handlers.
  261. * Deal with it without leaving
  262. * the kernel space.
  263. */
  264. return restart;
  265. }
  266. syscall = 0;
  267. } else {
  268. clear_thread_flag(TIF_NOTIFY_RESUME);
  269. tracehook_notify_resume(regs);
  270. }
  271. }
  272. local_irq_disable();
  273. thread_flags = current_thread_info()->flags;
  274. } while (thread_flags & _TIF_WORK_MASK);
  275. return 0;
  276. }