Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/i386/kernel/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 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/smp_lock.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/signal.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/wait.h> |
| 18 | #include <linux/unistd.h> |
| 19 | #include <linux/stddef.h> |
| 20 | #include <linux/personality.h> |
| 21 | #include <linux/suspend.h> |
| 22 | #include <linux/ptrace.h> |
| 23 | #include <linux/elf.h> |
Andi Kleen | 9fbbd4d | 2007-02-13 13:26:26 +0100 | [diff] [blame^] | 24 | #include <linux/binfmts.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/processor.h> |
| 26 | #include <asm/ucontext.h> |
| 27 | #include <asm/uaccess.h> |
| 28 | #include <asm/i387.h> |
| 29 | #include "sigframe.h" |
| 30 | |
| 31 | #define DEBUG_SIG 0 |
| 32 | |
| 33 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) |
| 34 | |
| 35 | /* |
| 36 | * Atomically swap in the new signal mask, and wait for a signal. |
| 37 | */ |
| 38 | asmlinkage int |
| 39 | sys_sigsuspend(int history0, int history1, old_sigset_t mask) |
| 40 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | mask &= _BLOCKABLE; |
| 42 | spin_lock_irq(¤t->sighand->siglock); |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 43 | current->saved_sigmask = current->blocked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | siginitset(¤t->blocked, mask); |
| 45 | recalc_sigpending(); |
| 46 | spin_unlock_irq(¤t->sighand->siglock); |
| 47 | |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 48 | current->state = TASK_INTERRUPTIBLE; |
| 49 | schedule(); |
| 50 | set_thread_flag(TIF_RESTORE_SIGMASK); |
| 51 | return -ERESTARTNOHAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | asmlinkage int |
| 55 | sys_sigaction(int sig, const struct old_sigaction __user *act, |
| 56 | struct old_sigaction __user *oact) |
| 57 | { |
| 58 | struct k_sigaction new_ka, old_ka; |
| 59 | int ret; |
| 60 | |
| 61 | if (act) { |
| 62 | old_sigset_t mask; |
| 63 | if (!access_ok(VERIFY_READ, act, sizeof(*act)) || |
| 64 | __get_user(new_ka.sa.sa_handler, &act->sa_handler) || |
| 65 | __get_user(new_ka.sa.sa_restorer, &act->sa_restorer)) |
| 66 | return -EFAULT; |
| 67 | __get_user(new_ka.sa.sa_flags, &act->sa_flags); |
| 68 | __get_user(mask, &act->sa_mask); |
| 69 | siginitset(&new_ka.sa.sa_mask, mask); |
| 70 | } |
| 71 | |
| 72 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); |
| 73 | |
| 74 | if (!ret && oact) { |
| 75 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || |
| 76 | __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || |
| 77 | __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer)) |
| 78 | return -EFAULT; |
| 79 | __put_user(old_ka.sa.sa_flags, &oact->sa_flags); |
| 80 | __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask); |
| 81 | } |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | asmlinkage int |
| 87 | sys_sigaltstack(unsigned long ebx) |
| 88 | { |
| 89 | /* This is needed to make gcc realize it doesn't own the "struct pt_regs" */ |
| 90 | struct pt_regs *regs = (struct pt_regs *)&ebx; |
| 91 | const stack_t __user *uss = (const stack_t __user *)ebx; |
| 92 | stack_t __user *uoss = (stack_t __user *)regs->ecx; |
| 93 | |
| 94 | return do_sigaltstack(uss, uoss, regs->esp); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* |
| 99 | * Do a signal return; undo the signal stack. |
| 100 | */ |
| 101 | |
| 102 | static int |
| 103 | restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *peax) |
| 104 | { |
| 105 | unsigned int err = 0; |
| 106 | |
| 107 | /* Always make any pending restarted system calls return -EINTR */ |
| 108 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 109 | |
| 110 | #define COPY(x) err |= __get_user(regs->x, &sc->x) |
| 111 | |
| 112 | #define COPY_SEG(seg) \ |
| 113 | { unsigned short tmp; \ |
| 114 | err |= __get_user(tmp, &sc->seg); \ |
| 115 | regs->x##seg = tmp; } |
| 116 | |
| 117 | #define COPY_SEG_STRICT(seg) \ |
| 118 | { unsigned short tmp; \ |
| 119 | err |= __get_user(tmp, &sc->seg); \ |
| 120 | regs->x##seg = tmp|3; } |
| 121 | |
| 122 | #define GET_SEG(seg) \ |
| 123 | { unsigned short tmp; \ |
| 124 | err |= __get_user(tmp, &sc->seg); \ |
| 125 | loadsegment(seg,tmp); } |
| 126 | |
Chuck Ebbert | 8bed51c | 2006-03-23 02:59:40 -0800 | [diff] [blame] | 127 | #define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_RF | \ |
| 128 | X86_EFLAGS_OF | X86_EFLAGS_DF | \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \ |
| 130 | X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF) |
| 131 | |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 132 | GET_SEG(gs); |
| 133 | COPY_SEG(fs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | COPY_SEG(es); |
| 135 | COPY_SEG(ds); |
| 136 | COPY(edi); |
| 137 | COPY(esi); |
| 138 | COPY(ebp); |
| 139 | COPY(esp); |
| 140 | COPY(ebx); |
| 141 | COPY(edx); |
| 142 | COPY(ecx); |
| 143 | COPY(eip); |
| 144 | COPY_SEG_STRICT(cs); |
| 145 | COPY_SEG_STRICT(ss); |
| 146 | |
| 147 | { |
| 148 | unsigned int tmpflags; |
| 149 | err |= __get_user(tmpflags, &sc->eflags); |
| 150 | regs->eflags = (regs->eflags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS); |
| 151 | regs->orig_eax = -1; /* disable syscall checks */ |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | struct _fpstate __user * buf; |
| 156 | err |= __get_user(buf, &sc->fpstate); |
| 157 | if (buf) { |
| 158 | if (!access_ok(VERIFY_READ, buf, sizeof(*buf))) |
| 159 | goto badframe; |
| 160 | err |= restore_i387(buf); |
| 161 | } else { |
| 162 | struct task_struct *me = current; |
| 163 | if (used_math()) { |
| 164 | clear_fpu(me); |
| 165 | clear_used_math(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | err |= __get_user(*peax, &sc->eax); |
| 171 | return err; |
| 172 | |
| 173 | badframe: |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | asmlinkage int sys_sigreturn(unsigned long __unused) |
| 178 | { |
| 179 | struct pt_regs *regs = (struct pt_regs *) &__unused; |
| 180 | struct sigframe __user *frame = (struct sigframe __user *)(regs->esp - 8); |
| 181 | sigset_t set; |
| 182 | int eax; |
| 183 | |
| 184 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 185 | goto badframe; |
| 186 | if (__get_user(set.sig[0], &frame->sc.oldmask) |
| 187 | || (_NSIG_WORDS > 1 |
| 188 | && __copy_from_user(&set.sig[1], &frame->extramask, |
| 189 | sizeof(frame->extramask)))) |
| 190 | goto badframe; |
| 191 | |
| 192 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 193 | spin_lock_irq(¤t->sighand->siglock); |
| 194 | current->blocked = set; |
| 195 | recalc_sigpending(); |
| 196 | spin_unlock_irq(¤t->sighand->siglock); |
| 197 | |
| 198 | if (restore_sigcontext(regs, &frame->sc, &eax)) |
| 199 | goto badframe; |
| 200 | return eax; |
| 201 | |
| 202 | badframe: |
| 203 | force_sig(SIGSEGV, current); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | asmlinkage int sys_rt_sigreturn(unsigned long __unused) |
| 208 | { |
| 209 | struct pt_regs *regs = (struct pt_regs *) &__unused; |
| 210 | struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4); |
| 211 | sigset_t set; |
| 212 | int eax; |
| 213 | |
| 214 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 215 | goto badframe; |
| 216 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 217 | goto badframe; |
| 218 | |
| 219 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 220 | spin_lock_irq(¤t->sighand->siglock); |
| 221 | current->blocked = set; |
| 222 | recalc_sigpending(); |
| 223 | spin_unlock_irq(¤t->sighand->siglock); |
| 224 | |
| 225 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax)) |
| 226 | goto badframe; |
| 227 | |
| 228 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT) |
| 229 | goto badframe; |
| 230 | |
| 231 | return eax; |
| 232 | |
| 233 | badframe: |
| 234 | force_sig(SIGSEGV, current); |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Set up a signal frame. |
| 240 | */ |
| 241 | |
| 242 | static int |
| 243 | setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate, |
| 244 | struct pt_regs *regs, unsigned long mask) |
| 245 | { |
| 246 | int tmp, err = 0; |
| 247 | |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 248 | err |= __put_user(regs->xfs, (unsigned int __user *)&sc->fs); |
| 249 | savesegment(gs, tmp); |
| 250 | err |= __put_user(tmp, (unsigned int __user *)&sc->gs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | err |= __put_user(regs->xes, (unsigned int __user *)&sc->es); |
| 253 | err |= __put_user(regs->xds, (unsigned int __user *)&sc->ds); |
| 254 | err |= __put_user(regs->edi, &sc->edi); |
| 255 | err |= __put_user(regs->esi, &sc->esi); |
| 256 | err |= __put_user(regs->ebp, &sc->ebp); |
| 257 | err |= __put_user(regs->esp, &sc->esp); |
| 258 | err |= __put_user(regs->ebx, &sc->ebx); |
| 259 | err |= __put_user(regs->edx, &sc->edx); |
| 260 | err |= __put_user(regs->ecx, &sc->ecx); |
| 261 | err |= __put_user(regs->eax, &sc->eax); |
| 262 | err |= __put_user(current->thread.trap_no, &sc->trapno); |
| 263 | err |= __put_user(current->thread.error_code, &sc->err); |
| 264 | err |= __put_user(regs->eip, &sc->eip); |
| 265 | err |= __put_user(regs->xcs, (unsigned int __user *)&sc->cs); |
| 266 | err |= __put_user(regs->eflags, &sc->eflags); |
| 267 | err |= __put_user(regs->esp, &sc->esp_at_signal); |
| 268 | err |= __put_user(regs->xss, (unsigned int __user *)&sc->ss); |
| 269 | |
| 270 | tmp = save_i387(fpstate); |
| 271 | if (tmp < 0) |
| 272 | err = 1; |
| 273 | else |
| 274 | err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate); |
| 275 | |
| 276 | /* non-iBCS2 extensions.. */ |
| 277 | err |= __put_user(mask, &sc->oldmask); |
| 278 | err |= __put_user(current->thread.cr2, &sc->cr2); |
| 279 | |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Determine which stack to use.. |
| 285 | */ |
| 286 | static inline void __user * |
| 287 | get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size) |
| 288 | { |
| 289 | unsigned long esp; |
| 290 | |
| 291 | /* Default to using normal stack */ |
| 292 | esp = regs->esp; |
| 293 | |
| 294 | /* This is the X/Open sanctioned signal stack switching. */ |
| 295 | if (ka->sa.sa_flags & SA_ONSTACK) { |
| 296 | if (sas_ss_flags(esp) == 0) |
| 297 | esp = current->sas_ss_sp + current->sas_ss_size; |
| 298 | } |
| 299 | |
| 300 | /* This is the legacy signal stack switching. */ |
| 301 | else if ((regs->xss & 0xffff) != __USER_DS && |
| 302 | !(ka->sa.sa_flags & SA_RESTORER) && |
| 303 | ka->sa.sa_restorer) { |
| 304 | esp = (unsigned long) ka->sa.sa_restorer; |
| 305 | } |
| 306 | |
Markus F.X.J. Oberhumer | d347f37 | 2005-10-09 18:54:23 +0200 | [diff] [blame] | 307 | esp -= frame_size; |
| 308 | /* Align the stack pointer according to the i386 ABI, |
| 309 | * i.e. so that on function entry ((sp + 4) & 15) == 0. */ |
| 310 | esp = ((esp + 4) & -16ul) - 4; |
| 311 | return (void __user *) esp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* These symbols are defined with the addresses in the vsyscall page. |
| 315 | See vsyscall-sigreturn.S. */ |
| 316 | extern void __user __kernel_sigreturn; |
| 317 | extern void __user __kernel_rt_sigreturn; |
| 318 | |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 319 | static int setup_frame(int sig, struct k_sigaction *ka, |
| 320 | sigset_t *set, struct pt_regs * regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
| 322 | void __user *restorer; |
| 323 | struct sigframe __user *frame; |
| 324 | int err = 0; |
| 325 | int usig; |
| 326 | |
| 327 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 328 | |
| 329 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 330 | goto give_sigsegv; |
| 331 | |
| 332 | usig = current_thread_info()->exec_domain |
| 333 | && current_thread_info()->exec_domain->signal_invmap |
| 334 | && sig < 32 |
| 335 | ? current_thread_info()->exec_domain->signal_invmap[sig] |
| 336 | : sig; |
| 337 | |
| 338 | err = __put_user(usig, &frame->sig); |
| 339 | if (err) |
| 340 | goto give_sigsegv; |
| 341 | |
| 342 | err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]); |
| 343 | if (err) |
| 344 | goto give_sigsegv; |
| 345 | |
| 346 | if (_NSIG_WORDS > 1) { |
| 347 | err = __copy_to_user(&frame->extramask, &set->sig[1], |
| 348 | sizeof(frame->extramask)); |
| 349 | if (err) |
| 350 | goto give_sigsegv; |
| 351 | } |
| 352 | |
Andi Kleen | 9fbbd4d | 2007-02-13 13:26:26 +0100 | [diff] [blame^] | 353 | if (current->binfmt->hasvdso) |
| 354 | restorer = (void *)VDSO_SYM(&__kernel_sigreturn); |
| 355 | else |
| 356 | restorer = (void *)&frame->retcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | if (ka->sa.sa_flags & SA_RESTORER) |
| 358 | restorer = ka->sa.sa_restorer; |
| 359 | |
| 360 | /* Set up to return from userspace. */ |
| 361 | err |= __put_user(restorer, &frame->pretcode); |
| 362 | |
| 363 | /* |
| 364 | * This is popl %eax ; movl $,%eax ; int $0x80 |
| 365 | * |
| 366 | * WE DO NOT USE IT ANY MORE! It's only left here for historical |
| 367 | * reasons and because gdb uses it as a signature to notice |
| 368 | * signal handler stack frames. |
| 369 | */ |
| 370 | err |= __put_user(0xb858, (short __user *)(frame->retcode+0)); |
| 371 | err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2)); |
| 372 | err |= __put_user(0x80cd, (short __user *)(frame->retcode+6)); |
| 373 | |
| 374 | if (err) |
| 375 | goto give_sigsegv; |
| 376 | |
| 377 | /* Set up registers for signal handler */ |
| 378 | regs->esp = (unsigned long) frame; |
| 379 | regs->eip = (unsigned long) ka->sa.sa_handler; |
| 380 | regs->eax = (unsigned long) sig; |
| 381 | regs->edx = (unsigned long) 0; |
| 382 | regs->ecx = (unsigned long) 0; |
| 383 | |
| 384 | set_fs(USER_DS); |
| 385 | regs->xds = __USER_DS; |
| 386 | regs->xes = __USER_DS; |
| 387 | regs->xss = __USER_DS; |
| 388 | regs->xcs = __USER_CS; |
| 389 | |
| 390 | /* |
| 391 | * Clear TF when entering the signal handler, but |
| 392 | * notify any tracer that was single-stepping it. |
| 393 | * The tracer may want to single-step inside the |
| 394 | * handler too. |
| 395 | */ |
| 396 | regs->eflags &= ~TF_MASK; |
| 397 | if (test_thread_flag(TIF_SINGLESTEP)) |
| 398 | ptrace_notify(SIGTRAP); |
| 399 | |
| 400 | #if DEBUG_SIG |
| 401 | printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", |
| 402 | current->comm, current->pid, frame, regs->eip, frame->pretcode); |
| 403 | #endif |
| 404 | |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 405 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | give_sigsegv: |
| 408 | force_sigsegv(sig, current); |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 409 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 412 | static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | sigset_t *set, struct pt_regs * regs) |
| 414 | { |
| 415 | void __user *restorer; |
| 416 | struct rt_sigframe __user *frame; |
| 417 | int err = 0; |
| 418 | int usig; |
| 419 | |
| 420 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 421 | |
| 422 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 423 | goto give_sigsegv; |
| 424 | |
| 425 | usig = current_thread_info()->exec_domain |
| 426 | && current_thread_info()->exec_domain->signal_invmap |
| 427 | && sig < 32 |
| 428 | ? current_thread_info()->exec_domain->signal_invmap[sig] |
| 429 | : sig; |
| 430 | |
| 431 | err |= __put_user(usig, &frame->sig); |
| 432 | err |= __put_user(&frame->info, &frame->pinfo); |
| 433 | err |= __put_user(&frame->uc, &frame->puc); |
| 434 | err |= copy_siginfo_to_user(&frame->info, info); |
| 435 | if (err) |
| 436 | goto give_sigsegv; |
| 437 | |
| 438 | /* Create the ucontext. */ |
| 439 | err |= __put_user(0, &frame->uc.uc_flags); |
| 440 | err |= __put_user(0, &frame->uc.uc_link); |
| 441 | err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp); |
| 442 | err |= __put_user(sas_ss_flags(regs->esp), |
| 443 | &frame->uc.uc_stack.ss_flags); |
| 444 | err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); |
| 445 | err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate, |
| 446 | regs, set->sig[0]); |
| 447 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 448 | if (err) |
| 449 | goto give_sigsegv; |
| 450 | |
| 451 | /* Set up to return from userspace. */ |
Ingo Molnar | e6e5494 | 2006-06-27 02:53:50 -0700 | [diff] [blame] | 452 | restorer = (void *)VDSO_SYM(&__kernel_rt_sigreturn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | if (ka->sa.sa_flags & SA_RESTORER) |
| 454 | restorer = ka->sa.sa_restorer; |
| 455 | err |= __put_user(restorer, &frame->pretcode); |
| 456 | |
| 457 | /* |
| 458 | * This is movl $,%eax ; int $0x80 |
| 459 | * |
| 460 | * WE DO NOT USE IT ANY MORE! It's only left here for historical |
| 461 | * reasons and because gdb uses it as a signature to notice |
| 462 | * signal handler stack frames. |
| 463 | */ |
| 464 | err |= __put_user(0xb8, (char __user *)(frame->retcode+0)); |
| 465 | err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1)); |
| 466 | err |= __put_user(0x80cd, (short __user *)(frame->retcode+5)); |
| 467 | |
| 468 | if (err) |
| 469 | goto give_sigsegv; |
| 470 | |
| 471 | /* Set up registers for signal handler */ |
| 472 | regs->esp = (unsigned long) frame; |
| 473 | regs->eip = (unsigned long) ka->sa.sa_handler; |
| 474 | regs->eax = (unsigned long) usig; |
| 475 | regs->edx = (unsigned long) &frame->info; |
| 476 | regs->ecx = (unsigned long) &frame->uc; |
| 477 | |
| 478 | set_fs(USER_DS); |
| 479 | regs->xds = __USER_DS; |
| 480 | regs->xes = __USER_DS; |
| 481 | regs->xss = __USER_DS; |
| 482 | regs->xcs = __USER_CS; |
| 483 | |
| 484 | /* |
| 485 | * Clear TF when entering the signal handler, but |
| 486 | * notify any tracer that was single-stepping it. |
| 487 | * The tracer may want to single-step inside the |
| 488 | * handler too. |
| 489 | */ |
| 490 | regs->eflags &= ~TF_MASK; |
| 491 | if (test_thread_flag(TIF_SINGLESTEP)) |
| 492 | ptrace_notify(SIGTRAP); |
| 493 | |
| 494 | #if DEBUG_SIG |
| 495 | printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n", |
| 496 | current->comm, current->pid, frame, regs->eip, frame->pretcode); |
| 497 | #endif |
| 498 | |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 499 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | give_sigsegv: |
| 502 | force_sigsegv(sig, current); |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 503 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
| 507 | * OK, we're invoking a handler |
| 508 | */ |
| 509 | |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 510 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, |
| 512 | sigset_t *oldset, struct pt_regs * regs) |
| 513 | { |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 514 | int ret; |
| 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* Are we from a system call? */ |
| 517 | if (regs->orig_eax >= 0) { |
| 518 | /* If so, check system call restarting.. */ |
| 519 | switch (regs->eax) { |
| 520 | case -ERESTART_RESTARTBLOCK: |
| 521 | case -ERESTARTNOHAND: |
| 522 | regs->eax = -EINTR; |
| 523 | break; |
| 524 | |
| 525 | case -ERESTARTSYS: |
| 526 | if (!(ka->sa.sa_flags & SA_RESTART)) { |
| 527 | regs->eax = -EINTR; |
| 528 | break; |
| 529 | } |
| 530 | /* fallthrough */ |
| 531 | case -ERESTARTNOINTR: |
| 532 | regs->eax = regs->orig_eax; |
| 533 | regs->eip -= 2; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * If TF is set due to a debugger (PT_DTRACE), clear the TF flag so |
| 539 | * that register information in the sigcontext is correct. |
| 540 | */ |
| 541 | if (unlikely(regs->eflags & TF_MASK) |
| 542 | && likely(current->ptrace & PT_DTRACE)) { |
| 543 | current->ptrace &= ~PT_DTRACE; |
| 544 | regs->eflags &= ~TF_MASK; |
| 545 | } |
| 546 | |
| 547 | /* Set up the stack frame */ |
| 548 | if (ka->sa.sa_flags & SA_SIGINFO) |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 549 | ret = setup_rt_frame(sig, ka, info, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | else |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 551 | ret = setup_frame(sig, ka, oldset, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 553 | if (ret == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | spin_lock_irq(¤t->sighand->siglock); |
| 555 | sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask); |
Steven Rostedt | 69be8f1 | 2005-08-29 11:44:09 -0400 | [diff] [blame] | 556 | if (!(ka->sa.sa_flags & SA_NODEFER)) |
| 557 | sigaddset(¤t->blocked,sig); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | recalc_sigpending(); |
| 559 | spin_unlock_irq(¤t->sighand->siglock); |
| 560 | } |
Roland McGrath | 7c1def1 | 2005-06-23 00:08:21 -0700 | [diff] [blame] | 561 | |
| 562 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 567 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 568 | * mistake. |
| 569 | */ |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 570 | static void fastcall do_signal(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
| 572 | siginfo_t info; |
| 573 | int signr; |
| 574 | struct k_sigaction ka; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 575 | sigset_t *oldset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | /* |
| 578 | * We want the common case to go fast, which |
| 579 | * is why we may in certain cases get here from |
| 580 | * kernel mode. Just return without doing anything |
Zachary Amsden | 0998e42 | 2005-09-03 15:56:43 -0700 | [diff] [blame] | 581 | * if so. vm86 regs switched out by assembly code |
| 582 | * before reaching here, so testing against kernel |
| 583 | * CS suffices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | */ |
Vincent Hanquez | 717b594 | 2005-06-23 00:08:45 -0700 | [diff] [blame] | 585 | if (!user_mode(regs)) |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 586 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 588 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) |
| 589 | oldset = ¤t->saved_sigmask; |
| 590 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | oldset = ¤t->blocked; |
| 592 | |
| 593 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
| 594 | if (signr > 0) { |
| 595 | /* Reenable any watchpoints before delivering the |
| 596 | * signal to user space. The processor register will |
| 597 | * have been cleared if the watchpoint triggered |
| 598 | * inside the kernel. |
| 599 | */ |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 600 | if (unlikely(current->thread.debugreg[7])) |
Vincent Hanquez | 1cc6f12 | 2005-06-23 00:08:43 -0700 | [diff] [blame] | 601 | set_debugreg(current->thread.debugreg[7], 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
| 603 | /* Whee! Actually deliver the signal. */ |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 604 | if (handle_signal(signr, &info, &ka, oldset, regs) == 0) { |
| 605 | /* a signal was successfully delivered; the saved |
| 606 | * sigmask will have been stored in the signal frame, |
| 607 | * and will be restored by sigreturn, so we can simply |
| 608 | * clear the TIF_RESTORE_SIGMASK flag */ |
| 609 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) |
| 610 | clear_thread_flag(TIF_RESTORE_SIGMASK); |
| 611 | } |
| 612 | |
| 613 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | /* Did we come from a system call? */ |
| 617 | if (regs->orig_eax >= 0) { |
| 618 | /* Restart the system call - no handlers present */ |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 619 | switch (regs->eax) { |
| 620 | case -ERESTARTNOHAND: |
| 621 | case -ERESTARTSYS: |
| 622 | case -ERESTARTNOINTR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | regs->eax = regs->orig_eax; |
| 624 | regs->eip -= 2; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 625 | break; |
| 626 | |
| 627 | case -ERESTART_RESTARTBLOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | regs->eax = __NR_restart_syscall; |
| 629 | regs->eip -= 2; |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 630 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 633 | |
| 634 | /* if there's no signal to deliver, we just put the saved sigmask |
| 635 | * back */ |
| 636 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) { |
| 637 | clear_thread_flag(TIF_RESTORE_SIGMASK); |
| 638 | sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); |
| 639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* |
| 643 | * notification of userspace execution resumption |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 644 | * - triggered by the TIF_WORK_MASK flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | */ |
| 646 | __attribute__((regparm(3))) |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 647 | void do_notify_resume(struct pt_regs *regs, void *_unused, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | __u32 thread_info_flags) |
| 649 | { |
| 650 | /* Pending single-step? */ |
| 651 | if (thread_info_flags & _TIF_SINGLESTEP) { |
| 652 | regs->eflags |= TF_MASK; |
| 653 | clear_thread_flag(TIF_SINGLESTEP); |
| 654 | } |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | /* deal with pending signal delivery */ |
David Howells | 283828f | 2006-01-18 17:44:00 -0800 | [diff] [blame] | 657 | if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK)) |
| 658 | do_signal(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
| 660 | clear_thread_flag(TIF_IRET); |
| 661 | } |