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