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>
|
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:33:08 +08:00
|
|
|
static void kernel_fpu_disable(void)
|
2015-01-16 03:20:28 +08:00
|
|
|
{
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(this_cpu_read(in_kernel_fpu));
|
2015-01-16 03:20:28 +08:00
|
|
|
this_cpu_write(in_kernel_fpu, true);
|
|
|
|
}
|
|
|
|
|
2015-04-22 22:33:08 +08:00
|
|
|
static void kernel_fpu_enable(void)
|
2015-01-16 03:20:28 +08:00
|
|
|
{
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(!this_cpu_read(in_kernel_fpu));
|
2015-01-16 03:20:28 +08:00
|
|
|
this_cpu_write(in_kernel_fpu, false);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2012-09-21 02:01:49 +08:00
|
|
|
void __kernel_fpu_begin(void)
|
2012-02-22 02:25:45 +08:00
|
|
|
{
|
2015-04-23 18:18:28 +08:00
|
|
|
struct fpu *fpu = ¤t->thread.fpu;
|
2012-02-22 02:25:45 +08:00
|
|
|
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(!irq_fpu_usable());
|
2015-05-01 16:54:22 +08:00
|
|
|
|
2015-04-22 22:40:56 +08:00
|
|
|
kernel_fpu_disable();
|
2015-01-16 03:19:43 +08:00
|
|
|
|
2017-09-26 15:43:36 +08:00
|
|
|
if (fpu->initialized) {
|
2016-01-25 06:38:07 +08:00
|
|
|
/*
|
|
|
|
* Ignore return value -- we don't care if reg state
|
|
|
|
* is clobbered.
|
|
|
|
*/
|
x86/fpu: Rename fpu_save_init() to copy_fpregs_to_fpstate()
So fpu_save_init() is a historic name that got its name when the only
way the FPU state was FNSAVE, which cleared (well, destroyed) the FPU
state after saving it.
Nowadays the name is misleading, because ever since the introduction of
FXSAVE (and more modern FPU saving instructions) the 'we need to reload
the FPU state' part is only true if there's a pending FPU exception [*],
which is almost never the case.
So rename it to copy_fpregs_to_fpstate() to make it clear what's
happening. Also add a few comments about why we cannot keep registers
in certain cases.
Also clean up the control flow a bit, to make it more apparent when
we are dropping/keeping FP registers, and to optimize the common
case (of keeping fpregs) some more.
[*] Probably not true anymore, modern instructions always leave the FPU
state intact, even if exceptions are pending: because pending FP
exceptions are posted on the next FP instruction, not asynchronously.
They were truly asynchronous back in the IRQ13 case, and we had to
synchronize with them, but that code is not working anymore: we don't
have IRQ13 mapped in the IDT anymore.
But a cleanup patch is obviously not the place to change subtle behavior.
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2015-04-27 08:53:16 +08:00
|
|
|
copy_fpregs_to_fpstate(fpu);
|
2015-01-20 02:51:32 +08:00
|
|
|
} else {
|
2016-10-14 20:15:30 +08:00
|
|
|
__cpu_invalidate_fpregs_state();
|
2012-02-22 02:25:45 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 02:01:49 +08:00
|
|
|
EXPORT_SYMBOL(__kernel_fpu_begin);
|
2012-02-22 02:25:45 +08:00
|
|
|
|
2012-09-21 02:01:49 +08:00
|
|
|
void __kernel_fpu_end(void)
|
2012-02-22 02:25:45 +08:00
|
|
|
{
|
2015-04-23 23:34:20 +08:00
|
|
|
struct fpu *fpu = ¤t->thread.fpu;
|
2015-01-16 03:20:05 +08:00
|
|
|
|
2017-09-26 15:43:36 +08:00
|
|
|
if (fpu->initialized)
|
2015-05-25 17:59:35 +08:00
|
|
|
copy_kernel_to_fpregs(&fpu->state);
|
2015-01-16 03:19:43 +08:00
|
|
|
|
2015-04-22 22:40:56 +08:00
|
|
|
kernel_fpu_enable();
|
2012-02-22 02:25:45 +08:00
|
|
|
}
|
2012-09-21 02:01:49 +08:00
|
|
|
EXPORT_SYMBOL(__kernel_fpu_end);
|
2012-02-22 02:25:45 +08:00
|
|
|
|
2015-04-26 18:07:18 +08:00
|
|
|
void kernel_fpu_begin(void)
|
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
__kernel_fpu_begin();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kernel_fpu_begin);
|
|
|
|
|
|
|
|
void kernel_fpu_end(void)
|
|
|
|
{
|
|
|
|
__kernel_fpu_end();
|
|
|
|
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
|
|
|
|
2012-02-22 02:25:45 +08:00
|
|
|
preempt_disable();
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_before_save(fpu);
|
2017-09-26 15:43:36 +08:00
|
|
|
if (fpu->initialized) {
|
2016-01-25 06:38:07 +08:00
|
|
|
if (!copy_fpregs_to_fpstate(fpu)) {
|
2016-10-05 08:34:33 +08:00
|
|
|
copy_kernel_to_fpregs(&fpu->state);
|
2016-01-25 06:38:07 +08:00
|
|
|
}
|
2015-02-07 04:01:58 +08:00
|
|
|
}
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_after_save(fpu);
|
2012-02-22 02:25:45 +08:00
|
|
|
preempt_enable();
|
|
|
|
}
|
2015-04-03 17:01:36 +08:00
|
|
|
EXPORT_SYMBOL_GPL(fpu__save);
|
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
|
|
|
|
2016-01-25 06:38:08 +08:00
|
|
|
int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
|
2015-04-23 02:09:29 +08:00
|
|
|
{
|
2016-01-25 06:38:08 +08:00
|
|
|
dst_fpu->last_cpu = -1;
|
|
|
|
|
2017-09-26 15:43:36 +08:00
|
|
|
if (!src_fpu->initialized || !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
|
|
|
|
|
|
|
/*
|
|
|
|
* Save current FPU registers directly into the child
|
|
|
|
* FPU context, without any memory-to-memory copying.
|
|
|
|
*
|
2017-09-23 21:00:14 +08:00
|
|
|
* ( The function 'fails' in the FNSAVE case, which destroys
|
|
|
|
* register contents so we have to copy them back. )
|
2015-04-27 16:08:39 +08:00
|
|
|
*/
|
|
|
|
if (!copy_fpregs_to_fpstate(dst_fpu)) {
|
2017-09-23 21:00:14 +08:00
|
|
|
memcpy(&src_fpu->state, &dst_fpu->state, fpu_kernel_xstate_size);
|
2016-10-05 08:34:33 +08:00
|
|
|
copy_kernel_to_fpregs(&src_fpu->state);
|
2015-04-23 02:09:29 +08:00
|
|
|
}
|
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
|
|
|
*/
|
2017-09-23 21:00:15 +08:00
|
|
|
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
|
|
|
|
2017-09-26 15:43:36 +08:00
|
|
|
if (!fpu->initialized) {
|
2015-04-30 16:23:42 +08:00
|
|
|
fpstate_init(&fpu->state);
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_init_state(fpu);
|
2015-04-03 18:02:02 +08:00
|
|
|
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_activate_state(fpu);
|
2015-04-27 13:18:17 +08:00
|
|
|
/* Safe to do for the current task: */
|
2017-09-26 15:43:36 +08:00
|
|
|
fpu->initialized = 1;
|
2015-04-27 13:18:17 +08:00
|
|
|
}
|
2015-04-03 18:02:02 +08:00
|
|
|
}
|
2017-09-23 21:00:15 +08:00
|
|
|
EXPORT_SYMBOL_GPL(fpu__initialize);
|
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
|
|
|
|
* the task scheduled out - we only have to initialize the registers
|
|
|
|
* if they've never been initialized.
|
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
|
|
|
{
|
2017-09-23 21:00:10 +08:00
|
|
|
if (fpu == ¤t->thread.fpu) {
|
2015-05-27 18:22:29 +08:00
|
|
|
fpu__save(fpu);
|
|
|
|
} else {
|
2017-09-26 15:43:36 +08:00
|
|
|
if (!fpu->initialized) {
|
2015-05-27 18:22:29 +08:00
|
|
|
fpstate_init(&fpu->state);
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_init_state(fpu);
|
2015-05-27 18:22:29 +08:00
|
|
|
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_activate_state(fpu);
|
2015-05-27 18:22:29 +08:00
|
|
|
/* Safe to do for current and for stopped child tasks: */
|
2017-09-26 15:43:36 +08:00
|
|
|
fpu->initialized = 1;
|
2015-05-27 18:22:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2017-09-23 21:00:13 +08:00
|
|
|
* If the task has used the FPU before then invalidate any cached FPU registers.
|
2015-05-27 18:22:29 +08:00
|
|
|
* If the task has not used the FPU before then initialize its fpstate.
|
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);
|
|
|
|
|
2017-09-26 15:43:36 +08:00
|
|
|
if (fpu->initialized) {
|
2017-09-23 21:00:13 +08:00
|
|
|
/* Invalidate any cached state: */
|
2016-10-05 08:34:36 +08:00
|
|
|
__fpu_invalidate_fpregs_state(fpu);
|
2015-04-27 12:55:54 +08:00
|
|
|
} else {
|
2015-05-27 18:22:29 +08:00
|
|
|
fpstate_init(&fpu->state);
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_init_state(fpu);
|
2015-05-27 18:22:29 +08:00
|
|
|
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_activate_state(fpu);
|
2015-05-27 18:22:29 +08:00
|
|
|
/* Safe to do for stopped child tasks: */
|
2017-09-26 15:43:36 +08:00
|
|
|
fpu->initialized = 1;
|
2015-04-27 12:55:54 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-04-22 18:50:13 +08:00
|
|
|
/*
|
2015-04-28 18:53:45 +08:00
|
|
|
* 'fpu__restore()' is called to copy FPU registers from
|
|
|
|
* the FPU fpstate to the live hw registers and to activate
|
|
|
|
* access to the hardware registers, so that FPU instructions
|
|
|
|
* can be used afterwards.
|
2015-04-22 18:50:13 +08:00
|
|
|
*
|
2015-04-28 18:53:45 +08:00
|
|
|
* Must be called with kernel preemption disabled (for example
|
|
|
|
* with local interrupts disabled, as it is in the case of
|
|
|
|
* do_device_not_available()).
|
2015-04-22 18:50:13 +08:00
|
|
|
*/
|
2015-05-04 17:49:58 +08:00
|
|
|
void fpu__restore(struct fpu *fpu)
|
2015-04-22 18:50:13 +08:00
|
|
|
{
|
2017-09-23 21:00:15 +08:00
|
|
|
fpu__initialize(fpu);
|
2015-04-22 18:50:13 +08:00
|
|
|
|
2015-04-24 20:30:38 +08:00
|
|
|
/* Avoid __kernel_fpu_begin() right after fpregs_activate() */
|
2015-04-22 18:50:13 +08:00
|
|
|
kernel_fpu_disable();
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_before_restore(fpu);
|
2015-04-24 20:30:38 +08:00
|
|
|
fpregs_activate(fpu);
|
2015-05-25 17:59:35 +08:00
|
|
|
copy_kernel_to_fpregs(&fpu->state);
|
2016-06-02 01:42:20 +08:00
|
|
|
trace_x86_fpu_after_restore(fpu);
|
2015-04-22 18:50:13 +08:00
|
|
|
kernel_fpu_enable();
|
|
|
|
}
|
2015-04-22 19:16:47 +08:00
|
|
|
EXPORT_SYMBOL_GPL(fpu__restore);
|
2015-04-22 18:50:13 +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) {
|
2017-09-26 15:43:36 +08:00
|
|
|
if (fpu->initialized) {
|
2017-09-23 21:00:00 +08:00
|
|
|
/* Ignore delayed exceptions from user space */
|
|
|
|
asm volatile("1: fwait\n"
|
|
|
|
"2:\n"
|
|
|
|
_ASM_EXTABLE(1b, 2b));
|
2017-09-23 21:00:02 +08:00
|
|
|
fpregs_deactivate(fpu);
|
2017-09-23 21:00:00 +08:00
|
|
|
}
|
2015-04-30 02:24:14 +08:00
|
|
|
}
|
|
|
|
|
2017-09-26 15:43:36 +08:00
|
|
|
fpu->initialized = 0;
|
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
|
|
|
/*
|
|
|
|
* Clear FPU registers by setting them up from
|
|
|
|
* the init fpstate:
|
|
|
|
*/
|
|
|
|
static inline void copy_init_fpstate_to_fpregs(void)
|
|
|
|
{
|
|
|
|
if (use_xsave())
|
2015-04-30 17:34:09 +08:00
|
|
|
copy_kernel_to_xregs(&init_fpstate.xsave, -1);
|
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
|
|
|
*/
|
2015-04-30 02:35:33 +08:00
|
|
|
void fpu__clear(struct fpu *fpu)
|
2015-04-22 17:52:13 +08:00
|
|
|
{
|
2015-05-05 17:34:49 +08:00
|
|
|
WARN_ON_FPU(fpu != ¤t->thread.fpu); /* Almost certainly an anomaly */
|
2015-04-23 18:46:20 +08:00
|
|
|
|
2016-11-18 01:11:35 +08:00
|
|
|
fpu__drop(fpu);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure fpstate is cleared and initialized.
|
|
|
|
*/
|
|
|
|
if (static_cpu_has(X86_FEATURE_FPU)) {
|
2017-09-23 20:59:59 +08:00
|
|
|
preempt_disable();
|
2017-09-23 21:00:15 +08:00
|
|
|
fpu__initialize(fpu);
|
2016-11-18 01:11:35 +08:00
|
|
|
user_fpu_begin();
|
2015-04-30 17:21:59 +08:00
|
|
|
copy_init_fpstate_to_fpregs();
|
2017-09-23 20:59:59 +08:00
|
|
|
preempt_enable();
|
2015-04-22 17:52:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|