Skip to content

Commit 0852b37

Browse files
Rik van RielIngo Molnar
authored andcommitted
x86/fpu: Add FPU state copying quirk to handle XRSTOR failure on Intel Skylake CPUs
On Skylake CPUs I noticed that XRSTOR is unable to deal with states created by copyout_from_xsaves() if the xstate has only SSE/YMM state, and no FP state. That is, xfeatures had XFEATURE_MASK_SSE set, but not XFEATURE_MASK_FP. The reason is that part of the SSE/YMM state lives in the MXCSR and MXCSR_FLAGS fields of the FP state. Ensure that whenever we copy SSE or YMM state around, the MXCSR and MXCSR_FLAGS fields are also copied around. Signed-off-by: Rik van Riel <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Eric Biggers <[email protected]> Cc: Fenghua Yu <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Yu-cheng Yu <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 99dc26b commit 0852b37

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

arch/x86/include/asm/fpu/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ struct fxregs_state {
6868
/* Default value for fxregs_state.mxcsr: */
6969
#define MXCSR_DEFAULT 0x1f80
7070

71+
/* Copy both mxcsr & mxcsr_flags with a single u64 memcpy: */
72+
#define MXCSR_AND_FLAGS_SIZE sizeof(u64)
73+
7174
/*
7275
* Software based FPU emulation state. This is arbitrary really,
7376
* it matches the x87 format to make it easier to understand:

arch/x86/kernel/fpu/xstate.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,23 @@ int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
920920
}
921921
#endif /* ! CONFIG_ARCH_HAS_PKEYS */
922922

923+
/*
924+
* Weird legacy quirk: SSE and YMM states store information in the
925+
* MXCSR and MXCSR_FLAGS fields of the FP area. That means if the FP
926+
* area is marked as unused in the xfeatures header, we need to copy
927+
* MXCSR and MXCSR_FLAGS if either SSE or YMM are in use.
928+
*/
929+
static inline bool xfeatures_mxcsr_quirk(u64 xfeatures)
930+
{
931+
if (!(xfeatures & (XFEATURE_MASK_SSE|XFEATURE_MASK_YMM)))
932+
return 0;
933+
934+
if (xfeatures & XFEATURE_MASK_FP)
935+
return 0;
936+
937+
return 1;
938+
}
939+
923940
/*
924941
* This is similar to user_regset_copyout(), but will not add offset to
925942
* the source data pointer or increment pos, count, kbuf, and ubuf.
@@ -988,6 +1005,12 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of
9881005

9891006
}
9901007

1008+
if (xfeatures_mxcsr_quirk(header.xfeatures)) {
1009+
offset = offsetof(struct fxregs_state, mxcsr);
1010+
size = MXCSR_AND_FLAGS_SIZE;
1011+
__copy_xstate_to_kernel(kbuf, &xsave->i387.mxcsr, offset, size, size_total);
1012+
}
1013+
9911014
/*
9921015
* Fill xsave->i387.sw_reserved value for ptrace frame:
9931016
*/
@@ -1070,6 +1093,12 @@ int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned i
10701093

10711094
}
10721095

1096+
if (xfeatures_mxcsr_quirk(header.xfeatures)) {
1097+
offset = offsetof(struct fxregs_state, mxcsr);
1098+
size = MXCSR_AND_FLAGS_SIZE;
1099+
__copy_xstate_to_user(ubuf, &xsave->i387.mxcsr, offset, size, size_total);
1100+
}
1101+
10731102
/*
10741103
* Fill xsave->i387.sw_reserved value for ptrace frame:
10751104
*/
@@ -1122,6 +1151,12 @@ int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
11221151
}
11231152
}
11241153

1154+
if (xfeatures_mxcsr_quirk(xfeatures)) {
1155+
offset = offsetof(struct fxregs_state, mxcsr);
1156+
size = MXCSR_AND_FLAGS_SIZE;
1157+
memcpy(&xsave->i387.mxcsr, kbuf + offset, size);
1158+
}
1159+
11251160
/*
11261161
* The state that came in from userspace was user-state only.
11271162
* Mask all the user states out of 'xfeatures':
@@ -1177,6 +1212,13 @@ int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
11771212
}
11781213
}
11791214

1215+
if (xfeatures_mxcsr_quirk(xfeatures)) {
1216+
offset = offsetof(struct fxregs_state, mxcsr);
1217+
size = MXCSR_AND_FLAGS_SIZE;
1218+
if (__copy_from_user(&xsave->i387.mxcsr, ubuf + offset, size))
1219+
return -EFAULT;
1220+
}
1221+
11801222
/*
11811223
* The state that came in from userspace was user-state only.
11821224
* Mask all the user states out of 'xfeatures':

0 commit comments

Comments
 (0)