x86, efi: Set runtime_version to the EFI spec revision
[linux/fpc-iii.git] / arch / sh / kernel / signal_32.c
blob2f1f65356c0c39005a605c77f285e614019d387b
1 /*
2 * linux/arch/sh/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/ptrace.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/tty.h>
22 #include <linux/elf.h>
23 #include <linux/personality.h>
24 #include <linux/binfmts.h>
25 #include <linux/io.h>
26 #include <linux/tracehook.h>
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/cacheflush.h>
31 #include <asm/syscalls.h>
32 #include <asm/fpu.h>
34 struct fdpic_func_descriptor {
35 unsigned long text;
36 unsigned long GOT;
40 * The following define adds a 64 byte gap between the signal
41 * stack frame and previous contents of the stack. This allows
42 * frame unwinding in a function epilogue but only if a frame
43 * pointer is used in the function. This is necessary because
44 * current gcc compilers (<4.3) do not generate unwind info on
45 * SH for function epilogues.
47 #define UNWINDGUARD 64
50 * Atomically swap in the new signal mask, and wait for a signal.
52 asmlinkage int
53 sys_sigsuspend(old_sigset_t mask)
55 sigset_t blocked;
56 siginitset(&blocked, mask);
57 return sigsuspend(&blocked);
60 asmlinkage int
61 sys_sigaction(int sig, const struct old_sigaction __user *act,
62 struct old_sigaction __user *oact)
64 struct k_sigaction new_ka, old_ka;
65 int ret;
67 if (act) {
68 old_sigset_t mask;
69 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
70 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
71 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
72 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
73 __get_user(mask, &act->sa_mask))
74 return -EFAULT;
75 siginitset(&new_ka.sa.sa_mask, mask);
78 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
80 if (!ret && oact) {
81 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
82 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
83 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
84 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
85 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
86 return -EFAULT;
89 return ret;
92 asmlinkage int
93 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
94 unsigned long r6, unsigned long r7,
95 struct pt_regs __regs)
97 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
99 return do_sigaltstack(uss, uoss, regs->regs[15]);
104 * Do a signal return; undo the signal stack.
107 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
108 #if defined(CONFIG_CPU_SH2)
109 #define TRAP_NOARG 0xc320 /* Syscall w/no args (NR in R3) */
110 #else
111 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) */
112 #endif
113 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
115 struct sigframe
117 struct sigcontext sc;
118 unsigned long extramask[_NSIG_WORDS-1];
119 u16 retcode[8];
122 struct rt_sigframe
124 struct siginfo info;
125 struct ucontext uc;
126 u16 retcode[8];
129 #ifdef CONFIG_SH_FPU
130 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
132 struct task_struct *tsk = current;
134 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
135 return 0;
137 set_used_math();
138 return __copy_from_user(&tsk->thread.xstate->hardfpu, &sc->sc_fpregs[0],
139 sizeof(long)*(16*2+2));
142 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
143 struct pt_regs *regs)
145 struct task_struct *tsk = current;
147 if (!(boot_cpu_data.flags & CPU_HAS_FPU))
148 return 0;
150 if (!used_math())
151 return __put_user(0, &sc->sc_ownedfp);
153 if (__put_user(1, &sc->sc_ownedfp))
154 return -EFAULT;
156 /* This will cause a "finit" to be triggered by the next
157 attempted FPU operation by the 'current' process.
159 clear_used_math();
161 unlazy_fpu(tsk, regs);
162 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.xstate->hardfpu,
163 sizeof(long)*(16*2+2));
165 #endif /* CONFIG_SH_FPU */
167 static int
168 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
170 unsigned int err = 0;
172 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
173 COPY(regs[1]);
174 COPY(regs[2]); COPY(regs[3]);
175 COPY(regs[4]); COPY(regs[5]);
176 COPY(regs[6]); COPY(regs[7]);
177 COPY(regs[8]); COPY(regs[9]);
178 COPY(regs[10]); COPY(regs[11]);
179 COPY(regs[12]); COPY(regs[13]);
180 COPY(regs[14]); COPY(regs[15]);
181 COPY(gbr); COPY(mach);
182 COPY(macl); COPY(pr);
183 COPY(sr); COPY(pc);
184 #undef COPY
186 #ifdef CONFIG_SH_FPU
187 if (boot_cpu_data.flags & CPU_HAS_FPU) {
188 int owned_fp;
189 struct task_struct *tsk = current;
191 regs->sr |= SR_FD; /* Release FPU */
192 clear_fpu(tsk, regs);
193 clear_used_math();
194 err |= __get_user (owned_fp, &sc->sc_ownedfp);
195 if (owned_fp)
196 err |= restore_sigcontext_fpu(sc);
198 #endif
200 regs->tra = -1; /* disable syscall checks */
201 err |= __get_user(*r0_p, &sc->sc_regs[0]);
202 return err;
205 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
206 unsigned long r6, unsigned long r7,
207 struct pt_regs __regs)
209 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
210 struct sigframe __user *frame = (struct sigframe __user *)regs->regs[15];
211 sigset_t set;
212 int r0;
214 /* Always make any pending restarted system calls return -EINTR */
215 current_thread_info()->restart_block.fn = do_no_restart_syscall;
217 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
218 goto badframe;
220 if (__get_user(set.sig[0], &frame->sc.oldmask)
221 || (_NSIG_WORDS > 1
222 && __copy_from_user(&set.sig[1], &frame->extramask,
223 sizeof(frame->extramask))))
224 goto badframe;
226 set_current_blocked(&set);
228 if (restore_sigcontext(regs, &frame->sc, &r0))
229 goto badframe;
230 return r0;
232 badframe:
233 force_sig(SIGSEGV, current);
234 return 0;
237 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
238 unsigned long r6, unsigned long r7,
239 struct pt_regs __regs)
241 struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
242 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->regs[15];
243 sigset_t set;
244 int r0;
246 /* Always make any pending restarted system calls return -EINTR */
247 current_thread_info()->restart_block.fn = do_no_restart_syscall;
249 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
250 goto badframe;
252 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
253 goto badframe;
255 set_current_blocked(&set);
257 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
258 goto badframe;
260 if (do_sigaltstack(&frame->uc.uc_stack, NULL,
261 regs->regs[15]) == -EFAULT)
262 goto badframe;
264 return r0;
266 badframe:
267 force_sig(SIGSEGV, current);
268 return 0;
272 * Set up a signal frame.
275 static int
276 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
277 unsigned long mask)
279 int err = 0;
281 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
282 COPY(regs[0]); COPY(regs[1]);
283 COPY(regs[2]); COPY(regs[3]);
284 COPY(regs[4]); COPY(regs[5]);
285 COPY(regs[6]); COPY(regs[7]);
286 COPY(regs[8]); COPY(regs[9]);
287 COPY(regs[10]); COPY(regs[11]);
288 COPY(regs[12]); COPY(regs[13]);
289 COPY(regs[14]); COPY(regs[15]);
290 COPY(gbr); COPY(mach);
291 COPY(macl); COPY(pr);
292 COPY(sr); COPY(pc);
293 #undef COPY
295 #ifdef CONFIG_SH_FPU
296 err |= save_sigcontext_fpu(sc, regs);
297 #endif
299 /* non-iBCS2 extensions.. */
300 err |= __put_user(mask, &sc->oldmask);
302 return err;
306 * Determine which stack to use..
308 static inline void __user *
309 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
311 if (ka->sa.sa_flags & SA_ONSTACK) {
312 if (sas_ss_flags(sp) == 0)
313 sp = current->sas_ss_sp + current->sas_ss_size;
316 return (void __user *)((sp - (frame_size+UNWINDGUARD)) & -8ul);
319 /* These symbols are defined with the addresses in the vsyscall page.
320 See vsyscall-trapa.S. */
321 extern void __kernel_sigreturn(void);
322 extern void __kernel_rt_sigreturn(void);
324 static int setup_frame(int sig, struct k_sigaction *ka,
325 sigset_t *set, struct pt_regs *regs)
327 struct sigframe __user *frame;
328 int err = 0;
329 int signal;
331 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
333 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
334 goto give_sigsegv;
336 signal = current_thread_info()->exec_domain
337 && current_thread_info()->exec_domain->signal_invmap
338 && sig < 32
339 ? current_thread_info()->exec_domain->signal_invmap[sig]
340 : sig;
342 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
344 if (_NSIG_WORDS > 1)
345 err |= __copy_to_user(frame->extramask, &set->sig[1],
346 sizeof(frame->extramask));
348 /* Set up to return from userspace. If provided, use a stub
349 already in userspace. */
350 if (ka->sa.sa_flags & SA_RESTORER) {
351 regs->pr = (unsigned long) ka->sa.sa_restorer;
352 #ifdef CONFIG_VSYSCALL
353 } else if (likely(current->mm->context.vdso)) {
354 regs->pr = VDSO_SYM(&__kernel_sigreturn);
355 #endif
356 } else {
357 /* Generate return code (system call to sigreturn) */
358 err |= __put_user(MOVW(7), &frame->retcode[0]);
359 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
360 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
361 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
362 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
363 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
364 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
365 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
366 regs->pr = (unsigned long) frame->retcode;
367 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
370 if (err)
371 goto give_sigsegv;
373 /* Set up registers for signal handler */
374 regs->regs[15] = (unsigned long) frame;
375 regs->regs[4] = signal; /* Arg for signal handler */
376 regs->regs[5] = 0;
377 regs->regs[6] = (unsigned long) &frame->sc;
379 if (current->personality & FDPIC_FUNCPTRS) {
380 struct fdpic_func_descriptor __user *funcptr =
381 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
383 err |= __get_user(regs->pc, &funcptr->text);
384 err |= __get_user(regs->regs[12], &funcptr->GOT);
385 } else
386 regs->pc = (unsigned long)ka->sa.sa_handler;
388 if (err)
389 goto give_sigsegv;
391 set_fs(USER_DS);
393 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
394 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
396 return 0;
398 give_sigsegv:
399 force_sigsegv(sig, current);
400 return -EFAULT;
403 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
404 sigset_t *set, struct pt_regs *regs)
406 struct rt_sigframe __user *frame;
407 int err = 0;
408 int signal;
410 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
412 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
413 goto give_sigsegv;
415 signal = current_thread_info()->exec_domain
416 && current_thread_info()->exec_domain->signal_invmap
417 && sig < 32
418 ? current_thread_info()->exec_domain->signal_invmap[sig]
419 : sig;
421 err |= copy_siginfo_to_user(&frame->info, info);
423 /* Create the ucontext. */
424 err |= __put_user(0, &frame->uc.uc_flags);
425 err |= __put_user(NULL, &frame->uc.uc_link);
426 err |= __put_user((void *)current->sas_ss_sp,
427 &frame->uc.uc_stack.ss_sp);
428 err |= __put_user(sas_ss_flags(regs->regs[15]),
429 &frame->uc.uc_stack.ss_flags);
430 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
431 err |= setup_sigcontext(&frame->uc.uc_mcontext,
432 regs, set->sig[0]);
433 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
435 /* Set up to return from userspace. If provided, use a stub
436 already in userspace. */
437 if (ka->sa.sa_flags & SA_RESTORER) {
438 regs->pr = (unsigned long) ka->sa.sa_restorer;
439 #ifdef CONFIG_VSYSCALL
440 } else if (likely(current->mm->context.vdso)) {
441 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
442 #endif
443 } else {
444 /* Generate return code (system call to rt_sigreturn) */
445 err |= __put_user(MOVW(7), &frame->retcode[0]);
446 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
447 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
448 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
449 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
450 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
451 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
452 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
453 regs->pr = (unsigned long) frame->retcode;
454 flush_icache_range(regs->pr, regs->pr + sizeof(frame->retcode));
457 if (err)
458 goto give_sigsegv;
460 /* Set up registers for signal handler */
461 regs->regs[15] = (unsigned long) frame;
462 regs->regs[4] = signal; /* Arg for signal handler */
463 regs->regs[5] = (unsigned long) &frame->info;
464 regs->regs[6] = (unsigned long) &frame->uc;
466 if (current->personality & FDPIC_FUNCPTRS) {
467 struct fdpic_func_descriptor __user *funcptr =
468 (struct fdpic_func_descriptor __user *)ka->sa.sa_handler;
470 err |= __get_user(regs->pc, &funcptr->text);
471 err |= __get_user(regs->regs[12], &funcptr->GOT);
472 } else
473 regs->pc = (unsigned long)ka->sa.sa_handler;
475 if (err)
476 goto give_sigsegv;
478 set_fs(USER_DS);
480 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
481 current->comm, task_pid_nr(current), frame, regs->pc, regs->pr);
483 return 0;
485 give_sigsegv:
486 force_sigsegv(sig, current);
487 return -EFAULT;
490 static inline void
491 handle_syscall_restart(unsigned long save_r0, struct pt_regs *regs,
492 struct sigaction *sa)
494 /* If we're not from a syscall, bail out */
495 if (regs->tra < 0)
496 return;
498 /* check for system call restart.. */
499 switch (regs->regs[0]) {
500 case -ERESTART_RESTARTBLOCK:
501 case -ERESTARTNOHAND:
502 no_system_call_restart:
503 regs->regs[0] = -EINTR;
504 break;
506 case -ERESTARTSYS:
507 if (!(sa->sa_flags & SA_RESTART))
508 goto no_system_call_restart;
509 /* fallthrough */
510 case -ERESTARTNOINTR:
511 regs->regs[0] = save_r0;
512 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
513 break;
518 * OK, we're invoking a handler
520 static void
521 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
522 struct pt_regs *regs, unsigned int save_r0)
524 sigset_t *oldset = sigmask_to_save();
525 int ret;
527 /* Set up the stack frame */
528 if (ka->sa.sa_flags & SA_SIGINFO)
529 ret = setup_rt_frame(sig, ka, info, oldset, regs);
530 else
531 ret = setup_frame(sig, ka, oldset, regs);
533 if (ret)
534 return;
535 signal_delivered(sig, info, ka, regs,
536 test_thread_flag(TIF_SINGLESTEP));
540 * Note that 'init' is a special process: it doesn't get signals it doesn't
541 * want to handle. Thus you cannot kill init even with a SIGKILL even by
542 * mistake.
544 * Note that we go through the signals twice: once to check the signals that
545 * the kernel can handle, and then we build all the user-level signal handling
546 * stack-frames in one go after that.
548 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
550 siginfo_t info;
551 int signr;
552 struct k_sigaction ka;
555 * We want the common case to go fast, which
556 * is why we may in certain cases get here from
557 * kernel mode. Just return without doing anything
558 * if so.
560 if (!user_mode(regs))
561 return;
563 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
564 if (signr > 0) {
565 handle_syscall_restart(save_r0, regs, &ka.sa);
567 /* Whee! Actually deliver the signal. */
568 handle_signal(signr, &ka, &info, regs, save_r0);
569 return;
572 /* Did we come from a system call? */
573 if (regs->tra >= 0) {
574 /* Restart the system call - no handlers present */
575 if (regs->regs[0] == -ERESTARTNOHAND ||
576 regs->regs[0] == -ERESTARTSYS ||
577 regs->regs[0] == -ERESTARTNOINTR) {
578 regs->regs[0] = save_r0;
579 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
580 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
581 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
582 regs->regs[3] = __NR_restart_syscall;
587 * If there's no signal to deliver, we just put the saved sigmask
588 * back.
590 restore_saved_sigmask();
593 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
594 unsigned long thread_info_flags)
596 /* deal with pending signal delivery */
597 if (thread_info_flags & _TIF_SIGPENDING)
598 do_signal(regs, save_r0);
600 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
601 clear_thread_flag(TIF_NOTIFY_RESUME);
602 tracehook_notify_resume(regs);