Skip to content

Commit 0a319ef

Browse files
committed
Merge tag 'x86-fpu-2020-06-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 FPU updates from Ingo Molnar: "Most of the changes here related to 'XSAVES supervisor state' support, which is a feature that allows kernel-only data to be automatically saved/restored by the FPU context switching code. CPU features that can be supported this way are Intel PT, 'PASID' and CET features" * tag 'x86-fpu-2020-06-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/fpu/xstate: Restore supervisor states for signal return x86/fpu/xstate: Preserve supervisor states for the slow path in __fpu__restore_sig() x86/fpu: Introduce copy_supervisor_to_kernel() x86/fpu/xstate: Update copy_kernel_to_xregs_err() for supervisor states x86/fpu/xstate: Update sanitize_restored_xstate() for supervisor xstates x86/fpu/xstate: Define new functions for clearing fpregs and xstates x86/fpu/xstate: Introduce XSAVES supervisor states x86/fpu/xstate: Separate user and supervisor xfeatures mask x86/fpu/xstate: Define new macros for supervisor and user xstates x86/fpu/xstate: Rename validate_xstate_header() to validate_user_xstate_header()
2 parents eff5dda + 55e00fb commit 0a319ef

File tree

9 files changed

+336
-131
lines changed

9 files changed

+336
-131
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ extern void fpu__save(struct fpu *fpu);
3131
extern int fpu__restore_sig(void __user *buf, int ia32_frame);
3232
extern void fpu__drop(struct fpu *fpu);
3333
extern int fpu__copy(struct task_struct *dst, struct task_struct *src);
34-
extern void fpu__clear(struct fpu *fpu);
34+
extern void fpu__clear_user_states(struct fpu *fpu);
35+
extern void fpu__clear_all(struct fpu *fpu);
3536
extern int fpu__exception_code(struct fpu *fpu, int trap_nr);
3637
extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate);
3738

@@ -92,7 +93,7 @@ static inline void fpstate_init_xstate(struct xregs_state *xsave)
9293
* XRSTORS requires these bits set in xcomp_bv, or it will
9394
* trigger #GP:
9495
*/
95-
xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask;
96+
xsave->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT | xfeatures_mask_all;
9697
}
9798

9899
static inline void fpstate_init_fxstate(struct fxregs_state *fx)
@@ -399,7 +400,10 @@ static inline int copy_kernel_to_xregs_err(struct xregs_state *xstate, u64 mask)
399400
u32 hmask = mask >> 32;
400401
int err;
401402

402-
XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
403+
if (static_cpu_has(X86_FEATURE_XSAVES))
404+
XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
405+
else
406+
XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
403407

404408
return err;
405409
}

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,48 @@
2121
#define XSAVE_YMM_SIZE 256
2222
#define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
2323

24-
/* Supervisor features */
25-
#define XFEATURE_MASK_SUPERVISOR (XFEATURE_MASK_PT)
26-
27-
/* All currently supported features */
28-
#define XCNTXT_MASK (XFEATURE_MASK_FP | \
29-
XFEATURE_MASK_SSE | \
30-
XFEATURE_MASK_YMM | \
31-
XFEATURE_MASK_OPMASK | \
32-
XFEATURE_MASK_ZMM_Hi256 | \
33-
XFEATURE_MASK_Hi16_ZMM | \
34-
XFEATURE_MASK_PKRU | \
35-
XFEATURE_MASK_BNDREGS | \
36-
XFEATURE_MASK_BNDCSR)
24+
/* All currently supported user features */
25+
#define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
26+
XFEATURE_MASK_SSE | \
27+
XFEATURE_MASK_YMM | \
28+
XFEATURE_MASK_OPMASK | \
29+
XFEATURE_MASK_ZMM_Hi256 | \
30+
XFEATURE_MASK_Hi16_ZMM | \
31+
XFEATURE_MASK_PKRU | \
32+
XFEATURE_MASK_BNDREGS | \
33+
XFEATURE_MASK_BNDCSR)
34+
35+
/* All currently supported supervisor features */
36+
#define XFEATURE_MASK_SUPERVISOR_SUPPORTED (0)
37+
38+
/*
39+
* Unsupported supervisor features. When a supervisor feature in this mask is
40+
* supported in the future, move it to the supported supervisor feature mask.
41+
*/
42+
#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)
43+
44+
/* All supervisor states including supported and unsupported states. */
45+
#define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
46+
XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
3747

3848
#ifdef CONFIG_X86_64
3949
#define REX_PREFIX "0x48, "
4050
#else
4151
#define REX_PREFIX
4252
#endif
4353

44-
extern u64 xfeatures_mask;
54+
extern u64 xfeatures_mask_all;
55+
56+
static inline u64 xfeatures_mask_supervisor(void)
57+
{
58+
return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
59+
}
60+
61+
static inline u64 xfeatures_mask_user(void)
62+
{
63+
return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
64+
}
65+
4566
extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
4667

4768
extern void __init update_regset_xstate_info(unsigned int size,
@@ -54,8 +75,9 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of
5475
int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned int offset, unsigned int size);
5576
int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
5677
int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
78+
void copy_supervisor_to_kernel(struct xregs_state *xsave);
5779

5880
/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
59-
extern int validate_xstate_header(const struct xstate_header *hdr);
81+
int validate_user_xstate_header(const struct xstate_header *hdr);
6082

6183
#endif

arch/x86/kernel/fpu/core.c

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -291,25 +291,20 @@ void fpu__drop(struct fpu *fpu)
291291
}
292292

293293
/*
294-
* Clear FPU registers by setting them up from
295-
* the init fpstate:
294+
* Clear FPU registers by setting them up from the init fpstate.
295+
* Caller must do fpregs_[un]lock() around it.
296296
*/
297-
static inline void copy_init_fpstate_to_fpregs(void)
297+
static inline void copy_init_fpstate_to_fpregs(u64 features_mask)
298298
{
299-
fpregs_lock();
300-
301299
if (use_xsave())
302-
copy_kernel_to_xregs(&init_fpstate.xsave, -1);
300+
copy_kernel_to_xregs(&init_fpstate.xsave, features_mask);
303301
else if (static_cpu_has(X86_FEATURE_FXSR))
304302
copy_kernel_to_fxregs(&init_fpstate.fxsave);
305303
else
306304
copy_kernel_to_fregs(&init_fpstate.fsave);
307305

308306
if (boot_cpu_has(X86_FEATURE_OSPKE))
309307
copy_init_pkru_to_fpregs();
310-
311-
fpregs_mark_activate();
312-
fpregs_unlock();
313308
}
314309

315310
/*
@@ -318,18 +313,40 @@ static inline void copy_init_fpstate_to_fpregs(void)
318313
* Called by sys_execve(), by the signal handler code and by various
319314
* error paths.
320315
*/
321-
void fpu__clear(struct fpu *fpu)
316+
static void fpu__clear(struct fpu *fpu, bool user_only)
322317
{
323-
WARN_ON_FPU(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
318+
WARN_ON_FPU(fpu != &current->thread.fpu);
324319

325-
fpu__drop(fpu);
320+
if (!static_cpu_has(X86_FEATURE_FPU)) {
321+
fpu__drop(fpu);
322+
fpu__initialize(fpu);
323+
return;
324+
}
326325

327-
/*
328-
* Make sure fpstate is cleared and initialized.
329-
*/
330-
fpu__initialize(fpu);
331-
if (static_cpu_has(X86_FEATURE_FPU))
332-
copy_init_fpstate_to_fpregs();
326+
fpregs_lock();
327+
328+
if (user_only) {
329+
if (!fpregs_state_valid(fpu, smp_processor_id()) &&
330+
xfeatures_mask_supervisor())
331+
copy_kernel_to_xregs(&fpu->state.xsave,
332+
xfeatures_mask_supervisor());
333+
copy_init_fpstate_to_fpregs(xfeatures_mask_user());
334+
} else {
335+
copy_init_fpstate_to_fpregs(xfeatures_mask_all);
336+
}
337+
338+
fpregs_mark_activate();
339+
fpregs_unlock();
340+
}
341+
342+
void fpu__clear_user_states(struct fpu *fpu)
343+
{
344+
fpu__clear(fpu, true);
345+
}
346+
347+
void fpu__clear_all(struct fpu *fpu)
348+
{
349+
fpu__clear(fpu, false);
333350
}
334351

335352
/*

arch/x86/kernel/fpu/init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ static void __init fpu__init_system_xstate_size_legacy(void)
224224
*/
225225
u64 __init fpu__get_supported_xfeatures_mask(void)
226226
{
227-
return XCNTXT_MASK;
227+
return XFEATURE_MASK_USER_SUPPORTED |
228+
XFEATURE_MASK_SUPERVISOR_SUPPORTED;
228229
}
229230

230231
/* Legacy code to initialize eager fpu mode. */

arch/x86/kernel/fpu/regset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
139139
} else {
140140
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
141141
if (!ret)
142-
ret = validate_xstate_header(&xsave->header);
142+
ret = validate_user_xstate_header(&xsave->header);
143143
}
144144

145145
/*

0 commit comments

Comments
 (0)