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 | */ |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/smp.h> |
| 13 | #include <linux/smp_lock.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/signal.h> |
| 16 | #include <linux/syscalls.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/wait.h> |
| 19 | #include <linux/ptrace.h> |
| 20 | #include <linux/compat.h> |
| 21 | #include <linux/suspend.h> |
| 22 | #include <linux/compiler.h> |
| 23 | |
| 24 | #include <asm/asm.h> |
| 25 | #include <linux/bitops.h> |
| 26 | #include <asm/cacheflush.h> |
| 27 | #include <asm/sim.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/ucontext.h> |
| 30 | #include <asm/system.h> |
| 31 | #include <asm/fpu.h> |
| 32 | |
| 33 | #define SI_PAD_SIZE32 ((SI_MAX_SIZE/sizeof(int)) - 3) |
| 34 | |
| 35 | typedef struct compat_siginfo { |
| 36 | int si_signo; |
| 37 | int si_code; |
| 38 | int si_errno; |
| 39 | |
| 40 | union { |
| 41 | int _pad[SI_PAD_SIZE32]; |
| 42 | |
| 43 | /* kill() */ |
| 44 | struct { |
| 45 | compat_pid_t _pid; /* sender's pid */ |
| 46 | compat_uid_t _uid; /* sender's uid */ |
| 47 | } _kill; |
| 48 | |
| 49 | /* SIGCHLD */ |
| 50 | struct { |
| 51 | compat_pid_t _pid; /* which child */ |
| 52 | compat_uid_t _uid; /* sender's uid */ |
| 53 | int _status; /* exit code */ |
| 54 | compat_clock_t _utime; |
| 55 | compat_clock_t _stime; |
| 56 | } _sigchld; |
| 57 | |
| 58 | /* IRIX SIGCHLD */ |
| 59 | struct { |
| 60 | compat_pid_t _pid; /* which child */ |
| 61 | compat_clock_t _utime; |
| 62 | int _status; /* exit code */ |
| 63 | compat_clock_t _stime; |
| 64 | } _irix_sigchld; |
| 65 | |
| 66 | /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */ |
| 67 | struct { |
| 68 | s32 _addr; /* faulting insn/memory ref. */ |
| 69 | } _sigfault; |
| 70 | |
| 71 | /* SIGPOLL, SIGXFSZ (To do ...) */ |
| 72 | struct { |
| 73 | int _band; /* POLL_IN, POLL_OUT, POLL_MSG */ |
| 74 | int _fd; |
| 75 | } _sigpoll; |
| 76 | |
| 77 | /* POSIX.1b timers */ |
| 78 | struct { |
Ralf Baechle | a982099 | 2005-02-16 21:24:16 +0000 | [diff] [blame^] | 79 | timer_t _tid; /* timer id */ |
| 80 | int _overrun; /* overrun count */ |
| 81 | sigval_t32 _sigval; /* same as below */ |
| 82 | int _sys_private; /* not to be passed to user */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } _timer; |
| 84 | |
| 85 | /* POSIX.1b signals */ |
| 86 | struct { |
| 87 | compat_pid_t _pid; /* sender's pid */ |
| 88 | compat_uid_t _uid; /* sender's uid */ |
| 89 | compat_sigval_t _sigval; |
| 90 | } _rt; |
| 91 | |
| 92 | } _sifields; |
| 93 | } compat_siginfo_t; |
| 94 | |
| 95 | /* |
| 96 | * Including <asm/unistd.h> would give use the 64-bit syscall numbers ... |
| 97 | */ |
| 98 | #define __NR_O32_sigreturn 4119 |
| 99 | #define __NR_O32_rt_sigreturn 4193 |
| 100 | #define __NR_O32_restart_syscall 4253 |
| 101 | |
| 102 | #define DEBUG_SIG 0 |
| 103 | |
| 104 | #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP))) |
| 105 | |
| 106 | extern int do_signal32(sigset_t *oldset, struct pt_regs *regs); |
| 107 | |
| 108 | /* 32-bit compatibility types */ |
| 109 | |
| 110 | #define _NSIG_BPW32 32 |
| 111 | #define _NSIG_WORDS32 (_NSIG / _NSIG_BPW32) |
| 112 | |
| 113 | typedef struct { |
| 114 | unsigned int sig[_NSIG_WORDS32]; |
| 115 | } sigset_t32; |
| 116 | |
| 117 | typedef unsigned int __sighandler32_t; |
| 118 | typedef void (*vfptr_t)(void); |
| 119 | |
| 120 | struct sigaction32 { |
| 121 | unsigned int sa_flags; |
| 122 | __sighandler32_t sa_handler; |
| 123 | compat_sigset_t sa_mask; |
| 124 | }; |
| 125 | |
| 126 | /* IRIX compatible stack_t */ |
| 127 | typedef struct sigaltstack32 { |
| 128 | s32 ss_sp; |
| 129 | compat_size_t ss_size; |
| 130 | int ss_flags; |
| 131 | } stack32_t; |
| 132 | |
| 133 | struct ucontext32 { |
| 134 | u32 uc_flags; |
| 135 | s32 uc_link; |
| 136 | stack32_t uc_stack; |
| 137 | struct sigcontext32 uc_mcontext; |
| 138 | sigset_t32 uc_sigmask; /* mask last for extensibility */ |
| 139 | }; |
| 140 | |
| 141 | extern void __put_sigset_unknown_nsig(void); |
| 142 | extern void __get_sigset_unknown_nsig(void); |
| 143 | |
| 144 | static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t *ubuf) |
| 145 | { |
| 146 | int err = 0; |
| 147 | |
| 148 | if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf))) |
| 149 | return -EFAULT; |
| 150 | |
| 151 | switch (_NSIG_WORDS) { |
| 152 | default: |
| 153 | __put_sigset_unknown_nsig(); |
| 154 | case 2: |
| 155 | err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]); |
| 156 | err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]); |
| 157 | case 1: |
| 158 | err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]); |
| 159 | err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]); |
| 160 | } |
| 161 | |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t *ubuf) |
| 166 | { |
| 167 | int err = 0; |
| 168 | unsigned long sig[4]; |
| 169 | |
| 170 | if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf))) |
| 171 | return -EFAULT; |
| 172 | |
| 173 | switch (_NSIG_WORDS) { |
| 174 | default: |
| 175 | __get_sigset_unknown_nsig(); |
| 176 | case 2: |
| 177 | err |= __get_user (sig[3], &ubuf->sig[3]); |
| 178 | err |= __get_user (sig[2], &ubuf->sig[2]); |
| 179 | kbuf->sig[1] = sig[2] | (sig[3] << 32); |
| 180 | case 1: |
| 181 | err |= __get_user (sig[1], &ubuf->sig[1]); |
| 182 | err |= __get_user (sig[0], &ubuf->sig[0]); |
| 183 | kbuf->sig[0] = sig[0] | (sig[1] << 32); |
| 184 | } |
| 185 | |
| 186 | return err; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Atomically swap in the new signal mask, and wait for a signal. |
| 191 | */ |
| 192 | |
| 193 | save_static_function(sys32_sigsuspend); |
| 194 | __attribute_used__ noinline static int |
| 195 | _sys32_sigsuspend(nabi_no_regargs struct pt_regs regs) |
| 196 | { |
| 197 | compat_sigset_t *uset; |
| 198 | sigset_t newset, saveset; |
| 199 | |
| 200 | uset = (compat_sigset_t *) regs.regs[4]; |
| 201 | if (get_sigset(&newset, uset)) |
| 202 | return -EFAULT; |
| 203 | sigdelsetmask(&newset, ~_BLOCKABLE); |
| 204 | |
| 205 | spin_lock_irq(¤t->sighand->siglock); |
| 206 | saveset = current->blocked; |
| 207 | current->blocked = newset; |
| 208 | recalc_sigpending(); |
| 209 | spin_unlock_irq(¤t->sighand->siglock); |
| 210 | |
| 211 | regs.regs[2] = EINTR; |
| 212 | regs.regs[7] = 1; |
| 213 | while (1) { |
| 214 | current->state = TASK_INTERRUPTIBLE; |
| 215 | schedule(); |
| 216 | if (do_signal32(&saveset, ®s)) |
| 217 | return -EINTR; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | save_static_function(sys32_rt_sigsuspend); |
| 222 | __attribute_used__ noinline static int |
| 223 | _sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs) |
| 224 | { |
| 225 | compat_sigset_t *uset; |
| 226 | sigset_t newset, saveset; |
| 227 | size_t sigsetsize; |
| 228 | |
| 229 | /* XXX Don't preclude handling different sized sigset_t's. */ |
| 230 | sigsetsize = regs.regs[5]; |
| 231 | if (sigsetsize != sizeof(compat_sigset_t)) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | uset = (compat_sigset_t *) regs.regs[4]; |
| 235 | if (get_sigset(&newset, uset)) |
| 236 | return -EFAULT; |
| 237 | sigdelsetmask(&newset, ~_BLOCKABLE); |
| 238 | |
| 239 | spin_lock_irq(¤t->sighand->siglock); |
| 240 | saveset = current->blocked; |
| 241 | current->blocked = newset; |
| 242 | recalc_sigpending(); |
| 243 | spin_unlock_irq(¤t->sighand->siglock); |
| 244 | |
| 245 | regs.regs[2] = EINTR; |
| 246 | regs.regs[7] = 1; |
| 247 | while (1) { |
| 248 | current->state = TASK_INTERRUPTIBLE; |
| 249 | schedule(); |
| 250 | if (do_signal32(&saveset, ®s)) |
| 251 | return -EINTR; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | asmlinkage int sys32_sigaction(int sig, const struct sigaction32 *act, |
| 256 | struct sigaction32 *oact) |
| 257 | { |
| 258 | struct k_sigaction new_ka, old_ka; |
| 259 | int ret; |
| 260 | int err = 0; |
| 261 | |
| 262 | if (act) { |
| 263 | old_sigset_t mask; |
| 264 | |
| 265 | if (!access_ok(VERIFY_READ, act, sizeof(*act))) |
| 266 | return -EFAULT; |
| 267 | err |= __get_user((u32)(u64)new_ka.sa.sa_handler, |
| 268 | &act->sa_handler); |
| 269 | err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags); |
| 270 | err |= __get_user(mask, &act->sa_mask.sig[0]); |
| 271 | if (err) |
| 272 | return -EFAULT; |
| 273 | |
| 274 | siginitset(&new_ka.sa.sa_mask, mask); |
| 275 | } |
| 276 | |
| 277 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); |
| 278 | |
| 279 | if (!ret && oact) { |
| 280 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact))) |
| 281 | return -EFAULT; |
| 282 | err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags); |
| 283 | err |= __put_user((u32)(u64)old_ka.sa.sa_handler, |
| 284 | &oact->sa_handler); |
| 285 | err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig); |
| 286 | err |= __put_user(0, &oact->sa_mask.sig[1]); |
| 287 | err |= __put_user(0, &oact->sa_mask.sig[2]); |
| 288 | err |= __put_user(0, &oact->sa_mask.sig[3]); |
| 289 | if (err) |
| 290 | return -EFAULT; |
| 291 | } |
| 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs) |
| 297 | { |
| 298 | const stack32_t *uss = (const stack32_t *) regs.regs[4]; |
| 299 | stack32_t *uoss = (stack32_t *) regs.regs[5]; |
| 300 | unsigned long usp = regs.regs[29]; |
| 301 | stack_t kss, koss; |
| 302 | int ret, err = 0; |
| 303 | mm_segment_t old_fs = get_fs(); |
| 304 | s32 sp; |
| 305 | |
| 306 | if (uss) { |
| 307 | if (!access_ok(VERIFY_READ, uss, sizeof(*uss))) |
| 308 | return -EFAULT; |
| 309 | err |= __get_user(sp, &uss->ss_sp); |
| 310 | kss.ss_sp = (void *) (long) sp; |
| 311 | err |= __get_user(kss.ss_size, &uss->ss_size); |
| 312 | err |= __get_user(kss.ss_flags, &uss->ss_flags); |
| 313 | if (err) |
| 314 | return -EFAULT; |
| 315 | } |
| 316 | |
| 317 | set_fs (KERNEL_DS); |
| 318 | ret = do_sigaltstack(uss ? &kss : NULL , uoss ? &koss : NULL, usp); |
| 319 | set_fs (old_fs); |
| 320 | |
| 321 | if (!ret && uoss) { |
| 322 | if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss))) |
| 323 | return -EFAULT; |
| 324 | sp = (int) (long) koss.ss_sp; |
| 325 | err |= __put_user(sp, &uoss->ss_sp); |
| 326 | err |= __put_user(koss.ss_size, &uoss->ss_size); |
| 327 | err |= __put_user(koss.ss_flags, &uoss->ss_flags); |
| 328 | if (err) |
| 329 | return -EFAULT; |
| 330 | } |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static int restore_sigcontext32(struct pt_regs *regs, struct sigcontext32 *sc) |
| 335 | { |
| 336 | int err = 0; |
| 337 | __u32 used_math; |
| 338 | |
| 339 | /* Always make any pending restarted system calls return -EINTR */ |
| 340 | current_thread_info()->restart_block.fn = do_no_restart_syscall; |
| 341 | |
| 342 | err |= __get_user(regs->cp0_epc, &sc->sc_pc); |
| 343 | err |= __get_user(regs->hi, &sc->sc_mdhi); |
| 344 | err |= __get_user(regs->lo, &sc->sc_mdlo); |
| 345 | |
| 346 | #define restore_gp_reg(i) do { \ |
| 347 | err |= __get_user(regs->regs[i], &sc->sc_regs[i]); \ |
| 348 | } while(0) |
| 349 | restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3); |
| 350 | restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6); |
| 351 | restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9); |
| 352 | restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12); |
| 353 | restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15); |
| 354 | restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18); |
| 355 | restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21); |
| 356 | restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24); |
| 357 | restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27); |
| 358 | restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30); |
| 359 | restore_gp_reg(31); |
| 360 | #undef restore_gp_reg |
| 361 | |
| 362 | err |= __get_user(used_math, &sc->sc_used_math); |
| 363 | conditional_used_math(used_math); |
| 364 | |
| 365 | preempt_disable(); |
| 366 | |
| 367 | if (used_math()) { |
| 368 | /* restore fpu context if we have used it before */ |
| 369 | own_fpu(); |
| 370 | err |= restore_fp_context32(sc); |
| 371 | } else { |
| 372 | /* signal handler may have used FPU. Give it up. */ |
| 373 | lose_fpu(); |
| 374 | } |
| 375 | |
| 376 | preempt_enable(); |
| 377 | |
| 378 | return err; |
| 379 | } |
| 380 | |
| 381 | struct sigframe { |
| 382 | u32 sf_ass[4]; /* argument save space for o32 */ |
| 383 | u32 sf_code[2]; /* signal trampoline */ |
| 384 | struct sigcontext32 sf_sc; |
| 385 | sigset_t sf_mask; |
| 386 | }; |
| 387 | |
| 388 | struct rt_sigframe32 { |
| 389 | u32 rs_ass[4]; /* argument save space for o32 */ |
| 390 | u32 rs_code[2]; /* signal trampoline */ |
| 391 | compat_siginfo_t rs_info; |
| 392 | struct ucontext32 rs_uc; |
| 393 | }; |
| 394 | |
| 395 | int copy_siginfo_to_user32(compat_siginfo_t *to, siginfo_t *from) |
| 396 | { |
| 397 | int err; |
| 398 | |
| 399 | if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t))) |
| 400 | return -EFAULT; |
| 401 | |
| 402 | /* If you change siginfo_t structure, please be sure |
| 403 | this code is fixed accordingly. |
| 404 | It should never copy any pad contained in the structure |
| 405 | to avoid security leaks, but must copy the generic |
| 406 | 3 ints plus the relevant union member. |
| 407 | This routine must convert siginfo from 64bit to 32bit as well |
| 408 | at the same time. */ |
| 409 | err = __put_user(from->si_signo, &to->si_signo); |
| 410 | err |= __put_user(from->si_errno, &to->si_errno); |
| 411 | err |= __put_user((short)from->si_code, &to->si_code); |
| 412 | if (from->si_code < 0) |
| 413 | err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE); |
| 414 | else { |
| 415 | switch (from->si_code >> 16) { |
Ralf Baechle | a982099 | 2005-02-16 21:24:16 +0000 | [diff] [blame^] | 416 | case __SI_TIMER >> 16: |
| 417 | err |= __put_user(from->si_tid, &to->si_tid); |
| 418 | err |= __put_user(from->si_overrun, &to->si_overrun); |
| 419 | err |= __put_user(from->si_int, &to->si_int); |
| 420 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | case __SI_CHLD >> 16: |
| 422 | err |= __put_user(from->si_utime, &to->si_utime); |
| 423 | err |= __put_user(from->si_stime, &to->si_stime); |
| 424 | err |= __put_user(from->si_status, &to->si_status); |
| 425 | default: |
| 426 | err |= __put_user(from->si_pid, &to->si_pid); |
| 427 | err |= __put_user(from->si_uid, &to->si_uid); |
| 428 | break; |
| 429 | case __SI_FAULT >> 16: |
| 430 | err |= __put_user((long)from->si_addr, &to->si_addr); |
| 431 | break; |
| 432 | case __SI_POLL >> 16: |
| 433 | err |= __put_user(from->si_band, &to->si_band); |
| 434 | err |= __put_user(from->si_fd, &to->si_fd); |
| 435 | break; |
| 436 | case __SI_RT >> 16: /* This is not generated by the kernel as of now. */ |
| 437 | case __SI_MESGQ >> 16: |
| 438 | err |= __put_user(from->si_pid, &to->si_pid); |
| 439 | err |= __put_user(from->si_uid, &to->si_uid); |
| 440 | err |= __put_user(from->si_int, &to->si_int); |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | return err; |
| 445 | } |
| 446 | |
| 447 | save_static_function(sys32_sigreturn); |
| 448 | __attribute_used__ noinline static void |
| 449 | _sys32_sigreturn(nabi_no_regargs struct pt_regs regs) |
| 450 | { |
| 451 | struct sigframe *frame; |
| 452 | sigset_t blocked; |
| 453 | |
| 454 | frame = (struct sigframe *) regs.regs[29]; |
| 455 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 456 | goto badframe; |
| 457 | if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked))) |
| 458 | goto badframe; |
| 459 | |
| 460 | sigdelsetmask(&blocked, ~_BLOCKABLE); |
| 461 | spin_lock_irq(¤t->sighand->siglock); |
| 462 | current->blocked = blocked; |
| 463 | recalc_sigpending(); |
| 464 | spin_unlock_irq(¤t->sighand->siglock); |
| 465 | |
| 466 | if (restore_sigcontext32(®s, &frame->sf_sc)) |
| 467 | goto badframe; |
| 468 | |
| 469 | /* |
| 470 | * Don't let your children do this ... |
| 471 | */ |
| 472 | if (current_thread_info()->flags & TIF_SYSCALL_TRACE) |
| 473 | do_syscall_trace(®s, 1); |
| 474 | __asm__ __volatile__( |
| 475 | "move\t$29, %0\n\t" |
| 476 | "j\tsyscall_exit" |
| 477 | :/* no outputs */ |
| 478 | :"r" (®s)); |
| 479 | /* Unreached */ |
| 480 | |
| 481 | badframe: |
| 482 | force_sig(SIGSEGV, current); |
| 483 | } |
| 484 | |
| 485 | save_static_function(sys32_rt_sigreturn); |
| 486 | __attribute_used__ noinline static void |
| 487 | _sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs) |
| 488 | { |
| 489 | struct rt_sigframe32 *frame; |
| 490 | sigset_t set; |
| 491 | stack_t st; |
| 492 | s32 sp; |
| 493 | |
| 494 | frame = (struct rt_sigframe32 *) regs.regs[29]; |
| 495 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
| 496 | goto badframe; |
| 497 | if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set))) |
| 498 | goto badframe; |
| 499 | |
| 500 | sigdelsetmask(&set, ~_BLOCKABLE); |
| 501 | spin_lock_irq(¤t->sighand->siglock); |
| 502 | current->blocked = set; |
| 503 | recalc_sigpending(); |
| 504 | spin_unlock_irq(¤t->sighand->siglock); |
| 505 | |
| 506 | if (restore_sigcontext32(®s, &frame->rs_uc.uc_mcontext)) |
| 507 | goto badframe; |
| 508 | |
| 509 | /* The ucontext contains a stack32_t, so we must convert! */ |
| 510 | if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp)) |
| 511 | goto badframe; |
| 512 | st.ss_size = (long) sp; |
| 513 | if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size)) |
| 514 | goto badframe; |
| 515 | if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags)) |
| 516 | goto badframe; |
| 517 | |
| 518 | /* It is more difficult to avoid calling this function than to |
| 519 | call it and ignore errors. */ |
| 520 | do_sigaltstack(&st, NULL, regs.regs[29]); |
| 521 | |
| 522 | /* |
| 523 | * Don't let your children do this ... |
| 524 | */ |
| 525 | __asm__ __volatile__( |
| 526 | "move\t$29, %0\n\t" |
| 527 | "j\tsyscall_exit" |
| 528 | :/* no outputs */ |
| 529 | :"r" (®s)); |
| 530 | /* Unreached */ |
| 531 | |
| 532 | badframe: |
| 533 | force_sig(SIGSEGV, current); |
| 534 | } |
| 535 | |
| 536 | static inline int setup_sigcontext32(struct pt_regs *regs, |
| 537 | struct sigcontext32 *sc) |
| 538 | { |
| 539 | int err = 0; |
| 540 | |
| 541 | err |= __put_user(regs->cp0_epc, &sc->sc_pc); |
| 542 | err |= __put_user(regs->cp0_status, &sc->sc_status); |
| 543 | |
| 544 | #define save_gp_reg(i) { \ |
| 545 | err |= __put_user(regs->regs[i], &sc->sc_regs[i]); \ |
| 546 | } while(0) |
| 547 | __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2); |
| 548 | save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6); |
| 549 | save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10); |
| 550 | save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14); |
| 551 | save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18); |
| 552 | save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22); |
| 553 | save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26); |
| 554 | save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30); |
| 555 | save_gp_reg(31); |
| 556 | #undef save_gp_reg |
| 557 | |
| 558 | err |= __put_user(regs->hi, &sc->sc_mdhi); |
| 559 | err |= __put_user(regs->lo, &sc->sc_mdlo); |
| 560 | err |= __put_user(regs->cp0_cause, &sc->sc_cause); |
| 561 | err |= __put_user(regs->cp0_badvaddr, &sc->sc_badvaddr); |
| 562 | |
| 563 | err |= __put_user(!!used_math(), &sc->sc_used_math); |
| 564 | |
| 565 | if (!used_math()) |
| 566 | goto out; |
| 567 | |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 568 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | * Save FPU state to signal context. Signal handler will "inherit" |
| 570 | * current FPU state. |
| 571 | */ |
| 572 | preempt_disable(); |
| 573 | |
| 574 | if (!is_fpu_owner()) { |
| 575 | own_fpu(); |
| 576 | restore_fp(current); |
| 577 | } |
| 578 | err |= save_fp_context32(sc); |
| 579 | |
| 580 | preempt_enable(); |
| 581 | |
| 582 | out: |
| 583 | return err; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Determine which stack to use.. |
| 588 | */ |
| 589 | static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, |
| 590 | size_t frame_size) |
| 591 | { |
| 592 | unsigned long sp; |
| 593 | |
| 594 | /* Default to using normal stack */ |
| 595 | sp = regs->regs[29]; |
| 596 | |
| 597 | /* |
| 598 | * FPU emulator may have it's own trampoline active just |
| 599 | * above the user stack, 16-bytes before the next lowest |
| 600 | * 16 byte boundary. Try to avoid trashing it. |
| 601 | */ |
| 602 | sp -= 32; |
| 603 | |
| 604 | /* This is the X/Open sanctioned signal stack switching. */ |
| 605 | if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0)) |
| 606 | sp = current->sas_ss_sp + current->sas_ss_size; |
| 607 | |
| 608 | return (void *)((sp - frame_size) & ALMASK); |
| 609 | } |
| 610 | |
| 611 | static inline void setup_frame(struct k_sigaction * ka, struct pt_regs *regs, |
| 612 | int signr, sigset_t *set) |
| 613 | { |
| 614 | struct sigframe *frame; |
| 615 | int err = 0; |
| 616 | |
| 617 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 618 | if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame))) |
| 619 | goto give_sigsegv; |
| 620 | |
| 621 | /* |
| 622 | * Set up the return code ... |
| 623 | * |
| 624 | * li v0, __NR_O32_sigreturn |
| 625 | * syscall |
| 626 | */ |
| 627 | err |= __put_user(0x24020000 + __NR_O32_sigreturn, frame->sf_code + 0); |
| 628 | err |= __put_user(0x0000000c , frame->sf_code + 1); |
| 629 | flush_cache_sigtramp((unsigned long) frame->sf_code); |
| 630 | |
| 631 | err |= setup_sigcontext32(regs, &frame->sf_sc); |
| 632 | err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set)); |
| 633 | if (err) |
| 634 | goto give_sigsegv; |
| 635 | |
| 636 | /* |
| 637 | * Arguments to signal handler: |
| 638 | * |
| 639 | * a0 = signal number |
| 640 | * a1 = 0 (should be cause) |
| 641 | * a2 = pointer to struct sigcontext |
| 642 | * |
| 643 | * $25 and c0_epc point to the signal handler, $29 points to the |
| 644 | * struct sigframe. |
| 645 | */ |
| 646 | regs->regs[ 4] = signr; |
| 647 | regs->regs[ 5] = 0; |
| 648 | regs->regs[ 6] = (unsigned long) &frame->sf_sc; |
| 649 | regs->regs[29] = (unsigned long) frame; |
| 650 | regs->regs[31] = (unsigned long) frame->sf_code; |
| 651 | regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler; |
| 652 | |
| 653 | #if DEBUG_SIG |
| 654 | printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n", |
| 655 | current->comm, current->pid, |
| 656 | frame, regs->cp0_epc, frame->sf_code); |
| 657 | #endif |
| 658 | return; |
| 659 | |
| 660 | give_sigsegv: |
| 661 | force_sigsegv(signr, current); |
| 662 | } |
| 663 | |
| 664 | static inline void setup_rt_frame(struct k_sigaction * ka, |
| 665 | struct pt_regs *regs, int signr, |
| 666 | sigset_t *set, siginfo_t *info) |
| 667 | { |
| 668 | struct rt_sigframe32 *frame; |
| 669 | int err = 0; |
| 670 | s32 sp; |
| 671 | |
| 672 | frame = get_sigframe(ka, regs, sizeof(*frame)); |
| 673 | if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame))) |
| 674 | goto give_sigsegv; |
| 675 | |
| 676 | /* Set up to return from userspace. If provided, use a stub already |
| 677 | in userspace. */ |
| 678 | /* |
| 679 | * Set up the return code ... |
| 680 | * |
| 681 | * li v0, __NR_O32_rt_sigreturn |
| 682 | * syscall |
| 683 | */ |
| 684 | err |= __put_user(0x24020000 + __NR_O32_rt_sigreturn, frame->rs_code + 0); |
| 685 | err |= __put_user(0x0000000c , frame->rs_code + 1); |
| 686 | flush_cache_sigtramp((unsigned long) frame->rs_code); |
| 687 | |
| 688 | /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */ |
| 689 | err |= copy_siginfo_to_user32(&frame->rs_info, info); |
| 690 | |
| 691 | /* Create the ucontext. */ |
| 692 | err |= __put_user(0, &frame->rs_uc.uc_flags); |
| 693 | err |= __put_user(0, &frame->rs_uc.uc_link); |
| 694 | sp = (int) (long) current->sas_ss_sp; |
| 695 | err |= __put_user(sp, |
| 696 | &frame->rs_uc.uc_stack.ss_sp); |
| 697 | err |= __put_user(sas_ss_flags(regs->regs[29]), |
| 698 | &frame->rs_uc.uc_stack.ss_flags); |
| 699 | err |= __put_user(current->sas_ss_size, |
| 700 | &frame->rs_uc.uc_stack.ss_size); |
| 701 | err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext); |
| 702 | err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set)); |
| 703 | |
| 704 | if (err) |
| 705 | goto give_sigsegv; |
| 706 | |
| 707 | /* |
| 708 | * Arguments to signal handler: |
| 709 | * |
| 710 | * a0 = signal number |
| 711 | * a1 = 0 (should be cause) |
| 712 | * a2 = pointer to ucontext |
| 713 | * |
| 714 | * $25 and c0_epc point to the signal handler, $29 points to |
| 715 | * the struct rt_sigframe32. |
| 716 | */ |
| 717 | regs->regs[ 4] = signr; |
| 718 | regs->regs[ 5] = (unsigned long) &frame->rs_info; |
| 719 | regs->regs[ 6] = (unsigned long) &frame->rs_uc; |
| 720 | regs->regs[29] = (unsigned long) frame; |
| 721 | regs->regs[31] = (unsigned long) frame->rs_code; |
| 722 | regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler; |
| 723 | |
| 724 | #if DEBUG_SIG |
| 725 | printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n", |
| 726 | current->comm, current->pid, |
| 727 | frame, regs->cp0_epc, frame->rs_code); |
| 728 | #endif |
| 729 | return; |
| 730 | |
| 731 | give_sigsegv: |
| 732 | force_sigsegv(signr, current); |
| 733 | } |
| 734 | |
| 735 | static inline void handle_signal(unsigned long sig, siginfo_t *info, |
| 736 | struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs) |
| 737 | { |
| 738 | switch (regs->regs[0]) { |
| 739 | case ERESTART_RESTARTBLOCK: |
| 740 | case ERESTARTNOHAND: |
| 741 | regs->regs[2] = EINTR; |
| 742 | break; |
| 743 | case ERESTARTSYS: |
| 744 | if(!(ka->sa.sa_flags & SA_RESTART)) { |
| 745 | regs->regs[2] = EINTR; |
| 746 | break; |
| 747 | } |
| 748 | /* fallthrough */ |
| 749 | case ERESTARTNOINTR: /* Userland will reload $v0. */ |
| 750 | regs->regs[7] = regs->regs[26]; |
| 751 | regs->cp0_epc -= 8; |
| 752 | } |
| 753 | |
| 754 | regs->regs[0] = 0; /* Don't deal with this again. */ |
| 755 | |
| 756 | if (ka->sa.sa_flags & SA_SIGINFO) |
| 757 | setup_rt_frame(ka, regs, sig, oldset, info); |
| 758 | else |
| 759 | setup_frame(ka, regs, sig, oldset); |
| 760 | |
Steven Rostedt | 69be8f1 | 2005-08-29 11:44:09 -0400 | [diff] [blame] | 761 | spin_lock_irq(¤t->sighand->siglock); |
| 762 | sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask); |
| 763 | if (!(ka->sa.sa_flags & SA_NODEFER)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | sigaddset(¤t->blocked,sig); |
Steven Rostedt | 69be8f1 | 2005-08-29 11:44:09 -0400 | [diff] [blame] | 765 | recalc_sigpending(); |
| 766 | spin_unlock_irq(¤t->sighand->siglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | int do_signal32(sigset_t *oldset, struct pt_regs *regs) |
| 770 | { |
| 771 | struct k_sigaction ka; |
| 772 | siginfo_t info; |
| 773 | int signr; |
| 774 | |
| 775 | /* |
| 776 | * We want the common case to go fast, which is why we may in certain |
| 777 | * cases get here from kernel mode. Just return without doing anything |
| 778 | * if so. |
| 779 | */ |
| 780 | if (!user_mode(regs)) |
| 781 | return 1; |
| 782 | |
Nigel Cunningham | 0e6c1f5 | 2005-07-27 11:43:34 -0700 | [diff] [blame] | 783 | if (try_to_freeze()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | goto no_signal; |
| 785 | |
| 786 | if (!oldset) |
| 787 | oldset = ¤t->blocked; |
| 788 | |
| 789 | signr = get_signal_to_deliver(&info, &ka, regs, NULL); |
| 790 | if (signr > 0) { |
| 791 | handle_signal(signr, &info, &ka, oldset, regs); |
| 792 | return 1; |
| 793 | } |
| 794 | |
| 795 | no_signal: |
| 796 | /* |
| 797 | * Who's code doesn't conform to the restartable syscall convention |
| 798 | * dies here!!! The li instruction, a single machine instruction, |
| 799 | * must directly be followed by the syscall instruction. |
| 800 | */ |
| 801 | if (regs->regs[0]) { |
| 802 | if (regs->regs[2] == ERESTARTNOHAND || |
| 803 | regs->regs[2] == ERESTARTSYS || |
| 804 | regs->regs[2] == ERESTARTNOINTR) { |
| 805 | regs->regs[7] = regs->regs[26]; |
| 806 | regs->cp0_epc -= 8; |
| 807 | } |
| 808 | if (regs->regs[2] == ERESTART_RESTARTBLOCK) { |
| 809 | regs->regs[2] = __NR_O32_restart_syscall; |
| 810 | regs->regs[7] = regs->regs[26]; |
| 811 | regs->cp0_epc -= 4; |
| 812 | } |
| 813 | } |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 *act, |
| 818 | struct sigaction32 *oact, |
| 819 | unsigned int sigsetsize) |
| 820 | { |
| 821 | struct k_sigaction new_sa, old_sa; |
| 822 | int ret = -EINVAL; |
| 823 | |
| 824 | /* XXX: Don't preclude handling different sized sigset_t's. */ |
| 825 | if (sigsetsize != sizeof(sigset_t)) |
| 826 | goto out; |
| 827 | |
| 828 | if (act) { |
| 829 | int err = 0; |
| 830 | |
| 831 | if (!access_ok(VERIFY_READ, act, sizeof(*act))) |
| 832 | return -EFAULT; |
| 833 | err |= __get_user((u32)(u64)new_sa.sa.sa_handler, |
| 834 | &act->sa_handler); |
| 835 | err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags); |
| 836 | err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask); |
| 837 | if (err) |
| 838 | return -EFAULT; |
| 839 | } |
| 840 | |
| 841 | ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL); |
| 842 | |
| 843 | if (!ret && oact) { |
| 844 | int err = 0; |
| 845 | |
| 846 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact))) |
| 847 | return -EFAULT; |
| 848 | |
| 849 | err |= __put_user((u32)(u64)old_sa.sa.sa_handler, |
| 850 | &oact->sa_handler); |
| 851 | err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags); |
| 852 | err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask); |
| 853 | if (err) |
| 854 | return -EFAULT; |
| 855 | } |
| 856 | out: |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | asmlinkage int sys32_rt_sigprocmask(int how, compat_sigset_t *set, |
| 861 | compat_sigset_t *oset, unsigned int sigsetsize) |
| 862 | { |
| 863 | sigset_t old_set, new_set; |
| 864 | int ret; |
| 865 | mm_segment_t old_fs = get_fs(); |
| 866 | |
| 867 | if (set && get_sigset(&new_set, set)) |
| 868 | return -EFAULT; |
| 869 | |
| 870 | set_fs (KERNEL_DS); |
| 871 | ret = sys_rt_sigprocmask(how, set ? &new_set : NULL, |
| 872 | oset ? &old_set : NULL, sigsetsize); |
| 873 | set_fs (old_fs); |
| 874 | |
| 875 | if (!ret && oset && put_sigset(&old_set, oset)) |
| 876 | return -EFAULT; |
| 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | asmlinkage int sys32_rt_sigpending(compat_sigset_t *uset, |
| 882 | unsigned int sigsetsize) |
| 883 | { |
| 884 | int ret; |
| 885 | sigset_t set; |
| 886 | mm_segment_t old_fs = get_fs(); |
| 887 | |
| 888 | set_fs (KERNEL_DS); |
| 889 | ret = sys_rt_sigpending(&set, sigsetsize); |
| 890 | set_fs (old_fs); |
| 891 | |
| 892 | if (!ret && put_sigset(&set, uset)) |
| 893 | return -EFAULT; |
| 894 | |
| 895 | return ret; |
| 896 | } |
| 897 | |
| 898 | asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t *uinfo) |
| 899 | { |
| 900 | siginfo_t info; |
| 901 | int ret; |
| 902 | mm_segment_t old_fs = get_fs(); |
| 903 | |
| 904 | if (copy_from_user (&info, uinfo, 3*sizeof(int)) || |
| 905 | copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE)) |
| 906 | return -EFAULT; |
| 907 | set_fs (KERNEL_DS); |
| 908 | ret = sys_rt_sigqueueinfo(pid, sig, &info); |
| 909 | set_fs (old_fs); |
| 910 | return ret; |
| 911 | } |
Ralf Baechle | 54f2da7 | 2005-02-16 21:21:29 +0000 | [diff] [blame] | 912 | |
| 913 | asmlinkage long |
| 914 | sys32_waitid(int which, compat_pid_t pid, |
| 915 | compat_siginfo_t __user *uinfo, int options, |
| 916 | struct compat_rusage __user *uru) |
| 917 | { |
| 918 | siginfo_t info; |
| 919 | struct rusage ru; |
| 920 | long ret; |
| 921 | mm_segment_t old_fs = get_fs(); |
| 922 | |
| 923 | info.si_signo = 0; |
| 924 | set_fs (KERNEL_DS); |
| 925 | ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options, |
| 926 | uru ? (struct rusage __user *) &ru : NULL); |
| 927 | set_fs (old_fs); |
| 928 | |
| 929 | if (ret < 0 || info.si_signo == 0) |
| 930 | return ret; |
| 931 | |
| 932 | if (uru && (ret = put_compat_rusage(&ru, uru))) |
| 933 | return ret; |
| 934 | |
| 935 | BUG_ON(info.si_code & __SI_MASK); |
| 936 | info.si_code |= __SI_CHLD; |
| 937 | return copy_siginfo_to_user32(uinfo, &info); |
| 938 | } |