Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 3 | * |
| 4 | * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson |
| 5 | * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes |
| 6 | */ |
| 7 | |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 8 | #include <linux/sched.h> |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 9 | #include <linux/mm.h> |
Hiroshi Shimamoto | 5c9b3a0 | 2008-11-21 17:36:41 -0800 | [diff] [blame] | 10 | #include <linux/smp.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/signal.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/wait.h> |
| 15 | #include <linux/ptrace.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> |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/processor.h> |
| 23 | #include <asm/ucontext.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/i387.h> |
Roland McGrath | 6c3652e | 2008-01-30 13:30:42 +0100 | [diff] [blame] | 25 | #include <asm/vdso.h> |
Hiroshi Shimamoto | 5c9b3a0 | 2008-11-21 17:36:41 -0800 | [diff] [blame] | 26 | |
| 27 | #ifdef CONFIG_X86_64 |
| 28 | #include <asm/proto.h> |
| 29 | #include <asm/ia32_unistd.h> |
| 30 | #include <asm/mce.h> |
| 31 | #endif /* CONFIG_X86_64 */ |
| 32 | |
Hiroshi Shimamoto | bb57925 | 2008-09-05 16:26:55 -0700 | [diff] [blame] | 33 | #include <asm/syscall.h> |
Jaswinder Singh | bbc1f69 | 2008-07-21 21:34:13 +0530 | [diff] [blame] | 34 | #include <asm/syscalls.h> |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 35 | |
Harvey Harrison | 123a634 | 2008-02-08 12:10:00 -0800 | [diff] [blame] | 36 | #include "sigframe.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) |
| 39 | |
Harvey Harrison | 1a17680 | 2008-02-08 12:09:59 -0800 | [diff] [blame] | 40 | #define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \ |
| 41 | X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \ |
| 42 | X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \ |
| 43 | X86_EFLAGS_CF) |
| 44 | |
| 45 | #ifdef CONFIG_X86_32 |
| 46 | # define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF) |
| 47 | #else |
| 48 | # define FIX_EFLAGS __FIX_EFLAGS |
| 49 | #endif |
| 50 | |
Hiroshi Shimamoto | 2601657 | 2008-11-24 18:21:37 -0800 | [diff] [blame] | 51 | #define COPY(x) { \ |
| 52 | err |= __get_user(regs->x, &sc->x); \ |
| 53 | } |
| 54 | |
| 55 | #define COPY_SEG(seg) { \ |
| 56 | unsigned short tmp; \ |
| 57 | err |= __get_user(tmp, &sc->seg); \ |
| 58 | regs->seg = tmp; \ |
| 59 | } |
| 60 | |
| 61 | #define COPY_SEG_CPL3(seg) { \ |
| 62 | unsigned short tmp; \ |
| 63 | err |= __get_user(tmp, &sc->seg); \ |
| 64 | regs->seg = tmp | 3; \ |
| 65 | } |
| 66 | |
| 67 | #define GET_SEG(seg) { \ |
| 68 | unsigned short tmp; \ |
| 69 | err |= __get_user(tmp, &sc->seg); \ |
| 70 | loadsegment(seg, tmp); \ |
| 71 | } |
| 72 | |
| 73 | static int |
| 74 | restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, |
| 75 | unsigned long *pax) |
| 76 | { |
| 77 | void __user *buf; |
| 78 | unsigned int tmpflags; |
| 79 | unsigned int err = 0; |
| 80 | |
| 81 | /* Always make any pending restarted system calls return -EINTR */ |
| 82 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 83 | |
| 84 | #ifdef CONFIG_X86_32 |
| 85 | GET_SEG(gs); |
| 86 | COPY_SEG(fs); |
| 87 | COPY_SEG(es); |
| 88 | COPY_SEG(ds); |
| 89 | #endif /* CONFIG_X86_32 */ |
| 90 | |
| 91 | COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx); |
| 92 | COPY(dx); COPY(cx); COPY(ip); |
| 93 | |
| 94 | #ifdef CONFIG_X86_64 |
| 95 | COPY(r8); |
| 96 | COPY(r9); |
| 97 | COPY(r10); |
| 98 | COPY(r11); |
| 99 | COPY(r12); |
| 100 | COPY(r13); |
| 101 | COPY(r14); |
| 102 | COPY(r15); |
| 103 | #endif /* CONFIG_X86_64 */ |
| 104 | |
| 105 | #ifdef CONFIG_X86_32 |
| 106 | COPY_SEG_CPL3(cs); |
| 107 | COPY_SEG_CPL3(ss); |
| 108 | #else /* !CONFIG_X86_32 */ |
| 109 | /* Kernel saves and restores only the CS segment register on signals, |
| 110 | * which is the bare minimum needed to allow mixed 32/64-bit code. |
| 111 | * App's signal handler can save/restore other segments if needed. */ |
| 112 | COPY_SEG_CPL3(cs); |
| 113 | #endif /* CONFIG_X86_32 */ |
| 114 | |
| 115 | err |= __get_user(tmpflags, &sc->flags); |
| 116 | regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS); |
| 117 | regs->orig_ax = -1; /* disable syscall checks */ |
| 118 | |
| 119 | err |= __get_user(buf, &sc->fpstate); |
| 120 | err |= restore_i387_xstate(buf); |
| 121 | |
| 122 | err |= __get_user(*pax, &sc->ax); |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | static int |
| 127 | setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate, |
| 128 | struct pt_regs *regs, unsigned long mask) |
| 129 | { |
| 130 | int err = 0; |
| 131 | |
| 132 | #ifdef CONFIG_X86_32 |
| 133 | { |
| 134 | unsigned int tmp; |
| 135 | |
| 136 | savesegment(gs, tmp); |
| 137 | err |= __put_user(tmp, (unsigned int __user *)&sc->gs); |
| 138 | } |
| 139 | err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs); |
| 140 | err |= __put_user(regs->es, (unsigned int __user *)&sc->es); |
| 141 | err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds); |
| 142 | #endif /* CONFIG_X86_32 */ |
| 143 | |
| 144 | err |= __put_user(regs->di, &sc->di); |
| 145 | err |= __put_user(regs->si, &sc->si); |
| 146 | err |= __put_user(regs->bp, &sc->bp); |
| 147 | err |= __put_user(regs->sp, &sc->sp); |
| 148 | err |= __put_user(regs->bx, &sc->bx); |
| 149 | err |= __put_user(regs->dx, &sc->dx); |
| 150 | err |= __put_user(regs->cx, &sc->cx); |
| 151 | err |= __put_user(regs->ax, &sc->ax); |
| 152 | #ifdef CONFIG_X86_64 |
| 153 | err |= __put_user(regs->r8, &sc->r8); |
| 154 | err |= __put_user(regs->r9, &sc->r9); |
| 155 | err |= __put_user(regs->r10, &sc->r10); |
| 156 | err |= __put_user(regs->r11, &sc->r11); |
| 157 | err |= __put_user(regs->r12, &sc->r12); |
| 158 | err |= __put_user(regs->r13, &sc->r13); |
| 159 | err |= __put_user(regs->r14, &sc->r14); |
| 160 | err |= __put_user(regs->r15, &sc->r15); |
| 161 | #endif /* CONFIG_X86_64 */ |
| 162 | |
| 163 | err |= __put_user(current->thread.trap_no, &sc->trapno); |
| 164 | err |= __put_user(current->thread.error_code, &sc->err); |
| 165 | err |= __put_user(regs->ip, &sc->ip); |
| 166 | #ifdef CONFIG_X86_32 |
| 167 | err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs); |
| 168 | err |= __put_user(regs->flags, &sc->flags); |
| 169 | err |= __put_user(regs->sp, &sc->sp_at_signal); |
| 170 | err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss); |
| 171 | #else /* !CONFIG_X86_32 */ |
| 172 | err |= __put_user(regs->flags, &sc->flags); |
| 173 | err |= __put_user(regs->cs, &sc->cs); |
| 174 | err |= __put_user(0, &sc->gs); |
| 175 | err |= __put_user(0, &sc->fs); |
| 176 | #endif /* CONFIG_X86_32 */ |
| 177 | |
| 178 | err |= __put_user(fpstate, &sc->fpstate); |
| 179 | |
| 180 | /* non-iBCS2 extensions.. */ |
| 181 | err |= __put_user(mask, &sc->oldmask); |
| 182 | err |= __put_user(current->thread.cr2, &sc->cr2); |
| 183 | |
| 184 | return err; |
| 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | * Set up a signal frame. |
| 189 | */ |
Hiroshi Shimamoto | bfeb91a | 2008-11-24 18:23:12 -0800 | [diff] [blame^] | 190 | #ifdef CONFIG_X86_32 |
| 191 | static const struct { |
| 192 | u16 poplmovl; |
| 193 | u32 val; |
| 194 | u16 int80; |
| 195 | } __attribute__((packed)) retcode = { |
| 196 | 0xb858, /* popl %eax; movl $..., %eax */ |
| 197 | __NR_sigreturn, |
| 198 | 0x80cd, /* int $0x80 */ |
| 199 | }; |
| 200 | |
| 201 | static const struct { |
| 202 | u8 movl; |
| 203 | u32 val; |
| 204 | u16 int80; |
| 205 | u8 pad; |
| 206 | } __attribute__((packed)) rt_retcode = { |
| 207 | 0xb8, /* movl $..., %eax */ |
| 208 | __NR_rt_sigreturn, |
| 209 | 0x80cd, /* int $0x80 */ |
| 210 | 0 |
| 211 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Determine which stack to use.. |
| 215 | */ |
| 216 | static inline void __user * |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 217 | get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size, |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 218 | void **fpstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
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 | |
Roland McGrath | 83bd010 | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 225 | /* |
| 226 | * If we are on the alternate signal stack and would overflow it, don't. |
| 227 | * Return an always-bogus address instead so we will die with SIGSEGV. |
| 228 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 229 | if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) |
Roland McGrath | 83bd010 | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 230 | return (void __user *) -1L; |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | /* This is the X/Open sanctioned signal stack switching. */ |
| 233 | if (ka->sa.sa_flags & SA_ONSTACK) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 234 | if (sas_ss_flags(sp) == 0) |
| 235 | sp = current->sas_ss_sp + current->sas_ss_size; |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 236 | } else { |
| 237 | /* This is the legacy signal stack switching. */ |
| 238 | if ((regs->ss & 0xffff) != __USER_DS && |
| 239 | !(ka->sa.sa_flags & SA_RESTORER) && |
| 240 | ka->sa.sa_restorer) |
| 241 | sp = (unsigned long) ka->sa.sa_restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 244 | if (used_math()) { |
| 245 | sp = sp - sig_xstate_size; |
| 246 | *fpstate = (struct _fpstate *) sp; |
Hiroshi Shimamoto | 4b33669 | 2008-11-05 18:30:25 -0800 | [diff] [blame] | 247 | if (save_i387_xstate(*fpstate) < 0) |
| 248 | return (void __user *)-1L; |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 249 | } |
| 250 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 251 | sp -= frame_size; |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 252 | /* |
| 253 | * Align the stack pointer according to the i386 ABI, |
| 254 | * i.e. so that on function entry ((sp + 4) & 15) == 0. |
| 255 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 256 | sp = ((sp + 4) & -16ul) - 4; |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 257 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 258 | return (void __user *) sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 261 | static int |
Hiroshi Shimamoto | 1d13024 | 2008-09-05 16:28:06 -0700 | [diff] [blame] | 262 | __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set, |
| 263 | struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | struct sigframe __user *frame; |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 266 | void __user *restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | int err = 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 268 | void __user *fpstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 270 | frame = get_sigframe(ka, 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 (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 (_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 | } |
| 286 | |
Roland McGrath | 1a3e4ca | 2008-04-09 01:29:27 -0700 | [diff] [blame] | 287 | if (current->mm->context.vdso) |
Roland McGrath | 6c3652e | 2008-01-30 13:30:42 +0100 | [diff] [blame] | 288 | restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn); |
Andi Kleen | 9fbbd4d | 2007-02-13 13:26:26 +0100 | [diff] [blame] | 289 | else |
Jan Engelhardt | ade1af7 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 290 | restorer = &frame->retcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (ka->sa.sa_flags & SA_RESTORER) |
| 292 | restorer = ka->sa.sa_restorer; |
| 293 | |
| 294 | /* Set up to return from userspace. */ |
| 295 | err |= __put_user(restorer, &frame->pretcode); |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 298 | * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | * |
| 300 | * WE DO NOT USE IT ANY MORE! It's only left here for historical |
| 301 | * reasons and because gdb uses it as a signature to notice |
| 302 | * signal handler stack frames. |
| 303 | */ |
Hiroshi Shimamoto | 4a61204 | 2008-11-11 19:09:29 -0800 | [diff] [blame] | 304 | err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | if (err) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 307 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | /* Set up registers for signal handler */ |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 310 | regs->sp = (unsigned long)frame; |
| 311 | regs->ip = (unsigned long)ka->sa.sa_handler; |
| 312 | regs->ax = (unsigned long)sig; |
Harvey Harrison | 92bc205 | 2008-02-08 12:09:56 -0800 | [diff] [blame] | 313 | regs->dx = 0; |
| 314 | regs->cx = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 316 | regs->ds = __USER_DS; |
| 317 | regs->es = __USER_DS; |
| 318 | regs->ss = __USER_DS; |
| 319 | regs->cs = __USER_CS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 321 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Hiroshi Shimamoto | 1d13024 | 2008-09-05 16:28:06 -0700 | [diff] [blame] | 324 | static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
| 325 | sigset_t *set, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | struct rt_sigframe __user *frame; |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 328 | void __user *restorer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | int err = 0; |
Suresh Siddha | ab51370 | 2008-07-29 10:29:22 -0700 | [diff] [blame] | 330 | void __user *fpstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 332 | frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 335 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Hiroshi Shimamoto | 13ad772 | 2008-09-05 16:28:38 -0700 | [diff] [blame] | 337 | err |= __put_user(sig, &frame->sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | err |= __put_user(&frame->info, &frame->pinfo); |
| 339 | err |= __put_user(&frame->uc, &frame->puc); |
| 340 | err |= copy_siginfo_to_user(&frame->info, info); |
| 341 | if (err) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 342 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | /* Create the ucontext. */ |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 345 | if (cpu_has_xsave) |
| 346 | err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags); |
| 347 | else |
| 348 | err |= __put_user(0, &frame->uc.uc_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | err |= __put_user(0, &frame->uc.uc_link); |
| 350 | err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 351 | err |= __put_user(sas_ss_flags(regs->sp), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | &frame->uc.uc_stack.ss_flags); |
| 353 | err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); |
Suresh Siddha | 3c1c7f1 | 2008-07-29 10:29:21 -0700 | [diff] [blame] | 354 | err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate, |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 355 | regs, set->sig[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 357 | if (err) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 358 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | /* Set up to return from userspace. */ |
Roland McGrath | 6c3652e | 2008-01-30 13:30:42 +0100 | [diff] [blame] | 361 | restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (ka->sa.sa_flags & SA_RESTORER) |
| 363 | restorer = ka->sa.sa_restorer; |
| 364 | err |= __put_user(restorer, &frame->pretcode); |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | /* |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 367 | * This is movl $__NR_rt_sigreturn, %ax ; int $0x80 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | * |
| 369 | * WE DO NOT USE IT ANY MORE! It's only left here for historical |
| 370 | * reasons and because gdb uses it as a signature to notice |
| 371 | * signal handler stack frames. |
| 372 | */ |
Hiroshi Shimamoto | 4a61204 | 2008-11-11 19:09:29 -0800 | [diff] [blame] | 373 | err |= __put_user(*((u64 *)&rt_retcode), (u64 *)frame->retcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | if (err) |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 376 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | /* Set up registers for signal handler */ |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 379 | regs->sp = (unsigned long)frame; |
| 380 | regs->ip = (unsigned long)ka->sa.sa_handler; |
Hiroshi Shimamoto | 13ad772 | 2008-09-05 16:28:38 -0700 | [diff] [blame] | 381 | regs->ax = (unsigned long)sig; |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 382 | regs->dx = (unsigned long)&frame->info; |
| 383 | regs->cx = (unsigned long)&frame->uc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 385 | regs->ds = __USER_DS; |
| 386 | regs->es = __USER_DS; |
| 387 | regs->ss = __USER_DS; |
| 388 | regs->cs = __USER_CS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 390 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
Hiroshi Shimamoto | bfeb91a | 2008-11-24 18:23:12 -0800 | [diff] [blame^] | 392 | #else /* !CONFIG_X86_32 */ |
| 393 | /* |
| 394 | * Determine which stack to use.. |
| 395 | */ |
| 396 | static void __user * |
| 397 | get_stack(struct k_sigaction *ka, unsigned long sp, unsigned long size) |
| 398 | { |
| 399 | /* Default to using normal stack - redzone*/ |
| 400 | sp -= 128; |
| 401 | |
| 402 | /* This is the X/Open sanctioned signal stack switching. */ |
| 403 | if (ka->sa.sa_flags & SA_ONSTACK) { |
| 404 | if (sas_ss_flags(sp) == 0) |
| 405 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 406 | } |
| 407 | |
| 408 | return (void __user *)round_down(sp - size, 64); |
| 409 | } |
| 410 | |
| 411 | static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
| 412 | sigset_t *set, struct pt_regs *regs) |
| 413 | { |
| 414 | struct rt_sigframe __user *frame; |
| 415 | void __user *fp = NULL; |
| 416 | int err = 0; |
| 417 | struct task_struct *me = current; |
| 418 | |
| 419 | if (used_math()) { |
| 420 | fp = get_stack(ka, regs->sp, sig_xstate_size); |
| 421 | frame = (void __user *)round_down( |
| 422 | (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8; |
| 423 | |
| 424 | if (save_i387_xstate(fp) < 0) |
| 425 | return -EFAULT; |
| 426 | } else |
| 427 | frame = get_stack(ka, regs->sp, sizeof(struct rt_sigframe)) - 8; |
| 428 | |
| 429 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 430 | return -EFAULT; |
| 431 | |
| 432 | if (ka->sa.sa_flags & SA_SIGINFO) { |
| 433 | if (copy_siginfo_to_user(&frame->info, info)) |
| 434 | return -EFAULT; |
| 435 | } |
| 436 | |
| 437 | /* Create the ucontext. */ |
| 438 | if (cpu_has_xsave) |
| 439 | err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags); |
| 440 | else |
| 441 | err |= __put_user(0, &frame->uc.uc_flags); |
| 442 | err |= __put_user(0, &frame->uc.uc_link); |
| 443 | err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp); |
| 444 | err |= __put_user(sas_ss_flags(regs->sp), |
| 445 | &frame->uc.uc_stack.ss_flags); |
| 446 | err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size); |
| 447 | err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]); |
| 448 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 449 | |
| 450 | /* Set up to return from userspace. If provided, use a stub |
| 451 | already in userspace. */ |
| 452 | /* x86-64 should always use SA_RESTORER. */ |
| 453 | if (ka->sa.sa_flags & SA_RESTORER) { |
| 454 | err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); |
| 455 | } else { |
| 456 | /* could use a vstub here */ |
| 457 | return -EFAULT; |
| 458 | } |
| 459 | |
| 460 | if (err) |
| 461 | return -EFAULT; |
| 462 | |
| 463 | /* Set up registers for signal handler */ |
| 464 | regs->di = sig; |
| 465 | /* In case the signal handler was declared without prototypes */ |
| 466 | regs->ax = 0; |
| 467 | |
| 468 | /* This also works for non SA_SIGINFO handlers because they expect the |
| 469 | next argument after the signal number on the stack. */ |
| 470 | regs->si = (unsigned long)&frame->info; |
| 471 | regs->dx = (unsigned long)&frame->uc; |
| 472 | regs->ip = (unsigned long) ka->sa.sa_handler; |
| 473 | |
| 474 | regs->sp = (unsigned long)frame; |
| 475 | |
| 476 | /* Set up the CS register to run signal handlers in 64-bit mode, |
| 477 | even if the handler happens to be interrupting 32-bit code. */ |
| 478 | regs->cs = __USER_CS; |
| 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | #endif /* CONFIG_X86_32 */ |
| 483 | |
| 484 | /* |
| 485 | * Atomically swap in the new signal mask, and wait for a signal. |
| 486 | */ |
| 487 | asmlinkage int |
| 488 | sys_sigsuspend(int history0, int history1, old_sigset_t mask) |
| 489 | { |
| 490 | mask &= _BLOCKABLE; |
| 491 | spin_lock_irq(¤t->sighand->siglock); |
| 492 | current->saved_sigmask = current->blocked; |
| 493 | siginitset(¤t->blocked, mask); |
| 494 | recalc_sigpending(); |
| 495 | spin_unlock_irq(¤t->sighand->siglock); |
| 496 | |
| 497 | current->state = TASK_INTERRUPTIBLE; |
| 498 | schedule(); |
| 499 | set_restore_sigmask(); |
| 500 | |
| 501 | return -ERESTARTNOHAND; |
| 502 | } |
| 503 | |
| 504 | asmlinkage int |
| 505 | sys_sigaction(int sig, const struct old_sigaction __user *act, |
| 506 | struct old_sigaction __user *oact) |
| 507 | { |
| 508 | struct k_sigaction new_ka, old_ka; |
| 509 | int ret; |
| 510 | |
| 511 | if (act) { |
| 512 | old_sigset_t mask; |
| 513 | |
| 514 | if (!access_ok(VERIFY_READ, act, sizeof(*act)) || |
| 515 | __get_user(new_ka.sa.sa_handler, &act->sa_handler) || |
| 516 | __get_user(new_ka.sa.sa_restorer, &act->sa_restorer)) |
| 517 | return -EFAULT; |
| 518 | |
| 519 | __get_user(new_ka.sa.sa_flags, &act->sa_flags); |
| 520 | __get_user(mask, &act->sa_mask); |
| 521 | siginitset(&new_ka.sa.sa_mask, mask); |
| 522 | } |
| 523 | |
| 524 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); |
| 525 | |
| 526 | if (!ret && oact) { |
| 527 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || |
| 528 | __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || |
| 529 | __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer)) |
| 530 | return -EFAULT; |
| 531 | |
| 532 | __put_user(old_ka.sa.sa_flags, &oact->sa_flags); |
| 533 | __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask); |
| 534 | } |
| 535 | |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | #ifdef CONFIG_X86_32 |
| 540 | asmlinkage int sys_sigaltstack(unsigned long bx) |
| 541 | { |
| 542 | /* |
| 543 | * This is needed to make gcc realize it doesn't own the |
| 544 | * "struct pt_regs" |
| 545 | */ |
| 546 | struct pt_regs *regs = (struct pt_regs *)&bx; |
| 547 | const stack_t __user *uss = (const stack_t __user *)bx; |
| 548 | stack_t __user *uoss = (stack_t __user *)regs->cx; |
| 549 | |
| 550 | return do_sigaltstack(uss, uoss, regs->sp); |
| 551 | } |
| 552 | #else /* !CONFIG_X86_32 */ |
| 553 | asmlinkage long |
| 554 | sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, |
| 555 | struct pt_regs *regs) |
| 556 | { |
| 557 | return do_sigaltstack(uss, uoss, regs->sp); |
| 558 | } |
| 559 | #endif /* CONFIG_X86_32 */ |
| 560 | |
| 561 | /* |
| 562 | * Do a signal return; undo the signal stack. |
| 563 | */ |
| 564 | asmlinkage unsigned long sys_sigreturn(unsigned long __unused) |
| 565 | { |
| 566 | struct sigframe __user *frame; |
| 567 | struct pt_regs *regs; |
| 568 | unsigned long ax; |
| 569 | sigset_t set; |
| 570 | |
| 571 | regs = (struct pt_regs *) &__unused; |
| 572 | frame = (struct sigframe __user *)(regs->sp - 8); |
| 573 | |
| 574 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 575 | goto badframe; |
| 576 | if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1 |
| 577 | && __copy_from_user(&set.sig[1], &frame->extramask, |
| 578 | sizeof(frame->extramask)))) |
| 579 | goto badframe; |
| 580 | |
| 581 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 582 | spin_lock_irq(¤t->sighand->siglock); |
| 583 | current->blocked = set; |
| 584 | recalc_sigpending(); |
| 585 | spin_unlock_irq(¤t->sighand->siglock); |
| 586 | |
| 587 | if (restore_sigcontext(regs, &frame->sc, &ax)) |
| 588 | goto badframe; |
| 589 | return ax; |
| 590 | |
| 591 | badframe: |
| 592 | if (show_unhandled_signals && printk_ratelimit()) { |
| 593 | printk("%s%s[%d] bad frame in sigreturn frame:" |
| 594 | "%p ip:%lx sp:%lx oeax:%lx", |
| 595 | task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG, |
| 596 | current->comm, task_pid_nr(current), frame, regs->ip, |
| 597 | regs->sp, regs->orig_ax); |
| 598 | print_vma_addr(" in ", regs->ip); |
| 599 | printk(KERN_CONT "\n"); |
| 600 | } |
| 601 | |
| 602 | force_sig(SIGSEGV, current); |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static long do_rt_sigreturn(struct pt_regs *regs) |
| 608 | { |
| 609 | struct rt_sigframe __user *frame; |
| 610 | unsigned long ax; |
| 611 | sigset_t set; |
| 612 | |
| 613 | frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long)); |
| 614 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 615 | goto badframe; |
| 616 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 617 | goto badframe; |
| 618 | |
| 619 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 620 | spin_lock_irq(¤t->sighand->siglock); |
| 621 | current->blocked = set; |
| 622 | recalc_sigpending(); |
| 623 | spin_unlock_irq(¤t->sighand->siglock); |
| 624 | |
| 625 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax)) |
| 626 | goto badframe; |
| 627 | |
| 628 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT) |
| 629 | goto badframe; |
| 630 | |
| 631 | return ax; |
| 632 | |
| 633 | badframe: |
| 634 | signal_fault(regs, frame, "rt_sigreturn"); |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | #ifdef CONFIG_X86_32 |
| 639 | asmlinkage int sys_rt_sigreturn(unsigned long __unused) |
| 640 | { |
| 641 | struct pt_regs *regs = (struct pt_regs *)&__unused; |
| 642 | |
| 643 | return do_rt_sigreturn(regs); |
| 644 | } |
| 645 | #else /* !CONFIG_X86_32 */ |
| 646 | asmlinkage long sys_rt_sigreturn(struct pt_regs *regs) |
| 647 | { |
| 648 | return do_rt_sigreturn(regs); |
| 649 | } |
| 650 | #endif /* CONFIG_X86_32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
| 652 | /* |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 653 | * OK, we're invoking a handler: |
| 654 | */ |
Hiroshi Shimamoto | 8d8c13b | 2008-09-24 19:10:29 -0700 | [diff] [blame] | 655 | static int signr_convert(int sig) |
| 656 | { |
Hiroshi Shimamoto | 96bf84b | 2008-10-29 18:44:08 -0700 | [diff] [blame] | 657 | #ifdef CONFIG_X86_32 |
Hiroshi Shimamoto | 8d8c13b | 2008-09-24 19:10:29 -0700 | [diff] [blame] | 658 | struct thread_info *info = current_thread_info(); |
| 659 | |
| 660 | if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32) |
| 661 | return info->exec_domain->signal_invmap[sig]; |
Hiroshi Shimamoto | 96bf84b | 2008-10-29 18:44:08 -0700 | [diff] [blame] | 662 | #endif /* CONFIG_X86_32 */ |
Hiroshi Shimamoto | 8d8c13b | 2008-09-24 19:10:29 -0700 | [diff] [blame] | 663 | return sig; |
| 664 | } |
| 665 | |
Hiroshi Shimamoto | cabf503 | 2008-10-29 18:46:07 -0700 | [diff] [blame] | 666 | #ifdef CONFIG_X86_32 |
| 667 | |
Hiroshi Shimamoto | 455edbc | 2008-09-24 19:13:11 -0700 | [diff] [blame] | 668 | #define is_ia32 1 |
Hiroshi Shimamoto | 4694d23 | 2008-09-24 19:13:29 -0700 | [diff] [blame] | 669 | #define ia32_setup_frame __setup_frame |
| 670 | #define ia32_setup_rt_frame __setup_rt_frame |
Hiroshi Shimamoto | 455edbc | 2008-09-24 19:13:11 -0700 | [diff] [blame] | 671 | |
Hiroshi Shimamoto | cabf503 | 2008-10-29 18:46:07 -0700 | [diff] [blame] | 672 | #else /* !CONFIG_X86_32 */ |
| 673 | |
| 674 | #ifdef CONFIG_IA32_EMULATION |
| 675 | #define is_ia32 test_thread_flag(TIF_IA32) |
| 676 | #else /* !CONFIG_IA32_EMULATION */ |
| 677 | #define is_ia32 0 |
| 678 | #endif /* CONFIG_IA32_EMULATION */ |
| 679 | |
| 680 | #endif /* CONFIG_X86_32 */ |
| 681 | |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 682 | static int |
Hiroshi Shimamoto | 1d13024 | 2008-09-05 16:28:06 -0700 | [diff] [blame] | 683 | setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
| 684 | sigset_t *set, struct pt_regs *regs) |
| 685 | { |
Hiroshi Shimamoto | 8d8c13b | 2008-09-24 19:10:29 -0700 | [diff] [blame] | 686 | int usig = signr_convert(sig); |
Hiroshi Shimamoto | 1d13024 | 2008-09-05 16:28:06 -0700 | [diff] [blame] | 687 | int ret; |
| 688 | |
| 689 | /* Set up the stack frame */ |
Hiroshi Shimamoto | 455edbc | 2008-09-24 19:13:11 -0700 | [diff] [blame] | 690 | if (is_ia32) { |
| 691 | if (ka->sa.sa_flags & SA_SIGINFO) |
Hiroshi Shimamoto | 4694d23 | 2008-09-24 19:13:29 -0700 | [diff] [blame] | 692 | ret = ia32_setup_rt_frame(usig, ka, info, set, regs); |
Hiroshi Shimamoto | 455edbc | 2008-09-24 19:13:11 -0700 | [diff] [blame] | 693 | else |
Hiroshi Shimamoto | 4694d23 | 2008-09-24 19:13:29 -0700 | [diff] [blame] | 694 | ret = ia32_setup_frame(usig, ka, set, regs); |
Hiroshi Shimamoto | 455edbc | 2008-09-24 19:13:11 -0700 | [diff] [blame] | 695 | } else |
| 696 | ret = __setup_rt_frame(sig, ka, info, set, regs); |
Hiroshi Shimamoto | 1d13024 | 2008-09-05 16:28:06 -0700 | [diff] [blame] | 697 | |
Hiroshi Shimamoto | 3d0aedd | 2008-09-12 17:01:09 -0700 | [diff] [blame] | 698 | if (ret) { |
| 699 | force_sigsegv(sig, current); |
| 700 | return -EFAULT; |
| 701 | } |
| 702 | |
Hiroshi Shimamoto | 1d13024 | 2008-09-05 16:28:06 -0700 | [diff] [blame] | 703 | return ret; |
| 704 | } |
| 705 | |
| 706 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, |
Harvey Harrison | ac66f3f | 2008-02-08 12:09:58 -0800 | [diff] [blame] | 708 | sigset_t *oldset, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 710 | int ret; |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | /* Are we from a system call? */ |
Hiroshi Shimamoto | bb57925 | 2008-09-05 16:26:55 -0700 | [diff] [blame] | 713 | if (syscall_get_nr(current, regs) >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | /* If so, check system call restarting.. */ |
Hiroshi Shimamoto | bb57925 | 2008-09-05 16:26:55 -0700 | [diff] [blame] | 715 | switch (syscall_get_error(current, regs)) { |
Harvey Harrison | ac66f3f | 2008-02-08 12:09:58 -0800 | [diff] [blame] | 716 | case -ERESTART_RESTARTBLOCK: |
| 717 | case -ERESTARTNOHAND: |
| 718 | regs->ax = -EINTR; |
| 719 | break; |
| 720 | |
| 721 | case -ERESTARTSYS: |
| 722 | if (!(ka->sa.sa_flags & SA_RESTART)) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 723 | regs->ax = -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | break; |
Harvey Harrison | ac66f3f | 2008-02-08 12:09:58 -0800 | [diff] [blame] | 725 | } |
| 726 | /* fallthrough */ |
| 727 | case -ERESTARTNOINTR: |
| 728 | regs->ax = regs->orig_ax; |
| 729 | regs->ip -= 2; |
| 730 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
| 734 | /* |
Roland McGrath | e1f2877 | 2008-01-30 13:30:50 +0100 | [diff] [blame] | 735 | * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF |
| 736 | * flag so that register information in the sigcontext is correct. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 738 | if (unlikely(regs->flags & X86_EFLAGS_TF) && |
Roland McGrath | e1f2877 | 2008-01-30 13:30:50 +0100 | [diff] [blame] | 739 | likely(test_and_clear_thread_flag(TIF_FORCED_TF))) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 740 | regs->flags &= ~X86_EFLAGS_TF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Hiroshi Shimamoto | 1d13024 | 2008-09-05 16:28:06 -0700 | [diff] [blame] | 742 | ret = setup_rt_frame(sig, ka, info, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 744 | if (ret) |
| 745 | return ret; |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 746 | |
Hiroshi Shimamoto | 86d3237 | 2008-09-23 17:22:32 -0700 | [diff] [blame] | 747 | #ifdef CONFIG_X86_64 |
| 748 | /* |
| 749 | * This has nothing to do with segment registers, |
| 750 | * despite the name. This magic affects uaccess.h |
| 751 | * macros' behavior. Reset it to the normal setting. |
| 752 | */ |
| 753 | set_fs(USER_DS); |
| 754 | #endif |
| 755 | |
Roland McGrath | 8b9c5ff | 2008-04-19 14:26:54 -0700 | [diff] [blame] | 756 | /* |
| 757 | * Clear the direction flag as per the ABI for function entry. |
| 758 | */ |
| 759 | regs->flags &= ~X86_EFLAGS_DF; |
| 760 | |
| 761 | /* |
| 762 | * Clear TF when entering the signal handler, but |
| 763 | * notify any tracer that was single-stepping it. |
| 764 | * The tracer may want to single-step inside the |
| 765 | * handler too. |
| 766 | */ |
| 767 | regs->flags &= ~X86_EFLAGS_TF; |
Roland McGrath | 8b9c5ff | 2008-04-19 14:26:54 -0700 | [diff] [blame] | 768 | |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 769 | spin_lock_irq(¤t->sighand->siglock); |
| 770 | sigorsets(¤t->blocked, ¤t->blocked, &ka->sa.sa_mask); |
| 771 | if (!(ka->sa.sa_flags & SA_NODEFER)) |
| 772 | sigaddset(¤t->blocked, sig); |
| 773 | recalc_sigpending(); |
| 774 | spin_unlock_irq(¤t->sighand->siglock); |
| 775 | |
Roland McGrath | 36a0330 | 2008-03-14 17:46:38 -0700 | [diff] [blame] | 776 | tracehook_signal_handler(sig, info, ka, regs, |
| 777 | test_thread_flag(TIF_SINGLESTEP)); |
| 778 | |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 779 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Hiroshi Shimamoto | 5791775 | 2008-10-29 18:46:40 -0700 | [diff] [blame] | 782 | #ifdef CONFIG_X86_32 |
Hiroshi Shimamoto | 8fcd8e2 | 2008-09-05 16:27:39 -0700 | [diff] [blame] | 783 | #define NR_restart_syscall __NR_restart_syscall |
Hiroshi Shimamoto | 5791775 | 2008-10-29 18:46:40 -0700 | [diff] [blame] | 784 | #else /* !CONFIG_X86_32 */ |
| 785 | #define NR_restart_syscall \ |
| 786 | test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall |
| 787 | #endif /* CONFIG_X86_32 */ |
| 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | /* |
| 790 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 791 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 792 | * mistake. |
| 793 | */ |
Harvey Harrison | 75604d7 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 794 | static void do_signal(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
Harvey Harrison | ac66f3f | 2008-02-08 12:09:58 -0800 | [diff] [blame] | 796 | struct k_sigaction ka; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | siginfo_t info; |
| 798 | int signr; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 799 | sigset_t *oldset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
| 801 | /* |
Harvey Harrison | ac66f3f | 2008-02-08 12:09:58 -0800 | [diff] [blame] | 802 | * We want the common case to go fast, which is why we may in certain |
| 803 | * cases get here from kernel mode. Just return without doing anything |
| 804 | * if so. |
| 805 | * X86_32: vm86 regs switched out by assembly code before reaching |
| 806 | * here, so testing against kernel CS suffices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | */ |
Vincent Hanquez | 717b594 | 2005-06-23 00:08:45 -0700 | [diff] [blame] | 808 | if (!user_mode(regs)) |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 809 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Roland McGrath | 5a8da0e | 2008-04-30 00:53:10 -0700 | [diff] [blame] | 811 | if (current_thread_info()->status & TS_RESTORE_SIGMASK) |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 812 | oldset = ¤t->saved_sigmask; |
| 813 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | oldset = ¤t->blocked; |
| 815 | |
| 816 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
| 817 | if (signr > 0) { |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 818 | /* |
| 819 | * Re-enable any watchpoints before delivering the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | * signal to user space. The processor register will |
| 821 | * have been cleared if the watchpoint triggered |
| 822 | * inside the kernel. |
| 823 | */ |
Harvey Harrison | ac66f3f | 2008-02-08 12:09:58 -0800 | [diff] [blame] | 824 | if (current->thread.debugreg7) |
Roland McGrath | 0f53409 | 2008-01-30 13:30:59 +0100 | [diff] [blame] | 825 | set_debugreg(current->thread.debugreg7, 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 827 | /* Whee! Actually deliver the signal. */ |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 828 | if (handle_signal(signr, &info, &ka, oldset, regs) == 0) { |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 829 | /* |
Roland McGrath | 5a8da0e | 2008-04-30 00:53:10 -0700 | [diff] [blame] | 830 | * A signal was successfully delivered; the saved |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 831 | * sigmask will have been stored in the signal frame, |
| 832 | * and will be restored by sigreturn, so we can simply |
Roland McGrath | 5a8da0e | 2008-04-30 00:53:10 -0700 | [diff] [blame] | 833 | * clear the TS_RESTORE_SIGMASK flag. |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 834 | */ |
Roland McGrath | 5a8da0e | 2008-04-30 00:53:10 -0700 | [diff] [blame] | 835 | current_thread_info()->status &= ~TS_RESTORE_SIGMASK; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 836 | } |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 837 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* Did we come from a system call? */ |
Hiroshi Shimamoto | bb57925 | 2008-09-05 16:26:55 -0700 | [diff] [blame] | 841 | if (syscall_get_nr(current, regs) >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | /* Restart the system call - no handlers present */ |
Hiroshi Shimamoto | bb57925 | 2008-09-05 16:26:55 -0700 | [diff] [blame] | 843 | switch (syscall_get_error(current, regs)) { |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 844 | case -ERESTARTNOHAND: |
| 845 | case -ERESTARTSYS: |
| 846 | case -ERESTARTNOINTR: |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 847 | regs->ax = regs->orig_ax; |
| 848 | regs->ip -= 2; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 849 | break; |
| 850 | |
| 851 | case -ERESTART_RESTARTBLOCK: |
Hiroshi Shimamoto | 8fcd8e2 | 2008-09-05 16:27:39 -0700 | [diff] [blame] | 852 | regs->ax = NR_restart_syscall; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 853 | regs->ip -= 2; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 854 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 857 | |
Harvey Harrison | ac66f3f | 2008-02-08 12:09:58 -0800 | [diff] [blame] | 858 | /* |
| 859 | * If there's no signal to deliver, we just put the saved sigmask |
| 860 | * back. |
| 861 | */ |
Roland McGrath | 5a8da0e | 2008-04-30 00:53:10 -0700 | [diff] [blame] | 862 | if (current_thread_info()->status & TS_RESTORE_SIGMASK) { |
| 863 | current_thread_info()->status &= ~TS_RESTORE_SIGMASK; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 864 | sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); |
| 865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* |
| 869 | * notification of userspace execution resumption |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 870 | * - triggered by the TIF_WORK_MASK flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | */ |
Ingo Molnar | 7e907f4 | 2008-03-06 10:33:08 +0100 | [diff] [blame] | 872 | void |
| 873 | do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Hiroshi Shimamoto | ee847c5 | 2008-09-23 17:21:45 -0700 | [diff] [blame] | 875 | #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE) |
| 876 | /* notify userspace of pending MCEs */ |
| 877 | if (thread_info_flags & _TIF_MCE_NOTIFY) |
| 878 | mce_notify_user(); |
| 879 | #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */ |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* deal with pending signal delivery */ |
Roland McGrath | 5a8da0e | 2008-04-30 00:53:10 -0700 | [diff] [blame] | 882 | if (thread_info_flags & _TIF_SIGPENDING) |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 883 | do_signal(regs); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 884 | |
Roland McGrath | 59e5213 | 2008-04-19 19:10:57 -0700 | [diff] [blame] | 885 | if (thread_info_flags & _TIF_NOTIFY_RESUME) { |
| 886 | clear_thread_flag(TIF_NOTIFY_RESUME); |
| 887 | tracehook_notify_resume(regs); |
| 888 | } |
| 889 | |
Hiroshi Shimamoto | ee847c5 | 2008-09-23 17:21:45 -0700 | [diff] [blame] | 890 | #ifdef CONFIG_X86_32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | clear_thread_flag(TIF_IRET); |
Hiroshi Shimamoto | ee847c5 | 2008-09-23 17:21:45 -0700 | [diff] [blame] | 892 | #endif /* CONFIG_X86_32 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | } |
Hiroshi Shimamoto | 72fa50f | 2008-09-05 16:27:11 -0700 | [diff] [blame] | 894 | |
| 895 | void signal_fault(struct pt_regs *regs, void __user *frame, char *where) |
| 896 | { |
| 897 | struct task_struct *me = current; |
| 898 | |
| 899 | if (show_unhandled_signals && printk_ratelimit()) { |
| 900 | printk(KERN_INFO |
| 901 | "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx", |
| 902 | me->comm, me->pid, where, frame, |
| 903 | regs->ip, regs->sp, regs->orig_ax); |
| 904 | print_vma_addr(" in ", regs->ip); |
| 905 | printk(KERN_CONT "\n"); |
| 906 | } |
| 907 | |
| 908 | force_sig(SIGSEGV, me); |
| 909 | } |