License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / arch / cris / arch-v32 / kernel / signal.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
51533b61
MS
2/*
3 * Copyright (C) 2003, Axis Communications AB.
4 */
5
6#include <linux/sched.h>
68db0cf1 7#include <linux/sched/task_stack.h>
51533b61 8#include <linux/mm.h>
5a0e3ad6 9#include <linux/slab.h>
51533b61
MS
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>
7c0f6ba6 23#include <linux/uaccess.h>
556dcee7 24#include <arch/hwregs/cpu_vect.h>
51533b61
MS
25
26extern unsigned long cris_signal_return_page;
27
51533b61
MS
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. */
38struct signal_frame {
39 struct sigcontext sc;
40 unsigned long extramask[_NSIG_WORDS - 1];
41 unsigned char retcode[8]; /* Trampoline code. */
42};
43
44struct 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
574852a2 52void do_signal(int restart, struct pt_regs *regs);
51533b61
MS
53void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
54 struct pt_regs *regs);
51533b61 55
51533b61
MS
56static int
57restore_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 */
f56141e3 63 current->restart_block.fn = do_no_restart_syscall;
51533b61
MS
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
0f72e5c0
RV
76 /* Don't perform syscall restarting */
77 regs->exs = -1;
78
51533b61
MS
79 /* Restore the old USP. */
80 err |= __get_user(old_usp, &sc->usp);
81 wrusp(old_usp);
82
83 return err;
84
85badframe:
86 return 1;
87}
88
e6a6d210 89asmlinkage int sys_sigreturn(void)
51533b61 90{
e6a6d210 91 struct pt_regs *regs = current_pt_regs();
51533b61
MS
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
f3b5e822 116 set_current_blocked(&set);
51533b61
MS
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
125badframe:
126 force_sig(SIGSEGV, current);
127 return 0;
128}
129
e6a6d210 130asmlinkage int sys_rt_sigreturn(void)
51533b61 131{
e6a6d210 132 struct pt_regs *regs = current_pt_regs();
51533b61
MS
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
f3b5e822 154 set_current_blocked(&set);
51533b61
MS
155
156 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
157 goto badframe;
158
d970e428 159 if (restore_altstack(&frame->uc.uc_stack))
574852a2 160 goto badframe;
51533b61
MS
161
162 keep_debug_flags(oldccs, oldspc, regs);
163
164 return regs->r10;
165
166badframe:
167 force_sig(SIGSEGV, current);
168 return 0;
169}
170
171/* Setup a signal frame. */
172static int
173setup_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. */
195static inline void __user *
8215ade8 196get_sigframe(struct ksignal *ksig, size_t frame_size)
51533b61 197{
8215ade8 198 unsigned long sp = sigsp(rdusp(), ksig);
51533b61
MS
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
574852a2 209 * user-mode program to return to the kernel using either a trampiline
51533b61
MS
210 * which performs the syscall sigreturn(), or a provided user-mode
211 * trampoline.
212 */
574852a2 213static int
fa019772 214setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
51533b61
MS
215{
216 int err;
217 unsigned long return_ip;
218 struct signal_frame __user *frame;
219
220 err = 0;
8215ade8 221 frame = get_sigframe(ksig, sizeof(*frame));
51533b61
MS
222
223 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
fa019772 224 return -EFAULT;
51533b61
MS
225
226 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
227
228 if (err)
fa019772 229 return -EFAULT;
51533b61
MS
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)
fa019772 237 return -EFAULT;
51533b61
MS
238
239 /*
240 * Set up to return from user-space. If provided, use a stub
241 * already located in user-space.
242 */
fa019772
RW
243 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
244 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
51533b61
MS
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)
fa019772 262 return -EFAULT;
51533b61
MS
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 */
fa019772 271 regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
51533b61 272 regs->srp = return_ip;
fa019772 273 regs->r10 = ksig->sig;
51533b61
MS
274
275 /* Actually move the USP to reflect the stacked frame. */
276 wrusp((unsigned long)frame);
277
574852a2 278 return 0;
51533b61
MS
279}
280
574852a2 281static int
fa019772 282setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
51533b61
MS
283{
284 int err;
285 unsigned long return_ip;
286 struct rt_signal_frame __user *frame;
287
288 err = 0;
8215ade8 289 frame = get_sigframe(ksig, sizeof(*frame));
51533b61
MS
290
291 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
fa019772 292 return -EFAULT;
51533b61 293
51533b61
MS
294 err |= __put_user(&frame->info, &frame->pinfo);
295 err |= __put_user(&frame->uc, &frame->puc);
fa019772 296 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
51533b61
MS
297
298 if (err)
fa019772 299 return -EFAULT;
51533b61
MS
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));
9df794d9 305 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
51533b61
MS
306
307 if (err)
fa019772 308 return -EFAULT;
51533b61
MS
309
310 /*
311 * Set up to return from user-space. If provided, use a stub
312 * already located in user-space.
313 */
fa019772
RW
314 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
315 return_ip = (unsigned long) ksig->ka.sa.sa_restorer;
51533b61
MS
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)
fa019772 336 return -EFAULT;
51533b61
MS
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 */
fa019772 347 regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
51533b61 348 regs->srp = return_ip;
fa019772 349 regs->r10 = ksig->sig;
51533b61
MS
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
574852a2 356 return 0;
51533b61
MS
357}
358
c03264a7 359/* Invoke a signal handler to, well, handle the signal. */
a610d6e6 360static inline void
fa019772 361handle_signal(int canrestart, struct ksignal *ksig, struct pt_regs *regs)
51533b61 362{
b7f9a11a 363 sigset_t *oldset = sigmask_to_save();
574852a2
JN
364 int ret;
365
51533b61
MS
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 */
fa019772 389 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
51533b61
MS
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. */
fa019772
RW
408 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
409 ret = setup_rt_frame(ksig, oldset, regs);
51533b61 410 else
fa019772 411 ret = setup_frame(ksig, oldset, regs);
51533b61 412
fa019772 413 signal_setup_done(ret, ksig, 0);
51533b61
MS
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 */
574852a2
JN
427void
428do_signal(int canrestart, struct pt_regs *regs)
51533b61 429{
fa019772 430 struct ksignal ksig;
51533b61 431
0f72e5c0
RV
432 canrestart = canrestart && ((int)regs->exs >= 0);
433
51533b61
MS
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))
574852a2 440 return;
51533b61 441
fa019772 442 if (get_signal(&ksig)) {
574852a2 443 /* Whee! Actually deliver the signal. */
fa019772 444 handle_signal(canrestart, &ksig, regs);
574852a2 445 return;
51533b61
MS
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){
39923326 458 regs->r9 = __NR_restart_syscall;
51533b61
MS
459 regs->erp -= 2;
460 }
461 }
462
574852a2
JN
463 /* if there's no signal to deliver, we just put the saved sigmask
464 * back */
51a7b448 465 restore_saved_sigmask();
51533b61
MS
466}
467
468asmlinkage void
469ugdb_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
25985edc 478 not within any configured h/w breakpoint range). Synchronize with
51533b61
MS
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
488void
489keep_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. */
520int __init
521cris_init_signal(void)
522{
5cbded58 523 u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
51533b61
MS
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);