1455caba252fbb279188c3ac105e220d42656f9e
[pandora-kernel.git] / arch / x86 / kernel / cpu / bugs.c
1 /*
2  *  Copyright (C) 1994  Linus Torvalds
3  *
4  *  Cyrix stuff, June 1998 by:
5  *      - Rafael R. Reilova (moved everything from head.S),
6  *        <rreilova@ececs.uc.edu>
7  *      - Channing Corn (tests & fixes),
8  *      - Andrew D. Balsa (code cleanup).
9  */
10 #include <linux/init.h>
11 #include <linux/utsname.h>
12 #include <linux/cpu.h>
13 #include <asm/bugs.h>
14 #include <asm/processor.h>
15 #include <asm/processor-flags.h>
16 #include <asm/i387.h>
17 #include <asm/msr.h>
18 #include <asm/paravirt.h>
19 #include <asm/alternative.h>
20 #include <asm/pgtable.h>
21 #include <asm/cacheflush.h>
22
23 #ifdef CONFIG_X86_32
24
25 static int __init no_halt(char *s)
26 {
27         WARN_ONCE(1, "\"no-hlt\" is deprecated, please use \"idle=poll\"\n");
28         boot_cpu_data.hlt_works_ok = 0;
29         return 1;
30 }
31
32 __setup("no-hlt", no_halt);
33
34 static int __init no_387(char *s)
35 {
36         boot_cpu_data.hard_math = 0;
37         write_cr0(X86_CR0_TS | X86_CR0_EM | X86_CR0_MP | read_cr0());
38         return 1;
39 }
40
41 __setup("no387", no_387);
42
43 static double __initdata x = 4195835.0;
44 static double __initdata y = 3145727.0;
45
46 /*
47  * This used to check for exceptions..
48  * However, it turns out that to support that,
49  * the XMM trap handlers basically had to
50  * be buggy. So let's have a correct XMM trap
51  * handler, and forget about printing out
52  * some status at boot.
53  *
54  * We should really only care about bugs here
55  * anyway. Not features.
56  */
57 static void __init check_fpu(void)
58 {
59         s32 fdiv_bug;
60
61         if (!boot_cpu_data.hard_math) {
62 #ifndef CONFIG_MATH_EMULATION
63                 printk(KERN_EMERG "No coprocessor found and no math emulation present.\n");
64                 printk(KERN_EMERG "Giving up.\n");
65                 for (;;) ;
66 #endif
67                 return;
68         }
69
70         kernel_fpu_begin();
71
72         /*
73          * trap_init() enabled FXSR and company _before_ testing for FP
74          * problems here.
75          *
76          * Test for the divl bug..
77          */
78         __asm__("fninit\n\t"
79                 "fldl %1\n\t"
80                 "fdivl %2\n\t"
81                 "fmull %2\n\t"
82                 "fldl %1\n\t"
83                 "fsubp %%st,%%st(1)\n\t"
84                 "fistpl %0\n\t"
85                 "fwait\n\t"
86                 "fninit"
87                 : "=m" (*&fdiv_bug)
88                 : "m" (*&x), "m" (*&y));
89
90         kernel_fpu_end();
91
92         boot_cpu_data.fdiv_bug = fdiv_bug;
93         if (boot_cpu_data.fdiv_bug)
94                 printk(KERN_WARNING "Hmm, FPU with FDIV bug.\n");
95 }
96
97 static void __init check_hlt(void)
98 {
99         if (boot_cpu_data.x86 >= 5 || paravirt_enabled())
100                 return;
101
102         printk(KERN_INFO "Checking 'hlt' instruction... ");
103         if (!boot_cpu_data.hlt_works_ok) {
104                 printk("disabled\n");
105                 return;
106         }
107         halt();
108         halt();
109         halt();
110         halt();
111         printk(KERN_CONT "OK.\n");
112 }
113
114 /*
115  *      Most 386 processors have a bug where a POPAD can lock the
116  *      machine even from user space.
117  */
118
119 static void __init check_popad(void)
120 {
121 #ifndef CONFIG_X86_POPAD_OK
122         int res, inp = (int) &res;
123
124         printk(KERN_INFO "Checking for popad bug... ");
125         __asm__ __volatile__(
126           "movl $12345678,%%eax; movl $0,%%edi; pusha; popa; movl (%%edx,%%edi),%%ecx "
127           : "=&a" (res)
128           : "d" (inp)
129           : "ecx", "edi");
130         /*
131          * If this fails, it means that any user program may lock the
132          * CPU hard. Too bad.
133          */
134         if (res != 12345678)
135                 printk(KERN_CONT "Buggy.\n");
136         else
137                 printk(KERN_CONT "OK.\n");
138 #endif
139 }
140
141 /*
142  * Check whether we are able to run this kernel safely on SMP.
143  *
144  * - In order to run on a i386, we need to be compiled for i386
145  *   (for due to lack of "invlpg" and working WP on a i386)
146  * - In order to run on anything without a TSC, we need to be
147  *   compiled for a i486.
148  */
149
150 static void __init check_config(void)
151 {
152 /*
153  * We'd better not be a i386 if we're configured to use some
154  * i486+ only features! (WP works in supervisor mode and the
155  * new "invlpg" and "bswap" instructions)
156  */
157 #if defined(CONFIG_X86_WP_WORKS_OK) || defined(CONFIG_X86_INVLPG) || \
158         defined(CONFIG_X86_BSWAP)
159         if (boot_cpu_data.x86 == 3)
160                 panic("Kernel requires i486+ for 'invlpg' and other features");
161 #endif
162 }
163
164 #endif /* CONFIG_X86_32 */
165
166 void __init check_bugs(void)
167 {
168 #ifdef CONFIG_X86_32
169         /*
170          * Regardless of whether PCID is enumerated, the SDM says
171          * that it can't be enabled in 32-bit mode.
172          */
173         setup_clear_cpu_cap(X86_FEATURE_PCID);
174 #endif
175
176         identify_boot_cpu();
177
178         if (!IS_ENABLED(CONFIG_SMP)) {
179                 pr_info("CPU: ");
180                 print_cpu_info(&boot_cpu_data);
181         }
182
183 #ifdef CONFIG_X86_32
184         check_config();
185         check_fpu();
186         check_hlt();
187         check_popad();
188         init_utsname()->machine[1] =
189                 '0' + (boot_cpu_data.x86 > 6 ? 6 : boot_cpu_data.x86);
190         alternative_instructions();
191 #else /* CONFIG_X86_64 */
192         alternative_instructions();
193
194         /*
195          * Make sure the first 2MB area is not mapped by huge pages
196          * There are typically fixed size MTRRs in there and overlapping
197          * MTRRs into large pages causes slow downs.
198          *
199          * Right now we don't do that with gbpages because there seems
200          * very little benefit for that case.
201          */
202         if (!direct_gbpages)
203                 set_memory_4k((unsigned long)__va(0), 1);
204 #endif
205 }
206
207 #ifdef CONFIG_SYSFS
208 ssize_t cpu_show_meltdown(struct sysdev_class *dev,
209                           struct sysdev_class_attribute *attr, char *buf)
210 {
211         if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
212                 return sprintf(buf, "Not affected\n");
213         if (boot_cpu_has(X86_FEATURE_KAISER))
214                 return sprintf(buf, "Mitigation: PTI\n");
215         return sprintf(buf, "Vulnerable\n");
216 }
217
218 ssize_t cpu_show_spectre_v1(struct sysdev_class *dev,
219                             struct sysdev_class_attribute *attr, char *buf)
220 {
221         if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V1))
222                 return sprintf(buf, "Not affected\n");
223         return sprintf(buf, "Vulnerable\n");
224 }
225
226 ssize_t cpu_show_spectre_v2(struct sysdev_class *dev,
227                             struct sysdev_class_attribute *attr, char *buf)
228 {
229         if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
230                 return sprintf(buf, "Not affected\n");
231         return sprintf(buf, "Vulnerable\n");
232 }
233 #endif