2019-05-19 20:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1994 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Pentium III FXSR, SSE support
|
|
|
|
* General FPU state handling cleanups
|
|
|
|
* Gareth Hughes <gareth@valinux.com>, May 2000
|
|
|
|
*/
|
2015-04-24 08:54:44 +08:00
|
|
|
#include <asm/fpu/internal.h>
|
2015-04-30 14:53:18 +08:00
|
|
|
#include <asm/fpu/regset.h>
|
2015-04-30 14:45:02 +08:00
|
|
|
#include <asm/fpu/signal.h>
|
2016-07-12 00:18:56 +08:00
|
|
|
#include <asm/fpu/types.h>
|
2015-04-30 15:29:38 +08:00
|
|
|
#include <asm/traps.h>
|
2018-07-29 18:15:33 +08:00
|
|
|
#include <asm/irq_regs.h>
|
2015-04-30 14:45:02 +08:00
|
|
|
|
2015-04-26 22:57:55 +08:00
|
|
|
#include <linux/hardirq.h>
|
x86/pkeys: Default to a restrictive init PKRU
PKRU is the register that lets you disallow writes or all access to a given
protection key.
The XSAVE hardware defines an "init state" of 0 for PKRU: its most
permissive state, allowing access/writes to everything. Since we start off
all new processes with the init state, we start all processes off with the
most permissive possible PKRU.
This is unfortunate. If a thread is clone()'d [1] before a program has
time to set PKRU to a restrictive value, that thread will be able to write
to all data, no matter what pkey is set on it. This weakens any integrity
guarantees that we want pkeys to provide.
To fix this, we define a very restrictive PKRU to override the
XSAVE-provided value when we create a new FPU context. We choose a value
that only allows access to pkey 0, which is as restrictive as we can
practically make it.
This does not cause any practical problems with applications using
protection keys because we require them to specify initial permissions for
each key when it is allocated, which override the restrictive default.
In the end, this ensures that threads which do not know how to manage their
own pkey rights can not do damage to data which is pkey-protected.
I would have thought this was a pretty contrived scenario, except that I
heard a bug report from an MPX user who was creating threads in some very
early code before main(). It may be crazy, but folks evidently _do_ it.
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-arch@vger.kernel.org
Cc: Dave Hansen <dave@sr71.net>
Cc: mgorman@techsingularity.net
Cc: arnd@arndb.de
Cc: linux-api@vger.kernel.org
Cc: linux-mm@kvack.org
Cc: luto@kernel.org
Cc: akpm@linux-foundation.org
Cc: torvalds@linux-foundation.org
Link: http://lkml.kernel.org/r/20160729163021.F3C25D4A@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2016-07-30 00:30:21 +08:00
|
|
|
#include <linux/pkeys.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-06-02 01:42:20 +08:00
|
|
|
#define CREATE_TRACE_POINTS
|
|
|
|
#include <asm/trace/fpu.h>
|
|
|
|
|
2015-04-30 17:07:06 +08:00
|
|
|
/*
|
|
|
|
* Represents the initial FPU state. It's mostly (but not completely) zeroes,
|
|
|
|
* depending on the FPU hardware format:
|
|
|
|
*/
|
2015-04-30 23:15:32 +08:00
|
|
|
union fpregs_state init_fpstate __read_mostly;
|
2015-04-30 17:07:06 +08:00
|
|
|
|
2015-04-22 22:52:03 +08:00
|
|
|
/*
|
|
|
|
* Track whether the kernel is using the FPU state
|
|
|
|
* currently.
|
|
|
|
*
|
|
|
|
* This flag is used:
|
|
|
|
*
|
|
|
|
* - by IRQ context code to potentially use the FPU
|
|
|
|
* if it's unused.
|
|
|
|
*
|
|
|
|
* - to debug kernel_fpu_begin()/end() correctness
|
|
|
|
*/
|
2015-01-16 03:19:43 +08:00
|
|
|
static DEFINE_PER_CPU(bool, in_kernel_fpu);
|
|
|
|
|
2015-04-23 18:13:04 +08:00
|
|
|
/*
|
2015-04-23 18:18:28 +08:00
|
|
|
* Track which context is using the FPU on the CPU:
|
2015-04-23 18:13:04 +08:00
|
|
|
*/
|
2015-04-23 18:18:28 +08:00
|
|
|
DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
|
2015-04-23 18:13:04 +08:00
|
|
|
|
2015-04-22 22:52:03 +08:00
|
|
|
static bool kernel_fpu_disabled(void)
|
|
|
|
{
|
|
|
|
return this_cpu_read(in_kernel_fpu);
|
|
|
|
}
|
|
|
|
|
2015-04-22 22:33:08 +08:00
|
|
|
static bool interrupted_kernel_fpu_idle(void)
|
2012-02-22 02:25:45 +08:00
|
|
|
{
|
2016-10-05 08:34:33 +08:00
|
|
|
return !kernel_fpu_disabled();
|
2012-02-22 02:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Were we in user mode (or vm86 mode) when we were
|
|
|
|
* interrupted?
|
|
|
|
*
|
|
|
|
* Doing kernel_fpu_begin/end() is ok if we are running
|
|
|
|
* in an interrupt context from user mode - we'll just
|
|
|
|
* save the FPU state as required.
|
|
|
|
*/
|
2015-04-22 22:33:08 +08:00
|
|
|
static bool interrupted_user_mode(void)
|
2012-02-22 02:25:45 +08:00
|
|
|
{
|
|
|
|
struct pt_regs *regs = get_irq_regs();
|
2015-03-19 09:33:33 +08:00
|
|
|
return regs && user_mode(regs);
|
2012-02-22 02:25:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Can we use the FPU in kernel mode with the
|
|
|
|
* whole "kernel_fpu_begin/end()" sequence?
|
|
|
|
*
|
|
|
|
* It's always ok in process context (ie "not interrupt")
|
|
|
|
* but it is sometimes ok even from an irq.
|
|
|
|
*/
|
|
|
|
bool irq_fpu_usable(void)
|
|
|
|
{
|
|
|
|
return !in_interrupt() ||
|
|
|
|
interrupted_user_mode() ||
|
|
|
|
interrupted_kernel_fpu_idle();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(irq_fpu_usable);
|
|
|
|
|
x86/fpu: Use proper mask to replace full instruction mask
When saving xstate to a kernel/user XSAVE area with the XSAVE family of
instructions, the current code applies the 'full' instruction mask (-1),
which tries to XSAVE all possible features. This method relies on
hardware to trim 'all possible' down to what is enabled in the
hardware. The code works well for now. However, there will be a
problem, if some features are enabled in hardware, but are not suitable
to be saved into all kernel XSAVE buffers, like task->fpu, due to
performance consideration.
One such example is the Last Branch Records (LBR) state. The LBR state
only contains valuable information when LBR is explicitly enabled by
the perf subsystem, and the size of an LBR state is large (808 bytes
for now). To avoid both CPU overhead and space overhead at each context
switch, the LBR state should not be saved into task->fpu like other
state components. It should be saved/restored on demand when LBR is
enabled in the perf subsystem. Current copy_xregs_to_* will trigger a
buffer overflow for such cases.
Three sites use the '-1' instruction mask which must be updated.
Two are saving/restoring the xstate to/from a kernel-allocated XSAVE
buffer and can use 'xfeatures_mask_all', which will save/restore all of
the features present in a normal task FPU buffer.
The last one saves the register state directly to a user buffer. It
could
also use 'xfeatures_mask_all'. Just as it was with the '-1' argument,
any supervisor states in the mask will be filtered out by the hardware
and not saved to the buffer. But, to be more explicit about what is
expected to be saved, use xfeatures_mask_user() for the instruction
mask.
KVM includes the header file fpu/internal.h. To avoid 'undefined
xfeatures_mask_all' compiling issue, move copy_fpregs_to_fpstate() to
fpu/core.c and export it, because:
- The xfeatures_mask_all is indirectly used via copy_fpregs_to_fpstate()
by KVM. The function which is directly used by other modules should be
exported.
- The copy_fpregs_to_fpstate() is a function, while xfeatures_mask_all
is a variable for the "internal" FPU state. It's safer to export a
function than a variable, which may be implicitly changed by others.
- The copy_fpregs_to_fpstate() is a big function with many checks. The
removal of the inline keyword should not impact the performance.
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Dave Hansen <dave.hansen@intel.com>
Link: https://lkml.kernel.org/r/1593780569-62993-20-git-send-email-kan.liang@linux.intel.com
2020-07-03 20:49:25 +08:00
|
|
|
/*
|
|
|
|
* These must be called with preempt disabled. Returns
|
|
|
|
* 'true' if the FPU state is still intact and we can
|
|
|
|
* keep registers active.
|
|
|
|
*
|
|
|
|
* The legacy FNSAVE instruction cleared all FPU state
|
|
|
|
* unconditionally, so registers are essentially destroyed.
|
|
|
|
* Modern FPU state can be kept in registers, if there are
|
|
|
|
* no pending FP exceptions.
|
|
|
|
*/
|
|
|
|
int copy_fpregs_to_fpstate(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
if (likely(use_xsave())) {
|
|
|
|
copy_xregs_to_kernel(&fpu->state.xsave);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AVX512 state is tracked here because its use is
|
|
|
|
* known to slow the max clock speed of the core.
|
|
|
|
*/
|
|
|
|
if (fpu->state.xsave.header.xfeatures & XFEATURE_MASK_AVX512)
|
|
|
|
fpu->avx512_timestamp = jiffies;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (likely(use_fxsr())) {
|
|
|
|
copy_fxregs_to_kernel(fpu);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Legacy FPU register saving, FNSAVE always clears FPU registers,
|
|
|
|
* so we have to mark them inactive:
|
|
|
|
*/
|
|
|
|
asm volatile("fnsave %[fp]; fwait" : [fp] "=m" (fpu->state.fsave));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(copy_fpregs_to_fpstate);
|
|
|
|
|
2019-06-04 15:15:23 +08:00
|
|
|
void kernel_fpu_begin(void)
|
2012-02-22 02:25:45 +08:00
|
|
|
{
|
2019-06-04 15:15:23 +08:00
|
|
|
preempt_disable();
|
2012-02-22 02:25:45 +08:00
|
|
|
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(!irq_fpu_usable());
|
2019-06-04 15:15:23 +08:00
|
|
|
WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
|
2015-05-01 16:54:22 +08:00
|
|
|
|
2019-06-04 15:15:23 +08:00
|
|
|
this_cpu_write(in_kernel_fpu, true);
|
|
|
|
|
|
|
|
if (!(current->flags & PF_KTHREAD) &&
|
|
|
|
!test_thread_flag(TIF_NEED_FPU_LOAD)) {
|
|
|
|
set_thread_flag(TIF_NEED_FPU_LOAD);
|
|
|
|
/*
|
|
|
|
* Ignore return value -- we don't care if reg state
|
|
|
|
* is clobbered.
|
|
|
|
*/
|
|
|
|
copy_fpregs_to_fpstate(¤t->thread.fpu);
|
2012-02-22 02:25:45 +08:00
|
|
|
}
|
2019-04-04 00:41:52 +08:00
|
|
|
__cpu_invalidate_fpregs_state();
|
2020-06-16 17:12:57 +08:00
|
|
|
|
|
|
|
if (boot_cpu_has(X86_FEATURE_XMM))
|
|
|
|
ldmxcsr(MXCSR_DEFAULT);
|
|
|
|
|
|
|
|
if (boot_cpu_has(X86_FEATURE_FPU))
|
|
|
|
asm volatile ("fninit");
|
2012-02-22 02:25:45 +08:00
|
|
|
}
|
2015-04-26 18:07:18 +08:00
|
|
|
EXPORT_SYMBOL_GPL(kernel_fpu_begin);
|
|
|
|
|
|
|
|
void kernel_fpu_end(void)
|
|
|
|
{
|
2019-06-04 15:15:22 +08:00
|
|
|
WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
|
|
|
|
|
|
|
|
this_cpu_write(in_kernel_fpu, false);
|
2015-04-26 18:07:18 +08:00
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kernel_fpu_end);
|
|
|
|
|
2015-04-03 17:01:36 +08:00
|
|
|
/*
|
2015-04-27 15:45:12 +08:00
|
|
|
* Save the FPU state (mark it for reload if necessary):
|
2015-04-03 17:06:43 +08:00
|
|
|
*
|
|
|
|
* This only ever gets called for the current task.
|
2015-04-03 17:01:36 +08:00
|
|
|
*/
|
2015-04-23 23:57:24 +08:00
|
|
|
void fpu__save(struct fpu *fpu)
|
2012-02-22 02:25:45 +08:00
|
|
|
{
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(fpu != ¤t->thread.fpu);
|
2015-04-03 17:06:43 +08:00
|
|
|
|
2019-04-04 00:41:52 +08:00
|
|
|
fpregs_lock();
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_before_save(fpu);
|
2019-04-04 00:41:36 +08:00
|
|
|
|
2019-04-04 00:41:52 +08:00
|
|
|
if (!test_thread_flag(TIF_NEED_FPU_LOAD)) {
|
|
|
|
if (!copy_fpregs_to_fpstate(fpu)) {
|
|
|
|
copy_kernel_to_fpregs(&fpu->state);
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 00:41:36 +08:00
|
|
|
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_after_save(fpu);
|
2019-04-04 00:41:52 +08:00
|
|
|
fpregs_unlock();
|
2012-02-22 02:25:45 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 16:08:36 +08:00
|
|
|
/*
|
|
|
|
* Legacy x87 fpstate state init:
|
|
|
|
*/
|
2015-04-30 23:15:32 +08:00
|
|
|
static inline void fpstate_init_fstate(struct fregs_state *fp)
|
2015-04-30 16:08:36 +08:00
|
|
|
{
|
|
|
|
fp->cwd = 0xffff037fu;
|
|
|
|
fp->swd = 0xffff0000u;
|
|
|
|
fp->twd = 0xffffffffu;
|
|
|
|
fp->fos = 0xffff0000u;
|
|
|
|
}
|
|
|
|
|
2015-04-30 23:15:32 +08:00
|
|
|
void fpstate_init(union fpregs_state *state)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2016-04-05 04:24:58 +08:00
|
|
|
if (!static_cpu_has(X86_FEATURE_FPU)) {
|
2015-04-30 16:23:42 +08:00
|
|
|
fpstate_init_soft(&state->soft);
|
2010-05-06 16:45:46 +08:00
|
|
|
return;
|
2008-05-24 07:26:37 +08:00
|
|
|
}
|
|
|
|
|
2016-05-21 01:47:06 +08:00
|
|
|
memset(state, 0, fpu_kernel_xstate_size);
|
2015-03-10 14:06:25 +08:00
|
|
|
|
2016-07-12 00:18:56 +08:00
|
|
|
if (static_cpu_has(X86_FEATURE_XSAVES))
|
2017-01-25 02:25:46 +08:00
|
|
|
fpstate_init_xstate(&state->xsave);
|
2016-04-05 04:25:01 +08:00
|
|
|
if (static_cpu_has(X86_FEATURE_FXSR))
|
2015-04-30 16:23:42 +08:00
|
|
|
fpstate_init_fxstate(&state->fxsave);
|
2015-04-30 16:08:36 +08:00
|
|
|
else
|
2015-04-30 16:23:42 +08:00
|
|
|
fpstate_init_fstate(&state->fsave);
|
2010-05-06 16:45:46 +08:00
|
|
|
}
|
2015-04-03 19:01:52 +08:00
|
|
|
EXPORT_SYMBOL_GPL(fpstate_init);
|
2010-05-06 16:45:46 +08:00
|
|
|
|
2019-04-04 00:41:52 +08:00
|
|
|
int fpu__copy(struct task_struct *dst, struct task_struct *src)
|
2015-04-23 02:09:29 +08:00
|
|
|
{
|
2019-04-04 00:41:52 +08:00
|
|
|
struct fpu *dst_fpu = &dst->thread.fpu;
|
|
|
|
struct fpu *src_fpu = &src->thread.fpu;
|
|
|
|
|
2016-01-25 06:38:08 +08:00
|
|
|
dst_fpu->last_cpu = -1;
|
|
|
|
|
2019-04-04 00:41:36 +08:00
|
|
|
if (!static_cpu_has(X86_FEATURE_FPU))
|
2016-01-25 06:38:08 +08:00
|
|
|
return 0;
|
|
|
|
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(src_fpu != ¤t->thread.fpu);
|
2015-04-23 14:55:34 +08:00
|
|
|
|
2015-04-27 16:08:39 +08:00
|
|
|
/*
|
|
|
|
* Don't let 'init optimized' areas of the XSAVE area
|
|
|
|
* leak into the child task:
|
|
|
|
*/
|
2016-10-05 08:34:33 +08:00
|
|
|
memset(&dst_fpu->state.xsave, 0, fpu_kernel_xstate_size);
|
2015-04-27 16:08:39 +08:00
|
|
|
|
|
|
|
/*
|
2019-04-04 00:41:52 +08:00
|
|
|
* If the FPU registers are not current just memcpy() the state.
|
|
|
|
* Otherwise save current FPU registers directly into the child's FPU
|
|
|
|
* context, without any memory-to-memory copying.
|
2015-04-27 16:08:39 +08:00
|
|
|
*
|
2017-09-23 21:00:14 +08:00
|
|
|
* ( The function 'fails' in the FNSAVE case, which destroys
|
2019-04-04 00:41:52 +08:00
|
|
|
* register contents so we have to load them back. )
|
2015-04-27 16:08:39 +08:00
|
|
|
*/
|
2019-04-04 00:41:52 +08:00
|
|
|
fpregs_lock();
|
|
|
|
if (test_thread_flag(TIF_NEED_FPU_LOAD))
|
|
|
|
memcpy(&dst_fpu->state, &src_fpu->state, fpu_kernel_xstate_size);
|
|
|
|
|
|
|
|
else if (!copy_fpregs_to_fpstate(dst_fpu))
|
|
|
|
copy_kernel_to_fpregs(&dst_fpu->state);
|
|
|
|
|
|
|
|
fpregs_unlock();
|
|
|
|
|
|
|
|
set_tsk_thread_flag(dst, TIF_NEED_FPU_LOAD);
|
2015-04-27 11:52:40 +08:00
|
|
|
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_copy_src(src_fpu);
|
|
|
|
trace_x86_fpu_copy_dst(dst_fpu);
|
|
|
|
|
2015-04-22 21:47:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-03 18:02:02 +08:00
|
|
|
/*
|
2015-04-27 13:18:17 +08:00
|
|
|
* Activate the current task's in-memory FPU context,
|
|
|
|
* if it has not been used before:
|
2015-04-03 18:02:02 +08:00
|
|
|
*/
|
2019-04-04 00:41:33 +08:00
|
|
|
static void fpu__initialize(struct fpu *fpu)
|
2015-04-03 18:02:02 +08:00
|
|
|
{
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(fpu != ¤t->thread.fpu);
|
2015-04-03 18:02:02 +08:00
|
|
|
|
2019-04-04 00:41:52 +08:00
|
|
|
set_thread_flag(TIF_NEED_FPU_LOAD);
|
2019-04-04 00:41:36 +08:00
|
|
|
fpstate_init(&fpu->state);
|
|
|
|
trace_x86_fpu_init_state(fpu);
|
2015-04-03 18:02:02 +08:00
|
|
|
}
|
|
|
|
|
2015-05-27 18:22:29 +08:00
|
|
|
/*
|
|
|
|
* This function must be called before we read a task's fpstate.
|
|
|
|
*
|
2017-09-23 21:00:10 +08:00
|
|
|
* There's two cases where this gets called:
|
|
|
|
*
|
|
|
|
* - for the current task (when coredumping), in which case we have
|
|
|
|
* to save the latest FPU registers into the fpstate,
|
|
|
|
*
|
|
|
|
* - or it's called for stopped tasks (ptrace), in which case the
|
|
|
|
* registers were already saved by the context-switch code when
|
2019-04-04 00:41:36 +08:00
|
|
|
* the task scheduled out.
|
2015-05-27 18:22:29 +08:00
|
|
|
*
|
|
|
|
* If the task has used the FPU before then save it.
|
|
|
|
*/
|
2017-09-23 19:37:45 +08:00
|
|
|
void fpu__prepare_read(struct fpu *fpu)
|
2015-05-27 18:22:29 +08:00
|
|
|
{
|
2019-04-04 00:41:36 +08:00
|
|
|
if (fpu == ¤t->thread.fpu)
|
2015-05-27 18:22:29 +08:00
|
|
|
fpu__save(fpu);
|
|
|
|
}
|
|
|
|
|
2010-05-06 16:45:46 +08:00
|
|
|
/*
|
2015-05-27 18:22:29 +08:00
|
|
|
* This function must be called before we write a task's fpstate.
|
2015-04-23 20:06:05 +08:00
|
|
|
*
|
2019-04-04 00:41:36 +08:00
|
|
|
* Invalidate any cached FPU registers.
|
2015-04-23 20:06:05 +08:00
|
|
|
*
|
2015-05-27 18:22:29 +08:00
|
|
|
* After this function call, after registers in the fpstate are
|
|
|
|
* modified and the child task has woken up, the child task will
|
|
|
|
* restore the modified FPU state from the modified context. If we
|
2017-09-23 21:00:13 +08:00
|
|
|
* didn't clear its cached status here then the cached in-registers
|
2015-05-27 18:22:29 +08:00
|
|
|
* state pending on its former CPU could be restored, corrupting
|
|
|
|
* the modifications.
|
2010-05-06 16:45:46 +08:00
|
|
|
*/
|
2017-09-23 19:37:45 +08:00
|
|
|
void fpu__prepare_write(struct fpu *fpu)
|
2010-05-06 16:45:46 +08:00
|
|
|
{
|
2015-05-27 18:22:29 +08:00
|
|
|
/*
|
2015-05-27 18:22:29 +08:00
|
|
|
* Only stopped child tasks can be used to modify the FPU
|
|
|
|
* state in the fpstate buffer:
|
2015-05-27 18:22:29 +08:00
|
|
|
*/
|
2015-05-27 18:22:29 +08:00
|
|
|
WARN_ON_FPU(fpu == ¤t->thread.fpu);
|
|
|
|
|
2019-04-04 00:41:36 +08:00
|
|
|
/* Invalidate any cached state: */
|
|
|
|
__fpu_invalidate_fpregs_state(fpu);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 02:24:14 +08:00
|
|
|
/*
|
|
|
|
* Drops current FPU state: deactivates the fpregs and
|
|
|
|
* the fpstate. NOTE: it still leaves previous contents
|
|
|
|
* in the fpregs in the eager-FPU case.
|
|
|
|
*
|
|
|
|
* This function can be used in cases where we know that
|
|
|
|
* a state-restore is coming: either an explicit one,
|
|
|
|
* or a reschedule.
|
|
|
|
*/
|
|
|
|
void fpu__drop(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
|
2017-09-23 21:00:00 +08:00
|
|
|
if (fpu == ¤t->thread.fpu) {
|
2019-04-04 00:41:36 +08:00
|
|
|
/* Ignore delayed exceptions from user space */
|
|
|
|
asm volatile("1: fwait\n"
|
|
|
|
"2:\n"
|
|
|
|
_ASM_EXTABLE(1b, 2b));
|
|
|
|
fpregs_deactivate(fpu);
|
2015-04-30 02:24:14 +08:00
|
|
|
}
|
|
|
|
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_dropped(fpu);
|
|
|
|
|
2015-04-30 02:24:14 +08:00
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
2015-04-30 17:21:59 +08:00
|
|
|
/*
|
2020-05-12 22:54:39 +08:00
|
|
|
* Clear FPU registers by setting them up from the init fpstate.
|
|
|
|
* Caller must do fpregs_[un]lock() around it.
|
2015-04-30 17:21:59 +08:00
|
|
|
*/
|
2020-05-12 22:54:39 +08:00
|
|
|
static inline void copy_init_fpstate_to_fpregs(u64 features_mask)
|
2015-04-30 17:21:59 +08:00
|
|
|
{
|
|
|
|
if (use_xsave())
|
2020-05-12 22:54:39 +08:00
|
|
|
copy_kernel_to_xregs(&init_fpstate.xsave, features_mask);
|
2016-03-11 19:32:06 +08:00
|
|
|
else if (static_cpu_has(X86_FEATURE_FXSR))
|
2015-04-30 17:34:09 +08:00
|
|
|
copy_kernel_to_fxregs(&init_fpstate.fxsave);
|
2016-03-11 19:32:06 +08:00
|
|
|
else
|
|
|
|
copy_kernel_to_fregs(&init_fpstate.fsave);
|
x86/pkeys: Default to a restrictive init PKRU
PKRU is the register that lets you disallow writes or all access to a given
protection key.
The XSAVE hardware defines an "init state" of 0 for PKRU: its most
permissive state, allowing access/writes to everything. Since we start off
all new processes with the init state, we start all processes off with the
most permissive possible PKRU.
This is unfortunate. If a thread is clone()'d [1] before a program has
time to set PKRU to a restrictive value, that thread will be able to write
to all data, no matter what pkey is set on it. This weakens any integrity
guarantees that we want pkeys to provide.
To fix this, we define a very restrictive PKRU to override the
XSAVE-provided value when we create a new FPU context. We choose a value
that only allows access to pkey 0, which is as restrictive as we can
practically make it.
This does not cause any practical problems with applications using
protection keys because we require them to specify initial permissions for
each key when it is allocated, which override the restrictive default.
In the end, this ensures that threads which do not know how to manage their
own pkey rights can not do damage to data which is pkey-protected.
I would have thought this was a pretty contrived scenario, except that I
heard a bug report from an MPX user who was creating threads in some very
early code before main(). It may be crazy, but folks evidently _do_ it.
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-arch@vger.kernel.org
Cc: Dave Hansen <dave@sr71.net>
Cc: mgorman@techsingularity.net
Cc: arnd@arndb.de
Cc: linux-api@vger.kernel.org
Cc: linux-mm@kvack.org
Cc: luto@kernel.org
Cc: akpm@linux-foundation.org
Cc: torvalds@linux-foundation.org
Link: http://lkml.kernel.org/r/20160729163021.F3C25D4A@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2016-07-30 00:30:21 +08:00
|
|
|
|
|
|
|
if (boot_cpu_has(X86_FEATURE_OSPKE))
|
|
|
|
copy_init_pkru_to_fpregs();
|
2015-04-30 17:21:59 +08:00
|
|
|
}
|
|
|
|
|
2015-04-30 02:24:14 +08:00
|
|
|
/*
|
2015-04-30 13:12:46 +08:00
|
|
|
* Clear the FPU state back to init state.
|
|
|
|
*
|
|
|
|
* Called by sys_execve(), by the signal handler code and by various
|
|
|
|
* error paths.
|
2015-04-29 14:46:26 +08:00
|
|
|
*/
|
2020-05-12 22:54:39 +08:00
|
|
|
static void fpu__clear(struct fpu *fpu, bool user_only)
|
2015-04-22 17:52:13 +08:00
|
|
|
{
|
2020-05-12 22:54:39 +08:00
|
|
|
WARN_ON_FPU(fpu != ¤t->thread.fpu);
|
2015-04-23 18:46:20 +08:00
|
|
|
|
2020-05-12 22:54:39 +08:00
|
|
|
if (!static_cpu_has(X86_FEATURE_FPU)) {
|
|
|
|
fpu__drop(fpu);
|
|
|
|
fpu__initialize(fpu);
|
|
|
|
return;
|
|
|
|
}
|
2016-11-18 01:11:35 +08:00
|
|
|
|
2020-05-12 22:54:39 +08:00
|
|
|
fpregs_lock();
|
|
|
|
|
|
|
|
if (user_only) {
|
|
|
|
if (!fpregs_state_valid(fpu, smp_processor_id()) &&
|
|
|
|
xfeatures_mask_supervisor())
|
|
|
|
copy_kernel_to_xregs(&fpu->state.xsave,
|
|
|
|
xfeatures_mask_supervisor());
|
|
|
|
copy_init_fpstate_to_fpregs(xfeatures_mask_user());
|
|
|
|
} else {
|
|
|
|
copy_init_fpstate_to_fpregs(xfeatures_mask_all);
|
|
|
|
}
|
|
|
|
|
|
|
|
fpregs_mark_activate();
|
|
|
|
fpregs_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fpu__clear_user_states(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
fpu__clear(fpu, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fpu__clear_all(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
fpu__clear(fpu, false);
|
2015-04-22 17:52:13 +08:00
|
|
|
}
|
|
|
|
|
2019-04-04 00:41:52 +08:00
|
|
|
/*
|
|
|
|
* Load FPU context before returning to userspace.
|
|
|
|
*/
|
|
|
|
void switch_fpu_return(void)
|
|
|
|
{
|
|
|
|
if (!static_cpu_has(X86_FEATURE_FPU))
|
|
|
|
return;
|
|
|
|
|
|
|
|
__fpregs_load_activate();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(switch_fpu_return);
|
|
|
|
|
|
|
|
#ifdef CONFIG_X86_DEBUG_FPU
|
|
|
|
/*
|
|
|
|
* If current FPU state according to its tracking (loaded FPU context on this
|
|
|
|
* CPU) is not valid then we must have TIF_NEED_FPU_LOAD set so the context is
|
|
|
|
* loaded on return to userland.
|
|
|
|
*/
|
|
|
|
void fpregs_assert_state_consistent(void)
|
|
|
|
{
|
|
|
|
struct fpu *fpu = ¤t->thread.fpu;
|
|
|
|
|
|
|
|
if (test_thread_flag(TIF_NEED_FPU_LOAD))
|
|
|
|
return;
|
|
|
|
|
|
|
|
WARN_ON_FPU(!fpregs_state_valid(fpu, smp_processor_id()));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(fpregs_assert_state_consistent);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void fpregs_mark_activate(void)
|
|
|
|
{
|
|
|
|
struct fpu *fpu = ¤t->thread.fpu;
|
|
|
|
|
|
|
|
fpregs_activate(fpu);
|
|
|
|
fpu->last_cpu = smp_processor_id();
|
|
|
|
clear_thread_flag(TIF_NEED_FPU_LOAD);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(fpregs_mark_activate);
|
|
|
|
|
2015-04-30 15:29:38 +08:00
|
|
|
/*
|
|
|
|
* x87 math exception handling:
|
|
|
|
*/
|
|
|
|
|
|
|
|
int fpu__exception_code(struct fpu *fpu, int trap_nr)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (trap_nr == X86_TRAP_MF) {
|
|
|
|
unsigned short cwd, swd;
|
|
|
|
/*
|
|
|
|
* (~cwd & swd) will mask out exceptions that are not set to unmasked
|
|
|
|
* status. 0x3f is the exception bits in these regs, 0x200 is the
|
|
|
|
* C1 reg you need in case of a stack fault, 0x040 is the stack
|
|
|
|
* fault bit. We should only be taking one exception at a time,
|
|
|
|
* so if this combination doesn't produce any single exception,
|
|
|
|
* then we have a bad program that isn't synchronizing its FPU usage
|
|
|
|
* and it will suffer the consequences since we won't be able to
|
2016-04-05 14:29:55 +08:00
|
|
|
* fully reproduce the context of the exception.
|
2015-04-30 15:29:38 +08:00
|
|
|
*/
|
2016-04-05 14:29:55 +08:00
|
|
|
if (boot_cpu_has(X86_FEATURE_FXSR)) {
|
|
|
|
cwd = fpu->state.fxsave.cwd;
|
|
|
|
swd = fpu->state.fxsave.swd;
|
|
|
|
} else {
|
|
|
|
cwd = (unsigned short)fpu->state.fsave.cwd;
|
|
|
|
swd = (unsigned short)fpu->state.fsave.swd;
|
|
|
|
}
|
2015-04-30 15:29:38 +08:00
|
|
|
|
|
|
|
err = swd & ~cwd;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The SIMD FPU exceptions are handled a little differently, as there
|
|
|
|
* is only a single status/control register. Thus, to determine which
|
|
|
|
* unmasked exception was caught we must mask the exception mask bits
|
|
|
|
* at 0x1f80, and then use these to mask the exception bits at 0x3f.
|
|
|
|
*/
|
2016-04-05 14:29:55 +08:00
|
|
|
unsigned short mxcsr = MXCSR_DEFAULT;
|
|
|
|
|
|
|
|
if (boot_cpu_has(X86_FEATURE_XMM))
|
|
|
|
mxcsr = fpu->state.fxsave.mxcsr;
|
|
|
|
|
2015-04-30 15:29:38 +08:00
|
|
|
err = ~(mxcsr >> 7) & mxcsr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err & 0x001) { /* Invalid op */
|
|
|
|
/*
|
|
|
|
* swd & 0x240 == 0x040: Stack Underflow
|
|
|
|
* swd & 0x240 == 0x240: Stack Overflow
|
|
|
|
* User must clear the SF bit (0x40) if set
|
|
|
|
*/
|
|
|
|
return FPE_FLTINV;
|
|
|
|
} else if (err & 0x004) { /* Divide by Zero */
|
|
|
|
return FPE_FLTDIV;
|
|
|
|
} else if (err & 0x008) { /* Overflow */
|
|
|
|
return FPE_FLTOVF;
|
|
|
|
} else if (err & 0x012) { /* Denormal, Underflow */
|
|
|
|
return FPE_FLTUND;
|
|
|
|
} else if (err & 0x020) { /* Precision */
|
|
|
|
return FPE_FLTRES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're using IRQ 13, or supposedly even some trap
|
|
|
|
* X86_TRAP_MF implementations, it's possible
|
|
|
|
* we get a spurious trap, which is not an error.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|