PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/x86/kernel/cpu/mcheck/mce.c

https://bitbucket.org/thekraven/iscream_thunderc-2.6.35
C | 2221 lines | 1410 code | 338 blank | 473 comment | 305 complexity | 519145303c4d5010c92a831f512e1730 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Machine check handler.
  3. *
  4. * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
  5. * Rest from unknown author(s).
  6. * 2004 Andi Kleen. Rewrote most of it.
  7. * Copyright 2008 Intel Corporation
  8. * Author: Andi Kleen
  9. */
  10. #include <linux/thread_info.h>
  11. #include <linux/capability.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/kallsyms.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/sysdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/ctype.h>
  26. #include <linux/sched.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/init.h>
  31. #include <linux/kmod.h>
  32. #include <linux/poll.h>
  33. #include <linux/nmi.h>
  34. #include <linux/cpu.h>
  35. #include <linux/smp.h>
  36. #include <linux/fs.h>
  37. #include <linux/mm.h>
  38. #include <linux/debugfs.h>
  39. #include <linux/edac_mce.h>
  40. #include <asm/processor.h>
  41. #include <asm/hw_irq.h>
  42. #include <asm/apic.h>
  43. #include <asm/idle.h>
  44. #include <asm/ipi.h>
  45. #include <asm/mce.h>
  46. #include <asm/msr.h>
  47. #include "mce-internal.h"
  48. static DEFINE_MUTEX(mce_read_mutex);
  49. #define rcu_dereference_check_mce(p) \
  50. rcu_dereference_check((p), \
  51. rcu_read_lock_sched_held() || \
  52. lockdep_is_held(&mce_read_mutex))
  53. #define CREATE_TRACE_POINTS
  54. #include <trace/events/mce.h>
  55. int mce_disabled __read_mostly;
  56. #define MISC_MCELOG_MINOR 227
  57. #define SPINUNIT 100 /* 100ns */
  58. atomic_t mce_entry;
  59. DEFINE_PER_CPU(unsigned, mce_exception_count);
  60. /*
  61. * Tolerant levels:
  62. * 0: always panic on uncorrected errors, log corrected errors
  63. * 1: panic or SIGBUS on uncorrected errors, log corrected errors
  64. * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
  65. * 3: never panic or SIGBUS, log all errors (for testing only)
  66. */
  67. static int tolerant __read_mostly = 1;
  68. static int banks __read_mostly;
  69. static int rip_msr __read_mostly;
  70. static int mce_bootlog __read_mostly = -1;
  71. static int monarch_timeout __read_mostly = -1;
  72. static int mce_panic_timeout __read_mostly;
  73. static int mce_dont_log_ce __read_mostly;
  74. int mce_cmci_disabled __read_mostly;
  75. int mce_ignore_ce __read_mostly;
  76. int mce_ser __read_mostly;
  77. struct mce_bank *mce_banks __read_mostly;
  78. /* User mode helper program triggered by machine check event */
  79. static unsigned long mce_need_notify;
  80. static char mce_helper[128];
  81. static char *mce_helper_argv[2] = { mce_helper, NULL };
  82. static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
  83. static DEFINE_PER_CPU(struct mce, mces_seen);
  84. static int cpu_missing;
  85. /*
  86. * CPU/chipset specific EDAC code can register a notifier call here to print
  87. * MCE errors in a human-readable form.
  88. */
  89. ATOMIC_NOTIFIER_HEAD(x86_mce_decoder_chain);
  90. EXPORT_SYMBOL_GPL(x86_mce_decoder_chain);
  91. static int default_decode_mce(struct notifier_block *nb, unsigned long val,
  92. void *data)
  93. {
  94. pr_emerg("No human readable MCE decoding support on this CPU type.\n");
  95. pr_emerg("Run the message through 'mcelog --ascii' to decode.\n");
  96. return NOTIFY_STOP;
  97. }
  98. static struct notifier_block mce_dec_nb = {
  99. .notifier_call = default_decode_mce,
  100. .priority = -1,
  101. };
  102. /* MCA banks polled by the period polling timer for corrected events */
  103. DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
  104. [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
  105. };
  106. static DEFINE_PER_CPU(struct work_struct, mce_work);
  107. /* Do initial initialization of a struct mce */
  108. void mce_setup(struct mce *m)
  109. {
  110. memset(m, 0, sizeof(struct mce));
  111. m->cpu = m->extcpu = smp_processor_id();
  112. rdtscll(m->tsc);
  113. /* We hope get_seconds stays lockless */
  114. m->time = get_seconds();
  115. m->cpuvendor = boot_cpu_data.x86_vendor;
  116. m->cpuid = cpuid_eax(1);
  117. #ifdef CONFIG_SMP
  118. m->socketid = cpu_data(m->extcpu).phys_proc_id;
  119. #endif
  120. m->apicid = cpu_data(m->extcpu).initial_apicid;
  121. rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
  122. }
  123. DEFINE_PER_CPU(struct mce, injectm);
  124. EXPORT_PER_CPU_SYMBOL_GPL(injectm);
  125. /*
  126. * Lockless MCE logging infrastructure.
  127. * This avoids deadlocks on printk locks without having to break locks. Also
  128. * separate MCEs from kernel messages to avoid bogus bug reports.
  129. */
  130. static struct mce_log mcelog = {
  131. .signature = MCE_LOG_SIGNATURE,
  132. .len = MCE_LOG_LEN,
  133. .recordlen = sizeof(struct mce),
  134. };
  135. void mce_log(struct mce *mce)
  136. {
  137. unsigned next, entry;
  138. /* Emit the trace record: */
  139. trace_mce_record(mce);
  140. mce->finished = 0;
  141. wmb();
  142. for (;;) {
  143. entry = rcu_dereference_check_mce(mcelog.next);
  144. for (;;) {
  145. /*
  146. * If edac_mce is enabled, it will check the error type
  147. * and will process it, if it is a known error.
  148. * Otherwise, the error will be sent through mcelog
  149. * interface
  150. */
  151. if (edac_mce_parse(mce))
  152. return;
  153. /*
  154. * When the buffer fills up discard new entries.
  155. * Assume that the earlier errors are the more
  156. * interesting ones:
  157. */
  158. if (entry >= MCE_LOG_LEN) {
  159. set_bit(MCE_OVERFLOW,
  160. (unsigned long *)&mcelog.flags);
  161. return;
  162. }
  163. /* Old left over entry. Skip: */
  164. if (mcelog.entry[entry].finished) {
  165. entry++;
  166. continue;
  167. }
  168. break;
  169. }
  170. smp_rmb();
  171. next = entry + 1;
  172. if (cmpxchg(&mcelog.next, entry, next) == entry)
  173. break;
  174. }
  175. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  176. wmb();
  177. mcelog.entry[entry].finished = 1;
  178. wmb();
  179. mce->finished = 1;
  180. set_bit(0, &mce_need_notify);
  181. }
  182. static void print_mce(struct mce *m)
  183. {
  184. pr_emerg("CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
  185. m->extcpu, m->mcgstatus, m->bank, m->status);
  186. if (m->ip) {
  187. pr_emerg("RIP%s %02x:<%016Lx> ",
  188. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  189. m->cs, m->ip);
  190. if (m->cs == __KERNEL_CS)
  191. print_symbol("{%s}", m->ip);
  192. pr_cont("\n");
  193. }
  194. pr_emerg("TSC %llx ", m->tsc);
  195. if (m->addr)
  196. pr_cont("ADDR %llx ", m->addr);
  197. if (m->misc)
  198. pr_cont("MISC %llx ", m->misc);
  199. pr_cont("\n");
  200. pr_emerg("PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  201. m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid);
  202. /*
  203. * Print out human-readable details about the MCE error,
  204. * (if the CPU has an implementation for that)
  205. */
  206. atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
  207. }
  208. static void print_mce_head(void)
  209. {
  210. pr_emerg("\nHARDWARE ERROR\n");
  211. }
  212. static void print_mce_tail(void)
  213. {
  214. pr_emerg("This is not a software problem!\n");
  215. }
  216. #define PANIC_TIMEOUT 5 /* 5 seconds */
  217. static atomic_t mce_paniced;
  218. static int fake_panic;
  219. static atomic_t mce_fake_paniced;
  220. /* Panic in progress. Enable interrupts and wait for final IPI */
  221. static void wait_for_panic(void)
  222. {
  223. long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
  224. preempt_disable();
  225. local_irq_enable();
  226. while (timeout-- > 0)
  227. udelay(1);
  228. if (panic_timeout == 0)
  229. panic_timeout = mce_panic_timeout;
  230. panic("Panicing machine check CPU died");
  231. }
  232. static void mce_panic(char *msg, struct mce *final, char *exp)
  233. {
  234. int i, apei_err = 0;
  235. if (!fake_panic) {
  236. /*
  237. * Make sure only one CPU runs in machine check panic
  238. */
  239. if (atomic_inc_return(&mce_paniced) > 1)
  240. wait_for_panic();
  241. barrier();
  242. bust_spinlocks(1);
  243. console_verbose();
  244. } else {
  245. /* Don't log too much for fake panic */
  246. if (atomic_inc_return(&mce_fake_paniced) > 1)
  247. return;
  248. }
  249. print_mce_head();
  250. /* First print corrected ones that are still unlogged */
  251. for (i = 0; i < MCE_LOG_LEN; i++) {
  252. struct mce *m = &mcelog.entry[i];
  253. if (!(m->status & MCI_STATUS_VAL))
  254. continue;
  255. if (!(m->status & MCI_STATUS_UC)) {
  256. print_mce(m);
  257. if (!apei_err)
  258. apei_err = apei_write_mce(m);
  259. }
  260. }
  261. /* Now print uncorrected but with the final one last */
  262. for (i = 0; i < MCE_LOG_LEN; i++) {
  263. struct mce *m = &mcelog.entry[i];
  264. if (!(m->status & MCI_STATUS_VAL))
  265. continue;
  266. if (!(m->status & MCI_STATUS_UC))
  267. continue;
  268. if (!final || memcmp(m, final, sizeof(struct mce))) {
  269. print_mce(m);
  270. if (!apei_err)
  271. apei_err = apei_write_mce(m);
  272. }
  273. }
  274. if (final) {
  275. print_mce(final);
  276. if (!apei_err)
  277. apei_err = apei_write_mce(final);
  278. }
  279. if (cpu_missing)
  280. printk(KERN_EMERG "Some CPUs didn't answer in synchronization\n");
  281. print_mce_tail();
  282. if (exp)
  283. printk(KERN_EMERG "Machine check: %s\n", exp);
  284. if (!fake_panic) {
  285. if (panic_timeout == 0)
  286. panic_timeout = mce_panic_timeout;
  287. panic(msg);
  288. } else
  289. printk(KERN_EMERG "Fake kernel panic: %s\n", msg);
  290. }
  291. /* Support code for software error injection */
  292. static int msr_to_offset(u32 msr)
  293. {
  294. unsigned bank = __get_cpu_var(injectm.bank);
  295. if (msr == rip_msr)
  296. return offsetof(struct mce, ip);
  297. if (msr == MSR_IA32_MCx_STATUS(bank))
  298. return offsetof(struct mce, status);
  299. if (msr == MSR_IA32_MCx_ADDR(bank))
  300. return offsetof(struct mce, addr);
  301. if (msr == MSR_IA32_MCx_MISC(bank))
  302. return offsetof(struct mce, misc);
  303. if (msr == MSR_IA32_MCG_STATUS)
  304. return offsetof(struct mce, mcgstatus);
  305. return -1;
  306. }
  307. /* MSR access wrappers used for error injection */
  308. static u64 mce_rdmsrl(u32 msr)
  309. {
  310. u64 v;
  311. if (__get_cpu_var(injectm).finished) {
  312. int offset = msr_to_offset(msr);
  313. if (offset < 0)
  314. return 0;
  315. return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
  316. }
  317. if (rdmsrl_safe(msr, &v)) {
  318. WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
  319. /*
  320. * Return zero in case the access faulted. This should
  321. * not happen normally but can happen if the CPU does
  322. * something weird, or if the code is buggy.
  323. */
  324. v = 0;
  325. }
  326. return v;
  327. }
  328. static void mce_wrmsrl(u32 msr, u64 v)
  329. {
  330. if (__get_cpu_var(injectm).finished) {
  331. int offset = msr_to_offset(msr);
  332. if (offset >= 0)
  333. *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
  334. return;
  335. }
  336. wrmsrl(msr, v);
  337. }
  338. /*
  339. * Simple lockless ring to communicate PFNs from the exception handler with the
  340. * process context work function. This is vastly simplified because there's
  341. * only a single reader and a single writer.
  342. */
  343. #define MCE_RING_SIZE 16 /* we use one entry less */
  344. struct mce_ring {
  345. unsigned short start;
  346. unsigned short end;
  347. unsigned long ring[MCE_RING_SIZE];
  348. };
  349. static DEFINE_PER_CPU(struct mce_ring, mce_ring);
  350. /* Runs with CPU affinity in workqueue */
  351. static int mce_ring_empty(void)
  352. {
  353. struct mce_ring *r = &__get_cpu_var(mce_ring);
  354. return r->start == r->end;
  355. }
  356. static int mce_ring_get(unsigned long *pfn)
  357. {
  358. struct mce_ring *r;
  359. int ret = 0;
  360. *pfn = 0;
  361. get_cpu();
  362. r = &__get_cpu_var(mce_ring);
  363. if (r->start == r->end)
  364. goto out;
  365. *pfn = r->ring[r->start];
  366. r->start = (r->start + 1) % MCE_RING_SIZE;
  367. ret = 1;
  368. out:
  369. put_cpu();
  370. return ret;
  371. }
  372. /* Always runs in MCE context with preempt off */
  373. static int mce_ring_add(unsigned long pfn)
  374. {
  375. struct mce_ring *r = &__get_cpu_var(mce_ring);
  376. unsigned next;
  377. next = (r->end + 1) % MCE_RING_SIZE;
  378. if (next == r->start)
  379. return -1;
  380. r->ring[r->end] = pfn;
  381. wmb();
  382. r->end = next;
  383. return 0;
  384. }
  385. int mce_available(struct cpuinfo_x86 *c)
  386. {
  387. if (mce_disabled)
  388. return 0;
  389. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  390. }
  391. static void mce_schedule_work(void)
  392. {
  393. if (!mce_ring_empty()) {
  394. struct work_struct *work = &__get_cpu_var(mce_work);
  395. if (!work_pending(work))
  396. schedule_work(work);
  397. }
  398. }
  399. /*
  400. * Get the address of the instruction at the time of the machine check
  401. * error.
  402. */
  403. static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
  404. {
  405. if (regs && (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV))) {
  406. m->ip = regs->ip;
  407. m->cs = regs->cs;
  408. } else {
  409. m->ip = 0;
  410. m->cs = 0;
  411. }
  412. if (rip_msr)
  413. m->ip = mce_rdmsrl(rip_msr);
  414. }
  415. #ifdef CONFIG_X86_LOCAL_APIC
  416. /*
  417. * Called after interrupts have been reenabled again
  418. * when a MCE happened during an interrupts off region
  419. * in the kernel.
  420. */
  421. asmlinkage void smp_mce_self_interrupt(struct pt_regs *regs)
  422. {
  423. ack_APIC_irq();
  424. exit_idle();
  425. irq_enter();
  426. mce_notify_irq();
  427. mce_schedule_work();
  428. irq_exit();
  429. }
  430. #endif
  431. static void mce_report_event(struct pt_regs *regs)
  432. {
  433. if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
  434. mce_notify_irq();
  435. /*
  436. * Triggering the work queue here is just an insurance
  437. * policy in case the syscall exit notify handler
  438. * doesn't run soon enough or ends up running on the
  439. * wrong CPU (can happen when audit sleeps)
  440. */
  441. mce_schedule_work();
  442. return;
  443. }
  444. #ifdef CONFIG_X86_LOCAL_APIC
  445. /*
  446. * Without APIC do not notify. The event will be picked
  447. * up eventually.
  448. */
  449. if (!cpu_has_apic)
  450. return;
  451. /*
  452. * When interrupts are disabled we cannot use
  453. * kernel services safely. Trigger an self interrupt
  454. * through the APIC to instead do the notification
  455. * after interrupts are reenabled again.
  456. */
  457. apic->send_IPI_self(MCE_SELF_VECTOR);
  458. /*
  459. * Wait for idle afterwards again so that we don't leave the
  460. * APIC in a non idle state because the normal APIC writes
  461. * cannot exclude us.
  462. */
  463. apic_wait_icr_idle();
  464. #endif
  465. }
  466. DEFINE_PER_CPU(unsigned, mce_poll_count);
  467. /*
  468. * Poll for corrected events or events that happened before reset.
  469. * Those are just logged through /dev/mcelog.
  470. *
  471. * This is executed in standard interrupt context.
  472. *
  473. * Note: spec recommends to panic for fatal unsignalled
  474. * errors here. However this would be quite problematic --
  475. * we would need to reimplement the Monarch handling and
  476. * it would mess up the exclusion between exception handler
  477. * and poll hander -- * so we skip this for now.
  478. * These cases should not happen anyways, or only when the CPU
  479. * is already totally * confused. In this case it's likely it will
  480. * not fully execute the machine check handler either.
  481. */
  482. void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
  483. {
  484. struct mce m;
  485. int i;
  486. percpu_inc(mce_poll_count);
  487. mce_setup(&m);
  488. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  489. for (i = 0; i < banks; i++) {
  490. if (!mce_banks[i].ctl || !test_bit(i, *b))
  491. continue;
  492. m.misc = 0;
  493. m.addr = 0;
  494. m.bank = i;
  495. m.tsc = 0;
  496. barrier();
  497. m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  498. if (!(m.status & MCI_STATUS_VAL))
  499. continue;
  500. /*
  501. * Uncorrected or signalled events are handled by the exception
  502. * handler when it is enabled, so don't process those here.
  503. *
  504. * TBD do the same check for MCI_STATUS_EN here?
  505. */
  506. if (!(flags & MCP_UC) &&
  507. (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
  508. continue;
  509. if (m.status & MCI_STATUS_MISCV)
  510. m.misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
  511. if (m.status & MCI_STATUS_ADDRV)
  512. m.addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
  513. if (!(flags & MCP_TIMESTAMP))
  514. m.tsc = 0;
  515. /*
  516. * Don't get the IP here because it's unlikely to
  517. * have anything to do with the actual error location.
  518. */
  519. if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce) {
  520. mce_log(&m);
  521. add_taint(TAINT_MACHINE_CHECK);
  522. }
  523. /*
  524. * Clear state for this bank.
  525. */
  526. mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  527. }
  528. /*
  529. * Don't clear MCG_STATUS here because it's only defined for
  530. * exceptions.
  531. */
  532. sync_core();
  533. }
  534. EXPORT_SYMBOL_GPL(machine_check_poll);
  535. /*
  536. * Do a quick check if any of the events requires a panic.
  537. * This decides if we keep the events around or clear them.
  538. */
  539. static int mce_no_way_out(struct mce *m, char **msg)
  540. {
  541. int i;
  542. for (i = 0; i < banks; i++) {
  543. m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  544. if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
  545. return 1;
  546. }
  547. return 0;
  548. }
  549. /*
  550. * Variable to establish order between CPUs while scanning.
  551. * Each CPU spins initially until executing is equal its number.
  552. */
  553. static atomic_t mce_executing;
  554. /*
  555. * Defines order of CPUs on entry. First CPU becomes Monarch.
  556. */
  557. static atomic_t mce_callin;
  558. /*
  559. * Check if a timeout waiting for other CPUs happened.
  560. */
  561. static int mce_timed_out(u64 *t)
  562. {
  563. /*
  564. * The others already did panic for some reason.
  565. * Bail out like in a timeout.
  566. * rmb() to tell the compiler that system_state
  567. * might have been modified by someone else.
  568. */
  569. rmb();
  570. if (atomic_read(&mce_paniced))
  571. wait_for_panic();
  572. if (!monarch_timeout)
  573. goto out;
  574. if ((s64)*t < SPINUNIT) {
  575. /* CHECKME: Make panic default for 1 too? */
  576. if (tolerant < 1)
  577. mce_panic("Timeout synchronizing machine check over CPUs",
  578. NULL, NULL);
  579. cpu_missing = 1;
  580. return 1;
  581. }
  582. *t -= SPINUNIT;
  583. out:
  584. touch_nmi_watchdog();
  585. return 0;
  586. }
  587. /*
  588. * The Monarch's reign. The Monarch is the CPU who entered
  589. * the machine check handler first. It waits for the others to
  590. * raise the exception too and then grades them. When any
  591. * error is fatal panic. Only then let the others continue.
  592. *
  593. * The other CPUs entering the MCE handler will be controlled by the
  594. * Monarch. They are called Subjects.
  595. *
  596. * This way we prevent any potential data corruption in a unrecoverable case
  597. * and also makes sure always all CPU's errors are examined.
  598. *
  599. * Also this detects the case of a machine check event coming from outer
  600. * space (not detected by any CPUs) In this case some external agent wants
  601. * us to shut down, so panic too.
  602. *
  603. * The other CPUs might still decide to panic if the handler happens
  604. * in a unrecoverable place, but in this case the system is in a semi-stable
  605. * state and won't corrupt anything by itself. It's ok to let the others
  606. * continue for a bit first.
  607. *
  608. * All the spin loops have timeouts; when a timeout happens a CPU
  609. * typically elects itself to be Monarch.
  610. */
  611. static void mce_reign(void)
  612. {
  613. int cpu;
  614. struct mce *m = NULL;
  615. int global_worst = 0;
  616. char *msg = NULL;
  617. char *nmsg = NULL;
  618. /*
  619. * This CPU is the Monarch and the other CPUs have run
  620. * through their handlers.
  621. * Grade the severity of the errors of all the CPUs.
  622. */
  623. for_each_possible_cpu(cpu) {
  624. int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
  625. &nmsg);
  626. if (severity > global_worst) {
  627. msg = nmsg;
  628. global_worst = severity;
  629. m = &per_cpu(mces_seen, cpu);
  630. }
  631. }
  632. /*
  633. * Cannot recover? Panic here then.
  634. * This dumps all the mces in the log buffer and stops the
  635. * other CPUs.
  636. */
  637. if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
  638. mce_panic("Fatal Machine check", m, msg);
  639. /*
  640. * For UC somewhere we let the CPU who detects it handle it.
  641. * Also must let continue the others, otherwise the handling
  642. * CPU could deadlock on a lock.
  643. */
  644. /*
  645. * No machine check event found. Must be some external
  646. * source or one CPU is hung. Panic.
  647. */
  648. if (global_worst <= MCE_KEEP_SEVERITY && tolerant < 3)
  649. mce_panic("Machine check from unknown source", NULL, NULL);
  650. /*
  651. * Now clear all the mces_seen so that they don't reappear on
  652. * the next mce.
  653. */
  654. for_each_possible_cpu(cpu)
  655. memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
  656. }
  657. static atomic_t global_nwo;
  658. /*
  659. * Start of Monarch synchronization. This waits until all CPUs have
  660. * entered the exception handler and then determines if any of them
  661. * saw a fatal event that requires panic. Then it executes them
  662. * in the entry order.
  663. * TBD double check parallel CPU hotunplug
  664. */
  665. static int mce_start(int *no_way_out)
  666. {
  667. int order;
  668. int cpus = num_online_cpus();
  669. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  670. if (!timeout)
  671. return -1;
  672. atomic_add(*no_way_out, &global_nwo);
  673. /*
  674. * global_nwo should be updated before mce_callin
  675. */
  676. smp_wmb();
  677. order = atomic_inc_return(&mce_callin);
  678. /*
  679. * Wait for everyone.
  680. */
  681. while (atomic_read(&mce_callin) != cpus) {
  682. if (mce_timed_out(&timeout)) {
  683. atomic_set(&global_nwo, 0);
  684. return -1;
  685. }
  686. ndelay(SPINUNIT);
  687. }
  688. /*
  689. * mce_callin should be read before global_nwo
  690. */
  691. smp_rmb();
  692. if (order == 1) {
  693. /*
  694. * Monarch: Starts executing now, the others wait.
  695. */
  696. atomic_set(&mce_executing, 1);
  697. } else {
  698. /*
  699. * Subject: Now start the scanning loop one by one in
  700. * the original callin order.
  701. * This way when there are any shared banks it will be
  702. * only seen by one CPU before cleared, avoiding duplicates.
  703. */
  704. while (atomic_read(&mce_executing) < order) {
  705. if (mce_timed_out(&timeout)) {
  706. atomic_set(&global_nwo, 0);
  707. return -1;
  708. }
  709. ndelay(SPINUNIT);
  710. }
  711. }
  712. /*
  713. * Cache the global no_way_out state.
  714. */
  715. *no_way_out = atomic_read(&global_nwo);
  716. return order;
  717. }
  718. /*
  719. * Synchronize between CPUs after main scanning loop.
  720. * This invokes the bulk of the Monarch processing.
  721. */
  722. static int mce_end(int order)
  723. {
  724. int ret = -1;
  725. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  726. if (!timeout)
  727. goto reset;
  728. if (order < 0)
  729. goto reset;
  730. /*
  731. * Allow others to run.
  732. */
  733. atomic_inc(&mce_executing);
  734. if (order == 1) {
  735. /* CHECKME: Can this race with a parallel hotplug? */
  736. int cpus = num_online_cpus();
  737. /*
  738. * Monarch: Wait for everyone to go through their scanning
  739. * loops.
  740. */
  741. while (atomic_read(&mce_executing) <= cpus) {
  742. if (mce_timed_out(&timeout))
  743. goto reset;
  744. ndelay(SPINUNIT);
  745. }
  746. mce_reign();
  747. barrier();
  748. ret = 0;
  749. } else {
  750. /*
  751. * Subject: Wait for Monarch to finish.
  752. */
  753. while (atomic_read(&mce_executing) != 0) {
  754. if (mce_timed_out(&timeout))
  755. goto reset;
  756. ndelay(SPINUNIT);
  757. }
  758. /*
  759. * Don't reset anything. That's done by the Monarch.
  760. */
  761. return 0;
  762. }
  763. /*
  764. * Reset all global state.
  765. */
  766. reset:
  767. atomic_set(&global_nwo, 0);
  768. atomic_set(&mce_callin, 0);
  769. barrier();
  770. /*
  771. * Let others run again.
  772. */
  773. atomic_set(&mce_executing, 0);
  774. return ret;
  775. }
  776. /*
  777. * Check if the address reported by the CPU is in a format we can parse.
  778. * It would be possible to add code for most other cases, but all would
  779. * be somewhat complicated (e.g. segment offset would require an instruction
  780. * parser). So only support physical addresses upto page granuality for now.
  781. */
  782. static int mce_usable_address(struct mce *m)
  783. {
  784. if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
  785. return 0;
  786. if ((m->misc & 0x3f) > PAGE_SHIFT)
  787. return 0;
  788. if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS)
  789. return 0;
  790. return 1;
  791. }
  792. static void mce_clear_state(unsigned long *toclear)
  793. {
  794. int i;
  795. for (i = 0; i < banks; i++) {
  796. if (test_bit(i, toclear))
  797. mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  798. }
  799. }
  800. /*
  801. * The actual machine check handler. This only handles real
  802. * exceptions when something got corrupted coming in through int 18.
  803. *
  804. * This is executed in NMI context not subject to normal locking rules. This
  805. * implies that most kernel services cannot be safely used. Don't even
  806. * think about putting a printk in there!
  807. *
  808. * On Intel systems this is entered on all CPUs in parallel through
  809. * MCE broadcast. However some CPUs might be broken beyond repair,
  810. * so be always careful when synchronizing with others.
  811. */
  812. void do_machine_check(struct pt_regs *regs, long error_code)
  813. {
  814. struct mce m, *final;
  815. int i;
  816. int worst = 0;
  817. int severity;
  818. /*
  819. * Establish sequential order between the CPUs entering the machine
  820. * check handler.
  821. */
  822. int order;
  823. /*
  824. * If no_way_out gets set, there is no safe way to recover from this
  825. * MCE. If tolerant is cranked up, we'll try anyway.
  826. */
  827. int no_way_out = 0;
  828. /*
  829. * If kill_it gets set, there might be a way to recover from this
  830. * error.
  831. */
  832. int kill_it = 0;
  833. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  834. char *msg = "Unknown";
  835. atomic_inc(&mce_entry);
  836. percpu_inc(mce_exception_count);
  837. if (notify_die(DIE_NMI, "machine check", regs, error_code,
  838. 18, SIGKILL) == NOTIFY_STOP)
  839. goto out;
  840. if (!banks)
  841. goto out;
  842. mce_setup(&m);
  843. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  844. final = &__get_cpu_var(mces_seen);
  845. *final = m;
  846. no_way_out = mce_no_way_out(&m, &msg);
  847. barrier();
  848. /*
  849. * When no restart IP must always kill or panic.
  850. */
  851. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  852. kill_it = 1;
  853. /*
  854. * Go through all the banks in exclusion of the other CPUs.
  855. * This way we don't report duplicated events on shared banks
  856. * because the first one to see it will clear it.
  857. */
  858. order = mce_start(&no_way_out);
  859. for (i = 0; i < banks; i++) {
  860. __clear_bit(i, toclear);
  861. if (!mce_banks[i].ctl)
  862. continue;
  863. m.misc = 0;
  864. m.addr = 0;
  865. m.bank = i;
  866. m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  867. if ((m.status & MCI_STATUS_VAL) == 0)
  868. continue;
  869. /*
  870. * Non uncorrected or non signaled errors are handled by
  871. * machine_check_poll. Leave them alone, unless this panics.
  872. */
  873. if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  874. !no_way_out)
  875. continue;
  876. /*
  877. * Set taint even when machine check was not enabled.
  878. */
  879. add_taint(TAINT_MACHINE_CHECK);
  880. severity = mce_severity(&m, tolerant, NULL);
  881. /*
  882. * When machine check was for corrected handler don't touch,
  883. * unless we're panicing.
  884. */
  885. if (severity == MCE_KEEP_SEVERITY && !no_way_out)
  886. continue;
  887. __set_bit(i, toclear);
  888. if (severity == MCE_NO_SEVERITY) {
  889. /*
  890. * Machine check event was not enabled. Clear, but
  891. * ignore.
  892. */
  893. continue;
  894. }
  895. /*
  896. * Kill on action required.
  897. */
  898. if (severity == MCE_AR_SEVERITY)
  899. kill_it = 1;
  900. if (m.status & MCI_STATUS_MISCV)
  901. m.misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
  902. if (m.status & MCI_STATUS_ADDRV)
  903. m.addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
  904. /*
  905. * Action optional error. Queue address for later processing.
  906. * When the ring overflows we just ignore the AO error.
  907. * RED-PEN add some logging mechanism when
  908. * usable_address or mce_add_ring fails.
  909. * RED-PEN don't ignore overflow for tolerant == 0
  910. */
  911. if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
  912. mce_ring_add(m.addr >> PAGE_SHIFT);
  913. mce_get_rip(&m, regs);
  914. mce_log(&m);
  915. if (severity > worst) {
  916. *final = m;
  917. worst = severity;
  918. }
  919. }
  920. if (!no_way_out)
  921. mce_clear_state(toclear);
  922. /*
  923. * Do most of the synchronization with other CPUs.
  924. * When there's any problem use only local no_way_out state.
  925. */
  926. if (mce_end(order) < 0)
  927. no_way_out = worst >= MCE_PANIC_SEVERITY;
  928. /*
  929. * If we have decided that we just CAN'T continue, and the user
  930. * has not set tolerant to an insane level, give up and die.
  931. *
  932. * This is mainly used in the case when the system doesn't
  933. * support MCE broadcasting or it has been disabled.
  934. */
  935. if (no_way_out && tolerant < 3)
  936. mce_panic("Fatal machine check on current CPU", final, msg);
  937. /*
  938. * If the error seems to be unrecoverable, something should be
  939. * done. Try to kill as little as possible. If we can kill just
  940. * one task, do that. If the user has set the tolerance very
  941. * high, don't try to do anything at all.
  942. */
  943. if (kill_it && tolerant < 3)
  944. force_sig(SIGBUS, current);
  945. /* notify userspace ASAP */
  946. set_thread_flag(TIF_MCE_NOTIFY);
  947. if (worst > 0)
  948. mce_report_event(regs);
  949. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  950. out:
  951. atomic_dec(&mce_entry);
  952. sync_core();
  953. }
  954. EXPORT_SYMBOL_GPL(do_machine_check);
  955. /* dummy to break dependency. actual code is in mm/memory-failure.c */
  956. void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
  957. {
  958. printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
  959. }
  960. /*
  961. * Called after mce notification in process context. This code
  962. * is allowed to sleep. Call the high level VM handler to process
  963. * any corrupted pages.
  964. * Assume that the work queue code only calls this one at a time
  965. * per CPU.
  966. * Note we don't disable preemption, so this code might run on the wrong
  967. * CPU. In this case the event is picked up by the scheduled work queue.
  968. * This is merely a fast path to expedite processing in some common
  969. * cases.
  970. */
  971. void mce_notify_process(void)
  972. {
  973. unsigned long pfn;
  974. mce_notify_irq();
  975. while (mce_ring_get(&pfn))
  976. memory_failure(pfn, MCE_VECTOR);
  977. }
  978. static void mce_process_work(struct work_struct *dummy)
  979. {
  980. mce_notify_process();
  981. }
  982. #ifdef CONFIG_X86_MCE_INTEL
  983. /***
  984. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  985. * @cpu: The CPU on which the event occurred.
  986. * @status: Event status information
  987. *
  988. * This function should be called by the thermal interrupt after the
  989. * event has been processed and the decision was made to log the event
  990. * further.
  991. *
  992. * The status parameter will be saved to the 'status' field of 'struct mce'
  993. * and historically has been the register value of the
  994. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  995. */
  996. void mce_log_therm_throt_event(__u64 status)
  997. {
  998. struct mce m;
  999. mce_setup(&m);
  1000. m.bank = MCE_THERMAL_BANK;
  1001. m.status = status;
  1002. mce_log(&m);
  1003. }
  1004. #endif /* CONFIG_X86_MCE_INTEL */
  1005. /*
  1006. * Periodic polling timer for "silent" machine check errors. If the
  1007. * poller finds an MCE, poll 2x faster. When the poller finds no more
  1008. * errors, poll 2x slower (up to check_interval seconds).
  1009. */
  1010. static int check_interval = 5 * 60; /* 5 minutes */
  1011. static DEFINE_PER_CPU(int, mce_next_interval); /* in jiffies */
  1012. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  1013. static void mce_start_timer(unsigned long data)
  1014. {
  1015. struct timer_list *t = &per_cpu(mce_timer, data);
  1016. int *n;
  1017. WARN_ON(smp_processor_id() != data);
  1018. if (mce_available(&current_cpu_data)) {
  1019. machine_check_poll(MCP_TIMESTAMP,
  1020. &__get_cpu_var(mce_poll_banks));
  1021. }
  1022. /*
  1023. * Alert userspace if needed. If we logged an MCE, reduce the
  1024. * polling interval, otherwise increase the polling interval.
  1025. */
  1026. n = &__get_cpu_var(mce_next_interval);
  1027. if (mce_notify_irq())
  1028. *n = max(*n/2, HZ/100);
  1029. else
  1030. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  1031. t->expires = jiffies + *n;
  1032. add_timer_on(t, smp_processor_id());
  1033. }
  1034. static void mce_do_trigger(struct work_struct *work)
  1035. {
  1036. call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
  1037. }
  1038. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  1039. /*
  1040. * Notify the user(s) about new machine check events.
  1041. * Can be called from interrupt context, but not from machine check/NMI
  1042. * context.
  1043. */
  1044. int mce_notify_irq(void)
  1045. {
  1046. /* Not more than two messages every minute */
  1047. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  1048. clear_thread_flag(TIF_MCE_NOTIFY);
  1049. if (test_and_clear_bit(0, &mce_need_notify)) {
  1050. wake_up_interruptible(&mce_wait);
  1051. /*
  1052. * There is no risk of missing notifications because
  1053. * work_pending is always cleared before the function is
  1054. * executed.
  1055. */
  1056. if (mce_helper[0] && !work_pending(&mce_trigger_work))
  1057. schedule_work(&mce_trigger_work);
  1058. if (__ratelimit(&ratelimit))
  1059. printk(KERN_INFO "Machine check events logged\n");
  1060. return 1;
  1061. }
  1062. return 0;
  1063. }
  1064. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1065. static int __cpuinit __mcheck_cpu_mce_banks_init(void)
  1066. {
  1067. int i;
  1068. mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
  1069. if (!mce_banks)
  1070. return -ENOMEM;
  1071. for (i = 0; i < banks; i++) {
  1072. struct mce_bank *b = &mce_banks[i];
  1073. b->ctl = -1ULL;
  1074. b->init = 1;
  1075. }
  1076. return 0;
  1077. }
  1078. /*
  1079. * Initialize Machine Checks for a CPU.
  1080. */
  1081. static int __cpuinit __mcheck_cpu_cap_init(void)
  1082. {
  1083. unsigned b;
  1084. u64 cap;
  1085. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1086. b = cap & MCG_BANKCNT_MASK;
  1087. if (!banks)
  1088. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  1089. if (b > MAX_NR_BANKS) {
  1090. printk(KERN_WARNING
  1091. "MCE: Using only %u machine check banks out of %u\n",
  1092. MAX_NR_BANKS, b);
  1093. b = MAX_NR_BANKS;
  1094. }
  1095. /* Don't support asymmetric configurations today */
  1096. WARN_ON(banks != 0 && b != banks);
  1097. banks = b;
  1098. if (!mce_banks) {
  1099. int err = __mcheck_cpu_mce_banks_init();
  1100. if (err)
  1101. return err;
  1102. }
  1103. /* Use accurate RIP reporting if available. */
  1104. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1105. rip_msr = MSR_IA32_MCG_EIP;
  1106. if (cap & MCG_SER_P)
  1107. mce_ser = 1;
  1108. return 0;
  1109. }
  1110. static void __mcheck_cpu_init_generic(void)
  1111. {
  1112. mce_banks_t all_banks;
  1113. u64 cap;
  1114. int i;
  1115. /*
  1116. * Log the machine checks left over from the previous reset.
  1117. */
  1118. bitmap_fill(all_banks, MAX_NR_BANKS);
  1119. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  1120. set_in_cr4(X86_CR4_MCE);
  1121. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1122. if (cap & MCG_CTL_P)
  1123. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1124. for (i = 0; i < banks; i++) {
  1125. struct mce_bank *b = &mce_banks[i];
  1126. if (!b->init)
  1127. continue;
  1128. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1129. wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  1130. }
  1131. }
  1132. /* Add per CPU specific workarounds here */
  1133. static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
  1134. {
  1135. if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
  1136. pr_info("MCE: unknown CPU type - not enabling MCE support.\n");
  1137. return -EOPNOTSUPP;
  1138. }
  1139. /* This should be disabled by the BIOS, but isn't always */
  1140. if (c->x86_vendor == X86_VENDOR_AMD) {
  1141. if (c->x86 == 15 && banks > 4) {
  1142. /*
  1143. * disable GART TBL walk error reporting, which
  1144. * trips off incorrectly with the IOMMU & 3ware
  1145. * & Cerberus:
  1146. */
  1147. clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
  1148. }
  1149. if (c->x86 <= 17 && mce_bootlog < 0) {
  1150. /*
  1151. * Lots of broken BIOS around that don't clear them
  1152. * by default and leave crap in there. Don't log:
  1153. */
  1154. mce_bootlog = 0;
  1155. }
  1156. /*
  1157. * Various K7s with broken bank 0 around. Always disable
  1158. * by default.
  1159. */
  1160. if (c->x86 == 6 && banks > 0)
  1161. mce_banks[0].ctl = 0;
  1162. }
  1163. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1164. /*
  1165. * SDM documents that on family 6 bank 0 should not be written
  1166. * because it aliases to another special BIOS controlled
  1167. * register.
  1168. * But it's not aliased anymore on model 0x1a+
  1169. * Don't ignore bank 0 completely because there could be a
  1170. * valid event later, merely don't write CTL0.
  1171. */
  1172. if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
  1173. mce_banks[0].init = 0;
  1174. /*
  1175. * All newer Intel systems support MCE broadcasting. Enable
  1176. * synchronization with a one second timeout.
  1177. */
  1178. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1179. monarch_timeout < 0)
  1180. monarch_timeout = USEC_PER_SEC;
  1181. /*
  1182. * There are also broken BIOSes on some Pentium M and
  1183. * earlier systems:
  1184. */
  1185. if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
  1186. mce_bootlog = 0;
  1187. }
  1188. if (monarch_timeout < 0)
  1189. monarch_timeout = 0;
  1190. if (mce_bootlog != 0)
  1191. mce_panic_timeout = 30;
  1192. return 0;
  1193. }
  1194. static void __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
  1195. {
  1196. if (c->x86 != 5)
  1197. return;
  1198. switch (c->x86_vendor) {
  1199. case X86_VENDOR_INTEL:
  1200. intel_p5_mcheck_init(c);
  1201. break;
  1202. case X86_VENDOR_CENTAUR:
  1203. winchip_mcheck_init(c);
  1204. break;
  1205. }
  1206. }
  1207. static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
  1208. {
  1209. switch (c->x86_vendor) {
  1210. case X86_VENDOR_INTEL:
  1211. mce_intel_feature_init(c);
  1212. break;
  1213. case X86_VENDOR_AMD:
  1214. mce_amd_feature_init(c);
  1215. break;
  1216. default:
  1217. break;
  1218. }
  1219. }
  1220. static void __mcheck_cpu_init_timer(void)
  1221. {
  1222. struct timer_list *t = &__get_cpu_var(mce_timer);
  1223. int *n = &__get_cpu_var(mce_next_interval);
  1224. setup_timer(t, mce_start_timer, smp_processor_id());
  1225. if (mce_ignore_ce)
  1226. return;
  1227. *n = check_interval * HZ;
  1228. if (!*n)
  1229. return;
  1230. t->expires = round_jiffies(jiffies + *n);
  1231. add_timer_on(t, smp_processor_id());
  1232. }
  1233. /* Handle unconfigured int18 (should never happen) */
  1234. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  1235. {
  1236. printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
  1237. smp_processor_id());
  1238. }
  1239. /* Call the installed machine check handler for this CPU setup. */
  1240. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  1241. unexpected_machine_check;
  1242. /*
  1243. * Called for each booted CPU to set up machine checks.
  1244. * Must be called with preempt off:
  1245. */
  1246. void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
  1247. {
  1248. if (mce_disabled)
  1249. return;
  1250. __mcheck_cpu_ancient_init(c);
  1251. if (!mce_available(c))
  1252. return;
  1253. if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
  1254. mce_disabled = 1;
  1255. return;
  1256. }
  1257. machine_check_vector = do_machine_check;
  1258. __mcheck_cpu_init_generic();
  1259. __mcheck_cpu_init_vendor(c);
  1260. __mcheck_cpu_init_timer();
  1261. INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
  1262. }
  1263. /*
  1264. * Character device to read and clear the MCE log.
  1265. */
  1266. static DEFINE_SPINLOCK(mce_state_lock);
  1267. static int open_count; /* #times opened */
  1268. static int open_exclu; /* already open exclusive? */
  1269. static int mce_open(struct inode *inode, struct file *file)
  1270. {
  1271. spin_lock(&mce_state_lock);
  1272. if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
  1273. spin_unlock(&mce_state_lock);
  1274. return -EBUSY;
  1275. }
  1276. if (file->f_flags & O_EXCL)
  1277. open_exclu = 1;
  1278. open_count++;
  1279. spin_unlock(&mce_state_lock);
  1280. return nonseekable_open(inode, file);
  1281. }
  1282. static int mce_release(struct inode *inode, struct file *file)
  1283. {
  1284. spin_lock(&mce_state_lock);
  1285. open_count--;
  1286. open_exclu = 0;
  1287. spin_unlock(&mce_state_lock);
  1288. return 0;
  1289. }
  1290. static void collect_tscs(void *data)
  1291. {
  1292. unsigned long *cpu_tsc = (unsigned long *)data;
  1293. rdtscll(cpu_tsc[smp_processor_id()]);
  1294. }
  1295. static int mce_apei_read_done;
  1296. /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
  1297. static int __mce_read_apei(char __user **ubuf, size_t usize)
  1298. {
  1299. int rc;
  1300. u64 record_id;
  1301. struct mce m;
  1302. if (usize < sizeof(struct mce))
  1303. return -EINVAL;
  1304. rc = apei_read_mce(&m, &record_id);
  1305. /* Error or no more MCE record */
  1306. if (rc <= 0) {
  1307. mce_apei_read_done = 1;
  1308. return rc;
  1309. }
  1310. rc = -EFAULT;
  1311. if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
  1312. return rc;
  1313. /*
  1314. * In fact, we should have cleared the record after that has
  1315. * been flushed to the disk or sent to network in
  1316. * /sbin/mcelog, but we have no interface to support that now,
  1317. * so just clear it to avoid duplication.
  1318. */
  1319. rc = apei_clear_mce(record_id);
  1320. if (rc) {
  1321. mce_apei_read_done = 1;
  1322. return rc;
  1323. }
  1324. *ubuf += sizeof(struct mce);
  1325. return 0;
  1326. }
  1327. static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
  1328. loff_t *off)
  1329. {
  1330. char __user *buf = ubuf;
  1331. unsigned long *cpu_tsc;
  1332. unsigned prev, next;
  1333. int i, err;
  1334. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1335. if (!cpu_tsc)
  1336. return -ENOMEM;
  1337. mutex_lock(&mce_read_mutex);
  1338. if (!mce_apei_read_done) {
  1339. err = __mce_read_apei(&buf, usize);
  1340. if (err || buf != ubuf)
  1341. goto out;
  1342. }
  1343. next = rcu_dereference_check_mce(mcelog.next);
  1344. /* Only supports full reads right now */
  1345. err = -EINVAL;
  1346. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
  1347. goto out;
  1348. err = 0;
  1349. prev = 0;
  1350. do {
  1351. for (i = prev; i < next; i++) {
  1352. unsigned long start = jiffies;
  1353. while (!mcelog.entry[i].finished) {
  1354. if (time_after_eq(jiffies, start + 2)) {
  1355. memset(mcelog.entry + i, 0,
  1356. sizeof(struct mce));
  1357. goto timeout;
  1358. }
  1359. cpu_relax();
  1360. }
  1361. smp_rmb();
  1362. err |= copy_to_user(buf, mcelog.entry + i,
  1363. sizeof(struct mce));
  1364. buf += sizeof(struct mce);
  1365. timeout:
  1366. ;
  1367. }
  1368. memset(mcelog.entry + prev, 0,
  1369. (next - prev) * sizeof(struct mce));
  1370. prev = next;
  1371. next = cmpxchg(&mcelog.next, prev, 0);
  1372. } while (next != prev);
  1373. synchronize_sched();
  1374. /*
  1375. * Collect entries that were still getting written before the
  1376. * synchronize.
  1377. */
  1378. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1379. for (i = next; i < MCE_LOG_LEN; i++) {
  1380. if (mcelog.entry[i].finished &&
  1381. mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
  1382. err |= copy_to_user(buf, mcelog.entry+i,
  1383. sizeof(struct mce));
  1384. smp_rmb();
  1385. buf += sizeof(struct mce);
  1386. memset(&mcelog.entry[i], 0, sizeof(struct mce));
  1387. }
  1388. }
  1389. if (err)
  1390. err = -EFAULT;
  1391. out:
  1392. mutex_unlock(&mce_read_mutex);
  1393. kfree(cpu_tsc);
  1394. return err ? err : buf - ubuf;
  1395. }
  1396. static unsigned int mce_poll(struct file *file, poll_table *wait)
  1397. {
  1398. poll_wait(file, &mce_wait, wait);
  1399. if (rcu_dereference_check_mce(mcelog.next))
  1400. return POLLIN | POLLRDNORM;
  1401. if (!mce_apei_read_done && apei_check_mce())
  1402. return POLLIN | POLLRDNORM;
  1403. return 0;
  1404. }
  1405. static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  1406. {
  1407. int __user *p = (int __user *)arg;
  1408. if (!capable(CAP_SYS_ADMIN))
  1409. return -EPERM;
  1410. switch (cmd) {
  1411. case MCE_GET_RECORD_LEN:
  1412. return put_user(sizeof(struct mce), p);
  1413. case MCE_GET_LOG_LEN:
  1414. return put_user(MCE_LOG_LEN, p);
  1415. case MCE_GETCLEAR_FLAGS: {
  1416. unsigned flags;
  1417. do {
  1418. flags = mcelog.flags;
  1419. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1420. return put_user(flags, p);
  1421. }
  1422. default:
  1423. return -ENOTTY;
  1424. }
  1425. }
  1426. /* Modified in mce-inject.c, so not static or const */
  1427. struct file_operations mce_chrdev_ops = {
  1428. .open = mce_open,
  1429. .release = mce_release,
  1430. .read = mce_read,
  1431. .poll = mce_poll,
  1432. .unlocked_ioctl = mce_ioctl,
  1433. };
  1434. EXPORT_SYMBOL_GPL(mce_chrdev_ops);
  1435. static struct miscdevice mce_log_device = {
  1436. MISC_MCELOG_MINOR,
  1437. "mcelog",
  1438. &mce_chrdev_ops,
  1439. };
  1440. /*
  1441. * mce=off Disables machine check
  1442. * mce=no_cmci Disables CMCI
  1443. * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
  1444. * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
  1445. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1446. * monarchtimeout is how long to wait for other CPUs on machine
  1447. * check, or 0 to not wait
  1448. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1449. * mce=nobootlog Don't log MCEs from before booting.
  1450. */
  1451. static int __init mcheck_enable(char *str)
  1452. {
  1453. if (*str == 0) {
  1454. enable_p5_mce();
  1455. return 1;
  1456. }
  1457. if (*str == '=')
  1458. str++;
  1459. if (!strcmp(str, "off"))
  1460. mce_disabled = 1;
  1461. else if (!strcmp(str, "no_cmci"))
  1462. mce_cmci_disabled = 1;
  1463. else if (!strcmp(str, "dont_log_ce"))
  1464. mce_dont_log_ce = 1;
  1465. else if (!strcmp(str, "ignore_ce"))
  1466. mce_ignore_ce = 1;
  1467. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1468. mce_bootlog = (str[0] == 'b');
  1469. else if (isdigit(str[0])) {
  1470. get_option(&str, &tolerant);
  1471. if (*str == ',') {
  1472. ++str;
  1473. get_option(&str, &monarch_timeout);
  1474. }
  1475. } else {
  1476. printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
  1477. str);
  1478. return 0;
  1479. }
  1480. return 1;
  1481. }
  1482. __setup("mce", mcheck_enable);
  1483. int __init mcheck_init(void)
  1484. {
  1485. atomic_notifier_chain_register(&x86_mce_decoder_chain, &mce_dec_nb);
  1486. mcheck_intel_therm_init();
  1487. return 0;
  1488. }
  1489. /*
  1490. * Sysfs support
  1491. */
  1492. /*
  1493. * Disable machine checks on suspend and shutdown. We can't really handle
  1494. * them later.
  1495. */
  1496. static int mce_disable_error_reporting(void)
  1497. {
  1498. int i;
  1499. for (i = 0; i < banks; i++) {
  1500. struct mce_bank *b = &mce_banks[i];
  1501. if (b->init)
  1502. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1503. }
  1504. return 0;
  1505. }
  1506. static int mce_suspend(struct sys_device *dev, pm_message_t state)
  1507. {
  1508. return mce_disable_error_reporting();
  1509. }
  1510. static int mce_shutdown(struct sys_device *dev)
  1511. {
  1512. return mce_disable_error_reporting();
  1513. }
  1514. /*
  1515. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1516. * Only one CPU is active at this time, the others get re-added later using
  1517. * CPU hotplug:
  1518. */
  1519. static int mce_resume(struct sys_device *dev)
  1520. {
  1521. __mcheck_cpu_init_generic();
  1522. __mcheck_cpu_init_vendor(&current_cpu_data);
  1523. return 0;
  1524. }
  1525. static void mce_cpu_restart(void *data)
  1526. {
  1527. del_timer_sync(&__get_cpu_var(mce_timer));
  1528. if (!mce_available(&current_cpu_data))
  1529. return;
  1530. __mcheck_cpu_init_generic();
  1531. __mcheck_cpu_init_timer();
  1532. }
  1533. /* Reinit MCEs after user configuration changes */
  1534. static void mce_restart(void)
  1535. {
  1536. on_each_cpu(mce_cpu_restart, NULL, 1);
  1537. }
  1538. /* Toggle features for corrected errors */
  1539. static void mce_disable_ce(void *all)
  1540. {
  1541. if (!mce_available(&current_cpu_data))
  1542. return;
  1543. if (all)
  1544. del_timer_sync(&__get_cpu_var(mce_timer));
  1545. cmci_clear();
  1546. }
  1547. static void mce_enable_ce(void *all)
  1548. {
  1549. if (!mce_available(&current_cpu_data))
  1550. return;
  1551. cmci_reenable();
  1552. cmci_recheck();
  1553. if (all)
  1554. __mcheck_cpu_init_timer();
  1555. }
  1556. static struct sysdev_class mce_sysclass = {
  1557. .suspend = mce_suspend,
  1558. .shutdown = mce_shutdown,
  1559. .resume = mce_resume,
  1560. .name = "machinecheck",
  1561. };
  1562. DEFINE_PER_CPU(struct sys_device, mce_dev);
  1563. __cpuinitdata
  1564. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  1565. static inline struct mce_bank *attr_to_bank(struct sysdev_attribute *attr)
  1566. {
  1567. return container_of(attr, struct mce_bank, attr);
  1568. }
  1569. static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1570. char *buf)
  1571. {
  1572. return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
  1573. }
  1574. static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1575. const char *buf, size_t size)
  1576. {
  1577. u64 new;
  1578. if (strict_strtoull(buf, 0, &new) < 0)
  1579. return -EINVAL;
  1580. attr_to_bank(attr)->ctl = new;
  1581. mce_restart();
  1582. return size;
  1583. }
  1584. static ssize_t
  1585. show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
  1586. {
  1587. strcpy(buf, mce_helper);
  1588. strcat(buf, "\n");
  1589. return strlen(mce_helper) + 1;
  1590. }
  1591. static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
  1592. const char *buf, size_t siz)
  1593. {
  1594. char *p;
  1595. strncpy(mce_helper, buf, sizeof(mce_helper));
  1596. mce_helper[sizeof(mce_helper)-1] = 0;
  1597. p = strchr(mce_helper, '\n');
  1598. if (p)
  1599. *p = 0;
  1600. return strlen(mce_helper) + !!p;
  1601. }
  1602. static ssize_t set_ignore_ce(struct sys_device *s,
  1603. struct sysdev_attribute *attr,
  1604. const char *buf, size_t size)
  1605. {
  1606. u64 new;
  1607. if (strict_strtoull(buf, 0, &new) < 0)
  1608. return -EINVAL;
  1609. if (mce_ignore_ce ^ !!new) {
  1610. if (new) {
  1611. /* disable ce features */
  1612. on_each_cpu(mce_disable_ce, (void *)1, 1);
  1613. mce_ignore_ce = 1;
  1614. } else {
  1615. /* enable ce features */
  1616. mce_ignore_ce = 0;
  1617. on_each_cpu(mce_enable_ce, (void *)1, 1);
  1618. }
  1619. }
  1620. return size;
  1621. }
  1622. static ssize_t set_cmci_disabled(struct sys_device *s,
  1623. struct sysdev_attribute *attr,
  1624. const char *buf, size_t size)
  1625. {
  1626. u64 new;
  1627. if (strict_strtoull(buf, 0, &new) < 0)
  1628. return -EINVAL;
  1629. if (mce_cmci_disabled ^ !!new) {
  1630. if (new) {
  1631. /* disable cmci */
  1632. on_each_cpu(mce_disable_ce, NULL, 1);
  1633. mce_cmci_disabled = 1;
  1634. } else {
  1635. /* enable cmci */
  1636. mce_cmci_disabled = 0;
  1637. on_each_cpu(mce_enable_ce, NULL, 1);
  1638. }
  1639. }
  1640. return size;
  1641. }
  1642. static ssize_t store_int_with_restart(struct sys_device *s,
  1643. struct sysdev_attribute *attr,
  1644. const char *buf, size_t size)
  1645. {
  1646. ssize_t ret = sysdev_store_int(s, attr, buf, size);
  1647. mce_restart();
  1648. return ret;
  1649. }
  1650. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  1651. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  1652. static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
  1653. static SYSDEV_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
  1654. static struct sysdev_ext_attribute attr_check_interval = {
  1655. _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
  1656. store_int_with_restart),
  1657. &check_interval
  1658. };
  1659. static struct sysdev_ext_attribute attr_ignore_ce = {
  1660. _SYSDEV_ATTR(ignore_ce, 0644, sysdev_show_int, set_ignore_ce),
  1661. &mce_ignore_ce
  1662. };
  1663. static struct sysdev_ext_attribute attr_cmci_disabled = {
  1664. _SYSDEV_ATTR(cmci_disabled, 0644, sysdev_show_int, set_cmci_disabled),
  1665. &mce_cmci_disabled
  1666. };
  1667. static struct sysdev_attribute *mce_attrs[] = {
  1668. &attr_tolerant.attr,
  1669. &attr_check_interval.attr,
  1670. &attr_trigger,
  1671. &attr_monarch_timeout.attr,
  1672. &attr_dont_log_ce.attr,
  1673. &attr_ignore_ce.attr,
  1674. &attr_cmci_disabled.attr,
  1675. NULL
  1676. };
  1677. static cpumask_var_t mce_dev_initialized;
  1678. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  1679. static __cpuinit int mce_create_device(unsigned int cpu)
  1680. {
  1681. int err;
  1682. int i, j;
  1683. if (!mce_available(&boot_cpu_data))
  1684. return -EIO;
  1685. memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
  1686. per_cpu(mce_dev, cpu).id = cpu;
  1687. per_cpu(mce_dev, cpu).cls = &mce_sysclass;
  1688. err = sysdev_register(&per_cpu(mce_dev, cpu));
  1689. if (err)
  1690. return err;
  1691. for (i = 0; mce_attrs[i]; i++) {
  1692. err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1693. if (err)
  1694. goto error;
  1695. }
  1696. for (j = 0; j < banks; j++) {
  1697. err = sysdev_create_file(&per_cpu(mce_dev, cpu),
  1698. &mce_banks[j].attr);
  1699. if (err)
  1700. goto error2;
  1701. }
  1702. cpumask_set_cpu(cpu, mce_dev_initialized);
  1703. return 0;
  1704. error2:
  1705. while (--j >= 0)
  1706. sysdev_remove_file(&per_cpu(mce_dev, cpu), &mce_banks[j].attr);
  1707. error:
  1708. while (--i >= 0)
  1709. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1710. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1711. return err;
  1712. }
  1713. static __cpuinit void mce_remove_device(unsigned int cpu)
  1714. {
  1715. int i;
  1716. if (!cpumask_test_cpu(cpu, mce_dev_initialized))
  1717. return;
  1718. for (i = 0; mce_attrs[i]; i++)
  1719. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1720. for (i = 0; i < banks; i++)
  1721. sysdev_remove_file(&per_cpu(mce_dev, cpu), &mce_banks[i].attr);
  1722. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1723. cpumask_clear_cpu(cpu, mce_dev_initialized);
  1724. }
  1725. /* Make sure there are no machine checks on offlined CPUs. */
  1726. static void __cpuinit mce_disable_cpu(void *h)
  1727. {
  1728. unsigned long action = *(unsigned long *)h;
  1729. int i;
  1730. if (!mce_available(&current_cpu_data))
  1731. return;
  1732. if (!(action & CPU_TASKS_FROZEN))
  1733. cmci_clear();
  1734. for (i = 0; i < banks; i++) {
  1735. struct mce_bank *b = &mce_banks[i];
  1736. if (b->init)
  1737. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1738. }
  1739. }
  1740. static void __cpuinit mce_reenable_cpu(void *h)
  1741. {
  1742. unsigned long action = *(unsigned long *)h;
  1743. int i;
  1744. if (!mce_available(&current_cpu_data))
  1745. return;
  1746. if (!(action & CPU_TASKS_FROZEN))
  1747. cmci_reenable();
  1748. for (i = 0; i < banks; i++) {
  1749. struct mce_bank *b = &mce_banks[i];
  1750. if (b->init)
  1751. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1752. }
  1753. }
  1754. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1755. static int __cpuinit
  1756. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1757. {
  1758. unsigned int cpu = (unsigned long)hcpu;
  1759. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1760. switch (action) {
  1761. case CPU_ONLINE:
  1762. case CPU_ONLINE_FROZEN:
  1763. mce_create_device(cpu);
  1764. if (threshold_cpu_callback)
  1765. threshold_cpu_callback(action, cpu);
  1766. break;
  1767. case CPU_DEAD:
  1768. case CPU_DEAD_FROZEN:
  1769. if (threshold_cpu_callback)
  1770. threshold_cpu_callback(action, cpu);
  1771. mce_remove_device(cpu);
  1772. break;
  1773. case CPU_DOWN_PREPARE:
  1774. case CPU_DOWN_PREPARE_FROZEN:
  1775. del_timer_sync(t);
  1776. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1777. break;
  1778. case CPU_DOWN_FAILED:
  1779. case CPU_DOWN_FAILED_FROZEN:
  1780. if (!mce_ignore_ce && check_interval) {
  1781. t->expires = round_jiffies(jiffies +
  1782. __get_cpu_var(mce_next_interval));
  1783. add_timer_on(t, cpu);
  1784. }
  1785. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1786. break;
  1787. case CPU_POST_DEAD:
  1788. /* intentionally ignoring frozen here */
  1789. cmci_rediscover(cpu);
  1790. break;
  1791. }
  1792. return NOTIFY_OK;
  1793. }
  1794. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1795. .notifier_call = mce_cpu_callback,
  1796. };
  1797. static __init void mce_init_banks(void)
  1798. {
  1799. int i;
  1800. for (i = 0; i < banks; i++) {
  1801. struct mce_bank *b = &mce_banks[i];
  1802. struct sysdev_attribute *a = &b->attr;
  1803. sysfs_attr_init(&a->attr);
  1804. a->attr.name = b->attrname;
  1805. snprintf(b->attrname, ATTR_LEN, "bank%d", i);
  1806. a->attr.mode = 0644;
  1807. a->show = show_bank;
  1808. a->store = set_bank;
  1809. }
  1810. }
  1811. static __init int mcheck_init_device(void)
  1812. {
  1813. int err;
  1814. int i = 0;
  1815. if (!mce_available(&boot_cpu_data))
  1816. return -EIO;
  1817. zalloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
  1818. mce_init_banks();
  1819. err = sysdev_class_register(&mce_sysclass);
  1820. if (err)
  1821. return err;
  1822. for_each_online_cpu(i) {
  1823. err = mce_create_device(i);
  1824. if (err)
  1825. return err;
  1826. }
  1827. register_hotcpu_notifier(&mce_cpu_notifier);
  1828. misc_register(&mce_log_device);
  1829. return err;
  1830. }
  1831. device_initcall(mcheck_init_device);
  1832. /*
  1833. * Old style boot options parsing. Only for compatibility.
  1834. */
  1835. static int __init mcheck_disable(char *str)
  1836. {
  1837. mce_disabled = 1;
  1838. return 1;
  1839. }
  1840. __setup("nomce", mcheck_disable);
  1841. #ifdef CONFIG_DEBUG_FS
  1842. struct dentry *mce_get_debugfs_dir(void)
  1843. {
  1844. static struct dentry *dmce;
  1845. if (!dmce)
  1846. dmce = debugfs_create_dir("mce", NULL);
  1847. return dmce;
  1848. }
  1849. static void mce_reset(void)
  1850. {
  1851. cpu_missing = 0;
  1852. atomic_set(&mce_fake_paniced, 0);
  1853. atomic_set(&mce_executing, 0);
  1854. atomic_set(&mce_callin, 0);
  1855. atomic_set(&global_nwo, 0);
  1856. }
  1857. static int fake_panic_get(void *data, u64 *val)
  1858. {
  1859. *val = fake_panic;
  1860. return 0;
  1861. }
  1862. static int fake_panic_set(void *data, u64 val)
  1863. {
  1864. mce_reset();
  1865. fake_panic = val;
  1866. return 0;
  1867. }
  1868. DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
  1869. fake_panic_set, "%llu\n");
  1870. static int __init mcheck_debugfs_init(void)
  1871. {
  1872. struct dentry *dmce, *ffake_panic;
  1873. dmce = mce_get_debugfs_dir();
  1874. if (!dmce)
  1875. return -ENOMEM;
  1876. ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
  1877. &fake_panic_fops);
  1878. if (!ffake_panic)
  1879. return -ENOMEM;
  1880. return 0;
  1881. }
  1882. late_initcall(mcheck_debugfs_init);
  1883. #endif