signal_64.c 15 KB

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