PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/release/src-rt/linux/linux-2.6/arch/x86_64/kernel/signal.c

https://gitlab.com/envieidoc/advancedtomato2
C | 488 lines | 336 code | 75 blank | 77 comment | 47 complexity | f2db8c8dc8ae97d5e7648cdf71bb43b9 MD5 | raw file
  1. /*
  2. * linux/arch/x86_64/kernel/signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  6. *
  7. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  8. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  9. * 2000-2002 x86-64 support by Andi Kleen
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/signal.h>
  16. #include <linux/errno.h>
  17. #include <linux/wait.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/unistd.h>
  20. #include <linux/stddef.h>
  21. #include <linux/personality.h>
  22. #include <linux/compiler.h>
  23. #include <asm/ucontext.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/i387.h>
  26. #include <asm/proto.h>
  27. #include <asm/ia32_unistd.h>
  28. /* #define DEBUG_SIG 1 */
  29. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  30. int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  31. sigset_t *set, struct pt_regs * regs);
  32. int ia32_setup_frame(int sig, struct k_sigaction *ka,
  33. sigset_t *set, struct pt_regs * regs);
  34. asmlinkage long
  35. sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
  36. struct pt_regs *regs)
  37. {
  38. return do_sigaltstack(uss, uoss, regs->rsp);
  39. }
  40. /*
  41. * Do a signal return; undo the signal stack.
  42. */
  43. struct rt_sigframe
  44. {
  45. char __user *pretcode;
  46. struct ucontext uc;
  47. struct siginfo info;
  48. };
  49. static int
  50. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, unsigned long *prax)
  51. {
  52. unsigned int err = 0;
  53. /* Always make any pending restarted system calls return -EINTR */
  54. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  55. #define COPY(x) err |= __get_user(regs->x, &sc->x)
  56. COPY(rdi); COPY(rsi); COPY(rbp); COPY(rsp); COPY(rbx);
  57. COPY(rdx); COPY(rcx); COPY(rip);
  58. COPY(r8);
  59. COPY(r9);
  60. COPY(r10);
  61. COPY(r11);
  62. COPY(r12);
  63. COPY(r13);
  64. COPY(r14);
  65. COPY(r15);
  66. /* Kernel saves and restores only the CS segment register on signals,
  67. * which is the bare minimum needed to allow mixed 32/64-bit code.
  68. * App's signal handler can save/restore other segments if needed. */
  69. {
  70. unsigned cs;
  71. err |= __get_user(cs, &sc->cs);
  72. regs->cs = cs | 3; /* Force into user mode */
  73. }
  74. {
  75. unsigned int tmpflags;
  76. err |= __get_user(tmpflags, &sc->eflags);
  77. regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
  78. regs->orig_rax = -1; /* disable syscall checks */
  79. }
  80. {
  81. struct _fpstate __user * buf;
  82. err |= __get_user(buf, &sc->fpstate);
  83. if (buf) {
  84. if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
  85. goto badframe;
  86. err |= restore_i387(buf);
  87. } else {
  88. struct task_struct *me = current;
  89. if (used_math()) {
  90. clear_fpu(me);
  91. clear_used_math();
  92. }
  93. }
  94. }
  95. err |= __get_user(*prax, &sc->rax);
  96. return err;
  97. badframe:
  98. return 1;
  99. }
  100. asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
  101. {
  102. struct rt_sigframe __user *frame;
  103. sigset_t set;
  104. unsigned long eax;
  105. frame = (struct rt_sigframe __user *)(regs->rsp - 8);
  106. if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) {
  107. goto badframe;
  108. }
  109. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) {
  110. goto badframe;
  111. }
  112. sigdelsetmask(&set, ~_BLOCKABLE);
  113. spin_lock_irq(&current->sighand->siglock);
  114. current->blocked = set;
  115. recalc_sigpending();
  116. spin_unlock_irq(&current->sighand->siglock);
  117. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
  118. goto badframe;
  119. #ifdef DEBUG_SIG
  120. printk("%d sigreturn rip:%lx rsp:%lx frame:%p rax:%lx\n",current->pid,regs->rip,regs->rsp,frame,eax);
  121. #endif
  122. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->rsp) == -EFAULT)
  123. goto badframe;
  124. return eax;
  125. badframe:
  126. signal_fault(regs,frame,"sigreturn");
  127. return 0;
  128. }
  129. /*
  130. * Set up a signal frame.
  131. */
  132. static inline int
  133. setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, unsigned long mask, struct task_struct *me)
  134. {
  135. int err = 0;
  136. err |= __put_user(regs->cs, &sc->cs);
  137. err |= __put_user(0, &sc->gs);
  138. err |= __put_user(0, &sc->fs);
  139. err |= __put_user(regs->rdi, &sc->rdi);
  140. err |= __put_user(regs->rsi, &sc->rsi);
  141. err |= __put_user(regs->rbp, &sc->rbp);
  142. err |= __put_user(regs->rsp, &sc->rsp);
  143. err |= __put_user(regs->rbx, &sc->rbx);
  144. err |= __put_user(regs->rdx, &sc->rdx);
  145. err |= __put_user(regs->rcx, &sc->rcx);
  146. err |= __put_user(regs->rax, &sc->rax);
  147. err |= __put_user(regs->r8, &sc->r8);
  148. err |= __put_user(regs->r9, &sc->r9);
  149. err |= __put_user(regs->r10, &sc->r10);
  150. err |= __put_user(regs->r11, &sc->r11);
  151. err |= __put_user(regs->r12, &sc->r12);
  152. err |= __put_user(regs->r13, &sc->r13);
  153. err |= __put_user(regs->r14, &sc->r14);
  154. err |= __put_user(regs->r15, &sc->r15);
  155. err |= __put_user(me->thread.trap_no, &sc->trapno);
  156. err |= __put_user(me->thread.error_code, &sc->err);
  157. err |= __put_user(regs->rip, &sc->rip);
  158. err |= __put_user(regs->eflags, &sc->eflags);
  159. err |= __put_user(mask, &sc->oldmask);
  160. err |= __put_user(me->thread.cr2, &sc->cr2);
  161. return err;
  162. }
  163. /*
  164. * Determine which stack to use..
  165. */
  166. static void __user *
  167. get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
  168. {
  169. unsigned long rsp;
  170. /* Default to using normal stack - redzone*/
  171. rsp = regs->rsp - 128;
  172. /* This is the X/Open sanctioned signal stack switching. */
  173. if (ka->sa.sa_flags & SA_ONSTACK) {
  174. if (sas_ss_flags(rsp) == 0)
  175. rsp = current->sas_ss_sp + current->sas_ss_size;
  176. }
  177. return (void __user *)round_down(rsp - size, 16);
  178. }
  179. static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  180. sigset_t *set, struct pt_regs * regs)
  181. {
  182. struct rt_sigframe __user *frame;
  183. struct _fpstate __user *fp = NULL;
  184. int err = 0;
  185. struct task_struct *me = current;
  186. if (used_math()) {
  187. fp = get_stack(ka, regs, sizeof(struct _fpstate));
  188. frame = (void __user *)round_down(
  189. (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
  190. if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate)))
  191. goto give_sigsegv;
  192. if (save_i387(fp) < 0)
  193. err |= -1;
  194. } else
  195. frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
  196. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  197. goto give_sigsegv;
  198. if (ka->sa.sa_flags & SA_SIGINFO) {
  199. err |= copy_siginfo_to_user(&frame->info, info);
  200. if (err)
  201. goto give_sigsegv;
  202. }
  203. /* Create the ucontext. */
  204. err |= __put_user(0, &frame->uc.uc_flags);
  205. err |= __put_user(0, &frame->uc.uc_link);
  206. err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  207. err |= __put_user(sas_ss_flags(regs->rsp),
  208. &frame->uc.uc_stack.ss_flags);
  209. err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
  210. err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0], me);
  211. err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
  212. if (sizeof(*set) == 16) {
  213. __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
  214. __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
  215. } else
  216. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  217. /* Set up to return from userspace. If provided, use a stub
  218. already in userspace. */
  219. /* x86-64 should always use SA_RESTORER. */
  220. if (ka->sa.sa_flags & SA_RESTORER) {
  221. err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
  222. } else {
  223. /* could use a vstub here */
  224. goto give_sigsegv;
  225. }
  226. if (err)
  227. goto give_sigsegv;
  228. #ifdef DEBUG_SIG
  229. printk("%d old rip %lx old rsp %lx old rax %lx\n", current->pid,regs->rip,regs->rsp,regs->rax);
  230. #endif
  231. /* Set up registers for signal handler */
  232. regs->rdi = sig;
  233. /* In case the signal handler was declared without prototypes */
  234. regs->rax = 0;
  235. /* This also works for non SA_SIGINFO handlers because they expect the
  236. next argument after the signal number on the stack. */
  237. regs->rsi = (unsigned long)&frame->info;
  238. regs->rdx = (unsigned long)&frame->uc;
  239. regs->rip = (unsigned long) ka->sa.sa_handler;
  240. regs->rsp = (unsigned long)frame;
  241. /* Set up the CS register to run signal handlers in 64-bit mode,
  242. even if the handler happens to be interrupting 32-bit code. */
  243. regs->cs = __USER_CS;
  244. /* This, by contrast, has nothing to do with segment registers -
  245. see include/asm-x86_64/uaccess.h for details. */
  246. set_fs(USER_DS);
  247. regs->eflags &= ~TF_MASK;
  248. if (test_thread_flag(TIF_SINGLESTEP))
  249. ptrace_notify(SIGTRAP);
  250. #ifdef DEBUG_SIG
  251. printk("SIG deliver (%s:%d): sp=%p pc=%lx ra=%p\n",
  252. current->comm, current->pid, frame, regs->rip, frame->pretcode);
  253. #endif
  254. return 0;
  255. give_sigsegv:
  256. force_sigsegv(sig, current);
  257. return -EFAULT;
  258. }
  259. /*
  260. * OK, we're invoking a handler
  261. */
  262. static int
  263. handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
  264. sigset_t *oldset, struct pt_regs *regs)
  265. {
  266. int ret;
  267. #ifdef DEBUG_SIG
  268. printk("handle_signal pid:%d sig:%lu rip:%lx rsp:%lx regs=%p\n",
  269. current->pid, sig,
  270. regs->rip, regs->rsp, regs);
  271. #endif
  272. /* Are we from a system call? */
  273. if ((long)regs->orig_rax >= 0) {
  274. /* If so, check system call restarting.. */
  275. switch (regs->rax) {
  276. case -ERESTART_RESTARTBLOCK:
  277. case -ERESTARTNOHAND:
  278. regs->rax = -EINTR;
  279. break;
  280. case -ERESTARTSYS:
  281. if (!(ka->sa.sa_flags & SA_RESTART)) {
  282. regs->rax = -EINTR;
  283. break;
  284. }
  285. /* fallthrough */
  286. case -ERESTARTNOINTR:
  287. regs->rax = regs->orig_rax;
  288. regs->rip -= 2;
  289. break;
  290. }
  291. }
  292. /*
  293. * If TF is set due to a debugger (PT_DTRACE), clear the TF
  294. * flag so that register information in the sigcontext is
  295. * correct.
  296. */
  297. if (unlikely(regs->eflags & TF_MASK)) {
  298. if (likely(current->ptrace & PT_DTRACE)) {
  299. current->ptrace &= ~PT_DTRACE;
  300. regs->eflags &= ~TF_MASK;
  301. }
  302. }
  303. #ifdef CONFIG_IA32_EMULATION
  304. if (test_thread_flag(TIF_IA32)) {
  305. if (ka->sa.sa_flags & SA_SIGINFO)
  306. ret = ia32_setup_rt_frame(sig, ka, info, oldset, regs);
  307. else
  308. ret = ia32_setup_frame(sig, ka, oldset, regs);
  309. } else
  310. #endif
  311. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  312. if (ret == 0) {
  313. spin_lock_irq(&current->sighand->siglock);
  314. sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
  315. if (!(ka->sa.sa_flags & SA_NODEFER))
  316. sigaddset(&current->blocked,sig);
  317. recalc_sigpending();
  318. spin_unlock_irq(&current->sighand->siglock);
  319. }
  320. return ret;
  321. }
  322. /*
  323. * Note that 'init' is a special process: it doesn't get signals it doesn't
  324. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  325. * mistake.
  326. */
  327. static void do_signal(struct pt_regs *regs)
  328. {
  329. struct k_sigaction ka;
  330. siginfo_t info;
  331. int signr;
  332. sigset_t *oldset;
  333. /*
  334. * We want the common case to go fast, which
  335. * is why we may in certain cases get here from
  336. * kernel mode. Just return without doing anything
  337. * if so.
  338. */
  339. if (!user_mode(regs))
  340. return;
  341. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  342. oldset = &current->saved_sigmask;
  343. else
  344. oldset = &current->blocked;
  345. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  346. if (signr > 0) {
  347. /* Reenable any watchpoints before delivering the
  348. * signal to user space. The processor register will
  349. * have been cleared if the watchpoint triggered
  350. * inside the kernel.
  351. */
  352. if (current->thread.debugreg7)
  353. set_debugreg(current->thread.debugreg7, 7);
  354. /* Whee! Actually deliver the signal. */
  355. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  356. /* a signal was successfully delivered; the saved
  357. * sigmask will have been stored in the signal frame,
  358. * and will be restored by sigreturn, so we can simply
  359. * clear the TIF_RESTORE_SIGMASK flag */
  360. clear_thread_flag(TIF_RESTORE_SIGMASK);
  361. }
  362. return;
  363. }
  364. /* Did we come from a system call? */
  365. if ((long)regs->orig_rax >= 0) {
  366. /* Restart the system call - no handlers present */
  367. long res = regs->rax;
  368. switch (res) {
  369. case -ERESTARTNOHAND:
  370. case -ERESTARTSYS:
  371. case -ERESTARTNOINTR:
  372. regs->rax = regs->orig_rax;
  373. regs->rip -= 2;
  374. break;
  375. case -ERESTART_RESTARTBLOCK:
  376. regs->rax = test_thread_flag(TIF_IA32) ?
  377. __NR_ia32_restart_syscall :
  378. __NR_restart_syscall;
  379. regs->rip -= 2;
  380. break;
  381. }
  382. }
  383. /* if there's no signal to deliver, we just put the saved sigmask
  384. back. */
  385. if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
  386. clear_thread_flag(TIF_RESTORE_SIGMASK);
  387. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  388. }
  389. }
  390. void
  391. do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
  392. {
  393. #ifdef DEBUG_SIG
  394. printk("do_notify_resume flags:%x rip:%lx rsp:%lx caller:%p pending:%x\n",
  395. thread_info_flags, regs->rip, regs->rsp, __builtin_return_address(0),signal_pending(current));
  396. #endif
  397. /* Pending single-step? */
  398. if (thread_info_flags & _TIF_SINGLESTEP) {
  399. regs->eflags |= TF_MASK;
  400. clear_thread_flag(TIF_SINGLESTEP);
  401. }
  402. /* deal with pending signal delivery */
  403. if (thread_info_flags & (_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK))
  404. do_signal(regs);
  405. }
  406. void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
  407. {
  408. struct task_struct *me = current;
  409. if (exception_trace)
  410. printk("%s[%d] bad frame in %s frame:%p rip:%lx rsp:%lx orax:%lx\n",
  411. me->comm,me->pid,where,frame,regs->rip,regs->rsp,regs->orig_rax);
  412. force_sig(SIGSEGV, me);
  413. }