2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
2013-12-13 00:57:19 +08:00
|
|
|
* Copyright (C) 2014, Imagination Technologies Ltd.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2005-06-15 21:00:12 +08:00
|
|
|
#include <linux/cache.h>
|
2013-05-29 07:07:19 +08:00
|
|
|
#include <linux/context_tracking.h>
|
2011-07-27 18:44:47 +08:00
|
|
|
#include <linux/irqflags.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#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>
|
2015-07-30 04:44:53 +08:00
|
|
|
#include <linux/uprobes.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/compiler.h>
|
2009-02-09 00:00:26 +08:00
|
|
|
#include <linux/syscalls.h>
|
2007-04-16 22:19:44 +08:00
|
|
|
#include <linux/uaccess.h>
|
2009-09-09 15:30:21 +08:00
|
|
|
#include <linux/tracehook.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-05-31 19:49:19 +08:00
|
|
|
#include <asm/abi.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#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>
|
2005-06-15 21:00:12 +08:00
|
|
|
#include <asm/war.h>
|
2012-03-29 01:30:02 +08:00
|
|
|
#include <asm/dsp.h>
|
2013-03-26 02:21:11 +08:00
|
|
|
#include <asm/inst.h>
|
2015-07-28 03:58:22 +08:00
|
|
|
#include <asm/msa.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include "signal-common.h"
|
|
|
|
|
2015-07-28 03:58:15 +08:00
|
|
|
static int (*save_fp_context)(void __user *sc);
|
|
|
|
static int (*restore_fp_context)(void __user *sc);
|
2009-11-25 03:35:41 +08:00
|
|
|
|
2007-02-13 09:31:48 +08:00
|
|
|
struct sigframe {
|
|
|
|
u32 sf_ass[4]; /* argument save space for o32 */
|
2010-02-19 08:13:05 +08:00
|
|
|
u32 sf_pad[2]; /* Was: signal trampoline */
|
2015-07-28 03:58:21 +08:00
|
|
|
|
|
|
|
/* Matches struct ucontext from its uc_mcontext field onwards */
|
2007-02-13 09:31:48 +08:00
|
|
|
struct sigcontext sf_sc;
|
|
|
|
sigset_t sf_mask;
|
2015-07-28 03:58:21 +08:00
|
|
|
unsigned long long sf_extcontext[0];
|
2007-02-13 09:31:48 +08:00
|
|
|
};
|
|
|
|
|
2007-02-05 22:24:21 +08:00
|
|
|
struct rt_sigframe {
|
|
|
|
u32 rs_ass[4]; /* argument save space for o32 */
|
2010-02-19 08:13:05 +08:00
|
|
|
u32 rs_pad[2]; /* Was: signal trampoline */
|
2007-02-05 22:24:21 +08:00
|
|
|
struct siginfo rs_info;
|
|
|
|
struct ucontext rs_uc;
|
|
|
|
};
|
|
|
|
|
2014-01-27 23:23:02 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-07-28 03:58:15 +08:00
|
|
|
static int copy_fp_to_sigcontext(void __user *sc)
|
2014-01-27 23:23:02 +08:00
|
|
|
{
|
2015-07-28 03:58:15 +08:00
|
|
|
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;
|
2014-01-27 23:23:02 +08:00
|
|
|
int i;
|
|
|
|
int err = 0;
|
2015-07-28 03:58:17 +08:00
|
|
|
int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
|
2014-01-27 23:23:02 +08:00
|
|
|
|
2015-07-28 03:58:17 +08:00
|
|
|
for (i = 0; i < NUM_FPU_REGS; i += inc) {
|
2014-01-27 23:23:02 +08:00
|
|
|
err |=
|
|
|
|
__put_user(get_fpr64(¤t->thread.fpu.fpr[i], 0),
|
2015-07-28 03:58:15 +08:00
|
|
|
&fpregs[i]);
|
2014-01-27 23:23:02 +08:00
|
|
|
}
|
2015-07-28 03:58:15 +08:00
|
|
|
err |= __put_user(current->thread.fpu.fcr31, csr);
|
2014-01-27 23:23:02 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-07-28 03:58:15 +08:00
|
|
|
static int copy_fp_from_sigcontext(void __user *sc)
|
2014-01-27 23:23:02 +08:00
|
|
|
{
|
2015-07-28 03:58:15 +08:00
|
|
|
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;
|
2014-01-27 23:23:02 +08:00
|
|
|
int i;
|
|
|
|
int err = 0;
|
2015-07-28 03:58:17 +08:00
|
|
|
int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
|
2014-01-27 23:23:02 +08:00
|
|
|
u64 fpr_val;
|
|
|
|
|
2015-07-28 03:58:17 +08:00
|
|
|
for (i = 0; i < NUM_FPU_REGS; i += inc) {
|
2015-07-28 03:58:15 +08:00
|
|
|
err |= __get_user(fpr_val, &fpregs[i]);
|
2014-01-27 23:23:02 +08:00
|
|
|
set_fpr64(¤t->thread.fpu.fpr[i], 0, fpr_val);
|
|
|
|
}
|
2015-07-28 03:58:15 +08:00
|
|
|
err |= __get_user(current->thread.fpu.fcr31, csr);
|
2014-01-27 23:23:02 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-07-28 03:58:15 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2015-07-28 03:58:22 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-08-04 04:45:50 +08:00
|
|
|
BUG_ON(IS_ENABLED(CONFIG_EVA));
|
2015-07-28 03:58:22 +08:00
|
|
|
|
|
|
|
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(¤t->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;
|
|
|
|
|
2016-08-04 04:45:50 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_CPU_HAS_MSA))
|
2016-04-22 01:04:53 +08:00
|
|
|
return SIGSYS;
|
|
|
|
|
2015-07-28 03:58:22 +08:00
|
|
|
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.
|
|
|
|
*/
|
2016-08-04 04:45:50 +08:00
|
|
|
BUG_ON(IS_ENABLED(CONFIG_EVA));
|
2015-07-28 03:58:22 +08:00
|
|
|
|
|
|
|
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(¤t->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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-05 22:24:20 +08:00
|
|
|
/*
|
|
|
|
* Helper routines
|
|
|
|
*/
|
2015-07-28 03:58:18 +08:00
|
|
|
int protected_save_fp_context(void __user *sc)
|
2007-04-16 22:19:44 +08:00
|
|
|
{
|
2015-07-28 03:58:15 +08:00
|
|
|
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;
|
2015-07-28 03:58:16 +08:00
|
|
|
uint32_t __user *used_math = sc + abi->off_sc_used_math;
|
2015-07-28 03:58:22 +08:00
|
|
|
unsigned int used, ext_sz;
|
2007-04-16 22:19:44 +08:00
|
|
|
int err;
|
2015-07-28 03:58:13 +08:00
|
|
|
|
2015-07-28 03:58:20 +08:00
|
|
|
used = used_math() ? USED_FP : 0;
|
2015-07-28 03:58:22 +08:00
|
|
|
if (!used)
|
|
|
|
goto fp_done;
|
2015-07-28 03:58:20 +08:00
|
|
|
|
2015-07-28 03:58:22 +08:00
|
|
|
if (!test_thread_flag(TIF_32BIT_FPREGS))
|
|
|
|
used |= USED_FR1;
|
|
|
|
if (test_thread_flag(TIF_HYBRID_FPREGS))
|
|
|
|
used |= USED_HYBRID_FPRS;
|
2015-07-28 03:58:16 +08:00
|
|
|
|
2015-07-28 03:58:13 +08:00
|
|
|
/*
|
|
|
|
* EVA does not have userland equivalents of ldc1 or sdc1, so
|
|
|
|
* save to the kernel FP context & copy that to userland below.
|
|
|
|
*/
|
2016-08-04 04:45:50 +08:00
|
|
|
if (IS_ENABLED(CONFIG_EVA))
|
2015-07-28 03:58:13 +08:00
|
|
|
lose_fpu(1);
|
|
|
|
|
2007-04-16 22:19:44 +08:00
|
|
|
while (1) {
|
|
|
|
lock_fpu_owner();
|
2014-01-27 23:23:03 +08:00
|
|
|
if (is_fpu_owner()) {
|
|
|
|
err = save_fp_context(sc);
|
|
|
|
unlock_fpu_owner();
|
|
|
|
} else {
|
|
|
|
unlock_fpu_owner();
|
|
|
|
err = copy_fp_to_sigcontext(sc);
|
|
|
|
}
|
2007-04-16 22:19:44 +08:00
|
|
|
if (likely(!err))
|
|
|
|
break;
|
|
|
|
/* touch the sigcontext and try again */
|
2015-07-28 03:58:15 +08:00
|
|
|
err = __put_user(0, &fpregs[0]) |
|
|
|
|
__put_user(0, &fpregs[31]) |
|
|
|
|
__put_user(0, csr);
|
2007-04-16 22:19:44 +08:00
|
|
|
if (err)
|
2015-07-28 03:58:22 +08:00
|
|
|
return err; /* really bad sigcontext */
|
2007-04-16 22:19:44 +08:00
|
|
|
}
|
2015-07-28 03:58:13 +08:00
|
|
|
|
2015-07-28 03:58:22 +08:00
|
|
|
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);
|
2007-04-16 22:19:44 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 03:58:18 +08:00
|
|
|
int protected_restore_fp_context(void __user *sc)
|
2007-04-16 22:19:44 +08:00
|
|
|
{
|
2015-07-28 03:58:15 +08:00
|
|
|
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;
|
2015-07-28 03:58:16 +08:00
|
|
|
uint32_t __user *used_math = sc + abi->off_sc_used_math;
|
|
|
|
unsigned int used;
|
2015-07-28 03:58:22 +08:00
|
|
|
int err, sig = 0, tmp __maybe_unused;
|
2015-07-28 03:58:16 +08:00
|
|
|
|
|
|
|
err = __get_user(used, used_math);
|
2015-07-28 03:58:20 +08:00
|
|
|
conditional_used_math(used & USED_FP);
|
2015-07-28 03:58:16 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The signal handler may have used FPU; give it up if the program
|
|
|
|
* doesn't want it following sigreturn.
|
|
|
|
*/
|
2015-07-28 03:58:22 +08:00
|
|
|
if (err || !(used & USED_FP))
|
2015-07-28 03:58:16 +08:00
|
|
|
lose_fpu(0);
|
2015-07-28 03:58:22 +08:00
|
|
|
if (err)
|
2015-07-28 03:58:16 +08:00
|
|
|
return err;
|
2015-07-28 03:58:22 +08:00
|
|
|
if (!(used & USED_FP))
|
|
|
|
goto fp_done;
|
2015-07-28 03:58:16 +08:00
|
|
|
|
|
|
|
err = sig = fpcsr_pending(csr);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2015-07-28 03:58:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-08-04 04:45:50 +08:00
|
|
|
if (IS_ENABLED(CONFIG_EVA))
|
2015-07-28 03:58:13 +08:00
|
|
|
lose_fpu(0);
|
|
|
|
|
2007-04-16 22:19:44 +08:00
|
|
|
while (1) {
|
|
|
|
lock_fpu_owner();
|
2014-01-27 23:23:03 +08:00
|
|
|
if (is_fpu_owner()) {
|
|
|
|
err = restore_fp_context(sc);
|
|
|
|
unlock_fpu_owner();
|
|
|
|
} else {
|
|
|
|
unlock_fpu_owner();
|
|
|
|
err = copy_fp_from_sigcontext(sc);
|
|
|
|
}
|
2007-04-16 22:19:44 +08:00
|
|
|
if (likely(!err))
|
|
|
|
break;
|
|
|
|
/* touch the sigcontext and try again */
|
2015-07-28 03:58:15 +08:00
|
|
|
err = __get_user(tmp, &fpregs[0]) |
|
|
|
|
__get_user(tmp, &fpregs[31]) |
|
|
|
|
__get_user(tmp, csr);
|
2007-04-16 22:19:44 +08:00
|
|
|
if (err)
|
|
|
|
break; /* really bad sigcontext */
|
|
|
|
}
|
2015-07-28 03:58:13 +08:00
|
|
|
|
2015-07-28 03:58:22 +08:00
|
|
|
fp_done:
|
2016-04-22 01:04:53 +08:00
|
|
|
if (!err && (used & USED_EXTCONTEXT))
|
|
|
|
err = restore_extcontext(sc_to_extcontext(sc));
|
2015-07-28 03:58:22 +08:00
|
|
|
|
2015-07-28 03:58:16 +08:00
|
|
|
return err ?: sig;
|
2007-04-16 22:19:44 +08:00
|
|
|
}
|
|
|
|
|
2007-02-05 22:24:20 +08:00
|
|
|
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]);
|
|
|
|
|
2007-02-03 00:41:47 +08:00
|
|
|
#ifdef CONFIG_CPU_HAS_SMARTMIPS
|
|
|
|
err |= __put_user(regs->acx, &sc->sc_acx);
|
|
|
|
#endif
|
2007-02-05 22:24:20 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-28 03:58:16 +08:00
|
|
|
/*
|
|
|
|
* Save FPU state to signal context. Signal handler
|
|
|
|
* will "inherit" current FPU state.
|
|
|
|
*/
|
|
|
|
err |= protected_save_fp_context(sc);
|
|
|
|
|
2007-02-05 22:24:20 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-07-28 03:58:22 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-03-10 00:03:48 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-02-05 22:24:20 +08:00
|
|
|
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 */
|
2015-02-13 07:01:14 +08:00
|
|
|
current->restart_block.fn = do_no_restart_syscall;
|
2007-02-05 22:24:20 +08:00
|
|
|
|
|
|
|
err |= __get_user(regs->cp0_epc, &sc->sc_pc);
|
2007-02-03 00:41:47 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_CPU_HAS_SMARTMIPS
|
|
|
|
err |= __get_user(regs->acx, &sc->sc_acx);
|
|
|
|
#endif
|
2007-02-05 22:24:20 +08:00
|
|
|
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]);
|
|
|
|
|
2015-07-28 03:58:16 +08:00
|
|
|
return err ?: protected_restore_fp_context(sc);
|
2007-02-05 22:24:20 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 22:35:41 +08:00
|
|
|
void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
|
2007-02-05 22:24:20 +08:00
|
|
|
size_t frame_size)
|
|
|
|
{
|
|
|
|
unsigned long sp;
|
|
|
|
|
2015-07-28 03:58:22 +08:00
|
|
|
/* Leave space for potential extended context */
|
|
|
|
frame_size += extcontext_max_size();
|
|
|
|
|
2007-02-05 22:24:20 +08:00
|
|
|
/* 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;
|
|
|
|
|
2014-03-05 22:35:41 +08:00
|
|
|
sp = sigsp(sp, ksig);
|
2007-02-05 22:24:20 +08:00
|
|
|
|
|
|
|
return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Atomically swap in the new signal mask, and wait for a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_TRAD_SIGNALS
|
2012-12-26 05:25:18 +08:00
|
|
|
SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-12-26 05:25:18 +08:00
|
|
|
return sys_rt_sigsuspend(uset, sizeof(sigset_t));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_TRAD_SIGNALS
|
2009-02-09 00:00:26 +08:00
|
|
|
SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
|
|
|
|
struct sigaction __user *, oact)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
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)))
|
2007-02-05 08:10:11 +08:00
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
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
|
2007-02-05 22:24:27 +08:00
|
|
|
asmlinkage void sys_sigreturn(nabi_no_regargs struct pt_regs regs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-02-01 00:41:09 +08:00
|
|
|
struct sigframe __user *frame;
|
2005-04-17 06:20:36 +08:00
|
|
|
sigset_t blocked;
|
2007-03-10 00:03:48 +08:00
|
|
|
int sig;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-02-01 00:41:09 +08:00
|
|
|
frame = (struct sigframe __user *) regs.regs[29];
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
|
|
|
|
goto badframe;
|
|
|
|
if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
|
|
|
|
goto badframe;
|
|
|
|
|
2012-02-14 19:40:52 +08:00
|
|
|
set_current_blocked(&blocked);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-10 00:03:48 +08:00
|
|
|
sig = restore_sigcontext(®s, &frame->sf_sc);
|
|
|
|
if (sig < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto badframe;
|
2007-03-10 00:03:48 +08:00
|
|
|
else if (sig)
|
|
|
|
force_sig(sig, current);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't let your children do this ...
|
|
|
|
*/
|
|
|
|
__asm__ __volatile__(
|
|
|
|
"move\t$29, %0\n\t"
|
|
|
|
"j\tsyscall_exit"
|
|
|
|
:/* no outputs */
|
|
|
|
:"r" (®s));
|
|
|
|
/* Unreached */
|
|
|
|
|
|
|
|
badframe:
|
|
|
|
force_sig(SIGSEGV, current);
|
|
|
|
}
|
2005-05-31 19:49:19 +08:00
|
|
|
#endif /* CONFIG_TRAD_SIGNALS */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-05 22:24:27 +08:00
|
|
|
asmlinkage void sys_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-02-01 00:41:09 +08:00
|
|
|
struct rt_sigframe __user *frame;
|
2005-04-17 06:20:36 +08:00
|
|
|
sigset_t set;
|
2007-03-10 00:03:48 +08:00
|
|
|
int sig;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-02-01 00:41:09 +08:00
|
|
|
frame = (struct rt_sigframe __user *) regs.regs[29];
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
|
|
|
|
goto badframe;
|
|
|
|
if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
|
|
|
|
goto badframe;
|
|
|
|
|
2012-02-14 19:40:52 +08:00
|
|
|
set_current_blocked(&set);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-03-10 00:03:48 +08:00
|
|
|
sig = restore_sigcontext(®s, &frame->rs_uc.uc_mcontext);
|
|
|
|
if (sig < 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto badframe;
|
2007-03-10 00:03:48 +08:00
|
|
|
else if (sig)
|
|
|
|
force_sig(sig, current);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2012-12-23 16:13:40 +08:00
|
|
|
if (restore_altstack(&frame->rs_uc.uc_stack))
|
|
|
|
goto badframe;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't let your children do this ...
|
|
|
|
*/
|
|
|
|
__asm__ __volatile__(
|
|
|
|
"move\t$29, %0\n\t"
|
|
|
|
"j\tsyscall_exit"
|
|
|
|
:/* no outputs */
|
|
|
|
:"r" (®s));
|
|
|
|
/* Unreached */
|
|
|
|
|
|
|
|
badframe:
|
|
|
|
force_sig(SIGSEGV, current);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_TRAD_SIGNALS
|
2013-10-07 04:25:42 +08:00
|
|
|
static int setup_frame(void *sig_return, struct ksignal *ksig,
|
|
|
|
struct pt_regs *regs, sigset_t *set)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-02-01 00:41:09 +08:00
|
|
|
struct sigframe __user *frame;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err = 0;
|
|
|
|
|
2014-03-05 22:35:41 +08:00
|
|
|
frame = get_sigframe(ksig, regs, sizeof(*frame));
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
|
2013-10-07 04:25:42 +08:00
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
err |= setup_sigcontext(regs, &frame->sf_sc);
|
|
|
|
err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
|
|
|
|
if (err)
|
2013-10-07 04:25:42 +08:00
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-10-07 04:25:42 +08:00
|
|
|
regs->regs[ 4] = ksig->sig;
|
2005-04-17 06:20:36 +08:00
|
|
|
regs->regs[ 5] = 0;
|
|
|
|
regs->regs[ 6] = (unsigned long) &frame->sf_sc;
|
|
|
|
regs->regs[29] = (unsigned long) frame;
|
2010-02-19 08:13:05 +08:00
|
|
|
regs->regs[31] = (unsigned long) sig_return;
|
2013-10-07 04:25:42 +08:00
|
|
|
regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-05 22:24:24 +08:00
|
|
|
DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
current->comm, current->pid,
|
2007-02-05 22:24:24 +08:00
|
|
|
frame, regs->cp0_epc, regs->regs[31]);
|
2007-02-05 08:10:11 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-07 04:25:42 +08:00
|
|
|
static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
|
|
|
|
struct pt_regs *regs, sigset_t *set)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-02-01 00:41:09 +08:00
|
|
|
struct rt_sigframe __user *frame;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err = 0;
|
|
|
|
|
2014-03-05 22:35:41 +08:00
|
|
|
frame = get_sigframe(ksig, regs, sizeof(*frame));
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
|
2013-10-07 04:25:42 +08:00
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Create siginfo. */
|
2013-10-07 04:25:42 +08:00
|
|
|
err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-01-22 19:59:30 +08:00
|
|
|
/* Create the ucontext. */
|
2005-04-17 06:20:36 +08:00
|
|
|
err |= __put_user(0, &frame->rs_uc.uc_flags);
|
2006-02-02 00:26:34 +08:00
|
|
|
err |= __put_user(NULL, &frame->rs_uc.uc_link);
|
2012-12-23 16:13:40 +08:00
|
|
|
err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
|
2005-04-17 06:20:36 +08:00
|
|
|
err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
|
|
|
|
err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
|
|
|
|
|
|
|
|
if (err)
|
2013-10-07 04:25:42 +08:00
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-10-07 04:25:42 +08:00
|
|
|
regs->regs[ 4] = ksig->sig;
|
2005-04-17 06:20:36 +08:00
|
|
|
regs->regs[ 5] = (unsigned long) &frame->rs_info;
|
|
|
|
regs->regs[ 6] = (unsigned long) &frame->rs_uc;
|
|
|
|
regs->regs[29] = (unsigned long) frame;
|
2010-02-19 08:13:05 +08:00
|
|
|
regs->regs[31] = (unsigned long) sig_return;
|
2013-10-07 04:25:42 +08:00
|
|
|
regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-05 22:24:24 +08:00
|
|
|
DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
current->comm, current->pid,
|
|
|
|
frame, regs->cp0_epc, regs->regs[31]);
|
2007-02-05 22:24:24 +08:00
|
|
|
|
2006-02-08 20:58:41 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-02-15 19:40:37 +08:00
|
|
|
struct mips_abi mips_abi = {
|
|
|
|
#ifdef CONFIG_TRAD_SIGNALS
|
|
|
|
.setup_frame = setup_frame,
|
|
|
|
#endif
|
2013-01-22 19:59:30 +08:00
|
|
|
.setup_rt_frame = setup_rt_frame,
|
2015-07-28 03:58:14 +08:00
|
|
|
.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),
|
2015-10-21 16:54:38 +08:00
|
|
|
|
|
|
|
.vdso = &vdso_image,
|
2007-02-15 19:40:37 +08:00
|
|
|
};
|
|
|
|
|
2013-10-07 04:25:42 +08:00
|
|
|
static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-05-02 21:59:21 +08:00
|
|
|
sigset_t *oldset = sigmask_to_save();
|
2005-07-12 04:45:51 +08:00
|
|
|
int ret;
|
2010-02-19 08:13:05 +08:00
|
|
|
struct mips_abi *abi = current->thread.abi;
|
|
|
|
void *vdso = current->mm->context.vdso;
|
2005-07-12 04:45:51 +08:00
|
|
|
|
MIPS: Use per-mm page to execute branch delay slot instructions
In some cases the kernel needs to execute an instruction from the delay
slot of an emulated branch instruction. These cases include:
- Emulated floating point branch instructions (bc1[ft]l?) for systems
which don't include an FPU, or upon which the kernel is run with the
"nofpu" parameter.
- MIPSr6 systems running binaries targeting older revisions of the
architecture, which may include branch instructions whose encodings
are no longer valid in MIPSr6.
Executing instructions from such delay slots is done by writing the
instruction to memory followed by a trap, as part of an "emuframe", and
executing it. This avoids the requirement of an emulator for the entire
MIPS instruction set. Prior to this patch such emuframes are written to
the user stack and executed from there.
This patch moves FP branch delay emuframes off of the user stack and
into a per-mm page. Allocating a page per-mm leaves userland with access
to only what it had access to previously, and compared to other
solutions is relatively simple.
When a thread requires a delay slot emulation, it is allocated a frame.
A thread may only have one frame allocated at any one time, since it may
only ever be executing one instruction at any one time. In order to
ensure that we can free up allocated frame later, its index is recorded
in struct thread_struct. In the typical case, after executing the delay
slot instruction we'll execute a break instruction with the BRK_MEMU
code. This traps back to the kernel & leads to a call to do_dsemulret
which frees the allocated frame & moves the user PC back to the
instruction that would have executed following the emulated branch.
In some cases the delay slot instruction may be invalid, such as a
branch, or may trigger an exception. In these cases the BRK_MEMU break
instruction will not be hit. In order to ensure that frames are freed
this patch introduces dsemul_thread_cleanup() and calls it to free any
allocated frame upon thread exit. If the instruction generated an
exception & leads to a signal being delivered to the thread, or indeed
if a signal simply happens to be delivered to the thread whilst it is
executing from the struct emuframe, then we need to take care to exit
the frame appropriately. This is done by either rolling back the user PC
to the branch or advancing it to the continuation PC prior to signal
delivery, using dsemul_thread_rollback(). If this were not done then a
sigreturn would return to the struct emuframe, and if that frame had
meanwhile been used in response to an emulated branch instruction within
the signal handler then we would execute the wrong user code.
Whilst a user could theoretically place something like a compact branch
to self in a delay slot and cause their thread to become stuck in an
infinite loop with the frame never being deallocated, this would:
- Only affect the users single process.
- Be architecturally invalid since there would be a branch in the
delay slot, which is forbidden.
- Be extremely unlikely to happen by mistake, and provide a program
with no more ability to harm the system than a simple infinite loop
would.
If a thread requires a delay slot emulation & no frame is available to
it (ie. the process has enough other threads that all frames are
currently in use) then the thread joins a waitqueue. It will sleep until
a frame is freed by another thread in the process.
Since we now know whether a thread has an allocated frame due to our
tracking of its index, the cookie field of struct emuframe is removed as
we can be more certain whether we have a valid frame. Since a thread may
only ever have a single frame at any given time, the epc field of struct
emuframe is also removed & the PC to continue from is instead stored in
struct thread_struct. Together these changes simplify & shrink struct
emuframe somewhat, allowing twice as many frames to fit into the page
allocated for them.
The primary benefit of this patch is that we are now free to mark the
user stack non-executable where that is possible.
Signed-off-by: Paul Burton <paul.burton@imgtec.com>
Cc: Leonid Yegoshin <leonid.yegoshin@imgtec.com>
Cc: Maciej Rozycki <maciej.rozycki@imgtec.com>
Cc: Faraz Shahbazker <faraz.shahbazker@imgtec.com>
Cc: Raghu Gandham <raghu.gandham@imgtec.com>
Cc: Matthew Fortune <matthew.fortune@imgtec.com>
Cc: linux-mips@linux-mips.org
Patchwork: https://patchwork.linux-mips.org/patch/13764/
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
2016-07-08 18:06:19 +08:00
|
|
|
/*
|
|
|
|
* If we were emulating a delay slot instruction, exit that frame such
|
|
|
|
* that addresses in the sigframe are as expected for userland and we
|
|
|
|
* don't have a problem if we reuse the thread's frame for an
|
|
|
|
* instruction within the signal handler.
|
|
|
|
*/
|
|
|
|
dsemul_thread_rollback(regs);
|
|
|
|
|
2010-09-29 01:50:37 +08:00
|
|
|
if (regs->regs[0]) {
|
|
|
|
switch(regs->regs[2]) {
|
|
|
|
case ERESTART_RESTARTBLOCK:
|
|
|
|
case ERESTARTNOHAND:
|
2005-04-17 06:20:36 +08:00
|
|
|
regs->regs[2] = EINTR;
|
|
|
|
break;
|
2010-09-29 01:50:37 +08:00
|
|
|
case ERESTARTSYS:
|
2013-10-07 04:25:42 +08:00
|
|
|
if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
|
2010-09-29 01:50:37 +08:00
|
|
|
regs->regs[2] = EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fallthrough */
|
|
|
|
case ERESTARTNOINTR:
|
|
|
|
regs->regs[7] = regs->regs[26];
|
|
|
|
regs->regs[2] = regs->regs[0];
|
|
|
|
regs->cp0_epc -= 4;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-01-22 19:59:30 +08:00
|
|
|
regs->regs[0] = 0; /* Don't deal with this again. */
|
2010-09-29 01:50:37 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-06-15 01:13:53 +08:00
|
|
|
rseq_signal_deliver(regs);
|
|
|
|
|
2015-10-20 02:39:55 +08:00
|
|
|
if (sig_uses_siginfo(&ksig->ka, abi))
|
2015-10-21 16:54:38 +08:00
|
|
|
ret = abi->setup_rt_frame(vdso + abi->vdso->off_rt_sigreturn,
|
2013-10-07 04:25:42 +08:00
|
|
|
ksig, regs, oldset);
|
2005-04-17 06:20:36 +08:00
|
|
|
else
|
2015-10-21 16:54:38 +08:00
|
|
|
ret = abi->setup_frame(vdso + abi->vdso->off_sigreturn,
|
|
|
|
ksig, regs, oldset);
|
2010-09-29 01:50:17 +08:00
|
|
|
|
2013-10-07 04:25:42 +08:00
|
|
|
signal_setup_done(ret, ksig, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-02-15 19:40:37 +08:00
|
|
|
static void do_signal(struct pt_regs *regs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-10-07 04:25:42 +08:00
|
|
|
struct ksignal ksig;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-10-07 04:25:42 +08:00
|
|
|
if (get_signal(&ksig)) {
|
2013-01-22 19:59:30 +08:00
|
|
|
/* Whee! Actually deliver the signal. */
|
2013-10-07 04:25:42 +08:00
|
|
|
handle_signal(&ksig, regs);
|
2006-08-04 04:54:13 +08:00
|
|
|
return;
|
2006-02-08 20:58:41 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (regs->regs[0]) {
|
2012-11-06 21:27:19 +08:00
|
|
|
switch (regs->regs[2]) {
|
|
|
|
case ERESTARTNOHAND:
|
|
|
|
case ERESTARTSYS:
|
|
|
|
case ERESTARTNOINTR:
|
2010-09-29 01:50:37 +08:00
|
|
|
regs->regs[2] = regs->regs[0];
|
2005-04-17 06:20:36 +08:00
|
|
|
regs->regs[7] = regs->regs[26];
|
2010-09-29 01:50:37 +08:00
|
|
|
regs->cp0_epc -= 4;
|
2012-11-06 21:27:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ERESTART_RESTARTBLOCK:
|
2007-02-15 19:40:37 +08:00
|
|
|
regs->regs[2] = current->thread.abi->restart;
|
2005-04-17 06:20:36 +08:00
|
|
|
regs->regs[7] = regs->regs[26];
|
|
|
|
regs->cp0_epc -= 4;
|
2012-11-06 21:27:19 +08:00
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-01-22 19:59:30 +08:00
|
|
|
regs->regs[0] = 0; /* Don't deal with this again. */
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-02-08 20:58:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's no signal to deliver, we just put the saved sigmask
|
|
|
|
* back
|
|
|
|
*/
|
2012-05-22 11:33:55 +08:00
|
|
|
restore_saved_sigmask();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* notification of userspace execution resumption
|
2006-02-08 20:58:41 +08:00
|
|
|
* - triggered by the TIF_WORK_MASK flags
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2006-02-08 20:58:41 +08:00
|
|
|
asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
|
2005-04-17 06:20:36 +08:00
|
|
|
__u32 thread_info_flags)
|
|
|
|
{
|
2011-07-27 18:44:47 +08:00
|
|
|
local_irq_enable();
|
|
|
|
|
2013-05-29 07:07:19 +08:00
|
|
|
user_exit();
|
|
|
|
|
2015-07-30 04:44:53 +08:00
|
|
|
if (thread_info_flags & _TIF_UPROBE)
|
|
|
|
uprobe_notify_resume(regs);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* deal with pending signal delivery */
|
2012-05-24 03:28:58 +08:00
|
|
|
if (thread_info_flags & _TIF_SIGPENDING)
|
2007-02-15 19:40:37 +08:00
|
|
|
do_signal(regs);
|
2009-09-02 16:14:16 +08:00
|
|
|
|
|
|
|
if (thread_info_flags & _TIF_NOTIFY_RESUME) {
|
|
|
|
clear_thread_flag(TIF_NOTIFY_RESUME);
|
|
|
|
tracehook_notify_resume(regs);
|
2018-06-15 01:13:53 +08:00
|
|
|
rseq_handle_notify_resume(regs);
|
2009-09-02 16:14:16 +08:00
|
|
|
}
|
2013-05-29 07:07:19 +08:00
|
|
|
|
|
|
|
user_enter();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2009-11-25 03:35:41 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
2015-07-28 03:58:15 +08:00
|
|
|
static int smp_save_fp_context(void __user *sc)
|
2009-11-25 03:35:41 +08:00
|
|
|
{
|
|
|
|
return raw_cpu_has_fpu
|
2015-07-28 03:58:15 +08:00
|
|
|
? save_hw_fp_context(sc)
|
2014-01-27 23:23:02 +08:00
|
|
|
: copy_fp_to_sigcontext(sc);
|
2009-11-25 03:35:41 +08:00
|
|
|
}
|
|
|
|
|
2015-07-28 03:58:15 +08:00
|
|
|
static int smp_restore_fp_context(void __user *sc)
|
2009-11-25 03:35:41 +08:00
|
|
|
{
|
|
|
|
return raw_cpu_has_fpu
|
2015-07-28 03:58:15 +08:00
|
|
|
? restore_hw_fp_context(sc)
|
2014-01-27 23:23:02 +08:00
|
|
|
: copy_fp_from_sigcontext(sc);
|
2009-11-25 03:35:41 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int signal_setup(void)
|
|
|
|
{
|
2015-07-28 03:58:21 +08:00
|
|
|
/*
|
|
|
|
* 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)));
|
|
|
|
|
2009-11-25 03:35:41 +08:00
|
|
|
#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) {
|
2015-07-28 03:58:15 +08:00
|
|
|
save_fp_context = save_hw_fp_context;
|
|
|
|
restore_fp_context = restore_hw_fp_context;
|
2009-11-25 03:35:41 +08:00
|
|
|
} else {
|
2014-10-28 19:25:51 +08:00
|
|
|
save_fp_context = copy_fp_to_sigcontext;
|
|
|
|
restore_fp_context = copy_fp_from_sigcontext;
|
2009-11-25 03:35:41 +08:00
|
|
|
}
|
2013-12-13 00:57:19 +08:00
|
|
|
#endif /* CONFIG_SMP */
|
2009-11-25 03:35:41 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
arch_initcall(signal_setup);
|