xref: /openbmc/linux/arch/x86/kernel/fpu/init.c (revision a06c488d)
1 /*
2  * x86 FPU boot time init code:
3  */
4 #include <asm/fpu/internal.h>
5 #include <asm/tlbflush.h>
6 #include <asm/setup.h>
7 #include <asm/cmdline.h>
8 
9 #include <linux/sched.h>
10 #include <linux/init.h>
11 
12 /*
13  * Initialize the TS bit in CR0 according to the style of context-switches
14  * we are using:
15  */
16 static void fpu__init_cpu_ctx_switch(void)
17 {
18 	if (!boot_cpu_has(X86_FEATURE_EAGER_FPU))
19 		stts();
20 	else
21 		clts();
22 }
23 
24 /*
25  * Initialize the registers found in all CPUs, CR0 and CR4:
26  */
27 static void fpu__init_cpu_generic(void)
28 {
29 	unsigned long cr0;
30 	unsigned long cr4_mask = 0;
31 
32 	if (cpu_has_fxsr)
33 		cr4_mask |= X86_CR4_OSFXSR;
34 	if (cpu_has_xmm)
35 		cr4_mask |= X86_CR4_OSXMMEXCPT;
36 	if (cr4_mask)
37 		cr4_set_bits(cr4_mask);
38 
39 	cr0 = read_cr0();
40 	cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
41 	if (!cpu_has_fpu)
42 		cr0 |= X86_CR0_EM;
43 	write_cr0(cr0);
44 
45 	/* Flush out any pending x87 state: */
46 #ifdef CONFIG_MATH_EMULATION
47 	if (!cpu_has_fpu)
48 		fpstate_init_soft(&current->thread.fpu.state.soft);
49 	else
50 #endif
51 		asm volatile ("fninit");
52 }
53 
54 /*
55  * Enable all supported FPU features. Called when a CPU is brought online:
56  */
57 void fpu__init_cpu(void)
58 {
59 	fpu__init_cpu_generic();
60 	fpu__init_cpu_xstate();
61 	fpu__init_cpu_ctx_switch();
62 }
63 
64 /*
65  * The earliest FPU detection code.
66  *
67  * Set the X86_FEATURE_FPU CPU-capability bit based on
68  * trying to execute an actual sequence of FPU instructions:
69  */
70 static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
71 {
72 	unsigned long cr0;
73 	u16 fsw, fcw;
74 
75 	fsw = fcw = 0xffff;
76 
77 	cr0 = read_cr0();
78 	cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
79 	write_cr0(cr0);
80 
81 	asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
82 		     : "+m" (fsw), "+m" (fcw));
83 
84 	if (fsw == 0 && (fcw & 0x103f) == 0x003f)
85 		set_cpu_cap(c, X86_FEATURE_FPU);
86 	else
87 		clear_cpu_cap(c, X86_FEATURE_FPU);
88 
89 #ifndef CONFIG_MATH_EMULATION
90 	if (!cpu_has_fpu) {
91 		pr_emerg("x86/fpu: Giving up, no FPU found and no math emulation present\n");
92 		for (;;)
93 			asm volatile("hlt");
94 	}
95 #endif
96 }
97 
98 /*
99  * Boot time FPU feature detection code:
100  */
101 unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
102 
103 static void __init fpu__init_system_mxcsr(void)
104 {
105 	unsigned int mask = 0;
106 
107 	if (cpu_has_fxsr) {
108 		/* Static because GCC does not get 16-byte stack alignment right: */
109 		static struct fxregs_state fxregs __initdata;
110 
111 		asm volatile("fxsave %0" : "+m" (fxregs));
112 
113 		mask = fxregs.mxcsr_mask;
114 
115 		/*
116 		 * If zero then use the default features mask,
117 		 * which has all features set, except the
118 		 * denormals-are-zero feature bit:
119 		 */
120 		if (mask == 0)
121 			mask = 0x0000ffbf;
122 	}
123 	mxcsr_feature_mask &= mask;
124 }
125 
126 /*
127  * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
128  */
129 static void __init fpu__init_system_generic(void)
130 {
131 	/*
132 	 * Set up the legacy init FPU context. (xstate init might overwrite this
133 	 * with a more modern format, if the CPU supports it.)
134 	 */
135 	fpstate_init_fxstate(&init_fpstate.fxsave);
136 
137 	fpu__init_system_mxcsr();
138 }
139 
140 /*
141  * Size of the FPU context state. All tasks in the system use the
142  * same context size, regardless of what portion they use.
143  * This is inherent to the XSAVE architecture which puts all state
144  * components into a single, continuous memory block:
145  */
146 unsigned int xstate_size;
147 EXPORT_SYMBOL_GPL(xstate_size);
148 
149 /* Get alignment of the TYPE. */
150 #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
151 
152 /*
153  * Enforce that 'MEMBER' is the last field of 'TYPE'.
154  *
155  * Align the computed size with alignment of the TYPE,
156  * because that's how C aligns structs.
157  */
158 #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
159 	BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
160 					   TYPE_ALIGN(TYPE)))
161 
162 /*
163  * We append the 'struct fpu' to the task_struct:
164  */
165 static void __init fpu__init_task_struct_size(void)
166 {
167 	int task_size = sizeof(struct task_struct);
168 
169 	/*
170 	 * Subtract off the static size of the register state.
171 	 * It potentially has a bunch of padding.
172 	 */
173 	task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);
174 
175 	/*
176 	 * Add back the dynamically-calculated register state
177 	 * size.
178 	 */
179 	task_size += xstate_size;
180 
181 	/*
182 	 * We dynamically size 'struct fpu', so we require that
183 	 * it be at the end of 'thread_struct' and that
184 	 * 'thread_struct' be at the end of 'task_struct'.  If
185 	 * you hit a compile error here, check the structure to
186 	 * see if something got added to the end.
187 	 */
188 	CHECK_MEMBER_AT_END_OF(struct fpu, state);
189 	CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
190 	CHECK_MEMBER_AT_END_OF(struct task_struct, thread);
191 
192 	arch_task_struct_size = task_size;
193 }
194 
195 /*
196  * Set up the xstate_size based on the legacy FPU context size.
197  *
198  * We set this up first, and later it will be overwritten by
199  * fpu__init_system_xstate() if the CPU knows about xstates.
200  */
201 static void __init fpu__init_system_xstate_size_legacy(void)
202 {
203 	static int on_boot_cpu __initdata = 1;
204 
205 	WARN_ON_FPU(!on_boot_cpu);
206 	on_boot_cpu = 0;
207 
208 	/*
209 	 * Note that xstate_size might be overwriten later during
210 	 * fpu__init_system_xstate().
211 	 */
212 
213 	if (!cpu_has_fpu) {
214 		/*
215 		 * Disable xsave as we do not support it if i387
216 		 * emulation is enabled.
217 		 */
218 		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
219 		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
220 		xstate_size = sizeof(struct swregs_state);
221 	} else {
222 		if (cpu_has_fxsr)
223 			xstate_size = sizeof(struct fxregs_state);
224 		else
225 			xstate_size = sizeof(struct fregs_state);
226 	}
227 	/*
228 	 * Quirk: we don't yet handle the XSAVES* instructions
229 	 * correctly, as we don't correctly convert between
230 	 * standard and compacted format when interfacing
231 	 * with user-space - so disable it for now.
232 	 *
233 	 * The difference is small: with recent CPUs the
234 	 * compacted format is only marginally smaller than
235 	 * the standard FPU state format.
236 	 *
237 	 * ( This is easy to backport while we are fixing
238 	 *   XSAVES* support. )
239 	 */
240 	setup_clear_cpu_cap(X86_FEATURE_XSAVES);
241 }
242 
243 /*
244  * FPU context switching strategies:
245  *
246  * Against popular belief, we don't do lazy FPU saves, due to the
247  * task migration complications it brings on SMP - we only do
248  * lazy FPU restores.
249  *
250  * 'lazy' is the traditional strategy, which is based on setting
251  * CR0::TS to 1 during context-switch (instead of doing a full
252  * restore of the FPU state), which causes the first FPU instruction
253  * after the context switch (whenever it is executed) to fault - at
254  * which point we lazily restore the FPU state into FPU registers.
255  *
256  * Tasks are of course under no obligation to execute FPU instructions,
257  * so it can easily happen that another context-switch occurs without
258  * a single FPU instruction being executed. If we eventually switch
259  * back to the original task (that still owns the FPU) then we have
260  * not only saved the restores along the way, but we also have the
261  * FPU ready to be used for the original task.
262  *
263  * 'eager' switching is used on modern CPUs, there we switch the FPU
264  * state during every context switch, regardless of whether the task
265  * has used FPU instructions in that time slice or not. This is done
266  * because modern FPU context saving instructions are able to optimize
267  * state saving and restoration in hardware: they can detect both
268  * unused and untouched FPU state and optimize accordingly.
269  *
270  * [ Note that even in 'lazy' mode we might optimize context switches
271  *   to use 'eager' restores, if we detect that a task is using the FPU
272  *   frequently. See the fpu->counter logic in fpu/internal.h for that. ]
273  */
274 static enum { AUTO, ENABLE, DISABLE } eagerfpu = AUTO;
275 
276 /*
277  * Find supported xfeatures based on cpu features and command-line input.
278  * This must be called after fpu__init_parse_early_param() is called and
279  * xfeatures_mask is enumerated.
280  */
281 u64 __init fpu__get_supported_xfeatures_mask(void)
282 {
283 	/* Support all xfeatures known to us */
284 	if (eagerfpu != DISABLE)
285 		return XCNTXT_MASK;
286 
287 	/* Warning of xfeatures being disabled for no eagerfpu mode */
288 	if (xfeatures_mask & XFEATURE_MASK_EAGER) {
289 		pr_err("x86/fpu: eagerfpu switching disabled, disabling the following xstate features: 0x%llx.\n",
290 			xfeatures_mask & XFEATURE_MASK_EAGER);
291 	}
292 
293 	/* Return a mask that masks out all features requiring eagerfpu mode */
294 	return ~XFEATURE_MASK_EAGER;
295 }
296 
297 /*
298  * Disable features dependent on eagerfpu.
299  */
300 static void __init fpu__clear_eager_fpu_features(void)
301 {
302 	setup_clear_cpu_cap(X86_FEATURE_MPX);
303 	setup_clear_cpu_cap(X86_FEATURE_AVX);
304 	setup_clear_cpu_cap(X86_FEATURE_AVX2);
305 	setup_clear_cpu_cap(X86_FEATURE_AVX512F);
306 	setup_clear_cpu_cap(X86_FEATURE_AVX512PF);
307 	setup_clear_cpu_cap(X86_FEATURE_AVX512ER);
308 	setup_clear_cpu_cap(X86_FEATURE_AVX512CD);
309 }
310 
311 /*
312  * Pick the FPU context switching strategy:
313  *
314  * When eagerfpu is AUTO or ENABLE, we ensure it is ENABLE if either of
315  * the following is true:
316  *
317  * (1) the cpu has xsaveopt, as it has the optimization and doing eager
318  *     FPU switching has a relatively low cost compared to a plain xsave;
319  * (2) the cpu has xsave features (e.g. MPX) that depend on eager FPU
320  *     switching. Should the kernel boot with noxsaveopt, we support MPX
321  *     with eager FPU switching at a higher cost.
322  */
323 static void __init fpu__init_system_ctx_switch(void)
324 {
325 	static bool on_boot_cpu __initdata = 1;
326 
327 	WARN_ON_FPU(!on_boot_cpu);
328 	on_boot_cpu = 0;
329 
330 	WARN_ON_FPU(current->thread.fpu.fpstate_active);
331 	current_thread_info()->status = 0;
332 
333 	if (boot_cpu_has(X86_FEATURE_XSAVEOPT) && eagerfpu != DISABLE)
334 		eagerfpu = ENABLE;
335 
336 	if (xfeatures_mask & XFEATURE_MASK_EAGER)
337 		eagerfpu = ENABLE;
338 
339 	if (eagerfpu == ENABLE)
340 		setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
341 
342 	printk(KERN_INFO "x86/fpu: Using '%s' FPU context switches.\n", eagerfpu == ENABLE ? "eager" : "lazy");
343 }
344 
345 /*
346  * We parse fpu parameters early because fpu__init_system() is executed
347  * before parse_early_param().
348  */
349 static void __init fpu__init_parse_early_param(void)
350 {
351 	/*
352 	 * No need to check "eagerfpu=auto" again, since it is the
353 	 * initial default.
354 	 */
355 	if (cmdline_find_option_bool(boot_command_line, "eagerfpu=off")) {
356 		eagerfpu = DISABLE;
357 		fpu__clear_eager_fpu_features();
358 	} else if (cmdline_find_option_bool(boot_command_line, "eagerfpu=on")) {
359 		eagerfpu = ENABLE;
360 	}
361 
362 	if (cmdline_find_option_bool(boot_command_line, "no387"))
363 		setup_clear_cpu_cap(X86_FEATURE_FPU);
364 
365 	if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
366 		setup_clear_cpu_cap(X86_FEATURE_FXSR);
367 		setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
368 		setup_clear_cpu_cap(X86_FEATURE_XMM);
369 	}
370 
371 	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
372 		fpu__xstate_clear_all_cpu_caps();
373 
374 	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
375 		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
376 
377 	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
378 		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
379 }
380 
381 /*
382  * Called on the boot CPU once per system bootup, to set up the initial
383  * FPU state that is later cloned into all processes:
384  */
385 void __init fpu__init_system(struct cpuinfo_x86 *c)
386 {
387 	fpu__init_parse_early_param();
388 	fpu__init_system_early_generic(c);
389 
390 	/*
391 	 * The FPU has to be operational for some of the
392 	 * later FPU init activities:
393 	 */
394 	fpu__init_cpu();
395 
396 	/*
397 	 * But don't leave CR0::TS set yet, as some of the FPU setup
398 	 * methods depend on being able to execute FPU instructions
399 	 * that will fault on a set TS, such as the FXSAVE in
400 	 * fpu__init_system_mxcsr().
401 	 */
402 	clts();
403 
404 	fpu__init_system_generic();
405 	fpu__init_system_xstate_size_legacy();
406 	fpu__init_system_xstate();
407 	fpu__init_task_struct_size();
408 
409 	fpu__init_system_ctx_switch();
410 }
411