Skip to content

Commit dc1e35c

Browse files
Suresh SiddhaIngo Molnar
authored andcommitted
x86, xsave: enable xsave/xrstor on cpus with xsave support
Enables xsave/xrstor by turning on cr4.osxsave on cpu's which have the xsave support. For now, features that OS supports/enabled are FP and SSE. Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com> Signed-off-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent a648bf4 commit dc1e35c

10 files changed

Lines changed: 148 additions & 6 deletions

File tree

‎arch/x86/kernel/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ obj-y += tsc.o io_delay.o rtc.o
3838

3939
obj-$(CONFIG_X86_TRAMPOLINE) += trampoline.o
4040
obj-y += process.o
41-
obj-y += i387.o
41+
obj-y += i387.o xsave.o
4242
obj-y += ptrace.o
4343
obj-y += ds.o
4444
obj-$(CONFIG_X86_32) += tls.o

‎arch/x86/kernel/cpu/common.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,14 @@ void __cpuinit cpu_init(void)
712712
current_thread_info()->status = 0;
713713
clear_used_math();
714714
mxcsr_feature_mask_init();
715+
716+
/*
717+
* Boot processor to setup the FP and extended state context info.
718+
*/
719+
if (!smp_processor_id())
720+
init_thread_xstate();
721+
722+
xsave_init();
715723
}
716724

717725
#ifdef CONFIG_HOTPLUG_CPU

‎arch/x86/kernel/i387.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ void __init init_thread_xstate(void)
6161
return;
6262
}
6363

64+
if (cpu_has_xsave) {
65+
xsave_cntxt_init();
66+
return;
67+
}
68+
6469
if (cpu_has_fxsr)
6570
xstate_size = sizeof(struct i387_fxsave_struct);
6671
#ifdef CONFIG_X86_32
@@ -83,6 +88,13 @@ void __cpuinit fpu_init(void)
8388

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

91+
/*
92+
* Boot processor to setup the FP and extended state context info.
93+
*/
94+
if (!smp_processor_id())
95+
init_thread_xstate();
96+
xsave_init();
97+
8698
mxcsr_feature_mask_init();
8799
/* clean state in init */
88100
current_thread_info()->status = 0;

‎arch/x86/kernel/traps_32.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,6 @@ void __init trap_init(void)
12281228

12291229
set_bit(SYSCALL_VECTOR, used_vectors);
12301230

1231-
init_thread_xstate();
12321231
/*
12331232
* Should be a barrier for any external CPU state:
12341233
*/

‎arch/x86/kernel/traps_64.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,6 @@ void __init trap_init(void)
11721172
#ifdef CONFIG_IA32_EMULATION
11731173
set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
11741174
#endif
1175-
/*
1176-
* initialize the per thread extended state:
1177-
*/
1178-
init_thread_xstate();
11791175
/*
11801176
* Should be a barrier for any external CPU state:
11811177
*/

‎arch/x86/kernel/xsave.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* xsave/xrstor support.
3+
*
4+
* Author: Suresh Siddha <suresh.b.siddha@intel.com>
5+
*/
6+
#include <linux/bootmem.h>
7+
#include <linux/compat.h>
8+
#include <asm/i387.h>
9+
10+
/*
11+
* Supported feature mask by the CPU and the kernel.
12+
*/
13+
unsigned int pcntxt_hmask, pcntxt_lmask;
14+
15+
/*
16+
* Represents init state for the supported extended state.
17+
*/
18+
struct xsave_struct *init_xstate_buf;
19+
20+
/*
21+
* Enable the extended processor state save/restore feature
22+
*/
23+
void __cpuinit xsave_init(void)
24+
{
25+
if (!cpu_has_xsave)
26+
return;
27+
28+
set_in_cr4(X86_CR4_OSXSAVE);
29+
30+
/*
31+
* Enable all the features that the HW is capable of
32+
* and the Linux kernel is aware of.
33+
*
34+
* xsetbv();
35+
*/
36+
asm volatile(".byte 0x0f,0x01,0xd1" : : "c" (0),
37+
"a" (pcntxt_lmask), "d" (pcntxt_hmask));
38+
}
39+
40+
/*
41+
* setup the xstate image representing the init state
42+
*/
43+
void setup_xstate_init(void)
44+
{
45+
init_xstate_buf = alloc_bootmem(xstate_size);
46+
init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
47+
}
48+
49+
/*
50+
* Enable and initialize the xsave feature.
51+
*/
52+
void __init xsave_cntxt_init(void)
53+
{
54+
unsigned int eax, ebx, ecx, edx;
55+
56+
cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
57+
58+
pcntxt_lmask = eax;
59+
pcntxt_hmask = edx;
60+
61+
if ((pcntxt_lmask & XSTATE_FPSSE) != XSTATE_FPSSE) {
62+
printk(KERN_ERR "FP/SSE not shown under xsave features %x\n",
63+
pcntxt_lmask);
64+
BUG();
65+
}
66+
67+
/*
68+
* for now OS knows only about FP/SSE
69+
*/
70+
pcntxt_lmask = pcntxt_lmask & XCNTXT_LMASK;
71+
pcntxt_hmask = pcntxt_hmask & XCNTXT_HMASK;
72+
73+
xsave_init();
74+
75+
/*
76+
* Recompute the context size for enabled features
77+
*/
78+
cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
79+
80+
xstate_size = ebx;
81+
82+
setup_xstate_init();
83+
84+
printk(KERN_INFO "xsave/xrstor: enabled xstate_bv 0x%Lx, "
85+
"cntxt size 0x%x\n",
86+
(pcntxt_lmask | ((u64) pcntxt_hmask << 32)), xstate_size);
87+
}

‎include/asm-x86/i387.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <asm/sigcontext.h>
1919
#include <asm/user.h>
2020
#include <asm/uaccess.h>
21+
#include <asm/xsave.h>
2122

2223
extern void fpu_init(void);
2324
extern void mxcsr_feature_mask_init(void);

‎include/asm-x86/processor-flags.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#define X86_CR4_OSFXSR 0x00000200 /* enable fast FPU save and restore */
6060
#define X86_CR4_OSXMMEXCPT 0x00000400 /* enable unmasked SSE exceptions */
6161
#define X86_CR4_VMXE 0x00002000 /* enable VMX virtualization */
62+
#define X86_CR4_OSXSAVE 0x00040000 /* enable xsave and xrestore */
6263

6364
/*
6465
* x86-64 Task Priority Register, CR8

‎include/asm-x86/processor.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,18 @@ struct i387_soft_struct {
346346
u32 entry_eip;
347347
};
348348

349+
struct xsave_hdr_struct {
350+
u64 xstate_bv;
351+
u64 reserved1[2];
352+
u64 reserved2[5];
353+
} __attribute__((packed));
354+
355+
struct xsave_struct {
356+
struct i387_fxsave_struct i387;
357+
struct xsave_hdr_struct xsave_hdr;
358+
/* new processor state extensions will go here */
359+
} __attribute__ ((packed, aligned (64)));
360+
349361
union thread_xstate {
350362
struct i387_fsave_struct fsave;
351363
struct i387_fxsave_struct fxsave;

‎include/asm-x86/xsave.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef __ASM_X86_XSAVE_H
2+
#define __ASM_X86_XSAVE_H
3+
4+
#include <asm/processor.h>
5+
#include <asm/i387.h>
6+
7+
#define XSTATE_FP 0x1
8+
#define XSTATE_SSE 0x2
9+
10+
#define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)
11+
12+
#define FXSAVE_SIZE 512
13+
14+
/*
15+
* These are the features that the OS can handle currently.
16+
*/
17+
#define XCNTXT_LMASK (XSTATE_FP | XSTATE_SSE)
18+
#define XCNTXT_HMASK 0x0
19+
20+
extern unsigned int xstate_size, pcntxt_hmask, pcntxt_lmask;
21+
extern struct xsave_struct *init_xstate_buf;
22+
23+
extern void xsave_cntxt_init(void);
24+
extern void xsave_init(void);
25+
26+
#endif

0 commit comments

Comments
 (0)