Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[pandora-kernel.git] / arch / m32r / kernel / signal.c
1 /*
2  *  linux/arch/m32r/kernel/signal.c
3  *
4  *  Copyright (c) 2003  Hitoshi Yamamoto
5  *
6  *  Taken from i386 version.
7  *  Copyright (C) 1991, 1992  Linus Torvalds
8  *
9  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
10  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
11  */
12
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/personality.h>
23 #include <linux/freezer.h>
24 #include <asm/cacheflush.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27
28 #define DEBUG_SIG 0
29
30 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
31
32 int do_signal(struct pt_regs *, sigset_t *);
33
34 asmlinkage int
35 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize,
36                   unsigned long r2, unsigned long r3, unsigned long r4,
37                   unsigned long r5, unsigned long r6, struct pt_regs *regs)
38 {
39         sigset_t saveset, newset;
40
41         /* XXX: Don't preclude handling different sized sigset_t's.  */
42         if (sigsetsize != sizeof(sigset_t))
43                 return -EINVAL;
44
45         if (copy_from_user(&newset, unewset, sizeof(newset)))
46                 return -EFAULT;
47         sigdelsetmask(&newset, ~_BLOCKABLE);
48
49         spin_lock_irq(&current->sighand->siglock);
50         saveset = current->blocked;
51         current->blocked = newset;
52         recalc_sigpending();
53         spin_unlock_irq(&current->sighand->siglock);
54
55         regs->r0 = -EINTR;
56         while (1) {
57                 current->state = TASK_INTERRUPTIBLE;
58                 schedule();
59                 if (do_signal(regs, &saveset))
60                         return regs->r0;
61         }
62 }
63
64 asmlinkage int
65 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
66                 unsigned long r2, unsigned long r3, unsigned long r4,
67                 unsigned long r5, unsigned long r6, struct pt_regs *regs)
68 {
69         return do_sigaltstack(uss, uoss, regs->spu);
70 }
71
72
73 /*
74  * Do a signal return; undo the signal stack.
75  */
76
77 struct rt_sigframe
78 {
79         int sig;
80         struct siginfo __user *pinfo;
81         void __user *puc;
82         struct siginfo info;
83         struct ucontext uc;
84 //      struct _fpstate fpstate;
85 };
86
87 static int
88 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
89                    int *r0_p)
90 {
91         unsigned int err = 0;
92
93         /* Always make any pending restarted system calls return -EINTR */
94         current_thread_info()->restart_block.fn = do_no_restart_syscall;
95
96 #define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
97         COPY(r4);
98         COPY(r5);
99         COPY(r6);
100         COPY(pt_regs);
101         /* COPY(r0); Skip r0 */
102         COPY(r1);
103         COPY(r2);
104         COPY(r3);
105         COPY(r7);
106         COPY(r8);
107         COPY(r9);
108         COPY(r10);
109         COPY(r11);
110         COPY(r12);
111         COPY(acc0h);
112         COPY(acc0l);
113         COPY(acc1h);            /* ISA_DSP_LEVEL2 only */
114         COPY(acc1l);            /* ISA_DSP_LEVEL2 only */
115         COPY(psw);
116         COPY(bpc);
117         COPY(bbpsw);
118         COPY(bbpc);
119         COPY(spu);
120         COPY(fp);
121         COPY(lr);
122         COPY(spi);
123 #undef COPY
124
125         regs->syscall_nr = -1;  /* disable syscall checks */
126         err |= __get_user(*r0_p, &sc->sc_r0);
127
128         return err;
129 }
130
131 asmlinkage int
132 sys_rt_sigreturn(unsigned long r0, unsigned long r1,
133                  unsigned long r2, unsigned long r3, unsigned long r4,
134                  unsigned long r5, unsigned long r6, struct pt_regs *regs)
135 {
136         struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
137         sigset_t set;
138         int result;
139
140         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
141                 goto badframe;
142         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
143                 goto badframe;
144
145         sigdelsetmask(&set, ~_BLOCKABLE);
146         spin_lock_irq(&current->sighand->siglock);
147         current->blocked = set;
148         recalc_sigpending();
149         spin_unlock_irq(&current->sighand->siglock);
150
151         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
152                 goto badframe;
153
154         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT)
155                 goto badframe;
156
157         return result;
158
159 badframe:
160         force_sig(SIGSEGV, current);
161         return 0;
162 }
163
164 /*
165  * Set up a signal frame.
166  */
167
168 static int
169 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
170                  unsigned long mask)
171 {
172         int err = 0;
173
174 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
175         COPY(r4);
176         COPY(r5);
177         COPY(r6);
178         COPY(pt_regs);
179         COPY(r0);
180         COPY(r1);
181         COPY(r2);
182         COPY(r3);
183         COPY(r7);
184         COPY(r8);
185         COPY(r9);
186         COPY(r10);
187         COPY(r11);
188         COPY(r12);
189         COPY(acc0h);
190         COPY(acc0l);
191         COPY(acc1h);            /* ISA_DSP_LEVEL2 only */
192         COPY(acc1l);            /* ISA_DSP_LEVEL2 only */
193         COPY(psw);
194         COPY(bpc);
195         COPY(bbpsw);
196         COPY(bbpc);
197         COPY(spu);
198         COPY(fp);
199         COPY(lr);
200         COPY(spi);
201 #undef COPY
202
203         err |= __put_user(mask, &sc->oldmask);
204
205         return err;
206 }
207
208 /*
209  * Determine which stack to use..
210  */
211 static inline void __user *
212 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
213 {
214         /* This is the X/Open sanctioned signal stack switching.  */
215         if (ka->sa.sa_flags & SA_ONSTACK) {
216                 if (sas_ss_flags(sp) == 0)
217                         sp = current->sas_ss_sp + current->sas_ss_size;
218         }
219
220         return (void __user *)((sp - frame_size) & -8ul);
221 }
222
223 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
224                            sigset_t *set, struct pt_regs *regs)
225 {
226         struct rt_sigframe __user *frame;
227         int err = 0;
228         int signal;
229
230         frame = get_sigframe(ka, regs->spu, sizeof(*frame));
231
232         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
233                 goto give_sigsegv;
234
235         signal = current_thread_info()->exec_domain
236                 && current_thread_info()->exec_domain->signal_invmap
237                 && sig < 32
238                 ? current_thread_info()->exec_domain->signal_invmap[sig]
239                 : sig;
240
241         err |= __put_user(signal, &frame->sig);
242         if (err)
243                 goto give_sigsegv;
244
245         err |= __put_user(&frame->info, &frame->pinfo);
246         err |= __put_user(&frame->uc, &frame->puc);
247         err |= copy_siginfo_to_user(&frame->info, info);
248         if (err)
249                 goto give_sigsegv;
250
251         /* Create the ucontext.  */
252         err |= __put_user(0, &frame->uc.uc_flags);
253         err |= __put_user(0, &frame->uc.uc_link);
254         err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
255         err |= __put_user(sas_ss_flags(regs->spu),
256                           &frame->uc.uc_stack.ss_flags);
257         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
258         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
259         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
260         if (err)
261                 goto give_sigsegv;
262
263         /* Set up to return from userspace.  */
264         regs->lr = (unsigned long)ka->sa.sa_restorer;
265
266         /* Set up registers for signal handler */
267         regs->spu = (unsigned long)frame;
268         regs->r0 = signal;      /* Arg for signal handler */
269         regs->r1 = (unsigned long)&frame->info;
270         regs->r2 = (unsigned long)&frame->uc;
271         regs->bpc = (unsigned long)ka->sa.sa_handler;
272
273         set_fs(USER_DS);
274
275 #if DEBUG_SIG
276         printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
277                 current->comm, current->pid, frame, regs->pc);
278 #endif
279
280         return;
281
282 give_sigsegv:
283         force_sigsegv(sig, current);
284 }
285
286 /*
287  * OK, we're invoking a handler
288  */
289
290 static void
291 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
292               sigset_t *oldset, struct pt_regs *regs)
293 {
294         unsigned short inst;
295
296         /* Are we from a system call? */
297         if (regs->syscall_nr >= 0) {
298                 /* If so, check system call restarting.. */
299                 switch (regs->r0) {
300                         case -ERESTART_RESTARTBLOCK:
301                         case -ERESTARTNOHAND:
302                                 regs->r0 = -EINTR;
303                                 break;
304
305                         case -ERESTARTSYS:
306                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
307                                         regs->r0 = -EINTR;
308                                         break;
309                                 }
310                         /* fallthrough */
311                         case -ERESTARTNOINTR:
312                                 regs->r0 = regs->orig_r0;
313                                 inst = *(unsigned short *)(regs->bpc - 2);
314                                 if ((inst & 0xfff0) == 0x10f0)  /* trap ? */
315                                         regs->bpc -= 2;
316                                 else
317                                         regs->bpc -= 4;
318                 }
319         }
320
321         /* Set up the stack frame */
322         setup_rt_frame(sig, ka, info, oldset, regs);
323
324         spin_lock_irq(&current->sighand->siglock);
325         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
326         if (!(ka->sa.sa_flags & SA_NODEFER))
327                 sigaddset(&current->blocked,sig);
328         recalc_sigpending();
329         spin_unlock_irq(&current->sighand->siglock);
330 }
331
332 /*
333  * Note that 'init' is a special process: it doesn't get signals it doesn't
334  * want to handle. Thus you cannot kill init even with a SIGKILL even by
335  * mistake.
336  */
337 int do_signal(struct pt_regs *regs, sigset_t *oldset)
338 {
339         siginfo_t info;
340         int signr;
341         struct k_sigaction ka;
342         unsigned short inst;
343
344         /*
345          * We want the common case to go fast, which
346          * is why we may in certain cases get here from
347          * kernel mode. Just return without doing anything
348          * if so.
349          */
350         if (!user_mode(regs))
351                 return 1;
352
353         if (try_to_freeze()) 
354                 goto no_signal;
355
356         if (!oldset)
357                 oldset = &current->blocked;
358
359         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
360         if (signr > 0) {
361                 /* Re-enable any watchpoints before delivering the
362                  * signal to user space. The processor register will
363                  * have been cleared if the watchpoint triggered
364                  * inside the kernel.
365                  */
366
367                 /* Whee!  Actually deliver the signal.  */
368                 handle_signal(signr, &ka, &info, oldset, regs);
369                 return 1;
370         }
371
372  no_signal:
373         /* Did we come from a system call? */
374         if (regs->syscall_nr >= 0) {
375                 /* Restart the system call - no handlers present */
376                 if (regs->r0 == -ERESTARTNOHAND ||
377                     regs->r0 == -ERESTARTSYS ||
378                     regs->r0 == -ERESTARTNOINTR) {
379                         regs->r0 = regs->orig_r0;
380                         inst = *(unsigned short *)(regs->bpc - 2);
381                         if ((inst & 0xfff0) == 0x10f0)  /* trap ? */
382                                 regs->bpc -= 2;
383                         else
384                                 regs->bpc -= 4;
385                 }
386                 if (regs->r0 == -ERESTART_RESTARTBLOCK){
387                         regs->r0 = regs->orig_r0;
388                         regs->r7 = __NR_restart_syscall;
389                         inst = *(unsigned short *)(regs->bpc - 2);
390                         if ((inst & 0xfff0) == 0x10f0)  /* trap ? */
391                                 regs->bpc -= 2;
392                         else
393                                 regs->bpc -= 4;
394                 }
395         }
396         return 0;
397 }
398
399 /*
400  * notification of userspace execution resumption
401  * - triggered by current->work.notify_resume
402  */
403 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
404                       __u32 thread_info_flags)
405 {
406         /* Pending single-step? */
407         if (thread_info_flags & _TIF_SINGLESTEP)
408                 clear_thread_flag(TIF_SINGLESTEP);
409
410         /* deal with pending signal delivery */
411         if (thread_info_flags & _TIF_SIGPENDING)
412                 do_signal(regs,oldset);
413
414         clear_thread_flag(TIF_IRET);
415 }