linux/arch/nds32/kernel/fpu.c
Linus Torvalds 5ad18b2e60 Merge branch 'siginfo-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace
Pull force_sig() argument change from Eric Biederman:
 "A source of error over the years has been that force_sig has taken a
  task parameter when it is only safe to use force_sig with the current
  task.

  The force_sig function is built for delivering synchronous signals
  such as SIGSEGV where the userspace application caused a synchronous
  fault (such as a page fault) and the kernel responded with a signal.

  Because the name force_sig does not make this clear, and because the
  force_sig takes a task parameter the function force_sig has been
  abused for sending other kinds of signals over the years. Slowly those
  have been fixed when the oopses have been tracked down.

  This set of changes fixes the remaining abusers of force_sig and
  carefully rips out the task parameter from force_sig and friends
  making this kind of error almost impossible in the future"

* 'siginfo-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm/user-namespace: (27 commits)
  signal/x86: Move tsk inside of CONFIG_MEMORY_FAILURE in do_sigbus
  signal: Remove the signal number and task parameters from force_sig_info
  signal: Factor force_sig_info_to_task out of force_sig_info
  signal: Generate the siginfo in force_sig
  signal: Move the computation of force into send_signal and correct it.
  signal: Properly set TRACE_SIGNAL_LOSE_INFO in __send_signal
  signal: Remove the task parameter from force_sig_fault
  signal: Use force_sig_fault_to_task for the two calls that don't deliver to current
  signal: Explicitly call force_sig_fault on current
  signal/unicore32: Remove tsk parameter from __do_user_fault
  signal/arm: Remove tsk parameter from __do_user_fault
  signal/arm: Remove tsk parameter from ptrace_break
  signal/nds32: Remove tsk parameter from send_sigtrap
  signal/riscv: Remove tsk parameter from do_trap
  signal/sh: Remove tsk parameter from force_sig_info_fault
  signal/um: Remove task parameter from send_sigtrap
  signal/x86: Remove task parameter from send_sigtrap
  signal: Remove task parameter from force_sig_mceerr
  signal: Remove task parameter from force_sig
  signal: Remove task parameter from force_sigsegv
  ...
2019-07-08 21:48:15 -07:00

267 lines
6.8 KiB
C

// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2005-2018 Andes Technology Corporation
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/sched/signal.h>
#include <asm/processor.h>
#include <asm/user.h>
#include <asm/io.h>
#include <asm/bitfield.h>
#include <asm/fpu.h>
const struct fpu_struct init_fpuregs = {
.fd_regs = {[0 ... 31] = sNAN64},
.fpcsr = FPCSR_INIT,
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
.UDF_IEX_trap = 0
#endif
};
void save_fpu(struct task_struct *tsk)
{
unsigned int fpcfg, fpcsr;
enable_fpu();
fpcfg = ((__nds32__fmfcfg() & FPCFG_mskFREG) >> FPCFG_offFREG);
switch (fpcfg) {
case SP32_DP32_reg:
asm volatile ("fsdi $fd31, [%0+0xf8]\n\t"
"fsdi $fd30, [%0+0xf0]\n\t"
"fsdi $fd29, [%0+0xe8]\n\t"
"fsdi $fd28, [%0+0xe0]\n\t"
"fsdi $fd27, [%0+0xd8]\n\t"
"fsdi $fd26, [%0+0xd0]\n\t"
"fsdi $fd25, [%0+0xc8]\n\t"
"fsdi $fd24, [%0+0xc0]\n\t"
"fsdi $fd23, [%0+0xb8]\n\t"
"fsdi $fd22, [%0+0xb0]\n\t"
"fsdi $fd21, [%0+0xa8]\n\t"
"fsdi $fd20, [%0+0xa0]\n\t"
"fsdi $fd19, [%0+0x98]\n\t"
"fsdi $fd18, [%0+0x90]\n\t"
"fsdi $fd17, [%0+0x88]\n\t"
"fsdi $fd16, [%0+0x80]\n\t"
: /* no output */
: "r" (&tsk->thread.fpu)
: "memory");
/* fall through */
case SP32_DP16_reg:
asm volatile ("fsdi $fd15, [%0+0x78]\n\t"
"fsdi $fd14, [%0+0x70]\n\t"
"fsdi $fd13, [%0+0x68]\n\t"
"fsdi $fd12, [%0+0x60]\n\t"
"fsdi $fd11, [%0+0x58]\n\t"
"fsdi $fd10, [%0+0x50]\n\t"
"fsdi $fd9, [%0+0x48]\n\t"
"fsdi $fd8, [%0+0x40]\n\t"
: /* no output */
: "r" (&tsk->thread.fpu)
: "memory");
/* fall through */
case SP16_DP8_reg:
asm volatile ("fsdi $fd7, [%0+0x38]\n\t"
"fsdi $fd6, [%0+0x30]\n\t"
"fsdi $fd5, [%0+0x28]\n\t"
"fsdi $fd4, [%0+0x20]\n\t"
: /* no output */
: "r" (&tsk->thread.fpu)
: "memory");
/* fall through */
case SP8_DP4_reg:
asm volatile ("fsdi $fd3, [%1+0x18]\n\t"
"fsdi $fd2, [%1+0x10]\n\t"
"fsdi $fd1, [%1+0x8]\n\t"
"fsdi $fd0, [%1+0x0]\n\t"
"fmfcsr %0\n\t"
"swi %0, [%1+0x100]\n\t"
: "=&r" (fpcsr)
: "r"(&tsk->thread.fpu)
: "memory");
}
disable_fpu();
}
void load_fpu(const struct fpu_struct *fpregs)
{
unsigned int fpcfg, fpcsr;
enable_fpu();
fpcfg = ((__nds32__fmfcfg() & FPCFG_mskFREG) >> FPCFG_offFREG);
switch (fpcfg) {
case SP32_DP32_reg:
asm volatile ("fldi $fd31, [%0+0xf8]\n\t"
"fldi $fd30, [%0+0xf0]\n\t"
"fldi $fd29, [%0+0xe8]\n\t"
"fldi $fd28, [%0+0xe0]\n\t"
"fldi $fd27, [%0+0xd8]\n\t"
"fldi $fd26, [%0+0xd0]\n\t"
"fldi $fd25, [%0+0xc8]\n\t"
"fldi $fd24, [%0+0xc0]\n\t"
"fldi $fd23, [%0+0xb8]\n\t"
"fldi $fd22, [%0+0xb0]\n\t"
"fldi $fd21, [%0+0xa8]\n\t"
"fldi $fd20, [%0+0xa0]\n\t"
"fldi $fd19, [%0+0x98]\n\t"
"fldi $fd18, [%0+0x90]\n\t"
"fldi $fd17, [%0+0x88]\n\t"
"fldi $fd16, [%0+0x80]\n\t"
: /* no output */
: "r" (fpregs));
/* fall through */
case SP32_DP16_reg:
asm volatile ("fldi $fd15, [%0+0x78]\n\t"
"fldi $fd14, [%0+0x70]\n\t"
"fldi $fd13, [%0+0x68]\n\t"
"fldi $fd12, [%0+0x60]\n\t"
"fldi $fd11, [%0+0x58]\n\t"
"fldi $fd10, [%0+0x50]\n\t"
"fldi $fd9, [%0+0x48]\n\t"
"fldi $fd8, [%0+0x40]\n\t"
: /* no output */
: "r" (fpregs));
/* fall through */
case SP16_DP8_reg:
asm volatile ("fldi $fd7, [%0+0x38]\n\t"
"fldi $fd6, [%0+0x30]\n\t"
"fldi $fd5, [%0+0x28]\n\t"
"fldi $fd4, [%0+0x20]\n\t"
: /* no output */
: "r" (fpregs));
/* fall through */
case SP8_DP4_reg:
asm volatile ("fldi $fd3, [%1+0x18]\n\t"
"fldi $fd2, [%1+0x10]\n\t"
"fldi $fd1, [%1+0x8]\n\t"
"fldi $fd0, [%1+0x0]\n\t"
"lwi %0, [%1+0x100]\n\t"
"fmtcsr %0\n\t":"=&r" (fpcsr)
: "r"(fpregs));
}
disable_fpu();
}
void store_fpu_for_suspend(void)
{
#ifdef CONFIG_LAZY_FPU
if (last_task_used_math != NULL)
save_fpu(last_task_used_math);
last_task_used_math = NULL;
#else
if (!used_math())
return;
unlazy_fpu(current);
#endif
clear_fpu(task_pt_regs(current));
}
inline void do_fpu_context_switch(struct pt_regs *regs)
{
/* Enable to use FPU. */
if (!user_mode(regs)) {
pr_err("BUG: FPU is used in kernel mode.\n");
BUG();
return;
}
enable_ptreg_fpu(regs);
#ifdef CONFIG_LAZY_FPU //Lazy FPU is used
if (last_task_used_math == current)
return;
if (last_task_used_math != NULL)
/* Other processes fpu state, save away */
save_fpu(last_task_used_math);
last_task_used_math = current;
#endif
if (used_math()) {
load_fpu(&current->thread.fpu);
} else {
/* First time FPU user. */
load_fpu(&init_fpuregs);
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
current->thread.fpu.UDF_IEX_trap = init_fpuregs.UDF_IEX_trap;
#endif
set_used_math();
}
}
inline void fill_sigfpe_signo(unsigned int fpcsr, int *signo)
{
if (fpcsr & FPCSR_mskOVFT)
*signo = FPE_FLTOVF;
#ifndef CONFIG_SUPPORT_DENORMAL_ARITHMETIC
else if (fpcsr & FPCSR_mskUDFT)
*signo = FPE_FLTUND;
#endif
else if (fpcsr & FPCSR_mskIVOT)
*signo = FPE_FLTINV;
else if (fpcsr & FPCSR_mskDBZT)
*signo = FPE_FLTDIV;
else if (fpcsr & FPCSR_mskIEXT)
*signo = FPE_FLTRES;
}
inline void handle_fpu_exception(struct pt_regs *regs)
{
unsigned int fpcsr;
int si_code = 0, si_signo = SIGFPE;
#if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
unsigned long redo_except = FPCSR_mskDNIT|FPCSR_mskUDFT|FPCSR_mskIEXT;
#else
unsigned long redo_except = FPCSR_mskDNIT;
#endif
lose_fpu();
fpcsr = current->thread.fpu.fpcsr;
if (fpcsr & redo_except) {
si_signo = do_fpuemu(regs, &current->thread.fpu);
fpcsr = current->thread.fpu.fpcsr;
if (!si_signo) {
current->thread.fpu.fpcsr &= ~(redo_except);
goto done;
}
} else if (fpcsr & FPCSR_mskRIT) {
if (!user_mode(regs))
do_exit(SIGILL);
si_signo = SIGILL;
}
switch (si_signo) {
case SIGFPE:
fill_sigfpe_signo(fpcsr, &si_code);
break;
case SIGILL:
show_regs(regs);
si_code = ILL_COPROC;
break;
case SIGBUS:
si_code = BUS_ADRERR;
break;
default:
break;
}
force_sig_fault(si_signo, si_code,
(void __user *)instruction_pointer(regs));
done:
own_fpu();
}
bool do_fpu_exception(unsigned int subtype, struct pt_regs *regs)
{
int done = true;
/* Coprocessor disabled exception */
if (subtype == FPU_DISABLE_EXCEPTION) {
preempt_disable();
do_fpu_context_switch(regs);
preempt_enable();
}
/* Coprocessor exception such as underflow and overflow */
else if (subtype == FPU_EXCEPTION)
handle_fpu_exception(regs);
else
done = false;
return done;
}