ia32_signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * linux/arch/x86_64/ia32/ia32_signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
  7. * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
  8. * 2000-12-* x86-64 compatibility mode signal handling by Andi Kleen
  9. */
  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/unistd.h>
  17. #include <linux/stddef.h>
  18. #include <linux/personality.h>
  19. #include <linux/compat.h>
  20. #include <linux/binfmts.h>
  21. #include <asm/ucontext.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/fpu/internal.h>
  24. #include <asm/fpu/signal.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/ia32_unistd.h>
  27. #include <asm/user32.h>
  28. #include <uapi/asm/sigcontext.h>
  29. #include <asm/proto.h>
  30. #include <asm/vdso.h>
  31. #include <asm/sigframe.h>
  32. #include <asm/sighandling.h>
  33. #include <asm/sys_ia32.h>
  34. #include <asm/smap.h>
  35. /*
  36. * Do a signal return; undo the signal stack.
  37. */
  38. #define loadsegment_gs(v) load_gs_index(v)
  39. #define loadsegment_fs(v) loadsegment(fs, v)
  40. #define loadsegment_ds(v) loadsegment(ds, v)
  41. #define loadsegment_es(v) loadsegment(es, v)
  42. #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
  43. #define set_user_seg(seg, v) loadsegment_##seg(v)
  44. #define COPY(x) { \
  45. get_user_ex(regs->x, &sc->x); \
  46. }
  47. #define GET_SEG(seg) ({ \
  48. unsigned short tmp; \
  49. get_user_ex(tmp, &sc->seg); \
  50. tmp; \
  51. })
  52. #define COPY_SEG_CPL3(seg) do { \
  53. regs->seg = GET_SEG(seg) | 3; \
  54. } while (0)
  55. #define RELOAD_SEG(seg) { \
  56. unsigned int pre = GET_SEG(seg); \
  57. unsigned int cur = get_user_seg(seg); \
  58. pre |= 3; \
  59. if (pre != cur) \
  60. set_user_seg(seg, pre); \
  61. }
  62. static int ia32_restore_sigcontext(struct pt_regs *regs,
  63. struct sigcontext_32 __user *sc)
  64. {
  65. unsigned int tmpflags, err = 0;
  66. void __user *buf;
  67. u32 tmp;
  68. /* Always make any pending restarted system calls return -EINTR */
  69. current->restart_block.fn = do_no_restart_syscall;
  70. get_user_try {
  71. /*
  72. * Reload fs and gs if they have changed in the signal
  73. * handler. This does not handle long fs/gs base changes in
  74. * the handler, but does not clobber them at least in the
  75. * normal case.
  76. */
  77. RELOAD_SEG(gs);
  78. RELOAD_SEG(fs);
  79. RELOAD_SEG(ds);
  80. RELOAD_SEG(es);
  81. COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
  82. COPY(dx); COPY(cx); COPY(ip); COPY(ax);
  83. /* Don't touch extended registers */
  84. COPY_SEG_CPL3(cs);
  85. COPY_SEG_CPL3(ss);
  86. get_user_ex(tmpflags, &sc->flags);
  87. regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
  88. /* disable syscall checks */
  89. regs->orig_ax = -1;
  90. get_user_ex(tmp, &sc->fpstate);
  91. buf = compat_ptr(tmp);
  92. } get_user_catch(err);
  93. err |= fpu__restore_sig(buf, 1);
  94. force_iret();
  95. return err;
  96. }
  97. asmlinkage long sys32_sigreturn(void)
  98. {
  99. struct pt_regs *regs = current_pt_regs();
  100. struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
  101. sigset_t set;
  102. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  103. goto badframe;
  104. if (__get_user(set.sig[0], &frame->sc.oldmask)
  105. || (_COMPAT_NSIG_WORDS > 1
  106. && __copy_from_user((((char *) &set.sig) + 4),
  107. &frame->extramask,
  108. sizeof(frame->extramask))))
  109. goto badframe;
  110. set_current_blocked(&set);
  111. if (ia32_restore_sigcontext(regs, &frame->sc))
  112. goto badframe;
  113. return regs->ax;
  114. badframe:
  115. signal_fault(regs, frame, "32bit sigreturn");
  116. return 0;
  117. }
  118. asmlinkage long sys32_rt_sigreturn(void)
  119. {
  120. struct pt_regs *regs = current_pt_regs();
  121. struct rt_sigframe_ia32 __user *frame;
  122. sigset_t set;
  123. frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
  124. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  125. goto badframe;
  126. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  127. goto badframe;
  128. set_current_blocked(&set);
  129. if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext))
  130. goto badframe;
  131. if (compat_restore_altstack(&frame->uc.uc_stack))
  132. goto badframe;
  133. return regs->ax;
  134. badframe:
  135. signal_fault(regs, frame, "32bit rt sigreturn");
  136. return 0;
  137. }
  138. /*
  139. * Set up a signal frame.
  140. */
  141. static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc,
  142. void __user *fpstate,
  143. struct pt_regs *regs, unsigned int mask)
  144. {
  145. int err = 0;
  146. put_user_try {
  147. put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs);
  148. put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs);
  149. put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds);
  150. put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es);
  151. put_user_ex(regs->di, &sc->di);
  152. put_user_ex(regs->si, &sc->si);
  153. put_user_ex(regs->bp, &sc->bp);
  154. put_user_ex(regs->sp, &sc->sp);
  155. put_user_ex(regs->bx, &sc->bx);
  156. put_user_ex(regs->dx, &sc->dx);
  157. put_user_ex(regs->cx, &sc->cx);
  158. put_user_ex(regs->ax, &sc->ax);
  159. put_user_ex(current->thread.trap_nr, &sc->trapno);
  160. put_user_ex(current->thread.error_code, &sc->err);
  161. put_user_ex(regs->ip, &sc->ip);
  162. put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
  163. put_user_ex(regs->flags, &sc->flags);
  164. put_user_ex(regs->sp, &sc->sp_at_signal);
  165. put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
  166. put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
  167. /* non-iBCS2 extensions.. */
  168. put_user_ex(mask, &sc->oldmask);
  169. put_user_ex(current->thread.cr2, &sc->cr2);
  170. } put_user_catch(err);
  171. return err;
  172. }
  173. /*
  174. * Determine which stack to use..
  175. */
  176. static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
  177. size_t frame_size,
  178. void __user **fpstate)
  179. {
  180. struct fpu *fpu = &current->thread.fpu;
  181. unsigned long sp;
  182. /* Default to using normal stack */
  183. sp = regs->sp;
  184. /* This is the X/Open sanctioned signal stack switching. */
  185. if (ksig->ka.sa.sa_flags & SA_ONSTACK)
  186. sp = sigsp(sp, ksig);
  187. /* This is the legacy signal stack switching. */
  188. else if ((regs->ss & 0xffff) != __USER32_DS &&
  189. !(ksig->ka.sa.sa_flags & SA_RESTORER) &&
  190. ksig->ka.sa.sa_restorer)
  191. sp = (unsigned long) ksig->ka.sa.sa_restorer;
  192. if (fpu->fpstate_active) {
  193. unsigned long fx_aligned, math_size;
  194. sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
  195. *fpstate = (struct _fpstate_32 __user *) sp;
  196. if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
  197. math_size) < 0)
  198. return (void __user *) -1L;
  199. }
  200. sp -= frame_size;
  201. /* Align the stack pointer according to the i386 ABI,
  202. * i.e. so that on function entry ((sp + 4) & 15) == 0. */
  203. sp = ((sp + 4) & -16ul) - 4;
  204. return (void __user *) sp;
  205. }
  206. int ia32_setup_frame(int sig, struct ksignal *ksig,
  207. compat_sigset_t *set, struct pt_regs *regs)
  208. {
  209. struct sigframe_ia32 __user *frame;
  210. void __user *restorer;
  211. int err = 0;
  212. void __user *fpstate = NULL;
  213. /* copy_to_user optimizes that into a single 8 byte store */
  214. static const struct {
  215. u16 poplmovl;
  216. u32 val;
  217. u16 int80;
  218. } __attribute__((packed)) code = {
  219. 0xb858, /* popl %eax ; movl $...,%eax */
  220. __NR_ia32_sigreturn,
  221. 0x80cd, /* int $0x80 */
  222. };
  223. frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
  224. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  225. return -EFAULT;
  226. if (__put_user(sig, &frame->sig))
  227. return -EFAULT;
  228. if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
  229. return -EFAULT;
  230. if (_COMPAT_NSIG_WORDS > 1) {
  231. if (__copy_to_user(frame->extramask, &set->sig[1],
  232. sizeof(frame->extramask)))
  233. return -EFAULT;
  234. }
  235. if (ksig->ka.sa.sa_flags & SA_RESTORER) {
  236. restorer = ksig->ka.sa.sa_restorer;
  237. } else {
  238. /* Return stub is in 32bit vsyscall page */
  239. if (current->mm->context.vdso)
  240. restorer = current->mm->context.vdso +
  241. vdso_image_32.sym___kernel_sigreturn;
  242. else
  243. restorer = &frame->retcode;
  244. }
  245. put_user_try {
  246. put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
  247. /*
  248. * These are actually not used anymore, but left because some
  249. * gdb versions depend on them as a marker.
  250. */
  251. put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
  252. } put_user_catch(err);
  253. if (err)
  254. return -EFAULT;
  255. /* Set up registers for signal handler */
  256. regs->sp = (unsigned long) frame;
  257. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  258. /* Make -mregparm=3 work */
  259. regs->ax = sig;
  260. regs->dx = 0;
  261. regs->cx = 0;
  262. loadsegment(ds, __USER32_DS);
  263. loadsegment(es, __USER32_DS);
  264. regs->cs = __USER32_CS;
  265. regs->ss = __USER32_DS;
  266. return 0;
  267. }
  268. int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
  269. compat_sigset_t *set, struct pt_regs *regs)
  270. {
  271. struct rt_sigframe_ia32 __user *frame;
  272. void __user *restorer;
  273. int err = 0;
  274. void __user *fpstate = NULL;
  275. /* __copy_to_user optimizes that into a single 8 byte store */
  276. static const struct {
  277. u8 movl;
  278. u32 val;
  279. u16 int80;
  280. u8 pad;
  281. } __attribute__((packed)) code = {
  282. 0xb8,
  283. __NR_ia32_rt_sigreturn,
  284. 0x80cd,
  285. 0,
  286. };
  287. frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate);
  288. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  289. return -EFAULT;
  290. put_user_try {
  291. put_user_ex(sig, &frame->sig);
  292. put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
  293. put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
  294. /* Create the ucontext. */
  295. if (cpu_has_xsave)
  296. put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
  297. else
  298. put_user_ex(0, &frame->uc.uc_flags);
  299. put_user_ex(0, &frame->uc.uc_link);
  300. compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
  301. if (ksig->ka.sa.sa_flags & SA_RESTORER)
  302. restorer = ksig->ka.sa.sa_restorer;
  303. else
  304. restorer = current->mm->context.vdso +
  305. vdso_image_32.sym___kernel_rt_sigreturn;
  306. put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
  307. /*
  308. * Not actually used anymore, but left because some gdb
  309. * versions need it.
  310. */
  311. put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode);
  312. } put_user_catch(err);
  313. err |= copy_siginfo_to_user32(&frame->info, &ksig->info);
  314. err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  315. regs, set->sig[0]);
  316. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  317. if (err)
  318. return -EFAULT;
  319. /* Set up registers for signal handler */
  320. regs->sp = (unsigned long) frame;
  321. regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
  322. /* Make -mregparm=3 work */
  323. regs->ax = sig;
  324. regs->dx = (unsigned long) &frame->info;
  325. regs->cx = (unsigned long) &frame->uc;
  326. loadsegment(ds, __USER32_DS);
  327. loadsegment(es, __USER32_DS);
  328. regs->cs = __USER32_CS;
  329. regs->ss = __USER32_DS;
  330. return 0;
  331. }