signal.c 22.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * 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
 * Copyright (C) 1994 - 2000  Ralf Baechle
 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9
 * Copyright (C) 2014, Imagination Technologies Ltd.
L
Linus Torvalds 已提交
10
 */
11
#include <linux/cache.h>
12
#include <linux/context_tracking.h>
13
#include <linux/irqflags.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23 24
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/personality.h>
#include <linux/smp.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/errno.h>
#include <linux/wait.h>
#include <linux/ptrace.h>
#include <linux/unistd.h>
#include <linux/compiler.h>
25
#include <linux/syscalls.h>
26
#include <linux/uaccess.h>
27
#include <linux/tracehook.h>
L
Linus Torvalds 已提交
28

29
#include <asm/abi.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36
#include <asm/asm.h>
#include <linux/bitops.h>
#include <asm/cacheflush.h>
#include <asm/fpu.h>
#include <asm/sim.h>
#include <asm/ucontext.h>
#include <asm/cpu-features.h>
37
#include <asm/war.h>
38
#include <asm/vdso.h>
39
#include <asm/dsp.h>
40
#include <asm/inst.h>
41
#include <asm/msa.h>
L
Linus Torvalds 已提交
42 43 44

#include "signal-common.h"

45 46
static int (*save_fp_context)(void __user *sc);
static int (*restore_fp_context)(void __user *sc);
47

48 49
struct sigframe {
	u32 sf_ass[4];		/* argument save space for o32 */
50
	u32 sf_pad[2];		/* Was: signal trampoline */
51 52

	/* Matches struct ucontext from its uc_mcontext field onwards */
53 54
	struct sigcontext sf_sc;
	sigset_t sf_mask;
55
	unsigned long long sf_extcontext[0];
56 57
};

58 59
struct rt_sigframe {
	u32 rs_ass[4];		/* argument save space for o32 */
60
	u32 rs_pad[2];		/* Was: signal trampoline */
61 62 63 64
	struct siginfo rs_info;
	struct ucontext rs_uc;
};

65 66 67 68
/*
 * Thread saved context copy to/from a signal context presumed to be on the
 * user stack, and therefore accessed with appropriate macros from uaccess.h.
 */
69
static int copy_fp_to_sigcontext(void __user *sc)
70
{
71 72 73
	struct mips_abi *abi = current->thread.abi;
	uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
	uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
74 75
	int i;
	int err = 0;
76
	int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
77

78
	for (i = 0; i < NUM_FPU_REGS; i += inc) {
79 80
		err |=
		    __put_user(get_fpr64(&current->thread.fpu.fpr[i], 0),
81
			       &fpregs[i]);
82
	}
83
	err |= __put_user(current->thread.fpu.fcr31, csr);
84 85 86 87

	return err;
}

88
static int copy_fp_from_sigcontext(void __user *sc)
89
{
90 91 92
	struct mips_abi *abi = current->thread.abi;
	uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
	uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
93 94
	int i;
	int err = 0;
95
	int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
96 97
	u64 fpr_val;

98
	for (i = 0; i < NUM_FPU_REGS; i += inc) {
99
		err |= __get_user(fpr_val, &fpregs[i]);
100 101
		set_fpr64(&current->thread.fpu.fpr[i], 0, fpr_val);
	}
102
	err |= __get_user(current->thread.fpu.fcr31, csr);
103 104 105 106

	return err;
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/*
 * Wrappers for the assembly _{save,restore}_fp_context functions.
 */
static int save_hw_fp_context(void __user *sc)
{
	struct mips_abi *abi = current->thread.abi;
	uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
	uint32_t __user *csr = sc + abi->off_sc_fpc_csr;

	return _save_fp_context(fpregs, csr);
}

static int restore_hw_fp_context(void __user *sc)
{
	struct mips_abi *abi = current->thread.abi;
	uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
	uint32_t __user *csr = sc + abi->off_sc_fpc_csr;

	return _restore_fp_context(fpregs, csr);
}

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
/*
 * Extended context handling.
 */

static inline void __user *sc_to_extcontext(void __user *sc)
{
	struct ucontext __user *uc;

	/*
	 * We can just pretend the sigcontext is always embedded in a struct
	 * ucontext here, because the offset from sigcontext to extended
	 * context is the same in the struct sigframe case.
	 */
	uc = container_of(sc, struct ucontext, uc_mcontext);
	return &uc->uc_extcontext;
}

static int save_msa_extcontext(void __user *buf)
{
	struct msa_extcontext __user *msa = buf;
	uint64_t val;
	int i, err;

	if (!thread_msa_context_live())
		return 0;

	/*
	 * Ensure that we can't lose the live MSA context between checking
	 * for it & writing it to memory.
	 */
	preempt_disable();

	if (is_msa_enabled()) {
		/*
		 * There are no EVA versions of the vector register load/store
		 * instructions, so MSA context has to be saved to kernel memory
		 * and then copied to user memory. The save to kernel memory
		 * should already have been done when handling scalar FP
		 * context.
		 */
		BUG_ON(config_enabled(CONFIG_EVA));

		err = __put_user(read_msa_csr(), &msa->csr);
		err |= _save_msa_all_upper(&msa->wr);

		preempt_enable();
	} else {
		preempt_enable();

		err = __put_user(current->thread.fpu.msacsr, &msa->csr);

		for (i = 0; i < NUM_FPU_REGS; i++) {
			val = get_fpr64(&current->thread.fpu.fpr[i], 1);
			err |= __put_user(val, &msa->wr[i]);
		}
	}

	err |= __put_user(MSA_EXTCONTEXT_MAGIC, &msa->ext.magic);
	err |= __put_user(sizeof(*msa), &msa->ext.size);

	return err ? -EFAULT : sizeof(*msa);
}

static int restore_msa_extcontext(void __user *buf, unsigned int size)
{
	struct msa_extcontext __user *msa = buf;
	unsigned long long val;
	unsigned int csr;
	int i, err;

	if (size != sizeof(*msa))
		return -EINVAL;

	err = get_user(csr, &msa->csr);
	if (err)
		return err;

	preempt_disable();

	if (is_msa_enabled()) {
		/*
		 * There are no EVA versions of the vector register load/store
		 * instructions, so MSA context has to be copied to kernel
		 * memory and later loaded to registers. The same is true of
		 * scalar FP context, so FPU & MSA should have already been
		 * disabled whilst handling scalar FP context.
		 */
		BUG_ON(config_enabled(CONFIG_EVA));

		write_msa_csr(csr);
		err |= _restore_msa_all_upper(&msa->wr);
		preempt_enable();
	} else {
		preempt_enable();

		current->thread.fpu.msacsr = csr;

		for (i = 0; i < NUM_FPU_REGS; i++) {
			err |= __get_user(val, &msa->wr[i]);
			set_fpr64(&current->thread.fpu.fpr[i], 1, val);
		}
	}

	return err;
}

static int save_extcontext(void __user *buf)
{
	int sz;

	sz = save_msa_extcontext(buf);
	if (sz < 0)
		return sz;
	buf += sz;

	/* If no context was saved then trivially return */
	if (!sz)
		return 0;

	/* Write the end marker */
	if (__put_user(END_EXTCONTEXT_MAGIC, (u32 *)buf))
		return -EFAULT;

	sz += sizeof(((struct extcontext *)NULL)->magic);
	return sz;
}

static int restore_extcontext(void __user *buf)
{
	struct extcontext ext;
	int err;

	while (1) {
		err = __get_user(ext.magic, (unsigned int *)buf);
		if (err)
			return err;

		if (ext.magic == END_EXTCONTEXT_MAGIC)
			return 0;

		err = __get_user(ext.size, (unsigned int *)(buf
			+ offsetof(struct extcontext, size)));
		if (err)
			return err;

		switch (ext.magic) {
		case MSA_EXTCONTEXT_MAGIC:
			err = restore_msa_extcontext(buf, ext.size);
			break;

		default:
			err = -EINVAL;
			break;
		}

		if (err)
			return err;

		buf += ext.size;
	}
}

290 291 292
/*
 * Helper routines
 */
293
int protected_save_fp_context(void __user *sc)
294
{
295 296 297
	struct mips_abi *abi = current->thread.abi;
	uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
	uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
298
	uint32_t __user *used_math = sc + abi->off_sc_used_math;
299
	unsigned int used, ext_sz;
300
	int err;
301

302
	used = used_math() ? USED_FP : 0;
303 304
	if (!used)
		goto fp_done;
305

306 307 308 309
	if (!test_thread_flag(TIF_32BIT_FPREGS))
		used |= USED_FR1;
	if (test_thread_flag(TIF_HYBRID_FPREGS))
		used |= USED_HYBRID_FPRS;
310

311 312 313 314 315 316 317
	/*
	 * EVA does not have userland equivalents of ldc1 or sdc1, so
	 * save to the kernel FP context & copy that to userland below.
	 */
	if (config_enabled(CONFIG_EVA))
		lose_fpu(1);

318 319
	while (1) {
		lock_fpu_owner();
320 321 322 323 324 325 326
		if (is_fpu_owner()) {
			err = save_fp_context(sc);
			unlock_fpu_owner();
		} else {
			unlock_fpu_owner();
			err = copy_fp_to_sigcontext(sc);
		}
327 328 329
		if (likely(!err))
			break;
		/* touch the sigcontext and try again */
330 331 332
		err = __put_user(0, &fpregs[0]) |
			__put_user(0, &fpregs[31]) |
			__put_user(0, csr);
333
		if (err)
334
			return err;	/* really bad sigcontext */
335
	}
336

337 338 339 340 341 342 343
fp_done:
	ext_sz = err = save_extcontext(sc_to_extcontext(sc));
	if (err < 0)
		return err;
	used |= ext_sz ? USED_EXTCONTEXT : 0;

	return __put_user(used, used_math);
344 345
}

346
int protected_restore_fp_context(void __user *sc)
347
{
348 349 350
	struct mips_abi *abi = current->thread.abi;
	uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
	uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
351 352
	uint32_t __user *used_math = sc + abi->off_sc_used_math;
	unsigned int used;
353
	int err, sig = 0, tmp __maybe_unused;
354 355

	err = __get_user(used, used_math);
356
	conditional_used_math(used & USED_FP);
357 358 359 360 361

	/*
	 * The signal handler may have used FPU; give it up if the program
	 * doesn't want it following sigreturn.
	 */
362
	if (err || !(used & USED_FP))
363
		lose_fpu(0);
364
	if (err)
365
		return err;
366 367
	if (!(used & USED_FP))
		goto fp_done;
368 369 370 371

	err = sig = fpcsr_pending(csr);
	if (err < 0)
		return err;
372 373 374 375 376 377 378 379 380

	/*
	 * EVA does not have userland equivalents of ldc1 or sdc1, so we
	 * disable the FPU here such that the code below simply copies to
	 * the kernel FP context.
	 */
	if (config_enabled(CONFIG_EVA))
		lose_fpu(0);

381 382
	while (1) {
		lock_fpu_owner();
383 384 385 386 387 388 389
		if (is_fpu_owner()) {
			err = restore_fp_context(sc);
			unlock_fpu_owner();
		} else {
			unlock_fpu_owner();
			err = copy_fp_from_sigcontext(sc);
		}
390 391 392
		if (likely(!err))
			break;
		/* touch the sigcontext and try again */
393 394 395
		err = __get_user(tmp, &fpregs[0]) |
			__get_user(tmp, &fpregs[31]) |
			__get_user(tmp, csr);
396 397 398
		if (err)
			break;	/* really bad sigcontext */
	}
399

400 401 402 403
fp_done:
	if (used & USED_EXTCONTEXT)
		err |= restore_extcontext(sc_to_extcontext(sc));

404
	return err ?: sig;
405 406
}

407 408 409 410 411 412 413 414 415 416 417
int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
{
	int err = 0;
	int i;

	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]);

418 419 420
#ifdef CONFIG_CPU_HAS_SMARTMIPS
	err |= __put_user(regs->acx, &sc->sc_acx);
#endif
421 422 423 424 425 426 427 428 429 430 431 432 433
	err |= __put_user(regs->hi, &sc->sc_mdhi);
	err |= __put_user(regs->lo, &sc->sc_mdlo);
	if (cpu_has_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);
		err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
	}


434 435 436 437 438 439
	/*
	 * Save FPU state to signal context. Signal handler
	 * will "inherit" current FPU state.
	 */
	err |= protected_save_fp_context(sc);

440 441 442
	return err;
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
static size_t extcontext_max_size(void)
{
	size_t sz = 0;

	/*
	 * The assumption here is that between this point & the point at which
	 * the extended context is saved the size of the context should only
	 * ever be able to shrink (if the task is preempted), but never grow.
	 * That is, what this function returns is an upper bound on the size of
	 * the extended context for the current task at the current time.
	 */

	if (thread_msa_context_live())
		sz += sizeof(struct msa_extcontext);

	/* If any context is saved then we'll append the end marker */
	if (sz)
		sz += sizeof(((struct extcontext *)NULL)->magic);

	return sz;
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
int fpcsr_pending(unsigned int __user *fpcsr)
{
	int err, sig = 0;
	unsigned int csr, enabled;

	err = __get_user(csr, fpcsr);
	enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
	/*
	 * If the signal handler set some FPU exceptions, clear it and
	 * send SIGFPE.
	 */
	if (csr & enabled) {
		csr &= ~enabled;
		err |= __put_user(csr, fpcsr);
		sig = SIGFPE;
	}
	return err ?: sig;
}

484 485 486 487 488 489 490
int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
{
	unsigned long treg;
	int err = 0;
	int i;

	/* Always make any pending restarted system calls return -EINTR */
491
	current->restart_block.fn = do_no_restart_syscall;
492 493

	err |= __get_user(regs->cp0_epc, &sc->sc_pc);
494 495 496 497

#ifdef CONFIG_CPU_HAS_SMARTMIPS
	err |= __get_user(regs->acx, &sc->sc_acx);
#endif
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	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]);

513
	return err ?: protected_restore_fp_context(sc);
514 515
}

R
Richard Weinberger 已提交
516
void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
517 518 519 520
			  size_t frame_size)
{
	unsigned long sp;

521 522 523
	/* Leave space for potential extended context */
	frame_size += extcontext_max_size();

524 525 526 527 528 529 530 531 532 533
	/* Default to using normal stack */
	sp = regs->regs[29];

	/*
	 * FPU emulator may have it's own trampoline active just
	 * above the user stack, 16-bytes before the next lowest
	 * 16 byte boundary.  Try to avoid trashing it.
	 */
	sp -= 32;

R
Richard Weinberger 已提交
534
	sp = sigsp(sp, ksig);
535 536 537 538

	return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
}

L
Linus Torvalds 已提交
539 540 541 542 543
/*
 * Atomically swap in the new signal mask, and wait for a signal.
 */

#ifdef CONFIG_TRAD_SIGNALS
544
SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
L
Linus Torvalds 已提交
545
{
546
	return sys_rt_sigsuspend(uset, sizeof(sigset_t));
L
Linus Torvalds 已提交
547 548 549 550
}
#endif

#ifdef CONFIG_TRAD_SIGNALS
551 552
SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
	struct sigaction __user *, oact)
L
Linus Torvalds 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
{
	struct k_sigaction new_ka, old_ka;
	int ret;
	int err = 0;

	if (act) {
		old_sigset_t mask;

		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
			return -EFAULT;
		err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
		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)))
R
Ralf Baechle 已提交
576
			return -EFAULT;
L
Linus Torvalds 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
		err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
		err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
		err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
		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)
			return -EFAULT;
	}

	return ret;
}
#endif

#ifdef CONFIG_TRAD_SIGNALS
592
asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
L
Linus Torvalds 已提交
593
{
594
	struct sigframe __user *frame;
L
Linus Torvalds 已提交
595
	sigset_t blocked;
596
	int sig;
L
Linus Torvalds 已提交
597

598
	frame = (struct sigframe __user *) regs.regs[29];
L
Linus Torvalds 已提交
599 600 601 602 603
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
		goto badframe;

604
	set_current_blocked(&blocked);
L
Linus Torvalds 已提交
605

606 607
	sig = restore_sigcontext(&regs, &frame->sf_sc);
	if (sig < 0)
L
Linus Torvalds 已提交
608
		goto badframe;
609 610
	else if (sig)
		force_sig(sig, current);
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624

	/*
	 * 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);
}
625
#endif /* CONFIG_TRAD_SIGNALS */
L
Linus Torvalds 已提交
626

627
asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
L
Linus Torvalds 已提交
628
{
629
	struct rt_sigframe __user *frame;
L
Linus Torvalds 已提交
630
	sigset_t set;
631
	int sig;
L
Linus Torvalds 已提交
632

633
	frame = (struct rt_sigframe __user *) regs.regs[29];
L
Linus Torvalds 已提交
634 635 636 637 638
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
		goto badframe;

639
	set_current_blocked(&set);
L
Linus Torvalds 已提交
640

641 642
	sig = restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext);
	if (sig < 0)
L
Linus Torvalds 已提交
643
		goto badframe;
644 645
	else if (sig)
		force_sig(sig, current);
L
Linus Torvalds 已提交
646

A
Al Viro 已提交
647 648
	if (restore_altstack(&frame->rs_uc.uc_stack))
		goto badframe;
L
Linus Torvalds 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

	/*
	 * 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);
}

#ifdef CONFIG_TRAD_SIGNALS
665 666
static int setup_frame(void *sig_return, struct ksignal *ksig,
		       struct pt_regs *regs, sigset_t *set)
L
Linus Torvalds 已提交
667
{
668
	struct sigframe __user *frame;
L
Linus Torvalds 已提交
669 670
	int err = 0;

R
Richard Weinberger 已提交
671
	frame = get_sigframe(ksig, regs, sizeof(*frame));
L
Linus Torvalds 已提交
672
	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
673
		return -EFAULT;
L
Linus Torvalds 已提交
674 675 676 677

	err |= setup_sigcontext(regs, &frame->sf_sc);
	err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
	if (err)
678
		return -EFAULT;
L
Linus Torvalds 已提交
679 680 681 682 683 684 685 686 687 688 689

	/*
	 * 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.
	 */
690
	regs->regs[ 4] = ksig->sig;
L
Linus Torvalds 已提交
691 692 693
	regs->regs[ 5] = 0;
	regs->regs[ 6] = (unsigned long) &frame->sf_sc;
	regs->regs[29] = (unsigned long) frame;
694
	regs->regs[31] = (unsigned long) sig_return;
695
	regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
L
Linus Torvalds 已提交
696

697
	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
L
Linus Torvalds 已提交
698
	       current->comm, current->pid,
699
	       frame, regs->cp0_epc, regs->regs[31]);
R
Ralf Baechle 已提交
700
	return 0;
L
Linus Torvalds 已提交
701 702 703
}
#endif

704 705
static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
			  struct pt_regs *regs, sigset_t *set)
L
Linus Torvalds 已提交
706
{
707
	struct rt_sigframe __user *frame;
L
Linus Torvalds 已提交
708 709
	int err = 0;

R
Richard Weinberger 已提交
710
	frame = get_sigframe(ksig, regs, sizeof(*frame));
L
Linus Torvalds 已提交
711
	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
712
		return -EFAULT;
L
Linus Torvalds 已提交
713 714

	/* Create siginfo.  */
715
	err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
L
Linus Torvalds 已提交
716

R
Ralf Baechle 已提交
717
	/* Create the ucontext.	 */
L
Linus Torvalds 已提交
718
	err |= __put_user(0, &frame->rs_uc.uc_flags);
719
	err |= __put_user(NULL, &frame->rs_uc.uc_link);
A
Al Viro 已提交
720
	err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
L
Linus Torvalds 已提交
721 722 723 724
	err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
	err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));

	if (err)
725
		return -EFAULT;
L
Linus Torvalds 已提交
726 727 728 729 730 731 732 733 734 735 736

	/*
	 * 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_sigframe.
	 */
737
	regs->regs[ 4] = ksig->sig;
L
Linus Torvalds 已提交
738 739 740
	regs->regs[ 5] = (unsigned long) &frame->rs_info;
	regs->regs[ 6] = (unsigned long) &frame->rs_uc;
	regs->regs[29] = (unsigned long) frame;
741
	regs->regs[31] = (unsigned long) sig_return;
742
	regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
L
Linus Torvalds 已提交
743

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

748
	return 0;
L
Linus Torvalds 已提交
749 750
}

751 752 753
struct mips_abi mips_abi = {
#ifdef CONFIG_TRAD_SIGNALS
	.setup_frame	= setup_frame,
754
	.signal_return_offset = offsetof(struct mips_vdso, signal_trampoline),
755
#endif
R
Ralf Baechle 已提交
756
	.setup_rt_frame = setup_rt_frame,
757 758
	.rt_signal_return_offset =
		offsetof(struct mips_vdso, rt_signal_trampoline),
759 760 761 762 763
	.restart	= __NR_restart_syscall,

	.off_sc_fpregs = offsetof(struct sigcontext, sc_fpregs),
	.off_sc_fpc_csr = offsetof(struct sigcontext, sc_fpc_csr),
	.off_sc_used_math = offsetof(struct sigcontext, sc_used_math),
764 765
};

766
static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
L
Linus Torvalds 已提交
767
{
A
Al Viro 已提交
768
	sigset_t *oldset = sigmask_to_save();
769
	int ret;
770
	struct mips_abi *abi = current->thread.abi;
771 772
#ifdef CONFIG_CPU_MICROMIPS
	void *vdso;
773
	unsigned long tmp = (unsigned long)current->mm->context.vdso;
774 775 776 777

	set_isa16_mode(tmp);
	vdso = (void *)tmp;
#else
778
	void *vdso = current->mm->context.vdso;
779
#endif
780

A
Al Viro 已提交
781 782 783 784
	if (regs->regs[0]) {
		switch(regs->regs[2]) {
		case ERESTART_RESTARTBLOCK:
		case ERESTARTNOHAND:
L
Linus Torvalds 已提交
785 786
			regs->regs[2] = EINTR;
			break;
A
Al Viro 已提交
787
		case ERESTARTSYS:
788
			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
A
Al Viro 已提交
789 790 791 792 793 794 795 796
				regs->regs[2] = EINTR;
				break;
			}
		/* fallthrough */
		case ERESTARTNOINTR:
			regs->regs[7] = regs->regs[26];
			regs->regs[2] = regs->regs[0];
			regs->cp0_epc -= 4;
L
Linus Torvalds 已提交
797 798
		}

R
Ralf Baechle 已提交
799
		regs->regs[0] = 0;		/* Don't deal with this again.	*/
A
Al Viro 已提交
800
	}
L
Linus Torvalds 已提交
801

802
	if (sig_uses_siginfo(&ksig->ka))
803
		ret = abi->setup_rt_frame(vdso + abi->rt_signal_return_offset,
804
					  ksig, regs, oldset);
L
Linus Torvalds 已提交
805
	else
806 807
		ret = abi->setup_frame(vdso + abi->signal_return_offset, ksig,
				       regs, oldset);
808

809
	signal_setup_done(ret, ksig, 0);
L
Linus Torvalds 已提交
810 811
}

812
static void do_signal(struct pt_regs *regs)
L
Linus Torvalds 已提交
813
{
814
	struct ksignal ksig;
L
Linus Torvalds 已提交
815

816
	if (get_signal(&ksig)) {
R
Ralf Baechle 已提交
817
		/* Whee!  Actually deliver the signal.	*/
818
		handle_signal(&ksig, regs);
819
		return;
820
	}
L
Linus Torvalds 已提交
821 822

	if (regs->regs[0]) {
823 824 825 826
		switch (regs->regs[2]) {
		case ERESTARTNOHAND:
		case ERESTARTSYS:
		case ERESTARTNOINTR:
A
Al Viro 已提交
827
			regs->regs[2] = regs->regs[0];
L
Linus Torvalds 已提交
828
			regs->regs[7] = regs->regs[26];
A
Al Viro 已提交
829
			regs->cp0_epc -= 4;
830 831 832
			break;

		case ERESTART_RESTARTBLOCK:
833
			regs->regs[2] = current->thread.abi->restart;
L
Linus Torvalds 已提交
834 835
			regs->regs[7] = regs->regs[26];
			regs->cp0_epc -= 4;
836
			break;
L
Linus Torvalds 已提交
837
		}
R
Ralf Baechle 已提交
838
		regs->regs[0] = 0;	/* Don't deal with this again.	*/
L
Linus Torvalds 已提交
839
	}
840 841 842 843 844

	/*
	 * If there's no signal to deliver, we just put the saved sigmask
	 * back
	 */
A
Al Viro 已提交
845
	restore_saved_sigmask();
L
Linus Torvalds 已提交
846 847 848 849
}

/*
 * notification of userspace execution resumption
850
 * - triggered by the TIF_WORK_MASK flags
L
Linus Torvalds 已提交
851
 */
852
asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
L
Linus Torvalds 已提交
853 854
	__u32 thread_info_flags)
{
855 856
	local_irq_enable();

857 858
	user_exit();

L
Linus Torvalds 已提交
859
	/* deal with pending signal delivery */
860
	if (thread_info_flags & _TIF_SIGPENDING)
861
		do_signal(regs);
862 863 864 865 866

	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
		clear_thread_flag(TIF_NOTIFY_RESUME);
		tracehook_notify_resume(regs);
	}
867 868

	user_enter();
L
Linus Torvalds 已提交
869
}
870 871

#ifdef CONFIG_SMP
872
static int smp_save_fp_context(void __user *sc)
873 874
{
	return raw_cpu_has_fpu
875
	       ? save_hw_fp_context(sc)
876
	       : copy_fp_to_sigcontext(sc);
877 878
}

879
static int smp_restore_fp_context(void __user *sc)
880 881
{
	return raw_cpu_has_fpu
882
	       ? restore_hw_fp_context(sc)
883
	       : copy_fp_from_sigcontext(sc);
884 885 886 887 888
}
#endif

static int signal_setup(void)
{
889 890 891 892 893 894 895 896 897 898
	/*
	 * The offset from sigcontext to extended context should be the same
	 * regardless of the type of signal, such that userland can always know
	 * where to look if it wishes to find the extended context structures.
	 */
	BUILD_BUG_ON((offsetof(struct sigframe, sf_extcontext) -
		      offsetof(struct sigframe, sf_sc)) !=
		     (offsetof(struct rt_sigframe, rs_uc.uc_extcontext) -
		      offsetof(struct rt_sigframe, rs_uc.uc_mcontext)));

899 900 901 902 903 904
#ifdef CONFIG_SMP
	/* For now just do the cpu_has_fpu check when the functions are invoked */
	save_fp_context = smp_save_fp_context;
	restore_fp_context = smp_restore_fp_context;
#else
	if (cpu_has_fpu) {
905 906
		save_fp_context = save_hw_fp_context;
		restore_fp_context = restore_hw_fp_context;
907
	} else {
908 909
		save_fp_context = copy_fp_to_sigcontext;
		restore_fp_context = copy_fp_from_sigcontext;
910
	}
911
#endif /* CONFIG_SMP */
912 913 914 915 916

	return 0;
}

arch_initcall(signal_setup);