signal_32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * linux/arch/sh/kernel/signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  7. *
  8. * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
  9. *
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/signal.h>
  16. #include <linux/errno.h>
  17. #include <linux/wait.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/unistd.h>
  20. #include <linux/stddef.h>
  21. #include <linux/tty.h>
  22. #include <linux/elf.h>
  23. #include <linux/personality.h>
  24. #include <linux/binfmts.h>
  25. #include <linux/freezer.h>
  26. #include <linux/io.h>
  27. #include <linux/tracehook.h>
  28. #include <asm/ucontext.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.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. * Atomically swap in the new signal mask, and wait for a signal.
  49. */
  50. asmlinkage int
  51. sys_sigsuspend(old_sigset_t mask)
  52. {
  53. sigset_t blocked;
  54. siginitset(&blocked, mask);
  55. return sigsuspend(&blocked);
  56. }
  57. asmlinkage int
  58. sys_sigaction(int sig, const struct old_sigaction __user *act,
  59. struct old_sigaction __user *oact)
  60. {
  61. struct k_sigaction new_ka, old_ka;
  62. int ret;
  63. if (act) {
  64. old_sigset_t mask;
  65. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  66. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  67. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
  68. __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
  69. __get_user(mask, &act->sa_mask))
  70. return -EFAULT;
  71. siginitset(&new_ka.sa.sa_mask, mask);
  72. }
  73. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  74. if (!ret && oact) {
  75. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  76. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  77. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
  78. __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
  79. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
  80. return -EFAULT;
  81. }
  82. return ret;
  83. }
  84. asmlinkage int
  85. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  86. unsigned long r6, unsigned long r7,
  87. struct pt_regs __regs)
  88. {
  89. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  90. return do_sigaltstack(uss, uoss, regs->regs[15]);
  91. }
  92. /*
  93. * Do a signal return; undo the signal stack.
  94. */
  95. #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
  96. #if defined(CONFIG_CPU_SH2)
  97. #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
  98. #else
  99. #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
  100. #endif
  101. #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
  102. struct sigframe
  103. {
  104. struct sigcontext sc;
  105. unsigned long extramask[_NSIG_WORDS-1];
  106. u16 retcode[8];
  107. };
  108. struct rt_sigframe
  109. {
  110. struct siginfo info;
  111. struct ucontext uc;
  112. u16 retcode[8];
  113. };
  114. #ifdef CONFIG_SH_FPU
  115. static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
  116. {
  117. struct task_struct *tsk = current;
  118. if (!(boot_cpu_data.flags & CPU_HAS_FPU))
  119. return 0;
  120. set_used_math();
  121. return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
  122. sizeof(long)*(16*2+2));
  123. }
  124. static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
  125. struct pt_regs *regs)
  126. {
  127. struct task_struct *tsk = current;
  128. if (!(boot_cpu_data.flags & CPU_HAS_FPU))
  129. return 0;
  130. if (!used_math())
  131. return __put_user(0, &sc->sc_ownedfp);
  132. if (__put_user(1, &sc->sc_ownedfp))
  133. return -EFAULT;
  134. /* This will cause a "finit" to be triggered by the next
  135. attempted FPU operation by the 'current' process.
  136. */
  137. clear_used_math();
  138. unlazy_fpu(tsk, regs);
  139. return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
  140. sizeof(long)*(16*2+2));
  141. }
  142. #endif /* CONFIG_SH_FPU */
  143. static int
  144. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
  145. {
  146. unsigned int err = 0;
  147. #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
  148. COPY(regs[1]);
  149. COPY(regs[2]); COPY(regs[3]);
  150. COPY(regs[4]); COPY(regs[5]);
  151. COPY(regs[6]); COPY(regs[7]);
  152. COPY(regs[8]); COPY(regs[9]);
  153. COPY(regs[10]); COPY(regs[11]);
  154. COPY(regs[12]); COPY(regs[13]);
  155. COPY(regs[14]); COPY(regs[15]);
  156. COPY(gbr); COPY(mach);
  157. COPY(macl); COPY(pr);
  158. COPY(sr); COPY(pc);
  159. #undef COPY
  160. #ifdef CONFIG_SH_FPU
  161. if (boot_cpu_data.flags & CPU_HAS_FPU) {
  162. int owned_fp;
  163. struct task_struct *tsk = current;
  164. regs->sr |= SR_FD; /* Release FPU */
  165. clear_fpu(tsk, regs);
  166. clear_used_math();
  167. err |= __get_user (owned_fp, &sc->sc_ownedfp);
  168. if (owned_fp)
  169. err |= restore_sigcontext_fpu(sc);
  170. }
  171. #endif
  172. regs->tra = -1; /* disable syscall checks */
  173. err |= __get_user(*r0_p, &sc->sc_regs[0]);
  174. return err;
  175. }
  176. asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
  177. unsigned long r6, unsigned long r7,
  178. struct pt_regs __regs)
  179. {
  180. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  181. struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
  182. sigset_t set;
  183. int r0;
  184. /* Always make any pending restarted system calls return -EINTR */
  185. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  186. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  187. goto badframe;
  188. if (__get_user(set.sig[0], &frame->sc.oldmask)
  189. || (_NSIG_WORDS > 1
  190. && __copy_from_user(&set.sig[1], &frame->extramask,
  191. sizeof(frame->extramask))))
  192. goto badframe;
  193. set_current_blocked(&set);
  194. if (restore_sigcontext(regs, &frame->sc, &r0))
  195. goto badframe;
  196. return r0;
  197. badframe:
  198. force_sig(SIGSEGV, current);
  199. return 0;
  200. }
  201. asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
  202. unsigned long r6, unsigned long r7,
  203. struct pt_regs __regs)
  204. {
  205. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  206. struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
  207. sigset_t set;
  208. int r0;
  209. /* Always make any pending restarted system calls return -EINTR */
  210. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  211. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  212. goto badframe;
  213. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  214. goto badframe;
  215. set_current_blocked(&set);
  216. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
  217. goto badframe;
  218. if (do_sigaltstack(&frame->uc.uc_stack, NULL,
  219. regs->regs[15]) == -EFAULT)
  220. goto badframe;
  221. return r0;
  222. badframe:
  223. force_sig(SIGSEGV, current);
  224. return 0;
  225. }
  226. /*
  227. * Set up a signal frame.
  228. */
  229. static int
  230. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  231. unsigned long mask)
  232. {
  233. int err = 0;
  234. #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
  235. COPY(regs[0]); COPY(regs[1]);
  236. COPY(regs[2]); COPY(regs[3]);
  237. COPY(regs[4]); COPY(regs[5]);
  238. COPY(regs[6]); COPY(regs[7]);
  239. COPY(regs[8]); COPY(regs[9]);
  240. COPY(regs[10]); COPY(regs[11]);
  241. COPY(regs[12]); COPY(regs[13]);
  242. COPY(regs[14]); COPY(regs[15]);
  243. COPY(gbr); COPY(mach);
  244. COPY(macl); COPY(pr);
  245. COPY(sr); COPY(pc);
  246. #undef COPY
  247. #ifdef CONFIG_SH_FPU
  248. err |= save_sigcontext_fpu(sc, regs);
  249. #endif
  250. /* non-iBCS2 extensions.. */
  251. err |= __put_user(mask, &sc->oldmask);
  252. return err;
  253. }
  254. /*
  255. * Determine which stack to use..
  256. */
  257. static inline void __user *
  258. get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
  259. {
  260. if (ka->sa.sa_flags & SA_ONSTACK) {
  261. if (sas_ss_flags(sp) == 0)
  262. sp = current->sas_ss_sp + current->sas_ss_size;
  263. }
  264. return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
  265. }
  266. /* These symbols are defined with the addresses in the vsyscall page.
  267. See vsyscall-trapa.S. */
  268. extern void __kernel_sigreturn(void);
  269. extern void __kernel_rt_sigreturn(void);
  270. static int setup_frame(int sig, struct k_sigaction *ka,
  271. sigset_t *set, struct pt_regs *regs)
  272. {
  273. struct sigframe __user *frame;
  274. int err = 0;
  275. int signal;
  276. frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
  277. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  278. goto give_sigsegv;
  279. signal = current_thread_info()->exec_domain
  280. && current_thread_info()->exec_domain->signal_invmap
  281. && sig < 32
  282. ? current_thread_info()->exec_domain->signal_invmap[sig]
  283. : sig;
  284. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  285. if (_NSIG_WORDS > 1)
  286. err |= __copy_to_user(frame->extramask, &set->sig[1],
  287. sizeof(frame->extramask));
  288. /* Set up to return from userspace. If provided, use a stub
  289. already in userspace. */
  290. if (ka->sa.sa_flags & SA_RESTORER) {
  291. regs->pr = (unsigned long) ka->sa.sa_restorer;
  292. #ifdef CONFIG_VSYSCALL
  293. } else if (likely(current->mm->context.vdso)) {
  294. regs->pr = VDSO_SYM(&__kernel_sigreturn);
  295. #endif
  296. } else {
  297. /* Generate return code (system call to sigreturn) */
  298. err |= __put_user(MOVW(7), &frame->retcode[0]);
  299. err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
  300. err |= __put_user(OR_R0_R0, &frame->retcode[2]);
  301. err |= __put_user(OR_R0_R0, &frame->retcode[3]);
  302. err |= __put_user(OR_R0_R0, &frame->retcode[4]);
  303. err |= __put_user(OR_R0_R0, &frame->retcode[5]);
  304. err |= __put_user(OR_R0_R0, &frame->retcode[6]);
  305. err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
  306. regs->pr = (unsigned long) frame->retcode;
  307. flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
  308. }
  309. if (err)
  310. goto give_sigsegv;
  311. /* Set up registers for signal handler */
  312. regs->regs[15] = (unsigned long) frame;
  313. regs->regs[4] = signal; /* Arg for signal handler */
  314. regs->regs[5] = 0;
  315. regs->regs[6] = (unsigned long) &frame->sc;
  316. if (current->personality & FDPIC_FUNCPTRS) {
  317. struct fdpic_func_descriptor __user *funcptr =
  318. (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
  319. err |= __get_user(regs->pc, &funcptr->text);
  320. err |= __get_user(regs->regs[12], &funcptr->GOT);
  321. } else
  322. regs->pc = (unsigned long)ka->sa.sa_handler;
  323. if (err)
  324. goto give_sigsegv;
  325. set_fs(USER_DS);
  326. pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
  327. current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
  328. return 0;
  329. give_sigsegv:
  330. force_sigsegv(sig, current);
  331. return -EFAULT;
  332. }
  333. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  334. sigset_t *set, struct pt_regs *regs)
  335. {
  336. struct rt_sigframe __user *frame;
  337. int err = 0;
  338. int signal;
  339. frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
  340. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  341. goto give_sigsegv;
  342. signal = current_thread_info()->exec_domain
  343. && current_thread_info()->exec_domain->signal_invmap
  344. && sig < 32
  345. ? current_thread_info()->exec_domain->signal_invmap[sig]
  346. : sig;
  347. err |= copy_siginfo_to_user(&frame->info, info);
  348. /* Create the ucontext. */
  349. err |= __put_user(0, &frame->uc.uc_flags);
  350. err |= __put_user(NULL, &frame->uc.uc_link);
  351. err |= __put_user((void *)current->sas_ss_sp,
  352. &frame->uc.uc_stack.ss_sp);
  353. err |= __put_user(sas_ss_flags(regs->regs[15]),
  354. &frame->uc.uc_stack.ss_flags);
  355. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  356. err |= setup_sigcontext(&frame->uc.uc_mcontext,
  357. regs, set->sig[0]);
  358. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  359. /* Set up to return from userspace. If provided, use a stub
  360. already in userspace. */
  361. if (ka->sa.sa_flags & SA_RESTORER) {
  362. regs->pr = (unsigned long) ka->sa.sa_restorer;
  363. #ifdef CONFIG_VSYSCALL
  364. } else if (likely(current->mm->context.vdso)) {
  365. regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
  366. #endif
  367. } else {
  368. /* Generate return code (system call to rt_sigreturn) */
  369. err |= __put_user(MOVW(7), &frame->retcode[0]);
  370. err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
  371. err |= __put_user(OR_R0_R0, &frame->retcode[2]);
  372. err |= __put_user(OR_R0_R0, &frame->retcode[3]);
  373. err |= __put_user(OR_R0_R0, &frame->retcode[4]);
  374. err |= __put_user(OR_R0_R0, &frame->retcode[5]);
  375. err |= __put_user(OR_R0_R0, &frame->retcode[6]);
  376. err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
  377. regs->pr = (unsigned long) frame->retcode;
  378. flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
  379. }
  380. if (err)
  381. goto give_sigsegv;
  382. /* Set up registers for signal handler */
  383. regs->regs[15] = (unsigned long) frame;
  384. regs->regs[4] = signal; /* Arg for signal handler */
  385. regs->regs[5] = (unsigned long) &frame->info;
  386. regs->regs[6] = (unsigned long) &frame->uc;
  387. if (current->personality & FDPIC_FUNCPTRS) {
  388. struct fdpic_func_descriptor __user *funcptr =
  389. (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
  390. err |= __get_user(regs->pc, &funcptr->text);
  391. err |= __get_user(regs->regs[12], &funcptr->GOT);
  392. } else
  393. regs->pc = (unsigned long)ka->sa.sa_handler;
  394. if (err)
  395. goto give_sigsegv;
  396. set_fs(USER_DS);
  397. pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
  398. current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
  399. return 0;
  400. give_sigsegv:
  401. force_sigsegv(sig, current);
  402. return -EFAULT;
  403. }
  404. static inline void
  405. handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
  406. struct sigaction *sa)
  407. {
  408. /* If we're not from a syscall, bail out */
  409. if (regs->tra < 0)
  410. return;
  411. /* check for system call restart.. */
  412. switch (regs->regs[0]) {
  413. case -ERESTART_RESTARTBLOCK:
  414. case -ERESTARTNOHAND:
  415. no_system_call_restart:
  416. regs->regs[0] = -EINTR;
  417. break;
  418. case -ERESTARTSYS:
  419. if (!(sa->sa_flags & SA_RESTART))
  420. goto no_system_call_restart;
  421. /* fallthrough */
  422. case -ERESTARTNOINTR:
  423. regs->regs[0] = save_r0;
  424. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  425. break;
  426. }
  427. }
  428. /*
  429. * OK, we're invoking a handler
  430. */
  431. static void
  432. handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
  433. struct pt_regs *regs, unsigned int save_r0)
  434. {
  435. sigset_t *oldset = sigmask_to_save();
  436. int ret;
  437. /* Set up the stack frame */
  438. if (ka->sa.sa_flags & SA_SIGINFO)
  439. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  440. else
  441. ret = setup_frame(sig, ka, oldset, regs);
  442. if (ret)
  443. return;
  444. signal_delivered(sig, info, ka, regs,
  445. test_thread_flag(TIF_SINGLESTEP));
  446. }
  447. /*
  448. * Note that 'init' is a special process: it doesn't get signals it doesn't
  449. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  450. * mistake.
  451. *
  452. * Note that we go through the signals twice: once to check the signals that
  453. * the kernel can handle, and then we build all the user-level signal handling
  454. * stack-frames in one go after that.
  455. */
  456. static void do_signal(struct pt_regs *regs, unsigned int save_r0)
  457. {
  458. siginfo_t info;
  459. int signr;
  460. struct k_sigaction ka;
  461. /*
  462. * We want the common case to go fast, which
  463. * is why we may in certain cases get here from
  464. * kernel mode. Just return without doing anything
  465. * if so.
  466. */
  467. if (!user_mode(regs))
  468. return;
  469. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  470. if (signr > 0) {
  471. handle_syscall_restart(save_r0, regs, &ka.sa);
  472. /* Whee! Actually deliver the signal. */
  473. handle_signal(signr, &ka, &info, regs, save_r0);
  474. return;
  475. }
  476. /* Did we come from a system call? */
  477. if (regs->tra >= 0) {
  478. /* Restart the system call - no handlers present */
  479. if (regs->regs[0] == -ERESTARTNOHAND ||
  480. regs->regs[0] == -ERESTARTSYS ||
  481. regs->regs[0] == -ERESTARTNOINTR) {
  482. regs->regs[0] = save_r0;
  483. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  484. } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
  485. regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
  486. regs->regs[3] = __NR_restart_syscall;
  487. }
  488. }
  489. /*
  490. * If there's no signal to deliver, we just put the saved sigmask
  491. * back.
  492. */
  493. restore_saved_sigmask();
  494. }
  495. asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
  496. unsigned long thread_info_flags)
  497. {
  498. /* deal with pending signal delivery */
  499. if (thread_info_flags & _TIF_SIGPENDING)
  500. do_signal(regs, save_r0);
  501. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  502. clear_thread_flag(TIF_NOTIFY_RESUME);
  503. tracehook_notify_resume(regs);
  504. }
  505. }