Skip to content

Commit

Permalink
Merge branch 'x86-xsave-for-linus' of git://git.kernel.org/pub/scm/li…
Browse files Browse the repository at this point in the history
…nux/kernel/git/tip/linux-2.6-tip

* 'x86-xsave-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip:
  x86, xsave: Make xstate_enable_boot_cpu() __init, protect on CPU 0
  x86, xsave: Add __init attribute to setup_xstate_features()
  x86, xsave: Make init_xstate_buf static
  x86, xsave: Check cpuid level for XSTATE_CPUID (0x0d)
  x86, xsave: Introduce xstate enable functions
  x86, xsave: Separate fpu and xsave initialization
  x86, xsave: Move boot cpu initialization to xsave_init()
  x86, xsave: 32/64 bit boot cpu check unification in initialization
  x86, xsave: Do not include asm/i387.h in asm/xsave.h
  x86, xsave: Use xsaveopt in context-switch path when supported
  x86, xsave: Sync xsave memory layout with its header for user handling
  x86, xsave: Track the offset, size of state in the xsave layout
  • Loading branch information
Linus Torvalds committed Aug 6, 2010
2 parents e877977 + 1cff92d commit 4a386c3
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 42 deletions.
15 changes: 14 additions & 1 deletion arch/x86/include/asm/i387.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extern void mxcsr_feature_mask_init(void);
extern int init_fpu(struct task_struct *child);
extern asmlinkage void math_state_restore(void);
extern void __math_state_restore(void);
extern void init_thread_xstate(void);
extern int dump_fpu(struct pt_regs *, struct user_i387_struct *);

extern user_regset_active_fn fpregs_active, xfpregs_active;
Expand All @@ -58,11 +57,25 @@ extern int restore_i387_xstate_ia32(void __user *buf);

#define X87_FSW_ES (1 << 7) /* Exception Summary */

static __always_inline __pure bool use_xsaveopt(void)
{
return static_cpu_has(X86_FEATURE_XSAVEOPT);
}

static __always_inline __pure bool use_xsave(void)
{
return static_cpu_has(X86_FEATURE_XSAVE);
}

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);
}

#ifdef CONFIG_X86_64

/* Ignore delayed exceptions from user space */
Expand Down
24 changes: 18 additions & 6 deletions arch/x86/include/asm/xsave.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include <linux/types.h>
#include <asm/processor.h>
#include <asm/i387.h>

#define XSTATE_CPUID 0x0000000d

#define XSTATE_FP 0x1
#define XSTATE_SSE 0x2
Expand Down Expand Up @@ -32,10 +33,8 @@

extern unsigned int xstate_size;
extern u64 pcntxt_mask;
extern struct xsave_struct *init_xstate_buf;
extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];

extern void xsave_cntxt_init(void);
extern void xsave_init(void);
extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
extern int init_fpu(struct task_struct *child);
Expand Down Expand Up @@ -127,12 +126,25 @@ static inline void xrstor_state(struct xsave_struct *fx, u64 mask)
: "memory");
}

static inline void xsave_state(struct xsave_struct *fx, u64 mask)
{
u32 lmask = mask;
u32 hmask = mask >> 32;

asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x27\n\t"
: : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
: "memory");
}

static inline void fpu_xsave(struct fpu *fpu)
{
/* This, however, we can work around by forcing the compiler to select
an addressing mode that doesn't require extended registers. */
__asm__ __volatile__(".byte " REX_PREFIX "0x0f,0xae,0x27"
: : "D" (&(fpu->state->xsave)),
"a" (-1), "d"(-1) : "memory");
alternative_input(
".byte " REX_PREFIX "0x0f,0xae,0x27",
".byte " REX_PREFIX "0x0f,0xae,0x37",
X86_FEATURE_XSAVEOPT,
[fx] "D" (&fpu->state->xsave), "a" (-1), "d" (-1) :
"memory");
}
#endif
16 changes: 10 additions & 6 deletions arch/x86/kernel/cpu/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,18 @@ EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
static int __init x86_xsave_setup(char *s)
{
setup_clear_cpu_cap(X86_FEATURE_XSAVE);
setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
return 1;
}
__setup("noxsave", x86_xsave_setup);

static int __init x86_xsaveopt_setup(char *s)
{
setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
return 1;
}
__setup("noxsaveopt", x86_xsaveopt_setup);

#ifdef CONFIG_X86_32
static int cachesize_override __cpuinitdata = -1;
static int disable_x86_serial_nr __cpuinitdata = 1;
Expand Down Expand Up @@ -1202,6 +1210,7 @@ void __cpuinit cpu_init(void)
dbg_restore_debug_regs();

fpu_init();
xsave_init();

raw_local_save_flags(kernel_eflags);

Expand Down Expand Up @@ -1262,12 +1271,7 @@ void __cpuinit cpu_init(void)
clear_used_math();
mxcsr_feature_mask_init();

/*
* Boot processor to setup the FP and extended state context info.
*/
if (smp_processor_id() == boot_cpu_id)
init_thread_xstate();

fpu_init();
xsave_init();
}
#endif
39 changes: 28 additions & 11 deletions arch/x86/kernel/i387.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ void __cpuinit mxcsr_feature_mask_init(void)
stts();
}

void __cpuinit init_thread_xstate(void)
static void __cpuinit init_thread_xstate(void)
{
/*
* Note that xstate_size might be overwriten later during
* xsave_init().
*/

if (!HAVE_HWFP) {
xstate_size = sizeof(struct i387_soft_struct);
return;
}

if (cpu_has_xsave) {
xsave_cntxt_init();
return;
}

if (cpu_has_fxsr)
xstate_size = sizeof(struct i387_fxsave_struct);
#ifdef CONFIG_X86_32
Expand All @@ -84,6 +84,7 @@ void __cpuinit init_thread_xstate(void)
* Called at bootup to set up the initial FPU state that is later cloned
* into all processes.
*/

void __cpuinit fpu_init(void)
{
unsigned long oldcr0 = read_cr0();
Expand All @@ -93,19 +94,24 @@ void __cpuinit fpu_init(void)

write_cr0(oldcr0 & ~(X86_CR0_TS|X86_CR0_EM)); /* clear TS and EM */

/*
* Boot processor to setup the FP and extended state context info.
*/
if (!smp_processor_id())
init_thread_xstate();
xsave_init();

mxcsr_feature_mask_init();
/* clean state in init */
current_thread_info()->status = 0;
clear_used_math();
}
#endif /* CONFIG_X86_64 */

#else /* CONFIG_X86_64 */

void __cpuinit fpu_init(void)
{
if (!smp_processor_id())
init_thread_xstate();
}

#endif /* CONFIG_X86_32 */

void fpu_finit(struct fpu *fpu)
{
Expand Down Expand Up @@ -191,6 +197,8 @@ int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
if (ret)
return ret;

sanitize_i387_state(target);

return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
&target->thread.fpu.state->fxsave, 0, -1);
}
Expand All @@ -208,6 +216,8 @@ int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
if (ret)
return ret;

sanitize_i387_state(target);

ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
&target->thread.fpu.state->fxsave, 0, -1);

Expand Down Expand Up @@ -447,6 +457,8 @@ int fpregs_get(struct task_struct *target, const struct user_regset *regset,
-1);
}

sanitize_i387_state(target);

if (kbuf && pos == 0 && count == sizeof(env)) {
convert_from_fxsr(kbuf, target);
return 0;
Expand All @@ -468,6 +480,8 @@ int fpregs_set(struct task_struct *target, const struct user_regset *regset,
if (ret)
return ret;

sanitize_i387_state(target);

if (!HAVE_HWFP)
return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);

Expand Down Expand Up @@ -534,6 +548,9 @@ static int save_i387_xsave(void __user *buf)
struct _fpstate_ia32 __user *fx = buf;
int err = 0;


sanitize_i387_state(tsk);

/*
* For legacy compatible, we always set FP/SSE bits in the bit
* vector while saving the state to the user context.
Expand Down
Loading

0 comments on commit 4a386c3

Please sign in to comment.