1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2669ebabbSIngo Molnar #ifndef __ASM_X86_XSAVE_H
3669ebabbSIngo Molnar #define __ASM_X86_XSAVE_H
4669ebabbSIngo Molnar
559a36d16SIngo Molnar #include <linux/uaccess.h>
60cecca9dSRik van Riel #include <linux/types.h>
70cecca9dSRik van Riel
80cecca9dSRik van Riel #include <asm/processor.h>
9784a4661SDave Hansen #include <asm/fpu/api.h>
100cecca9dSRik van Riel #include <asm/user.h>
11669ebabbSIngo Molnar
12669ebabbSIngo Molnar /* Bit 63 of XCR0 is reserved for future expansion */
13d91cab78SDave Hansen #define XFEATURE_MASK_EXTEND (~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
14669ebabbSIngo Molnar
15677b98bdSIngo Molnar #define XSTATE_CPUID 0x0000000d
16677b98bdSIngo Molnar
17eec2113eSChang S. Bae #define TILE_CPUID 0x0000001d
18eec2113eSChang S. Bae
19669ebabbSIngo Molnar #define FXSAVE_SIZE 512
20669ebabbSIngo Molnar
21669ebabbSIngo Molnar #define XSAVE_HDR_SIZE 64
22669ebabbSIngo Molnar #define XSAVE_HDR_OFFSET FXSAVE_SIZE
23669ebabbSIngo Molnar
24669ebabbSIngo Molnar #define XSAVE_YMM_SIZE 256
25669ebabbSIngo Molnar #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
26669ebabbSIngo Molnar
27ce711ea3SKan Liang #define XSAVE_ALIGNMENT 64
28ce711ea3SKan Liang
298ab22804SFenghua Yu /* All currently supported user features */
308ab22804SFenghua Yu #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \
31a65050c6SYu-cheng Yu XFEATURE_MASK_SSE | \
32d91cab78SDave Hansen XFEATURE_MASK_YMM | \
33d91cab78SDave Hansen XFEATURE_MASK_OPMASK | \
34d91cab78SDave Hansen XFEATURE_MASK_ZMM_Hi256 | \
35c8df4009SDave Hansen XFEATURE_MASK_Hi16_ZMM | \
362f7fada2SAndy Lutomirski XFEATURE_MASK_PKRU | \
372f7fada2SAndy Lutomirski XFEATURE_MASK_BNDREGS | \
382308ee57SChang S. Bae XFEATURE_MASK_BNDCSR | \
392308ee57SChang S. Bae XFEATURE_MASK_XTILE)
40669ebabbSIngo Molnar
412ebe81c6SThomas Gleixner /*
422ebe81c6SThomas Gleixner * Features which are restored when returning to user space.
432ebe81c6SThomas Gleixner * PKRU is not restored on return to user space because PKRU
442ebe81c6SThomas Gleixner * is switched eagerly in switch_to() and flush_thread()
452ebe81c6SThomas Gleixner */
462ebe81c6SThomas Gleixner #define XFEATURE_MASK_USER_RESTORE \
472ebe81c6SThomas Gleixner (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU)
482ebe81c6SThomas Gleixner
4923686ef2SThomas Gleixner /* Features which are dynamically enabled for a process on request */
502308ee57SChang S. Bae #define XFEATURE_MASK_USER_DYNAMIC XFEATURE_MASK_XTILE_DATA
5123686ef2SThomas Gleixner
528ab22804SFenghua Yu /* All currently supported supervisor features */
53*8970ef02SRick Edgecombe #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID | \
54*8970ef02SRick Edgecombe XFEATURE_MASK_CET_USER)
558ab22804SFenghua Yu
568ab22804SFenghua Yu /*
57f0dccc9dSKan Liang * A supervisor state component may not always contain valuable information,
58f0dccc9dSKan Liang * and its size may be huge. Saving/restoring such supervisor state components
59f0dccc9dSKan Liang * at each context switch can cause high CPU and space overhead, which should
60f0dccc9dSKan Liang * be avoided. Such supervisor state components should only be saved/restored
6101707b66SAndy Lutomirski * on demand. The on-demand supervisor features are set in this mask.
62f0dccc9dSKan Liang *
6301707b66SAndy Lutomirski * Unlike the existing supported supervisor features, an independent supervisor
64f0dccc9dSKan Liang * feature does not allocate a buffer in task->fpu, and the corresponding
65f0dccc9dSKan Liang * supervisor state component cannot be saved/restored at each context switch.
66f0dccc9dSKan Liang *
6701707b66SAndy Lutomirski * To support an independent supervisor feature, a developer should follow the
68f0dccc9dSKan Liang * dos and don'ts as below:
69f0dccc9dSKan Liang * - Do dynamically allocate a buffer for the supervisor state component.
70f0dccc9dSKan Liang * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
71f0dccc9dSKan Liang * state component to/from the buffer.
7201707b66SAndy Lutomirski * - Don't set the bit corresponding to the independent supervisor feature in
73f0dccc9dSKan Liang * IA32_XSS at run time, since it has been set at boot time.
74f0dccc9dSKan Liang */
7501707b66SAndy Lutomirski #define XFEATURE_MASK_INDEPENDENT (XFEATURE_MASK_LBR)
76f0dccc9dSKan Liang
77f0dccc9dSKan Liang /*
788ab22804SFenghua Yu * Unsupported supervisor features. When a supervisor feature in this mask is
798ab22804SFenghua Yu * supported in the future, move it to the supported supervisor feature mask.
808ab22804SFenghua Yu */
81*8970ef02SRick Edgecombe #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT | \
82*8970ef02SRick Edgecombe XFEATURE_MASK_CET_KERNEL)
838ab22804SFenghua Yu
848ab22804SFenghua Yu /* All supervisor states including supported and unsupported states. */
858ab22804SFenghua Yu #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
8601707b66SAndy Lutomirski XFEATURE_MASK_INDEPENDENT | \
878ab22804SFenghua Yu XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
888ab22804SFenghua Yu
89eda32f4fSThomas Gleixner /*
90eda32f4fSThomas Gleixner * The feature mask required to restore FPU state:
91eda32f4fSThomas Gleixner * - All user states which are not eagerly switched in switch_to()/exec()
92eda32f4fSThomas Gleixner * - The suporvisor states
93eda32f4fSThomas Gleixner */
94eda32f4fSThomas Gleixner #define XFEATURE_MASK_FPSTATE (XFEATURE_MASK_USER_RESTORE | \
95eda32f4fSThomas Gleixner XFEATURE_MASK_SUPERVISOR_SUPPORTED)
96eda32f4fSThomas Gleixner
9730d02551SDave Hansen /*
9830d02551SDave Hansen * Features in this mask have space allocated in the signal frame, but may not
9930d02551SDave Hansen * have that space initialized when the feature is in its init state.
10030d02551SDave Hansen */
10130d02551SDave Hansen #define XFEATURE_MASK_SIGFRAME_INITOPT (XFEATURE_MASK_XTILE | \
10230d02551SDave Hansen XFEATURE_MASK_USER_DYNAMIC)
10330d02551SDave Hansen
104669ebabbSIngo Molnar extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
105669ebabbSIngo Molnar
106404f6aacSKees Cook extern void __init update_regset_xstate_info(unsigned int size,
107404f6aacSKees Cook u64 xstate_mask);
108669ebabbSIngo Molnar
109ce711ea3SKan Liang int xfeature_size(int xfeature_nr);
110a75c5289SThomas Gleixner
111a75c5289SThomas Gleixner void xsaves(struct xregs_state *xsave, u64 mask);
112a75c5289SThomas Gleixner void xrstors(struct xregs_state *xsave, u64 mask);
11350f408d9SKan Liang
114783e87b4SChang S. Bae int xfd_enable_feature(u64 xfd_err);
115783e87b4SChang S. Bae
11623686ef2SThomas Gleixner #ifdef CONFIG_X86_64
11723686ef2SThomas Gleixner DECLARE_STATIC_KEY_FALSE(__fpu_state_size_dynamic);
11823686ef2SThomas Gleixner #endif
11923686ef2SThomas Gleixner
12023686ef2SThomas Gleixner #ifdef CONFIG_X86_64
12123686ef2SThomas Gleixner DECLARE_STATIC_KEY_FALSE(__fpu_state_size_dynamic);
12223686ef2SThomas Gleixner
fpu_state_size_dynamic(void)12323686ef2SThomas Gleixner static __always_inline __pure bool fpu_state_size_dynamic(void)
12423686ef2SThomas Gleixner {
12523686ef2SThomas Gleixner return static_branch_unlikely(&__fpu_state_size_dynamic);
12623686ef2SThomas Gleixner }
12723686ef2SThomas Gleixner #else
fpu_state_size_dynamic(void)12823686ef2SThomas Gleixner static __always_inline __pure bool fpu_state_size_dynamic(void)
12923686ef2SThomas Gleixner {
13023686ef2SThomas Gleixner return false;
13123686ef2SThomas Gleixner }
13223686ef2SThomas Gleixner #endif
13323686ef2SThomas Gleixner
134669ebabbSIngo Molnar #endif
135