Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/arch/x86_64/ia32/ia32_signal.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | * |
| 7 | * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson |
| 8 | * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes |
| 9 | * 2000-12-* x86-64 compatibility mode signal handling by Andi Kleen |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 13 | #include <linux/sched/task_stack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/mm.h> |
| 15 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/unistd.h> |
| 20 | #include <linux/stddef.h> |
| 21 | #include <linux/personality.h> |
| 22 | #include <linux/compat.h> |
Andi Kleen | 9fbbd4d | 2007-02-13 13:26:26 +0100 | [diff] [blame] | 23 | #include <linux/binfmts.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/ucontext.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Ingo Molnar | 78f7f1e | 2015-04-24 02:54:44 +0200 | [diff] [blame] | 26 | #include <asm/fpu/internal.h> |
Ingo Molnar | fcbc99c | 2015-04-30 08:45:02 +0200 | [diff] [blame] | 27 | #include <asm/fpu/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/ptrace.h> |
| 29 | #include <asm/ia32_unistd.h> |
| 30 | #include <asm/user32.h> |
Ingo Molnar | decb4c4 | 2015-09-05 09:32:43 +0200 | [diff] [blame] | 31 | #include <uapi/asm/sigcontext.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/proto.h> |
Roland McGrath | af65d64 | 2008-01-30 13:30:43 +0100 | [diff] [blame] | 33 | #include <asm/vdso.h> |
Hiroshi Shimamoto | d98f9d8 | 2008-12-17 18:52:45 -0800 | [diff] [blame] | 34 | #include <asm/sigframe.h> |
H. Peter Anvin | f28f0c2 | 2012-02-19 07:38:43 -0800 | [diff] [blame] | 35 | #include <asm/sighandling.h> |
H. Peter Anvin | 49b8c69 | 2012-09-21 17:18:44 -0700 | [diff] [blame] | 36 | #include <asm/smap.h> |
Hiroshi Shimamoto | d98f9d8 | 2008-12-17 18:52:45 -0800 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* |
| 39 | * Do a signal return; undo the signal stack. |
| 40 | */ |
Hiroshi Shimamoto | a967bb3 | 2009-02-20 19:00:54 -0800 | [diff] [blame] | 41 | #define loadsegment_gs(v) load_gs_index(v) |
| 42 | #define loadsegment_fs(v) loadsegment(fs, v) |
| 43 | #define loadsegment_ds(v) loadsegment(ds, v) |
| 44 | #define loadsegment_es(v) loadsegment(es, v) |
| 45 | |
| 46 | #define get_user_seg(seg) ({ unsigned int v; savesegment(seg, v); v; }) |
| 47 | #define set_user_seg(seg, v) loadsegment_##seg(v) |
| 48 | |
Hiroshi Shimamoto | b78a5b5 | 2008-11-17 15:44:50 -0800 | [diff] [blame] | 49 | #define COPY(x) { \ |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 50 | get_user_ex(regs->x, &sc->x); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Hiroshi Shimamoto | 8801ead | 2009-02-20 19:00:13 -0800 | [diff] [blame] | 53 | #define GET_SEG(seg) ({ \ |
| 54 | unsigned short tmp; \ |
| 55 | get_user_ex(tmp, &sc->seg); \ |
| 56 | tmp; \ |
| 57 | }) |
| 58 | |
| 59 | #define COPY_SEG_CPL3(seg) do { \ |
| 60 | regs->seg = GET_SEG(seg) | 3; \ |
| 61 | } while (0) |
Hiroshi Shimamoto | d71a68d | 2008-11-17 15:47:06 -0800 | [diff] [blame] | 62 | |
Hiroshi Shimamoto | 8c6e5ce | 2008-11-17 15:47:48 -0800 | [diff] [blame] | 63 | #define RELOAD_SEG(seg) { \ |
Hiroshi Shimamoto | a967bb3 | 2009-02-20 19:00:54 -0800 | [diff] [blame] | 64 | unsigned int pre = GET_SEG(seg); \ |
| 65 | unsigned int cur = get_user_seg(seg); \ |
Hiroshi Shimamoto | 8c6e5ce | 2008-11-17 15:47:48 -0800 | [diff] [blame] | 66 | pre |= 3; \ |
| 67 | if (pre != cur) \ |
Hiroshi Shimamoto | a967bb3 | 2009-02-20 19:00:54 -0800 | [diff] [blame] | 68 | set_user_seg(seg, pre); \ |
Hiroshi Shimamoto | 8c6e5ce | 2008-11-17 15:47:48 -0800 | [diff] [blame] | 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 71 | static int ia32_restore_sigcontext(struct pt_regs *regs, |
Ingo Molnar | 8fcb346 | 2015-09-05 09:32:41 +0200 | [diff] [blame] | 72 | struct sigcontext_32 __user *sc) |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 73 | { |
Hiroshi Shimamoto | a967bb3 | 2009-02-20 19:00:54 -0800 | [diff] [blame] | 74 | unsigned int tmpflags, err = 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 75 | void __user *buf; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 76 | u32 tmp; |
| 77 | |
| 78 | /* Always make any pending restarted system calls return -EINTR */ |
Andy Lutomirski | f56141e | 2015-02-12 15:01:14 -0800 | [diff] [blame] | 79 | current->restart_block.fn = do_no_restart_syscall; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 80 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 81 | get_user_try { |
| 82 | /* |
| 83 | * Reload fs and gs if they have changed in the signal |
| 84 | * handler. This does not handle long fs/gs base changes in |
| 85 | * the handler, but does not clobber them at least in the |
| 86 | * normal case. |
| 87 | */ |
Hiroshi Shimamoto | a967bb3 | 2009-02-20 19:00:54 -0800 | [diff] [blame] | 88 | RELOAD_SEG(gs); |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 89 | RELOAD_SEG(fs); |
| 90 | RELOAD_SEG(ds); |
| 91 | RELOAD_SEG(es); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 93 | COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx); |
Brian Gerst | 6a3713f | 2015-04-04 08:58:23 -0400 | [diff] [blame] | 94 | COPY(dx); COPY(cx); COPY(ip); COPY(ax); |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 95 | /* Don't touch extended registers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 97 | COPY_SEG_CPL3(cs); |
| 98 | COPY_SEG_CPL3(ss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 100 | get_user_ex(tmpflags, &sc->flags); |
| 101 | regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS); |
| 102 | /* disable syscall checks */ |
| 103 | regs->orig_ax = -1; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 104 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 105 | get_user_ex(tmp, &sc->fpstate); |
| 106 | buf = compat_ptr(tmp); |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 107 | } get_user_catch(err); |
| 108 | |
Ingo Molnar | 9dfe99b | 2015-04-29 20:55:19 +0200 | [diff] [blame] | 109 | err |= fpu__restore_sig(buf, 1); |
H. Peter Anvin | 5e88353 | 2012-09-21 12:43:15 -0700 | [diff] [blame] | 110 | |
Brian Gerst | 1daeaa3 | 2015-03-21 18:54:21 -0400 | [diff] [blame] | 111 | force_iret(); |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Al Viro | 3fe26fa | 2012-11-12 14:32:42 -0500 | [diff] [blame] | 116 | asmlinkage long sys32_sigreturn(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Al Viro | 3fe26fa | 2012-11-12 14:32:42 -0500 | [diff] [blame] | 118 | struct pt_regs *regs = current_pt_regs(); |
Hiroshi Shimamoto | 3b0d29e | 2008-12-17 18:51:46 -0800 | [diff] [blame] | 119 | struct sigframe_ia32 __user *frame = (struct sigframe_ia32 __user *)(regs->sp-8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | sigset_t set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 123 | goto badframe; |
| 124 | if (__get_user(set.sig[0], &frame->sc.oldmask) |
| 125 | || (_COMPAT_NSIG_WORDS > 1 |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 126 | && __copy_from_user((((char *) &set.sig) + 4), |
| 127 | &frame->extramask, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | sizeof(frame->extramask)))) |
| 129 | goto badframe; |
| 130 | |
Oleg Nesterov | 905f29e | 2011-07-10 21:27:24 +0200 | [diff] [blame] | 131 | set_current_blocked(&set); |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 132 | |
Brian Gerst | 6a3713f | 2015-04-04 08:58:23 -0400 | [diff] [blame] | 133 | if (ia32_restore_sigcontext(regs, &frame->sc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | goto badframe; |
Brian Gerst | 6a3713f | 2015-04-04 08:58:23 -0400 | [diff] [blame] | 135 | return regs->ax; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | badframe: |
| 138 | signal_fault(regs, frame, "32bit sigreturn"); |
| 139 | return 0; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Al Viro | 3fe26fa | 2012-11-12 14:32:42 -0500 | [diff] [blame] | 142 | asmlinkage long sys32_rt_sigreturn(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Al Viro | 3fe26fa | 2012-11-12 14:32:42 -0500 | [diff] [blame] | 144 | struct pt_regs *regs = current_pt_regs(); |
Hiroshi Shimamoto | 3b0d29e | 2008-12-17 18:51:46 -0800 | [diff] [blame] | 145 | struct rt_sigframe_ia32 __user *frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | sigset_t set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Hiroshi Shimamoto | 3b0d29e | 2008-12-17 18:51:46 -0800 | [diff] [blame] | 148 | frame = (struct rt_sigframe_ia32 __user *)(regs->sp - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 151 | goto badframe; |
| 152 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 153 | goto badframe; |
| 154 | |
Oleg Nesterov | 905f29e | 2011-07-10 21:27:24 +0200 | [diff] [blame] | 155 | set_current_blocked(&set); |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 156 | |
Brian Gerst | 6a3713f | 2015-04-04 08:58:23 -0400 | [diff] [blame] | 157 | if (ia32_restore_sigcontext(regs, &frame->uc.uc_mcontext)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | goto badframe; |
| 159 | |
Al Viro | 9026843 | 2012-12-14 14:47:53 -0500 | [diff] [blame] | 160 | if (compat_restore_altstack(&frame->uc.uc_stack)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | goto badframe; |
| 162 | |
Brian Gerst | 6a3713f | 2015-04-04 08:58:23 -0400 | [diff] [blame] | 163 | return regs->ax; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | badframe: |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 166 | signal_fault(regs, frame, "32bit rt sigreturn"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return 0; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * Set up a signal frame. |
| 172 | */ |
| 173 | |
Ingo Molnar | 8fcb346 | 2015-09-05 09:32:41 +0200 | [diff] [blame] | 174 | static int ia32_setup_sigcontext(struct sigcontext_32 __user *sc, |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 175 | void __user *fpstate, |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 176 | struct pt_regs *regs, unsigned int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Hiroshi Shimamoto | a967bb3 | 2009-02-20 19:00:54 -0800 | [diff] [blame] | 178 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 180 | put_user_try { |
Hiroshi Shimamoto | a967bb3 | 2009-02-20 19:00:54 -0800 | [diff] [blame] | 181 | put_user_ex(get_user_seg(gs), (unsigned int __user *)&sc->gs); |
| 182 | put_user_ex(get_user_seg(fs), (unsigned int __user *)&sc->fs); |
| 183 | put_user_ex(get_user_seg(ds), (unsigned int __user *)&sc->ds); |
| 184 | put_user_ex(get_user_seg(es), (unsigned int __user *)&sc->es); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 186 | put_user_ex(regs->di, &sc->di); |
| 187 | put_user_ex(regs->si, &sc->si); |
| 188 | put_user_ex(regs->bp, &sc->bp); |
| 189 | put_user_ex(regs->sp, &sc->sp); |
| 190 | put_user_ex(regs->bx, &sc->bx); |
| 191 | put_user_ex(regs->dx, &sc->dx); |
| 192 | put_user_ex(regs->cx, &sc->cx); |
| 193 | put_user_ex(regs->ax, &sc->ax); |
Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 194 | put_user_ex(current->thread.trap_nr, &sc->trapno); |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 195 | put_user_ex(current->thread.error_code, &sc->err); |
| 196 | put_user_ex(regs->ip, &sc->ip); |
| 197 | put_user_ex(regs->cs, (unsigned int __user *)&sc->cs); |
| 198 | put_user_ex(regs->flags, &sc->flags); |
| 199 | put_user_ex(regs->sp, &sc->sp_at_signal); |
| 200 | put_user_ex(regs->ss, (unsigned int __user *)&sc->ss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 202 | put_user_ex(ptr_to_compat(fpstate), &sc->fpstate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 204 | /* non-iBCS2 extensions.. */ |
| 205 | put_user_ex(mask, &sc->oldmask); |
| 206 | put_user_ex(current->thread.cr2, &sc->cr2); |
| 207 | } put_user_catch(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Determine which stack to use.. |
| 214 | */ |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 215 | static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 216 | size_t frame_size, |
Mathias Krause | 0ff8fef | 2012-09-02 23:31:42 +0200 | [diff] [blame] | 217 | void __user **fpstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Ingo Molnar | c5bedc6 | 2015-04-23 12:49:20 +0200 | [diff] [blame] | 219 | struct fpu *fpu = ¤t->thread.fpu; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 220 | unsigned long sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | /* Default to using normal stack */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 223 | sp = regs->sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /* This is the X/Open sanctioned signal stack switching. */ |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 226 | if (ksig->ka.sa.sa_flags & SA_ONSTACK) |
| 227 | sp = sigsp(sp, ksig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* This is the legacy signal stack switching. */ |
Andy Lutomirski | 9950481 | 2017-07-28 06:00:32 -0700 | [diff] [blame] | 229 | else if (regs->ss != __USER32_DS && |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 230 | !(ksig->ka.sa.sa_flags & SA_RESTORER) && |
| 231 | ksig->ka.sa.sa_restorer) |
| 232 | sp = (unsigned long) ksig->ka.sa.sa_restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Ingo Molnar | e4a81bf | 2017-09-26 09:43:36 +0200 | [diff] [blame] | 234 | if (fpu->initialized) { |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 235 | unsigned long fx_aligned, math_size; |
| 236 | |
Ingo Molnar | 82c0e45 | 2015-04-29 21:09:18 +0200 | [diff] [blame] | 237 | sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size); |
Ingo Molnar | 86e9fc3 | 2015-09-05 09:32:36 +0200 | [diff] [blame] | 238 | *fpstate = (struct _fpstate_32 __user *) sp; |
Ingo Molnar | c8e1404 | 2015-04-28 11:35:20 +0200 | [diff] [blame] | 239 | if (copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned, |
Suresh Siddha | 72a671c | 2012-07-24 16:05:29 -0700 | [diff] [blame] | 240 | math_size) < 0) |
Hiroshi Shimamoto | 99ea1b93 | 2008-11-05 18:32:54 -0800 | [diff] [blame] | 241 | return (void __user *) -1L; |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 242 | } |
| 243 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 244 | sp -= frame_size; |
Markus F.X.J. Oberhumer | d347f37 | 2005-10-09 18:54:23 +0200 | [diff] [blame] | 245 | /* Align the stack pointer according to the i386 ABI, |
| 246 | * i.e. so that on function entry ((sp + 4) & 15) == 0. */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 247 | sp = ((sp + 4) & -16ul) - 4; |
| 248 | return (void __user *) sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 251 | int ia32_setup_frame(int sig, struct ksignal *ksig, |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 252 | compat_sigset_t *set, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Hiroshi Shimamoto | 3b0d29e | 2008-12-17 18:51:46 -0800 | [diff] [blame] | 254 | struct sigframe_ia32 __user *frame; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 255 | void __user *restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | int err = 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 257 | void __user *fpstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 259 | /* copy_to_user optimizes that into a single 8 byte store */ |
| 260 | static const struct { |
| 261 | u16 poplmovl; |
| 262 | u32 val; |
| 263 | u16 int80; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 264 | } __attribute__((packed)) code = { |
| 265 | 0xb858, /* popl %eax ; movl $...,%eax */ |
| 266 | __NR_ia32_sigreturn, |
| 267 | 0x80cd, /* int $0x80 */ |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 268 | }; |
| 269 | |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 270 | frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 273 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Hiroshi Shimamoto | 2ba48e1 | 2008-09-12 17:02:53 -0700 | [diff] [blame] | 275 | if (__put_user(sig, &frame->sig)) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 276 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Hiroshi Shimamoto | 2ba48e1 | 2008-09-12 17:02:53 -0700 | [diff] [blame] | 278 | if (ia32_setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0])) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 279 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | if (_COMPAT_NSIG_WORDS > 1) { |
Hiroshi Shimamoto | 2ba48e1 | 2008-09-12 17:02:53 -0700 | [diff] [blame] | 282 | if (__copy_to_user(frame->extramask, &set->sig[1], |
| 283 | sizeof(frame->extramask))) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 284 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 287 | if (ksig->ka.sa.sa_flags & SA_RESTORER) { |
| 288 | restorer = ksig->ka.sa.sa_restorer; |
Roland McGrath | af65d64 | 2008-01-30 13:30:43 +0100 | [diff] [blame] | 289 | } else { |
| 290 | /* Return stub is in 32bit vsyscall page */ |
Roland McGrath | 1a3e4ca | 2008-04-09 01:29:27 -0700 | [diff] [blame] | 291 | if (current->mm->context.vdso) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 292 | restorer = current->mm->context.vdso + |
Andy Lutomirski | 0a6d1fa | 2015-10-05 17:47:56 -0700 | [diff] [blame] | 293 | vdso_image_32.sym___kernel_sigreturn; |
Roland McGrath | af65d64 | 2008-01-30 13:30:43 +0100 | [diff] [blame] | 294 | else |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 295 | restorer = &frame->retcode; |
Roland McGrath | af65d64 | 2008-01-30 13:30:43 +0100 | [diff] [blame] | 296 | } |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 297 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 298 | put_user_try { |
| 299 | put_user_ex(ptr_to_compat(restorer), &frame->pretcode); |
| 300 | |
| 301 | /* |
| 302 | * These are actually not used anymore, but left because some |
| 303 | * gdb versions depend on them as a marker. |
| 304 | */ |
Mathias Krause | 0ff8fef | 2012-09-02 23:31:42 +0200 | [diff] [blame] | 305 | put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode); |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 306 | } put_user_catch(err); |
| 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | if (err) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 309 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | /* Set up registers for signal handler */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 312 | regs->sp = (unsigned long) frame; |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 313 | regs->ip = (unsigned long) ksig->ka.sa.sa_handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Andi Kleen | 536e3ee | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 315 | /* Make -mregparm=3 work */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 316 | regs->ax = sig; |
| 317 | regs->dx = 0; |
| 318 | regs->cx = 0; |
Andi Kleen | 536e3ee | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 319 | |
Jeremy Fitzhardinge | b6edbb1 | 2008-08-19 13:04:19 -0700 | [diff] [blame] | 320 | loadsegment(ds, __USER32_DS); |
| 321 | loadsegment(es, __USER32_DS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 323 | regs->cs = __USER32_CS; |
| 324 | regs->ss = __USER32_DS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Andi Kleen | 1d001df | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 326 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 329 | int ia32_setup_rt_frame(int sig, struct ksignal *ksig, |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 330 | compat_sigset_t *set, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
Hiroshi Shimamoto | 3b0d29e | 2008-12-17 18:51:46 -0800 | [diff] [blame] | 332 | struct rt_sigframe_ia32 __user *frame; |
Roland McGrath | af65d64 | 2008-01-30 13:30:43 +0100 | [diff] [blame] | 333 | void __user *restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | int err = 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 335 | void __user *fpstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 337 | /* __copy_to_user optimizes that into a single 8 byte store */ |
| 338 | static const struct { |
| 339 | u8 movl; |
| 340 | u32 val; |
| 341 | u16 int80; |
Hiroshi Shimamoto | 9cc3c49 | 2008-11-11 19:11:39 -0800 | [diff] [blame] | 342 | u8 pad; |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 343 | } __attribute__((packed)) code = { |
| 344 | 0xb8, |
| 345 | __NR_ia32_rt_sigreturn, |
| 346 | 0x80cd, |
| 347 | 0, |
| 348 | }; |
| 349 | |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 350 | frame = get_sigframe(ksig, regs, sizeof(*frame), &fpstate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 353 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 355 | put_user_try { |
| 356 | put_user_ex(sig, &frame->sig); |
| 357 | put_user_ex(ptr_to_compat(&frame->info), &frame->pinfo); |
| 358 | put_user_ex(ptr_to_compat(&frame->uc), &frame->puc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 360 | /* Create the ucontext. */ |
Borislav Petkov | d366bf7 | 2016-04-04 22:25:02 +0200 | [diff] [blame] | 361 | if (boot_cpu_has(X86_FEATURE_XSAVE)) |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 362 | put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags); |
| 363 | else |
| 364 | put_user_ex(0, &frame->uc.uc_flags); |
| 365 | put_user_ex(0, &frame->uc.uc_link); |
Al Viro | bd1c149a | 2013-09-01 20:35:01 +0100 | [diff] [blame] | 366 | compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 368 | if (ksig->ka.sa.sa_flags & SA_RESTORER) |
| 369 | restorer = ksig->ka.sa.sa_restorer; |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 370 | else |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 371 | restorer = current->mm->context.vdso + |
Andy Lutomirski | 0a6d1fa | 2015-10-05 17:47:56 -0700 | [diff] [blame] | 372 | vdso_image_32.sym___kernel_rt_sigreturn; |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 373 | put_user_ex(ptr_to_compat(restorer), &frame->pretcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 375 | /* |
| 376 | * Not actually used anymore, but left because some gdb |
| 377 | * versions need it. |
| 378 | */ |
Mathias Krause | 0ff8fef | 2012-09-02 23:31:42 +0200 | [diff] [blame] | 379 | put_user_ex(*((u64 *)&code), (u64 __user *)frame->retcode); |
Hiroshi Shimamoto | 3b4b757 | 2009-01-23 15:50:38 -0800 | [diff] [blame] | 380 | } put_user_catch(err); |
| 381 | |
Dmitry Safonov | 6846351 | 2016-09-05 16:33:08 +0300 | [diff] [blame] | 382 | err |= __copy_siginfo_to_user32(&frame->info, &ksig->info, false); |
H. Peter Anvin | 5e88353 | 2012-09-21 12:43:15 -0700 | [diff] [blame] | 383 | err |= ia32_setup_sigcontext(&frame->uc.uc_mcontext, fpstate, |
| 384 | regs, set->sig[0]); |
| 385 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (err) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 388 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | /* Set up registers for signal handler */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 391 | regs->sp = (unsigned long) frame; |
Al Viro | 235b802 | 2012-11-09 23:51:47 -0500 | [diff] [blame] | 392 | regs->ip = (unsigned long) ksig->ka.sa.sa_handler; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Albert Cahalan | a7aacdf | 2006-10-29 22:26:17 -0500 | [diff] [blame] | 394 | /* Make -mregparm=3 work */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 395 | regs->ax = sig; |
| 396 | regs->dx = (unsigned long) &frame->info; |
| 397 | regs->cx = (unsigned long) &frame->uc; |
Albert Cahalan | a7aacdf | 2006-10-29 22:26:17 -0500 | [diff] [blame] | 398 | |
Jeremy Fitzhardinge | b6edbb1 | 2008-08-19 13:04:19 -0700 | [diff] [blame] | 399 | loadsegment(ds, __USER32_DS); |
| 400 | loadsegment(es, __USER32_DS); |
Thomas Gleixner | 99b9cdf | 2008-01-30 13:30:07 +0100 | [diff] [blame] | 401 | |
| 402 | regs->cs = __USER32_CS; |
| 403 | regs->ss = __USER32_DS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Andi Kleen | 1d001df | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 405 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |