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