signal_64.c 14 KB

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