xref: /linux/arch/x86/kernel/cpu/mce/core.c (revision ec6347bb43395cb92126788a1a5b25302543f815)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Machine check handler.
4  *
5  * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
6  * Rest from unknown author(s).
7  * 2004 Andi Kleen. Rewrote most of it.
8  * Copyright 2008 Intel Corporation
9  * Author: Andi Kleen
10  */
11 
12 #include <linux/thread_info.h>
13 #include <linux/capability.h>
14 #include <linux/miscdevice.h>
15 #include <linux/ratelimit.h>
16 #include <linux/rcupdate.h>
17 #include <linux/kobject.h>
18 #include <linux/uaccess.h>
19 #include <linux/kdebug.h>
20 #include <linux/kernel.h>
21 #include <linux/percpu.h>
22 #include <linux/string.h>
23 #include <linux/device.h>
24 #include <linux/syscore_ops.h>
25 #include <linux/delay.h>
26 #include <linux/ctype.h>
27 #include <linux/sched.h>
28 #include <linux/sysfs.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/kmod.h>
33 #include <linux/poll.h>
34 #include <linux/nmi.h>
35 #include <linux/cpu.h>
36 #include <linux/ras.h>
37 #include <linux/smp.h>
38 #include <linux/fs.h>
39 #include <linux/mm.h>
40 #include <linux/debugfs.h>
41 #include <linux/irq_work.h>
42 #include <linux/export.h>
43 #include <linux/set_memory.h>
44 #include <linux/sync_core.h>
45 #include <linux/task_work.h>
46 #include <linux/hardirq.h>
47 
48 #include <asm/intel-family.h>
49 #include <asm/processor.h>
50 #include <asm/traps.h>
51 #include <asm/tlbflush.h>
52 #include <asm/mce.h>
53 #include <asm/msr.h>
54 #include <asm/reboot.h>
55 
56 #include "internal.h"
57 
58 /* sysfs synchronization */
59 static DEFINE_MUTEX(mce_sysfs_mutex);
60 
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/mce.h>
63 
64 #define SPINUNIT		100	/* 100ns */
65 
66 DEFINE_PER_CPU(unsigned, mce_exception_count);
67 
68 DEFINE_PER_CPU_READ_MOSTLY(unsigned int, mce_num_banks);
69 
70 struct mce_bank {
71 	u64			ctl;			/* subevents to enable */
72 	bool			init;			/* initialise bank? */
73 };
74 static DEFINE_PER_CPU_READ_MOSTLY(struct mce_bank[MAX_NR_BANKS], mce_banks_array);
75 
76 #define ATTR_LEN               16
77 /* One object for each MCE bank, shared by all CPUs */
78 struct mce_bank_dev {
79 	struct device_attribute	attr;			/* device attribute */
80 	char			attrname[ATTR_LEN];	/* attribute name */
81 	u8			bank;			/* bank number */
82 };
83 static struct mce_bank_dev mce_bank_devs[MAX_NR_BANKS];
84 
85 struct mce_vendor_flags mce_flags __read_mostly;
86 
87 struct mca_config mca_cfg __read_mostly = {
88 	.bootlog  = -1,
89 	/*
90 	 * Tolerant levels:
91 	 * 0: always panic on uncorrected errors, log corrected errors
92 	 * 1: panic or SIGBUS on uncorrected errors, log corrected errors
93 	 * 2: SIGBUS or log uncorrected errors (if possible), log corr. errors
94 	 * 3: never panic or SIGBUS, log all errors (for testing only)
95 	 */
96 	.tolerant = 1,
97 	.monarch_timeout = -1
98 };
99 
100 static DEFINE_PER_CPU(struct mce, mces_seen);
101 static unsigned long mce_need_notify;
102 static int cpu_missing;
103 
104 /*
105  * MCA banks polled by the period polling timer for corrected events.
106  * With Intel CMCI, this only has MCA banks which do not support CMCI (if any).
107  */
108 DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
109 	[0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
110 };
111 
112 /*
113  * MCA banks controlled through firmware first for corrected errors.
114  * This is a global list of banks for which we won't enable CMCI and we
115  * won't poll. Firmware controls these banks and is responsible for
116  * reporting corrected errors through GHES. Uncorrected/recoverable
117  * errors are still notified through a machine check.
118  */
119 mce_banks_t mce_banks_ce_disabled;
120 
121 static struct work_struct mce_work;
122 static struct irq_work mce_irq_work;
123 
124 static void (*quirk_no_way_out)(int bank, struct mce *m, struct pt_regs *regs);
125 
126 /*
127  * CPU/chipset specific EDAC code can register a notifier call here to print
128  * MCE errors in a human-readable form.
129  */
130 BLOCKING_NOTIFIER_HEAD(x86_mce_decoder_chain);
131 
132 /* Do initial initialization of a struct mce */
133 noinstr void mce_setup(struct mce *m)
134 {
135 	memset(m, 0, sizeof(struct mce));
136 	m->cpu = m->extcpu = smp_processor_id();
137 	/* need the internal __ version to avoid deadlocks */
138 	m->time = __ktime_get_real_seconds();
139 	m->cpuvendor = boot_cpu_data.x86_vendor;
140 	m->cpuid = cpuid_eax(1);
141 	m->socketid = cpu_data(m->extcpu).phys_proc_id;
142 	m->apicid = cpu_data(m->extcpu).initial_apicid;
143 	m->mcgcap = __rdmsr(MSR_IA32_MCG_CAP);
144 
145 	if (this_cpu_has(X86_FEATURE_INTEL_PPIN))
146 		m->ppin = __rdmsr(MSR_PPIN);
147 	else if (this_cpu_has(X86_FEATURE_AMD_PPIN))
148 		m->ppin = __rdmsr(MSR_AMD_PPIN);
149 
150 	m->microcode = boot_cpu_data.microcode;
151 }
152 
153 DEFINE_PER_CPU(struct mce, injectm);
154 EXPORT_PER_CPU_SYMBOL_GPL(injectm);
155 
156 void mce_log(struct mce *m)
157 {
158 	if (!mce_gen_pool_add(m))
159 		irq_work_queue(&mce_irq_work);
160 }
161 EXPORT_SYMBOL_GPL(mce_log);
162 
163 void mce_register_decode_chain(struct notifier_block *nb)
164 {
165 	if (WARN_ON(nb->priority > MCE_PRIO_MCELOG && nb->priority < MCE_PRIO_EDAC))
166 		return;
167 
168 	blocking_notifier_chain_register(&x86_mce_decoder_chain, nb);
169 }
170 EXPORT_SYMBOL_GPL(mce_register_decode_chain);
171 
172 void mce_unregister_decode_chain(struct notifier_block *nb)
173 {
174 	blocking_notifier_chain_unregister(&x86_mce_decoder_chain, nb);
175 }
176 EXPORT_SYMBOL_GPL(mce_unregister_decode_chain);
177 
178 static inline u32 ctl_reg(int bank)
179 {
180 	return MSR_IA32_MCx_CTL(bank);
181 }
182 
183 static inline u32 status_reg(int bank)
184 {
185 	return MSR_IA32_MCx_STATUS(bank);
186 }
187 
188 static inline u32 addr_reg(int bank)
189 {
190 	return MSR_IA32_MCx_ADDR(bank);
191 }
192 
193 static inline u32 misc_reg(int bank)
194 {
195 	return MSR_IA32_MCx_MISC(bank);
196 }
197 
198 static inline u32 smca_ctl_reg(int bank)
199 {
200 	return MSR_AMD64_SMCA_MCx_CTL(bank);
201 }
202 
203 static inline u32 smca_status_reg(int bank)
204 {
205 	return MSR_AMD64_SMCA_MCx_STATUS(bank);
206 }
207 
208 static inline u32 smca_addr_reg(int bank)
209 {
210 	return MSR_AMD64_SMCA_MCx_ADDR(bank);
211 }
212 
213 static inline u32 smca_misc_reg(int bank)
214 {
215 	return MSR_AMD64_SMCA_MCx_MISC(bank);
216 }
217 
218 struct mca_msr_regs msr_ops = {
219 	.ctl	= ctl_reg,
220 	.status	= status_reg,
221 	.addr	= addr_reg,
222 	.misc	= misc_reg
223 };
224 
225 static void __print_mce(struct mce *m)
226 {
227 	pr_emerg(HW_ERR "CPU %d: Machine Check%s: %Lx Bank %d: %016Lx\n",
228 		 m->extcpu,
229 		 (m->mcgstatus & MCG_STATUS_MCIP ? " Exception" : ""),
230 		 m->mcgstatus, m->bank, m->status);
231 
232 	if (m->ip) {
233 		pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
234 			!(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
235 			m->cs, m->ip);
236 
237 		if (m->cs == __KERNEL_CS)
238 			pr_cont("{%pS}", (void *)(unsigned long)m->ip);
239 		pr_cont("\n");
240 	}
241 
242 	pr_emerg(HW_ERR "TSC %llx ", m->tsc);
243 	if (m->addr)
244 		pr_cont("ADDR %llx ", m->addr);
245 	if (m->misc)
246 		pr_cont("MISC %llx ", m->misc);
247 	if (m->ppin)
248 		pr_cont("PPIN %llx ", m->ppin);
249 
250 	if (mce_flags.smca) {
251 		if (m->synd)
252 			pr_cont("SYND %llx ", m->synd);
253 		if (m->ipid)
254 			pr_cont("IPID %llx ", m->ipid);
255 	}
256 
257 	pr_cont("\n");
258 
259 	/*
260 	 * Note this output is parsed by external tools and old fields
261 	 * should not be changed.
262 	 */
263 	pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x microcode %x\n",
264 		m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid,
265 		m->microcode);
266 }
267 
268 static void print_mce(struct mce *m)
269 {
270 	__print_mce(m);
271 
272 	if (m->cpuvendor != X86_VENDOR_AMD && m->cpuvendor != X86_VENDOR_HYGON)
273 		pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
274 }
275 
276 #define PANIC_TIMEOUT 5 /* 5 seconds */
277 
278 static atomic_t mce_panicked;
279 
280 static int fake_panic;
281 static atomic_t mce_fake_panicked;
282 
283 /* Panic in progress. Enable interrupts and wait for final IPI */
284 static void wait_for_panic(void)
285 {
286 	long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
287 
288 	preempt_disable();
289 	local_irq_enable();
290 	while (timeout-- > 0)
291 		udelay(1);
292 	if (panic_timeout == 0)
293 		panic_timeout = mca_cfg.panic_timeout;
294 	panic("Panicing machine check CPU died");
295 }
296 
297 static void mce_panic(const char *msg, struct mce *final, char *exp)
298 {
299 	int apei_err = 0;
300 	struct llist_node *pending;
301 	struct mce_evt_llist *l;
302 
303 	if (!fake_panic) {
304 		/*
305 		 * Make sure only one CPU runs in machine check panic
306 		 */
307 		if (atomic_inc_return(&mce_panicked) > 1)
308 			wait_for_panic();
309 		barrier();
310 
311 		bust_spinlocks(1);
312 		console_verbose();
313 	} else {
314 		/* Don't log too much for fake panic */
315 		if (atomic_inc_return(&mce_fake_panicked) > 1)
316 			return;
317 	}
318 	pending = mce_gen_pool_prepare_records();
319 	/* First print corrected ones that are still unlogged */
320 	llist_for_each_entry(l, pending, llnode) {
321 		struct mce *m = &l->mce;
322 		if (!(m->status & MCI_STATUS_UC)) {
323 			print_mce(m);
324 			if (!apei_err)
325 				apei_err = apei_write_mce(m);
326 		}
327 	}
328 	/* Now print uncorrected but with the final one last */
329 	llist_for_each_entry(l, pending, llnode) {
330 		struct mce *m = &l->mce;
331 		if (!(m->status & MCI_STATUS_UC))
332 			continue;
333 		if (!final || mce_cmp(m, final)) {
334 			print_mce(m);
335 			if (!apei_err)
336 				apei_err = apei_write_mce(m);
337 		}
338 	}
339 	if (final) {
340 		print_mce(final);
341 		if (!apei_err)
342 			apei_err = apei_write_mce(final);
343 	}
344 	if (cpu_missing)
345 		pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
346 	if (exp)
347 		pr_emerg(HW_ERR "Machine check: %s\n", exp);
348 	if (!fake_panic) {
349 		if (panic_timeout == 0)
350 			panic_timeout = mca_cfg.panic_timeout;
351 		panic(msg);
352 	} else
353 		pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
354 }
355 
356 /* Support code for software error injection */
357 
358 static int msr_to_offset(u32 msr)
359 {
360 	unsigned bank = __this_cpu_read(injectm.bank);
361 
362 	if (msr == mca_cfg.rip_msr)
363 		return offsetof(struct mce, ip);
364 	if (msr == msr_ops.status(bank))
365 		return offsetof(struct mce, status);
366 	if (msr == msr_ops.addr(bank))
367 		return offsetof(struct mce, addr);
368 	if (msr == msr_ops.misc(bank))
369 		return offsetof(struct mce, misc);
370 	if (msr == MSR_IA32_MCG_STATUS)
371 		return offsetof(struct mce, mcgstatus);
372 	return -1;
373 }
374 
375 __visible bool ex_handler_rdmsr_fault(const struct exception_table_entry *fixup,
376 				      struct pt_regs *regs, int trapnr,
377 				      unsigned long error_code,
378 				      unsigned long fault_addr)
379 {
380 	pr_emerg("MSR access error: RDMSR from 0x%x at rIP: 0x%lx (%pS)\n",
381 		 (unsigned int)regs->cx, regs->ip, (void *)regs->ip);
382 
383 	show_stack_regs(regs);
384 
385 	panic("MCA architectural violation!\n");
386 
387 	while (true)
388 		cpu_relax();
389 
390 	return true;
391 }
392 
393 /* MSR access wrappers used for error injection */
394 static noinstr u64 mce_rdmsrl(u32 msr)
395 {
396 	DECLARE_ARGS(val, low, high);
397 
398 	if (__this_cpu_read(injectm.finished)) {
399 		int offset;
400 		u64 ret;
401 
402 		instrumentation_begin();
403 
404 		offset = msr_to_offset(msr);
405 		if (offset < 0)
406 			ret = 0;
407 		else
408 			ret = *(u64 *)((char *)this_cpu_ptr(&injectm) + offset);
409 
410 		instrumentation_end();
411 
412 		return ret;
413 	}
414 
415 	/*
416 	 * RDMSR on MCA MSRs should not fault. If they do, this is very much an
417 	 * architectural violation and needs to be reported to hw vendor. Panic
418 	 * the box to not allow any further progress.
419 	 */
420 	asm volatile("1: rdmsr\n"
421 		     "2:\n"
422 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_rdmsr_fault)
423 		     : EAX_EDX_RET(val, low, high) : "c" (msr));
424 
425 
426 	return EAX_EDX_VAL(val, low, high);
427 }
428 
429 __visible bool ex_handler_wrmsr_fault(const struct exception_table_entry *fixup,
430 				      struct pt_regs *regs, int trapnr,
431 				      unsigned long error_code,
432 				      unsigned long fault_addr)
433 {
434 	pr_emerg("MSR access error: WRMSR to 0x%x (tried to write 0x%08x%08x) at rIP: 0x%lx (%pS)\n",
435 		 (unsigned int)regs->cx, (unsigned int)regs->dx, (unsigned int)regs->ax,
436 		  regs->ip, (void *)regs->ip);
437 
438 	show_stack_regs(regs);
439 
440 	panic("MCA architectural violation!\n");
441 
442 	while (true)
443 		cpu_relax();
444 
445 	return true;
446 }
447 
448 static noinstr void mce_wrmsrl(u32 msr, u64 v)
449 {
450 	u32 low, high;
451 
452 	if (__this_cpu_read(injectm.finished)) {
453 		int offset;
454 
455 		instrumentation_begin();
456 
457 		offset = msr_to_offset(msr);
458 		if (offset >= 0)
459 			*(u64 *)((char *)this_cpu_ptr(&injectm) + offset) = v;
460 
461 		instrumentation_end();
462 
463 		return;
464 	}
465 
466 	low  = (u32)v;
467 	high = (u32)(v >> 32);
468 
469 	/* See comment in mce_rdmsrl() */
470 	asm volatile("1: wrmsr\n"
471 		     "2:\n"
472 		     _ASM_EXTABLE_HANDLE(1b, 2b, ex_handler_wrmsr_fault)
473 		     : : "c" (msr), "a"(low), "d" (high) : "memory");
474 }
475 
476 /*
477  * Collect all global (w.r.t. this processor) status about this machine
478  * check into our "mce" struct so that we can use it later to assess
479  * the severity of the problem as we read per-bank specific details.
480  */
481 static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
482 {
483 	mce_setup(m);
484 
485 	m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
486 	if (regs) {
487 		/*
488 		 * Get the address of the instruction at the time of
489 		 * the machine check error.
490 		 */
491 		if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
492 			m->ip = regs->ip;
493 			m->cs = regs->cs;
494 
495 			/*
496 			 * When in VM86 mode make the cs look like ring 3
497 			 * always. This is a lie, but it's better than passing
498 			 * the additional vm86 bit around everywhere.
499 			 */
500 			if (v8086_mode(regs))
501 				m->cs |= 3;
502 		}
503 		/* Use accurate RIP reporting if available. */
504 		if (mca_cfg.rip_msr)
505 			m->ip = mce_rdmsrl(mca_cfg.rip_msr);
506 	}
507 }
508 
509 int mce_available(struct cpuinfo_x86 *c)
510 {
511 	if (mca_cfg.disabled)
512 		return 0;
513 	return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
514 }
515 
516 static void mce_schedule_work(void)
517 {
518 	if (!mce_gen_pool_empty())
519 		schedule_work(&mce_work);
520 }
521 
522 static void mce_irq_work_cb(struct irq_work *entry)
523 {
524 	mce_schedule_work();
525 }
526 
527 /*
528  * Check if the address reported by the CPU is in a format we can parse.
529  * It would be possible to add code for most other cases, but all would
530  * be somewhat complicated (e.g. segment offset would require an instruction
531  * parser). So only support physical addresses up to page granuality for now.
532  */
533 int mce_usable_address(struct mce *m)
534 {
535 	if (!(m->status & MCI_STATUS_ADDRV))
536 		return 0;
537 
538 	/* Checks after this one are Intel/Zhaoxin-specific: */
539 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
540 	    boot_cpu_data.x86_vendor != X86_VENDOR_ZHAOXIN)
541 		return 1;
542 
543 	if (!(m->status & MCI_STATUS_MISCV))
544 		return 0;
545 
546 	if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
547 		return 0;
548 
549 	if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
550 		return 0;
551 
552 	return 1;
553 }
554 EXPORT_SYMBOL_GPL(mce_usable_address);
555 
556 bool mce_is_memory_error(struct mce *m)
557 {
558 	switch (m->cpuvendor) {
559 	case X86_VENDOR_AMD:
560 	case X86_VENDOR_HYGON:
561 		return amd_mce_is_memory_error(m);
562 
563 	case X86_VENDOR_INTEL:
564 	case X86_VENDOR_ZHAOXIN:
565 		/*
566 		 * Intel SDM Volume 3B - 15.9.2 Compound Error Codes
567 		 *
568 		 * Bit 7 of the MCACOD field of IA32_MCi_STATUS is used for
569 		 * indicating a memory error. Bit 8 is used for indicating a
570 		 * cache hierarchy error. The combination of bit 2 and bit 3
571 		 * is used for indicating a `generic' cache hierarchy error
572 		 * But we can't just blindly check the above bits, because if
573 		 * bit 11 is set, then it is a bus/interconnect error - and
574 		 * either way the above bits just gives more detail on what
575 		 * bus/interconnect error happened. Note that bit 12 can be
576 		 * ignored, as it's the "filter" bit.
577 		 */
578 		return (m->status & 0xef80) == BIT(7) ||
579 		       (m->status & 0xef00) == BIT(8) ||
580 		       (m->status & 0xeffc) == 0xc;
581 
582 	default:
583 		return false;
584 	}
585 }
586 EXPORT_SYMBOL_GPL(mce_is_memory_error);
587 
588 static bool whole_page(struct mce *m)
589 {
590 	if (!mca_cfg.ser || !(m->status & MCI_STATUS_MISCV))
591 		return true;
592 
593 	return MCI_MISC_ADDR_LSB(m->misc) >= PAGE_SHIFT;
594 }
595 
596 bool mce_is_correctable(struct mce *m)
597 {
598 	if (m->cpuvendor == X86_VENDOR_AMD && m->status & MCI_STATUS_DEFERRED)
599 		return false;
600 
601 	if (m->cpuvendor == X86_VENDOR_HYGON && m->status & MCI_STATUS_DEFERRED)
602 		return false;
603 
604 	if (m->status & MCI_STATUS_UC)
605 		return false;
606 
607 	return true;
608 }
609 EXPORT_SYMBOL_GPL(mce_is_correctable);
610 
611 static int mce_early_notifier(struct notifier_block *nb, unsigned long val,
612 			      void *data)
613 {
614 	struct mce *m = (struct mce *)data;
615 
616 	if (!m)
617 		return NOTIFY_DONE;
618 
619 	/* Emit the trace record: */
620 	trace_mce_record(m);
621 
622 	set_bit(0, &mce_need_notify);
623 
624 	mce_notify_irq();
625 
626 	return NOTIFY_DONE;
627 }
628 
629 static struct notifier_block early_nb = {
630 	.notifier_call	= mce_early_notifier,
631 	.priority	= MCE_PRIO_EARLY,
632 };
633 
634 static int uc_decode_notifier(struct notifier_block *nb, unsigned long val,
635 			      void *data)
636 {
637 	struct mce *mce = (struct mce *)data;
638 	unsigned long pfn;
639 
640 	if (!mce || !mce_usable_address(mce))
641 		return NOTIFY_DONE;
642 
643 	if (mce->severity != MCE_AO_SEVERITY &&
644 	    mce->severity != MCE_DEFERRED_SEVERITY)
645 		return NOTIFY_DONE;
646 
647 	pfn = mce->addr >> PAGE_SHIFT;
648 	if (!memory_failure(pfn, 0)) {
649 		set_mce_nospec(pfn, whole_page(mce));
650 		mce->kflags |= MCE_HANDLED_UC;
651 	}
652 
653 	return NOTIFY_OK;
654 }
655 
656 static struct notifier_block mce_uc_nb = {
657 	.notifier_call	= uc_decode_notifier,
658 	.priority	= MCE_PRIO_UC,
659 };
660 
661 static int mce_default_notifier(struct notifier_block *nb, unsigned long val,
662 				void *data)
663 {
664 	struct mce *m = (struct mce *)data;
665 
666 	if (!m)
667 		return NOTIFY_DONE;
668 
669 	if (mca_cfg.print_all || !m->kflags)
670 		__print_mce(m);
671 
672 	return NOTIFY_DONE;
673 }
674 
675 static struct notifier_block mce_default_nb = {
676 	.notifier_call	= mce_default_notifier,
677 	/* lowest prio, we want it to run last. */
678 	.priority	= MCE_PRIO_LOWEST,
679 };
680 
681 /*
682  * Read ADDR and MISC registers.
683  */
684 static void mce_read_aux(struct mce *m, int i)
685 {
686 	if (m->status & MCI_STATUS_MISCV)
687 		m->misc = mce_rdmsrl(msr_ops.misc(i));
688 
689 	if (m->status & MCI_STATUS_ADDRV) {
690 		m->addr = mce_rdmsrl(msr_ops.addr(i));
691 
692 		/*
693 		 * Mask the reported address by the reported granularity.
694 		 */
695 		if (mca_cfg.ser && (m->status & MCI_STATUS_MISCV)) {
696 			u8 shift = MCI_MISC_ADDR_LSB(m->misc);
697 			m->addr >>= shift;
698 			m->addr <<= shift;
699 		}
700 
701 		/*
702 		 * Extract [55:<lsb>] where lsb is the least significant
703 		 * *valid* bit of the address bits.
704 		 */
705 		if (mce_flags.smca) {
706 			u8 lsb = (m->addr >> 56) & 0x3f;
707 
708 			m->addr &= GENMASK_ULL(55, lsb);
709 		}
710 	}
711 
712 	if (mce_flags.smca) {
713 		m->ipid = mce_rdmsrl(MSR_AMD64_SMCA_MCx_IPID(i));
714 
715 		if (m->status & MCI_STATUS_SYNDV)
716 			m->synd = mce_rdmsrl(MSR_AMD64_SMCA_MCx_SYND(i));
717 	}
718 }
719 
720 DEFINE_PER_CPU(unsigned, mce_poll_count);
721 
722 /*
723  * Poll for corrected events or events that happened before reset.
724  * Those are just logged through /dev/mcelog.
725  *
726  * This is executed in standard interrupt context.
727  *
728  * Note: spec recommends to panic for fatal unsignalled
729  * errors here. However this would be quite problematic --
730  * we would need to reimplement the Monarch handling and
731  * it would mess up the exclusion between exception handler
732  * and poll handler -- * so we skip this for now.
733  * These cases should not happen anyways, or only when the CPU
734  * is already totally * confused. In this case it's likely it will
735  * not fully execute the machine check handler either.
736  */
737 bool machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
738 {
739 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
740 	bool error_seen = false;
741 	struct mce m;
742 	int i;
743 
744 	this_cpu_inc(mce_poll_count);
745 
746 	mce_gather_info(&m, NULL);
747 
748 	if (flags & MCP_TIMESTAMP)
749 		m.tsc = rdtsc();
750 
751 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
752 		if (!mce_banks[i].ctl || !test_bit(i, *b))
753 			continue;
754 
755 		m.misc = 0;
756 		m.addr = 0;
757 		m.bank = i;
758 
759 		barrier();
760 		m.status = mce_rdmsrl(msr_ops.status(i));
761 
762 		/* If this entry is not valid, ignore it */
763 		if (!(m.status & MCI_STATUS_VAL))
764 			continue;
765 
766 		/*
767 		 * If we are logging everything (at CPU online) or this
768 		 * is a corrected error, then we must log it.
769 		 */
770 		if ((flags & MCP_UC) || !(m.status & MCI_STATUS_UC))
771 			goto log_it;
772 
773 		/*
774 		 * Newer Intel systems that support software error
775 		 * recovery need to make additional checks. Other
776 		 * CPUs should skip over uncorrected errors, but log
777 		 * everything else.
778 		 */
779 		if (!mca_cfg.ser) {
780 			if (m.status & MCI_STATUS_UC)
781 				continue;
782 			goto log_it;
783 		}
784 
785 		/* Log "not enabled" (speculative) errors */
786 		if (!(m.status & MCI_STATUS_EN))
787 			goto log_it;
788 
789 		/*
790 		 * Log UCNA (SDM: 15.6.3 "UCR Error Classification")
791 		 * UC == 1 && PCC == 0 && S == 0
792 		 */
793 		if (!(m.status & MCI_STATUS_PCC) && !(m.status & MCI_STATUS_S))
794 			goto log_it;
795 
796 		/*
797 		 * Skip anything else. Presumption is that our read of this
798 		 * bank is racing with a machine check. Leave the log alone
799 		 * for do_machine_check() to deal with it.
800 		 */
801 		continue;
802 
803 log_it:
804 		error_seen = true;
805 
806 		if (flags & MCP_DONTLOG)
807 			goto clear_it;
808 
809 		mce_read_aux(&m, i);
810 		m.severity = mce_severity(&m, mca_cfg.tolerant, NULL, false);
811 		/*
812 		 * Don't get the IP here because it's unlikely to
813 		 * have anything to do with the actual error location.
814 		 */
815 
816 		if (mca_cfg.dont_log_ce && !mce_usable_address(&m))
817 			goto clear_it;
818 
819 		mce_log(&m);
820 
821 clear_it:
822 		/*
823 		 * Clear state for this bank.
824 		 */
825 		mce_wrmsrl(msr_ops.status(i), 0);
826 	}
827 
828 	/*
829 	 * Don't clear MCG_STATUS here because it's only defined for
830 	 * exceptions.
831 	 */
832 
833 	sync_core();
834 
835 	return error_seen;
836 }
837 EXPORT_SYMBOL_GPL(machine_check_poll);
838 
839 /*
840  * Do a quick check if any of the events requires a panic.
841  * This decides if we keep the events around or clear them.
842  */
843 static int mce_no_way_out(struct mce *m, char **msg, unsigned long *validp,
844 			  struct pt_regs *regs)
845 {
846 	char *tmp = *msg;
847 	int i;
848 
849 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
850 		m->status = mce_rdmsrl(msr_ops.status(i));
851 		if (!(m->status & MCI_STATUS_VAL))
852 			continue;
853 
854 		__set_bit(i, validp);
855 		if (quirk_no_way_out)
856 			quirk_no_way_out(i, m, regs);
857 
858 		m->bank = i;
859 		if (mce_severity(m, mca_cfg.tolerant, &tmp, true) >= MCE_PANIC_SEVERITY) {
860 			mce_read_aux(m, i);
861 			*msg = tmp;
862 			return 1;
863 		}
864 	}
865 	return 0;
866 }
867 
868 /*
869  * Variable to establish order between CPUs while scanning.
870  * Each CPU spins initially until executing is equal its number.
871  */
872 static atomic_t mce_executing;
873 
874 /*
875  * Defines order of CPUs on entry. First CPU becomes Monarch.
876  */
877 static atomic_t mce_callin;
878 
879 /*
880  * Check if a timeout waiting for other CPUs happened.
881  */
882 static int mce_timed_out(u64 *t, const char *msg)
883 {
884 	/*
885 	 * The others already did panic for some reason.
886 	 * Bail out like in a timeout.
887 	 * rmb() to tell the compiler that system_state
888 	 * might have been modified by someone else.
889 	 */
890 	rmb();
891 	if (atomic_read(&mce_panicked))
892 		wait_for_panic();
893 	if (!mca_cfg.monarch_timeout)
894 		goto out;
895 	if ((s64)*t < SPINUNIT) {
896 		if (mca_cfg.tolerant <= 1)
897 			mce_panic(msg, NULL, NULL);
898 		cpu_missing = 1;
899 		return 1;
900 	}
901 	*t -= SPINUNIT;
902 out:
903 	touch_nmi_watchdog();
904 	return 0;
905 }
906 
907 /*
908  * The Monarch's reign.  The Monarch is the CPU who entered
909  * the machine check handler first. It waits for the others to
910  * raise the exception too and then grades them. When any
911  * error is fatal panic. Only then let the others continue.
912  *
913  * The other CPUs entering the MCE handler will be controlled by the
914  * Monarch. They are called Subjects.
915  *
916  * This way we prevent any potential data corruption in a unrecoverable case
917  * and also makes sure always all CPU's errors are examined.
918  *
919  * Also this detects the case of a machine check event coming from outer
920  * space (not detected by any CPUs) In this case some external agent wants
921  * us to shut down, so panic too.
922  *
923  * The other CPUs might still decide to panic if the handler happens
924  * in a unrecoverable place, but in this case the system is in a semi-stable
925  * state and won't corrupt anything by itself. It's ok to let the others
926  * continue for a bit first.
927  *
928  * All the spin loops have timeouts; when a timeout happens a CPU
929  * typically elects itself to be Monarch.
930  */
931 static void mce_reign(void)
932 {
933 	int cpu;
934 	struct mce *m = NULL;
935 	int global_worst = 0;
936 	char *msg = NULL;
937 
938 	/*
939 	 * This CPU is the Monarch and the other CPUs have run
940 	 * through their handlers.
941 	 * Grade the severity of the errors of all the CPUs.
942 	 */
943 	for_each_possible_cpu(cpu) {
944 		struct mce *mtmp = &per_cpu(mces_seen, cpu);
945 
946 		if (mtmp->severity > global_worst) {
947 			global_worst = mtmp->severity;
948 			m = &per_cpu(mces_seen, cpu);
949 		}
950 	}
951 
952 	/*
953 	 * Cannot recover? Panic here then.
954 	 * This dumps all the mces in the log buffer and stops the
955 	 * other CPUs.
956 	 */
957 	if (m && global_worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3) {
958 		/* call mce_severity() to get "msg" for panic */
959 		mce_severity(m, mca_cfg.tolerant, &msg, true);
960 		mce_panic("Fatal machine check", m, msg);
961 	}
962 
963 	/*
964 	 * For UC somewhere we let the CPU who detects it handle it.
965 	 * Also must let continue the others, otherwise the handling
966 	 * CPU could deadlock on a lock.
967 	 */
968 
969 	/*
970 	 * No machine check event found. Must be some external
971 	 * source or one CPU is hung. Panic.
972 	 */
973 	if (global_worst <= MCE_KEEP_SEVERITY && mca_cfg.tolerant < 3)
974 		mce_panic("Fatal machine check from unknown source", NULL, NULL);
975 
976 	/*
977 	 * Now clear all the mces_seen so that they don't reappear on
978 	 * the next mce.
979 	 */
980 	for_each_possible_cpu(cpu)
981 		memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
982 }
983 
984 static atomic_t global_nwo;
985 
986 /*
987  * Start of Monarch synchronization. This waits until all CPUs have
988  * entered the exception handler and then determines if any of them
989  * saw a fatal event that requires panic. Then it executes them
990  * in the entry order.
991  * TBD double check parallel CPU hotunplug
992  */
993 static int mce_start(int *no_way_out)
994 {
995 	int order;
996 	int cpus = num_online_cpus();
997 	u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
998 
999 	if (!timeout)
1000 		return -1;
1001 
1002 	atomic_add(*no_way_out, &global_nwo);
1003 	/*
1004 	 * Rely on the implied barrier below, such that global_nwo
1005 	 * is updated before mce_callin.
1006 	 */
1007 	order = atomic_inc_return(&mce_callin);
1008 
1009 	/*
1010 	 * Wait for everyone.
1011 	 */
1012 	while (atomic_read(&mce_callin) != cpus) {
1013 		if (mce_timed_out(&timeout,
1014 				  "Timeout: Not all CPUs entered broadcast exception handler")) {
1015 			atomic_set(&global_nwo, 0);
1016 			return -1;
1017 		}
1018 		ndelay(SPINUNIT);
1019 	}
1020 
1021 	/*
1022 	 * mce_callin should be read before global_nwo
1023 	 */
1024 	smp_rmb();
1025 
1026 	if (order == 1) {
1027 		/*
1028 		 * Monarch: Starts executing now, the others wait.
1029 		 */
1030 		atomic_set(&mce_executing, 1);
1031 	} else {
1032 		/*
1033 		 * Subject: Now start the scanning loop one by one in
1034 		 * the original callin order.
1035 		 * This way when there are any shared banks it will be
1036 		 * only seen by one CPU before cleared, avoiding duplicates.
1037 		 */
1038 		while (atomic_read(&mce_executing) < order) {
1039 			if (mce_timed_out(&timeout,
1040 					  "Timeout: Subject CPUs unable to finish machine check processing")) {
1041 				atomic_set(&global_nwo, 0);
1042 				return -1;
1043 			}
1044 			ndelay(SPINUNIT);
1045 		}
1046 	}
1047 
1048 	/*
1049 	 * Cache the global no_way_out state.
1050 	 */
1051 	*no_way_out = atomic_read(&global_nwo);
1052 
1053 	return order;
1054 }
1055 
1056 /*
1057  * Synchronize between CPUs after main scanning loop.
1058  * This invokes the bulk of the Monarch processing.
1059  */
1060 static int mce_end(int order)
1061 {
1062 	int ret = -1;
1063 	u64 timeout = (u64)mca_cfg.monarch_timeout * NSEC_PER_USEC;
1064 
1065 	if (!timeout)
1066 		goto reset;
1067 	if (order < 0)
1068 		goto reset;
1069 
1070 	/*
1071 	 * Allow others to run.
1072 	 */
1073 	atomic_inc(&mce_executing);
1074 
1075 	if (order == 1) {
1076 		/* CHECKME: Can this race with a parallel hotplug? */
1077 		int cpus = num_online_cpus();
1078 
1079 		/*
1080 		 * Monarch: Wait for everyone to go through their scanning
1081 		 * loops.
1082 		 */
1083 		while (atomic_read(&mce_executing) <= cpus) {
1084 			if (mce_timed_out(&timeout,
1085 					  "Timeout: Monarch CPU unable to finish machine check processing"))
1086 				goto reset;
1087 			ndelay(SPINUNIT);
1088 		}
1089 
1090 		mce_reign();
1091 		barrier();
1092 		ret = 0;
1093 	} else {
1094 		/*
1095 		 * Subject: Wait for Monarch to finish.
1096 		 */
1097 		while (atomic_read(&mce_executing) != 0) {
1098 			if (mce_timed_out(&timeout,
1099 					  "Timeout: Monarch CPU did not finish machine check processing"))
1100 				goto reset;
1101 			ndelay(SPINUNIT);
1102 		}
1103 
1104 		/*
1105 		 * Don't reset anything. That's done by the Monarch.
1106 		 */
1107 		return 0;
1108 	}
1109 
1110 	/*
1111 	 * Reset all global state.
1112 	 */
1113 reset:
1114 	atomic_set(&global_nwo, 0);
1115 	atomic_set(&mce_callin, 0);
1116 	barrier();
1117 
1118 	/*
1119 	 * Let others run again.
1120 	 */
1121 	atomic_set(&mce_executing, 0);
1122 	return ret;
1123 }
1124 
1125 static void mce_clear_state(unsigned long *toclear)
1126 {
1127 	int i;
1128 
1129 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1130 		if (test_bit(i, toclear))
1131 			mce_wrmsrl(msr_ops.status(i), 0);
1132 	}
1133 }
1134 
1135 /*
1136  * Cases where we avoid rendezvous handler timeout:
1137  * 1) If this CPU is offline.
1138  *
1139  * 2) If crashing_cpu was set, e.g. we're entering kdump and we need to
1140  *  skip those CPUs which remain looping in the 1st kernel - see
1141  *  crash_nmi_callback().
1142  *
1143  * Note: there still is a small window between kexec-ing and the new,
1144  * kdump kernel establishing a new #MC handler where a broadcasted MCE
1145  * might not get handled properly.
1146  */
1147 static noinstr bool mce_check_crashing_cpu(void)
1148 {
1149 	unsigned int cpu = smp_processor_id();
1150 
1151 	if (arch_cpu_is_offline(cpu) ||
1152 	    (crashing_cpu != -1 && crashing_cpu != cpu)) {
1153 		u64 mcgstatus;
1154 
1155 		mcgstatus = __rdmsr(MSR_IA32_MCG_STATUS);
1156 
1157 		if (boot_cpu_data.x86_vendor == X86_VENDOR_ZHAOXIN) {
1158 			if (mcgstatus & MCG_STATUS_LMCES)
1159 				return false;
1160 		}
1161 
1162 		if (mcgstatus & MCG_STATUS_RIPV) {
1163 			__wrmsr(MSR_IA32_MCG_STATUS, 0, 0);
1164 			return true;
1165 		}
1166 	}
1167 	return false;
1168 }
1169 
1170 static void __mc_scan_banks(struct mce *m, struct mce *final,
1171 			    unsigned long *toclear, unsigned long *valid_banks,
1172 			    int no_way_out, int *worst)
1173 {
1174 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1175 	struct mca_config *cfg = &mca_cfg;
1176 	int severity, i;
1177 
1178 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1179 		__clear_bit(i, toclear);
1180 		if (!test_bit(i, valid_banks))
1181 			continue;
1182 
1183 		if (!mce_banks[i].ctl)
1184 			continue;
1185 
1186 		m->misc = 0;
1187 		m->addr = 0;
1188 		m->bank = i;
1189 
1190 		m->status = mce_rdmsrl(msr_ops.status(i));
1191 		if (!(m->status & MCI_STATUS_VAL))
1192 			continue;
1193 
1194 		/*
1195 		 * Corrected or non-signaled errors are handled by
1196 		 * machine_check_poll(). Leave them alone, unless this panics.
1197 		 */
1198 		if (!(m->status & (cfg->ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
1199 			!no_way_out)
1200 			continue;
1201 
1202 		/* Set taint even when machine check was not enabled. */
1203 		add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
1204 
1205 		severity = mce_severity(m, cfg->tolerant, NULL, true);
1206 
1207 		/*
1208 		 * When machine check was for corrected/deferred handler don't
1209 		 * touch, unless we're panicking.
1210 		 */
1211 		if ((severity == MCE_KEEP_SEVERITY ||
1212 		     severity == MCE_UCNA_SEVERITY) && !no_way_out)
1213 			continue;
1214 
1215 		__set_bit(i, toclear);
1216 
1217 		/* Machine check event was not enabled. Clear, but ignore. */
1218 		if (severity == MCE_NO_SEVERITY)
1219 			continue;
1220 
1221 		mce_read_aux(m, i);
1222 
1223 		/* assuming valid severity level != 0 */
1224 		m->severity = severity;
1225 
1226 		mce_log(m);
1227 
1228 		if (severity > *worst) {
1229 			*final = *m;
1230 			*worst = severity;
1231 		}
1232 	}
1233 
1234 	/* mce_clear_state will clear *final, save locally for use later */
1235 	*m = *final;
1236 }
1237 
1238 static void kill_me_now(struct callback_head *ch)
1239 {
1240 	force_sig(SIGBUS);
1241 }
1242 
1243 static void kill_me_maybe(struct callback_head *cb)
1244 {
1245 	struct task_struct *p = container_of(cb, struct task_struct, mce_kill_me);
1246 	int flags = MF_ACTION_REQUIRED;
1247 
1248 	pr_err("Uncorrected hardware memory error in user-access at %llx", p->mce_addr);
1249 
1250 	if (!p->mce_ripv)
1251 		flags |= MF_MUST_KILL;
1252 
1253 	if (!memory_failure(p->mce_addr >> PAGE_SHIFT, flags)) {
1254 		set_mce_nospec(p->mce_addr >> PAGE_SHIFT, p->mce_whole_page);
1255 		sync_core();
1256 		return;
1257 	}
1258 
1259 	pr_err("Memory error not recovered");
1260 	kill_me_now(cb);
1261 }
1262 
1263 /*
1264  * The actual machine check handler. This only handles real
1265  * exceptions when something got corrupted coming in through int 18.
1266  *
1267  * This is executed in NMI context not subject to normal locking rules. This
1268  * implies that most kernel services cannot be safely used. Don't even
1269  * think about putting a printk in there!
1270  *
1271  * On Intel systems this is entered on all CPUs in parallel through
1272  * MCE broadcast. However some CPUs might be broken beyond repair,
1273  * so be always careful when synchronizing with others.
1274  *
1275  * Tracing and kprobes are disabled: if we interrupted a kernel context
1276  * with IF=1, we need to minimize stack usage.  There are also recursion
1277  * issues: if the machine check was due to a failure of the memory
1278  * backing the user stack, tracing that reads the user stack will cause
1279  * potentially infinite recursion.
1280  */
1281 noinstr void do_machine_check(struct pt_regs *regs)
1282 {
1283 	DECLARE_BITMAP(valid_banks, MAX_NR_BANKS);
1284 	DECLARE_BITMAP(toclear, MAX_NR_BANKS);
1285 	struct mca_config *cfg = &mca_cfg;
1286 	struct mce m, *final;
1287 	char *msg = NULL;
1288 	int worst = 0;
1289 
1290 	/*
1291 	 * Establish sequential order between the CPUs entering the machine
1292 	 * check handler.
1293 	 */
1294 	int order = -1;
1295 
1296 	/*
1297 	 * If no_way_out gets set, there is no safe way to recover from this
1298 	 * MCE.  If mca_cfg.tolerant is cranked up, we'll try anyway.
1299 	 */
1300 	int no_way_out = 0;
1301 
1302 	/*
1303 	 * If kill_it gets set, there might be a way to recover from this
1304 	 * error.
1305 	 */
1306 	int kill_it = 0;
1307 
1308 	/*
1309 	 * MCEs are always local on AMD. Same is determined by MCG_STATUS_LMCES
1310 	 * on Intel.
1311 	 */
1312 	int lmce = 1;
1313 
1314 	this_cpu_inc(mce_exception_count);
1315 
1316 	mce_gather_info(&m, regs);
1317 	m.tsc = rdtsc();
1318 
1319 	final = this_cpu_ptr(&mces_seen);
1320 	*final = m;
1321 
1322 	memset(valid_banks, 0, sizeof(valid_banks));
1323 	no_way_out = mce_no_way_out(&m, &msg, valid_banks, regs);
1324 
1325 	barrier();
1326 
1327 	/*
1328 	 * When no restart IP might need to kill or panic.
1329 	 * Assume the worst for now, but if we find the
1330 	 * severity is MCE_AR_SEVERITY we have other options.
1331 	 */
1332 	if (!(m.mcgstatus & MCG_STATUS_RIPV))
1333 		kill_it = 1;
1334 
1335 	/*
1336 	 * Check if this MCE is signaled to only this logical processor,
1337 	 * on Intel, Zhaoxin only.
1338 	 */
1339 	if (m.cpuvendor == X86_VENDOR_INTEL ||
1340 	    m.cpuvendor == X86_VENDOR_ZHAOXIN)
1341 		lmce = m.mcgstatus & MCG_STATUS_LMCES;
1342 
1343 	/*
1344 	 * Local machine check may already know that we have to panic.
1345 	 * Broadcast machine check begins rendezvous in mce_start()
1346 	 * Go through all banks in exclusion of the other CPUs. This way we
1347 	 * don't report duplicated events on shared banks because the first one
1348 	 * to see it will clear it.
1349 	 */
1350 	if (lmce) {
1351 		if (no_way_out)
1352 			mce_panic("Fatal local machine check", &m, msg);
1353 	} else {
1354 		order = mce_start(&no_way_out);
1355 	}
1356 
1357 	__mc_scan_banks(&m, final, toclear, valid_banks, no_way_out, &worst);
1358 
1359 	if (!no_way_out)
1360 		mce_clear_state(toclear);
1361 
1362 	/*
1363 	 * Do most of the synchronization with other CPUs.
1364 	 * When there's any problem use only local no_way_out state.
1365 	 */
1366 	if (!lmce) {
1367 		if (mce_end(order) < 0)
1368 			no_way_out = worst >= MCE_PANIC_SEVERITY;
1369 	} else {
1370 		/*
1371 		 * If there was a fatal machine check we should have
1372 		 * already called mce_panic earlier in this function.
1373 		 * Since we re-read the banks, we might have found
1374 		 * something new. Check again to see if we found a
1375 		 * fatal error. We call "mce_severity()" again to
1376 		 * make sure we have the right "msg".
1377 		 */
1378 		if (worst >= MCE_PANIC_SEVERITY && mca_cfg.tolerant < 3) {
1379 			mce_severity(&m, cfg->tolerant, &msg, true);
1380 			mce_panic("Local fatal machine check!", &m, msg);
1381 		}
1382 	}
1383 
1384 	/*
1385 	 * If tolerant is at an insane level we drop requests to kill
1386 	 * processes and continue even when there is no way out.
1387 	 */
1388 	if (cfg->tolerant == 3)
1389 		kill_it = 0;
1390 	else if (no_way_out)
1391 		mce_panic("Fatal machine check on current CPU", &m, msg);
1392 
1393 	if (worst > 0)
1394 		irq_work_queue(&mce_irq_work);
1395 
1396 	if (worst != MCE_AR_SEVERITY && !kill_it)
1397 		goto out;
1398 
1399 	/* Fault was in user mode and we need to take some action */
1400 	if ((m.cs & 3) == 3) {
1401 		/* If this triggers there is no way to recover. Die hard. */
1402 		BUG_ON(!on_thread_stack() || !user_mode(regs));
1403 
1404 		current->mce_addr = m.addr;
1405 		current->mce_ripv = !!(m.mcgstatus & MCG_STATUS_RIPV);
1406 		current->mce_whole_page = whole_page(&m);
1407 		current->mce_kill_me.func = kill_me_maybe;
1408 		if (kill_it)
1409 			current->mce_kill_me.func = kill_me_now;
1410 		task_work_add(current, &current->mce_kill_me, true);
1411 	} else {
1412 		/*
1413 		 * Handle an MCE which has happened in kernel space but from
1414 		 * which the kernel can recover: ex_has_fault_handler() has
1415 		 * already verified that the rIP at which the error happened is
1416 		 * a rIP from which the kernel can recover (by jumping to
1417 		 * recovery code specified in _ASM_EXTABLE_FAULT()) and the
1418 		 * corresponding exception handler which would do that is the
1419 		 * proper one.
1420 		 */
1421 		if (m.kflags & MCE_IN_KERNEL_RECOV) {
1422 			if (!fixup_exception(regs, X86_TRAP_MC, 0, 0))
1423 				mce_panic("Failed kernel mode recovery", &m, msg);
1424 		}
1425 	}
1426 out:
1427 	mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
1428 }
1429 EXPORT_SYMBOL_GPL(do_machine_check);
1430 
1431 #ifndef CONFIG_MEMORY_FAILURE
1432 int memory_failure(unsigned long pfn, int flags)
1433 {
1434 	/* mce_severity() should not hand us an ACTION_REQUIRED error */
1435 	BUG_ON(flags & MF_ACTION_REQUIRED);
1436 	pr_err("Uncorrected memory error in page 0x%lx ignored\n"
1437 	       "Rebuild kernel with CONFIG_MEMORY_FAILURE=y for smarter handling\n",
1438 	       pfn);
1439 
1440 	return 0;
1441 }
1442 #endif
1443 
1444 /*
1445  * Periodic polling timer for "silent" machine check errors.  If the
1446  * poller finds an MCE, poll 2x faster.  When the poller finds no more
1447  * errors, poll 2x slower (up to check_interval seconds).
1448  */
1449 static unsigned long check_interval = INITIAL_CHECK_INTERVAL;
1450 
1451 static DEFINE_PER_CPU(unsigned long, mce_next_interval); /* in jiffies */
1452 static DEFINE_PER_CPU(struct timer_list, mce_timer);
1453 
1454 static unsigned long mce_adjust_timer_default(unsigned long interval)
1455 {
1456 	return interval;
1457 }
1458 
1459 static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
1460 
1461 static void __start_timer(struct timer_list *t, unsigned long interval)
1462 {
1463 	unsigned long when = jiffies + interval;
1464 	unsigned long flags;
1465 
1466 	local_irq_save(flags);
1467 
1468 	if (!timer_pending(t) || time_before(when, t->expires))
1469 		mod_timer(t, round_jiffies(when));
1470 
1471 	local_irq_restore(flags);
1472 }
1473 
1474 static void mce_timer_fn(struct timer_list *t)
1475 {
1476 	struct timer_list *cpu_t = this_cpu_ptr(&mce_timer);
1477 	unsigned long iv;
1478 
1479 	WARN_ON(cpu_t != t);
1480 
1481 	iv = __this_cpu_read(mce_next_interval);
1482 
1483 	if (mce_available(this_cpu_ptr(&cpu_info))) {
1484 		machine_check_poll(0, this_cpu_ptr(&mce_poll_banks));
1485 
1486 		if (mce_intel_cmci_poll()) {
1487 			iv = mce_adjust_timer(iv);
1488 			goto done;
1489 		}
1490 	}
1491 
1492 	/*
1493 	 * Alert userspace if needed. If we logged an MCE, reduce the polling
1494 	 * interval, otherwise increase the polling interval.
1495 	 */
1496 	if (mce_notify_irq())
1497 		iv = max(iv / 2, (unsigned long) HZ/100);
1498 	else
1499 		iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
1500 
1501 done:
1502 	__this_cpu_write(mce_next_interval, iv);
1503 	__start_timer(t, iv);
1504 }
1505 
1506 /*
1507  * Ensure that the timer is firing in @interval from now.
1508  */
1509 void mce_timer_kick(unsigned long interval)
1510 {
1511 	struct timer_list *t = this_cpu_ptr(&mce_timer);
1512 	unsigned long iv = __this_cpu_read(mce_next_interval);
1513 
1514 	__start_timer(t, interval);
1515 
1516 	if (interval < iv)
1517 		__this_cpu_write(mce_next_interval, interval);
1518 }
1519 
1520 /* Must not be called in IRQ context where del_timer_sync() can deadlock */
1521 static void mce_timer_delete_all(void)
1522 {
1523 	int cpu;
1524 
1525 	for_each_online_cpu(cpu)
1526 		del_timer_sync(&per_cpu(mce_timer, cpu));
1527 }
1528 
1529 /*
1530  * Notify the user(s) about new machine check events.
1531  * Can be called from interrupt context, but not from machine check/NMI
1532  * context.
1533  */
1534 int mce_notify_irq(void)
1535 {
1536 	/* Not more than two messages every minute */
1537 	static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
1538 
1539 	if (test_and_clear_bit(0, &mce_need_notify)) {
1540 		mce_work_trigger();
1541 
1542 		if (__ratelimit(&ratelimit))
1543 			pr_info(HW_ERR "Machine check events logged\n");
1544 
1545 		return 1;
1546 	}
1547 	return 0;
1548 }
1549 EXPORT_SYMBOL_GPL(mce_notify_irq);
1550 
1551 static void __mcheck_cpu_mce_banks_init(void)
1552 {
1553 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1554 	u8 n_banks = this_cpu_read(mce_num_banks);
1555 	int i;
1556 
1557 	for (i = 0; i < n_banks; i++) {
1558 		struct mce_bank *b = &mce_banks[i];
1559 
1560 		/*
1561 		 * Init them all, __mcheck_cpu_apply_quirks() is going to apply
1562 		 * the required vendor quirks before
1563 		 * __mcheck_cpu_init_clear_banks() does the final bank setup.
1564 		 */
1565 		b->ctl = -1ULL;
1566 		b->init = 1;
1567 	}
1568 }
1569 
1570 /*
1571  * Initialize Machine Checks for a CPU.
1572  */
1573 static void __mcheck_cpu_cap_init(void)
1574 {
1575 	u64 cap;
1576 	u8 b;
1577 
1578 	rdmsrl(MSR_IA32_MCG_CAP, cap);
1579 
1580 	b = cap & MCG_BANKCNT_MASK;
1581 
1582 	if (b > MAX_NR_BANKS) {
1583 		pr_warn("CPU%d: Using only %u machine check banks out of %u\n",
1584 			smp_processor_id(), MAX_NR_BANKS, b);
1585 		b = MAX_NR_BANKS;
1586 	}
1587 
1588 	this_cpu_write(mce_num_banks, b);
1589 
1590 	__mcheck_cpu_mce_banks_init();
1591 
1592 	/* Use accurate RIP reporting if available. */
1593 	if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
1594 		mca_cfg.rip_msr = MSR_IA32_MCG_EIP;
1595 
1596 	if (cap & MCG_SER_P)
1597 		mca_cfg.ser = 1;
1598 }
1599 
1600 static void __mcheck_cpu_init_generic(void)
1601 {
1602 	enum mcp_flags m_fl = 0;
1603 	mce_banks_t all_banks;
1604 	u64 cap;
1605 
1606 	if (!mca_cfg.bootlog)
1607 		m_fl = MCP_DONTLOG;
1608 
1609 	/*
1610 	 * Log the machine checks left over from the previous reset.
1611 	 */
1612 	bitmap_fill(all_banks, MAX_NR_BANKS);
1613 	machine_check_poll(MCP_UC | m_fl, &all_banks);
1614 
1615 	cr4_set_bits(X86_CR4_MCE);
1616 
1617 	rdmsrl(MSR_IA32_MCG_CAP, cap);
1618 	if (cap & MCG_CTL_P)
1619 		wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
1620 }
1621 
1622 static void __mcheck_cpu_init_clear_banks(void)
1623 {
1624 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1625 	int i;
1626 
1627 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1628 		struct mce_bank *b = &mce_banks[i];
1629 
1630 		if (!b->init)
1631 			continue;
1632 		wrmsrl(msr_ops.ctl(i), b->ctl);
1633 		wrmsrl(msr_ops.status(i), 0);
1634 	}
1635 }
1636 
1637 /*
1638  * Do a final check to see if there are any unused/RAZ banks.
1639  *
1640  * This must be done after the banks have been initialized and any quirks have
1641  * been applied.
1642  *
1643  * Do not call this from any user-initiated flows, e.g. CPU hotplug or sysfs.
1644  * Otherwise, a user who disables a bank will not be able to re-enable it
1645  * without a system reboot.
1646  */
1647 static void __mcheck_cpu_check_banks(void)
1648 {
1649 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1650 	u64 msrval;
1651 	int i;
1652 
1653 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
1654 		struct mce_bank *b = &mce_banks[i];
1655 
1656 		if (!b->init)
1657 			continue;
1658 
1659 		rdmsrl(msr_ops.ctl(i), msrval);
1660 		b->init = !!msrval;
1661 	}
1662 }
1663 
1664 /*
1665  * During IFU recovery Sandy Bridge -EP4S processors set the RIPV and
1666  * EIPV bits in MCG_STATUS to zero on the affected logical processor (SDM
1667  * Vol 3B Table 15-20). But this confuses both the code that determines
1668  * whether the machine check occurred in kernel or user mode, and also
1669  * the severity assessment code. Pretend that EIPV was set, and take the
1670  * ip/cs values from the pt_regs that mce_gather_info() ignored earlier.
1671  */
1672 static void quirk_sandybridge_ifu(int bank, struct mce *m, struct pt_regs *regs)
1673 {
1674 	if (bank != 0)
1675 		return;
1676 	if ((m->mcgstatus & (MCG_STATUS_EIPV|MCG_STATUS_RIPV)) != 0)
1677 		return;
1678 	if ((m->status & (MCI_STATUS_OVER|MCI_STATUS_UC|
1679 		          MCI_STATUS_EN|MCI_STATUS_MISCV|MCI_STATUS_ADDRV|
1680 			  MCI_STATUS_PCC|MCI_STATUS_S|MCI_STATUS_AR|
1681 			  MCACOD)) !=
1682 			 (MCI_STATUS_UC|MCI_STATUS_EN|
1683 			  MCI_STATUS_MISCV|MCI_STATUS_ADDRV|MCI_STATUS_S|
1684 			  MCI_STATUS_AR|MCACOD_INSTR))
1685 		return;
1686 
1687 	m->mcgstatus |= MCG_STATUS_EIPV;
1688 	m->ip = regs->ip;
1689 	m->cs = regs->cs;
1690 }
1691 
1692 /* Add per CPU specific workarounds here */
1693 static int __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
1694 {
1695 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1696 	struct mca_config *cfg = &mca_cfg;
1697 
1698 	if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
1699 		pr_info("unknown CPU type - not enabling MCE support\n");
1700 		return -EOPNOTSUPP;
1701 	}
1702 
1703 	/* This should be disabled by the BIOS, but isn't always */
1704 	if (c->x86_vendor == X86_VENDOR_AMD) {
1705 		if (c->x86 == 15 && this_cpu_read(mce_num_banks) > 4) {
1706 			/*
1707 			 * disable GART TBL walk error reporting, which
1708 			 * trips off incorrectly with the IOMMU & 3ware
1709 			 * & Cerberus:
1710 			 */
1711 			clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
1712 		}
1713 		if (c->x86 < 0x11 && cfg->bootlog < 0) {
1714 			/*
1715 			 * Lots of broken BIOS around that don't clear them
1716 			 * by default and leave crap in there. Don't log:
1717 			 */
1718 			cfg->bootlog = 0;
1719 		}
1720 		/*
1721 		 * Various K7s with broken bank 0 around. Always disable
1722 		 * by default.
1723 		 */
1724 		if (c->x86 == 6 && this_cpu_read(mce_num_banks) > 0)
1725 			mce_banks[0].ctl = 0;
1726 
1727 		/*
1728 		 * overflow_recov is supported for F15h Models 00h-0fh
1729 		 * even though we don't have a CPUID bit for it.
1730 		 */
1731 		if (c->x86 == 0x15 && c->x86_model <= 0xf)
1732 			mce_flags.overflow_recov = 1;
1733 
1734 	}
1735 
1736 	if (c->x86_vendor == X86_VENDOR_INTEL) {
1737 		/*
1738 		 * SDM documents that on family 6 bank 0 should not be written
1739 		 * because it aliases to another special BIOS controlled
1740 		 * register.
1741 		 * But it's not aliased anymore on model 0x1a+
1742 		 * Don't ignore bank 0 completely because there could be a
1743 		 * valid event later, merely don't write CTL0.
1744 		 */
1745 
1746 		if (c->x86 == 6 && c->x86_model < 0x1A && this_cpu_read(mce_num_banks) > 0)
1747 			mce_banks[0].init = 0;
1748 
1749 		/*
1750 		 * All newer Intel systems support MCE broadcasting. Enable
1751 		 * synchronization with a one second timeout.
1752 		 */
1753 		if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
1754 			cfg->monarch_timeout < 0)
1755 			cfg->monarch_timeout = USEC_PER_SEC;
1756 
1757 		/*
1758 		 * There are also broken BIOSes on some Pentium M and
1759 		 * earlier systems:
1760 		 */
1761 		if (c->x86 == 6 && c->x86_model <= 13 && cfg->bootlog < 0)
1762 			cfg->bootlog = 0;
1763 
1764 		if (c->x86 == 6 && c->x86_model == 45)
1765 			quirk_no_way_out = quirk_sandybridge_ifu;
1766 	}
1767 
1768 	if (c->x86_vendor == X86_VENDOR_ZHAOXIN) {
1769 		/*
1770 		 * All newer Zhaoxin CPUs support MCE broadcasting. Enable
1771 		 * synchronization with a one second timeout.
1772 		 */
1773 		if (c->x86 > 6 || (c->x86_model == 0x19 || c->x86_model == 0x1f)) {
1774 			if (cfg->monarch_timeout < 0)
1775 				cfg->monarch_timeout = USEC_PER_SEC;
1776 		}
1777 	}
1778 
1779 	if (cfg->monarch_timeout < 0)
1780 		cfg->monarch_timeout = 0;
1781 	if (cfg->bootlog != 0)
1782 		cfg->panic_timeout = 30;
1783 
1784 	return 0;
1785 }
1786 
1787 static int __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
1788 {
1789 	if (c->x86 != 5)
1790 		return 0;
1791 
1792 	switch (c->x86_vendor) {
1793 	case X86_VENDOR_INTEL:
1794 		intel_p5_mcheck_init(c);
1795 		return 1;
1796 		break;
1797 	case X86_VENDOR_CENTAUR:
1798 		winchip_mcheck_init(c);
1799 		return 1;
1800 		break;
1801 	default:
1802 		return 0;
1803 	}
1804 
1805 	return 0;
1806 }
1807 
1808 /*
1809  * Init basic CPU features needed for early decoding of MCEs.
1810  */
1811 static void __mcheck_cpu_init_early(struct cpuinfo_x86 *c)
1812 {
1813 	if (c->x86_vendor == X86_VENDOR_AMD || c->x86_vendor == X86_VENDOR_HYGON) {
1814 		mce_flags.overflow_recov = !!cpu_has(c, X86_FEATURE_OVERFLOW_RECOV);
1815 		mce_flags.succor	 = !!cpu_has(c, X86_FEATURE_SUCCOR);
1816 		mce_flags.smca		 = !!cpu_has(c, X86_FEATURE_SMCA);
1817 		mce_flags.amd_threshold	 = 1;
1818 
1819 		if (mce_flags.smca) {
1820 			msr_ops.ctl	= smca_ctl_reg;
1821 			msr_ops.status	= smca_status_reg;
1822 			msr_ops.addr	= smca_addr_reg;
1823 			msr_ops.misc	= smca_misc_reg;
1824 		}
1825 	}
1826 }
1827 
1828 static void mce_centaur_feature_init(struct cpuinfo_x86 *c)
1829 {
1830 	struct mca_config *cfg = &mca_cfg;
1831 
1832 	 /*
1833 	  * All newer Centaur CPUs support MCE broadcasting. Enable
1834 	  * synchronization with a one second timeout.
1835 	  */
1836 	if ((c->x86 == 6 && c->x86_model == 0xf && c->x86_stepping >= 0xe) ||
1837 	     c->x86 > 6) {
1838 		if (cfg->monarch_timeout < 0)
1839 			cfg->monarch_timeout = USEC_PER_SEC;
1840 	}
1841 }
1842 
1843 static void mce_zhaoxin_feature_init(struct cpuinfo_x86 *c)
1844 {
1845 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
1846 
1847 	/*
1848 	 * These CPUs have MCA bank 8 which reports only one error type called
1849 	 * SVAD (System View Address Decoder). The reporting of that error is
1850 	 * controlled by IA32_MC8.CTL.0.
1851 	 *
1852 	 * If enabled, prefetching on these CPUs will cause SVAD MCE when
1853 	 * virtual machines start and result in a system  panic. Always disable
1854 	 * bank 8 SVAD error by default.
1855 	 */
1856 	if ((c->x86 == 7 && c->x86_model == 0x1b) ||
1857 	    (c->x86_model == 0x19 || c->x86_model == 0x1f)) {
1858 		if (this_cpu_read(mce_num_banks) > 8)
1859 			mce_banks[8].ctl = 0;
1860 	}
1861 
1862 	intel_init_cmci();
1863 	intel_init_lmce();
1864 	mce_adjust_timer = cmci_intel_adjust_timer;
1865 }
1866 
1867 static void mce_zhaoxin_feature_clear(struct cpuinfo_x86 *c)
1868 {
1869 	intel_clear_lmce();
1870 }
1871 
1872 static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
1873 {
1874 	switch (c->x86_vendor) {
1875 	case X86_VENDOR_INTEL:
1876 		mce_intel_feature_init(c);
1877 		mce_adjust_timer = cmci_intel_adjust_timer;
1878 		break;
1879 
1880 	case X86_VENDOR_AMD: {
1881 		mce_amd_feature_init(c);
1882 		break;
1883 		}
1884 
1885 	case X86_VENDOR_HYGON:
1886 		mce_hygon_feature_init(c);
1887 		break;
1888 
1889 	case X86_VENDOR_CENTAUR:
1890 		mce_centaur_feature_init(c);
1891 		break;
1892 
1893 	case X86_VENDOR_ZHAOXIN:
1894 		mce_zhaoxin_feature_init(c);
1895 		break;
1896 
1897 	default:
1898 		break;
1899 	}
1900 }
1901 
1902 static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
1903 {
1904 	switch (c->x86_vendor) {
1905 	case X86_VENDOR_INTEL:
1906 		mce_intel_feature_clear(c);
1907 		break;
1908 
1909 	case X86_VENDOR_ZHAOXIN:
1910 		mce_zhaoxin_feature_clear(c);
1911 		break;
1912 
1913 	default:
1914 		break;
1915 	}
1916 }
1917 
1918 static void mce_start_timer(struct timer_list *t)
1919 {
1920 	unsigned long iv = check_interval * HZ;
1921 
1922 	if (mca_cfg.ignore_ce || !iv)
1923 		return;
1924 
1925 	this_cpu_write(mce_next_interval, iv);
1926 	__start_timer(t, iv);
1927 }
1928 
1929 static void __mcheck_cpu_setup_timer(void)
1930 {
1931 	struct timer_list *t = this_cpu_ptr(&mce_timer);
1932 
1933 	timer_setup(t, mce_timer_fn, TIMER_PINNED);
1934 }
1935 
1936 static void __mcheck_cpu_init_timer(void)
1937 {
1938 	struct timer_list *t = this_cpu_ptr(&mce_timer);
1939 
1940 	timer_setup(t, mce_timer_fn, TIMER_PINNED);
1941 	mce_start_timer(t);
1942 }
1943 
1944 bool filter_mce(struct mce *m)
1945 {
1946 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1947 		return amd_filter_mce(m);
1948 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1949 		return intel_filter_mce(m);
1950 
1951 	return false;
1952 }
1953 
1954 /* Handle unconfigured int18 (should never happen) */
1955 static noinstr void unexpected_machine_check(struct pt_regs *regs)
1956 {
1957 	instrumentation_begin();
1958 	pr_err("CPU#%d: Unexpected int18 (Machine Check)\n",
1959 	       smp_processor_id());
1960 	instrumentation_end();
1961 }
1962 
1963 /* Call the installed machine check handler for this CPU setup. */
1964 void (*machine_check_vector)(struct pt_regs *) = unexpected_machine_check;
1965 
1966 static __always_inline void exc_machine_check_kernel(struct pt_regs *regs)
1967 {
1968 	WARN_ON_ONCE(user_mode(regs));
1969 
1970 	/*
1971 	 * Only required when from kernel mode. See
1972 	 * mce_check_crashing_cpu() for details.
1973 	 */
1974 	if (machine_check_vector == do_machine_check &&
1975 	    mce_check_crashing_cpu())
1976 		return;
1977 
1978 	nmi_enter();
1979 	/*
1980 	 * The call targets are marked noinstr, but objtool can't figure
1981 	 * that out because it's an indirect call. Annotate it.
1982 	 */
1983 	instrumentation_begin();
1984 	trace_hardirqs_off_finish();
1985 	machine_check_vector(regs);
1986 	if (regs->flags & X86_EFLAGS_IF)
1987 		trace_hardirqs_on_prepare();
1988 	instrumentation_end();
1989 	nmi_exit();
1990 }
1991 
1992 static __always_inline void exc_machine_check_user(struct pt_regs *regs)
1993 {
1994 	irqentry_enter_from_user_mode(regs);
1995 	instrumentation_begin();
1996 	machine_check_vector(regs);
1997 	instrumentation_end();
1998 	irqentry_exit_to_user_mode(regs);
1999 }
2000 
2001 #ifdef CONFIG_X86_64
2002 /* MCE hit kernel mode */
2003 DEFINE_IDTENTRY_MCE(exc_machine_check)
2004 {
2005 	unsigned long dr7;
2006 
2007 	dr7 = local_db_save();
2008 	exc_machine_check_kernel(regs);
2009 	local_db_restore(dr7);
2010 }
2011 
2012 /* The user mode variant. */
2013 DEFINE_IDTENTRY_MCE_USER(exc_machine_check)
2014 {
2015 	unsigned long dr7;
2016 
2017 	dr7 = local_db_save();
2018 	exc_machine_check_user(regs);
2019 	local_db_restore(dr7);
2020 }
2021 #else
2022 /* 32bit unified entry point */
2023 DEFINE_IDTENTRY_RAW(exc_machine_check)
2024 {
2025 	unsigned long dr7;
2026 
2027 	dr7 = local_db_save();
2028 	if (user_mode(regs))
2029 		exc_machine_check_user(regs);
2030 	else
2031 		exc_machine_check_kernel(regs);
2032 	local_db_restore(dr7);
2033 }
2034 #endif
2035 
2036 /*
2037  * Called for each booted CPU to set up machine checks.
2038  * Must be called with preempt off:
2039  */
2040 void mcheck_cpu_init(struct cpuinfo_x86 *c)
2041 {
2042 	if (mca_cfg.disabled)
2043 		return;
2044 
2045 	if (__mcheck_cpu_ancient_init(c))
2046 		return;
2047 
2048 	if (!mce_available(c))
2049 		return;
2050 
2051 	__mcheck_cpu_cap_init();
2052 
2053 	if (__mcheck_cpu_apply_quirks(c) < 0) {
2054 		mca_cfg.disabled = 1;
2055 		return;
2056 	}
2057 
2058 	if (mce_gen_pool_init()) {
2059 		mca_cfg.disabled = 1;
2060 		pr_emerg("Couldn't allocate MCE records pool!\n");
2061 		return;
2062 	}
2063 
2064 	machine_check_vector = do_machine_check;
2065 
2066 	__mcheck_cpu_init_early(c);
2067 	__mcheck_cpu_init_generic();
2068 	__mcheck_cpu_init_vendor(c);
2069 	__mcheck_cpu_init_clear_banks();
2070 	__mcheck_cpu_check_banks();
2071 	__mcheck_cpu_setup_timer();
2072 }
2073 
2074 /*
2075  * Called for each booted CPU to clear some machine checks opt-ins
2076  */
2077 void mcheck_cpu_clear(struct cpuinfo_x86 *c)
2078 {
2079 	if (mca_cfg.disabled)
2080 		return;
2081 
2082 	if (!mce_available(c))
2083 		return;
2084 
2085 	/*
2086 	 * Possibly to clear general settings generic to x86
2087 	 * __mcheck_cpu_clear_generic(c);
2088 	 */
2089 	__mcheck_cpu_clear_vendor(c);
2090 
2091 }
2092 
2093 static void __mce_disable_bank(void *arg)
2094 {
2095 	int bank = *((int *)arg);
2096 	__clear_bit(bank, this_cpu_ptr(mce_poll_banks));
2097 	cmci_disable_bank(bank);
2098 }
2099 
2100 void mce_disable_bank(int bank)
2101 {
2102 	if (bank >= this_cpu_read(mce_num_banks)) {
2103 		pr_warn(FW_BUG
2104 			"Ignoring request to disable invalid MCA bank %d.\n",
2105 			bank);
2106 		return;
2107 	}
2108 	set_bit(bank, mce_banks_ce_disabled);
2109 	on_each_cpu(__mce_disable_bank, &bank, 1);
2110 }
2111 
2112 /*
2113  * mce=off Disables machine check
2114  * mce=no_cmci Disables CMCI
2115  * mce=no_lmce Disables LMCE
2116  * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
2117  * mce=print_all Print all machine check logs to console
2118  * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
2119  * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
2120  *	monarchtimeout is how long to wait for other CPUs on machine
2121  *	check, or 0 to not wait
2122  * mce=bootlog Log MCEs from before booting. Disabled by default on AMD Fam10h
2123 	and older.
2124  * mce=nobootlog Don't log MCEs from before booting.
2125  * mce=bios_cmci_threshold Don't program the CMCI threshold
2126  * mce=recovery force enable copy_mc_fragile()
2127  */
2128 static int __init mcheck_enable(char *str)
2129 {
2130 	struct mca_config *cfg = &mca_cfg;
2131 
2132 	if (*str == 0) {
2133 		enable_p5_mce();
2134 		return 1;
2135 	}
2136 	if (*str == '=')
2137 		str++;
2138 	if (!strcmp(str, "off"))
2139 		cfg->disabled = 1;
2140 	else if (!strcmp(str, "no_cmci"))
2141 		cfg->cmci_disabled = true;
2142 	else if (!strcmp(str, "no_lmce"))
2143 		cfg->lmce_disabled = 1;
2144 	else if (!strcmp(str, "dont_log_ce"))
2145 		cfg->dont_log_ce = true;
2146 	else if (!strcmp(str, "print_all"))
2147 		cfg->print_all = true;
2148 	else if (!strcmp(str, "ignore_ce"))
2149 		cfg->ignore_ce = true;
2150 	else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
2151 		cfg->bootlog = (str[0] == 'b');
2152 	else if (!strcmp(str, "bios_cmci_threshold"))
2153 		cfg->bios_cmci_threshold = 1;
2154 	else if (!strcmp(str, "recovery"))
2155 		cfg->recovery = 1;
2156 	else if (isdigit(str[0])) {
2157 		if (get_option(&str, &cfg->tolerant) == 2)
2158 			get_option(&str, &(cfg->monarch_timeout));
2159 	} else {
2160 		pr_info("mce argument %s ignored. Please use /sys\n", str);
2161 		return 0;
2162 	}
2163 	return 1;
2164 }
2165 __setup("mce", mcheck_enable);
2166 
2167 int __init mcheck_init(void)
2168 {
2169 	mcheck_intel_therm_init();
2170 	mce_register_decode_chain(&early_nb);
2171 	mce_register_decode_chain(&mce_uc_nb);
2172 	mce_register_decode_chain(&mce_default_nb);
2173 	mcheck_vendor_init_severity();
2174 
2175 	INIT_WORK(&mce_work, mce_gen_pool_process);
2176 	init_irq_work(&mce_irq_work, mce_irq_work_cb);
2177 
2178 	return 0;
2179 }
2180 
2181 /*
2182  * mce_syscore: PM support
2183  */
2184 
2185 /*
2186  * Disable machine checks on suspend and shutdown. We can't really handle
2187  * them later.
2188  */
2189 static void mce_disable_error_reporting(void)
2190 {
2191 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
2192 	int i;
2193 
2194 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
2195 		struct mce_bank *b = &mce_banks[i];
2196 
2197 		if (b->init)
2198 			wrmsrl(msr_ops.ctl(i), 0);
2199 	}
2200 	return;
2201 }
2202 
2203 static void vendor_disable_error_reporting(void)
2204 {
2205 	/*
2206 	 * Don't clear on Intel or AMD or Hygon or Zhaoxin CPUs. Some of these
2207 	 * MSRs are socket-wide. Disabling them for just a single offlined CPU
2208 	 * is bad, since it will inhibit reporting for all shared resources on
2209 	 * the socket like the last level cache (LLC), the integrated memory
2210 	 * controller (iMC), etc.
2211 	 */
2212 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL ||
2213 	    boot_cpu_data.x86_vendor == X86_VENDOR_HYGON ||
2214 	    boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
2215 	    boot_cpu_data.x86_vendor == X86_VENDOR_ZHAOXIN)
2216 		return;
2217 
2218 	mce_disable_error_reporting();
2219 }
2220 
2221 static int mce_syscore_suspend(void)
2222 {
2223 	vendor_disable_error_reporting();
2224 	return 0;
2225 }
2226 
2227 static void mce_syscore_shutdown(void)
2228 {
2229 	vendor_disable_error_reporting();
2230 }
2231 
2232 /*
2233  * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
2234  * Only one CPU is active at this time, the others get re-added later using
2235  * CPU hotplug:
2236  */
2237 static void mce_syscore_resume(void)
2238 {
2239 	__mcheck_cpu_init_generic();
2240 	__mcheck_cpu_init_vendor(raw_cpu_ptr(&cpu_info));
2241 	__mcheck_cpu_init_clear_banks();
2242 }
2243 
2244 static struct syscore_ops mce_syscore_ops = {
2245 	.suspend	= mce_syscore_suspend,
2246 	.shutdown	= mce_syscore_shutdown,
2247 	.resume		= mce_syscore_resume,
2248 };
2249 
2250 /*
2251  * mce_device: Sysfs support
2252  */
2253 
2254 static void mce_cpu_restart(void *data)
2255 {
2256 	if (!mce_available(raw_cpu_ptr(&cpu_info)))
2257 		return;
2258 	__mcheck_cpu_init_generic();
2259 	__mcheck_cpu_init_clear_banks();
2260 	__mcheck_cpu_init_timer();
2261 }
2262 
2263 /* Reinit MCEs after user configuration changes */
2264 static void mce_restart(void)
2265 {
2266 	mce_timer_delete_all();
2267 	on_each_cpu(mce_cpu_restart, NULL, 1);
2268 }
2269 
2270 /* Toggle features for corrected errors */
2271 static void mce_disable_cmci(void *data)
2272 {
2273 	if (!mce_available(raw_cpu_ptr(&cpu_info)))
2274 		return;
2275 	cmci_clear();
2276 }
2277 
2278 static void mce_enable_ce(void *all)
2279 {
2280 	if (!mce_available(raw_cpu_ptr(&cpu_info)))
2281 		return;
2282 	cmci_reenable();
2283 	cmci_recheck();
2284 	if (all)
2285 		__mcheck_cpu_init_timer();
2286 }
2287 
2288 static struct bus_type mce_subsys = {
2289 	.name		= "machinecheck",
2290 	.dev_name	= "machinecheck",
2291 };
2292 
2293 DEFINE_PER_CPU(struct device *, mce_device);
2294 
2295 static inline struct mce_bank_dev *attr_to_bank(struct device_attribute *attr)
2296 {
2297 	return container_of(attr, struct mce_bank_dev, attr);
2298 }
2299 
2300 static ssize_t show_bank(struct device *s, struct device_attribute *attr,
2301 			 char *buf)
2302 {
2303 	u8 bank = attr_to_bank(attr)->bank;
2304 	struct mce_bank *b;
2305 
2306 	if (bank >= per_cpu(mce_num_banks, s->id))
2307 		return -EINVAL;
2308 
2309 	b = &per_cpu(mce_banks_array, s->id)[bank];
2310 
2311 	if (!b->init)
2312 		return -ENODEV;
2313 
2314 	return sprintf(buf, "%llx\n", b->ctl);
2315 }
2316 
2317 static ssize_t set_bank(struct device *s, struct device_attribute *attr,
2318 			const char *buf, size_t size)
2319 {
2320 	u8 bank = attr_to_bank(attr)->bank;
2321 	struct mce_bank *b;
2322 	u64 new;
2323 
2324 	if (kstrtou64(buf, 0, &new) < 0)
2325 		return -EINVAL;
2326 
2327 	if (bank >= per_cpu(mce_num_banks, s->id))
2328 		return -EINVAL;
2329 
2330 	b = &per_cpu(mce_banks_array, s->id)[bank];
2331 
2332 	if (!b->init)
2333 		return -ENODEV;
2334 
2335 	b->ctl = new;
2336 	mce_restart();
2337 
2338 	return size;
2339 }
2340 
2341 static ssize_t set_ignore_ce(struct device *s,
2342 			     struct device_attribute *attr,
2343 			     const char *buf, size_t size)
2344 {
2345 	u64 new;
2346 
2347 	if (kstrtou64(buf, 0, &new) < 0)
2348 		return -EINVAL;
2349 
2350 	mutex_lock(&mce_sysfs_mutex);
2351 	if (mca_cfg.ignore_ce ^ !!new) {
2352 		if (new) {
2353 			/* disable ce features */
2354 			mce_timer_delete_all();
2355 			on_each_cpu(mce_disable_cmci, NULL, 1);
2356 			mca_cfg.ignore_ce = true;
2357 		} else {
2358 			/* enable ce features */
2359 			mca_cfg.ignore_ce = false;
2360 			on_each_cpu(mce_enable_ce, (void *)1, 1);
2361 		}
2362 	}
2363 	mutex_unlock(&mce_sysfs_mutex);
2364 
2365 	return size;
2366 }
2367 
2368 static ssize_t set_cmci_disabled(struct device *s,
2369 				 struct device_attribute *attr,
2370 				 const char *buf, size_t size)
2371 {
2372 	u64 new;
2373 
2374 	if (kstrtou64(buf, 0, &new) < 0)
2375 		return -EINVAL;
2376 
2377 	mutex_lock(&mce_sysfs_mutex);
2378 	if (mca_cfg.cmci_disabled ^ !!new) {
2379 		if (new) {
2380 			/* disable cmci */
2381 			on_each_cpu(mce_disable_cmci, NULL, 1);
2382 			mca_cfg.cmci_disabled = true;
2383 		} else {
2384 			/* enable cmci */
2385 			mca_cfg.cmci_disabled = false;
2386 			on_each_cpu(mce_enable_ce, NULL, 1);
2387 		}
2388 	}
2389 	mutex_unlock(&mce_sysfs_mutex);
2390 
2391 	return size;
2392 }
2393 
2394 static ssize_t store_int_with_restart(struct device *s,
2395 				      struct device_attribute *attr,
2396 				      const char *buf, size_t size)
2397 {
2398 	unsigned long old_check_interval = check_interval;
2399 	ssize_t ret = device_store_ulong(s, attr, buf, size);
2400 
2401 	if (check_interval == old_check_interval)
2402 		return ret;
2403 
2404 	mutex_lock(&mce_sysfs_mutex);
2405 	mce_restart();
2406 	mutex_unlock(&mce_sysfs_mutex);
2407 
2408 	return ret;
2409 }
2410 
2411 static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
2412 static DEVICE_INT_ATTR(monarch_timeout, 0644, mca_cfg.monarch_timeout);
2413 static DEVICE_BOOL_ATTR(dont_log_ce, 0644, mca_cfg.dont_log_ce);
2414 static DEVICE_BOOL_ATTR(print_all, 0644, mca_cfg.print_all);
2415 
2416 static struct dev_ext_attribute dev_attr_check_interval = {
2417 	__ATTR(check_interval, 0644, device_show_int, store_int_with_restart),
2418 	&check_interval
2419 };
2420 
2421 static struct dev_ext_attribute dev_attr_ignore_ce = {
2422 	__ATTR(ignore_ce, 0644, device_show_bool, set_ignore_ce),
2423 	&mca_cfg.ignore_ce
2424 };
2425 
2426 static struct dev_ext_attribute dev_attr_cmci_disabled = {
2427 	__ATTR(cmci_disabled, 0644, device_show_bool, set_cmci_disabled),
2428 	&mca_cfg.cmci_disabled
2429 };
2430 
2431 static struct device_attribute *mce_device_attrs[] = {
2432 	&dev_attr_tolerant.attr,
2433 	&dev_attr_check_interval.attr,
2434 #ifdef CONFIG_X86_MCELOG_LEGACY
2435 	&dev_attr_trigger,
2436 #endif
2437 	&dev_attr_monarch_timeout.attr,
2438 	&dev_attr_dont_log_ce.attr,
2439 	&dev_attr_print_all.attr,
2440 	&dev_attr_ignore_ce.attr,
2441 	&dev_attr_cmci_disabled.attr,
2442 	NULL
2443 };
2444 
2445 static cpumask_var_t mce_device_initialized;
2446 
2447 static void mce_device_release(struct device *dev)
2448 {
2449 	kfree(dev);
2450 }
2451 
2452 /* Per CPU device init. All of the CPUs still share the same bank device: */
2453 static int mce_device_create(unsigned int cpu)
2454 {
2455 	struct device *dev;
2456 	int err;
2457 	int i, j;
2458 
2459 	if (!mce_available(&boot_cpu_data))
2460 		return -EIO;
2461 
2462 	dev = per_cpu(mce_device, cpu);
2463 	if (dev)
2464 		return 0;
2465 
2466 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2467 	if (!dev)
2468 		return -ENOMEM;
2469 	dev->id  = cpu;
2470 	dev->bus = &mce_subsys;
2471 	dev->release = &mce_device_release;
2472 
2473 	err = device_register(dev);
2474 	if (err) {
2475 		put_device(dev);
2476 		return err;
2477 	}
2478 
2479 	for (i = 0; mce_device_attrs[i]; i++) {
2480 		err = device_create_file(dev, mce_device_attrs[i]);
2481 		if (err)
2482 			goto error;
2483 	}
2484 	for (j = 0; j < per_cpu(mce_num_banks, cpu); j++) {
2485 		err = device_create_file(dev, &mce_bank_devs[j].attr);
2486 		if (err)
2487 			goto error2;
2488 	}
2489 	cpumask_set_cpu(cpu, mce_device_initialized);
2490 	per_cpu(mce_device, cpu) = dev;
2491 
2492 	return 0;
2493 error2:
2494 	while (--j >= 0)
2495 		device_remove_file(dev, &mce_bank_devs[j].attr);
2496 error:
2497 	while (--i >= 0)
2498 		device_remove_file(dev, mce_device_attrs[i]);
2499 
2500 	device_unregister(dev);
2501 
2502 	return err;
2503 }
2504 
2505 static void mce_device_remove(unsigned int cpu)
2506 {
2507 	struct device *dev = per_cpu(mce_device, cpu);
2508 	int i;
2509 
2510 	if (!cpumask_test_cpu(cpu, mce_device_initialized))
2511 		return;
2512 
2513 	for (i = 0; mce_device_attrs[i]; i++)
2514 		device_remove_file(dev, mce_device_attrs[i]);
2515 
2516 	for (i = 0; i < per_cpu(mce_num_banks, cpu); i++)
2517 		device_remove_file(dev, &mce_bank_devs[i].attr);
2518 
2519 	device_unregister(dev);
2520 	cpumask_clear_cpu(cpu, mce_device_initialized);
2521 	per_cpu(mce_device, cpu) = NULL;
2522 }
2523 
2524 /* Make sure there are no machine checks on offlined CPUs. */
2525 static void mce_disable_cpu(void)
2526 {
2527 	if (!mce_available(raw_cpu_ptr(&cpu_info)))
2528 		return;
2529 
2530 	if (!cpuhp_tasks_frozen)
2531 		cmci_clear();
2532 
2533 	vendor_disable_error_reporting();
2534 }
2535 
2536 static void mce_reenable_cpu(void)
2537 {
2538 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
2539 	int i;
2540 
2541 	if (!mce_available(raw_cpu_ptr(&cpu_info)))
2542 		return;
2543 
2544 	if (!cpuhp_tasks_frozen)
2545 		cmci_reenable();
2546 	for (i = 0; i < this_cpu_read(mce_num_banks); i++) {
2547 		struct mce_bank *b = &mce_banks[i];
2548 
2549 		if (b->init)
2550 			wrmsrl(msr_ops.ctl(i), b->ctl);
2551 	}
2552 }
2553 
2554 static int mce_cpu_dead(unsigned int cpu)
2555 {
2556 	mce_intel_hcpu_update(cpu);
2557 
2558 	/* intentionally ignoring frozen here */
2559 	if (!cpuhp_tasks_frozen)
2560 		cmci_rediscover();
2561 	return 0;
2562 }
2563 
2564 static int mce_cpu_online(unsigned int cpu)
2565 {
2566 	struct timer_list *t = this_cpu_ptr(&mce_timer);
2567 	int ret;
2568 
2569 	mce_device_create(cpu);
2570 
2571 	ret = mce_threshold_create_device(cpu);
2572 	if (ret) {
2573 		mce_device_remove(cpu);
2574 		return ret;
2575 	}
2576 	mce_reenable_cpu();
2577 	mce_start_timer(t);
2578 	return 0;
2579 }
2580 
2581 static int mce_cpu_pre_down(unsigned int cpu)
2582 {
2583 	struct timer_list *t = this_cpu_ptr(&mce_timer);
2584 
2585 	mce_disable_cpu();
2586 	del_timer_sync(t);
2587 	mce_threshold_remove_device(cpu);
2588 	mce_device_remove(cpu);
2589 	return 0;
2590 }
2591 
2592 static __init void mce_init_banks(void)
2593 {
2594 	int i;
2595 
2596 	for (i = 0; i < MAX_NR_BANKS; i++) {
2597 		struct mce_bank_dev *b = &mce_bank_devs[i];
2598 		struct device_attribute *a = &b->attr;
2599 
2600 		b->bank = i;
2601 
2602 		sysfs_attr_init(&a->attr);
2603 		a->attr.name	= b->attrname;
2604 		snprintf(b->attrname, ATTR_LEN, "bank%d", i);
2605 
2606 		a->attr.mode	= 0644;
2607 		a->show		= show_bank;
2608 		a->store	= set_bank;
2609 	}
2610 }
2611 
2612 /*
2613  * When running on XEN, this initcall is ordered against the XEN mcelog
2614  * initcall:
2615  *
2616  *   device_initcall(xen_late_init_mcelog);
2617  *   device_initcall_sync(mcheck_init_device);
2618  */
2619 static __init int mcheck_init_device(void)
2620 {
2621 	int err;
2622 
2623 	/*
2624 	 * Check if we have a spare virtual bit. This will only become
2625 	 * a problem if/when we move beyond 5-level page tables.
2626 	 */
2627 	MAYBE_BUILD_BUG_ON(__VIRTUAL_MASK_SHIFT >= 63);
2628 
2629 	if (!mce_available(&boot_cpu_data)) {
2630 		err = -EIO;
2631 		goto err_out;
2632 	}
2633 
2634 	if (!zalloc_cpumask_var(&mce_device_initialized, GFP_KERNEL)) {
2635 		err = -ENOMEM;
2636 		goto err_out;
2637 	}
2638 
2639 	mce_init_banks();
2640 
2641 	err = subsys_system_register(&mce_subsys, NULL);
2642 	if (err)
2643 		goto err_out_mem;
2644 
2645 	err = cpuhp_setup_state(CPUHP_X86_MCE_DEAD, "x86/mce:dead", NULL,
2646 				mce_cpu_dead);
2647 	if (err)
2648 		goto err_out_mem;
2649 
2650 	/*
2651 	 * Invokes mce_cpu_online() on all CPUs which are online when
2652 	 * the state is installed.
2653 	 */
2654 	err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/mce:online",
2655 				mce_cpu_online, mce_cpu_pre_down);
2656 	if (err < 0)
2657 		goto err_out_online;
2658 
2659 	register_syscore_ops(&mce_syscore_ops);
2660 
2661 	return 0;
2662 
2663 err_out_online:
2664 	cpuhp_remove_state(CPUHP_X86_MCE_DEAD);
2665 
2666 err_out_mem:
2667 	free_cpumask_var(mce_device_initialized);
2668 
2669 err_out:
2670 	pr_err("Unable to init MCE device (rc: %d)\n", err);
2671 
2672 	return err;
2673 }
2674 device_initcall_sync(mcheck_init_device);
2675 
2676 /*
2677  * Old style boot options parsing. Only for compatibility.
2678  */
2679 static int __init mcheck_disable(char *str)
2680 {
2681 	mca_cfg.disabled = 1;
2682 	return 1;
2683 }
2684 __setup("nomce", mcheck_disable);
2685 
2686 #ifdef CONFIG_DEBUG_FS
2687 struct dentry *mce_get_debugfs_dir(void)
2688 {
2689 	static struct dentry *dmce;
2690 
2691 	if (!dmce)
2692 		dmce = debugfs_create_dir("mce", NULL);
2693 
2694 	return dmce;
2695 }
2696 
2697 static void mce_reset(void)
2698 {
2699 	cpu_missing = 0;
2700 	atomic_set(&mce_fake_panicked, 0);
2701 	atomic_set(&mce_executing, 0);
2702 	atomic_set(&mce_callin, 0);
2703 	atomic_set(&global_nwo, 0);
2704 }
2705 
2706 static int fake_panic_get(void *data, u64 *val)
2707 {
2708 	*val = fake_panic;
2709 	return 0;
2710 }
2711 
2712 static int fake_panic_set(void *data, u64 val)
2713 {
2714 	mce_reset();
2715 	fake_panic = val;
2716 	return 0;
2717 }
2718 
2719 DEFINE_DEBUGFS_ATTRIBUTE(fake_panic_fops, fake_panic_get, fake_panic_set,
2720 			 "%llu\n");
2721 
2722 static void __init mcheck_debugfs_init(void)
2723 {
2724 	struct dentry *dmce;
2725 
2726 	dmce = mce_get_debugfs_dir();
2727 	debugfs_create_file_unsafe("fake_panic", 0444, dmce, NULL,
2728 				   &fake_panic_fops);
2729 }
2730 #else
2731 static void __init mcheck_debugfs_init(void) { }
2732 #endif
2733 
2734 static int __init mcheck_late_init(void)
2735 {
2736 	if (mca_cfg.recovery)
2737 		enable_copy_mc_fragile();
2738 
2739 	mcheck_debugfs_init();
2740 
2741 	/*
2742 	 * Flush out everything that has been logged during early boot, now that
2743 	 * everything has been initialized (workqueues, decoders, ...).
2744 	 */
2745 	mce_schedule_work();
2746 
2747 	return 0;
2748 }
2749 late_initcall(mcheck_late_init);
2750