signal.c 14 KB

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