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