Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 7 | * Copyright (C) 1994 - 2000 Ralf Baechle |
| 8 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc. |
| 9 | */ |
Ralf Baechle | 02416dc | 2005-06-15 13:00:12 +0000 | [diff] [blame] | 10 | #include <linux/cache.h> |
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/personality.h> |
| 14 | #include <linux/smp.h> |
| 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/signal.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/wait.h> |
| 20 | #include <linux/ptrace.h> |
| 21 | #include <linux/unistd.h> |
| 22 | #include <linux/compiler.h> |
| 23 | |
Ralf Baechle | e50c0a8 | 2005-05-31 11:49:19 +0000 | [diff] [blame] | 24 | #include <asm/abi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/asm.h> |
| 26 | #include <linux/bitops.h> |
| 27 | #include <asm/cacheflush.h> |
| 28 | #include <asm/fpu.h> |
| 29 | #include <asm/sim.h> |
| 30 | #include <asm/uaccess.h> |
| 31 | #include <asm/ucontext.h> |
| 32 | #include <asm/cpu-features.h> |
Ralf Baechle | 02416dc | 2005-06-15 13:00:12 +0000 | [diff] [blame] | 33 | #include <asm/war.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include "signal-common.h" |
| 36 | |
| 37 | #define DEBUG_SIG 0 |
| 38 | |
| 39 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) |
| 40 | |
Franck Bui-Huu | c0b9bae | 2007-02-05 15:24:21 +0100 | [diff] [blame] | 41 | #if ICACHE_REFILLS_WORKAROUND_WAR == 0 |
| 42 | |
| 43 | struct rt_sigframe { |
| 44 | u32 rs_ass[4]; /* argument save space for o32 */ |
| 45 | u32 rs_code[2]; /* signal trampoline */ |
| 46 | struct siginfo rs_info; |
| 47 | struct ucontext rs_uc; |
| 48 | }; |
| 49 | |
| 50 | #else |
| 51 | |
| 52 | struct rt_sigframe { |
| 53 | u32 rs_ass[4]; /* argument save space for o32 */ |
| 54 | u32 rs_pad[2]; |
| 55 | struct siginfo rs_info; |
| 56 | struct ucontext rs_uc; |
| 57 | u32 rs_code[8] ____cacheline_aligned; /* signal trampoline */ |
| 58 | }; |
| 59 | |
| 60 | #endif |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Franck Bui-Huu | c3fc4ab | 2007-02-05 15:24:20 +0100 | [diff] [blame] | 63 | * Helper routines |
| 64 | */ |
| 65 | int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) |
| 66 | { |
| 67 | int err = 0; |
| 68 | int i; |
| 69 | |
| 70 | err |= __put_user(regs->cp0_epc, &sc->sc_pc); |
| 71 | |
| 72 | err |= __put_user(0, &sc->sc_regs[0]); |
| 73 | for (i = 1; i < 32; i++) |
| 74 | err |= __put_user(regs->regs[i], &sc->sc_regs[i]); |
| 75 | |
| 76 | err |= __put_user(regs->hi, &sc->sc_mdhi); |
| 77 | err |= __put_user(regs->lo, &sc->sc_mdlo); |
| 78 | if (cpu_has_dsp) { |
| 79 | err |= __put_user(mfhi1(), &sc->sc_hi1); |
| 80 | err |= __put_user(mflo1(), &sc->sc_lo1); |
| 81 | err |= __put_user(mfhi2(), &sc->sc_hi2); |
| 82 | err |= __put_user(mflo2(), &sc->sc_lo2); |
| 83 | err |= __put_user(mfhi3(), &sc->sc_hi3); |
| 84 | err |= __put_user(mflo3(), &sc->sc_lo3); |
| 85 | err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp); |
| 86 | } |
| 87 | |
| 88 | err |= __put_user(!!used_math(), &sc->sc_used_math); |
| 89 | |
| 90 | if (used_math()) { |
| 91 | /* |
| 92 | * Save FPU state to signal context. Signal handler |
| 93 | * will "inherit" current FPU state. |
| 94 | */ |
| 95 | preempt_disable(); |
| 96 | |
| 97 | if (!is_fpu_owner()) { |
| 98 | own_fpu(); |
| 99 | restore_fp(current); |
| 100 | } |
| 101 | err |= save_fp_context(sc); |
| 102 | |
| 103 | preempt_enable(); |
| 104 | } |
| 105 | return err; |
| 106 | } |
| 107 | |
| 108 | int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) |
| 109 | { |
| 110 | unsigned int used_math; |
| 111 | unsigned long treg; |
| 112 | int err = 0; |
| 113 | int i; |
| 114 | |
| 115 | /* Always make any pending restarted system calls return -EINTR */ |
| 116 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 117 | |
| 118 | err |= __get_user(regs->cp0_epc, &sc->sc_pc); |
| 119 | err |= __get_user(regs->hi, &sc->sc_mdhi); |
| 120 | err |= __get_user(regs->lo, &sc->sc_mdlo); |
| 121 | if (cpu_has_dsp) { |
| 122 | err |= __get_user(treg, &sc->sc_hi1); mthi1(treg); |
| 123 | err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg); |
| 124 | err |= __get_user(treg, &sc->sc_hi2); mthi2(treg); |
| 125 | err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg); |
| 126 | err |= __get_user(treg, &sc->sc_hi3); mthi3(treg); |
| 127 | err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg); |
| 128 | err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK); |
| 129 | } |
| 130 | |
| 131 | for (i = 1; i < 32; i++) |
| 132 | err |= __get_user(regs->regs[i], &sc->sc_regs[i]); |
| 133 | |
| 134 | err |= __get_user(used_math, &sc->sc_used_math); |
| 135 | conditional_used_math(used_math); |
| 136 | |
| 137 | preempt_disable(); |
| 138 | |
| 139 | if (used_math()) { |
| 140 | /* restore fpu context if we have used it before */ |
| 141 | own_fpu(); |
| 142 | err |= restore_fp_context(sc); |
| 143 | } else { |
| 144 | /* signal handler may have used FPU. Give it up. */ |
| 145 | lose_fpu(); |
| 146 | } |
| 147 | |
| 148 | preempt_enable(); |
| 149 | |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, |
| 154 | size_t frame_size) |
| 155 | { |
| 156 | unsigned long sp; |
| 157 | |
| 158 | /* Default to using normal stack */ |
| 159 | sp = regs->regs[29]; |
| 160 | |
| 161 | /* |
| 162 | * FPU emulator may have it's own trampoline active just |
| 163 | * above the user stack, 16-bytes before the next lowest |
| 164 | * 16 byte boundary. Try to avoid trashing it. |
| 165 | */ |
| 166 | sp -= 32; |
| 167 | |
| 168 | /* This is the X/Open sanctioned signal stack switching. */ |
| 169 | if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0)) |
| 170 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 171 | |
| 172 | return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK)); |
| 173 | } |
| 174 | |
| 175 | int install_sigtramp(unsigned int __user *tramp, unsigned int syscall) |
| 176 | { |
| 177 | int err; |
| 178 | |
| 179 | /* |
| 180 | * Set up the return code ... |
| 181 | * |
| 182 | * li v0, __NR__foo_sigreturn |
| 183 | * syscall |
| 184 | */ |
| 185 | |
| 186 | err = __put_user(0x24020000 + syscall, tramp + 0); |
Franck Bui-Huu | 601dde4 | 2007-02-05 15:24:23 +0100 | [diff] [blame^] | 187 | err |= __put_user(0x0000000c , tramp + 1); |
Franck Bui-Huu | c3fc4ab | 2007-02-05 15:24:20 +0100 | [diff] [blame] | 188 | if (ICACHE_REFILLS_WORKAROUND_WAR) { |
| 189 | err |= __put_user(0, tramp + 2); |
| 190 | err |= __put_user(0, tramp + 3); |
| 191 | err |= __put_user(0, tramp + 4); |
| 192 | err |= __put_user(0, tramp + 5); |
| 193 | err |= __put_user(0, tramp + 6); |
| 194 | err |= __put_user(0, tramp + 7); |
| 195 | } |
| 196 | flush_cache_sigtramp((unsigned long) tramp); |
| 197 | |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | * Atomically swap in the new signal mask, and wait for a signal. |
| 203 | */ |
| 204 | |
| 205 | #ifdef CONFIG_TRAD_SIGNALS |
| 206 | save_static_function(sys_sigsuspend); |
| 207 | __attribute_used__ noinline static int |
| 208 | _sys_sigsuspend(nabi_no_regargs struct pt_regs regs) |
| 209 | { |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 210 | sigset_t newset; |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 211 | sigset_t __user *uset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 213 | uset = (sigset_t __user *) regs.regs[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (copy_from_user(&newset, uset, sizeof(sigset_t))) |
| 215 | return -EFAULT; |
| 216 | sigdelsetmask(&newset, ~_BLOCKABLE); |
| 217 | |
| 218 | spin_lock_irq(¤t->sighand->siglock); |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 219 | current->saved_sigmask = current->blocked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | current->blocked = newset; |
| 221 | recalc_sigpending(); |
| 222 | spin_unlock_irq(¤t->sighand->siglock); |
| 223 | |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 224 | current->state = TASK_INTERRUPTIBLE; |
| 225 | schedule(); |
| 226 | set_thread_flag(TIF_RESTORE_SIGMASK); |
| 227 | return -ERESTARTNOHAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | #endif |
| 230 | |
| 231 | save_static_function(sys_rt_sigsuspend); |
| 232 | __attribute_used__ noinline static int |
| 233 | _sys_rt_sigsuspend(nabi_no_regargs struct pt_regs regs) |
| 234 | { |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 235 | sigset_t newset; |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 236 | sigset_t __user *unewset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | size_t sigsetsize; |
| 238 | |
| 239 | /* XXX Don't preclude handling different sized sigset_t's. */ |
| 240 | sigsetsize = regs.regs[5]; |
| 241 | if (sigsetsize != sizeof(sigset_t)) |
| 242 | return -EINVAL; |
| 243 | |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 244 | unewset = (sigset_t __user *) regs.regs[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (copy_from_user(&newset, unewset, sizeof(newset))) |
| 246 | return -EFAULT; |
| 247 | sigdelsetmask(&newset, ~_BLOCKABLE); |
| 248 | |
| 249 | spin_lock_irq(¤t->sighand->siglock); |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 250 | current->saved_sigmask = current->blocked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | current->blocked = newset; |
Ralf Baechle | e0daad4 | 2007-02-05 00:10:11 +0000 | [diff] [blame] | 252 | recalc_sigpending(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | spin_unlock_irq(¤t->sighand->siglock); |
| 254 | |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 255 | current->state = TASK_INTERRUPTIBLE; |
| 256 | schedule(); |
| 257 | set_thread_flag(TIF_RESTORE_SIGMASK); |
| 258 | return -ERESTARTNOHAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | #ifdef CONFIG_TRAD_SIGNALS |
Atsushi Nemoto | 9c6031c | 2006-02-19 23:46:44 +0900 | [diff] [blame] | 262 | asmlinkage int sys_sigaction(int sig, const struct sigaction __user *act, |
| 263 | struct sigaction __user *oact) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
| 265 | struct k_sigaction new_ka, old_ka; |
| 266 | int ret; |
| 267 | int err = 0; |
| 268 | |
| 269 | if (act) { |
| 270 | old_sigset_t mask; |
| 271 | |
| 272 | if (!access_ok(VERIFY_READ, act, sizeof(*act))) |
| 273 | return -EFAULT; |
| 274 | err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler); |
| 275 | err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags); |
| 276 | err |= __get_user(mask, &act->sa_mask.sig[0]); |
| 277 | if (err) |
| 278 | return -EFAULT; |
| 279 | |
| 280 | siginitset(&new_ka.sa.sa_mask, mask); |
| 281 | } |
| 282 | |
| 283 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); |
| 284 | |
| 285 | if (!ret && oact) { |
| 286 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact))) |
Ralf Baechle | e0daad4 | 2007-02-05 00:10:11 +0000 | [diff] [blame] | 287 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags); |
| 289 | err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler); |
| 290 | err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig); |
| 291 | err |= __put_user(0, &oact->sa_mask.sig[1]); |
| 292 | err |= __put_user(0, &oact->sa_mask.sig[2]); |
| 293 | err |= __put_user(0, &oact->sa_mask.sig[3]); |
| 294 | if (err) |
| 295 | return -EFAULT; |
| 296 | } |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | asmlinkage int sys_sigaltstack(nabi_no_regargs struct pt_regs regs) |
| 303 | { |
Ralf Baechle | fe00f94 | 2005-03-01 19:22:29 +0000 | [diff] [blame] | 304 | const stack_t __user *uss = (const stack_t __user *) regs.regs[4]; |
| 305 | stack_t __user *uoss = (stack_t __user *) regs.regs[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | unsigned long usp = regs.regs[29]; |
| 307 | |
| 308 | return do_sigaltstack(uss, uoss, usp); |
| 309 | } |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | #ifdef CONFIG_TRAD_SIGNALS |
| 312 | save_static_function(sys_sigreturn); |
| 313 | __attribute_used__ noinline static void |
| 314 | _sys_sigreturn(nabi_no_regargs struct pt_regs regs) |
| 315 | { |
Atsushi Nemoto | 9bbf28a3 | 2006-02-01 01:41:09 +0900 | [diff] [blame] | 316 | struct sigframe __user *frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | sigset_t blocked; |
| 318 | |
Atsushi Nemoto | 9bbf28a3 | 2006-02-01 01:41:09 +0900 | [diff] [blame] | 319 | frame = (struct sigframe __user *) regs.regs[29]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 321 | goto badframe; |
| 322 | if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked))) |
| 323 | goto badframe; |
| 324 | |
| 325 | sigdelsetmask(&blocked, ~_BLOCKABLE); |
| 326 | spin_lock_irq(¤t->sighand->siglock); |
| 327 | current->blocked = blocked; |
| 328 | recalc_sigpending(); |
| 329 | spin_unlock_irq(¤t->sighand->siglock); |
| 330 | |
| 331 | if (restore_sigcontext(®s, &frame->sf_sc)) |
| 332 | goto badframe; |
| 333 | |
| 334 | /* |
| 335 | * Don't let your children do this ... |
| 336 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | __asm__ __volatile__( |
| 338 | "move\t$29, %0\n\t" |
| 339 | "j\tsyscall_exit" |
| 340 | :/* no outputs */ |
| 341 | :"r" (®s)); |
| 342 | /* Unreached */ |
| 343 | |
| 344 | badframe: |
| 345 | force_sig(SIGSEGV, current); |
| 346 | } |
Ralf Baechle | e50c0a8 | 2005-05-31 11:49:19 +0000 | [diff] [blame] | 347 | #endif /* CONFIG_TRAD_SIGNALS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | save_static_function(sys_rt_sigreturn); |
| 350 | __attribute_used__ noinline static void |
| 351 | _sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs) |
| 352 | { |
Atsushi Nemoto | 9bbf28a3 | 2006-02-01 01:41:09 +0900 | [diff] [blame] | 353 | struct rt_sigframe __user *frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | sigset_t set; |
| 355 | stack_t st; |
| 356 | |
Atsushi Nemoto | 9bbf28a3 | 2006-02-01 01:41:09 +0900 | [diff] [blame] | 357 | frame = (struct rt_sigframe __user *) regs.regs[29]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 359 | goto badframe; |
| 360 | if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set))) |
| 361 | goto badframe; |
| 362 | |
| 363 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 364 | spin_lock_irq(¤t->sighand->siglock); |
| 365 | current->blocked = set; |
| 366 | recalc_sigpending(); |
| 367 | spin_unlock_irq(¤t->sighand->siglock); |
| 368 | |
| 369 | if (restore_sigcontext(®s, &frame->rs_uc.uc_mcontext)) |
| 370 | goto badframe; |
| 371 | |
| 372 | if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st))) |
| 373 | goto badframe; |
| 374 | /* It is more difficult to avoid calling this function than to |
| 375 | call it and ignore errors. */ |
Atsushi Nemoto | 9bbf28a3 | 2006-02-01 01:41:09 +0900 | [diff] [blame] | 376 | do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | * Don't let your children do this ... |
| 380 | */ |
| 381 | __asm__ __volatile__( |
| 382 | "move\t$29, %0\n\t" |
| 383 | "j\tsyscall_exit" |
| 384 | :/* no outputs */ |
| 385 | :"r" (®s)); |
| 386 | /* Unreached */ |
| 387 | |
| 388 | badframe: |
| 389 | force_sig(SIGSEGV, current); |
| 390 | } |
| 391 | |
| 392 | #ifdef CONFIG_TRAD_SIGNALS |
Ralf Baechle | 129bc8f | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 393 | int setup_frame(struct k_sigaction * ka, struct pt_regs *regs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | int signr, sigset_t *set) |
| 395 | { |
Atsushi Nemoto | 9bbf28a3 | 2006-02-01 01:41:09 +0900 | [diff] [blame] | 396 | struct sigframe __user *frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | int err = 0; |
| 398 | |
| 399 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 400 | if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame))) |
| 401 | goto give_sigsegv; |
| 402 | |
Franck Bui-Huu | 601dde4 | 2007-02-05 15:24:23 +0100 | [diff] [blame^] | 403 | err |= install_sigtramp(frame->sf_code, __NR_sigreturn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | err |= setup_sigcontext(regs, &frame->sf_sc); |
| 406 | err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set)); |
| 407 | if (err) |
| 408 | goto give_sigsegv; |
| 409 | |
| 410 | /* |
| 411 | * Arguments to signal handler: |
| 412 | * |
| 413 | * a0 = signal number |
| 414 | * a1 = 0 (should be cause) |
| 415 | * a2 = pointer to struct sigcontext |
| 416 | * |
| 417 | * $25 and c0_epc point to the signal handler, $29 points to the |
| 418 | * struct sigframe. |
| 419 | */ |
| 420 | regs->regs[ 4] = signr; |
| 421 | regs->regs[ 5] = 0; |
| 422 | regs->regs[ 6] = (unsigned long) &frame->sf_sc; |
| 423 | regs->regs[29] = (unsigned long) frame; |
| 424 | regs->regs[31] = (unsigned long) frame->sf_code; |
| 425 | regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler; |
| 426 | |
| 427 | #if DEBUG_SIG |
| 428 | printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n", |
| 429 | current->comm, current->pid, |
| 430 | frame, regs->cp0_epc, frame->regs[31]); |
| 431 | #endif |
Ralf Baechle | e0daad4 | 2007-02-05 00:10:11 +0000 | [diff] [blame] | 432 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | give_sigsegv: |
| 435 | force_sigsegv(signr, current); |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 436 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | #endif |
| 439 | |
Ralf Baechle | 129bc8f | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 440 | int setup_rt_frame(struct k_sigaction * ka, struct pt_regs *regs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | int signr, sigset_t *set, siginfo_t *info) |
| 442 | { |
Atsushi Nemoto | 9bbf28a3 | 2006-02-01 01:41:09 +0900 | [diff] [blame] | 443 | struct rt_sigframe __user *frame; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | int err = 0; |
| 445 | |
| 446 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 447 | if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame))) |
| 448 | goto give_sigsegv; |
| 449 | |
Franck Bui-Huu | 601dde4 | 2007-02-05 15:24:23 +0100 | [diff] [blame^] | 450 | err |= install_sigtramp(frame->rs_code, __NR_rt_sigreturn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | /* Create siginfo. */ |
| 453 | err |= copy_siginfo_to_user(&frame->rs_info, info); |
| 454 | |
| 455 | /* Create the ucontext. */ |
| 456 | err |= __put_user(0, &frame->rs_uc.uc_flags); |
Atsushi Nemoto | 5665a0a | 2006-02-02 01:26:34 +0900 | [diff] [blame] | 457 | err |= __put_user(NULL, &frame->rs_uc.uc_link); |
Atsushi Nemoto | 9c6031c | 2006-02-19 23:46:44 +0900 | [diff] [blame] | 458 | err |= __put_user((void __user *)current->sas_ss_sp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | &frame->rs_uc.uc_stack.ss_sp); |
| 460 | err |= __put_user(sas_ss_flags(regs->regs[29]), |
| 461 | &frame->rs_uc.uc_stack.ss_flags); |
| 462 | err |= __put_user(current->sas_ss_size, |
| 463 | &frame->rs_uc.uc_stack.ss_size); |
| 464 | err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext); |
| 465 | err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set)); |
| 466 | |
| 467 | if (err) |
| 468 | goto give_sigsegv; |
| 469 | |
| 470 | /* |
| 471 | * Arguments to signal handler: |
| 472 | * |
| 473 | * a0 = signal number |
| 474 | * a1 = 0 (should be cause) |
| 475 | * a2 = pointer to ucontext |
| 476 | * |
| 477 | * $25 and c0_epc point to the signal handler, $29 points to |
| 478 | * the struct rt_sigframe. |
| 479 | */ |
| 480 | regs->regs[ 4] = signr; |
| 481 | regs->regs[ 5] = (unsigned long) &frame->rs_info; |
| 482 | regs->regs[ 6] = (unsigned long) &frame->rs_uc; |
| 483 | regs->regs[29] = (unsigned long) frame; |
| 484 | regs->regs[31] = (unsigned long) frame->rs_code; |
| 485 | regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler; |
| 486 | |
| 487 | #if DEBUG_SIG |
| 488 | printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n", |
| 489 | current->comm, current->pid, |
| 490 | frame, regs->cp0_epc, regs->regs[31]); |
| 491 | #endif |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 492 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | give_sigsegv: |
| 495 | force_sigsegv(signr, current); |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 496 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Ralf Baechle | 129bc8f | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 499 | static inline int handle_signal(unsigned long sig, siginfo_t *info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs) |
| 501 | { |
Ralf Baechle | 129bc8f | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 502 | int ret; |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | switch(regs->regs[0]) { |
| 505 | case ERESTART_RESTARTBLOCK: |
| 506 | case ERESTARTNOHAND: |
| 507 | regs->regs[2] = EINTR; |
| 508 | break; |
| 509 | case ERESTARTSYS: |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 510 | if (!(ka->sa.sa_flags & SA_RESTART)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | regs->regs[2] = EINTR; |
| 512 | break; |
| 513 | } |
| 514 | /* fallthrough */ |
| 515 | case ERESTARTNOINTR: /* Userland will reload $v0. */ |
| 516 | regs->regs[7] = regs->regs[26]; |
| 517 | regs->cp0_epc -= 8; |
| 518 | } |
| 519 | |
| 520 | regs->regs[0] = 0; /* Don't deal with this again. */ |
| 521 | |
Ralf Baechle | e50c0a8 | 2005-05-31 11:49:19 +0000 | [diff] [blame] | 522 | if (sig_uses_siginfo(ka)) |
Ralf Baechle | 129bc8f | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 523 | ret = current->thread.abi->setup_rt_frame(ka, regs, sig, oldset, info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | else |
Ralf Baechle | 129bc8f | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 525 | ret = current->thread.abi->setup_frame(ka, regs, sig, oldset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Steven Rostedt | 69be8f1 | 2005-08-29 11:44:09 -0400 | [diff] [blame] | 527 | spin_lock_irq(¤t->sighand->siglock); |
| 528 | sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask); |
| 529 | if (!(ka->sa.sa_flags & SA_NODEFER)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | sigaddset(¤t->blocked,sig); |
Steven Rostedt | 69be8f1 | 2005-08-29 11:44:09 -0400 | [diff] [blame] | 531 | recalc_sigpending(); |
| 532 | spin_unlock_irq(¤t->sighand->siglock); |
Ralf Baechle | 129bc8f | 2005-07-11 20:45:51 +0000 | [diff] [blame] | 533 | |
| 534 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Ralf Baechle | 40ac5d4 | 2006-02-08 13:38:18 +0000 | [diff] [blame] | 537 | void do_signal(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
| 539 | struct k_sigaction ka; |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 540 | sigset_t *oldset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | siginfo_t info; |
| 542 | int signr; |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | /* |
| 545 | * We want the common case to go fast, which is why we may in certain |
| 546 | * cases get here from kernel mode. Just return without doing anything |
| 547 | * if so. |
| 548 | */ |
| 549 | if (!user_mode(regs)) |
Ralf Baechle | 40ac5d4 | 2006-02-08 13:38:18 +0000 | [diff] [blame] | 550 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 552 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) |
| 553 | oldset = ¤t->saved_sigmask; |
| 554 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | oldset = ¤t->blocked; |
| 556 | |
| 557 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 558 | if (signr > 0) { |
| 559 | /* Whee! Actually deliver the signal. */ |
| 560 | if (handle_signal(signr, &info, &ka, oldset, regs) == 0) { |
| 561 | /* |
| 562 | * A signal was successfully delivered; the saved |
| 563 | * sigmask will have been stored in the signal frame, |
| 564 | * and will be restored by sigreturn, so we can simply |
| 565 | * clear the TIF_RESTORE_SIGMASK flag. |
| 566 | */ |
| 567 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) |
| 568 | clear_thread_flag(TIF_RESTORE_SIGMASK); |
| 569 | } |
Ralf Baechle | 45887e1 | 2006-08-03 21:54:13 +0100 | [diff] [blame] | 570 | |
| 571 | return; |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | /* |
| 575 | * Who's code doesn't conform to the restartable syscall convention |
| 576 | * dies here!!! The li instruction, a single machine instruction, |
| 577 | * must directly be followed by the syscall instruction. |
| 578 | */ |
| 579 | if (regs->regs[0]) { |
| 580 | if (regs->regs[2] == ERESTARTNOHAND || |
| 581 | regs->regs[2] == ERESTARTSYS || |
| 582 | regs->regs[2] == ERESTARTNOINTR) { |
| 583 | regs->regs[7] = regs->regs[26]; |
| 584 | regs->cp0_epc -= 8; |
| 585 | } |
| 586 | if (regs->regs[2] == ERESTART_RESTARTBLOCK) { |
| 587 | regs->regs[2] = __NR_restart_syscall; |
| 588 | regs->regs[7] = regs->regs[26]; |
| 589 | regs->cp0_epc -= 4; |
| 590 | } |
Ralf Baechle | 13fdd31 | 2006-08-08 03:47:01 +0100 | [diff] [blame] | 591 | regs->regs[0] = 0; /* Don't deal with this again. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 593 | |
| 594 | /* |
| 595 | * If there's no signal to deliver, we just put the saved sigmask |
| 596 | * back |
| 597 | */ |
| 598 | if (test_thread_flag(TIF_RESTORE_SIGMASK)) { |
| 599 | clear_thread_flag(TIF_RESTORE_SIGMASK); |
| 600 | sigprocmask(SIG_SETMASK, ¤t->saved_sigmask, NULL); |
| 601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
| 605 | * notification of userspace execution resumption |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 606 | * - triggered by the TIF_WORK_MASK flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | */ |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 608 | asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | __u32 thread_info_flags) |
| 610 | { |
| 611 | /* deal with pending signal delivery */ |
Ralf Baechle | 7b3e2fc | 2006-02-08 12:58:41 +0000 | [diff] [blame] | 612 | if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK)) |
| 613 | current->thread.abi->do_signal(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |