signal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * Copyright (C) 2003, Axis Communications AB.
  3. */
  4. #include <linux/sched.h>
  5. #include <linux/mm.h>
  6. #include <linux/slab.h>
  7. #include <linux/kernel.h>
  8. #include <linux/signal.h>
  9. #include <linux/errno.h>
  10. #include <linux/wait.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/unistd.h>
  13. #include <linux/stddef.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/vmalloc.h>
  16. #include <asm/io.h>
  17. #include <asm/processor.h>
  18. #include <asm/ucontext.h>
  19. #include <asm/uaccess.h>
  20. #include <arch/ptrace.h>
  21. #include <arch/hwregs/cpu_vect.h>
  22. extern unsigned long cris_signal_return_page;
  23. /* Flag to check if a signal is blockable. */
  24. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  25. /*
  26. * A syscall in CRIS is really a "break 13" instruction, which is 2
  27. * bytes. The registers is manipulated so upon return the instruction
  28. * will be executed again.
  29. *
  30. * This relies on that PC points to the instruction after the break call.
  31. */
  32. #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
  33. /* Signal frames. */
  34. struct signal_frame {
  35. struct sigcontext sc;
  36. unsigned long extramask[_NSIG_WORDS - 1];
  37. unsigned char retcode[8]; /* Trampoline code. */
  38. };
  39. struct rt_signal_frame {
  40. struct siginfo *pinfo;
  41. void *puc;
  42. struct siginfo info;
  43. struct ucontext uc;
  44. unsigned char retcode[8]; /* Trampoline code. */
  45. };
  46. void do_signal(int restart, struct pt_regs *regs);
  47. void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
  48. struct pt_regs *regs);
  49. /*
  50. * Swap in the new signal mask, and wait for a signal. Define some
  51. * dummy arguments to be able to reach the regs argument.
  52. */
  53. int
  54. sys_sigsuspend(old_sigset_t mask)
  55. {
  56. sigset_t blocked;
  57. siginitset(&blocked, mask);
  58. return sigsuspend(&blocked);
  59. }
  60. int
  61. sys_sigaction(int signal, const struct old_sigaction *act,
  62. struct old_sigaction *oact)
  63. {
  64. int retval;
  65. struct k_sigaction newk;
  66. struct k_sigaction oldk;
  67. if (act) {
  68. old_sigset_t mask;
  69. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  70. __get_user(newk.sa.sa_handler, &act->sa_handler) ||
  71. __get_user(newk.sa.sa_restorer, &act->sa_restorer))
  72. return -EFAULT;
  73. __get_user(newk.sa.sa_flags, &act->sa_flags);
  74. __get_user(mask, &act->sa_mask);
  75. siginitset(&newk.sa.sa_mask, mask);
  76. }
  77. retval = do_sigaction(signal, act ? &newk : NULL, oact ? &oldk : NULL);
  78. if (!retval && oact) {
  79. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  80. __put_user(oldk.sa.sa_handler, &oact->sa_handler) ||
  81. __put_user(oldk.sa.sa_restorer, &oact->sa_restorer))
  82. return -EFAULT;
  83. __put_user(oldk.sa.sa_flags, &oact->sa_flags);
  84. __put_user(oldk.sa.sa_mask.sig[0], &oact->sa_mask);
  85. }
  86. return retval;
  87. }
  88. int
  89. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  90. {
  91. return do_sigaltstack(uss, uoss, rdusp());
  92. }
  93. static int
  94. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  95. {
  96. unsigned int err = 0;
  97. unsigned long old_usp;
  98. /* Always make any pending restarted system calls return -EINTR */
  99. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  100. /*
  101. * Restore the registers from &sc->regs. sc is already checked
  102. * for VERIFY_READ since the signal_frame was previously
  103. * checked in sys_sigreturn().
  104. */
  105. if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
  106. goto badframe;
  107. /* Make that the user-mode flag is set. */
  108. regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
  109. /* Restore the old USP. */
  110. err |= __get_user(old_usp, &sc->usp);
  111. wrusp(old_usp);
  112. return err;
  113. badframe:
  114. return 1;
  115. }
  116. /* Define some dummy arguments to be able to reach the regs argument. */
  117. asmlinkage int
  118. sys_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
  119. struct pt_regs *regs)
  120. {
  121. sigset_t set;
  122. struct signal_frame __user *frame;
  123. unsigned long oldspc = regs->spc;
  124. unsigned long oldccs = regs->ccs;
  125. frame = (struct signal_frame *) rdusp();
  126. /*
  127. * Since the signal is stacked on a dword boundary, the frame
  128. * should be dword aligned here as well. It it's not, then the
  129. * user is trying some funny business.
  130. */
  131. if (((long)frame) & 3)
  132. goto badframe;
  133. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  134. goto badframe;
  135. if (__get_user(set.sig[0], &frame->sc.oldmask) ||
  136. (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
  137. frame->extramask,
  138. sizeof(frame->extramask))))
  139. goto badframe;
  140. sigdelsetmask(&set, ~_BLOCKABLE);
  141. set_current_blocked(&set);
  142. if (restore_sigcontext(regs, &frame->sc))
  143. goto badframe;
  144. keep_debug_flags(oldccs, oldspc, regs);
  145. return regs->r10;
  146. badframe:
  147. force_sig(SIGSEGV, current);
  148. return 0;
  149. }
  150. /* Define some dummy variables to be able to reach the regs argument. */
  151. asmlinkage int
  152. sys_rt_sigreturn(long r10, long r11, long r12, long r13, long mof, long srp,
  153. struct pt_regs *regs)
  154. {
  155. sigset_t set;
  156. struct rt_signal_frame __user *frame;
  157. unsigned long oldspc = regs->spc;
  158. unsigned long oldccs = regs->ccs;
  159. frame = (struct rt_signal_frame *) rdusp();
  160. /*
  161. * Since the signal is stacked on a dword boundary, the frame
  162. * should be dword aligned here as well. It it's not, then the
  163. * user is trying some funny business.
  164. */
  165. if (((long)frame) & 3)
  166. goto badframe;
  167. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  168. goto badframe;
  169. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  170. goto badframe;
  171. sigdelsetmask(&set, ~_BLOCKABLE);
  172. set_current_blocked(&set);
  173. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  174. goto badframe;
  175. if (do_sigaltstack(&frame->uc.uc_stack, NULL, rdusp()) == -EFAULT)
  176. goto badframe;
  177. keep_debug_flags(oldccs, oldspc, regs);
  178. return regs->r10;
  179. badframe:
  180. force_sig(SIGSEGV, current);
  181. return 0;
  182. }
  183. /* Setup a signal frame. */
  184. static int
  185. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
  186. unsigned long mask)
  187. {
  188. int err;
  189. unsigned long usp;
  190. err = 0;
  191. usp = rdusp();
  192. /*
  193. * Copy the registers. They are located first in sc, so it's
  194. * possible to use sc directly.
  195. */
  196. err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
  197. err |= __put_user(mask, &sc->oldmask);
  198. err |= __put_user(usp, &sc->usp);
  199. return err;
  200. }
  201. /* Figure out where to put the new signal frame - usually on the stack. */
  202. static inline void __user *
  203. get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  204. {
  205. unsigned long sp;
  206. sp = rdusp();
  207. /* This is the X/Open sanctioned signal stack switching. */
  208. if (ka->sa.sa_flags & SA_ONSTACK) {
  209. if (!on_sig_stack(sp))
  210. sp = current->sas_ss_sp + current->sas_ss_size;
  211. }
  212. /* Make sure the frame is dword-aligned. */
  213. sp &= ~3;
  214. return (void __user *)(sp - frame_size);
  215. }
  216. /* Grab and setup a signal frame.
  217. *
  218. * Basically a lot of state-info is stacked, and arranged for the
  219. * user-mode program to return to the kernel using either a trampiline
  220. * which performs the syscall sigreturn(), or a provided user-mode
  221. * trampoline.
  222. */
  223. static int
  224. setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
  225. struct pt_regs * regs)
  226. {
  227. int err;
  228. unsigned long return_ip;
  229. struct signal_frame __user *frame;
  230. err = 0;
  231. frame = get_sigframe(ka, regs, sizeof(*frame));
  232. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  233. goto give_sigsegv;
  234. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  235. if (err)
  236. goto give_sigsegv;
  237. if (_NSIG_WORDS > 1) {
  238. err |= __copy_to_user(frame->extramask, &set->sig[1],
  239. sizeof(frame->extramask));
  240. }
  241. if (err)
  242. goto give_sigsegv;
  243. /*
  244. * Set up to return from user-space. If provided, use a stub
  245. * already located in user-space.
  246. */
  247. if (ka->sa.sa_flags & SA_RESTORER) {
  248. return_ip = (unsigned long)ka->sa.sa_restorer;
  249. } else {
  250. /* Trampoline - the desired return ip is in the signal return page. */
  251. return_ip = cris_signal_return_page;
  252. /*
  253. * This is movu.w __NR_sigreturn, r9; break 13;
  254. *
  255. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  256. * reasons and because gdb uses it as a signature to notice
  257. * signal handler stack frames.
  258. */
  259. err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
  260. err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
  261. err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
  262. }
  263. if (err)
  264. goto give_sigsegv;
  265. /*
  266. * Set up registers for signal handler.
  267. *
  268. * Where the code enters now.
  269. * Where the code enter later.
  270. * First argument, signo.
  271. */
  272. regs->erp = (unsigned long) ka->sa.sa_handler;
  273. regs->srp = return_ip;
  274. regs->r10 = sig;
  275. /* Actually move the USP to reflect the stacked frame. */
  276. wrusp((unsigned long)frame);
  277. return 0;
  278. give_sigsegv:
  279. if (sig == SIGSEGV)
  280. ka->sa.sa_handler = SIG_DFL;
  281. force_sig(SIGSEGV, current);
  282. return -EFAULT;
  283. }
  284. static int
  285. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  286. sigset_t *set, struct pt_regs * regs)
  287. {
  288. int err;
  289. unsigned long return_ip;
  290. struct rt_signal_frame __user *frame;
  291. err = 0;
  292. frame = get_sigframe(ka, regs, sizeof(*frame));
  293. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  294. goto give_sigsegv;
  295. /* TODO: what is the current->exec_domain stuff and invmap ? */
  296. err |= __put_user(&frame->info, &frame->pinfo);
  297. err |= __put_user(&frame->uc, &frame->puc);
  298. err |= copy_siginfo_to_user(&frame->info, info);
  299. if (err)
  300. goto give_sigsegv;
  301. /* Clear all the bits of the ucontext we don't use. */
  302. err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
  303. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  304. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  305. if (err)
  306. goto give_sigsegv;
  307. /*
  308. * Set up to return from user-space. If provided, use a stub
  309. * already located in user-space.
  310. */
  311. if (ka->sa.sa_flags & SA_RESTORER) {
  312. return_ip = (unsigned long) ka->sa.sa_restorer;
  313. } else {
  314. /* Trampoline - the desired return ip is in the signal return page. */
  315. return_ip = cris_signal_return_page + 6;
  316. /*
  317. * This is movu.w __NR_rt_sigreturn, r9; break 13;
  318. *
  319. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  320. * reasons and because gdb uses it as a signature to notice
  321. * signal handler stack frames.
  322. */
  323. err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
  324. err |= __put_user(__NR_rt_sigreturn,
  325. (short __user*)(frame->retcode+2));
  326. err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
  327. }
  328. if (err)
  329. goto give_sigsegv;
  330. /*
  331. * Set up registers for signal handler.
  332. *
  333. * Where the code enters now.
  334. * Where the code enters later.
  335. * First argument is signo.
  336. * Second argument is (siginfo_t *).
  337. * Third argument is unused.
  338. */
  339. regs->erp = (unsigned long) ka->sa.sa_handler;
  340. regs->srp = return_ip;
  341. regs->r10 = sig;
  342. regs->r11 = (unsigned long) &frame->info;
  343. regs->r12 = 0;
  344. /* Actually move the usp to reflect the stacked frame. */
  345. wrusp((unsigned long)frame);
  346. return 0;
  347. give_sigsegv:
  348. if (sig == SIGSEGV)
  349. ka->sa.sa_handler = SIG_DFL;
  350. force_sig(SIGSEGV, current);
  351. return -EFAULT;
  352. }
  353. /* Invoke a signal handler to, well, handle the signal. */
  354. static inline int
  355. handle_signal(int canrestart, unsigned long sig,
  356. siginfo_t *info, struct k_sigaction *ka,
  357. sigset_t *oldset, struct pt_regs * regs)
  358. {
  359. int ret;
  360. /* Check if this got called from a system call. */
  361. if (canrestart) {
  362. /* If so, check system call restarting. */
  363. switch (regs->r10) {
  364. case -ERESTART_RESTARTBLOCK:
  365. case -ERESTARTNOHAND:
  366. /*
  367. * This means that the syscall should
  368. * only be restarted if there was no
  369. * handler for the signal, and since
  370. * this point isn't reached unless
  371. * there is a handler, there's no need
  372. * to restart.
  373. */
  374. regs->r10 = -EINTR;
  375. break;
  376. case -ERESTARTSYS:
  377. /*
  378. * This means restart the syscall if
  379. * there is no handler, or the handler
  380. * was registered with SA_RESTART.
  381. */
  382. if (!(ka->sa.sa_flags & SA_RESTART)) {
  383. regs->r10 = -EINTR;
  384. break;
  385. }
  386. /* Fall through. */
  387. case -ERESTARTNOINTR:
  388. /*
  389. * This means that the syscall should
  390. * be called again after the signal
  391. * handler returns.
  392. */
  393. RESTART_CRIS_SYS(regs);
  394. break;
  395. }
  396. }
  397. /* Set up the stack frame. */
  398. if (ka->sa.sa_flags & SA_SIGINFO)
  399. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  400. else
  401. ret = setup_frame(sig, ka, oldset, regs);
  402. if (ret == 0)
  403. block_sigmask(ka, sig);
  404. return ret;
  405. }
  406. /*
  407. * Note that 'init' is a special process: it doesn't get signals it doesn't
  408. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  409. * mistake.
  410. *
  411. * Also note that the regs structure given here as an argument, is the latest
  412. * pushed pt_regs. It may or may not be the same as the first pushed registers
  413. * when the initial usermode->kernelmode transition took place. Therefore
  414. * we can use user_mode(regs) to see if we came directly from kernel or user
  415. * mode below.
  416. */
  417. void
  418. do_signal(int canrestart, struct pt_regs *regs)
  419. {
  420. int signr;
  421. siginfo_t info;
  422. struct k_sigaction ka;
  423. sigset_t *oldset;
  424. /*
  425. * The common case should go fast, which is why this point is
  426. * reached from kernel-mode. If that's the case, just return
  427. * without doing anything.
  428. */
  429. if (!user_mode(regs))
  430. return;
  431. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  432. oldset = &current->saved_sigmask;
  433. else
  434. oldset = &current->blocked;
  435. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  436. if (signr > 0) {
  437. /* Whee! Actually deliver the signal. */
  438. if (handle_signal(canrestart, signr, &info, &ka,
  439. oldset, regs)) {
  440. /* a signal was successfully delivered; the saved
  441. * sigmask will have been stored in the signal frame,
  442. * and will be restored by sigreturn, so we can simply
  443. * clear the TIF_RESTORE_SIGMASK flag */
  444. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  445. clear_thread_flag(TIF_RESTORE_SIGMASK);
  446. }
  447. return;
  448. }
  449. /* Got here from a system call? */
  450. if (canrestart) {
  451. /* Restart the system call - no handlers present. */
  452. if (regs->r10 == -ERESTARTNOHAND ||
  453. regs->r10 == -ERESTARTSYS ||
  454. regs->r10 == -ERESTARTNOINTR) {
  455. RESTART_CRIS_SYS(regs);
  456. }
  457. if (regs->r10 == -ERESTART_RESTARTBLOCK){
  458. regs->r9 = __NR_restart_syscall;
  459. regs->erp -= 2;
  460. }
  461. }
  462. /* if there's no signal to deliver, we just put the saved sigmask
  463. * back */
  464. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  465. clear_thread_flag(TIF_RESTORE_SIGMASK);
  466. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  467. }
  468. }
  469. asmlinkage void
  470. ugdb_trap_user(struct thread_info *ti, int sig)
  471. {
  472. if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
  473. /* Zero single-step PC if the reason we stopped wasn't a single
  474. step exception. This is to avoid relying on it when it isn't
  475. reliable. */
  476. user_regs(ti)->spc = 0;
  477. }
  478. /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
  479. not within any configured h/w breakpoint range). Synchronize with
  480. what already exists for kernel debugging. */
  481. if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
  482. /* Break 8: subtract 2 from ERP unless in a delay slot. */
  483. if (!(user_regs(ti)->erp & 0x1))
  484. user_regs(ti)->erp -= 2;
  485. }
  486. sys_kill(ti->task->pid, sig);
  487. }
  488. void
  489. keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
  490. struct pt_regs *regs)
  491. {
  492. if (oldccs & (1 << Q_CCS_BITNR)) {
  493. /* Pending single step due to single-stepping the break 13
  494. in the signal trampoline: keep the Q flag. */
  495. regs->ccs |= (1 << Q_CCS_BITNR);
  496. /* S flag should be set - complain if it's not. */
  497. if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
  498. printk("Q flag but no S flag?");
  499. }
  500. regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
  501. /* Assume the SPC is valid and interesting. */
  502. regs->spc = oldspc;
  503. } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
  504. /* If a h/w bp was set in the signal handler we need
  505. to keep the S flag. */
  506. regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
  507. /* Don't keep the old SPC though; if we got here due to
  508. a single-step, the Q flag should have been set. */
  509. } else if (regs->spc) {
  510. /* If we were single-stepping *before* the signal was taken,
  511. we don't want to restore that state now, because GDB will
  512. have forgotten all about it. */
  513. regs->spc = 0;
  514. regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
  515. }
  516. }
  517. /* Set up the trampolines on the signal return page. */
  518. int __init
  519. cris_init_signal(void)
  520. {
  521. u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
  522. /* This is movu.w __NR_sigreturn, r9; break 13; */
  523. data[0] = 0x9c5f;
  524. data[1] = __NR_sigreturn;
  525. data[2] = 0xe93d;
  526. /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
  527. data[3] = 0x9c5f;
  528. data[4] = __NR_rt_sigreturn;
  529. data[5] = 0xe93d;
  530. /* Map to userspace with appropriate permissions (no write access...) */
  531. cris_signal_return_page = (unsigned long)
  532. __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
  533. return 0;
  534. }
  535. __initcall(cris_init_signal);