x86/fpu: Extend fpu_xstate_prctl() with guest permissions
[linux-2.6-microblaze.git] / arch / x86 / kernel / fpu / xstate.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __X86_KERNEL_FPU_XSTATE_H
3 #define __X86_KERNEL_FPU_XSTATE_H
4
5 #include <asm/cpufeature.h>
6 #include <asm/fpu/xstate.h>
7 #include <asm/fpu/xcr.h>
8
9 #ifdef CONFIG_X86_64
10 DECLARE_PER_CPU(u64, xfd_state);
11 #endif
12
13 static inline void xstate_init_xcomp_bv(struct xregs_state *xsave, u64 mask)
14 {
15         /*
16          * XRSTORS requires these bits set in xcomp_bv, or it will
17          * trigger #GP:
18          */
19         if (cpu_feature_enabled(X86_FEATURE_XSAVES))
20                 xsave->header.xcomp_bv = mask | XCOMP_BV_COMPACTED_FORMAT;
21 }
22
23 static inline u64 xstate_get_group_perm(bool guest)
24 {
25         struct fpu *fpu = &current->group_leader->thread.fpu;
26         struct fpu_state_perm *perm;
27
28         /* Pairs with WRITE_ONCE() in xstate_request_perm() */
29         perm = guest ? &fpu->guest_perm : &fpu->perm;
30         return READ_ONCE(perm->__state_perm);
31 }
32
33 static inline u64 xstate_get_host_group_perm(void)
34 {
35         return xstate_get_group_perm(false);
36 }
37
38 enum xstate_copy_mode {
39         XSTATE_COPY_FP,
40         XSTATE_COPY_FX,
41         XSTATE_COPY_XSAVE,
42 };
43
44 struct membuf;
45 extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate,
46                                       u32 pkru_val, enum xstate_copy_mode copy_mode);
47 extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk,
48                                     enum xstate_copy_mode mode);
49 extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf);
50 extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void __user *ubuf);
51
52
53 extern void fpu__init_cpu_xstate(void);
54 extern void fpu__init_system_xstate(unsigned int legacy_size);
55
56 extern void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr);
57
58 static inline u64 xfeatures_mask_supervisor(void)
59 {
60         return fpu_kernel_cfg.max_features & XFEATURE_MASK_SUPERVISOR_SUPPORTED;
61 }
62
63 static inline u64 xfeatures_mask_independent(void)
64 {
65         if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR))
66                 return XFEATURE_MASK_INDEPENDENT & ~XFEATURE_MASK_LBR;
67
68         return XFEATURE_MASK_INDEPENDENT;
69 }
70
71 /* XSAVE/XRSTOR wrapper functions */
72
73 #ifdef CONFIG_X86_64
74 #define REX_PREFIX      "0x48, "
75 #else
76 #define REX_PREFIX
77 #endif
78
79 /* These macros all use (%edi)/(%rdi) as the single memory argument. */
80 #define XSAVE           ".byte " REX_PREFIX "0x0f,0xae,0x27"
81 #define XSAVEOPT        ".byte " REX_PREFIX "0x0f,0xae,0x37"
82 #define XSAVES          ".byte " REX_PREFIX "0x0f,0xc7,0x2f"
83 #define XRSTOR          ".byte " REX_PREFIX "0x0f,0xae,0x2f"
84 #define XRSTORS         ".byte " REX_PREFIX "0x0f,0xc7,0x1f"
85
86 /*
87  * After this @err contains 0 on success or the trap number when the
88  * operation raises an exception.
89  */
90 #define XSTATE_OP(op, st, lmask, hmask, err)                            \
91         asm volatile("1:" op "\n\t"                                     \
92                      "xor %[err], %[err]\n"                             \
93                      "2:\n\t"                                           \
94                      _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_FAULT_MCE_SAFE)  \
95                      : [err] "=a" (err)                                 \
96                      : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)    \
97                      : "memory")
98
99 /*
100  * If XSAVES is enabled, it replaces XSAVEOPT because it supports a compact
101  * format and supervisor states in addition to modified optimization in
102  * XSAVEOPT.
103  *
104  * Otherwise, if XSAVEOPT is enabled, XSAVEOPT replaces XSAVE because XSAVEOPT
105  * supports modified optimization which is not supported by XSAVE.
106  *
107  * We use XSAVE as a fallback.
108  *
109  * The 661 label is defined in the ALTERNATIVE* macros as the address of the
110  * original instruction which gets replaced. We need to use it here as the
111  * address of the instruction where we might get an exception at.
112  */
113 #define XSTATE_XSAVE(st, lmask, hmask, err)                             \
114         asm volatile(ALTERNATIVE_2(XSAVE,                               \
115                                    XSAVEOPT, X86_FEATURE_XSAVEOPT,      \
116                                    XSAVES,   X86_FEATURE_XSAVES)        \
117                      "\n"                                               \
118                      "xor %[err], %[err]\n"                             \
119                      "3:\n"                                             \
120                      ".pushsection .fixup,\"ax\"\n"                     \
121                      "4: movl $-2, %[err]\n"                            \
122                      "jmp 3b\n"                                         \
123                      ".popsection\n"                                    \
124                      _ASM_EXTABLE(661b, 4b)                             \
125                      : [err] "=r" (err)                                 \
126                      : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)    \
127                      : "memory")
128
129 /*
130  * Use XRSTORS to restore context if it is enabled. XRSTORS supports compact
131  * XSAVE area format.
132  */
133 #define XSTATE_XRESTORE(st, lmask, hmask)                               \
134         asm volatile(ALTERNATIVE(XRSTOR,                                \
135                                  XRSTORS, X86_FEATURE_XSAVES)           \
136                      "\n"                                               \
137                      "3:\n"                                             \
138                      _ASM_EXTABLE_TYPE(661b, 3b, EX_TYPE_FPU_RESTORE)   \
139                      :                                                  \
140                      : "D" (st), "m" (*st), "a" (lmask), "d" (hmask)    \
141                      : "memory")
142
143 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_DEBUG_FPU)
144 extern void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor);
145 #else
146 static inline void xfd_validate_state(struct fpstate *fpstate, u64 mask, bool rstor) { }
147 #endif
148
149 #ifdef CONFIG_X86_64
150 static inline void xfd_update_state(struct fpstate *fpstate)
151 {
152         if (fpu_state_size_dynamic()) {
153                 u64 xfd = fpstate->xfd;
154
155                 if (__this_cpu_read(xfd_state) != xfd) {
156                         wrmsrl(MSR_IA32_XFD, xfd);
157                         __this_cpu_write(xfd_state, xfd);
158                 }
159         }
160 }
161 #else
162 static inline void xfd_update_state(struct fpstate *fpstate) { }
163 #endif
164
165 /*
166  * Save processor xstate to xsave area.
167  *
168  * Uses either XSAVE or XSAVEOPT or XSAVES depending on the CPU features
169  * and command line options. The choice is permanent until the next reboot.
170  */
171 static inline void os_xsave(struct fpstate *fpstate)
172 {
173         u64 mask = fpstate->xfeatures;
174         u32 lmask = mask;
175         u32 hmask = mask >> 32;
176         int err;
177
178         WARN_ON_FPU(!alternatives_patched);
179         xfd_validate_state(fpstate, mask, false);
180
181         XSTATE_XSAVE(&fpstate->regs.xsave, lmask, hmask, err);
182
183         /* We should never fault when copying to a kernel buffer: */
184         WARN_ON_FPU(err);
185 }
186
187 /*
188  * Restore processor xstate from xsave area.
189  *
190  * Uses XRSTORS when XSAVES is used, XRSTOR otherwise.
191  */
192 static inline void os_xrstor(struct fpstate *fpstate, u64 mask)
193 {
194         u32 lmask = mask;
195         u32 hmask = mask >> 32;
196
197         xfd_validate_state(fpstate, mask, true);
198         XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
199 }
200
201 /* Restore of supervisor state. Does not require XFD */
202 static inline void os_xrstor_supervisor(struct fpstate *fpstate)
203 {
204         u64 mask = xfeatures_mask_supervisor();
205         u32 lmask = mask;
206         u32 hmask = mask >> 32;
207
208         XSTATE_XRESTORE(&fpstate->regs.xsave, lmask, hmask);
209 }
210
211 /*
212  * XSAVE itself always writes all requested xfeatures.  Removing features
213  * from the request bitmap reduces the features which are written.
214  * Generate a mask of features which must be written to a sigframe.  The
215  * unset features can be optimized away and not written.
216  *
217  * This optimization is user-visible.  Only use for states where
218  * uninitialized sigframe contents are tolerable, like dynamic features.
219  *
220  * Users of buffers produced with this optimization must check XSTATE_BV
221  * to determine which features have been optimized out.
222  */
223 static inline u64 xfeatures_need_sigframe_write(void)
224 {
225         u64 xfeaures_to_write;
226
227         /* In-use features must be written: */
228         xfeaures_to_write = xfeatures_in_use();
229
230         /* Also write all non-optimizable sigframe features: */
231         xfeaures_to_write |= XFEATURE_MASK_USER_SUPPORTED &
232                              ~XFEATURE_MASK_SIGFRAME_INITOPT;
233
234         return xfeaures_to_write;
235 }
236
237 /*
238  * Save xstate to user space xsave area.
239  *
240  * We don't use modified optimization because xrstor/xrstors might track
241  * a different application.
242  *
243  * We don't use compacted format xsave area for backward compatibility for
244  * old applications which don't understand the compacted format of the
245  * xsave area.
246  *
247  * The caller has to zero buf::header before calling this because XSAVE*
248  * does not touch the reserved fields in the header.
249  */
250 static inline int xsave_to_user_sigframe(struct xregs_state __user *buf)
251 {
252         /*
253          * Include the features which are not xsaved/rstored by the kernel
254          * internally, e.g. PKRU. That's user space ABI and also required
255          * to allow the signal handler to modify PKRU.
256          */
257         struct fpstate *fpstate = current->thread.fpu.fpstate;
258         u64 mask = fpstate->user_xfeatures;
259         u32 lmask;
260         u32 hmask;
261         int err;
262
263         /* Optimize away writing unnecessary xfeatures: */
264         if (fpu_state_size_dynamic())
265                 mask &= xfeatures_need_sigframe_write();
266
267         lmask = mask;
268         hmask = mask >> 32;
269         xfd_validate_state(fpstate, mask, false);
270
271         stac();
272         XSTATE_OP(XSAVE, buf, lmask, hmask, err);
273         clac();
274
275         return err;
276 }
277
278 /*
279  * Restore xstate from user space xsave area.
280  */
281 static inline int xrstor_from_user_sigframe(struct xregs_state __user *buf, u64 mask)
282 {
283         struct xregs_state *xstate = ((__force struct xregs_state *)buf);
284         u32 lmask = mask;
285         u32 hmask = mask >> 32;
286         int err;
287
288         xfd_validate_state(current->thread.fpu.fpstate, mask, true);
289
290         stac();
291         XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
292         clac();
293
294         return err;
295 }
296
297 /*
298  * Restore xstate from kernel space xsave area, return an error code instead of
299  * an exception.
300  */
301 static inline int os_xrstor_safe(struct fpstate *fpstate, u64 mask)
302 {
303         struct xregs_state *xstate = &fpstate->regs.xsave;
304         u32 lmask = mask;
305         u32 hmask = mask >> 32;
306         int err;
307
308         /* Ensure that XFD is up to date */
309         xfd_update_state(fpstate);
310
311         if (cpu_feature_enabled(X86_FEATURE_XSAVES))
312                 XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
313         else
314                 XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
315
316         return err;
317 }
318
319
320 #endif