signal.c 22 KB

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