signal_32.c 16 KB

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