2008-01-30 12:31:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1994 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Pentium III FXSR, SSE support
|
|
|
|
* General FPU state handling cleanups
|
|
|
|
* Gareth Hughes <gareth@valinux.com>, May 2000
|
|
|
|
* x86-64 work by Andi Kleen 2002
|
|
|
|
*/
|
|
|
|
|
2008-10-23 05:26:29 +00:00
|
|
|
#ifndef _ASM_X86_I387_H
|
|
|
|
#define _ASM_X86_I387_H
|
2008-01-30 12:31:51 +00:00
|
|
|
|
2009-11-03 14:11:15 +00:00
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
|
2008-01-30 12:31:51 +00:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/kernel_stat.h>
|
|
|
|
#include <linux/regset.h>
|
crypto: padlock - fix VIA PadLock instruction usage with irq_ts_save/restore()
Wolfgang Walter reported this oops on his via C3 using padlock for
AES-encryption:
##################################################################
BUG: unable to handle kernel NULL pointer dereference at 000001f0
IP: [<c01028c5>] __switch_to+0x30/0x117
*pde = 00000000
Oops: 0002 [#1] PREEMPT
Modules linked in:
Pid: 2071, comm: sleep Not tainted (2.6.26 #11)
EIP: 0060:[<c01028c5>] EFLAGS: 00010002 CPU: 0
EIP is at __switch_to+0x30/0x117
EAX: 00000000 EBX: c0493300 ECX: dc48dd00 EDX: c0493300
ESI: dc48dd00 EDI: c0493530 EBP: c04cff8c ESP: c04cff7c
DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068
Process sleep (pid: 2071, ti=c04ce000 task=dc48dd00 task.ti=d2fe6000)
Stack: dc48df30 c0493300 00000000 00000000 d2fe7f44 c03b5b43 c04cffc8 00000046
c0131856 0000005a dc472d3c c0493300 c0493470 d983ae00 00002696 00000000
c0239f54 00000000 c04c4000 c04cffd8 c01025fe c04f3740 00049800 c04cffe0
Call Trace:
[<c03b5b43>] ? schedule+0x285/0x2ff
[<c0131856>] ? pm_qos_requirement+0x3c/0x53
[<c0239f54>] ? acpi_processor_idle+0x0/0x434
[<c01025fe>] ? cpu_idle+0x73/0x7f
[<c03a4dcd>] ? rest_init+0x61/0x63
=======================
Wolfgang also found out that adding kernel_fpu_begin() and kernel_fpu_end()
around the padlock instructions fix the oops.
Suresh wrote:
These padlock instructions though don't use/touch SSE registers, but it behaves
similar to other SSE instructions. For example, it might cause DNA faults
when cr0.ts is set. While this is a spurious DNA trap, it might cause
oops with the recent fpu code changes.
This is the code sequence that is probably causing this problem:
a) new app is getting exec'd and it is somewhere in between
start_thread() and flush_old_exec() in the load_xyz_binary()
b) At pont "a", task's fpu state (like TS_USEDFPU, used_math() etc) is
cleared.
c) Now we get an interrupt/softirq which starts using these encrypt/decrypt
routines in the network stack. This generates a math fault (as
cr0.ts is '1') which sets TS_USEDFPU and restores the math that is
in the task's xstate.
d) Return to exec code path, which does start_thread() which does
free_thread_xstate() and sets xstate pointer to NULL while
the TS_USEDFPU is still set.
e) At the next context switch from the new exec'd task to another task,
we have a scenarios where TS_USEDFPU is set but xstate pointer is null.
This can cause an oops during unlazy_fpu() in __switch_to()
Now:
1) This should happen with or with out pre-emption. Viro also encountered
similar problem with out CONFIG_PREEMPT.
2) kernel_fpu_begin() and kernel_fpu_end() will fix this problem, because
kernel_fpu_begin() will manually do a clts() and won't run in to the
situation of setting TS_USEDFPU in step "c" above.
3) This was working before the fpu changes, because its a spurious
math fault which doesn't corrupt any fpu/sse registers and the task's
math state was always in an allocated state.
With out the recent lazy fpu allocation changes, while we don't see oops,
there is a possible race still present in older kernels(for example,
while kernel is using kernel_fpu_begin() in some optimized clear/copy
page and an interrupt/softirq happens which uses these padlock
instructions generating DNA fault).
This is the failing scenario that existed even before the lazy fpu allocation
changes:
0. CPU's TS flag is set
1. kernel using FPU in some optimized copy routine and while doing
kernel_fpu_begin() takes an interrupt just before doing clts()
2. Takes an interrupt and ipsec uses padlock instruction. And we
take a DNA fault as TS flag is still set.
3. We handle the DNA fault and set TS_USEDFPU and clear cr0.ts
4. We complete the padlock routine
5. Go back to step-1, which resumes clts() in kernel_fpu_begin(), finishes
the optimized copy routine and does kernel_fpu_end(). At this point,
we have cr0.ts again set to '1' but the task's TS_USEFPU is stilll
set and not cleared.
6. Now kernel resumes its user operation. And at the next context
switch, kernel sees it has do a FP save as TS_USEDFPU is still set
and then will do a unlazy_fpu() in __switch_to(). unlazy_fpu()
will take a DNA fault, as cr0.ts is '1' and now, because we are
in __switch_to(), math_state_restore() will get confused and will
restore the next task's FP state and will save it in prev tasks's FP state.
Remember, in __switch_to() we are already on the stack of the next task
but take a DNA fault for the prev task.
This causes the fpu leakage.
Fix the padlock instruction usage by calling them inside the
context of new routines irq_ts_save/restore(), which clear/restore cr0.ts
manually in the interrupt context. This will not generate spurious DNA
in the context of the interrupt which will fix the oops encountered and
the possible FPU leakage issue.
Reported-and-bisected-by: Wolfgang Walter <wolfgang.walter@stwm.de>
Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2008-08-13 12:02:26 +00:00
|
|
|
#include <linux/hardirq.h>
|
2010-05-06 08:45:46 +00:00
|
|
|
#include <linux/slab.h>
|
2008-02-04 15:47:58 +00:00
|
|
|
#include <asm/asm.h>
|
2010-05-12 00:49:54 +00:00
|
|
|
#include <asm/cpufeature.h>
|
2008-01-30 12:31:51 +00:00
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/sigcontext.h>
|
|
|
|
#include <asm/user.h>
|
|
|
|
#include <asm/uaccess.h>
|
2008-07-29 17:29:19 +00:00
|
|
|
#include <asm/xsave.h>
|
2008-01-30 12:31:51 +00:00
|
|
|
|
2008-07-29 17:29:21 +00:00
|
|
|
extern unsigned int sig_xstate_size;
|
2008-01-30 12:31:51 +00:00
|
|
|
extern void fpu_init(void);
|
|
|
|
extern void mxcsr_feature_mask_init(void);
|
2008-03-10 22:28:05 +00:00
|
|
|
extern int init_fpu(struct task_struct *child);
|
2008-01-30 12:31:51 +00:00
|
|
|
extern asmlinkage void math_state_restore(void);
|
2009-04-24 07:40:59 +00:00
|
|
|
extern void __math_state_restore(void);
|
2008-07-21 17:01:57 +00:00
|
|
|
extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);
|
2008-01-30 12:31:51 +00:00
|
|
|
|
|
|
|
extern user_regset_active_fn fpregs_active, xfpregs_active;
|
2010-02-11 19:50:59 +00:00
|
|
|
extern user_regset_get_fn fpregs_get, xfpregs_get, fpregs_soft_get,
|
|
|
|
xstateregs_get;
|
|
|
|
extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set,
|
|
|
|
xstateregs_set;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xstateregs_active == fpregs_active. Please refer to the comment
|
|
|
|
* at the definition of fpregs_active.
|
|
|
|
*/
|
|
|
|
#define xstateregs_active fpregs_active
|
2008-01-30 12:31:51 +00:00
|
|
|
|
2008-07-29 17:29:25 +00:00
|
|
|
extern struct _fpx_sw_bytes fx_sw_reserved;
|
2008-01-30 12:31:51 +00:00
|
|
|
#ifdef CONFIG_IA32_EMULATION
|
2008-07-29 17:29:21 +00:00
|
|
|
extern unsigned int sig_xstate_ia32_size;
|
2008-07-29 17:29:25 +00:00
|
|
|
extern struct _fpx_sw_bytes fx_sw_reserved_ia32;
|
2008-01-30 12:31:51 +00:00
|
|
|
struct _fpstate_ia32;
|
2008-07-29 17:29:22 +00:00
|
|
|
struct _xstate_ia32;
|
|
|
|
extern int save_i387_xstate_ia32(void __user *buf);
|
|
|
|
extern int restore_i387_xstate_ia32(void __user *buf);
|
2008-01-30 12:31:51 +00:00
|
|
|
#endif
|
|
|
|
|
2010-09-04 01:17:16 +00:00
|
|
|
#ifdef CONFIG_MATH_EMULATION
|
|
|
|
extern void finit_soft_fpu(struct i387_soft_struct *soft);
|
|
|
|
#else
|
|
|
|
static inline void finit_soft_fpu(struct i387_soft_struct *soft) {}
|
|
|
|
#endif
|
|
|
|
|
2008-07-29 17:29:20 +00:00
|
|
|
#define X87_FSW_ES (1 << 7) /* Exception Summary */
|
|
|
|
|
2010-07-19 23:05:49 +00:00
|
|
|
static __always_inline __pure bool use_xsaveopt(void)
|
|
|
|
{
|
2010-07-19 23:05:52 +00:00
|
|
|
return static_cpu_has(X86_FEATURE_XSAVEOPT);
|
2010-07-19 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
2010-05-12 00:49:54 +00:00
|
|
|
static __always_inline __pure bool use_xsave(void)
|
2010-05-06 08:45:45 +00:00
|
|
|
{
|
2010-05-12 00:49:54 +00:00
|
|
|
return static_cpu_has(X86_FEATURE_XSAVE);
|
2010-05-06 08:45:45 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:17:18 +00:00
|
|
|
static __always_inline __pure bool use_fxsr(void)
|
|
|
|
{
|
|
|
|
return static_cpu_has(X86_FEATURE_FXSR);
|
|
|
|
}
|
|
|
|
|
2010-07-19 23:05:49 +00:00
|
|
|
extern void __sanitize_i387_state(struct task_struct *);
|
|
|
|
|
|
|
|
static inline void sanitize_i387_state(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (!use_xsaveopt())
|
|
|
|
return;
|
|
|
|
__sanitize_i387_state(tsk);
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:31:51 +00:00
|
|
|
#ifdef CONFIG_X86_64
|
2008-07-29 17:29:20 +00:00
|
|
|
static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
|
2008-01-30 12:31:51 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2010-09-04 01:17:14 +00:00
|
|
|
/* See comment in fxsave() below. */
|
2010-10-22 22:33:38 +00:00
|
|
|
#ifdef CONFIG_AS_FXSAVEQ
|
|
|
|
asm volatile("1: fxrstorq %[fx]\n\t"
|
|
|
|
"2:\n"
|
|
|
|
".section .fixup,\"ax\"\n"
|
|
|
|
"3: movl $-1,%[err]\n"
|
|
|
|
" jmp 2b\n"
|
|
|
|
".previous\n"
|
|
|
|
_ASM_EXTABLE(1b, 3b)
|
|
|
|
: [err] "=r" (err)
|
|
|
|
: [fx] "m" (*fx), "0" (0));
|
|
|
|
#else
|
2008-01-30 12:31:51 +00:00
|
|
|
asm volatile("1: rex64/fxrstor (%[fx])\n\t"
|
|
|
|
"2:\n"
|
|
|
|
".section .fixup,\"ax\"\n"
|
|
|
|
"3: movl $-1,%[err]\n"
|
|
|
|
" jmp 2b\n"
|
|
|
|
".previous\n"
|
2008-03-23 08:02:18 +00:00
|
|
|
_ASM_EXTABLE(1b, 3b)
|
2008-01-30 12:31:51 +00:00
|
|
|
: [err] "=r" (err)
|
2010-09-04 01:17:14 +00:00
|
|
|
: [fx] "R" (fx), "m" (*fx), "0" (0));
|
2010-10-22 22:33:38 +00:00
|
|
|
#endif
|
2008-01-30 12:31:51 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-07-29 17:29:25 +00:00
|
|
|
static inline int fxsave_user(struct i387_fxsave_struct __user *fx)
|
2008-01-30 12:31:51 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2010-06-22 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* Clear the bytes not touched by the fxsave and reserved
|
|
|
|
* for the SW usage.
|
|
|
|
*/
|
|
|
|
err = __clear_user(&fx->sw_reserved,
|
|
|
|
sizeof(struct _fpx_sw_bytes));
|
|
|
|
if (unlikely(err))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2010-09-04 01:17:14 +00:00
|
|
|
/* See comment in fxsave() below. */
|
2010-10-22 22:33:38 +00:00
|
|
|
#ifdef CONFIG_AS_FXSAVEQ
|
|
|
|
asm volatile("1: fxsaveq %[fx]\n\t"
|
|
|
|
"2:\n"
|
|
|
|
".section .fixup,\"ax\"\n"
|
|
|
|
"3: movl $-1,%[err]\n"
|
|
|
|
" jmp 2b\n"
|
|
|
|
".previous\n"
|
|
|
|
_ASM_EXTABLE(1b, 3b)
|
|
|
|
: [err] "=r" (err), [fx] "=m" (*fx)
|
|
|
|
: "0" (0));
|
|
|
|
#else
|
2008-01-30 12:31:51 +00:00
|
|
|
asm volatile("1: rex64/fxsave (%[fx])\n\t"
|
|
|
|
"2:\n"
|
|
|
|
".section .fixup,\"ax\"\n"
|
|
|
|
"3: movl $-1,%[err]\n"
|
|
|
|
" jmp 2b\n"
|
|
|
|
".previous\n"
|
2008-03-23 08:02:18 +00:00
|
|
|
_ASM_EXTABLE(1b, 3b)
|
2008-01-30 12:31:51 +00:00
|
|
|
: [err] "=r" (err), "=m" (*fx)
|
2010-09-04 01:17:14 +00:00
|
|
|
: [fx] "R" (fx), "0" (0));
|
2010-10-22 22:33:38 +00:00
|
|
|
#endif
|
2008-03-23 08:02:18 +00:00
|
|
|
if (unlikely(err) &&
|
|
|
|
__clear_user(fx, sizeof(struct i387_fxsave_struct)))
|
2008-01-30 12:31:51 +00:00
|
|
|
err = -EFAULT;
|
|
|
|
/* No need to clear here because the caller clears USED_MATH */
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:45:46 +00:00
|
|
|
static inline void fpu_fxsave(struct fpu *fpu)
|
2008-01-30 12:31:51 +00:00
|
|
|
{
|
|
|
|
/* Using "rex64; fxsave %0" is broken because, if the memory operand
|
|
|
|
uses any extended registers for addressing, a second REX prefix
|
|
|
|
will be generated (to the assembler, rex64 followed by semicolon
|
|
|
|
is a separate instruction), and hence the 64-bitness is lost. */
|
Merge branch 'x86-fpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'x86-fpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip:
x86, fpu: Merge fpu_save_init()
x86-32, fpu: Rewrite fpu_save_init()
x86, fpu: Remove PSHUFB_XMM5_* macros
x86, fpu: Remove unnecessary ifdefs from i387 code.
x86-32, fpu: Remove math_emulate stub
x86-64, fpu: Simplify constraints for fxsave/fxtstor
x86-64, fpu: Fix %cs value in convert_from_fxsr()
x86-64, fpu: Disable preemption when using TS_USEDFPU
x86, fpu: Merge __save_init_fpu()
x86, fpu: Merge tolerant_fwait()
x86, fpu: Merge fpu_init()
x86: Use correct type for %cr4
x86, xsave: Disable xsave in i387 emulation mode
Fixed up fxsaveq-induced conflict in arch/x86/include/asm/i387.h
2010-10-21 20:34:32 +00:00
|
|
|
|
2010-10-13 23:00:29 +00:00
|
|
|
#ifdef CONFIG_AS_FXSAVEQ
|
2008-01-30 12:31:51 +00:00
|
|
|
/* Using "fxsaveq %0" would be the ideal choice, but is only supported
|
|
|
|
starting with gas 2.16. */
|
|
|
|
__asm__ __volatile__("fxsaveq %0"
|
2010-05-06 08:45:46 +00:00
|
|
|
: "=m" (fpu->state->fxsave));
|
Merge branch 'x86-fpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'x86-fpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip:
x86, fpu: Merge fpu_save_init()
x86-32, fpu: Rewrite fpu_save_init()
x86, fpu: Remove PSHUFB_XMM5_* macros
x86, fpu: Remove unnecessary ifdefs from i387 code.
x86-32, fpu: Remove math_emulate stub
x86-64, fpu: Simplify constraints for fxsave/fxtstor
x86-64, fpu: Fix %cs value in convert_from_fxsr()
x86-64, fpu: Disable preemption when using TS_USEDFPU
x86, fpu: Merge __save_init_fpu()
x86, fpu: Merge tolerant_fwait()
x86, fpu: Merge fpu_init()
x86: Use correct type for %cr4
x86, xsave: Disable xsave in i387 emulation mode
Fixed up fxsaveq-induced conflict in arch/x86/include/asm/i387.h
2010-10-21 20:34:32 +00:00
|
|
|
#else
|
2008-01-30 12:31:51 +00:00
|
|
|
/* Using, as a workaround, the properly prefixed form below isn't
|
|
|
|
accepted by any binutils version so far released, complaining that
|
|
|
|
the same type of prefix is used twice if an extended register is
|
2010-09-04 01:17:14 +00:00
|
|
|
needed for addressing (fix submitted to mainline 2005-11-21).
|
|
|
|
asm volatile("rex64/fxsave %0"
|
|
|
|
: "=m" (fpu->state->fxsave));
|
|
|
|
This, however, we can work around by forcing the compiler to select
|
2008-01-30 12:31:51 +00:00
|
|
|
an addressing mode that doesn't require extended registers. */
|
2010-09-04 01:17:14 +00:00
|
|
|
asm volatile("rex64/fxsave (%[fx])"
|
|
|
|
: "=m" (fpu->state->fxsave)
|
|
|
|
: [fx] "R" (&fpu->state->fxsave));
|
2008-01-30 12:31:51 +00:00
|
|
|
#endif
|
2008-07-29 17:29:20 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 12:31:51 +00:00
|
|
|
#else /* CONFIG_X86_32 */
|
|
|
|
|
2009-04-08 11:31:59 +00:00
|
|
|
/* perform fxrstor iff the processor has extended states, otherwise frstor */
|
|
|
|
static inline int fxrstor_checking(struct i387_fxsave_struct *fx)
|
2008-01-30 12:31:51 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The "nop" is needed to make the instructions the same
|
|
|
|
* length.
|
|
|
|
*/
|
|
|
|
alternative_input(
|
|
|
|
"nop ; frstor %1",
|
|
|
|
"fxrstor %1",
|
|
|
|
X86_FEATURE_FXSR,
|
2009-04-08 11:31:59 +00:00
|
|
|
"m" (*fx));
|
|
|
|
|
2009-04-08 11:31:58 +00:00
|
|
|
return 0;
|
2008-01-30 12:31:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:17:18 +00:00
|
|
|
static inline void fpu_fxsave(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
asm volatile("fxsave %[fx]"
|
|
|
|
: [fx] "=m" (fpu->state->fxsave));
|
|
|
|
}
|
|
|
|
|
2010-09-04 01:17:19 +00:00
|
|
|
#endif /* CONFIG_X86_64 */
|
|
|
|
|
2008-01-30 12:31:51 +00:00
|
|
|
/* We need a safe address that is cheap to find and that is already
|
|
|
|
in L1 during context switch. The best choices are unfortunately
|
|
|
|
different for UP and SMP */
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
#define safe_address (__per_cpu_offset[0])
|
|
|
|
#else
|
|
|
|
#define safe_address (kstat_cpu(0).cpustat.user)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These must be called with preempt disabled
|
|
|
|
*/
|
2010-05-06 08:45:46 +00:00
|
|
|
static inline void fpu_save_init(struct fpu *fpu)
|
2008-01-30 12:31:51 +00:00
|
|
|
{
|
2010-05-06 08:45:45 +00:00
|
|
|
if (use_xsave()) {
|
2010-05-06 08:45:46 +00:00
|
|
|
fpu_xsave(fpu);
|
2008-07-29 17:29:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* xsave header may indicate the init state of the FP.
|
|
|
|
*/
|
2010-09-04 01:17:18 +00:00
|
|
|
if (!(fpu->state->xsave.xsave_hdr.xstate_bv & XSTATE_FP))
|
|
|
|
return;
|
|
|
|
} else if (use_fxsr()) {
|
|
|
|
fpu_fxsave(fpu);
|
|
|
|
} else {
|
|
|
|
asm volatile("fsave %[fx]; fwait"
|
|
|
|
: [fx] "=m" (fpu->state->fsave));
|
|
|
|
return;
|
2008-07-29 17:29:20 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 01:17:18 +00:00
|
|
|
if (unlikely(fpu->state->fxsave.swd & X87_FSW_ES))
|
|
|
|
asm volatile("fnclex");
|
|
|
|
|
2008-01-30 12:31:51 +00:00
|
|
|
/* AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception
|
|
|
|
is pending. Clear the x87 state here by setting it to fixed
|
|
|
|
values. safe_address is a random variable that should be in L1 */
|
|
|
|
alternative_input(
|
2010-09-04 01:17:19 +00:00
|
|
|
ASM_NOP8 ASM_NOP2,
|
2008-01-30 12:31:51 +00:00
|
|
|
"emms\n\t" /* clear stack tags */
|
2010-09-04 01:17:19 +00:00
|
|
|
"fildl %P[addr]", /* set F?P to defined value */
|
2008-01-30 12:31:51 +00:00
|
|
|
X86_FEATURE_FXSAVE_LEAK,
|
|
|
|
[addr] "m" (safe_address));
|
2010-05-06 08:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void __save_init_fpu(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
fpu_save_init(&tsk->thread.fpu);
|
2008-01-30 12:31:51 +00:00
|
|
|
task_thread_info(tsk)->status &= ~TS_USEDFPU;
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:45:46 +00:00
|
|
|
static inline int fpu_fxrstor_checking(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
return fxrstor_checking(&fpu->state->fxsave);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fpu_restore_checking(struct fpu *fpu)
|
2009-04-08 11:31:59 +00:00
|
|
|
{
|
2010-05-06 08:45:45 +00:00
|
|
|
if (use_xsave())
|
2010-05-06 08:45:46 +00:00
|
|
|
return fpu_xrstor_checking(fpu);
|
2009-04-08 11:31:59 +00:00
|
|
|
else
|
2010-05-06 08:45:46 +00:00
|
|
|
return fpu_fxrstor_checking(fpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int restore_fpu_checking(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
return fpu_restore_checking(&tsk->thread.fpu);
|
2009-04-08 11:31:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 12:31:51 +00:00
|
|
|
/*
|
|
|
|
* Signal frame handlers...
|
|
|
|
*/
|
2008-07-29 17:29:22 +00:00
|
|
|
extern int save_i387_xstate(void __user *buf);
|
|
|
|
extern int restore_i387_xstate(void __user *buf);
|
2008-01-30 12:31:51 +00:00
|
|
|
|
|
|
|
static inline void __unlazy_fpu(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (task_thread_info(tsk)->status & TS_USEDFPU) {
|
|
|
|
__save_init_fpu(tsk);
|
|
|
|
stts();
|
|
|
|
} else
|
|
|
|
tsk->fpu_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void __clear_fpu(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (task_thread_info(tsk)->status & TS_USEDFPU) {
|
2010-09-04 01:17:10 +00:00
|
|
|
/* Ignore delayed exceptions from user space */
|
|
|
|
asm volatile("1: fwait\n"
|
|
|
|
"2:\n"
|
|
|
|
_ASM_EXTABLE(1b, 2b));
|
2008-01-30 12:31:51 +00:00
|
|
|
task_thread_info(tsk)->status &= ~TS_USEDFPU;
|
|
|
|
stts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kernel_fpu_begin(void)
|
|
|
|
{
|
|
|
|
struct thread_info *me = current_thread_info();
|
|
|
|
preempt_disable();
|
|
|
|
if (me->status & TS_USEDFPU)
|
|
|
|
__save_init_fpu(me->task);
|
|
|
|
else
|
|
|
|
clts();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void kernel_fpu_end(void)
|
|
|
|
{
|
|
|
|
stts();
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
2009-08-31 05:11:54 +00:00
|
|
|
static inline bool irq_fpu_usable(void)
|
|
|
|
{
|
|
|
|
struct pt_regs *regs;
|
|
|
|
|
|
|
|
return !in_interrupt() || !(regs = get_irq_regs()) || \
|
|
|
|
user_mode(regs) || (read_cr0() & X86_CR0_TS);
|
|
|
|
}
|
|
|
|
|
crypto: padlock - fix VIA PadLock instruction usage with irq_ts_save/restore()
Wolfgang Walter reported this oops on his via C3 using padlock for
AES-encryption:
##################################################################
BUG: unable to handle kernel NULL pointer dereference at 000001f0
IP: [<c01028c5>] __switch_to+0x30/0x117
*pde = 00000000
Oops: 0002 [#1] PREEMPT
Modules linked in:
Pid: 2071, comm: sleep Not tainted (2.6.26 #11)
EIP: 0060:[<c01028c5>] EFLAGS: 00010002 CPU: 0
EIP is at __switch_to+0x30/0x117
EAX: 00000000 EBX: c0493300 ECX: dc48dd00 EDX: c0493300
ESI: dc48dd00 EDI: c0493530 EBP: c04cff8c ESP: c04cff7c
DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068
Process sleep (pid: 2071, ti=c04ce000 task=dc48dd00 task.ti=d2fe6000)
Stack: dc48df30 c0493300 00000000 00000000 d2fe7f44 c03b5b43 c04cffc8 00000046
c0131856 0000005a dc472d3c c0493300 c0493470 d983ae00 00002696 00000000
c0239f54 00000000 c04c4000 c04cffd8 c01025fe c04f3740 00049800 c04cffe0
Call Trace:
[<c03b5b43>] ? schedule+0x285/0x2ff
[<c0131856>] ? pm_qos_requirement+0x3c/0x53
[<c0239f54>] ? acpi_processor_idle+0x0/0x434
[<c01025fe>] ? cpu_idle+0x73/0x7f
[<c03a4dcd>] ? rest_init+0x61/0x63
=======================
Wolfgang also found out that adding kernel_fpu_begin() and kernel_fpu_end()
around the padlock instructions fix the oops.
Suresh wrote:
These padlock instructions though don't use/touch SSE registers, but it behaves
similar to other SSE instructions. For example, it might cause DNA faults
when cr0.ts is set. While this is a spurious DNA trap, it might cause
oops with the recent fpu code changes.
This is the code sequence that is probably causing this problem:
a) new app is getting exec'd and it is somewhere in between
start_thread() and flush_old_exec() in the load_xyz_binary()
b) At pont "a", task's fpu state (like TS_USEDFPU, used_math() etc) is
cleared.
c) Now we get an interrupt/softirq which starts using these encrypt/decrypt
routines in the network stack. This generates a math fault (as
cr0.ts is '1') which sets TS_USEDFPU and restores the math that is
in the task's xstate.
d) Return to exec code path, which does start_thread() which does
free_thread_xstate() and sets xstate pointer to NULL while
the TS_USEDFPU is still set.
e) At the next context switch from the new exec'd task to another task,
we have a scenarios where TS_USEDFPU is set but xstate pointer is null.
This can cause an oops during unlazy_fpu() in __switch_to()
Now:
1) This should happen with or with out pre-emption. Viro also encountered
similar problem with out CONFIG_PREEMPT.
2) kernel_fpu_begin() and kernel_fpu_end() will fix this problem, because
kernel_fpu_begin() will manually do a clts() and won't run in to the
situation of setting TS_USEDFPU in step "c" above.
3) This was working before the fpu changes, because its a spurious
math fault which doesn't corrupt any fpu/sse registers and the task's
math state was always in an allocated state.
With out the recent lazy fpu allocation changes, while we don't see oops,
there is a possible race still present in older kernels(for example,
while kernel is using kernel_fpu_begin() in some optimized clear/copy
page and an interrupt/softirq happens which uses these padlock
instructions generating DNA fault).
This is the failing scenario that existed even before the lazy fpu allocation
changes:
0. CPU's TS flag is set
1. kernel using FPU in some optimized copy routine and while doing
kernel_fpu_begin() takes an interrupt just before doing clts()
2. Takes an interrupt and ipsec uses padlock instruction. And we
take a DNA fault as TS flag is still set.
3. We handle the DNA fault and set TS_USEDFPU and clear cr0.ts
4. We complete the padlock routine
5. Go back to step-1, which resumes clts() in kernel_fpu_begin(), finishes
the optimized copy routine and does kernel_fpu_end(). At this point,
we have cr0.ts again set to '1' but the task's TS_USEFPU is stilll
set and not cleared.
6. Now kernel resumes its user operation. And at the next context
switch, kernel sees it has do a FP save as TS_USEDFPU is still set
and then will do a unlazy_fpu() in __switch_to(). unlazy_fpu()
will take a DNA fault, as cr0.ts is '1' and now, because we are
in __switch_to(), math_state_restore() will get confused and will
restore the next task's FP state and will save it in prev tasks's FP state.
Remember, in __switch_to() we are already on the stack of the next task
but take a DNA fault for the prev task.
This causes the fpu leakage.
Fix the padlock instruction usage by calling them inside the
context of new routines irq_ts_save/restore(), which clear/restore cr0.ts
manually in the interrupt context. This will not generate spurious DNA
in the context of the interrupt which will fix the oops encountered and
the possible FPU leakage issue.
Reported-and-bisected-by: Wolfgang Walter <wolfgang.walter@stwm.de>
Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2008-08-13 12:02:26 +00:00
|
|
|
/*
|
|
|
|
* Some instructions like VIA's padlock instructions generate a spurious
|
|
|
|
* DNA fault but don't modify SSE registers. And these instructions
|
2009-06-09 14:40:50 +00:00
|
|
|
* get used from interrupt context as well. To prevent these kernel instructions
|
|
|
|
* in interrupt context interacting wrongly with other user/kernel fpu usage, we
|
crypto: padlock - fix VIA PadLock instruction usage with irq_ts_save/restore()
Wolfgang Walter reported this oops on his via C3 using padlock for
AES-encryption:
##################################################################
BUG: unable to handle kernel NULL pointer dereference at 000001f0
IP: [<c01028c5>] __switch_to+0x30/0x117
*pde = 00000000
Oops: 0002 [#1] PREEMPT
Modules linked in:
Pid: 2071, comm: sleep Not tainted (2.6.26 #11)
EIP: 0060:[<c01028c5>] EFLAGS: 00010002 CPU: 0
EIP is at __switch_to+0x30/0x117
EAX: 00000000 EBX: c0493300 ECX: dc48dd00 EDX: c0493300
ESI: dc48dd00 EDI: c0493530 EBP: c04cff8c ESP: c04cff7c
DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068
Process sleep (pid: 2071, ti=c04ce000 task=dc48dd00 task.ti=d2fe6000)
Stack: dc48df30 c0493300 00000000 00000000 d2fe7f44 c03b5b43 c04cffc8 00000046
c0131856 0000005a dc472d3c c0493300 c0493470 d983ae00 00002696 00000000
c0239f54 00000000 c04c4000 c04cffd8 c01025fe c04f3740 00049800 c04cffe0
Call Trace:
[<c03b5b43>] ? schedule+0x285/0x2ff
[<c0131856>] ? pm_qos_requirement+0x3c/0x53
[<c0239f54>] ? acpi_processor_idle+0x0/0x434
[<c01025fe>] ? cpu_idle+0x73/0x7f
[<c03a4dcd>] ? rest_init+0x61/0x63
=======================
Wolfgang also found out that adding kernel_fpu_begin() and kernel_fpu_end()
around the padlock instructions fix the oops.
Suresh wrote:
These padlock instructions though don't use/touch SSE registers, but it behaves
similar to other SSE instructions. For example, it might cause DNA faults
when cr0.ts is set. While this is a spurious DNA trap, it might cause
oops with the recent fpu code changes.
This is the code sequence that is probably causing this problem:
a) new app is getting exec'd and it is somewhere in between
start_thread() and flush_old_exec() in the load_xyz_binary()
b) At pont "a", task's fpu state (like TS_USEDFPU, used_math() etc) is
cleared.
c) Now we get an interrupt/softirq which starts using these encrypt/decrypt
routines in the network stack. This generates a math fault (as
cr0.ts is '1') which sets TS_USEDFPU and restores the math that is
in the task's xstate.
d) Return to exec code path, which does start_thread() which does
free_thread_xstate() and sets xstate pointer to NULL while
the TS_USEDFPU is still set.
e) At the next context switch from the new exec'd task to another task,
we have a scenarios where TS_USEDFPU is set but xstate pointer is null.
This can cause an oops during unlazy_fpu() in __switch_to()
Now:
1) This should happen with or with out pre-emption. Viro also encountered
similar problem with out CONFIG_PREEMPT.
2) kernel_fpu_begin() and kernel_fpu_end() will fix this problem, because
kernel_fpu_begin() will manually do a clts() and won't run in to the
situation of setting TS_USEDFPU in step "c" above.
3) This was working before the fpu changes, because its a spurious
math fault which doesn't corrupt any fpu/sse registers and the task's
math state was always in an allocated state.
With out the recent lazy fpu allocation changes, while we don't see oops,
there is a possible race still present in older kernels(for example,
while kernel is using kernel_fpu_begin() in some optimized clear/copy
page and an interrupt/softirq happens which uses these padlock
instructions generating DNA fault).
This is the failing scenario that existed even before the lazy fpu allocation
changes:
0. CPU's TS flag is set
1. kernel using FPU in some optimized copy routine and while doing
kernel_fpu_begin() takes an interrupt just before doing clts()
2. Takes an interrupt and ipsec uses padlock instruction. And we
take a DNA fault as TS flag is still set.
3. We handle the DNA fault and set TS_USEDFPU and clear cr0.ts
4. We complete the padlock routine
5. Go back to step-1, which resumes clts() in kernel_fpu_begin(), finishes
the optimized copy routine and does kernel_fpu_end(). At this point,
we have cr0.ts again set to '1' but the task's TS_USEFPU is stilll
set and not cleared.
6. Now kernel resumes its user operation. And at the next context
switch, kernel sees it has do a FP save as TS_USEDFPU is still set
and then will do a unlazy_fpu() in __switch_to(). unlazy_fpu()
will take a DNA fault, as cr0.ts is '1' and now, because we are
in __switch_to(), math_state_restore() will get confused and will
restore the next task's FP state and will save it in prev tasks's FP state.
Remember, in __switch_to() we are already on the stack of the next task
but take a DNA fault for the prev task.
This causes the fpu leakage.
Fix the padlock instruction usage by calling them inside the
context of new routines irq_ts_save/restore(), which clear/restore cr0.ts
manually in the interrupt context. This will not generate spurious DNA
in the context of the interrupt which will fix the oops encountered and
the possible FPU leakage issue.
Reported-and-bisected-by: Wolfgang Walter <wolfgang.walter@stwm.de>
Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2008-08-13 12:02:26 +00:00
|
|
|
* should use them only in the context of irq_ts_save/restore()
|
|
|
|
*/
|
|
|
|
static inline int irq_ts_save(void)
|
|
|
|
{
|
|
|
|
/*
|
2009-06-09 14:40:50 +00:00
|
|
|
* If in process context and not atomic, we can take a spurious DNA fault.
|
|
|
|
* Otherwise, doing clts() in process context requires disabling preemption
|
|
|
|
* or some heavy lifting like kernel_fpu_begin()
|
crypto: padlock - fix VIA PadLock instruction usage with irq_ts_save/restore()
Wolfgang Walter reported this oops on his via C3 using padlock for
AES-encryption:
##################################################################
BUG: unable to handle kernel NULL pointer dereference at 000001f0
IP: [<c01028c5>] __switch_to+0x30/0x117
*pde = 00000000
Oops: 0002 [#1] PREEMPT
Modules linked in:
Pid: 2071, comm: sleep Not tainted (2.6.26 #11)
EIP: 0060:[<c01028c5>] EFLAGS: 00010002 CPU: 0
EIP is at __switch_to+0x30/0x117
EAX: 00000000 EBX: c0493300 ECX: dc48dd00 EDX: c0493300
ESI: dc48dd00 EDI: c0493530 EBP: c04cff8c ESP: c04cff7c
DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068
Process sleep (pid: 2071, ti=c04ce000 task=dc48dd00 task.ti=d2fe6000)
Stack: dc48df30 c0493300 00000000 00000000 d2fe7f44 c03b5b43 c04cffc8 00000046
c0131856 0000005a dc472d3c c0493300 c0493470 d983ae00 00002696 00000000
c0239f54 00000000 c04c4000 c04cffd8 c01025fe c04f3740 00049800 c04cffe0
Call Trace:
[<c03b5b43>] ? schedule+0x285/0x2ff
[<c0131856>] ? pm_qos_requirement+0x3c/0x53
[<c0239f54>] ? acpi_processor_idle+0x0/0x434
[<c01025fe>] ? cpu_idle+0x73/0x7f
[<c03a4dcd>] ? rest_init+0x61/0x63
=======================
Wolfgang also found out that adding kernel_fpu_begin() and kernel_fpu_end()
around the padlock instructions fix the oops.
Suresh wrote:
These padlock instructions though don't use/touch SSE registers, but it behaves
similar to other SSE instructions. For example, it might cause DNA faults
when cr0.ts is set. While this is a spurious DNA trap, it might cause
oops with the recent fpu code changes.
This is the code sequence that is probably causing this problem:
a) new app is getting exec'd and it is somewhere in between
start_thread() and flush_old_exec() in the load_xyz_binary()
b) At pont "a", task's fpu state (like TS_USEDFPU, used_math() etc) is
cleared.
c) Now we get an interrupt/softirq which starts using these encrypt/decrypt
routines in the network stack. This generates a math fault (as
cr0.ts is '1') which sets TS_USEDFPU and restores the math that is
in the task's xstate.
d) Return to exec code path, which does start_thread() which does
free_thread_xstate() and sets xstate pointer to NULL while
the TS_USEDFPU is still set.
e) At the next context switch from the new exec'd task to another task,
we have a scenarios where TS_USEDFPU is set but xstate pointer is null.
This can cause an oops during unlazy_fpu() in __switch_to()
Now:
1) This should happen with or with out pre-emption. Viro also encountered
similar problem with out CONFIG_PREEMPT.
2) kernel_fpu_begin() and kernel_fpu_end() will fix this problem, because
kernel_fpu_begin() will manually do a clts() and won't run in to the
situation of setting TS_USEDFPU in step "c" above.
3) This was working before the fpu changes, because its a spurious
math fault which doesn't corrupt any fpu/sse registers and the task's
math state was always in an allocated state.
With out the recent lazy fpu allocation changes, while we don't see oops,
there is a possible race still present in older kernels(for example,
while kernel is using kernel_fpu_begin() in some optimized clear/copy
page and an interrupt/softirq happens which uses these padlock
instructions generating DNA fault).
This is the failing scenario that existed even before the lazy fpu allocation
changes:
0. CPU's TS flag is set
1. kernel using FPU in some optimized copy routine and while doing
kernel_fpu_begin() takes an interrupt just before doing clts()
2. Takes an interrupt and ipsec uses padlock instruction. And we
take a DNA fault as TS flag is still set.
3. We handle the DNA fault and set TS_USEDFPU and clear cr0.ts
4. We complete the padlock routine
5. Go back to step-1, which resumes clts() in kernel_fpu_begin(), finishes
the optimized copy routine and does kernel_fpu_end(). At this point,
we have cr0.ts again set to '1' but the task's TS_USEFPU is stilll
set and not cleared.
6. Now kernel resumes its user operation. And at the next context
switch, kernel sees it has do a FP save as TS_USEDFPU is still set
and then will do a unlazy_fpu() in __switch_to(). unlazy_fpu()
will take a DNA fault, as cr0.ts is '1' and now, because we are
in __switch_to(), math_state_restore() will get confused and will
restore the next task's FP state and will save it in prev tasks's FP state.
Remember, in __switch_to() we are already on the stack of the next task
but take a DNA fault for the prev task.
This causes the fpu leakage.
Fix the padlock instruction usage by calling them inside the
context of new routines irq_ts_save/restore(), which clear/restore cr0.ts
manually in the interrupt context. This will not generate spurious DNA
in the context of the interrupt which will fix the oops encountered and
the possible FPU leakage issue.
Reported-and-bisected-by: Wolfgang Walter <wolfgang.walter@stwm.de>
Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2008-08-13 12:02:26 +00:00
|
|
|
*/
|
2009-06-09 14:40:50 +00:00
|
|
|
if (!in_atomic())
|
crypto: padlock - fix VIA PadLock instruction usage with irq_ts_save/restore()
Wolfgang Walter reported this oops on his via C3 using padlock for
AES-encryption:
##################################################################
BUG: unable to handle kernel NULL pointer dereference at 000001f0
IP: [<c01028c5>] __switch_to+0x30/0x117
*pde = 00000000
Oops: 0002 [#1] PREEMPT
Modules linked in:
Pid: 2071, comm: sleep Not tainted (2.6.26 #11)
EIP: 0060:[<c01028c5>] EFLAGS: 00010002 CPU: 0
EIP is at __switch_to+0x30/0x117
EAX: 00000000 EBX: c0493300 ECX: dc48dd00 EDX: c0493300
ESI: dc48dd00 EDI: c0493530 EBP: c04cff8c ESP: c04cff7c
DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 0068
Process sleep (pid: 2071, ti=c04ce000 task=dc48dd00 task.ti=d2fe6000)
Stack: dc48df30 c0493300 00000000 00000000 d2fe7f44 c03b5b43 c04cffc8 00000046
c0131856 0000005a dc472d3c c0493300 c0493470 d983ae00 00002696 00000000
c0239f54 00000000 c04c4000 c04cffd8 c01025fe c04f3740 00049800 c04cffe0
Call Trace:
[<c03b5b43>] ? schedule+0x285/0x2ff
[<c0131856>] ? pm_qos_requirement+0x3c/0x53
[<c0239f54>] ? acpi_processor_idle+0x0/0x434
[<c01025fe>] ? cpu_idle+0x73/0x7f
[<c03a4dcd>] ? rest_init+0x61/0x63
=======================
Wolfgang also found out that adding kernel_fpu_begin() and kernel_fpu_end()
around the padlock instructions fix the oops.
Suresh wrote:
These padlock instructions though don't use/touch SSE registers, but it behaves
similar to other SSE instructions. For example, it might cause DNA faults
when cr0.ts is set. While this is a spurious DNA trap, it might cause
oops with the recent fpu code changes.
This is the code sequence that is probably causing this problem:
a) new app is getting exec'd and it is somewhere in between
start_thread() and flush_old_exec() in the load_xyz_binary()
b) At pont "a", task's fpu state (like TS_USEDFPU, used_math() etc) is
cleared.
c) Now we get an interrupt/softirq which starts using these encrypt/decrypt
routines in the network stack. This generates a math fault (as
cr0.ts is '1') which sets TS_USEDFPU and restores the math that is
in the task's xstate.
d) Return to exec code path, which does start_thread() which does
free_thread_xstate() and sets xstate pointer to NULL while
the TS_USEDFPU is still set.
e) At the next context switch from the new exec'd task to another task,
we have a scenarios where TS_USEDFPU is set but xstate pointer is null.
This can cause an oops during unlazy_fpu() in __switch_to()
Now:
1) This should happen with or with out pre-emption. Viro also encountered
similar problem with out CONFIG_PREEMPT.
2) kernel_fpu_begin() and kernel_fpu_end() will fix this problem, because
kernel_fpu_begin() will manually do a clts() and won't run in to the
situation of setting TS_USEDFPU in step "c" above.
3) This was working before the fpu changes, because its a spurious
math fault which doesn't corrupt any fpu/sse registers and the task's
math state was always in an allocated state.
With out the recent lazy fpu allocation changes, while we don't see oops,
there is a possible race still present in older kernels(for example,
while kernel is using kernel_fpu_begin() in some optimized clear/copy
page and an interrupt/softirq happens which uses these padlock
instructions generating DNA fault).
This is the failing scenario that existed even before the lazy fpu allocation
changes:
0. CPU's TS flag is set
1. kernel using FPU in some optimized copy routine and while doing
kernel_fpu_begin() takes an interrupt just before doing clts()
2. Takes an interrupt and ipsec uses padlock instruction. And we
take a DNA fault as TS flag is still set.
3. We handle the DNA fault and set TS_USEDFPU and clear cr0.ts
4. We complete the padlock routine
5. Go back to step-1, which resumes clts() in kernel_fpu_begin(), finishes
the optimized copy routine and does kernel_fpu_end(). At this point,
we have cr0.ts again set to '1' but the task's TS_USEFPU is stilll
set and not cleared.
6. Now kernel resumes its user operation. And at the next context
switch, kernel sees it has do a FP save as TS_USEDFPU is still set
and then will do a unlazy_fpu() in __switch_to(). unlazy_fpu()
will take a DNA fault, as cr0.ts is '1' and now, because we are
in __switch_to(), math_state_restore() will get confused and will
restore the next task's FP state and will save it in prev tasks's FP state.
Remember, in __switch_to() we are already on the stack of the next task
but take a DNA fault for the prev task.
This causes the fpu leakage.
Fix the padlock instruction usage by calling them inside the
context of new routines irq_ts_save/restore(), which clear/restore cr0.ts
manually in the interrupt context. This will not generate spurious DNA
in the context of the interrupt which will fix the oops encountered and
the possible FPU leakage issue.
Reported-and-bisected-by: Wolfgang Walter <wolfgang.walter@stwm.de>
Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2008-08-13 12:02:26 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (read_cr0() & X86_CR0_TS) {
|
|
|
|
clts();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void irq_ts_restore(int TS_state)
|
|
|
|
{
|
|
|
|
if (TS_state)
|
|
|
|
stts();
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:31:51 +00:00
|
|
|
/*
|
|
|
|
* These disable preemption on their own and are safe
|
|
|
|
*/
|
|
|
|
static inline void save_init_fpu(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
__save_init_fpu(tsk);
|
|
|
|
stts();
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void unlazy_fpu(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
__unlazy_fpu(tsk);
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void clear_fpu(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
__clear_fpu(tsk);
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* i387 state interaction
|
|
|
|
*/
|
|
|
|
static inline unsigned short get_fpu_cwd(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (cpu_has_fxsr) {
|
2010-05-06 08:45:46 +00:00
|
|
|
return tsk->thread.fpu.state->fxsave.cwd;
|
2008-01-30 12:31:51 +00:00
|
|
|
} else {
|
2010-05-06 08:45:46 +00:00
|
|
|
return (unsigned short)tsk->thread.fpu.state->fsave.cwd;
|
2008-01-30 12:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned short get_fpu_swd(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (cpu_has_fxsr) {
|
2010-05-06 08:45:46 +00:00
|
|
|
return tsk->thread.fpu.state->fxsave.swd;
|
2008-01-30 12:31:51 +00:00
|
|
|
} else {
|
2010-05-06 08:45:46 +00:00
|
|
|
return (unsigned short)tsk->thread.fpu.state->fsave.swd;
|
2008-01-30 12:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned short get_fpu_mxcsr(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (cpu_has_xmm) {
|
2010-05-06 08:45:46 +00:00
|
|
|
return tsk->thread.fpu.state->fxsave.mxcsr;
|
2008-01-30 12:31:51 +00:00
|
|
|
} else {
|
|
|
|
return MXCSR_DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:45:46 +00:00
|
|
|
static bool fpu_allocated(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
return fpu->state != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int fpu_alloc(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
if (fpu_allocated(fpu))
|
|
|
|
return 0;
|
|
|
|
fpu->state = kmem_cache_alloc(task_xstate_cachep, GFP_KERNEL);
|
|
|
|
if (!fpu->state)
|
|
|
|
return -ENOMEM;
|
|
|
|
WARN_ON((unsigned long)fpu->state & 15);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fpu_free(struct fpu *fpu)
|
|
|
|
{
|
|
|
|
if (fpu->state) {
|
|
|
|
kmem_cache_free(task_xstate_cachep, fpu->state);
|
|
|
|
fpu->state = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void fpu_copy(struct fpu *dst, struct fpu *src)
|
|
|
|
{
|
|
|
|
memcpy(dst->state, src->state, xstate_size);
|
|
|
|
}
|
|
|
|
|
2010-05-17 09:22:23 +00:00
|
|
|
extern void fpu_finit(struct fpu *fpu);
|
|
|
|
|
2009-11-03 14:11:15 +00:00
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
|
2008-10-23 05:26:29 +00:00
|
|
|
#endif /* _ASM_X86_I387_H */
|