x86/fpu: Dont restore PKRU in fpregs_restore_userspace()
[linux-2.6-microblaze.git] / arch / x86 / include / asm / fpu / xstate.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_X86_XSAVE_H
3 #define __ASM_X86_XSAVE_H
4
5 #include <linux/uaccess.h>
6 #include <linux/types.h>
7
8 #include <asm/processor.h>
9 #include <asm/fpu/api.h>
10 #include <asm/user.h>
11
12 /* Bit 63 of XCR0 is reserved for future expansion */
13 #define XFEATURE_MASK_EXTEND    (~(XFEATURE_MASK_FPSSE | (1ULL << 63)))
14
15 #define XSTATE_CPUID            0x0000000d
16
17 #define FXSAVE_SIZE     512
18
19 #define XSAVE_HDR_SIZE      64
20 #define XSAVE_HDR_OFFSET    FXSAVE_SIZE
21
22 #define XSAVE_YMM_SIZE      256
23 #define XSAVE_YMM_OFFSET    (XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET)
24
25 #define XSAVE_ALIGNMENT     64
26
27 /* All currently supported user features */
28 #define XFEATURE_MASK_USER_SUPPORTED (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)
37
38 /*
39  * Features which are restored when returning to user space.
40  * PKRU is not restored on return to user space because PKRU
41  * is switched eagerly in switch_to() and flush_thread()
42  */
43 #define XFEATURE_MASK_USER_RESTORE      \
44         (XFEATURE_MASK_USER_SUPPORTED & ~XFEATURE_MASK_PKRU)
45
46 /* All currently supported supervisor features */
47 #define XFEATURE_MASK_SUPERVISOR_SUPPORTED (XFEATURE_MASK_PASID)
48
49 /*
50  * A supervisor state component may not always contain valuable information,
51  * and its size may be huge. Saving/restoring such supervisor state components
52  * at each context switch can cause high CPU and space overhead, which should
53  * be avoided. Such supervisor state components should only be saved/restored
54  * on demand. The on-demand supervisor features are set in this mask.
55  *
56  * Unlike the existing supported supervisor features, an independent supervisor
57  * feature does not allocate a buffer in task->fpu, and the corresponding
58  * supervisor state component cannot be saved/restored at each context switch.
59  *
60  * To support an independent supervisor feature, a developer should follow the
61  * dos and don'ts as below:
62  * - Do dynamically allocate a buffer for the supervisor state component.
63  * - Do manually invoke the XSAVES/XRSTORS instruction to save/restore the
64  *   state component to/from the buffer.
65  * - Don't set the bit corresponding to the independent supervisor feature in
66  *   IA32_XSS at run time, since it has been set at boot time.
67  */
68 #define XFEATURE_MASK_INDEPENDENT (XFEATURE_MASK_LBR)
69
70 /*
71  * Unsupported supervisor features. When a supervisor feature in this mask is
72  * supported in the future, move it to the supported supervisor feature mask.
73  */
74 #define XFEATURE_MASK_SUPERVISOR_UNSUPPORTED (XFEATURE_MASK_PT)
75
76 /* All supervisor states including supported and unsupported states. */
77 #define XFEATURE_MASK_SUPERVISOR_ALL (XFEATURE_MASK_SUPERVISOR_SUPPORTED | \
78                                       XFEATURE_MASK_INDEPENDENT | \
79                                       XFEATURE_MASK_SUPERVISOR_UNSUPPORTED)
80
81 #ifdef CONFIG_X86_64
82 #define REX_PREFIX      "0x48, "
83 #else
84 #define REX_PREFIX
85 #endif
86
87 extern u64 xfeatures_mask_all;
88
89 static inline u64 xfeatures_mask_supervisor(void)
90 {
91         return xfeatures_mask_all & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
92 }
93
94 /*
95  * The xfeatures which are enabled in XCR0 and expected to be in ptrace
96  * buffers and signal frames.
97  */
98 static inline u64 xfeatures_mask_uabi(void)
99 {
100         return xfeatures_mask_all & XFEATURE_MASK_USER_SUPPORTED;
101 }
102
103 /*
104  * The xfeatures which are restored by the kernel when returning to user
105  * mode. This is not necessarily the same as xfeatures_mask_uabi() as the
106  * kernel does not manage all XCR0 enabled features via xsave/xrstor as
107  * some of them have to be switched eagerly on context switch and exec().
108  */
109 static inline u64 xfeatures_mask_restore_user(void)
110 {
111         return xfeatures_mask_all & XFEATURE_MASK_USER_RESTORE;
112 }
113
114 static inline u64 xfeatures_mask_independent(void)
115 {
116         if (!boot_cpu_has(X86_FEATURE_ARCH_LBR))
117                 return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
118
119         return XFEATURE_MASK_INDEPENDENT;
120 }
121
122 extern u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
123
124 extern void __init update_regset_xstate_info(unsigned int size,
125                                              u64 xstate_mask);
126
127 void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
128 int xfeature_size(int xfeature_nr);
129 int copy_uabi_from_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf);
130 int copy_sigframe_from_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf);
131
132 void xsaves(struct xregs_state *xsave, u64 mask);
133 void xrstors(struct xregs_state *xsave, u64 mask);
134
135 enum xstate_copy_mode {
136         XSTATE_COPY_FP,
137         XSTATE_COPY_FX,
138         XSTATE_COPY_XSAVE,
139 };
140
141 struct membuf;
142 void copy_xstate_to_uabi_buf(struct membuf to, struct xregs_state *xsave,
143                              enum xstate_copy_mode mode);
144
145 #endif