signal.c 8.3 KB

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