2 * x86 FPU boot time init code:
4 #include <asm/fpu/internal.h>
5 #include <asm/tlbflush.h>
7 #include <asm/cmdline.h>
9 #include <linux/sched.h>
10 #include <linux/init.h>
13 * Initialize the TS bit in CR0 according to the style of context-switches
16 static void fpu__init_cpu_ctx_switch(void)
18 if (!boot_cpu_has(X86_FEATURE_EAGER_FPU))
25 * Initialize the registers found in all CPUs, CR0 and CR4:
27 static void fpu__init_cpu_generic(void)
30 unsigned long cr4_mask = 0;
32 if (boot_cpu_has(X86_FEATURE_FXSR))
33 cr4_mask |= X86_CR4_OSFXSR;
34 if (boot_cpu_has(X86_FEATURE_XMM))
35 cr4_mask |= X86_CR4_OSXMMEXCPT;
37 cr4_set_bits(cr4_mask);
40 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
41 if (!boot_cpu_has(X86_FEATURE_FPU))
45 /* Flush out any pending x87 state: */
46 #ifdef CONFIG_MATH_EMULATION
47 if (!boot_cpu_has(X86_FEATURE_FPU))
48 fpstate_init_soft(¤t->thread.fpu.state.soft);
51 asm volatile ("fninit");
55 * Enable all supported FPU features. Called when a CPU is brought online:
57 void fpu__init_cpu(void)
59 fpu__init_cpu_generic();
60 fpu__init_cpu_xstate();
61 fpu__init_cpu_ctx_switch();
65 * The earliest FPU detection code.
67 * Set the X86_FEATURE_FPU CPU-capability bit based on
68 * trying to execute an actual sequence of FPU instructions:
70 static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
78 cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
81 if (!test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
82 asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
83 : "+m" (fsw), "+m" (fcw));
85 if (fsw == 0 && (fcw & 0x103f) == 0x003f)
86 set_cpu_cap(c, X86_FEATURE_FPU);
88 clear_cpu_cap(c, X86_FEATURE_FPU);
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");
101 * Boot time FPU feature detection code:
103 unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
105 static void __init fpu__init_system_mxcsr(void)
107 unsigned int mask = 0;
109 if (boot_cpu_has(X86_FEATURE_FXSR)) {
110 /* Static because GCC does not get 16-byte stack alignment right: */
111 static struct fxregs_state fxregs __initdata;
113 asm volatile("fxsave %0" : "+m" (fxregs));
115 mask = fxregs.mxcsr_mask;
118 * If zero then use the default features mask,
119 * which has all features set, except the
120 * denormals-are-zero feature bit:
125 mxcsr_feature_mask &= mask;
129 * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
131 static void __init fpu__init_system_generic(void)
134 * Set up the legacy init FPU context. (xstate init might overwrite this
135 * with a more modern format, if the CPU supports it.)
137 fpstate_init(&init_fpstate);
139 fpu__init_system_mxcsr();
143 * Size of the FPU context state. All tasks in the system use the
144 * same context size, regardless of what portion they use.
145 * This is inherent to the XSAVE architecture which puts all state
146 * components into a single, continuous memory block:
148 unsigned int fpu_kernel_xstate_size;
149 EXPORT_SYMBOL_GPL(fpu_kernel_xstate_size);
151 /* Get alignment of the TYPE. */
152 #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
155 * Enforce that 'MEMBER' is the last field of 'TYPE'.
157 * Align the computed size with alignment of the TYPE,
158 * because that's how C aligns structs.
160 #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
161 BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
165 * We append the 'struct fpu' to the task_struct:
167 static void __init fpu__init_task_struct_size(void)
169 int task_size = sizeof(struct task_struct);
172 * Subtract off the static size of the register state.
173 * It potentially has a bunch of padding.
175 task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);
178 * Add back the dynamically-calculated register state
181 task_size += fpu_kernel_xstate_size;
184 * We dynamically size 'struct fpu', so we require that
185 * it be at the end of 'thread_struct' and that
186 * 'thread_struct' be at the end of 'task_struct'. If
187 * you hit a compile error here, check the structure to
188 * see if something got added to the end.
190 CHECK_MEMBER_AT_END_OF(struct fpu, state);
191 CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
192 CHECK_MEMBER_AT_END_OF(struct task_struct, thread);
194 arch_task_struct_size = task_size;
198 * Set up the user and kernel xstate sizes based on the legacy FPU context size.
200 * We set this up first, and later it will be overwritten by
201 * fpu__init_system_xstate() if the CPU knows about xstates.
203 static void __init fpu__init_system_xstate_size_legacy(void)
205 static int on_boot_cpu __initdata = 1;
207 WARN_ON_FPU(!on_boot_cpu);
211 * Note that xstate sizes might be overwritten later during
212 * fpu__init_system_xstate().
215 if (!boot_cpu_has(X86_FEATURE_FPU)) {
217 * Disable xsave as we do not support it if i387
218 * emulation is enabled.
220 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
221 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
222 fpu_kernel_xstate_size = sizeof(struct swregs_state);
224 if (boot_cpu_has(X86_FEATURE_FXSR))
225 fpu_kernel_xstate_size =
226 sizeof(struct fxregs_state);
228 fpu_kernel_xstate_size =
229 sizeof(struct fregs_state);
232 fpu_user_xstate_size = fpu_kernel_xstate_size;
236 * FPU context switching strategies:
238 * Against popular belief, we don't do lazy FPU saves, due to the
239 * task migration complications it brings on SMP - we only do
242 * 'lazy' is the traditional strategy, which is based on setting
243 * CR0::TS to 1 during context-switch (instead of doing a full
244 * restore of the FPU state), which causes the first FPU instruction
245 * after the context switch (whenever it is executed) to fault - at
246 * which point we lazily restore the FPU state into FPU registers.
248 * Tasks are of course under no obligation to execute FPU instructions,
249 * so it can easily happen that another context-switch occurs without
250 * a single FPU instruction being executed. If we eventually switch
251 * back to the original task (that still owns the FPU) then we have
252 * not only saved the restores along the way, but we also have the
253 * FPU ready to be used for the original task.
255 * 'lazy' is deprecated because it's almost never a performance win
256 * and it's much more complicated than 'eager'.
258 * 'eager' switching is by default on all CPUs, there we switch the FPU
259 * state during every context switch, regardless of whether the task
260 * has used FPU instructions in that time slice or not. This is done
261 * because modern FPU context saving instructions are able to optimize
262 * state saving and restoration in hardware: they can detect both
263 * unused and untouched FPU state and optimize accordingly.
265 * [ Note that even in 'lazy' mode we might optimize context switches
266 * to use 'eager' restores, if we detect that a task is using the FPU
267 * frequently. See the fpu->counter logic in fpu/internal.h for that. ]
269 static enum { ENABLE, DISABLE } eagerfpu = ENABLE;
272 * Find supported xfeatures based on cpu features and command-line input.
273 * This must be called after fpu__init_parse_early_param() is called and
274 * xfeatures_mask is enumerated.
276 u64 __init fpu__get_supported_xfeatures_mask(void)
278 /* Support all xfeatures known to us */
279 if (eagerfpu != DISABLE)
282 /* Warning of xfeatures being disabled for no eagerfpu mode */
283 if (xfeatures_mask & XFEATURE_MASK_EAGER) {
284 pr_err("x86/fpu: eagerfpu switching disabled, disabling the following xstate features: 0x%llx.\n",
285 xfeatures_mask & XFEATURE_MASK_EAGER);
288 /* Return a mask that masks out all features requiring eagerfpu mode */
289 return ~XFEATURE_MASK_EAGER;
293 * Disable features dependent on eagerfpu.
295 static void __init fpu__clear_eager_fpu_features(void)
297 setup_clear_cpu_cap(X86_FEATURE_MPX);
301 * Pick the FPU context switching strategy:
303 * When eagerfpu is AUTO or ENABLE, we ensure it is ENABLE if either of
304 * the following is true:
306 * (1) the cpu has xsaveopt, as it has the optimization and doing eager
307 * FPU switching has a relatively low cost compared to a plain xsave;
308 * (2) the cpu has xsave features (e.g. MPX) that depend on eager FPU
309 * switching. Should the kernel boot with noxsaveopt, we support MPX
310 * with eager FPU switching at a higher cost.
312 static void __init fpu__init_system_ctx_switch(void)
314 static bool on_boot_cpu __initdata = 1;
316 WARN_ON_FPU(!on_boot_cpu);
319 WARN_ON_FPU(current->thread.fpu.fpstate_active);
321 if (boot_cpu_has(X86_FEATURE_XSAVEOPT) && eagerfpu != DISABLE)
324 if (xfeatures_mask & XFEATURE_MASK_EAGER)
327 if (eagerfpu == ENABLE)
328 setup_force_cpu_cap(X86_FEATURE_EAGER_FPU);
330 printk(KERN_INFO "x86/fpu: Using '%s' FPU context switches.\n", eagerfpu == ENABLE ? "eager" : "lazy");
334 * We parse fpu parameters early because fpu__init_system() is executed
335 * before parse_early_param().
337 static void __init fpu__init_parse_early_param(void)
339 if (cmdline_find_option_bool(boot_command_line, "eagerfpu=off")) {
341 fpu__clear_eager_fpu_features();
344 if (cmdline_find_option_bool(boot_command_line, "no387"))
345 setup_clear_cpu_cap(X86_FEATURE_FPU);
347 if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
348 setup_clear_cpu_cap(X86_FEATURE_FXSR);
349 setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
350 setup_clear_cpu_cap(X86_FEATURE_XMM);
353 if (cmdline_find_option_bool(boot_command_line, "noxsave"))
354 fpu__xstate_clear_all_cpu_caps();
356 if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
357 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
359 if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
360 setup_clear_cpu_cap(X86_FEATURE_XSAVES);
364 * Called on the boot CPU once per system bootup, to set up the initial
365 * FPU state that is later cloned into all processes:
367 void __init fpu__init_system(struct cpuinfo_x86 *c)
369 fpu__init_parse_early_param();
370 fpu__init_system_early_generic(c);
373 * The FPU has to be operational for some of the
374 * later FPU init activities:
379 * But don't leave CR0::TS set yet, as some of the FPU setup
380 * methods depend on being able to execute FPU instructions
381 * that will fault on a set TS, such as the FXSAVE in
382 * fpu__init_system_mxcsr().
386 fpu__init_system_generic();
387 fpu__init_system_xstate_size_legacy();
388 fpu__init_system_xstate();
389 fpu__init_task_struct_size();
391 fpu__init_system_ctx_switch();