ia32_signal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/signal.h>
  15. #include <linux/errno.h>
  16. #include <linux/wait.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/unistd.h>
  19. #include <linux/stddef.h>
  20. #include <linux/personality.h>
  21. #include <linux/compat.h>
  22. #include <linux/binfmts.h>
  23. #include <asm/ucontext.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/i387.h>
  26. #include <asm/ptrace.h>
  27. #include <asm/ia32_unistd.h>
  28. #include <asm/user32.h>
  29. #include <asm/sigcontext32.h>
  30. #include <asm/proto.h>
  31. #include <asm/vdso.h>
  32. #include <asm/sigframe.h>
  33. #include <asm/sys_ia32.h>
  34. #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
  35. #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
  36. X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
  37. X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
  38. X86_EFLAGS_CF)
  39. void signal_fault(struct pt_regs *regs, void __user *frame, char *where);
  40. int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
  41. {
  42. int err = 0;
  43. if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
  44. return -EFAULT;
  45. put_user_try {
  46. /* If you change siginfo_t structure, please make sure that
  47. this code is fixed accordingly.
  48. It should never copy any pad contained in the structure
  49. to avoid security leaks, but must copy the generic
  50. 3 ints plus the relevant union member. */
  51. put_user_ex(from->si_signo, &to->si_signo);
  52. put_user_ex(from->si_errno, &to->si_errno);
  53. put_user_ex((short)from->si_code, &to->si_code);
  54. if (from->si_code < 0) {
  55. put_user_ex(from->si_pid, &to->si_pid);
  56. put_user_ex(from->si_uid, &to->si_uid);
  57. put_user_ex(ptr_to_compat(from->si_ptr), &to->si_ptr);
  58. } else {
  59. /*
  60. * First 32bits of unions are always present:
  61. * si_pid === si_band === si_tid === si_addr(LS half)
  62. */
  63. put_user_ex(from->_sifields._pad[0],
  64. &to->_sifields._pad[0]);
  65. switch (from->si_code >> 16) {
  66. case __SI_FAULT >> 16:
  67. break;
  68. case __SI_CHLD >> 16:
  69. put_user_ex(from->si_utime, &to->si_utime);
  70. put_user_ex(from->si_stime, &to->si_stime);
  71. put_user_ex(from->si_status, &to->si_status);
  72. /* FALL THROUGH */
  73. default:
  74. case __SI_KILL >> 16:
  75. put_user_ex(from->si_uid, &to->si_uid);
  76. break;
  77. case __SI_POLL >> 16:
  78. put_user_ex(from->si_fd, &to->si_fd);
  79. break;
  80. case __SI_TIMER >> 16:
  81. put_user_ex(from->si_overrun, &to->si_overrun);
  82. put_user_ex(ptr_to_compat(from->si_ptr),
  83. &to->si_ptr);
  84. break;
  85. /* This is not generated by the kernel as of now. */
  86. case __SI_RT >> 16:
  87. case __SI_MESGQ >> 16:
  88. put_user_ex(from->si_uid, &to->si_uid);
  89. put_user_ex(from->si_int, &to->si_int);
  90. break;
  91. }
  92. }
  93. } put_user_catch(err);
  94. return err;
  95. }
  96. int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
  97. {
  98. int err = 0;
  99. u32 ptr32;
  100. if (!access_ok(VERIFY_READ, from, sizeof(compat_siginfo_t)))
  101. return -EFAULT;
  102. get_user_try {
  103. get_user_ex(to->si_signo, &from->si_signo);
  104. get_user_ex(to->si_errno, &from->si_errno);
  105. get_user_ex(to->si_code, &from->si_code);
  106. get_user_ex(to->si_pid, &from->si_pid);
  107. get_user_ex(to->si_uid, &from->si_uid);
  108. get_user_ex(ptr32, &from->si_ptr);
  109. to->si_ptr = compat_ptr(ptr32);
  110. } get_user_catch(err);
  111. return err;
  112. }
  113. asmlinkage long sys32_sigsuspend(int history0, int history1, old_sigset_t mask)
  114. {
  115. mask &= _BLOCKABLE;
  116. spin_lock_irq(&current->sighand->siglock);
  117. current->saved_sigmask = current->blocked;
  118. siginitset(&current->blocked, mask);
  119. recalc_sigpending();
  120. spin_unlock_irq(&current->sighand->siglock);
  121. current->state = TASK_INTERRUPTIBLE;
  122. schedule();
  123. set_restore_sigmask();
  124. return -ERESTARTNOHAND;
  125. }
  126. asmlinkage long sys32_sigaltstack(const stack_ia32_t __user *uss_ptr,
  127. stack_ia32_t __user *uoss_ptr,
  128. struct pt_regs *regs)
  129. {
  130. stack_t uss, uoss;
  131. int ret, err = 0;
  132. mm_segment_t seg;
  133. if (uss_ptr) {
  134. u32 ptr;
  135. memset(&uss, 0, sizeof(stack_t));
  136. if (!access_ok(VERIFY_READ, uss_ptr, sizeof(stack_ia32_t)))
  137. return -EFAULT;
  138. get_user_try {
  139. get_user_ex(ptr, &uss_ptr->ss_sp);
  140. get_user_ex(uss.ss_flags, &uss_ptr->ss_flags);
  141. get_user_ex(uss.ss_size, &uss_ptr->ss_size);
  142. } get_user_catch(err);
  143. if (err)
  144. return -EFAULT;
  145. uss.ss_sp = compat_ptr(ptr);
  146. }
  147. seg = get_fs();
  148. set_fs(KERNEL_DS);
  149. ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss, regs->sp);
  150. set_fs(seg);
  151. if (ret >= 0 && uoss_ptr) {
  152. if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(stack_ia32_t)))
  153. return -EFAULT;
  154. put_user_try {
  155. put_user_ex(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp);
  156. put_user_ex(uoss.ss_flags, &uoss_ptr->ss_flags);
  157. put_user_ex(uoss.ss_size, &uoss_ptr->ss_size);
  158. } put_user_catch(err);
  159. if (err)
  160. ret = -EFAULT;
  161. }
  162. return ret;
  163. }
  164. /*
  165. * Do a signal return; undo the signal stack.
  166. */
  167. #define loadsegment_gs(v) load_gs_index(v)
  168. #define loadsegment_fs(v) loadsegment(fs, v)
  169. #define loadsegment_ds(v) loadsegment(ds, v)
  170. #define loadsegment_es(v) loadsegment(es, v)
  171. #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; })
  172. #define set_user_seg(seg, v) loadsegment_##seg(v)
  173. #define COPY(x) { \
  174. get_user_ex(regs->x, &sc->x); \
  175. }
  176. #define GET_SEG(seg) ({ \
  177. unsigned short tmp; \
  178. get_user_ex(tmp, &sc->seg); \
  179. tmp; \
  180. })
  181. #define COPY_SEG_CPL3(seg) do { \
  182. regs->seg = GET_SEG(seg) | 3; \
  183. } while (0)
  184. #define RELOAD_SEG(seg) { \
  185. unsigned int pre = GET_SEG(seg); \
  186. unsigned int cur = get_user_seg(seg); \
  187. pre |= 3; \
  188. if (pre != cur) \
  189. set_user_seg(seg, pre); \
  190. }
  191. static int ia32_restore_sigcontext(struct pt_regs *regs,
  192. struct sigcontext_ia32 __user *sc,
  193. unsigned int *pax)
  194. {
  195. unsigned int tmpflags, err = 0;
  196. void __user *buf;
  197. u32 tmp;
  198. /* Always make any pending restarted system calls return -EINTR */
  199. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  200. get_user_try {
  201. /*
  202. * Reload fs and gs if they have changed in the signal
  203. * handler. This does not handle long fs/gs base changes in
  204. * the handler, but does not clobber them at least in the
  205. * normal case.
  206. */
  207. RELOAD_SEG(gs);
  208. RELOAD_SEG(fs);
  209. RELOAD_SEG(ds);
  210. RELOAD_SEG(es);
  211. COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
  212. COPY(dx); COPY(cx); COPY(ip);
  213. /* Don't touch extended registers */
  214. COPY_SEG_CPL3(cs);
  215. COPY_SEG_CPL3(ss);
  216. get_user_ex(tmpflags, &sc->flags);
  217. regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
  218. /* disable syscall checks */
  219. regs->orig_ax = -1;
  220. get_user_ex(tmp, &sc->fpstate);
  221. buf = compat_ptr(tmp);
  222. err |= restore_i387_xstate_ia32(buf);
  223. get_user_ex(*pax, &sc->ax);
  224. } get_user_catch(err);
  225. return err;
  226. }
  227. asmlinkage long sys32_sigreturn(struct pt_regs *regs)
  228. {
  229. struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8);
  230. sigset_t set;
  231. unsigned int ax;
  232. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  233. goto badframe;
  234. if (__get_user(set.sig[0], &frame->sc.oldmask)
  235. || (_COMPAT_NSIG_WORDS > 1
  236. && __copy_from_user((((char *) &set.sig) + 4),
  237. &frame->extramask,
  238. sizeof(frame->extramask))))
  239. goto badframe;
  240. sigdelsetmask(&set, ~_BLOCKABLE);
  241. spin_lock_irq(&current->sighand->siglock);
  242. current->blocked = set;
  243. recalc_sigpending();
  244. spin_unlock_irq(&current->sighand->siglock);
  245. if (ia32_restore_sigcontext(regs, &frame->sc, &ax))
  246. goto badframe;
  247. return ax;
  248. badframe:
  249. signal_fault(regs, frame, "32bit sigreturn");
  250. return 0;
  251. }
  252. asmlinkage long sys32_rt_sigreturn(struct pt_regs *regs)
  253. {
  254. struct rt_sigframe_ia32 __user *frame;
  255. sigset_t set;
  256. unsigned int ax;
  257. struct pt_regs tregs;
  258. frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4);
  259. if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
  260. goto badframe;
  261. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  262. goto badframe;
  263. sigdelsetmask(&set, ~_BLOCKABLE);
  264. spin_lock_irq(&current->sighand->siglock);
  265. current->blocked = set;
  266. recalc_sigpending();
  267. spin_unlock_irq(&current->sighand->siglock);
  268. if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
  269. goto badframe;
  270. tregs = *regs;
  271. if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
  272. goto badframe;
  273. return ax;
  274. badframe:
  275. signal_fault(regs, frame, "32bit rt sigreturn");
  276. return 0;
  277. }
  278. /*
  279. * Set up a signal frame.
  280. */
  281. static int ia32_setup_sigcontext(struct sigcontext_ia32 __user *sc,
  282. void __user *fpstate,
  283. struct pt_regs *regs, unsigned int mask)
  284. {
  285. int err = 0;
  286. put_user_try {
  287. put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs);
  288. put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs);
  289. put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds);
  290. put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es);
  291. put_user_ex(regs->di, &sc->di);
  292. put_user_ex(regs->si, &sc->si);
  293. put_user_ex(regs->bp, &sc->bp);
  294. put_user_ex(regs->sp, &sc->sp);
  295. put_user_ex(regs->bx, &sc->bx);
  296. put_user_ex(regs->dx, &sc->dx);
  297. put_user_ex(regs->cx, &sc->cx);
  298. put_user_ex(regs->ax, &sc->ax);
  299. put_user_ex(current->thread.trap_no, &sc->trapno);
  300. put_user_ex(current->thread.error_code, &sc->err);
  301. put_user_ex(regs->ip, &sc->ip);
  302. put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
  303. put_user_ex(regs->flags, &sc->flags);
  304. put_user_ex(regs->sp, &sc->sp_at_signal);
  305. put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
  306. put_user_ex(ptr_to_compat(fpstate), &sc->fpstate);
  307. /* non-iBCS2 extensions.. */
  308. put_user_ex(mask, &sc->oldmask);
  309. put_user_ex(current->thread.cr2, &sc->cr2);
  310. } put_user_catch(err);
  311. return err;
  312. }
  313. /*
  314. * Determine which stack to use..
  315. */
  316. static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
  317. size_t frame_size,
  318. void **fpstate)
  319. {
  320. unsigned long sp;
  321. /* Default to using normal stack */
  322. sp = regs->sp;
  323. /* This is the X/Open sanctioned signal stack switching. */
  324. if (ka->sa.sa_flags & SA_ONSTACK) {
  325. if (sas_ss_flags(sp) == 0)
  326. sp = current->sas_ss_sp + current->sas_ss_size;
  327. }
  328. /* This is the legacy signal stack switching. */
  329. else if ((regs->ss & 0xffff) != __USER32_DS &&
  330. !(ka->sa.sa_flags & SA_RESTORER) &&
  331. ka->sa.sa_restorer)
  332. sp = (unsigned long) ka->sa.sa_restorer;
  333. if (used_math()) {
  334. sp = sp - sig_xstate_ia32_size;
  335. *fpstate = (struct _fpstate_ia32 *) sp;
  336. if (save_i387_xstate_ia32(*fpstate) < 0)
  337. return (void __user *) -1L;
  338. }
  339. sp -= frame_size;
  340. /* Align the stack pointer according to the i386 ABI,
  341. * i.e. so that on function entry ((sp + 4) & 15) == 0. */
  342. sp = ((sp + 4) & -16ul) - 4;
  343. return (void __user *) sp;
  344. }
  345. int ia32_setup_frame(int sig, struct k_sigaction *ka,
  346. compat_sigset_t *set, struct pt_regs *regs)
  347. {
  348. struct sigframe_ia32 __user *frame;
  349. void __user *restorer;
  350. int err = 0;
  351. void __user *fpstate = NULL;
  352. /* copy_to_user optimizes that into a single 8 byte store */
  353. static const struct {
  354. u16 poplmovl;
  355. u32 val;
  356. u16 int80;
  357. } __attribute__((packed)) code = {
  358. 0xb858, /* popl %eax ; movl $...,%eax */
  359. __NR_ia32_sigreturn,
  360. 0x80cd, /* int $0x80 */
  361. };
  362. frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
  363. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  364. return -EFAULT;
  365. if (__put_user(sig, &frame->sig))
  366. return -EFAULT;
  367. if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
  368. return -EFAULT;
  369. if (_COMPAT_NSIG_WORDS > 1) {
  370. if (__copy_to_user(frame->extramask, &set->sig[1],
  371. sizeof(frame->extramask)))
  372. return -EFAULT;
  373. }
  374. if (ka->sa.sa_flags & SA_RESTORER) {
  375. restorer = ka->sa.sa_restorer;
  376. } else {
  377. /* Return stub is in 32bit vsyscall page */
  378. if (current->mm->context.vdso)
  379. restorer = VDSO32_SYMBOL(current->mm->context.vdso,
  380. sigreturn);
  381. else
  382. restorer = &frame->retcode;
  383. }
  384. put_user_try {
  385. put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
  386. /*
  387. * These are actually not used anymore, but left because some
  388. * gdb versions depend on them as a marker.
  389. */
  390. put_user_ex(*((u64 *)&code), (u64 *)frame->retcode);
  391. } put_user_catch(err);
  392. if (err)
  393. return -EFAULT;
  394. /* Set up registers for signal handler */
  395. regs->sp = (unsigned long) frame;
  396. regs->ip = (unsigned long) ka->sa.sa_handler;
  397. /* Make -mregparm=3 work */
  398. regs->ax = sig;
  399. regs->dx = 0;
  400. regs->cx = 0;
  401. loadsegment(ds, __USER32_DS);
  402. loadsegment(es, __USER32_DS);
  403. regs->cs = __USER32_CS;
  404. regs->ss = __USER32_DS;
  405. return 0;
  406. }
  407. int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
  408. compat_sigset_t *set, struct pt_regs *regs)
  409. {
  410. struct rt_sigframe_ia32 __user *frame;
  411. void __user *restorer;
  412. int err = 0;
  413. void __user *fpstate = NULL;
  414. /* __copy_to_user optimizes that into a single 8 byte store */
  415. static const struct {
  416. u8 movl;
  417. u32 val;
  418. u16 int80;
  419. u8 pad;
  420. } __attribute__((packed)) code = {
  421. 0xb8,
  422. __NR_ia32_rt_sigreturn,
  423. 0x80cd,
  424. 0,
  425. };
  426. frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
  427. if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
  428. return -EFAULT;
  429. put_user_try {
  430. put_user_ex(sig, &frame->sig);
  431. put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo);
  432. put_user_ex(ptr_to_compat(&frame->uc), &frame->puc);
  433. err |= copy_siginfo_to_user32(&frame->info, info);
  434. /* Create the ucontext. */
  435. if (cpu_has_xsave)
  436. put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
  437. else
  438. put_user_ex(0, &frame->uc.uc_flags);
  439. put_user_ex(0, &frame->uc.uc_link);
  440. put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
  441. put_user_ex(sas_ss_flags(regs->sp),
  442. &frame->uc.uc_stack.ss_flags);
  443. put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
  444. err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
  445. regs, set->sig[0]);
  446. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  447. if (ka->sa.sa_flags & SA_RESTORER)
  448. restorer = ka->sa.sa_restorer;
  449. else
  450. restorer = VDSO32_SYMBOL(current->mm->context.vdso,
  451. rt_sigreturn);
  452. put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
  453. /*
  454. * Not actually used anymore, but left because some gdb
  455. * versions need it.
  456. */
  457. put_user_ex(*((u64 *)&code), (u64 *)frame->retcode);
  458. } put_user_catch(err);
  459. if (err)
  460. return -EFAULT;
  461. /* Set up registers for signal handler */
  462. regs->sp = (unsigned long) frame;
  463. regs->ip = (unsigned long) ka->sa.sa_handler;
  464. /* Make -mregparm=3 work */
  465. regs->ax = sig;
  466. regs->dx = (unsigned long) &frame->info;
  467. regs->cx = (unsigned long) &frame->uc;
  468. loadsegment(ds, __USER32_DS);
  469. loadsegment(es, __USER32_DS);
  470. regs->cs = __USER32_CS;
  471. regs->ss = __USER32_DS;
  472. return 0;
  473. }