signal_32.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. *
  4. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  5. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  6. */
  7. #include <linux/list.h>
  8. #include <linux/personality.h>
  9. #include <linux/binfmts.h>
  10. #include <linux/suspend.h>
  11. #include <linux/kernel.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/signal.h>
  14. #include <linux/stddef.h>
  15. #include <linux/unistd.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/tracehook.h>
  20. #include <linux/elf.h>
  21. #include <linux/smp.h>
  22. #include <linux/mm.h>
  23. #include <asm/processor.h>
  24. #include <asm/ucontext.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/i387.h>
  27. #include <asm/vdso.h>
  28. #include <asm/syscall.h>
  29. #include <asm/syscalls.h>
  30. #include "sigframe.h"
  31. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  32. #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
  33. X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
  34. X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
  35. X86_EFLAGS_CF)
  36. #ifdef CONFIG_X86_32
  37. # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
  38. #else
  39. # define FIX_EFLAGS __FIX_EFLAGS
  40. #endif
  41. /*
  42. * Atomically swap in the new signal mask, and wait for a signal.
  43. */
  44. asmlinkage int
  45. sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  46. {
  47. mask &= _BLOCKABLE;
  48. spin_lock_irq(&current->sighand->siglock);
  49. current->saved_sigmask = current->blocked;
  50. siginitset(&current->blocked, mask);
  51. recalc_sigpending();
  52. spin_unlock_irq(&current->sighand->siglock);
  53. current->state = TASK_INTERRUPTIBLE;
  54. schedule();
  55. set_restore_sigmask();
  56. return -ERESTARTNOHAND;
  57. }
  58. asmlinkage int
  59. sys_sigaction(int sig, const struct old_sigaction __user *act,
  60. struct old_sigaction __user *oact)
  61. {
  62. struct k_sigaction new_ka, old_ka;
  63. int ret;
  64. if (act) {
  65. old_sigset_t mask;
  66. if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
  67. __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
  68. __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
  69. return -EFAULT;
  70. __get_user(new_ka.sa.sa_flags, &act->sa_flags);
  71. __get_user(mask, &act->sa_mask);
  72. siginitset(&new_ka.sa.sa_mask, mask);
  73. }
  74. ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
  75. if (!ret && oact) {
  76. if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
  77. __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
  78. __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
  79. return -EFAULT;
  80. __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
  81. __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
  82. }
  83. return ret;
  84. }
  85. asmlinkage int sys_sigaltstack(unsigned long bx)
  86. {
  87. /*
  88. * This is needed to make gcc realize it doesn't own the
  89. * "struct pt_regs"
  90. */
  91. struct pt_regs *regs = (struct pt_regs *)&bx;
  92. const stack_t __user *uss = (const stack_t __user *)bx;
  93. stack_t __user *uoss = (stack_t __user *)regs->cx;
  94. return do_sigaltstack(uss, uoss, regs->sp);
  95. }
  96. #define COPY(x) { \
  97. err |= __get_user(regs->x, &sc->x); \
  98. }
  99. #define COPY_SEG(seg) { \
  100. unsigned short tmp; \
  101. err |= __get_user(tmp, &sc->seg); \
  102. regs->seg = tmp; \
  103. }
  104. #define COPY_SEG_STRICT(seg) { \
  105. unsigned short tmp; \
  106. err |= __get_user(tmp, &sc->seg); \
  107. regs->seg = tmp | 3; \
  108. }
  109. #define GET_SEG(seg) { \
  110. unsigned short tmp; \
  111. err |= __get_user(tmp, &sc->seg); \
  112. loadsegment(seg, tmp); \
  113. }
  114. /*
  115. * Do a signal return; undo the signal stack.
  116. */
  117. static int
  118. restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
  119. unsigned long *pax)
  120. {
  121. void __user *buf;
  122. unsigned int tmpflags;
  123. unsigned int err = 0;
  124. /* Always make any pending restarted system calls return -EINTR */
  125. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  126. #ifdef CONFIG_X86_32
  127. GET_SEG(gs);
  128. COPY_SEG(fs);
  129. COPY_SEG(es);
  130. COPY_SEG(ds);
  131. #endif /* CONFIG_X86_32 */
  132. COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
  133. COPY(dx); COPY(cx); COPY(ip);
  134. #ifdef CONFIG_X86_64
  135. COPY(r8);
  136. COPY(r9);
  137. COPY(r10);
  138. COPY(r11);
  139. COPY(r12);
  140. COPY(r13);
  141. COPY(r14);
  142. COPY(r15);
  143. #endif /* CONFIG_X86_64 */
  144. #ifdef CONFIG_X86_32
  145. COPY_SEG_STRICT(cs);
  146. COPY_SEG_STRICT(ss);
  147. #else /* !CONFIG_X86_32 */
  148. /* Kernel saves and restores only the CS segment register on signals,
  149. * which is the bare minimum needed to allow mixed 32/64-bit code.
  150. * App's signal handler can save/restore other segments if needed. */
  151. COPY_SEG_STRICT(cs);
  152. #endif /* CONFIG_X86_32 */
  153. err |= __get_user(tmpflags, &sc->flags);
  154. regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
  155. regs->orig_ax = -1; /* disable syscall checks */
  156. err |= __get_user(buf, &sc->fpstate);
  157. err |= restore_i387_xstate(buf);
  158. err |= __get_user(*pax, &sc->ax);
  159. return err;
  160. }
  161. asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
  162. {
  163. struct sigframe __user *frame;
  164. struct pt_regs *regs;
  165. unsigned long ax;
  166. sigset_t set;
  167. regs = (struct pt_regs *) &__unused;
  168. frame = (struct sigframe __user *)(regs->sp - 8);
  169. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  170. goto badframe;
  171. if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
  172. && __copy_from_user(&set.sig[1], &frame->extramask,
  173. sizeof(frame->extramask))))
  174. goto badframe;
  175. sigdelsetmask(&set, ~_BLOCKABLE);
  176. spin_lock_irq(&current->sighand->siglock);
  177. current->blocked = set;
  178. recalc_sigpending();
  179. spin_unlock_irq(&current->sighand->siglock);
  180. if (restore_sigcontext(regs, &frame->sc, &ax))
  181. goto badframe;
  182. return ax;
  183. badframe:
  184. if (show_unhandled_signals && printk_ratelimit()) {
  185. printk("%s%s[%d] bad frame in sigreturn frame:"
  186. "%p ip:%lx sp:%lx oeax:%lx",
  187. task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
  188. current->comm, task_pid_nr(current), frame, regs->ip,
  189. regs->sp, regs->orig_ax);
  190. print_vma_addr(" in ", regs->ip);
  191. printk(KERN_CONT "\n");
  192. }
  193. force_sig(SIGSEGV, current);
  194. return 0;
  195. }
  196. static long do_rt_sigreturn(struct pt_regs *regs)
  197. {
  198. struct rt_sigframe __user *frame;
  199. unsigned long ax;
  200. sigset_t set;
  201. frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
  202. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  203. goto badframe;
  204. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  205. goto badframe;
  206. sigdelsetmask(&set, ~_BLOCKABLE);
  207. spin_lock_irq(&current->sighand->siglock);
  208. current->blocked = set;
  209. recalc_sigpending();
  210. spin_unlock_irq(&current->sighand->siglock);
  211. if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
  212. goto badframe;
  213. if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
  214. goto badframe;
  215. return ax;
  216. badframe:
  217. signal_fault(regs, frame, "rt_sigreturn");
  218. return 0;
  219. }
  220. asmlinkage int sys_rt_sigreturn(unsigned long __unused)
  221. {
  222. struct pt_regs *regs = (struct pt_regs *)&__unused;
  223. return do_rt_sigreturn(regs);
  224. }
  225. /*
  226. * Set up a signal frame.
  227. */
  228. static int
  229. setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
  230. struct pt_regs *regs, unsigned long mask)
  231. {
  232. int err = 0;
  233. #ifdef CONFIG_X86_32
  234. {
  235. unsigned int tmp;
  236. savesegment(gs, tmp);
  237. err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
  238. }
  239. err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
  240. err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
  241. err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
  242. #endif /* CONFIG_X86_32 */
  243. err |= __put_user(regs->di, &sc->di);
  244. err |= __put_user(regs->si, &sc->si);
  245. err |= __put_user(regs->bp, &sc->bp);
  246. err |= __put_user(regs->sp, &sc->sp);
  247. err |= __put_user(regs->bx, &sc->bx);
  248. err |= __put_user(regs->dx, &sc->dx);
  249. err |= __put_user(regs->cx, &sc->cx);
  250. err |= __put_user(regs->ax, &sc->ax);
  251. #ifdef CONFIG_X86_64
  252. err |= __put_user(regs->r8, &sc->r8);
  253. err |= __put_user(regs->r9, &sc->r9);
  254. err |= __put_user(regs->r10, &sc->r10);
  255. err |= __put_user(regs->r11, &sc->r11);
  256. err |= __put_user(regs->r12, &sc->r12);
  257. err |= __put_user(regs->r13, &sc->r13);
  258. err |= __put_user(regs->r14, &sc->r14);
  259. err |= __put_user(regs->r15, &sc->r15);
  260. #endif /* CONFIG_X86_64 */
  261. err |= __put_user(current->thread.trap_no, &sc->trapno);
  262. err |= __put_user(current->thread.error_code, &sc->err);
  263. err |= __put_user(regs->ip, &sc->ip);
  264. #ifdef CONFIG_X86_32
  265. err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
  266. err |= __put_user(regs->flags, &sc->flags);
  267. err |= __put_user(regs->sp, &sc->sp_at_signal);
  268. err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
  269. #else /* !CONFIG_X86_32 */
  270. err |= __put_user(regs->flags, &sc->flags);
  271. err |= __put_user(regs->cs, &sc->cs);
  272. err |= __put_user(0, &sc->gs);
  273. err |= __put_user(0, &sc->fs);
  274. #endif /* CONFIG_X86_32 */
  275. err |= __put_user(fpstate, &sc->fpstate);
  276. /* non-iBCS2 extensions.. */
  277. err |= __put_user(mask, &sc->oldmask);
  278. err |= __put_user(current->thread.cr2, &sc->cr2);
  279. return err;
  280. }
  281. /*
  282. * Determine which stack to use..
  283. */
  284. static inline void __user *
  285. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
  286. void **fpstate)
  287. {
  288. unsigned long sp;
  289. /* Default to using normal stack */
  290. sp = regs->sp;
  291. /*
  292. * If we are on the alternate signal stack and would overflow it, don't.
  293. * Return an always-bogus address instead so we will die with SIGSEGV.
  294. */
  295. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
  296. return (void __user *) -1L;
  297. /* This is the X/Open sanctioned signal stack switching. */
  298. if (ka->sa.sa_flags & SA_ONSTACK) {
  299. if (sas_ss_flags(sp) == 0)
  300. sp = current->sas_ss_sp + current->sas_ss_size;
  301. } else {
  302. /* This is the legacy signal stack switching. */
  303. if ((regs->ss & 0xffff) != __USER_DS &&
  304. !(ka->sa.sa_flags & SA_RESTORER) &&
  305. ka->sa.sa_restorer)
  306. sp = (unsigned long) ka->sa.sa_restorer;
  307. }
  308. if (used_math()) {
  309. sp = sp - sig_xstate_size;
  310. *fpstate = (struct _fpstate *) sp;
  311. if (save_i387_xstate(*fpstate) < 0)
  312. return (void __user *)-1L;
  313. }
  314. sp -= frame_size;
  315. /*
  316. * Align the stack pointer according to the i386 ABI,
  317. * i.e. so that on function entry ((sp + 4) & 15) == 0.
  318. */
  319. sp = ((sp + 4) & -16ul) - 4;
  320. return (void __user *) sp;
  321. }
  322. static int
  323. __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
  324. struct pt_regs *regs)
  325. {
  326. struct sigframe __user *frame;
  327. void __user *restorer;
  328. int err = 0;
  329. void __user *fpstate = NULL;
  330. frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
  331. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  332. return -EFAULT;
  333. if (__put_user(sig, &frame->sig))
  334. return -EFAULT;
  335. if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
  336. return -EFAULT;
  337. if (_NSIG_WORDS > 1) {
  338. if (__copy_to_user(&frame->extramask, &set->sig[1],
  339. sizeof(frame->extramask)))
  340. return -EFAULT;
  341. }
  342. if (current->mm->context.vdso)
  343. restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
  344. else
  345. restorer = &frame->retcode;
  346. if (ka->sa.sa_flags & SA_RESTORER)
  347. restorer = ka->sa.sa_restorer;
  348. /* Set up to return from userspace. */
  349. err |= __put_user(restorer, &frame->pretcode);
  350. /*
  351. * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
  352. *
  353. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  354. * reasons and because gdb uses it as a signature to notice
  355. * signal handler stack frames.
  356. */
  357. err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
  358. err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
  359. err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
  360. if (err)
  361. return -EFAULT;
  362. /* Set up registers for signal handler */
  363. regs->sp = (unsigned long)frame;
  364. regs->ip = (unsigned long)ka->sa.sa_handler;
  365. regs->ax = (unsigned long)sig;
  366. regs->dx = 0;
  367. regs->cx = 0;
  368. regs->ds = __USER_DS;
  369. regs->es = __USER_DS;
  370. regs->ss = __USER_DS;
  371. regs->cs = __USER_CS;
  372. return 0;
  373. }
  374. static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  375. sigset_t *set, struct pt_regs *regs)
  376. {
  377. struct rt_sigframe __user *frame;
  378. void __user *restorer;
  379. int err = 0;
  380. void __user *fpstate = NULL;
  381. frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
  382. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  383. return -EFAULT;
  384. err |= __put_user(sig, &frame->sig);
  385. err |= __put_user(&frame->info, &frame->pinfo);
  386. err |= __put_user(&frame->uc, &frame->puc);
  387. err |= copy_siginfo_to_user(&frame->info, info);
  388. if (err)
  389. return -EFAULT;
  390. /* Create the ucontext. */
  391. if (cpu_has_xsave)
  392. err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
  393. else
  394. err |= __put_user(0, &frame->uc.uc_flags);
  395. err |= __put_user(0, &frame->uc.uc_link);
  396. err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  397. err |= __put_user(sas_ss_flags(regs->sp),
  398. &frame->uc.uc_stack.ss_flags);
  399. err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  400. err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  401. regs, set->sig[0]);
  402. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  403. if (err)
  404. return -EFAULT;
  405. /* Set up to return from userspace. */
  406. restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
  407. if (ka->sa.sa_flags & SA_RESTORER)
  408. restorer = ka->sa.sa_restorer;
  409. err |= __put_user(restorer, &frame->pretcode);
  410. /*
  411. * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
  412. *
  413. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  414. * reasons and because gdb uses it as a signature to notice
  415. * signal handler stack frames.
  416. */
  417. err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
  418. err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
  419. err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
  420. if (err)
  421. return -EFAULT;
  422. /* Set up registers for signal handler */
  423. regs->sp = (unsigned long)frame;
  424. regs->ip = (unsigned long)ka->sa.sa_handler;
  425. regs->ax = (unsigned long)sig;
  426. regs->dx = (unsigned long)&frame->info;
  427. regs->cx = (unsigned long)&frame->uc;
  428. regs->ds = __USER_DS;
  429. regs->es = __USER_DS;
  430. regs->ss = __USER_DS;
  431. regs->cs = __USER_CS;
  432. return 0;
  433. }
  434. /*
  435. * OK, we're invoking a handler:
  436. */
  437. static int signr_convert(int sig)
  438. {
  439. #ifdef CONFIG_X86_32
  440. struct thread_info *info = current_thread_info();
  441. if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
  442. return info->exec_domain->signal_invmap[sig];
  443. #endif /* CONFIG_X86_32 */
  444. return sig;
  445. }
  446. #ifdef CONFIG_X86_32
  447. #define is_ia32 1
  448. #define ia32_setup_frame __setup_frame
  449. #define ia32_setup_rt_frame __setup_rt_frame
  450. #else /* !CONFIG_X86_32 */
  451. #ifdef CONFIG_IA32_EMULATION
  452. #define is_ia32 test_thread_flag(TIF_IA32)
  453. #else /* !CONFIG_IA32_EMULATION */
  454. #define is_ia32 0
  455. #endif /* CONFIG_IA32_EMULATION */
  456. #endif /* CONFIG_X86_32 */
  457. static int
  458. setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  459. sigset_t *set, struct pt_regs *regs)
  460. {
  461. int usig = signr_convert(sig);
  462. int ret;
  463. /* Set up the stack frame */
  464. if (is_ia32) {
  465. if (ka->sa.sa_flags & SA_SIGINFO)
  466. ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
  467. else
  468. ret = ia32_setup_frame(usig, ka, set, regs);
  469. } else
  470. ret = __setup_rt_frame(sig, ka, info, set, regs);
  471. if (ret) {
  472. force_sigsegv(sig, current);
  473. return -EFAULT;
  474. }
  475. return ret;
  476. }
  477. static int
  478. handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
  479. sigset_t *oldset, struct pt_regs *regs)
  480. {
  481. int ret;
  482. /* Are we from a system call? */
  483. if (syscall_get_nr(current, regs) >= 0) {
  484. /* If so, check system call restarting.. */
  485. switch (syscall_get_error(current, regs)) {
  486. case -ERESTART_RESTARTBLOCK:
  487. case -ERESTARTNOHAND:
  488. regs->ax = -EINTR;
  489. break;
  490. case -ERESTARTSYS:
  491. if (!(ka->sa.sa_flags & SA_RESTART)) {
  492. regs->ax = -EINTR;
  493. break;
  494. }
  495. /* fallthrough */
  496. case -ERESTARTNOINTR:
  497. regs->ax = regs->orig_ax;
  498. regs->ip -= 2;
  499. break;
  500. }
  501. }
  502. /*
  503. * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
  504. * flag so that register information in the sigcontext is correct.
  505. */
  506. if (unlikely(regs->flags & X86_EFLAGS_TF) &&
  507. likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
  508. regs->flags &= ~X86_EFLAGS_TF;
  509. ret = setup_rt_frame(sig, ka, info, oldset, regs);
  510. if (ret)
  511. return ret;
  512. #ifdef CONFIG_X86_64
  513. /*
  514. * This has nothing to do with segment registers,
  515. * despite the name. This magic affects uaccess.h
  516. * macros' behavior. Reset it to the normal setting.
  517. */
  518. set_fs(USER_DS);
  519. #endif
  520. /*
  521. * Clear the direction flag as per the ABI for function entry.
  522. */
  523. regs->flags &= ~X86_EFLAGS_DF;
  524. /*
  525. * Clear TF when entering the signal handler, but
  526. * notify any tracer that was single-stepping it.
  527. * The tracer may want to single-step inside the
  528. * handler too.
  529. */
  530. regs->flags &= ~X86_EFLAGS_TF;
  531. spin_lock_irq(&current->sighand->siglock);
  532. sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
  533. if (!(ka->sa.sa_flags & SA_NODEFER))
  534. sigaddset(&current->blocked, sig);
  535. recalc_sigpending();
  536. spin_unlock_irq(&current->sighand->siglock);
  537. tracehook_signal_handler(sig, info, ka, regs,
  538. test_thread_flag(TIF_SINGLESTEP));
  539. return 0;
  540. }
  541. #ifdef CONFIG_X86_32
  542. #define NR_restart_syscall __NR_restart_syscall
  543. #else /* !CONFIG_X86_32 */
  544. #define NR_restart_syscall \
  545. test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
  546. #endif /* CONFIG_X86_32 */
  547. /*
  548. * Note that 'init' is a special process: it doesn't get signals it doesn't
  549. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  550. * mistake.
  551. */
  552. static void do_signal(struct pt_regs *regs)
  553. {
  554. struct k_sigaction ka;
  555. siginfo_t info;
  556. int signr;
  557. sigset_t *oldset;
  558. /*
  559. * We want the common case to go fast, which is why we may in certain
  560. * cases get here from kernel mode. Just return without doing anything
  561. * if so.
  562. * X86_32: vm86 regs switched out by assembly code before reaching
  563. * here, so testing against kernel CS suffices.
  564. */
  565. if (!user_mode(regs))
  566. return;
  567. if (current_thread_info()->status & TS_RESTORE_SIGMASK)
  568. oldset = &current->saved_sigmask;
  569. else
  570. oldset = &current->blocked;
  571. signr = get_signal_to_deliver(&info, &ka, regs, NULL);
  572. if (signr > 0) {
  573. /*
  574. * Re-enable any watchpoints before delivering the
  575. * signal to user space. The processor register will
  576. * have been cleared if the watchpoint triggered
  577. * inside the kernel.
  578. */
  579. if (current->thread.debugreg7)
  580. set_debugreg(current->thread.debugreg7, 7);
  581. /* Whee! Actually deliver the signal. */
  582. if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
  583. /*
  584. * A signal was successfully delivered; the saved
  585. * sigmask will have been stored in the signal frame,
  586. * and will be restored by sigreturn, so we can simply
  587. * clear the TS_RESTORE_SIGMASK flag.
  588. */
  589. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  590. }
  591. return;
  592. }
  593. /* Did we come from a system call? */
  594. if (syscall_get_nr(current, regs) >= 0) {
  595. /* Restart the system call - no handlers present */
  596. switch (syscall_get_error(current, regs)) {
  597. case -ERESTARTNOHAND:
  598. case -ERESTARTSYS:
  599. case -ERESTARTNOINTR:
  600. regs->ax = regs->orig_ax;
  601. regs->ip -= 2;
  602. break;
  603. case -ERESTART_RESTARTBLOCK:
  604. regs->ax = NR_restart_syscall;
  605. regs->ip -= 2;
  606. break;
  607. }
  608. }
  609. /*
  610. * If there's no signal to deliver, we just put the saved sigmask
  611. * back.
  612. */
  613. if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
  614. current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
  615. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  616. }
  617. }
  618. /*
  619. * notification of userspace execution resumption
  620. * - triggered by the TIF_WORK_MASK flags
  621. */
  622. void
  623. do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
  624. {
  625. #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
  626. /* notify userspace of pending MCEs */
  627. if (thread_info_flags & _TIF_MCE_NOTIFY)
  628. mce_notify_user();
  629. #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
  630. /* deal with pending signal delivery */
  631. if (thread_info_flags & _TIF_SIGPENDING)
  632. do_signal(regs);
  633. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  634. clear_thread_flag(TIF_NOTIFY_RESUME);
  635. tracehook_notify_resume(regs);
  636. }
  637. #ifdef CONFIG_X86_32
  638. clear_thread_flag(TIF_IRET);
  639. #endif /* CONFIG_X86_32 */
  640. }
  641. void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
  642. {
  643. struct task_struct *me = current;
  644. if (show_unhandled_signals && printk_ratelimit()) {
  645. printk(KERN_INFO
  646. "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
  647. me->comm, me->pid, where, frame,
  648. regs->ip, regs->sp, regs->orig_ax);
  649. print_vma_addr(" in ", regs->ip);
  650. printk(KERN_CONT "\n");
  651. }
  652. force_sig(SIGSEGV, me);
  653. }