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