signal.c 14 KB

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