signal_32.c 13 KB

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