signal_64.c 14 KB

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