signal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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_thread_info()->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 k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
  152. {
  153. unsigned long sp;
  154. sp = rdusp();
  155. /* This is the X/Open sanctioned signal stack switching. */
  156. if (ka->sa.sa_flags & SA_ONSTACK) {
  157. if (!on_sig_stack(sp))
  158. sp = current->sas_ss_sp + current->sas_ss_size;
  159. }
  160. /* Make sure the frame is dword-aligned. */
  161. sp &= ~3;
  162. return (void __user *)(sp - frame_size);
  163. }
  164. /* Grab and setup a signal frame.
  165. *
  166. * Basically a lot of state-info is stacked, and arranged for the
  167. * user-mode program to return to the kernel using either a trampiline
  168. * which performs the syscall sigreturn(), or a provided user-mode
  169. * trampoline.
  170. */
  171. static int
  172. setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
  173. struct pt_regs * regs)
  174. {
  175. int err;
  176. unsigned long return_ip;
  177. struct signal_frame __user *frame;
  178. err = 0;
  179. frame = get_sigframe(ka, regs, sizeof(*frame));
  180. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  181. goto give_sigsegv;
  182. err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
  183. if (err)
  184. goto give_sigsegv;
  185. if (_NSIG_WORDS > 1) {
  186. err |= __copy_to_user(frame->extramask, &set->sig[1],
  187. sizeof(frame->extramask));
  188. }
  189. if (err)
  190. goto give_sigsegv;
  191. /*
  192. * Set up to return from user-space. If provided, use a stub
  193. * already located in user-space.
  194. */
  195. if (ka->sa.sa_flags & SA_RESTORER) {
  196. return_ip = (unsigned long)ka->sa.sa_restorer;
  197. } else {
  198. /* Trampoline - the desired return ip is in the signal return page. */
  199. return_ip = cris_signal_return_page;
  200. /*
  201. * This is movu.w __NR_sigreturn, r9; break 13;
  202. *
  203. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  204. * reasons and because gdb uses it as a signature to notice
  205. * signal handler stack frames.
  206. */
  207. err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
  208. err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
  209. err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
  210. }
  211. if (err)
  212. goto give_sigsegv;
  213. /*
  214. * Set up registers for signal handler.
  215. *
  216. * Where the code enters now.
  217. * Where the code enter later.
  218. * First argument, signo.
  219. */
  220. regs->erp = (unsigned long) ka->sa.sa_handler;
  221. regs->srp = return_ip;
  222. regs->r10 = sig;
  223. /* Actually move the USP to reflect the stacked frame. */
  224. wrusp((unsigned long)frame);
  225. return 0;
  226. give_sigsegv:
  227. force_sigsegv(sig, current);
  228. return -EFAULT;
  229. }
  230. static int
  231. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  232. sigset_t *set, struct pt_regs * regs)
  233. {
  234. int err;
  235. unsigned long return_ip;
  236. struct rt_signal_frame __user *frame;
  237. err = 0;
  238. frame = get_sigframe(ka, regs, sizeof(*frame));
  239. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  240. goto give_sigsegv;
  241. /* TODO: what is the current->exec_domain stuff and invmap ? */
  242. err |= __put_user(&frame->info, &frame->pinfo);
  243. err |= __put_user(&frame->uc, &frame->puc);
  244. err |= copy_siginfo_to_user(&frame->info, info);
  245. if (err)
  246. goto give_sigsegv;
  247. /* Clear all the bits of the ucontext we don't use. */
  248. err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
  249. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
  250. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  251. err |= __save_altstack(&frame->uc.uc_stack, rdusp());
  252. if (err)
  253. goto give_sigsegv;
  254. /*
  255. * Set up to return from user-space. If provided, use a stub
  256. * already located in user-space.
  257. */
  258. if (ka->sa.sa_flags & SA_RESTORER) {
  259. return_ip = (unsigned long) ka->sa.sa_restorer;
  260. } else {
  261. /* Trampoline - the desired return ip is in the signal return page. */
  262. return_ip = cris_signal_return_page + 6;
  263. /*
  264. * This is movu.w __NR_rt_sigreturn, r9; break 13;
  265. *
  266. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  267. * reasons and because gdb uses it as a signature to notice
  268. * signal handler stack frames.
  269. */
  270. err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
  271. err |= __put_user(__NR_rt_sigreturn,
  272. (short __user*)(frame->retcode+2));
  273. err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
  274. }
  275. if (err)
  276. goto give_sigsegv;
  277. /*
  278. * Set up registers for signal handler.
  279. *
  280. * Where the code enters now.
  281. * Where the code enters later.
  282. * First argument is signo.
  283. * Second argument is (siginfo_t *).
  284. * Third argument is unused.
  285. */
  286. regs->erp = (unsigned long) ka->sa.sa_handler;
  287. regs->srp = return_ip;
  288. regs->r10 = sig;
  289. regs->r11 = (unsigned long) &frame->info;
  290. regs->r12 = 0;
  291. /* Actually move the usp to reflect the stacked frame. */
  292. wrusp((unsigned long)frame);
  293. return 0;
  294. give_sigsegv:
  295. force_sigsegv(sig, current);
  296. return -EFAULT;
  297. }
  298. /* Invoke a signal handler to, well, handle the signal. */
  299. static inline void
  300. handle_signal(int canrestart, unsigned long sig,
  301. siginfo_t *info, struct k_sigaction *ka,
  302. struct pt_regs * regs)
  303. {
  304. sigset_t *oldset = sigmask_to_save();
  305. int ret;
  306. /* Check if this got called from a system call. */
  307. if (canrestart) {
  308. /* If so, check system call restarting. */
  309. switch (regs->r10) {
  310. case -ERESTART_RESTARTBLOCK:
  311. case -ERESTARTNOHAND:
  312. /*
  313. * This means that the syscall should
  314. * only be restarted if there was no
  315. * handler for the signal, and since
  316. * this point isn't reached unless
  317. * there is a handler, there's no need
  318. * to restart.
  319. */
  320. regs->r10 = -EINTR;
  321. break;
  322. case -ERESTARTSYS:
  323. /*
  324. * This means restart the syscall if
  325. * there is no handler, or the handler
  326. * was registered with SA_RESTART.
  327. */
  328. if (!(ka->sa.sa_flags & SA_RESTART)) {
  329. regs->r10 = -EINTR;
  330. break;
  331. }
  332. /* Fall through. */
  333. case -ERESTARTNOINTR:
  334. /*
  335. * This means that the syscall should
  336. * be called again after the signal
  337. * handler returns.
  338. */
  339. RESTART_CRIS_SYS(regs);
  340. break;
  341. }
  342. }
  343. /* Set up the stack frame. */
  344. if (ka->sa.sa_flags & SA_SIGINFO)
  345. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  346. else
  347. ret = setup_frame(sig, ka, oldset, regs);
  348. if (ret == 0)
  349. signal_delivered(sig, info, ka, regs, 0);
  350. }
  351. /*
  352. * Note that 'init' is a special process: it doesn't get signals it doesn't
  353. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  354. * mistake.
  355. *
  356. * Also note that the regs structure given here as an argument, is the latest
  357. * pushed pt_regs. It may or may not be the same as the first pushed registers
  358. * when the initial usermode->kernelmode transition took place. Therefore
  359. * we can use user_mode(regs) to see if we came directly from kernel or user
  360. * mode below.
  361. */
  362. void
  363. do_signal(int canrestart, struct pt_regs *regs)
  364. {
  365. int signr;
  366. siginfo_t info;
  367. struct k_sigaction ka;
  368. /*
  369. * The common case should go fast, which is why this point is
  370. * reached from kernel-mode. If that's the case, just return
  371. * without doing anything.
  372. */
  373. if (!user_mode(regs))
  374. return;
  375. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  376. if (signr > 0) {
  377. /* Whee! Actually deliver the signal. */
  378. handle_signal(canrestart, signr, &info, &ka, regs);
  379. return;
  380. }
  381. /* Got here from a system call? */
  382. if (canrestart) {
  383. /* Restart the system call - no handlers present. */
  384. if (regs->r10 == -ERESTARTNOHAND ||
  385. regs->r10 == -ERESTARTSYS ||
  386. regs->r10 == -ERESTARTNOINTR) {
  387. RESTART_CRIS_SYS(regs);
  388. }
  389. if (regs->r10 == -ERESTART_RESTARTBLOCK){
  390. regs->r9 = __NR_restart_syscall;
  391. regs->erp -= 2;
  392. }
  393. }
  394. /* if there's no signal to deliver, we just put the saved sigmask
  395. * back */
  396. restore_saved_sigmask();
  397. }
  398. asmlinkage void
  399. ugdb_trap_user(struct thread_info *ti, int sig)
  400. {
  401. if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
  402. /* Zero single-step PC if the reason we stopped wasn't a single
  403. step exception. This is to avoid relying on it when it isn't
  404. reliable. */
  405. user_regs(ti)->spc = 0;
  406. }
  407. /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
  408. not within any configured h/w breakpoint range). Synchronize with
  409. what already exists for kernel debugging. */
  410. if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
  411. /* Break 8: subtract 2 from ERP unless in a delay slot. */
  412. if (!(user_regs(ti)->erp & 0x1))
  413. user_regs(ti)->erp -= 2;
  414. }
  415. sys_kill(ti->task->pid, sig);
  416. }
  417. void
  418. keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
  419. struct pt_regs *regs)
  420. {
  421. if (oldccs & (1 << Q_CCS_BITNR)) {
  422. /* Pending single step due to single-stepping the break 13
  423. in the signal trampoline: keep the Q flag. */
  424. regs->ccs |= (1 << Q_CCS_BITNR);
  425. /* S flag should be set - complain if it's not. */
  426. if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
  427. printk("Q flag but no S flag?");
  428. }
  429. regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
  430. /* Assume the SPC is valid and interesting. */
  431. regs->spc = oldspc;
  432. } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
  433. /* If a h/w bp was set in the signal handler we need
  434. to keep the S flag. */
  435. regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
  436. /* Don't keep the old SPC though; if we got here due to
  437. a single-step, the Q flag should have been set. */
  438. } else if (regs->spc) {
  439. /* If we were single-stepping *before* the signal was taken,
  440. we don't want to restore that state now, because GDB will
  441. have forgotten all about it. */
  442. regs->spc = 0;
  443. regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
  444. }
  445. }
  446. /* Set up the trampolines on the signal return page. */
  447. int __init
  448. cris_init_signal(void)
  449. {
  450. u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
  451. /* This is movu.w __NR_sigreturn, r9; break 13; */
  452. data[0] = 0x9c5f;
  453. data[1] = __NR_sigreturn;
  454. data[2] = 0xe93d;
  455. /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
  456. data[3] = 0x9c5f;
  457. data[4] = __NR_rt_sigreturn;
  458. data[5] = 0xe93d;
  459. /* Map to userspace with appropriate permissions (no write access...) */
  460. cris_signal_return_page = (unsigned long)
  461. __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
  462. return 0;
  463. }
  464. __initcall(cris_init_signal);