signal_32.c 20 KB

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