signal_64.c 14 KB

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