• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * FPU register's regset abstraction, for ptrace, core dumps, etc.
4  */
5 #include <linux/sched/task_stack.h>
6 #include <linux/vmalloc.h>
7 
8 #include <asm/fpu/internal.h>
9 #include <asm/fpu/signal.h>
10 #include <asm/fpu/regset.h>
11 #include <asm/fpu/xstate.h>
12 
13 /*
14  * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
15  * as the "regset->n" for the xstate regset will be updated based on the feature
16  * capabilities supported by the xsave.
17  */
regset_fpregs_active(struct task_struct * target,const struct user_regset * regset)18 int regset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
19 {
20 	return regset->n;
21 }
22 
regset_xregset_fpregs_active(struct task_struct * target,const struct user_regset * regset)23 int regset_xregset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
24 {
25 	if (boot_cpu_has(X86_FEATURE_FXSR))
26 		return regset->n;
27 	else
28 		return 0;
29 }
30 
31 /*
32  * The regset get() functions are invoked from:
33  *
34  *   - coredump to dump the current task's fpstate. If the current task
35  *     owns the FPU then the memory state has to be synchronized and the
36  *     FPU register state preserved. Otherwise fpstate is already in sync.
37  *
38  *   - ptrace to dump fpstate of a stopped task, in which case the registers
39  *     have already been saved to fpstate on context switch.
40  */
sync_fpstate(struct fpu * fpu)41 static void sync_fpstate(struct fpu *fpu)
42 {
43 	if (fpu == &current->thread.fpu)
44 		fpu_sync_fpstate(fpu);
45 }
46 
47 /*
48  * Invalidate cached FPU registers before modifying the stopped target
49  * task's fpstate.
50  *
51  * This forces the target task on resume to restore the FPU registers from
52  * modified fpstate. Otherwise the task might skip the restore and operate
53  * with the cached FPU registers which discards the modifications.
54  */
fpu_force_restore(struct fpu * fpu)55 static void fpu_force_restore(struct fpu *fpu)
56 {
57 	/*
58 	 * Only stopped child tasks can be used to modify the FPU
59 	 * state in the fpstate buffer:
60 	 */
61 	WARN_ON_FPU(fpu == &current->thread.fpu);
62 
63 	__fpu_invalidate_fpregs_state(fpu);
64 }
65 
xfpregs_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)66 int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
67 		struct membuf to)
68 {
69 	struct fpu *fpu = &target->thread.fpu;
70 
71 	if (!cpu_feature_enabled(X86_FEATURE_FXSR))
72 		return -ENODEV;
73 
74 	sync_fpstate(fpu);
75 
76 	if (!use_xsave()) {
77 		return membuf_write(&to, &fpu->state.fxsave,
78 				    sizeof(fpu->state.fxsave));
79 	}
80 
81 	copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_FX);
82 	return 0;
83 }
84 
xfpregs_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)85 int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
86 		unsigned int pos, unsigned int count,
87 		const void *kbuf, const void __user *ubuf)
88 {
89 	struct fpu *fpu = &target->thread.fpu;
90 	struct fxregs_state newstate;
91 	int ret;
92 
93 	if (!cpu_feature_enabled(X86_FEATURE_FXSR))
94 		return -ENODEV;
95 
96 	/* No funny business with partial or oversized writes is permitted. */
97 	if (pos != 0 || count != sizeof(newstate))
98 		return -EINVAL;
99 
100 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
101 	if (ret)
102 		return ret;
103 
104 	/* Do not allow an invalid MXCSR value. */
105 	if (newstate.mxcsr & ~mxcsr_feature_mask)
106 		return -EINVAL;
107 
108 	fpu_force_restore(fpu);
109 
110 	/* Copy the state  */
111 	memcpy(&fpu->state.fxsave, &newstate, sizeof(newstate));
112 
113 	/* Clear xmm8..15 for 32-bit callers */
114 	BUILD_BUG_ON(sizeof(fpu->state.fxsave.xmm_space) != 16 * 16);
115 	if (in_ia32_syscall())
116 		memset(&fpu->state.fxsave.xmm_space[8*4], 0, 8 * 16);
117 
118 	/* Mark FP and SSE as in use when XSAVE is enabled */
119 	if (use_xsave())
120 		fpu->state.xsave.header.xfeatures |= XFEATURE_MASK_FPSSE;
121 
122 	return 0;
123 }
124 
xstateregs_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)125 int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
126 		struct membuf to)
127 {
128 	if (!cpu_feature_enabled(X86_FEATURE_XSAVE))
129 		return -ENODEV;
130 
131 	sync_fpstate(&target->thread.fpu);
132 
133 	copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_XSAVE);
134 	return 0;
135 }
136 
xstateregs_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)137 int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
138 		  unsigned int pos, unsigned int count,
139 		  const void *kbuf, const void __user *ubuf)
140 {
141 	struct fpu *fpu = &target->thread.fpu;
142 	struct xregs_state *tmpbuf = NULL;
143 	int ret;
144 
145 	if (!cpu_feature_enabled(X86_FEATURE_XSAVE))
146 		return -ENODEV;
147 
148 	/*
149 	 * A whole standard-format XSAVE buffer is needed:
150 	 */
151 	if (pos != 0 || count != fpu_user_xstate_size)
152 		return -EFAULT;
153 
154 	if (!kbuf) {
155 		tmpbuf = vmalloc(count);
156 		if (!tmpbuf)
157 			return -ENOMEM;
158 
159 		if (copy_from_user(tmpbuf, ubuf, count)) {
160 			ret = -EFAULT;
161 			goto out;
162 		}
163 	}
164 
165 	fpu_force_restore(fpu);
166 	ret = copy_uabi_from_kernel_to_xstate(&fpu->state.xsave, kbuf ?: tmpbuf, &target->thread.pkru);
167 
168 out:
169 	vfree(tmpbuf);
170 	return ret;
171 }
172 
173 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
174 
175 /*
176  * FPU tag word conversions.
177  */
178 
twd_i387_to_fxsr(unsigned short twd)179 static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
180 {
181 	unsigned int tmp; /* to avoid 16 bit prefixes in the code */
182 
183 	/* Transform each pair of bits into 01 (valid) or 00 (empty) */
184 	tmp = ~twd;
185 	tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
186 	/* and move the valid bits to the lower byte. */
187 	tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
188 	tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
189 	tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
190 
191 	return tmp;
192 }
193 
194 #define FPREG_ADDR(f, n)	((void *)&(f)->st_space + (n) * 16)
195 #define FP_EXP_TAG_VALID	0
196 #define FP_EXP_TAG_ZERO		1
197 #define FP_EXP_TAG_SPECIAL	2
198 #define FP_EXP_TAG_EMPTY	3
199 
twd_fxsr_to_i387(struct fxregs_state * fxsave)200 static inline u32 twd_fxsr_to_i387(struct fxregs_state *fxsave)
201 {
202 	struct _fpxreg *st;
203 	u32 tos = (fxsave->swd >> 11) & 7;
204 	u32 twd = (unsigned long) fxsave->twd;
205 	u32 tag;
206 	u32 ret = 0xffff0000u;
207 	int i;
208 
209 	for (i = 0; i < 8; i++, twd >>= 1) {
210 		if (twd & 0x1) {
211 			st = FPREG_ADDR(fxsave, (i - tos) & 7);
212 
213 			switch (st->exponent & 0x7fff) {
214 			case 0x7fff:
215 				tag = FP_EXP_TAG_SPECIAL;
216 				break;
217 			case 0x0000:
218 				if (!st->significand[0] &&
219 				    !st->significand[1] &&
220 				    !st->significand[2] &&
221 				    !st->significand[3])
222 					tag = FP_EXP_TAG_ZERO;
223 				else
224 					tag = FP_EXP_TAG_SPECIAL;
225 				break;
226 			default:
227 				if (st->significand[3] & 0x8000)
228 					tag = FP_EXP_TAG_VALID;
229 				else
230 					tag = FP_EXP_TAG_SPECIAL;
231 				break;
232 			}
233 		} else {
234 			tag = FP_EXP_TAG_EMPTY;
235 		}
236 		ret |= tag << (2 * i);
237 	}
238 	return ret;
239 }
240 
241 /*
242  * FXSR floating point environment conversions.
243  */
244 
__convert_from_fxsr(struct user_i387_ia32_struct * env,struct task_struct * tsk,struct fxregs_state * fxsave)245 static void __convert_from_fxsr(struct user_i387_ia32_struct *env,
246 				struct task_struct *tsk,
247 				struct fxregs_state *fxsave)
248 {
249 	struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
250 	struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
251 	int i;
252 
253 	env->cwd = fxsave->cwd | 0xffff0000u;
254 	env->swd = fxsave->swd | 0xffff0000u;
255 	env->twd = twd_fxsr_to_i387(fxsave);
256 
257 #ifdef CONFIG_X86_64
258 	env->fip = fxsave->rip;
259 	env->foo = fxsave->rdp;
260 	/*
261 	 * should be actually ds/cs at fpu exception time, but
262 	 * that information is not available in 64bit mode.
263 	 */
264 	env->fcs = task_pt_regs(tsk)->cs;
265 	if (tsk == current) {
266 		savesegment(ds, env->fos);
267 	} else {
268 		env->fos = tsk->thread.ds;
269 	}
270 	env->fos |= 0xffff0000;
271 #else
272 	env->fip = fxsave->fip;
273 	env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
274 	env->foo = fxsave->foo;
275 	env->fos = fxsave->fos;
276 #endif
277 
278 	for (i = 0; i < 8; ++i)
279 		memcpy(&to[i], &from[i], sizeof(to[0]));
280 }
281 
282 void
convert_from_fxsr(struct user_i387_ia32_struct * env,struct task_struct * tsk)283 convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
284 {
285 	__convert_from_fxsr(env, tsk, &tsk->thread.fpu.state.fxsave);
286 }
287 
convert_to_fxsr(struct fxregs_state * fxsave,const struct user_i387_ia32_struct * env)288 void convert_to_fxsr(struct fxregs_state *fxsave,
289 		     const struct user_i387_ia32_struct *env)
290 
291 {
292 	struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
293 	struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
294 	int i;
295 
296 	fxsave->cwd = env->cwd;
297 	fxsave->swd = env->swd;
298 	fxsave->twd = twd_i387_to_fxsr(env->twd);
299 	fxsave->fop = (u16) ((u32) env->fcs >> 16);
300 #ifdef CONFIG_X86_64
301 	fxsave->rip = env->fip;
302 	fxsave->rdp = env->foo;
303 	/* cs and ds ignored */
304 #else
305 	fxsave->fip = env->fip;
306 	fxsave->fcs = (env->fcs & 0xffff);
307 	fxsave->foo = env->foo;
308 	fxsave->fos = env->fos;
309 #endif
310 
311 	for (i = 0; i < 8; ++i)
312 		memcpy(&to[i], &from[i], sizeof(from[0]));
313 }
314 
fpregs_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)315 int fpregs_get(struct task_struct *target, const struct user_regset *regset,
316 	       struct membuf to)
317 {
318 	struct fpu *fpu = &target->thread.fpu;
319 	struct user_i387_ia32_struct env;
320 	struct fxregs_state fxsave, *fx;
321 
322 	sync_fpstate(fpu);
323 
324 	if (!cpu_feature_enabled(X86_FEATURE_FPU))
325 		return fpregs_soft_get(target, regset, to);
326 
327 	if (!cpu_feature_enabled(X86_FEATURE_FXSR)) {
328 		return membuf_write(&to, &fpu->state.fsave,
329 				    sizeof(struct fregs_state));
330 	}
331 
332 	if (use_xsave()) {
333 		struct membuf mb = { .p = &fxsave, .left = sizeof(fxsave) };
334 
335 		/* Handle init state optimized xstate correctly */
336 		copy_xstate_to_uabi_buf(mb, target, XSTATE_COPY_FP);
337 		fx = &fxsave;
338 	} else {
339 		fx = &fpu->state.fxsave;
340 	}
341 
342 	__convert_from_fxsr(&env, target, fx);
343 	return membuf_write(&to, &env, sizeof(env));
344 }
345 
fpregs_set(struct task_struct * target,const struct user_regset * regset,unsigned int pos,unsigned int count,const void * kbuf,const void __user * ubuf)346 int fpregs_set(struct task_struct *target, const struct user_regset *regset,
347 	       unsigned int pos, unsigned int count,
348 	       const void *kbuf, const void __user *ubuf)
349 {
350 	struct fpu *fpu = &target->thread.fpu;
351 	struct user_i387_ia32_struct env;
352 	int ret;
353 
354 	/* No funny business with partial or oversized writes is permitted. */
355 	if (pos != 0 || count != sizeof(struct user_i387_ia32_struct))
356 		return -EINVAL;
357 
358 	if (!cpu_feature_enabled(X86_FEATURE_FPU))
359 		return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
360 
361 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
362 	if (ret)
363 		return ret;
364 
365 	fpu_force_restore(fpu);
366 
367 	if (cpu_feature_enabled(X86_FEATURE_FXSR))
368 		convert_to_fxsr(&fpu->state.fxsave, &env);
369 	else
370 		memcpy(&fpu->state.fsave, &env, sizeof(env));
371 
372 	/*
373 	 * Update the header bit in the xsave header, indicating the
374 	 * presence of FP.
375 	 */
376 	if (cpu_feature_enabled(X86_FEATURE_XSAVE))
377 		fpu->state.xsave.header.xfeatures |= XFEATURE_MASK_FP;
378 
379 	return 0;
380 }
381 
382 #endif	/* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
383