GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arc / kernel / signal.c
1 /*
2  * Signal Handling for ARC
3  *
4  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * vineetg: Jan 2010 (Restarting of timer related syscalls)
11  *
12  * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
13  *  -do_signal() supports TIF_RESTORE_SIGMASK
14  *  -do_signal() no loner needs oldset, required by OLD sys_sigsuspend
15  *  -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen
16  *  -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed
17  *  -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves
18  *   the job to do_signal()
19  *
20  * vineetg: July 2009
21  *  -Modified Code to support the uClibc provided userland sigreturn stub
22  *   to avoid kernel synthesing it on user stack at runtime, costing TLB
23  *   probes and Cache line flushes.
24  *
25  * vineetg: July 2009
26  *  -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs
27  *   in done in block copy rather than one word at a time.
28  *   This saves around 2K of code and improves LMBench lat_sig <catch>
29  *
30  * rajeshwarr: Feb 2009
31  *  - Support for Realtime Signals
32  *
33  * vineetg: Aug 11th 2008: Bug #94183
34  *  -ViXS were still seeing crashes when using insmod to load drivers.
35  *   It turned out that the code to change Execute permssions for TLB entries
36  *   of user was not guarded for interrupts (mod_tlb_permission)
37  *   This was causing TLB entries to be overwritten on unrelated indexes
38  *
39  * Vineetg: July 15th 2008: Bug #94183
40  *  -Exception happens in Delay slot of a JMP, and before user space resumes,
41  *   Signal is delivered (Ctrl + C) = >SIGINT.
42  *   setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler
43  *   to run, but doesn't clear the Delay slot bit from status32. As a result,
44  *   on resuming user mode, signal handler branches off to BTA of orig JMP
45  *  -FIX: clear the DE bit from status32 in setup_frame( )
46  *
47  * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
48  */
49
50 #include <linux/signal.h>
51 #include <linux/ptrace.h>
52 #include <linux/personality.h>
53 #include <linux/uaccess.h>
54 #include <linux/syscalls.h>
55 #include <linux/tracehook.h>
56 #include <linux/sched/task_stack.h>
57
58 #include <asm/ucontext.h>
59
60 struct rt_sigframe {
61         struct siginfo info;
62         struct ucontext uc;
63 #define MAGIC_SIGALTSTK         0x07302004
64         unsigned int sigret_magic;
65 };
66
67 static int save_arcv2_regs(struct sigcontext *mctx, struct pt_regs *regs)
68 {
69         int err = 0;
70 #ifndef CONFIG_ISA_ARCOMPACT
71         struct user_regs_arcv2 v2abi;
72
73         v2abi.r30 = regs->r30;
74 #ifdef CONFIG_ARC_HAS_ACCL_REGS
75         v2abi.r58 = regs->r58;
76         v2abi.r59 = regs->r59;
77 #else
78         v2abi.r58 = v2abi.r59 = 0;
79 #endif
80         err = __copy_to_user(&mctx->v2abi, &v2abi, sizeof(v2abi));
81 #endif
82         return err;
83 }
84
85 static int restore_arcv2_regs(struct sigcontext *mctx, struct pt_regs *regs)
86 {
87         int err = 0;
88 #ifndef CONFIG_ISA_ARCOMPACT
89         struct user_regs_arcv2 v2abi;
90
91         err = __copy_from_user(&v2abi, &mctx->v2abi, sizeof(v2abi));
92
93         regs->r30 = v2abi.r30;
94 #ifdef CONFIG_ARC_HAS_ACCL_REGS
95         regs->r58 = v2abi.r58;
96         regs->r59 = v2abi.r59;
97 #endif
98 #endif
99         return err;
100 }
101
102 static int
103 stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
104                sigset_t *set)
105 {
106         int err;
107         struct user_regs_struct uregs;
108
109         uregs.scratch.bta       = regs->bta;
110         uregs.scratch.lp_start  = regs->lp_start;
111         uregs.scratch.lp_end    = regs->lp_end;
112         uregs.scratch.lp_count  = regs->lp_count;
113         uregs.scratch.status32  = regs->status32;
114         uregs.scratch.ret       = regs->ret;
115         uregs.scratch.blink     = regs->blink;
116         uregs.scratch.fp        = regs->fp;
117         uregs.scratch.gp        = regs->r26;
118         uregs.scratch.r12       = regs->r12;
119         uregs.scratch.r11       = regs->r11;
120         uregs.scratch.r10       = regs->r10;
121         uregs.scratch.r9        = regs->r9;
122         uregs.scratch.r8        = regs->r8;
123         uregs.scratch.r7        = regs->r7;
124         uregs.scratch.r6        = regs->r6;
125         uregs.scratch.r5        = regs->r5;
126         uregs.scratch.r4        = regs->r4;
127         uregs.scratch.r3        = regs->r3;
128         uregs.scratch.r2        = regs->r2;
129         uregs.scratch.r1        = regs->r1;
130         uregs.scratch.r0        = regs->r0;
131         uregs.scratch.sp        = regs->sp;
132
133         err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), &uregs.scratch,
134                              sizeof(sf->uc.uc_mcontext.regs.scratch));
135
136         if (is_isa_arcv2())
137                 err |= save_arcv2_regs(&(sf->uc.uc_mcontext), regs);
138
139         err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
140
141         return err ? -EFAULT : 0;
142 }
143
144 static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
145 {
146         sigset_t set;
147         int err;
148         struct user_regs_struct uregs;
149
150         err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
151         err |= __copy_from_user(&uregs.scratch,
152                                 &(sf->uc.uc_mcontext.regs.scratch),
153                                 sizeof(sf->uc.uc_mcontext.regs.scratch));
154
155         if (is_isa_arcv2())
156                 err |= restore_arcv2_regs(&(sf->uc.uc_mcontext), regs);
157
158         if (err)
159                 return -EFAULT;
160
161         set_current_blocked(&set);
162         regs->bta       = uregs.scratch.bta;
163         regs->lp_start  = uregs.scratch.lp_start;
164         regs->lp_end    = uregs.scratch.lp_end;
165         regs->lp_count  = uregs.scratch.lp_count;
166         regs->status32  = uregs.scratch.status32;
167         regs->ret       = uregs.scratch.ret;
168         regs->blink     = uregs.scratch.blink;
169         regs->fp        = uregs.scratch.fp;
170         regs->r26       = uregs.scratch.gp;
171         regs->r12       = uregs.scratch.r12;
172         regs->r11       = uregs.scratch.r11;
173         regs->r10       = uregs.scratch.r10;
174         regs->r9        = uregs.scratch.r9;
175         regs->r8        = uregs.scratch.r8;
176         regs->r7        = uregs.scratch.r7;
177         regs->r6        = uregs.scratch.r6;
178         regs->r5        = uregs.scratch.r5;
179         regs->r4        = uregs.scratch.r4;
180         regs->r3        = uregs.scratch.r3;
181         regs->r2        = uregs.scratch.r2;
182         regs->r1        = uregs.scratch.r1;
183         regs->r0        = uregs.scratch.r0;
184         regs->sp        = uregs.scratch.sp;
185
186         return 0;
187 }
188
189 static inline int is_do_ss_needed(unsigned int magic)
190 {
191         if (MAGIC_SIGALTSTK == magic)
192                 return 1;
193         else
194                 return 0;
195 }
196
197 SYSCALL_DEFINE0(rt_sigreturn)
198 {
199         struct rt_sigframe __user *sf;
200         unsigned int magic;
201         struct pt_regs *regs = current_pt_regs();
202
203         /* Always make any pending restarted system calls return -EINTR */
204         current->restart_block.fn = do_no_restart_syscall;
205
206         /* Since we stacked the signal on a word boundary,
207          * then 'sp' should be word aligned here.  If it's
208          * not, then the user is trying to mess with us.
209          */
210         if (regs->sp & 3)
211                 goto badframe;
212
213         sf = (struct rt_sigframe __force __user *)(regs->sp);
214
215         if (!access_ok(VERIFY_READ, sf, sizeof(*sf)))
216                 goto badframe;
217
218         if (__get_user(magic, &sf->sigret_magic))
219                 goto badframe;
220
221         if (unlikely(is_do_ss_needed(magic)))
222                 if (restore_altstack(&sf->uc.uc_stack))
223                         goto badframe;
224
225         if (restore_usr_regs(regs, sf))
226                 goto badframe;
227
228         /* Don't restart from sigreturn */
229         syscall_wont_restart(regs);
230
231         /*
232          * Ensure that sigreturn always returns to user mode (in case the
233          * regs saved on user stack got fudged between save and sigreturn)
234          * Otherwise it is easy to panic the kernel with a custom
235          * signal handler and/or restorer which clobberes the status32/ret
236          * to return to a bogus location in kernel mode.
237          */
238         regs->status32 |= STATUS_U_MASK;
239
240         return regs->r0;
241
242 badframe:
243         force_sig(SIGSEGV, current);
244         return 0;
245 }
246
247 /*
248  * Determine which stack to use..
249  */
250 static inline void __user *get_sigframe(struct ksignal *ksig,
251                                         struct pt_regs *regs,
252                                         unsigned long framesize)
253 {
254         unsigned long sp = sigsp(regs->sp, ksig);
255         void __user *frame;
256
257         /* No matter what happens, 'sp' must be word
258          * aligned otherwise nasty things could happen
259          */
260
261         /* ATPCS B01 mandates 8-byte alignment */
262         frame = (void __user *)((sp - framesize) & ~7);
263
264         /* Check that we can actually write to the signal frame */
265         if (!access_ok(VERIFY_WRITE, frame, framesize))
266                 frame = NULL;
267
268         return frame;
269 }
270
271 static int
272 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
273 {
274         struct rt_sigframe __user *sf;
275         unsigned int magic = 0;
276         int err = 0;
277
278         sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
279         if (!sf)
280                 return 1;
281
282         /*
283          * w/o SA_SIGINFO, struct ucontext is partially populated (only
284          * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
285          * during signal handler execution. This works for SA_SIGINFO as well
286          * although the semantics are now overloaded (the same reg state can be
287          * inspected by userland: but are they allowed to fiddle with it ?
288          */
289         err |= stash_usr_regs(sf, regs, set);
290
291         /*
292          * SA_SIGINFO requires 3 args to signal handler:
293          *  #1: sig-no (common to any handler)
294          *  #2: struct siginfo
295          *  #3: struct ucontext (completely populated)
296          */
297         if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) {
298                 err |= copy_siginfo_to_user(&sf->info, &ksig->info);
299                 err |= __put_user(0, &sf->uc.uc_flags);
300                 err |= __put_user(NULL, &sf->uc.uc_link);
301                 err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
302
303                 /* setup args 2 and 3 for user mode handler */
304                 regs->r1 = (unsigned long)&sf->info;
305                 regs->r2 = (unsigned long)&sf->uc;
306
307                 /*
308                  * small optim to avoid unconditonally calling do_sigaltstack
309                  * in sigreturn path, now that we only have rt_sigreturn
310                  */
311                 magic = MAGIC_SIGALTSTK;
312         }
313
314         err |= __put_user(magic, &sf->sigret_magic);
315         if (err)
316                 return err;
317
318         /* #1 arg to the user Signal handler */
319         regs->r0 = ksig->sig;
320
321         /* setup PC of user space signal handler */
322         regs->ret = (unsigned long)ksig->ka.sa.sa_handler;
323
324         /*
325          * handler returns using sigreturn stub provided already by userpsace
326          * If not, nuke the process right away
327          */
328         if(!(ksig->ka.sa.sa_flags & SA_RESTORER))
329                 return 1;
330
331         regs->blink = (unsigned long)ksig->ka.sa.sa_restorer;
332
333         /* User Stack for signal handler will be above the frame just carved */
334         regs->sp = (unsigned long)sf;
335
336         /*
337          * Bug 94183, Clear the DE bit, so that when signal handler
338          * starts to run, it doesn't use BTA
339          */
340         regs->status32 &= ~STATUS_DE_MASK;
341         regs->status32 |= STATUS_L_MASK;
342
343         return err;
344 }
345
346 static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
347 {
348         switch (regs->r0) {
349         case -ERESTART_RESTARTBLOCK:
350         case -ERESTARTNOHAND:
351                 /*
352                  * ERESTARTNOHAND means that the syscall should
353                  * only be restarted if there was no handler for
354                  * the signal, and since we only get here if there
355                  * is a handler, we don't restart
356                  */
357                 regs->r0 = -EINTR;   /* ERESTART_xxx is internal */
358                 break;
359
360         case -ERESTARTSYS:
361                 /*
362                  * ERESTARTSYS means to restart the syscall if
363                  * there is no handler or the handler was
364                  * registered with SA_RESTART
365                  */
366                 if (!(ka->sa.sa_flags & SA_RESTART)) {
367                         regs->r0 = -EINTR;
368                         break;
369                 }
370                 /* fallthrough */
371
372         case -ERESTARTNOINTR:
373                 /*
374                  * ERESTARTNOINTR means that the syscall should
375                  * be called again after the signal handler returns.
376                  * Setup reg state just as it was before doing the trap
377                  * r0 has been clobbered with sys call ret code thus it
378                  * needs to be reloaded with orig first arg to syscall
379                  * in orig_r0. Rest of relevant reg-file:
380                  * r8 (syscall num) and (r1 - r7) will be reset to
381                  * their orig user space value when we ret from kernel
382                  */
383                 regs->r0 = regs->orig_r0;
384                 regs->ret -= is_isa_arcv2() ? 2 : 4;
385                 break;
386         }
387 }
388
389 /*
390  * OK, we're invoking a handler
391  */
392 static void
393 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
394 {
395         sigset_t *oldset = sigmask_to_save();
396         int failed;
397
398         /* Set up the stack frame */
399         failed = setup_rt_frame(ksig, oldset, regs);
400
401         signal_setup_done(failed, ksig, 0);
402 }
403
404 void do_signal(struct pt_regs *regs)
405 {
406         struct ksignal ksig;
407         int restart_scall;
408
409         restart_scall = in_syscall(regs) && syscall_restartable(regs);
410
411         if (get_signal(&ksig)) {
412                 if (restart_scall) {
413                         arc_restart_syscall(&ksig.ka, regs);
414                         syscall_wont_restart(regs);     /* No more restarts */
415                 }
416                 handle_signal(&ksig, regs);
417                 return;
418         }
419
420         if (restart_scall) {
421                 /* No handler for syscall: restart it */
422                 if (regs->r0 == -ERESTARTNOHAND ||
423                     regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
424                         regs->r0 = regs->orig_r0;
425                         regs->ret -= is_isa_arcv2() ? 2 : 4;
426                 } else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
427                         regs->r8 = __NR_restart_syscall;
428                         regs->ret -= is_isa_arcv2() ? 2 : 4;
429                 }
430                 syscall_wont_restart(regs);     /* No more restarts */
431         }
432
433         /* If there's no signal to deliver, restore the saved sigmask back */
434         restore_saved_sigmask();
435 }
436
437 void do_notify_resume(struct pt_regs *regs)
438 {
439         /*
440          * ASM glue gaurantees that this is only called when returning to
441          * user mode
442          */
443         if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
444                 tracehook_notify_resume(regs);
445 }