2019-05-19 13:08:55 +01:00
// SPDX-License-Identifier: GPL-2.0-only
2005-04-16 15:20:36 -07:00
/*
* Copyright ( C ) 1994 Linus Torvalds
*
* Pentium III FXSR , SSE support
* General FPU state handling cleanups
* Gareth Hughes < gareth @ valinux . com > , May 2000
*/
2021-10-15 03:16:39 +02:00
# include <asm/fpu/api.h>
2015-04-30 08:53:18 +02:00
# include <asm/fpu/regset.h>
2021-10-15 03:16:20 +02:00
# include <asm/fpu/sched.h>
2015-04-30 08:45:02 +02:00
# include <asm/fpu/signal.h>
2016-07-11 09:18:56 -07:00
# include <asm/fpu/types.h>
2015-04-30 09:29:38 +02:00
# include <asm/traps.h>
2018-07-29 12:15:33 +02:00
# include <asm/irq_regs.h>
2015-04-30 08:45:02 +02:00
2015-04-26 16:57:55 +02: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-29 09:30:21 -07:00
# include <linux/pkeys.h>
2021-10-25 15:04:13 +11:00
# include <linux/vmalloc.h>
2005-04-16 15:20:36 -07:00
2021-10-15 03:16:30 +02:00
# include "context.h"
2021-10-15 03:16:21 +02:00
# include "internal.h"
2021-10-15 03:16:26 +02:00
# include "legacy.h"
2021-10-15 03:16:07 +02:00
# include "xstate.h"
2016-06-01 10:42:20 -07:00
# define CREATE_TRACE_POINTS
# include <asm/trace/fpu.h>
2021-10-21 15:55:11 -07:00
# ifdef CONFIG_X86_64
DEFINE_STATIC_KEY_FALSE ( __fpu_state_size_dynamic ) ;
2021-10-21 15:55:18 -07:00
DEFINE_PER_CPU ( u64 , xfd_state ) ;
2021-10-21 15:55:11 -07:00
# endif
2021-10-15 01:09:29 +02:00
/* The FPU state configuration data for kernel and user space */
struct fpu_state_config fpu_kernel_cfg __ro_after_init ;
struct fpu_state_config fpu_user_cfg __ro_after_init ;
2015-04-30 11:07:06 +02:00
/*
* Represents the initial FPU state . It ' s mostly ( but not completely ) zeroes ,
* depending on the FPU hardware format :
*/
2021-10-13 16:55:28 +02:00
struct fpstate init_fpstate __ro_after_init ;
2015-04-30 11:07:06 +02:00
2015-04-22 16:52:03 +02: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-15 20:19:43 +01:00
static DEFINE_PER_CPU ( bool , in_kernel_fpu ) ;
2015-04-23 12:13:04 +02:00
/*
2015-04-23 12:18:28 +02:00
* Track which context is using the FPU on the CPU :
2015-04-23 12:13:04 +02:00
*/
2015-04-23 12:18:28 +02:00
DEFINE_PER_CPU ( struct fpu * , fpu_fpregs_owner_ctx ) ;
2015-04-23 12:13:04 +02:00
2015-04-22 16:52:03 +02:00
static bool kernel_fpu_disabled ( void )
{
return this_cpu_read ( in_kernel_fpu ) ;
}
2015-04-22 16:33:08 +02:00
static bool interrupted_kernel_fpu_idle ( void )
2012-02-21 10:25:45 -08:00
{
2016-10-04 20:34:33 -04:00
return ! kernel_fpu_disabled ( ) ;
2012-02-21 10: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 16:33:08 +02:00
static bool interrupted_user_mode ( void )
2012-02-21 10:25:45 -08:00
{
struct pt_regs * regs = get_irq_regs ( ) ;
2015-03-18 18:33:33 -07:00
return regs & & user_mode ( regs ) ;
2012-02-21 10: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 05:49:25 -07:00
/*
2021-10-13 16:55:39 +02:00
* Save the FPU register state in fpu - > fpstate - > regs . The register state is
2021-06-23 14:02:00 +02:00
* preserved .
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 05:49:25 -07:00
*
2021-06-23 14:02:00 +02:00
* Must be called with fpregs_lock ( ) held .
*
* The legacy FNSAVE instruction clears all FPU state unconditionally , so
* register state has to be reloaded . That might be a pointless exercise
* when the FPU is going to be used by another task right after that . But
* this only affects 20 + years old 32 bit systems and avoids conditionals all
* over the place .
*
* FXSAVE and all XSAVE variants preserve the FPU register state .
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 05:49:25 -07:00
*/
2021-06-23 14:02:00 +02:00
void save_fpregs_to_fpstate ( struct fpu * fpu )
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 05:49:25 -07:00
{
if ( likely ( use_xsave ( ) ) ) {
2021-10-13 16:55:49 +02:00
os_xsave ( fpu - > fpstate ) ;
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 05:49:25 -07:00
/*
* AVX512 state is tracked here because its use is
* known to slow the max clock speed of the core .
*/
2021-10-13 16:55:39 +02:00
if ( fpu - > fpstate - > regs . xsave . header . xfeatures & XFEATURE_MASK_AVX512 )
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 05:49:25 -07:00
fpu - > avx512_timestamp = jiffies ;
2021-06-23 14:02:00 +02:00
return ;
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 05:49:25 -07:00
}
if ( likely ( use_fxsr ( ) ) ) {
2021-10-13 16:55:39 +02:00
fxsave ( & fpu - > fpstate - > regs . fxsave ) ;
2021-06-23 14:02:00 +02:00
return ;
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 05:49:25 -07:00
}
/*
* Legacy FPU register saving , FNSAVE always clears FPU registers ,
2021-06-23 14:02:00 +02:00
* so we have to reload them from the memory state .
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 05:49:25 -07:00
*/
2021-10-13 16:55:39 +02:00
asm volatile ( " fnsave %[fp]; fwait " : [ fp ] " =m " ( fpu - > fpstate - > regs . fsave ) ) ;
frstor ( & fpu - > fpstate - > regs . fsave ) ;
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 05:49:25 -07:00
}
2021-10-13 16:55:30 +02:00
void restore_fpregs_from_fpstate ( struct fpstate * fpstate , u64 mask )
2021-06-23 14:02:15 +02:00
{
/*
* AMD K7 / K8 and later CPUs up to Zen don ' t save / restore
* FDP / FIP / FOP unless an exception is pending . Clear the x87 state
* here by setting it to fixed values . " m " is a random variable
* that should be in L1 .
*/
if ( unlikely ( static_cpu_has_bug ( X86_BUG_FXSAVE_LEAK ) ) ) {
asm volatile (
" fnclex \n \t "
" emms \n \t "
" fildl %P[addr] " /* set F?P to defined value */
: : [ addr ] " m " ( fpstate ) ) ;
}
if ( use_xsave ( ) ) {
2021-10-21 15:55:20 -07:00
/*
* Dynamically enabled features are enabled in XCR0 , but
* usage requires also that the corresponding bits in XFD
* are cleared . If the bits are set then using a related
* instruction will raise # NM . This allows to do the
* allocation of the larger FPU buffer lazy from # NM or if
* the task has no permission to kill it which would happen
* via # UD if the feature is disabled in XCR0 .
*
* XFD state is following the same life time rules as
* XSTATE and to restore state correctly XFD has to be
* updated before XRSTORS otherwise the component would
* stay in or go into init state even if the bits are set
* in fpstate : : regs : : xsave : : xfeatures .
*/
xfd_update_state ( fpstate ) ;
2021-10-15 01:09:38 +02:00
/*
* Restoring state always needs to modify all features
* which are in @ mask even if the current task cannot use
* extended features .
*
* So fpstate - > xfeatures cannot be used here , because then
* a feature for which the task has no permission but was
* used by the previous task would not go into init state .
*/
mask = fpu_kernel_cfg . max_features & mask ;
2021-10-21 15:55:19 -07:00
os_xrstor ( fpstate , mask ) ;
2021-06-23 14:02:15 +02:00
} else {
if ( use_fxsr ( ) )
2021-10-13 16:55:30 +02:00
fxrstor ( & fpstate - > regs . fxsave ) ;
2021-06-23 14:02:15 +02:00
else
2021-10-13 16:55:30 +02:00
frstor ( & fpstate - > regs . fsave ) ;
2021-06-23 14:02:15 +02:00
}
}
2021-10-15 03:16:12 +02:00
2021-10-15 03:16:41 +02:00
void fpu_reset_from_exception_fixup ( void )
{
2021-10-15 01:09:38 +02:00
restore_fpregs_from_fpstate ( & init_fpstate , XFEATURE_MASK_FPSTATE ) ;
2021-10-15 03:16:41 +02:00
}
2021-10-15 03:16:12 +02:00
# if IS_ENABLED(CONFIG_KVM)
2022-01-05 04:35:16 -08:00
static void __fpstate_reset ( struct fpstate * fpstate , u64 xfd ) ;
2021-10-22 20:55:51 +02:00
2022-01-05 04:35:13 -08:00
static void fpu_init_guest_permissions ( struct fpu_guest * gfpu )
{
struct fpu_state_perm * fpuperm ;
u64 perm ;
if ( ! IS_ENABLED ( CONFIG_X86_64 ) )
return ;
spin_lock_irq ( & current - > sighand - > siglock ) ;
fpuperm = & current - > group_leader - > thread . fpu . guest_perm ;
perm = fpuperm - > __state_perm ;
/* First fpstate allocation locks down permissions. */
WRITE_ONCE ( fpuperm - > __state_perm , perm | FPU_GUEST_PERM_LOCKED ) ;
spin_unlock_irq ( & current - > sighand - > siglock ) ;
gfpu - > perm = perm & ~ FPU_GUEST_PERM_LOCKED ;
}
2021-10-22 20:55:51 +02:00
bool fpu_alloc_guest_fpstate ( struct fpu_guest * gfpu )
{
struct fpstate * fpstate ;
unsigned int size ;
size = fpu_user_cfg . default_size + ALIGN ( offsetof ( struct fpstate , regs ) , 64 ) ;
fpstate = vzalloc ( size ) ;
if ( ! fpstate )
return false ;
2022-01-05 04:35:16 -08:00
/* Leave xfd to 0 (the reset value defined by spec) */
__fpstate_reset ( fpstate , 0 ) ;
2021-10-22 20:55:51 +02:00
fpstate_init_user ( fpstate ) ;
fpstate - > is_valloc = true ;
fpstate - > is_guest = true ;
2022-01-05 04:35:13 -08:00
gfpu - > fpstate = fpstate ;
gfpu - > xfeatures = fpu_user_cfg . default_features ;
gfpu - > perm = fpu_user_cfg . default_features ;
2022-01-05 04:35:28 -08:00
gfpu - > uabi_size = fpu_user_cfg . default_size ;
2022-01-05 04:35:13 -08:00
fpu_init_guest_permissions ( gfpu ) ;
2021-10-22 20:55:51 +02:00
return true ;
}
EXPORT_SYMBOL_GPL ( fpu_alloc_guest_fpstate ) ;
void fpu_free_guest_fpstate ( struct fpu_guest * gfpu )
{
struct fpstate * fps = gfpu - > fpstate ;
if ( ! fps )
return ;
if ( WARN_ON_ONCE ( ! fps - > is_valloc | | ! fps - > is_guest | | fps - > in_use ) )
return ;
gfpu - > fpstate = NULL ;
vfree ( fps ) ;
}
EXPORT_SYMBOL_GPL ( fpu_free_guest_fpstate ) ;
2022-01-05 04:35:18 -08:00
/*
* fpu_enable_guest_xfd_features - Check xfeatures against guest perm and enable
* @ guest_fpu : Pointer to the guest FPU container
* @ xfeatures : Features requested by guest CPUID
*
* Enable all dynamic xfeatures according to guest perm and requested CPUID .
*
* Return : 0 on success , error code otherwise
*/
int fpu_enable_guest_xfd_features ( struct fpu_guest * guest_fpu , u64 xfeatures )
{
lockdep_assert_preemption_enabled ( ) ;
/* Nothing to do if all requested features are already enabled. */
xfeatures & = ~ guest_fpu - > xfeatures ;
if ( ! xfeatures )
return 0 ;
return __xfd_enable_feature ( xfeatures , guest_fpu ) ;
}
EXPORT_SYMBOL_GPL ( fpu_enable_guest_xfd_features ) ;
2022-01-05 04:35:20 -08:00
# ifdef CONFIG_X86_64
void fpu_update_guest_xfd ( struct fpu_guest * guest_fpu , u64 xfd )
{
fpregs_lock ( ) ;
guest_fpu - > fpstate - > xfd = xfd ;
if ( guest_fpu - > fpstate - > in_use )
xfd_update_state ( guest_fpu - > fpstate ) ;
fpregs_unlock ( ) ;
}
EXPORT_SYMBOL_GPL ( fpu_update_guest_xfd ) ;
2022-01-05 04:35:31 -08:00
/**
* fpu_sync_guest_vmexit_xfd_state - Synchronize XFD MSR and software state
*
* Must be invoked from KVM after a VMEXIT before enabling interrupts when
* XFD write emulation is disabled . This is required because the guest can
* freely modify XFD and the state at VMEXIT is not guaranteed to be the
* same as the state on VMENTER . So software state has to be udpated before
* any operation which depends on it can take place .
*
* Note : It can be invoked unconditionally even when write emulation is
* enabled for the price of a then pointless MSR read .
*/
void fpu_sync_guest_vmexit_xfd_state ( void )
{
struct fpstate * fps = current - > thread . fpu . fpstate ;
lockdep_assert_irqs_disabled ( ) ;
if ( fpu_state_size_dynamic ( ) ) {
rdmsrl ( MSR_IA32_XFD , fps - > xfd ) ;
__this_cpu_write ( xfd_state , fps - > xfd ) ;
}
}
EXPORT_SYMBOL_GPL ( fpu_sync_guest_vmexit_xfd_state ) ;
2022-01-05 04:35:20 -08:00
# endif /* CONFIG_X86_64 */
2021-10-22 20:55:51 +02:00
int fpu_swap_kvm_fpstate ( struct fpu_guest * guest_fpu , bool enter_guest )
{
struct fpstate * guest_fps = guest_fpu - > fpstate ;
struct fpu * fpu = & current - > thread . fpu ;
struct fpstate * cur_fps = fpu - > fpstate ;
fpregs_lock ( ) ;
if ( ! cur_fps - > is_confidential & & ! test_thread_flag ( TIF_NEED_FPU_LOAD ) )
save_fpregs_to_fpstate ( fpu ) ;
/* Swap fpstate */
if ( enter_guest ) {
fpu - > __task_fpstate = cur_fps ;
fpu - > fpstate = guest_fps ;
guest_fps - > in_use = true ;
} else {
guest_fps - > in_use = false ;
fpu - > fpstate = fpu - > __task_fpstate ;
fpu - > __task_fpstate = NULL ;
}
cur_fps = fpu - > fpstate ;
2021-10-21 15:55:20 -07:00
if ( ! cur_fps - > is_confidential ) {
/* Includes XFD update */
2021-10-22 20:55:51 +02:00
restore_fpregs_from_fpstate ( cur_fps , XFEATURE_MASK_FPSTATE ) ;
2021-10-21 15:55:20 -07:00
} else {
/*
* XSTATE is restored by firmware from encrypted
* memory . Make sure XFD state is correct while
* running with guest fpstate
*/
xfd_update_state ( cur_fps ) ;
}
2021-10-22 20:55:51 +02:00
fpregs_mark_activate ( ) ;
fpregs_unlock ( ) ;
return 0 ;
}
EXPORT_SYMBOL_GPL ( fpu_swap_kvm_fpstate ) ;
2021-10-22 20:55:53 +02:00
void fpu_copy_guest_fpstate_to_uabi ( struct fpu_guest * gfpu , void * buf ,
unsigned int size , u32 pkru )
2021-10-15 03:16:17 +02:00
{
2021-10-22 20:55:53 +02:00
struct fpstate * kstate = gfpu - > fpstate ;
2021-10-15 03:16:17 +02:00
union fpregs_state * ustate = buf ;
struct membuf mb = { . p = buf , . left = size } ;
if ( cpu_feature_enabled ( X86_FEATURE_XSAVE ) ) {
2021-10-13 16:55:54 +02:00
__copy_xstate_to_uabi_buf ( mb , kstate , pkru , XSTATE_COPY_XSAVE ) ;
2021-10-15 03:16:17 +02:00
} else {
2021-10-13 16:55:54 +02:00
memcpy ( & ustate - > fxsave , & kstate - > regs . fxsave ,
sizeof ( ustate - > fxsave ) ) ;
2021-10-15 03:16:17 +02:00
/* Make it restorable on a XSAVE enabled host */
ustate - > xsave . header . xfeatures = XFEATURE_MASK_FPSSE ;
}
}
2021-10-22 20:55:53 +02:00
EXPORT_SYMBOL_GPL ( fpu_copy_guest_fpstate_to_uabi ) ;
2021-10-15 03:16:17 +02:00
2021-10-22 20:55:53 +02:00
int fpu_copy_uabi_to_guest_fpstate ( struct fpu_guest * gfpu , const void * buf ,
u64 xcr0 , u32 * vpkru )
2021-10-15 03:16:13 +02:00
{
2021-10-22 20:55:53 +02:00
struct fpstate * kstate = gfpu - > fpstate ;
2021-10-15 03:16:13 +02:00
const union fpregs_state * ustate = buf ;
struct pkru_state * xpkru ;
int ret ;
if ( ! cpu_feature_enabled ( X86_FEATURE_XSAVE ) ) {
if ( ustate - > xsave . header . xfeatures & ~ XFEATURE_MASK_FPSSE )
return - EINVAL ;
if ( ustate - > fxsave . mxcsr & ~ mxcsr_feature_mask )
return - EINVAL ;
2021-10-13 16:55:52 +02:00
memcpy ( & kstate - > regs . fxsave , & ustate - > fxsave , sizeof ( ustate - > fxsave ) ) ;
2021-10-15 03:16:13 +02:00
return 0 ;
}
if ( ustate - > xsave . header . xfeatures & ~ xcr0 )
return - EINVAL ;
2021-10-13 16:55:55 +02:00
ret = copy_uabi_from_kernel_to_xstate ( kstate , ustate ) ;
2021-10-15 03:16:13 +02:00
if ( ret )
return ret ;
/* Retrieve PKRU if not in init state */
2021-10-13 16:55:52 +02:00
if ( kstate - > regs . xsave . header . xfeatures & XFEATURE_MASK_PKRU ) {
xpkru = get_xsave_addr ( & kstate - > regs . xsave , XFEATURE_PKRU ) ;
2021-10-15 03:16:13 +02:00
* vpkru = xpkru - > pkru ;
}
/* Ensure that XCOMP_BV is set up for XSAVES */
2021-10-15 01:09:37 +02:00
xstate_init_xcomp_bv ( & kstate - > regs . xsave , kstate - > xfeatures ) ;
2021-10-15 03:16:13 +02:00
return 0 ;
}
2021-10-22 20:55:53 +02:00
EXPORT_SYMBOL_GPL ( fpu_copy_uabi_to_guest_fpstate ) ;
2021-10-15 03:16:13 +02:00
# endif /* CONFIG_KVM */
2021-06-23 14:02:15 +02:00
2021-01-20 21:09:48 -08:00
void kernel_fpu_begin_mask ( unsigned int kfpu_mask )
2012-02-21 10:25:45 -08:00
{
2019-06-04 09:15:23 +02:00
preempt_disable ( ) ;
2012-02-21 10:25:45 -08:00
2015-05-05 11:34:49 +02:00
WARN_ON_FPU ( ! irq_fpu_usable ( ) ) ;
2019-06-04 09:15:23 +02:00
WARN_ON_FPU ( this_cpu_read ( in_kernel_fpu ) ) ;
2015-05-01 10:54:22 +02:00
2019-06-04 09:15:23 +02: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 ) ;
2021-06-23 14:01:59 +02:00
save_fpregs_to_fpstate ( & current - > thread . fpu ) ;
2012-02-21 10:25:45 -08:00
}
2019-04-03 18:41:52 +02:00
__cpu_invalidate_fpregs_state ( ) ;
2020-06-16 11:12:57 +02:00
2021-01-20 21:09:48 -08:00
/* Put sane initial values into the control registers. */
if ( likely ( kfpu_mask & KFPU_MXCSR ) & & boot_cpu_has ( X86_FEATURE_XMM ) )
2020-06-16 11:12:57 +02:00
ldmxcsr ( MXCSR_DEFAULT ) ;
2021-01-20 21:09:48 -08:00
if ( unlikely ( kfpu_mask & KFPU_387 ) & & boot_cpu_has ( X86_FEATURE_FPU ) )
2020-06-16 11:12:57 +02:00
asm volatile ( " fninit " ) ;
2012-02-21 10:25:45 -08:00
}
2021-01-20 21:09:48 -08:00
EXPORT_SYMBOL_GPL ( kernel_fpu_begin_mask ) ;
2015-04-26 12:07:18 +02:00
void kernel_fpu_end ( void )
{
2019-06-04 09:15:22 +02:00
WARN_ON_FPU ( ! this_cpu_read ( in_kernel_fpu ) ) ;
this_cpu_write ( in_kernel_fpu , false ) ;
2015-04-26 12:07:18 +02:00
preempt_enable ( ) ;
}
EXPORT_SYMBOL_GPL ( kernel_fpu_end ) ;
2015-04-03 11:01:36 +02:00
/*
2021-06-23 14:02:06 +02:00
* Sync the FPU register state to current ' s memory register state when the
* current task owns the FPU . The hardware register state is preserved .
2015-04-03 11:01:36 +02:00
*/
2021-06-23 14:02:06 +02:00
void fpu_sync_fpstate ( struct fpu * fpu )
2012-02-21 10:25:45 -08:00
{
2015-05-05 11:34:49 +02:00
WARN_ON_FPU ( fpu ! = & current - > thread . fpu ) ;
2015-04-03 11:06:43 +02:00
2019-04-03 18:41:52 +02:00
fpregs_lock ( ) ;
2016-06-01 10:42:20 -07:00
trace_x86_fpu_before_save ( fpu ) ;
2019-04-03 18:41:36 +02:00
2021-06-23 14:02:00 +02:00
if ( ! test_thread_flag ( TIF_NEED_FPU_LOAD ) )
save_fpregs_to_fpstate ( fpu ) ;
2019-04-03 18:41:36 +02:00
2016-06-01 10:42:20 -07:00
trace_x86_fpu_after_save ( fpu ) ;
2019-04-03 18:41:52 +02:00
fpregs_unlock ( ) ;
2012-02-21 10:25:45 -08:00
}
2021-10-15 03:16:06 +02:00
static inline unsigned int init_fpstate_copy_size ( void )
{
if ( ! use_xsave ( ) )
2021-10-15 01:09:34 +02:00
return fpu_kernel_cfg . default_size ;
2021-10-15 03:16:06 +02:00
/* XSAVE(S) just needs the legacy and the xstate header part */
2021-10-13 16:55:28 +02:00
return sizeof ( init_fpstate . regs . xsave ) ;
2021-10-15 03:16:06 +02:00
}
2021-10-13 16:55:28 +02:00
static inline void fpstate_init_fxstate ( struct fpstate * fpstate )
2021-06-23 14:01:34 +02:00
{
2021-10-13 16:55:28 +02:00
fpstate - > regs . fxsave . cwd = 0x37f ;
fpstate - > regs . fxsave . mxcsr = MXCSR_DEFAULT ;
2021-06-23 14:01:34 +02:00
}
2015-04-30 10:08:36 +02:00
/*
* Legacy x87 fpstate state init :
*/
2021-10-13 16:55:28 +02:00
static inline void fpstate_init_fstate ( struct fpstate * fpstate )
2015-04-30 10:08:36 +02:00
{
2021-10-13 16:55:28 +02:00
fpstate - > regs . fsave . cwd = 0xffff037fu ;
fpstate - > regs . fsave . swd = 0xffff0000u ;
fpstate - > regs . fsave . twd = 0xffffffffu ;
fpstate - > regs . fsave . fos = 0xffff0000u ;
2015-04-30 10:08:36 +02:00
}
2021-10-15 03:16:07 +02:00
/*
* Used in two places :
* 1 ) Early boot to setup init_fpstate for non XSAVE systems
* 2 ) fpu_init_fpstate_user ( ) which is invoked from KVM
*/
2021-10-13 16:55:28 +02:00
void fpstate_init_user ( struct fpstate * fpstate )
2005-04-16 15:20:36 -07:00
{
2021-10-15 03:16:07 +02:00
if ( ! cpu_feature_enabled ( X86_FEATURE_FPU ) ) {
2021-10-13 16:55:28 +02:00
fpstate_init_soft ( & fpstate - > regs . soft ) ;
2010-05-06 11:45:46 +03:00
return ;
2008-05-23 16:26:37 -07:00
}
2021-10-15 01:09:37 +02:00
xstate_init_xcomp_bv ( & fpstate - > regs . xsave , fpstate - > xfeatures ) ;
2015-03-10 07:06:25 +01:00
2021-10-15 03:16:07 +02:00
if ( cpu_feature_enabled ( X86_FEATURE_FXSR ) )
2021-10-13 16:55:28 +02:00
fpstate_init_fxstate ( fpstate ) ;
2015-04-30 10:08:36 +02:00
else
2021-10-13 16:55:28 +02:00
fpstate_init_fstate ( fpstate ) ;
2010-05-06 11:45:46 +03:00
}
2021-10-15 03:16:07 +02:00
2022-01-05 04:35:16 -08:00
static void __fpstate_reset ( struct fpstate * fpstate , u64 xfd )
2021-10-22 20:55:51 +02:00
{
/* Initialize sizes and feature masks */
fpstate - > size = fpu_kernel_cfg . default_size ;
fpstate - > user_size = fpu_user_cfg . default_size ;
fpstate - > xfeatures = fpu_kernel_cfg . default_features ;
fpstate - > user_xfeatures = fpu_user_cfg . default_features ;
2022-01-05 04:35:16 -08:00
fpstate - > xfd = xfd ;
2021-10-22 20:55:51 +02:00
}
2021-10-13 16:55:27 +02:00
void fpstate_reset ( struct fpu * fpu )
{
/* Set the fpstate pointer to the default fpstate */
fpu - > fpstate = & fpu - > __fpstate ;
2022-01-05 04:35:16 -08:00
__fpstate_reset ( fpu - > fpstate , init_fpstate . xfd ) ;
2021-10-21 15:55:08 -07:00
/* Initialize the permission related info in fpu */
fpu - > perm . __state_perm = fpu_kernel_cfg . default_features ;
fpu - > perm . __state_size = fpu_kernel_cfg . default_size ;
fpu - > perm . __user_state_size = fpu_user_cfg . default_size ;
2022-01-05 04:35:12 -08:00
/* Same defaults for guests */
fpu - > guest_perm = fpu - > perm ;
2021-10-13 16:55:27 +02:00
}
2021-10-21 15:55:14 -07:00
static inline void fpu_inherit_perms ( struct fpu * dst_fpu )
{
if ( fpu_state_size_dynamic ( ) ) {
struct fpu * src_fpu = & current - > group_leader - > thread . fpu ;
spin_lock_irq ( & current - > sighand - > siglock ) ;
/* Fork also inherits the permissions of the parent */
dst_fpu - > perm = src_fpu - > perm ;
2022-01-05 04:35:12 -08:00
dst_fpu - > guest_perm = src_fpu - > guest_perm ;
2021-10-21 15:55:14 -07:00
spin_unlock_irq ( & current - > sighand - > siglock ) ;
}
}
2021-06-23 14:02:06 +02:00
/* Clone current's FPU state on fork */
2021-10-21 15:55:14 -07:00
int fpu_clone ( struct task_struct * dst , unsigned long clone_flags )
2015-04-22 20:09:29 +02:00
{
2021-06-23 14:02:06 +02:00
struct fpu * src_fpu = & current - > thread . fpu ;
2019-04-03 18:41:52 +02:00
struct fpu * dst_fpu = & dst - > thread . fpu ;
2021-06-23 14:02:06 +02:00
/* The new task's FPU state cannot be valid in the hardware. */
2016-01-24 14:38:08 -08:00
dst_fpu - > last_cpu = - 1 ;
2021-10-13 16:55:27 +02:00
fpstate_reset ( dst_fpu ) ;
2021-06-23 14:02:06 +02:00
if ( ! cpu_feature_enabled ( X86_FEATURE_FPU ) )
2016-01-24 14:38:08 -08:00
return 0 ;
2021-10-15 03:16:06 +02:00
/*
* Enforce reload for user space tasks and prevent kernel threads
* from trying to save the FPU registers on context switch .
*/
set_tsk_thread_flag ( dst , TIF_NEED_FPU_LOAD ) ;
/*
* No FPU state inheritance for kernel threads and IO
* worker threads .
*/
if ( dst - > flags & ( PF_KTHREAD | PF_IO_WORKER ) ) {
/* Clear out the minimal state */
2021-10-13 16:55:39 +02:00
memcpy ( & dst_fpu - > fpstate - > regs , & init_fpstate . regs ,
2021-10-15 03:16:06 +02:00
init_fpstate_copy_size ( ) ) ;
return 0 ;
}
2021-10-21 15:55:27 -07:00
/*
* If a new feature is added , ensure all dynamic features are
* caller - saved from here !
*/
BUILD_BUG_ON ( XFEATURE_MASK_USER_DYNAMIC ! = XFEATURE_MASK_XTILE_DATA ) ;
2015-04-27 10:08:39 +02:00
/*
2021-10-21 15:55:14 -07:00
* Save the default portion of the current FPU state into the
* clone . Assume all dynamic features to be defined as caller -
* saved , which enables skipping both the expansion of fpstate
* and the copying of any dynamic state .
*
* Do not use memcpy ( ) when TIF_NEED_FPU_LOAD is set because
* copying is not valid when current uses non - default states .
2015-04-27 10:08:39 +02:00
*/
2019-04-03 18:41:52 +02:00
fpregs_lock ( ) ;
2021-10-21 15:55:14 -07:00
if ( test_thread_flag ( TIF_NEED_FPU_LOAD ) )
fpregs_restore_userregs ( ) ;
save_fpregs_to_fpstate ( dst_fpu ) ;
if ( ! ( clone_flags & CLONE_THREAD ) )
fpu_inherit_perms ( dst_fpu ) ;
2019-04-03 18:41:52 +02:00
fpregs_unlock ( ) ;
2016-06-01 10:42:20 -07:00
trace_x86_fpu_copy_src ( src_fpu ) ;
trace_x86_fpu_copy_dst ( dst_fpu ) ;
2015-04-22 15:47:05 +02:00
return 0 ;
}
2021-10-13 16:55:45 +02:00
/*
* Whitelist the FPU register state embedded into task_struct for hardened
* usercopy .
*/
void fpu_thread_struct_whitelist ( unsigned long * offset , unsigned long * size )
{
* offset = offsetof ( struct thread_struct , fpu . __fpstate . regs ) ;
2021-10-15 01:09:34 +02:00
* size = fpu_kernel_cfg . default_size ;
2021-10-13 16:55:45 +02:00
}
2015-04-29 20:24:14 +02: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 15:00:00 +02:00
if ( fpu = = & current - > thread . fpu ) {
2019-04-03 18:41:36 +02:00
/* Ignore delayed exceptions from user space */
asm volatile ( " 1: fwait \n "
" 2: \n "
_ASM_EXTABLE ( 1 b , 2 b ) ) ;
fpregs_deactivate ( fpu ) ;
2015-04-29 20:24:14 +02:00
}
2016-06-01 10:42:20 -07:00
trace_x86_fpu_dropped ( fpu ) ;
2015-04-29 20:24:14 +02:00
preempt_enable ( ) ;
}
2015-04-30 11:21:59 +02:00
/*
2020-05-12 07:54:39 -07:00
* Clear FPU registers by setting them up from the init fpstate .
* Caller must do fpregs_ [ un ] lock ( ) around it .
2015-04-30 11:21:59 +02:00
*/
2021-06-23 14:02:02 +02:00
static inline void restore_fpregs_from_init_fpstate ( u64 features_mask )
2015-04-30 11:21:59 +02:00
{
if ( use_xsave ( ) )
2021-10-21 15:55:19 -07:00
os_xrstor ( & init_fpstate , features_mask ) ;
2021-06-23 14:01:54 +02:00
else if ( use_fxsr ( ) )
2021-10-13 16:55:28 +02:00
fxrstor ( & init_fpstate . regs . fxsave ) ;
2016-03-11 12:32:06 +01:00
else
2021-10-13 16:55:28 +02:00
frstor ( & init_fpstate . regs . 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-29 09:30:21 -07:00
2021-06-23 14:02:11 +02:00
pkru_write_default ( ) ;
2015-04-30 11:21:59 +02:00
}
2015-04-29 20:24:14 +02:00
/*
2021-06-23 14:02:13 +02:00
* Reset current - > fpu memory state to the init values .
2015-04-29 08:46:26 +02:00
*/
2021-10-21 15:55:15 -07:00
static void fpu_reset_fpregs ( void )
2021-06-23 14:02:13 +02:00
{
struct fpu * fpu = & current - > thread . fpu ;
fpregs_lock ( ) ;
fpu__drop ( fpu ) ;
/*
* This does not change the actual hardware registers . It just
* resets the memory image and sets TIF_NEED_FPU_LOAD so a
* subsequent return to usermode will reload the registers from the
* task ' s memory image .
*
* Do not use fpstate_init ( ) here . Just copy init_fpstate which has
* the correct content already except for PKRU .
2021-06-23 14:02:22 +02:00
*
* PKRU handling does not rely on the xstate when restoring for
* user space as PKRU is eagerly written in switch_to ( ) and
* flush_thread ( ) .
2021-06-23 14:02:13 +02:00
*/
2021-10-13 16:55:39 +02:00
memcpy ( & fpu - > fpstate - > regs , & init_fpstate . regs , init_fpstate_copy_size ( ) ) ;
2021-06-23 14:02:13 +02:00
set_thread_flag ( TIF_NEED_FPU_LOAD ) ;
fpregs_unlock ( ) ;
}
/*
* Reset current ' s user FPU states to the init states . current ' s
* supervisor states , if any , are not modified by this function . The
* caller guarantees that the XSTATE header in memory is intact .
*/
void fpu__clear_user_states ( struct fpu * fpu )
2015-04-22 11:52:13 +02:00
{
2020-05-12 07:54:39 -07:00
WARN_ON_FPU ( fpu ! = & current - > thread . fpu ) ;
2015-04-23 12:46:20 +02:00
2021-06-23 14:02:13 +02:00
fpregs_lock ( ) ;
if ( ! cpu_feature_enabled ( X86_FEATURE_FPU ) ) {
2021-10-21 15:55:15 -07:00
fpu_reset_fpregs ( ) ;
2021-06-23 14:02:13 +02:00
fpregs_unlock ( ) ;
2020-05-12 07:54:39 -07:00
return ;
}
2016-11-17 09:11:35 -08:00
2021-06-23 14:02:13 +02:00
/*
* Ensure that current ' s supervisor states are loaded into their
* corresponding registers .
*/
if ( xfeatures_mask_supervisor ( ) & &
2021-10-21 15:55:19 -07:00
! fpregs_state_valid ( fpu , smp_processor_id ( ) ) )
os_xrstor_supervisor ( fpu - > fpstate ) ;
2020-05-12 07:54:39 -07:00
2021-06-23 14:02:13 +02:00
/* Reset user states in registers. */
2021-10-15 01:09:38 +02:00
restore_fpregs_from_init_fpstate ( XFEATURE_MASK_USER_RESTORE ) ;
2021-06-23 14:02:13 +02:00
/*
* Now all FPU registers have their desired values . Inform the FPU
* state machine that current ' s FPU registers are in the hardware
* registers . The memory image does not need to be updated because
* any operation relying on it has to save the registers first when
* current ' s FPU is marked active .
*/
2020-05-12 07:54:39 -07:00
fpregs_mark_activate ( ) ;
fpregs_unlock ( ) ;
}
2021-06-23 14:02:12 +02:00
void fpu_flush_thread ( void )
2020-05-12 07:54:39 -07:00
{
2021-10-21 15:55:15 -07:00
fpstate_reset ( & current - > thread . fpu ) ;
fpu_reset_fpregs ( ) ;
2015-04-22 11:52:13 +02:00
}
2019-04-03 18:41:52 +02:00
/*
* Load FPU context before returning to userspace .
*/
void switch_fpu_return ( void )
{
if ( ! static_cpu_has ( X86_FEATURE_FPU ) )
return ;
2021-06-23 14:02:14 +02:00
fpregs_restore_userregs ( ) ;
2019-04-03 18:41:52 +02:00
}
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 = & current - > 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 = & current - > thread . fpu ;
fpregs_activate ( fpu ) ;
fpu - > last_cpu = smp_processor_id ( ) ;
clear_thread_flag ( TIF_NEED_FPU_LOAD ) ;
}
2015-04-30 09:29:38 +02: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 08:29:55 +02:00
* fully reproduce the context of the exception .
2015-04-30 09:29:38 +02:00
*/
2016-04-05 08:29:55 +02:00
if ( boot_cpu_has ( X86_FEATURE_FXSR ) ) {
2021-10-13 16:55:39 +02:00
cwd = fpu - > fpstate - > regs . fxsave . cwd ;
swd = fpu - > fpstate - > regs . fxsave . swd ;
2016-04-05 08:29:55 +02:00
} else {
2021-10-13 16:55:39 +02:00
cwd = ( unsigned short ) fpu - > fpstate - > regs . fsave . cwd ;
swd = ( unsigned short ) fpu - > fpstate - > regs . fsave . swd ;
2016-04-05 08:29:55 +02:00
}
2015-04-30 09:29:38 +02: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 08:29:55 +02:00
unsigned short mxcsr = MXCSR_DEFAULT ;
if ( boot_cpu_has ( X86_FEATURE_XMM ) )
2021-10-13 16:55:39 +02:00
mxcsr = fpu - > fpstate - > regs . fxsave . mxcsr ;
2016-04-05 08:29:55 +02:00
2015-04-30 09:29:38 +02: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 ;
}