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> 90cecca9dSRik van Riel #include <asm/user.h> 10669ebabbSIngo Molnar 11669ebabbSIngo Molnar /* Bit 63 of XCR0 is reserved for future expansion */ 12d91cab78SDave Hansen #define XFEATURE_MASK_EXTEND (~(XFEATURE_MASK_FPSSE | (1ULL << 63))) 13669ebabbSIngo Molnar 14677b98bdSIngo Molnar #define XSTATE_CPUID 0x0000000d 15677b98bdSIngo Molnar 16669ebabbSIngo Molnar #define FXSAVE_SIZE 512 17669ebabbSIngo Molnar 18669ebabbSIngo Molnar #define XSAVE_HDR_SIZE 64 19669ebabbSIngo Molnar #define XSAVE_HDR_OFFSET FXSAVE_SIZE 20669ebabbSIngo Molnar 21669ebabbSIngo Molnar #define XSAVE_YMM_SIZE 256 22669ebabbSIngo Molnar #define XSAVE_YMM_OFFSET (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET) 23669ebabbSIngo Molnar 248ab22804SFenghua Yu /* All currently supported user features */ 258ab22804SFenghua Yu #define XFEATURE_MASK_USER_SUPPORTED (XFEATURE_MASK_FP | \ 26a65050c6SYu-cheng Yu XFEATURE_MASK_SSE | \ 27d91cab78SDave Hansen XFEATURE_MASK_YMM | \ 28d91cab78SDave Hansen XFEATURE_MASK_OPMASK | \ 29d91cab78SDave Hansen XFEATURE_MASK_ZMM_Hi256 | \ 30c8df4009SDave Hansen XFEATURE_MASK_Hi16_ZMM | \ 312f7fada2SAndy Lutomirski XFEATURE_MASK_PKRU | \ 322f7fada2SAndy Lutomirski XFEATURE_MASK_BNDREGS | \ 332f7fada2SAndy Lutomirski XFEATURE_MASK_BNDCSR) 34669ebabbSIngo Molnar 358ab22804SFenghua Yu /* All currently supported supervisor features */ 368ab22804SFenghua Yu #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (0) 378ab22804SFenghua Yu 388ab22804SFenghua Yu /* 39f0dccc9dSKan Liang * A supervisor state component may not always contain valuable information, 40f0dccc9dSKan Liang * and its size may be huge. Saving/restoring such supervisor state components 41f0dccc9dSKan Liang * at each context switch can cause high CPU and space overhead, which should 42f0dccc9dSKan Liang * be avoided. Such supervisor state components should only be saved/restored 43f0dccc9dSKan Liang * on demand. The on-demand dynamic supervisor features are set in this mask. 44f0dccc9dSKan Liang * 45f0dccc9dSKan Liang * Unlike the existing supported supervisor features, a dynamic supervisor 46f0dccc9dSKan Liang * feature does not allocate a buffer in task->fpu, and the corresponding 47f0dccc9dSKan Liang * supervisor state component cannot be saved/restored at each context switch. 48f0dccc9dSKan Liang * 49f0dccc9dSKan Liang * To support a dynamic supervisor feature, a developer should follow the 50f0dccc9dSKan Liang * dos and don'ts as below: 51f0dccc9dSKan Liang * - Do dynamically allocate a buffer for the supervisor state component. 52f0dccc9dSKan Liang * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the 53f0dccc9dSKan Liang * state component to/from the buffer. 54f0dccc9dSKan Liang * - Don't set the bit corresponding to the dynamic supervisor feature in 55f0dccc9dSKan Liang * IA32_XSS at run time, since it has been set at boot time. 56f0dccc9dSKan Liang */ 57f0dccc9dSKan Liang #define XFEATURE_MASK_DYNAMIC (XFEATURE_MASK_LBR) 58f0dccc9dSKan Liang 59f0dccc9dSKan Liang /* 608ab22804SFenghua Yu * Unsupported supervisor features. When a supervisor feature in this mask is 618ab22804SFenghua Yu * supported in the future, move it to the supported supervisor feature mask. 628ab22804SFenghua Yu */ 638ab22804SFenghua Yu #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT) 648ab22804SFenghua Yu 658ab22804SFenghua Yu /* All supervisor states including supported and unsupported states. */ 668ab22804SFenghua Yu #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \ 67f0dccc9dSKan Liang XFEATURE_MASK_DYNAMIC | \ 688ab22804SFenghua Yu XFEATURE_MASK_SUPERVISOR_UNSUPPORTED) 698ab22804SFenghua Yu 70669ebabbSIngo Molnar #ifdef CONFIG_X86_64 71669ebabbSIngo Molnar #define REX_PREFIX "0x48, " 72669ebabbSIngo Molnar #else 73669ebabbSIngo Molnar #define REX_PREFIX 74669ebabbSIngo Molnar #endif 75669ebabbSIngo Molnar 76524bb73bSYu-cheng Yu extern u64 xfeatures_mask_all; 77524bb73bSYu-cheng Yu 78524bb73bSYu-cheng Yu static inline u64 xfeatures_mask_supervisor(void) 79524bb73bSYu-cheng Yu { 80524bb73bSYu-cheng Yu return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED; 81524bb73bSYu-cheng Yu } 82524bb73bSYu-cheng Yu 83524bb73bSYu-cheng Yu static inline u64 xfeatures_mask_user(void) 84524bb73bSYu-cheng Yu { 85524bb73bSYu-cheng Yu return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED; 86524bb73bSYu-cheng Yu } 87524bb73bSYu-cheng Yu 88f0dccc9dSKan Liang static inline u64 xfeatures_mask_dynamic(void) 89f0dccc9dSKan Liang { 90f0dccc9dSKan Liang if (!boot_cpu_has(X86_FEATURE_ARCH_LBR)) 91f0dccc9dSKan Liang return XFEATURE_MASK_DYNAMIC & ~XFEATURE_MASK_LBR; 92f0dccc9dSKan Liang 93f0dccc9dSKan Liang return XFEATURE_MASK_DYNAMIC; 94f0dccc9dSKan Liang } 95f0dccc9dSKan Liang 96669ebabbSIngo Molnar extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS]; 97669ebabbSIngo Molnar 98404f6aacSKees Cook extern void __init update_regset_xstate_info(unsigned int size, 99404f6aacSKees Cook u64 xstate_mask); 100669ebabbSIngo Molnar 101abd16d68SSebastian Andrzej Siewior void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr); 102abd16d68SSebastian Andrzej Siewior const void *get_xsave_field_ptr(int xfeature_nr); 10399aa22d0SYu-cheng Yu int using_compacted_format(void); 10456583c9aSIngo Molnar int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int offset, unsigned int size); 10556583c9aSIngo Molnar int copy_xstate_to_user(void __user *ubuf, struct xregs_state *xsave, unsigned int offset, unsigned int size); 1066d7f7da5SIngo Molnar int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf); 1076d7f7da5SIngo Molnar int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf); 108eeedf153SYu-cheng Yu void copy_supervisor_to_kernel(struct xregs_state *xsave); 109*50f408d9SKan Liang void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask); 110*50f408d9SKan Liang void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask); 111*50f408d9SKan Liang 112e63e5d5cSEric Biggers 113e63e5d5cSEric Biggers /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */ 1145274e6c1SFenghua Yu int validate_user_xstate_header(const struct xstate_header *hdr); 115e63e5d5cSEric Biggers 116669ebabbSIngo Molnar #endif 117