signal_64.c 14 KB

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