internal.h 12.3 KB
Newer Older
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
 * x86-64 work by Andi Kleen 2002
 */

10 11
#ifndef _ASM_X86_FPU_INTERNAL_H
#define _ASM_X86_FPU_INTERNAL_H
12

13
#include <linux/compat.h>
14
#include <linux/sched.h>
15
#include <linux/slab.h>
16

17
#include <asm/user.h>
18
#include <asm/fpu/api.h>
19
#include <asm/fpu/xstate.h>
20

21 22
#define	MXCSR_DEFAULT		0x1f80

23
extern unsigned int mxcsr_feature_mask;
24

25
extern union fpregs_state init_fpstate;
26

27
extern void fpu__init_cpu(void);
28 29
extern void fpu__init_system_xstate(void);
extern void fpu__init_cpu_xstate(void);
30
extern void fpu__init_system(struct cpuinfo_x86 *c);
31

32
extern void fpstate_init(union fpregs_state *state);
33
#ifdef CONFIG_MATH_EMULATION
34
extern void fpstate_init_soft(struct swregs_state *soft);
35
#else
36
static inline void fpstate_init_soft(struct swregs_state *soft) {}
37
#endif
38
static inline void fpstate_init_fxstate(struct fxregs_state *fx)
39 40 41 42
{
	fx->cwd = 0x37f;
	fx->mxcsr = MXCSR_DEFAULT;
}
43

44 45
extern int  dump_fpu(struct pt_regs *, struct user_i387_struct *);
extern int  fpu__exception_code(struct fpu *fpu, int trap_nr);
46 47 48 49

/*
 * High level FPU state handling functions:
 */
50 51
extern void fpu__activate_curr(struct fpu *fpu);
extern void fpu__activate_stopped(struct fpu *fpu);
52
extern void fpu__save(struct fpu *fpu);
53
extern void fpu__restore(void);
54
extern int  fpu__restore_sig(void __user *buf, int ia32_frame);
55 56
extern void fpu__drop(struct fpu *fpu);
extern int  fpu__copy(struct fpu *dst_fpu, struct fpu *src_fpu);
57
extern void fpu__clear(struct fpu *fpu);
58

59 60 61
extern void fpu__init_check_bugs(void);
extern void fpu__resume_cpu(void);

62
DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx);
63

64
/*
65
 * Must be run with preemption disabled: this clears the fpu_fpregs_owner_ctx,
66 67 68 69 70 71 72
 * on this CPU.
 *
 * This will disable any lazy FPU state restore of the current FPU state,
 * but if the current thread owns the FPU, it will still be saved by.
 */
static inline void __cpu_disable_lazy_restore(unsigned int cpu)
{
73
	per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
74 75
}

76
static inline int fpu_want_lazy_restore(struct fpu *fpu, unsigned int cpu)
77
{
78
	return fpu == this_cpu_read_stable(fpu_fpregs_owner_ctx) && cpu == fpu->last_cpu;
79 80
}

81 82
#define X87_FSW_ES (1 << 7)	/* Exception Summary */

83 84
static __always_inline __pure bool use_eager_fpu(void)
{
85
	return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
86 87
}

88 89
static __always_inline __pure bool use_xsaveopt(void)
{
90
	return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
91 92 93 94
}

static __always_inline __pure bool use_xsave(void)
{
95
	return static_cpu_has_safe(X86_FEATURE_XSAVE);
96 97 98 99
}

static __always_inline __pure bool use_fxsr(void)
{
100
	return static_cpu_has_safe(X86_FEATURE_FXSR);
101 102
}

103
extern void fpstate_sanitize_xstate(struct fpu *fpu);
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#define user_insn(insn, output, input...)				\
({									\
	int err;							\
	asm volatile(ASM_STAC "\n"					\
		     "1:" #insn "\n\t"					\
		     "2: " ASM_CLAC "\n"				\
		     ".section .fixup,\"ax\"\n"				\
		     "3:  movl $-1,%[err]\n"				\
		     "    jmp  2b\n"					\
		     ".previous\n"					\
		     _ASM_EXTABLE(1b, 3b)				\
		     : [err] "=r" (err), output				\
		     : "0"(0), input);					\
	err;								\
})

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#define check_insn(insn, output, input...)				\
({									\
	int err;							\
	asm volatile("1:" #insn "\n\t"					\
		     "2:\n"						\
		     ".section .fixup,\"ax\"\n"				\
		     "3:  movl $-1,%[err]\n"				\
		     "    jmp  2b\n"					\
		     ".previous\n"					\
		     _ASM_EXTABLE(1b, 3b)				\
		     : [err] "=r" (err), output				\
		     : "0"(0), input);					\
	err;								\
})

136
static inline int copy_fregs_to_user(struct fregs_state __user *fx)
137
{
138
	return user_insn(fnsave %[fx]; fwait,  [fx] "=m" (*fx), "m" (*fx));
139 140
}

141
static inline int copy_fxregs_to_user(struct fxregs_state __user *fx)
142
{
143
	if (config_enabled(CONFIG_X86_32))
144
		return user_insn(fxsave %[fx], [fx] "=m" (*fx), "m" (*fx));
145
	else if (config_enabled(CONFIG_AS_FXSAVEQ))
146
		return user_insn(fxsaveq %[fx], [fx] "=m" (*fx), "m" (*fx));
147

148
	/* See comment in copy_fxregs_to_kernel() below. */
149
	return user_insn(rex64/fxsave (%[fx]), "=m" (*fx), [fx] "R" (fx));
150 151
}

152
static inline int copy_kernel_to_fxregs(struct fxregs_state *fx)
153
{
154 155 156 157
	if (config_enabled(CONFIG_X86_32))
		return check_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
	else if (config_enabled(CONFIG_AS_FXSAVEQ))
		return check_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));
158

159
	/* See comment in copy_fxregs_to_kernel() below. */
160 161
	return check_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
			  "m" (*fx));
162 163
}

164
static inline int copy_user_to_fxregs(struct fxregs_state __user *fx)
165 166 167 168 169 170
{
	if (config_enabled(CONFIG_X86_32))
		return user_insn(fxrstor %[fx], "=m" (*fx), [fx] "m" (*fx));
	else if (config_enabled(CONFIG_AS_FXSAVEQ))
		return user_insn(fxrstorq %[fx], "=m" (*fx), [fx] "m" (*fx));

171
	/* See comment in copy_fxregs_to_kernel() below. */
172 173 174 175
	return user_insn(rex64/fxrstor (%[fx]), "=m" (*fx), [fx] "R" (fx),
			  "m" (*fx));
}

176
static inline int copy_kernel_to_fregs(struct fregs_state *fx)
177
{
178
	return check_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
179 180
}

181
static inline int copy_user_to_fregs(struct fregs_state __user *fx)
182 183
{
	return user_insn(frstor %[fx], "=m" (*fx), [fx] "m" (*fx));
184 185
}

186
static inline void copy_fxregs_to_kernel(struct fpu *fpu)
187
{
188
	if (config_enabled(CONFIG_X86_32))
189
		asm volatile( "fxsave %[fx]" : [fx] "=m" (fpu->state.fxsave));
190
	else if (config_enabled(CONFIG_AS_FXSAVEQ))
191
		asm volatile("fxsaveq %[fx]" : [fx] "=m" (fpu->state.fxsave));
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	else {
		/* Using "rex64; fxsave %0" is broken because, if the memory
		 * operand uses any extended registers for addressing, a second
		 * REX prefix will be generated (to the assembler, rex64
		 * followed by semicolon is a separate instruction), and hence
		 * the 64-bitness is lost.
		 *
		 * Using "fxsaveq %0" would be the ideal choice, but is only
		 * supported starting with gas 2.16.
		 *
		 * Using, as a workaround, the properly prefixed form below
		 * isn't accepted by any binutils version so far released,
		 * complaining that the same type of prefix is used twice if
		 * an extended register is needed for addressing (fix submitted
		 * to mainline 2005-11-21).
		 *
208
		 *  asm volatile("rex64/fxsave %0" : "=m" (fpu->state.fxsave));
209 210 211 212 213 214
		 *
		 * This, however, we can work around by forcing the compiler to
		 * select an addressing mode that doesn't require extended
		 * registers.
		 */
		asm volatile( "rex64/fxsave (%[fx])"
215 216
			     : "=m" (fpu->state.fxsave)
			     : [fx] "R" (&fpu->state.fxsave));
217
	}
218 219 220 221
}

/*
 * These must be called with preempt disabled. Returns
222 223 224 225 226 227
 * 'true' if the FPU state is still intact and we can
 * keep registers active.
 *
 * The legacy FNSAVE instruction cleared all FPU state
 * unconditionally, so registers are essentially destroyed.
 * Modern FPU state can be kept in registers, if there are
228
 * no pending FP exceptions.
229
 */
230
static inline int copy_fpregs_to_fpstate(struct fpu *fpu)
231
{
232
	if (likely(use_xsave())) {
233
		copy_xregs_to_kernel(&fpu->state.xsave);
234 235
		return 1;
	}
236

237
	if (likely(use_fxsr())) {
238
		copy_fxregs_to_kernel(fpu);
239
		return 1;
240 241 242
	}

	/*
243 244
	 * Legacy FPU register saving, FNSAVE always clears FPU registers,
	 * so we have to mark them inactive:
245
	 */
246
	asm volatile("fnsave %[fx]; fwait" : [fx] "=m" (fpu->state.fsave));
247 248

	return 0;
249 250
}

251
static inline int __copy_fpstate_to_fpregs(struct fpu *fpu)
252 253
{
	if (use_xsave())
254
		return copy_kernel_to_xregs(&fpu->state.xsave, -1);
255
	else if (use_fxsr())
256
		return copy_kernel_to_fxregs(&fpu->state.fxsave);
257
	else
258
		return copy_kernel_to_fregs(&fpu->state.fsave);
259 260
}

261
static inline int copy_fpstate_to_fpregs(struct fpu *fpu)
262
{
263 264 265 266 267
	/*
	 * AMD K7/K8 CPUs don't save/restore FDP/FIP/FOP unless an exception is
	 * pending. Clear the x87 state here by setting it to fixed values.
	 * "m" is a random variable that should be in L1.
	 */
268
	if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
269 270 271 272
		asm volatile(
			"fnclex\n\t"
			"emms\n\t"
			"fildl %P[addr]"	/* set F?P to defined value */
273
			: : [addr] "m" (fpu->fpregs_active));
274
	}
275

276
	return __copy_fpstate_to_fpregs(fpu);
277 278
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/*
 * Wrap lazy FPU TS handling in a 'hw fpregs activation/deactivation'
 * idiom, which is then paired with the sw-flag (fpregs_active) later on:
 */

static inline void __fpregs_activate_hw(void)
{
	if (!use_eager_fpu())
		clts();
}

static inline void __fpregs_deactivate_hw(void)
{
	if (!use_eager_fpu())
		stts();
}

/* Must be paired with an 'stts' (fpregs_deactivate_hw()) after! */
297
static inline void __fpregs_deactivate(struct fpu *fpu)
298
{
299
	fpu->fpregs_active = 0;
300
	this_cpu_write(fpu_fpregs_owner_ctx, NULL);
301 302
}

303
/* Must be paired with a 'clts' (fpregs_activate_hw()) before! */
304
static inline void __fpregs_activate(struct fpu *fpu)
305
{
306
	fpu->fpregs_active = 1;
307
	this_cpu_write(fpu_fpregs_owner_ctx, fpu);
308 309
}

310 311 312 313 314 315 316 317 318 319
/*
 * The question "does this thread have fpu access?"
 * is slightly racy, since preemption could come in
 * and revoke it immediately after the test.
 *
 * However, even in that very unlikely scenario,
 * we can just assume we have FPU access - typically
 * to save the FP state - we'll just take a #NM
 * fault and get the FPU access back.
 */
320
static inline int fpregs_active(void)
321 322 323 324
{
	return current->thread.fpu.fpregs_active;
}

325 326 327 328 329 330 331
/*
 * Encapsulate the CR0.TS handling together with the
 * software flag.
 *
 * These generally need preemption protection to work,
 * do try to avoid using these on their own.
 */
332
static inline void fpregs_activate(struct fpu *fpu)
333
{
334
	__fpregs_activate_hw();
335
	__fpregs_activate(fpu);
336 337
}

338
static inline void fpregs_deactivate(struct fpu *fpu)
339
{
340
	__fpregs_deactivate(fpu);
341
	__fpregs_deactivate_hw();
342 343
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/*
 * Definitions for the eXtended Control Register instructions
 */

#define XCR_XFEATURE_ENABLED_MASK	0x00000000

static inline u64 xgetbv(u32 index)
{
	u32 eax, edx;

	asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
		     : "=a" (eax), "=d" (edx)
		     : "c" (index));
	return eax + ((u64)edx << 32);
}

static inline void xsetbv(u32 index, u64 value)
{
	u32 eax = value;
	u32 edx = value >> 32;

	asm volatile(".byte 0x0f,0x01,0xd1" /* xsetbv */
		     : : "a" (eax), "d" (edx), "c" (index));
}

369 370 371 372 373 374 375 376 377 378 379 380 381 382
/*
 * FPU state switching for scheduling.
 *
 * This is a two-stage process:
 *
 *  - switch_fpu_prepare() saves the old state and
 *    sets the new state of the CR0.TS bit. This is
 *    done within the context of the old process.
 *
 *  - switch_fpu_finish() restores the new state as
 *    necessary.
 */
typedef struct { int preload; } fpu_switch_t;

383 384
static inline fpu_switch_t
switch_fpu_prepare(struct fpu *old_fpu, struct fpu *new_fpu, int cpu)
385 386 387
{
	fpu_switch_t fpu;

388 389 390 391
	/*
	 * If the task has used the math, pre-load the FPU on xsave processors
	 * or if the past 5 consecutive context-switches used math.
	 */
392
	fpu.preload = new_fpu->fpstate_active &&
393
		      (use_eager_fpu() || new_fpu->counter > 5);
394

395
	if (old_fpu->fpregs_active) {
396
		if (!copy_fpregs_to_fpstate(old_fpu))
397
			old_fpu->last_cpu = -1;
398
		else
399
			old_fpu->last_cpu = cpu;
400

401
		/* But leave fpu_fpregs_owner_ctx! */
402
		old_fpu->fpregs_active = 0;
403 404 405

		/* Don't change CR0.TS if we just switch! */
		if (fpu.preload) {
406
			new_fpu->counter++;
407
			__fpregs_activate(new_fpu);
408
			prefetch(&new_fpu->state);
409 410 411
		} else {
			__fpregs_deactivate_hw();
		}
412
	} else {
413 414
		old_fpu->counter = 0;
		old_fpu->last_cpu = -1;
415
		if (fpu.preload) {
416
			new_fpu->counter++;
417
			if (fpu_want_lazy_restore(new_fpu, cpu))
418 419
				fpu.preload = 0;
			else
420
				prefetch(&new_fpu->state);
421
			fpregs_activate(new_fpu);
422 423 424 425 426 427 428 429 430 431 432
		}
	}
	return fpu;
}

/*
 * By the time this gets called, we've already cleared CR0.TS and
 * given the process the FPU if we are going to preload the FPU
 * state - all we need to do is to conditionally restore the register
 * state itself.
 */
433
static inline void switch_fpu_finish(struct fpu *new_fpu, fpu_switch_t fpu_switch)
434
{
435
	if (fpu_switch.preload) {
436
		if (unlikely(copy_fpstate_to_fpregs(new_fpu)))
437
			fpu__clear(new_fpu);
438 439 440 441 442 443
	}
}

/*
 * Signal frame handlers...
 */
444
extern int copy_fpstate_to_sigframe(void __user *buf, void __user *fx, int size);
445 446

/*
447
 * Needs to be preemption-safe.
448
 *
449
 * NOTE! user_fpu_begin() must be used only immediately before restoring
450 451 452
 * the save state. It does not do any saving/restoring on its own. In
 * lazy FPU mode, it is just an optimization to avoid a #NM exception,
 * the task can lose the FPU right after preempt_enable().
453 454 455
 */
static inline void user_fpu_begin(void)
{
456 457
	struct fpu *fpu = &current->thread.fpu;

458
	preempt_disable();
459
	if (!fpregs_active())
460
		fpregs_activate(fpu);
461 462 463
	preempt_enable();
}

464
#endif /* _ASM_X86_FPU_INTERNAL_H */