signal_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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, void __user *fpstate,
  136. struct pt_regs *regs, unsigned long mask)
  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(current->thread.trap_no, &sc->trapno);
  159. err |= __put_user(current->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(fpstate, &sc->fpstate);
  163. err |= __put_user(mask, &sc->oldmask);
  164. err |= __put_user(current->thread.cr2, &sc->cr2);
  165. return err;
  166. }
  167. /*
  168. * Determine which stack to use..
  169. */
  170. static void __user *
  171. get_stack(struct k_sigaction *ka, unsigned long sp, unsigned long size)
  172. {
  173. /* Default to using normal stack - redzone*/
  174. 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, 64);
  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. void __user *fp = NULL;
  187. int err = 0;
  188. struct task_struct *me = current;
  189. if (used_math()) {
  190. fp = get_stack(ka, regs->sp, sig_xstate_size);
  191. frame = (void __user *)round_down(
  192. (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
  193. if (save_i387_xstate(fp) < 0)
  194. return -EFAULT;
  195. } else
  196. frame = get_stack(ka, regs->sp, sizeof(struct rt_sigframe)) - 8;
  197. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  198. return -EFAULT;
  199. if (ka->sa.sa_flags & SA_SIGINFO) {
  200. if (copy_siginfo_to_user(&frame->info, info))
  201. return -EFAULT;
  202. }
  203. /* Create the ucontext. */
  204. if (cpu_has_xsave)
  205. err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
  206. else
  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, fp, regs, set->sig[0]);
  214. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  215. /* Set up to return from userspace. If provided, use a stub
  216. already in userspace. */
  217. /* x86-64 should always use SA_RESTORER. */
  218. if (ka->sa.sa_flags & SA_RESTORER) {
  219. err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
  220. } else {
  221. /* could use a vstub here */
  222. return -EFAULT;
  223. }
  224. if (err)
  225. return -EFAULT;
  226. /* Set up registers for signal handler */
  227. regs->di = sig;
  228. /* In case the signal handler was declared without prototypes */
  229. regs->ax = 0;
  230. /* This also works for non SA_SIGINFO handlers because they expect the
  231. next argument after the signal number on the stack. */
  232. regs->si = (unsigned long)&frame->info;
  233. regs->dx = (unsigned long)&frame->uc;
  234. regs->ip = (unsigned long) ka->sa.sa_handler;
  235. regs->sp = (unsigned long)frame;
  236. /* Set up the CS register to run signal handlers in 64-bit mode,
  237. even if the handler happens to be interrupting 32-bit code. */
  238. regs->cs = __USER_CS;
  239. return 0;
  240. }
  241. /*
  242. * OK, we're invoking a handler
  243. */
  244. static int signr_convert(int sig)
  245. {
  246. #ifdef CONFIG_X86_32
  247. struct thread_info *info = current_thread_info();
  248. if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
  249. return info->exec_domain->signal_invmap[sig];
  250. #endif /* CONFIG_X86_32 */
  251. return sig;
  252. }
  253. #ifdef CONFIG_X86_32
  254. #define is_ia32 1
  255. #define ia32_setup_frame __setup_frame
  256. #define ia32_setup_rt_frame __setup_rt_frame
  257. #else /* !CONFIG_X86_32 */
  258. #ifdef CONFIG_IA32_EMULATION
  259. #define is_ia32 test_thread_flag(TIF_IA32)
  260. #else /* !CONFIG_IA32_EMULATION */
  261. #define is_ia32 0
  262. #endif /* CONFIG_IA32_EMULATION */
  263. #endif /* CONFIG_X86_32 */
  264. static int
  265. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  266. sigset_t *set, struct pt_regs *regs)
  267. {
  268. int usig = signr_convert(sig);
  269. int ret;
  270. /* Set up the stack frame */
  271. if (is_ia32) {
  272. if (ka->sa.sa_flags & SA_SIGINFO)
  273. ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
  274. else
  275. ret = ia32_setup_frame(usig, ka, set, regs);
  276. } else
  277. ret = __setup_rt_frame(sig, ka, info, set, regs);
  278. if (ret) {
  279. force_sigsegv(sig, current);
  280. return -EFAULT;
  281. }
  282. return ret;
  283. }
  284. static int
  285. handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
  286. sigset_t *oldset, struct pt_regs *regs)
  287. {
  288. int ret;
  289. /* Are we from a system call? */
  290. if (syscall_get_nr(current, regs) >= 0) {
  291. /* If so, check system call restarting.. */
  292. switch (syscall_get_error(current, regs)) {
  293. case -ERESTART_RESTARTBLOCK:
  294. case -ERESTARTNOHAND:
  295. regs->ax = -EINTR;
  296. break;
  297. case -ERESTARTSYS:
  298. if (!(ka->sa.sa_flags & SA_RESTART)) {
  299. regs->ax = -EINTR;
  300. break;
  301. }
  302. /* fallthrough */
  303. case -ERESTARTNOINTR:
  304. regs->ax = regs->orig_ax;
  305. regs->ip -= 2;
  306. break;
  307. }
  308. }
  309. /*
  310. * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
  311. * flag so that register information in the sigcontext is correct.
  312. */
  313. if (unlikely(regs->flags & X86_EFLAGS_TF) &&
  314. likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
  315. regs->flags &= ~X86_EFLAGS_TF;
  316. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  317. if (ret)
  318. return ret;
  319. #ifdef CONFIG_X86_64
  320. /*
  321. * This has nothing to do with segment registers,
  322. * despite the name. This magic affects uaccess.h
  323. * macros' behavior. Reset it to the normal setting.
  324. */
  325. set_fs(USER_DS);
  326. #endif
  327. /*
  328. * Clear the direction flag as per the ABI for function entry.
  329. */
  330. regs->flags &= ~X86_EFLAGS_DF;
  331. /*
  332. * Clear TF when entering the signal handler, but
  333. * notify any tracer that was single-stepping it.
  334. * The tracer may want to single-step inside the
  335. * handler too.
  336. */
  337. regs->flags &= ~X86_EFLAGS_TF;
  338. spin_lock_irq(&current->sighand->siglock);
  339. sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
  340. if (!(ka->sa.sa_flags & SA_NODEFER))
  341. sigaddset(&current->blocked, sig);
  342. recalc_sigpending();
  343. spin_unlock_irq(&current->sighand->siglock);
  344. tracehook_signal_handler(sig, info, ka, regs,
  345. test_thread_flag(TIF_SINGLESTEP));
  346. return 0;
  347. }
  348. #ifdef CONFIG_X86_32
  349. #define NR_restart_syscall __NR_restart_syscall
  350. #else /* !CONFIG_X86_32 */
  351. #define NR_restart_syscall \
  352. test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
  353. #endif /* CONFIG_X86_32 */
  354. /*
  355. * Note that 'init' is a special process: it doesn't get signals it doesn't
  356. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  357. * mistake.
  358. */
  359. static void do_signal(struct pt_regs *regs)
  360. {
  361. struct k_sigaction ka;
  362. siginfo_t info;
  363. int signr;
  364. sigset_t *oldset;
  365. /*
  366. * We want the common case to go fast, which is why we may in certain
  367. * cases get here from kernel mode. Just return without doing anything
  368. * if so.
  369. * X86_32: vm86 regs switched out by assembly code before reaching
  370. * here, so testing against kernel CS suffices.
  371. */
  372. if (!user_mode(regs))
  373. return;
  374. if (current_thread_info()->status & TS_RESTORE_SIGMASK)
  375. oldset = &current->saved_sigmask;
  376. else
  377. oldset = &current->blocked;
  378. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  379. if (signr > 0) {
  380. /*
  381. * Re-enable any watchpoints before delivering the
  382. * signal to user space. The processor register will
  383. * have been cleared if the watchpoint triggered
  384. * inside the kernel.
  385. */
  386. if (current->thread.debugreg7)
  387. set_debugreg(current->thread.debugreg7, 7);
  388. /* Whee! Actually deliver the signal. */
  389. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  390. /*
  391. * A signal was successfully delivered; the saved
  392. * sigmask will have been stored in the signal frame,
  393. * and will be restored by sigreturn, so we can simply
  394. * clear the TS_RESTORE_SIGMASK flag.
  395. */
  396. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  397. }
  398. return;
  399. }
  400. /* Did we come from a system call? */
  401. if (syscall_get_nr(current, regs) >= 0) {
  402. /* Restart the system call - no handlers present */
  403. switch (syscall_get_error(current, regs)) {
  404. case -ERESTARTNOHAND:
  405. case -ERESTARTSYS:
  406. case -ERESTARTNOINTR:
  407. regs->ax = regs->orig_ax;
  408. regs->ip -= 2;
  409. break;
  410. case -ERESTART_RESTARTBLOCK:
  411. regs->ax = NR_restart_syscall;
  412. regs->ip -= 2;
  413. break;
  414. }
  415. }
  416. /*
  417. * If there's no signal to deliver, we just put the saved sigmask
  418. * back.
  419. */
  420. if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
  421. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  422. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  423. }
  424. }
  425. /*
  426. * notification of userspace execution resumption
  427. * - triggered by the TIF_WORK_MASK flags
  428. */
  429. void
  430. do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
  431. {
  432. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
  433. /* notify userspace of pending MCEs */
  434. if (thread_info_flags & _TIF_MCE_NOTIFY)
  435. mce_notify_user();
  436. #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
  437. /* deal with pending signal delivery */
  438. if (thread_info_flags & _TIF_SIGPENDING)
  439. do_signal(regs);
  440. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  441. clear_thread_flag(TIF_NOTIFY_RESUME);
  442. tracehook_notify_resume(regs);
  443. }
  444. #ifdef CONFIG_X86_32
  445. clear_thread_flag(TIF_IRET);
  446. #endif /* CONFIG_X86_32 */
  447. }
  448. void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
  449. {
  450. struct task_struct *me = current;
  451. if (show_unhandled_signals && printk_ratelimit()) {
  452. printk(KERN_INFO
  453. "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
  454. me->comm, me->pid, where, frame,
  455. regs->ip, regs->sp, regs->orig_ax);
  456. print_vma_addr(" in ", regs->ip);
  457. printk(KERN_CONT "\n");
  458. }
  459. force_sig(SIGSEGV, me);
  460. }