added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / arch / x86_64 / kernel / signal.c
1 /*
2  *  linux/arch/x86_64/kernel/signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 2000, 2001  SuSE Labs
6  *
7  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
8  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
9  *  2000-2001   x86-64 support by Andi Kleen
10  * 
11  *  $Id: signal.c,v 1.1.1.1 2005/04/11 02:50:14 jack Exp $
12  */
13
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/kernel.h>
19 #include <linux/signal.h>
20 #include <linux/errno.h>
21 #include <linux/wait.h>
22 #include <linux/ptrace.h>
23 #include <linux/unistd.h>
24 #include <linux/stddef.h>
25 #include <linux/tty.h>
26 #include <linux/personality.h>
27 #include <linux/compiler.h>
28 #include <linux/binfmts.h>
29 #include <asm/ucontext.h>
30 #include <asm/uaccess.h>
31 #include <asm/i387.h>
32 #include <asm/proto.h>
33
34 #define DEBUG_SIG 0
35
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37
38 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
39
40 void ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
41                sigset_t *set, struct pt_regs * regs); 
42 void ia32_setup_frame(int sig, struct k_sigaction *ka,
43             sigset_t *set, struct pt_regs * regs); 
44 void signal_fault(struct pt_regs *regs, void *frame, char *where);
45 extern int exception_trace;
46
47 int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
48 {
49         if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
50                 return -EFAULT;
51         if (from->si_code < 0)
52                 return __copy_to_user(to, from, sizeof(siginfo_t));
53         else {
54                 int err;
55
56                 /* If you change siginfo_t structure, please be sure
57                    this code is fixed accordingly.
58                    It should never copy any pad contained in the structure
59                    to avoid security leaks, but must copy the generic
60                    3 ints plus the relevant union member.  */
61                 err = __put_user(from->si_signo, &to->si_signo);
62                 err |= __put_user(from->si_errno, &to->si_errno);
63                 err |= __put_user((short)from->si_code, &to->si_code);
64                 /* First 32bits of unions are always present.  */
65                 err |= __put_user(from->si_pid, &to->si_pid);
66                 switch (from->si_code >> 16) {
67                 case __SI_CHLD >> 16:
68                         err |= __put_user(from->si_utime, &to->si_utime);
69                         err |= __put_user(from->si_stime, &to->si_stime);
70                         err |= __put_user(from->si_status, &to->si_status);
71                 default:
72                         err |= __put_user(from->si_uid, &to->si_uid);
73                         break;
74                 /* case __SI_RT: This is not generated by the kernel as of now.  */
75                 }
76                 return err;
77         }
78 }
79
80 asmlinkage long
81 sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs regs)
82 {
83         sigset_t saveset, newset;
84
85         /* XXX: Don't preclude handling different sized sigset_t's.  */
86         if (sigsetsize != sizeof(sigset_t))
87                 return -EINVAL;
88
89         if (copy_from_user(&newset, unewset, sizeof(newset)))
90                 return -EFAULT;
91         sigdelsetmask(&newset, ~_BLOCKABLE);
92
93         spin_lock_irq(&current->sigmask_lock);
94         saveset = current->blocked;
95         current->blocked = newset;
96         recalc_sigpending(current);
97         spin_unlock_irq(&current->sigmask_lock);
98 #if DEBUG_SIG
99         printk("rt_sigsuspend savset(%lx) newset(%lx) regs(%p) rip(%lx)\n",
100                 saveset, newset, &regs, regs.rip);
101 #endif 
102         regs.rax = -EINTR;
103         while (1) {
104                 current->state = TASK_INTERRUPTIBLE;
105                 schedule();
106                 if (do_signal(&regs, &saveset))
107                         return -EINTR;
108         }
109 }
110
111 asmlinkage long
112 sys_sigaltstack(const stack_t *uss, stack_t *uoss, struct pt_regs regs)
113 {
114         return do_sigaltstack(uss, uoss, regs.rsp);
115 }
116
117
118 /*
119  * Do a signal return; undo the signal stack.
120  */
121
122 struct rt_sigframe
123 {
124         char *pretcode;
125         struct ucontext uc;
126         struct siginfo info;
127 };
128
129 static int
130 restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc, unsigned long *prax)
131 {
132         unsigned int err = 0;
133
134
135 #define COPY(x)         err |= __get_user(regs->x, &sc->x)
136 #define COPY_CANON(x)   \
137         COPY(x); \
138         if ((regs->x >> 48)  != 0 && (regs->x >> 48) != 0xffff) \
139                                 regs->x = 0; 
140
141         { 
142                 unsigned int seg, oldseg; 
143                 err |= __get_user(seg, &sc->gs); 
144                 asm("movl %%gs,%0" : "=r" (oldseg)); 
145                 if (oldseg != seg) {
146                         seg |= 3;                      
147                 load_gs_index(seg); 
148                 }
149                 err |= __get_user(seg, &sc->fs);
150                 seg |= 3; 
151                 loadsegment(fs,seg);
152         }
153
154         COPY(rdi); COPY(rsi); COPY(rbp); COPY_CANON(rsp); COPY(rbx);
155         COPY(rdx); COPY(rcx); COPY_CANON(rip);
156         COPY(r8);
157         COPY(r9);
158         COPY(r10);
159         COPY(r11);
160         COPY(r12);
161         COPY(r13);
162         COPY(r14);
163         COPY(r15);
164
165         /* do not copy CS/SS because 64bit should not need it. 
166            also need IRET exception handling anyways. */
167
168         {
169                 unsigned int tmpflags;
170                 err |= __get_user(tmpflags, &sc->eflags);
171                 regs->eflags = (regs->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
172                 regs->orig_rax = -1;            /* disable syscall checks */
173         }
174
175         {
176                 struct _fpstate * buf;
177                 err |= __get_user(buf, &sc->fpstate);
178                 if (buf) {
179                         if (unlikely(verify_area(VERIFY_READ, buf, sizeof(*buf))))
180                                 return 1;
181                         err |= restore_i387(buf);
182                 }
183         }
184
185         err |= __get_user(*prax, &sc->rax);
186         return err;
187 }
188 #undef COPY
189
190 asmlinkage long sys_rt_sigreturn(struct pt_regs regs)
191 {
192         struct rt_sigframe *frame = (struct rt_sigframe *)(regs.rsp - 8);
193         sigset_t set;
194         stack_t st;
195         long eax;
196
197         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
198                 goto badframe;
199         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
200                 goto badframe;
201
202         sigdelsetmask(&set, ~_BLOCKABLE);
203         spin_lock_irq(&current->sigmask_lock);
204         current->blocked = set;
205         recalc_sigpending(current);
206         spin_unlock_irq(&current->sigmask_lock);
207         
208         if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &eax))
209                 goto badframe;
210
211 #if DEBUG_SIG
212         printk("%d sigreturn rip:%lx rsp:%lx frame:%p rax:%lx\n",current->pid,regs.rip,regs.rsp,frame,eax);
213 #endif
214
215         if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
216                 goto badframe;
217         /* It is more difficult to avoid calling this function than to
218            call it and ignore errors.  */
219         do_sigaltstack(&st, NULL, regs.rsp);
220
221         return eax;
222
223 badframe:
224         signal_fault(&regs, frame, "rt_sigreturn"); 
225         return 0;
226 }       
227
228 /*
229  * Set up a signal frame.
230  */
231
232 static int
233 setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, unsigned long mask)
234 {
235         int tmp, err = 0;
236         struct task_struct *me = current;
237
238         tmp = 0;
239         __asm__("movl %%gs,%0" : "=r"(tmp): "0"(tmp));
240         err |= __put_user(tmp, (unsigned int *)&sc->gs);
241         __asm__("movl %%fs,%0" : "=r"(tmp): "0"(tmp));
242         err |= __put_user(tmp, (unsigned int *)&sc->fs);
243
244         err |= __put_user(regs->rdi, &sc->rdi);
245         err |= __put_user(regs->rsi, &sc->rsi);
246         err |= __put_user(regs->rbp, &sc->rbp);
247         err |= __put_user(regs->rsp, &sc->rsp);
248         err |= __put_user(regs->rbx, &sc->rbx);
249         err |= __put_user(regs->rdx, &sc->rdx);
250         err |= __put_user(regs->rcx, &sc->rcx);
251         err |= __put_user(regs->rax, &sc->rax);
252         err |= __put_user(regs->r8, &sc->r8);
253         err |= __put_user(regs->r9, &sc->r9);
254         err |= __put_user(regs->r10, &sc->r10);
255         err |= __put_user(regs->r11, &sc->r11);
256         err |= __put_user(regs->r12, &sc->r12);
257         err |= __put_user(regs->r13, &sc->r13);
258         err |= __put_user(regs->r14, &sc->r14);
259         err |= __put_user(regs->r15, &sc->r15);
260         err |= __put_user(me->thread.trap_no, &sc->trapno);
261         err |= __put_user(me->thread.error_code, &sc->err);
262         err |= __put_user(regs->rip, &sc->rip);
263         err |= __put_user(regs->cs, &sc->cs);
264         err |= __put_user(regs->eflags, &sc->eflags);
265         err |= __put_user(mask, &sc->oldmask);
266         err |= __put_user(me->thread.cr2, &sc->cr2);
267
268         return err;
269 }
270
271 static void * 
272 get_stack(struct k_sigaction *ka, struct pt_regs *regs, unsigned long size)
273 {
274         unsigned long rsp;
275
276         /* Default to using normal stack - redzone*/
277         rsp = regs->rsp - 128;
278
279         /* This is the X/Open sanctioned signal stack switching.  */
280         /* may need to subtract redzone there too */
281         if (ka->sa.sa_flags & SA_ONSTACK) {
282                 if (sas_ss_flags(rsp) == 0)
283                         rsp = current->sas_ss_sp + current->sas_ss_size;
284         }
285
286         return (void *)round_down(rsp - size, 16);      
287 }
288
289 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
290                            sigset_t *set, struct pt_regs * regs)
291 {
292         struct rt_sigframe *frame;
293         struct _fpstate *fp; 
294         int err = 0;
295
296         if (current->used_math) {
297                 fp = get_stack(ka, regs, sizeof(struct _fpstate)); 
298                 frame = (void *)round_down((unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
299
300                 if (!access_ok(VERIFY_WRITE, fp, sizeof(struct _fpstate))) { 
301                         goto give_sigsegv;
302                 }
303                 if (save_i387(fp) < 0) 
304                         err |= -1; 
305         } else {
306                 frame = get_stack(ka, regs, sizeof(struct rt_sigframe)) - 8;
307                 fp = NULL;
308         }
309
310         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) {
311                 goto give_sigsegv;
312         }
313
314
315         if (ka->sa.sa_flags & SA_SIGINFO) { 
316                 err |= copy_siginfo_to_user(&frame->info, info);
317                 if (err)
318                         goto give_sigsegv;
319         }
320                 
321         /* Create the ucontext.  */
322         err |= __put_user(0, &frame->uc.uc_flags);
323         err |= __put_user(0, &frame->uc.uc_link);
324         err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
325         err |= __put_user(sas_ss_flags(regs->rsp),
326                           &frame->uc.uc_stack.ss_flags);
327         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
328         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
329         err |= __put_user(fp, &frame->uc.uc_mcontext.fpstate);
330         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
331
332         /* Set up to return from userspace.  If provided, use a stub
333            already in userspace.  */
334         /* x86-64 should always use SA_RESTORER. */
335         if (ka->sa.sa_flags & SA_RESTORER) {
336                 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
337         } else {
338                 printk("%s forgot to set SA_RESTORER for signal %d.\n", current->comm, sig); 
339                 goto give_sigsegv; 
340         }
341
342         if (err)
343                 goto give_sigsegv;
344
345 #if DEBUG_SIG
346         printk("%d old rip %lx old rsp %lx old rax %lx\n", current->pid,regs->rip,regs->rsp,regs->rax);
347 #endif
348
349         /* Set up registers for signal handler */
350         { 
351                 struct exec_domain *ed = current->exec_domain;
352                 if (unlikely(ed && ed->signal_invmap && sig < 32))
353                         sig = ed->signal_invmap[sig];
354         } 
355         regs->rdi = sig;
356
357         /* could reload DS/ES to __USER_DS here, but assume for now
358            that 64bit does not care */
359
360         /* In case the signal handler was declared without prototypes */ 
361         regs->rax = 0;
362
363         /* This also works for non SA_SIGINFO handlers because they expect the
364            next argument after the signal number on the stack. */
365         regs->rsi = (unsigned long)&frame->info; 
366         regs->rdx = (unsigned long)&frame->uc; 
367         regs->rsp = (unsigned long) frame;
368         regs->rip = (unsigned long) ka->sa.sa_handler;
369         regs->cs = __USER_CS;
370         regs->ss = __USER_DS; 
371
372         set_fs(USER_DS);
373         regs->eflags &= ~TF_MASK;
374
375 #if DEBUG_SIG
376         printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
377                 current->comm, current->pid, frame, regs->rip, frame->pretcode);
378 #endif
379
380         return;
381
382 give_sigsegv:
383         if (sig == SIGSEGV)
384                 ka->sa.sa_handler = SIG_DFL;
385         signal_fault(regs, frame, "signal deliver"); 
386 }
387
388 /*
389  * OK, we're invoking a handler
390  */     
391
392 static void
393 handle_signal(unsigned long sig, struct k_sigaction *ka,
394               siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
395 {
396 #if DEBUG_SIG
397         printk("handle_signal pid:%d sig:%lu rip:%lx rsp:%lx regs=%p\n", current->pid, sig, 
398                 regs->rip, regs->rsp, regs);
399 #endif
400
401         /* Are we from a system call? */
402         if ((long)regs->orig_rax >= 0) {
403                 /* If so, check system call restarting.. */
404                 switch (regs->rax) {
405                         case -ERESTARTNOHAND:
406                                 regs->rax = -EINTR;
407                                 break;
408
409                         case -ERESTARTSYS:
410                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
411                                         regs->rax = -EINTR;
412                                         break;
413                                 }
414                         /* fallthrough */
415                         case -ERESTARTNOINTR:
416                                 regs->rax = regs->orig_rax;
417                                 regs->rip -= 2;
418                                 break; 
419                 }
420         }
421
422 #ifdef CONFIG_IA32_EMULATION
423         if (current->thread.flags & THREAD_IA32) { 
424                 if (ka->sa.sa_flags & SA_SIGINFO)
425                         ia32_setup_rt_frame(sig, ka, info, oldset, regs);
426                 else
427                         ia32_setup_frame(sig, ka, oldset, regs);
428         } else 
429 #endif
430         setup_rt_frame(sig, ka, info, oldset, regs);
431
432         if (ka->sa.sa_flags & SA_ONESHOT)
433                 ka->sa.sa_handler = SIG_DFL;
434
435         if (!(ka->sa.sa_flags & SA_NODEFER)) {
436                 spin_lock_irq(&current->sigmask_lock);
437                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
438                 sigaddset(&current->blocked,sig);
439                 recalc_sigpending(current);
440                 spin_unlock_irq(&current->sigmask_lock);
441         }
442 }
443
444 /*
445  * Note that 'init' is a special process: it doesn't get signals it doesn't
446  * want to handle. Thus you cannot kill init even with a SIGKILL even by
447  * mistake.
448  */
449 int do_signal(struct pt_regs *regs, sigset_t *oldset)
450 {
451         siginfo_t info;
452         struct k_sigaction *ka;
453
454         /*
455          * We want the common case to go fast, which
456          * is why we may in certain cases get here from
457          * kernel mode. Just return without doing anything
458          * if so.
459          */
460         if ((regs->cs & 3) != 3) {
461                 return 1;
462         }       
463
464         if (!oldset)
465                 oldset = &current->blocked;
466
467         for (;;) {
468                 unsigned long signr;
469
470                 spin_lock_irq(&current->sigmask_lock);
471                 signr = dequeue_signal(&current->blocked, &info);
472                 spin_unlock_irq(&current->sigmask_lock);
473
474                 if (!signr) { 
475                         break;
476                 }
477
478                 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
479                         /* Let the debugger run.  */
480                         current->exit_code = signr;
481                         current->state = TASK_STOPPED;
482                         notify_parent(current, SIGCHLD);
483                         schedule();
484
485                         /* We're back.  Did the debugger cancel the sig?  */
486                         if (!(signr = current->exit_code))
487                                 continue;
488                         current->exit_code = 0;
489
490                         /* The debugger continued.  Ignore SIGSTOP.  */
491                         if (signr == SIGSTOP)
492                                 continue;
493
494                         /* Update the siginfo structure.  Is this good?  */
495                         if (signr != info.si_signo) {
496                                 info.si_signo = signr;
497                                 info.si_errno = 0;
498                                 info.si_code = SI_USER;
499                                 info.si_pid = current->p_pptr->pid;
500                                 info.si_uid = current->p_pptr->uid;
501                         }
502
503                         /* If the (new) signal is now blocked, requeue it.  */
504                         if (sigismember(&current->blocked, signr)) {
505                                 send_sig_info(signr, &info, current);
506                                 continue;
507                         }
508                 }
509
510                 ka = &current->sig->action[signr-1];
511                 if (ka->sa.sa_handler == SIG_IGN) {
512                         if (signr != SIGCHLD)
513                                 continue;
514                         /* Check for SIGCHLD: it's special.  */
515                         while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
516                                 /* nothing */;
517                         continue;
518                 }
519
520                 if (ka->sa.sa_handler == SIG_DFL) {
521                         int exit_code = signr;
522
523                         /* Init gets no signals it doesn't want.  */
524                         if (current->pid == 1)                        
525                                 continue;
526
527                         switch (signr) {
528                         case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
529                                 continue;
530
531                         case SIGTSTP: case SIGTTIN: case SIGTTOU:
532                                 if (is_orphaned_pgrp(current->pgrp))
533                                         continue;
534                                 /* FALLTHRU */
535
536                         case SIGSTOP: {
537                                 struct signal_struct *sig;
538                                 current->state = TASK_STOPPED;
539                                 current->exit_code = signr;
540                                 sig = current->p_pptr->sig;
541                                 if (sig && !(sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
542                                         notify_parent(current, SIGCHLD);
543                                 schedule();
544                                 continue;
545                         }
546
547                         case SIGQUIT: case SIGILL: case SIGTRAP:
548                         case SIGABRT: case SIGFPE: case SIGSEGV:
549                         case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
550                                 if (do_coredump(signr, regs))
551                                         exit_code |= 0x80;
552                                 /* FALLTHRU */
553
554                         default:
555                                 sig_exit(signr, exit_code, &info);
556                                 /* NOTREACHED */
557                         }
558                 }
559
560                 /* Reenable any watchpoints before delivering the
561                  * signal to user space. The processor register will
562                  * have been cleared if the watchpoint triggered
563                  * inside the kernel.
564                  */
565                 asm volatile("movq %0,%%db7"::"r"(current->thread.debugreg[7]));
566                 /* Whee!  Actually deliver the signal.  */
567                 handle_signal(signr, ka, &info, oldset, regs);
568                 return 1;
569         }
570
571         /* Did we come from a system call? */
572         if (regs->orig_rax >= 0) {
573                 /* Restart the system call - no handlers present */
574                 if (regs->rax == -ERESTARTNOHAND ||
575                     regs->rax == -ERESTARTSYS ||
576                     regs->rax == -ERESTARTNOINTR) {
577                         regs->rax = regs->orig_rax;
578                         regs->rip -= 2;
579                 }
580         }
581         return 0;
582 }
583
584
585 void signal_fault(struct pt_regs *regs, void *frame, char *where)
586
587         struct task_struct *me = current; 
588         if (exception_trace)
589                 printk("%s[%d] bad frame in %s frame:%p rip:%lx rsp:%lx orax:%lx\n",
590                me->comm,me->pid,where,frame,regs->rip,regs->rsp,regs->orig_rax); 
591
592         force_sig(SIGSEGV, me); 
593