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