core.c 17.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *  Copyright (C) 1994 Linus Torvalds
 *
 *  Pentium III FXSR, SSE support
 *  General FPU state handling cleanups
 *	Gareth Hughes <gareth@valinux.com>, May 2000
 */
8
#include <asm/fpu/internal.h>
9
#include <asm/fpu/regset.h>
10 11
#include <asm/fpu/signal.h>

12
#include <linux/hardirq.h>
L
Linus Torvalds 已提交
13

I
Ingo Molnar 已提交
14 15 16 17 18 19 20 21 22 23 24
/*
 * Track whether the kernel is using the FPU state
 * currently.
 *
 * This flag is used:
 *
 *   - by IRQ context code to potentially use the FPU
 *     if it's unused.
 *
 *   - to debug kernel_fpu_begin()/end() correctness
 */
25 26
static DEFINE_PER_CPU(bool, in_kernel_fpu);

27
/*
28
 * Track which context is using the FPU on the CPU:
29
 */
30
DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
31

32
static void kernel_fpu_disable(void)
33 34 35 36 37
{
	WARN_ON(this_cpu_read(in_kernel_fpu));
	this_cpu_write(in_kernel_fpu, true);
}

38
static void kernel_fpu_enable(void)
39
{
40
	WARN_ON_ONCE(!this_cpu_read(in_kernel_fpu));
41 42 43
	this_cpu_write(in_kernel_fpu, false);
}

I
Ingo Molnar 已提交
44 45 46 47 48
static bool kernel_fpu_disabled(void)
{
	return this_cpu_read(in_kernel_fpu);
}

49 50 51
/*
 * Were we in an interrupt that interrupted kernel mode?
 *
52
 * On others, we can do a kernel_fpu_begin/end() pair *ONLY* if that
53 54 55 56
 * pair does nothing at all: the thread must not have fpu (so
 * that we don't try to save the FPU state), and TS must
 * be set (so that the clts/stts pair does nothing that is
 * visible in the interrupted kernel thread).
57
 *
58 59
 * Except for the eagerfpu case when we return true; in the likely case
 * the thread has FPU but we are not going to set/clear TS.
60
 */
61
static bool interrupted_kernel_fpu_idle(void)
62
{
I
Ingo Molnar 已提交
63
	if (kernel_fpu_disabled())
64 65
		return false;

66
	if (use_eager_fpu())
67
		return true;
68

69
	return !current->thread.fpu.fpregs_active && (read_cr0() & X86_CR0_TS);
70 71 72 73 74 75 76 77 78 79
}

/*
 * Were we in user mode (or vm86 mode) when we were
 * interrupted?
 *
 * Doing kernel_fpu_begin/end() is ok if we are running
 * in an interrupt context from user mode - we'll just
 * save the FPU state as required.
 */
80
static bool interrupted_user_mode(void)
81 82
{
	struct pt_regs *regs = get_irq_regs();
83
	return regs && user_mode(regs);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

/*
 * Can we use the FPU in kernel mode with the
 * whole "kernel_fpu_begin/end()" sequence?
 *
 * It's always ok in process context (ie "not interrupt")
 * but it is sometimes ok even from an irq.
 */
bool irq_fpu_usable(void)
{
	return !in_interrupt() ||
		interrupted_user_mode() ||
		interrupted_kernel_fpu_idle();
}
EXPORT_SYMBOL(irq_fpu_usable);

101
void __kernel_fpu_begin(void)
102
{
103
	struct fpu *fpu = &current->thread.fpu;
104

105
	kernel_fpu_disable();
106

107
	if (fpu->fpregs_active) {
108
		copy_fpregs_to_fpstate(fpu);
109
	} else {
110
		this_cpu_write(fpu_fpregs_owner_ctx, NULL);
111
		__fpregs_activate_hw();
112 113
	}
}
114
EXPORT_SYMBOL(__kernel_fpu_begin);
115

116
void __kernel_fpu_end(void)
117
{
118
	struct fpu *fpu = &current->thread.fpu;
119

120
	if (fpu->fpregs_active) {
121
		if (WARN_ON(copy_fpstate_to_fpregs(fpu)))
122
			fpu__clear(fpu);
123 124
	} else {
		__fpregs_deactivate_hw();
125
	}
126

127
	kernel_fpu_enable();
128
}
129
EXPORT_SYMBOL(__kernel_fpu_end);
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
void kernel_fpu_begin(void)
{
	preempt_disable();
	WARN_ON_ONCE(!irq_fpu_usable());
	__kernel_fpu_begin();
}
EXPORT_SYMBOL_GPL(kernel_fpu_begin);

void kernel_fpu_end(void)
{
	__kernel_fpu_end();
	preempt_enable();
}
EXPORT_SYMBOL_GPL(kernel_fpu_end);

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
/*
 * CR0::TS save/restore functions:
 */
int irq_ts_save(void)
{
	/*
	 * If in process context and not atomic, we can take a spurious DNA fault.
	 * Otherwise, doing clts() in process context requires disabling preemption
	 * or some heavy lifting like kernel_fpu_begin()
	 */
	if (!in_atomic())
		return 0;

	if (read_cr0() & X86_CR0_TS) {
		clts();
		return 1;
	}

	return 0;
}
EXPORT_SYMBOL_GPL(irq_ts_save);

void irq_ts_restore(int TS_state)
{
	if (TS_state)
		stts();
}
EXPORT_SYMBOL_GPL(irq_ts_restore);

175
/*
I
Ingo Molnar 已提交
176
 * Save the FPU state (mark it for reload if necessary):
177 178
 *
 * This only ever gets called for the current task.
179
 */
180
void fpu__save(struct fpu *fpu)
181
{
182
	WARN_ON(fpu != &current->thread.fpu);
183

184
	preempt_disable();
185
	if (fpu->fpregs_active) {
I
Ingo Molnar 已提交
186
		if (!copy_fpregs_to_fpstate(fpu))
187
			fpregs_deactivate(fpu);
188
	}
189 190
	preempt_enable();
}
191
EXPORT_SYMBOL_GPL(fpu__save);
192

193
void fpstate_init(struct fpu *fpu)
L
Linus Torvalds 已提交
194
{
195
	if (!cpu_has_fpu) {
196
		finit_soft_fpu(&fpu->state.soft);
197
		return;
198 199
	}

200
	memset(&fpu->state, 0, xstate_size);
201

L
Linus Torvalds 已提交
202
	if (cpu_has_fxsr) {
203
		fx_finit(&fpu->state.fxsave);
L
Linus Torvalds 已提交
204
	} else {
205
		struct i387_fsave_struct *fp = &fpu->state.fsave;
206 207 208 209
		fp->cwd = 0xffff037fu;
		fp->swd = 0xffff0000u;
		fp->twd = 0xffffffffu;
		fp->fos = 0xffff0000u;
L
Linus Torvalds 已提交
210
	}
211
}
212
EXPORT_SYMBOL_GPL(fpstate_init);
213

214 215 216 217 218 219 220 221
/*
 * Copy the current task's FPU state to a new task's FPU context.
 *
 * In the 'eager' case we just save to the destination context.
 *
 * In the 'lazy' case we save to the source context, mark the FPU lazy
 * via stts() and copy the source context into the destination context.
 */
222
static void fpu_copy(struct fpu *dst_fpu, struct fpu *src_fpu)
223
{
224
	WARN_ON(src_fpu != &current->thread.fpu);
225

226 227 228 229 230
	/*
	 * Don't let 'init optimized' areas of the XSAVE area
	 * leak into the child task:
	 */
	if (use_eager_fpu())
231
		memset(&dst_fpu->state.xsave, 0, xstate_size);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

	/*
	 * Save current FPU registers directly into the child
	 * FPU context, without any memory-to-memory copying.
	 *
	 * If the FPU context got destroyed in the process (FNSAVE
	 * done on old CPUs) then copy it back into the source
	 * context and mark the current task for lazy restore.
	 *
	 * We have to do all this with preemption disabled,
	 * mostly because of the FNSAVE case, because in that
	 * case we must not allow preemption in the window
	 * between the FNSAVE and us marking the context lazy.
	 *
	 * It shouldn't be an issue as even FNSAVE is plenty
	 * fast in terms of critical section length.
	 */
	preempt_disable();
	if (!copy_fpregs_to_fpstate(dst_fpu)) {
		memcpy(&src_fpu->state, &dst_fpu->state, xstate_size);
		fpregs_deactivate(src_fpu);
253
	}
254
	preempt_enable();
255 256
}

257
int fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu)
I
Ingo Molnar 已提交
258
{
259
	dst_fpu->counter = 0;
260
	dst_fpu->fpregs_active = 0;
261
	dst_fpu->last_cpu = -1;
I
Ingo Molnar 已提交
262

263
	if (src_fpu->fpstate_active)
264
		fpu_copy(dst_fpu, src_fpu);
265

I
Ingo Molnar 已提交
266 267 268
	return 0;
}

269
/*
270 271
 * Activate the current task's in-memory FPU context,
 * if it has not been used before:
272
 */
273
void fpu__activate_curr(struct fpu *fpu)
274
{
275
	WARN_ON_ONCE(fpu != &current->thread.fpu);
276

277 278
	if (!fpu->fpstate_active) {
		fpstate_init(fpu);
279

280 281 282
		/* Safe to do for the current task: */
		fpu->fpstate_active = 1;
	}
283
}
284
EXPORT_SYMBOL_GPL(fpu__activate_curr);
285

286
/*
287 288
 * This function must be called before we modify a stopped child's
 * fpstate.
289 290
 *
 * If the child has not used the FPU before then initialize its
291
 * fpstate.
292 293 294
 *
 * If the child has used the FPU before then unlazy it.
 *
295 296 297
 * [ After this function call, after registers in the fpstate are
 *   modified and the child task has woken up, the child task will
 *   restore the modified FPU state from the modified context. If we
298
 *   didn't clear its lazy status here then the lazy in-registers
299
 *   state pending on its former CPU could be restored, corrupting
300 301 302
 *   the modifications. ]
 *
 * This function is also called before we read a stopped child's
303 304
 * FPU state - to make sure it's initialized if the child has
 * no active FPU state.
305 306 307 308
 *
 * TODO: A future optimization would be to skip the unlazying in
 *       the read-only case, it's not strictly necessary for
 *       read-only access to the context.
309
 */
310
static void fpu__activate_stopped(struct fpu *child_fpu)
311
{
312
	WARN_ON_ONCE(child_fpu == &current->thread.fpu);
313

314
	if (child_fpu->fpstate_active) {
315
		child_fpu->last_cpu = -1;
316 317
	} else {
		fpstate_init(child_fpu);
318

319 320 321
		/* Safe to do for stopped child tasks: */
		child_fpu->fpstate_active = 1;
	}
L
Linus Torvalds 已提交
322 323
}

324
/*
325 326 327 328
 * 'fpu__restore()' is called to copy FPU registers from
 * the FPU fpstate to the live hw registers and to activate
 * access to the hardware registers, so that FPU instructions
 * can be used afterwards.
329
 *
330 331 332
 * Must be called with kernel preemption disabled (for example
 * with local interrupts disabled, as it is in the case of
 * do_device_not_available()).
333
 */
334
void fpu__restore(void)
335 336
{
	struct task_struct *tsk = current;
337
	struct fpu *fpu = &tsk->thread.fpu;
338

339
	fpu__activate_curr(fpu);
340

341
	/* Avoid __kernel_fpu_begin() right after fpregs_activate() */
342
	kernel_fpu_disable();
343
	fpregs_activate(fpu);
344
	if (unlikely(copy_fpstate_to_fpregs(fpu))) {
345
		fpu__clear(fpu);
346 347 348 349 350 351
		force_sig_info(SIGSEGV, SEND_SIG_PRIV, tsk);
	} else {
		tsk->thread.fpu.counter++;
	}
	kernel_fpu_enable();
}
352
EXPORT_SYMBOL_GPL(fpu__restore);
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/*
 * Drops current FPU state: deactivates the fpregs and
 * the fpstate. NOTE: it still leaves previous contents
 * in the fpregs in the eager-FPU case.
 *
 * This function can be used in cases where we know that
 * a state-restore is coming: either an explicit one,
 * or a reschedule.
 */
void fpu__drop(struct fpu *fpu)
{
	preempt_disable();
	fpu->counter = 0;

	if (fpu->fpregs_active) {
		/* Ignore delayed exceptions from user space */
		asm volatile("1: fwait\n"
			     "2:\n"
			     _ASM_EXTABLE(1b, 2b));
		fpregs_deactivate(fpu);
	}

	fpu->fpstate_active = 0;

	preempt_enable();
}

/*
382 383 384 385
 * Clear the FPU state back to init state.
 *
 * Called by sys_execve(), by the signal handler code and by various
 * error paths.
386
 */
387
void fpu__clear(struct fpu *fpu)
388
{
389
	WARN_ON_ONCE(fpu != &current->thread.fpu); /* Almost certainly an anomaly */
390

391 392
	if (!use_eager_fpu()) {
		/* FPU state will be reallocated lazily at the first use. */
393
		fpu__drop(fpu);
394
	} else {
395
		if (!fpu->fpstate_active) {
396
			fpu__activate_curr(fpu);
397 398 399 400 401 402
			user_fpu_begin();
		}
		restore_init_xstate();
	}
}

403
/*
404
 * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
405 406 407
 * as the "regset->n" for the xstate regset will be updated based on the feature
 * capabilites supported by the xsave.
 */
408
int regset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
R
Roland McGrath 已提交
409
{
410 411 412
	struct fpu *target_fpu = &target->thread.fpu;

	return target_fpu->fpstate_active ? regset->n : 0;
R
Roland McGrath 已提交
413
}
L
Linus Torvalds 已提交
414

415
int regset_xregset_fpregs_active(struct task_struct *target, const struct user_regset *regset)
L
Linus Torvalds 已提交
416
{
417 418 419
	struct fpu *target_fpu = &target->thread.fpu;

	return (cpu_has_fxsr && target_fpu->fpstate_active) ? regset->n : 0;
R
Roland McGrath 已提交
420
}
L
Linus Torvalds 已提交
421

R
Roland McGrath 已提交
422 423 424 425
int xfpregs_get(struct task_struct *target, const struct user_regset *regset,
		unsigned int pos, unsigned int count,
		void *kbuf, void __user *ubuf)
{
426
	struct fpu *fpu = &target->thread.fpu;
427

R
Roland McGrath 已提交
428 429 430
	if (!cpu_has_fxsr)
		return -ENODEV;

431
	fpu__activate_stopped(fpu);
432
	fpstate_sanitize_xstate(fpu);
433

R
Roland McGrath 已提交
434
	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
435
				   &fpu->state.fxsave, 0, -1);
L
Linus Torvalds 已提交
436
}
R
Roland McGrath 已提交
437 438 439 440 441

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)
{
442
	struct fpu *fpu = &target->thread.fpu;
R
Roland McGrath 已提交
443 444 445 446 447
	int ret;

	if (!cpu_has_fxsr)
		return -ENODEV;

448
	fpu__activate_stopped(fpu);
449
	fpstate_sanitize_xstate(fpu);
450

R
Roland McGrath 已提交
451
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
452
				 &fpu->state.fxsave, 0, -1);
R
Roland McGrath 已提交
453 454 455 456

	/*
	 * mxcsr reserved bits must be masked to zero for security reasons.
	 */
457
	fpu->state.fxsave.mxcsr &= mxcsr_feature_mask;
R
Roland McGrath 已提交
458

459 460 461 462 463
	/*
	 * update the header bits in the xsave header, indicating the
	 * presence of FP and SSE state.
	 */
	if (cpu_has_xsave)
464
		fpu->state.xsave.header.xfeatures |= XSTATE_FPSSE;
465

R
Roland McGrath 已提交
466 467 468
	return ret;
}

469 470 471 472
int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
		unsigned int pos, unsigned int count,
		void *kbuf, void __user *ubuf)
{
473
	struct fpu *fpu = &target->thread.fpu;
474
	struct xsave_struct *xsave;
475 476 477 478 479
	int ret;

	if (!cpu_has_xsave)
		return -ENODEV;

480
	fpu__activate_stopped(fpu);
481

482
	xsave = &fpu->state.xsave;
483

484
	/*
485 486 487
	 * Copy the 48bytes defined by the software first into the xstate
	 * memory layout in the thread struct, so that we can copy the entire
	 * xstateregs to the user using one user_regset_copyout().
488
	 */
489 490
	memcpy(&xsave->i387.sw_reserved,
		xstate_fx_sw_bytes, sizeof(xstate_fx_sw_bytes));
491
	/*
492
	 * Copy the xstate memory layout.
493
	 */
494
	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
495 496 497 498 499 500 501
	return ret;
}

int xstateregs_set(struct task_struct *target, const struct user_regset *regset,
		  unsigned int pos, unsigned int count,
		  const void *kbuf, const void __user *ubuf)
{
502
	struct fpu *fpu = &target->thread.fpu;
503
	struct xsave_struct *xsave;
504 505 506 507 508
	int ret;

	if (!cpu_has_xsave)
		return -ENODEV;

509
	fpu__activate_stopped(fpu);
510

511
	xsave = &fpu->state.xsave;
512

513
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
514 515 516
	/*
	 * mxcsr reserved bits must be masked to zero for security reasons.
	 */
517
	xsave->i387.mxcsr &= mxcsr_feature_mask;
518
	xsave->header.xfeatures &= xfeatures_mask;
519 520 521
	/*
	 * These bits must be zero.
	 */
522
	memset(&xsave->header.reserved, 0, 48);
I
Ingo Molnar 已提交
523

524 525 526
	return ret;
}

R
Roland McGrath 已提交
527
#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
L
Linus Torvalds 已提交
528 529 530 531 532

/*
 * FPU tag word conversions.
 */

533
static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
L
Linus Torvalds 已提交
534 535
{
	unsigned int tmp; /* to avoid 16 bit prefixes in the code */
536

L
Linus Torvalds 已提交
537
	/* Transform each pair of bits into 01 (valid) or 00 (empty) */
538
	tmp = ~twd;
R
Roland McGrath 已提交
539
	tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
540 541 542 543
	/* 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 */
I
Ingo Molnar 已提交
544

545
	return tmp;
L
Linus Torvalds 已提交
546 547
}

548
#define FPREG_ADDR(f, n)	((void *)&(f)->st_space + (n) * 16)
R
Roland McGrath 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561
#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 已提交
562

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

567
			switch (st->exponent & 0x7fff) {
L
Linus Torvalds 已提交
568
			case 0x7fff:
R
Roland McGrath 已提交
569
				tag = FP_EXP_TAG_SPECIAL;
L
Linus Torvalds 已提交
570 571
				break;
			case 0x0000:
572 573 574
				if (!st->significand[0] &&
				    !st->significand[1] &&
				    !st->significand[2] &&
R
Roland McGrath 已提交
575 576 577 578
				    !st->significand[3])
					tag = FP_EXP_TAG_ZERO;
				else
					tag = FP_EXP_TAG_SPECIAL;
L
Linus Torvalds 已提交
579 580
				break;
			default:
R
Roland McGrath 已提交
581 582 583 584
				if (st->significand[3] & 0x8000)
					tag = FP_EXP_TAG_VALID;
				else
					tag = FP_EXP_TAG_SPECIAL;
L
Linus Torvalds 已提交
585 586 587
				break;
			}
		} else {
R
Roland McGrath 已提交
588
			tag = FP_EXP_TAG_EMPTY;
L
Linus Torvalds 已提交
589
		}
R
Roland McGrath 已提交
590
		ret |= tag << (2 * i);
L
Linus Torvalds 已提交
591 592 593 594 595
	}
	return ret;
}

/*
R
Roland McGrath 已提交
596
 * FXSR floating point environment conversions.
L
Linus Torvalds 已提交
597 598
 */

599
void
I
Ingo Molnar 已提交
600
convert_from_fxsr(struct user_i387_ia32_struct *env, struct task_struct *tsk)
L
Linus Torvalds 已提交
601
{
602
	struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state.fxsave;
R
Roland McGrath 已提交
603 604 605
	struct _fpreg *to = (struct _fpreg *) &env->st_space[0];
	struct _fpxreg *from = (struct _fpxreg *) &fxsave->st_space[0];
	int i;
L
Linus Torvalds 已提交
606

R
Roland McGrath 已提交
607 608 609 610 611 612 613
	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;
614 615 616 617 618
	/*
	 * should be actually ds/cs at fpu exception time, but
	 * that information is not available in 64bit mode.
	 */
	env->fcs = task_pt_regs(tsk)->cs;
R
Roland McGrath 已提交
619
	if (tsk == current) {
620
		savesegment(ds, env->fos);
L
Linus Torvalds 已提交
621
	} else {
622
		env->fos = tsk->thread.ds;
L
Linus Torvalds 已提交
623
	}
624
	env->fos |= 0xffff0000;
R
Roland McGrath 已提交
625 626
#else
	env->fip = fxsave->fip;
J
Jan Beulich 已提交
627
	env->fcs = (u16) fxsave->fcs | ((u32) fxsave->fop << 16);
R
Roland McGrath 已提交
628 629 630
	env->foo = fxsave->foo;
	env->fos = fxsave->fos;
#endif
L
Linus Torvalds 已提交
631

R
Roland McGrath 已提交
632 633
	for (i = 0; i < 8; ++i)
		memcpy(&to[i], &from[i], sizeof(to[0]));
L
Linus Torvalds 已提交
634 635
}

636 637
void convert_to_fxsr(struct task_struct *tsk,
		     const struct user_i387_ia32_struct *env)
L
Linus Torvalds 已提交
638 639

{
640
	struct i387_fxsave_struct *fxsave = &tsk->thread.fpu.state.fxsave;
R
Roland McGrath 已提交
641 642 643
	struct _fpreg *from = (struct _fpreg *) &env->st_space[0];
	struct _fpxreg *to = (struct _fpxreg *) &fxsave->st_space[0];
	int i;
L
Linus Torvalds 已提交
644

R
Roland McGrath 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658
	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 已提交
659

R
Roland McGrath 已提交
660 661
	for (i = 0; i < 8; ++i)
		memcpy(&to[i], &from[i], sizeof(from[0]));
L
Linus Torvalds 已提交
662 663
}

R
Roland McGrath 已提交
664 665 666
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 已提交
667
{
668
	struct fpu *fpu = &target->thread.fpu;
R
Roland McGrath 已提交
669
	struct user_i387_ia32_struct env;
L
Linus Torvalds 已提交
670

671
	fpu__activate_stopped(fpu);
L
Linus Torvalds 已提交
672

673
	if (!static_cpu_has(X86_FEATURE_FPU))
674 675
		return fpregs_soft_get(target, regset, pos, count, kbuf, ubuf);

676
	if (!cpu_has_fxsr)
R
Roland McGrath 已提交
677
		return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
678
					   &fpu->state.fsave, 0,
679
					   -1);
L
Linus Torvalds 已提交
680

681
	fpstate_sanitize_xstate(fpu);
682

R
Roland McGrath 已提交
683 684 685
	if (kbuf && pos == 0 && count == sizeof(env)) {
		convert_from_fxsr(kbuf, target);
		return 0;
L
Linus Torvalds 已提交
686
	}
R
Roland McGrath 已提交
687 688

	convert_from_fxsr(&env, target);
I
Ingo Molnar 已提交
689

R
Roland McGrath 已提交
690
	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &env, 0, -1);
L
Linus Torvalds 已提交
691 692
}

R
Roland McGrath 已提交
693 694 695
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 已提交
696
{
697
	struct fpu *fpu = &target->thread.fpu;
R
Roland McGrath 已提交
698 699
	struct user_i387_ia32_struct env;
	int ret;
L
Linus Torvalds 已提交
700

701
	fpu__activate_stopped(fpu);
702
	fpstate_sanitize_xstate(fpu);
703

704
	if (!static_cpu_has(X86_FEATURE_FPU))
705 706
		return fpregs_soft_set(target, regset, pos, count, kbuf, ubuf);

707
	if (!cpu_has_fxsr)
R
Roland McGrath 已提交
708
		return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
709
					  &fpu->state.fsave, 0,
710
					  -1);
R
Roland McGrath 已提交
711 712 713 714 715 716 717 718

	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);

719 720 721 722 723
	/*
	 * update the header bit in the xsave header, indicating the
	 * presence of FP.
	 */
	if (cpu_has_xsave)
724
		fpu->state.xsave.header.xfeatures |= XSTATE_FP;
R
Roland McGrath 已提交
725
	return ret;
L
Linus Torvalds 已提交
726 727 728 729
}

/*
 * FPU state for core dumps.
R
Roland McGrath 已提交
730 731 732 733
 * This is only used for a.out dumps now.
 * It is declared generically using elf_fpregset_t (which is
 * struct user_i387_struct) but is in fact only used for 32-bit
 * dumps, so on 64-bit it is really struct user_i387_ia32_struct.
L
Linus Torvalds 已提交
734
 */
735
int dump_fpu(struct pt_regs *regs, struct user_i387_struct *ufpu)
L
Linus Torvalds 已提交
736 737
{
	struct task_struct *tsk = current;
738
	struct fpu *fpu = &tsk->thread.fpu;
I
Ingo Molnar 已提交
739
	int fpvalid;
L
Linus Torvalds 已提交
740

741
	fpvalid = fpu->fpstate_active;
R
Roland McGrath 已提交
742 743 744
	if (fpvalid)
		fpvalid = !fpregs_get(tsk, NULL,
				      0, sizeof(struct user_i387_ia32_struct),
745
				      ufpu, NULL);
L
Linus Torvalds 已提交
746 747 748

	return fpvalid;
}
749
EXPORT_SYMBOL(dump_fpu);
L
Linus Torvalds 已提交
750

R
Roland McGrath 已提交
751
#endif	/* CONFIG_X86_32 || CONFIG_IA32_EMULATION */