Merge tag 'metag-for-v4.1' of git://git.kernel.org/pub/scm/linux/kernel/git/jhogan...
[firefly-linux-kernel-4.4.55.git] / arch / cris / arch-v32 / kernel / signal.c
1 /*
2  * Copyright (C) 2003, Axis Communications AB.
3  */
4
5 #include <linux/sched.h>
6 #include <linux/mm.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/signal.h>
10 #include <linux/errno.h>
11 #include <linux/wait.h>
12 #include <linux/ptrace.h>
13 #include <linux/unistd.h>
14 #include <linux/stddef.h>
15 #include <linux/syscalls.h>
16 #include <linux/vmalloc.h>
17
18 #include <asm/io.h>
19 #include <asm/processor.h>
20 #include <asm/ucontext.h>
21 #include <asm/uaccess.h>
22 #include <arch/ptrace.h>
23 #include <arch/hwregs/cpu_vect.h>
24
25 extern unsigned long cris_signal_return_page;
26
27 /*
28  * A syscall in CRIS is really a "break 13" instruction, which is 2
29  * bytes. The registers is manipulated so upon return the instruction
30  * will be executed again.
31  *
32  * This relies on that PC points to the instruction after the break call.
33  */
34 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
35
36 /* Signal frames. */
37 struct signal_frame {
38         struct sigcontext sc;
39         unsigned long extramask[_NSIG_WORDS - 1];
40         unsigned char retcode[8];       /* Trampoline code. */
41 };
42
43 struct rt_signal_frame {
44         struct siginfo *pinfo;
45         void *puc;
46         struct siginfo info;
47         struct ucontext uc;
48         unsigned char retcode[8];       /* Trampoline code. */
49 };
50
51 void do_signal(int restart, struct pt_regs *regs);
52 void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
53                       struct pt_regs *regs);
54
55 static int
56 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
57 {
58         unsigned int err = 0;
59         unsigned long old_usp;
60
61         /* Always make any pending restarted system calls return -EINTR */
62         current->restart_block.fn = do_no_restart_syscall;
63
64         /*
65          * Restore the registers from &sc->regs. sc is already checked
66          * for VERIFY_READ since the signal_frame was previously
67          * checked in sys_sigreturn().
68          */
69         if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
70                 goto badframe;
71
72         /* Make that the user-mode flag is set. */
73         regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
74
75         /* Restore the old USP. */
76         err |= __get_user(old_usp, &sc->usp);
77         wrusp(old_usp);
78
79         return err;
80
81 badframe:
82         return 1;
83 }
84
85 asmlinkage int sys_sigreturn(void)
86 {
87         struct pt_regs *regs = current_pt_regs();
88         sigset_t set;
89         struct signal_frame __user *frame;
90         unsigned long oldspc = regs->spc;
91         unsigned long oldccs = regs->ccs;
92
93         frame = (struct signal_frame *) rdusp();
94
95         /*
96          * Since the signal is stacked on a dword boundary, the frame
97          * should be dword aligned here as well. It it's not, then the
98          * user is trying some funny business.
99          */
100         if (((long)frame) & 3)
101                 goto badframe;
102
103         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
104                 goto badframe;
105
106         if (__get_user(set.sig[0], &frame->sc.oldmask) ||
107             (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
108                                                  frame->extramask,
109                                                  sizeof(frame->extramask))))
110                 goto badframe;
111
112         set_current_blocked(&set);
113
114         if (restore_sigcontext(regs, &frame->sc))
115                 goto badframe;
116
117         keep_debug_flags(oldccs, oldspc, regs);
118
119         return regs->r10;
120
121 badframe:
122         force_sig(SIGSEGV, current);
123         return 0;
124 }
125
126 asmlinkage int sys_rt_sigreturn(void)
127 {
128         struct pt_regs *regs = current_pt_regs();
129         sigset_t set;
130         struct rt_signal_frame __user *frame;
131         unsigned long oldspc = regs->spc;
132         unsigned long oldccs = regs->ccs;
133
134         frame = (struct rt_signal_frame *) rdusp();
135
136         /*
137          * Since the signal is stacked on a dword boundary, the frame
138          * should be dword aligned here as well. It it's not, then the
139          * user is trying some funny business.
140          */
141         if (((long)frame) & 3)
142                 goto badframe;
143
144         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
145                 goto badframe;
146
147         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
148                 goto badframe;
149
150         set_current_blocked(&set);
151
152         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
153                 goto badframe;
154
155         if (restore_altstack(&frame->uc.uc_stack))
156                 goto badframe;
157
158         keep_debug_flags(oldccs, oldspc, regs);
159
160         return regs->r10;
161
162 badframe:
163         force_sig(SIGSEGV, current);
164         return 0;
165 }
166
167 /* Setup a signal frame. */
168 static int
169 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
170                  unsigned long mask)
171 {
172         int err;
173         unsigned long usp;
174
175         err = 0;
176         usp = rdusp();
177
178         /*
179          * Copy the registers. They are located first in sc, so it's
180          * possible to use sc directly.
181          */
182         err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
183
184         err |= __put_user(mask, &sc->oldmask);
185         err |= __put_user(usp, &sc->usp);
186
187         return err;
188 }
189
190 /* Figure out where to put the new signal frame - usually on the stack. */
191 static inline void __user *
192 get_sigframe(struct ksignal *ksig, size_t frame_size)
193 {
194         unsigned long sp = sigsp(rdusp(), ksig);
195
196         /* Make sure the frame is dword-aligned. */
197         sp &= ~3;
198
199         return (void __user *)(sp - frame_size);
200 }
201
202 /* Grab and setup a signal frame.
203  *
204  * Basically a lot of state-info is stacked, and arranged for the
205  * user-mode program to return to the kernel using either a trampiline
206  * which performs the syscall sigreturn(), or a provided user-mode
207  * trampoline.
208   */
209 static int
210 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
211 {
212         int err;
213         unsigned long return_ip;
214         struct signal_frame __user *frame;
215
216         err = 0;
217         frame = get_sigframe(ksig, sizeof(*frame));
218
219         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
220                 return -EFAULT;
221
222         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
223
224         if (err)
225                 return -EFAULT;
226
227         if (_NSIG_WORDS > 1) {
228                 err |= __copy_to_user(frame->extramask, &set->sig[1],
229                                       sizeof(frame->extramask));
230         }
231
232         if (err)
233                 return -EFAULT;
234
235         /*
236          * Set up to return from user-space. If provided, use a stub
237          * already located in user-space.
238          */
239         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
240                 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
241         } else {
242                 /* Trampoline - the desired return ip is in the signal return page. */
243                 return_ip = cris_signal_return_page;
244
245                 /*
246                  * This is movu.w __NR_sigreturn, r9; break 13;
247                  *
248                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
249                  * reasons and because gdb uses it as a signature to notice
250                  * signal handler stack frames.
251                  */
252                 err |= __put_user(0x9c5f,         (short __user*)(frame->retcode+0));
253                 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
254                 err |= __put_user(0xe93d,         (short __user*)(frame->retcode+4));
255         }
256
257         if (err)
258                 return -EFAULT;
259
260         /*
261          * Set up registers for signal handler.
262          *
263          * Where the code enters now.
264          * Where the code enter later.
265          * First argument, signo.
266          */
267         regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
268         regs->srp = return_ip;
269         regs->r10 = ksig->sig;
270
271         /* Actually move the USP to reflect the stacked frame. */
272         wrusp((unsigned long)frame);
273
274         return 0;
275 }
276
277 static int
278 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
279 {
280         int err;
281         unsigned long return_ip;
282         struct rt_signal_frame __user *frame;
283
284         err = 0;
285         frame = get_sigframe(ksig, sizeof(*frame));
286
287         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288                 return -EFAULT;
289
290         err |= __put_user(&frame->info, &frame->pinfo);
291         err |= __put_user(&frame->uc, &frame->puc);
292         err |= copy_siginfo_to_user(&frame->info, &ksig->info);
293
294         if (err)
295                 return -EFAULT;
296
297         /* Clear all the bits of the ucontext we don't use.  */
298         err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
299         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
300         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
301         err |= __save_altstack(&frame->uc.uc_stack, rdusp());
302
303         if (err)
304                 return -EFAULT;
305
306         /*
307          * Set up to return from user-space. If provided, use a stub
308          * already located in user-space.
309          */
310         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
311                 return_ip = (unsigned long) ksig->ka.sa.sa_restorer;
312         } else {
313                 /* Trampoline - the desired return ip is in the signal return page. */
314                 return_ip = cris_signal_return_page + 6;
315
316                 /*
317                  * This is movu.w __NR_rt_sigreturn, r9; break 13;
318                  *
319                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
320                  * reasons and because gdb uses it as a signature to notice
321                  * signal handler stack frames.
322                  */
323                 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
324
325                 err |= __put_user(__NR_rt_sigreturn,
326                                   (short __user*)(frame->retcode+2));
327
328                 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
329         }
330
331         if (err)
332                 return -EFAULT;
333
334         /*
335          * Set up registers for signal handler.
336          *
337          * Where the code enters now.
338          * Where the code enters later.
339          * First argument is signo.
340          * Second argument is (siginfo_t *).
341          * Third argument is unused.
342          */
343         regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
344         regs->srp = return_ip;
345         regs->r10 = ksig->sig;
346         regs->r11 = (unsigned long) &frame->info;
347         regs->r12 = 0;
348
349         /* Actually move the usp to reflect the stacked frame. */
350         wrusp((unsigned long)frame);
351
352         return 0;
353 }
354
355 /* Invoke a signal handler to, well, handle the signal. */
356 static inline void
357 handle_signal(int canrestart, struct ksignal *ksig, struct pt_regs *regs)
358 {
359         sigset_t *oldset = sigmask_to_save();
360         int ret;
361
362         /* Check if this got called from a system call. */
363         if (canrestart) {
364                 /* If so, check system call restarting. */
365                 switch (regs->r10) {
366                         case -ERESTART_RESTARTBLOCK:
367                         case -ERESTARTNOHAND:
368                                 /*
369                                  * This means that the syscall should
370                                  * only be restarted if there was no
371                                  * handler for the signal, and since
372                                  * this point isn't reached unless
373                                  * there is a handler, there's no need
374                                  * to restart.
375                                  */
376                                 regs->r10 = -EINTR;
377                                 break;
378
379                         case -ERESTARTSYS:
380                                 /*
381                                  * This means restart the syscall if
382                                  * there is no handler, or the handler
383                                  * was registered with SA_RESTART.
384                                  */
385                                 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
386                                         regs->r10 = -EINTR;
387                                         break;
388                                 }
389
390                                 /* Fall through. */
391
392                         case -ERESTARTNOINTR:
393                                 /*
394                                  * This means that the syscall should
395                                  * be called again after the signal
396                                  * handler returns.
397                                  */
398                                 RESTART_CRIS_SYS(regs);
399                                 break;
400                 }
401         }
402
403         /* Set up the stack frame. */
404         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
405                 ret = setup_rt_frame(ksig, oldset, regs);
406         else
407                 ret = setup_frame(ksig, oldset, regs);
408
409         signal_setup_done(ret, ksig, 0);
410 }
411
412 /*
413  * Note that 'init' is a special process: it doesn't get signals it doesn't
414  * want to handle. Thus you cannot kill init even with a SIGKILL even by
415  * mistake.
416  *
417  * Also note that the regs structure given here as an argument, is the latest
418  * pushed pt_regs. It may or may not be the same as the first pushed registers
419  * when the initial usermode->kernelmode transition took place. Therefore
420  * we can use user_mode(regs) to see if we came directly from kernel or user
421  * mode below.
422  */
423 void
424 do_signal(int canrestart, struct pt_regs *regs)
425 {
426         struct ksignal ksig;
427
428         /*
429          * The common case should go fast, which is why this point is
430          * reached from kernel-mode. If that's the case, just return
431          * without doing anything.
432          */
433         if (!user_mode(regs))
434                 return;
435
436         if (get_signal(&ksig)) {
437                 /* Whee!  Actually deliver the signal.  */
438                 handle_signal(canrestart, &ksig, regs);
439                 return;
440         }
441
442         /* Got here from a system call? */
443         if (canrestart) {
444                 /* Restart the system call - no handlers present. */
445                 if (regs->r10 == -ERESTARTNOHAND ||
446                     regs->r10 == -ERESTARTSYS ||
447                     regs->r10 == -ERESTARTNOINTR) {
448                         RESTART_CRIS_SYS(regs);
449                 }
450
451                 if (regs->r10 == -ERESTART_RESTARTBLOCK){
452                         regs->r9 = __NR_restart_syscall;
453                         regs->erp -= 2;
454                 }
455         }
456
457         /* if there's no signal to deliver, we just put the saved sigmask
458          * back */
459         restore_saved_sigmask();
460 }
461
462 asmlinkage void
463 ugdb_trap_user(struct thread_info *ti, int sig)
464 {
465         if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
466                 /* Zero single-step PC if the reason we stopped wasn't a single
467                    step exception. This is to avoid relying on it when it isn't
468                    reliable. */
469                 user_regs(ti)->spc = 0;
470         }
471         /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
472            not within any configured h/w breakpoint range). Synchronize with
473            what already exists for kernel debugging.  */
474         if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
475                 /* Break 8: subtract 2 from ERP unless in a delay slot. */
476                 if (!(user_regs(ti)->erp & 0x1))
477                         user_regs(ti)->erp -= 2;
478         }
479         sys_kill(ti->task->pid, sig);
480 }
481
482 void
483 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
484                  struct pt_regs *regs)
485 {
486         if (oldccs & (1 << Q_CCS_BITNR)) {
487                 /* Pending single step due to single-stepping the break 13
488                    in the signal trampoline: keep the Q flag. */
489                 regs->ccs |= (1 << Q_CCS_BITNR);
490                 /* S flag should be set - complain if it's not. */
491                 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
492                         printk("Q flag but no S flag?");
493                 }
494                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
495                 /* Assume the SPC is valid and interesting. */
496                 regs->spc = oldspc;
497
498         } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
499                 /* If a h/w bp was set in the signal handler we need
500                    to keep the S flag. */
501                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
502                 /* Don't keep the old SPC though; if we got here due to
503                    a single-step, the Q flag should have been set. */
504         } else if (regs->spc) {
505                 /* If we were single-stepping *before* the signal was taken,
506                    we don't want to restore that state now, because GDB will
507                    have forgotten all about it. */
508                 regs->spc = 0;
509                 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
510         }
511 }
512
513 /* Set up the trampolines on the signal return page. */
514 int __init
515 cris_init_signal(void)
516 {
517         u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
518
519         /* This is movu.w __NR_sigreturn, r9; break 13; */
520         data[0] = 0x9c5f;
521         data[1] = __NR_sigreturn;
522         data[2] = 0xe93d;
523         /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
524         data[3] = 0x9c5f;
525         data[4] = __NR_rt_sigreturn;
526         data[5] = 0xe93d;
527
528         /* Map to userspace with appropriate permissions (no write access...) */
529         cris_signal_return_page = (unsigned long)
530           __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
531
532         return 0;
533 }
534
535 __initcall(cris_init_signal);