signal_32.c 13 KB

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