]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/sh/kernel/signal.c
sh: Initial vsyscall page support.
[mirror_ubuntu-bionic-kernel.git] / arch / sh / kernel / signal.c
1 /*
2 * linux/arch/sh/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 *
8 * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
9 *
10 */
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/smp_lock.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/elf.h>
24 #include <linux/personality.h>
25 #include <linux/binfmts.h>
26
27 #include <asm/ucontext.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/cacheflush.h>
31
32 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
33
34 /*
35 * Atomically swap in the new signal mask, and wait for a signal.
36 */
37 asmlinkage int
38 sys_sigsuspend(old_sigset_t mask,
39 unsigned long r5, unsigned long r6, unsigned long r7,
40 struct pt_regs regs)
41 {
42 mask &= _BLOCKABLE;
43 spin_lock_irq(&current->sighand->siglock);
44 current->saved_sigmask = current->blocked;
45 siginitset(&current->blocked, mask);
46 recalc_sigpending();
47 spin_unlock_irq(&current->sighand->siglock);
48
49 current->state = TASK_INTERRUPTIBLE;
50 schedule();
51 set_thread_flag(TIF_RESTORE_SIGMASK);
52 return -ERESTARTNOHAND;
53 }
54
55 asmlinkage int
56 sys_sigaction(int sig, const struct old_sigaction __user *act,
57 struct old_sigaction __user *oact)
58 {
59 struct k_sigaction new_ka, old_ka;
60 int ret;
61
62 if (act) {
63 old_sigset_t mask;
64 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
65 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
66 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
67 return -EFAULT;
68 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
69 __get_user(mask, &act->sa_mask);
70 siginitset(&new_ka.sa.sa_mask, mask);
71 }
72
73 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
74
75 if (!ret && oact) {
76 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
77 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
78 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
79 return -EFAULT;
80 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
81 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
82 }
83
84 return ret;
85 }
86
87 asmlinkage int
88 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
89 unsigned long r6, unsigned long r7,
90 struct pt_regs regs)
91 {
92 return do_sigaltstack(uss, uoss, regs.regs[15]);
93 }
94
95
96 /*
97 * Do a signal return; undo the signal stack.
98 */
99
100 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
101 #define TRAP16 0xc310 /* Syscall w/no args (NR in R3) */
102 #define OR_R0_R0 0x200b /* or r0,r0 (insert to avoid hardware bug) */
103
104 struct sigframe
105 {
106 struct sigcontext sc;
107 unsigned long extramask[_NSIG_WORDS-1];
108 u16 retcode[8];
109 };
110
111 struct rt_sigframe
112 {
113 struct siginfo info;
114 struct ucontext uc;
115 u16 retcode[8];
116 };
117
118 #ifdef CONFIG_SH_FPU
119 static inline int restore_sigcontext_fpu(struct sigcontext __user *sc)
120 {
121 struct task_struct *tsk = current;
122
123 if (!(cpu_data->flags & CPU_HAS_FPU))
124 return 0;
125
126 set_used_math();
127 return __copy_from_user(&tsk->thread.fpu.hard, &sc->sc_fpregs[0],
128 sizeof(long)*(16*2+2));
129 }
130
131 static inline int save_sigcontext_fpu(struct sigcontext __user *sc,
132 struct pt_regs *regs)
133 {
134 struct task_struct *tsk = current;
135
136 if (!(cpu_data->flags & CPU_HAS_FPU))
137 return 0;
138
139 if (!used_math()) {
140 __put_user(0, &sc->sc_ownedfp);
141 return 0;
142 }
143
144 __put_user(1, &sc->sc_ownedfp);
145
146 /* This will cause a "finit" to be triggered by the next
147 attempted FPU operation by the 'current' process.
148 */
149 clear_used_math();
150
151 unlazy_fpu(tsk, regs);
152 return __copy_to_user(&sc->sc_fpregs[0], &tsk->thread.fpu.hard,
153 sizeof(long)*(16*2+2));
154 }
155 #endif /* CONFIG_SH_FPU */
156
157 static int
158 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *r0_p)
159 {
160 unsigned int err = 0;
161
162 #define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
163 COPY(regs[1]);
164 COPY(regs[2]); COPY(regs[3]);
165 COPY(regs[4]); COPY(regs[5]);
166 COPY(regs[6]); COPY(regs[7]);
167 COPY(regs[8]); COPY(regs[9]);
168 COPY(regs[10]); COPY(regs[11]);
169 COPY(regs[12]); COPY(regs[13]);
170 COPY(regs[14]); COPY(regs[15]);
171 COPY(gbr); COPY(mach);
172 COPY(macl); COPY(pr);
173 COPY(sr); COPY(pc);
174 #undef COPY
175
176 #ifdef CONFIG_SH_FPU
177 if (cpu_data->flags & CPU_HAS_FPU) {
178 int owned_fp;
179 struct task_struct *tsk = current;
180
181 regs->sr |= SR_FD; /* Release FPU */
182 clear_fpu(tsk, regs);
183 clear_used_math();
184 __get_user (owned_fp, &sc->sc_ownedfp);
185 if (owned_fp)
186 err |= restore_sigcontext_fpu(sc);
187 }
188 #endif
189
190 regs->tra = -1; /* disable syscall checks */
191 err |= __get_user(*r0_p, &sc->sc_regs[0]);
192 return err;
193 }
194
195 asmlinkage int sys_sigreturn(unsigned long r4, unsigned long r5,
196 unsigned long r6, unsigned long r7,
197 struct pt_regs regs)
198 {
199 struct sigframe __user *frame = (struct sigframe __user *)regs.regs[15];
200 sigset_t set;
201 int r0;
202
203 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
204 goto badframe;
205
206 if (__get_user(set.sig[0], &frame->sc.oldmask)
207 || (_NSIG_WORDS > 1
208 && __copy_from_user(&set.sig[1], &frame->extramask,
209 sizeof(frame->extramask))))
210 goto badframe;
211
212 sigdelsetmask(&set, ~_BLOCKABLE);
213
214 spin_lock_irq(&current->sighand->siglock);
215 current->blocked = set;
216 recalc_sigpending();
217 spin_unlock_irq(&current->sighand->siglock);
218
219 if (restore_sigcontext(&regs, &frame->sc, &r0))
220 goto badframe;
221 return r0;
222
223 badframe:
224 force_sig(SIGSEGV, current);
225 return 0;
226 }
227
228 asmlinkage int sys_rt_sigreturn(unsigned long r4, unsigned long r5,
229 unsigned long r6, unsigned long r7,
230 struct pt_regs regs)
231 {
232 struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs.regs[15];
233 sigset_t set;
234 stack_t st;
235 int r0;
236
237 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
238 goto badframe;
239
240 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
241 goto badframe;
242
243 sigdelsetmask(&set, ~_BLOCKABLE);
244 spin_lock_irq(&current->sighand->siglock);
245 current->blocked = set;
246 recalc_sigpending();
247 spin_unlock_irq(&current->sighand->siglock);
248
249 if (restore_sigcontext(&regs, &frame->uc.uc_mcontext, &r0))
250 goto badframe;
251
252 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
253 goto badframe;
254 /* It is more difficult to avoid calling this function than to
255 call it and ignore errors. */
256 do_sigaltstack(&st, NULL, regs.regs[15]);
257
258 return r0;
259
260 badframe:
261 force_sig(SIGSEGV, current);
262 return 0;
263 }
264
265 /*
266 * Set up a signal frame.
267 */
268
269 static int
270 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
271 unsigned long mask)
272 {
273 int err = 0;
274
275 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
276 COPY(regs[0]); COPY(regs[1]);
277 COPY(regs[2]); COPY(regs[3]);
278 COPY(regs[4]); COPY(regs[5]);
279 COPY(regs[6]); COPY(regs[7]);
280 COPY(regs[8]); COPY(regs[9]);
281 COPY(regs[10]); COPY(regs[11]);
282 COPY(regs[12]); COPY(regs[13]);
283 COPY(regs[14]); COPY(regs[15]);
284 COPY(gbr); COPY(mach);
285 COPY(macl); COPY(pr);
286 COPY(sr); COPY(pc);
287 #undef COPY
288
289 #ifdef CONFIG_SH_FPU
290 err |= save_sigcontext_fpu(sc, regs);
291 #endif
292
293 /* non-iBCS2 extensions.. */
294 err |= __put_user(mask, &sc->oldmask);
295
296 return err;
297 }
298
299 /*
300 * Determine which stack to use..
301 */
302 static inline void __user *
303 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
304 {
305 if (ka->sa.sa_flags & SA_ONSTACK) {
306 if (sas_ss_flags(sp) == 0)
307 sp = current->sas_ss_sp + current->sas_ss_size;
308 }
309
310 return (void __user *)((sp - frame_size) & -8ul);
311 }
312
313 /* These symbols are defined with the addresses in the vsyscall page.
314 See vsyscall-trapa.S. */
315 extern void __user __kernel_sigreturn;
316 extern void __user __kernel_rt_sigreturn;
317
318 static int setup_frame(int sig, struct k_sigaction *ka,
319 sigset_t *set, struct pt_regs *regs)
320 {
321 struct sigframe __user *frame;
322 int err = 0;
323 int signal;
324
325 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
326
327 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
328 goto give_sigsegv;
329
330 signal = current_thread_info()->exec_domain
331 && current_thread_info()->exec_domain->signal_invmap
332 && sig < 32
333 ? current_thread_info()->exec_domain->signal_invmap[sig]
334 : sig;
335
336 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
337
338 if (_NSIG_WORDS > 1)
339 err |= __copy_to_user(frame->extramask, &set->sig[1],
340 sizeof(frame->extramask));
341
342 /* Set up to return from userspace. If provided, use a stub
343 already in userspace. */
344 if (ka->sa.sa_flags & SA_RESTORER) {
345 regs->pr = (unsigned long) ka->sa.sa_restorer;
346 #ifdef CONFIG_VSYSCALL
347 } else if (likely(current->mm->context.vdso)) {
348 regs->pr = VDSO_SYM(&__kernel_sigreturn);
349 #endif
350 } else {
351 /* Generate return code (system call to sigreturn) */
352 err |= __put_user(MOVW(7), &frame->retcode[0]);
353 err |= __put_user(TRAP16, &frame->retcode[1]);
354 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
355 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
356 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
357 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
358 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
359 err |= __put_user((__NR_sigreturn), &frame->retcode[7]);
360 regs->pr = (unsigned long) frame->retcode;
361 }
362
363 if (err)
364 goto give_sigsegv;
365
366 /* Set up registers for signal handler */
367 regs->regs[15] = (unsigned long) frame;
368 regs->regs[4] = signal; /* Arg for signal handler */
369 regs->regs[5] = 0;
370 regs->regs[6] = (unsigned long) &frame->sc;
371 regs->pc = (unsigned long) ka->sa.sa_handler;
372
373 set_fs(USER_DS);
374
375 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
376 current->comm, current->pid, frame, regs->pc, regs->pr);
377
378 flush_cache_sigtramp(regs->pr);
379
380 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
381 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
382
383 return 0;
384
385 give_sigsegv:
386 force_sigsegv(sig, current);
387 return -EFAULT;
388 }
389
390 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
391 sigset_t *set, struct pt_regs *regs)
392 {
393 struct rt_sigframe __user *frame;
394 int err = 0;
395 int signal;
396
397 frame = get_sigframe(ka, regs->regs[15], sizeof(*frame));
398
399 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
400 goto give_sigsegv;
401
402 signal = current_thread_info()->exec_domain
403 && current_thread_info()->exec_domain->signal_invmap
404 && sig < 32
405 ? current_thread_info()->exec_domain->signal_invmap[sig]
406 : sig;
407
408 err |= copy_siginfo_to_user(&frame->info, info);
409
410 /* Create the ucontext. */
411 err |= __put_user(0, &frame->uc.uc_flags);
412 err |= __put_user(0, &frame->uc.uc_link);
413 err |= __put_user((void *)current->sas_ss_sp,
414 &frame->uc.uc_stack.ss_sp);
415 err |= __put_user(sas_ss_flags(regs->regs[15]),
416 &frame->uc.uc_stack.ss_flags);
417 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
418 err |= setup_sigcontext(&frame->uc.uc_mcontext,
419 regs, set->sig[0]);
420 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
421
422 /* Set up to return from userspace. If provided, use a stub
423 already in userspace. */
424 if (ka->sa.sa_flags & SA_RESTORER) {
425 regs->pr = (unsigned long) ka->sa.sa_restorer;
426 #ifdef CONFIG_VSYSCALL
427 } else if (likely(current->mm->context.vdso)) {
428 regs->pr = VDSO_SYM(&__kernel_rt_sigreturn);
429 #endif
430 } else {
431 /* Generate return code (system call to rt_sigreturn) */
432 err |= __put_user(MOVW(7), &frame->retcode[0]);
433 err |= __put_user(TRAP16, &frame->retcode[1]);
434 err |= __put_user(OR_R0_R0, &frame->retcode[2]);
435 err |= __put_user(OR_R0_R0, &frame->retcode[3]);
436 err |= __put_user(OR_R0_R0, &frame->retcode[4]);
437 err |= __put_user(OR_R0_R0, &frame->retcode[5]);
438 err |= __put_user(OR_R0_R0, &frame->retcode[6]);
439 err |= __put_user((__NR_rt_sigreturn), &frame->retcode[7]);
440 regs->pr = (unsigned long) frame->retcode;
441 }
442
443 if (err)
444 goto give_sigsegv;
445
446 /* Set up registers for signal handler */
447 regs->regs[15] = (unsigned long) frame;
448 regs->regs[4] = signal; /* Arg for signal handler */
449 regs->regs[5] = (unsigned long) &frame->info;
450 regs->regs[6] = (unsigned long) &frame->uc;
451 regs->pc = (unsigned long) ka->sa.sa_handler;
452
453 set_fs(USER_DS);
454
455 pr_debug("SIG deliver (%s:%d): sp=%p pc=%08lx pr=%08lx\n",
456 current->comm, current->pid, frame, regs->pc, regs->pr);
457
458 flush_cache_sigtramp(regs->pr);
459
460 if ((-regs->pr & (L1_CACHE_BYTES-1)) < sizeof(frame->retcode))
461 flush_cache_sigtramp(regs->pr + L1_CACHE_BYTES);
462
463 return 0;
464
465 give_sigsegv:
466 force_sigsegv(sig, current);
467 return -EFAULT;
468 }
469
470 /*
471 * OK, we're invoking a handler
472 */
473
474 static int
475 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
476 sigset_t *oldset, struct pt_regs *regs)
477 {
478 int ret;
479
480 /* Are we from a system call? */
481 if (regs->tra >= 0) {
482 /* If so, check system call restarting.. */
483 switch (regs->regs[0]) {
484 case -ERESTARTNOHAND:
485 regs->regs[0] = -EINTR;
486 break;
487
488 case -ERESTARTSYS:
489 if (!(ka->sa.sa_flags & SA_RESTART)) {
490 regs->regs[0] = -EINTR;
491 break;
492 }
493 /* fallthrough */
494 case -ERESTARTNOINTR:
495 regs->pc -= 2;
496 }
497 } else {
498 /* gUSA handling */
499 #ifdef CONFIG_PREEMPT
500 unsigned long flags;
501
502 local_irq_save(flags);
503 #endif
504 if (regs->regs[15] >= 0xc0000000) {
505 int offset = (int)regs->regs[15];
506
507 /* Reset stack pointer: clear critical region mark */
508 regs->regs[15] = regs->regs[1];
509 if (regs->pc < regs->regs[0])
510 /* Go to rewind point #1 */
511 regs->pc = regs->regs[0] + offset - 2;
512 }
513 #ifdef CONFIG_PREEMPT
514 local_irq_restore(flags);
515 #endif
516 }
517
518 /* Set up the stack frame */
519 if (ka->sa.sa_flags & SA_SIGINFO)
520 ret = setup_rt_frame(sig, ka, info, oldset, regs);
521 else
522 ret = setup_frame(sig, ka, oldset, regs);
523
524 if (ka->sa.sa_flags & SA_ONESHOT)
525 ka->sa.sa_handler = SIG_DFL;
526
527 if (ret == 0) {
528 spin_lock_irq(&current->sighand->siglock);
529 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
530 if (!(ka->sa.sa_flags & SA_NODEFER))
531 sigaddset(&current->blocked,sig);
532 recalc_sigpending();
533 spin_unlock_irq(&current->sighand->siglock);
534 }
535
536 return ret;
537 }
538
539 /*
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.
543 *
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.
547 */
548 static void do_signal(struct pt_regs *regs, unsigned int save_r0)
549 {
550 siginfo_t info;
551 int signr;
552 struct k_sigaction ka;
553 sigset_t *oldset;
554
555 /*
556 * We want the common case to go fast, which
557 * is why we may in certain cases get here from
558 * kernel mode. Just return without doing anything
559 * if so.
560 */
561 if (!user_mode(regs))
562 return;
563
564 if (try_to_freeze())
565 goto no_signal;
566
567 if (test_thread_flag(TIF_RESTORE_SIGMASK))
568 oldset = &current->saved_sigmask;
569 else
570 oldset = &current->blocked;
571
572 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
573 if (signr > 0) {
574 /* Whee! Actually deliver the signal. */
575 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
576 /* a signal was successfully delivered; the saved
577 * sigmask will have been stored in the signal frame,
578 * and will be restored by sigreturn, so we can simply
579 * clear the TIF_RESTORE_SIGMASK flag */
580 if (test_thread_flag(TIF_RESTORE_SIGMASK))
581 clear_thread_flag(TIF_RESTORE_SIGMASK);
582 }
583 }
584
585 no_signal:
586 /* Did we come from a system call? */
587 if (regs->tra >= 0) {
588 /* Restart the system call - no handlers present */
589 if (regs->regs[0] == -ERESTARTNOHAND ||
590 regs->regs[0] == -ERESTARTSYS ||
591 regs->regs[0] == -ERESTARTNOINTR) {
592 regs->regs[0] = save_r0;
593 regs->pc -= 2;
594 } else if (regs->regs[0] == -ERESTART_RESTARTBLOCK) {
595 regs->pc -= 2;
596 regs->regs[3] = __NR_restart_syscall;
597 }
598 }
599
600 /* if there's no signal to deliver, we just put the saved sigmask
601 * back */
602 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
603 clear_thread_flag(TIF_RESTORE_SIGMASK);
604 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
605 }
606 }
607
608 asmlinkage void do_notify_resume(struct pt_regs *regs, unsigned int save_r0,
609 __u32 thread_info_flags)
610 {
611 /* deal with pending signal delivery */
612 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
613 do_signal(regs, save_r0);
614 }