signal_64.c 13 KB

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