signal.c 15 KB

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