blob: 6c1d7411eb009a5a96ef55004fdedf60aa1ef822 [file] [log] [blame]
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07001#ifndef __ASM_X86_XSAVE_H
2#define __ASM_X86_XSAVE_H
3
H. Peter Anvin6152e4b2008-07-29 17:23:16 -07004#include <linux/types.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07005#include <asm/processor.h>
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07006
Robert Richteree813d52010-07-21 19:03:54 +02007#define XSTATE_CPUID 0x0000000d
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07008
Fenghua Yuc2bc11f2014-02-20 13:24:51 -08009#define XSTATE_FP 0x1
10#define XSTATE_SSE 0x2
11#define XSTATE_YMM 0x4
12#define XSTATE_BNDREGS 0x8
13#define XSTATE_BNDCSR 0x10
14#define XSTATE_OPMASK 0x20
15#define XSTATE_ZMM_Hi256 0x40
16#define XSTATE_Hi16_ZMM 0x80
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070017
18#define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)
19
20#define FXSAVE_SIZE 512
21
Sheng Yang2d5b5a62010-06-13 17:29:39 +080022#define XSAVE_HDR_SIZE 64
23#define XSAVE_HDR_OFFSET FXSAVE_SIZE
24
25#define XSAVE_YMM_SIZE 256
26#define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
Sheng Yang5ee481d2010-05-17 17:22:23 +080027
Qiaowei Rene7d820a2013-12-05 17:15:34 +080028/* Supported features which support lazy state saving */
Fenghua Yuc2bc11f2014-02-20 13:24:51 -080029#define XSTATE_LAZY (XSTATE_FP | XSTATE_SSE | XSTATE_YMM \
30 | XSTATE_OPMASK | XSTATE_ZMM_Hi256 | XSTATE_Hi16_ZMM)
Qiaowei Rene7d820a2013-12-05 17:15:34 +080031
32/* Supported features which require eager state saving */
33#define XSTATE_EAGER (XSTATE_BNDREGS | XSTATE_BNDCSR)
34
35/* All currently supported features */
36#define XCNTXT_MASK (XSTATE_LAZY | XSTATE_EAGER)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070037
Suresh Siddhab359e8a2008-07-29 10:29:20 -070038#ifdef CONFIG_X86_64
39#define REX_PREFIX "0x48, "
40#else
41#define REX_PREFIX
42#endif
43
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070044extern unsigned int xstate_size;
45extern u64 pcntxt_mask;
Suresh Siddha5b3efd52010-02-11 11:50:59 -080046extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
Suresh Siddha304bced2012-08-24 14:13:02 -070047extern struct xsave_struct *init_xstate_buf;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070048
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070049extern void xsave_init(void);
Suresh Siddha5b3efd52010-02-11 11:50:59 -080050extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
Suresh Siddhab359e8a2008-07-29 10:29:20 -070051extern int init_fpu(struct task_struct *child);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070052
Suresh Siddha0ca5bd02012-07-24 16:05:28 -070053static inline int fpu_xrstor_checking(struct xsave_struct *fx)
Suresh Siddhab359e8a2008-07-29 10:29:20 -070054{
55 int err;
56
57 asm volatile("1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n\t"
58 "2:\n"
59 ".section .fixup,\"ax\"\n"
60 "3: movl $-1,%[err]\n"
61 " jmp 2b\n"
62 ".previous\n"
63 _ASM_EXTABLE(1b, 3b)
64 : [err] "=r" (err)
65 : "D" (fx), "m" (*fx), "a" (-1), "d" (-1), "0" (0)
66 : "memory");
67
68 return err;
69}
70
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070071static inline int xsave_user(struct xsave_struct __user *buf)
Suresh Siddha9dc89c02008-07-29 10:29:23 -070072{
73 int err;
Suresh Siddha8e221b62010-06-22 16:23:37 -070074
75 /*
76 * Clear the xsave header first, so that reserved fields are
77 * initialized to zero.
78 */
Suresh Siddha72a671c2012-07-24 16:05:29 -070079 err = __clear_user(&buf->xsave_hdr, sizeof(buf->xsave_hdr));
Suresh Siddha8e221b62010-06-22 16:23:37 -070080 if (unlikely(err))
81 return -EFAULT;
82
H. Peter Anvin63bcff22012-09-21 12:43:12 -070083 __asm__ __volatile__(ASM_STAC "\n"
84 "1: .byte " REX_PREFIX "0x0f,0xae,0x27\n"
85 "2: " ASM_CLAC "\n"
Suresh Siddha9dc89c02008-07-29 10:29:23 -070086 ".section .fixup,\"ax\"\n"
87 "3: movl $-1,%[err]\n"
88 " jmp 2b\n"
89 ".previous\n"
H. Peter Anvin7a040a42012-04-20 13:42:25 -070090 _ASM_EXTABLE(1b,3b)
Suresh Siddha9dc89c02008-07-29 10:29:23 -070091 : [err] "=r" (err)
92 : "D" (buf), "a" (-1), "d" (-1), "0" (0)
93 : "memory");
Suresh Siddha9dc89c02008-07-29 10:29:23 -070094 return err;
95}
96
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070097static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
Suresh Siddha9dc89c02008-07-29 10:29:23 -070098{
99 int err;
100 struct xsave_struct *xstate = ((__force struct xsave_struct *)buf);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700101 u32 lmask = mask;
102 u32 hmask = mask >> 32;
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700103
H. Peter Anvin63bcff22012-09-21 12:43:12 -0700104 __asm__ __volatile__(ASM_STAC "\n"
105 "1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n"
106 "2: " ASM_CLAC "\n"
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700107 ".section .fixup,\"ax\"\n"
108 "3: movl $-1,%[err]\n"
109 " jmp 2b\n"
110 ".previous\n"
H. Peter Anvin7a040a42012-04-20 13:42:25 -0700111 _ASM_EXTABLE(1b,3b)
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700112 : [err] "=r" (err)
113 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
114 : "memory"); /* memory required? */
115 return err;
116}
117
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700118static inline void xrstor_state(struct xsave_struct *fx, u64 mask)
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700119{
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700120 u32 lmask = mask;
121 u32 hmask = mask >> 32;
122
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700123 asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x2f\n\t"
124 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
125 : "memory");
126}
127
Suresh Siddha29104e12010-07-19 16:05:49 -0700128static inline void xsave_state(struct xsave_struct *fx, u64 mask)
129{
130 u32 lmask = mask;
131 u32 hmask = mask >> 32;
132
133 asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x27\n\t"
134 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
135 : "memory");
136}
137
Avi Kivity86603282010-05-06 11:45:46 +0300138static inline void fpu_xsave(struct fpu *fpu)
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700139{
140 /* This, however, we can work around by forcing the compiler to select
141 an addressing mode that doesn't require extended registers. */
Suresh Siddha6bad06b2010-07-19 16:05:52 -0700142 alternative_input(
143 ".byte " REX_PREFIX "0x0f,0xae,0x27",
144 ".byte " REX_PREFIX "0x0f,0xae,0x37",
145 X86_FEATURE_XSAVEOPT,
146 [fx] "D" (&fpu->state->xsave), "a" (-1), "d" (-1) :
147 "memory");
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700148}
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700149#endif