signal.c 9.2 KB

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