signal.c 24 KB

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