Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 3 | * Copyright 2010 Tilera Corporation. All Rights Reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation, version 2. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 12 | * NON INFRINGEMENT. See the GNU General Public License for |
| 13 | * more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/smp.h> |
| 19 | #include <linux/smp_lock.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/signal.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/wait.h> |
| 24 | #include <linux/unistd.h> |
| 25 | #include <linux/stddef.h> |
| 26 | #include <linux/personality.h> |
| 27 | #include <linux/suspend.h> |
| 28 | #include <linux/ptrace.h> |
| 29 | #include <linux/elf.h> |
| 30 | #include <linux/compat.h> |
| 31 | #include <linux/syscalls.h> |
| 32 | #include <linux/uaccess.h> |
| 33 | #include <asm/processor.h> |
| 34 | #include <asm/ucontext.h> |
| 35 | #include <asm/sigframe.h> |
Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 36 | #include <asm/syscalls.h> |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 37 | #include <arch/interrupts.h> |
| 38 | |
| 39 | #define DEBUG_SIG 0 |
| 40 | |
| 41 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) |
| 42 | |
| 43 | |
Chris Metcalf | d929b6a | 2010-10-14 14:34:33 -0400 | [diff] [blame] | 44 | SYSCALL_DEFINE3(sigaltstack, const stack_t __user *, uss, |
| 45 | stack_t __user *, uoss, struct pt_regs *, regs) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 46 | { |
| 47 | return do_sigaltstack(uss, uoss, regs->sp); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * Do a signal return; undo the signal stack. |
| 53 | */ |
| 54 | |
| 55 | int restore_sigcontext(struct pt_regs *regs, |
| 56 | struct sigcontext __user *sc, long *pr0) |
| 57 | { |
| 58 | int err = 0; |
| 59 | int i; |
| 60 | |
| 61 | /* Always make any pending restarted system calls return -EINTR */ |
| 62 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 63 | |
Chris Metcalf | 74fca9d | 2010-09-15 11:16:08 -0400 | [diff] [blame] | 64 | /* |
| 65 | * Enforce that sigcontext is like pt_regs, and doesn't mess |
| 66 | * up our stack alignment rules. |
| 67 | */ |
| 68 | BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs)); |
| 69 | BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0); |
| 70 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 71 | for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i) |
Chris Metcalf | 74fca9d | 2010-09-15 11:16:08 -0400 | [diff] [blame] | 72 | err |= __get_user(regs->regs[i], &sc->gregs[i]); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 73 | |
| 74 | regs->faultnum = INT_SWINT_1_SIGRETURN; |
| 75 | |
Chris Metcalf | 74fca9d | 2010-09-15 11:16:08 -0400 | [diff] [blame] | 76 | err |= __get_user(*pr0, &sc->gregs[0]); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 77 | return err; |
| 78 | } |
| 79 | |
Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 80 | /* sigreturn() returns long since it restores r0 in the interrupted code. */ |
Chris Metcalf | d929b6a | 2010-10-14 14:34:33 -0400 | [diff] [blame] | 81 | SYSCALL_DEFINE1(rt_sigreturn, struct pt_regs *, regs) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 82 | { |
| 83 | struct rt_sigframe __user *frame = |
| 84 | (struct rt_sigframe __user *)(regs->sp); |
| 85 | sigset_t set; |
| 86 | long r0; |
| 87 | |
| 88 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 89 | goto badframe; |
| 90 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) |
| 91 | goto badframe; |
| 92 | |
| 93 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 94 | spin_lock_irq(¤t->sighand->siglock); |
| 95 | current->blocked = set; |
| 96 | recalc_sigpending(); |
| 97 | spin_unlock_irq(¤t->sighand->siglock); |
| 98 | |
| 99 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0)) |
| 100 | goto badframe; |
| 101 | |
| 102 | if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT) |
| 103 | goto badframe; |
| 104 | |
| 105 | return r0; |
| 106 | |
| 107 | badframe: |
| 108 | force_sig(SIGSEGV, current); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Set up a signal frame. |
| 114 | */ |
| 115 | |
| 116 | int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs) |
| 117 | { |
| 118 | int i, err = 0; |
| 119 | |
| 120 | for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i) |
Chris Metcalf | 74fca9d | 2010-09-15 11:16:08 -0400 | [diff] [blame] | 121 | err |= __put_user(regs->regs[i], &sc->gregs[i]); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 122 | |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Determine which stack to use.. |
| 128 | */ |
| 129 | static inline void __user *get_sigframe(struct k_sigaction *ka, |
| 130 | struct pt_regs *regs, |
| 131 | size_t frame_size) |
| 132 | { |
| 133 | unsigned long sp; |
| 134 | |
| 135 | /* Default to using normal stack */ |
| 136 | sp = regs->sp; |
| 137 | |
| 138 | /* |
| 139 | * If we are on the alternate signal stack and would overflow |
| 140 | * it, don't. Return an always-bogus address instead so we |
| 141 | * will die with SIGSEGV. |
| 142 | */ |
| 143 | if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) |
Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 144 | return (void __user __force *)-1UL; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 145 | |
| 146 | /* This is the X/Open sanctioned signal stack switching. */ |
| 147 | if (ka->sa.sa_flags & SA_ONSTACK) { |
| 148 | if (sas_ss_flags(sp) == 0) |
| 149 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 150 | } |
| 151 | |
| 152 | sp -= frame_size; |
| 153 | /* |
| 154 | * Align the stack pointer according to the TILE ABI, |
| 155 | * i.e. so that on function entry (sp & 15) == 0. |
| 156 | */ |
| 157 | sp &= -16UL; |
| 158 | return (void __user *) sp; |
| 159 | } |
| 160 | |
| 161 | static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info, |
| 162 | sigset_t *set, struct pt_regs *regs) |
| 163 | { |
| 164 | unsigned long restorer; |
| 165 | struct rt_sigframe __user *frame; |
| 166 | int err = 0; |
| 167 | int usig; |
| 168 | |
| 169 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 170 | |
| 171 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
| 172 | goto give_sigsegv; |
| 173 | |
| 174 | usig = current_thread_info()->exec_domain |
| 175 | && current_thread_info()->exec_domain->signal_invmap |
| 176 | && sig < 32 |
| 177 | ? current_thread_info()->exec_domain->signal_invmap[sig] |
| 178 | : sig; |
| 179 | |
| 180 | /* Always write at least the signal number for the stack backtracer. */ |
| 181 | if (ka->sa.sa_flags & SA_SIGINFO) { |
| 182 | /* At sigreturn time, restore the callee-save registers too. */ |
| 183 | err |= copy_siginfo_to_user(&frame->info, info); |
| 184 | regs->flags |= PT_FLAGS_RESTORE_REGS; |
| 185 | } else { |
| 186 | err |= __put_user(info->si_signo, &frame->info.si_signo); |
| 187 | } |
| 188 | |
| 189 | /* Create the ucontext. */ |
| 190 | err |= __clear_user(&frame->save_area, sizeof(frame->save_area)); |
| 191 | err |= __put_user(0, &frame->uc.uc_flags); |
Chris Metcalf | 0707ad3 | 2010-06-25 17:04:17 -0400 | [diff] [blame] | 192 | err |= __put_user(NULL, &frame->uc.uc_link); |
| 193 | err |= __put_user((void __user *)(current->sas_ss_sp), |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 194 | &frame->uc.uc_stack.ss_sp); |
| 195 | err |= __put_user(sas_ss_flags(regs->sp), |
| 196 | &frame->uc.uc_stack.ss_flags); |
| 197 | err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size); |
| 198 | err |= setup_sigcontext(&frame->uc.uc_mcontext, regs); |
| 199 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); |
| 200 | if (err) |
| 201 | goto give_sigsegv; |
| 202 | |
| 203 | restorer = VDSO_BASE; |
| 204 | if (ka->sa.sa_flags & SA_RESTORER) |
| 205 | restorer = (unsigned long) ka->sa.sa_restorer; |
| 206 | |
| 207 | /* |
| 208 | * Set up registers for signal handler. |
| 209 | * Registers that we don't modify keep the value they had from |
| 210 | * user-space at the time we took the signal. |
Chris Metcalf | 74fca9d | 2010-09-15 11:16:08 -0400 | [diff] [blame] | 211 | * We always pass siginfo and mcontext, regardless of SA_SIGINFO, |
| 212 | * since some things rely on this (e.g. glibc's debug/segfault.c). |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 213 | */ |
| 214 | regs->pc = (unsigned long) ka->sa.sa_handler; |
| 215 | regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */ |
| 216 | regs->sp = (unsigned long) frame; |
| 217 | regs->lr = restorer; |
| 218 | regs->regs[0] = (unsigned long) usig; |
Chris Metcalf | 74fca9d | 2010-09-15 11:16:08 -0400 | [diff] [blame] | 219 | regs->regs[1] = (unsigned long) &frame->info; |
| 220 | regs->regs[2] = (unsigned long) &frame->uc; |
| 221 | regs->flags |= PT_FLAGS_CALLER_SAVES; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Notify any tracer that was single-stepping it. |
| 225 | * The tracer may want to single-step inside the |
| 226 | * handler too. |
| 227 | */ |
| 228 | if (test_thread_flag(TIF_SINGLESTEP)) |
| 229 | ptrace_notify(SIGTRAP); |
| 230 | |
| 231 | return 0; |
| 232 | |
| 233 | give_sigsegv: |
| 234 | force_sigsegv(sig, current); |
| 235 | return -EFAULT; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * OK, we're invoking a handler |
| 240 | */ |
| 241 | |
| 242 | static int handle_signal(unsigned long sig, siginfo_t *info, |
| 243 | struct k_sigaction *ka, sigset_t *oldset, |
| 244 | struct pt_regs *regs) |
| 245 | { |
| 246 | int ret; |
| 247 | |
| 248 | |
| 249 | /* Are we from a system call? */ |
| 250 | if (regs->faultnum == INT_SWINT_1) { |
| 251 | /* If so, check system call restarting.. */ |
| 252 | switch (regs->regs[0]) { |
| 253 | case -ERESTART_RESTARTBLOCK: |
| 254 | case -ERESTARTNOHAND: |
| 255 | regs->regs[0] = -EINTR; |
| 256 | break; |
| 257 | |
| 258 | case -ERESTARTSYS: |
| 259 | if (!(ka->sa.sa_flags & SA_RESTART)) { |
| 260 | regs->regs[0] = -EINTR; |
| 261 | break; |
| 262 | } |
| 263 | /* fallthrough */ |
| 264 | case -ERESTARTNOINTR: |
| 265 | /* Reload caller-saves to restore r0..r5 and r10. */ |
| 266 | regs->flags |= PT_FLAGS_CALLER_SAVES; |
| 267 | regs->regs[0] = regs->orig_r0; |
| 268 | regs->pc -= 8; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* Set up the stack frame */ |
| 273 | #ifdef CONFIG_COMPAT |
| 274 | if (is_compat_task()) |
| 275 | ret = compat_setup_rt_frame(sig, ka, info, oldset, regs); |
| 276 | else |
| 277 | #endif |
| 278 | ret = setup_rt_frame(sig, ka, info, oldset, regs); |
| 279 | if (ret == 0) { |
| 280 | /* This code is only called from system calls or from |
| 281 | * the work_pending path in the return-to-user code, and |
| 282 | * either way we can re-enable interrupts unconditionally. |
| 283 | */ |
| 284 | spin_lock_irq(¤t->sighand->siglock); |
| 285 | sigorsets(¤t->blocked, |
| 286 | ¤t->blocked, &ka->sa.sa_mask); |
| 287 | if (!(ka->sa.sa_flags & SA_NODEFER)) |
| 288 | sigaddset(¤t->blocked, sig); |
| 289 | recalc_sigpending(); |
| 290 | spin_unlock_irq(¤t->sighand->siglock); |
| 291 | } |
| 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Note that 'init' is a special process: it doesn't get signals it doesn't |
| 298 | * want to handle. Thus you cannot kill init even with a SIGKILL even by |
| 299 | * mistake. |
| 300 | */ |
| 301 | void do_signal(struct pt_regs *regs) |
| 302 | { |
| 303 | siginfo_t info; |
| 304 | int signr; |
| 305 | struct k_sigaction ka; |
| 306 | sigset_t *oldset; |
| 307 | |
| 308 | /* |
| 309 | * i386 will check if we're coming from kernel mode and bail out |
| 310 | * here. In my experience this just turns weird crashes into |
| 311 | * weird spin-hangs. But if we find a case where this seems |
| 312 | * helpful, we can reinstate the check on "!user_mode(regs)". |
| 313 | */ |
| 314 | |
| 315 | if (current_thread_info()->status & TS_RESTORE_SIGMASK) |
| 316 | oldset = ¤t->saved_sigmask; |
| 317 | else |
| 318 | oldset = ¤t->blocked; |
| 319 | |
| 320 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
| 321 | if (signr > 0) { |
| 322 | /* Whee! Actually deliver the signal. */ |
| 323 | if (handle_signal(signr, &info, &ka, oldset, regs) == 0) { |
| 324 | /* |
| 325 | * A signal was successfully delivered; the saved |
| 326 | * sigmask will have been stored in the signal frame, |
| 327 | * and will be restored by sigreturn, so we can simply |
| 328 | * clear the TS_RESTORE_SIGMASK flag. |
| 329 | */ |
| 330 | current_thread_info()->status &= ~TS_RESTORE_SIGMASK; |
| 331 | } |
| 332 | |
Chris Metcalf | 34a89d2 | 2010-10-28 15:03:30 -0400 | [diff] [blame^] | 333 | goto done; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Did we come from a system call? */ |
| 337 | if (regs->faultnum == INT_SWINT_1) { |
| 338 | /* Restart the system call - no handlers present */ |
| 339 | switch (regs->regs[0]) { |
| 340 | case -ERESTARTNOHAND: |
| 341 | case -ERESTARTSYS: |
| 342 | case -ERESTARTNOINTR: |
| 343 | regs->flags |= PT_FLAGS_CALLER_SAVES; |
| 344 | regs->regs[0] = regs->orig_r0; |
| 345 | regs->pc -= 8; |
| 346 | break; |
| 347 | |
| 348 | case -ERESTART_RESTARTBLOCK: |
| 349 | regs->flags |= PT_FLAGS_CALLER_SAVES; |
| 350 | regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall; |
| 351 | regs->pc -= 8; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /* If there's no signal to deliver, just put the saved sigmask back. */ |
| 357 | if (current_thread_info()->status & TS_RESTORE_SIGMASK) { |
| 358 | current_thread_info()->status &= ~TS_RESTORE_SIGMASK; |
| 359 | sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); |
| 360 | } |
Chris Metcalf | 34a89d2 | 2010-10-28 15:03:30 -0400 | [diff] [blame^] | 361 | |
| 362 | done: |
| 363 | /* Avoid double syscall restart if there are nested signals. */ |
| 364 | regs->faultnum = INT_SWINT_1_SIGRETURN; |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 365 | } |