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