signal.c 15 KB

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