blob: d1b5f3a2fa20b4f70aa6a1d0742b533d6081497c [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
8
Suresh Siddhadc1e35c2008-07-29 10:29:19 -07009#define XSTATE_FP 0x1
10#define XSTATE_SSE 0x2
Suresh Siddhaa30469e2009-04-10 15:21:24 -070011#define XSTATE_YMM 0x4
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070012
13#define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE)
14
15#define FXSAVE_SIZE 512
16
17/*
18 * These are the features that the OS can handle currently.
19 */
Suresh Siddhaa30469e2009-04-10 15:21:24 -070020#define XCNTXT_MASK (XSTATE_FP | XSTATE_SSE | XSTATE_YMM)
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070021
Suresh Siddhab359e8a2008-07-29 10:29:20 -070022#ifdef CONFIG_X86_64
23#define REX_PREFIX "0x48, "
24#else
25#define REX_PREFIX
26#endif
27
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070028extern unsigned int xstate_size;
29extern u64 pcntxt_mask;
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070030extern struct xsave_struct *init_xstate_buf;
Suresh Siddha5b3efd52010-02-11 11:50:59 -080031extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070032
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070033extern void xsave_init(void);
Suresh Siddha5b3efd52010-02-11 11:50:59 -080034extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
Suresh Siddhab359e8a2008-07-29 10:29:20 -070035extern int init_fpu(struct task_struct *child);
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070036extern int check_for_xstate(struct i387_fxsave_struct __user *buf,
37 void __user *fpstate,
38 struct _fpx_sw_bytes *sw);
Suresh Siddhadc1e35c2008-07-29 10:29:19 -070039
Avi Kivity86603282010-05-06 11:45:46 +030040static inline int fpu_xrstor_checking(struct fpu *fpu)
Suresh Siddhab359e8a2008-07-29 10:29:20 -070041{
Avi Kivity86603282010-05-06 11:45:46 +030042 struct xsave_struct *fx = &fpu->state->xsave;
Suresh Siddhab359e8a2008-07-29 10:29:20 -070043 int err;
44
45 asm volatile("1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n\t"
46 "2:\n"
47 ".section .fixup,\"ax\"\n"
48 "3: movl $-1,%[err]\n"
49 " jmp 2b\n"
50 ".previous\n"
51 _ASM_EXTABLE(1b, 3b)
52 : [err] "=r" (err)
53 : "D" (fx), "m" (*fx), "a" (-1), "d" (-1), "0" (0)
54 : "memory");
55
56 return err;
57}
58
Suresh Siddhac37b5ef2008-07-29 10:29:25 -070059static inline int xsave_user(struct xsave_struct __user *buf)
Suresh Siddha9dc89c02008-07-29 10:29:23 -070060{
61 int err;
62 __asm__ __volatile__("1: .byte " REX_PREFIX "0x0f,0xae,0x27\n"
63 "2:\n"
64 ".section .fixup,\"ax\"\n"
65 "3: movl $-1,%[err]\n"
66 " jmp 2b\n"
67 ".previous\n"
68 ".section __ex_table,\"a\"\n"
69 _ASM_ALIGN "\n"
70 _ASM_PTR "1b,3b\n"
71 ".previous"
72 : [err] "=r" (err)
73 : "D" (buf), "a" (-1), "d" (-1), "0" (0)
74 : "memory");
75 if (unlikely(err) && __clear_user(buf, xstate_size))
76 err = -EFAULT;
77 /* No need to clear here because the caller clears USED_MATH */
78 return err;
79}
80
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070081static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask)
Suresh Siddha9dc89c02008-07-29 10:29:23 -070082{
83 int err;
84 struct xsave_struct *xstate = ((__force struct xsave_struct *)buf);
H. Peter Anvin6152e4b2008-07-29 17:23:16 -070085 u32 lmask = mask;
86 u32 hmask = mask >> 32;
Suresh Siddha9dc89c02008-07-29 10:29:23 -070087
88 __asm__ __volatile__("1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n"
89 "2:\n"
90 ".section .fixup,\"ax\"\n"
91 "3: movl $-1,%[err]\n"
92 " jmp 2b\n"
93 ".previous\n"
94 ".section __ex_table,\"a\"\n"
95 _ASM_ALIGN "\n"
96 _ASM_PTR "1b,3b\n"
97 ".previous"
98 : [err] "=r" (err)
99 : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0)
100 : "memory"); /* memory required? */
101 return err;
102}
103
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700104static inline void xrstor_state(struct xsave_struct *fx, u64 mask)
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700105{
H. Peter Anvin6152e4b2008-07-29 17:23:16 -0700106 u32 lmask = mask;
107 u32 hmask = mask >> 32;
108
Suresh Siddha9dc89c02008-07-29 10:29:23 -0700109 asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x2f\n\t"
110 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
111 : "memory");
112}
113
Suresh Siddha29104e12010-07-19 16:05:49 -0700114static inline void xsave_state(struct xsave_struct *fx, u64 mask)
115{
116 u32 lmask = mask;
117 u32 hmask = mask >> 32;
118
119 asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x27\n\t"
120 : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
121 : "memory");
122}
123
Avi Kivity86603282010-05-06 11:45:46 +0300124static inline void fpu_xsave(struct fpu *fpu)
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700125{
126 /* This, however, we can work around by forcing the compiler to select
127 an addressing mode that doesn't require extended registers. */
Suresh Siddha6bad06b2010-07-19 16:05:52 -0700128 alternative_input(
129 ".byte " REX_PREFIX "0x0f,0xae,0x27",
130 ".byte " REX_PREFIX "0x0f,0xae,0x37",
131 X86_FEATURE_XSAVEOPT,
132 [fx] "D" (&fpu->state->xsave), "a" (-1), "d" (-1) :
133 "memory");
Suresh Siddhab359e8a2008-07-29 10:29:20 -0700134}
Suresh Siddhadc1e35c2008-07-29 10:29:19 -0700135#endif