signal_64.c 14 KB

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