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