1 // SPDX-License-Identifier: GPL-2.0
3 * FPU register's regset abstraction, for ptrace, core dumps, etc.
5 #include <asm/fpu/internal.h>
6 #include <asm/fpu/signal.h>
7 #include <asm/fpu/regset.h>
8 #include <asm/fpu/xstate.h>
9 #include <linux/sched/task_stack.h>
12 * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
13 * as the "regset->n" for the xstate regset will be updated based on the feature
14 * capabilities supported by the xsave.
16 int regset_fpregs_active(struct task_struct
*target
, const struct user_regset
*regset
)
18 struct fpu
*target_fpu
= &target
->thread
.fpu
;
20 return target_fpu
->initialized
? regset
->n
: 0;
23 int regset_xregset_fpregs_active(struct task_struct
*target
, const struct user_regset
*regset
)
25 struct fpu
*target_fpu
= &target
->thread
.fpu
;
27 if (boot_cpu_has(X86_FEATURE_FXSR
) && target_fpu
->initialized
)
33 int xfpregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
34 unsigned int pos
, unsigned int count
,
35 void *kbuf
, void __user
*ubuf
)
37 struct fpu
*fpu
= &target
->thread
.fpu
;
39 if (!boot_cpu_has(X86_FEATURE_FXSR
))
42 fpu__prepare_read(fpu
);
43 fpstate_sanitize_xstate(fpu
);
45 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
46 &fpu
->state
.fxsave
, 0, -1);
49 int xfpregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
50 unsigned int pos
, unsigned int count
,
51 const void *kbuf
, const void __user
*ubuf
)
53 struct fpu
*fpu
= &target
->thread
.fpu
;
56 if (!boot_cpu_has(X86_FEATURE_FXSR
))
59 fpu__prepare_write(fpu
);
60 fpstate_sanitize_xstate(fpu
);
62 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
63 &fpu
->state
.fxsave
, 0, -1);
66 * mxcsr reserved bits must be masked to zero for security reasons.
68 fpu
->state
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
71 * update the header bits in the xsave header, indicating the
72 * presence of FP and SSE state.
74 if (boot_cpu_has(X86_FEATURE_XSAVE
))
75 fpu
->state
.xsave
.header
.xfeatures
|= XFEATURE_MASK_FPSSE
;
80 int xstateregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
81 unsigned int pos
, unsigned int count
,
82 void *kbuf
, void __user
*ubuf
)
84 struct fpu
*fpu
= &target
->thread
.fpu
;
85 struct xregs_state
*xsave
;
88 if (!boot_cpu_has(X86_FEATURE_XSAVE
))
91 xsave
= &fpu
->state
.xsave
;
93 fpu__prepare_read(fpu
);
95 if (using_compacted_format()) {
97 ret
= copy_xstate_to_kernel(kbuf
, xsave
, pos
, count
);
99 ret
= copy_xstate_to_user(ubuf
, xsave
, pos
, count
);
101 fpstate_sanitize_xstate(fpu
);
103 * Copy the 48 bytes defined by the software into the xsave
104 * area in the thread struct, so that we can copy the whole
105 * area to user using one user_regset_copyout().
107 memcpy(&xsave
->i387
.sw_reserved
, xstate_fx_sw_bytes
, sizeof(xstate_fx_sw_bytes
));
110 * Copy the xstate memory layout.
112 ret
= user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, xsave
, 0, -1);
117 int xstateregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
118 unsigned int pos
, unsigned int count
,
119 const void *kbuf
, const void __user
*ubuf
)
121 struct fpu
*fpu
= &target
->thread
.fpu
;
122 struct xregs_state
*xsave
;
125 if (!boot_cpu_has(X86_FEATURE_XSAVE
))
129 * A whole standard-format XSAVE buffer is needed:
131 if ((pos
!= 0) || (count
< fpu_user_xstate_size
))
134 xsave
= &fpu
->state
.xsave
;
136 fpu__prepare_write(fpu
);
138 if (using_compacted_format()) {
140 ret
= copy_kernel_to_xstate(xsave
, kbuf
);
142 ret
= copy_user_to_xstate(xsave
, ubuf
);
144 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, xsave
, 0, -1);
146 ret
= validate_xstate_header(&xsave
->header
);
150 * mxcsr reserved bits must be masked to zero for security reasons.
152 xsave
->i387
.mxcsr
&= mxcsr_feature_mask
;
155 * In case of failure, mark all states as init:
158 fpstate_init(&fpu
->state
);
163 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
166 * FPU tag word conversions.
169 static inline unsigned short twd_i387_to_fxsr(unsigned short twd
)
171 unsigned int tmp
; /* to avoid 16 bit prefixes in the code */
173 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
175 tmp
= (tmp
| (tmp
>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
176 /* and move the valid bits to the lower byte. */
177 tmp
= (tmp
| (tmp
>> 1)) & 0x3333; /* 00VV00VV00VV00VV */
178 tmp
= (tmp
| (tmp
>> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
179 tmp
= (tmp
| (tmp
>> 4)) & 0x00ff; /* 00000000VVVVVVVV */
184 #define FPREG_ADDR(f, n) ((void *)&(f)->st_space + (n) * 16)
185 #define FP_EXP_TAG_VALID 0
186 #define FP_EXP_TAG_ZERO 1
187 #define FP_EXP_TAG_SPECIAL 2
188 #define FP_EXP_TAG_EMPTY 3
190 static inline u32
twd_fxsr_to_i387(struct fxregs_state
*fxsave
)
193 u32 tos
= (fxsave
->swd
>> 11) & 7;
194 u32 twd
= (unsigned long) fxsave
->twd
;
196 u32 ret
= 0xffff0000u
;
199 for (i
= 0; i
< 8; i
++, twd
>>= 1) {
201 st
= FPREG_ADDR(fxsave
, (i
- tos
) & 7);
203 switch (st
->exponent
& 0x7fff) {
205 tag
= FP_EXP_TAG_SPECIAL
;
208 if (!st
->significand
[0] &&
209 !st
->significand
[1] &&
210 !st
->significand
[2] &&
212 tag
= FP_EXP_TAG_ZERO
;
214 tag
= FP_EXP_TAG_SPECIAL
;
217 if (st
->significand
[3] & 0x8000)
218 tag
= FP_EXP_TAG_VALID
;
220 tag
= FP_EXP_TAG_SPECIAL
;
224 tag
= FP_EXP_TAG_EMPTY
;
226 ret
|= tag
<< (2 * i
);
232 * FXSR floating point environment conversions.
236 convert_from_fxsr(struct user_i387_ia32_struct
*env
, struct task_struct
*tsk
)
238 struct fxregs_state
*fxsave
= &tsk
->thread
.fpu
.state
.fxsave
;
239 struct _fpreg
*to
= (struct _fpreg
*) &env
->st_space
[0];
240 struct _fpxreg
*from
= (struct _fpxreg
*) &fxsave
->st_space
[0];
243 env
->cwd
= fxsave
->cwd
| 0xffff0000u
;
244 env
->swd
= fxsave
->swd
| 0xffff0000u
;
245 env
->twd
= twd_fxsr_to_i387(fxsave
);
248 env
->fip
= fxsave
->rip
;
249 env
->foo
= fxsave
->rdp
;
251 * should be actually ds/cs at fpu exception time, but
252 * that information is not available in 64bit mode.
254 env
->fcs
= task_pt_regs(tsk
)->cs
;
255 if (tsk
== current
) {
256 savesegment(ds
, env
->fos
);
258 env
->fos
= tsk
->thread
.ds
;
260 env
->fos
|= 0xffff0000;
262 env
->fip
= fxsave
->fip
;
263 env
->fcs
= (u16
) fxsave
->fcs
| ((u32
) fxsave
->fop
<< 16);
264 env
->foo
= fxsave
->foo
;
265 env
->fos
= fxsave
->fos
;
268 for (i
= 0; i
< 8; ++i
)
269 memcpy(&to
[i
], &from
[i
], sizeof(to
[0]));
272 void convert_to_fxsr(struct task_struct
*tsk
,
273 const struct user_i387_ia32_struct
*env
)
276 struct fxregs_state
*fxsave
= &tsk
->thread
.fpu
.state
.fxsave
;
277 struct _fpreg
*from
= (struct _fpreg
*) &env
->st_space
[0];
278 struct _fpxreg
*to
= (struct _fpxreg
*) &fxsave
->st_space
[0];
281 fxsave
->cwd
= env
->cwd
;
282 fxsave
->swd
= env
->swd
;
283 fxsave
->twd
= twd_i387_to_fxsr(env
->twd
);
284 fxsave
->fop
= (u16
) ((u32
) env
->fcs
>> 16);
286 fxsave
->rip
= env
->fip
;
287 fxsave
->rdp
= env
->foo
;
288 /* cs and ds ignored */
290 fxsave
->fip
= env
->fip
;
291 fxsave
->fcs
= (env
->fcs
& 0xffff);
292 fxsave
->foo
= env
->foo
;
293 fxsave
->fos
= env
->fos
;
296 for (i
= 0; i
< 8; ++i
)
297 memcpy(&to
[i
], &from
[i
], sizeof(from
[0]));
300 int fpregs_get(struct task_struct
*target
, const struct user_regset
*regset
,
301 unsigned int pos
, unsigned int count
,
302 void *kbuf
, void __user
*ubuf
)
304 struct fpu
*fpu
= &target
->thread
.fpu
;
305 struct user_i387_ia32_struct env
;
307 fpu__prepare_read(fpu
);
309 if (!boot_cpu_has(X86_FEATURE_FPU
))
310 return fpregs_soft_get(target
, regset
, pos
, count
, kbuf
, ubuf
);
312 if (!boot_cpu_has(X86_FEATURE_FXSR
))
313 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
314 &fpu
->state
.fsave
, 0,
317 fpstate_sanitize_xstate(fpu
);
319 if (kbuf
&& pos
== 0 && count
== sizeof(env
)) {
320 convert_from_fxsr(kbuf
, target
);
324 convert_from_fxsr(&env
, target
);
326 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, &env
, 0, -1);
329 int fpregs_set(struct task_struct
*target
, const struct user_regset
*regset
,
330 unsigned int pos
, unsigned int count
,
331 const void *kbuf
, const void __user
*ubuf
)
333 struct fpu
*fpu
= &target
->thread
.fpu
;
334 struct user_i387_ia32_struct env
;
337 fpu__prepare_write(fpu
);
338 fpstate_sanitize_xstate(fpu
);
340 if (!boot_cpu_has(X86_FEATURE_FPU
))
341 return fpregs_soft_set(target
, regset
, pos
, count
, kbuf
, ubuf
);
343 if (!boot_cpu_has(X86_FEATURE_FXSR
))
344 return user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
345 &fpu
->state
.fsave
, 0,
348 if (pos
> 0 || count
< sizeof(env
))
349 convert_from_fxsr(&env
, target
);
351 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, &env
, 0, -1);
353 convert_to_fxsr(target
, &env
);
356 * update the header bit in the xsave header, indicating the
359 if (boot_cpu_has(X86_FEATURE_XSAVE
))
360 fpu
->state
.xsave
.header
.xfeatures
|= XFEATURE_MASK_FP
;
365 * FPU state for core dumps.
366 * This is only used for a.out dumps now.
367 * It is declared generically using elf_fpregset_t (which is
368 * struct user_i387_struct) but is in fact only used for 32-bit
369 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
371 int dump_fpu(struct pt_regs
*regs
, struct user_i387_struct
*ufpu
)
373 struct task_struct
*tsk
= current
;
374 struct fpu
*fpu
= &tsk
->thread
.fpu
;
377 fpvalid
= fpu
->initialized
;
379 fpvalid
= !fpregs_get(tsk
, NULL
,
380 0, sizeof(struct user_i387_ia32_struct
),
385 EXPORT_SYMBOL(dump_fpu
);
387 #endif /* CONFIG_X86_32 || CONFIG_IA32_EMULATION */