init.c 11.1 KB
Newer Older
1
/*
2
 * x86 FPU boot time init code:
3
 */
4
#include <asm/fpu/internal.h>
5
#include <asm/tlbflush.h>
6 7
#include <asm/setup.h>
#include <asm/cmdline.h>
8

9
#include <linux/sched.h>
10
#include <linux/init.h>
11

12 13 14 15
/*
 * Initialize the TS bit in CR0 according to the style of context-switches
 * we are using:
 */
I
Ingo Molnar 已提交
16 17
static void fpu__init_cpu_ctx_switch(void)
{
18
	if (!boot_cpu_has(X86_FEATURE_EAGER_FPU))
I
Ingo Molnar 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
		stts();
	else
		clts();
}

/*
 * Initialize the registers found in all CPUs, CR0 and CR4:
 */
static void fpu__init_cpu_generic(void)
{
	unsigned long cr0;
	unsigned long cr4_mask = 0;

32
	if (boot_cpu_has(X86_FEATURE_FXSR))
I
Ingo Molnar 已提交
33
		cr4_mask |= X86_CR4_OSFXSR;
34
	if (boot_cpu_has(X86_FEATURE_XMM))
I
Ingo Molnar 已提交
35 36 37 38 39 40
		cr4_mask |= X86_CR4_OSXMMEXCPT;
	if (cr4_mask)
		cr4_set_bits(cr4_mask);

	cr0 = read_cr0();
	cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
41
	if (!boot_cpu_has(X86_FEATURE_FPU))
I
Ingo Molnar 已提交
42 43
		cr0 |= X86_CR0_EM;
	write_cr0(cr0);
44 45

	/* Flush out any pending x87 state: */
46
#ifdef CONFIG_MATH_EMULATION
47
	if (!boot_cpu_has(X86_FEATURE_FPU))
48 49 50 51
		fpstate_init_soft(&current->thread.fpu.state.soft);
	else
#endif
		asm volatile ("fninit");
I
Ingo Molnar 已提交
52 53 54
}

/*
55
 * Enable all supported FPU features. Called when a CPU is brought online:
I
Ingo Molnar 已提交
56 57 58 59 60 61 62 63
 */
void fpu__init_cpu(void)
{
	fpu__init_cpu_generic();
	fpu__init_cpu_xstate();
	fpu__init_cpu_ctx_switch();
}

64
/*
65 66 67 68
 * The earliest FPU detection code.
 *
 * Set the X86_FEATURE_FPU CPU-capability bit based on
 * trying to execute an actual sequence of FPU instructions:
69 70 71 72 73 74 75 76 77 78 79 80
 */
static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
{
	unsigned long cr0;
	u16 fsw, fcw;

	fsw = fcw = 0xffff;

	cr0 = read_cr0();
	cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
	write_cr0(cr0);

81 82 83 84 85 86 87 88 89
	if (!test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
		asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
			     : "+m" (fsw), "+m" (fcw));

		if (fsw == 0 && (fcw & 0x103f) == 0x003f)
			set_cpu_cap(c, X86_FEATURE_FPU);
		else
			clear_cpu_cap(c, X86_FEATURE_FPU);
	}
90 91

#ifndef CONFIG_MATH_EMULATION
92
	if (!boot_cpu_has(X86_FEATURE_FPU)) {
93
		pr_emerg("x86/fpu: Giving up, no FPU found and no math emulation present\n");
94 95 96 97
		for (;;)
			asm volatile("hlt");
	}
#endif
98 99
}

100 101 102
/*
 * Boot time FPU feature detection code:
 */
103
unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
104

105
static void __init fpu__init_system_mxcsr(void)
106
{
107
	unsigned int mask = 0;
108

109
	if (boot_cpu_has(X86_FEATURE_FXSR)) {
110 111
		/* Static because GCC does not get 16-byte stack alignment right: */
		static struct fxregs_state fxregs __initdata;
112

113
		asm volatile("fxsave %0" : "+m" (fxregs));
114

115
		mask = fxregs.mxcsr_mask;
116 117 118 119 120 121

		/*
		 * If zero then use the default features mask,
		 * which has all features set, except the
		 * denormals-are-zero feature bit:
		 */
122 123 124 125 126 127
		if (mask == 0)
			mask = 0x0000ffbf;
	}
	mxcsr_feature_mask &= mask;
}

128 129 130
/*
 * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
 */
131
static void __init fpu__init_system_generic(void)
132 133 134 135 136
{
	/*
	 * Set up the legacy init FPU context. (xstate init might overwrite this
	 * with a more modern format, if the CPU supports it.)
	 */
137
	fpstate_init(&init_fpstate);
138 139 140 141

	fpu__init_system_mxcsr();
}

142 143 144 145 146 147
/*
 * Size of the FPU context state. All tasks in the system use the
 * same context size, regardless of what portion they use.
 * This is inherent to the XSAVE architecture which puts all state
 * components into a single, continuous memory block:
 */
I
Ingo Molnar 已提交
148 149 150
unsigned int xstate_size;
EXPORT_SYMBOL_GPL(xstate_size);

151 152 153 154 155 156 157 158 159
/* Get alignment of the TYPE. */
#define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)

/*
 * Enforce that 'MEMBER' is the last field of 'TYPE'.
 *
 * Align the computed size with alignment of the TYPE,
 * because that's how C aligns structs.
 */
160
#define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
161 162
	BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
					   TYPE_ALIGN(TYPE)))
163 164

/*
165
 * We append the 'struct fpu' to the task_struct:
166
 */
167
static void __init fpu__init_task_struct_size(void)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
{
	int task_size = sizeof(struct task_struct);

	/*
	 * Subtract off the static size of the register state.
	 * It potentially has a bunch of padding.
	 */
	task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);

	/*
	 * Add back the dynamically-calculated register state
	 * size.
	 */
	task_size += xstate_size;

	/*
	 * We dynamically size 'struct fpu', so we require that
	 * it be at the end of 'thread_struct' and that
	 * 'thread_struct' be at the end of 'task_struct'.  If
	 * you hit a compile error here, check the structure to
	 * see if something got added to the end.
	 */
	CHECK_MEMBER_AT_END_OF(struct fpu, state);
	CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
	CHECK_MEMBER_AT_END_OF(struct task_struct, thread);

194
	arch_task_struct_size = task_size;
195 196
}

I
Ingo Molnar 已提交
197 198 199 200 201 202
/*
 * Set up the xstate_size based on the legacy FPU context size.
 *
 * We set this up first, and later it will be overwritten by
 * fpu__init_system_xstate() if the CPU knows about xstates.
 */
203
static void __init fpu__init_system_xstate_size_legacy(void)
204
{
205
	static int on_boot_cpu __initdata = 1;
206 207 208 209

	WARN_ON_FPU(!on_boot_cpu);
	on_boot_cpu = 0;

210 211
	/*
	 * Note that xstate_size might be overwriten later during
I
Ingo Molnar 已提交
212
	 * fpu__init_system_xstate().
213 214
	 */

215
	if (!boot_cpu_has(X86_FEATURE_FPU)) {
216 217 218 219 220 221
		/*
		 * Disable xsave as we do not support it if i387
		 * emulation is enabled.
		 */
		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
222
		xstate_size = sizeof(struct swregs_state);
223
	} else {
224
		if (boot_cpu_has(X86_FEATURE_FXSR))
225
			xstate_size = sizeof(struct fxregs_state);
226
		else
227
			xstate_size = sizeof(struct fregs_state);
228
	}
229 230 231 232 233 234 235 236 237 238 239 240 241 242
	/*
	 * Quirk: we don't yet handle the XSAVES* instructions
	 * correctly, as we don't correctly convert between
	 * standard and compacted format when interfacing
	 * with user-space - so disable it for now.
	 *
	 * The difference is small: with recent CPUs the
	 * compacted format is only marginally smaller than
	 * the standard FPU state format.
	 *
	 * ( This is easy to backport while we are fixing
	 *   XSAVES* support. )
	 */
	setup_clear_cpu_cap(X86_FEATURE_XSAVES);
243 244
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/*
 * FPU context switching strategies:
 *
 * Against popular belief, we don't do lazy FPU saves, due to the
 * task migration complications it brings on SMP - we only do
 * lazy FPU restores.
 *
 * 'lazy' is the traditional strategy, which is based on setting
 * CR0::TS to 1 during context-switch (instead of doing a full
 * restore of the FPU state), which causes the first FPU instruction
 * after the context switch (whenever it is executed) to fault - at
 * which point we lazily restore the FPU state into FPU registers.
 *
 * Tasks are of course under no obligation to execute FPU instructions,
 * so it can easily happen that another context-switch occurs without
 * a single FPU instruction being executed. If we eventually switch
 * back to the original task (that still owns the FPU) then we have
 * not only saved the restores along the way, but we also have the
 * FPU ready to be used for the original task.
 *
265 266 267 268
 * 'lazy' is deprecated because it's almost never a performance win
 * and it's much more complicated than 'eager'.
 *
 * 'eager' switching is by default on all CPUs, there we switch the FPU
269 270 271 272 273 274 275 276 277 278
 * state during every context switch, regardless of whether the task
 * has used FPU instructions in that time slice or not. This is done
 * because modern FPU context saving instructions are able to optimize
 * state saving and restoration in hardware: they can detect both
 * unused and untouched FPU state and optimize accordingly.
 *
 * [ Note that even in 'lazy' mode we might optimize context switches
 *   to use 'eager' restores, if we detect that a task is using the FPU
 *   frequently. See the fpu->counter logic in fpu/internal.h for that. ]
 */
279
static enum { ENABLE, DISABLE } eagerfpu = ENABLE;
280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
/*
 * Find supported xfeatures based on cpu features and command-line input.
 * This must be called after fpu__init_parse_early_param() is called and
 * xfeatures_mask is enumerated.
 */
u64 __init fpu__get_supported_xfeatures_mask(void)
{
	/* Support all xfeatures known to us */
	if (eagerfpu != DISABLE)
		return XCNTXT_MASK;

	/* Warning of xfeatures being disabled for no eagerfpu mode */
	if (xfeatures_mask & XFEATURE_MASK_EAGER) {
		pr_err("x86/fpu: eagerfpu switching disabled, disabling the following xstate features: 0x%llx.\n",
			xfeatures_mask & XFEATURE_MASK_EAGER);
	}

	/* Return a mask that masks out all features requiring eagerfpu mode */
	return ~XFEATURE_MASK_EAGER;
}

/*
 * Disable features dependent on eagerfpu.
 */
static void __init fpu__clear_eager_fpu_features(void)
{
	setup_clear_cpu_cap(X86_FEATURE_MPX);
}

310
/*
311
 * Pick the FPU context switching strategy:
312 313 314 315 316 317 318 319 320
 *
 * When eagerfpu is AUTO or ENABLE, we ensure it is ENABLE if either of
 * the following is true:
 *
 * (1) the cpu has xsaveopt, as it has the optimization and doing eager
 *     FPU switching has a relatively low cost compared to a plain xsave;
 * (2) the cpu has xsave features (e.g. MPX) that depend on eager FPU
 *     switching. Should the kernel boot with noxsaveopt, we support MPX
 *     with eager FPU switching at a higher cost.
321
 */
322
static void __init fpu__init_system_ctx_switch(void)
323
{
324
	static bool on_boot_cpu __initdata = 1;
325 326 327 328 329

	WARN_ON_FPU(!on_boot_cpu);
	on_boot_cpu = 0;

	WARN_ON_FPU(current->thread.fpu.fpstate_active);
330 331
	current_thread_info()->status = 0;

332
	if (boot_cpu_has(X86_FEATURE_XSAVEOPT) && eagerfpu != DISABLE)
333 334
		eagerfpu = ENABLE;

335 336
	if (xfeatures_mask & XFEATURE_MASK_EAGER)
		eagerfpu = ENABLE;
337 338 339 340

	if (eagerfpu == ENABLE)
		setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);

341
	printk(KERN_INFO "x86/fpu: Using '%s' FPU context switches.\n", eagerfpu == ENABLE ? "eager" : "lazy");
342 343
}

344 345 346 347 348 349
/*
 * We parse fpu parameters early because fpu__init_system() is executed
 * before parse_early_param().
 */
static void __init fpu__init_parse_early_param(void)
{
350
	if (cmdline_find_option_bool(boot_command_line, "eagerfpu=off")) {
351
		eagerfpu = DISABLE;
352 353
		fpu__clear_eager_fpu_features();
	}
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

	if (cmdline_find_option_bool(boot_command_line, "no387"))
		setup_clear_cpu_cap(X86_FEATURE_FPU);

	if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
		setup_clear_cpu_cap(X86_FEATURE_FXSR);
		setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
		setup_clear_cpu_cap(X86_FEATURE_XMM);
	}

	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
		fpu__xstate_clear_all_cpu_caps();

	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);

	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
}

374
/*
375 376
 * Called on the boot CPU once per system bootup, to set up the initial
 * FPU state that is later cloned into all processes:
377
 */
378
void __init fpu__init_system(struct cpuinfo_x86 *c)
379
{
380
	fpu__init_parse_early_param();
381 382
	fpu__init_system_early_generic(c);

383 384 385 386
	/*
	 * The FPU has to be operational for some of the
	 * later FPU init activities:
	 */
387
	fpu__init_cpu();
388

389
	/*
390 391 392 393
	 * But don't leave CR0::TS set yet, as some of the FPU setup
	 * methods depend on being able to execute FPU instructions
	 * that will fault on a set TS, such as the FXSAVE in
	 * fpu__init_system_mxcsr().
394 395 396
	 */
	clts();

397
	fpu__init_system_generic();
398
	fpu__init_system_xstate_size_legacy();
I
Ingo Molnar 已提交
399
	fpu__init_system_xstate();
400
	fpu__init_task_struct_size();
401

402
	fpu__init_system_ctx_switch();
403
}