xstate.h 3.8 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4
#ifndef __ASM_X86_XSAVE_H
#define __ASM_X86_XSAVE_H

R
Rik van Riel 已提交
5
#include <linux/uaccess.h>
6
#include <linux/types.h>
R
Rik van Riel 已提交
7

8
#include <asm/processor.h>
R
Rik van Riel 已提交
9
#include <asm/user.h>
10

11
/* Bit 63 of XCR0 is reserved for future expansion */
D
Dave Hansen 已提交
12
#define XFEATURE_MASK_EXTEND	(~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
13

I
Ingo Molnar 已提交
14 15
#define XSTATE_CPUID		0x0000000d

16 17
#define FXSAVE_SIZE	512

18 19 20 21 22
#define XSAVE_HDR_SIZE	    64
#define XSAVE_HDR_OFFSET    FXSAVE_SIZE

#define XSAVE_YMM_SIZE	    256
#define XSAVE_YMM_OFFSET    (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
S
Sheng Yang 已提交
23

24 25
#define XSAVE_ALIGNMENT     64

26 27 28 29 30 31 32 33 34 35 36 37
/* All currently supported user features */
#define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
				      XFEATURE_MASK_SSE | \
				      XFEATURE_MASK_YMM | \
				      XFEATURE_MASK_OPMASK | \
				      XFEATURE_MASK_ZMM_Hi256 | \
				      XFEATURE_MASK_Hi16_ZMM	 | \
				      XFEATURE_MASK_PKRU | \
				      XFEATURE_MASK_BNDREGS | \
				      XFEATURE_MASK_BNDCSR)

/* All currently supported supervisor features */
38
#define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
39

40 41 42 43 44
/*
 * A supervisor state component may not always contain valuable information,
 * and its size may be huge. Saving/restoring such supervisor state components
 * at each context switch can cause high CPU and space overhead, which should
 * be avoided. Such supervisor state components should only be saved/restored
45
 * on demand. The on-demand supervisor features are set in this mask.
46
 *
47
 * Unlike the existing supported supervisor features, an independent supervisor
48 49 50
 * feature does not allocate a buffer in task->fpu, and the corresponding
 * supervisor state component cannot be saved/restored at each context switch.
 *
51
 * To support an independent supervisor feature, a developer should follow the
52 53 54 55
 * dos and don'ts as below:
 * - Do dynamically allocate a buffer for the supervisor state component.
 * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
 *   state component to/from the buffer.
56
 * - Don't set the bit corresponding to the independent supervisor feature in
57 58
 *   IA32_XSS at run time, since it has been set at boot time.
 */
59
#define XFEATURE_MASK_INDEPENDENT (XFEATURE_MASK_LBR)
60

61 62 63 64 65 66 67 68
/*
 * Unsupported supervisor features. When a supervisor feature in this mask is
 * supported in the future, move it to the supported supervisor feature mask.
 */
#define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)

/* All supervisor states including supported and unsupported states. */
#define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
69
				      XFEATURE_MASK_INDEPENDENT | \
70
				      XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
71

72 73 74 75 76 77
#ifdef CONFIG_X86_64
#define REX_PREFIX	"0x48, "
#else
#define REX_PREFIX
#endif

78 79 80 81 82 83 84 85 86 87 88 89
extern u64 xfeatures_mask_all;

static inline u64 xfeatures_mask_supervisor(void)
{
	return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
}

static inline u64 xfeatures_mask_user(void)
{
	return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
}

90
static inline u64 xfeatures_mask_independent(void)
91 92
{
	if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
93
		return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
94

95
	return XFEATURE_MASK_INDEPENDENT;
96 97
}

98
extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
99

100 101
extern void __init update_regset_xstate_info(unsigned int size,
					     u64 xstate_mask);
102

103
void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
104
int xfeature_size(int xfeature_nr);
105 106
int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
int copy_sigframe_from_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
107 108 109

void xsaves(struct xregs_state *xsave, u64 mask);
void xrstors(struct xregs_state *xsave, u64 mask);
110

111 112 113 114 115 116 117 118 119 120
enum xstate_copy_mode {
	XSTATE_COPY_FP,
	XSTATE_COPY_FX,
	XSTATE_COPY_XSAVE,
};

struct membuf;
void copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,
			     enum xstate_copy_mode mode);

121
#endif