treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / arc / kernel / signal.c
blob3d57ed0d8535098d958b03091ba59c271c98f126
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Signal Handling for ARC
5 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
7 * vineetg: Jan 2010 (Restarting of timer related syscalls)
9 * vineetg: Nov 2009 (Everything needed for TIF_RESTORE_SIGMASK)
10 * -do_signal() supports TIF_RESTORE_SIGMASK
11 * -do_signal() no loner needs oldset, required by OLD sys_sigsuspend
12 * -sys_rt_sigsuspend() now comes from generic code, so discard arch implemen
13 * -sys_sigsuspend() no longer needs to fudge ptregs, hence that arg removed
14 * -sys_sigsuspend() no longer loops for do_signal(), sets TIF_xxx and leaves
15 * the job to do_signal()
17 * vineetg: July 2009
18 * -Modified Code to support the uClibc provided userland sigreturn stub
19 * to avoid kernel synthesing it on user stack at runtime, costing TLB
20 * probes and Cache line flushes.
22 * vineetg: July 2009
23 * -In stash_usr_regs( ) and restore_usr_regs( ), save/restore of user regs
24 * in done in block copy rather than one word at a time.
25 * This saves around 2K of code and improves LMBench lat_sig <catch>
27 * rajeshwarr: Feb 2009
28 * - Support for Realtime Signals
30 * vineetg: Aug 11th 2008: Bug #94183
31 * -ViXS were still seeing crashes when using insmod to load drivers.
32 * It turned out that the code to change Execute permssions for TLB entries
33 * of user was not guarded for interrupts (mod_tlb_permission)
34 * This was causing TLB entries to be overwritten on unrelated indexes
36 * Vineetg: July 15th 2008: Bug #94183
37 * -Exception happens in Delay slot of a JMP, and before user space resumes,
38 * Signal is delivered (Ctrl + C) = >SIGINT.
39 * setup_frame( ) sets up PC,SP,BLINK to enable user space signal handler
40 * to run, but doesn't clear the Delay slot bit from status32. As a result,
41 * on resuming user mode, signal handler branches off to BTA of orig JMP
42 * -FIX: clear the DE bit from status32 in setup_frame( )
44 * Rahul Trivedi, Kanika Nema: Codito Technologies 2004
47 #include <linux/signal.h>
48 #include <linux/ptrace.h>
49 #include <linux/personality.h>
50 #include <linux/uaccess.h>
51 #include <linux/syscalls.h>
52 #include <linux/tracehook.h>
53 #include <linux/sched/task_stack.h>
55 #include <asm/ucontext.h>
57 struct rt_sigframe {
58 struct siginfo info;
59 struct ucontext uc;
60 #define MAGIC_SIGALTSTK 0x07302004
61 unsigned int sigret_magic;
64 static int
65 stash_usr_regs(struct rt_sigframe __user *sf, struct pt_regs *regs,
66 sigset_t *set)
68 int err;
69 struct user_regs_struct uregs;
71 uregs.scratch.bta = regs->bta;
72 uregs.scratch.lp_start = regs->lp_start;
73 uregs.scratch.lp_end = regs->lp_end;
74 uregs.scratch.lp_count = regs->lp_count;
75 uregs.scratch.status32 = regs->status32;
76 uregs.scratch.ret = regs->ret;
77 uregs.scratch.blink = regs->blink;
78 uregs.scratch.fp = regs->fp;
79 uregs.scratch.gp = regs->r26;
80 uregs.scratch.r12 = regs->r12;
81 uregs.scratch.r11 = regs->r11;
82 uregs.scratch.r10 = regs->r10;
83 uregs.scratch.r9 = regs->r9;
84 uregs.scratch.r8 = regs->r8;
85 uregs.scratch.r7 = regs->r7;
86 uregs.scratch.r6 = regs->r6;
87 uregs.scratch.r5 = regs->r5;
88 uregs.scratch.r4 = regs->r4;
89 uregs.scratch.r3 = regs->r3;
90 uregs.scratch.r2 = regs->r2;
91 uregs.scratch.r1 = regs->r1;
92 uregs.scratch.r0 = regs->r0;
93 uregs.scratch.sp = regs->sp;
95 err = __copy_to_user(&(sf->uc.uc_mcontext.regs.scratch), &uregs.scratch,
96 sizeof(sf->uc.uc_mcontext.regs.scratch));
97 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(sigset_t));
99 return err;
102 static int restore_usr_regs(struct pt_regs *regs, struct rt_sigframe __user *sf)
104 sigset_t set;
105 int err;
106 struct user_regs_struct uregs;
108 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
109 err |= __copy_from_user(&uregs.scratch,
110 &(sf->uc.uc_mcontext.regs.scratch),
111 sizeof(sf->uc.uc_mcontext.regs.scratch));
112 if (err)
113 return err;
115 set_current_blocked(&set);
116 regs->bta = uregs.scratch.bta;
117 regs->lp_start = uregs.scratch.lp_start;
118 regs->lp_end = uregs.scratch.lp_end;
119 regs->lp_count = uregs.scratch.lp_count;
120 regs->status32 = uregs.scratch.status32;
121 regs->ret = uregs.scratch.ret;
122 regs->blink = uregs.scratch.blink;
123 regs->fp = uregs.scratch.fp;
124 regs->r26 = uregs.scratch.gp;
125 regs->r12 = uregs.scratch.r12;
126 regs->r11 = uregs.scratch.r11;
127 regs->r10 = uregs.scratch.r10;
128 regs->r9 = uregs.scratch.r9;
129 regs->r8 = uregs.scratch.r8;
130 regs->r7 = uregs.scratch.r7;
131 regs->r6 = uregs.scratch.r6;
132 regs->r5 = uregs.scratch.r5;
133 regs->r4 = uregs.scratch.r4;
134 regs->r3 = uregs.scratch.r3;
135 regs->r2 = uregs.scratch.r2;
136 regs->r1 = uregs.scratch.r1;
137 regs->r0 = uregs.scratch.r0;
138 regs->sp = uregs.scratch.sp;
140 return 0;
143 static inline int is_do_ss_needed(unsigned int magic)
145 if (MAGIC_SIGALTSTK == magic)
146 return 1;
147 else
148 return 0;
151 SYSCALL_DEFINE0(rt_sigreturn)
153 struct rt_sigframe __user *sf;
154 unsigned int magic;
155 struct pt_regs *regs = current_pt_regs();
157 /* Always make any pending restarted system calls return -EINTR */
158 current->restart_block.fn = do_no_restart_syscall;
160 /* Since we stacked the signal on a word boundary,
161 * then 'sp' should be word aligned here. If it's
162 * not, then the user is trying to mess with us.
164 if (regs->sp & 3)
165 goto badframe;
167 sf = (struct rt_sigframe __force __user *)(regs->sp);
169 if (!access_ok(sf, sizeof(*sf)))
170 goto badframe;
172 if (__get_user(magic, &sf->sigret_magic))
173 goto badframe;
175 if (unlikely(is_do_ss_needed(magic)))
176 if (restore_altstack(&sf->uc.uc_stack))
177 goto badframe;
179 if (restore_usr_regs(regs, sf))
180 goto badframe;
182 /* Don't restart from sigreturn */
183 syscall_wont_restart(regs);
186 * Ensure that sigreturn always returns to user mode (in case the
187 * regs saved on user stack got fudged between save and sigreturn)
188 * Otherwise it is easy to panic the kernel with a custom
189 * signal handler and/or restorer which clobberes the status32/ret
190 * to return to a bogus location in kernel mode.
192 regs->status32 |= STATUS_U_MASK;
194 return regs->r0;
196 badframe:
197 force_sig(SIGSEGV);
198 return 0;
202 * Determine which stack to use..
204 static inline void __user *get_sigframe(struct ksignal *ksig,
205 struct pt_regs *regs,
206 unsigned long framesize)
208 unsigned long sp = sigsp(regs->sp, ksig);
209 void __user *frame;
211 /* No matter what happens, 'sp' must be word
212 * aligned otherwise nasty things could happen
215 /* ATPCS B01 mandates 8-byte alignment */
216 frame = (void __user *)((sp - framesize) & ~7);
218 /* Check that we can actually write to the signal frame */
219 if (!access_ok(frame, framesize))
220 frame = NULL;
222 return frame;
225 static int
226 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
228 struct rt_sigframe __user *sf;
229 unsigned int magic = 0;
230 int err = 0;
232 sf = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
233 if (!sf)
234 return 1;
237 * w/o SA_SIGINFO, struct ucontext is partially populated (only
238 * uc_mcontext/uc_sigmask) for kernel's normal user state preservation
239 * during signal handler execution. This works for SA_SIGINFO as well
240 * although the semantics are now overloaded (the same reg state can be
241 * inspected by userland: but are they allowed to fiddle with it ?
243 err |= stash_usr_regs(sf, regs, set);
246 * SA_SIGINFO requires 3 args to signal handler:
247 * #1: sig-no (common to any handler)
248 * #2: struct siginfo
249 * #3: struct ucontext (completely populated)
251 if (unlikely(ksig->ka.sa.sa_flags & SA_SIGINFO)) {
252 err |= copy_siginfo_to_user(&sf->info, &ksig->info);
253 err |= __put_user(0, &sf->uc.uc_flags);
254 err |= __put_user(NULL, &sf->uc.uc_link);
255 err |= __save_altstack(&sf->uc.uc_stack, regs->sp);
257 /* setup args 2 and 3 for user mode handler */
258 regs->r1 = (unsigned long)&sf->info;
259 regs->r2 = (unsigned long)&sf->uc;
262 * small optim to avoid unconditonally calling do_sigaltstack
263 * in sigreturn path, now that we only have rt_sigreturn
265 magic = MAGIC_SIGALTSTK;
268 err |= __put_user(magic, &sf->sigret_magic);
269 if (err)
270 return err;
272 /* #1 arg to the user Signal handler */
273 regs->r0 = ksig->sig;
275 /* setup PC of user space signal handler */
276 regs->ret = (unsigned long)ksig->ka.sa.sa_handler;
279 * handler returns using sigreturn stub provided already by userpsace
280 * If not, nuke the process right away
282 if(!(ksig->ka.sa.sa_flags & SA_RESTORER))
283 return 1;
285 regs->blink = (unsigned long)ksig->ka.sa.sa_restorer;
287 /* User Stack for signal handler will be above the frame just carved */
288 regs->sp = (unsigned long)sf;
291 * Bug 94183, Clear the DE bit, so that when signal handler
292 * starts to run, it doesn't use BTA
294 regs->status32 &= ~STATUS_DE_MASK;
295 regs->status32 |= STATUS_L_MASK;
297 return err;
300 static void arc_restart_syscall(struct k_sigaction *ka, struct pt_regs *regs)
302 switch (regs->r0) {
303 case -ERESTART_RESTARTBLOCK:
304 case -ERESTARTNOHAND:
306 * ERESTARTNOHAND means that the syscall should
307 * only be restarted if there was no handler for
308 * the signal, and since we only get here if there
309 * is a handler, we don't restart
311 regs->r0 = -EINTR; /* ERESTART_xxx is internal */
312 break;
314 case -ERESTARTSYS:
316 * ERESTARTSYS means to restart the syscall if
317 * there is no handler or the handler was
318 * registered with SA_RESTART
320 if (!(ka->sa.sa_flags & SA_RESTART)) {
321 regs->r0 = -EINTR;
322 break;
324 /* fallthrough */
326 case -ERESTARTNOINTR:
328 * ERESTARTNOINTR means that the syscall should
329 * be called again after the signal handler returns.
330 * Setup reg state just as it was before doing the trap
331 * r0 has been clobbered with sys call ret code thus it
332 * needs to be reloaded with orig first arg to syscall
333 * in orig_r0. Rest of relevant reg-file:
334 * r8 (syscall num) and (r1 - r7) will be reset to
335 * their orig user space value when we ret from kernel
337 regs->r0 = regs->orig_r0;
338 regs->ret -= is_isa_arcv2() ? 2 : 4;
339 break;
344 * OK, we're invoking a handler
346 static void
347 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
349 sigset_t *oldset = sigmask_to_save();
350 int failed;
352 /* Set up the stack frame */
353 failed = setup_rt_frame(ksig, oldset, regs);
355 signal_setup_done(failed, ksig, 0);
358 void do_signal(struct pt_regs *regs)
360 struct ksignal ksig;
361 int restart_scall;
363 restart_scall = in_syscall(regs) && syscall_restartable(regs);
365 if (get_signal(&ksig)) {
366 if (restart_scall) {
367 arc_restart_syscall(&ksig.ka, regs);
368 syscall_wont_restart(regs); /* No more restarts */
370 handle_signal(&ksig, regs);
371 return;
374 if (restart_scall) {
375 /* No handler for syscall: restart it */
376 if (regs->r0 == -ERESTARTNOHAND ||
377 regs->r0 == -ERESTARTSYS || regs->r0 == -ERESTARTNOINTR) {
378 regs->r0 = regs->orig_r0;
379 regs->ret -= is_isa_arcv2() ? 2 : 4;
380 } else if (regs->r0 == -ERESTART_RESTARTBLOCK) {
381 regs->r8 = __NR_restart_syscall;
382 regs->ret -= is_isa_arcv2() ? 2 : 4;
384 syscall_wont_restart(regs); /* No more restarts */
387 /* If there's no signal to deliver, restore the saved sigmask back */
388 restore_saved_sigmask();
391 void do_notify_resume(struct pt_regs *regs)
394 * ASM glue gaurantees that this is only called when returning to
395 * user mode
397 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
398 tracehook_notify_resume(regs);