signal.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/wait.h>
  16. #include <linux/tracehook.h>
  17. #include <linux/unistd.h>
  18. #include <linux/stddef.h>
  19. #include <linux/personality.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/user-return-notifier.h>
  22. #include <linux/uprobes.h>
  23. #include <linux/context_tracking.h>
  24. #include <asm/processor.h>
  25. #include <asm/ucontext.h>
  26. #include <asm/fpu/internal.h>
  27. #include <asm/fpu/signal.h>
  28. #include <asm/vdso.h>
  29. #include <asm/mce.h>
  30. #include <asm/sighandling.h>
  31. #include <asm/vm86.h>
  32. #ifdef CONFIG_X86_64
  33. #include <asm/proto.h>
  34. #include <asm/ia32_unistd.h>
  35. #endif /* CONFIG_X86_64 */
  36. #include <asm/syscall.h>
  37. #include <asm/syscalls.h>
  38. #include <asm/sigframe.h>
  39. #define COPY(x) do { \
  40. get_user_ex(regs->x, &sc->x); \
  41. } while (0)
  42. #define GET_SEG(seg) ({ \
  43. unsigned short tmp; \
  44. get_user_ex(tmp, &sc->seg); \
  45. tmp; \
  46. })
  47. #define COPY_SEG(seg) do { \
  48. regs->seg = GET_SEG(seg); \
  49. } while (0)
  50. #define COPY_SEG_CPL3(seg) do { \
  51. regs->seg = GET_SEG(seg) | 3; \
  52. } while (0)
  53. int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
  54. {
  55. unsigned long buf_val;
  56. void __user *buf;
  57. unsigned int tmpflags;
  58. unsigned int err = 0;
  59. /* Always make any pending restarted system calls return -EINTR */
  60. current->restart_block.fn = do_no_restart_syscall;
  61. get_user_try {
  62. #ifdef CONFIG_X86_32
  63. set_user_gs(regs, GET_SEG(gs));
  64. COPY_SEG(fs);
  65. COPY_SEG(es);
  66. COPY_SEG(ds);
  67. #endif /* CONFIG_X86_32 */
  68. COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
  69. COPY(dx); COPY(cx); COPY(ip); COPY(ax);
  70. #ifdef CONFIG_X86_64
  71. COPY(r8);
  72. COPY(r9);
  73. COPY(r10);
  74. COPY(r11);
  75. COPY(r12);
  76. COPY(r13);
  77. COPY(r14);
  78. COPY(r15);
  79. #endif /* CONFIG_X86_64 */
  80. #ifdef CONFIG_X86_32
  81. COPY_SEG_CPL3(cs);
  82. COPY_SEG_CPL3(ss);
  83. #else /* !CONFIG_X86_32 */
  84. /* Kernel saves and restores only the CS segment register on signals,
  85. * which is the bare minimum needed to allow mixed 32/64-bit code.
  86. * App's signal handler can save/restore other segments if needed. */
  87. COPY_SEG_CPL3(cs);
  88. #endif /* CONFIG_X86_32 */
  89. get_user_ex(tmpflags, &sc->flags);
  90. regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
  91. regs->orig_ax = -1; /* disable syscall checks */
  92. get_user_ex(buf_val, &sc->fpstate);
  93. buf = (void __user *)buf_val;
  94. } get_user_catch(err);
  95. err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
  96. force_iret();
  97. return err;
  98. }
  99. int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
  100. struct pt_regs *regs, unsigned long mask)
  101. {
  102. int err = 0;
  103. put_user_try {
  104. #ifdef CONFIG_X86_32
  105. put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
  106. put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
  107. put_user_ex(regs->es, (unsigned int __user *)&sc->es);
  108. put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
  109. #endif /* CONFIG_X86_32 */
  110. put_user_ex(regs->di, &sc->di);
  111. put_user_ex(regs->si, &sc->si);
  112. put_user_ex(regs->bp, &sc->bp);
  113. put_user_ex(regs->sp, &sc->sp);
  114. put_user_ex(regs->bx, &sc->bx);
  115. put_user_ex(regs->dx, &sc->dx);
  116. put_user_ex(regs->cx, &sc->cx);
  117. put_user_ex(regs->ax, &sc->ax);
  118. #ifdef CONFIG_X86_64
  119. put_user_ex(regs->r8, &sc->r8);
  120. put_user_ex(regs->r9, &sc->r9);
  121. put_user_ex(regs->r10, &sc->r10);
  122. put_user_ex(regs->r11, &sc->r11);
  123. put_user_ex(regs->r12, &sc->r12);
  124. put_user_ex(regs->r13, &sc->r13);
  125. put_user_ex(regs->r14, &sc->r14);
  126. put_user_ex(regs->r15, &sc->r15);
  127. #endif /* CONFIG_X86_64 */
  128. put_user_ex(current->thread.trap_nr, &sc->trapno);
  129. put_user_ex(current->thread.error_code, &sc->err);
  130. put_user_ex(regs->ip, &sc->ip);
  131. #ifdef CONFIG_X86_32
  132. put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
  133. put_user_ex(regs->flags, &sc->flags);
  134. put_user_ex(regs->sp, &sc->sp_at_signal);
  135. put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
  136. #else /* !CONFIG_X86_32 */
  137. put_user_ex(regs->flags, &sc->flags);
  138. put_user_ex(regs->cs, &sc->cs);
  139. put_user_ex(0, &sc->gs);
  140. put_user_ex(0, &sc->fs);
  141. #endif /* CONFIG_X86_32 */
  142. put_user_ex(fpstate, &sc->fpstate);
  143. /* non-iBCS2 extensions.. */
  144. put_user_ex(mask, &sc->oldmask);
  145. put_user_ex(current->thread.cr2, &sc->cr2);
  146. } put_user_catch(err);
  147. return err;
  148. }
  149. /*
  150. * Set up a signal frame.
  151. */
  152. /*
  153. * Determine which stack to use..
  154. */
  155. static unsigned long align_sigframe(unsigned long sp)
  156. {
  157. #ifdef CONFIG_X86_32
  158. /*
  159. * Align the stack pointer according to the i386 ABI,
  160. * i.e. so that on function entry ((sp + 4) & 15) == 0.
  161. */
  162. sp = ((sp + 4) & -16ul) - 4;
  163. #else /* !CONFIG_X86_32 */
  164. sp = round_down(sp, 16) - 8;
  165. #endif
  166. return sp;
  167. }
  168. static void __user *
  169. get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
  170. void __user **fpstate)
  171. {
  172. /* Default to using normal stack */
  173. unsigned long math_size = 0;
  174. unsigned long sp = regs->sp;
  175. unsigned long buf_fx = 0;
  176. int onsigstack = on_sig_stack(sp);
  177. struct fpu *fpu = &current->thread.fpu;
  178. /* redzone */
  179. if (config_enabled(CONFIG_X86_64))
  180. sp -= 128;
  181. if (!onsigstack) {
  182. /* This is the X/Open sanctioned signal stack switching. */
  183. if (ka->sa.sa_flags & SA_ONSTACK) {
  184. if (current->sas_ss_size)
  185. sp = current->sas_ss_sp + current->sas_ss_size;
  186. } else if (config_enabled(CONFIG_X86_32) &&
  187. (regs->ss & 0xffff) != __USER_DS &&
  188. !(ka->sa.sa_flags & SA_RESTORER) &&
  189. ka->sa.sa_restorer) {
  190. /* This is the legacy signal stack switching. */
  191. sp = (unsigned long) ka->sa.sa_restorer;
  192. }
  193. }
  194. if (fpu->fpstate_active) {
  195. sp = fpu__alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
  196. &buf_fx, &math_size);
  197. *fpstate = (void __user *)sp;
  198. }
  199. sp = align_sigframe(sp - frame_size);
  200. /*
  201. * If we are on the alternate signal stack and would overflow it, don't.
  202. * Return an always-bogus address instead so we will die with SIGSEGV.
  203. */
  204. if (onsigstack && !likely(on_sig_stack(sp)))
  205. return (void __user *)-1L;
  206. /* save i387 and extended state */
  207. if (fpu->fpstate_active &&
  208. copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
  209. return (void __user *)-1L;
  210. return (void __user *)sp;
  211. }
  212. #ifdef CONFIG_X86_32
  213. static const struct {
  214. u16 poplmovl;
  215. u32 val;
  216. u16 int80;
  217. } __attribute__((packed)) retcode = {
  218. 0xb858, /* popl %eax; movl $..., %eax */
  219. __NR_sigreturn,
  220. 0x80cd, /* int $0x80 */
  221. };
  222. static const struct {
  223. u8 movl;
  224. u32 val;
  225. u16 int80;
  226. u8 pad;
  227. } __attribute__((packed)) rt_retcode = {
  228. 0xb8, /* movl $..., %eax */
  229. __NR_rt_sigreturn,
  230. 0x80cd, /* int $0x80 */
  231. 0
  232. };
  233. static int
  234. __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
  235. struct pt_regs *regs)
  236. {
  237. struct sigframe __user *frame;
  238. void __user *restorer;
  239. int err = 0;
  240. void __user *fpstate = NULL;
  241. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
  242. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  243. return -EFAULT;
  244. if (__put_user(sig, &frame->sig))
  245. return -EFAULT;
  246. if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
  247. return -EFAULT;
  248. if (_NSIG_WORDS > 1) {
  249. if (__copy_to_user(&frame->extramask, &set->sig[1],
  250. sizeof(frame->extramask)))
  251. return -EFAULT;
  252. }
  253. if (current->mm->context.vdso)
  254. restorer = current->mm->context.vdso +
  255. vdso_image_32.sym___kernel_sigreturn;
  256. else
  257. restorer = &frame->retcode;
  258. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  259. restorer = ksig->ka.sa.sa_restorer;
  260. /* Set up to return from userspace. */
  261. err |= __put_user(restorer, &frame->pretcode);
  262. /*
  263. * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
  264. *
  265. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  266. * reasons and because gdb uses it as a signature to notice
  267. * signal handler stack frames.
  268. */
  269. err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
  270. if (err)
  271. return -EFAULT;
  272. /* Set up registers for signal handler */
  273. regs->sp = (unsigned long)frame;
  274. regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
  275. regs->ax = (unsigned long)sig;
  276. regs->dx = 0;
  277. regs->cx = 0;
  278. regs->ds = __USER_DS;
  279. regs->es = __USER_DS;
  280. regs->ss = __USER_DS;
  281. regs->cs = __USER_CS;
  282. return 0;
  283. }
  284. static int __setup_rt_frame(int sig, struct ksignal *ksig,
  285. sigset_t *set, struct pt_regs *regs)
  286. {
  287. struct rt_sigframe __user *frame;
  288. void __user *restorer;
  289. int err = 0;
  290. void __user *fpstate = NULL;
  291. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
  292. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  293. return -EFAULT;
  294. put_user_try {
  295. put_user_ex(sig, &frame->sig);
  296. put_user_ex(&frame->info, &frame->pinfo);
  297. put_user_ex(&frame->uc, &frame->puc);
  298. /* Create the ucontext. */
  299. if (cpu_has_xsave)
  300. put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
  301. else
  302. put_user_ex(0, &frame->uc.uc_flags);
  303. put_user_ex(0, &frame->uc.uc_link);
  304. save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  305. /* Set up to return from userspace. */
  306. restorer = current->mm->context.vdso +
  307. vdso_image_32.sym___kernel_rt_sigreturn;
  308. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  309. restorer = ksig->ka.sa.sa_restorer;
  310. put_user_ex(restorer, &frame->pretcode);
  311. /*
  312. * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
  313. *
  314. * WE DO NOT USE IT ANY MORE! It's only left here for historical
  315. * reasons and because gdb uses it as a signature to notice
  316. * signal handler stack frames.
  317. */
  318. put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
  319. } put_user_catch(err);
  320. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  321. err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  322. regs, set->sig[0]);
  323. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  324. if (err)
  325. return -EFAULT;
  326. /* Set up registers for signal handler */
  327. regs->sp = (unsigned long)frame;
  328. regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
  329. regs->ax = (unsigned long)sig;
  330. regs->dx = (unsigned long)&frame->info;
  331. regs->cx = (unsigned long)&frame->uc;
  332. regs->ds = __USER_DS;
  333. regs->es = __USER_DS;
  334. regs->ss = __USER_DS;
  335. regs->cs = __USER_CS;
  336. return 0;
  337. }
  338. #else /* !CONFIG_X86_32 */
  339. static int __setup_rt_frame(int sig, struct ksignal *ksig,
  340. sigset_t *set, struct pt_regs *regs)
  341. {
  342. struct rt_sigframe __user *frame;
  343. void __user *fp = NULL;
  344. int err = 0;
  345. frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
  346. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  347. return -EFAULT;
  348. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  349. if (copy_siginfo_to_user(&frame->info, &ksig->info))
  350. return -EFAULT;
  351. }
  352. put_user_try {
  353. /* Create the ucontext. */
  354. if (cpu_has_xsave)
  355. put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
  356. else
  357. put_user_ex(0, &frame->uc.uc_flags);
  358. put_user_ex(0, &frame->uc.uc_link);
  359. save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  360. /* Set up to return from userspace. If provided, use a stub
  361. already in userspace. */
  362. /* x86-64 should always use SA_RESTORER. */
  363. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  364. put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
  365. } else {
  366. /* could use a vstub here */
  367. err |= -EFAULT;
  368. }
  369. } put_user_catch(err);
  370. err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
  371. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  372. if (err)
  373. return -EFAULT;
  374. /* Set up registers for signal handler */
  375. regs->di = sig;
  376. /* In case the signal handler was declared without prototypes */
  377. regs->ax = 0;
  378. /* This also works for non SA_SIGINFO handlers because they expect the
  379. next argument after the signal number on the stack. */
  380. regs->si = (unsigned long)&frame->info;
  381. regs->dx = (unsigned long)&frame->uc;
  382. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  383. regs->sp = (unsigned long)frame;
  384. /* Set up the CS register to run signal handlers in 64-bit mode,
  385. even if the handler happens to be interrupting 32-bit code. */
  386. regs->cs = __USER_CS;
  387. return 0;
  388. }
  389. #endif /* CONFIG_X86_32 */
  390. static int x32_setup_rt_frame(struct ksignal *ksig,
  391. compat_sigset_t *set,
  392. struct pt_regs *regs)
  393. {
  394. #ifdef CONFIG_X86_X32_ABI
  395. struct rt_sigframe_x32 __user *frame;
  396. void __user *restorer;
  397. int err = 0;
  398. void __user *fpstate = NULL;
  399. frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
  400. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  401. return -EFAULT;
  402. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  403. if (copy_siginfo_to_user32(&frame->info, &ksig->info))
  404. return -EFAULT;
  405. }
  406. put_user_try {
  407. /* Create the ucontext. */
  408. if (cpu_has_xsave)
  409. put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
  410. else
  411. put_user_ex(0, &frame->uc.uc_flags);
  412. put_user_ex(0, &frame->uc.uc_link);
  413. compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  414. put_user_ex(0, &frame->uc.uc__pad0);
  415. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  416. restorer = ksig->ka.sa.sa_restorer;
  417. } else {
  418. /* could use a vstub here */
  419. restorer = NULL;
  420. err |= -EFAULT;
  421. }
  422. put_user_ex(restorer, &frame->pretcode);
  423. } put_user_catch(err);
  424. err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  425. regs, set->sig[0]);
  426. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  427. if (err)
  428. return -EFAULT;
  429. /* Set up registers for signal handler */
  430. regs->sp = (unsigned long) frame;
  431. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  432. /* We use the x32 calling convention here... */
  433. regs->di = ksig->sig;
  434. regs->si = (unsigned long) &frame->info;
  435. regs->dx = (unsigned long) &frame->uc;
  436. loadsegment(ds, __USER_DS);
  437. loadsegment(es, __USER_DS);
  438. regs->cs = __USER_CS;
  439. regs->ss = __USER_DS;
  440. #endif /* CONFIG_X86_X32_ABI */
  441. return 0;
  442. }
  443. /*
  444. * Do a signal return; undo the signal stack.
  445. */
  446. #ifdef CONFIG_X86_32
  447. asmlinkage unsigned long sys_sigreturn(void)
  448. {
  449. struct pt_regs *regs = current_pt_regs();
  450. struct sigframe __user *frame;
  451. sigset_t set;
  452. frame = (struct sigframe __user *)(regs->sp - 8);
  453. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  454. goto badframe;
  455. if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
  456. && __copy_from_user(&set.sig[1], &frame->extramask,
  457. sizeof(frame->extramask))))
  458. goto badframe;
  459. set_current_blocked(&set);
  460. if (restore_sigcontext(regs, &frame->sc))
  461. goto badframe;
  462. return regs->ax;
  463. badframe:
  464. signal_fault(regs, frame, "sigreturn");
  465. return 0;
  466. }
  467. #endif /* CONFIG_X86_32 */
  468. asmlinkage long sys_rt_sigreturn(void)
  469. {
  470. struct pt_regs *regs = current_pt_regs();
  471. struct rt_sigframe __user *frame;
  472. sigset_t set;
  473. frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
  474. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  475. goto badframe;
  476. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  477. goto badframe;
  478. set_current_blocked(&set);
  479. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  480. goto badframe;
  481. if (restore_altstack(&frame->uc.uc_stack))
  482. goto badframe;
  483. return regs->ax;
  484. badframe:
  485. signal_fault(regs, frame, "rt_sigreturn");
  486. return 0;
  487. }
  488. static inline int is_ia32_compat_frame(void)
  489. {
  490. return config_enabled(CONFIG_IA32_EMULATION) &&
  491. test_thread_flag(TIF_IA32);
  492. }
  493. static inline int is_ia32_frame(void)
  494. {
  495. return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
  496. }
  497. static inline int is_x32_frame(void)
  498. {
  499. return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
  500. }
  501. static int
  502. setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
  503. {
  504. int usig = ksig->sig;
  505. sigset_t *set = sigmask_to_save();
  506. compat_sigset_t *cset = (compat_sigset_t *) set;
  507. /* Set up the stack frame */
  508. if (is_ia32_frame()) {
  509. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  510. return ia32_setup_rt_frame(usig, ksig, cset, regs);
  511. else
  512. return ia32_setup_frame(usig, ksig, cset, regs);
  513. } else if (is_x32_frame()) {
  514. return x32_setup_rt_frame(ksig, cset, regs);
  515. } else {
  516. return __setup_rt_frame(ksig->sig, ksig, set, regs);
  517. }
  518. }
  519. static void
  520. handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  521. {
  522. bool stepping, failed;
  523. struct fpu *fpu = &current->thread.fpu;
  524. if (v8086_mode(regs))
  525. save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
  526. /* Are we from a system call? */
  527. if (syscall_get_nr(current, regs) >= 0) {
  528. /* If so, check system call restarting.. */
  529. switch (syscall_get_error(current, regs)) {
  530. case -ERESTART_RESTARTBLOCK:
  531. case -ERESTARTNOHAND:
  532. regs->ax = -EINTR;
  533. break;
  534. case -ERESTARTSYS:
  535. if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
  536. regs->ax = -EINTR;
  537. break;
  538. }
  539. /* fallthrough */
  540. case -ERESTARTNOINTR:
  541. regs->ax = regs->orig_ax;
  542. regs->ip -= 2;
  543. break;
  544. }
  545. }
  546. /*
  547. * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
  548. * so that register information in the sigcontext is correct and
  549. * then notify the tracer before entering the signal handler.
  550. */
  551. stepping = test_thread_flag(TIF_SINGLESTEP);
  552. if (stepping)
  553. user_disable_single_step(current);
  554. failed = (setup_rt_frame(ksig, regs) < 0);
  555. if (!failed) {
  556. /*
  557. * Clear the direction flag as per the ABI for function entry.
  558. *
  559. * Clear RF when entering the signal handler, because
  560. * it might disable possible debug exception from the
  561. * signal handler.
  562. *
  563. * Clear TF for the case when it wasn't set by debugger to
  564. * avoid the recursive send_sigtrap() in SIGTRAP handler.
  565. */
  566. regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
  567. /*
  568. * Ensure the signal handler starts with the new fpu state.
  569. */
  570. if (fpu->fpstate_active)
  571. fpu__clear(fpu);
  572. }
  573. signal_setup_done(failed, ksig, stepping);
  574. }
  575. static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
  576. {
  577. #if defined(CONFIG_X86_32) || !defined(CONFIG_X86_64)
  578. return __NR_restart_syscall;
  579. #else /* !CONFIG_X86_32 && CONFIG_X86_64 */
  580. return test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall :
  581. __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
  582. #endif /* CONFIG_X86_32 || !CONFIG_X86_64 */
  583. }
  584. /*
  585. * Note that 'init' is a special process: it doesn't get signals it doesn't
  586. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  587. * mistake.
  588. */
  589. void do_signal(struct pt_regs *regs)
  590. {
  591. struct ksignal ksig;
  592. if (get_signal(&ksig)) {
  593. /* Whee! Actually deliver the signal. */
  594. handle_signal(&ksig, regs);
  595. return;
  596. }
  597. /* Did we come from a system call? */
  598. if (syscall_get_nr(current, regs) >= 0) {
  599. /* Restart the system call - no handlers present */
  600. switch (syscall_get_error(current, regs)) {
  601. case -ERESTARTNOHAND:
  602. case -ERESTARTSYS:
  603. case -ERESTARTNOINTR:
  604. regs->ax = regs->orig_ax;
  605. regs->ip -= 2;
  606. break;
  607. case -ERESTART_RESTARTBLOCK:
  608. regs->ax = get_nr_restart_syscall(regs);
  609. regs->ip -= 2;
  610. break;
  611. }
  612. }
  613. /*
  614. * If there's no signal to deliver, we just put the saved sigmask
  615. * back.
  616. */
  617. restore_saved_sigmask();
  618. }
  619. void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
  620. {
  621. struct task_struct *me = current;
  622. if (show_unhandled_signals && printk_ratelimit()) {
  623. printk("%s"
  624. "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
  625. task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
  626. me->comm, me->pid, where, frame,
  627. regs->ip, regs->sp, regs->orig_ax);
  628. print_vma_addr(" in ", regs->ip);
  629. pr_cont("\n");
  630. }
  631. force_sig(SIGSEGV, me);
  632. }
  633. #ifdef CONFIG_X86_X32_ABI
  634. asmlinkage long sys32_x32_rt_sigreturn(void)
  635. {
  636. struct pt_regs *regs = current_pt_regs();
  637. struct rt_sigframe_x32 __user *frame;
  638. sigset_t set;
  639. frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
  640. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  641. goto badframe;
  642. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  643. goto badframe;
  644. set_current_blocked(&set);
  645. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  646. goto badframe;
  647. if (compat_restore_altstack(&frame->uc.uc_stack))
  648. goto badframe;
  649. return regs->ax;
  650. badframe:
  651. signal_fault(regs, frame, "x32 rt_sigreturn");
  652. return 0;
  653. }
  654. #endif