]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/fpu/init.c
Merge tag 'v4.9-rc1' into x86/fpu, to resolve conflict
[karo-tx-linux.git] / arch / x86 / kernel / fpu / init.c
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         clts();
19 }
20
21 /*
22  * Initialize the registers found in all CPUs, CR0 and CR4:
23  */
24 static void fpu__init_cpu_generic(void)
25 {
26         unsigned long cr0;
27         unsigned long cr4_mask = 0;
28
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;
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 (!boot_cpu_has(X86_FEATURE_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 (!boot_cpu_has(X86_FEATURE_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  */
54 void 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  */
67 static 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         if (!test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
79                 asm volatile("fninit ; fnstsw %0 ; fnstcw %1"
80                              : "+m" (fsw), "+m" (fcw));
81
82                 if (fsw == 0 && (fcw & 0x103f) == 0x003f)
83                         set_cpu_cap(c, X86_FEATURE_FPU);
84                 else
85                         clear_cpu_cap(c, X86_FEATURE_FPU);
86         }
87
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");
91                 for (;;)
92                         asm volatile("hlt");
93         }
94 #endif
95 }
96
97 /*
98  * Boot time FPU feature detection code:
99  */
100 unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
101
102 static void __init fpu__init_system_mxcsr(void)
103 {
104         unsigned int mask = 0;
105
106         if (boot_cpu_has(X86_FEATURE_FXSR)) {
107                 /* Static because GCC does not get 16-byte stack alignment right: */
108                 static struct fxregs_state fxregs __initdata;
109
110                 asm volatile("fxsave %0" : "+m" (fxregs));
111
112                 mask = fxregs.mxcsr_mask;
113
114                 /*
115                  * If zero then use the default features mask,
116                  * which has all features set, except the
117                  * denormals-are-zero feature bit:
118                  */
119                 if (mask == 0)
120                         mask = 0x0000ffbf;
121         }
122         mxcsr_feature_mask &= mask;
123 }
124
125 /*
126  * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
127  */
128 static void __init fpu__init_system_generic(void)
129 {
130         /*
131          * Set up the legacy init FPU context. (xstate init might overwrite this
132          * with a more modern format, if the CPU supports it.)
133          */
134         fpstate_init(&init_fpstate);
135
136         fpu__init_system_mxcsr();
137 }
138
139 /*
140  * Size of the FPU context state. All tasks in the system use the
141  * same context size, regardless of what portion they use.
142  * This is inherent to the XSAVE architecture which puts all state
143  * components into a single, continuous memory block:
144  */
145 unsigned int fpu_kernel_xstate_size;
146 EXPORT_SYMBOL_GPL(fpu_kernel_xstate_size);
147
148 /* Get alignment of the TYPE. */
149 #define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)
150
151 /*
152  * Enforce that 'MEMBER' is the last field of 'TYPE'.
153  *
154  * Align the computed size with alignment of the TYPE,
155  * because that's how C aligns structs.
156  */
157 #define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
158         BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
159                                            TYPE_ALIGN(TYPE)))
160
161 /*
162  * We append the 'struct fpu' to the task_struct:
163  */
164 static void __init fpu__init_task_struct_size(void)
165 {
166         int task_size = sizeof(struct task_struct);
167
168         /*
169          * Subtract off the static size of the register state.
170          * It potentially has a bunch of padding.
171          */
172         task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);
173
174         /*
175          * Add back the dynamically-calculated register state
176          * size.
177          */
178         task_size += fpu_kernel_xstate_size;
179
180         /*
181          * We dynamically size 'struct fpu', so we require that
182          * it be at the end of 'thread_struct' and that
183          * 'thread_struct' be at the end of 'task_struct'.  If
184          * you hit a compile error here, check the structure to
185          * see if something got added to the end.
186          */
187         CHECK_MEMBER_AT_END_OF(struct fpu, state);
188         CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
189         CHECK_MEMBER_AT_END_OF(struct task_struct, thread);
190
191         arch_task_struct_size = task_size;
192 }
193
194 /*
195  * Set up the user and kernel xstate sizes based on the legacy FPU context size.
196  *
197  * We set this up first, and later it will be overwritten by
198  * fpu__init_system_xstate() if the CPU knows about xstates.
199  */
200 static void __init fpu__init_system_xstate_size_legacy(void)
201 {
202         static int on_boot_cpu __initdata = 1;
203
204         WARN_ON_FPU(!on_boot_cpu);
205         on_boot_cpu = 0;
206
207         /*
208          * Note that xstate sizes might be overwritten later during
209          * fpu__init_system_xstate().
210          */
211
212         if (!boot_cpu_has(X86_FEATURE_FPU)) {
213                 /*
214                  * Disable xsave as we do not support it if i387
215                  * emulation is enabled.
216                  */
217                 setup_clear_cpu_cap(X86_FEATURE_XSAVE);
218                 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
219                 fpu_kernel_xstate_size = sizeof(struct swregs_state);
220         } else {
221                 if (boot_cpu_has(X86_FEATURE_FXSR))
222                         fpu_kernel_xstate_size =
223                                 sizeof(struct fxregs_state);
224                 else
225                         fpu_kernel_xstate_size =
226                                 sizeof(struct fregs_state);
227         }
228
229         fpu_user_xstate_size = fpu_kernel_xstate_size;
230 }
231
232 /*
233  * Find supported xfeatures based on cpu features and command-line input.
234  * This must be called after fpu__init_parse_early_param() is called and
235  * xfeatures_mask is enumerated.
236  */
237 u64 __init fpu__get_supported_xfeatures_mask(void)
238 {
239         return XCNTXT_MASK;
240 }
241
242 /* Legacy code to initialize eager fpu mode. */
243 static void __init fpu__init_system_ctx_switch(void)
244 {
245         static bool on_boot_cpu __initdata = 1;
246
247         WARN_ON_FPU(!on_boot_cpu);
248         on_boot_cpu = 0;
249
250         WARN_ON_FPU(current->thread.fpu.fpstate_active);
251 }
252
253 /*
254  * We parse fpu parameters early because fpu__init_system() is executed
255  * before parse_early_param().
256  */
257 static void __init fpu__init_parse_early_param(void)
258 {
259         if (cmdline_find_option_bool(boot_command_line, "no387"))
260                 setup_clear_cpu_cap(X86_FEATURE_FPU);
261
262         if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
263                 setup_clear_cpu_cap(X86_FEATURE_FXSR);
264                 setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
265                 setup_clear_cpu_cap(X86_FEATURE_XMM);
266         }
267
268         if (cmdline_find_option_bool(boot_command_line, "noxsave"))
269                 fpu__xstate_clear_all_cpu_caps();
270
271         if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
272                 setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
273
274         if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
275                 setup_clear_cpu_cap(X86_FEATURE_XSAVES);
276 }
277
278 /*
279  * Called on the boot CPU once per system bootup, to set up the initial
280  * FPU state that is later cloned into all processes:
281  */
282 void __init fpu__init_system(struct cpuinfo_x86 *c)
283 {
284         fpu__init_parse_early_param();
285         fpu__init_system_early_generic(c);
286
287         /*
288          * The FPU has to be operational for some of the
289          * later FPU init activities:
290          */
291         fpu__init_cpu();
292
293         /*
294          * But don't leave CR0::TS set yet, as some of the FPU setup
295          * methods depend on being able to execute FPU instructions
296          * that will fault on a set TS, such as the FXSAVE in
297          * fpu__init_system_mxcsr().
298          */
299         clts();
300
301         fpu__init_system_generic();
302         fpu__init_system_xstate_size_legacy();
303         fpu__init_system_xstate();
304         fpu__init_task_struct_size();
305
306         fpu__init_system_ctx_switch();
307 }