signal32.c 22.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1991, 1992  Linus Torvalds
7
 * Copyright (C) 1994 - 2000, 2006  Ralf Baechle
L
Linus Torvalds 已提交
8 9
 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
 */
10
#include <linux/cache.h>
11
#include <linux/compat.h>
L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19 20 21 22
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/syscalls.h>
#include <linux/errno.h>
#include <linux/wait.h>
#include <linux/ptrace.h>
#include <linux/suspend.h>
#include <linux/compiler.h>
23
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
24

25
#include <asm/abi.h>
L
Linus Torvalds 已提交
26
#include <asm/asm.h>
27
#include <asm/compat-signal.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33
#include <linux/bitops.h>
#include <asm/cacheflush.h>
#include <asm/sim.h>
#include <asm/ucontext.h>
#include <asm/system.h>
#include <asm/fpu.h>
34
#include <asm/war.h>
L
Linus Torvalds 已提交
35

36 37
#include "signal-common.h"

38 39 40 41 42 43 44 45 46
static int (*save_fp_context32)(struct sigcontext32 __user *sc);
static int (*restore_fp_context32)(struct sigcontext32 __user *sc);

extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);

extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);

L
Linus Torvalds 已提交
47 48 49 50 51
/*
 * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
 */
#define __NR_O32_sigreturn		4119
#define __NR_O32_rt_sigreturn		4193
52
#define __NR_O32_restart_syscall        4253
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

/* 32-bit compatibility types */

typedef unsigned int __sighandler32_t;
typedef void (*vfptr_t)(void);

struct sigaction32 {
	unsigned int		sa_flags;
	__sighandler32_t	sa_handler;
	compat_sigset_t		sa_mask;
};

/* IRIX compatible stack_t  */
typedef struct sigaltstack32 {
	s32 ss_sp;
	compat_size_t ss_size;
	int ss_flags;
} stack32_t;

struct ucontext32 {
	u32                 uc_flags;
	s32                 uc_link;
	stack32_t           uc_stack;
	struct sigcontext32 uc_mcontext;
77
	compat_sigset_t     uc_sigmask;   /* mask last for extensibility */
L
Linus Torvalds 已提交
78 79
};

80 81 82 83 84
/*
 * Horribly complicated - with the bloody RM9000 workarounds enabled
 * the signal trampolines is moving to the end of the structure so we can
 * increase the alignment without breaking software compatibility.
 */
85 86
#if ICACHE_REFILLS_WORKAROUND_WAR == 0

87 88 89 90
struct sigframe32 {
	u32 sf_ass[4];		/* argument save space for o32 */
	u32 sf_code[2];		/* signal trampoline */
	struct sigcontext32 sf_sc;
91
	compat_sigset_t sf_mask;
92 93
};

94 95 96 97 98 99 100 101 102
struct rt_sigframe32 {
	u32 rs_ass[4];			/* argument save space for o32 */
	u32 rs_code[2];			/* signal trampoline */
	compat_siginfo_t rs_info;
	struct ucontext32 rs_uc;
};

#else  /* ICACHE_REFILLS_WORKAROUND_WAR */

103 104 105 106
struct sigframe32 {
	u32 sf_ass[4];			/* argument save space for o32 */
	u32 sf_pad[2];
	struct sigcontext32 sf_sc;	/* hw context */
107
	compat_sigset_t sf_mask;
108 109 110
	u32 sf_code[8] ____cacheline_aligned;	/* signal trampoline */
};

111 112 113 114 115 116 117 118 119 120
struct rt_sigframe32 {
	u32 rs_ass[4];			/* argument save space for o32 */
	u32 rs_pad[2];
	compat_siginfo_t rs_info;
	struct ucontext32 rs_uc;
	u32 rs_code[8] __attribute__((aligned(32)));	/* signal trampoline */
};

#endif	/* !ICACHE_REFILLS_WORKAROUND_WAR */

121 122 123
/*
 * sigcontext handlers
 */
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static int protected_save_fp_context32(struct sigcontext32 __user *sc)
{
	int err;
	while (1) {
		lock_fpu_owner();
		own_fpu_inatomic(1);
		err = save_fp_context32(sc); /* this might fail */
		unlock_fpu_owner();
		if (likely(!err))
			break;
		/* touch the sigcontext and try again */
		err = __put_user(0, &sc->sc_fpregs[0]) |
			__put_user(0, &sc->sc_fpregs[31]) |
			__put_user(0, &sc->sc_fpc_csr);
		if (err)
			break;	/* really bad sigcontext */
	}
	return err;
}

static int protected_restore_fp_context32(struct sigcontext32 __user *sc)
{
	int err, tmp;
	while (1) {
		lock_fpu_owner();
		own_fpu_inatomic(0);
		err = restore_fp_context32(sc); /* this might fail */
		unlock_fpu_owner();
		if (likely(!err))
			break;
		/* touch the sigcontext and try again */
		err = __get_user(tmp, &sc->sc_fpregs[0]) |
			__get_user(tmp, &sc->sc_fpregs[31]) |
			__get_user(tmp, &sc->sc_fpc_csr);
		if (err)
			break;	/* really bad sigcontext */
	}
	return err;
}

164 165 166 167 168
static int setup_sigcontext32(struct pt_regs *regs,
			      struct sigcontext32 __user *sc)
{
	int err = 0;
	int i;
169
	u32 used_math;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

	err |= __put_user(regs->cp0_epc, &sc->sc_pc);

	err |= __put_user(0, &sc->sc_regs[0]);
	for (i = 1; i < 32; i++)
		err |= __put_user(regs->regs[i], &sc->sc_regs[i]);

	err |= __put_user(regs->hi, &sc->sc_mdhi);
	err |= __put_user(regs->lo, &sc->sc_mdlo);
	if (cpu_has_dsp) {
		err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
		err |= __put_user(mfhi1(), &sc->sc_hi1);
		err |= __put_user(mflo1(), &sc->sc_lo1);
		err |= __put_user(mfhi2(), &sc->sc_hi2);
		err |= __put_user(mflo2(), &sc->sc_lo2);
		err |= __put_user(mfhi3(), &sc->sc_hi3);
		err |= __put_user(mflo3(), &sc->sc_lo3);
	}

189 190
	used_math = !!used_math();
	err |= __put_user(used_math, &sc->sc_used_math);
191

192
	if (used_math) {
193 194 195 196
		/*
		 * Save FPU state to signal context.  Signal handler
		 * will "inherit" current FPU state.
		 */
197
		err |= protected_save_fp_context32(sc);
198 199 200 201
	}
	return err;
}

202 203 204 205 206 207 208 209
static int
check_and_restore_fp_context32(struct sigcontext32 __user *sc)
{
	int err, sig;

	err = sig = fpcsr_pending(&sc->sc_fpc_csr);
	if (err > 0)
		err = 0;
210
	err |= protected_restore_fp_context32(sc);
211 212 213
	return err ?: sig;
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
static int restore_sigcontext32(struct pt_regs *regs,
				struct sigcontext32 __user *sc)
{
	u32 used_math;
	int err = 0;
	s32 treg;
	int i;

	/* Always make any pending restarted system calls return -EINTR */
	current_thread_info()->restart_block.fn = do_no_restart_syscall;

	err |= __get_user(regs->cp0_epc, &sc->sc_pc);
	err |= __get_user(regs->hi, &sc->sc_mdhi);
	err |= __get_user(regs->lo, &sc->sc_mdlo);
	if (cpu_has_dsp) {
		err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
		err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
		err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
		err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
		err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
		err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
		err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
	}

	for (i = 1; i < 32; i++)
		err |= __get_user(regs->regs[i], &sc->sc_regs[i]);

	err |= __get_user(used_math, &sc->sc_used_math);
	conditional_used_math(used_math);

244
	if (used_math) {
245
		/* restore fpu context if we have used it before */
246 247
		if (!err)
			err = check_and_restore_fp_context32(sc);
248 249
	} else {
		/* signal handler may have used FPU.  Give it up. */
250
		lose_fpu(0);
251 252 253 254 255 256 257 258
	}

	return err;
}

/*
 *
 */
L
Linus Torvalds 已提交
259 260 261
extern void __put_sigset_unknown_nsig(void);
extern void __get_sigset_unknown_nsig(void);

262
static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t __user *ubuf)
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270 271 272
{
	int err = 0;

	if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
		return -EFAULT;

	switch (_NSIG_WORDS) {
	default:
		__put_sigset_unknown_nsig();
	case 2:
273 274
		err |= __put_user(kbuf->sig[1] >> 32, &ubuf->sig[3]);
		err |= __put_user(kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
L
Linus Torvalds 已提交
275
	case 1:
276 277
		err |= __put_user(kbuf->sig[0] >> 32, &ubuf->sig[1]);
		err |= __put_user(kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
L
Linus Torvalds 已提交
278 279 280 281 282
	}

	return err;
}

A
Atsushi Nemoto 已提交
283
static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t __user *ubuf)
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291 292 293 294
{
	int err = 0;
	unsigned long sig[4];

	if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
		return -EFAULT;

	switch (_NSIG_WORDS) {
	default:
		__get_sigset_unknown_nsig();
	case 2:
295 296
		err |= __get_user(sig[3], &ubuf->sig[3]);
		err |= __get_user(sig[2], &ubuf->sig[2]);
L
Linus Torvalds 已提交
297 298
		kbuf->sig[1] = sig[2] | (sig[3] << 32);
	case 1:
299 300
		err |= __get_user(sig[1], &ubuf->sig[1]);
		err |= __get_user(sig[0], &ubuf->sig[0]);
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309 310
		kbuf->sig[0] = sig[0] | (sig[1] << 32);
	}

	return err;
}

/*
 * Atomically swap in the new signal mask, and wait for a signal.
 */

311
asmlinkage int sys32_sigsuspend(nabi_no_regargs struct pt_regs regs)
L
Linus Torvalds 已提交
312
{
A
Atsushi Nemoto 已提交
313
	compat_sigset_t __user *uset;
314
	sigset_t newset;
L
Linus Torvalds 已提交
315

A
Atsushi Nemoto 已提交
316
	uset = (compat_sigset_t __user *) regs.regs[4];
L
Linus Torvalds 已提交
317 318 319 320 321
	if (get_sigset(&newset, uset))
		return -EFAULT;
	sigdelsetmask(&newset, ~_BLOCKABLE);

	spin_lock_irq(&current->sighand->siglock);
322
	current->saved_sigmask = current->blocked;
L
Linus Torvalds 已提交
323 324 325 326
	current->blocked = newset;
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);

327 328 329 330
	current->state = TASK_INTERRUPTIBLE;
	schedule();
	set_thread_flag(TIF_RESTORE_SIGMASK);
	return -ERESTARTNOHAND;
L
Linus Torvalds 已提交
331 332
}

333
asmlinkage int sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
L
Linus Torvalds 已提交
334
{
A
Atsushi Nemoto 已提交
335
	compat_sigset_t __user *uset;
336
	sigset_t newset;
337
	size_t sigsetsize;
L
Linus Torvalds 已提交
338 339 340 341 342 343

	/* XXX Don't preclude handling different sized sigset_t's.  */
	sigsetsize = regs.regs[5];
	if (sigsetsize != sizeof(compat_sigset_t))
		return -EINVAL;

A
Atsushi Nemoto 已提交
344
	uset = (compat_sigset_t __user *) regs.regs[4];
L
Linus Torvalds 已提交
345 346 347 348 349
	if (get_sigset(&newset, uset))
		return -EFAULT;
	sigdelsetmask(&newset, ~_BLOCKABLE);

	spin_lock_irq(&current->sighand->siglock);
350
	current->saved_sigmask = current->blocked;
L
Linus Torvalds 已提交
351
	current->blocked = newset;
352
	recalc_sigpending();
L
Linus Torvalds 已提交
353 354
	spin_unlock_irq(&current->sighand->siglock);

355 356 357 358
	current->state = TASK_INTERRUPTIBLE;
	schedule();
	set_thread_flag(TIF_RESTORE_SIGMASK);
	return -ERESTARTNOHAND;
L
Linus Torvalds 已提交
359 360
}

361 362
SYSCALL_DEFINE3(32_sigaction, long, sig, const struct sigaction32 __user *, act,
	struct sigaction32 __user *, oact)
L
Linus Torvalds 已提交
363 364 365 366 367 368 369
{
	struct k_sigaction new_ka, old_ka;
	int ret;
	int err = 0;

	if (act) {
		old_sigset_t mask;
R
Ralf Baechle 已提交
370
		s32 handler;
L
Linus Torvalds 已提交
371 372 373

		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
			return -EFAULT;
R
Ralf Baechle 已提交
374
		err |= __get_user(handler, &act->sa_handler);
375
		new_ka.sa.sa_handler = (void __user *)(s64)handler;
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387
		err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
		err |= __get_user(mask, &act->sa_mask.sig[0]);
		if (err)
			return -EFAULT;

		siginitset(&new_ka.sa.sa_mask, mask);
	}

	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);

	if (!ret && oact) {
		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
388
			return -EFAULT;
L
Linus Torvalds 已提交
389 390 391 392
		err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
		err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
		                  &oact->sa_handler);
		err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
393 394 395 396
		err |= __put_user(0, &oact->sa_mask.sig[1]);
		err |= __put_user(0, &oact->sa_mask.sig[2]);
		err |= __put_user(0, &oact->sa_mask.sig[3]);
		if (err)
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404
			return -EFAULT;
	}

	return ret;
}

asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs)
{
405 406
	const stack32_t __user *uss = (const stack32_t __user *) regs.regs[4];
	stack32_t __user *uoss = (stack32_t __user *) regs.regs[5];
L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416
	unsigned long usp = regs.regs[29];
	stack_t kss, koss;
	int ret, err = 0;
	mm_segment_t old_fs = get_fs();
	s32 sp;

	if (uss) {
		if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
			return -EFAULT;
		err |= __get_user(sp, &uss->ss_sp);
A
Atsushi Nemoto 已提交
417
		kss.ss_sp = (void __user *) (long) sp;
L
Linus Torvalds 已提交
418 419 420 421 422 423
		err |= __get_user(kss.ss_size, &uss->ss_size);
		err |= __get_user(kss.ss_flags, &uss->ss_flags);
		if (err)
			return -EFAULT;
	}

424
	set_fs(KERNEL_DS);
425 426
	ret = do_sigaltstack(uss ? (stack_t __user *)&kss : NULL,
			     uoss ? (stack_t __user *)&koss : NULL, usp);
427
	set_fs(old_fs);
L
Linus Torvalds 已提交
428 429 430 431

	if (!ret && uoss) {
		if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
			return -EFAULT;
A
Atsushi Nemoto 已提交
432
		sp = (int) (unsigned long) koss.ss_sp;
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441
		err |= __put_user(sp, &uoss->ss_sp);
		err |= __put_user(koss.ss_size, &uoss->ss_size);
		err |= __put_user(koss.ss_flags, &uoss->ss_flags);
		if (err)
			return -EFAULT;
	}
	return ret;
}

442
int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
{
	int err;

	if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
		return -EFAULT;

	/* If you change siginfo_t structure, please be sure
	   this code is fixed accordingly.
	   It should never copy any pad contained in the structure
	   to avoid security leaks, but must copy the generic
	   3 ints plus the relevant union member.
	   This routine must convert siginfo from 64bit to 32bit as well
	   at the same time.  */
	err = __put_user(from->si_signo, &to->si_signo);
	err |= __put_user(from->si_errno, &to->si_errno);
	err |= __put_user((short)from->si_code, &to->si_code);
	if (from->si_code < 0)
		err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
	else {
		switch (from->si_code >> 16) {
463 464 465 466 467
		case __SI_TIMER >> 16:
			err |= __put_user(from->si_tid, &to->si_tid);
			err |= __put_user(from->si_overrun, &to->si_overrun);
			err |= __put_user(from->si_int, &to->si_int);
			break;
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476
		case __SI_CHLD >> 16:
			err |= __put_user(from->si_utime, &to->si_utime);
			err |= __put_user(from->si_stime, &to->si_stime);
			err |= __put_user(from->si_status, &to->si_status);
		default:
			err |= __put_user(from->si_pid, &to->si_pid);
			err |= __put_user(from->si_uid, &to->si_uid);
			break;
		case __SI_FAULT >> 16:
477
			err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
L
Linus Torvalds 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
			break;
		case __SI_POLL >> 16:
			err |= __put_user(from->si_band, &to->si_band);
			err |= __put_user(from->si_fd, &to->si_fd);
			break;
		case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
		case __SI_MESGQ >> 16:
			err |= __put_user(from->si_pid, &to->si_pid);
			err |= __put_user(from->si_uid, &to->si_uid);
			err |= __put_user(from->si_int, &to->si_int);
			break;
		}
	}
	return err;
}

T
Thomas Bogendoerfer 已提交
494 495 496 497 498 499 500 501 502 503 504 505
int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
{
	memset(to, 0, sizeof *to);

	if (copy_from_user(to, from, 3*sizeof(int)) ||
	    copy_from_user(to->_sifields._pad,
			   from->_sifields._pad, SI_PAD_SIZE32))
		return -EFAULT;

	return 0;
}

506
asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
L
Linus Torvalds 已提交
507
{
508
	struct sigframe32 __user *frame;
L
Linus Torvalds 已提交
509
	sigset_t blocked;
510
	int sig;
L
Linus Torvalds 已提交
511

512
	frame = (struct sigframe32 __user *) regs.regs[29];
L
Linus Torvalds 已提交
513 514
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
515
	if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523
		goto badframe;

	sigdelsetmask(&blocked, ~_BLOCKABLE);
	spin_lock_irq(&current->sighand->siglock);
	current->blocked = blocked;
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);

524 525
	sig = restore_sigcontext32(&regs, &frame->sf_sc);
	if (sig < 0)
L
Linus Torvalds 已提交
526
		goto badframe;
527 528
	else if (sig)
		force_sig(sig, current);
L
Linus Torvalds 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

	/*
	 * Don't let your children do this ...
	 */
	__asm__ __volatile__(
		"move\t$29, %0\n\t"
		"j\tsyscall_exit"
		:/* no outputs */
		:"r" (&regs));
	/* Unreached */

badframe:
	force_sig(SIGSEGV, current);
}

544
asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
L
Linus Torvalds 已提交
545
{
546
	struct rt_sigframe32 __user *frame;
547
	mm_segment_t old_fs;
L
Linus Torvalds 已提交
548 549 550
	sigset_t set;
	stack_t st;
	s32 sp;
551
	int sig;
L
Linus Torvalds 已提交
552

553
	frame = (struct rt_sigframe32 __user *) regs.regs[29];
L
Linus Torvalds 已提交
554 555
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
556
	if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
L
Linus Torvalds 已提交
557 558 559 560 561 562 563 564
		goto badframe;

	sigdelsetmask(&set, ~_BLOCKABLE);
	spin_lock_irq(&current->sighand->siglock);
	current->blocked = set;
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);

565 566
	sig = restore_sigcontext32(&regs, &frame->rs_uc.uc_mcontext);
	if (sig < 0)
L
Linus Torvalds 已提交
567
		goto badframe;
568 569
	else if (sig)
		force_sig(sig, current);
L
Linus Torvalds 已提交
570 571 572 573

	/* The ucontext contains a stack32_t, so we must convert!  */
	if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
		goto badframe;
A
Atsushi Nemoto 已提交
574
	st.ss_sp = (void __user *)(long) sp;
L
Linus Torvalds 已提交
575 576 577 578 579 580 581
	if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
		goto badframe;
	if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
		goto badframe;

	/* It is more difficult to avoid calling this function than to
	   call it and ignore errors.  */
582
	old_fs = get_fs();
583
	set_fs(KERNEL_DS);
584
	do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
585
	set_fs(old_fs);
L
Linus Torvalds 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

	/*
	 * Don't let your children do this ...
	 */
	__asm__ __volatile__(
		"move\t$29, %0\n\t"
		"j\tsyscall_exit"
		:/* no outputs */
		:"r" (&regs));
	/* Unreached */

badframe:
	force_sig(SIGSEGV, current);
}

601
static int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
602
	int signr, sigset_t *set)
L
Linus Torvalds 已提交
603
{
604
	struct sigframe32 __user *frame;
L
Linus Torvalds 已提交
605 606 607 608 609 610
	int err = 0;

	frame = get_sigframe(ka, regs, sizeof(*frame));
	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
		goto give_sigsegv;

611
	err |= install_sigtramp(frame->sf_code, __NR_O32_sigreturn);
L
Linus Torvalds 已提交
612 613

	err |= setup_sigcontext32(regs, &frame->sf_sc);
614 615
	err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);

L
Linus Torvalds 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	if (err)
		goto give_sigsegv;

	/*
	 * Arguments to signal handler:
	 *
	 *   a0 = signal number
	 *   a1 = 0 (should be cause)
	 *   a2 = pointer to struct sigcontext
	 *
	 * $25 and c0_epc point to the signal handler, $29 points to the
	 * struct sigframe.
	 */
	regs->regs[ 4] = signr;
	regs->regs[ 5] = 0;
	regs->regs[ 6] = (unsigned long) &frame->sf_sc;
	regs->regs[29] = (unsigned long) frame;
	regs->regs[31] = (unsigned long) frame->sf_code;
	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;

636
	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
L
Linus Torvalds 已提交
637
	       current->comm, current->pid,
638 639
	       frame, regs->cp0_epc, regs->regs[31]);

640
	return 0;
L
Linus Torvalds 已提交
641 642 643

give_sigsegv:
	force_sigsegv(signr, current);
644
	return -EFAULT;
L
Linus Torvalds 已提交
645 646
}

647
static int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
648
	int signr, sigset_t *set, siginfo_t *info)
L
Linus Torvalds 已提交
649
{
650
	struct rt_sigframe32 __user *frame;
L
Linus Torvalds 已提交
651 652 653 654 655 656 657
	int err = 0;
	s32 sp;

	frame = get_sigframe(ka, regs, sizeof(*frame));
	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
		goto give_sigsegv;

658
	err |= install_sigtramp(frame->rs_code, __NR_O32_rt_sigreturn);
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

	/* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
	err |= copy_siginfo_to_user32(&frame->rs_info, info);

	/* Create the ucontext.  */
	err |= __put_user(0, &frame->rs_uc.uc_flags);
	err |= __put_user(0, &frame->rs_uc.uc_link);
	sp = (int) (long) current->sas_ss_sp;
	err |= __put_user(sp,
	                  &frame->rs_uc.uc_stack.ss_sp);
	err |= __put_user(sas_ss_flags(regs->regs[29]),
	                  &frame->rs_uc.uc_stack.ss_flags);
	err |= __put_user(current->sas_ss_size,
	                  &frame->rs_uc.uc_stack.ss_size);
	err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
674
	err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
L
Linus Torvalds 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

	if (err)
		goto give_sigsegv;

	/*
	 * Arguments to signal handler:
	 *
	 *   a0 = signal number
	 *   a1 = 0 (should be cause)
	 *   a2 = pointer to ucontext
	 *
	 * $25 and c0_epc point to the signal handler, $29 points to
	 * the struct rt_sigframe32.
	 */
	regs->regs[ 4] = signr;
	regs->regs[ 5] = (unsigned long) &frame->rs_info;
	regs->regs[ 6] = (unsigned long) &frame->rs_uc;
	regs->regs[29] = (unsigned long) frame;
	regs->regs[31] = (unsigned long) frame->rs_code;
	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;

696
	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
L
Linus Torvalds 已提交
697
	       current->comm, current->pid,
698 699
	       frame, regs->cp0_epc, regs->regs[31]);

700
	return 0;
L
Linus Torvalds 已提交
701 702 703

give_sigsegv:
	force_sigsegv(signr, current);
704
	return -EFAULT;
L
Linus Torvalds 已提交
705 706
}

707 708 709 710 711 712 713 714
/*
 * o32 compatibility on 64-bit kernels, without DSP ASE
 */
struct mips_abi mips_abi_32 = {
	.setup_frame	= setup_frame_32,
	.setup_rt_frame	= setup_rt_frame_32,
	.restart	= __NR_O32_restart_syscall
};
L
Linus Torvalds 已提交
715

716 717 718
SYSCALL_DEFINE4(32_rt_sigaction, int, sig,
	const struct sigaction32 __user *, act,
	struct sigaction32 __user *, oact, unsigned int, sigsetsize)
L
Linus Torvalds 已提交
719 720 721 722 723 724 725 726 727
{
	struct k_sigaction new_sa, old_sa;
	int ret = -EINVAL;

	/* XXX: Don't preclude handling different sized sigset_t's.  */
	if (sigsetsize != sizeof(sigset_t))
		goto out;

	if (act) {
R
Ralf Baechle 已提交
728
		s32 handler;
L
Linus Torvalds 已提交
729 730 731 732
		int err = 0;

		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
			return -EFAULT;
R
Ralf Baechle 已提交
733
		err |= __get_user(handler, &act->sa_handler);
734
		new_sa.sa.sa_handler = (void __user *)(s64)handler;
L
Linus Torvalds 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
		err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
		err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
		if (err)
			return -EFAULT;
	}

	ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);

	if (!ret && oact) {
		int err = 0;

		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
			return -EFAULT;

		err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
		                   &oact->sa_handler);
		err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
		err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
		if (err)
			return -EFAULT;
	}
out:
	return ret;
}

760 761
SYSCALL_DEFINE4(32_rt_sigprocmask, int, how, compat_sigset_t __user *, set,
	compat_sigset_t __user *, oset, unsigned int, sigsetsize)
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769
{
	sigset_t old_set, new_set;
	int ret;
	mm_segment_t old_fs = get_fs();

	if (set && get_sigset(&new_set, set))
		return -EFAULT;

770
	set_fs(KERNEL_DS);
771 772 773
	ret = sys_rt_sigprocmask(how, set ? (sigset_t __user *)&new_set : NULL,
				 oset ? (sigset_t __user *)&old_set : NULL,
				 sigsetsize);
774
	set_fs(old_fs);
L
Linus Torvalds 已提交
775 776 777 778 779 780 781

	if (!ret && oset && put_sigset(&old_set, oset))
		return -EFAULT;

	return ret;
}

782 783
SYSCALL_DEFINE2(32_rt_sigpending, compat_sigset_t __user *, uset,
	unsigned int, sigsetsize)
L
Linus Torvalds 已提交
784 785 786 787 788
{
	int ret;
	sigset_t set;
	mm_segment_t old_fs = get_fs();

789
	set_fs(KERNEL_DS);
790
	ret = sys_rt_sigpending((sigset_t __user *)&set, sigsetsize);
791
	set_fs(old_fs);
L
Linus Torvalds 已提交
792 793 794 795 796 797 798

	if (!ret && put_sigset(&set, uset))
		return -EFAULT;

	return ret;
}

799 800
SYSCALL_DEFINE3(32_rt_sigqueueinfo, int, pid, int, sig,
	compat_siginfo_t __user *, uinfo)
L
Linus Torvalds 已提交
801 802 803 804 805
{
	siginfo_t info;
	int ret;
	mm_segment_t old_fs = get_fs();

806 807
	if (copy_from_user(&info, uinfo, 3*sizeof(int)) ||
	    copy_from_user(info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
L
Linus Torvalds 已提交
808
		return -EFAULT;
809
	set_fs(KERNEL_DS);
810
	ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
811
	set_fs(old_fs);
L
Linus Torvalds 已提交
812 813
	return ret;
}
814

815 816 817
SYSCALL_DEFINE5(32_waitid, int, which, compat_pid_t, pid,
	     compat_siginfo_t __user *, uinfo, int, options,
	     struct compat_rusage __user *, uru)
818 819 820 821 822 823 824
{
	siginfo_t info;
	struct rusage ru;
	long ret;
	mm_segment_t old_fs = get_fs();

	info.si_signo = 0;
825
	set_fs(KERNEL_DS);
826 827
	ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
			 uru ? (struct rusage __user *) &ru : NULL);
828
	set_fs(old_fs);
829 830 831 832 833 834 835 836 837 838 839

	if (ret < 0 || info.si_signo == 0)
		return ret;

	if (uru && (ret = put_compat_rusage(&ru, uru)))
		return ret;

	BUG_ON(info.si_code & __SI_MASK);
	info.si_code |= __SI_CHLD;
	return copy_siginfo_to_user32(uinfo, &info);
}
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

static int signal32_init(void)
{
	if (cpu_has_fpu) {
		save_fp_context32 = _save_fp_context32;
		restore_fp_context32 = _restore_fp_context32;
	} else {
		save_fp_context32 = fpu_emulator_save_context32;
		restore_fp_context32 = fpu_emulator_restore_context32;
	}

	return 0;
}

arch_initcall(signal32_init);