1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/pgtable.h>
5 #include <linux/string.h>
6 #include <linux/bitops.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>
18 #include <asm/cpufeature.h>
22 #include <asm/intel-family.h>
23 #include <asm/microcode.h>
24 #include <asm/hwcap2.h>
26 #include <asm/cpu_device_id.h>
27 #include <asm/cmdline.h>
28 #include <asm/traps.h>
29 #include <asm/resctrl.h>
31 #include <asm/thermal.h>
34 #include <linux/topology.h>
39 #ifdef CONFIG_X86_LOCAL_APIC
40 #include <asm/mpspec.h>
44 enum split_lock_detect_state
{
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.
56 static enum split_lock_detect_state sld_state __ro_after_init
= sld_off
;
57 static u64 msr_test_ctrl_cache __ro_after_init
;
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'.
64 static bool cpu_model_supports_sld __ro_after_init
;
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.
73 static void check_memory_type_self_snoop_errata(struct cpuinfo_x86
*c
)
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
:
83 case INTEL_NEHALEM_EP
:
84 case INTEL_NEHALEM_EX
:
86 case INTEL_WESTMERE_EP
:
87 case INTEL_SANDYBRIDGE
:
88 setup_clear_cpu_cap(X86_FEATURE_SELFSNOOP
);
92 static bool ring3mwait_disabled __read_mostly
;
94 static int __init
ring3mwait_disable(char *__unused
)
96 ring3mwait_disabled
= true;
99 __setup("ring3mwait=disable", ring3mwait_disable
);
101 static void probe_xeon_phi_r3mwait(struct cpuinfo_x86
*c
)
104 * Ring 3 MONITOR/MWAIT feature cannot be detected without
105 * cpu model and family comparison.
109 switch (c
->x86_vfm
) {
110 case INTEL_XEON_PHI_KNL
:
111 case INTEL_XEON_PHI_KNM
:
117 if (ring3mwait_disabled
)
120 set_cpu_cap(c
, X86_FEATURE_RING3MWAIT
);
121 this_cpu_or(msr_misc_features_shadow
,
122 1UL << MSR_MISC_FEATURES_ENABLES_RING3MWAIT_BIT
);
124 if (c
== &boot_cpu_data
)
125 ELF_HWCAP2
|= HWCAP2_RING3MWAIT
;
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
136 struct sku_microcode
{
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 },
165 static bool bad_spectre_microcode(struct cpuinfo_x86
*c
)
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.
173 if (cpu_has(c
, X86_FEATURE_HYPERVISOR
))
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
);
184 #define MSR_IA32_TME_ACTIVATE 0x982
186 /* Helpers to access TME_ACTIVATE MSR */
187 #define TME_ACTIVATE_LOCKED(x) (x & 0x1)
188 #define TME_ACTIVATE_ENABLED(x) (x & 0x2)
190 #define TME_ACTIVATE_KEYID_BITS(x) ((x >> 32) & 0xf) /* Bits 35:32 */
192 static void detect_tme_early(struct cpuinfo_x86
*c
)
197 rdmsrl(MSR_IA32_TME_ACTIVATE
, tme_activate
);
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
);
204 pr_info_once("x86/tme: enabled by BIOS\n");
205 keyid_bits
= TME_ACTIVATE_KEYID_BITS(tme_activate
);
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.
214 * Update cpuinfo_x86::x86_phys_bits accordingly.
216 c
->x86_phys_bits
-= keyid_bits
;
217 pr_info_once("x86/mktme: BIOS enabled: x86_phys_bits reduced by %d\n",
221 void intel_unlock_cpuid_leafs(struct cpuinfo_x86
*c
)
223 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
)
226 if (c
->x86
< 6 || (c
->x86
== 6 && c
->x86_model
< 0xd))
230 * The BIOS can have limited CPUID to leaf 2, which breaks feature
231 * enumeration. Unlock it and update the maximum leaf info.
233 if (msr_clear_bit(MSR_IA32_MISC_ENABLE
, MSR_IA32_MISC_ENABLE_LIMIT_CPUID_BIT
) > 0)
234 c
->cpuid_level
= cpuid_eax(0);
237 static void early_init_intel(struct cpuinfo_x86
*c
)
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
);
245 if (c
->x86
>= 6 && !cpu_has(c
, X86_FEATURE_IA64
))
246 c
->microcode
= intel_get_microcode_revision();
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
);
265 * Atom erratum AAE44/AAF40/AAG38/AAH41:
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.
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
);
279 set_cpu_cap(c
, X86_FEATURE_SYSENTER32
);
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;
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;
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.
295 * It is also reliable across cores and sockets. (but not across
296 * cabinets - we turn it off in that case explicitly.)
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
);
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
);
314 * PAT is broken on early family 6 CPUs, the last of which
315 * is "Yonah" where the erratum is named "AN7":
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
322 * Disable PAT and fall back to MTRR on these CPUs.
324 if (c
->x86_vfm
>= INTEL_PENTIUM_PRO
&&
325 c
->x86_vfm
<= INTEL_CORE_YONAH
)
326 clear_cpu_cap(c
, X86_FEATURE_PAT
);
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.
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
);
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"
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
351 if (c
->x86_vfm
== INTEL_QUARK_X1000
) {
352 pr_info("Disabling PGE capability bit\n");
353 setup_clear_cpu_cap(X86_FEATURE_PGE
);
356 check_memory_type_self_snoop_errata(c
);
359 * Adjust the number of physical bits early because it affects the
360 * valid bits of the MTRR mask registers.
362 if (cpu_has(c
, X86_FEATURE_TME
))
366 static void bsp_init_intel(struct cpuinfo_x86
*c
)
368 resctrl_cpu_detect(c
);
373 * Early probe support logic for ppro memory erratum #50
375 * This is called before we do cpu ident work
378 int ppro_with_ram_bug(void)
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");
391 static void intel_smp_check(struct cpuinfo_x86
*c
)
393 /* calling is from identify_secondary_cpu() ? */
398 * Mask B, Pentium, but not Pentium MMX
401 c
->x86_stepping
>= 1 && c
->x86_stepping
<= 4 &&
404 * Remember we have B step Pentia with bugs
406 WARN_ONCE(1, "WARNING: SMP operation may be unreliable"
407 "with B stepping processors.\n");
412 static int __init
forcepae_setup(char *__unused
)
417 __setup("forcepae", forcepae_setup
);
419 static void intel_workarounds(struct cpuinfo_x86
*c
)
421 #ifdef CONFIG_X86_F00F_BUG
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.
428 clear_cpu_bug(c
, X86_BUG_F00F
);
429 if (c
->x86
== 5 && c
->x86_model
< 9) {
430 static int f00f_workaround_enabled
;
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;
441 * SEP CPUID bug: Pentium Pro reports SEP but doesn't have it until
444 if ((c
->x86
<<8 | c
->x86_model
<<4 | c
->x86_stepping
) < 0x633)
445 clear_cpu_cap(c
, X86_FEATURE_SEP
);
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.
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
);
459 * P4 Xeon erratum 037 workaround.
460 * Hardware prefetcher may cause stale data to be loaded into the cache.
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");
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").
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
);
481 #ifdef CONFIG_X86_INTEL_USERCOPY
483 * Set up the preferred alignment for movsl bulk memory moves
486 case 4: /* 486: untested */
488 case 5: /* Old Pentia: untested */
490 case 6: /* PII/PIII only like movsl with 8-byte alignment */
493 case 15: /* P4 is OK down to 8-byte alignment */
502 static void intel_workarounds(struct cpuinfo_x86
*c
)
507 static void srat_detect_node(struct cpuinfo_x86
*c
)
511 int cpu
= smp_processor_id();
513 /* Don't do the funky fallback heuristics the AMD version employs
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
);
520 numa_set_node(cpu
, node
);
524 static void init_cpuid_fault(struct cpuinfo_x86
*c
)
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
);
534 static void init_intel_misc_features(struct cpuinfo_x86
*c
)
538 if (rdmsrl_safe(MSR_MISC_FEATURES_ENABLES
, &msr
))
541 /* Clear all MISC features */
542 this_cpu_write(msr_misc_features_shadow
, 0);
544 /* Check features and update capabilities and shadow control bits */
546 probe_xeon_phi_r3mwait(c
);
548 msr
= this_cpu_read(msr_misc_features_shadow
);
549 wrmsrl(MSR_MISC_FEATURES_ENABLES
, msr
);
552 static void split_lock_init(void);
553 static void bus_lock_init(void);
555 static void init_intel(struct cpuinfo_x86
*c
)
559 intel_workarounds(c
);
561 init_intel_cacheinfo(c
);
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
);
570 if (cpu_has(c
, X86_FEATURE_XMM2
))
571 set_cpu_cap(c
, X86_FEATURE_LFENCE_RDTSC
);
573 if (boot_cpu_has(X86_FEATURE_DS
)) {
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
);
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
);
589 if (boot_cpu_has(X86_FEATURE_MWAIT
) && c
->x86_vfm
== INTEL_ATOM_GOLDMONT
)
590 set_cpu_bug(c
, X86_BUG_MONITOR
);
594 c
->x86_cache_alignment
= c
->x86_clflush_size
* 2;
596 set_cpu_cap(c
, X86_FEATURE_REP_GOOD
);
599 * Names for the Pentium II/Celeron processors
600 * detectable only by also checking the cache size.
601 * Dixon is NOT a Celeron.
604 unsigned int l2
= c
->x86_cache_size
;
607 switch (c
->x86_model
) {
610 p
= "Celeron (Covington)";
612 p
= "Mobile Pentium II (Dixon)";
617 p
= "Celeron (Mendocino)";
618 else if (c
->x86_stepping
== 0 || c
->x86_stepping
== 5)
624 p
= "Celeron (Coppermine)";
629 strcpy(c
->x86_model_id
, p
);
633 set_cpu_cap(c
, X86_FEATURE_P4
);
635 set_cpu_cap(c
, X86_FEATURE_P3
);
638 /* Work around errata */
641 init_ia32_feat_ctl(c
);
643 init_intel_misc_features(c
);
648 intel_init_thermal(c
);
652 static unsigned int intel_size_cache(struct cpuinfo_x86
*c
, unsigned int size
)
655 * Intel PIII Tualatin. This comes in two flavours.
656 * One has 256kb of cache, the other 512. We have no way
657 * to determine which, so we use a boottime override
658 * for the 512kb model, and assume 256 otherwise.
660 if ((c
->x86
== 6) && (c
->x86_model
== 11) && (size
== 0))
664 * Intel Quark SoC X1000 contains a 4-way set associative
665 * 16K cache with a 16 byte cache line and 256 lines per tag
667 if ((c
->x86
== 5) && (c
->x86_model
== 9))
673 #define TLB_INST_4K 0x01
674 #define TLB_INST_4M 0x02
675 #define TLB_INST_2M_4M 0x03
677 #define TLB_INST_ALL 0x05
678 #define TLB_INST_1G 0x06
680 #define TLB_DATA_4K 0x11
681 #define TLB_DATA_4M 0x12
682 #define TLB_DATA_2M_4M 0x13
683 #define TLB_DATA_4K_4M 0x14
685 #define TLB_DATA_1G 0x16
687 #define TLB_DATA0_4K 0x21
688 #define TLB_DATA0_4M 0x22
689 #define TLB_DATA0_2M_4M 0x23
692 #define STLB_4K_2M 0x42
694 static const struct _tlb_table intel_tlb_table
[] = {
695 { 0x01, TLB_INST_4K
, 32, " TLB_INST 4 KByte pages, 4-way set associative" },
696 { 0x02, TLB_INST_4M
, 2, " TLB_INST 4 MByte pages, full associative" },
697 { 0x03, TLB_DATA_4K
, 64, " TLB_DATA 4 KByte pages, 4-way set associative" },
698 { 0x04, TLB_DATA_4M
, 8, " TLB_DATA 4 MByte pages, 4-way set associative" },
699 { 0x05, TLB_DATA_4M
, 32, " TLB_DATA 4 MByte pages, 4-way set associative" },
700 { 0x0b, TLB_INST_4M
, 4, " TLB_INST 4 MByte pages, 4-way set associative" },
701 { 0x4f, TLB_INST_4K
, 32, " TLB_INST 4 KByte pages" },
702 { 0x50, TLB_INST_ALL
, 64, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
703 { 0x51, TLB_INST_ALL
, 128, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
704 { 0x52, TLB_INST_ALL
, 256, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
705 { 0x55, TLB_INST_2M_4M
, 7, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
706 { 0x56, TLB_DATA0_4M
, 16, " TLB_DATA0 4 MByte pages, 4-way set associative" },
707 { 0x57, TLB_DATA0_4K
, 16, " TLB_DATA0 4 KByte pages, 4-way associative" },
708 { 0x59, TLB_DATA0_4K
, 16, " TLB_DATA0 4 KByte pages, fully associative" },
709 { 0x5a, TLB_DATA0_2M_4M
, 32, " TLB_DATA0 2-MByte or 4 MByte pages, 4-way set associative" },
710 { 0x5b, TLB_DATA_4K_4M
, 64, " TLB_DATA 4 KByte and 4 MByte pages" },
711 { 0x5c, TLB_DATA_4K_4M
, 128, " TLB_DATA 4 KByte and 4 MByte pages" },
712 { 0x5d, TLB_DATA_4K_4M
, 256, " TLB_DATA 4 KByte and 4 MByte pages" },
713 { 0x61, TLB_INST_4K
, 48, " TLB_INST 4 KByte pages, full associative" },
714 { 0x63, TLB_DATA_1G
, 4, " TLB_DATA 1 GByte pages, 4-way set associative" },
715 { 0x6b, TLB_DATA_4K
, 256, " TLB_DATA 4 KByte pages, 8-way associative" },
716 { 0x6c, TLB_DATA_2M_4M
, 128, " TLB_DATA 2 MByte or 4 MByte pages, 8-way associative" },
717 { 0x6d, TLB_DATA_1G
, 16, " TLB_DATA 1 GByte pages, fully associative" },
718 { 0x76, TLB_INST_2M_4M
, 8, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
719 { 0xb0, TLB_INST_4K
, 128, " TLB_INST 4 KByte pages, 4-way set associative" },
720 { 0xb1, TLB_INST_2M_4M
, 4, " TLB_INST 2M pages, 4-way, 8 entries or 4M pages, 4-way entries" },
721 { 0xb2, TLB_INST_4K
, 64, " TLB_INST 4KByte pages, 4-way set associative" },
722 { 0xb3, TLB_DATA_4K
, 128, " TLB_DATA 4 KByte pages, 4-way set associative" },
723 { 0xb4, TLB_DATA_4K
, 256, " TLB_DATA 4 KByte pages, 4-way associative" },
724 { 0xb5, TLB_INST_4K
, 64, " TLB_INST 4 KByte pages, 8-way set associative" },
725 { 0xb6, TLB_INST_4K
, 128, " TLB_INST 4 KByte pages, 8-way set associative" },
726 { 0xba, TLB_DATA_4K
, 64, " TLB_DATA 4 KByte pages, 4-way associative" },
727 { 0xc0, TLB_DATA_4K_4M
, 8, " TLB_DATA 4 KByte and 4 MByte pages, 4-way associative" },
728 { 0xc1, STLB_4K_2M
, 1024, " STLB 4 KByte and 2 MByte pages, 8-way associative" },
729 { 0xc2, TLB_DATA_2M_4M
, 16, " TLB_DATA 2 MByte/4MByte pages, 4-way associative" },
730 { 0xca, STLB_4K
, 512, " STLB 4 KByte pages, 4-way associative" },
734 static void intel_tlb_lookup(const unsigned char desc
)
740 /* look up this descriptor in the table */
741 for (k
= 0; intel_tlb_table
[k
].descriptor
!= desc
&&
742 intel_tlb_table
[k
].descriptor
!= 0; k
++)
745 if (intel_tlb_table
[k
].tlb_type
== 0)
748 switch (intel_tlb_table
[k
].tlb_type
) {
750 if (tlb_lli_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
751 tlb_lli_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
752 if (tlb_lld_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
753 tlb_lld_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
756 if (tlb_lli_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
757 tlb_lli_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
758 if (tlb_lld_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
759 tlb_lld_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
760 if (tlb_lli_2m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
761 tlb_lli_2m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
762 if (tlb_lld_2m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
763 tlb_lld_2m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
764 if (tlb_lli_4m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
765 tlb_lli_4m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
766 if (tlb_lld_4m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
767 tlb_lld_4m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
770 if (tlb_lli_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
771 tlb_lli_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
772 if (tlb_lli_2m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
773 tlb_lli_2m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
774 if (tlb_lli_4m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
775 tlb_lli_4m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
778 if (tlb_lli_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
779 tlb_lli_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
782 if (tlb_lli_4m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
783 tlb_lli_4m
[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
;
793 if (tlb_lld_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
794 tlb_lld_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
798 if (tlb_lld_4m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
799 tlb_lld_4m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
802 case TLB_DATA0_2M_4M
:
803 if (tlb_lld_2m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
804 tlb_lld_2m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
805 if (tlb_lld_4m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
806 tlb_lld_4m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
809 if (tlb_lld_4k
[ENTRIES
] < intel_tlb_table
[k
].entries
)
810 tlb_lld_4k
[ENTRIES
] = intel_tlb_table
[k
].entries
;
811 if (tlb_lld_4m
[ENTRIES
] < intel_tlb_table
[k
].entries
)
812 tlb_lld_4m
[ENTRIES
] = intel_tlb_table
[k
].entries
;
815 if (tlb_lld_1g
[ENTRIES
] < intel_tlb_table
[k
].entries
)
816 tlb_lld_1g
[ENTRIES
] = intel_tlb_table
[k
].entries
;
821 static void intel_detect_tlb(struct cpuinfo_x86
*c
)
824 unsigned int regs
[4];
825 unsigned char *desc
= (unsigned char *)regs
;
827 if (c
->cpuid_level
< 2)
830 /* Number of times to iterate */
831 n
= cpuid_eax(2) & 0xFF;
833 for (i
= 0 ; i
< n
; i
++) {
834 cpuid(2, ®s
[0], ®s
[1], ®s
[2], ®s
[3]);
836 /* If bit 31 is set, this is an unknown format */
837 for (j
= 0 ; j
< 3 ; j
++)
838 if (regs
[j
] & (1 << 31))
841 /* Byte 0 is level count, not a descriptor */
842 for (j
= 1 ; j
< 16 ; j
++)
843 intel_tlb_lookup(desc
[j
]);
847 static const struct cpu_dev intel_cpu_dev
= {
849 .c_ident
= { "GenuineIntel" },
852 { .family
= 4, .model_names
=
854 [0] = "486 DX-25/33",
865 { .family
= 5, .model_names
=
867 [0] = "Pentium 60/66 A-step",
868 [1] = "Pentium 60/66",
869 [2] = "Pentium 75 - 200",
870 [3] = "OverDrive PODP5V83",
872 [7] = "Mobile Pentium 75 - 200",
873 [8] = "Mobile Pentium MMX",
874 [9] = "Quark SoC X1000",
877 { .family
= 6, .model_names
=
879 [0] = "Pentium Pro A-step",
881 [3] = "Pentium II (Klamath)",
882 [4] = "Pentium II (Deschutes)",
883 [5] = "Pentium II (Deschutes)",
884 [6] = "Mobile Pentium II",
885 [7] = "Pentium III (Katmai)",
886 [8] = "Pentium III (Coppermine)",
887 [10] = "Pentium III (Cascades)",
888 [11] = "Pentium III (Tualatin)",
891 { .family
= 15, .model_names
=
893 [0] = "Pentium 4 (Unknown)",
894 [1] = "Pentium 4 (Willamette)",
895 [2] = "Pentium 4 (Northwood)",
896 [4] = "Pentium 4 (Foster)",
897 [5] = "Pentium 4 (Foster)",
901 .legacy_cache_size
= intel_size_cache
,
903 .c_detect_tlb
= intel_detect_tlb
,
904 .c_early_init
= early_init_intel
,
905 .c_bsp_init
= bsp_init_intel
,
906 .c_init
= init_intel
,
907 .c_x86_vendor
= X86_VENDOR_INTEL
,
910 cpu_dev_register(intel_cpu_dev
);
913 #define pr_fmt(fmt) "x86/split lock detection: " fmt
915 static const struct {
917 enum split_lock_detect_state state
;
918 } sld_options
[] __initconst
= {
920 { "warn", sld_warn
},
921 { "fatal", sld_fatal
},
922 { "ratelimit:", sld_ratelimit
},
925 static struct ratelimit_state bld_ratelimit
;
927 static unsigned int sysctl_sld_mitigate
= 1;
928 static DEFINE_SEMAPHORE(buslock_sem
, 1);
930 #ifdef CONFIG_PROC_SYSCTL
931 static struct ctl_table sld_sysctls
[] = {
933 .procname
= "split_lock_mitigate",
934 .data
= &sysctl_sld_mitigate
,
935 .maxlen
= sizeof(unsigned int),
937 .proc_handler
= proc_douintvec_minmax
,
938 .extra1
= SYSCTL_ZERO
,
939 .extra2
= SYSCTL_ONE
,
943 static int __init
sld_mitigate_sysctl_init(void)
945 register_sysctl_init("kernel", sld_sysctls
);
949 late_initcall(sld_mitigate_sysctl_init
);
952 static inline bool match_option(const char *arg
, int arglen
, const char *opt
)
954 int len
= strlen(opt
), ratelimit
;
956 if (strncmp(arg
, opt
, len
))
960 * Min ratelimit is 1 bus lock/sec.
961 * Max ratelimit is 1000 bus locks/sec.
963 if (sscanf(arg
, "ratelimit:%d", &ratelimit
) == 1 &&
964 ratelimit
> 0 && ratelimit
<= 1000) {
965 ratelimit_state_init(&bld_ratelimit
, HZ
, ratelimit
);
966 ratelimit_set_flags(&bld_ratelimit
, RATELIMIT_MSG_ON_RELEASE
);
970 return len
== arglen
;
973 static bool split_lock_verify_msr(bool on
)
977 if (rdmsrl_safe(MSR_TEST_CTRL
, &ctrl
))
980 ctrl
|= MSR_TEST_CTRL_SPLIT_LOCK_DETECT
;
982 ctrl
&= ~MSR_TEST_CTRL_SPLIT_LOCK_DETECT
;
983 if (wrmsrl_safe(MSR_TEST_CTRL
, ctrl
))
985 rdmsrl(MSR_TEST_CTRL
, tmp
);
989 static void __init
sld_state_setup(void)
991 enum split_lock_detect_state state
= sld_warn
;
995 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT
) &&
996 !boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT
))
999 ret
= cmdline_find_option(boot_command_line
, "split_lock_detect",
1002 for (i
= 0; i
< ARRAY_SIZE(sld_options
); i
++) {
1003 if (match_option(arg
, ret
, sld_options
[i
].option
)) {
1004 state
= sld_options
[i
].state
;
1012 static void __init
__split_lock_setup(void)
1014 if (!split_lock_verify_msr(false)) {
1015 pr_info("MSR access failed: Disabled\n");
1019 rdmsrl(MSR_TEST_CTRL
, msr_test_ctrl_cache
);
1021 if (!split_lock_verify_msr(true)) {
1022 pr_info("MSR access failed: Disabled\n");
1026 /* Restore the MSR to its cached value. */
1027 wrmsrl(MSR_TEST_CTRL
, msr_test_ctrl_cache
);
1029 setup_force_cpu_cap(X86_FEATURE_SPLIT_LOCK_DETECT
);
1033 * MSR_TEST_CTRL is per core, but we treat it like a per CPU MSR. Locking
1034 * is not implemented as one thread could undo the setting of the other
1035 * thread immediately after dropping the lock anyway.
1037 static void sld_update_msr(bool on
)
1039 u64 test_ctrl_val
= msr_test_ctrl_cache
;
1042 test_ctrl_val
|= MSR_TEST_CTRL_SPLIT_LOCK_DETECT
;
1044 wrmsrl(MSR_TEST_CTRL
, test_ctrl_val
);
1047 static void split_lock_init(void)
1050 * #DB for bus lock handles ratelimit and #AC for split lock is
1053 if (sld_state
== sld_ratelimit
) {
1054 split_lock_verify_msr(false);
1058 if (cpu_model_supports_sld
)
1059 split_lock_verify_msr(sld_state
!= sld_off
);
1062 static void __split_lock_reenable_unlock(struct work_struct
*work
)
1064 sld_update_msr(true);
1068 static DECLARE_DELAYED_WORK(sl_reenable_unlock
, __split_lock_reenable_unlock
);
1070 static void __split_lock_reenable(struct work_struct
*work
)
1072 sld_update_msr(true);
1074 static DECLARE_DELAYED_WORK(sl_reenable
, __split_lock_reenable
);
1077 * If a CPU goes offline with pending delayed work to re-enable split lock
1078 * detection then the delayed work will be executed on some other CPU. That
1079 * handles releasing the buslock_sem, but because it executes on a
1080 * different CPU probably won't re-enable split lock detection. This is a
1081 * problem on HT systems since the sibling CPU on the same core may then be
1082 * left running with split lock detection disabled.
1084 * Unconditionally re-enable detection here.
1086 static int splitlock_cpu_offline(unsigned int cpu
)
1088 sld_update_msr(true);
1093 static void split_lock_warn(unsigned long ip
)
1095 struct delayed_work
*work
;
1098 if (!current
->reported_split_lock
)
1099 pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
1100 current
->comm
, current
->pid
, ip
);
1101 current
->reported_split_lock
= 1;
1103 if (sysctl_sld_mitigate
) {
1106 * sleep 10ms before trying to execute split lock.
1108 if (msleep_interruptible(10) > 0)
1112 * only allow one buslocked disabled core at a time.
1114 if (down_interruptible(&buslock_sem
) == -EINTR
)
1116 work
= &sl_reenable_unlock
;
1118 work
= &sl_reenable
;
1122 schedule_delayed_work_on(cpu
, work
, 2);
1124 /* Disable split lock detection on this CPU to make progress */
1125 sld_update_msr(false);
1129 bool handle_guest_split_lock(unsigned long ip
)
1131 if (sld_state
== sld_warn
) {
1132 split_lock_warn(ip
);
1136 pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
1137 current
->comm
, current
->pid
,
1138 sld_state
== sld_fatal
? "fatal" : "bogus", ip
);
1140 current
->thread
.error_code
= 0;
1141 current
->thread
.trap_nr
= X86_TRAP_AC
;
1142 force_sig_fault(SIGBUS
, BUS_ADRALN
, NULL
);
1145 EXPORT_SYMBOL_GPL(handle_guest_split_lock
);
1147 static void bus_lock_init(void)
1151 if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT
))
1154 rdmsrl(MSR_IA32_DEBUGCTLMSR
, val
);
1156 if ((boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT
) &&
1157 (sld_state
== sld_warn
|| sld_state
== sld_fatal
)) ||
1158 sld_state
== sld_off
) {
1160 * Warn and fatal are handled by #AC for split lock if #AC for
1161 * split lock is supported.
1163 val
&= ~DEBUGCTLMSR_BUS_LOCK_DETECT
;
1165 val
|= DEBUGCTLMSR_BUS_LOCK_DETECT
;
1168 wrmsrl(MSR_IA32_DEBUGCTLMSR
, val
);
1171 bool handle_user_split_lock(struct pt_regs
*regs
, long error_code
)
1173 if ((regs
->flags
& X86_EFLAGS_AC
) || sld_state
== sld_fatal
)
1175 split_lock_warn(regs
->ip
);
1179 void handle_bus_lock(struct pt_regs
*regs
)
1181 switch (sld_state
) {
1185 /* Enforce no more than bld_ratelimit bus locks/sec. */
1186 while (!__ratelimit(&bld_ratelimit
))
1188 /* Warn on the bus lock. */
1191 pr_warn_ratelimited("#DB: %s/%d took a bus_lock trap at address: 0x%lx\n",
1192 current
->comm
, current
->pid
, regs
->ip
);
1195 force_sig_fault(SIGBUS
, BUS_ADRALN
, NULL
);
1201 * CPU models that are known to have the per-core split-lock detection
1202 * feature even though they do not enumerate IA32_CORE_CAPABILITIES.
1204 static const struct x86_cpu_id split_lock_cpu_ids
[] __initconst
= {
1205 X86_MATCH_VFM(INTEL_ICELAKE_X
, 0),
1206 X86_MATCH_VFM(INTEL_ICELAKE_L
, 0),
1207 X86_MATCH_VFM(INTEL_ICELAKE_D
, 0),
1211 static void __init
split_lock_setup(struct cpuinfo_x86
*c
)
1213 const struct x86_cpu_id
*m
;
1216 if (boot_cpu_has(X86_FEATURE_HYPERVISOR
))
1219 /* Check for CPUs that have support but do not enumerate it: */
1220 m
= x86_match_cpu(split_lock_cpu_ids
);
1224 if (!cpu_has(c
, X86_FEATURE_CORE_CAPABILITIES
))
1228 * Not all bits in MSR_IA32_CORE_CAPS are architectural, but
1229 * MSR_IA32_CORE_CAPS_SPLIT_LOCK_DETECT is. All CPUs that set
1230 * it have split lock detection.
1232 rdmsrl(MSR_IA32_CORE_CAPS
, ia32_core_caps
);
1233 if (ia32_core_caps
& MSR_IA32_CORE_CAPS_SPLIT_LOCK_DETECT
)
1236 /* CPU is not in the model list and does not have the MSR bit: */
1240 cpu_model_supports_sld
= true;
1241 __split_lock_setup();
1244 static void sld_state_show(void)
1246 if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT
) &&
1247 !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT
))
1250 switch (sld_state
) {
1252 pr_info("disabled\n");
1255 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT
)) {
1256 pr_info("#AC: crashing the kernel on kernel split_locks and warning on user-space split_locks\n");
1257 if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
,
1258 "x86/splitlock", NULL
, splitlock_cpu_offline
) < 0)
1259 pr_warn("No splitlock CPU offline handler\n");
1260 } else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT
)) {
1261 pr_info("#DB: warning on user-space bus_locks\n");
1265 if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT
)) {
1266 pr_info("#AC: crashing the kernel on kernel split_locks and sending SIGBUS on user-space split_locks\n");
1267 } else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT
)) {
1268 pr_info("#DB: sending SIGBUS on user-space bus_locks%s\n",
1269 boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT
) ?
1270 " from non-WB" : "");
1274 if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT
))
1275 pr_info("#DB: setting system wide bus lock rate limit to %u/sec\n", bld_ratelimit
.burst
);
1280 void __init
sld_setup(struct cpuinfo_x86
*c
)
1282 split_lock_setup(c
);
1287 #define X86_HYBRID_CPU_TYPE_ID_SHIFT 24
1290 * get_this_hybrid_cpu_type() - Get the type of this hybrid CPU
1292 * Returns the CPU type [31:24] (i.e., Atom or Core) of a CPU in
1293 * a hybrid processor. If the processor is not hybrid, returns 0.
1295 u8
get_this_hybrid_cpu_type(void)
1297 if (!cpu_feature_enabled(X86_FEATURE_HYBRID_CPU
))
1300 return cpuid_eax(0x0000001a) >> X86_HYBRID_CPU_TYPE_ID_SHIFT
;