i387.c 13.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 *  Copyright (C) 1994 Linus Torvalds
 *
 *  Pentium III FXSR, SSE support
 *  General FPU state handling cleanups
 *	Gareth Hughes <gareth@valinux.com>, May 2000
 */

#include <linux/sched.h>
10
#include <linux/module.h>
R
Roland McGrath 已提交
11
#include <linux/regset.h>
L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19
#include <asm/processor.h>
#include <asm/i387.h>
#include <asm/math_emu.h>
#include <asm/sigcontext.h>
#include <asm/user.h>
#include <asm/ptrace.h>
#include <asm/uaccess.h>

R
Roland McGrath 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#ifdef CONFIG_X86_64

#include <asm/sigcontext32.h>
#include <asm/user32.h>

#else

#define	save_i387_ia32		save_i387
#define	restore_i387_ia32	restore_i387

#define _fpstate_ia32 		_fpstate
#define user_i387_ia32_struct	user_i387_struct
#define user32_fxsr_struct	user_fxsr_struct

#endif

L
Linus Torvalds 已提交
36 37 38 39 40 41
#ifdef CONFIG_MATH_EMULATION
#define HAVE_HWFP (boot_cpu_data.hard_math)
#else
#define HAVE_HWFP 1
#endif

R
Roland McGrath 已提交
42
unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
L
Linus Torvalds 已提交
43 44 45 46 47 48

void mxcsr_feature_mask_init(void)
{
	unsigned long mask = 0;
	clts();
	if (cpu_has_fxsr) {
49 50 51
		memset(&current->thread.i387.fxsave, 0,
		       sizeof(struct i387_fxsave_struct));
		asm volatile("fxsave %0" : : "m" (current->thread.i387.fxsave));
L
Linus Torvalds 已提交
52
		mask = current->thread.i387.fxsave.mxcsr_mask;
53 54 55
		if (mask == 0)
			mask = 0x0000ffbf;
	}
L
Linus Torvalds 已提交
56 57 58 59
	mxcsr_feature_mask &= mask;
	stts();
}

R
Roland McGrath 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#ifdef CONFIG_X86_64
/*
 * Called at bootup to set up the initial FPU state that is later cloned
 * into all processes.
 */
void __cpuinit fpu_init(void)
{
	unsigned long oldcr0 = read_cr0();
	extern void __bad_fxsave_alignment(void);

	if (offsetof(struct task_struct, thread.i387.fxsave) & 15)
		__bad_fxsave_alignment();
	set_in_cr4(X86_CR4_OSFXSR);
	set_in_cr4(X86_CR4_OSXMMEXCPT);

	write_cr0(oldcr0 & ~((1UL<<3)|(1UL<<2))); /* clear TS and EM */

	mxcsr_feature_mask_init();
	/* clean state in init */
	current_thread_info()->status = 0;
	clear_used_math();
}
#endif	/* CONFIG_X86_64 */

L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91
/*
 * The _current_ task is using the FPU for the first time
 * so initialize it and set the mxcsr to its default
 * value at reset if we support XMM instructions and then
 * remeber the current task has used the FPU.
 */
void init_fpu(struct task_struct *tsk)
{
R
Roland McGrath 已提交
92 93 94 95 96 97
	if (tsk_used_math(tsk)) {
		if (tsk == current)
			unlazy_fpu(tsk);
		return;
	}

L
Linus Torvalds 已提交
98
	if (cpu_has_fxsr) {
99 100
		memset(&tsk->thread.i387.fxsave, 0,
		       sizeof(struct i387_fxsave_struct));
L
Linus Torvalds 已提交
101 102
		tsk->thread.i387.fxsave.cwd = 0x37f;
		if (cpu_has_xmm)
R
Roland McGrath 已提交
103
			tsk->thread.i387.fxsave.mxcsr = MXCSR_DEFAULT;
L
Linus Torvalds 已提交
104
	} else {
105 106
		memset(&tsk->thread.i387.fsave, 0,
		       sizeof(struct i387_fsave_struct));
L
Linus Torvalds 已提交
107 108 109 110 111
		tsk->thread.i387.fsave.cwd = 0xffff037fu;
		tsk->thread.i387.fsave.swd = 0xffff0000u;
		tsk->thread.i387.fsave.twd = 0xffffffffu;
		tsk->thread.i387.fsave.fos = 0xffff0000u;
	}
R
Roland McGrath 已提交
112 113 114
	/*
	 * Only the device not available exception or ptrace can call init_fpu.
	 */
L
Linus Torvalds 已提交
115 116 117
	set_stopped_child_used_math(tsk);
}

R
Roland McGrath 已提交
118 119 120 121
int fpregs_active(struct task_struct *target, const struct user_regset *regset)
{
	return tsk_used_math(target) ? regset->n : 0;
}
L
Linus Torvalds 已提交
122

R
Roland McGrath 已提交
123
int xfpregs_active(struct task_struct *target, const struct user_regset *regset)
L
Linus Torvalds 已提交
124
{
R
Roland McGrath 已提交
125 126
	return (cpu_has_fxsr && tsk_used_math(target)) ? regset->n : 0;
}
L
Linus Torvalds 已提交
127

R
Roland McGrath 已提交
128 129 130 131 132 133 134 135 136 137 138
int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
		unsigned int pos, unsigned int count,
		void *kbuf, void __user *ubuf)
{
	if (!cpu_has_fxsr)
		return -ENODEV;

	unlazy_fpu(target);

	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
				   &target->thread.i387.fxsave, 0, -1);
L
Linus Torvalds 已提交
139
}
R
Roland McGrath 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

int xfpregs_set(struct task_struct *target, const struct user_regset *regset,
		unsigned int pos, unsigned int count,
		const void *kbuf, const void __user *ubuf)
{
	int ret;

	if (!cpu_has_fxsr)
		return -ENODEV;

	unlazy_fpu(target);
	set_stopped_child_used_math(target);

	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 &target->thread.i387.fxsave, 0, -1);

	/*
	 * mxcsr reserved bits must be masked to zero for security reasons.
	 */
	target->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;

	return ret;
}

#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
L
Linus Torvalds 已提交
165 166 167 168 169

/*
 * FPU tag word conversions.
 */

170
static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
L
Linus Torvalds 已提交
171 172
{
	unsigned int tmp; /* to avoid 16 bit prefixes in the code */
173

L
Linus Torvalds 已提交
174
	/* Transform each pair of bits into 01 (valid) or 00 (empty) */
175
	tmp = ~twd;
R
Roland McGrath 已提交
176
	tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
177 178 179 180 181
	/* and move the valid bits to the lower byte. */
	tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
	tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
	tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
	return tmp;
L
Linus Torvalds 已提交
182 183 184
}

#define FPREG_ADDR(f, n)	((void *)&(f)->st_space + (n) * 16);
R
Roland McGrath 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
#define FP_EXP_TAG_VALID	0
#define FP_EXP_TAG_ZERO		1
#define FP_EXP_TAG_SPECIAL	2
#define FP_EXP_TAG_EMPTY	3

static inline u32 twd_fxsr_to_i387(struct i387_fxsave_struct *fxsave)
{
	struct _fpxreg *st;
	u32 tos = (fxsave->swd >> 11) & 7;
	u32 twd = (unsigned long) fxsave->twd;
	u32 tag;
	u32 ret = 0xffff0000u;
	int i;
L
Linus Torvalds 已提交
198

R
Roland McGrath 已提交
199
	for (i = 0; i < 8; i++, twd >>= 1) {
200 201
		if (twd & 0x1) {
			st = FPREG_ADDR(fxsave, (i - tos) & 7);
L
Linus Torvalds 已提交
202

203
			switch (st->exponent & 0x7fff) {
L
Linus Torvalds 已提交
204
			case 0x7fff:
R
Roland McGrath 已提交
205
				tag = FP_EXP_TAG_SPECIAL;
L
Linus Torvalds 已提交
206 207
				break;
			case 0x0000:
208 209 210
				if (!st->significand[0] &&
				    !st->significand[1] &&
				    !st->significand[2] &&
R
Roland McGrath 已提交
211 212 213 214
				    !st->significand[3])
					tag = FP_EXP_TAG_ZERO;
				else
					tag = FP_EXP_TAG_SPECIAL;
L
Linus Torvalds 已提交
215 216
				break;
			default:
R
Roland McGrath 已提交
217 218 219 220
				if (st->significand[3] & 0x8000)
					tag = FP_EXP_TAG_VALID;
				else
					tag = FP_EXP_TAG_SPECIAL;
L
Linus Torvalds 已提交
221 222 223
				break;
			}
		} else {
R
Roland McGrath 已提交
224
			tag = FP_EXP_TAG_EMPTY;
L
Linus Torvalds 已提交
225
		}
R
Roland McGrath 已提交
226
		ret |= tag << (2 * i);
L
Linus Torvalds 已提交
227 228 229 230 231
	}
	return ret;
}

/*
R
Roland McGrath 已提交
232
 * FXSR floating point environment conversions.
L
Linus Torvalds 已提交
233 234
 */

R
Roland McGrath 已提交
235 236
static void convert_from_fxsr(struct user_i387_ia32_struct *env,
			      struct task_struct *tsk)
L
Linus Torvalds 已提交
237
{
R
Roland McGrath 已提交
238 239 240 241
	struct i387_fxsave_struct *fxsave = &tsk->thread.i387.fxsave;
	struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
	struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
	int i;
L
Linus Torvalds 已提交
242

R
Roland McGrath 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256
	env->cwd = fxsave->cwd | 0xffff0000u;
	env->swd = fxsave->swd | 0xffff0000u;
	env->twd = twd_fxsr_to_i387(fxsave);

#ifdef CONFIG_X86_64
	env->fip = fxsave->rip;
	env->foo = fxsave->rdp;
	if (tsk == current) {
		/*
		 * should be actually ds/cs at fpu exception time, but
		 * that information is not available in 64bit mode.
		 */
		asm("mov %%ds,%0" : "=r" (env->fos));
		asm("mov %%cs,%0" : "=r" (env->fcs));
L
Linus Torvalds 已提交
257
	} else {
R
Roland McGrath 已提交
258 259 260
		struct pt_regs *regs = task_pt_regs(tsk);
		env->fos = 0xffff0000 | tsk->thread.ds;
		env->fcs = regs->cs;
L
Linus Torvalds 已提交
261
	}
R
Roland McGrath 已提交
262 263 264 265 266 267
#else
	env->fip = fxsave->fip;
	env->fcs = fxsave->fcs;
	env->foo = fxsave->foo;
	env->fos = fxsave->fos;
#endif
L
Linus Torvalds 已提交
268

R
Roland McGrath 已提交
269 270
	for (i = 0; i < 8; ++i)
		memcpy(&to[i], &from[i], sizeof(to[0]));
L
Linus Torvalds 已提交
271 272
}

R
Roland McGrath 已提交
273 274
static void convert_to_fxsr(struct task_struct *tsk,
			    const struct user_i387_ia32_struct *env)
L
Linus Torvalds 已提交
275 276

{
R
Roland McGrath 已提交
277 278 279 280
	struct i387_fxsave_struct *fxsave = &tsk->thread.i387.fxsave;
	struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
	struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
	int i;
L
Linus Torvalds 已提交
281

R
Roland McGrath 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
	fxsave->cwd = env->cwd;
	fxsave->swd = env->swd;
	fxsave->twd = twd_i387_to_fxsr(env->twd);
	fxsave->fop = (u16) ((u32) env->fcs >> 16);
#ifdef CONFIG_X86_64
	fxsave->rip = env->fip;
	fxsave->rdp = env->foo;
	/* cs and ds ignored */
#else
	fxsave->fip = env->fip;
	fxsave->fcs = (env->fcs & 0xffff);
	fxsave->foo = env->foo;
	fxsave->fos = env->fos;
#endif
L
Linus Torvalds 已提交
296

R
Roland McGrath 已提交
297 298
	for (i = 0; i < 8; ++i)
		memcpy(&to[i], &from[i], sizeof(from[0]));
L
Linus Torvalds 已提交
299 300
}

R
Roland McGrath 已提交
301 302 303
int fpregs_get(struct task_struct *target, const struct user_regset *regset,
	       unsigned int pos, unsigned int count,
	       void *kbuf, void __user *ubuf)
L
Linus Torvalds 已提交
304
{
R
Roland McGrath 已提交
305
	struct user_i387_ia32_struct env;
L
Linus Torvalds 已提交
306

R
Roland McGrath 已提交
307 308
	if (!HAVE_HWFP)
		return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);
L
Linus Torvalds 已提交
309

R
Roland McGrath 已提交
310
	unlazy_fpu(target);
L
Linus Torvalds 已提交
311

R
Roland McGrath 已提交
312 313 314
	if (!cpu_has_fxsr)
		return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
					   &target->thread.i387.fsave, 0, -1);
L
Linus Torvalds 已提交
315

R
Roland McGrath 已提交
316 317 318
	if (kbuf && pos == 0 && count == sizeof(env)) {
		convert_from_fxsr(kbuf, target);
		return 0;
L
Linus Torvalds 已提交
319
	}
R
Roland McGrath 已提交
320 321 322

	convert_from_fxsr(&env, target);
	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
L
Linus Torvalds 已提交
323 324
}

R
Roland McGrath 已提交
325 326 327
int fpregs_set(struct task_struct *target, const struct user_regset *regset,
	       unsigned int pos, unsigned int count,
	       const void *kbuf, const void __user *ubuf)
L
Linus Torvalds 已提交
328
{
R
Roland McGrath 已提交
329 330
	struct user_i387_ia32_struct env;
	int ret;
L
Linus Torvalds 已提交
331

R
Roland McGrath 已提交
332 333
	if (!HAVE_HWFP)
		return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);
L
Linus Torvalds 已提交
334

R
Roland McGrath 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	unlazy_fpu(target);
	set_stopped_child_used_math(target);

	if (!cpu_has_fxsr)
		return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
					  &target->thread.i387.fsave, 0, -1);

	if (pos > 0 || count < sizeof(env))
		convert_from_fxsr(&env, target);

	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
	if (!ret)
		convert_to_fxsr(target, &env);

	return ret;
L
Linus Torvalds 已提交
350 351 352 353 354 355
}

/*
 * Signal frame handlers.
 */

R
Roland McGrath 已提交
356
static inline int save_i387_fsave(struct _fpstate_ia32 __user *buf)
L
Linus Torvalds 已提交
357 358 359
{
	struct task_struct *tsk = current;

360
	unlazy_fpu(tsk);
L
Linus Torvalds 已提交
361
	tsk->thread.i387.fsave.status = tsk->thread.i387.fsave.swd;
362 363
	if (__copy_to_user(buf, &tsk->thread.i387.fsave,
			   sizeof(struct i387_fsave_struct)))
L
Linus Torvalds 已提交
364 365 366 367
		return -1;
	return 1;
}

R
Roland McGrath 已提交
368
static int save_i387_fxsave(struct _fpstate_ia32 __user *buf)
L
Linus Torvalds 已提交
369 370
{
	struct task_struct *tsk = current;
R
Roland McGrath 已提交
371
	struct user_i387_ia32_struct env;
L
Linus Torvalds 已提交
372 373
	int err = 0;

374
	unlazy_fpu(tsk);
L
Linus Torvalds 已提交
375

R
Roland McGrath 已提交
376 377
	convert_from_fxsr(&env, tsk);
	if (__copy_to_user(buf, &env, sizeof(env)))
L
Linus Torvalds 已提交
378 379
		return -1;

380 381 382
	err |= __put_user(tsk->thread.i387.fxsave.swd, &buf->status);
	err |= __put_user(X86_FXSR_MAGIC, &buf->magic);
	if (err)
L
Linus Torvalds 已提交
383 384
		return -1;

385 386
	if (__copy_to_user(&buf->_fxsr_env[0], &tsk->thread.i387.fxsave,
			   sizeof(struct i387_fxsave_struct)))
L
Linus Torvalds 已提交
387 388 389 390
		return -1;
	return 1;
}

R
Roland McGrath 已提交
391
int save_i387_ia32(struct _fpstate_ia32 __user *buf)
L
Linus Torvalds 已提交
392
{
393
	if (!used_math())
L
Linus Torvalds 已提交
394 395 396 397 398 399 400
		return 0;

	/* This will cause a "finit" to be triggered by the next
	 * attempted FPU operation by the 'current' process.
	 */
	clear_used_math();

401 402 403
	if (HAVE_HWFP) {
		if (cpu_has_fxsr) {
			return save_i387_fxsave(buf);
L
Linus Torvalds 已提交
404
		} else {
405
			return save_i387_fsave(buf);
L
Linus Torvalds 已提交
406 407
		}
	} else {
R
Roland McGrath 已提交
408 409 410
		return fpregs_soft_get(current, NULL,
				       0, sizeof(struct user_i387_ia32_struct),
				       NULL, buf) ? -1 : 1;
L
Linus Torvalds 已提交
411 412 413
	}
}

R
Roland McGrath 已提交
414
static inline int restore_i387_fsave(struct _fpstate_ia32 __user *buf)
L
Linus Torvalds 已提交
415 416
{
	struct task_struct *tsk = current;
417 418 419
	clear_fpu(tsk);
	return __copy_from_user(&tsk->thread.i387.fsave, buf,
				sizeof(struct i387_fsave_struct));
L
Linus Torvalds 已提交
420 421
}

R
Roland McGrath 已提交
422
static int restore_i387_fxsave(struct _fpstate_ia32 __user *buf)
L
Linus Torvalds 已提交
423 424 425
{
	int err;
	struct task_struct *tsk = current;
R
Roland McGrath 已提交
426
	struct user_i387_ia32_struct env;
427 428 429
	clear_fpu(tsk);
	err = __copy_from_user(&tsk->thread.i387.fxsave, &buf->_fxsr_env[0],
			       sizeof(struct i387_fxsave_struct));
L
Linus Torvalds 已提交
430 431
	/* mxcsr reserved bits must be masked to zero for security reasons */
	tsk->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
R
Roland McGrath 已提交
432 433 434 435
	if (err || __copy_from_user(&env, buf, sizeof(env)))
		return 1;
	convert_to_fxsr(tsk, &env);
	return 0;
L
Linus Torvalds 已提交
436 437
}

R
Roland McGrath 已提交
438
int restore_i387_ia32(struct _fpstate_ia32 __user *buf)
L
Linus Torvalds 已提交
439 440 441
{
	int err;

442 443 444
	if (HAVE_HWFP) {
		if (cpu_has_fxsr) {
			err = restore_i387_fxsave(buf);
L
Linus Torvalds 已提交
445
		} else {
446
			err = restore_i387_fsave(buf);
L
Linus Torvalds 已提交
447 448
		}
	} else {
R
Roland McGrath 已提交
449 450 451
		err = fpregs_soft_set(current, NULL,
				      0, sizeof(struct user_i387_ia32_struct),
				      NULL, buf) != 0;
L
Linus Torvalds 已提交
452 453 454 455 456
	}
	set_used_math();
	return err;
}

R
Roland McGrath 已提交
457
#endif	/* CONFIG_X86_32 || CONFIG_IA32_EMULATION */
L
Linus Torvalds 已提交
458

R
Roland McGrath 已提交
459
#ifdef CONFIG_X86_64
L
Linus Torvalds 已提交
460

461
int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
L
Linus Torvalds 已提交
462
{
R
Roland McGrath 已提交
463
	return xfpregs_get(tsk, NULL, 0, sizeof(*buf), NULL, buf);
L
Linus Torvalds 已提交
464 465
}

R
Roland McGrath 已提交
466
int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
L
Linus Torvalds 已提交
467
{
R
Roland McGrath 已提交
468
	return xfpregs_set(tsk, NULL, 0, sizeof(*buf), NULL, buf);
L
Linus Torvalds 已提交
469 470
}

R
Roland McGrath 已提交
471 472 473
#else

int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *tsk)
L
Linus Torvalds 已提交
474
{
R
Roland McGrath 已提交
475
	return fpregs_get(tsk, NULL, 0, sizeof(*buf), NULL, buf);
L
Linus Torvalds 已提交
476 477
}

478
int set_fpregs(struct task_struct *tsk, struct user_i387_struct __user *buf)
L
Linus Torvalds 已提交
479
{
R
Roland McGrath 已提交
480
	return fpregs_set(tsk, NULL, 0, sizeof(*buf), NULL, buf);
L
Linus Torvalds 已提交
481 482
}

483
int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *tsk)
L
Linus Torvalds 已提交
484
{
R
Roland McGrath 已提交
485
	return xfpregs_get(tsk, NULL, 0, sizeof(*buf), NULL, buf);
L
Linus Torvalds 已提交
486 487
}

488
int set_fpxregs(struct task_struct *tsk, struct user_fxsr_struct __user *buf)
L
Linus Torvalds 已提交
489
{
R
Roland McGrath 已提交
490
	return xfpregs_get(tsk, NULL, 0, sizeof(*buf), NULL, buf);
L
Linus Torvalds 已提交
491 492
}

R
Roland McGrath 已提交
493 494
#endif

L
Linus Torvalds 已提交
495 496 497 498
/*
 * FPU state for core dumps.
 */

499 500
static inline void copy_fpu_fsave(struct task_struct *tsk,
				  struct user_i387_struct *fpu)
L
Linus Torvalds 已提交
501
{
502 503
	memcpy(fpu, &tsk->thread.i387.fsave,
	       sizeof(struct user_i387_struct));
L
Linus Torvalds 已提交
504 505
}

506 507
static inline void copy_fpu_fxsave(struct task_struct *tsk,
				   struct user_i387_struct *fpu)
L
Linus Torvalds 已提交
508 509 510 511 512
{
	unsigned short *to;
	unsigned short *from;
	int i;

513
	memcpy(fpu, &tsk->thread.i387.fxsave, 7 * sizeof(long));
L
Linus Torvalds 已提交
514 515 516

	to = (unsigned short *)&fpu->st_space[0];
	from = (unsigned short *)&tsk->thread.i387.fxsave.st_space[0];
517 518
	for (i = 0; i < 8; i++, to += 5, from += 8)
		memcpy(to, from, 5 * sizeof(unsigned short));
L
Linus Torvalds 已提交
519 520
}

521
int dump_fpu(struct pt_regs *regs, struct user_i387_struct *fpu)
L
Linus Torvalds 已提交
522 523 524 525 526
{
	int fpvalid;
	struct task_struct *tsk = current;

	fpvalid = !!used_math();
527 528 529 530
	if (fpvalid) {
		unlazy_fpu(tsk);
		if (cpu_has_fxsr) {
			copy_fpu_fxsave(tsk, fpu);
L
Linus Torvalds 已提交
531
		} else {
532
			copy_fpu_fsave(tsk, fpu);
L
Linus Torvalds 已提交
533 534 535 536 537
		}
	}

	return fpvalid;
}
538
EXPORT_SYMBOL(dump_fpu);
L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

int dump_task_fpu(struct task_struct *tsk, struct user_i387_struct *fpu)
{
	int fpvalid = !!tsk_used_math(tsk);

	if (fpvalid) {
		if (tsk == current)
			unlazy_fpu(tsk);
		if (cpu_has_fxsr)
			copy_fpu_fxsave(tsk, fpu);
		else
			copy_fpu_fsave(tsk, fpu);
	}
	return fpvalid;
}

555
int dump_task_extended_fpu(struct task_struct *tsk,
R
Roland McGrath 已提交
556
			   struct user32_fxsr_struct *fpu)
L
Linus Torvalds 已提交
557 558 559 560 561 562 563 564 565 566
{
	int fpvalid = tsk_used_math(tsk) && cpu_has_fxsr;

	if (fpvalid) {
		if (tsk == current)
		       unlazy_fpu(tsk);
		memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(*fpu));
	}
	return fpvalid;
}