Linux 4.13.16
[linux/fpc-iii.git] / arch / cris / arch-v10 / kernel / signal.c
blobbab4a8dd6bfda2b39b0fc4a72c543a3f0a40e4e6
1 /*
2 * linux/arch/cris/kernel/signal.c
4 * Based on arch/i386/kernel/signal.c by
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson *
8 * Ideas also taken from arch/arm.
10 * Copyright (C) 2000-2007 Axis Communications AB
12 * Authors: Bjorn Wesen (bjornw@axis.com)
16 #include <linux/sched.h>
17 #include <linux/sched/task_stack.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
28 #include <asm/processor.h>
29 #include <asm/ucontext.h>
30 #include <linux/uaccess.h>
31 #include <arch/system.h>
33 #define DEBUG_SIG 0
35 /* a syscall in Linux/CRIS is a break 13 instruction which is 2 bytes */
36 /* manipulate regs so that upon return, it will be re-executed */
38 /* We rely on that pc points to the instruction after "break 13", so the
39 * library must never do strange things like putting it in a delay slot.
41 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->irp -= 2;
43 void do_signal(int canrestart, struct pt_regs *regs);
46 * Do a signal return; undo the signal stack.
49 struct sigframe {
50 struct sigcontext sc;
51 unsigned long extramask[_NSIG_WORDS-1];
52 unsigned char retcode[8]; /* trampoline code */
55 struct rt_sigframe {
56 struct siginfo *pinfo;
57 void *puc;
58 struct siginfo info;
59 struct ucontext uc;
60 unsigned char retcode[8]; /* trampoline code */
64 static int
65 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
67 unsigned int err = 0;
68 unsigned long old_usp;
70 /* Always make any pending restarted system calls return -EINTR */
71 current->restart_block.fn = do_no_restart_syscall;
73 /* restore the regs from &sc->regs (same as sc, since regs is first)
74 * (sc is already checked for VERIFY_READ since the sigframe was
75 * checked in sys_sigreturn previously)
78 if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
79 goto badframe;
81 /* make sure the U-flag is set so user-mode cannot fool us */
83 regs->dccr |= 1 << 8;
85 /* restore the old USP as it was before we stacked the sc etc.
86 * (we cannot just pop the sigcontext since we aligned the sp and
87 * stuff after pushing it)
90 err |= __get_user(old_usp, &sc->usp);
92 wrusp(old_usp);
94 /* TODO: the other ports use regs->orig_XX to disable syscall checks
95 * after this completes, but we don't use that mechanism. maybe we can
96 * use it now ?
99 return err;
101 badframe:
102 return 1;
105 asmlinkage int sys_sigreturn(void)
107 struct pt_regs *regs = current_pt_regs();
108 struct sigframe __user *frame = (struct sigframe *)rdusp();
109 sigset_t set;
112 * Since we stacked the signal on a dword boundary,
113 * then frame should be dword aligned here. If it's
114 * not, then the user is trying to mess with us.
116 if (((long)frame) & 3)
117 goto badframe;
119 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
120 goto badframe;
121 if (__get_user(set.sig[0], &frame->sc.oldmask)
122 || (_NSIG_WORDS > 1
123 && __copy_from_user(&set.sig[1], frame->extramask,
124 sizeof(frame->extramask))))
125 goto badframe;
127 set_current_blocked(&set);
129 if (restore_sigcontext(regs, &frame->sc))
130 goto badframe;
132 /* TODO: SIGTRAP when single-stepping as in arm ? */
134 return regs->r10;
136 badframe:
137 force_sig(SIGSEGV, current);
138 return 0;
141 asmlinkage int sys_rt_sigreturn(void)
143 struct pt_regs *regs = current_pt_regs();
144 struct rt_sigframe __user *frame = (struct rt_sigframe *)rdusp();
145 sigset_t set;
148 * Since we stacked the signal on a dword boundary,
149 * then frame should be dword aligned here. If it's
150 * not, then the user is trying to mess with us.
152 if (((long)frame) & 3)
153 goto badframe;
155 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
156 goto badframe;
157 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
158 goto badframe;
160 set_current_blocked(&set);
162 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
163 goto badframe;
165 if (restore_altstack(&frame->uc.uc_stack))
166 goto badframe;
168 return regs->r10;
170 badframe:
171 force_sig(SIGSEGV, current);
172 return 0;
176 * Set up a signal frame.
179 static int setup_sigcontext(struct sigcontext __user *sc,
180 struct pt_regs *regs, unsigned long mask)
182 int err = 0;
183 unsigned long usp = rdusp();
185 /* copy the regs. they are first in sc so we can use sc directly */
187 err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
189 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
190 the signal handler. The frametype will be restored to its previous
191 value in restore_sigcontext. */
192 regs->frametype = CRIS_FRAME_NORMAL;
194 /* then some other stuff */
196 err |= __put_user(mask, &sc->oldmask);
198 err |= __put_user(usp, &sc->usp);
200 return err;
203 /* Figure out where we want to put the new signal frame
204 * - usually on the stack. */
206 static inline void __user *
207 get_sigframe(struct ksignal *ksig, size_t frame_size)
209 unsigned long sp = sigsp(rdusp(), ksig);
211 /* make sure the frame is dword-aligned */
213 sp &= ~3;
215 return (void __user*)(sp - frame_size);
218 /* grab and setup a signal frame.
220 * basically we stack a lot of state info, and arrange for the
221 * user-mode program to return to the kernel using either a
222 * trampoline which performs the syscall sigreturn, or a provided
223 * user-mode trampoline.
226 static int setup_frame(struct ksignal *ksig, sigset_t *set,
227 struct pt_regs *regs)
229 struct sigframe __user *frame;
230 unsigned long return_ip;
231 int err = 0;
233 frame = get_sigframe(ksig, sizeof(*frame));
235 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
236 return -EFAULT;
238 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
239 if (err)
240 return -EFAULT;
242 if (_NSIG_WORDS > 1) {
243 err |= __copy_to_user(frame->extramask, &set->sig[1],
244 sizeof(frame->extramask));
246 if (err)
247 return -EFAULT;
249 /* Set up to return from userspace. If provided, use a stub
250 already in userspace. */
251 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
252 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
253 } else {
254 /* trampoline - the desired return ip is the retcode itself */
255 return_ip = (unsigned long)&frame->retcode;
256 /* This is movu.w __NR_sigreturn, r9; break 13; */
257 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
258 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
259 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
262 if (err)
263 return -EFAULT;
265 /* Set up registers for signal handler */
267 regs->irp = (unsigned long) ksig->ka.sa.sa_handler; /* what we enter NOW */
268 regs->srp = return_ip; /* what we enter LATER */
269 regs->r10 = ksig->sig; /* first argument is signo */
271 /* actually move the usp to reflect the stacked frame */
273 wrusp((unsigned long)frame);
275 return 0;
278 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
279 struct pt_regs *regs)
281 struct rt_sigframe __user *frame;
282 unsigned long return_ip;
283 int err = 0;
285 frame = get_sigframe(ksig, sizeof(*frame));
287 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288 return -EFAULT;
290 err |= __put_user(&frame->info, &frame->pinfo);
291 err |= __put_user(&frame->uc, &frame->puc);
292 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
293 if (err)
294 return -EFAULT;
296 /* Clear all the bits of the ucontext we don't use. */
297 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
299 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
301 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
303 err |= __save_altstack(&frame->uc.uc_stack, rdusp());
305 if (err)
306 return -EFAULT;
308 /* Set up to return from userspace. If provided, use a stub
309 already in userspace. */
310 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
311 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
312 } else {
313 /* trampoline - the desired return ip is the retcode itself */
314 return_ip = (unsigned long)&frame->retcode;
315 /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
316 err |= __put_user(0x9c5f, (short __user *)(frame->retcode+0));
317 err |= __put_user(__NR_rt_sigreturn,
318 (short __user *)(frame->retcode+2));
319 err |= __put_user(0xe93d, (short __user *)(frame->retcode+4));
322 if (err)
323 return -EFAULT;
325 /* Set up registers for signal handler */
327 /* What we enter NOW */
328 regs->irp = (unsigned long) ksig->ka.sa.sa_handler;
329 /* What we enter LATER */
330 regs->srp = return_ip;
331 /* First argument is signo */
332 regs->r10 = ksig->sig;
333 /* Second argument is (siginfo_t *) */
334 regs->r11 = (unsigned long)&frame->info;
335 /* Third argument is unused */
336 regs->r12 = 0;
338 /* Actually move the usp to reflect the stacked frame */
339 wrusp((unsigned long)frame);
341 return 0;
345 * OK, we're invoking a handler
348 static inline void handle_signal(int canrestart, struct ksignal *ksig,
349 struct pt_regs *regs)
351 sigset_t *oldset = sigmask_to_save();
352 int ret;
354 /* Are we from a system call? */
355 if (canrestart) {
356 /* If so, check system call restarting.. */
357 switch (regs->r10) {
358 case -ERESTART_RESTARTBLOCK:
359 case -ERESTARTNOHAND:
360 /* ERESTARTNOHAND means that the syscall should
361 * only be restarted if there was no handler for
362 * the signal, and since we only get here if there
363 * is a handler, we don't restart */
364 regs->r10 = -EINTR;
365 break;
366 case -ERESTARTSYS:
367 /* ERESTARTSYS means to restart the syscall if
368 * there is no handler or the handler was
369 * registered with SA_RESTART */
370 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
371 regs->r10 = -EINTR;
372 break;
374 /* fallthrough */
375 case -ERESTARTNOINTR:
376 /* ERESTARTNOINTR means that the syscall should
377 * be called again after the signal handler returns. */
378 RESTART_CRIS_SYS(regs);
382 /* Set up the stack frame */
383 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
384 ret = setup_rt_frame(ksig, oldset, regs);
385 else
386 ret = setup_frame(ksig, oldset, regs);
388 signal_setup_done(ret, ksig, 0);
392 * Note that 'init' is a special process: it doesn't get signals it doesn't
393 * want to handle. Thus you cannot kill init even with a SIGKILL even by
394 * mistake.
396 * Also note that the regs structure given here as an argument, is the latest
397 * pushed pt_regs. It may or may not be the same as the first pushed registers
398 * when the initial usermode->kernelmode transition took place. Therefore
399 * we can use user_mode(regs) to see if we came directly from kernel or user
400 * mode below.
403 void do_signal(int canrestart, struct pt_regs *regs)
405 struct ksignal ksig;
408 * We want the common case to go fast, which
409 * is why we may in certain cases get here from
410 * kernel mode. Just return without doing anything
411 * if so.
413 if (!user_mode(regs))
414 return;
416 if (get_signal(&ksig)) {
417 /* Whee! Actually deliver the signal. */
418 handle_signal(canrestart, &ksig, regs);
419 return;
422 /* Did we come from a system call? */
423 if (canrestart) {
424 /* Restart the system call - no handlers present */
425 if (regs->r10 == -ERESTARTNOHAND ||
426 regs->r10 == -ERESTARTSYS ||
427 regs->r10 == -ERESTARTNOINTR) {
428 RESTART_CRIS_SYS(regs);
430 if (regs->r10 == -ERESTART_RESTARTBLOCK) {
431 regs->r9 = __NR_restart_syscall;
432 regs->irp -= 2;
436 /* if there's no signal to deliver, we just put the saved sigmask
437 * back */
438 restore_saved_sigmask();