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)
22 * Initialize the registers found in all CPUs, CR0 and CR4:
24 static void fpu__init_cpu_generic(void)
27 unsigned long cr4_mask = 0;
29 if (boot_cpu_has(X86_FEATURE_FXSR))
30 cr4_mask |= X86_CR4_OSFXSR;
31 if (boot_cpu_has(X86_FEATURE_XMM))
32 cr4_mask |= X86_CR4_OSXMMEXCPT;
34 cr4_set_bits(cr4_mask);
37 cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
38 if (!boot_cpu_has(X86_FEATURE_FPU))
42 /* Flush out any pending x87 state: */
43 #ifdef CONFIG_MATH_EMULATION
44 if (!boot_cpu_has(X86_FEATURE_FPU))
45 fpstate_init_soft(¤t->thread.fpu.state.soft);
48 asm volatile ("fninit");
52 * Enable all supported FPU features. Called when a CPU is brought online:
54 void fpu__init_cpu(void)
56 fpu__init_cpu_generic();
57 fpu__init_cpu_xstate();
58 fpu__init_cpu_ctx_switch();
62 * The earliest FPU detection code.
64 * Set the X86_FEATURE_FPU CPU-capability bit based on
65 * trying to execute an actual sequence of FPU instructions:
67 static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
75 cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
78 if (!test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
79 asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
80 : "+m" (fsw), "+m" (fcw));
82 if (fsw == 0 && (fcw & 0x103f) == 0x003f)
83 set_cpu_cap(c, X86_FEATURE_FPU);
85 clear_cpu_cap(c, X86_FEATURE_FPU);
88 #ifndef CONFIG_MATH_EMULATION
89 if (!boot_cpu_has(X86_FEATURE_FPU)) {
90 pr_emerg("x86/fpu: Giving up, no FPU found and no math emulation present\n");
98 * Boot time FPU feature detection code:
100 unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
101 EXPORT_SYMBOL_GPL(mxcsr_feature_mask);
103 static void __init fpu__init_system_mxcsr(void)
105 unsigned int mask = 0;
107 if (boot_cpu_has(X86_FEATURE_FXSR)) {
108 /* Static because GCC does not get 16-byte stack alignment right: */
109 static struct fxregs_state fxregs __initdata;
111 asm volatile("fxsave %0" : "+m" (fxregs));
113 mask = fxregs.mxcsr_mask;
116 * If zero then use the default features mask,
117 * which has all features set, except the
118 * denormals-are-zero feature bit:
123 mxcsr_feature_mask &= mask;
127 * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
129 static void __init fpu__init_system_generic(void)
132 * Set up the legacy init FPU context. (xstate init might overwrite this
133 * with a more modern format, if the CPU supports it.)
135 fpstate_init(&init_fpstate);
137 fpu__init_system_mxcsr();
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:
146 unsigned int fpu_kernel_xstate_size;
147 EXPORT_SYMBOL_GPL(fpu_kernel_xstate_size);
149 /* Get alignment of the TYPE. */
150 #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
153 * Enforce that 'MEMBER' is the last field of 'TYPE'.
155 * Align the computed size with alignment of the TYPE,
156 * because that's how C aligns structs.
158 #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
159 BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
163 * We append the 'struct fpu' to the task_struct:
165 static void __init fpu__init_task_struct_size(void)
167 int task_size = sizeof(struct task_struct);
170 * Subtract off the static size of the register state.
171 * It potentially has a bunch of padding.
173 task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);
176 * Add back the dynamically-calculated register state
179 task_size += fpu_kernel_xstate_size;
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.
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);
192 arch_task_struct_size = task_size;
196 * Set up the user and kernel xstate sizes based on the legacy FPU context size.
198 * We set this up first, and later it will be overwritten by
199 * fpu__init_system_xstate() if the CPU knows about xstates.
201 static void __init fpu__init_system_xstate_size_legacy(void)
203 static int on_boot_cpu __initdata = 1;
205 WARN_ON_FPU(!on_boot_cpu);
209 * Note that xstate sizes might be overwritten later during
210 * fpu__init_system_xstate().
213 if (!boot_cpu_has(X86_FEATURE_FPU)) {
215 * Disable xsave as we do not support it if i387
216 * emulation is enabled.
218 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
219 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
220 fpu_kernel_xstate_size = sizeof(struct swregs_state);
222 if (boot_cpu_has(X86_FEATURE_FXSR))
223 fpu_kernel_xstate_size =
224 sizeof(struct fxregs_state);
226 fpu_kernel_xstate_size =
227 sizeof(struct fregs_state);
230 fpu_user_xstate_size = fpu_kernel_xstate_size;
234 * Find supported xfeatures based on cpu features and command-line input.
235 * This must be called after fpu__init_parse_early_param() is called and
236 * xfeatures_mask is enumerated.
238 u64 __init fpu__get_supported_xfeatures_mask(void)
243 /* Legacy code to initialize eager fpu mode. */
244 static void __init fpu__init_system_ctx_switch(void)
246 static bool on_boot_cpu __initdata = 1;
248 WARN_ON_FPU(!on_boot_cpu);
251 WARN_ON_FPU(current->thread.fpu.fpstate_active);
255 * We parse fpu parameters early because fpu__init_system() is executed
256 * before parse_early_param().
258 static void __init fpu__init_parse_early_param(void)
260 if (cmdline_find_option_bool(boot_command_line, "no387"))
261 setup_clear_cpu_cap(X86_FEATURE_FPU);
263 if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
264 setup_clear_cpu_cap(X86_FEATURE_FXSR);
265 setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
266 setup_clear_cpu_cap(X86_FEATURE_XMM);
269 if (cmdline_find_option_bool(boot_command_line, "noxsave"))
270 fpu__xstate_clear_all_cpu_caps();
272 if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
273 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
275 if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
276 setup_clear_cpu_cap(X86_FEATURE_XSAVES);
280 * Called on the boot CPU once per system bootup, to set up the initial
281 * FPU state that is later cloned into all processes:
283 void __init fpu__init_system(struct cpuinfo_x86 *c)
285 fpu__init_parse_early_param();
286 fpu__init_system_early_generic(c);
289 * The FPU has to be operational for some of the
290 * later FPU init activities:
295 * But don't leave CR0::TS set yet, as some of the FPU setup
296 * methods depend on being able to execute FPU instructions
297 * that will fault on a set TS, such as the FXSAVE in
298 * fpu__init_system_mxcsr().
302 fpu__init_system_generic();
303 fpu__init_system_xstate_size_legacy();
304 fpu__init_system_xstate();
305 fpu__init_task_struct_size();
307 fpu__init_system_ctx_switch();