signal_32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/sh/kernel/signal.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  8. *
  9. * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
  10. *
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/sched/task_stack.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/tty.h>
  24. #include <linux/elf.h>
  25. #include <linux/personality.h>
  26. #include <linux/binfmts.h>
  27. #include <linux/io.h>
  28. #include <linux/resume_user_mode.h>
  29. #include <asm/ucontext.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/syscalls.h>
  33. #include <asm/fpu.h>
  34. struct fdpic_func_descriptor {
  35. unsigned long text;
  36. unsigned long GOT;
  37. };
  38. /*
  39. * The following define adds a 64 byte gap between the signal
  40. * stack frame and previous contents of the stack. This allows
  41. * frame unwinding in a function epilogue but only if a frame
  42. * pointer is used in the function. This is necessary because
  43. * current gcc compilers (<4.3) do not generate unwind info on
  44. * SH for function epilogues.
  45. */
  46. #define UNWINDGUARD 64
  47. /*
  48. * Do a signal return; undo the signal stack.
  49. */
  50. #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
  51. #if defined(CONFIG_CPU_SH2)
  52. #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
  53. #else
  54. #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
  55. #endif
  56. #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
  57. struct sigframe
  58. {
  59. struct sigcontext sc;
  60. unsigned long extramask[_NSIG_WORDS-1];
  61. u16 retcode[8];
  62. };
  63. struct rt_sigframe
  64. {
  65. struct siginfo info;
  66. struct ucontext uc;
  67. u16 retcode[8];
  68. };
  69. #ifdef CONFIG_SH_FPU
  70. static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
  71. {
  72. struct task_struct *tsk = current;
  73. if (!(boot_cpu_data.flags & CPU_HAS_FPU))
  74. return 0;
  75. set_used_math();
  76. return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
  77. sizeof(long)*(16*2+2));
  78. }
  79. static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
  80. struct pt_regs *regs)
  81. {
  82. struct task_struct *tsk = current;
  83. if (!(boot_cpu_data.flags & CPU_HAS_FPU))
  84. return 0;
  85. if (!used_math())
  86. return __put_user(0, &sc->sc_ownedfp);
  87. if (__put_user(1, &sc->sc_ownedfp))
  88. return -EFAULT;
  89. /* This will cause a "finit" to be triggered by the next
  90. attempted FPU operation by the 'current' process.
  91. */
  92. clear_used_math();
  93. unlazy_fpu(tsk, regs);
  94. return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
  95. sizeof(long)*(16*2+2));
  96. }
  97. #endif /* CONFIG_SH_FPU */
  98. static int
  99. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
  100. {
  101. unsigned int err = 0;
  102. unsigned int sr = regs->sr & ~SR_USER_MASK;
  103. #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
  104. COPY(regs[1]);
  105. COPY(regs[2]); COPY(regs[3]);
  106. COPY(regs[4]); COPY(regs[5]);
  107. COPY(regs[6]); COPY(regs[7]);
  108. COPY(regs[8]); COPY(regs[9]);
  109. COPY(regs[10]); COPY(regs[11]);
  110. COPY(regs[12]); COPY(regs[13]);
  111. COPY(regs[14]); COPY(regs[15]);
  112. COPY(gbr); COPY(mach);
  113. COPY(macl); COPY(pr);
  114. COPY(sr); COPY(pc);
  115. #undef COPY
  116. regs->sr = (regs->sr & SR_USER_MASK) | sr;
  117. #ifdef CONFIG_SH_FPU
  118. if (boot_cpu_data.flags & CPU_HAS_FPU) {
  119. int owned_fp;
  120. struct task_struct *tsk = current;
  121. regs->sr |= SR_FD; /* Release FPU */
  122. clear_fpu(tsk, regs);
  123. clear_used_math();
  124. err |= __get_user (owned_fp, &sc->sc_ownedfp);
  125. if (owned_fp)
  126. err |= restore_sigcontext_fpu(sc);
  127. }
  128. #endif
  129. regs->tra = -1; /* disable syscall checks */
  130. err |= __get_user(*r0_p, &sc->sc_regs[0]);
  131. return err;
  132. }
  133. asmlinkage int sys_sigreturn(void)
  134. {
  135. struct pt_regs *regs = current_pt_regs();
  136. struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
  137. sigset_t set;
  138. int r0;
  139. /* Always make any pending restarted system calls return -EINTR */
  140. current->restart_block.fn = do_no_restart_syscall;
  141. if (!access_ok(frame, sizeof(*frame)))
  142. goto badframe;
  143. if (__get_user(set.sig[0], &frame->sc.oldmask)
  144. || (_NSIG_WORDS > 1
  145. && __copy_from_user(&set.sig[1], &frame->extramask,
  146. sizeof(frame->extramask))))
  147. goto badframe;
  148. set_current_blocked(&set);
  149. if (restore_sigcontext(regs, &frame->sc, &r0))
  150. goto badframe;
  151. return r0;
  152. badframe:
  153. force_sig(SIGSEGV);
  154. return 0;
  155. }
  156. asmlinkage int sys_rt_sigreturn(void)
  157. {
  158. struct pt_regs *regs = current_pt_regs();
  159. struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
  160. sigset_t set;
  161. int r0;
  162. /* Always make any pending restarted system calls return -EINTR */
  163. current->restart_block.fn = do_no_restart_syscall;
  164. if (!access_ok(frame, sizeof(*frame)))
  165. goto badframe;
  166. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  167. goto badframe;
  168. set_current_blocked(&set);
  169. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  170. goto badframe;
  171. if (restore_altstack(&frame->uc.uc_stack))
  172. goto badframe;
  173. return r0;
  174. badframe:
  175. force_sig(SIGSEGV);
  176. return 0;
  177. }
  178. /*
  179. * Set up a signal frame.
  180. */
  181. static int
  182. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  183. unsigned long mask)
  184. {
  185. int err = 0;
  186. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  187. COPY(regs[0]); COPY(regs[1]);
  188. COPY(regs[2]); COPY(regs[3]);
  189. COPY(regs[4]); COPY(regs[5]);
  190. COPY(regs[6]); COPY(regs[7]);
  191. COPY(regs[8]); COPY(regs[9]);
  192. COPY(regs[10]); COPY(regs[11]);
  193. COPY(regs[12]); COPY(regs[13]);
  194. COPY(regs[14]); COPY(regs[15]);
  195. COPY(gbr); COPY(mach);
  196. COPY(macl); COPY(pr);
  197. COPY(sr); COPY(pc);
  198. #undef COPY
  199. #ifdef CONFIG_SH_FPU
  200. err |= save_sigcontext_fpu(sc, regs);
  201. #endif
  202. /* non-iBCS2 extensions.. */
  203. err |= __put_user(mask, &sc->oldmask);
  204. return err;
  205. }
  206. /*
  207. * Determine which stack to use..
  208. */
  209. static inline void __user *
  210. get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
  211. {
  212. if (ka->sa.sa_flags & SA_ONSTACK) {
  213. if (sas_ss_flags(sp) == 0)
  214. sp = current->sas_ss_sp + current->sas_ss_size;
  215. }
  216. return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
  217. }
  218. /* These symbols are defined with the addresses in the vsyscall page.
  219. See vsyscall-trapa.S. */
  220. extern void __kernel_sigreturn(void);
  221. extern void __kernel_rt_sigreturn(void);
  222. static int setup_frame(struct ksignal *ksig, sigset_t *set,
  223. struct pt_regs *regs)
  224. {
  225. struct sigframe __user *frame;
  226. int err = 0, sig = ksig->sig;
  227. frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
  228. if (!access_ok(frame, sizeof(*frame)))
  229. return -EFAULT;
  230. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  231. if (_NSIG_WORDS > 1)
  232. err |= __copy_to_user(frame->extramask, &set->sig[1],
  233. sizeof(frame->extramask));
  234. /* Set up to return from userspace. If provided, use a stub
  235. already in userspace. */
  236. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  237. regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
  238. #ifdef CONFIG_VSYSCALL
  239. } else if (likely(current->mm->context.vdso)) {
  240. regs->pr = VDSO_SYM(&__kernel_sigreturn);
  241. #endif
  242. } else {
  243. /* Generate return code (system call to sigreturn) */
  244. err |= __put_user(MOVW(7), &frame->retcode[0]);
  245. err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
  246. err |= __put_user(OR_R0_R0, &frame->retcode[2]);
  247. err |= __put_user(OR_R0_R0, &frame->retcode[3]);
  248. err |= __put_user(OR_R0_R0, &frame->retcode[4]);
  249. err |= __put_user(OR_R0_R0, &frame->retcode[5]);
  250. err |= __put_user(OR_R0_R0, &frame->retcode[6]);
  251. err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
  252. regs->pr = (unsigned long) frame->retcode;
  253. flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
  254. }
  255. if (err)
  256. return -EFAULT;
  257. /* Set up registers for signal handler */
  258. regs->regs[15] = (unsigned long) frame;
  259. regs->regs[4] = sig; /* Arg for signal handler */
  260. regs->regs[5] = 0;
  261. regs->regs[6] = (unsigned long) &frame->sc;
  262. if (current->personality & FDPIC_FUNCPTRS) {
  263. struct fdpic_func_descriptor __user *funcptr =
  264. (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
  265. err |= __get_user(regs->pc, &funcptr->text);
  266. err |= __get_user(regs->regs[12], &funcptr->GOT);
  267. } else
  268. regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
  269. if (err)
  270. return -EFAULT;
  271. pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
  272. current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
  273. return 0;
  274. }
  275. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  276. struct pt_regs *regs)
  277. {
  278. struct rt_sigframe __user *frame;
  279. int err = 0, sig = ksig->sig;
  280. frame = get_sigframe(&ksig->ka, regs->regs[15], sizeof(*frame));
  281. if (!access_ok(frame, sizeof(*frame)))
  282. return -EFAULT;
  283. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  284. /* Create the ucontext. */
  285. err |= __put_user(0, &frame->uc.uc_flags);
  286. err |= __put_user(NULL, &frame->uc.uc_link);
  287. err |= __save_altstack(&frame->uc.uc_stack, regs->regs[15]);
  288. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  289. regs, set->sig[0]);
  290. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  291. /* Set up to return from userspace. If provided, use a stub
  292. already in userspace. */
  293. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  294. regs->pr = (unsigned long) ksig->ka.sa.sa_restorer;
  295. #ifdef CONFIG_VSYSCALL
  296. } else if (likely(current->mm->context.vdso)) {
  297. regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
  298. #endif
  299. } else {
  300. /* Generate return code (system call to rt_sigreturn) */
  301. err |= __put_user(MOVW(7), &frame->retcode[0]);
  302. err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
  303. err |= __put_user(OR_R0_R0, &frame->retcode[2]);
  304. err |= __put_user(OR_R0_R0, &frame->retcode[3]);
  305. err |= __put_user(OR_R0_R0, &frame->retcode[4]);
  306. err |= __put_user(OR_R0_R0, &frame->retcode[5]);
  307. err |= __put_user(OR_R0_R0, &frame->retcode[6]);
  308. err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
  309. regs->pr = (unsigned long) frame->retcode;
  310. flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
  311. }
  312. if (err)
  313. return -EFAULT;
  314. /* Set up registers for signal handler */
  315. regs->regs[15] = (unsigned long) frame;
  316. regs->regs[4] = sig; /* Arg for signal handler */
  317. regs->regs[5] = (unsigned long) &frame->info;
  318. regs->regs[6] = (unsigned long) &frame->uc;
  319. if (current->personality & FDPIC_FUNCPTRS) {
  320. struct fdpic_func_descriptor __user *funcptr =
  321. (struct fdpic_func_descriptor __user *)ksig->ka.sa.sa_handler;
  322. err |= __get_user(regs->pc, &funcptr->text);
  323. err |= __get_user(regs->regs[12], &funcptr->GOT);
  324. } else
  325. regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
  326. if (err)
  327. return -EFAULT;
  328. pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
  329. current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
  330. return 0;
  331. }
  332. static inline void
  333. handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
  334. struct sigaction *sa)
  335. {
  336. /* If we're not from a syscall, bail out */
  337. if (regs->tra < 0)
  338. return;
  339. /* check for system call restart.. */
  340. switch (regs->regs[0]) {
  341. case -ERESTART_RESTARTBLOCK:
  342. case -ERESTARTNOHAND:
  343. no_system_call_restart:
  344. regs->regs[0] = -EINTR;
  345. break;
  346. case -ERESTARTSYS:
  347. if (!(sa->sa_flags & SA_RESTART))
  348. goto no_system_call_restart;
  349. fallthrough;
  350. case -ERESTARTNOINTR:
  351. regs->regs[0] = save_r0;
  352. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  353. break;
  354. }
  355. }
  356. /*
  357. * OK, we're invoking a handler
  358. */
  359. static void
  360. handle_signal(struct ksignal *ksig, struct pt_regs *regs, unsigned int save_r0)
  361. {
  362. sigset_t *oldset = sigmask_to_save();
  363. int ret;
  364. /* Set up the stack frame */
  365. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  366. ret = setup_rt_frame(ksig, oldset, regs);
  367. else
  368. ret = setup_frame(ksig, oldset, regs);
  369. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  370. }
  371. /*
  372. * Note that 'init' is a special process: it doesn't get signals it doesn't
  373. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  374. * mistake.
  375. *
  376. * Note that we go through the signals twice: once to check the signals that
  377. * the kernel can handle, and then we build all the user-level signal handling
  378. * stack-frames in one go after that.
  379. */
  380. static void do_signal(struct pt_regs *regs, unsigned int save_r0)
  381. {
  382. struct ksignal ksig;
  383. /*
  384. * We want the common case to go fast, which
  385. * is why we may in certain cases get here from
  386. * kernel mode. Just return without doing anything
  387. * if so.
  388. */
  389. if (!user_mode(regs))
  390. return;
  391. if (get_signal(&ksig)) {
  392. handle_syscall_restart(save_r0, regs, &ksig.ka.sa);
  393. /* Whee! Actually deliver the signal. */
  394. handle_signal(&ksig, regs, save_r0);
  395. return;
  396. }
  397. /* Did we come from a system call? */
  398. if (regs->tra >= 0) {
  399. /* Restart the system call - no handlers present */
  400. if (regs->regs[0] == -ERESTARTNOHAND ||
  401. regs->regs[0] == -ERESTARTSYS ||
  402. regs->regs[0] == -ERESTARTNOINTR) {
  403. regs->regs[0] = save_r0;
  404. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  405. } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
  406. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  407. regs->regs[3] = __NR_restart_syscall;
  408. }
  409. }
  410. /*
  411. * If there's no signal to deliver, we just put the saved sigmask
  412. * back.
  413. */
  414. restore_saved_sigmask();
  415. }
  416. asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
  417. unsigned long thread_info_flags)
  418. {
  419. /* deal with pending signal delivery */
  420. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
  421. do_signal(regs, save_r0);
  422. if (thread_info_flags & _TIF_NOTIFY_RESUME)
  423. resume_user_mode_work(regs);
  424. }