signal.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_thread_info()->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. /* TODO what is the current->exec_domain stuff and invmap ? */
  159. /* Set up registers for signal handler */
  160. regs->pc = (unsigned long)ksig->ka.sa.sa_handler; /* what we enter NOW */
  161. regs->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
  162. regs->gpr[3] = (unsigned long)ksig->sig; /* arg 1: signo */
  163. regs->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
  164. regs->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
  165. /* actually move the usp to reflect the stacked frame */
  166. regs->sp = (unsigned long)frame;
  167. return 0;
  168. }
  169. static inline void
  170. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  171. {
  172. int ret;
  173. ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
  174. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  175. }
  176. /*
  177. * Note that 'init' is a special process: it doesn't get signals it doesn't
  178. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  179. * mistake.
  180. *
  181. * Also note that the regs structure given here as an argument, is the latest
  182. * pushed pt_regs. It may or may not be the same as the first pushed registers
  183. * when the initial usermode->kernelmode transition took place. Therefore
  184. * we can use user_mode(regs) to see if we came directly from kernel or user
  185. * mode below.
  186. */
  187. int do_signal(struct pt_regs *regs, int syscall)
  188. {
  189. struct ksignal ksig;
  190. unsigned long continue_addr = 0;
  191. unsigned long restart_addr = 0;
  192. unsigned long retval = 0;
  193. int restart = 0;
  194. if (syscall) {
  195. continue_addr = regs->pc;
  196. restart_addr = continue_addr - 4;
  197. retval = regs->gpr[11];
  198. /*
  199. * Setup syscall restart here so that a debugger will
  200. * see the already changed PC.
  201. */
  202. switch (retval) {
  203. case -ERESTART_RESTARTBLOCK:
  204. restart = -2;
  205. /* Fall through */
  206. case -ERESTARTNOHAND:
  207. case -ERESTARTSYS:
  208. case -ERESTARTNOINTR:
  209. restart++;
  210. regs->gpr[11] = regs->orig_gpr11;
  211. regs->pc = restart_addr;
  212. break;
  213. }
  214. }
  215. /*
  216. * Get the signal to deliver. During the call to get_signal the
  217. * debugger may change all our registers so we may need to revert
  218. * the decision to restart the syscall; specifically, if the PC is
  219. * changed, don't restart the syscall.
  220. */
  221. if (get_signal(&ksig)) {
  222. if (unlikely(restart) && regs->pc == restart_addr) {
  223. if (retval == -ERESTARTNOHAND ||
  224. retval == -ERESTART_RESTARTBLOCK
  225. || (retval == -ERESTARTSYS
  226. && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
  227. /* No automatic restart */
  228. regs->gpr[11] = -EINTR;
  229. regs->pc = continue_addr;
  230. }
  231. }
  232. handle_signal(&ksig, regs);
  233. } else {
  234. /* no handler */
  235. restore_saved_sigmask();
  236. /*
  237. * Restore pt_regs PC as syscall restart will be handled by
  238. * kernel without return to userspace
  239. */
  240. if (unlikely(restart) && regs->pc == restart_addr) {
  241. regs->pc = continue_addr;
  242. return restart;
  243. }
  244. }
  245. return 0;
  246. }
  247. asmlinkage int
  248. do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
  249. {
  250. do {
  251. if (likely(thread_flags & _TIF_NEED_RESCHED)) {
  252. schedule();
  253. } else {
  254. if (unlikely(!user_mode(regs)))
  255. return 0;
  256. local_irq_enable();
  257. if (thread_flags & _TIF_SIGPENDING) {
  258. int restart = do_signal(regs, syscall);
  259. if (unlikely(restart)) {
  260. /*
  261. * Restart without handlers.
  262. * Deal with it without leaving
  263. * the kernel space.
  264. */
  265. return restart;
  266. }
  267. syscall = 0;
  268. } else {
  269. clear_thread_flag(TIF_NOTIFY_RESUME);
  270. tracehook_notify_resume(regs);
  271. }
  272. }
  273. local_irq_disable();
  274. thread_flags = current_thread_info()->flags;
  275. } while (thread_flags & _TIF_WORK_MASK);
  276. return 0;
  277. }