Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 1 | #ifndef __ASM_X86_XSAVE_H |
| 2 | #define __ASM_X86_XSAVE_H |
| 3 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 4 | #include <linux/types.h> |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 5 | #include <asm/processor.h> |
| 6 | #include <asm/i387.h> |
| 7 | |
| 8 | #define XSTATE_FP 0x1 |
| 9 | #define XSTATE_SSE 0x2 |
Suresh Siddha | a30469e | 2009-04-10 15:21:24 -0700 | [diff] [blame] | 10 | #define XSTATE_YMM 0x4 |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 11 | |
| 12 | #define XSTATE_FPSSE (XSTATE_FP | XSTATE_SSE) |
| 13 | |
| 14 | #define FXSAVE_SIZE 512 |
| 15 | |
Sheng Yang | 2d5b5a6 | 2010-06-13 17:29:39 +0800 | [diff] [blame^] | 16 | #define XSAVE_HDR_SIZE 64 |
| 17 | #define XSAVE_HDR_OFFSET FXSAVE_SIZE |
| 18 | |
| 19 | #define XSAVE_YMM_SIZE 256 |
| 20 | #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET) |
Sheng Yang | 5ee481d | 2010-05-17 17:22:23 +0800 | [diff] [blame] | 21 | |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 22 | /* |
| 23 | * These are the features that the OS can handle currently. |
| 24 | */ |
Suresh Siddha | a30469e | 2009-04-10 15:21:24 -0700 | [diff] [blame] | 25 | #define XCNTXT_MASK (XSTATE_FP | XSTATE_SSE | XSTATE_YMM) |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 26 | |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 27 | #ifdef CONFIG_X86_64 |
| 28 | #define REX_PREFIX "0x48, " |
| 29 | #else |
| 30 | #define REX_PREFIX |
| 31 | #endif |
| 32 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 33 | extern unsigned int xstate_size; |
| 34 | extern u64 pcntxt_mask; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 35 | extern struct xsave_struct *init_xstate_buf; |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 36 | extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS]; |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 37 | |
| 38 | extern void xsave_cntxt_init(void); |
| 39 | extern void xsave_init(void); |
Suresh Siddha | 5b3efd5 | 2010-02-11 11:50:59 -0800 | [diff] [blame] | 40 | extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask); |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 41 | extern int init_fpu(struct task_struct *child); |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 42 | extern int check_for_xstate(struct i387_fxsave_struct __user *buf, |
| 43 | void __user *fpstate, |
| 44 | struct _fpx_sw_bytes *sw); |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 45 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 46 | static inline int fpu_xrstor_checking(struct fpu *fpu) |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 47 | { |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 48 | struct xsave_struct *fx = &fpu->state->xsave; |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 49 | int err; |
| 50 | |
| 51 | asm volatile("1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n\t" |
| 52 | "2:\n" |
| 53 | ".section .fixup,\"ax\"\n" |
| 54 | "3: movl $-1,%[err]\n" |
| 55 | " jmp 2b\n" |
| 56 | ".previous\n" |
| 57 | _ASM_EXTABLE(1b, 3b) |
| 58 | : [err] "=r" (err) |
| 59 | : "D" (fx), "m" (*fx), "a" (-1), "d" (-1), "0" (0) |
| 60 | : "memory"); |
| 61 | |
| 62 | return err; |
| 63 | } |
| 64 | |
Suresh Siddha | c37b5ef | 2008-07-29 10:29:25 -0700 | [diff] [blame] | 65 | static inline int xsave_user(struct xsave_struct __user *buf) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 66 | { |
| 67 | int err; |
| 68 | __asm__ __volatile__("1: .byte " REX_PREFIX "0x0f,0xae,0x27\n" |
| 69 | "2:\n" |
| 70 | ".section .fixup,\"ax\"\n" |
| 71 | "3: movl $-1,%[err]\n" |
| 72 | " jmp 2b\n" |
| 73 | ".previous\n" |
| 74 | ".section __ex_table,\"a\"\n" |
| 75 | _ASM_ALIGN "\n" |
| 76 | _ASM_PTR "1b,3b\n" |
| 77 | ".previous" |
| 78 | : [err] "=r" (err) |
| 79 | : "D" (buf), "a" (-1), "d" (-1), "0" (0) |
| 80 | : "memory"); |
| 81 | if (unlikely(err) && __clear_user(buf, xstate_size)) |
| 82 | err = -EFAULT; |
| 83 | /* No need to clear here because the caller clears USED_MATH */ |
| 84 | return err; |
| 85 | } |
| 86 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 87 | static inline int xrestore_user(struct xsave_struct __user *buf, u64 mask) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 88 | { |
| 89 | int err; |
| 90 | struct xsave_struct *xstate = ((__force struct xsave_struct *)buf); |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 91 | u32 lmask = mask; |
| 92 | u32 hmask = mask >> 32; |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 93 | |
| 94 | __asm__ __volatile__("1: .byte " REX_PREFIX "0x0f,0xae,0x2f\n" |
| 95 | "2:\n" |
| 96 | ".section .fixup,\"ax\"\n" |
| 97 | "3: movl $-1,%[err]\n" |
| 98 | " jmp 2b\n" |
| 99 | ".previous\n" |
| 100 | ".section __ex_table,\"a\"\n" |
| 101 | _ASM_ALIGN "\n" |
| 102 | _ASM_PTR "1b,3b\n" |
| 103 | ".previous" |
| 104 | : [err] "=r" (err) |
| 105 | : "D" (xstate), "a" (lmask), "d" (hmask), "0" (0) |
| 106 | : "memory"); /* memory required? */ |
| 107 | return err; |
| 108 | } |
| 109 | |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 110 | static inline void xrstor_state(struct xsave_struct *fx, u64 mask) |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 111 | { |
H. Peter Anvin | 6152e4b | 2008-07-29 17:23:16 -0700 | [diff] [blame] | 112 | u32 lmask = mask; |
| 113 | u32 hmask = mask >> 32; |
| 114 | |
Suresh Siddha | 9dc89c0 | 2008-07-29 10:29:23 -0700 | [diff] [blame] | 115 | asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x2f\n\t" |
| 116 | : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask) |
| 117 | : "memory"); |
| 118 | } |
| 119 | |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 120 | static inline void fpu_xsave(struct fpu *fpu) |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 121 | { |
| 122 | /* This, however, we can work around by forcing the compiler to select |
| 123 | an addressing mode that doesn't require extended registers. */ |
| 124 | __asm__ __volatile__(".byte " REX_PREFIX "0x0f,0xae,0x27" |
Avi Kivity | 8660328 | 2010-05-06 11:45:46 +0300 | [diff] [blame] | 125 | : : "D" (&(fpu->state->xsave)), |
Suresh Siddha | b359e8a | 2008-07-29 10:29:20 -0700 | [diff] [blame] | 126 | "a" (-1), "d"(-1) : "memory"); |
| 127 | } |
Suresh Siddha | dc1e35c | 2008-07-29 10:29:19 -0700 | [diff] [blame] | 128 | #endif |