• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/pgtable.h>
4 
5 #include <linux/string.h>
6 #include <linux/bitops.h>
7 #include <linux/smp.h>
8 #include <linux/sched.h>
9 #include <linux/sched/clock.h>
10 #include <linux/semaphore.h>
11 #include <linux/thread_info.h>
12 #include <linux/init.h>
13 #include <linux/uaccess.h>
14 #include <linux/workqueue.h>
15 #include <linux/delay.h>
16 #include <linux/cpuhotplug.h>
17 
18 #include <asm/cpufeature.h>
19 #include <asm/msr.h>
20 #include <asm/bugs.h>
21 #include <asm/cpu.h>
22 #include <asm/intel-family.h>
23 #include <asm/microcode.h>
24 #include <asm/hwcap2.h>
25 #include <asm/elf.h>
26 #include <asm/cpu_device_id.h>
27 #include <asm/cmdline.h>
28 #include <asm/traps.h>
29 #include <asm/resctrl.h>
30 #include <asm/numa.h>
31 #include <asm/thermal.h>
32 
33 #ifdef CONFIG_X86_64
34 #include <linux/topology.h>
35 #endif
36 
37 #include "cpu.h"
38 
39 #ifdef CONFIG_X86_LOCAL_APIC
40 #include <asm/mpspec.h>
41 #include <asm/apic.h>
42 #endif
43 
44 enum split_lock_detect_state {
45 	sld_off = 0,
46 	sld_warn,
47 	sld_fatal,
48 	sld_ratelimit,
49 };
50 
51 /*
52  * Default to sld_off because most systems do not support split lock detection.
53  * sld_state_setup() will switch this to sld_warn on systems that support
54  * split lock/bus lock detect, unless there is a command line override.
55  */
56 static enum split_lock_detect_state sld_state __ro_after_init = sld_off;
57 static u64 msr_test_ctrl_cache __ro_after_init;
58 
59 /*
60  * With a name like MSR_TEST_CTL it should go without saying, but don't touch
61  * MSR_TEST_CTL unless the CPU is one of the whitelisted models.  Writing it
62  * on CPUs that do not support SLD can cause fireworks, even when writing '0'.
63  */
64 static bool cpu_model_supports_sld __ro_after_init;
65 
66 /*
67  * Processors which have self-snooping capability can handle conflicting
68  * memory type across CPUs by snooping its own cache. However, there exists
69  * CPU models in which having conflicting memory types still leads to
70  * unpredictable behavior, machine check errors, or hangs. Clear this
71  * feature to prevent its use on machines with known erratas.
72  */
check_memory_type_self_snoop_errata(struct cpuinfo_x86 * c)73 static void check_memory_type_self_snoop_errata(struct cpuinfo_x86 *c)
74 {
75 	switch (c->x86_vfm) {
76 	case INTEL_CORE_YONAH:
77 	case INTEL_CORE2_MEROM:
78 	case INTEL_CORE2_MEROM_L:
79 	case INTEL_CORE2_PENRYN:
80 	case INTEL_CORE2_DUNNINGTON:
81 	case INTEL_NEHALEM:
82 	case INTEL_NEHALEM_G:
83 	case INTEL_NEHALEM_EP:
84 	case INTEL_NEHALEM_EX:
85 	case INTEL_WESTMERE:
86 	case INTEL_WESTMERE_EP:
87 	case INTEL_SANDYBRIDGE:
88 		setup_clear_cpu_cap(X86_FEATURE_SELFSNOOP);
89 	}
90 }
91 
92 static bool ring3mwait_disabled __read_mostly;
93 
ring3mwait_disable(char * __unused)94 static int __init ring3mwait_disable(char *__unused)
95 {
96 	ring3mwait_disabled = true;
97 	return 1;
98 }
99 __setup("ring3mwait=disable", ring3mwait_disable);
100 
probe_xeon_phi_r3mwait(struct cpuinfo_x86 * c)101 static void probe_xeon_phi_r3mwait(struct cpuinfo_x86 *c)
102 {
103 	/*
104 	 * Ring 3 MONITOR/MWAIT feature cannot be detected without
105 	 * cpu model and family comparison.
106 	 */
107 	if (c->x86 != 6)
108 		return;
109 	switch (c->x86_vfm) {
110 	case INTEL_XEON_PHI_KNL:
111 	case INTEL_XEON_PHI_KNM:
112 		break;
113 	default:
114 		return;
115 	}
116 
117 	if (ring3mwait_disabled)
118 		return;
119 
120 	set_cpu_cap(c, X86_FEATURE_RING3MWAIT);
121 	this_cpu_or(msr_misc_features_shadow,
122 		    1UL << MSR_MISC_FEATURES_ENABLES_RING3MWAIT_BIT);
123 
124 	if (c == &boot_cpu_data)
125 		ELF_HWCAP2 |= HWCAP2_RING3MWAIT;
126 }
127 
128 /*
129  * Early microcode releases for the Spectre v2 mitigation were broken.
130  * Information taken from;
131  * - https://newsroom.intel.com/wp-content/uploads/sites/11/2018/03/microcode-update-guidance.pdf
132  * - https://kb.vmware.com/s/article/52345
133  * - Microcode revisions observed in the wild
134  * - Release note from 20180108 microcode release
135  */
136 struct sku_microcode {
137 	u32 vfm;
138 	u8 stepping;
139 	u32 microcode;
140 };
141 static const struct sku_microcode spectre_bad_microcodes[] = {
142 	{ INTEL_KABYLAKE,	0x0B,	0x80 },
143 	{ INTEL_KABYLAKE,	0x0A,	0x80 },
144 	{ INTEL_KABYLAKE,	0x09,	0x80 },
145 	{ INTEL_KABYLAKE_L,	0x0A,	0x80 },
146 	{ INTEL_KABYLAKE_L,	0x09,	0x80 },
147 	{ INTEL_SKYLAKE_X,	0x03,	0x0100013e },
148 	{ INTEL_SKYLAKE_X,	0x04,	0x0200003c },
149 	{ INTEL_BROADWELL,	0x04,	0x28 },
150 	{ INTEL_BROADWELL_G,	0x01,	0x1b },
151 	{ INTEL_BROADWELL_D,	0x02,	0x14 },
152 	{ INTEL_BROADWELL_D,	0x03,	0x07000011 },
153 	{ INTEL_BROADWELL_X,	0x01,	0x0b000025 },
154 	{ INTEL_HASWELL_L,	0x01,	0x21 },
155 	{ INTEL_HASWELL_G,	0x01,	0x18 },
156 	{ INTEL_HASWELL,	0x03,	0x23 },
157 	{ INTEL_HASWELL_X,	0x02,	0x3b },
158 	{ INTEL_HASWELL_X,	0x04,	0x10 },
159 	{ INTEL_IVYBRIDGE_X,	0x04,	0x42a },
160 	/* Observed in the wild */
161 	{ INTEL_SANDYBRIDGE_X,	0x06,	0x61b },
162 	{ INTEL_SANDYBRIDGE_X,	0x07,	0x712 },
163 };
164 
bad_spectre_microcode(struct cpuinfo_x86 * c)165 static bool bad_spectre_microcode(struct cpuinfo_x86 *c)
166 {
167 	int i;
168 
169 	/*
170 	 * We know that the hypervisor lie to us on the microcode version so
171 	 * we may as well hope that it is running the correct version.
172 	 */
173 	if (cpu_has(c, X86_FEATURE_HYPERVISOR))
174 		return false;
175 
176 	for (i = 0; i < ARRAY_SIZE(spectre_bad_microcodes); i++) {
177 		if (c->x86_vfm == spectre_bad_microcodes[i].vfm &&
178 		    c->x86_stepping == spectre_bad_microcodes[i].stepping)
179 			return (c->microcode <= spectre_bad_microcodes[i].microcode);
180 	}
181 	return false;
182 }
183 
184 #define MSR_IA32_TME_ACTIVATE		0x982
185 
186 /* Helpers to access TME_ACTIVATE MSR */
187 #define TME_ACTIVATE_LOCKED(x)		(x & 0x1)
188 #define TME_ACTIVATE_ENABLED(x)		(x & 0x2)
189 
190 #define TME_ACTIVATE_KEYID_BITS(x)	((x >> 32) & 0xf)	/* Bits 35:32 */
191 
detect_tme_early(struct cpuinfo_x86 * c)192 static void detect_tme_early(struct cpuinfo_x86 *c)
193 {
194 	u64 tme_activate;
195 	int keyid_bits;
196 
197 	rdmsrl(MSR_IA32_TME_ACTIVATE, tme_activate);
198 
199 	if (!TME_ACTIVATE_LOCKED(tme_activate) || !TME_ACTIVATE_ENABLED(tme_activate)) {
200 		pr_info_once("x86/tme: not enabled by BIOS\n");
201 		clear_cpu_cap(c, X86_FEATURE_TME);
202 		return;
203 	}
204 	pr_info_once("x86/tme: enabled by BIOS\n");
205 	keyid_bits = TME_ACTIVATE_KEYID_BITS(tme_activate);
206 	if (!keyid_bits)
207 		return;
208 
209 	/*
210 	 * KeyID bits are set by BIOS and can be present regardless
211 	 * of whether the kernel is using them. They effectively lower
212 	 * the number of physical address bits.
213 	 *
214 	 * Update cpuinfo_x86::x86_phys_bits accordingly.
215 	 */
216 	c->x86_phys_bits -= keyid_bits;
217 	pr_info_once("x86/mktme: BIOS enabled: x86_phys_bits reduced by %d\n",
218 		     keyid_bits);
219 }
220 
intel_unlock_cpuid_leafs(struct cpuinfo_x86 * c)221 void intel_unlock_cpuid_leafs(struct cpuinfo_x86 *c)
222 {
223 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
224 		return;
225 
226 	if (c->x86 < 6 || (c->x86 == 6 && c->x86_model < 0xd))
227 		return;
228 
229 	/*
230 	 * The BIOS can have limited CPUID to leaf 2, which breaks feature
231 	 * enumeration. Unlock it and update the maximum leaf info.
232 	 */
233 	if (msr_clear_bit(MSR_IA32_MISC_ENABLE, MSR_IA32_MISC_ENABLE_LIMIT_CPUID_BIT) > 0)
234 		c->cpuid_level = cpuid_eax(0);
235 }
236 
early_init_intel(struct cpuinfo_x86 * c)237 static void early_init_intel(struct cpuinfo_x86 *c)
238 {
239 	u64 misc_enable;
240 
241 	if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
242 		(c->x86 == 0x6 && c->x86_model >= 0x0e))
243 		set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
244 
245 	if (c->x86 >= 6 && !cpu_has(c, X86_FEATURE_IA64))
246 		c->microcode = intel_get_microcode_revision();
247 
248 	/* Now if any of them are set, check the blacklist and clear the lot */
249 	if ((cpu_has(c, X86_FEATURE_SPEC_CTRL) ||
250 	     cpu_has(c, X86_FEATURE_INTEL_STIBP) ||
251 	     cpu_has(c, X86_FEATURE_IBRS) || cpu_has(c, X86_FEATURE_IBPB) ||
252 	     cpu_has(c, X86_FEATURE_STIBP)) && bad_spectre_microcode(c)) {
253 		pr_warn("Intel Spectre v2 broken microcode detected; disabling Speculation Control\n");
254 		setup_clear_cpu_cap(X86_FEATURE_IBRS);
255 		setup_clear_cpu_cap(X86_FEATURE_IBPB);
256 		setup_clear_cpu_cap(X86_FEATURE_STIBP);
257 		setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL);
258 		setup_clear_cpu_cap(X86_FEATURE_MSR_SPEC_CTRL);
259 		setup_clear_cpu_cap(X86_FEATURE_INTEL_STIBP);
260 		setup_clear_cpu_cap(X86_FEATURE_SSBD);
261 		setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL_SSBD);
262 	}
263 
264 	/*
265 	 * Atom erratum AAE44/AAF40/AAG38/AAH41:
266 	 *
267 	 * A race condition between speculative fetches and invalidating
268 	 * a large page.  This is worked around in microcode, but we
269 	 * need the microcode to have already been loaded... so if it is
270 	 * not, recommend a BIOS update and disable large pages.
271 	 */
272 	if (c->x86_vfm == INTEL_ATOM_BONNELL && c->x86_stepping <= 2 &&
273 	    c->microcode < 0x20e) {
274 		pr_warn("Atom PSE erratum detected, BIOS microcode update recommended\n");
275 		clear_cpu_cap(c, X86_FEATURE_PSE);
276 	}
277 
278 #ifdef CONFIG_X86_64
279 	set_cpu_cap(c, X86_FEATURE_SYSENTER32);
280 #else
281 	/* Netburst reports 64 bytes clflush size, but does IO in 128 bytes */
282 	if (c->x86 == 15 && c->x86_cache_alignment == 64)
283 		c->x86_cache_alignment = 128;
284 #endif
285 
286 	/* CPUID workaround for 0F33/0F34 CPU */
287 	if (c->x86 == 0xF && c->x86_model == 0x3
288 	    && (c->x86_stepping == 0x3 || c->x86_stepping == 0x4))
289 		c->x86_phys_bits = 36;
290 
291 	/*
292 	 * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
293 	 * with P/T states and does not stop in deep C-states.
294 	 *
295 	 * It is also reliable across cores and sockets. (but not across
296 	 * cabinets - we turn it off in that case explicitly.)
297 	 */
298 	if (c->x86_power & (1 << 8)) {
299 		set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
300 		set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
301 	}
302 
303 	/* Penwell and Cloverview have the TSC which doesn't sleep on S3 */
304 	switch (c->x86_vfm) {
305 	case INTEL_ATOM_SALTWELL_MID:
306 	case INTEL_ATOM_SALTWELL_TABLET:
307 	case INTEL_ATOM_SILVERMONT_MID:
308 	case INTEL_ATOM_AIRMONT_NP:
309 		set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC_S3);
310 		break;
311 	}
312 
313 	/*
314 	 * PAT is broken on early family 6 CPUs, the last of which
315 	 * is "Yonah" where the erratum is named "AN7":
316 	 *
317 	 * 	Page with PAT (Page Attribute Table) Set to USWC
318 	 * 	(Uncacheable Speculative Write Combine) While
319 	 * 	Associated MTRR (Memory Type Range Register) Is UC
320 	 * 	(Uncacheable) May Consolidate to UC
321 	 *
322 	 * Disable PAT and fall back to MTRR on these CPUs.
323 	 */
324 	if (c->x86_vfm >= INTEL_PENTIUM_PRO &&
325 	    c->x86_vfm <= INTEL_CORE_YONAH)
326 		clear_cpu_cap(c, X86_FEATURE_PAT);
327 
328 	/*
329 	 * If fast string is not enabled in IA32_MISC_ENABLE for any reason,
330 	 * clear the fast string and enhanced fast string CPU capabilities.
331 	 */
332 	if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
333 		rdmsrl(MSR_IA32_MISC_ENABLE, misc_enable);
334 		if (!(misc_enable & MSR_IA32_MISC_ENABLE_FAST_STRING)) {
335 			pr_info("Disabled fast string operations\n");
336 			setup_clear_cpu_cap(X86_FEATURE_REP_GOOD);
337 			setup_clear_cpu_cap(X86_FEATURE_ERMS);
338 		}
339 	}
340 
341 	/*
342 	 * Intel Quark Core DevMan_001.pdf section 6.4.11
343 	 * "The operating system also is required to invalidate (i.e., flush)
344 	 *  the TLB when any changes are made to any of the page table entries.
345 	 *  The operating system must reload CR3 to cause the TLB to be flushed"
346 	 *
347 	 * As a result, boot_cpu_has(X86_FEATURE_PGE) in arch/x86/include/asm/tlbflush.h
348 	 * should be false so that __flush_tlb_all() causes CR3 instead of CR4.PGE
349 	 * to be modified.
350 	 */
351 	if (c->x86_vfm == INTEL_QUARK_X1000) {
352 		pr_info("Disabling PGE capability bit\n");
353 		setup_clear_cpu_cap(X86_FEATURE_PGE);
354 	}
355 
356 	check_memory_type_self_snoop_errata(c);
357 
358 	/*
359 	 * Adjust the number of physical bits early because it affects the
360 	 * valid bits of the MTRR mask registers.
361 	 */
362 	if (cpu_has(c, X86_FEATURE_TME))
363 		detect_tme_early(c);
364 }
365 
bsp_init_intel(struct cpuinfo_x86 * c)366 static void bsp_init_intel(struct cpuinfo_x86 *c)
367 {
368 	resctrl_cpu_detect(c);
369 }
370 
371 #ifdef CONFIG_X86_32
372 /*
373  *	Early probe support logic for ppro memory erratum #50
374  *
375  *	This is called before we do cpu ident work
376  */
377 
ppro_with_ram_bug(void)378 int ppro_with_ram_bug(void)
379 {
380 	/* Uses data from early_cpu_detect now */
381 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
382 	    boot_cpu_data.x86 == 6 &&
383 	    boot_cpu_data.x86_model == 1 &&
384 	    boot_cpu_data.x86_stepping < 8) {
385 		pr_info("Pentium Pro with Errata#50 detected. Taking evasive action.\n");
386 		return 1;
387 	}
388 	return 0;
389 }
390 
intel_smp_check(struct cpuinfo_x86 * c)391 static void intel_smp_check(struct cpuinfo_x86 *c)
392 {
393 	/* calling is from identify_secondary_cpu() ? */
394 	if (!c->cpu_index)
395 		return;
396 
397 	/*
398 	 * Mask B, Pentium, but not Pentium MMX
399 	 */
400 	if (c->x86 == 5 &&
401 	    c->x86_stepping >= 1 && c->x86_stepping <= 4 &&
402 	    c->x86_model <= 3) {
403 		/*
404 		 * Remember we have B step Pentia with bugs
405 		 */
406 		WARN_ONCE(1, "WARNING: SMP operation may be unreliable"
407 				    "with B stepping processors.\n");
408 	}
409 }
410 
411 static int forcepae;
forcepae_setup(char * __unused)412 static int __init forcepae_setup(char *__unused)
413 {
414 	forcepae = 1;
415 	return 1;
416 }
417 __setup("forcepae", forcepae_setup);
418 
intel_workarounds(struct cpuinfo_x86 * c)419 static void intel_workarounds(struct cpuinfo_x86 *c)
420 {
421 #ifdef CONFIG_X86_F00F_BUG
422 	/*
423 	 * All models of Pentium and Pentium with MMX technology CPUs
424 	 * have the F0 0F bug, which lets nonprivileged users lock up the
425 	 * system. Announce that the fault handler will be checking for it.
426 	 * The Quark is also family 5, but does not have the same bug.
427 	 */
428 	clear_cpu_bug(c, X86_BUG_F00F);
429 	if (c->x86 == 5 && c->x86_model < 9) {
430 		static int f00f_workaround_enabled;
431 
432 		set_cpu_bug(c, X86_BUG_F00F);
433 		if (!f00f_workaround_enabled) {
434 			pr_notice("Intel Pentium with F0 0F bug - workaround enabled.\n");
435 			f00f_workaround_enabled = 1;
436 		}
437 	}
438 #endif
439 
440 	/*
441 	 * SEP CPUID bug: Pentium Pro reports SEP but doesn't have it until
442 	 * model 3 mask 3
443 	 */
444 	if ((c->x86<<8 | c->x86_model<<4 | c->x86_stepping) < 0x633)
445 		clear_cpu_cap(c, X86_FEATURE_SEP);
446 
447 	/*
448 	 * PAE CPUID issue: many Pentium M report no PAE but may have a
449 	 * functionally usable PAE implementation.
450 	 * Forcefully enable PAE if kernel parameter "forcepae" is present.
451 	 */
452 	if (forcepae) {
453 		pr_warn("PAE forced!\n");
454 		set_cpu_cap(c, X86_FEATURE_PAE);
455 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_NOW_UNRELIABLE);
456 	}
457 
458 	/*
459 	 * P4 Xeon erratum 037 workaround.
460 	 * Hardware prefetcher may cause stale data to be loaded into the cache.
461 	 */
462 	if ((c->x86 == 15) && (c->x86_model == 1) && (c->x86_stepping == 1)) {
463 		if (msr_set_bit(MSR_IA32_MISC_ENABLE,
464 				MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE_BIT) > 0) {
465 			pr_info("CPU: C0 stepping P4 Xeon detected.\n");
466 			pr_info("CPU: Disabling hardware prefetching (Erratum 037)\n");
467 		}
468 	}
469 
470 	/*
471 	 * See if we have a good local APIC by checking for buggy Pentia,
472 	 * i.e. all B steppings and the C2 stepping of P54C when using their
473 	 * integrated APIC (see 11AP erratum in "Pentium Processor
474 	 * Specification Update").
475 	 */
476 	if (boot_cpu_has(X86_FEATURE_APIC) && (c->x86<<8 | c->x86_model<<4) == 0x520 &&
477 	    (c->x86_stepping < 0x6 || c->x86_stepping == 0xb))
478 		set_cpu_bug(c, X86_BUG_11AP);
479 
480 
481 #ifdef CONFIG_X86_INTEL_USERCOPY
482 	/*
483 	 * Set up the preferred alignment for movsl bulk memory moves
484 	 */
485 	switch (c->x86) {
486 	case 4:		/* 486: untested */
487 		break;
488 	case 5:		/* Old Pentia: untested */
489 		break;
490 	case 6:		/* PII/PIII only like movsl with 8-byte alignment */
491 		movsl_mask.mask = 7;
492 		break;
493 	case 15:	/* P4 is OK down to 8-byte alignment */
494 		movsl_mask.mask = 7;
495 		break;
496 	}
497 #endif
498 
499 	intel_smp_check(c);
500 }
501 #else
intel_workarounds(struct cpuinfo_x86 * c)502 static void intel_workarounds(struct cpuinfo_x86 *c)
503 {
504 }
505 #endif
506 
srat_detect_node(struct cpuinfo_x86 * c)507 static void srat_detect_node(struct cpuinfo_x86 *c)
508 {
509 #ifdef CONFIG_NUMA
510 	unsigned node;
511 	int cpu = smp_processor_id();
512 
513 	/* Don't do the funky fallback heuristics the AMD version employs
514 	   for now. */
515 	node = numa_cpu_node(cpu);
516 	if (node == NUMA_NO_NODE || !node_online(node)) {
517 		/* reuse the value from init_cpu_to_node() */
518 		node = cpu_to_node(cpu);
519 	}
520 	numa_set_node(cpu, node);
521 #endif
522 }
523 
init_cpuid_fault(struct cpuinfo_x86 * c)524 static void init_cpuid_fault(struct cpuinfo_x86 *c)
525 {
526 	u64 msr;
527 
528 	if (!rdmsrl_safe(MSR_PLATFORM_INFO, &msr)) {
529 		if (msr & MSR_PLATFORM_INFO_CPUID_FAULT)
530 			set_cpu_cap(c, X86_FEATURE_CPUID_FAULT);
531 	}
532 }
533 
init_intel_misc_features(struct cpuinfo_x86 * c)534 static void init_intel_misc_features(struct cpuinfo_x86 *c)
535 {
536 	u64 msr;
537 
538 	if (rdmsrl_safe(MSR_MISC_FEATURES_ENABLES, &msr))
539 		return;
540 
541 	/* Clear all MISC features */
542 	this_cpu_write(msr_misc_features_shadow, 0);
543 
544 	/* Check features and update capabilities and shadow control bits */
545 	init_cpuid_fault(c);
546 	probe_xeon_phi_r3mwait(c);
547 
548 	msr = this_cpu_read(msr_misc_features_shadow);
549 	wrmsrl(MSR_MISC_FEATURES_ENABLES, msr);
550 }
551 
552 static void split_lock_init(void);
553 static void bus_lock_init(void);
554 
init_intel(struct cpuinfo_x86 * c)555 static void init_intel(struct cpuinfo_x86 *c)
556 {
557 	early_init_intel(c);
558 
559 	intel_workarounds(c);
560 
561 	init_intel_cacheinfo(c);
562 
563 	if (c->cpuid_level > 9) {
564 		unsigned eax = cpuid_eax(10);
565 		/* Check for version and the number of counters */
566 		if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
567 			set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
568 	}
569 
570 	if (cpu_has(c, X86_FEATURE_XMM2))
571 		set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
572 
573 	if (boot_cpu_has(X86_FEATURE_DS)) {
574 		unsigned int l1, l2;
575 
576 		rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
577 		if (!(l1 & MSR_IA32_MISC_ENABLE_BTS_UNAVAIL))
578 			set_cpu_cap(c, X86_FEATURE_BTS);
579 		if (!(l1 & MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL))
580 			set_cpu_cap(c, X86_FEATURE_PEBS);
581 	}
582 
583 	if (boot_cpu_has(X86_FEATURE_CLFLUSH) &&
584 	    (c->x86_vfm == INTEL_CORE2_DUNNINGTON ||
585 	     c->x86_vfm == INTEL_NEHALEM_EX ||
586 	     c->x86_vfm == INTEL_WESTMERE_EX))
587 		set_cpu_bug(c, X86_BUG_CLFLUSH_MONITOR);
588 
589 	if (boot_cpu_has(X86_FEATURE_MWAIT) &&
590 	    (c->x86_vfm == INTEL_ATOM_GOLDMONT ||
591 	     c->x86_vfm == INTEL_LUNARLAKE_M))
592 		set_cpu_bug(c, X86_BUG_MONITOR);
593 
594 #ifdef CONFIG_X86_64
595 	if (c->x86 == 15)
596 		c->x86_cache_alignment = c->x86_clflush_size * 2;
597 	if (c->x86 == 6)
598 		set_cpu_cap(c, X86_FEATURE_REP_GOOD);
599 #else
600 	/*
601 	 * Names for the Pentium II/Celeron processors
602 	 * detectable only by also checking the cache size.
603 	 * Dixon is NOT a Celeron.
604 	 */
605 	if (c->x86 == 6) {
606 		unsigned int l2 = c->x86_cache_size;
607 		char *p = NULL;
608 
609 		switch (c->x86_model) {
610 		case 5:
611 			if (l2 == 0)
612 				p = "Celeron (Covington)";
613 			else if (l2 == 256)
614 				p = "Mobile Pentium II (Dixon)";
615 			break;
616 
617 		case 6:
618 			if (l2 == 128)
619 				p = "Celeron (Mendocino)";
620 			else if (c->x86_stepping == 0 || c->x86_stepping == 5)
621 				p = "Celeron-A";
622 			break;
623 
624 		case 8:
625 			if (l2 == 128)
626 				p = "Celeron (Coppermine)";
627 			break;
628 		}
629 
630 		if (p)
631 			strcpy(c->x86_model_id, p);
632 	}
633 
634 	if (c->x86 == 15)
635 		set_cpu_cap(c, X86_FEATURE_P4);
636 	if (c->x86 == 6)
637 		set_cpu_cap(c, X86_FEATURE_P3);
638 #endif
639 
640 	/* Work around errata */
641 	srat_detect_node(c);
642 
643 	init_ia32_feat_ctl(c);
644 
645 	init_intel_misc_features(c);
646 
647 	split_lock_init();
648 	bus_lock_init();
649 
650 	intel_init_thermal(c);
651 }
652 
653 #ifdef CONFIG_X86_32
intel_size_cache(struct cpuinfo_x86 * c,unsigned int size)654 static unsigned int intel_size_cache(struct cpuinfo_x86 *c, unsigned int size)
655 {
656 	/*
657 	 * Intel PIII Tualatin. This comes in two flavours.
658 	 * One has 256kb of cache, the other 512. We have no way
659 	 * to determine which, so we use a boottime override
660 	 * for the 512kb model, and assume 256 otherwise.
661 	 */
662 	if ((c->x86 == 6) && (c->x86_model == 11) && (size == 0))
663 		size = 256;
664 
665 	/*
666 	 * Intel Quark SoC X1000 contains a 4-way set associative
667 	 * 16K cache with a 16 byte cache line and 256 lines per tag
668 	 */
669 	if ((c->x86 == 5) && (c->x86_model == 9))
670 		size = 16;
671 	return size;
672 }
673 #endif
674 
675 #define TLB_INST_4K		0x01
676 #define TLB_INST_4M		0x02
677 #define TLB_INST_2M_4M		0x03
678 
679 #define TLB_INST_ALL		0x05
680 #define TLB_INST_1G		0x06
681 
682 #define TLB_DATA_4K		0x11
683 #define TLB_DATA_4M		0x12
684 #define TLB_DATA_2M_4M		0x13
685 #define TLB_DATA_4K_4M		0x14
686 
687 #define TLB_DATA_1G		0x16
688 #define TLB_DATA_1G_2M_4M	0x17
689 
690 #define TLB_DATA0_4K		0x21
691 #define TLB_DATA0_4M		0x22
692 #define TLB_DATA0_2M_4M		0x23
693 
694 #define STLB_4K			0x41
695 #define STLB_4K_2M		0x42
696 
697 /*
698  * All of leaf 0x2's one-byte TLB descriptors implies the same number of
699  * entries for their respective TLB types.  The 0x63 descriptor is an
700  * exception: it implies 4 dTLB entries for 1GB pages 32 dTLB entries
701  * for 2MB or 4MB pages.  Encode descriptor 0x63 dTLB entry count for
702  * 2MB/4MB pages here, as its count for dTLB 1GB pages is already at the
703  * intel_tlb_table[] mapping.
704  */
705 #define TLB_0x63_2M_4M_ENTRIES	32
706 
707 static const struct _tlb_table intel_tlb_table[] = {
708 	{ 0x01, TLB_INST_4K,		32,	" TLB_INST 4 KByte pages, 4-way set associative" },
709 	{ 0x02, TLB_INST_4M,		2,	" TLB_INST 4 MByte pages, full associative" },
710 	{ 0x03, TLB_DATA_4K,		64,	" TLB_DATA 4 KByte pages, 4-way set associative" },
711 	{ 0x04, TLB_DATA_4M,		8,	" TLB_DATA 4 MByte pages, 4-way set associative" },
712 	{ 0x05, TLB_DATA_4M,		32,	" TLB_DATA 4 MByte pages, 4-way set associative" },
713 	{ 0x0b, TLB_INST_4M,		4,	" TLB_INST 4 MByte pages, 4-way set associative" },
714 	{ 0x4f, TLB_INST_4K,		32,	" TLB_INST 4 KByte pages" },
715 	{ 0x50, TLB_INST_ALL,		64,	" TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
716 	{ 0x51, TLB_INST_ALL,		128,	" TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
717 	{ 0x52, TLB_INST_ALL,		256,	" TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
718 	{ 0x55, TLB_INST_2M_4M,		7,	" TLB_INST 2-MByte or 4-MByte pages, fully associative" },
719 	{ 0x56, TLB_DATA0_4M,		16,	" TLB_DATA0 4 MByte pages, 4-way set associative" },
720 	{ 0x57, TLB_DATA0_4K,		16,	" TLB_DATA0 4 KByte pages, 4-way associative" },
721 	{ 0x59, TLB_DATA0_4K,		16,	" TLB_DATA0 4 KByte pages, fully associative" },
722 	{ 0x5a, TLB_DATA0_2M_4M,	32,	" TLB_DATA0 2-MByte or 4 MByte pages, 4-way set associative" },
723 	{ 0x5b, TLB_DATA_4K_4M,		64,	" TLB_DATA 4 KByte and 4 MByte pages" },
724 	{ 0x5c, TLB_DATA_4K_4M,		128,	" TLB_DATA 4 KByte and 4 MByte pages" },
725 	{ 0x5d, TLB_DATA_4K_4M,		256,	" TLB_DATA 4 KByte and 4 MByte pages" },
726 	{ 0x61, TLB_INST_4K,		48,	" TLB_INST 4 KByte pages, full associative" },
727 	{ 0x63, TLB_DATA_1G_2M_4M,	4,	" TLB_DATA 1 GByte pages, 4-way set associative"
728 						" (plus 32 entries TLB_DATA 2 MByte or 4 MByte pages, not encoded here)" },
729 	{ 0x6b, TLB_DATA_4K,		256,	" TLB_DATA 4 KByte pages, 8-way associative" },
730 	{ 0x6c, TLB_DATA_2M_4M,		128,	" TLB_DATA 2 MByte or 4 MByte pages, 8-way associative" },
731 	{ 0x6d, TLB_DATA_1G,		16,	" TLB_DATA 1 GByte pages, fully associative" },
732 	{ 0x76, TLB_INST_2M_4M,		8,	" TLB_INST 2-MByte or 4-MByte pages, fully associative" },
733 	{ 0xb0, TLB_INST_4K,		128,	" TLB_INST 4 KByte pages, 4-way set associative" },
734 	{ 0xb1, TLB_INST_2M_4M,		4,	" TLB_INST 2M pages, 4-way, 8 entries or 4M pages, 4-way entries" },
735 	{ 0xb2, TLB_INST_4K,		64,	" TLB_INST 4KByte pages, 4-way set associative" },
736 	{ 0xb3, TLB_DATA_4K,		128,	" TLB_DATA 4 KByte pages, 4-way set associative" },
737 	{ 0xb4, TLB_DATA_4K,		256,	" TLB_DATA 4 KByte pages, 4-way associative" },
738 	{ 0xb5, TLB_INST_4K,		64,	" TLB_INST 4 KByte pages, 8-way set associative" },
739 	{ 0xb6, TLB_INST_4K,		128,	" TLB_INST 4 KByte pages, 8-way set associative" },
740 	{ 0xba, TLB_DATA_4K,		64,	" TLB_DATA 4 KByte pages, 4-way associative" },
741 	{ 0xc0, TLB_DATA_4K_4M,		8,	" TLB_DATA 4 KByte and 4 MByte pages, 4-way associative" },
742 	{ 0xc1, STLB_4K_2M,		1024,	" STLB 4 KByte and 2 MByte pages, 8-way associative" },
743 	{ 0xc2, TLB_DATA_2M_4M,		16,	" TLB_DATA 2 MByte/4MByte pages, 4-way associative" },
744 	{ 0xca, STLB_4K,		512,	" STLB 4 KByte pages, 4-way associative" },
745 	{ 0x00, 0, 0 }
746 };
747 
intel_tlb_lookup(const unsigned char desc)748 static void intel_tlb_lookup(const unsigned char desc)
749 {
750 	unsigned char k;
751 	if (desc == 0)
752 		return;
753 
754 	/* look up this descriptor in the table */
755 	for (k = 0; intel_tlb_table[k].descriptor != desc &&
756 	     intel_tlb_table[k].descriptor != 0; k++)
757 		;
758 
759 	if (intel_tlb_table[k].tlb_type == 0)
760 		return;
761 
762 	switch (intel_tlb_table[k].tlb_type) {
763 	case STLB_4K:
764 		if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
765 			tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
766 		if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
767 			tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
768 		break;
769 	case STLB_4K_2M:
770 		if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
771 			tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
772 		if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
773 			tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
774 		if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
775 			tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
776 		if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
777 			tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
778 		if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
779 			tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
780 		if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
781 			tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
782 		break;
783 	case TLB_INST_ALL:
784 		if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
785 			tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
786 		if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
787 			tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
788 		if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
789 			tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
790 		break;
791 	case TLB_INST_4K:
792 		if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
793 			tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
794 		break;
795 	case TLB_INST_4M:
796 		if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
797 			tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
798 		break;
799 	case TLB_INST_2M_4M:
800 		if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
801 			tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
802 		if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
803 			tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
804 		break;
805 	case TLB_DATA_4K:
806 	case TLB_DATA0_4K:
807 		if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
808 			tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
809 		break;
810 	case TLB_DATA_4M:
811 	case TLB_DATA0_4M:
812 		if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
813 			tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
814 		break;
815 	case TLB_DATA_2M_4M:
816 	case TLB_DATA0_2M_4M:
817 		if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
818 			tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
819 		if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
820 			tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
821 		break;
822 	case TLB_DATA_4K_4M:
823 		if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
824 			tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
825 		if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
826 			tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
827 		break;
828 	case TLB_DATA_1G_2M_4M:
829 		if (tlb_lld_2m[ENTRIES] < TLB_0x63_2M_4M_ENTRIES)
830 			tlb_lld_2m[ENTRIES] = TLB_0x63_2M_4M_ENTRIES;
831 		if (tlb_lld_4m[ENTRIES] < TLB_0x63_2M_4M_ENTRIES)
832 			tlb_lld_4m[ENTRIES] = TLB_0x63_2M_4M_ENTRIES;
833 		fallthrough;
834 	case TLB_DATA_1G:
835 		if (tlb_lld_1g[ENTRIES] < intel_tlb_table[k].entries)
836 			tlb_lld_1g[ENTRIES] = intel_tlb_table[k].entries;
837 		break;
838 	}
839 }
840 
intel_detect_tlb(struct cpuinfo_x86 * c)841 static void intel_detect_tlb(struct cpuinfo_x86 *c)
842 {
843 	int i, j, n;
844 	unsigned int regs[4];
845 	unsigned char *desc = (unsigned char *)regs;
846 
847 	if (c->cpuid_level < 2)
848 		return;
849 
850 	/* Number of times to iterate */
851 	n = cpuid_eax(2) & 0xFF;
852 
853 	for (i = 0 ; i < n ; i++) {
854 		cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
855 
856 		/* If bit 31 is set, this is an unknown format */
857 		for (j = 0 ; j < 4 ; j++)
858 			if (regs[j] & (1 << 31))
859 				regs[j] = 0;
860 
861 		/* Byte 0 is level count, not a descriptor */
862 		for (j = 1 ; j < 16 ; j++)
863 			intel_tlb_lookup(desc[j]);
864 	}
865 }
866 
867 static const struct cpu_dev intel_cpu_dev = {
868 	.c_vendor	= "Intel",
869 	.c_ident	= { "GenuineIntel" },
870 #ifdef CONFIG_X86_32
871 	.legacy_models = {
872 		{ .family = 4, .model_names =
873 		  {
874 			  [0] = "486 DX-25/33",
875 			  [1] = "486 DX-50",
876 			  [2] = "486 SX",
877 			  [3] = "486 DX/2",
878 			  [4] = "486 SL",
879 			  [5] = "486 SX/2",
880 			  [7] = "486 DX/2-WB",
881 			  [8] = "486 DX/4",
882 			  [9] = "486 DX/4-WB"
883 		  }
884 		},
885 		{ .family = 5, .model_names =
886 		  {
887 			  [0] = "Pentium 60/66 A-step",
888 			  [1] = "Pentium 60/66",
889 			  [2] = "Pentium 75 - 200",
890 			  [3] = "OverDrive PODP5V83",
891 			  [4] = "Pentium MMX",
892 			  [7] = "Mobile Pentium 75 - 200",
893 			  [8] = "Mobile Pentium MMX",
894 			  [9] = "Quark SoC X1000",
895 		  }
896 		},
897 		{ .family = 6, .model_names =
898 		  {
899 			  [0] = "Pentium Pro A-step",
900 			  [1] = "Pentium Pro",
901 			  [3] = "Pentium II (Klamath)",
902 			  [4] = "Pentium II (Deschutes)",
903 			  [5] = "Pentium II (Deschutes)",
904 			  [6] = "Mobile Pentium II",
905 			  [7] = "Pentium III (Katmai)",
906 			  [8] = "Pentium III (Coppermine)",
907 			  [10] = "Pentium III (Cascades)",
908 			  [11] = "Pentium III (Tualatin)",
909 		  }
910 		},
911 		{ .family = 15, .model_names =
912 		  {
913 			  [0] = "Pentium 4 (Unknown)",
914 			  [1] = "Pentium 4 (Willamette)",
915 			  [2] = "Pentium 4 (Northwood)",
916 			  [4] = "Pentium 4 (Foster)",
917 			  [5] = "Pentium 4 (Foster)",
918 		  }
919 		},
920 	},
921 	.legacy_cache_size = intel_size_cache,
922 #endif
923 	.c_detect_tlb	= intel_detect_tlb,
924 	.c_early_init   = early_init_intel,
925 	.c_bsp_init	= bsp_init_intel,
926 	.c_init		= init_intel,
927 	.c_x86_vendor	= X86_VENDOR_INTEL,
928 };
929 
930 cpu_dev_register(intel_cpu_dev);
931 
932 #undef pr_fmt
933 #define pr_fmt(fmt) "x86/split lock detection: " fmt
934 
935 static const struct {
936 	const char			*option;
937 	enum split_lock_detect_state	state;
938 } sld_options[] __initconst = {
939 	{ "off",	sld_off   },
940 	{ "warn",	sld_warn  },
941 	{ "fatal",	sld_fatal },
942 	{ "ratelimit:", sld_ratelimit },
943 };
944 
945 static struct ratelimit_state bld_ratelimit;
946 
947 static unsigned int sysctl_sld_mitigate = 1;
948 static DEFINE_SEMAPHORE(buslock_sem, 1);
949 
950 #ifdef CONFIG_PROC_SYSCTL
951 static struct ctl_table sld_sysctls[] = {
952 	{
953 		.procname       = "split_lock_mitigate",
954 		.data           = &sysctl_sld_mitigate,
955 		.maxlen         = sizeof(unsigned int),
956 		.mode           = 0644,
957 		.proc_handler	= proc_douintvec_minmax,
958 		.extra1         = SYSCTL_ZERO,
959 		.extra2         = SYSCTL_ONE,
960 	},
961 };
962 
sld_mitigate_sysctl_init(void)963 static int __init sld_mitigate_sysctl_init(void)
964 {
965 	register_sysctl_init("kernel", sld_sysctls);
966 	return 0;
967 }
968 
969 late_initcall(sld_mitigate_sysctl_init);
970 #endif
971 
match_option(const char * arg,int arglen,const char * opt)972 static inline bool match_option(const char *arg, int arglen, const char *opt)
973 {
974 	int len = strlen(opt), ratelimit;
975 
976 	if (strncmp(arg, opt, len))
977 		return false;
978 
979 	/*
980 	 * Min ratelimit is 1 bus lock/sec.
981 	 * Max ratelimit is 1000 bus locks/sec.
982 	 */
983 	if (sscanf(arg, "ratelimit:%d", &ratelimit) == 1 &&
984 	    ratelimit > 0 && ratelimit <= 1000) {
985 		ratelimit_state_init(&bld_ratelimit, HZ, ratelimit);
986 		ratelimit_set_flags(&bld_ratelimit, RATELIMIT_MSG_ON_RELEASE);
987 		return true;
988 	}
989 
990 	return len == arglen;
991 }
992 
split_lock_verify_msr(bool on)993 static bool split_lock_verify_msr(bool on)
994 {
995 	u64 ctrl, tmp;
996 
997 	if (rdmsrl_safe(MSR_TEST_CTRL, &ctrl))
998 		return false;
999 	if (on)
1000 		ctrl |= MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
1001 	else
1002 		ctrl &= ~MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
1003 	if (wrmsrl_safe(MSR_TEST_CTRL, ctrl))
1004 		return false;
1005 	rdmsrl(MSR_TEST_CTRL, tmp);
1006 	return ctrl == tmp;
1007 }
1008 
sld_state_setup(void)1009 static void __init sld_state_setup(void)
1010 {
1011 	enum split_lock_detect_state state = sld_warn;
1012 	char arg[20];
1013 	int i, ret;
1014 
1015 	if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
1016 	    !boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
1017 		return;
1018 
1019 	ret = cmdline_find_option(boot_command_line, "split_lock_detect",
1020 				  arg, sizeof(arg));
1021 	if (ret >= 0) {
1022 		for (i = 0; i < ARRAY_SIZE(sld_options); i++) {
1023 			if (match_option(arg, ret, sld_options[i].option)) {
1024 				state = sld_options[i].state;
1025 				break;
1026 			}
1027 		}
1028 	}
1029 	sld_state = state;
1030 }
1031 
__split_lock_setup(void)1032 static void __init __split_lock_setup(void)
1033 {
1034 	if (!split_lock_verify_msr(false)) {
1035 		pr_info("MSR access failed: Disabled\n");
1036 		return;
1037 	}
1038 
1039 	rdmsrl(MSR_TEST_CTRL, msr_test_ctrl_cache);
1040 
1041 	if (!split_lock_verify_msr(true)) {
1042 		pr_info("MSR access failed: Disabled\n");
1043 		return;
1044 	}
1045 
1046 	/* Restore the MSR to its cached value. */
1047 	wrmsrl(MSR_TEST_CTRL, msr_test_ctrl_cache);
1048 
1049 	setup_force_cpu_cap(X86_FEATURE_SPLIT_LOCK_DETECT);
1050 }
1051 
1052 /*
1053  * MSR_TEST_CTRL is per core, but we treat it like a per CPU MSR. Locking
1054  * is not implemented as one thread could undo the setting of the other
1055  * thread immediately after dropping the lock anyway.
1056  */
sld_update_msr(bool on)1057 static void sld_update_msr(bool on)
1058 {
1059 	u64 test_ctrl_val = msr_test_ctrl_cache;
1060 
1061 	if (on)
1062 		test_ctrl_val |= MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
1063 
1064 	wrmsrl(MSR_TEST_CTRL, test_ctrl_val);
1065 }
1066 
split_lock_init(void)1067 static void split_lock_init(void)
1068 {
1069 	/*
1070 	 * #DB for bus lock handles ratelimit and #AC for split lock is
1071 	 * disabled.
1072 	 */
1073 	if (sld_state == sld_ratelimit) {
1074 		split_lock_verify_msr(false);
1075 		return;
1076 	}
1077 
1078 	if (cpu_model_supports_sld)
1079 		split_lock_verify_msr(sld_state != sld_off);
1080 }
1081 
__split_lock_reenable_unlock(struct work_struct * work)1082 static void __split_lock_reenable_unlock(struct work_struct *work)
1083 {
1084 	sld_update_msr(true);
1085 	up(&buslock_sem);
1086 }
1087 
1088 static DECLARE_DELAYED_WORK(sl_reenable_unlock, __split_lock_reenable_unlock);
1089 
__split_lock_reenable(struct work_struct * work)1090 static void __split_lock_reenable(struct work_struct *work)
1091 {
1092 	sld_update_msr(true);
1093 }
1094 static DECLARE_DELAYED_WORK(sl_reenable, __split_lock_reenable);
1095 
1096 /*
1097  * If a CPU goes offline with pending delayed work to re-enable split lock
1098  * detection then the delayed work will be executed on some other CPU. That
1099  * handles releasing the buslock_sem, but because it executes on a
1100  * different CPU probably won't re-enable split lock detection. This is a
1101  * problem on HT systems since the sibling CPU on the same core may then be
1102  * left running with split lock detection disabled.
1103  *
1104  * Unconditionally re-enable detection here.
1105  */
splitlock_cpu_offline(unsigned int cpu)1106 static int splitlock_cpu_offline(unsigned int cpu)
1107 {
1108 	sld_update_msr(true);
1109 
1110 	return 0;
1111 }
1112 
split_lock_warn(unsigned long ip)1113 static void split_lock_warn(unsigned long ip)
1114 {
1115 	struct delayed_work *work;
1116 	int cpu;
1117 
1118 	if (!current->reported_split_lock)
1119 		pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
1120 				    current->comm, current->pid, ip);
1121 	current->reported_split_lock = 1;
1122 
1123 	if (sysctl_sld_mitigate) {
1124 		/*
1125 		 * misery factor #1:
1126 		 * sleep 10ms before trying to execute split lock.
1127 		 */
1128 		if (msleep_interruptible(10) > 0)
1129 			return;
1130 		/*
1131 		 * Misery factor #2:
1132 		 * only allow one buslocked disabled core at a time.
1133 		 */
1134 		if (down_interruptible(&buslock_sem) == -EINTR)
1135 			return;
1136 		work = &sl_reenable_unlock;
1137 	} else {
1138 		work = &sl_reenable;
1139 	}
1140 
1141 	cpu = get_cpu();
1142 	schedule_delayed_work_on(cpu, work, 2);
1143 
1144 	/* Disable split lock detection on this CPU to make progress */
1145 	sld_update_msr(false);
1146 	put_cpu();
1147 }
1148 
handle_guest_split_lock(unsigned long ip)1149 bool handle_guest_split_lock(unsigned long ip)
1150 {
1151 	if (sld_state == sld_warn) {
1152 		split_lock_warn(ip);
1153 		return true;
1154 	}
1155 
1156 	pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
1157 		     current->comm, current->pid,
1158 		     sld_state == sld_fatal ? "fatal" : "bogus", ip);
1159 
1160 	current->thread.error_code = 0;
1161 	current->thread.trap_nr = X86_TRAP_AC;
1162 	force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
1163 	return false;
1164 }
1165 EXPORT_SYMBOL_GPL(handle_guest_split_lock);
1166 
bus_lock_init(void)1167 static void bus_lock_init(void)
1168 {
1169 	u64 val;
1170 
1171 	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
1172 		return;
1173 
1174 	rdmsrl(MSR_IA32_DEBUGCTLMSR, val);
1175 
1176 	if ((boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
1177 	    (sld_state == sld_warn || sld_state == sld_fatal)) ||
1178 	    sld_state == sld_off) {
1179 		/*
1180 		 * Warn and fatal are handled by #AC for split lock if #AC for
1181 		 * split lock is supported.
1182 		 */
1183 		val &= ~DEBUGCTLMSR_BUS_LOCK_DETECT;
1184 	} else {
1185 		val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
1186 	}
1187 
1188 	wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
1189 }
1190 
handle_user_split_lock(struct pt_regs * regs,long error_code)1191 bool handle_user_split_lock(struct pt_regs *regs, long error_code)
1192 {
1193 	if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
1194 		return false;
1195 	split_lock_warn(regs->ip);
1196 	return true;
1197 }
1198 
handle_bus_lock(struct pt_regs * regs)1199 void handle_bus_lock(struct pt_regs *regs)
1200 {
1201 	switch (sld_state) {
1202 	case sld_off:
1203 		break;
1204 	case sld_ratelimit:
1205 		/* Enforce no more than bld_ratelimit bus locks/sec. */
1206 		while (!__ratelimit(&bld_ratelimit))
1207 			msleep(20);
1208 		/* Warn on the bus lock. */
1209 		fallthrough;
1210 	case sld_warn:
1211 		pr_warn_ratelimited("#DB: %s/%d took a bus_lock trap at address: 0x%lx\n",
1212 				    current->comm, current->pid, regs->ip);
1213 		break;
1214 	case sld_fatal:
1215 		force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
1216 		break;
1217 	}
1218 }
1219 
1220 /*
1221  * CPU models that are known to have the per-core split-lock detection
1222  * feature even though they do not enumerate IA32_CORE_CAPABILITIES.
1223  */
1224 static const struct x86_cpu_id split_lock_cpu_ids[] __initconst = {
1225 	X86_MATCH_VFM(INTEL_ICELAKE_X,	0),
1226 	X86_MATCH_VFM(INTEL_ICELAKE_L,	0),
1227 	X86_MATCH_VFM(INTEL_ICELAKE_D,	0),
1228 	{}
1229 };
1230 
split_lock_setup(struct cpuinfo_x86 * c)1231 static void __init split_lock_setup(struct cpuinfo_x86 *c)
1232 {
1233 	const struct x86_cpu_id *m;
1234 	u64 ia32_core_caps;
1235 
1236 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
1237 		return;
1238 
1239 	/* Check for CPUs that have support but do not enumerate it: */
1240 	m = x86_match_cpu(split_lock_cpu_ids);
1241 	if (m)
1242 		goto supported;
1243 
1244 	if (!cpu_has(c, X86_FEATURE_CORE_CAPABILITIES))
1245 		return;
1246 
1247 	/*
1248 	 * Not all bits in MSR_IA32_CORE_CAPS are architectural, but
1249 	 * MSR_IA32_CORE_CAPS_SPLIT_LOCK_DETECT is.  All CPUs that set
1250 	 * it have split lock detection.
1251 	 */
1252 	rdmsrl(MSR_IA32_CORE_CAPS, ia32_core_caps);
1253 	if (ia32_core_caps & MSR_IA32_CORE_CAPS_SPLIT_LOCK_DETECT)
1254 		goto supported;
1255 
1256 	/* CPU is not in the model list and does not have the MSR bit: */
1257 	return;
1258 
1259 supported:
1260 	cpu_model_supports_sld = true;
1261 	__split_lock_setup();
1262 }
1263 
sld_state_show(void)1264 static void sld_state_show(void)
1265 {
1266 	if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) &&
1267 	    !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
1268 		return;
1269 
1270 	switch (sld_state) {
1271 	case sld_off:
1272 		pr_info("disabled\n");
1273 		break;
1274 	case sld_warn:
1275 		if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
1276 			pr_info("#AC: crashing the kernel on kernel split_locks and warning on user-space split_locks\n");
1277 			if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
1278 					      "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
1279 				pr_warn("No splitlock CPU offline handler\n");
1280 		} else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
1281 			pr_info("#DB: warning on user-space bus_locks\n");
1282 		}
1283 		break;
1284 	case sld_fatal:
1285 		if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
1286 			pr_info("#AC: crashing the kernel on kernel split_locks and sending SIGBUS on user-space split_locks\n");
1287 		} else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
1288 			pr_info("#DB: sending SIGBUS on user-space bus_locks%s\n",
1289 				boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) ?
1290 				" from non-WB" : "");
1291 		}
1292 		break;
1293 	case sld_ratelimit:
1294 		if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
1295 			pr_info("#DB: setting system wide bus lock rate limit to %u/sec\n", bld_ratelimit.burst);
1296 		break;
1297 	}
1298 }
1299 
sld_setup(struct cpuinfo_x86 * c)1300 void __init sld_setup(struct cpuinfo_x86 *c)
1301 {
1302 	split_lock_setup(c);
1303 	sld_state_setup();
1304 	sld_state_show();
1305 }
1306 
1307 #define X86_HYBRID_CPU_TYPE_ID_SHIFT	24
1308 
1309 /**
1310  * get_this_hybrid_cpu_type() - Get the type of this hybrid CPU
1311  *
1312  * Returns the CPU type [31:24] (i.e., Atom or Core) of a CPU in
1313  * a hybrid processor. If the processor is not hybrid, returns 0.
1314  */
get_this_hybrid_cpu_type(void)1315 u8 get_this_hybrid_cpu_type(void)
1316 {
1317 	if (!cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))
1318 		return 0;
1319 
1320 	return cpuid_eax(0x0000001a) >> X86_HYBRID_CPU_TYPE_ID_SHIFT;
1321 }
1322