Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Signal handling |
| 3 | * |
| 4 | * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> |
| 5 | * Copyright (C) 2008-2009 PetaLogix |
| 6 | * Copyright (C) 2003,2004 John Williams <jwilliams@itee.uq.edu.au> |
| 7 | * Copyright (C) 2001 NEC Corporation |
| 8 | * Copyright (C) 2001 Miles Bader <miles@gnu.org> |
| 9 | * Copyright (C) 1999,2000 Niibe Yutaka & Kaz Kojima |
| 10 | * Copyright (C) 1991,1992 Linus Torvalds |
| 11 | * |
| 12 | * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson |
| 13 | * |
| 14 | * This file was was derived from the sh version, arch/sh/kernel/signal.c |
| 15 | * |
| 16 | * This file is subject to the terms and conditions of the GNU General |
| 17 | * Public License. See the file COPYING in the main directory of this |
| 18 | * archive for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/smp.h> |
| 24 | #include <linux/smp_lock.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/signal.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/wait.h> |
| 29 | #include <linux/ptrace.h> |
| 30 | #include <linux/unistd.h> |
| 31 | #include <linux/stddef.h> |
| 32 | #include <linux/personality.h> |
| 33 | #include <linux/percpu.h> |
| 34 | #include <linux/linkage.h> |
| 35 | #include <asm/entry.h> |
| 36 | #include <asm/ucontext.h> |
| 37 | #include <linux/uaccess.h> |
| 38 | #include <asm/pgtable.h> |
| 39 | #include <asm/pgalloc.h> |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 40 | #include <linux/syscalls.h> |
| 41 | #include <asm/cacheflush.h> |
| 42 | #include <asm/syscalls.h> |
| 43 | |
| 44 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) |
| 45 | |
| 46 | asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_sycall); |
| 47 | |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 48 | |
| 49 | asmlinkage int |
| 50 | sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss, |
| 51 | struct pt_regs *regs) |
| 52 | { |
| 53 | return do_sigaltstack(uss, uoss, regs->r1); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Do a signal return; undo the signal stack. |
| 58 | */ |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 59 | struct sigframe { |
| 60 | struct sigcontext sc; |
| 61 | unsigned long extramask[_NSIG_WORDS-1]; |
| 62 | unsigned long tramp[2]; /* signal trampoline */ |
| 63 | }; |
| 64 | |
| 65 | struct rt_sigframe { |
| 66 | struct siginfo info; |
| 67 | struct ucontext uc; |
| 68 | unsigned long tramp[2]; /* signal trampoline */ |
| 69 | }; |
| 70 | |
Arnd Bergmann | 353b431 | 2009-05-01 13:37:46 +0000 | [diff] [blame] | 71 | static int restore_sigcontext(struct pt_regs *regs, |
| 72 | struct sigcontext __user *sc, int *rval_p) |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 73 | { |
| 74 | unsigned int err = 0; |
| 75 | |
| 76 | #define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); } |
| 77 | COPY(r0); |
| 78 | COPY(r1); |
| 79 | COPY(r2); COPY(r3); COPY(r4); COPY(r5); |
| 80 | COPY(r6); COPY(r7); COPY(r8); COPY(r9); |
| 81 | COPY(r10); COPY(r11); COPY(r12); COPY(r13); |
| 82 | COPY(r14); COPY(r15); COPY(r16); COPY(r17); |
| 83 | COPY(r18); COPY(r19); COPY(r20); COPY(r21); |
| 84 | COPY(r22); COPY(r23); COPY(r24); COPY(r25); |
| 85 | COPY(r26); COPY(r27); COPY(r28); COPY(r29); |
| 86 | COPY(r30); COPY(r31); |
| 87 | COPY(pc); COPY(ear); COPY(esr); COPY(fsr); |
| 88 | #undef COPY |
| 89 | |
| 90 | *rval_p = regs->r3; |
| 91 | |
| 92 | return err; |
| 93 | } |
| 94 | |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 95 | asmlinkage long sys_rt_sigreturn(struct pt_regs *regs) |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 96 | { |
Arnd Bergmann | 353b431 | 2009-05-01 13:37:46 +0000 | [diff] [blame] | 97 | struct rt_sigframe __user *frame = |
| 98 | (struct rt_sigframe __user *)(regs->r1 + STATE_SAVE_ARG_SPACE); |
Michal Simek | 2921e2b | 2009-04-21 14:08:47 +0200 | [diff] [blame] | 99 | |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 100 | sigset_t set; |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 101 | int rval; |
| 102 | |
| 103 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 104 | goto badframe; |
| 105 | |
| 106 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 107 | goto badframe; |
| 108 | |
| 109 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 110 | spin_lock_irq(¤t->sighand->siglock); |
| 111 | current->blocked = set; |
| 112 | recalc_sigpending(); |
| 113 | spin_unlock_irq(¤t->sighand->siglock); |
| 114 | |
| 115 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval)) |
| 116 | goto badframe; |
| 117 | |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 118 | /* It is more difficult to avoid calling this function than to |
| 119 | call it and ignore errors. */ |
Arnd Bergmann | 353b431 | 2009-05-01 13:37:46 +0000 | [diff] [blame] | 120 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->r1)) |
| 121 | goto badframe; |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 122 | |
| 123 | return rval; |
| 124 | |
| 125 | badframe: |
| 126 | force_sig(SIGSEGV, current); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Set up a signal frame. |
| 132 | */ |
| 133 | |
| 134 | static int |
Arnd Bergmann | 353b431 | 2009-05-01 13:37:46 +0000 | [diff] [blame] | 135 | setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 136 | unsigned long mask) |
| 137 | { |
| 138 | int err = 0; |
| 139 | |
| 140 | #define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); } |
| 141 | COPY(r0); |
| 142 | COPY(r1); |
| 143 | COPY(r2); COPY(r3); COPY(r4); COPY(r5); |
| 144 | COPY(r6); COPY(r7); COPY(r8); COPY(r9); |
| 145 | COPY(r10); COPY(r11); COPY(r12); COPY(r13); |
| 146 | COPY(r14); COPY(r15); COPY(r16); COPY(r17); |
| 147 | COPY(r18); COPY(r19); COPY(r20); COPY(r21); |
| 148 | COPY(r22); COPY(r23); COPY(r24); COPY(r25); |
| 149 | COPY(r26); COPY(r27); COPY(r28); COPY(r29); |
| 150 | COPY(r30); COPY(r31); |
| 151 | COPY(pc); COPY(ear); COPY(esr); COPY(fsr); |
| 152 | #undef COPY |
| 153 | |
| 154 | err |= __put_user(mask, &sc->oldmask); |
| 155 | |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Determine which stack to use.. |
| 161 | */ |
Arnd Bergmann | 353b431 | 2009-05-01 13:37:46 +0000 | [diff] [blame] | 162 | static inline void __user * |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 163 | get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size) |
| 164 | { |
| 165 | /* Default to using normal stack */ |
| 166 | unsigned long sp = regs->r1; |
| 167 | |
| 168 | if ((ka->sa.sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp)) |
| 169 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 170 | |
Arnd Bergmann | 353b431 | 2009-05-01 13:37:46 +0000 | [diff] [blame] | 171 | return (void __user *)((sp - frame_size) & -8UL); |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 174 | static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
| 175 | sigset_t *set, struct pt_regs *regs) |
| 176 | { |
Arnd Bergmann | 353b431 | 2009-05-01 13:37:46 +0000 | [diff] [blame] | 177 | struct rt_sigframe __user *frame; |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 178 | int err = 0; |
| 179 | int signal; |
| 180 | |
| 181 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 182 | |
| 183 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 184 | goto give_sigsegv; |
| 185 | |
| 186 | signal = current_thread_info()->exec_domain |
| 187 | && current_thread_info()->exec_domain->signal_invmap |
| 188 | && sig < 32 |
| 189 | ? current_thread_info()->exec_domain->signal_invmap[sig] |
| 190 | : sig; |
| 191 | |
Michal Simek | a6029d1 | 2009-05-12 12:10:52 +0200 | [diff] [blame] | 192 | if (info) |
| 193 | err |= copy_siginfo_to_user(&frame->info, info); |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 194 | |
| 195 | /* Create the ucontext. */ |
| 196 | err |= __put_user(0, &frame->uc.uc_flags); |
| 197 | err |= __put_user(0, &frame->uc.uc_link); |
| 198 | err |= __put_user((void *)current->sas_ss_sp, |
| 199 | &frame->uc.uc_stack.ss_sp); |
| 200 | err |= __put_user(sas_ss_flags(regs->r1), |
| 201 | &frame->uc.uc_stack.ss_flags); |
| 202 | err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); |
| 203 | err |= setup_sigcontext(&frame->uc.uc_mcontext, |
| 204 | regs, set->sig[0]); |
| 205 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 206 | |
| 207 | /* Set up to return from userspace. If provided, use a stub |
| 208 | already in userspace. */ |
| 209 | /* minus 8 is offset to cater for "rtsd r15,8" */ |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 210 | /* addi r12, r0, __NR_sigreturn */ |
| 211 | err |= __put_user(0x31800000 | __NR_rt_sigreturn , |
| 212 | frame->tramp + 0); |
| 213 | /* brki r14, 0x8 */ |
| 214 | err |= __put_user(0xb9cc0008, frame->tramp + 1); |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 215 | |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 216 | /* Return from sighandler will jump to the tramp. |
| 217 | Negative 8 offset because return is rtsd r15, 8 */ |
| 218 | regs->r15 = ((unsigned long)frame->tramp)-8; |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 219 | |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 220 | __invalidate_cache_sigtramp((unsigned long)frame->tramp); |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 221 | |
| 222 | if (err) |
| 223 | goto give_sigsegv; |
| 224 | |
| 225 | /* Set up registers for signal handler */ |
Michal Simek | 2921e2b | 2009-04-21 14:08:47 +0200 | [diff] [blame] | 226 | regs->r1 = (unsigned long) frame - STATE_SAVE_ARG_SPACE; |
| 227 | |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 228 | /* Signal handler args: */ |
| 229 | regs->r5 = signal; /* arg 0: signum */ |
| 230 | regs->r6 = (unsigned long) &frame->info; /* arg 1: siginfo */ |
| 231 | regs->r7 = (unsigned long) &frame->uc; /* arg2: ucontext */ |
| 232 | /* Offset to handle microblaze rtid r14, 0 */ |
| 233 | regs->pc = (unsigned long)ka->sa.sa_handler; |
| 234 | |
| 235 | set_fs(USER_DS); |
| 236 | |
| 237 | #ifdef DEBUG_SIG |
| 238 | printk(KERN_INFO "SIG deliver (%s:%d): sp=%p pc=%08lx\n", |
| 239 | current->comm, current->pid, frame, regs->pc); |
| 240 | #endif |
| 241 | |
| 242 | return; |
| 243 | |
| 244 | give_sigsegv: |
| 245 | if (sig == SIGSEGV) |
| 246 | ka->sa.sa_handler = SIG_DFL; |
| 247 | force_sig(SIGSEGV, current); |
| 248 | } |
| 249 | |
| 250 | /* Handle restarting system calls */ |
| 251 | static inline void |
| 252 | handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler) |
| 253 | { |
| 254 | switch (regs->r3) { |
| 255 | case -ERESTART_RESTARTBLOCK: |
| 256 | case -ERESTARTNOHAND: |
| 257 | if (!has_handler) |
| 258 | goto do_restart; |
| 259 | regs->r3 = -EINTR; |
| 260 | break; |
| 261 | case -ERESTARTSYS: |
| 262 | if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) { |
| 263 | regs->r3 = -EINTR; |
| 264 | break; |
| 265 | } |
| 266 | /* fallthrough */ |
| 267 | case -ERESTARTNOINTR: |
| 268 | do_restart: |
| 269 | /* offset of 4 bytes to re-execute trap (brki) instruction */ |
Michal Simek | 8b28626 | 2009-05-26 16:30:28 +0200 | [diff] [blame] | 270 | #ifndef CONFIG_MMU |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 271 | regs->pc -= 4; |
Michal Simek | 8b28626 | 2009-05-26 16:30:28 +0200 | [diff] [blame] | 272 | #else |
| 273 | /* offset of 8 bytes required = 4 for rtbd |
| 274 | offset, plus 4 for size of |
| 275 | "brki r14,8" |
| 276 | instruction. */ |
| 277 | regs->pc -= 8; |
| 278 | #endif |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * OK, we're invoking a handler |
| 285 | */ |
| 286 | |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 287 | static int |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 288 | handle_signal(unsigned long sig, struct k_sigaction *ka, |
| 289 | siginfo_t *info, sigset_t *oldset, struct pt_regs *regs) |
| 290 | { |
| 291 | /* Set up the stack frame */ |
| 292 | if (ka->sa.sa_flags & SA_SIGINFO) |
| 293 | setup_rt_frame(sig, ka, info, oldset, regs); |
| 294 | else |
Michal Simek | a6029d1 | 2009-05-12 12:10:52 +0200 | [diff] [blame] | 295 | setup_rt_frame(sig, ka, NULL, oldset, regs); |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 296 | |
| 297 | if (ka->sa.sa_flags & SA_ONESHOT) |
| 298 | ka->sa.sa_handler = SIG_DFL; |
| 299 | |
| 300 | if (!(ka->sa.sa_flags & SA_NODEFER)) { |
| 301 | spin_lock_irq(¤t->sighand->siglock); |
| 302 | sigorsets(¤t->blocked, |
| 303 | ¤t->blocked, &ka->sa.sa_mask); |
| 304 | sigaddset(¤t->blocked, sig); |
| 305 | recalc_sigpending(); |
| 306 | spin_unlock_irq(¤t->sighand->siglock); |
| 307 | } |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 308 | return 1; |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 313 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 314 | * mistake. |
| 315 | * |
| 316 | * Note that we go through the signals twice: once to check the signals that |
| 317 | * the kernel can handle, and then we build all the user-level signal handling |
| 318 | * stack-frames in one go after that. |
| 319 | */ |
| 320 | int do_signal(struct pt_regs *regs, sigset_t *oldset, int in_syscall) |
| 321 | { |
| 322 | siginfo_t info; |
| 323 | int signr; |
| 324 | struct k_sigaction ka; |
| 325 | #ifdef DEBUG_SIG |
| 326 | printk(KERN_INFO "do signal: %p %p %d\n", regs, oldset, in_syscall); |
| 327 | printk(KERN_INFO "do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1, |
| 328 | regs->r12, current_thread_info()->flags); |
| 329 | #endif |
| 330 | /* |
| 331 | * We want the common case to go fast, which |
| 332 | * is why we may in certain cases get here from |
| 333 | * kernel mode. Just return without doing anything |
| 334 | * if so. |
| 335 | */ |
| 336 | if (kernel_mode(regs)) |
| 337 | return 1; |
| 338 | |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 339 | if (current_thread_info()->status & TS_RESTORE_SIGMASK) |
| 340 | oldset = ¤t->saved_sigmask; |
| 341 | else |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 342 | oldset = ¤t->blocked; |
| 343 | |
| 344 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
| 345 | if (signr > 0) { |
| 346 | /* Whee! Actually deliver the signal. */ |
| 347 | if (in_syscall) |
| 348 | handle_restart(regs, &ka, 1); |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 349 | if (handle_signal(signr, &ka, &info, oldset, regs)) { |
| 350 | /* |
| 351 | * A signal was successfully delivered; the saved |
| 352 | * sigmask will have been stored in the signal frame, |
| 353 | * and will be restored by sigreturn, so we can simply |
| 354 | * clear the TS_RESTORE_SIGMASK flag. |
| 355 | */ |
| 356 | current_thread_info()->status &= |
| 357 | ~TS_RESTORE_SIGMASK; |
| 358 | } |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | |
| 362 | if (in_syscall) |
| 363 | handle_restart(regs, NULL, 0); |
| 364 | |
Arnd Bergmann | 3183e06 | 2009-06-18 19:55:29 +0200 | [diff] [blame^] | 365 | /* |
| 366 | * If there's no signal to deliver, we just put the saved sigmask |
| 367 | * back. |
| 368 | */ |
| 369 | if (current_thread_info()->status & TS_RESTORE_SIGMASK) { |
| 370 | current_thread_info()->status &= ~TS_RESTORE_SIGMASK; |
| 371 | sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); |
| 372 | } |
| 373 | |
Michal Simek | 2148daa9 | 2009-03-27 14:25:14 +0100 | [diff] [blame] | 374 | /* Did we come from a system call? */ |
| 375 | return 0; |
| 376 | } |