signal.c 16 KB

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