7cebb6832c107b396a11cbaaec58a4ca7536c5cb
[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
340 static void setup_frame(int sig, struct k_sigaction *ka,
341                         sigset_t *set, struct pt_regs * regs)
342 {
343         void __user *restorer;
344         struct sigframe __user *frame;
345         int err = 0;
346
347         frame = get_sigframe(ka, regs, sizeof(*frame));
348
349         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
350                 goto give_sigsegv;
351
352         err |= __put_user((current_thread_info()->exec_domain
353                            && current_thread_info()->exec_domain->signal_invmap
354                            && sig < 32
355                            ? current_thread_info()->exec_domain->signal_invmap[sig]
356                            : sig),
357                           &frame->sig);
358         if (err)
359                 goto give_sigsegv;
360
361         err |= setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
362         if (err)
363                 goto give_sigsegv;
364
365         if (_NSIG_WORDS > 1) {
366                 err |= __copy_to_user(&frame->extramask, &set->sig[1],
367                                       sizeof(frame->extramask));
368         }
369         if (err)
370                 goto give_sigsegv;
371
372         restorer = current->mm->context.vdso + (long)&__kernel_sigreturn;
373         if (ka->sa.sa_flags & SA_RESTORER)
374                 restorer = ka->sa.sa_restorer;
375
376         /* Set up to return from userspace.  */
377         err |= __put_user(restorer, &frame->pretcode);
378          
379         /*
380          * This is popl %eax ; movl $,%eax ; int $0x80
381          *
382          * WE DO NOT USE IT ANY MORE! It's only left here for historical
383          * reasons and because gdb uses it as a signature to notice
384          * signal handler stack frames.
385          */
386         err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
387         err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
388         err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
389
390         if (err)
391                 goto give_sigsegv;
392
393         /* Set up registers for signal handler */
394         regs->esp = (unsigned long) frame;
395         regs->eip = (unsigned long) ka->sa.sa_handler;
396
397         set_fs(USER_DS);
398         regs->xds = __USER_DS;
399         regs->xes = __USER_DS;
400         regs->xss = __USER_DS;
401         regs->xcs = __USER_CS;
402         regs->eflags &= ~TF_MASK;
403
404 #if DEBUG_SIG
405         printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
406                 current->comm, current->pid, frame, regs->eip, frame->pretcode);
407 #endif
408
409         return;
410
411 give_sigsegv:
412         if (sig == SIGSEGV)
413                 ka->sa.sa_handler = SIG_DFL;
414         force_sig(SIGSEGV, current);
415 }
416
417 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
418                            sigset_t *set, struct pt_regs * regs)
419 {
420         void __user *restorer;
421         struct rt_sigframe __user *frame;
422         int err = 0;
423
424         frame = get_sigframe(ka, regs, sizeof(*frame));
425
426         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
427                 goto give_sigsegv;
428
429         err |= __put_user((current_thread_info()->exec_domain
430                            && current_thread_info()->exec_domain->signal_invmap
431                            && sig < 32
432                            ? current_thread_info()->exec_domain->signal_invmap[sig]
433                            : sig),
434                           &frame->sig);
435         err |= __put_user(&frame->info, &frame->pinfo);
436         err |= __put_user(&frame->uc, &frame->puc);
437         err |= copy_siginfo_to_user(&frame->info, info);
438         if (err)
439                 goto give_sigsegv;
440
441         /* Create the ucontext.  */
442         err |= __put_user(0, &frame->uc.uc_flags);
443         err |= __put_user(0, &frame->uc.uc_link);
444         err |= __put_user(current->sas_ss_sp, (unsigned long *)&frame->uc.uc_stack.ss_sp);
445         err |= __put_user(sas_ss_flags(regs->esp),
446                           &frame->uc.uc_stack.ss_flags);
447         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
448         err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
449                                 regs, set->sig[0]);
450         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
451         if (err)
452                 goto give_sigsegv;
453
454         /* Set up to return from userspace.  */
455         restorer = current->mm->context.vdso + (long)&__kernel_rt_sigreturn;
456         if (ka->sa.sa_flags & SA_RESTORER)
457                 restorer = ka->sa.sa_restorer;
458
459         err |= __put_user(restorer, &frame->pretcode);
460          
461         /*
462          * This is movl $,%eax ; int $0x80
463          *
464          * WE DO NOT USE IT ANY MORE! It's only left here for historical
465          * reasons and because gdb uses it as a signature to notice
466          * signal handler stack frames.
467          */
468         err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
469         err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
470         err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
471
472         if (err)
473                 goto give_sigsegv;
474
475         /* Set up registers for signal handler */
476         regs->esp = (unsigned long) frame;
477         regs->eip = (unsigned long) ka->sa.sa_handler;
478
479         set_fs(USER_DS);
480         regs->xds = __USER_DS;
481         regs->xes = __USER_DS;
482         regs->xss = __USER_DS;
483         regs->xcs = __USER_CS;
484         regs->eflags &= ~TF_MASK;
485
486 #if DEBUG_SIG
487         printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
488                 current->comm, current->pid, frame, regs->eip, frame->pretcode);
489 #endif
490
491         return;
492
493 give_sigsegv:
494         if (sig == SIGSEGV)
495                 ka->sa.sa_handler = SIG_DFL;
496         force_sig(SIGSEGV, current);
497 }
498
499 /*
500  * OK, we're invoking a handler
501  */     
502
503 static void
504 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
505         struct pt_regs * regs)
506 {
507         struct k_sigaction *ka = &current->sighand->action[sig-1];
508
509         /* Are we from a system call? */
510         if (regs->orig_eax >= 0) {
511                 /* If so, check system call restarting.. */
512                 switch (regs->eax) {
513                         case -ERESTART_RESTARTBLOCK:
514                         case -ERESTARTNOHAND:
515                                 regs->eax = -EINTR;
516                                 break;
517
518                         case -ERESTARTSYS:
519                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
520                                         regs->eax = -EINTR;
521                                         break;
522                                 }
523                         /* fallthrough */
524                         case -ERESTARTNOINTR:
525                                 regs->eax = regs->orig_eax;
526                                 regs->eip -= 2;
527                 }
528         }
529
530         /* Set up the stack frame */
531         if (ka->sa.sa_flags & SA_SIGINFO)
532                 setup_rt_frame(sig, ka, info, oldset, regs);
533         else
534                 setup_frame(sig, ka, oldset, regs);
535
536         if (ka->sa.sa_flags & SA_ONESHOT)
537                 ka->sa.sa_handler = SIG_DFL;
538
539         if (!(ka->sa.sa_flags & SA_NODEFER)) {
540                 spin_lock_irq(&current->sighand->siglock);
541                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
542                 sigaddset(&current->blocked,sig);
543                 recalc_sigpending();
544                 spin_unlock_irq(&current->sighand->siglock);
545         }
546 }
547
548 /*
549  * Note that 'init' is a special process: it doesn't get signals it doesn't
550  * want to handle. Thus you cannot kill init even with a SIGKILL even by
551  * mistake.
552  */
553 int fastcall do_signal(struct pt_regs *regs, sigset_t *oldset)
554 {
555         siginfo_t info;
556         int signr;
557
558         /*
559          * We want the common case to go fast, which
560          * is why we may in certain cases get here from
561          * kernel mode. Just return without doing anything
562          * if so.
563          */
564         if ((regs->xcs & 3) != 3)
565                 return 1;
566
567         if (current->flags & PF_FREEZE) {
568                 refrigerator(0);
569                 goto no_signal;
570         }
571
572         if (!oldset)
573                 oldset = &current->blocked;
574
575         signr = get_signal_to_deliver(&info, regs, NULL);
576         if (signr > 0) {
577                 /* Reenable any watchpoints before delivering the
578                  * signal to user space. The processor register will
579                  * have been cleared if the watchpoint triggered
580                  * inside the kernel.
581                  */
582                 __asm__("movl %0,%%db7" : : "r" (current->thread.debugreg[7]));
583
584                 /* Whee!  Actually deliver the signal.  */
585                 handle_signal(signr, &info, oldset, regs);
586                 return 1;
587         }
588
589  no_signal:
590         /* Did we come from a system call? */
591         if (regs->orig_eax >= 0) {
592                 /* Restart the system call - no handlers present */
593                 if (regs->eax == -ERESTARTNOHAND ||
594                     regs->eax == -ERESTARTSYS ||
595                     regs->eax == -ERESTARTNOINTR) {
596                         regs->eax = regs->orig_eax;
597                         regs->eip -= 2;
598                 }
599                 if (regs->eax == -ERESTART_RESTARTBLOCK){
600                         regs->eax = __NR_restart_syscall;
601                         regs->eip -= 2;
602                 }
603         }
604         return 0;
605 }
606
607 /*
608  * notification of userspace execution resumption
609  * - triggered by current->work.notify_resume
610  */
611 __attribute__((regparm(3)))
612 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
613                       __u32 thread_info_flags)
614 {
615         /* Pending single-step? */
616         if (thread_info_flags & _TIF_SINGLESTEP) {
617                 regs->eflags |= TF_MASK;
618                 clear_thread_flag(TIF_SINGLESTEP);
619         }
620         /* deal with pending signal delivery */
621         if (thread_info_flags & _TIF_SIGPENDING)
622                 do_signal(regs,oldset);
623         
624         clear_thread_flag(TIF_IRET);
625 }